import { ChatMessageResult, EmployeeSearchResult } from "@/utils/clientsApis"; /** * 聊天记录 * * @interface ChatMessage * @property {string} id - 消息ID * @property {string} role - 角色 user-用户 assistant-助手 * @property {string} content - 内容 * @property {model} role - 模型 * @property {number} createAt - 创建时间 * @property {number} updateAt - 更新时间 */ export interface ChatMessage { id: string; role: string;//user assistant content: string; parentId: string; model: string; createAt: number; updateAt: number; } export function MessageChange(list: ChatMessageResult[]) { const chatMessageList: ChatMessage[] = []; list.forEach((item) => { if (item.query) { chatMessageList.push({ id: item.id, role: 'user', parentId: '', model: '', content: item.query, createAt: item.createdAt, updateAt: item.createdAt }) } if (item.answer) { chatMessageList.push({ id: item.id + '_assistant', role: 'assistant', parentId: item.id, model: '', content: item.answer, createAt: item.createdAt, updateAt: item.createdAt }) } }) return chatMessageList } const STORAGE_KEY_CHAT_MODEL_LIST = 'chat-model-list'; /** * 本地存储 - 助手列表 * * @interface ChatAiModel * @property {number} id - ID * @property {string} name - 名称 * @property {string} avatar - 头像 * @property {string} estimate - 描述 * @property {string} ConversationId - 会话ID * @property {number} createdAt - 创建时间(时间戳) */ export interface ChatAiModel { id: number; name: string; avatar: string; estimate: string; createdAt: number; ConversationId: string; } export function SetChatModelListForLocalStorage(list: ChatAiModel[]) { localStorage.setItem(STORAGE_KEY_CHAT_MODEL_LIST, JSON.stringify(list)); } export const GetChatModelListForLocalStorage=(): ChatAiModel[] => { let list = JSON.parse(localStorage.getItem(STORAGE_KEY_CHAT_MODEL_LIST) || '[]')as ChatAiModel[]; return list } export const LoadLocalChatList = (model: EmployeeSearchResult): ChatAiModel[] => { var list = GetChatModelListForLocalStorage(); if (list === null) { list = [ { id: model.id, name: model.title, avatar: model.avatar, estimate: model.estimate, createdAt: model.createdAt, ConversationId: "", }, ]; } else { var index = list.findIndex((v) => v.id == model.id); if (index < 0) { list = [ ...[ { id: model.id, name: model.title, avatar: model.avatar, estimate: model.estimate, createdAt: model.createdAt, ConversationId: "", }, ], ...list, ]; } } SetChatModelListForLocalStorage(list); return list; } export const UpdateLocalChatListForConversationId = (id:number,conversationId:string): ChatAiModel[] => { var list = GetChatModelListForLocalStorage(); if (list !== null) { var index = list.findIndex((v) => v.id == id); if (index >= 0 && (list[index].ConversationId == '' || list[index].ConversationId.includes("guid_0000_"))) { list[index].ConversationId = conversationId; } } SetChatModelListForLocalStorage(list); return list; } export const GetLocalChatModelDetails = (id:number): ChatAiModel => { var list = GetChatModelListForLocalStorage(); if (list !== null) { var index = list.findIndex((v) => v.id == id); if (index >= 0 ) { return list[index]; } } return {id:0,name:"",avatar:"",estimate:"",createdAt:0,ConversationId:"" }; } export const DeleteLocalChatModelDetails = (id:number): ChatAiModel[] => { var list = GetChatModelListForLocalStorage(); if (list !== null) { var index = list.findIndex((v) => v.id == id); if (index >= 0 ) { list.splice(index, 1); } } SetChatModelListForLocalStorage(list); if(id == Storage_getAgentId()){ if(list!=null && list.length > 0){ Storage_setListType(1); Storage_setAgentId(list[0].id); Storage_setConversationId(list[0].ConversationId); } else{ Storage_setListType(1); Storage_setAgentId(0); Storage_setConversationId(''); } } return list; } //存储智能体 const STORAGE_KEY_CHAT_USED_LIST_TYPE = 'chat-used-list-type'; const STORAGE_KEY_CHAT_USED_AGENT_ID = 'chat-used-agent-id'; const STORAGE_KEY_CHAT_USED_CONVERSATION_ID = 'chat-used-conversation-id'; export const Storage_setListType = (value: number) => { localStorage.setItem(STORAGE_KEY_CHAT_USED_LIST_TYPE, JSON.stringify(value)); } export const Storage_getListType = () : number => { let value = 0; if (typeof localStorage === 'undefined') return value; const data = localStorage.getItem(STORAGE_KEY_CHAT_USED_LIST_TYPE); if (data) { value = parseInt(data ?? 0); } return value; } export const Storage_setAgentId = (value: number) => { localStorage.setItem(STORAGE_KEY_CHAT_USED_AGENT_ID, value.toString()); } export const Storage_getAgentId = () : number => { let value = 0; if (typeof localStorage === 'undefined') return value; const data = localStorage.getItem(STORAGE_KEY_CHAT_USED_AGENT_ID); if (data) { value = parseInt(data ?? 0); } return value; } export const Storage_setConversationId = (value: string) => { localStorage.setItem(STORAGE_KEY_CHAT_USED_CONVERSATION_ID, value); } export const Storage_getConversationId = () : string => { let value = ''; if (typeof localStorage === 'undefined') return value; const data = localStorage.getItem(STORAGE_KEY_CHAT_USED_CONVERSATION_ID); if (data) { value = data; } return value; }