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. isGoogleSearch: false,
  30. },
  31. getters: {},
  32. actions: {
  33. /**
  34. * 新增消息
  35. */
  36. async addMessage(
  37. message: string,
  38. role: 'user' | 'assistant' | 'system',
  39. chatId: string
  40. ): Promise<boolean> {
  41. this.messages.push({
  42. chatId,
  43. role: role,
  44. message: message,
  45. createTime: formatToDateTime(new Date()),
  46. });
  47. return true;
  48. },
  49. /**
  50. * 更新消息
  51. * chatId 仅仅用于更新流式消息内容
  52. */
  53. async updateMessage(chatId: string, message: string, isError?: boolean) {
  54. const index = this.messages.findIndex((item) => item?.chatId == chatId);
  55. if (index !== -1) {
  56. this.messages[index].message = message;
  57. this.messages[index].isError = isError;
  58. }
  59. },
  60. /**
  61. * 删除消息
  62. */
  63. async delMessage(item: any) {
  64. this.messages = this.messages.filter((i) => i.promptId !== item.promptId);
  65. },
  66. },
  67. });