Matt Low
3fde58b77d
- More emphasis on `api` package. It now holds database model structs from `lmcli/models` (which is now gone) as well as the tool spec, call, and result types. `tools.Tool` is now `api.ToolSpec`. `api.ChatCompletionClient` was renamed to `api.ChatCompletionProvider`. - Change ChatCompletion interface and implementations to no longer do automatic tool call recursion - they simply return a ToolCall message which the caller can decide what to do with (e.g. prompt for user confirmation before executing) - `api.ChatCompletionProvider` functions have had their ReplyCallback parameter removed, as now they only return a single reply. - Added a top-level `agent` package, moved the current built-in tools implementations under `agent/toolbox`. `tools.ExecuteToolCalls` is now `agent.ExecuteToolCalls`. - Fixed request context handling in openai, google, ollama (use `NewRequestWithContext`), cleaned up request cancellation in TUI - Fix tool call tui persistence bug (we were skipping message with empty content) - Now handle tool calling from TUI layer TODO: - Prompt users before executing tool calls - Automatically send tool results to the model (or make this toggleable)
72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package toolbox
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
toolutil "git.mlow.ca/mlow/lmcli/pkg/agent/toolbox/util"
|
|
"git.mlow.ca/mlow/lmcli/pkg/api"
|
|
)
|
|
|
|
const WRITE_FILE_DESCRIPTION = `Write the provided contents to a file relative to the current working directory.
|
|
|
|
Example result:
|
|
{
|
|
"message": "success"
|
|
}`
|
|
|
|
var WriteFileTool = api.ToolSpec{
|
|
Name: "write_file",
|
|
Description: WRITE_FILE_DESCRIPTION,
|
|
Parameters: []api.ToolParameter{
|
|
{
|
|
Name: "path",
|
|
Type: "string",
|
|
Description: "Path to a file within the current working directory to write to.",
|
|
Required: true,
|
|
},
|
|
{
|
|
Name: "content",
|
|
Type: "string",
|
|
Description: "The content to write to the file. Overwrites any existing content!",
|
|
Required: true,
|
|
},
|
|
},
|
|
Impl: func(t *api.ToolSpec, args map[string]interface{}) (string, error) {
|
|
tmp, ok := args["path"]
|
|
if !ok {
|
|
return "", fmt.Errorf("Path parameter to write_file was not included.")
|
|
}
|
|
path, ok := tmp.(string)
|
|
if !ok {
|
|
return "", fmt.Errorf("Invalid path in function arguments: %v", tmp)
|
|
}
|
|
tmp, ok = args["content"]
|
|
if !ok {
|
|
return "", fmt.Errorf("Content parameter to write_file was not included.")
|
|
}
|
|
content, ok := tmp.(string)
|
|
if !ok {
|
|
return "", fmt.Errorf("Invalid content in function arguments: %v", tmp)
|
|
}
|
|
result := writeFile(path, content)
|
|
ret, err := result.ToJson()
|
|
if err != nil {
|
|
return "", fmt.Errorf("Could not serialize result: %v", err)
|
|
}
|
|
return ret, nil
|
|
},
|
|
}
|
|
|
|
func writeFile(path string, content string) api.CallResult {
|
|
ok, reason := toolutil.IsPathWithinCWD(path)
|
|
if !ok {
|
|
return api.CallResult{Message: reason}
|
|
}
|
|
err := os.WriteFile(path, []byte(content), 0644)
|
|
if err != nil {
|
|
return api.CallResult{Message: fmt.Sprintf("Could not write to path: %s", err.Error())}
|
|
}
|
|
return api.CallResult{}
|
|
}
|