write

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 25 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package write changes files in a vault: create, append, patch frontmatter, edit text, replace. Each op, built next to its transform in its own file, works out a file's new content; the batch engine (batch.go) holds every op to the document rules, replaces each changed file atomically and makes at most one commit.

Index

Constants

View Source
const (
	ToolCreate  = "create_doc"
	ToolAppend  = "append_section"
	ToolPatch   = "update_frontmatter"
	ToolReplace = "replace_doc"
	ToolEdit    = "edit_text"
)

The write tools an Op stands for, as named in commit messages, errors and the server's tool list.

View Source
const MaxBatchOps = 200

Variables

This section is empty.

Functions

This section is empty.

Types

type BatchResult

type BatchResult struct {
	Results  []Result `json:"results"`
	Commit   string   `json:"commit,omitempty"`
	Warnings []string `json:"warnings"`
}

BatchResult is what Batch returns: a Result per op, in op order, and the one commit with its warnings. A Result's Hash is its file's hash after the batch.

type ConflictError

type ConflictError struct{ Path, Current string }

ConflictError means replace_doc saw a file that changed since it was read.

func (*ConflictError) Error

func (e *ConflictError) Error() string

type Draft

type Draft struct {
	Type        string     // a configured type
	Frontmatter *yaml.Node // a mapping in the order to write it; nil for none
	Body        string
	Slug        string // fills {slug} in the filename; empty means the title field
}

type Git

type Git struct {
	Dir, Name, Email string
	Bin              string        // default: git on the PATH
	Timeout          time.Duration // for the whole commit, not per git command; zero means 10 s
}

Git commits written files in the vault folder Dir, as Name <Email>, so every AI change can be reviewed (git log --author) and reverted. It shells out to the git binary and never pushes.

func (*Git) Commit

func (g *Git) Commit(ctx context.Context, paths []string, msg string) (hash, warning string)

Commit commits paths (slash-separated, relative to Dir) with message msg. It only commits when Dir is the root of its git repository, and only those paths, so staged work by a human is left alone. It never fails a write: every problem comes back as warning, and then hash is empty.

Root only, because a vault inside a bigger repository, like examples/vault in this one, must never receive AI commits. A commit covers whole files, so a human's uncommitted edit to a written file goes into the AI's commit.

type Op

type Op struct {
	// contains filtered or unexported fields
}

Op is one write: the tool it stands for, what it targets (a path, or the type for create_doc) and the new content it works out for one file. Build it with NewCreateOp, NewAppendOp, NewPatchOp, NewReplaceOp, NewEditOp or NewFailedOp, and run it with Apply or Batch.

func NewAppendOp

func NewAppendOp(path, heading, content string) Op

NewAppendOp returns the op that adds content to the end of a "## heading" section, or adds the section at the end of the file.

func NewCreateOp

func NewCreateOp(d Draft) Op

NewCreateOp returns the op that makes a new document of a configured type. The path comes from the type's folder and filename pattern.

func NewEditOp

func NewEditOp(path, old, replacement string) Op

NewEditOp returns the op that replaces the one occurrence of old in a file with replacement. An empty replacement deletes it.

func NewFailedOp

func NewFailedOp(tool string, err error) Op

NewFailedOp returns the op of a tool call the caller could not build, for example because its arguments did not decode. Apply and Batch report err as its failure.

func NewPatchOp

func NewPatchOp(path string, patch Patch) Op

NewPatchOp returns the op that changes frontmatter keys and tags, leaving the body's bytes alone.

func NewReplaceOp

func NewReplaceOp(path, content, expectedHash string) Op

NewReplaceOp returns the op that rewrites a whole file, but only if it still has the content whose hash the caller read.

type Patch

type Patch struct {
	Set                        *yaml.Node // ordered mapping of keys to set; nil for none
	Unset, AddTags, RemoveTags []string
}

type Result

type Result struct {
	Path     string   `json:"path"`
	Hash     string   `json:"hash"`
	Warnings []string `json:"warnings"`
	Commit   string   `json:"commit,omitempty"`
}

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer applies safe changes to one vault. Writes are serialised, since tool calls may overlap; every write goes through the batch engine (batch.go, Apply and Batch), and every file is replaced atomically. A nil Git means no commits and no commit warnings: main passes nil when commits are off, and tests do, so they do not depend on git state.

func New

func New(v *vault.Vault, cfg *config.Config, g *Git, now func() time.Time) *Writer

New builds a Writer. now is the clock for created and updated dates.

func (*Writer) Apply

func (w *Writer) Apply(ctx context.Context, op Op) (*Result, error)

Apply runs one op and commits it as "<tool>: <path>". Its errors are the op's own, unlabelled.

func (*Writer) Batch

func (w *Writer) Batch(ctx context.Context, ops []Op, message string) (*BatchResult, error)

Batch applies ops in order, all or nothing, as one commit. If any op fails its checks nothing is written and every failure is reported. message is the commit message; empty means one counting the ops per tool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL