useChatStore.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2024 LangChat. TyCoding All Rights Reserved.
  3. *
  4. * Licensed under the GNU Affero General Public License, Version 3 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.gnu.org/licenses/agpl-3.0.html
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { defineStore } from 'pinia';
  17. import { formatToDateTime } from '@/utils/dateUtil';
  18. import { ChatState } from '@/views/chat/store/chat';
  19. export const useChatStore = defineStore('chat-store', {
  20. state: (): ChatState =>
  21. <ChatState>{
  22. modelId: null,
  23. modelName: '',
  24. modelProvider: '',
  25. conversationId: null,
  26. messages: [],
  27. metadata: null,
  28. appId: null,
  29. },
  30. getters: {},
  31. actions: {
  32. /**
  33. * 新增消息
  34. */
  35. async addMessage(
  36. message: string,
  37. role: 'user' | 'assistant' | 'system',
  38. chatId: string
  39. ): Promise<boolean> {
  40. this.messages.push({
  41. chatId,
  42. role: role,
  43. message: message,
  44. createTime: formatToDateTime(new Date()),
  45. });
  46. return true;
  47. },
  48. /**
  49. * 更新消息
  50. * chatId 仅仅用于更新流式消息内容
  51. */
  52. async updateMessage(chatId: string, message: string, isError?: boolean) {
  53. const index = this.messages.findIndex((item) => item?.chatId == chatId);
  54. if (index !== -1) {
  55. this.messages[index].message = message;
  56. this.messages[index].isError = isError;
  57. }
  58. },
  59. /**
  60. * 删除消息
  61. */
  62. async delMessage(item: any) {
  63. this.messages = this.messages.filter((i) => i.promptId !== item.promptId);
  64. this.metadata = null;
  65. },
  66. },
  67. });