chat.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { ChatMessageResult, EmployeeSearchResult } from "@/utils/clientsApis";
  2. /**
  3. * 聊天记录
  4. *
  5. * @interface ChatMessage
  6. * @property {string} id - 消息ID
  7. * @property {string} role - 角色 user-用户 assistant-助手
  8. * @property {string} content - 内容
  9. * @property {model} role - 模型
  10. * @property {number} createAt - 创建时间
  11. * @property {number} updateAt - 更新时间
  12. */
  13. export interface ChatMessage {
  14. id: string;
  15. role: string;//user assistant
  16. content: string;
  17. parentId: string;
  18. model: string;
  19. createAt: number;
  20. updateAt: number;
  21. }
  22. export function MessageChange(list: ChatMessageResult[]) {
  23. const chatMessageList: ChatMessage[] = [];
  24. list.forEach((item) => {
  25. if (item.query) {
  26. chatMessageList.push({
  27. id: item.id,
  28. role: 'user',
  29. parentId: '',
  30. model: '',
  31. content: item.query,
  32. createAt: item.createdAt,
  33. updateAt: item.createdAt
  34. })
  35. }
  36. if (item.answer) {
  37. chatMessageList.push({
  38. id: item.id + '_assistant',
  39. role: 'assistant',
  40. parentId: item.id,
  41. model: '',
  42. content: item.answer,
  43. createAt: item.createdAt,
  44. updateAt: item.createdAt
  45. })
  46. }
  47. })
  48. return chatMessageList
  49. }
  50. const STORAGE_KEY_CHAT_MODEL_LIST = 'chat-model-list';
  51. /**
  52. * 本地存储 - 助手列表
  53. *
  54. * @interface ChatAiModel
  55. * @property {number} id - ID
  56. * @property {string} name - 名称
  57. * @property {string} avatar - 头像
  58. * @property {string} estimate - 描述
  59. * @property {string} ConversationId - 会话ID
  60. * @property {number} createdAt - 创建时间(时间戳)
  61. */
  62. export interface ChatAiModel {
  63. id: number;
  64. name: string;
  65. avatar: string;
  66. estimate: string;
  67. createdAt: number;
  68. ConversationId: string;
  69. }
  70. export function SetChatModelListForLocalStorage(list: ChatAiModel[]) {
  71. localStorage.setItem(STORAGE_KEY_CHAT_MODEL_LIST, JSON.stringify(list));
  72. }
  73. export const GetChatModelListForLocalStorage=(): ChatAiModel[] => {
  74. let list = JSON.parse(localStorage.getItem(STORAGE_KEY_CHAT_MODEL_LIST) || '[]')as ChatAiModel[];
  75. return list
  76. }
  77. export const LoadLocalChatList = (model: EmployeeSearchResult): ChatAiModel[] => {
  78. var list = GetChatModelListForLocalStorage();
  79. if (list === null) {
  80. list = [
  81. {
  82. id: model.id,
  83. name: model.title,
  84. avatar: model.avatar,
  85. estimate: model.estimate,
  86. createdAt: model.createdAt,
  87. ConversationId: "",
  88. },
  89. ];
  90. } else {
  91. var index = list.findIndex((v) => v.id == model.id);
  92. if (index < 0) {
  93. list = [
  94. ...[
  95. {
  96. id: model.id,
  97. name: model.title,
  98. avatar: model.avatar,
  99. estimate: model.estimate,
  100. createdAt: model.createdAt,
  101. ConversationId: "",
  102. },
  103. ],
  104. ...list,
  105. ];
  106. }
  107. }
  108. SetChatModelListForLocalStorage(list);
  109. return list;
  110. }
  111. export const UpdateLocalChatListForConversationId = (id:number,conversationId:string): ChatAiModel[] => {
  112. var list = GetChatModelListForLocalStorage();
  113. if (list !== null) {
  114. var index = list.findIndex((v) => v.id == id);
  115. if (index >= 0 && (list[index].ConversationId == '' || list[index].ConversationId.includes("guid_0000_"))) {
  116. list[index].ConversationId = conversationId;
  117. }
  118. }
  119. SetChatModelListForLocalStorage(list);
  120. return list;
  121. }
  122. export const GetLocalChatModelDetails = (id:number): ChatAiModel => {
  123. var list = GetChatModelListForLocalStorage();
  124. if (list !== null) {
  125. var index = list.findIndex((v) => v.id == id);
  126. if (index >= 0 ) {
  127. return list[index];
  128. }
  129. }
  130. return {id:0,name:"",avatar:"",estimate:"",createdAt:0,ConversationId:"" };
  131. }
  132. export const DeleteLocalChatModelDetails = (id:number): ChatAiModel[] => {
  133. var list = GetChatModelListForLocalStorage();
  134. if (list !== null) {
  135. var index = list.findIndex((v) => v.id == id);
  136. if (index >= 0 ) {
  137. list.splice(index, 1);
  138. }
  139. }
  140. SetChatModelListForLocalStorage(list);
  141. if(id == Storage_getAgentId()){
  142. if(list!=null && list.length > 0){
  143. Storage_setListType(1);
  144. Storage_setAgentId(list[0].id);
  145. Storage_setConversationId(list[0].ConversationId);
  146. }
  147. else{
  148. Storage_setListType(1);
  149. Storage_setAgentId(0);
  150. Storage_setConversationId('');
  151. }
  152. }
  153. return list;
  154. }
  155. //存储智能体
  156. const STORAGE_KEY_CHAT_USED_LIST_TYPE = 'chat-used-list-type';
  157. const STORAGE_KEY_CHAT_USED_AGENT_ID = 'chat-used-agent-id';
  158. const STORAGE_KEY_CHAT_USED_CONVERSATION_ID = 'chat-used-conversation-id';
  159. export const Storage_setListType = (value: number) => {
  160. localStorage.setItem(STORAGE_KEY_CHAT_USED_LIST_TYPE, JSON.stringify(value));
  161. }
  162. export const Storage_getListType = () : number => {
  163. let value = 0;
  164. if (typeof localStorage === 'undefined') return value;
  165. const data = localStorage.getItem(STORAGE_KEY_CHAT_USED_LIST_TYPE);
  166. if (data) {
  167. value = parseInt(data ?? 0);
  168. }
  169. return value;
  170. }
  171. export const Storage_setAgentId = (value: number) => {
  172. localStorage.setItem(STORAGE_KEY_CHAT_USED_AGENT_ID, value.toString());
  173. }
  174. export const Storage_getAgentId = () : number => {
  175. let value = 0;
  176. if (typeof localStorage === 'undefined') return value;
  177. const data = localStorage.getItem(STORAGE_KEY_CHAT_USED_AGENT_ID);
  178. if (data) {
  179. value = parseInt(data ?? 0);
  180. }
  181. return value;
  182. }
  183. export const Storage_setConversationId = (value: string) => {
  184. localStorage.setItem(STORAGE_KEY_CHAT_USED_CONVERSATION_ID, value);
  185. }
  186. export const Storage_getConversationId = () : string => {
  187. let value = '';
  188. if (typeof localStorage === 'undefined') return value;
  189. const data = localStorage.getItem(STORAGE_KEY_CHAT_USED_CONVERSATION_ID);
  190. if (data) {
  191. value = data;
  192. }
  193. return value;
  194. }