zot/packages/agent/tools/write.go
patriceckhart fa7d8d8be5 refactor: split source into packages/{provider,core,tui,agent}
Single Go module, four top-level packages under packages/. Import
paths become github.com/patriceckhart/zot/packages/<name>; downstream
consumers can depend on individual packages without pulling the rest.

Layout:
  packages/provider/     LLM clients + catalog
  packages/provider/auth/ credential store + OAuth + login server
  packages/core/         agent loop, sessions, cost
  packages/tui/          terminal toolkit + chat view
  packages/agent/        CLI wiring, system prompt
    extensions/ extproto/ modes/ tools/ skills/ swarm/
    sdk/  (was pkg/zotcore, package renamed zotcore -> sdk)
    ext/  (was pkg/zotext, package renamed zotext -> ext)

internal/ and pkg/ removed. The internal/assets logo moved into
packages/provider/auth/assets.

Public Go SDK identifiers renamed:
  pkg/zotcore (package zotcore) -> packages/agent/sdk (package sdk)
  pkg/zotext  (package zotext)  -> packages/agent/ext (package ext)

This breaks Go-based extensions and embedders; the JSON wire protocol
for extensions and RPC is unchanged, so non-Go extensions, already-
built extension binaries, and zot rpc consumers are unaffected.

Docs, examples, and the built-in write-zot-extension skill updated
for the new paths and identifiers. Shadow-bug fixes in code samples
(ext := ext.New -> e := ext.New).
2026-05-27 09:07:15 +02:00

74 lines
2.2 KiB
Go

package tools
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/patriceckhart/zot/packages/core"
"github.com/patriceckhart/zot/packages/provider"
)
// WriteTool writes content to a file, creating parent directories.
type WriteTool struct {
CWD string
Sandbox *Sandbox
}
type writeArgs struct {
Path string `json:"path"`
Content string `json:"content"`
}
const writeSchema = `{"type":"object","properties":{"path":{"type":"string"},"content":{"type":"string"}},"required":["path","content"]}`
func (t *WriteTool) Name() string { return "write" }
func (t *WriteTool) Description() string {
return "Write a file. Creates parent dirs. Overwrites."
}
func (t *WriteTool) Schema() json.RawMessage { return json.RawMessage(writeSchema) }
func (t *WriteTool) Execute(ctx context.Context, raw json.RawMessage, progress func(string)) (core.ToolResult, error) {
var a writeArgs
if err := json.Unmarshal(raw, &a); err != nil {
return core.ToolResult{}, fmt.Errorf("invalid args: %w", err)
}
if a.Path == "" {
return core.ToolResult{}, fmt.Errorf("path is required")
}
path := resolvePath(t.CWD, a.Path)
if err := t.Sandbox.CheckPath(path); err != nil {
return core.ToolResult{}, err
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return core.ToolResult{}, err
}
if err := os.WriteFile(path, []byte(a.Content), 0o644); err != nil {
return core.ToolResult{}, err
}
// Return the file content as the result body, just like `read`
// does. The TUI renders it with a syntax-highlighted gutter so
// the on-screen view after a `write` matches the pre-write
// streaming preview seamlessly. The model also sees the written
// content in its tool_result, which is useful on follow-up turns
// where it wants to reference what it just wrote without a
// second `read` call.
totalLines := strings.Count(a.Content, "\n")
if len(a.Content) > 0 && !strings.HasSuffix(a.Content, "\n") {
totalLines++ // count the last unterminated line
}
return core.ToolResult{
Content: []provider.Content{provider.TextBlock{Text: a.Content}},
Details: map[string]any{
"path": path,
"bytes": len(a.Content),
"total_lines": totalLines,
"start_line": 1,
},
}, nil
}