openaiapi

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package 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

View Source
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

View Source
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

func Scrub(s, key string) string

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

func ValidateBaseURL(raw string) error

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

type Brief struct {
	Instructions string
	Input        string
}

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

func New(baseURL, key string, opts ...Option) (*Client, error)

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

func WithTimeout(d time.Duration) Option

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.

type Result

type Result struct {
	Content       []byte
	ModelAsked    string
	ModelReported string
}

Result is one validated answer: the document the output contract admitted, the model asked for, and the model the provider reported (bounded, with any hidden or control rune percent-encoded), so a substitution is visible.

Jump to

Keyboard shortcuts

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