Documentation
¶
Overview ¶
Package config loads the OpenCode-compatible configuration surface (opencode.json + AGENTS.md) into neutral config values (ADR-0001).
It reads only the parts the rest of the system needs to select a provider adapter and resolve its options: instructions, the default model string, the provider table (options.baseURL/apiKey with {env:VAR} expansion), and the permission rules. No provider-specific type escapes this package.
Index ¶
- Constants
- func LoadInstructions(root string, files []string) (string, error)
- func ParseModel(model string) (providerID, modelID string, err error)
- func ResolveConfigPath(root, configPath string) string
- func SetTUITheme(path, theme string) error
- type Config
- type Context
- type Coordination
- type Embedder
- type LSP
- type LSPServer
- type MCPServer
- type Max
- type Memory
- type Model
- type Permission
- type ProjectContext
- type Provider
- type TUI
Constants ¶
const ( MCPTransportStdio = "stdio" MCPTransportHTTP = "http" )
MCP transport names. Anything else is a config error: silently defaulting would start a server the user did not describe.
const DefaultConfigFile = "opencode.json"
DefaultConfigFile is the config file looked for under a project root.
const DefaultInstructionFile = "AGENTS.md"
DefaultInstructionFile is read when opencode.json names no instruction files. It is the canonical location for project rules (see the repo's own AGENTS.md).
Variables ¶
This section is empty.
Functions ¶
func LoadInstructions ¶
LoadInstructions reads each instruction file relative to root, in order, concatenating them under a header naming the source so the model can tell which file a rule came from. Missing files are skipped (a project need not have every file); a path escaping root is an error.
func ParseModel ¶
ParseModel splits a "<provider>/<model>" string into its provider id and provider-native model id. The prefix selects the adapter (ADR-0005). Returns an error if the string has no "/" separator or is empty.
func ResolveConfigPath ¶
ResolveConfigPath returns the config file a root plus an optional override resolves to, whether or not it exists. An empty override means <root>/opencode.json; a relative override is taken relative to root. Callers that need to write the config (e.g. /theme persisting a palette) use this to find the same file the session was loaded from.
func SetTUITheme ¶
SetTUITheme persists theme as tui.theme in the config file at path, leaving every other key intact — the file belongs to the user, so this is a targeted patch and not a rewrite from the parsed Config (which would drop keys this package does not model, e.g. "$schema"). A missing file is created: a fresh project has no opencode.json yet, and picking a theme should not require one.
Keys are re-emitted in sorted order (Go marshals maps sorted); content is preserved, formatting is not.
Types ¶
type Config ¶
type Config struct {
// Instructions is the list of project-instruction files (e.g. AGENTS.md)
// whose contents the agent prepends to the system prompt.
Instructions []string
// Model is the default "<provider>/<model>" string. The prefix selects the
// provider adapter (ADR-0005); the suffix is the provider-native model id.
Model string
// Providers maps a provider id (anthropic, openai, local, custom…) to its
// resolved options. The id is the opencode.json key under "provider".
Providers map[string]Provider
// Permission is the parsed permission ruleset (ADR-0007).
Permission Permission
// Embedder configures the embedding model used for memory (ADR-0004).
// Absent means memory is disabled.
Embedder Embedder
// Memory configures the on-disk memory store (ADR-0003).
Memory Memory
// Context configures the token budget and window (ADR-0008).
Context Context
// Coordination configures the subagent symbol coordinator (change 0013).
Coordination Coordination
// TUI configures the front-end's appearance (change 0017).
TUI TUI
// Max configures Max Mode, the best-of-N sampler (change 0016).
Max Max
// MCP maps a server name to its declaration (change 0015). Empty means no
// MCP servers. The name prefixes that server's tool names.
MCP map[string]MCPServer
// LSP configures the language servers behind the LanguageService port
// (change 0026, ADR-0017). Opt-in: absent means no server is ever spawned.
LSP LSP
}
Config is the parsed, env-expanded configuration.
type Context ¶
type Context struct {
// Budget is the soft token ceiling for assembled context.
Budget int
// Window is the model's full context window, used for the checkpoint
// high-water mark.
Window int
}
Context is the context-window configuration (ADR-0008).
type Coordination ¶
type Coordination struct {
// Backend selects the coordinator: "native" (default, ships with OpenPlus),
// "grit" (external binary), or "none" (disable coordinated fan-out).
Backend string
}
Coordination is the subagent symbol-coordinator configuration (change 0013).
type Embedder ¶
type Embedder struct {
Model string
BaseURL string
APIKey string
// Timeout bounds a single Embed call when no caller-supplied http.Client
// is set. Zero means embed.DefaultTimeout (30s).
Timeout time.Duration
}
Embedder is the embedding-model configuration. Memory is only enabled when Configured reports true.
func (Embedder) Configured ¶
Configured reports whether enough is set to embed. A model is required: it names the vector space, and a store built against the wrong one is worse than no store at all.
type LSP ¶
type LSP struct {
// Enabled turns the LanguageService on. Off by default — a coding agent
// must not start subprocesses the user did not ask for.
Enabled bool
// Servers maps a file extension (including the dot, e.g. ".go") to the
// language server that handles it.
Servers map[string]LSPServer
}
LSP is the language-server configuration (ADR-0017). It is opt-in: a session spawns nothing unless Configured reports true.
func (LSP) Configured ¶
Configured reports whether LSP should actually run. Both halves are required: enabling with no servers would spawn nothing, and servers without the flag is a declaration the user has not switched on.
type LSPServer ¶
LSPServer is one language server: the command to run and its arguments. The user supplies the binary; OpenPlus never downloads a toolchain.
type MCPServer ¶
type MCPServer struct {
// Name is the config key, carried here so an error can name the server.
Name string
// Transport is MCPTransportStdio or MCPTransportHTTP.
Transport string
// Command, Args, Env and Dir describe a stdio subprocess.
Command string
Args []string
Env map[string]string
Dir string
// URL and Headers describe a streamable-HTTP endpoint.
URL string
Headers map[string]string
}
MCPServer is one declared MCP server (change 0015, ADR-0010). Stdio fields and HTTP fields are mutually exclusive in practice; Transport selects which apply.
Note what this is: a stdio server is an arbitrary executable and an http server an arbitrary endpoint, both named by the user. Their tools go through the PolicyGate like any other.
type Max ¶
type Max struct {
// Samples is the default N for /max. Zero means orchestrate.DefaultSamples;
// a value above the cap is clamped at use, with the clamp reported.
Samples int
// Model is the judge model id ("<provider>/<model>"). Empty judges with the
// session's model — an independent judge is better, but requiring one would
// make Max Mode unusable on a single-model setup.
Model string
}
Max is the Max Mode configuration (change 0016, ADR-0011). Max Mode is opt-in per invocation; this only sets its defaults.
type Memory ¶
type Memory struct {
// Path is the database file, relative to the project root when not absolute.
Path string
// AutoOpen creates the file (and its parent directory) if it does not
// exist. Default false: a missing path is a configuration error, not a
// silent side effect. Set true in opencode.json to opt in.
AutoOpen bool
// MaxEntries caps the stored chunks; oldest are pruned first on each
// write. Zero means unbounded.
MaxEntries int
}
Memory is the memory-store configuration.
type Permission ¶
Permission is the parsed permission ruleset. Tools maps a tool name to its decision string ("allow"/"ask"/"deny"); Paths maps a glob path pattern to its decision string. The full rule engine (glob matching, last-match-wins, forced-ask timeout) is T-022 — this struct only holds the raw parsed rules.
type ProjectContext ¶
type ProjectContext struct {
// Root is the project root the context was loaded from.
Root string
// Config is the parsed opencode.json. Never nil: a project without the file
// gets a zero-value Config.
Config *Config
// Instructions is the concatenated content of the instruction files, each
// under a header naming its source file.
Instructions string
}
ProjectContext is a project's assembled configuration and instructions — everything the agent needs to know about *this* repo before its first turn (T-003).
func LoadProjectContext ¶
func LoadProjectContext(root string) (ProjectContext, error)
LoadProjectContext loads opencode.json (when present) and the instruction files it names, defaulting to AGENTS.md. A missing opencode.json is fine — a project can be configured entirely by its AGENTS.md — but a malformed one is an error rather than a silent fallback to defaults.
func LoadProjectContextWithConfig ¶
func LoadProjectContextWithConfig(root, configPath string) (ProjectContext, error)
LoadProjectContextWithConfig is LoadProjectContext with an explicit config path (T-422). Empty configPath means "use the default <root>/opencode.json with lenient missing-file semantics". A non-empty path is treated as an explicit override — a missing or malformed file there is an error, because --config is a deliberate operator choice and a typo should surface as a clear failure.
Callers that always want the strict behavior can call this function with a non-empty path; callers that want lenient default-path behavior call LoadProjectContext directly.
func (ProjectContext) SystemPrompt ¶
func (pc ProjectContext) SystemPrompt(base string) string
SystemPrompt appends the project instructions to a base system prompt. The base comes first so the agent's own identity and rules outrank project instructions that might contradict them.
type Provider ¶
type Provider struct {
// ID is the opencode.json key (anthropic, openai, local, …).
ID string
// Name is the optional human label (opencode.json provider.<id>.name).
Name string
// BaseURL is options.baseURL after {env:VAR} expansion. Empty means the
// provider's default endpoint (e.g. api.anthropic.com for the anthropic id).
BaseURL string
// APIKey is options.apiKey after {env:VAR} expansion.
APIKey string
// Models maps the provider-native model id to its display name.
Models map[string]Model
}
Provider is one configured model backend.
type TUI ¶
type TUI struct {
// Theme names the initial palette ("default", "deutan", "protan",
// "tritan"). Empty means the front-end's default; an unknown name falls
// back to it with a warning rather than failing the session, because an
// appearance setting must never block work.
Theme string
}
TUI is the front-end configuration (change 0017, ADR-0012).