Help us shape the future of Go! Take the 2026 Go Developer Survey

proxy

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package proxy implements the Universal Agent Governance Proxy (design 001): a loopback HTTP proxy that intercepts the LLM provider traffic of third-party agent CLIs (Claude Code, Codex, aider, …) running inside panes and applies rysh governance to it — SecretNAT redaction on request bodies, token/cost metering into the usage ledger (design 003), and audit — by base-URL environment injection, NOT TLS MITM.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultUpstreams

func DefaultUpstreams() map[string]string

DefaultUpstreams maps a dialect to its real provider base URL, as declared by each registered Dialect.

func Endpoint

func Endpoint() string

Endpoint returns the running proxy's base URL, or "" if none is running. PaneActor injects <endpoint>/<dialect>/<paneID> as ANTHROPIC_BASE_URL etc. only when this is non-empty, so a disabled proxy leaves pane env untouched.

func SetEndpoint

func SetEndpoint(base string)

SetEndpoint records the running proxy's loopback base URL (or "" to clear).

Types

type AuditLine

type AuditLine struct {
	TS            time.Time
	PaneID        string
	Dialect       string
	Model         string
	Endpoint      string
	ReqBytes      int
	RedactionHits int
	Status        int
	InTokens      int
	OutTokens     int
}

AuditLine is a metadata-only record of one proxied request (never content).

func (AuditLine) String

func (a AuditLine) String() string

type Dialect

type Dialect interface {
	// Name is the routing key in /{dialect}/{paneID}/... paths.
	Name() string
	// Upstream is the default provider base URL (overridable via config).
	Upstream() string
	// ApplyAuth attaches key using the provider's convention. key == "" means
	// no key is configured: the caller's own credentials (already copied onto
	// req) must pass through untouched.
	ApplyAuth(req *http.Request, key string)
	// ExtractUsage pulls token usage and model out of a response body
	// (JSON or SSE).
	ExtractUsage(body []byte) Usage
	// WriteBudgetExceeded writes the synthetic budget 429 (design 001 §4.4)
	// in the provider's native error JSON shape, so the wrapped CLI's own
	// error handling recognises it as a rate limit and exits cleanly rather
	// than treating an unfamiliar body as a crash.
	WriteBudgetExceeded(w http.ResponseWriter, message string)
}

Dialect abstracts one provider wire format (design 001 §4.2): the default upstream its traffic goes to, how the real API key is attached to a request, how token usage is read out of a response, and what its native rate-limit error looks like — so the budget 429 the proxy synthesizes is indistinguishable in shape from the provider's own.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is the loopback governance proxy. It runs on its own HTTP goroutines (data plane). The mutex guards only the small audit ring buffer — a documented exception to the no-mutex rule, at the HTTP/actor boundary.

func New

func New(pub *msg.NATSPublisher, snat *secretnat.Manager, keys map[string]string, upstreams map[string]string, auditContent bool) *Server

New builds a proxy server. upstreams nil ⇒ DefaultUpstreams.

keys maps a dialect (anthropic|openai|gemini) to the real upstream API key. A dialect absent from the map is NOT rewritten — the caller's own credential passes through. Passing one key for every dialect is what previously sent an Anthropic key as the OpenAI bearer and broke wrapped OpenAI CLIs.

func (*Server) BaseURL

func (s *Server) BaseURL() string

BaseURL returns the running base URL ("" if not started).

func (*Server) Port

func (s *Server) Port() int

Port returns the loopback port the proxy bound, or 0 if not started. Used to record the governed endpoint in the session registry (design 001).

func (*Server) RecentAudits

func (s *Server) RecentAudits(n int) []AuditLine

RecentAudits returns up to n most-recent audit lines (newest last).

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the per-request pipeline: parse → SNAT → forward → stream back → usage/audit.

func (*Server) SetAuditDir

func (s *Server) SetAuditDir(dir string)

SetAuditDir sets the directory redacted request bodies are written to when audit_content is enabled (design 001 §4.5). Call before Start. With no dir set, audit_content records metadata only — the body sink stays inert.

func (*Server) Start

func (s *Server) Start(addr string) (string, error)

Start binds a loopback listener (ephemeral port unless addr specifies one), serves in the background, and publishes the endpoint for pane env injection. Returns the base URL (http://127.0.0.1:PORT).

func (*Server) Stop

func (s *Server) Stop()

Stop clears the endpoint and shuts the server down.

type Usage

type Usage struct {
	InTokens   int
	OutTokens  int
	CacheRead  int
	CacheWrite int
	Model      string
}

Usage is the token accounting a Dialect extracts from one response.

Jump to

Keyboard shortcuts

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