agent

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 10 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 agent knows the coding agents a launch can start, Claude Code and OpenAI Codex, and checks the options a launch passes them, so every place that stores those options (profiles, prompts, and launch defaults) and the launcher that passes them apply the same rules.

Each agent is a backend spec: its command, the environment variable that points it at a profile's config directory, the entries that mark such a directory, its effort levels, model suggestions, and permission modes, the flags the launcher sets itself (and so may not be added by hand), and how a launch's model, effort, access, extra directories, and session name become its command-line flags. Adding a backend adds a spec here; the launcher, the profiles, and the editors read everything else from it.

It also reads extra flags typed on one line, quoted the way a shell would, into one argument each. CheckOverrides and CheckSettings check a model, an effort level, and extra flags the way the prompt and launch-default editors show them, as FieldErrors.

The package is pure: it reads no files and runs nothing.

Index

Constants

View Source
const (
	FieldModel      = "model"
	FieldModels     = "models"
	FieldEffort     = "effort"
	FieldExtraFlags = "extraFlags"
)

Fields a FieldError from CheckOverrides and CheckSettings names, matching the editors' JSON names.

Variables

View Source
var (
	// ErrInvalidEffort is returned for an effort level the agent does not accept.
	ErrInvalidEffort = errors.New("unknown effort level")

	// ErrInvalidModel is returned for a model name the agent could misread.
	ErrInvalidModel = errors.New("invalid model name")

	// ErrReservedFlag is returned for an extra flag the launcher sets itself.
	ErrReservedFlag = errors.New("reserved flag")

	// ErrEmptyFlag is returned for an empty extra argument.
	ErrEmptyFlag = errors.New("empty argument")

	// ErrNULByte is returned for an argument holding a NUL byte, which no process
	// argument can carry.
	ErrNULByte = errors.New("value contains a NUL byte")

	// ErrTilde is returned for an argument that starts with ~: launch scripts quote every
	// argument, so no shell expands it.
	ErrTilde = errors.New("~ is not expanded")

	// ErrUnterminatedQuote is returned by Split for a quote that is never closed.
	ErrUnterminatedQuote = errors.New("unterminated quote")

	// ErrEndOfFlags is returned for a "--" in extra arguments: everything after it would
	// be read as the prompt.
	ErrEndOfFlags = errors.New(`"--" can't be added: everything after it would be read as the prompt`)
)
View Source
var ErrInvalidAccess = errors.New("unknown access")

ErrInvalidAccess is returned for an access kind this build does not know.

View Source
var ErrInvalidPermissionMode = errors.New("unknown permission mode")

ErrInvalidPermissionMode is returned for a permission mode the agent does not accept.

View Source
var ErrInvalidSessionID = errors.New("invalid session ID")

ErrInvalidSessionID is returned for a session ID that is not a UUID, or for a launch that both starts a session with an ID and resumes one.

View Source
var ErrUnknownAgent = errors.New("unknown coding agent")

ErrUnknownAgent is returned for an agent ID this build does not know.

Functions

func AnyEffortLevels

func AnyEffortLevels() []string

AnyEffortLevels returns every effort level some agent accepts, lowest first.

func Args

func Args(id ID, o Options) ([]string, error)

Args returns the flags a launch passes the agent with id, in order: model, effort, access, extra directories, extra arguments, session name, and the session to start or resume. It refuses a model or effort level the agent would not accept, a permission mode it does not have, and a session ID that is not one.

func CheckAccess

func CheckAccess(id ID, args []string) error

CheckAccess returns the first of args that sets a permission switch of the agent with id, as a ReservedFlagError, or nil. Permissions come only from the launcher's own policy, so an outside contributor's item can never be launched with more access than it allows, whatever a profile's extra arguments say.

func CheckExtra

func CheckExtra(id ID, args []string) error

