templates

package
v0.149.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 29 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 templates starts a project from a template (ADR-065): a GitHub template repository the person names, or a folder on this machine, copied into a new repository with the person's brief written into it, ready for an agent. The templates are the person's own list, empty until they add one; nothing is built in.

GitHub is reached only through the person's own `gh`, on a click: Caprock never reads or keeps the gh token, and never deletes a repository it made. Without a ready gh the local path copies the template folder instead; the daemon never picks the other path on its own.

Index

Constants

View Source
const (
	MaxCopyFiles = wtsetup.MaxFiles
	MaxCopyBytes = wtsetup.MaxBytes
)

The local copy's limits, ADR-050's: past either the copy stops and the project is not made.

View Source
const (
	ViaGitHub = "github" // gh repo create --template, then gh repo clone
	ViaLocal  = "local"  // the template folder's last commit, copied, then git init
)

The two ways a project is made from a template. The request names one; the daemon never takes the other on its own.

View Source
const (
	StepCreate = "create"
	StepClone  = "clone"
	StepCopy   = "copy"
	StepBrief  = "brief"
	StepCommit = "commit"
	StepAdd    = "add"
)

The steps of an operation, as Op.Step names them.

View Source
const (
	FromMarker  = "marker"  // the caprock-template comment at the top of TEMPLATE.md
	FromMention = "mention" // the first `docs/<x>.md` TEMPLATE.md or README.md names
	FromDefault = "default"
)

Where a template's brief path and first prompt came from (inspect).

View Source
const (
	DefaultPromptWithTemplateMD = "Read TEMPLATE.md and follow it. The brief of this project is {brief}."
	DefaultPrompt               = "Read {brief}, the brief of this project, and the README, then start the work as they say."
)

Default first prompts: one for a template that carries instructions for the agent (TEMPLATE.md), one for a template that does not.

View Source
const (
	MaxTemplates  = 50
	MaxNameRunes  = 80
	MaxPrompt     = 8 << 10
	MaxBriefPath  = 256
	MaxFolderPath = 4096
)

Limits on the list and on one template.

View Source
const DefaultBriefPath = "docs/brief.md"

DefaultBriefPath is where a brief goes when the template says nothing.

View Source
const MaxBrief = 1 << 20

MaxBrief is the largest brief, in bytes of UTF-8.

View Source
const OpKind = "template"

OpKind is the kind of a template operation.

View Source
const StateFile = "project-templates.json"

StateFile is the templates list's name in the data directory.

Variables

View Source
var ErrFull = fmt.Errorf("you have %d templates; remove one first", MaxTemplates)

ErrFull is a list already holding MaxTemplates.

View Source
var ErrNotFound = errors.New("no such template")

ErrNotFound is an id that names no template.

Functions

func CheckBriefPath

func CheckBriefPath(p string) error

CheckBriefPath accepts where a brief may be written in a new project: a relative path with forward slashes, inside the project, not into .git, each segment a name every OS can make.

func CheckGitHub

func CheckGitHub(s string) error

CheckGitHub accepts owner/name.

func CheckPrompt

func CheckPrompt(p string) error

CheckPrompt accepts a first prompt: some text, at most MaxPrompt bytes, no NUL, and no variable but {brief}.

func Defaults

func Defaults(templateMD, readme string) (brief, briefFrom, prompt, promptFrom string)

Defaults works out a template's brief path and first prompt from its TEMPLATE.md and README.md, and says where each came from: (1) the marker comment; (2) for the brief, the first `docs/<x>.md` TEMPLATE.md names, then README.md; (3) docs/brief.md and a default prompt.

Types

type CreateRequest

type CreateRequest struct {
	OpID         string `json:"op_id"`
	Source       Source `json:"source"`
	Via          string `json:"via"`
	Name         string `json:"name"`
	Parent       string `json:"parent"`
	Visibility   string `json:"visibility"` // private (default) | public; GitHub only
	Brief        string `json:"brief"`
	BriefPath    string `json:"brief_path"`
	TemplateName string `json:"template_name"`
}

CreateRequest is POST /v1/projects/from-template.

type Error

type Error struct {
	Status int
	Msg    string
}

Error is a refusal with the HTTP status it answers with.

func (*Error) Error

func (e *Error) Error() string

type GHStatus

type GHStatus struct {
	Installed bool   `json:"installed"`
	LoggedIn  bool   `json:"logged_in"`
	Login     string `json:"login,omitempty"`
	Error     string `json:"error,omitempty"`
}

GHStatus is whether the person's own GitHub CLI can make a repository: installed, and signed in to github.com as login. Read from gh's own config on this machine; nothing is sent and the token is never read.

func (GHStatus) Ready

func (g GHStatus) Ready() bool

Ready reports whether the GitHub path can be taken.

type Input

type Input struct {
	Name      *string `json:"name,omitempty"`
	GitHub    *string `json:"github,omitempty"`
	Folder    *string `json:"folder,omitempty"`
	BriefPath *string `json:"brief_path,omitempty"`
	Prompt    *string `json:"prompt,omitempty"`
	Agent     *string `json:"agent,omitempty"`
}

Input is a template as a client sends it: every field optional, so a PATCH changes only what it names. "" clears github, folder and agent.

type Inspection

