agent

package
v0.3.17 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 19 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 describes the coding agents Flockdeck can run in a pane.

A pane is either a shell or an agent, and an agent is a Spec -- a program to run, or an API to talk to -- together with the model it was asked for. Every Claude-specific decision Flockdeck once made in line is a field here, so that a second agent is a table entry rather than another branch.

Index

Constants

View Source
const (
	// RoutingOff routes nothing and shows nothing. It is what a file that
	// says nothing about routing means.
	RoutingOff = "off"
	// RoutingSuggest pre-fills a routed choice and marks it, for the user to
	// confirm or change before anything starts.
	RoutingSuggest = "suggest"
	// RoutingAuto applies a routed choice and shows it, to be overridden after
	// the fact.
	RoutingAuto = "auto"
)

The routing modes.

View Source
const (
	TierSmall = "small"
	TierMid   = "mid"
	TierTop   = "top"
)

The tiers a model can be in, from the least capable to the most.

View Source
const ConfigName = "agents.json"

ConfigName is the file, in Flockdeck's state directory, where the user's own agents and defaults live. It is read fresh every time the catalog is asked for, so editing it by hand takes effect without a restart.

View Source
const ConfigVersion = 1

ConfigVersion is the schema version written into that file. Nothing rejects a file for its version: an agent list is additive, and refusing to start with somebody's agents because a newer build wrote a 2 into it would be a poor trade for a field nothing reads.

View Source
const DefaultAgentID = "claude"

DefaultAgentID is the agent a pane gets when nothing -- not the pane, not the project, not the user's defaults -- has said which one it wants. It is Claude because that is what every pane was before agents were a choice, and a saved layout from that build has to come back exactly as it went away.

View Source
const FallbackKeyEnv = "FLOCKDECK_API_KEY"

FallbackKeyEnv is Flockdeck's own variable, which any API agent reads a key from when nothing of its own holds one.

View Source
const KeysName = "keys.json"

KeysName is the file internal/creds keeps API keys in. It is named here only so that availability can ask whether one is set before creds exists.

View Source
const OpenAICompatibleID = "openai-compatible"

OpenAICompatibleID is the built-in entry for an OpenAI-compatible endpoint, which ships with no address and is no use until it is given one.

Variables

View Source
var KeyProbe = keyIsSet

KeyProbe reports whether an API agent has a key available. It is a variable because key resolution belongs to internal/creds, which imports this package for Spec and so cannot be imported back from it; creds installs its own from an init if it wants to. The default below asks the same two questions creds does -- the environment, then the key store -- but only ever asks whether there is a key, never what it is.

Functions

func Available

func Available(s Spec) bool

Available reports whether a spec can be started here, remembering the answer for probeTTL.

func BuildArgv

func BuildArgv(s Spec, resume bool, t Tokens) []string

BuildArgv turns a Spec into the argv for one pane. The first element is the program; resolving it on PATH is the caller's job.

An argument that is exactly "{{prompt}}" and expands to something beginning with a dash is preceded by "--", because the opening task is free to begin with one -- "-p is not what I meant, use ..." -- and without it the CLI reads the task as an option it does not have and the pane dies on the spot.

func CheckBaseURL added in v0.2.10

func CheckBaseURL(baseURL string) error

CheckBaseURL says whether an address is one an endpoint can have, and when it is not, what to type instead. An empty address is fine: it means the vendor's own.

The picker shows this to whoever typed the address, so it says what to type rather than what was wrong with it. What people type is nearly always the address their model server printed, without its http:// -- "localhost:11434" -- and that is answered with the address it should have been.

func ConfigPath

func ConfigPath() (string, error)

ConfigPath returns where agents.json lives.

func KeyNames added in v0.3.1

func KeyNames(s Spec) (first, last []string)

KeyNames are the environment variables an API agent's key is looked for in, split where the key stored for it with `flockdeck keys set` is looked at: first before it, last after it. It is the one order the chat client, the keys dialog, `flockdeck keys` and the picker all go by:

  1. the entry's own variables, less its vendor's where it talks to somebody else (OwnKeyEnv), then the vendor's usual one where it talks to the vendor;
  2. the key stored for it;
  3. FLOCKDECK_API_KEY.