CheckExtra checks extra arguments added by a profile, a prompt, or a launch default for the agent with id. Each must be non-empty and free of NUL bytes, must not start with ~ or give a flag a value that does (--add-dir=~/x), since nothing expands it, must not be "--", and must not set a flag the launcher owns (see ReservedFlagError), including a config override of a value the launcher sets, such as Codex's -c model=<name>.

func CheckSettings

func CheckSettings(in Settings) (Checked, []FieldError)

CheckSettings checks Settings the way the prompt and launch-default editors show them. It returns them ready to store, and a FieldError for each problem: an agent this build does not know (on FieldModels or FieldExtraFlags), a model the agent would misread or flags it cannot take (on FieldFor(FieldModels, id) or FieldFor(FieldExtraFlags, id)), and an effort level no agent accepts (on FieldEffort).

func CloneArgs

func CloneArgs(m map[ID][]string) map[ID][]string

CloneArgs returns a deep copy of each agent's extra arguments with the empty lists left out; never nil.

func CloneModels

func CloneModels(m map[ID]string) map[ID]string

CloneModels returns each agent's model with the blank ones left out; never nil.

func EffortLevels

func EffortLevels(id ID) []string

EffortLevels returns the effort levels the agent with id accepts, lowest first, or nil for an agent this build does not know.

func EqualArgs

func EqualArgs(a, b map[ID][]string) bool

EqualArgs reports whether two agents' extra arguments are the same, an empty list and no entry being the same.

func EqualModels

func EqualModels(a, b map[ID]string) bool

EqualModels reports whether two agents' models are the same, a blank model and no entry being the same.

func FieldFor

func FieldFor(field string, id ID) string

FieldFor names one agent's entry of a per-agent field, as in "models.codex".

func Join

func Join(args []string) string

Join writes arguments on one line that Split reads back unchanged, each as textutil.ShellWord writes it: as is when a shell reads it as itself, as --add-dir=/srv/x is, and single-quoted otherwise.

func JoinArgs

func JoinArgs(m map[ID][]string) map[ID]string

JoinArgs writes each agent's extra arguments on one line, as the editors show them; never nil.

func Known

func Known(id ID) bool

Known reports whether id names an agent; Inherit names none.

func PromptLast

func PromptLast(id ID) bool

PromptLast reports whether the agent's prompt follows its flags, after "--", rather than coming first. An unknown agent reports false.

func Split

func Split(line string) ([]string, error)

Split reads extra flags typed on one line into one argument each, the way a POSIX shell splits words, but without expanding anything: whitespace separates arguments, single quotes keep everything up to the next single quote, double quotes keep everything but an escaped \" or \\, and a backslash outside quotes keeps the next character. Two single quotes in a row make an empty argument. An unclosed quote is an ErrUnterminatedQuote. Split(Join(args)) returns args.

func ValidPermissionMode

func ValidPermissionMode(id ID, mode string) bool

ValidPermissionMode reports whether mode is one of the agent's permission modes.

func ValidSessionID

func ValidSessionID(id string) bool

ValidSessionID reports whether id can be passed as a session ID: a UUID (see sessionIDPattern).

func ValidateEffort

func ValidateEffort(id ID, effort string) error

