123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- 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;
- }
|