MCPcopy
hub / github.com/wavetermdev/waveterm / ResolveToolCall

Function ResolveToolCall

pkg/aiusechat/usechat.go:495–547  ·  view source on GitHub ↗
(toolDef *uctypes.ToolDefinition, toolCall uctypes.WaveToolCall, chatOpts uctypes.WaveChatOpts)

Source from the content-addressed store, hash-verified

493}
494
495func ResolveToolCall(toolDef *uctypes.ToolDefinition, toolCall uctypes.WaveToolCall, chatOpts uctypes.WaveChatOpts) (result uctypes.AIToolResult) {
496 result = uctypes.AIToolResult{
497 ToolName: toolCall.Name,
498 ToolUseID: toolCall.ID,
499 }
500
501 defer func() {
502 if r := recover(); r != nil {
503 result.ErrorText = fmt.Sprintf("panic in tool execution: %v", r)
504 result.Text = ""
505 }
506 }()
507
508 if toolDef == nil {
509 result.ErrorText = fmt.Sprintf("tool '%s' not found", toolCall.Name)
510 return
511 }
512
513 // Try ToolTextCallback first, then ToolAnyCallback
514 if toolDef.ToolTextCallback != nil {
515 text, err := toolDef.ToolTextCallback(toolCall.Input)
516 if err != nil {
517 result.ErrorText = err.Error()
518 } else {
519 result.Text = text
520 // Recompute tool description with the result
521 if toolDef.ToolCallDesc != nil && toolCall.ToolUseData != nil {
522 toolCall.ToolUseData.ToolDesc = toolDef.ToolCallDesc(toolCall.Input, text, toolCall.ToolUseData)
523 }
524 }
525 } else if toolDef.ToolAnyCallback != nil {
526 output, err := toolDef.ToolAnyCallback(toolCall.Input, toolCall.ToolUseData)
527 if err != nil {
528 result.ErrorText = err.Error()
529 } else {
530 // Marshal the result to JSON
531 jsonBytes, marshalErr := json.Marshal(output)
532 if marshalErr != nil {
533 result.ErrorText = fmt.Sprintf("failed to marshal tool output: %v", marshalErr)
534 } else {
535 result.Text = string(jsonBytes)
536 // Recompute tool description with the result
537 if toolDef.ToolCallDesc != nil && toolCall.ToolUseData != nil {
538 toolCall.ToolUseData.ToolDesc = toolDef.ToolCallDesc(toolCall.Input, output, toolCall.ToolUseData)
539 }
540 }
541 }
542 } else {
543 result.ErrorText = fmt.Sprintf("tool '%s' has no callback functions", toolCall.Name)
544 }
545
546 return
547}
548
549func WaveAIPostMessageWrap(ctx context.Context, sseHandler *sse.SSEHandlerCh, message *uctypes.AIMessage, chatOpts uctypes.WaveChatOpts) error {
550 startTime := time.Now()

Callers 1

processToolCallInternalFunction · 0.85

Calls 1

ErrorMethod · 0.45

Tested by

no test coverage detected