@@ -2,7 +2,11 @@ import { defineStore } from 'pinia'
22import { useSnackbarStore } from '@/renderer/store/snackbar'
33import { useMcpStore } from '@/renderer/store/mcp'
44import { useHistoryStore } from '@/renderer/store/history'
5- import { createCompletion , isEmptyTools } from '@/renderer/composables/chatCompletions'
5+ import {
6+ createCompletion ,
7+ isEmptyTools ,
8+ ChatProcessResult
9+ } from '@/renderer/composables/chatCompletions'
610
711import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js'
812
@@ -15,7 +19,7 @@ interface MessageStoreState {
1519 conversation : ChatConversationMessage [ ]
1620 historyId : string
1721 base64 : string
18- generating : Record < string , AbortController >
22+ generating : Record < string , 'prepare' | AbortController | 'toolcall' >
1923}
2024
2125export const useMessageStore = defineStore ( 'messageStore' , {
@@ -41,12 +45,21 @@ export const useMessageStore = defineStore('messageStore', {
4145 this . historyId = ''
4246 } ,
4347 stop ( ) {
44- const id = this . historyId
48+ this . delete ( this . historyId )
4549 const snackbarStore = useSnackbarStore ( )
46- this . generating [ id ] . abort ( )
47- delete this . generating [ id ]
4850 snackbarStore . showInfoMessage ( 'snackbar.stopped' )
4951 } ,
52+ delete ( id : string ) {
53+ if ( id in this . generating ) {
54+ if ( this . generating [ id ] instanceof AbortController ) {
55+ this . generating [ id ] . abort ( )
56+ }
57+ delete this . generating [ id ]
58+ return true
59+ } else {
60+ return false
61+ }
62+ } ,
5063 clear ( ) {
5164 this . userMessage = ''
5265 } ,
@@ -120,13 +133,17 @@ export const useMessageStore = defineStore('messageStore', {
120133 const historyId = this . syncHistory ( )
121134 this . clear ( )
122135
123- createCompletion ( this . conversation , historyId ) . then ( ( ) => {
124- this . postToolCall ( )
136+ this . generating [ historyId ] = 'prepare'
137+
138+ createCompletion ( this . conversation , historyId ) . then ( ( reason : ChatProcessResult ) => {
139+ if ( reason === 'done' ) {
140+ this . postToolCall ( historyId )
141+ }
125142 } )
126143
127144 return historyId
128145 } ,
129- postToolCall : async function ( ) {
146+ postToolCall : async function ( historyId : string ) {
130147 const mcpStore = useMcpStore ( )
131148 const last = this . conversation . at ( - 1 )
132149
@@ -141,6 +158,7 @@ export const useMessageStore = defineStore('messageStore', {
141158
142159 let toolCalled = false
143160 console . log ( last . tool_calls )
161+ this . generating [ historyId ] = 'toolcall'
144162
145163 for ( const toolCall of last . tool_calls ) {
146164 let result : CallToolResult
@@ -160,7 +178,7 @@ export const useMessageStore = defineStore('messageStore', {
160178 }
161179 }
162180
163- if ( toolCalled ) {
181+ if ( this . delete ( historyId ) && toolCalled ) {
164182 this . startInference ( )
165183 }
166184 } ,
0 commit comments