Documentation
¶
Overview ¶
Package openaiapi is abcd's OpenAI-compatible API adapter (itd-2609081951381895): one client over the chat-completions protocol, which OpenRouter and a local OpenAI-compatible server both speak, so a provider is configuration of this adapter and never code (the intent's Decision 2).
The client asks for the model it is given and nothing else: which models a provider may serve (its allowlist, and any oracle.denylist entry the configuration writes) is internal/core/oracle's to decide before a Client is ever built (adr-2609221009491186, adr-2609300107513982). The adapter's own guarantees are the network path's:
- the base URL is pinned per provider block, plain HTTP is admitted only to this machine (a local server), and a redirect is never followed, so a provider cannot move the key or the brief to another host;
- every response is bounded (MaxResponseBytes) and every call is bounded in time (WithTimeout), so a provider that floods or stalls is refused rather than waited on;
- the key travels only as the Authorization header of a request to the pinned base URL. No error, result or log line carries it: a provider's own text (its error, the model it reports and the answer itself) is decoded and scrubbed of every representation of the key (Scrub), the error and the model bounded and sanitised as well, before it can reach an error or a result, because a provider may echo what it was sent, in whatever encoding its stack applies;
- a setting the protocol does not take is refused before any call, and the answer is judged by the caller's output contract, the same one the host sub-agent's payload is judged by, so an answer that does not satisfy it is refused rather than used.
It uses net/http and encoding/json alone; it reads no file and no credential store: the caller resolves the key by name through internal/core/credential and hands the value in. The one environment it honours is net/http's own, through the default transport: the standard proxy variables for https (HTTPS_PROXY, NO_PROXY) and the platform's trust roots. An https call through a proxy is a CONNECT tunnel, so the key and the brief stay inside TLS, and a call to this machine (the only one plain HTTP may reach) is never proxied.
Index ¶
Constants ¶
const ( // DefaultTimeout bounds one call end to end: connecting, sending the brief // and reading the whole answer. DefaultTimeout = 120 * time.Second // MaxResponseBytes bounds a successful answer. A chat completion carrying a // verdict is a few kilobytes; 4 MiB refuses a flood without ever refusing a // real one. MaxResponseBytes = 4 << 20 // MaxModelBytes bounds the model a provider reports. MaxModelBytes = 200 )
Variables ¶
var ErrUnreachable = errors.New("openaiapi: the provider could not be reached")
ErrUnreachable is what a call's error wraps when the provider could not be reached at all: no connection to it (or to the proxy in front of it) was ever made, so nothing of the request, the key or the brief left this machine. A caller may leave such a step to another leg. A provider that answered, or that took the request and never answered, is not unreachable: the brief may already have been sent.
Functions ¶
func AcceptedSettings ¶
func AcceptedSettings() []string
AcceptedSettings returns the settings this adapter accepts, sorted: the declaration internal/core/oracle carries on a provider connection, so a setting outside it is refused before a step runs (spc-2609251028149555, AC 8). The slice is a copy.
func Scrub ¶
Scrub replaces every representation of key in s with "[credential]": the key itself and the forms an encoder in a provider's stack or in abcd's own error path may give it (JSON with and without HTML escaping, with '/' escaped and with non-ASCII escaped, Go's quoting, HTML escaping, URL query and path escaping, and the terminal-safe renderings). An empty key scrubs nothing. A front door that formats an error built from a provider's text scrubs it with this a last time.
func ValidateBaseURL ¶
ValidateBaseURL admits an absolute https URL, or an http URL to this machine (localhost or a loopback address), with a host and no credentials, query or fragment. The refusal never quotes the URL, which may carry a secret.
Types ¶
type Brief ¶
Brief is what the host sub-agent is given, in the protocol's two roles: Instructions is the agent's prompt (the system message) and Input is the request the verb emitted for it (the user message).
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is one provider connection. It holds the key; it never prints it.
func New ¶
New returns a client for the provider at baseURL (validated by ValidateBaseURL), sending key as a bearer token; an empty key sends none, for a local server that needs none.
func (*Client) Complete ¶
func (c *Client) Complete(ctx context.Context, req Request, contract func([]byte) error) (Result, error)
Complete sends one brief and returns the answer the contract admits. The request is refused before any call when it names no model or carries a setting the protocol does not take. contract is the output contract the host sub-agent's payload is judged by; nil admits any answer (a verification call, which judges only that the provider answered).
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithTimeout ¶
WithTimeout bounds one call; d <= 0 keeps DefaultTimeout.
type Request ¶
type Request struct {
Model string
Brief Brief
Settings map[string]json.RawMessage
}
Request is one call: the model asked for, the brief and the settings as sent. Settings are JSON scalars keyed by an accepted setting's name.