From 7375c7560fa8fc39433e80f18fe2eea91d811fab Mon Sep 17 00:00:00 2001 From: chen wei bo <1025839511@qq.com> Date: Sun, 21 Sep 2025 14:08:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=B7=E6=B1=82AI=E8=A7=92=E8=89=B2=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Scripts/chat18x/data/AiCharacterData.ts | 78 ++++++++++++++++--- 1 file changed, 68 insertions(+), 10 deletions(-) diff --git a/assets/Scripts/chat18x/data/AiCharacterData.ts b/assets/Scripts/chat18x/data/AiCharacterData.ts index 4b76dcc4..3d4f8b4b 100644 --- a/assets/Scripts/chat18x/data/AiCharacterData.ts +++ b/assets/Scripts/chat18x/data/AiCharacterData.ts @@ -4,34 +4,92 @@ import { BaseData } from "./BaseData"; import proto from "db://assets/Scripts/proto/proto.pb.js"; -export class AiCharacterData extends BaseData { - // api键 - private _apiKey: string = ""; +// 数据结构 +export interface AiCharacters { + id: number; + basePrompt: string; + additionPrompt: string; +} +export class AiCharacterData extends BaseData { + // 数据 map + private _dataMap: Map; + // 数据 id + private _ids: number[]; + + constructor() { + super(); + this._dataMap = new Map(), + this._ids = []; + } public reset(): void { - this._apiKey = ""; + this._dataMap.clear(); + this._ids = []; } public clear(): void { - this._apiKey = ""; + this._dataMap.clear(); + this._ids = []; } public destroy(): void { super.destroy(); - this._apiKey = null; + this._dataMap = null; + this._ids = null; } + // 解析数据,一个 + private parseOneAiCharacters(data: any): AiCharacters | null { + if (!data) return null; + const oneData: AiCharacters = { + id: data.id, // 角色id + basePrompt: data.basePrompt, // 个人信息基础prompt(聊天&评分都需要) + additionPrompt: data.additionPrompt, // 额外信息prompt(聊天需要) + }; + return oneData; + } + + // 解析数据,多个 + private parseAllAiCharacters(data: any): AiCharacters[] { + if (!data) return []; + let allData = []; + for (const value of data) { + const oneData: AiCharacters = this.parseOneAiCharacters(value); + if (oneData) { + allData.push(oneData); + } + } + return allData; + } + /** 设置Ai角色配置*/ public set aiCharacter(data: proto.cs.ICSGetResConfigRes) { const parsed = JSON.parse(data.data); console.log("请求Ai角色配置的解析数据:", parsed); - + } - /** 获取api键 */ - public getApiKey(): string { - return this._apiKey; + /** 获取所有 id */ + public getAllIds(): number[] { + return this._ids; } + + /** 获取数据,根据 id */ + private getAiCharactersById(id: number): AiCharacters | null { + return this._dataMap.get(id) ?? null; + } + + /** 获取个人信息基础prompt,根据 id*/ + public getBasePromptById(id: number): string { + const oneData = this.getAiCharactersById(id); + return oneData ? oneData.basePrompt : ""; + } + + /** 获取额外信息prompt,根据 id*/ + public getAdditionPromptById(id: number): string { + const oneData = this.getAiCharactersById(id); + return oneData ? oneData.additionPrompt : ""; + } }