From 8edd089047787f86591225fcc1add87b1e24168f Mon Sep 17 00:00:00 2001 From: Li Dong Date: Wed, 22 Mar 2023 22:26:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.go | 15 ++- frontend/src/views/chat/index.vue | 181 +++++++++--------------------- frontend/wailsjs/go/main/App.d.ts | 2 + frontend/wailsjs/go/main/App.js | 4 + 4 files changed, 73 insertions(+), 129 deletions(-) diff --git a/app.go b/app.go index 51adda6..1398135 100644 --- a/app.go +++ b/app.go @@ -12,7 +12,8 @@ import ( // App struct type App struct { - ctx context.Context + ctx context.Context + chatStop chan bool } // NewApp creates a new App application struct @@ -62,6 +63,7 @@ type ChatProcessReq struct { // ChatProcess func (a *App) ChatProcess(req *ChatProcessReq) (err error) { + // a.chatStop <- false ctx := a.ctx g.DumpWithType(req) cli := chatgpt.NewClient( @@ -101,7 +103,11 @@ func (a *App) ChatProcess(req *ChatProcessReq) (err error) { "conversationId": text.ConversationID, "text": text.Content, } - runtime.EventsEmit(a.ctx, "chat", responseData) + chatChannel := "chat" + if req.Options.ParentMessageId != "" { + chatChannel = req.Options.ParentMessageId + } + runtime.EventsEmit(a.ctx, chatChannel, responseData) // responseJson := gjson.New(responseData) // // runtime.EventsEmit(ctx, "chat", answer) // // 将 JSON 数据写入响应 @@ -119,3 +125,8 @@ func (a *App) ChatProcess(req *ChatProcessReq) (err error) { return } + +// StopChat +func (a *App) StopChat() { + g.Log().Info(a.ctx, "StopChat~~~~~~~~~~~~~~~~~~~~") +} diff --git a/frontend/src/views/chat/index.vue b/frontend/src/views/chat/index.vue index 7a61355..3bab8f8 100644 --- a/frontend/src/views/chat/index.vue +++ b/frontend/src/views/chat/index.vue @@ -5,8 +5,8 @@ import { useRoute } from 'vue-router' import { storeToRefs } from 'pinia' import { NAutoComplete, NButton, NInput, useDialog, useMessage } from 'naive-ui' import html2canvas from 'html2canvas' -import { EventsOn } from '../../../wailsjs/runtime/runtime' -import { ChatProcess } from '../../../wailsjs/go/main/App' +import { EventsOff, EventsOn } from '../../../wailsjs/runtime/runtime' +import { ChatProcess, StopChat } from '../../../wailsjs/go/main/App' import { Message } from './components' import { useScroll } from './hooks/useScroll' import { useChat } from './hooks/useChat' @@ -19,9 +19,11 @@ import { useChatStore, usePromptStore } from '@/store' // import { fetchChatAPIProcess } from '@/api' import { t } from '@/locales' +let currentChannel = '' + let controller = new AbortController() -const openLongReply = import.meta.env.VITE_GLOB_OPEN_LONG_REPLY === 'true' +// const openLongReply = import.meta.env.VITE_GLOB_OPEN_LONG_REPLY === 'true' const route = useRoute() const dialog = useDialog() @@ -32,7 +34,7 @@ const chatStore = useChatStore() useCopyCode() const { isMobile } = useBasicLayout() -const { addChat, updateChat, updateChatSome, getChatByUuidAndIndex } = useChat() +const { addChat, updateChat, updateChatSome } = useChat() const { scrollRef, scrollToBottom } = useScroll() const { usingContext, toggleUsingContext } = useUsingContext() @@ -106,96 +108,49 @@ async function onConversation() { }, ) scrollToBottom() - try { - let lastText = '' - EventsOn('chat', (data: any) => { - console.log(data) - updateChat( - +uuid, - dataSources.value.length - 1, - { - dateTime: new Date().toLocaleString(), - text: lastText + data.text ?? '', - inversion: false, - error: false, - loading: false, - conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id }, - requestOptions: { prompt: message, options: { ...options } }, - }, - ) - - if (openLongReply && data.detail.choices[0].finish_reason === 'length') { - options.parentMessageId = data.id - lastText = data.text - message = '' - return onConversation() - } - - scrollToBottom() - }) - ChatProcess({ - prompt: message, - options, - signal: controller.signal, - }) - } - catch (error: any) { - const errorMessage = error?.message ?? t('common.wrong') - - if (error.message === 'canceled') { - updateChatSome( - +uuid, - dataSources.value.length - 1, - { - loading: false, - }, - ) - scrollToBottom() - return - } - - const currentChat = getChatByUuidAndIndex(+uuid, dataSources.value.length - 1) - - if (currentChat?.text && currentChat.text !== '') { - updateChatSome( - +uuid, - dataSources.value.length - 1, - { - text: `${currentChat.text}\n[${errorMessage}]`, - error: false, - loading: false, - }, - ) - return - } + let lastText = '' + console.log("parentMessageId",options.parentMessageId) + // 如果options.parentMessageId不存在 则 chatchannel 为 options.parentMessageId 否则为 chat + const chatChannel = options.parentMessageId ? options.parentMessageId : 'chat' + currentChannel = chatChannel + EventsOn(chatChannel, (data: any) => { updateChat( +uuid, dataSources.value.length - 1, { dateTime: new Date().toLocaleString(), - text: errorMessage, + text: lastText + data.text ?? '', inversion: false, - error: true, + error: false, loading: false, - conversationOptions: null, + conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id }, requestOptions: { prompt: message, options: { ...options } }, }, ) scrollToBottom() - } - finally { + }) + ChatProcess({ + prompt: message, + options, + signal: controller.signal, + }).then((data: any) => { + EventsOff(chatChannel) loading.value = false - } + }) + } async function onRegenerate(index: number) { if (loading.value) return - controller = new AbortController() - const { requestOptions } = dataSources.value[index] + controller = new AbortController() + console.log("onRegenerate index", index) + console.log(dataSources.value) + const { requestOptions } = dataSources.value[index - 1] + console.log("requestOptions", requestOptions) let message = requestOptions?.prompt ?? '' @@ -205,6 +160,7 @@ async function onRegenerate(index: number) { options = { ...requestOptions.options } loading.value = true + console.log("loading.value", loading.value) updateChat( +uuid, @@ -220,70 +176,39 @@ async function onRegenerate(index: number) { }, ) - try { - let lastText = '' - EventsOn('chat', (data: any) => { - console.log(data) - updateChat( - +uuid, - index, - { - dateTime: new Date().toLocaleString(), - text: lastText + data.text ?? '', - inversion: false, - error: false, - loading: false, - conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id }, - requestOptions: { prompt: message, options: { ...options } }, - }, - ) - - if (openLongReply && data.detail.choices[0].finish_reason === 'length') { - options.parentMessageId = data.id - lastText = data.text - message = '' - return onConversation() - } - - scrollToBottom() - }) - ChatProcess({ - prompt: message, - options, - signal: controller.signal, - }) - } - catch (error: any) { - if (error.message === 'canceled') { - updateChatSome( - +uuid, - index, - { - loading: false, - }, - ) - return - } - - const errorMessage = error?.message ?? t('common.wrong') + let lastText = '' + const chatChannel = options.parentMessageId ? options.parentMessageId : 'chat' + currentChannel = chatChannel + EventsOn(chatChannel, (data: any) => { + // console.log(data) updateChat( +uuid, index, { dateTime: new Date().toLocaleString(), - text: errorMessage, + text: lastText + data.text ?? '', inversion: false, - error: true, + error: false, loading: false, - conversationOptions: null, - requestOptions: { prompt: message, ...options }, + conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id }, + requestOptions: { prompt: message, options: { ...options } }, }, ) - } - finally { + + scrollToBottom() + }) + ChatProcess({ + prompt: message, + options, + signal: controller.signal, + }).then((data: any) => { + // console.log(data) + EventsOff(chatChannel) loading.value = false - } + }) + + } function handleExport() { @@ -377,6 +302,8 @@ function handleStop() { if (loading.value) { controller.abort() loading.value = false + StopChat() + EventsOff(currentChannel) } } diff --git a/frontend/wailsjs/go/main/App.d.ts b/frontend/wailsjs/go/main/App.d.ts index 384b9fe..242ef8d 100755 --- a/frontend/wailsjs/go/main/App.d.ts +++ b/frontend/wailsjs/go/main/App.d.ts @@ -6,3 +6,5 @@ export function ChatProcess(arg1:any):Promise; export function Greet(arg1:string):Promise; export function Session():Promise; + +export function StopChat():Promise; diff --git a/frontend/wailsjs/go/main/App.js b/frontend/wailsjs/go/main/App.js index 3f254a6..a7e06c6 100755 --- a/frontend/wailsjs/go/main/App.js +++ b/frontend/wailsjs/go/main/App.js @@ -13,3 +13,7 @@ export function Greet(arg1) { export function Session() { return window['go']['main']['App']['Session'](); } + +export function StopChat() { + return window['go']['main']['App']['StopChat'](); +}