The stored key comes before Flockdeck's own variable because it is this agent's, and the variable is any agent's. The order used to depend on the entry: one with a variable of its own was handed its stored key under that variable, and so used it first, while one with none -- a built-in pointed at a gateway -- took FLOCKDECK_API_KEY first, and `flockdeck keys check` asked about one key while the pane sent the other.

The vendor's variable is left out for an entry pointed anywhere else, a gateway above all: OPENAI_API_KEY is the user's key for OpenAI, and an entry given only a third party's address would have handed it to that party with every request, even with a key stored for the entry itself.

An entry with neither an endpoint nor a variable of its own is the OpenAI-compatible one as it ships, with no address filled in yet; a key exported for OpenAI proper is not a reason to offer it, so it has only its own names, which are none, and its stored key.

func NeedsNoKey

func NeedsNoKey(s Spec) bool

NeedsNoKey reports whether an endpoint can be talked to without one. A model server on this machine -- Ollama, LM Studio, vLLM -- almost never asks for a key, and refusing to offer it until somebody invents one to store would be obtuse.

func OwnKeyEnv added in v0.3.4

func OwnKeyEnv(api APISpec) []string

OwnKeyEnv are the variables an entry names for its key, as far as it may be given them: all of them where it talks to its wire's vendor, and all but that vendor's own where it talks to anybody else.

A built-in keeps the vendor's variable in its entry when it is pointed at a gateway -- `flockdeck keys endpoint` and the picker's address field change the address and nothing else -- so an OPENAI_API_KEY exported for OpenAI was sent to the gateway with every request, ahead of the key stored for it. Everything that looks for a key, or tells a pane where to, goes through this.

func ProbeAll

func ProbeAll(specs []Spec) map[string]bool

ProbeAll returns availability for a whole catalog, keyed by id.

The probes are made side by side. Each one that misses walks the whole of PATH, and one after another they took 270ms over the built-in catalog on a Windows machine with half the agents not installed, against 160ms together.

func Refresh

func Refresh()

Refresh forgets what was probed, so the next look is a fresh one. The picker opening is the moment worth spending that on: it is where somebody who has just installed an agent goes to find it.

func SetBaseURL added in v0.2.3

func SetBaseURL(dir, id, baseURL string) error

SetBaseURL records the address of an API agent's endpoint in agents.json.

It is the one thing the OpenAI-compatible entry needs before it can be used -- a local model's address -- and it could be given only by editing the file by hand. An entry for the agent is changed in place, keeping everything else it says; one is added when there is none. An empty address takes the one recorded away.

Where the agent has more than one entry the last is the one given the address, because entries are merged in order and the last one's address is the one used: an address written into an earlier entry was saved without error and changed nothing. Every other entry for the agent has its address taken out, since each is merged over the one before: an address an earlier entry gave, which the last never mentions, was still the one panes used after being told the vendor's own was back.

It is what the picker's address field and `flockdeck keys endpoint` both save with.

func SetDefaults

func SetDefaults(dir, project string, d Defaults) error

SetDefaults records the agent and model to use where nothing else says. An empty project sets them for the whole installation; a project path sets them for that project alone, which is the picker's "set as default for this project".

func SetRouting added in v0.3.0

func SetRouting(dir, project, field, value string) error

SetRouting changes one setting of a routing policy in agents.json: "mode" or "floor", for one project where project names one and for every project otherwise. Everything else the policy says -- its rules, and any key a later build wrote -- is kept as it was written.

A project given a policy of its own starts from a copy of every project's, since a project's policy replaces that one whole: turning routing on for one project must not quietly drop the rules and the floor every project has. A mode of "" for a project takes its own policy away, so that it routes as every project does.

func TakesAddress added in v0.2.10

func TakesAddress(s Spec) bool

