Documentation
¶
Overview ¶
Package chat provides a unified multi-provider AI chat client supporting Claude, OpenAI, Gemini, Claude Local (via CLI binary), and OpenAI-compatible endpoints.
The ChatClient interface exposes five methods: Add (append a message), Chat (multi-turn conversation with tool use via a ReAct loop), Ask (structured output with JSON schema validation), SetTools (register callable tools), and Usage (token accounting). Streaming-capable providers also implement StreamingChatClient (StreamChat).
Tool calling follows a JSON Schema parameter definition, and the ReAct loop automatically dispatches tool calls and feeds results back until the model produces a final text response. Per-provider token limits and maximum agent steps are configurable via Config.
Providers other than claude-local live in separate per-SDK modules and are activated by a blank import, so a consumer links only the SDK(s) it uses:
import _ "gitlab.com/phpboyscout/go/chat-anthropic" // provider "claude" import _ "gitlab.com/phpboyscout/go/chat-openai" // "openai", "openai-compatible" import _ "gitlab.com/phpboyscout/go/chat-gemini" // "gemini"
Each provider module self-registers via RegisterProvider (and a failover HTTPStatusExtractor via RegisterStatusExtractor) in its init(). Custom providers register the same way. The SDK-free claude-local provider ships in this core module. Structured-output helpers such as GenerateSchema simplify schema generation for Ask calls.
New constructs clients from package-owned Settings; the package is config-system-agnostic and owns typed config shapes such as RuntimeConfig, FallbackConfig, and CredentialConfig. A host application maps its own configuration into Settings/Config (go-tool-base ships such a Props adapter, which lives in go-tool-base, not here). Credentials resolve through ResolveAPIKey; the HTTP transport and OS-keychain lookup are injected via Config so the core depends on no specific HTTP or secret-store stack. Existing chat clients do not mutate provider settings live on config reload; construct a new client to pick up changed settings.
Conversation state ¶
A client has two modes, chosen at construction. By default it is a conversation: every Chat, Ask and StreamChat call appends the exchange to the client's history and the next call re-sends all of it. Set Config.Stateless and each call becomes a one-shot instead — no prior turns sent, none retained, and one client can serve many independent calls across goroutines.
The default suits a conversation and is expensive for batch work: a caller holding one client across N independent calls pays for the accumulated prefix every time, so cost grows with the square of N, and call N is answered in the light of calls 1..N-1 without the caller asking. Stateless mode removes both. Tool calling is unaffected — the ReAct loop keeps the turns it needs within a call and discards them on return.
Providers that honour the flag implement StatelessCapable; New refuses to build a stateless client from one that does not, rather than silently ignoring the request. Under stateless mode PersistentChatClient.Save returns a snapshot with no conversation and Restore returns ErrStatelessRestore.
Generation controls ¶
Config.Temperature and Config.TopP influence sampling; Config.Effort selects how much reasoning the model spends before answering. All are optional and default to the provider's own behaviour.
Support for these is per *model*, not per provider — two models from one vendor can accept opposite controls — so it cannot be settled at construction. A provider with no such concept at all (claude-local has no sampling flag) fails in New; a model refusing one it structurally supports yields ErrModelRejectedParameter at request time. Neither path silently drops the parameter, because a control that is quietly ignored cannot be told apart from one that does nothing.
Lower temperatures reduce variance between runs; they do not make output deterministic. Effort is the most direct cost dial here — observe it through Usage.ReasoningTokens.
Prompt caching ¶
Providers that can cache a large stable block implement CachingChatClient; discover it by type assertion. AddCached marks content so repeated calls are billed at the provider's cached rate, and Config.CacheTTL chooses how long it is retained.
It is a hint. Every provider declines to cache content below a per-model minimum, and most do so without an error, so confirm the effect through Usage.CachedTokens rather than assuming it. Unlike Config.Stateless or Config.Temperature, being ignored here costs money rather than corrupting the result, which is why an unsupported provider simply does not implement the interface instead of failing construction.
Multimodal input ¶
Add, Ask, Chat and StreamChat accept a trailing variadic of Media — images (and, on Gemini, PDF and A/V) sent alongside the text prompt. A text-only call passes no media and is unchanged. Each attachment's type is sniffed from its bytes (never a caller-supplied filename), cross-checked against any declared MIMEType, allowlisted, and checked against the selected provider's support before any network call; disguised or unsupported content is rejected with ErrMediaRejected or ErrMediaUnsupported. Media support is per provider: Gemini (images, PDF, A/V), Claude and OpenAI (images, PDF); ProviderClaudeLocal accepts no media.
Index ¶
- Constants
- Variables
- func ApplyHistoryEdit(turns []TurnInfo, edit HistoryEdit) ([]int, error)
- func AssignText(text string, target any) error
- func BoundConversation(ctx context.Context, policy HistoryPolicy, turns []TurnInfo) ([]int, map[int]string, error)
- func ChatHTTPClient(cfg Config) *http.Client
- func Compact(ctx context.Context, policy HistoryPolicy, turns []TurnInfo) (keep []int, replace map[int]string, changed bool, err error)
- func ExecuteTool(ctx context.Context, l *slog.Logger, tools map[string]Tool, name string, ...) string
- func GenerateEncryptionKey() ([]byte, error)
- func GenerateSchema[T any]() *jsonschema.Schema
- func MarkAs(err, sentinel error) error
- func ProviderHTTPStatus(err error) (int, bool)
- func RegisterCapabilities(name Provider, resolver CapabilityResolver)
- func RegisterProvider(name Provider, factory ProviderFactory)
- func RegisterStatusExtractor(extractor HTTPStatusExtractor)
- func ResolveAPIKey(ctx context.Context, direct string, credential CredentialConfig, ...) string
- func ValidateBaseURL(baseURL string, allowInsecure bool) error
- func ValidateSnapshotID(id string) error
- type CachingChatClient
- type CachingSupport
- type Capabilities
- type Capability
- type CapabilityResolver
- type ChatClient
- func New(ctx context.Context, settings Settings) (ChatClient, error)
- func NewClaudeLocal(ctx context.Context, settings Settings, opts ...ClaudeLocalOption) (ChatClient, error)
- func NewFallback(clients []ChatClient, opts ...FallbackOption) (ChatClient, error)
- func NewFallbackFromConfigs(ctx context.Context, cfgs []Config, opts ...FallbackOption) (ChatClient, error)
- func NewFallbackFromSettings(ctx context.Context, settings []Settings, opts ...FallbackOption) (ChatClient, error)
- func NewWithFactory(ctx context.Context, settings Settings, factory ProviderFactory) (ChatClient, error)
- func NewWithFallbackSettings(ctx context.Context, settings Settings, fallback FallbackConfig, ...) (ChatClient, error)
- type ClaudeLocal
- func (c *ClaudeLocal) Add(_ context.Context, prompt string, media ...Media) error
- func (c *ClaudeLocal) Ask(ctx context.Context, question string, target any, media ...Media) error
- func (c *ClaudeLocal) Chat(ctx context.Context, prompt string, media ...Media) (string, error)
- func (c *ClaudeLocal) History() History
- func (c *ClaudeLocal) SetTools(_ []Tool) error
- func (c *ClaudeLocal) SupportsEffort()
- func (c *ClaudeLocal) SupportsStateless()
- type ClaudeLocalOption
- type Command
- type CommandFunc
- type CommandRequest
- type CommandResult
- type CommandSet
- type CompactOption
- func WithCompactionFallback(fallback HistoryPolicy) CompactOption
- func WithSummarizer(cfg Config) CompactOption
- func WithSummarizerClient(client ChatClient) CompactOption
- func WithSummaryPrompt(prompt string) CompactOption
- func WithTokenBudget(tokens int) CompactOption
- func WithTurnBudget(turns int) CompactOption
- type Config
- type ConfigErrors
- type ConversationStore
- type CredentialConfig
- type DroppedSetting
- type Effort
- type EffortCapabledeprecated
- type EffortSupport
- type FailoverDecision
- type FailoverPolicy
- type FallbackConfig
- type FallbackOption
- func WithFailoverPolicy(policy FailoverPolicy) FallbackOption
- func WithFallbackLogger(log *slog.Logger) FallbackOption
- func WithOnFailover(fn func(from, to Provider)) FallbackOption
- func WithProviderCredentials(resolve ProviderCredentials) FallbackOption
- func WithStateless() FallbackOption
- func WithStrictToolContext() FallbackOption
- type FileStoreOption
- type HTTPStatusExtractor
- type History
- type HistoryEdit
- type HistoryPolicy
- type KeychainLookup
- type Limits
- type Media
- type ModelIdentifier
- type ModelInfo
- type PersistentChatClient
- type Provider
- type ProviderCredentials
- type ProviderFactory
- type ResolvedMedia
- type Role
- type RuntimeConfig
- type SamplingCapabledeprecated
- type SamplingSupport
- type Settings
- type Snapshot
- type SnapshotSummary
- type StatelessCapable
- type StreamCallback
- type StreamEvent
- type StreamEventType
- type StreamToolCall
- type StreamingChatClient
- type Support
- type Tool
- type ToolCall
- type ToolResult
- type ToolSnapshot
- type TranscriptEditor
- type TurnInfo
- type Usage
- type UsageTracker
Examples ¶
Constants ¶
const ( // DefaultModelGemini is the default model for the Gemini provider. // // Flash rather than Pro because Google ships no generally-available // Pro-tier model: gemini-3.1-pro-preview is preview-only, and both // gemini-3-pro-preview and gemini-2.5-pro have been withdrawn. This is the // most capable Gemini available at GA, and it is a genuine step below // [DefaultModelClaude] and [DefaultModelOpenAI] — a known gap, accepted in // preference to defaulting onto a preview model that may disappear. DefaultModelGemini = "gemini-3.7-flash" // DefaultModelClaude is the default model for the Claude provider. // Anthropic's top tier, generally available since 2026-07-24. DefaultModelClaude = "claude-opus-5" // DefaultModelOpenAI is the default model for the OpenAI provider. // // The gpt-5.6 generation ships as three variants — sol, terra and luna — // and nothing in the models API distinguishes them: same creation date, // same owner. Choosing sol is therefore a recorded human judgement rather // than something the rule above derives, and is revisited if the variants // turn out to be specialised rather than peers. // // Mirrors openai.ChatModelGPT5_6Sol from openai-go/v3; inlined as a string // literal so the SDK-free chat core carries no OpenAI dependency. The // chat-openai provider module owns the mapping back onto the SDK constant. DefaultModelOpenAI = "gpt-5.6-sol" // DefaultMaxSteps is the default maximum number of ReAct loop iterations. DefaultMaxSteps = 20 // DefaultMaxTokensOpenAI is the default maximum tokens per response for OpenAI. DefaultMaxTokensOpenAI = 4096 // DefaultMaxTokensClaude is the default maximum tokens per response for Claude. DefaultMaxTokensClaude = 8192 // DefaultMaxTokensGemini is the default maximum tokens per response for Gemini. DefaultMaxTokensGemini = 8192 )
Default models are chosen by one rule, applied identically to every provider:
the most capable model a provider makes generally available that supports the module's baseline capabilities.
"Most capable" rather than a named tier, because tier names are vendor nomenclature that does not generalise — OpenAI's current generation ships as sol/terra/luna, which is not a tier ordering. "Generally available" excludes preview and experimental models: a default must not point at something a vendor may withdraw. Baseline capabilities are structured outputs, tool use and multimodal input, all of which this module exposes.
A default is what a caller gets when they have expressed no preference, so it favours capability over cost. Callers with an opinion about cost set Config.Model.
See https://gitlab.com/phpboyscout/go/chat/-/wikis/specs/0005-default-model-formula.
const DefaultChatRequestTimeout = 5 * time.Minute
DefaultChatRequestTimeout bounds a single AI request. LLM generations — especially a large single-shot code conversion on a slower flagship model like Opus — run well past the shared 30s HTTP default, so chat clients use a more generous cap. It is deliberately bounded (not unlimited): a model stuck in a loop or never returning must still fail rather than hang forever. Override per-environment with the ai.request_timeout config key (e.g. "8m") or programmatically via Config.RequestTimeout.
const DefaultCompactionTurns = 200
DefaultCompactionTurns is the retained-turn count above which a compacting policy summarises, unless WithTurnBudget says otherwise.
It is far larger than a truncation budget would be, deliberately. Dropping turns is free and summarising them is not, so a conversation should be allowed to get genuinely long before it starts paying for a round-trip it did not ask for. See spec 0014 D14.
const EnvAIProvider = "AI_PROVIDER"
EnvAIProvider is the environment variable for overriding the AI provider. The chat core honours it as an ecosystem default in applyDefaultProvider; the GTB adapter owns the ai.* / <provider>.api.* config-key schema.
const MaxBaseURLLength = 2048
MaxBaseURLLength caps the length, in bytes, of a provider BaseURL. Normal BaseURLs are well under 200 bytes; 2 KiB is generous for legitimate proxy configurations and far short of any pathological input.
const SnapshotVersion = 1
SnapshotVersion is the current version of the snapshot format. Increment when the format changes in a way that requires migration.
Variables ¶
ErrCommandUnavailable is returned by a command the current client cannot serve — one needing a contract this provider does not implement.
It is a distinct error rather than prose so a consumer can tell "this provider cannot do that" from "that went wrong", and say so differently.
var ErrInvalidBaseURL = errors.NewSentinel("chat.invalid_base_url", "invalid chat provider base URL")
ErrInvalidBaseURL is returned when Config.BaseURL fails validation. Callers can distinguish validation failures from other errors via errors.Is.
var ErrInvalidSnapshotID = errors.NewSentinel("chat.invalid_snapshot_id", "invalid snapshot identifier")
ErrInvalidSnapshotID is returned when a snapshot identifier fails validation — not a canonical UUID, contains path separators, or produces a filesystem path outside the store directory.
Callers can distinguish validation failures from I/O failures via errors.Is(err, ErrInvalidSnapshotID).
var ErrMediaRejected = errors.NewSentinel("chat.media_rejected", "media rejected")
ErrMediaRejected is returned when an attachment fails the safety filter — empty, too large, an unidentifiable/disallowed content type, or a declared type that contradicts the sniffed bytes. Wrapped with detail; test with errors.Is.
var ErrMediaUnsupported = errors.NewSentinel("chat.media_unsupported", "media not supported by provider")
ErrMediaUnsupported is returned when the selected provider (or model) cannot accept media, or cannot accept a particular attachment's type.
var ErrModelRejectedParameter = errors.NewSentinel("chat.model_rejected_parameter", "model rejected a generation parameter")
ErrModelRejectedParameter wraps a provider's refusal of a generation parameter for the selected model, as distinct from the provider not supporting that parameter at all — which is a construction error.
The two demand different fixes, which is why they are different errors: a construction error means change provider, this means change model or drop the parameter. Support is per model rather than per provider and moves between model generations, so it cannot be known before the request is made.
if errors.Is(err, chat.ErrModelRejectedParameter) {
// this model will not take the temperature (or effort) that was set
}
The underlying provider error remains reachable by unwrapping.
var ErrStatelessRestore = errors.NewSentinel("chat.stateless_restore", "cannot restore a snapshot into a stateless chat client")
ErrStatelessRestore is returned by PersistentChatClient.Restore on a client built with Config.Stateless. A stateless client retains no conversation, so there is nothing for a snapshot to be restored into — and silently accepting the snapshot and discarding it would hide the caller's mistake. Save still works, and returns a snapshot with no messages.
var ErrThrottled = errors.NewSentinel("chat.throttled", "provider throttled the request")
ErrThrottled wraps a provider throttling failure that survived retry.
It exists so a caller can tell a rate limit from an auth, schema or model error without matching on message text — which is fragile by construction, and worse here than usual because the vendors' own wording misleads. Gemini reports a per-minute input-token burst limit as "You exceeded your current quota, please check your plan and billing details", which sends a reader to billing for a condition that clears in two seconds.
if errors.Is(err, chat.ErrThrottled) {
// the provider is rate-limiting; retries were already exhausted
}
The underlying provider error remains reachable by unwrapping.
var ErrTranscriptMoved = errors.New("the conversation changed while the policy was being applied")
ErrTranscriptMoved reports that the conversation changed while a policy was being applied, so the edit was computed against turns that no longer exist.
A policy may call the model — compaction does — and a client's lock must not be held across that round-trip. So an implementation copies the transcript out, runs the policy unlocked, and checks the conversation has not moved before applying the result. If another goroutine appended in the meantime, applying the edit anyway would silently drop their turns.
It is a retry, not a failure: ask again and the policy runs against the conversation as it now stands.
var ErrUnableToConstruct = errors.NewSentinel("chat.unable_to_construct", "unable to construct client")
ErrUnableToConstruct marks the fatal class of construction failure: no client was returned and none could be.
It is the one question every caller must be able to ask without knowing anything else about the error:
client, err := chat.New(ctx, settings)
if errors.Is(err, chat.ErrUnableToConstruct) {
return err // nothing was built
}
// client is usable; err, if non-nil, lists settings that were not applied
Functions ¶
func ApplyHistoryEdit ¶ added in v0.10.0
func ApplyHistoryEdit(turns []TurnInfo, edit HistoryEdit) ([]int, error)
ApplyHistoryEdit validates an edit against the invariants every policy owes, and returns the indices to keep in order.
A provider calls it between asking its policy and rewriting its transcript. The checks live here rather than in each policy because there is one right answer and several policies: a compacting policy written later inherits them without knowing they exist.
It refuses an edit that drops a pinned turn, splits a tool group, names an index that does not exist, or replaces a turn it did not keep.
Example ¶
package main
import (
"fmt"
"gitlab.com/phpboyscout/go/chat"
)
func main() {
turns := []chat.TurnInfo{
{Index: 0, Role: chat.RoleUser, Text: "cached reference material", Pinned: true},
{Index: 1, Role: chat.RoleUser, Text: "an old question"},
{Index: 2, Role: chat.RoleAssistant, Text: "an old answer"},
}
// Dropping the pinned turn is refused: it is billed as a cached prefix, so
// losing it destroys every later cache hit.
_, err := chat.ApplyHistoryEdit(turns, chat.HistoryEdit{Keep: []int{1, 2}})
fmt.Println(err != nil)
}
Output: true
func AssignText ¶ added in v0.10.0
AssignText writes a provider's raw text answer into an Ask target, per the no-schema half of the ChatClient.Ask contract: with no Config.ResponseSchema set, the model's text is what the caller asked for, and target must be a *string or a json.Unmarshaler.
Any other pointer gets a best-effort JSON unmarshal, so a caller who set no schema but whose model returned JSON anyway still works.
It lives in the core because every provider owes the same behaviour and the contract is stated on the core's own interface. Before spec 0011 only the API-based Claude provider implemented it, so schema-less Ask returned prose on `claude` and failed with "invalid character" on `claude-local` — the same code against two providers of the same model. See https://gitlab.com/phpboyscout/go/chat/-/wikis/specs/0011-chat-provider-conformance §3.2.2.
func BoundConversation ¶ added in v0.11.0
func BoundConversation( ctx context.Context, policy HistoryPolicy, turns []TurnInfo, ) ([]int, map[int]string, error)
BoundConversation asks a policy for an edit, validates it, and returns what the provider should send: the indices to keep, oldest first, and the replacement text for any turn the policy superseded.
It is the middle of the three-step sequence every provider runs — describe, bound, apply — and exists so that only the ends differ per provider. Three providers writing these six lines each is how they drift, which is the failure the conformance suite exists to prevent; and a provider that skips ApplyHistoryEdit gets no validation at all while appearing to work.
A nil policy returns every index unchanged and a nil map, so a provider can call it unconditionally rather than guarding at each site.
Callers pass the turns they have *retained*: the turn being sent is not among them, so a budget of forty means forty turns of history and a request carrying forty-one. That is what ChatClient.History reports, and the agreement is deliberate. Pinned turns are included and marked, never omitted — a policy budgeting against a conversation smaller than the one being sent is a policy that does not do what the caller asked.
An error from the policy, or an edit that breaks an invariant, fails the call. Sending unbounded at the moment the guard broke is the failure the caller set a policy to prevent. A caller who prefers degradation writes it into a policy that catches its own errors, where it is visible.
See https://gitlab.com/phpboyscout/go/chat/-/wikis/specs/0013-history-policy-application D2, D3, D5 and D8.
func ChatHTTPClient ¶
ChatHTTPClient returns the HTTP client a provider should use: the host-injected Config.HTTPClient verbatim when set (go-tool-base injects its hardened transport), otherwise the module's own plain bounded default. This is the seam that keeps the chat core free of any specific HTTP stack.
func Compact ¶ added in v0.12.0
func Compact( ctx context.Context, policy HistoryPolicy, turns []TurnInfo, ) (keep []int, replace map[int]string, changed bool, err error)
Compact bounds the conversation now, regardless of whether either budget has been reached.
A policy runs before a client is about to send something. An interactive request — a person typing "/compact" — has no request to attach to, so it needs a way in that does not wait for one. See spec 0014 D8, and https://gitlab.com/phpboyscout/go/chat/-/issues/15 for the command surface that calls this.
The caller supplies the turns because only a provider can enumerate its own transcript; the returned indices and replacements are applied exactly as a policy's would be. A policy that is not a compacting one is asked to bound normally, so a caller need not check which policy is configured.
It reports whether anything changed, so a command can say "nothing to compact" rather than claiming work it did not do.
func ExecuteTool ¶
func ExecuteTool(ctx context.Context, l *slog.Logger, tools map[string]Tool, name string, input json.RawMessage) string
ExecuteTool looks up a tool by name from the provided registry, executes it, and returns the result as a string. If the result is not a string, it is JSON marshalled. Errors at any stage are returned as formatted error strings suitable for feeding back into the AI conversation (matching existing provider behaviour where tool errors become conversation content rather than aborting the ReAct loop).
A tool handler that panics — handlers run model-generated, adversarial input — is recovered and converted to a tool-error string rather than crashing the process. This holds for both the serial and the parallel dispatch paths, since both route through ExecuteTool.
func GenerateEncryptionKey ¶
GenerateEncryptionKey returns a fresh 32-byte AES-256 key from crypto/rand, suitable for use with WithEncryption. Each snapshot store should use a distinct key obtained either from this helper or from an operator-controlled source such as a KMS or secret manager.
Closes L-2 from docs/development/reports/security-audit-2026-04-17.md — using this helper avoids the footgun of deriving keys from human-readable passphrases, which have insufficient entropy for the AES-GCM threat model.
func GenerateSchema ¶
func GenerateSchema[T any]() *jsonschema.Schema
GenerateSchema creates a JSON schema for a given type T.
The result is assignable straight to Config.ResponseSchema and Tool.Parameters. It returned any until v0.8.0, which meant callers had to assert it back to use it — two codebases independently wrote that assertion with a dead error branch, which is what a bad default looks like from the outside. OpenAI's structured outputs feature uses a subset of JSON schema. The reflector is configured with flags to ensure the generated schema complies with this specific subset.
func MarkAs ¶ added in v0.3.2
MarkAs tags err with a package sentinel so a caller can identify the class of failure without matching on message text, while keeping the provider's own error reachable by unwrapping.
Provider modules should use this rather than wrapping sentinels themselves. Annotation libraries have historically offered a Mark for this, and it is a trap: those markers are recognised only by the library's own Is, so a consumer writing the idiomatic standard-library check gets false. That is how this module shipped an undetectable sentinel once. Both of these hold for the result here, under the standard library:
errors.Is(chat.MarkAs(providerErr, chat.ErrThrottled), chat.ErrThrottled) // true errors.Is(chat.MarkAs(providerErr, chat.ErrThrottled), providerErr) // true
A nil err returns nil, so a call site can wrap unconditionally.
func ProviderHTTPStatus ¶ added in v0.10.0
ProviderHTTPStatus reads the HTTP status a provider SDK error carries, using the extractors registered by whichever provider modules are linked in. The second result is false when no registered extractor recognises the error, and when none is registered at all.
It is the exported view of what FailoverPolicy classifies on, so a caller can ask why a call failed over — and so the provider-conformance suite can assert that an error chain survives a provider's own wrapping rather than being flattened to a string. See https://gitlab.com/phpboyscout/go/chat/-/wikis/specs/0011-chat-provider-conformance.
func RegisterCapabilities ¶ added in v0.6.0
func RegisterCapabilities(name Provider, resolver CapabilityResolver)
RegisterCapabilities registers a capability resolver for a provider. Call it from an init() function alongside RegisterProvider, so an adapter cannot be linked without its capabilities coming with it.
Registering twice for the same provider replaces the previous resolver, which keeps test doubles simple.
func RegisterProvider ¶
func RegisterProvider(name Provider, factory ProviderFactory)
RegisterProvider registers a factory function for a provider name. Call this from an init() function in your provider file or external package.
func RegisterStatusExtractor ¶
func RegisterStatusExtractor(extractor HTTPStatusExtractor)
RegisterStatusExtractor adds a provider HTTP-status extractor to the registry. Call it from a provider module's init(); a nil extractor is ignored.
func ResolveAPIKey ¶
func ResolveAPIKey( ctx context.Context, direct string, credential CredentialConfig, envFallback string, ) string
ResolveAPIKey implements the five-step precedence above. Whitespace is trimmed at every step and empty values fall through so a half-configured key cannot mask a fully-configured one at a lower priority.
It is exported because the per-provider modules (chat-anthropic, chat-openai, chat-gemini) call it to resolve their API key, each passing its own well-known fallback env var.
direct is the caller-supplied value (typically Config.Token); pass "" to skip it. credential is the already-adapted provider config, owned by this package rather than the host application's config system. envFallback is the well-known unprefixed environment variable name for the provider (e.g. "ANTHROPIC_API_KEY").
func ValidateBaseURL ¶
ValidateBaseURL returns nil if baseURL is acceptable for use as a chat provider endpoint, or an error wrapping ErrInvalidBaseURL otherwise.
An empty baseURL is always accepted — callers that require a value (e.g. ProviderOpenAICompatible) must enforce non-emptiness separately. Every non-empty URL is checked against the seven rejection rules documented at the top of this file.
Pass allowInsecure=true ONLY from tests that point at an net/http/httptest.Server (which serves HTTP). Production callers must leave it false; the Config.AllowInsecureBaseURL field that drives this is tagged `json:"-"` so config files cannot set it.
Downstream tool authors should call this at the boundary where they accept BaseURL input (their own setup wizard, CLI flag, env var) so misconfiguration surfaces early rather than at New time.
func ValidateSnapshotID ¶
ValidateSnapshotID returns nil if id is a canonical UUID that will be accepted by the [FileStore] methods, or an error wrapping ErrInvalidSnapshotID otherwise.
Use this at the boundary of your own system — e.g. in a CLI flag or HTTP handler that accepts a snapshot identifier from an external source — so validation happens before the value reaches Save, Load, or Delete.
Types ¶
type CachingChatClient ¶ added in v0.5.0
type CachingChatClient interface {
ChatClient
// AddCached appends a user turn and asks the provider to cache it, so
// later calls carrying the same prefix are billed at the cached rate.
//
// A hint, not a guarantee. Every provider declines to cache content below
// a minimum size — 1024 tokens on Gemini and OpenAI, and between 512 and
// 4096 on Claude depending on the model — and most do so silently. The
// call still succeeds and the content is still sent; it is simply not
// cached.
//
// Confirm the effect through Usage.CachedTokens rather than assuming it.
// That is what the providers' own documentation recommends, and a number
// that moves is worth more than a promise in this interface.
AddCached(ctx context.Context, prompt string, media ...Media) error
}
CachingChatClient extends ChatClient with explicit prompt caching.
Providers that can carry a cache annotation implement it; discover support the same way as StreamingChatClient and PersistentChatClient:
if cc, ok := client.(chat.CachingChatClient); ok {
err = cc.AddCached(ctx, referenceCorpus)
}
Caching changes what a call costs, never what the model is asked, so a provider that cannot do it simply does not implement this — unlike Config.Stateless or Config.Temperature, where being ignored would corrupt the result and is therefore a construction error.
type CachingSupport ¶ added in v0.6.0
type CachingSupport struct {
// Explicit reports whether content can be marked for caching deliberately.
Explicit Support
// Implicit reports whether the provider caches eligible prefixes with no
// action from the caller.
Implicit Support
// ImplicitMinTokens is the prefix size at which implicit caching begins to
// engage. Zero means unknown.
ImplicitMinTokens int
// MinCacheableTokens is the smallest block a provider will cache at all.
// Zero means unknown.
MinCacheableTokens int
// TTLs lists the retention periods the provider accepts. Empty means
// unknown; providers offer discrete choices rather than arbitrary durations.
TTLs []time.Duration
}
CachingSupport carries the specifics of CapCaching.
This is the capability that shows why a bare bool is not enough: "caching is supported" is not actionable, while "implicit caching engages above N tokens" tells a caller whether restructuring a prompt is worth it.
type Capabilities ¶ added in v0.6.0
type Capabilities struct {
// Supported maps each capability to its support level. A capability absent
// from the map reports SupportUnknown, which is the zero value, so a
// partially-populated map is always safe to read.
Supported map[Capability]Support
// Sampling, Effort and Caching carry specifics where a provider supplied
// them. Nil means the provider said nothing.
Sampling *SamplingSupport
Effort *EffortSupport
Caching *CachingSupport
}
Capabilities reports what a provider and model support.
Read three-valued support with Capabilities.Support. The pointer fields carry per-capability specifics and are nil when the provider said nothing — which is distinct from a provider that answered with zeroes.
func (Capabilities) Support ¶ added in v0.6.0
func (c Capabilities) Support(cap Capability) Support
Support reports whether cap is supported. An unpopulated map, an absent key and an explicitly-unknown entry all report SupportUnknown.
There is deliberately no Supports() bool. Any boolean has to choose a reading of SupportUnknown, and either choice is wrong about half the time — reading it as unsupported makes claude-local look featureless, and reading it as supported promises things that may not work. A switch that ignores a case is visible in review; a misread bool is not.
type Capability ¶ added in v0.6.0
type Capability string
Capability names one thing a caller can ask a provider to do.
The set is closed and hand-chosen: the module defines the vocabulary and providers answer in it. It is deliberately not derived from whatever a vendor happens to report, because mirroring one vendor's product surface would make the vocabulary move whenever that vendor ships a feature.
A name earns its place by being something the module exposes, being meaningful for every provider (including those that lack it), and being something a caller would change behaviour over.
See https://gitlab.com/phpboyscout/go/chat/-/wikis/specs/0006-capability-reporting.
const ( // CapSampling covers Config.Temperature and Config.TopP. CapSampling Capability = "sampling" // CapEffort covers Config.Effort. CapEffort Capability = "effort" // CapCaching covers CachingChatClient.AddCached and Config.CacheTTL. CapCaching Capability = "caching" // CapStateless covers Config.Stateless. CapStateless Capability = "stateless" // CapStreaming covers StreamingChatClient.StreamChat. CapStreaming Capability = "streaming" // CapPersistence covers PersistentChatClient save and restore. CapPersistence Capability = "persistence" // CapStructuredOutput covers Config.ResponseSchema. CapStructuredOutput Capability = "structured_output" // CapTools covers tool calling. CapTools Capability = "tools" // CapMultimodal covers image and PDF input parts. CapMultimodal Capability = "multimodal" )
type CapabilityResolver ¶ added in v0.6.0
CapabilityResolver reports what a provider supports for a given model.
Provider modules register one in init() alongside their factory. The model string is whatever the caller configured, which may be empty (meaning the provider default) or a model the resolver has never heard of — in both cases the resolver should report SupportUnknown rather than guessing.
type ChatClient ¶
type ChatClient interface {
// Add appends a user message to the conversation history without
// triggering a completion. The message persists for subsequent
// Chat() or Ask() calls.
//
// Under Config.Stateless the message is buffered rather than retained: it
// is sent with the next Chat/Ask/StreamChat call and then cleared.
Add(ctx context.Context, prompt string, media ...Media) error
// Ask sends a question and unmarshals the structured response into
// target. If Config.ResponseSchema was set during construction, the
// provider enforces that schema. If no schema is set, the provider
// returns the raw text content unmarshalled into target (which must
// be a *string or implement json.Unmarshaler).
//
// The question and the answer are appended to the conversation, so a
// subsequent call re-sends them. Callers making independent calls — batch
// classification, extraction, judging — want Config.Stateless; without it,
// each call carries every call before it.
//
// Ask is tool-free: tools registered with SetTools are not offered to the
// model. Ask promises a structured answer, and a model that replies with a
// tool call has not delivered one. Use Chat for tool use, then Ask to shape
// the result — Chat commits its tool calls and their results to the
// conversation, so a following Ask can read them:
//
// client.SetTools(tools)
// _, _ = client.Chat(ctx, "research X with the tools")
// var out Report
// _ = client.Ask(ctx, "now give me that as JSON", &out)
//
// That idiom costs an extra round-trip, and it does not work under
// Config.Stateless, where the session is dropped rather than committed.
//
// The contract is enforced by the shared conformance suite as each provider
// module adopts a core release carrying it, so a provider pinned to an
// earlier core may still offer tools to Ask.
Ask(ctx context.Context, question string, target any, media ...Media) error
// SetTools configures the tools available to the AI. This replaces
// (not appends to) any previously set tools.
SetTools(tools []Tool) error
// Chat sends a message and returns the response content. If tools
// are configured, the provider handles tool calls internally via a
// ReAct loop bounded by Config.MaxSteps (default 20).
//
// Like Ask, the exchange is appended to the conversation and re-sent by the
// next call unless Config.Stateless is set.
Chat(ctx context.Context, prompt string, media ...Media) (string, error)
// Usage returns the cumulative token usage across every provider
// round-trip made by this client instance since construction. A single
// Chat/Ask/StreamChat call may make multiple round-trips (one per ReAct
// step) and all are summed. Providers that do not report token counts
// (e.g. ProviderClaudeLocal) contribute a zero, Known == false Usage.
// Wire Config.UsageObserver to observe usage per round-trip instead.
Usage() Usage
// History reports the conversation this client is carrying — how many turns
// it will re-send on the next call, and the provider's own token count for
// the last call it made.
//
// Every provider appends and re-sends, so a long-lived conversation grows
// until it overflows the model's context window. This is what makes that
// growth visible in time to do something about it: reset the client, or set
// Config.Stateless if the calls were independent all along.
//
// Check History.Known before trusting Turns.
History() History
}
ChatClient defines the interface for interacting with a chat service.
Conversation state ¶
By default a client is a conversation: every Chat, Ask and StreamChat call appends the exchange to the client's history, and the next call re-sends all of it. Message history from Add() calls persists the same way. To start a fresh conversation, create a new client via chat.New().
That default is expensive for batch work. A caller holding one client across N independent calls pays for the accumulated prefix on every call — cost grows with the square of N — and each call is answered in the light of the ones before it. Set Config.Stateless for one-shot calls that neither read nor retain history.
Concurrency ¶
Conversational (default) clients are NOT safe for concurrent use by multiple goroutines; each goroutine should use its own instance.
Stateless provider clients ARE safe for concurrent use — one client can serve a pool of workers, which is the shape batch work wants. Two exceptions:
- The fallback composite from NewFallback is never safe for concurrent use, stateless or not: it advances its active provider mid-call. Give each worker its own composite.
- A provider that does not implement StatelessCapable makes no such guarantee, and New refuses to build one with Config.Stateless set.
Example (History) ¶
package main
import (
"context"
"fmt"
"gitlab.com/phpboyscout/go/chat"
)
func main() {
client, err := chat.NewClaudeLocal(context.Background(),
chat.Settings{Config: chat.Config{}},
chat.WithClaudeBinaryLookup(func(string) (string, error) {
return "/usr/local/bin/claude", nil
}),
)
if err != nil {
return
}
h := client.History()
// Known is false for a provider that cannot count its own transcript —
// claude-local delegates the conversation to the CLI's own session.
fmt.Println(h.Turns, h.LastInputTokens, h.Known)
}
Output: 0 0 false
func New ¶
func New(ctx context.Context, settings Settings) (ChatClient, error)
New creates a ChatClient for the configured provider.
func NewClaudeLocal ¶ added in v0.10.0
func NewClaudeLocal(ctx context.Context, settings Settings, opts ...ClaudeLocalOption) (ChatClient, error)
NewClaudeLocal builds a client backed by a locally installed, already authenticated claude CLI. No API key is required — the binary handles its own authentication.
Reach for it directly when you need one of the ClaudeLocalOption knobs; otherwise New with Provider set to ProviderClaudeLocal does the same job through the registry.
It runs the same validation New does, so a setting this provider cannot honour — Config.Temperature, say, which the CLI has no flag for at any version — is still a construction error rather than a silent miss.
func NewFallback ¶
func NewFallback(clients []ChatClient, opts ...FallbackOption) (ChatClient, error)
NewFallback builds a composite ChatClient that tries clients in order, advancing to the next on a retryable failure. The first client is the primary; the rest are fallbacks. At least one client is required.
Retry happens before failover ¶
A provider's own retry runs inside the call, so the composite only sees a throttling error once those retries are exhausted. That ordering is deliberate: a per-minute burst limit clears in seconds, and waiting is cheaper than moving to a provider with a different model, different pricing and a lossy transcript replay. Failing over on the first 429 would make a burst limit look like an outage and strand a long batch on a fallback for a condition that resolves itself.
Set Config.MaxRetries to 0 for the opposite behaviour — advance immediately rather than waiting out the primary.
The returned client also satisfies StreamingChatClient iff every supplied client does.
func NewFallbackFromConfigs ¶
func NewFallbackFromConfigs(ctx context.Context, cfgs []Config, opts ...FallbackOption) (ChatClient, error)
NewFallbackFromConfigs constructs each provider from Config values and wraps the result in a composite. Use NewFallbackFromSettings when each provider needs distinct construction dependencies.
func NewFallbackFromSettings ¶
func NewFallbackFromSettings(ctx context.Context, settings []Settings, opts ...FallbackOption) (ChatClient, error)
NewFallbackFromSettings constructs each provider via New and wraps the result in a composite. The first Settings value is the primary. A construction failure for a non-primary provider (e.g. a missing credential) is downgraded to a logged WARN and that provider is dropped, so one missing fallback credential does not break the whole client; if the primary fails to construct, the error is returned.
func NewWithFactory ¶ added in v0.13.0
func NewWithFactory(ctx context.Context, settings Settings, factory ProviderFactory) (ChatClient, error)
NewWithFactory creates a ChatClient from a provider's own factory, running every guard New runs around the one the registry supplies.
It exists for a provider module's second door: a constructor taking options that cannot travel through Config — a seed, an injected SDK constructor — which still has to validate the endpoint before credentials reach it, resolve the logger its factory will log through, apply provider defaults, drop what cannot be applied, and assert the built client can carry what survived.
func New(ctx context.Context, settings chat.Settings, opts ...Option) (chat.ChatClient, error) {
return chat.NewWithFactory(ctx, settings, func(ctx context.Context, s chat.Settings) (chat.ChatClient, error) {
return build(ctx, s, opts...)
})
}
Handing over the factory rather than calling a validation helper first is deliberate. A helper is something a module can forget to call, and two of them did: an http:// endpoint was accepted with a nil error and the API key went over the wire in cleartext, while a call with no logger panicked. See https://gitlab.com/phpboyscout/go/chat/-/wikis/specs/0015-one-construction-path.
func NewWithFallbackSettings ¶
func NewWithFallbackSettings(ctx context.Context, settings Settings, fallback FallbackConfig, opts ...FallbackOption) (ChatClient, error)
NewWithFallbackSettings builds a single provider client or fallback composite from package-owned settings and fallback config. Per the resolved OQ-3, fallback.Providers[0] is the primary and overrides Settings.Config.Provider.
type ClaudeLocal ¶
type ClaudeLocal struct {
UsageTracker
// contains filtered or unexported fields
}
ClaudeLocal implements the ChatClient interface using a locally installed claude CLI binary. This provider is useful in environments where direct API access to api.anthropic.com is blocked but the pre-authenticated claude binary is permitted.
func (*ClaudeLocal) Ask ¶
Ask sends a question to the local claude binary and unmarshals the structured response into the target using --json-schema for schema-enforced output.
func (*ClaudeLocal) Chat ¶
Chat sends a message to the local claude binary and returns the text response.
func (*ClaudeLocal) History ¶ added in v0.10.0
func (c *ClaudeLocal) History() History
History reports what this client is carrying.
Turns counts only the locally buffered Add() turns, and Known is false: the conversation itself lives in the claude CLI's own session, resumed by ID, so the transcript the far side will replay is not something this provider can see or count. Reporting a confident number for the buffer alone would be worse than admitting the gap — a caller watching for growth would see a conversation that never grows.
func (*ClaudeLocal) SetTools ¶
func (c *ClaudeLocal) SetTools(_ []Tool) error
SetTools is not supported in Phase 1 of ProviderClaudeLocal. Tool integration via MCP server is planned for a future release.
func (*ClaudeLocal) SupportsEffort ¶ added in v0.3.0
func (c *ClaudeLocal) SupportsEffort()
SupportsEffort marks ClaudeLocal as honouring Config.Effort. The CLI's --effort flag takes the same five levels as the Anthropic API it wraps, so the neutral ladder maps across without clamping.
Note the absence of SupportsSampling: the claude CLI exposes no temperature or top-p flag at any version, so ClaudeLocal is the provider that holds one generation-capability marker and not the other.
func (*ClaudeLocal) SupportsStateless ¶ added in v0.2.0
func (c *ClaudeLocal) SupportsStateless()
SupportsStateless marks ClaudeLocal as honouring Config.Stateless.
Statelessness here means not resuming: a stateless call omits --resume and discards the session ID the CLI returns, so each invocation starts a fresh session and the transcript never accumulates on the far side.
type ClaudeLocalOption ¶ added in v0.10.0
type ClaudeLocalOption func(*claudeLocalOptions)
ClaudeLocalOption configures a ProviderClaudeLocal client at construction.
These knobs live here rather than on Config because only one provider reads them. Config carries what every provider understands; anything a single provider owns belongs to that provider's own constructor, where the compiler can see it. See https://gitlab.com/phpboyscout/go/chat/-/wikis/specs/0011-chat-provider-conformance D10.
func WithClaudeBinaryLookup ¶ added in v0.10.0
func WithClaudeBinaryLookup(lookPath func(string) (string, error)) ClaudeLocalOption
WithClaudeBinaryLookup overrides how the claude binary is located, replacing exec.LookPath. It is the seam for testing the not-installed path without a machine that lacks the binary.
func WithClaudeCommand ¶ added in v0.10.0
func WithClaudeCommand(command func(context.Context, string, ...string) *exec.Cmd) ClaudeLocalOption
WithClaudeCommand overrides how the claude subprocess is built, replacing exec.CommandContext. It is the seam that lets the provider be driven against a fake subprocess — the provider-conformance suite uses it to supply backends that hang, fail or return a scripted result.
type Command ¶ added in v0.14.0
type Command struct {
// Name is the verb, without its leading slash.
Name string
// Summary is one line, shown by [HelpCommand].
Summary string
// Run performs it. A nil Run makes the command listable but inert, which is
// useful for describing a command the consumer handles itself.
Run CommandFunc
}
Command is one verb a user can invoke against a conversation.
func ClearCommand ¶ added in v0.14.0
func ClearCommand() Command
ClearCommand empties the conversation, keeping the configuration.
The system prompt and model are carried forward deliberately. Restoring a bare empty snapshot takes them with it, which would let whoever is typing delete the consumer's system instruction with a keystroke — a command changes what is in a conversation, never how the client is configured. See spec 0016 D3 and D10.
Turns added with AddCached go, because AddCached appends a user turn and a clear that leaves user content behind is not a clear. It does not waste the cache: clearing stops referencing one rather than destroying it, so re-adding the same content hits it again inside its TTL.
Requires PersistentChatClient; a client without it returns ErrCommandUnavailable.
func CompactCommand ¶ added in v0.14.0
func CompactCommand() Command
CompactCommand applies the configured history policy on demand.
It is the interactive trigger for what CompactOldest does automatically: a policy runs when the client is about to send something, and a person typing "/compact" has no request to attach to.
It applies whatever Config.HistoryPolicy holds. A caller who configured truncation gets truncation — the command asks for the conversation to be bounded now, not for a particular way of bounding it.
Requires TranscriptEditor; a provider without it returns ErrCommandUnavailable.
func HelpCommand ¶ added in v0.14.0
func HelpCommand() Command
HelpCommand lists the registered vocabulary, including whatever the consumer added.
It is the one built-in that gets more useful as a consumer registers their own, and the reason CommandRequest carries the set.
func ToolsCommand ¶ added in v0.14.0
ToolsCommand lists the tools a user can expect the model to reach for.
The consumer supplies them because ChatClient.SetTools is write-only: there is no way to read a registered set back off a client, and inventing one to serve a help command would be the wrong reason to widen the interface. Pass the same slice given to SetTools.
func UndoCommand ¶ added in v0.14.0
func UndoCommand() Command
UndoCommand removes the most recent exchange.
The operation is a HistoryPolicy the core supplies, so this needs nothing from a provider beyond TranscriptEditor — the same seam `/compact` uses.
Requires TranscriptEditor; a provider without it returns ErrCommandUnavailable.
func UsageCommand ¶ added in v0.14.0
func UsageCommand() Command
UsageCommand reports what the conversation has cost so far.
type CommandFunc ¶ added in v0.14.0
type CommandFunc func(ctx context.Context, req CommandRequest) (CommandResult, error)
CommandFunc runs one command.
type CommandRequest ¶ added in v0.14.0
type CommandRequest struct {
// Client is the client the command acts on.
Client ChatClient
// Args is everything after the verb, trimmed. Empty when none was given.
Args string
// Set is the whole registered set, so a command can describe its siblings.
// [HelpCommand] is the reason this is here.
Set CommandSet
}
CommandRequest is what a handler is given.
type CommandResult ¶ added in v0.14.0
type CommandResult struct {
// Name is the command that ran, without its leading slash.
Name string
// Text is human-readable output. It may be empty for a command that only
// acts.
Text string
// Changed reports whether the conversation was altered.
Changed bool
}
CommandResult is what a command produced.
Data rather than a bare string: a consumer rendering /usage wants more than a sentence, and Changed is what lets a UI refresh a turn counter without guessing whether the transcript moved.
func RunCommand ¶ added in v0.14.0
func RunCommand( ctx context.Context, client ChatClient, set CommandSet, input string, ) (CommandResult, bool, error)
RunCommand dispatches input against set, and reports whether it did.
It is the consumer's own call, on input the consumer chose to treat as a candidate — the library never inspects a prompt on its way to a model. That is what makes escaping a non-problem and prompt injection impossible through this path: text the consumer did not pass here is never parsed as a command. See https://gitlab.com/phpboyscout/go/chat/-/wikis/specs/0016-slash-commands D1 and D2.
if res, handled, err := chat.RunCommand(ctx, client, commands, input); handled {
render(res)
continue
}
reply, err := client.Chat(ctx, input)
Input that is not a registered command returns handled false and is otherwise untouched, so it can go straight to the model. A registered command that fails is still handled: the error is the command's, not a signal to send the text on.
type CommandSet ¶ added in v0.14.0
type CommandSet []Command
CommandSet is the vocabulary a consumer offers. Order is preserved for display; lookup is by name.
type CompactOption ¶ added in v0.12.0
type CompactOption func(*compactOldest)
CompactOption configures a compacting policy. Every option has a default that works, so CompactOldest takes none in the simple case.
func WithCompactionFallback ¶ added in v0.12.0
func WithCompactionFallback(fallback HistoryPolicy) CompactOption
WithCompactionFallback bounds the conversation with fallback when summarisation fails, instead of failing the call.
Off by default. A compaction failure fails the call, per spec 0013 D3: a caller who asked for summarisation and silently got truncation learns it from the model's behaviour rather than from an error. Opting in makes the degradation visible where it was chosen.
func WithSummarizer ¶ added in v0.12.0
func WithSummarizer(cfg Config) CompactOption
WithSummarizer summarises through a client built from cfg rather than from the live client's configuration.
Use it to summarise with a cheaper model than the conversation runs on. Tools, response schema and history policy are cleared from the copy regardless: a summarisation call must not carry the caller's tools, must not be forced into their response schema, and must not be able to trigger a compaction of its own.
func WithSummarizerClient ¶ added in v0.12.0
func WithSummarizerClient(client ChatClient) CompactOption
WithSummarizerClient summarises through a client the caller built and owns.
The caller takes on what the derived client would otherwise guarantee: it must not carry a Config.HistoryPolicy that could re-enter compaction, and its tools and response schema are its own business. Config.Stateless is the straightforward way to be sure of the first.
func WithSummaryPrompt ¶ added in v0.12.0
func WithSummaryPrompt(prompt string) CompactOption
WithSummaryPrompt replaces the house summarisation prompt.
Set it where a domain has terms worth preserving that a general prompt would discard — order numbers, case references, the units a measurement is in. The transcript is still appended as delimited data, so the framing that keeps it from reading as instructions survives an override.
func WithTokenBudget ¶ added in v0.12.0
func WithTokenBudget(tokens int) CompactOption
WithTokenBudget sets an absolute input-token count above which compaction runs, replacing the default share of the model's reported limit.
The count compared against it is the provider's own figure for the previous call, so it is one call out of date and absent before the first call. The turn budget is the backstop for both cases.
func WithTurnBudget ¶ added in v0.12.0
func WithTurnBudget(turns int) CompactOption
WithTurnBudget sets the retained-turn count above which compaction runs.
type Config ¶
type Config struct {
// Provider is the AI service provider to use.
Provider Provider
// Model is the specific model to use (e.g. "claude-opus-5", "gpt-5.6-sol").
//
// Empty selects the provider's default — see [DefaultModelClaude],
// [DefaultModelOpenAI] and [DefaultModelGemini]. Those are chosen by one
// rule applied to every provider: the most capable model the provider makes
// generally available that supports the module's baseline capabilities.
//
// The defaults therefore favour capability over cost, because a default is
// what a caller gets having expressed no preference. Set this field if cost
// matters, if you need a particular model's behaviour, or if you want a
// model pinned against vendor churn.
Model string
// Token is the API key or token for the service.
Token string
// Credentials carries provider-specific credential config resolved by the
// host application. Token still wins when both are set.
Credentials CredentialConfig `json:"-" mapstructure:"-" yaml:"-"`
// Project is the cloud project a provider addresses. Required by
// ProviderGeminiVertex, which falls back to GOOGLE_CLOUD_PROJECT when it is
// empty and refuses to construct when both are. Ignored by every other
// provider.
//
// It lives here rather than on a provider's constructor because this is the
// only surface a config file decodes into, and an option is a function call.
// See spec 0017 D7 and D10.
Project string
// Location is the region a provider addresses. Required by
// ProviderGeminiVertex, which falls back to GOOGLE_CLOUD_LOCATION when it is
// empty and refuses to construct when both are.
//
// Named for the concept rather than for one vendor: AWS Bedrock addresses by
// region and means the same thing by it. See spec 0017 D7.
Location string
// BaseURL overrides the API endpoint. Required when using ProviderOpenAICompatible.
// Example: "http://localhost:11434/v1" for Ollama, "https://api.groq.com/openai/v1" for Groq.
BaseURL string
// SystemPrompt is the initial system prompt to set the context for the AI.
SystemPrompt string
// ResponseSchema is the JSON schema used to force a structured output from
// the AI. Build one with [GenerateSchema].
//
// Typed since v0.8.0; it was any beforehand, so the only statement of its
// contract was a check in New that the compiler could not read.
ResponseSchema *jsonschema.Schema
// SchemaName is the name of the response schema (e.g., "error_analysis").
SchemaName string
// SchemaDescription is a description of the response schema.
SchemaDescription string
// MaxSteps limits the number of ReAct loop iterations in Chat().
// Zero means use the default (DefaultMaxSteps = 20).
MaxSteps int
// MaxTokens sets the maximum tokens per response.
// Zero means use the provider default (OpenAI: 4096, Claude: 8192, Gemini: 8192).
MaxTokens int
// RequestTimeout bounds a single HTTP request to the provider. Zero falls
// back to the ai.request_timeout config key, then DefaultChatRequestTimeout.
// Bounded on purpose so a stuck model fails rather than hanging forever.
RequestTimeout time.Duration
// HTTPClient, when non-nil, is used verbatim for provider API calls. The
// host injects a hardened transport here (go-tool-base supplies one built on
// the go/httpclient module);
// nil ⇒ the module builds a plain bounded client from RequestTimeout.
HTTPClient *http.Client `json:"-" mapstructure:"-" yaml:"-"`
// HistoryPolicy bounds the conversation before each call. Nil leaves it
// unbounded, which is the default and what every provider did before.
//
// Use it for a conversation that legitimately accumulates. For independent
// calls that never needed history at all, Stateless is the cheaper answer.
//
// [TruncateOldest] is the policy the module ships. A policy never drops a
// turn added with AddCached and never splits a tool request from its
// results; [ApplyHistoryEdit] enforces both, so a policy that gets them
// wrong fails loudly rather than corrupting a transcript.
HistoryPolicy HistoryPolicy `json:"-" mapstructure:"-" yaml:"-"`
// ParallelTools enables concurrent execution of multiple tool calls
// within a single ReAct step. Disabled by default.
ParallelTools bool
// MaxParallelTools limits the number of tools executing concurrently.
// Zero means use the default (5). Only effective when ParallelTools is true.
MaxParallelTools int
// Temperature, when non-nil, overrides the provider's default sampling
// temperature. Nil samples at the provider's default.
//
// Passed through in the provider's own units and never rescaled: ranges
// differ (Claude 0-1, OpenAI 0-2, Gemini 0-2) and quietly rescaling what
// the caller asked for would be worse than making them read the range. The
// value is validated against the selected provider's range before any
// request is made.
//
// Support is narrowing sharply as vendors move to reasoning models, and
// **none of the three default models accepts it**: it is deprecated on
// claude-opus-5, rejected by gpt-5.6-sol (which permits only its default of
// 1), and absent from claude-local entirely. Gemini still accepts it, as do
// older models on every provider — but reaching them means setting [Model]
// explicitly.
//
// Setting it against a provider that has no sampling concept is a
// construction error; a model that refuses it yields
// [ErrModelRejectedParameter] at request time. See [Effort], which is the
// forward-looking control, is supported everywhere, and is what these
// models expect to be steered with instead.
//
// Lower values reduce variance between runs. They do not make output
// deterministic — see the sampling how-to.
Temperature *float64
// TopP, when non-nil, overrides the provider's default nucleus-sampling
// threshold, on a 0-1 scale. Nil samples at the provider's default.
//
// Providers recommend adjusting temperature or top-p, not both.
//
// Its support is narrowing alongside [Temperature]: gpt-5.6-sol rejects it
// outright, where gpt-5.4 accepted it. The same guidance applies — prefer
// [Effort] on reasoning models, or set [Model] to one that still takes it.
TopP *float64
// Effort selects how much reasoning the model spends before answering.
// Empty uses the provider's default.
//
// This is the forward-looking sibling of Temperature: every provider
// supports it, including claude-local, and on reasoning models it is the
// control that replaced temperature. It is also the most direct cost dial
// in Config — observe its effect through Usage.ReasoningTokens.
Effort Effort
// CacheTTL requests how long a provider retains content marked with
// CachingChatClient.AddCached. Zero uses the provider default.
//
// Providers offer discrete choices rather than arbitrary durations and
// select the nearest they support: Claude offers 5 minutes or 1 hour,
// Gemini takes a duration. The longer option costs more to write and is
// what makes caching pay at moderate call rates, so it is a decision for
// the caller rather than a default.
CacheTTL time.Duration
// MaxRetries bounds automatic retries of transient provider failures —
// throttling, request timeouts, 5xx. Nil uses the provider's own default,
// which is 2 on Claude and OpenAI and is matched by Gemini.
//
// Zero disables retry, for a caller who would rather fail fast than have a
// call silently take tens of seconds. That is a coherent thing to want:
// with a fallback composite configured, MaxRetries: 0 moves to the next
// provider on the first throttle instead of waiting out the primary.
//
// Retries honour the provider's own Retry-After hint where one is given —
// a delay guessed from outside is worse than the answer already supplied.
//
// Note that RequestTimeout bounds a single request and nothing bounds the
// retry sequence as a whole, so a high MaxRetries against a provider
// supplying long hints can take a while. Cancel the context to bound it.
MaxRetries *int
// Stateless makes every Chat, Ask and StreamChat call a one-shot: the
// client neither seeds the request with prior turns nor retains the turns
// from this call. One client then serves many independent calls with no
// per-call construction cost and no cross-call contamination.
//
// The zero value keeps the conversational behaviour: history accumulates
// across calls on a single client instance. That is right for a
// conversation and wrong for batch work, where holding one client across N
// independent calls re-sends the whole accumulated prefix every time —
// quadratic input cost, and call N answered in the light of calls 1..N-1
// without the caller ever asking for it.
//
// Tool calling is unaffected. Stateless means "no history across calls",
// not "no messages within one": the ReAct loop keeps the assistant and tool
// turns it needs to make progress, and discards them when the call returns.
//
// Add still works, and buffers a one-shot preamble — the buffered turns go
// out with the next call and are then cleared rather than retained.
//
// Stateless provider clients are safe for concurrent use by multiple
// goroutines; see [ChatClient] for the exceptions. Setting this against a
// provider module that predates the flag is a construction error rather
// than a silent miss — see [StatelessCapable].
Stateless bool
// UsageObserver, when non-nil, is invoked once per provider round-trip
// with that round-trip's token usage. A ReAct tool-calling loop fires it
// once per step. This is the opt-in hook for emitting a telemetry event,
// metric, or log line; the chat client never depends on a telemetry
// collector. Providers that do not report usage still fire the observer
// with a Known == false Usage. The callback runs synchronously on the
// calling goroutine, so keep it fast and non-blocking.
UsageObserver func(Usage) `json:"-" mapstructure:"-" yaml:"-"`
// AllowInsecureBaseURL permits HTTP (non-HTTPS) BaseURLs. This is
// exclusively for tests that point at an httptest.Server. Production
// callers must leave this false. The field is tagged json:"-" so
// config files cannot enable it.
AllowInsecureBaseURL bool `json:"-" mapstructure:"-" yaml:"-"`
}
Config holds configuration for a chat client.
type ConfigErrors ¶ added in v0.6.0
type ConfigErrors struct {
// contains filtered or unexported fields
}
ConfigErrors aggregates everything wrong with one construction.
Construction reports every problem rather than the first, so a caller fixing three mistakes learns all three from one call instead of one round-trip at a time.
It implements multi-error unwrapping, so errors.Is finds any member sentinel whether it was the only problem or one of four.
func (*ConfigErrors) Dropped ¶ added in v0.6.0
func (e *ConfigErrors) Dropped() []DroppedSetting
Dropped reports the settings that were not applied.
It is structured on purpose: a caller deciding whether the shortfall matters needs to branch on which settings were lost, and parsing that back out of a message string is not a contract anyone should depend on.
func (*ConfigErrors) Error ¶ added in v0.6.0
func (e *ConfigErrors) Error() string
Error implements error.
func (*ConfigErrors) Fatal ¶ added in v0.6.0
func (e *ConfigErrors) Fatal() bool
Fatal reports whether construction failed outright. Equivalent to errors.Is(err, ErrUnableToConstruct), and cheaper when the type is in hand.
func (*ConfigErrors) Unwrap ¶ added in v0.6.0
func (e *ConfigErrors) Unwrap() []error
Unwrap returns the individual errors, so stdlib errors.Is and errors.As reach every member.
type ConversationStore ¶
type ConversationStore interface {
// Save writes a snapshot to the store.
Save(ctx context.Context, snapshot *Snapshot) error
// Load retrieves a snapshot by ID.
Load(ctx context.Context, id string) (*Snapshot, error)
// List returns summaries of all stored snapshots.
List(ctx context.Context) ([]SnapshotSummary, error)
// Delete removes a snapshot by ID.
Delete(ctx context.Context, id string) error
}
ConversationStore persists and retrieves conversation snapshots.
func NewFileStore ¶
func NewFileStore(fs afero.Fs, dir string, opts ...FileStoreOption) (ConversationStore, error)
NewFileStore creates a ConversationStore that persists snapshots as JSON files. Files are stored in dir with 0600 permissions. The directory is created with 0700 permissions if it doesn't exist.
Example ¶
package main
import (
"github.com/spf13/afero"
"gitlab.com/phpboyscout/go/chat"
)
func main() {
// Create a FileStore for persisting chat conversation snapshots.
store, err := chat.NewFileStore(afero.NewMemMapFs(), "/conversations")
if err != nil {
return
}
// Save, Load, List, Delete snapshots
_ = store
}
Output:
Example (WithEncryption) ¶
package main
import (
"github.com/spf13/afero"
"gitlab.com/phpboyscout/go/chat"
)
func main() {
// Encrypt stored snapshots with AES-256-GCM (key must be 32 bytes).
key := make([]byte, 32) // In real usage, use a secure key source
store, err := chat.NewFileStore(afero.NewMemMapFs(), "/conversations",
chat.WithEncryption(key),
)
if err != nil {
return
}
_ = store
}
Output:
type CredentialConfig ¶
type CredentialConfig struct {
Env string `mapstructure:"env"`
Keychain string `mapstructure:"keychain"`
Key string `mapstructure:"key"`
// Lookup resolves the Keychain reference. Injected by the host (nil ⇒ the
// keychain step is skipped); never decoded from config.
Lookup KeychainLookup `mapstructure:"-" json:"-"`
}
CredentialConfig is the package-owned shape for provider api credentials.
func (CredentialConfig) IsZero ¶
func (c CredentialConfig) IsZero() bool
IsZero reports whether no credential config values were supplied.
type DroppedSetting ¶ added in v0.6.0
type DroppedSetting struct {
// Fields names the Config fields that were not applied, e.g. "Temperature".
Fields []string
// Capability is the capability the fields required.
Capability Capability
// Reason is a short human-readable explanation.
Reason string
}
DroppedSetting names a Config field that could not be applied, and why.
type Effort ¶ added in v0.3.0
type Effort string
Effort is a provider-neutral reasoning-effort level.
An ordinal ladder is neutral in a way a numeric range is not: "low" means the same thing on every provider, whereas 0.5 means different things on a 0-1 scale and a 0-2 one.
The five rungs are what Anthropic and the claude CLI accept natively, and a strict subset of what OpenAI accepts. Gemini has four levels, so EffortXHigh and EffortMax clamp to its highest — documented rather than silent.
const ( // EffortLow spends the least reasoning the provider will accept. EffortLow Effort = "low" // EffortMedium is a middling amount of reasoning. EffortMedium Effort = "medium" // EffortHigh reasons substantially before answering. EffortHigh Effort = "high" // EffortXHigh reasons more than High. Clamps to High on Gemini. EffortXHigh Effort = "xhigh" // EffortMax spends the most reasoning the provider will accept. Clamps to // High on Gemini. This is the most direct cost dial in [Config] — observe // its effect through Usage.ReasoningTokens. EffortMax Effort = "max" )
type EffortCapable
deprecated
added in
v0.3.0
type EffortCapable interface {
ChatClient
// SupportsEffort is a compile-time marker. It is never called.
SupportsEffort()
}
EffortCapable is implemented by provider clients that can carry Config.Effort.
Deprecated: prefer CapabilitiesFor with CapEffort. This marker still gates construction and is not going away in this release.
type EffortSupport ¶ added in v0.6.0
type EffortSupport struct {
// Levels lists the rungs the model actually implements. Empty means the
// ladder is not known, not that no levels exist.
Levels []Effort
}
EffortSupport carries the specifics of CapEffort.
type FailoverDecision ¶
type FailoverDecision int
FailoverDecision is the outcome of classifying a provider error.
const ( // FailoverFatal — do not advance; return the error to the caller. FailoverFatal FailoverDecision = iota // FailoverNext — the active provider failed transiently or is // unavailable; advance to the next provider. FailoverNext )
type FailoverPolicy ¶
type FailoverPolicy interface {
Classify(err error) FailoverDecision
}
FailoverPolicy classifies a provider error into a FailoverDecision.
Implementations MUST NOT log the error's message directly. A policy only decides whether to advance; the composite logs a single coarse WARN per transition (provider names + a "status"/"network" reason, never the raw error), and reduces any endpoint detail to the host only.
var DefaultFailoverPolicy FailoverPolicy = defaultFailoverPolicy{}
DefaultFailoverPolicy advances on transient/unavailable conditions (HTTP 408, 429, 5xx, and network errors) and treats everything else — auth, bad request, unknown model, caller cancellation, and local-CLI (claude-local) failures — as fatal so an operator-fixable problem surfaces instead of being masked.
See the resolved open questions in https://gitlab.com/phpboyscout/go-tool-base/-/wikis/specs/0093-chat-provider-fallback (OQ-1: a claude-local non-zero exit is fatal).
type FallbackConfig ¶
type FallbackConfig struct {
Enabled bool `mapstructure:"enabled"`
Providers []Provider `mapstructure:"providers"`
}
FallbackConfig is the package-owned shape for GTB's ai.fallback.* config.
type FallbackOption ¶
type FallbackOption func(*fallbackConfig)
FallbackOption configures a composite fallback client.
func WithFailoverPolicy ¶
func WithFailoverPolicy(policy FailoverPolicy) FallbackOption
WithFailoverPolicy overrides the error-classification policy (default DefaultFailoverPolicy).
func WithFallbackLogger ¶
func WithFallbackLogger(log *slog.Logger) FallbackOption
WithFallbackLogger sets the logger used for the single WARN line per failover transition. Defaults to a no-op logger; NewFallbackFromConfigs wires the tool's logger automatically.
func WithOnFailover ¶
func WithOnFailover(fn func(from, to Provider)) FallbackOption
WithOnFailover registers an observability hook invoked on each provider transition. It is called after the WARN log, before the new provider is used.
func WithProviderCredentials ¶ added in v0.10.0
func WithProviderCredentials(resolve ProviderCredentials) FallbackOption
WithProviderCredentials supplies a resolver called once per provider while a chain is built, so each member gets its own credentials rather than inheriting the caller's or falling back to a well-known environment variable.
Without it, only the provider matching Config.Provider carries the caller's explicit Token and Credentials; the rest self-resolve. That is the right default — a token for one vendor is meaningless to another, and copying it across a chain would send a caller's key to a provider they never named.
client, err := chat.NewWithFallbackSettings(ctx, settings, fallbackCfg,
chat.WithProviderCredentials(func(p chat.Provider) (chat.CredentialConfig, bool) {
key, ok := vault[p]
return chat.CredentialConfig{Key: key}, ok
}),
)
func WithStateless ¶ added in v0.2.0
func WithStateless() FallbackOption
WithStateless declares that the supplied clients are stateless, so the composite keeps no replay transcript of its own.
NewFallbackFromSettings and NewFallbackFromConfigs infer this from Config.Stateless and you do not need to pass it. It exists for NewFallback, which is handed already-built clients and cannot see their config. Passing it for clients that are *not* stateless loses cross-provider context on failover; omitting it for clients that are means the composite accumulates a transcript no one will use, and prepends it to the next one-shot call on failover.
func WithStrictToolContext ¶
func WithStrictToolContext() FallbackOption
WithStrictToolContext makes failover fail fast once a tool call has executed in the current conversation, instead of advancing with a lossy text-only transcript replay (resolved OQ-2).
type FileStoreOption ¶
type FileStoreOption func(*fileStoreConfig)
FileStoreOption configures a FileStore.
func WithEncryption ¶
func WithEncryption(key []byte) FileStoreOption
WithEncryption enables AES-256-GCM encryption for stored snapshots. The key must be exactly 32 bytes and must come from a cryptographically secure source. Use GenerateEncryptionKey to generate one.
func WithLogger ¶
func WithLogger(log *slog.Logger) FileStoreOption
WithLogger attaches a logger used for diagnostic DEBUG-level events (e.g. when [FileStore.List] skips a file whose name is not a canonical snapshot identifier). Defaults to a noop logger.
type HTTPStatusExtractor ¶
HTTPStatusExtractor pulls an HTTP status code out of a provider SDK error, unwrapping the wrapper layers the providers add. The second return is false when the error is not this provider's status-bearing type.
Each provider module registers one via RegisterStatusExtractor in its init() so the chat core can classify failover decisions without importing any vendor SDK. This mirrors the RegisterProvider registry.
type History ¶ added in v0.10.0
type History struct {
// Turns is the number of conversation turns the client will re-send on its
// next call, including any buffered by Add and not yet sent.
Turns int
// LastInputTokens is the provider-reported input-token count of the most
// recent call — the measured size of everything that call sent. Zero before
// the first call, and from providers that report no usage at all.
//
// It is a measurement rather than an estimate, and one call stale. See
// [UsageTracker.LastUsage] for why the core does not compute it directly.
LastInputTokens int
// Known reports whether Turns is authoritative. False from a provider that
// cannot count its own history — ProviderClaudeLocal delegates the
// transcript to the claude CLI's own session, so it can only see what it has
// buffered locally, not what the far side is carrying.
Known bool
}
History reports what a client is currently carrying in its conversation, so a caller can see the growth that would otherwise only surface as a context-overflow 400 — which the failover policy correctly, and unhelpfully, classifies as fatal.
Every provider appends to its conversation and re-sends the whole of it on the next call, so a long-lived agent loop degrades monotonically. Config.Stateless answers the independent-calls case outright; this is for the conversation that legitimately accumulates and needs watching.
Check Known before treating Turns as authoritative, exactly as with Usage: a provider that delegates its transcript elsewhere cannot count it.
type HistoryEdit ¶ added in v0.10.0
type HistoryEdit struct {
// Keep lists the indices to retain, oldest first. An index absent from Keep
// is dropped.
Keep []int
// Replace maps an index in Keep to text that supersedes that turn. It is
// how a compacting policy substitutes a summary for the turns it folded up;
// a truncating policy leaves it nil.
//
// A replaced turn becomes plain text of the same role, losing any structure
// it had. That is why a tool group is replaced as a whole or not at all.
Replace map[int]string
}
HistoryEdit is a policy's decision about a conversation: what to keep, and what to rewrite.
type HistoryPolicy ¶ added in v0.10.0
type HistoryPolicy interface {
Bound(ctx context.Context, turns []TurnInfo) (HistoryEdit, error)
}
HistoryPolicy bounds a conversation before it is sent.
Bound takes a context and returns an error because a policy may call the model: compaction summarises rather than drops, which is a round-trip that can fail. It returns replacement text rather than only a subset for the same reason. Truncation uses neither, and that asymmetry is deliberate — the interface is shaped so compaction arrives as an implementation rather than as a breaking change. See https://gitlab.com/phpboyscout/go/chat/-/wikis/specs/0011-chat-provider-conformance D7.
A policy need not enforce the pinning and tool-group invariants itself: ApplyHistoryEdit checks them, so a policy that gets them wrong fails loudly instead of corrupting a transcript.
Example ¶
package main
import (
"context"
"gitlab.com/phpboyscout/go/chat"
)
// keepEverySecondTurn is a deliberately silly policy, written to show the shape
// rather than to be useful: a policy reads a description of the conversation and
// returns the indices to keep.
type keepEverySecondTurn struct{}
func (keepEverySecondTurn) Bound(
_ context.Context,
turns []chat.TurnInfo,
) (chat.HistoryEdit, error) {
keep := make([]int, 0, len(turns))
for i := range turns {
if i%2 == 0 || turns[i].Pinned || turns[i].ToolGroup != 0 {
keep = append(keep, i)
}
}
return chat.HistoryEdit{Keep: keep}, nil
}
func main() {
cfg := chat.Config{
Provider: chat.ProviderClaudeLocal,
HistoryPolicy: keepEverySecondTurn{},
}
_ = cfg
}
Output:
func CompactOldest ¶ added in v0.12.0
func CompactOldest(opts ...CompactOption) HistoryPolicy
CompactOldest returns a policy that summarises the older region of a conversation and substitutes the summary, rather than dropping turns as TruncateOldest does.
It calls the model, so it costs tokens and can fail. It runs only when a budget is exceeded, and deciding it has nothing to do is free.
// every default: the live model, 200 turns, 30% of the model's input limit
cfg.HistoryPolicy = chat.CompactOldest()
// a cheaper summariser, a tighter budget, and degradation rather than failure
cfg.HistoryPolicy = chat.CompactOldest(
chat.WithSummarizer(chat.Config{Provider: chat.ProviderClaude, Model: "claude-haiku-4-5-20251001"}),
chat.WithTokenBudget(250_000),
chat.WithCompactionFallback(chat.TruncateOldest(200)),
)
Cached turns are never dropped or rewritten, and a tool request travels with its results — both enforced by ApplyHistoryEdit rather than by this policy.
See https://gitlab.com/phpboyscout/go/chat/-/wikis/specs/0014-history-compaction.
func DropLastExchange ¶ added in v0.14.0
func DropLastExchange() HistoryPolicy
DropLastExchange returns a policy that removes the most recent exchange: the caller's last prompt, the reply to it, and any tool traffic between them.
It is a HistoryPolicy rather than a provider method so that `/undo` needs no capability `/compact` does not already need. A pinned turn is never dropped — ApplyHistoryEdit refuses that — so undoing back into a cached prefix fails rather than quietly invalidating it.
func TruncateOldest ¶ added in v0.10.0
func TruncateOldest(maxTurns int) HistoryPolicy
TruncateOldest returns a policy that keeps the most recent turns within maxTurns, dropping oldest first.
Pinned turns are always kept and do not count against the budget in the sense that they are never candidates for dropping — a conversation whose cached prefix alone exceeds maxTurns keeps the prefix and nothing else, because dropping a cached turn is the one thing a policy may not do.
Turns are kept as a contiguous recent run rather than cherry-picked to fill the budget exactly: skipping a large tool group to fit two smaller older turns would leave the model reading a conversation that never happened.
A maxTurns of zero or less returns a policy that changes nothing.
Example ¶
package main
import (
"gitlab.com/phpboyscout/go/chat"
)
func main() {
// Nil is the default: the conversation grows without bound. Set a policy for
// a long-running loop that would otherwise fill the context window.
cfg := chat.Config{
Provider: chat.ProviderClaudeLocal,
HistoryPolicy: chat.TruncateOldest(40),
}
_ = cfg
}
Output:
type KeychainLookup ¶
KeychainLookup resolves an OS-keychain (or remote secret-store) reference of the form "service/account" to its secret value. The host application injects it — go-tool-base wires the go/credentials module's Retrieve — so the chat core needs no keychain backend of its own. A nil lookup means the keychain resolution step is skipped (the caller falls through to the next credential source).
type Limits ¶ added in v0.6.0
Limits carries a model's token ceilings.
Limits are deliberately not a Capability: every model has them and no provider lacks the concept, so a Support value could only ever say yes or unknown — and unknown there says nothing a nil does not. Keeping the vocabulary to things that can genuinely be absent is what makes Support mean something.
type Media ¶
Media is one input attachment (an image, PDF, or supported A/V clip) sent alongside a text prompt to a multimodal model (spec 2026-07-05-chat-multimodal). Data holds the raw bytes. MIMEType is an OPTIONAL cross-check: the type is always sniffed from Data and the sniffed type is authoritative (and what is sent); when MIMEType is set it must match the sniffed family or the attachment is rejected, so a declared type can never smuggle disguised content past the safety filter.
type ModelIdentifier ¶ added in v0.6.0
ModelIdentifier is implemented by clients that can report the provider and model they were built with. Discover it by type assertion, as with the other optional interfaces.
CapabilitiesOf needs it because ChatClient itself carries no identity, and the core deliberately does not wrap provider clients to add one — a wrapper would have to re-advertise every optional interface or silently break capability discovery for the ones it forgot.
type ModelInfo ¶ added in v0.6.0
type ModelInfo struct {
Capabilities Capabilities
// Limits is nil when the ceilings are not known.
Limits *Limits
}
ModelInfo is everything the module can say about a provider and model.
func CapabilitiesFor ¶ added in v0.6.0
CapabilitiesFor reports what provider supports for model, without constructing anything. Use it to choose between models before building a client.
A provider whose module is not linked reports every capability as SupportUnknown and no limits, rather than an error: not importing an adapter is a legitimate state, and Unknown is already the right answer for it.
func CapabilitiesOf ¶ added in v0.6.0
func CapabilitiesOf(client ChatClient) ModelInfo
CapabilitiesOf reports what an existing client supports.
Clients that do not implement ModelIdentifier report every capability as SupportUnknown, for the same reason CapabilitiesFor does on an unregistered provider.
type PersistentChatClient ¶
type PersistentChatClient interface {
ChatClient
// Save captures the current conversation state as an immutable snapshot.
// The snapshot includes provider-specific messages as opaque JSON, tool
// metadata (without handlers), and configuration. Tokens are never saved.
Save() (*Snapshot, error)
// Restore replaces the current conversation state with a previously saved
// snapshot. The snapshot's Provider must match the client's provider.
// After restore, tools must be re-registered via SetTools with live handlers.
Restore(snapshot *Snapshot) error
}
PersistentChatClient extends ChatClient with the ability to save and restore conversation state. Discover via type assertion (same pattern as StreamingChatClient):
if pc, ok := client.(chat.PersistentChatClient); ok {
snapshot, err := pc.Save()
}
ClaudeLocal does not implement this interface — it delegates to an external subprocess and has no internal message state to persist.
Example ¶
package main
import (
"context"
)
func main() {
// Discover persistence support via type assertion:
//
// client, _ := chat.New(ctx, chat.Settings{Config: cfg, Logger: log})
// if pc, ok := client.(chat.PersistentChatClient); ok {
// snapshot, _ := pc.Save()
// // ... store snapshot ...
// pc.Restore(snapshot)
// }
//
// ClaudeLocal does not implement PersistentChatClient.
_ = context.Background()
}
Output:
type Provider ¶
type Provider string
Provider defines the AI service provider.
const ( // ProviderOpenAI uses OpenAI's API. ProviderOpenAI Provider = "openai" // ProviderOpenAICompatible uses any OpenAI-compatible API endpoint (e.g. Ollama, Groq). ProviderOpenAICompatible Provider = "openai-compatible" // ProviderClaude uses Anthropic's Claude API. ProviderClaude Provider = "claude" // ProviderClaudeLocal uses a locally installed claude CLI binary. ProviderClaudeLocal Provider = "claude-local" // ProviderGemini uses Google's Gemini API. ProviderGemini Provider = "gemini" // ProviderGeminiVertex uses Google's Gemini models through the Vertex AI // backend, addressed by Config.Project and Config.Location and authenticated // by Google application default credentials rather than an API key. // // It is a separate name rather than a flag on ProviderGemini because the two // differ in how they are addressed and how they authenticate, and because a // name is reachable from the registry and from a config file. See // https://gitlab.com/phpboyscout/go/chat/-/wikis/specs/0017-gemini-vertex-backend D5. ProviderGeminiVertex Provider = "gemini-vertex" )
type ProviderCredentials ¶ added in v0.10.0
type ProviderCredentials func(provider Provider) (CredentialConfig, bool)
ProviderCredentials resolves the credentials for one provider in a fallback chain. Returning false leaves that provider to its own credential lookup.
It is the module's equivalent of the per-provider re-resolution a host application does in its own config adapter: a chain is built from one Config, but each provider needs its own key, and only the host knows where they live.
type ProviderFactory ¶
type ProviderFactory func(ctx context.Context, settings Settings) (ChatClient, error)
ProviderFactory creates a ChatClient for a named provider. Register implementations via RegisterProvider in an init() function to allow external packages to add providers without modifying this file.
type ResolvedMedia ¶
ResolvedMedia is an attachment that has passed the safety filter: its type is known, allowlisted, and provider-accepted, and it is ready to map to a provider request. Providers consume []ResolvedMedia, never raw Media.
func ValidateMediaSet ¶
func ValidateMediaSet(provider Provider, media []Media) ([]ResolvedMedia, error)
ValidateMediaSet runs the safety choke point over a request's attachments before any network call (spec §5): it enforces the provider's media capability, the per-attachment sniff + declared cross-check + allowlist, the provider's type support, and the size/count caps. It returns the resolved, ready-to-send attachments or a typed error.
type Role ¶ added in v0.10.0
type Role string
Role identifies who produced a conversation turn.
const ( // RoleUser is a turn from the caller, including tool results, which every // provider sends back as user-side content. RoleUser Role = "user" // RoleAssistant is a turn from the model, including tool requests. RoleAssistant Role = "assistant" // RoleSystem is the system prompt. RoleSystem Role = "system" )
type RuntimeConfig ¶
type RuntimeConfig struct {
Provider Provider `mapstructure:"provider"`
RequestTimeout time.Duration `mapstructure:"request_timeout"`
Fallback FallbackConfig `mapstructure:"fallback"`
}
RuntimeConfig is the package-owned shape for GTB's ai.* config section.
type SamplingCapable
deprecated
added in
v0.3.0
type SamplingCapable interface {
ChatClient
// SupportsSampling is a compile-time marker. It is never called.
SupportsSampling()
}
SamplingCapable is implemented by provider clients that can carry Config.Temperature and Config.TopP.
This marks a *structural* capability — whether the provider has the concept at all — not whether the selected model will accept the value. ProviderClaudeLocal does not implement it: the claude CLI exposes no sampling flag at any version, so there is nothing to plumb. A model refusing a value it structurally supports is a different failure; see ErrModelRejectedParameter.
Deprecated: prefer CapabilitiesFor with CapSampling, which reports the same structural fact three-valued and carries the model specifics with it. This marker still gates construction and is not going away in this release.
type SamplingSupport ¶ added in v0.6.0
type SamplingSupport struct {
// MinTemperature and MaxTemperature bound Config.Temperature in the
// provider's own units. Both zero means the range is not known.
MinTemperature float64
MaxTemperature float64
// TopP reports whether nucleus sampling is separately settable.
TopP Support
}
SamplingSupport carries the specifics of CapSampling. A zero field means "not known", never "zero".
type Settings ¶
Settings contains the package-owned construction dependencies for a chat client. Config carries provider behaviour; Logger is optional and defaults to a no-op logger.
type Snapshot ¶
type Snapshot struct {
// ID uniquely identifies this snapshot.
ID string `json:"id"`
// Provider identifies which chat provider created this snapshot.
Provider Provider `json:"provider"`
// Model is the AI model used in the conversation.
Model string `json:"model"`
// SystemPrompt is the system instruction active at snapshot time.
SystemPrompt string `json:"system_prompt,omitempty"`
// Messages contains provider-specific message history as opaque JSON.
// The format varies by provider — do not parse or modify directly.
Messages json.RawMessage `json:"messages"`
// Tools captures tool metadata (name, description, parameters) without
// handlers. After restoring, call SetTools to re-register live handlers.
Tools []ToolSnapshot `json:"tools,omitempty"`
// Metadata holds arbitrary key-value pairs for consumer use.
Metadata map[string]string `json:"metadata,omitempty"`
// CreatedAt is when this snapshot was taken.
CreatedAt time.Time `json:"created_at"`
// Version is the snapshot format version for forward compatibility.
Version int `json:"version"`
}
Snapshot is an immutable point-in-time capture of a conversation.
func NewSnapshot ¶
func NewSnapshot(provider Provider, model, systemPrompt string, messages json.RawMessage, tools map[string]Tool, metadata map[string]string) *Snapshot
NewSnapshot creates a Snapshot with a new UUID and the current timestamp.
The ID is generated via uuid.New so that it always satisfies the canonical-UUID contract enforced by [FileStore.Save], [FileStore.Load], and [FileStore.Delete]. Constructing a Snapshot struct directly and populating ID by hand is supported but discouraged — any value that fails ValidateSnapshotID will be rejected by the store. If you do need to accept a caller-supplied ID (for example, reconstructing a snapshot parsed from an external payload), call ValidateSnapshotID at the boundary rather than relying on Save/Load/Delete to reject it later.
Example ¶
package main
import (
"encoding/json"
"gitlab.com/phpboyscout/go/chat"
)
func main() {
snap := chat.NewSnapshot(
chat.ProviderClaude,
"claude-3-5-sonnet",
"You are a helpful assistant.",
json.RawMessage(`[{"role":"user","content":"hello"}]`),
nil,
map[string]string{"session": "demo"},
)
_ = snap.ID // UUID
_ = snap.CreatedAt // timestamp
}
Output:
type SnapshotSummary ¶
type SnapshotSummary struct {
ID string `json:"id"`
Provider Provider `json:"provider"`
Model string `json:"model"`
CreatedAt time.Time `json:"created_at"`
MessageCount int `json:"message_count"`
}
SnapshotSummary is a lightweight view of a snapshot for listing without loading the full message history.
type StatelessCapable ¶ added in v0.2.0
type StatelessCapable interface {
ChatClient
// SupportsStateless is a compile-time marker. It is never called.
SupportsStateless()
}
StatelessCapable is implemented by provider clients that honour Config.Stateless.
The chat core and each provider module version independently, so a caller can pin a provider built before stateless mode existed. Without this marker, Config.Stateless on such a provider would compile, be ignored, and bill the caller for the accumulated history they asked not to send — the exact silent failure the flag exists to remove. New therefore type-asserts the constructed client and refuses to hand back one that cannot honour the request.
Discover it the same way as StreamingChatClient and PersistentChatClient:
if _, ok := client.(chat.StatelessCapable); ok {
// one-shot calls are honoured
}
type StreamCallback ¶
type StreamCallback func(event StreamEvent) error
StreamCallback receives streaming events. Return a non-nil error to cancel the stream.
type StreamEvent ¶
type StreamEvent struct {
// Type indicates the kind of event.
Type StreamEventType
// Delta contains the text fragment for EventTextDelta events.
Delta string
// ToolCall contains tool call information for EventToolCallStart/EventToolCallEnd events.
ToolCall *StreamToolCall
// Error contains error information for EventError events.
Error error
}
StreamEvent represents a single event in a streaming response.
type StreamEventType ¶
type StreamEventType int
StreamEventType identifies the kind of stream event.
const ( // EventTextDelta is a partial text response fragment. EventTextDelta StreamEventType = iota // EventToolCallStart indicates a tool call has begun execution. EventToolCallStart // EventToolCallEnd indicates a tool call has completed execution. EventToolCallEnd // EventComplete indicates the stream has finished successfully. EventComplete // EventError indicates an error occurred during streaming. EventError )
type StreamToolCall ¶
type StreamToolCall struct {
// ID is the provider-assigned identifier for the tool call.
ID string
// Name is the tool name.
Name string
// Arguments is the complete JSON argument payload (only populated on EventToolCallEnd).
Arguments string
// Result is the tool execution result (only populated on EventToolCallEnd).
Result string
}
StreamToolCall contains information about a tool call within a stream.
type StreamingChatClient ¶
type StreamingChatClient interface {
ChatClient
// StreamChat sends a message and streams the response via callback.
// The callback is invoked for each event in the stream. If the callback
// returns a non-nil error, the stream is cancelled and that error is returned.
// The return value is the complete assembled response text (concatenation of
// all EventTextDelta fragments) or an error if streaming failed.
// Tool calls are handled internally via the same ReAct loop as Chat(). If
// Config.ParallelTools is enabled, multiple tool calls are executed concurrently.
StreamChat(ctx context.Context, prompt string, callback StreamCallback, media ...Media) (string, error)
}
StreamingChatClient extends ChatClient with streaming support. Implementations that support streaming implement this interface in addition to ChatClient. Discover support via type assertion:
if streamer, ok := client.(chat.StreamingChatClient); ok {
result, err := streamer.StreamChat(ctx, "prompt", callback)
}
type Support ¶ added in v0.6.0
type Support uint8
Support is a three-valued answer to "does this support that".
Unknown is a first-class answer rather than a failure, and it is the reason this is not a bool. Two of the five providers cannot be interrogated at all — claude-local is a CLI with no models endpoint, and openai-compatible points at an arbitrary server — so a two-valued design would have to report those as unsupported. That is a lie which suppresses working features, and it is the worse of the two available errors: Unknown merely means "find out by trying", which is what callers do today.
Unknown is also what makes a new capability safe to add: a provider that has not been taught about one reports Unknown rather than becoming wrong.
type Tool ¶
type Tool struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters *jsonschema.Schema `json:"parameters"`
Handler func(ctx context.Context, args json.RawMessage) (any, error) `json:"-" mapstructure:"-" yaml:"-"`
}
Tool represents a function that the AI can call.
type ToolCall ¶
type ToolCall struct {
Name string
Input json.RawMessage
}
ToolCall represents a single tool invocation request.
type ToolResult ¶
ToolResult holds the result of a single tool execution.
func DispatchToolExecution ¶
func DispatchToolExecution(ctx context.Context, l *slog.Logger, tools map[string]Tool, calls []ToolCall, parallelTools bool, maxParallelTools int) []ToolResult
DispatchToolExecution runs tool calls sequentially or in parallel depending on parallelTools and the number of calls. It is the shared dispatch entry point for all provider ReAct loops.
func ExecuteToolsParallel ¶
func ExecuteToolsParallel(ctx context.Context, l *slog.Logger, tools map[string]Tool, calls []ToolCall, maxConcurrency int) []ToolResult
ExecuteToolsParallel executes multiple tool calls concurrently, bounded by maxConcurrency. Results are returned in the same order as the input calls. If maxConcurrency is zero or negative, it defaults to 5.
type ToolSnapshot ¶
type ToolSnapshot struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters *jsonschema.Schema `json:"parameters,omitempty"`
}
ToolSnapshot captures tool metadata without the handler function.
type TranscriptEditor ¶ added in v0.14.0
type TranscriptEditor interface {
// ApplyPolicyNow bounds the retained conversation immediately, rather than
// waiting for the next request to do it, and reports how many turns went.
//
// A nil policy means the one on [Config.HistoryPolicy]; with none configured
// there is nothing to apply and it returns zero. Zero with a nil error means
// the policy kept everything, which is a legitimate outcome rather than a
// failure — a conversation inside its budget has nothing to do.
//
// Implementations run the policy through [BoundConversation], so an edit
// that would drop a cached turn or split a tool group is refused rather than
// applied.
//
// It returns [ErrTranscriptMoved] when another goroutine changed the
// conversation while the policy was running, which a caller may retry.
ApplyPolicyNow(ctx context.Context, policy HistoryPolicy) (removed int, err error)
}
TranscriptEditor is the optional contract for a provider that can apply a HistoryPolicy to its retained conversation on demand.
It exists because the core cannot reach a transcript, and that is deliberate: Snapshot.Messages is provider JSON the core must not parse, and BoundConversation takes turns because only a provider can enumerate its own. So a consumer has no way to say "shorten this now" unless the provider offers one.
One seam, not one method per operation ¶
The single method takes a policy rather than naming an operation, so the provider surface does not grow every time a command needs to change a transcript. `/compact` and `/undo` are both this method with a different policy, and a future operation is a policy the core writes — no adapter change, no release train.
It is also the shape a provider already runs. Before every request it describes its retained turns, calls BoundConversation, and rewrites itself from the result. This exposes that sequence rather than adding a second way to do it, so the pinning and tool-group invariants come along unchanged.
ProviderClaudeLocal will not implement it: its conversation lives in the claude CLI's own session, so it cannot enumerate turns at all. See https://gitlab.com/phpboyscout/go/chat/-/wikis/specs/0013-history-policy-application D6, and spec 0016 D5.
type TurnInfo ¶ added in v0.10.0
type TurnInfo struct {
// Index is the turn's position in the conversation, oldest first.
Index int
// Role is who produced the turn.
Role Role
// Text is the provider's best-effort rendering of the turn as plain text,
// supplied so a policy can reason about content — a compacting policy
// summarises from it. It is lossy by construction: a tool call rendered as
// text is not the tool call. Nothing reconstructs a turn from it; it exists
// to be read, and to be replaced wholesale via [HistoryEdit.Replace].
Text string
// Pinned marks a turn added via [CachingChatClient.AddCached]. A policy
// must never drop one, and [ApplyHistoryEdit] refuses an edit that does.
//
// Caching bills a matching prefix at the cached rate, so dropping a cached
// turn destroys every subsequent cache hit — a failure that shows up only
// as a larger invoice.
Pinned bool
// ToolGroup ties a tool request to its results. Turns sharing a non-zero
// ToolGroup are one indivisible unit; zero means the turn stands alone.
//
// Providers require a tool request and its result to travel together, so an
// edit that keeps one without the other yields an API error rather than a
// shorter conversation. [ApplyHistoryEdit] refuses to split a group.
ToolGroup int
}
TurnInfo describes one conversation turn to a HistoryPolicy.
It is a description, not the turn itself. Provider message formats do not share a shape — an Anthropic tool_use block, a Gemini session and an OpenAI params struct have nothing useful in common — and the core deliberately treats a provider's transcript as opaque (see Snapshot.Messages). So a provider describes its turns for a policy to reason about, and applies the resulting HistoryEdit itself, in its own format.
type Usage ¶
type Usage struct {
// InputTokens is the number of prompt/input tokens consumed.
InputTokens int
// OutputTokens is the number of completion/output tokens produced.
OutputTokens int
// TotalTokens is the sum of input and output tokens. When a provider
// supplies its own total it is preserved; otherwise it is computed as
// InputTokens + OutputTokens.
TotalTokens int
// CachedTokens is the number of input tokens served from a provider-side
// prompt cache, when reported. Zero when the provider does not expose it.
CachedTokens int
// ReasoningTokens is the number of tokens spent on internal reasoning
// ("thinking") output, when reported. Zero when not exposed.
ReasoningTokens int
// Known reports whether the provider supplied token counts for this call.
// False indicates the counts are not authoritative (e.g. ProviderClaudeLocal).
Known bool
}
Usage reports the token consumption of one or more provider round-trips in a provider-neutral shape. Tools built on GTB use it to observe and cost their LLM calls without coupling to any one provider's SDK.
Token counts are summed across every provider round-trip made within a single Chat, Ask, or StreamChat call — a ReAct tool-calling loop makes one round-trip per step, and the usage of every step is accumulated. The Usage() accessor on a client returns the cumulative total across the lifetime of that client instance; see ChatClient.Usage for details.
Known reports whether the provider supplied token counts. Providers that do not expose usage (notably ProviderClaudeLocal, which wraps the claude CLI and returns no token data) report a zero-valued Usage with Known == false. Always check Known before treating the counts as authoritative.
type UsageTracker ¶
type UsageTracker struct {
// contains filtered or unexported fields
}
UsageTracker accumulates per-round-trip usage for a single client instance and fans each round-trip out to an optional observer. It is embedded by every provider implementation. The zero value is ready to use.
It is safe for concurrent use, but note that ChatClient implementations are not themselves safe for concurrent use; the mutex guards only against an observer reading Usage() from another goroutine while a call is in flight.
func (*UsageTracker) LastUsage ¶ added in v0.10.0
func (t *UsageTracker) LastUsage() Usage
LastUsage returns the most recent round-trip's usage, or a zero Usage with Known == false when no call has been made.
This is what makes a conversation's growth observable without the core owning a tokenizer: InputTokens here is the provider's own count of everything the last call sent — system prompt, retained history and the new turn together — so it is measured rather than estimated, and stale by exactly one call.
The core cannot do better. Counting locally would need a tokenizer per model family, and there is no universal one: Anthropic publishes none (its count is an API round-trip), Gemini counts server-side, and only OpenAI can be tokenised offline. depfootprint_test.go forbids that dependency in the core for exactly this reason. See https://gitlab.com/phpboyscout/go/chat/-/wikis/specs/0011-chat-provider-conformance D7.
func (*UsageTracker) RecordUsage ¶
func (t *UsageTracker) RecordUsage(u Usage)
RecordUsage accumulates one round-trip's usage into the lifetime total and, if an observer is configured, invokes it with that round-trip's usage. Passing a usage with Known == false (e.g. a provider that reports nothing) still fires the observer so callers can distinguish "no usage" from "no call".
func (*UsageTracker) SetObserver ¶
func (t *UsageTracker) SetObserver(observer func(Usage))
SetObserver wires a per-round-trip usage observer onto the tracker. Provider modules call it during construction to forward Config.UsageObserver; the observer field stays unexported so it is only settable through this method.
func (*UsageTracker) Usage ¶
func (t *UsageTracker) Usage() Usage
Usage returns the cumulative token usage across every provider round-trip made by this client instance since construction. It is promoted onto each provider type via embedding to satisfy ChatClient.Usage.
Source Files
¶
- asktarget.go
- baseurl.go
- boundconversation.go
- caching.go
- capability.go
- claude_local.go
- client.go
- commands.go
- compaction.go
- config.go
- configerrors.go
- constants.go
- credentials.go
- doc.go
- fallback.go
- fallback_policy.go
- filestore.go
- generation.go
- history.go
- historypolicy.go
- httpclient.go
- media.go
- media_persist.go
- persistence.go
- requirements.go
- schema.go
- sentinel.go
- stateless.go
- streaming.go
- throttling.go
- tools.go
- transcripteditor.go
- usage.go
Directories
¶
| Path | Synopsis |
|---|---|
|
Package conformance is a reusable test suite that a chat provider module runs against its own client to prove it behaves like every other provider.
|
Package conformance is a reusable test suite that a chat provider module runs against its own client to prove it behaves like every other provider. |