mirror of
https://github.com/mark3labs/kit.git
synced 2026-06-14 03:30:26 +00:00
d8f40039fe
- Add ToolOption/WithWorkDir functional options pattern to internal/core - Update all 7 tool constructors to accept ...ToolOption and resolve paths relative to the configured working directory - Create pkg/kit/tools.go with public exports: individual constructors, bundles (AllTools, CodingTools, ReadOnlyTools), and WithWorkDir - Add CoreTools field to AgentConfig/AgentCreationOptions so callers can inject custom tool sets instead of hardcoding core.AllTools() - Add Tools field to kit.Options and GetTools() to kit.Kit - Fully backward compatible: no-arg calls use os.Getwd() as before
68 lines
2.0 KiB
Go
68 lines
2.0 KiB
Go
package core
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"charm.land/fantasy"
|
|
)
|
|
|
|
type writeArgs struct {
|
|
Path string `json:"path"`
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
// NewWriteTool creates the write core tool.
|
|
func NewWriteTool(opts ...ToolOption) fantasy.AgentTool {
|
|
cfg := ApplyOptions(opts)
|
|
return &coreTool{
|
|
info: fantasy.ToolInfo{
|
|
Name: "write",
|
|
Description: "Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.",
|
|
Parameters: map[string]any{
|
|
"path": map[string]any{
|
|
"type": "string",
|
|
"description": "Path to the file to write (relative or absolute)",
|
|
},
|
|
"content": map[string]any{
|
|
"type": "string",
|
|
"description": "Content to write to the file",
|
|
},
|
|
},
|
|
Required: []string{"path", "content"},
|
|
},
|
|
handler: func(ctx context.Context, call fantasy.ToolCall) (fantasy.ToolResponse, error) {
|
|
return executeWrite(ctx, call, cfg.WorkDir)
|
|
},
|
|
}
|
|
}
|
|
|
|
func executeWrite(ctx context.Context, call fantasy.ToolCall, workDir string) (fantasy.ToolResponse, error) {
|
|
var args writeArgs
|
|
if err := parseArgs(call.Input, &args); err != nil {
|
|
return fantasy.NewTextErrorResponse("path and content parameters are required"), nil
|
|
}
|
|
if args.Path == "" {
|
|
return fantasy.NewTextErrorResponse("path parameter is required"), nil
|
|
}
|
|
|
|
absPath, err := resolvePathWithWorkDir(args.Path, workDir)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(fmt.Sprintf("invalid path: %v", err)), nil
|
|
}
|
|
|
|
// Create parent directories
|
|
dir := filepath.Dir(absPath)
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
return fantasy.NewTextErrorResponse(fmt.Sprintf("failed to create directories: %v", err)), nil
|
|
}
|
|
|
|
if err := os.WriteFile(absPath, []byte(args.Content), 0644); err != nil {
|
|
return fantasy.NewTextErrorResponse(fmt.Sprintf("failed to write file: %v", err)), nil
|
|
}
|
|
|
|
return fantasy.NewTextResponse(fmt.Sprintf("Wrote %d bytes to %s", len(args.Content), args.Path)), nil
|
|
}
|