Documentation
¶
Overview ¶
Package scratchtool provides a session-scoped key/value scratchpad tool for ds4go tool loops.
A scratchpad is small, durable working memory for the model that is neither the chat log nor the user's project tree: a flat map of key → text blob, persisted one file per key under $DS4_DIR/scratch/<session>/. The model lists keys, reads a value (optionally head or tail bytes), replaces a value, appends to a value, and deletes a key.
Keys are flat names of 1–64 characters from [a-z0-9._-]; there are no directories or nested paths. Limits (MaxKeys, MaxValueBytes, MaxTotalBytes) are enforced before every write. Routine failures are returned to the model as "ERROR: ..." tool results so a tool loop can continue; only context cancellation aborts the loop.
Writes are atomic (temp file renamed over the destination) and a Store is safe for concurrent use within one process. Per-key file I/O is confined to the session directory through an os.Root handle, and reads reject symlinks and other non-regular files. Two processes sharing a session are not coordinated and can race each other's writes.
A typical setup registers the scratch tools alongside other tools:
store, err := scratchtool.New(scratchtool.Config{Session: "mytask"})
if err != nil {
return err
}
defer store.Close()
reg := ds4go.NewToolRegistry()
if err := store.Register(reg); err != nil {
return err
}
system := scratchtool.SystemHint + "\n\n" + mySystemPrompt
A read-only registration advertises only scratch_list and scratch_get:
store, err := scratchtool.New(scratchtool.Config{
Session: "mytask",
ReadOnly: true,
})
if err != nil {
return err
}
defer store.Close()
if err := store.RegisterReadOnly(reg); err != nil {
return err
}
Index ¶
Constants ¶
const SystemHint = "" /* 163-byte string literal not displayed */
SystemHint is an optional line callers can prepend to the system prompt when the scratch tools are registered.
Variables ¶
This section is empty.
Functions ¶
func DefaultDir ¶
DefaultDir returns the default session directory for a scratchpad session: the "scratch/<session>" subdirectory of the ds4go data directory (DS4_DIR, default ~/.ds4). An empty session means "default".
Types ¶
type Config ¶
type Config struct {
// Dir is the session directory. Empty → DefaultDir(Session).
Dir string
// Session names the pad namespace. Empty → "default".
// Must pass the same key rules as pad keys.
Session string
// MaxKeys default 32.
MaxKeys int
// MaxValueBytes default 16KiB.
MaxValueBytes int
// MaxTotalBytes default 128KiB.
MaxTotalBytes int
// ReadOnly registers get/list only.
ReadOnly bool
}
Config controls scratchpad storage and limits.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is one session's key/value scratchpad, persisted one file per key.
func New ¶
New creates the session directory if needed and returns a Store for it.
The session name must pass the same rules as pad keys, so a session can never escape the scratch base directory.
func (*Store) Close ¶
Close releases the session-directory handle. Every mutation is already durable via write-temp-and-rename, so Close flushes nothing.
func (*Store) Register ¶
func (s *Store) Register(reg *ds4.ToolRegistry) error
Register adds scratch_list, scratch_get, scratch_set, scratch_append, and scratch_delete to reg. It fails when Config.ReadOnly is set, so registered tools are always usable; use RegisterReadOnly instead.
func (*Store) RegisterReadOnly ¶
func (s *Store) RegisterReadOnly(reg *ds4.ToolRegistry) error
RegisterReadOnly adds scratch_list and scratch_get to reg.