template

package
v0.177.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 14 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 template implements gofer task-book templates (SUP-01 P5, design §六): a markdown file with an optional YAML frontmatter, living in <project>/.gofer/templates/ or <config-dir>/templates/, that a submit renders into a job prompt.

It is a data-layer package — it imports the standard library plus the dependency-free leaf util, nothing else — so the job service (which renders) and the CLI (which previews) can both use it without an import cycle (G022). Rendering is stdlib-only by design: a template is untrusted input from a file tree the operator owns, so a placeholder is either a declared variable, a builtin the caller resolved, or it stays verbatim.

Index

Constants

View Source
const (
	SourceProject = "project"
	SourceGlobal  = "global"
)

Sources of a resolved template (Template.Source / Info.Source).

View Source
const (
	// DirName is the per-project template directory, relative to the project root.
	DirName = ".gofer/templates"
	// GlobalDirName is the template directory under the user config dir.
	GlobalDirName = "templates"
	// Ext is the template file extension.
	Ext = ".md"
)

Directory layout: the project copy lives under the project root, the global one under the user config directory (config.ConfigDir()).

Variables

View Source
var (
	// ErrNotFound is returned by Resolve for a valid name that no directory holds.
	ErrNotFound = errors.New("template not found")
	// ErrInvalidName is returned for a name that is not a bare file name
	// ([A-Za-z0-9_.-]+): the name reaches the filesystem, so a path separator or a
	// parent-dir token is refused instead of resolved.
	ErrInvalidName = errors.New("invalid template name")
)
View Source
var BuiltinNames = []string{
	"project", "cwd", "date", "head",
	"plan_title", "plan_description", "todo_title", "todo_note", "todo_id",
}

BuiltinNames are the placeholders the SERVER resolves itself; a builtin the caller could not resolve ({{head}} outside a git checkout) renders empty with a warning instead of leaking the braces into the prompt.

The plan/todo names (PLAN-02 P2) are resolved by the JOB layer, which owns the checklist: a task book rendered for a todo-attached submit gets them filled, and one rendered without a todo (a plain `job run -t`, a preview) reports them as unavailable rather than printing the braces.

Functions

func Builtins

func Builtins(projectKey, cwd, dir string, now time.Time) map[string]string

Builtins returns the standard placeholder values for one render: project / cwd / date always, head only when dir is inside a git checkout (so an unresolvable {{head}} is reported rather than rendered as a stale or invented sha).

func GlobalDirPath

func GlobalDirPath(globalDir string) string

func GlobalPath

func GlobalPath(globalDir, name string) string
func Head(dir string) string

Head returns the short HEAD sha of the git checkout at dir, or "" when dir is empty, is not a repository, or git is unavailable.

func ProjectDirPath

func ProjectDirPath(projectDir string) string

ProjectDirPath / ProjectPath / GlobalDirPath / GlobalPath build the template locations. Exported so callers (tests, docs tooling, an operator writing a template) do not re-implement the layout.

func ProjectPath

func ProjectPath(projectDir, name string) string

func SplitFrontmatter

func SplitFrontmatter(body []byte) (fm, rest []byte, ok bool)

SplitFrontmatter separates a leading '---' yaml block from the body. It tolerates leading whitespace and \r\n line endings. ok=false when there is no opening '---' or no closing '---' line — the caller then treats the whole input as body. It is the same splitter the md submit path uses, so a task file and a template agree on what frontmatter is.

func ValidName

func ValidName(name string) bool

ValidName reports whether name is a bare template file name: [A-Za-z0-9_.-]+, with "." and ".." refused. The name is joined onto a directory, so a separator, a space or a parent-dir token never reaches the filesystem.

Types

type Budget

type Budget struct {
	MaxTokens  int64   `json:"max_tokens,omitempty" yaml:"max_tokens"`
	MaxCostUSD float64 `json:"max_cost_usd,omitempty" yaml:"max_cost_usd"`
	MaxTurns   int     `json:"max_turns,omitempty" yaml:"max_turns"`
	// AgentTokens / AgentCostUSD are the soft budget handed to the agent itself.
	AgentTokens  int64   `json:"agent_tokens,omitempty" yaml:"agent_tokens"`
	AgentCostUSD float64 `json:"agent_cost_usd,omitempty" yaml:"agent_cost_usd"`
}