ValidateEffort accepts "" (the agent's default) or one of the agent's EffortLevels.

func ValidateEffortAny

func ValidateEffortAny(effort string) error

ValidateEffortAny accepts "" or a level some agent accepts, for an effort shared by every agent, as prompts and launch defaults store it.

func ValidateModel

func ValidateModel(model string) error

ValidateModel accepts "" (the agent's default) or a model alias or full name: one word of printable characters, at most 100 long, that does not start with "-", so the agent can never read it as a flag.

Types

type Access

type Access struct {
	Kind AccessKind
	Mode string
}

Access is a launch's permissions: a kind, and for AccessMode, the mode.

type AccessKind

type AccessKind string

AccessKind is what a session may do without asking, as the launcher's trust policy decides it. Each agent turns it into its own flags.

const (
	// AccessDefault passes no permission flag, so the agent's own settings apply.
	AccessDefault AccessKind = ""

	// AccessMode starts the session in one of the agent's permission modes (Access.Mode).
	AccessMode AccessKind = "mode"

	// AccessFull starts the session with full access: no prompts and no sandbox.
	AccessFull AccessKind = "full"

	// AccessPlan starts the session planning only: it reads, and changes nothing.
	AccessPlan AccessKind = "plan"

	// AccessPlanAllowFull starts the session planning, and lets it switch to full access
	// once the plan is accepted, where the agent can.
	AccessPlanAllowFull AccessKind = "plan-allow-full"
)

Access kinds.

type Agent

type Agent struct {
	// ID is what profiles and launch defaults store, such as "claude".
	ID ID `json:"id"`

	// Name is what the UI shows, as in "Open in Claude".
	Name string `json:"name"`

	// Command is the program a launch script runs.
	Command string `json:"command"`

	// ConfigEnv is the environment variable that points the agent at a profile's config
	// directory, such as CLAUDE_CONFIG_DIR.
	ConfigEnv string `json:"configEnv"`

	// ConfigDir is the agent's default config directory under the home directory, such
	// as ".claude". A sibling named ConfigDir_<name> or ConfigDir-<name> is profile
	// <name>.
	ConfigDir string `json:"configDir"`

	// Markers are the entries that mark the agent's config directory.
	Markers []Marker `json:"markers"`

	// EffortLevels are the effort levels the agent accepts, lowest first.
	EffortLevels []Choice `json:"effortLevels"`

	// Models suggests model names; any model name the agent accepts works too.
	Models []Choice `json:"models"`

	// PermissionModes are the permission modes a profile may set, besides full access.
	PermissionModes []Choice `json:"permissionModes"`

	// PlanLabel names the agent's planning mode, which launches use for outside
	// contributors' items: "Plan mode" or "Read-only".
	PlanLabel string `json:"planLabel"`

	// SessionNames reports that a launch can name the session.
	SessionNames bool `json:"sessionNames"`

	// SessionIDs reports that a launch passes the agent the session's ID, so the app
	// follows the session from its start. Without it, the app finds the agent's own ID
	// once the session starts.
	SessionIDs bool `json:"sessionIds"`

	// FastMode reports that a launch can start the session in the agent's Fast mode.
	FastMode bool `json:"fastMode"`

	// ConfigOverrides reports an agent that takes the effort level and Fast mode only as
	// config overrides, and runs a session started with any outside its shared app
	// server, where the app can't see that it waits for the user. A launch passes it the
	// effort only when the user's settings say to.
	ConfigOverrides bool `json:"configOverrides"`

	// AuthFile is the file in a config directory that holds the agent's sign-in, or ""
	// when the app does not check the agent's sign-in. Only its presence is checked; it is
	// never read.
	AuthFile string `json:"authFile"`

	// AuthEnv lists environment variables that sign the agent in with a key instead.
	AuthEnv []string `json:"authEnv"`

	// ModelsCache is the file in a config directory where the agent keeps the models it
	// offers, or "" when it keeps none the app reads.
	ModelsCache string `json:"modelsCache"`
}

Agent is a coding agent the launcher can start. Every field is a plain value.

func All

func All() []Agent

All returns every agent, the default first. Each call returns fresh copies.

func Default

func Default() Agent

Default returns the agent used when nothing names one.

func Lookup

func Lookup(id ID) (Agent, error)

Lookup returns the agent with id, or the default agent for Inherit.

type Checked

type Checked struct {
	Models map[ID]string
	Args   map[ID][]string
}

Checked is Settings ready to store: the models that are set, trimmed, and the extra arguments of each agent that has any. Neither map is nil.

type Choice

type Choice struct {
	Value string `json:"value"`
	Label string `json:"label"`
}

Choice is one value an agent setting can take, with what the UI calls it.

type FieldError

type FieldError struct {
	// Field is the field's JSON name, such as "model".
	Field string `json:"field"`

	// Message is a sentence saying what is wrong and how to fix it.
	Message string `json:"message"`
}

FieldError is a problem with one field of something being saved.

func CheckOverrides

func CheckOverrides(id ID, model, effort, flags string) ([]string, []FieldError)

CheckOverrides checks the launch settings a prompt or a launch default may set for the agent with id: the model, the effort level, and the extra flags typed on one line. It returns the extra flags as arguments, or nil with a FieldError when they cannot be read or used, and a FieldError for a model or effort level the agent would not accept.

type ID

type ID string

ID names a coding agent, as profiles, settings, and launch defaults store it.

const (
	// Inherit is no choice of agent: a launch default or a launch that leaves the choice
	// to the next layer.
	Inherit ID = ""

	// Claude is Claude Code, the default agent.
	Claude ID = "claude"

	// Codex is OpenAI Codex.
	Codex ID = "codex"
)

The coding agents, and the empty choice.

func IDs

func IDs() []ID

IDs returns every agent's ID, the default first.

type Marker

type Marker struct {
	// Name is the entry's name, such as "settings.json".
	Name string `json:"name"`

	// Dir reports a directory rather than a file.
	Dir bool `json:"dir"`
}

Marker is an entry an agent writes into a config directory it runs with. A directory that holds one of an agent's markers is that agent's config directory.

type Options

type Options struct {
	// Model and Effort are "" for the agent's own default.
	Model  string
	Effort string

	// Access is the session's permissions.
	Access Access

	// AddDirs are extra directories the session may use, as absolute paths.
	AddDirs []string

	// Extra are extra arguments, one argv element each, passed as they are.
	Extra []string

	// SessionName names the session, or is "". An agent without SessionNames ignores it.
	SessionName string

	// SessionID is the ID the app minted for a new session, or "". An agent without
	// SessionIDs ignores it.
	SessionID string

	// ResumeID is the agent's own ID of a session to resume instead of starting one, or
	// "". A session resumed takes no prompt.
	ResumeID string

	// Fast starts the session in the agent's Fast mode. An agent without FastMode
	// ignores it.
	Fast bool
}

Options are what a launch passes an agent, besides its prompt. The launcher has checked the directories and the extra arguments.

type ReservedFlagError

type ReservedFlagError struct {
	// Flag is the option the argument sets, such as "--model" or "-w", without any value,
	// or for a config override, the option and its key, such as "-c model".
	Flag string

	// Reason completes the sentence "<flag> ...": what to do instead.
	Reason string

	// Access reports a flag that changes the session's permissions.
	Access bool
}

ReservedFlagError is returned for an extra flag the launcher sets itself. Its message says which setting owns the flag.

func (*ReservedFlagError) Error

func (e *ReservedFlagError) Error() string

Error reads like "--model is set by the Model setting".

func (*ReservedFlagError) Is

func (e *ReservedFlagError) Is(target error) bool

Is makes a ReservedFlagError match ErrReservedFlag.

type Settings

type Settings struct {
	// Models is each agent's model.
	Models map[ID]string

	// Effort is the effort level, shared by every agent; an agent that lacks it is
	// launched with its own default.
	Effort string

	// ExtraFlags is each agent's extra arguments typed on one line, quoted as in a shell.
	ExtraFlags map[ID]string
}

Settings are the launch settings a prompt or a launch default stores: one effort level for every agent, and each agent's own model and extra flags. A setting that is "" (or an agent with no entry) inherits.

type ValidationError

type ValidationError struct {
	Err    error
	Fields []FieldError
}

ValidationError lists why something cannot be saved, one FieldError per problem. It matches Err, the error naming what could not be saved, such as "the prompt can't be saved".

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error reads like "<Err>: <each field's message>".

func (*ValidationError) Unwrap

func (e *ValidationError) Unwrap() error

Unwrap returns Err, so errors.Is matches it.

Jump to

Keyboard shortcuts

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