executeToolCommands executes !tool_name(arg=value) patterns and replaces them with output.
(ctx context.Context, rt Runtime, instruction string)
| 213 | |
| 214 | // executeToolCommands executes !tool_name(arg=value) patterns and replaces them with output. |
| 215 | func executeToolCommands(ctx context.Context, rt Runtime, instruction string) string { |
| 216 | commands := parseToolCommands(instruction) |
| 217 | if len(commands) == 0 { |
| 218 | return instruction |
| 219 | } |
| 220 | |
| 221 | agentTools, err := rt.CurrentAgentTools(ctx) |
| 222 | if err != nil { |
| 223 | slog.WarnContext(ctx, "Failed to get agent tools for command execution", "error", err) |
| 224 | return instruction |
| 225 | } |
| 226 | |
| 227 | toolMap := make(map[string]tools.Tool, len(agentTools)) |
| 228 | for _, t := range agentTools { |
| 229 | toolMap[t.Name] = t |
| 230 | } |
| 231 | |
| 232 | // Process in reverse order to maintain correct indices |
| 233 | result := instruction |
| 234 | for _, cmd := range slices.Backward(commands) { |
| 235 | replacement := executeSingleToolCommand(ctx, toolMap, cmd.toolName, cmd.argsStr) |
| 236 | result = result[:cmd.start] + replacement + result[cmd.end:] |
| 237 | } |
| 238 | |
| 239 | return result |
| 240 | } |
| 241 | |
| 242 | // executeSingleToolCommand executes a single tool command and returns the output. |
| 243 | func executeSingleToolCommand(ctx context.Context, toolMap map[string]tools.Tool, toolName, argsStr string) string { |
no test coverage detected