From 4d3c5ef7c18ae519598a91e4b514e5de615960bd Mon Sep 17 00:00:00 2001 From: YunaiV Date: Wed, 15 May 2024 23:06:27 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91AI=EF=BC=9A?= =?UTF-8?q?=E6=B5=81=E5=BC=8F=E5=8F=91=E9=80=81=E6=B6=88=E6=81=AF=E7=9A=84?= =?UTF-8?q?=E5=BE=AE=E8=B0=83=EF=BC=8C=E7=BB=9F=E4=B8=80=E6=88=90=E5=8D=95?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/ai/chat/message/index.ts | 22 +++++++++---------- src/views/ai/chat/index.vue | 36 ++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/api/ai/chat/message/index.ts b/src/api/ai/chat/message/index.ts index 0138651b..b4e80816 100644 --- a/src/api/ai/chat/message/index.ts +++ b/src/api/ai/chat/message/index.ts @@ -31,19 +31,16 @@ export const ChatMessageApi = { }) }, - // 发送 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 消息 // TODO axios 可以么? https://apifox.com/apiskills/how-to-create-axios-stream/ - sendStream: async (id: string, ctrl, onMessage, onError, onClose) => { + sendStream: async ( + conversationId: number, + content: string, + ctrl, + onMessage, + onError, + onClose + ) => { const token = getAccessToken() return fetchEventSource(`${config.base_url}/ai/chat/message/send-stream`, { method: 'post', @@ -53,7 +50,8 @@ export const ChatMessageApi = { }, openWhenHidden: true, body: JSON.stringify({ - id: id + conversationId, + content }), onmessage: onMessage, onerror: onError, diff --git a/src/views/ai/chat/index.vue b/src/views/ai/chat/index.vue index 9284f83b..a2e73ebd 100644 --- a/src/views/ai/chat/index.vue +++ b/src/views/ai/chat/index.vue @@ -90,6 +90,7 @@
+
{ return } const content = prompt.value?.trim() - if (content?.length < 2) { + if (content.length < 2) { ElMessage({ message: '请输入内容!', type: 'error' }) return } + // TODO 芋艿:这块交互要在优化;应该是先插入到 UI 界面,里面会有当前的消息,和正在思考中;之后发起请求; // 清空输入框 prompt.value = '' - const requestParams = { + // const requestParams = { + // conversationId: conversationId.value, + // content: content + // } as unknown as ChatMessageSendVO + // // 添加 message + const userMessage = { conversationId: conversationId.value, content: content - } as unknown as ChatMessageSendVO - // 添加 message - const userMessage = (await ChatMessageApi.add(requestParams)) as unknown as ChatMessageVO - list.value.push(userMessage) - // 滚动到住下面 - scrollToBottom() + } + // list.value.push(userMessage) + // // 滚动到住下面 + // scrollToBottom() // stream await doSendStream(userMessage) // @@ -330,13 +335,15 @@ const doSendStream = async (userMessage: ChatMessageVO) => { let isFirstMessage = true let content = '' ChatMessageApi.sendStream( - userMessage.id, + userMessage.conversationId, // TODO 芋艿:这里可能要在优化; + userMessage.content, conversationInAbortController.value, (message) => { console.log('message', message) - const data = JSON.parse(message.data) as unknown as ChatMessageVO + const data = JSON.parse(message.data) // TODO 芋艿:类型处理; + // debugger // 如果没有内容结束链接 - if (data.content === '') { + if (data.receive.content === '') { // 标记对话结束 conversationInProgress.value = false // 结束 stream 对话 @@ -345,9 +352,12 @@ const doSendStream = async (userMessage: ChatMessageVO) => { // 首次返回需要添加一个 message 到页面,后面的都是更新 if (isFirstMessage) { isFirstMessage = false - list.value.push(data) + // debugger + list.value.push(data.send) + list.value.push(data.receive) } else { - content = content + data.content + // debugger + content = content + data.receive.content const lastMessage = list.value[list.value.length - 1] lastMessage.content = marked(content) as unknown as string list.value[list.value - 1] = lastMessage