(toolDef *uctypes.ToolDefinition, toolCall uctypes.WaveToolCall, chatOpts uctypes.WaveChatOpts)
| 493 | } |
| 494 | |
| 495 | func 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 | |
| 549 | func WaveAIPostMessageWrap(ctx context.Context, sseHandler *sse.SSEHandlerCh, message *uctypes.AIMessage, chatOpts uctypes.WaveChatOpts) error { |
| 550 | startTime := time.Now() |
no test coverage detected