@@ -14,6 +14,7 @@ import (
1414 "strings"
1515
1616 "github.com/wavetermdev/waveterm/pkg/aiusechat/aiutil"
17+ "github.com/wavetermdev/waveterm/pkg/aiusechat/chatstore"
1718 "github.com/wavetermdev/waveterm/pkg/aiusechat/uctypes"
1819 "github.com/wavetermdev/waveterm/pkg/wavebase"
1920)
@@ -292,3 +293,54 @@ func ConvertAIChatToUIChat(aiChat uctypes.AIChat) (*uctypes.UIChat, error) {
292293
293294 return uiChat , nil
294295}
296+
297+ // GetFunctionCallInputByToolCallId searches for a tool call by ID in the chat history
298+ func GetFunctionCallInputByToolCallId (aiChat uctypes.AIChat , toolCallId string ) * uctypes.AIFunctionCallInput {
299+ for _ , genMsg := range aiChat .NativeMessages {
300+ compMsg , ok := genMsg .(* CompletionsChatMessage )
301+ if ! ok {
302+ continue
303+ }
304+ idx := compMsg .Message .FindToolCallIndex (toolCallId )
305+ if idx == - 1 {
306+ continue
307+ }
308+ toolCall := compMsg .Message .ToolCalls [idx ]
309+ return & uctypes.AIFunctionCallInput {
310+ CallId : toolCall .ID ,
311+ Name : toolCall .Function .Name ,
312+ Arguments : toolCall .Function .Arguments ,
313+ ToolUseData : toolCall .ToolUseData ,
314+ }
315+ }
316+ return nil
317+ }
318+
319+ // UpdateToolUseData updates the ToolUseData for a specific tool call in the chat history
320+ func UpdateToolUseData (chatId string , callId string , newToolUseData * uctypes.UIMessageDataToolUse ) error {
321+ chat := chatstore .DefaultChatStore .Get (chatId )
322+ if chat == nil {
323+ return fmt .Errorf ("chat not found: %s" , chatId )
324+ }
325+
326+ for _ , genMsg := range chat .NativeMessages {
327+ compMsg , ok := genMsg .(* CompletionsChatMessage )
328+ if ! ok {
329+ continue
330+ }
331+ idx := compMsg .Message .FindToolCallIndex (callId )
332+ if idx == - 1 {
333+ continue
334+ }
335+ updatedMsg := compMsg .Copy ()
336+ updatedMsg .Message .ToolCalls [idx ].ToolUseData = newToolUseData
337+ aiOpts := & uctypes.AIOptsType {
338+ APIType : chat .APIType ,
339+ Model : chat .Model ,
340+ APIVersion : chat .APIVersion ,
341+ }
342+ return chatstore .DefaultChatStore .PostMessage (chatId , aiOpts , updatedMsg )
343+ }
344+
345+ return fmt .Errorf ("tool call with callId %s not found in chat %s" , callId , chatId )
346+ }
0 commit comments