useChatStore.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { defineStore } from 'pinia';
  2. import { ChatState } from './chat';
  3. import { formatToDateTime } from '@/utils/dateUtil';
  4. import { getMessages } from '@/api/aigc/conversation';
  5. export const useChatStore = defineStore('chat-store', {
  6. state: (): ChatState =>
  7. <ChatState>{
  8. active: '',
  9. isEdit: '',
  10. siderCollapsed: true,
  11. sideIsLoading: true,
  12. chatIsLoading: true,
  13. messages: [],
  14. },
  15. getters: {},
  16. actions: {
  17. setSiderCollapsed(collapsed: boolean) {
  18. this.siderCollapsed = collapsed;
  19. },
  20. async setActive(id: string) {
  21. this.active = id;
  22. },
  23. async setEdit(id: string) {
  24. this.isEdit = id;
  25. },
  26. /**
  27. * 加载会话窗口和聊天信息
  28. */
  29. async loadData() {
  30. try {
  31. } finally {
  32. this.sideIsLoading = false;
  33. this.chatIsLoading = false;
  34. }
  35. },
  36. /**
  37. * 选择会话窗口
  38. */
  39. async selectConversation(params: any) {
  40. if (params.id == undefined) {
  41. return;
  42. }
  43. console.log('set', params.id);
  44. await this.setActive(params.id);
  45. await this.setEdit('');
  46. this.messages = await getMessages(params.id);
  47. console.log(this.messages);
  48. },
  49. /**
  50. * 新增消息
  51. */
  52. async addMessage(
  53. message: string,
  54. role: 'user' | 'assistant' | 'system',
  55. promptId: string,
  56. parentRefId: string
  57. ): Promise<boolean> {
  58. this.messages.push({
  59. promptId: promptId,
  60. parentRefId: parentRefId,
  61. role: role,
  62. content: message,
  63. createTime: formatToDateTime(new Date()),
  64. });
  65. return true;
  66. },
  67. /**
  68. * 更新消息
  69. * promptId 仅仅用于更新流式消息内容
  70. */
  71. async updateMessage(promptId: string, content: string, isError?: boolean) {
  72. const promptIndex = this.messages.findIndex((item) => item?.promptId == promptId);
  73. if (promptIndex !== -1) {
  74. this.messages[promptIndex].content = content;
  75. this.messages[promptIndex].isError = isError;
  76. }
  77. console.log(this.messages);
  78. },
  79. /**
  80. * 删除消息
  81. */
  82. async delMessage(item: any) {
  83. this.messages = this.messages.filter((i) => i.promptId !== item.promptId);
  84. },
  85. },
  86. });