feat: complete v0.3.1 agent workflow
This commit is contained in:
+44
-16
@@ -13,6 +13,7 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"agentu/internal/safeio"
|
||||
"agentu/pkg/llm"
|
||||
)
|
||||
|
||||
@@ -83,6 +84,33 @@ func NewFileWriteTool(workingDir string) *FileWriteTool {
|
||||
}
|
||||
func (*FileWriteTool) Mutates() bool { return true }
|
||||
|
||||
func (t *FileWriteTool) Preview(ctx context.Context, raw json.RawMessage) (string, error) {
|
||||
var args fileWriteArgs
|
||||
if err := json.Unmarshal(raw, &args); err != nil {
|
||||
return "", fmt.Errorf("parse arguments: %w", err)
|
||||
}
|
||||
action := "write"
|
||||
if args.Append {
|
||||
action = "append"
|
||||
}
|
||||
return fmt.Sprintf("%s %d bytes to %s", action, len(args.Content), args.Path), ctx.Err()
|
||||
}
|
||||
|
||||
func (t *FileWriteTool) TargetPaths(raw json.RawMessage) ([]string, error) {
|
||||
var args fileWriteArgs
|
||||
if err := json.Unmarshal(raw, &args); err != nil {
|
||||
return nil, fmt.Errorf("parse arguments: %w", err)
|
||||
}
|
||||
if strings.TrimSpace(args.Path) == "" {
|
||||
return nil, errors.New("path is required")
|
||||
}
|
||||
path, err := resolvePathForWrite(t.workingDir, args.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []string{path}, nil
|
||||
}
|
||||
|
||||
func (t *FileWriteTool) Definition() llm.Tool {
|
||||
return llm.Tool{
|
||||
Type: "function",
|
||||
@@ -117,28 +145,28 @@ func (t *FileWriteTool) Execute(ctx context.Context, raw json.RawMessage) (strin
|
||||
if strings.TrimSpace(args.Path) == "" {
|
||||
return "", errors.New("path is required")
|
||||
}
|
||||
path, err := resolvePath(t.workingDir, args.Path)
|
||||
path, err := resolvePathForWrite(t.workingDir, args.Path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
flag := os.O_CREATE | os.O_WRONLY | os.O_TRUNC
|
||||
if args.Append {
|
||||
flag = os.O_CREATE | os.O_WRONLY | os.O_APPEND
|
||||
}
|
||||
file, err := os.OpenFile(path, flag, 0o644)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
if _, err := file.WriteString(args.Content); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
content := []byte(args.Content)
|
||||
perm := os.FileMode(0o644)
|
||||
if info, statErr := os.Stat(path); statErr == nil {
|
||||
perm = info.Mode().Perm()
|
||||
}
|
||||
if args.Append {
|
||||
existing, readErr := os.ReadFile(path)
|
||||
if readErr != nil && !os.IsNotExist(readErr) {
|
||||
return "", readErr
|
||||
}
|
||||
content = append(existing, content...)
|
||||
}
|
||||
if err := safeio.AtomicWriteFile(path, content, perm); err != nil {
|
||||
return "", err
|
||||
}
|
||||
action := "wrote"
|
||||
if args.Append {
|
||||
action = "appended"
|
||||
|
||||
Reference in New Issue
Block a user