@@ -4,13 +4,26 @@ import { useMcpStore } from '@/renderer/store/mcp'
44import { useHistoryStore } from '@/renderer/store/history'
55import { createCompletion , isEmptyTools } from '@/renderer/composables/chatCompletions'
66
7+ import type {
8+ ToolCall ,
9+ ToolMessage ,
10+ UserMessage ,
11+ ChatConversationMessage
12+ } from '@/renderer/types/message'
13+
14+ interface MessageStoreState {
15+ userMessage : string
16+ conversation : ChatConversationMessage [ ]
17+ historyId : string
18+ base64 : string
19+ generating : boolean
20+ }
21+
722export const useMessageStore = defineStore ( 'messageStore' , {
8- // TODO: fix any to type
9- state : ( ) : any => ( {
23+ state : ( ) : MessageStoreState => ( {
1024 userMessage : '' ,
1125 conversation : [ ] ,
1226 historyId : '' ,
13- images : [ ] ,
1427 base64 : '' ,
1528 generating : false
1629 } ) ,
@@ -35,7 +48,6 @@ export const useMessageStore = defineStore('messageStore', {
3548 } ,
3649 clear ( ) {
3750 this . userMessage = ''
38- this . images = [ ]
3951 } ,
4052 handleKeydown ( e ) {
4153 if ( e . key === 'Enter' && e . shiftKey ) {
@@ -105,48 +117,50 @@ export const useMessageStore = defineStore('messageStore', {
105117 postToolCall : async function ( ) {
106118 const mcpStore = useMcpStore ( )
107119 const last = this . conversation . at ( - 1 )
108- if ( ! last || ! last . tool_calls ) {
120+
121+ if ( ! last || ! ( 'tool_calls' in last ) || ! last . tool_calls ) {
109122 return
110123 }
124+
111125 if ( isEmptyTools ( last . tool_calls ) ) {
112126 delete last . tool_calls
113- // return
114- } else {
115- let toolCalled = false
116- console . log ( last . tool_calls )
127+ return
128+ }
117129
118- const callNextTool = async ( toolCalls , index ) => {
119- if ( index >= toolCalls . length ) {
120- return
121- }
130+ let toolCalled = false
131+ console . log ( last . tool_calls )
122132
123- const toolCall = toolCalls [ index ]
133+ const callNextTool = async ( toolCalls : ToolCall [ ] , index : number ) => {
134+ if ( index >= toolCalls . length ) {
135+ return
136+ }
124137
125- let result
138+ const toolCall = toolCalls [ index ]
126139
127- try {
128- result = await mcpStore . callTool ( toolCall . function . name , toolCall . function . arguments )
129- console . log ( result )
130- } catch ( error ) {
131- result = mcpStore . packReturn ( `Error calling tool: ${ error } ` )
132- }
140+ let result
133141
134- if ( result . content ) {
135- this . contentConvert ( result . content , toolCall . id ) . forEach ( ( item ) => {
136- this . conversation . push ( item )
137- } )
138- toolCalled = true
139- }
142+ try {
143+ result = await mcpStore . callTool ( toolCall . function . name , toolCall . function . arguments )
144+ console . log ( result )
145+ } catch ( error ) {
146+ result = mcpStore . packReturn ( `Error calling tool: ${ error } ` )
140147 }
141148
142- await callNextTool ( last . tool_calls , 0 )
143-
144- if ( toolCalled ) {
145- this . startInference ( )
149+ if ( result . content ) {
150+ this . contentConvert ( result . content , toolCall . id ) . forEach ( ( item ) => {
151+ this . conversation . push ( item )
152+ } )
153+ toolCalled = true
146154 }
147155 }
156+
157+ await callNextTool ( last . tool_calls , 0 )
158+
159+ if ( toolCalled ) {
160+ this . startInference ( )
161+ }
148162 } ,
149- contentConvert : function ( content , toolCallId ) {
163+ contentConvert : function ( content , toolCallId ) : Array < UserMessage | ToolMessage > {
150164 const mcpStore = useMcpStore ( )
151165 const msg = content . map ( ( item ) => mcpStore . convertItem ( item ) )
152166 console . log ( msg )
0 commit comments