48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package tools
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/lmcli/model"
|
|
)
|
|
|
|
var AvailableTools map[string]model.Tool = map[string]model.Tool{
|
|
"read_dir": ReadDirTool,
|
|
"read_file": ReadFileTool,
|
|
"write_file": WriteFileTool,
|
|
"file_insert_lines": FileInsertLinesTool,
|
|
"file_replace_lines": FileReplaceLinesTool,
|
|
}
|
|
|
|
func ExecuteToolCalls(toolCalls []model.ToolCall, toolBag []model.Tool) ([]model.ToolResult, error) {
|
|
var toolResults []model.ToolResult
|
|
for _, toolCall := range toolCalls {
|
|
var tool *model.Tool
|
|
for _, available := range toolBag {
|
|
if available.Name == toolCall.Name {
|
|
tool = &available
|
|
break
|
|
}
|
|
}
|
|
if tool == nil {
|
|
return nil, fmt.Errorf("Requested tool '%s' does not exist. Hallucination?", toolCall.Name)
|
|
}
|
|
|
|
// Execute the tool
|
|
result, err := tool.Impl(tool, toolCall.Parameters)
|
|
if err != nil {
|
|
// This can happen if the model missed or supplied invalid tool args
|
|
return nil, fmt.Errorf("Tool '%s' error: %v\n", toolCall.Name, err)
|
|
}
|
|
|
|
toolResult := model.ToolResult{
|
|
ToolCallID: toolCall.ID,
|
|
ToolName: toolCall.Name,
|
|
Result: result,
|
|
}
|
|
|
|
toolResults = append(toolResults, toolResult)
|
|
}
|
|
return toolResults, nil
|
|
}
|