feat: complete v0.3.1 agent workflow
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"agentu/internal/safeio"
|
||||
"agentu/pkg/llm"
|
||||
)
|
||||
|
||||
@@ -21,6 +22,36 @@ func NewFileEditTool(workingDir string) *FileEditTool {
|
||||
|
||||
func (*FileEditTool) Mutates() bool { return true }
|
||||
|
||||
func (t *FileEditTool) Preview(ctx context.Context, raw json.RawMessage) (string, error) {
|
||||
var args fileEditArgs
|
||||
if err := json.Unmarshal(raw, &args); err != nil {
|
||||
return "", fmt.Errorf("parse arguments: %w", err)
|
||||
}
|
||||
if args.StartLine > 0 {
|
||||
end := args.EndLine
|
||||
if end == 0 {
|
||||
end = args.StartLine
|
||||
}
|
||||
return fmt.Sprintf("edit %s lines %d-%d", args.Path, args.StartLine, end), ctx.Err()
|
||||
}
|
||||
return fmt.Sprintf("replace text in %s", args.Path), ctx.Err()
|
||||
}
|
||||
|
||||
func (t *FileEditTool) TargetPaths(raw json.RawMessage) ([]string, error) {
|
||||
var args fileEditArgs
|
||||
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 := resolvePath(t.workingDir, args.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []string{path}, nil
|
||||
}
|
||||
|
||||
func (t *FileEditTool) Definition() llm.Tool {
|
||||
return llm.Tool{
|
||||
Type: "function",
|
||||
@@ -111,7 +142,7 @@ func (t *FileEditTool) Execute(ctx context.Context, raw json.RawMessage) (string
|
||||
if err := ctx.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(result), perm); err != nil {
|
||||
if err := safeio.AtomicWriteFile(path, []byte(result), perm); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user