TakesAddress reports whether the picker offers to change an agent's address: the OpenAI-compatible entry, whose address is the one thing it needs, and any other API agent that has been given one -- a local model of the user's own, or a vendor's agent pointed at a gateway. An API agent still talking to its vendor is left alone, since there is nothing to change there that anybody asked to.

func TierRank added in v0.3.0

func TierRank(tier string) int

TierRank orders the tiers: 1 for small, 2 for mid and 3 for top. Anything else is 0, a model whose tier is not known.

func VendorsOwn added in v0.3.3

func VendorsOwn(api APISpec) bool

VendorsOwn reports whether an API entry talks to the vendor its wire is named for -- at no address of its own, or at the vendor's own host over https -- which is the only place the vendor's usual key variable may be sent. It is the one rule the picker, the keys dialog and the chat client all decide that by.

func WriteConfig

func WriteConfig(dir string, f *File) error

WriteConfig replaces agents.json in a directory.

The write is atomic, because this file is the user's own: a half-written one left behind by an interrupted save would lose every agent they had defined, and would do it at the moment Flockdeck was closing rather than somewhere they could see it happen.

Types

type APISpec

type APISpec struct {
	// Wire is the request shape: "anthropic", "openai" or "gemini". An
	// OpenAI-compatible endpoint -- Ollama, LM Studio, vLLM, a gateway -- is
	// "openai" with a BaseURL of its own.
	Wire string `json:"wire,omitempty"`
	// BaseURL is the endpoint root; empty means the vendor's own.
	BaseURL string `json:"baseURL,omitempty"`
	// KeyEnv names the environment variables a key may arrive in, tried in
	// order before Flockdeck's own key store.
	KeyEnv []string `json:"keyEnv,omitempty"`
}

APISpec describes an HTTP model API for the built-in chat client.

type Arg

type Arg struct {
	// Value is one argument, with tokens expanded. Empty when Args is set.
	Value string `json:"value,omitempty"`
	// If names a token; the group is used only when that token has a value.
	If string `json:"if,omitempty"`
	// Args is the group.
	Args []Arg `json:"args,omitempty"`
}

Arg is one entry in a Spec's argument list: a literal, or a group used only when a token has a value.

The group is what keeps an option with its value: dropping "{{model}}" alone from ["--model", "{{model}}"] would leave the flag behind to swallow the next argument as its value.

func Group

func Group(token string, values ...string) Arg

func Lit

func Lit(v string) Arg

Lit is one literal argument, and Group a set of them used only when a token has a value. They are shorthand for writing a catalog by hand.

type Caps

type Caps struct {
	// Hooks reports lifecycle events to Flockdeck, so the pane's status is known
	// rather than inferred from what it prints.
	Hooks bool `json:"hooks,omitempty"`
	// Resume reattaches a conversation by id, which is what makes restoring a
	// layout more than cosmetic.
	Resume bool `json:"resume,omitempty"`
	// Transcript records what was said somewhere Flockdeck can read it: where a
	// fan-out finds a plan, and the history overlay finds a conversation.
	Transcript bool `json:"transcript,omitempty"`
	// Trust has a per-directory trust question Flockdeck can answer ahead of a
	// fan-out, so that twelve fresh worktrees do not each stop on it.
	Trust bool `json:"trust,omitempty"`
	// Context is how the pane briefing reaches the agent.
	Context ContextMode `json:"context,omitempty"`
}

Caps says which of Flockdeck's facilities an agent supports. Everything Flockdeck does beyond drawing a terminal is gated on one of these, so an agent that supports none of it still works -- it is a terminal with a program in it.

type Catalog

type Catalog struct {
	// Specs are every agent, in picker order and including hidden ones.
	Specs []Spec
	// Defaults apply where a project has nothing to say.
	Defaults Defaults
	// Projects are the per-project defaults, keyed as the user wrote them.
	Projects map[string]Defaults
	// Routing is the routing policy every project shares, and ProjectRouting
	// each project's own, keyed as the user wrote them; RoutingFor says which
	// applies where. Both are as agents.json gave them, less whatever in them
	// could not be used, which Notice names.
	Routing        RoutingPolicy
	ProjectRouting map[string]RoutingPolicy
	// Notice is empty unless the user's file could not be used in full. It is
	// something for the interface to show, never a reason not to start: a
	// mistyped agents.json leaves somebody with the built-in agents, not with
	// an application that will not open.
	Notice string
}

