Documentation
¶
Overview ¶
Package config defines agent2api's configuration schema and assembles the effective config from three layers: built-in defaults, an optional config file, and CLI flags — later layers winning per key.
The defaults are Go, not a file: see Defaults. They merge as a map rather than decoding onto the struct, because decoding replaces a whole map[string]Adapter entry, silently dropping the rest of that adapter.
The schema is small on purpose: which backends run is discovered by probing, and which models they serve is the CLIs' business.
Index ¶
Constants ¶
const ( // SystemPromptAppend adds the caller's system message to the CLI's. SystemPromptAppend = "append" // SystemPromptReplace sends only the caller's system message. SystemPromptReplace = "replace" )
System prompt handling modes, used by adapters whose CLI ships a system prompt of its own.
const ( // DefaultHost keeps the listener on this machine. Binding anywhere else // requires a bearer key; see [Config.Validate]. DefaultHost = "127.0.0.1" DefaultPort = 8055 // The deadlines are in seconds; see [Seconds] for why they carry no unit. // // They are generous because a request here is a whole CLI run: a tool-less // turn still takes seconds, and a long answer takes minutes. DefaultRequestTimeout = 600 * Second DefaultIdleTimeout = 120 * Second DefaultQueueTimeout = 30 * Second // DefaultHeartbeatInterval is the SSE keepalive cadence while the CLI is // quiet. Zero would disable keepalives. DefaultHeartbeatInterval = 15 * Second DefaultShutdownTimeout = 30 * Second // DefaultMaxConcurrency bounds the CLI processes one adapter may run at // once. Every request is a whole subprocess, so an unbounded gateway is a // fork bomb waiting for a busy client. DefaultMaxConcurrency = 4 DefaultLogLevel = "info" DefaultLogFormat = "text" )
Built-in default values. They are named constants rather than literals buried in Defaults so that the ones worth citing elsewhere — a message, a doc comment, a test — have somewhere to be cited from.
const ( DefaultClaudeCodeBinary = "claude" DefaultCodexBinary = "codex" DefaultCursorBinary = "cursor-agent" DefaultQwenBinary = "qwen" )
Default binaries, looked up on PATH unless overridden with an absolute path.
const DefaultCursorMode = "ask"
DefaultCursorMode answers the question and nothing else. Both cursor modes are read-only, but "plan" replies with a plan for the work rather than with the answer, which is not what a caller asking for a completion wants.
const DefaultSandbox = "read-only"
DefaultSandbox is the codex sandbox mode: the most restrictive one the CLI offers, and the only one agent2api recommends. The prompt should be the only thing the backend can act on.
const DefaultSystemPromptMode = SystemPromptReplace
DefaultSystemPromptMode sends only the caller's system message, so a backend asked for a completion answers as a plain LLM rather than as the agent its CLI ships as, and keeps that CLI's multi-thousand-token preamble out of every request.
const Delim = "."
Delim is the key separator used for flattened config paths and flag names.
Variables ¶
var CursorModes = []string{"ask", "plan"}
CursorModes are the values adapters.cursor.mode accepts. Both are read-only: "ask" answers the question, "plan" proposes a plan for it. The CLI's other modes let it edit files, which is not what a completions API is for, so they are not offered here.
var KnownAdapters = []string{"claude-code", "codex", "cursor", "qwen-code"}
KnownAdapters are the adapter ids the schema accepts. An adapter listed here may still be unimplemented in this build; that is reported at startup rather than at config load.
var SandboxModes = []string{"read-only", "workspace-write", "danger-full-access"}
SandboxModes are the values adapters.codex.sandbox accepts, in order of increasing reach. Anything else is a typo, and a typo here would quietly widen what the CLI can touch.
Functions ¶
func ConfigFileHelp ¶
func ConfigFileHelp() string
ConfigFileHelp points at the config file for everything the flags omit.
func Load ¶
func Load(flags *pflag.FlagSet) (*Config, *Provenance, error)
Load assembles the effective configuration: built-in defaults, then the file named by -c/--config if there is one, then any flag the user actually set.
The file is decoded strictly: an unknown key is a startup error, so typos fail loudly instead of being silently ignored.
func RegisterAdapterID ¶
func RegisterAdapterID(id string)
RegisterAdapterID makes an adapter id valid in a config file. Adapter packages that are not part of a normal build (the conformance-only mock backend) call this from their init, so linking one in is all it takes for its config block to be accepted.
func RegisterFlags ¶
RegisterFlags adds agent2api's command-line flags to fs.
Types ¶
type Adapter ¶
type Adapter struct {
Binary string `koanf:"binary"`
ExtraArgs []string `koanf:"extra_args"`
Env map[string]string `koanf:"env"`
// EnvPassthrough names variables the CLI inherits from the gateway's own
// environment, beyond the vendor prefixes each adapter already lets
// through. Env sets a value in the config file; this names one that stays
// out of it, which is what a credential wants.
EnvPassthrough []string `koanf:"env_passthrough"`
// Sandbox is codex-specific and ignored by other adapters.
Sandbox string `koanf:"sandbox"`
// Mode is cursor-specific and ignored by other adapters: it picks the
// CLI's execution mode, one of [CursorModes].
Mode string `koanf:"mode"`
// SystemPromptMode is claude-code-specific: "replace" (the default) sends
// only the caller's system message, so the backend behaves like a bare LLM;
// "append" keeps the CLI's own agent system prompt and adds the caller's to
// it. See SystemPrompt* above.
SystemPromptMode string `koanf:"system_prompt_mode"`
}
Adapter configures one agent CLI backend.
There is no model list: a request names "<adapter>" for the CLI's own default model, or "<adapter>:<model>" to pass a model through. The CLI validates the name, so agent2api never carries a stale copy of it.
type Config ¶
type Config struct {
Server Server `koanf:"server"`
Log Log `koanf:"log"`
Adapters map[string]Adapter `koanf:"adapters"`
}
Config is the effective configuration.
func Defaults ¶
func Defaults() Config
Defaults is the lowest configuration layer, with the file and then the flags merged over it per key.
It lives in Go so the compiler checks every field name and value, and each default sits beside the field it belongs to. agent2api.example.yaml documents the same keys; TestExampleConfigCoversTheSchema keeps the two in step.
Every adapter is listed; which ones run is discovered by probing at startup.
func (Config) AdapterIDs ¶
AdapterIDs returns the configured adapter ids in a stable order. Being listed is not the same as being served: an adapter joins the router only if probing finds its CLI usable on this machine.
type Log ¶
type Log struct {
Level string `koanf:"level"`
Format string `koanf:"format"`
File string `koanf:"file"`
}
Log configures the structured logger.
type Provenance ¶
type Provenance struct {
// File is the config file that was loaded, empty if none was found.
File string
// Of maps a flattened config path to the layer that set it.
Of map[string]Source
// Values holds the effective value of every flattened config path.
Values map[string]any
}
Provenance answers "where did this value come from?" for `config print`.
func (*Provenance) Report ¶
func (p *Provenance) Report() string
Report renders the effective configuration one key per line, annotated with the layer each value came from.
The output is meant for bug reports, so anything that can hold a credential — the bearer key, adapter env values, extra_args values — is shown as set rather than shown. Names stay: whether a key is set, and from which layer, is what the report is read for.
type Seconds ¶
type Seconds int
Seconds is a duration a config file writes as a plain number, with no unit: `request_timeout: 600` is ten minutes.
It is its own type rather than a time.Duration because a duration is an int64 count of nanoseconds, so a config file's `600` would decode as 600ns — a timeout that fires the instant it is armed. Naming the unit in the type keeps that out of the schema, and the compiler routes every use site through Seconds.Duration.
const Second Seconds = 1
Second is the unit of the Seconds defaults above, so they read as durations rather than as bare magic numbers.
type Server ¶
type Server struct {
Host string `koanf:"host"`
Port int `koanf:"port"`
// APIKey is the static bearer key every request must present. Empty means
// no authentication, which is only allowed on a loopback bind.
APIKey string `koanf:"api_key"`
// The four deadlines and the keepalive cadence are all in seconds; see
// [Seconds] for why they carry no unit.
RequestTimeout Seconds `koanf:"request_timeout"`
IdleTimeout Seconds `koanf:"idle_timeout"`
QueueTimeout Seconds `koanf:"queue_timeout"`
// HeartbeatInterval is how often a streaming response emits a keepalive
// while the CLI is thinking. Zero disables keepalives.
HeartbeatInterval Seconds `koanf:"heartbeat_interval"`
// ShutdownTimeout is how long a shutdown waits for in-flight requests
// before cutting them off. A streaming answer can be minutes long, so this
// is not the same order of magnitude as an ordinary web server's.
ShutdownTimeout Seconds `koanf:"shutdown_timeout"`
// MaxConcurrency caps how many CLI processes one adapter may run at once.
MaxConcurrency int `koanf:"max_concurrency"`
// ScratchDir is the parent of the per-request workdirs; empty means the OS
// temp directory.
ScratchDir string `koanf:"scratch_dir"`
}
Server holds HTTP listener and per-request timeout settings.