type Inspection struct {
	GitHub     string `json:"github,omitempty"` // canonical owner/name (GitHub follows a renamed one)
	Folder     string `json:"folder,omitempty"`
	IsTemplate *bool  `json:"is_template,omitempty"`
	Visibility string `json:"visibility,omitempty"` // public | private | internal
	TemplateMD string `json:"template_md"`          // its TEMPLATE.md, at most 64 KiB; "" when it has none
	BriefPath  string `json:"brief_path"`
	BriefFrom  string `json:"brief_from"` // marker | mention | default
	Prompt     string `json:"prompt"`
	PromptFrom string `json:"prompt_from"` // marker | default
	// LocalFolder is a clone of the template already on this machine, in
	// the default folder: what the local path would copy.
	LocalFolder   string `json:"local_folder,omitempty"`
	SuggestedName string `json:"suggested_name"`
	Error         string `json:"error,omitempty"`
}

Inspection is what a template says about itself, read when the person adds one, so the form opens filled in.

type Marker

type Marker struct {
	Brief  string
	Prompt string
}

Marker is what a template says about itself in a comment at the very top of its TEMPLATE.md:

<!-- caprock-template
brief: docs/source-spec.md
prompt: Read TEMPLATE.md. Build the .ai/ corpus from {brief}.
-->

Either key may be missing. A line that names no key continues the prompt.

func ParseMarker

func ParseMarker(md string) (Marker, bool)

ParseMarker reads the marker comment, if TEMPLATE.md starts with one. A brief path or prompt that would not pass CheckBriefPath or CheckPrompt is left out, so the next default applies.

type Service

type Service struct {
	Store    *Store
	Projects *projects.Service
	Log      *slog.Logger
	// Env is the login shell's environment, so gh, git, ssh keys and git's
	// identity are found as in a terminal; os.Environ when nil.
	Env func() []string
	// GHBin is gh's path (tests); found on the login PATH when empty.
	GHBin string
	// CloneWait is the pause before cloning a fresh repository again (2 s).
	CloneWait time.Duration
	// Ctx is the daemon's life: an operation outlives its request.
	Ctx context.Context
	// contains filtered or unexported fields
}

Service makes projects from templates.

func New

func New(st *Store, projs *projects.Service, log *slog.Logger) *Service

New is a Service over a store and the projects list.

func (*Service) FromTemplate

func (s *Service) FromTemplate(ctx context.Context, req CreateRequest) (projects.Op, bool, error)

FromTemplate checks a request and starts making the project, returning the operation at once. An op_id already known returns that operation with existing true and starts nothing.

func (*Service) GH

func (s *Service) GH(ctx context.Context) GHStatus

GH reads whether gh is installed and signed in, from gh's own config on this machine (`gh config get -h github.com user`): nothing goes to GitHub.

func (*Service) Inspect

func (s *Service) Inspect(ctx context.Context, github, folder, defaultFolder string) (Inspection, error)

Inspect reads a template — exactly one of github (owner/name) or folder — and works out its defaults. defaultFolder is where a local clone is looked for and the suggested name is free ("" is the home folder). A GitHub template is read through the person's own gh on this click: `gh repo view` and the raw TEMPLATE.md and README; when gh cannot, Error says why and a local clone, if there is one, is read instead.

func (*Service) Retry

func (s *Service) Retry(ctx context.Context, opID string) (projects.Op, error)

Retry runs a failed, retryable operation again from the step it failed at; a GitHub repository already made is cloned, not made again.

type Source

type Source struct {
	GitHub string `json:"github,omitempty"`
	Folder string `json:"folder,omitempty"`
}

Source is the template a project is made from.

type Store

type Store struct {
	// File is where the list is kept; "" keeps it in memory (tests).
	File string
	Log  *slog.Logger
	Now  func() time.Time
	// contains filtered or unexported fields
}

Store is the person's templates, kept in one JSON file.

func (*Store) Add

func (s *Store) Add(in Input) (Template, error)

Add adds a template.

func (*Store) Delete

func (s *Store) Delete(id string) error

Delete removes a template from the list; nothing on disk or on GitHub is touched.

func (*Store) Get

func (s *Store) Get(id string) (Template, bool)

Get is one template.

func (*Store) List

func (s *Store) List() []Template

List is every template, in the order they were added.

func (*Store) MarkUsed

func (s *Store) MarkUsed(name string)

MarkUsed stamps used_at on the template called name, if there is one.

func (*Store) Update

func (s *Store) Update(id string, in Input) (Template, error)

Update changes what in names.

type Template

type Template struct {
	ID        string `json:"id"`
	Name      string `json:"name"`
	GitHub    string `json:"github,omitempty"` // owner/name of a template repository
	Folder    string `json:"folder,omitempty"` // a folder on this machine
	BriefPath string `json:"brief_path"`       // where the brief is written, relative, forward slashes
	Prompt    string `json:"prompt"`           // the first prompt; {brief} is the brief's path
	Agent     string `json:"agent,omitempty"`
	CreatedAt int64  `json:"created_at"`
	UsedAt    int64  `json:"used_at,omitempty"`
}

Template is one way to start a project.

func Check

func Check(t Template) (Template, error)

Check validates a whole template and returns it normalised.

Jump to

Keyboard shortcuts

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