Catalog is the agents this run can offer: the built-ins with the user's agents.json merged over them, together with the defaults from that file.

func Load

func Load() *Catalog

Load reads the catalog from Flockdeck's state directory.

It never fails. Where the state directory itself cannot be found there is nowhere for an agents.json to be, and the built-ins are the whole answer.

func LoadFrom

func LoadFrom(dir string) *Catalog

LoadFrom reads the catalog from a named directory, which is what the tests use and what a future --state-dir would use.

func Merge

func Merge(f *File) *Catalog

Merge overlays a parsed agents.json on the built-in catalog.

A user entry whose id is a built-in's is merged field by field over it: a field the user set wins, a field they left out keeps the built-in's answer. That is why the entries arrive as raw JSON -- it is the only way left, by then, to tell "" the user wrote from "" the decoder filled in.

func (*Catalog) DefaultsFor

func (c *Catalog) DefaultsFor(project string) Defaults

DefaultsFor returns the agent and model a project starts panes with: its own entry where it has one, otherwise the installation's.

func (*Catalog) Find

func (c *Catalog) Find(id string) (Spec, bool)

Find returns the spec with an id.

func (*Catalog) Resolve

func (c *Catalog) Resolve(project, agentID, model string) (Spec, string, bool)

Resolve answers what a new pane should start: the spec and the model id.

Both arguments are what the pane asked for and may be empty, which is the ordinary case -- splitting with the default agent is one keystroke and asks for nothing. A restored pane is not resolved this way: it recorded its agent and model when it was created, and Find is what gives those back to it, because a default changed since then must not quietly move a conversation onto another model.

The last return is false only when the pane named an agent the catalog has no entry for and the picker offers no agent at all, which takes a user file that hides every built-in.

func (*Catalog) RoutingFor added in v0.3.0

func (c *Catalog) RoutingFor(project string) (RoutingPolicy, bool)

RoutingFor is the policy work in a project is routed by, and whether it is the project's own rather than the one every project shares.

func (*Catalog) StripEnv

func (c *Catalog) StripEnv() []string

StripEnv is every variable any agent in the catalog wants taken out of a pane's environment, in the order the entries give them and without duplicates.

It is the union rather than the chosen agent's own list because the markers each of these tools leaves behind say "you are running inside me", and a Codex pane opened from inside a Claude session is no more a child of it than a Claude pane is. A pane is a clean top-level session whatever is running in it.

func (*Catalog) Visible

func (c *Catalog) Visible() []Spec

Visible returns the agents the picker offers, which is everything not hidden. Unavailable agents are still here: somebody who has not installed Codex should still learn that Flockdeck would run it.

type ContextMode

type ContextMode string

ContextMode is how a pane's briefing -- which pane it is, who else is working -- reaches the agent running in it.

const (
	// ContextHook answers the agent's own session-start hook with the
	// briefing, which is the only way it survives a compaction.
	ContextHook ContextMode = "hook"
	// ContextPrompt puts the briefing in front of the opening prompt, for an
	// agent with no hooks to answer.
	ContextPrompt ContextMode = "prompt"
	// ContextNone leaves the agent unbriefed.
	ContextNone ContextMode = "none"
)

type Defaults

type Defaults struct {
	Agent string `json:"agent,omitempty"`
	Model string `json:"model,omitempty"`
	// Routing is a project's own routing policy, kept exactly as it was
	// written so that saving a default cannot lose any of it -- a key this
	// build does not know included. Catalog.RoutingFor reads it.
	Routing *json.RawMessage `json:"routing,omitempty"`
}

Defaults are the agent and model chosen when a pane asks for neither: once for the whole installation, and once per project for somebody who works in two repositories that want different agents.

type File

