ExecuteToolDirect 直接执行工具(程序化工具调用) 这个方法允许 Agent 或外部代码直接调用工具,绕过 LLM 决策 主要用于程序化工具编排场景
(ctx context.Context, toolName string, input map[string]any)
| 1109 | // 这个方法允许 Agent 或外部代码直接调用工具,绕过 LLM 决策 |
| 1110 | // 主要用于程序化工具编排场景 |
| 1111 | func (a *Agent) ExecuteToolDirect(ctx context.Context, toolName string, input map[string]any) (any, error) { |
| 1112 | a.mu.RLock() |
| 1113 | tool, exists := a.toolMap[toolName] |
| 1114 | a.mu.RUnlock() |
| 1115 | |
| 1116 | if !exists { |
| 1117 | return nil, fmt.Errorf("tool not found: %s", toolName) |
| 1118 | } |
| 1119 | |
| 1120 | // 构建工具上下文 |
| 1121 | tc := a.buildToolContext(ctx) |
| 1122 | |
| 1123 | // 执行工具 |
| 1124 | result, err := tool.Execute(ctx, input, tc) |
| 1125 | if err != nil { |
| 1126 | return nil, fmt.Errorf("execute tool %s: %w", toolName, err) |
| 1127 | } |
| 1128 | |
| 1129 | return result, nil |
| 1130 | } |
| 1131 | |
| 1132 | // ExecuteToolsDirect 批量直接执行工具(顺序执行) |
| 1133 | func (a *Agent) ExecuteToolsDirect(ctx context.Context, calls []ToolCall) []ToolCallResult { |
no test coverage detected