From 151076a79ec6c4ce5c588852ba1c503b49a5dc1f Mon Sep 17 00:00:00 2001 From: cherishsince Date: Sun, 12 May 2024 19:02:45 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=A2=9E=E5=8A=A0=E3=80=91=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20chat=20stream=20=E5=8A=9F=E8=83=BD(=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E3=80=81=E5=A4=8D=E5=88=B6=E3=80=81=E6=BB=9A=E5=8A=A8?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E3=80=81=E5=8F=91=E9=80=81=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=88=B0=E6=9C=80=E4=B8=8B=E9=9D=A2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/api/ai/chat/message/index.ts | 37 +- src/views/ai/chat/components/MessageList.vue | 124 +------ src/views/ai/chat/index.vue | 348 +++++++++++++++++-- 4 files changed, 359 insertions(+), 151 deletions(-) diff --git a/package.json b/package.json index 645d06b3..21a57724 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "@form-create/designer": "^3.1.3", "@form-create/element-ui": "^3.1.24", "@iconify/iconify": "^3.1.1", + "@microsoft/fetch-event-source": "^2.0.1", "@videojs-player/vue": "^1.0.0", "@vueuse/core": "^10.9.0", "@wangeditor/editor": "^5.1.23", diff --git a/src/api/ai/chat/message/index.ts b/src/api/ai/chat/message/index.ts index 3dc1ea0f..acd9b3b6 100644 --- a/src/api/ai/chat/message/index.ts +++ b/src/api/ai/chat/message/index.ts @@ -1,15 +1,18 @@ import request from '@/config/axios' +import {fetchEventSource} from '@microsoft/fetch-event-source'; +import {getAccessToken} from '@/utils/auth' +import {config} from '@/config/axios/config' // 聊天VO export interface ChatMessageVO { - id: number // 编号 + id: string // 编号 conversationId: string // 会话编号 type: string // 消息类型 userId: string // 用户编号 roleId: string // 角色编号 model: number // 模型标志 modelId: number // 模型编号 - content: number // 聊天内容 + content: string // 聊天内容 tokens: number // 消耗 Token 数量 createTime: Date // 创建时间 } @@ -27,11 +30,35 @@ export const ChatMessageApi = { return await request.get({ url: `/ai/chat/message/list-by-conversation-id?conversationId=${conversationId}`}) }, - // 发送 send 消息 - send: async (data: ChatMessageSendVO) => { - return await request.post({ url: `/ai/chat/message/send`, data }) + // 发送 add 消息 + add: async (data: ChatMessageSendVO) => { + return await request.post({ url: `/ai/chat/message/add`, data}) }, + // 发送 send 消息 + send: async (data: ChatMessageSendVO) => { + return await request.post({ url: `/ai/chat/message/send`, data}) + }, + + // 发送 send stream 消息 + sendStream: async (id: string, ctrl, onMessage, onError, onClose) => { + const token = getAccessToken() + return fetchEventSource(`${ config.base_url}/ai/chat/message/send-stream`, { + method: 'post', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` + }, + openWhenHidden: true, + body: JSON.stringify({ + id: id, + }), + onmessage: onMessage, + onerror:onError, + onclose: onClose, + signal: ctrl.signal, + }); + }, // 发送 send 消息 delete: async (id: string) => { diff --git a/src/views/ai/chat/components/MessageList.vue b/src/views/ai/chat/components/MessageList.vue index 3038f886..0f77f8ec 100644 --- a/src/views/ai/chat/components/MessageList.vue +++ b/src/views/ai/chat/components/MessageList.vue @@ -62,12 +62,11 @@ -