type File struct {
	Version  int                 `json:"version"`
	Defaults Defaults            `json:"defaults,omitempty"`
	Projects map[string]Defaults `json:"projects,omitempty"`
	Agents   []json.RawMessage   `json:"agents,omitempty"`
	// Routing is the routing policy every project without one of its own is
	// routed by, kept as written for the same reason a project's is.
	Routing json.RawMessage `json:"routing,omitempty"`
	// Extra is every other top-level key the file held. It is kept so that
	// writing a default back from the picker cannot quietly delete something a
	// later build, or the user, put there.
	Extra map[string]json.RawMessage `json:"-"`
}

File is agents.json as it is written on disk.

The agents are kept as the raw JSON they were written as, rather than as Specs, because merging them over the built-ins has to tell a field the user set from a field they left out -- and once decoded into a struct the two look exactly alike.

func ReadConfig

func ReadConfig(dir string) (*File, error)

ReadConfig reads agents.json from a directory. A file that is not there is not an error -- it is the ordinary case, and means nothing but the built-ins.

func (File) MarshalJSON

func (f File) MarshalJSON() ([]byte, error)

func (*File) UnmarshalJSON

func (f *File) UnmarshalJSON(data []byte) error

type Model

type Model struct {
	ID   string `json:"id"`             // what the CLI or the API is given
	Name string `json:"name,omitempty"` // what the picker shows
	Note string `json:"note,omitempty"` // a few words on when to reach for it
	// Tier is how capable, and so how costly, this model is among the agent's
	// own: "small", "mid" or "top". Routing speaks in tiers rather than model
	// ids, so one rule serves every agent, and a model whose tier is not known
	// is never routed to or away from.
	Tier string `json:"tier,omitempty"`
}

Model is one model a Spec can be asked for.

type Patterns

type Patterns struct {
	// Waiting matches a line that means the agent is blocked on the user: a
	// permission question, a confirmation, an editor prompt.
	Waiting []string `json:"waiting,omitempty"`
	// Idle matches a line that means it has finished and is at its prompt.
	Idle []string `json:"idle,omitempty"`
}

Patterns recognise, in what an agent prints, the two states its own lifecycle would have reported. They are used only when Caps.Hooks is false.

type RoutingPolicy added in v0.3.0

type RoutingPolicy struct {
	Mode string `json:"mode,omitempty"`
	// Floor is the lowest tier routing may choose; empty is small.
	Floor string `json:"floor,omitempty"`
	// Rules are tried in order and the first that matches decides. Absent
	// means the built-in rules; an empty list means none at all.
	Rules []RoutingRule `json:"rules,omitempty"`
}

RoutingPolicy says whether, and how, Flockdeck chooses the model a piece of work runs on.

It lives in agents.json and nowhere else: the top-level "routing" for every project, and a project entry's own "routing" for that project, which replaces the other whole so that what a project says is all there is to read about it. It is never read from a file inside a repository, because a cloned repository must not be able to decide what the user's key spends.

type RoutingRule added in v0.3.0

type RoutingRule struct {
	Name string `json:"name"`
	Tier string `json:"tier,omitempty"`
	// Model and Agent name one agent's model, for a rule that wants exactly
	// that rather than a tier.
	Model string `json:"model,omitempty"`
	Agent string `json:"agent,omitempty"`
	// SuggestInPane lets the rule suggest a switch in a pane already running.
	// Nothing reads it yet; it is kept so a file written for a later build
	// means the same here.
	SuggestInPane bool      `json:"suggestInPane,omitempty"`
	When          RuleMatch `json:"when"`
}

RoutingRule is one rule: when a piece of work matches, it asks for a tier, or for one agent's model.

type RuleMatch added in v0.3.0

type RuleMatch struct {
	// Task is a regular expression, matched without regard to case, against
	// the task or prompt.
	Task     string `json:"task,omitempty"`
	MinWords int    `json:"minWords,omitempty"`
	MaxWords int    `json:"maxWords,omitempty"`
	// Files are globs, matched against the paths the task names.
	Files []string `json:"files,omitempty"`
	// Kind is "fanout", "spawn" or "turn".
	Kind  string `json:"kind,omitempty"`
	Agent string `json:"agent,omitempty"`
}