Budget mirrors config.Budget (this package imports nothing of gofer's): the task book's default spend ceiling, 0 = no limit on that dimension.

type Info

type Info struct {
	Name   string         `json:"name"`
	Source string         `json:"source"`
	Path   string         `json:"path"`
	Desc   string         `json:"desc,omitempty"`
	Meta   Meta           `json:"meta,omitempty"`
	Vars   map[string]Var `json:"vars,omitempty"`
	Err    string         `json:"error,omitempty"`
}

Info is one listing entry (List). Err carries the parse error of a template that exists but is broken — reported, never silently hidden: that is the file a reader is looking for when a submit fails.

func List

func List(projectDir, globalDir string) []Info

List enumerates the templates both directories hold, sorted by name, with the project copy shadowing the global one of the same name. A directory that does not exist contributes nothing (a machine may have no templates at all).

type Meta

type Meta struct {
	Agent  string `json:"agent,omitempty" yaml:"agent"`
	Runner string `json:"runner,omitempty" yaml:"runner"`
	Model  string `json:"model,omitempty" yaml:"model"`
	// Budget is the task book's default spend ceiling (N2 §B): fills the dimensions the
	// request leaves unset, below the request and above the agent/project defaults.
	Budget           *Budget  `json:"budget,omitempty" yaml:"budget"`
	TimeoutSec       int      `json:"timeout_sec,omitempty" yaml:"timeout_sec"`
	Tags             []string `json:"tags,omitempty" yaml:"tags"`
	Verify           []string `json:"verify,omitempty" yaml:"verify"`
	VerifyTimeoutSec int      `json:"verify_timeout_sec,omitempty" yaml:"verify_timeout_sec"`
	Review           bool     `json:"review,omitempty" yaml:"review"`
	ReadOnly         bool     `json:"read_only,omitempty" yaml:"read_only"`
	Worktree         bool     `json:"worktree,omitempty" yaml:"worktree"`
	FallbackAgents   []string `json:"fallback_agents,omitempty" yaml:"fallback_agents"`
}

Meta is the whitelisted set of job defaults a frontmatter may preset. It maps 1:1 onto job.JobRequest fields; anything else in the frontmatter is rejected at parse time, so a task book cannot smuggle submit fields (caller, plan_id, cmd) past the server's own resolution.

type Preview

type Preview struct {
	Template
	Render Rendered `json:"render"`
}

Preview is a template plus a render of it: what the read-only preview surfaces (CLI `template show`, the console's submit form) show a reader. Rendering happens on the SERVER because only the server can expand {{include: …}} and only the server knows the {{head}} of the project it will actually run in — a client-side preview would drift from the prompt the submit produces.

type Rendered

type Rendered struct {
	Prompt   string   `json:"prompt"`
	Missing  []string `json:"missing,omitempty"`
	Warnings []string `json:"warnings,omitempty"`
}

Rendered is the outcome of one render: the prompt, the REQUIRED variables no value was supplied for (the caller rejects the submit), and the warnings a human should see (an undeclared placeholder kept verbatim, an unresolvable builtin, a nested include left unexpanded).

func Render

func Render(tpl Template, vars, builtins map[string]string) (Rendered, error)

Render substitutes the declared variables (a supplied value beats the declared default; a REQUIRED variable with neither is reported in Missing, sorted), the builtins, and one level of same-directory {{include: file.md}}.

A value the CALLER supplied is substituted even when the template did not declare that name — the submitter is naming a value for the very task book in front of them. A placeholder nobody resolved stays verbatim with a warning: an undeclared {{x}} is a typo, not an instruction to erase the text.

type Template

type Template struct {
	Name   string         `json:"name,omitempty"`
	Source string         `json:"source,omitempty"`
	Path   string         `json:"path,omitempty"`
	Desc   string         `json:"desc,omitempty"`
	Meta   Meta           `json:"meta,omitempty"`
	Vars   map[string]Var `json:"vars,omitempty"`
	Body   string         `json:"body,omitempty"`
}

Template is one parsed task book. Name/Source/Path are filled in by Resolve (and by ParseFile, which sets the two of them it can see); Parse itself leaves them empty for a source that is not a file.

func Parse

func Parse(src []byte) (Template, error)

Parse parses template source: the optional frontmatter (whitelisted job defaults + `desc` + `vars`) and the prose body. Unknown frontmatter keys are an error.

func ParseFile

func ParseFile(path string) (Template, error)

ParseFile parses one template file and fills the Name/Path it can derive. A read or parse failure carries the path, so a broken template names itself.

func Resolve

func Resolve(name, projectDir, globalDir string) (Template, error)

Resolve finds the template named name: the project copy (<projectDir>/.gofer/templates/<name>.md) first, then the global one (<globalDir>/templates/<name>.md). An empty projectDir means the project is not readable on this host (a worker-only project) and only the global directory is consulted. A template that exists but does not parse is an error — it is never silently shadowed by the other copy.

type Var

type Var struct {
	Default  string `json:"default,omitempty" yaml:"default"`
	Required bool   `json:"required,omitempty" yaml:"required"`
	Desc     string `json:"desc,omitempty" yaml:"desc"`
}

Var is one declared template variable (frontmatter `vars:`). A variable with no Default and Required:true must be supplied by the submitter.

Jump to

Keyboard shortcuts

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