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
- Variables
- func CheckBriefPath(p string) error
- func CheckGitHub(s string) error
- func CheckPrompt(p string) error
- func Defaults(templateMD, readme string) (brief, briefFrom, prompt, promptFrom string)
- type CreateRequest
- type Error
- type GHStatus
- type Input
- type Inspection
- type Marker
- type Service
- func (s *Service) FromTemplate(ctx context.Context, req CreateRequest) (projects.Op, bool, error)
- func (s *Service) GH(ctx context.Context) GHStatus
- func (s *Service) Inspect(ctx context.Context, github, folder, defaultFolder string) (Inspection, error)
- func (s *Service) Retry(ctx context.Context, opID string) (projects.Op, error)
- type Source
- type Store
- type Template
Constants ¶
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.
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.
const ( StepCreate = "create" StepClone = "clone" StepCopy = "copy" StepBrief = "brief" StepCommit = "commit" StepAdd = "add" )
The steps of an operation, as Op.Step names them.
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).
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.
const ( MaxTemplates = 50 MaxNameRunes = 80 MaxPrompt = 8 << 10 MaxBriefPath = 256 MaxFolderPath = 4096 )
Limits on the list and on one template.
const DefaultBriefPath = "docs/brief.md"
DefaultBriefPath is where a brief goes when the template says nothing.
const MaxBrief = 1 << 20
MaxBrief is the largest brief, in bytes of UTF-8.
const OpKind = "template"
OpKind is the kind of a template operation.
const StateFile = "project-templates.json"
StateFile is the templates list's name in the data directory.
Variables ¶
var ErrFull = fmt.Errorf("you have %d templates; remove one first", MaxTemplates)
ErrFull is a list already holding MaxTemplates.
var ErrNotFound = errors.New("no such template")
ErrNotFound is an id that names no template.
Functions ¶
func CheckBriefPath ¶
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 CheckPrompt ¶
CheckPrompt accepts a first prompt: some text, at most MaxPrompt bytes, no NUL, and no variable but {brief}.
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 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.
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 ¶
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 ¶
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 (*Service) FromTemplate ¶
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 ¶
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.
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) Delete ¶
Delete removes a template from the list; nothing on disk or on GitHub is touched.
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.