RuleMatch is what a piece of work must be for a rule to match. Every field given must hold.

type Runner

type Runner string

Runner is how a Spec is started.

const (
	// RunnerCLI runs an external command in the pane's pseudo-terminal.
	RunnerCLI Runner = "cli"
	// RunnerAPI runs Flockdeck's own chat client in the pane, talking directly to
	// a model API. The command is `flockdeck chat`; the Spec says which endpoint.
	RunnerAPI Runner = "api"
)

type Spec

type Spec struct {
	ID     string `json:"id"`
	Name   string `json:"name"`
	Runner Runner `json:"runner,omitempty"`
	// Exe is the command to look for on PATH, for a CLI runner.
	Exe string `json:"exe,omitempty"`
	// Args is the argument list; ResumeArgs replaces it when a conversation is
	// being reattached rather than started.
	Args       []Arg `json:"args,omitempty"`
	ResumeArgs []Arg `json:"resumeArgs,omitempty"`
	// Models are what the picker offers. DefaultModel is taken when nothing is
	// chosen; empty leaves the choice to the agent, which is how a CLI keeps
	// whatever it was configured with.
	Models       []Model `json:"models,omitempty"`
	DefaultModel string  `json:"defaultModel,omitempty"`
	// Switch is the command, typed into the agent itself, that changes the
	// model of the session already running: "/model {{model}}". It is set only
	// where the agent documents that command as changing the model for that
	// session alone. Empty means the model is changed by restarting the pane
	// on the new one and resuming the conversation, which works for any agent
	// that can resume. Claude Code has none: its /model saves what it is given
	// as the default for every later session, inside Flockdeck or not.
	Switch string `json:"switch,omitempty"`
	// Env is added to the pane's environment; StripEnv is removed from it,
	// which is how the markers of the session Flockdeck was launched from are kept
	// from making every pane look like a nested child of it.
	Env      []string `json:"env,omitempty"`
	StripEnv []string `json:"stripEnv,omitempty"`
	API      APISpec  `json:"api,omitempty"`
	Caps     Caps     `json:"caps,omitempty"`
	Patterns Patterns `json:"patterns,omitempty"`
	// Install is what to tell somebody who picked an agent their machine does
	// not have.
	Install string `json:"install,omitempty"`
	// NoDashDash suppresses the "--" that otherwise guards an opening prompt
	// beginning with a dash.
	NoDashDash bool `json:"noDashDash,omitempty"`
	// Hidden keeps a built-in out of the picker without removing it.
	Hidden bool `json:"hidden,omitempty"`
}

Spec is one agent Flockdeck can run.

func Builtins

func Builtins() []Spec

Builtins returns a fresh copy of the built-in specs, in the order the picker shows them: Claude first because it is the default, then the other command-line agents, then the API runners that need no CLI at all.

The copy is fresh on every call because the catalog merges the user's file over these entries in place, and a shared slice would carry one run's agents.json into the next read of the built-ins.

type Tokens

type Tokens struct {
	Session  string // the pane id, which is also the conversation id
	Model    string
	Settings string // a generated settings file, for an agent that takes one
	Prompt   string // the opening task; empty when there is none
	Cwd      string
	Pane     string
}

Tokens are the values an argument list may refer to. A token with no value removes the group it appears in.

func (Tokens) Expand

func (t Tokens) Expand(s string) string

Expand replaces every {{token}} in s with its value.

It is one pass, so a value is never read for tokens of its own. Replacing them one name after another did exactly that to the opening task, which comes before "cwd" and "pane": a task about a Handlebars template reached the agent with its "{{pane}}" swapped for the pane's id.

Space inside the braces is allowed, as Value already allowed it in an `if`: "{{ model }}" is how anyone used to a template language writes it, and it went through untouched -- the CLI was handed a model called "{{ model }}", and a task written "{{ prompt }}" never reached the agent at all.

func (Tokens) Value

func (t Tokens) Value(name string) string

Value returns the token named by an `if`, or by "{{name}}".

Jump to

Keyboard shortcuts

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