Skip to content

Commit

Permalink
优化结构
Browse files Browse the repository at this point in the history
  • Loading branch information
gcslaoli committed Mar 22, 2023
1 parent 81a33bc commit 8edd089
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 129 deletions.
15 changes: 13 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 数据写入响应
Expand All @@ -119,3 +125,8 @@ func (a *App) ChatProcess(req *ChatProcessReq) (err error) {

return
}

// StopChat
func (a *App) StopChat() {
g.Log().Info(a.ctx, "StopChat~~~~~~~~~~~~~~~~~~~~")
}
181 changes: 54 additions & 127 deletions frontend/src/views/chat/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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 ?? ''
Expand All @@ -205,6 +160,7 @@ async function onRegenerate(index: number) {
options = { ...requestOptions.options }
loading.value = true
console.log("loading.value", loading.value)
updateChat(
+uuid,
Expand All @@ -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() {
Expand Down Expand Up @@ -377,6 +302,8 @@ function handleStop() {
if (loading.value) {
controller.abort()
loading.value = false
StopChat()
EventsOff(currentChannel)
}
}
Expand Down
2 changes: 2 additions & 0 deletions frontend/wailsjs/go/main/App.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export function ChatProcess(arg1:any):Promise<void>;
export function Greet(arg1:string):Promise<string>;

export function Session():Promise<any>;

export function StopChat():Promise<void>;
4 changes: 4 additions & 0 deletions frontend/wailsjs/go/main/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']();
}

0 comments on commit 8edd089

Please sign in to comment.