executeToolCalls 执行工具调用
(ctx context.Context, toolCalls []types.ToolCall)
| 639 | |
| 640 | // executeToolCalls 执行工具调用 |
| 641 | func (a *Agent) executeToolCalls(ctx context.Context, toolCalls []types.ToolCall) error { |
| 642 | results := make([]types.Message, len(toolCalls)) |
| 643 | |
| 644 | for i, call := range toolCalls { |
| 645 | tool, ok := a.toolMap[call.Name] |
| 646 | if !ok { |
| 647 | results[i] = types.Message{ |
| 648 | Role: types.RoleTool, |
| 649 | ToolCallID: call.ID, |
| 650 | Content: fmt.Sprintf("Error: tool '%s' not found", call.Name), |
| 651 | } |
| 652 | continue |
| 653 | } |
| 654 | |
| 655 | // 执行工具 |
| 656 | req := &tools.ExecuteRequest{ |
| 657 | Tool: tool, |
| 658 | Input: call.Arguments, |
| 659 | Context: a.buildToolContext(ctx), |
| 660 | } |
| 661 | execResult := a.executor.Execute(ctx, req) |
| 662 | if execResult.Error != nil { |
| 663 | results[i] = types.Message{ |
| 664 | Role: types.RoleTool, |
| 665 | ToolCallID: call.ID, |
| 666 | Content: fmt.Sprintf("Error: %v", execResult.Error), |
| 667 | } |
| 668 | continue |
| 669 | } |
| 670 | |
| 671 | results[i] = types.Message{ |
| 672 | Role: types.RoleTool, |
| 673 | ToolCallID: call.ID, |
| 674 | Content: fmt.Sprint(execResult.Output), |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | // 追加工具结果到消息历史 |
| 679 | a.mu.Lock() |
| 680 | a.messages = append(a.messages, results...) |
| 681 | a.mu.Unlock() |
| 682 | |
| 683 | return nil |
| 684 | } |
| 685 | |
| 686 | // getToolsForProvider 获取 Provider 格式的工具定义 |
| 687 | func (a *Agent) getToolsForProvider() []types.ToolDefinition { |
no test coverage detected