proxy

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 35 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 proxy serves the compacted API endpoints as a context-compaction proxy (spec §19.1, §19.2 mode 1, §18.2, §16). The HTTP traffic, the wire codecs, session and store meet here. Each dialect is an adapter behind the protocol interface (protocol.go), and one server-side loop serves all of them. Every failure on the compaction side is fail-open: the client request is then forwarded unchanged.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckListen

func CheckListen(addr string, allowNonLoopback bool, allowedHosts []string) error

CheckListen is CheckLoopback with the explicit opt-in for container deployments. A non-loopback address needs both allowNonLoopback and a non-empty allowedHosts list; the list is what the Host guard then enforces. The default (loopback only) is unchanged.

func CheckLoopback

func CheckLoopback(addr string) error

CheckLoopback rejects listen addresses that are reachable from other hosts. The proxy holds API credentials in transit, so it binds to loopback only.

func IsCompactedPath

func IsCompactedPath(path string) bool

IsCompactedPath reports whether a request path is one of the endpoints the proxy compacts (any dialect, with or without a gateway prefix).

func NormalizeHosts

func NormalizeHosts(list []string) ([]string, error)

NormalizeHosts turns Host header values (for example "localhost:8787", "[::1]" or "host.docker.internal") into the bare lower-case names the guard compares against. Wildcards and paths are refused, since they would widen the guard silently.

func ProjectMarkers

func ProjectMarkers() []string

ProjectMarkers returns the project root markers of the proxy.

func Run

func Run(ctx context.Context, addr string, h http.Handler) error

Run serves h on addr until ctx is done, then shuts down gracefully.

func RunListen

func RunListen(ctx context.Context, addr string, allowNonLoopback bool, allowedHosts []string, h http.Handler) error

RunListen serves h on addr like Run, but accepts a non-loopback address when CheckListen allows it.

func SessionID

func SessionID(projectID, conv string, msgs []kernel.Message) string

SessionID returns the session id the proxy derives for a project and a client transcript, so a library client and a proxy on the same database share sessions.

Types

type Config

type Config struct {
	Upstream *url.URL
	Store    *store.Store
	// OpenStore opens the database of a project with storage: local
	// (<root>/.acd-ctx/acd-ctx.db, spec §18.3) named by X-Acd-Ctx-Project-Dir.
	// Nil keeps every project in Store, as a launcher proxy does: its Store is
	// already the project's database.
	OpenStore    func(path string) (*store.Store, error)
	Version      string
	ContextLimit int          // window used when the model has no entry, default 200000
	MaxLoop      int          // server-side tool rounds per client request, default 4
	Markers      []string     // project root markers, default go.mod, package.json, ...
	Logger       *slog.Logger // default: discard
	Transport    http.RoundTripper
	// GlobalPath is the global configuration file (spec §18.1). Empty means the
	// user config path. Project files are read per request-project root.
	GlobalPath string
	// ConfigFlags are explicit configuration keys (the highest layer of §18.5).
	ConfigFlags map[string]string
	// ContextWindows maps model name prefixes to context windows, added to the
	// built-in table (builtinWindows).
	ContextWindows map[string]int
	// LookupEnv reads ACD_CTX_* variables. Nil means os.LookupEnv.
	LookupEnv func(string) (string, bool)
	// Now is the clock used for idle times and config reloads. Nil means time.Now.
	Now func() time.Time
	// ListenHost is the host part of the listen address. It is accepted in the
	// Host header in addition to the loopback names. Empty when unknown.
	ListenHost string
	// UpstreamAllow lists the origins (scheme://host[:port]) reachable through the
	// /ctx/<upstream>/ prefix. Default: the origin of Upstream only.
	UpstreamAllow []string
	// AllowedHosts, when non-empty, is the exact set of Host names the guard
	// accepts (replacing the loopback defaults and ListenHost). Set by serve for
	// non-loopback listening; see CheckListen.
	AllowedHosts []string
	// WSIdleTimeout closes a WebSocket connection that has had no client frame for
	// this long. Zero means defaultWSIdleTimeout.
	WSIdleTimeout time.Duration
	// WSMaxFrameBytes caps one WebSocket message in either direction. Zero means
	// maxBodyBytes.
	WSMaxFrameBytes int64
}

Config wires the proxy. Upstream and Store are required.

type Server

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

Server is the http.Handler of the proxy.

func New

func New(cfg Config) (*Server, error)

New validates cfg and builds the handler.

func (*Server) Close added in v1.0.1

func (s *Server) Close() error

Close closes the local databases the proxy opened. Config.Store belongs to the caller and stays open.

func (*Server) CloseWebSockets

func (s *Server) CloseWebSockets()

CloseWebSockets asks every open WebSocket session to close with 1001 (going away). serve calls it on shutdown, because http.Server does not track hijacked connections.

func (*Server) ServeHTTP

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

ServeHTTP checks the request against the loopback guard, then routes internal endpoints, the compaction endpoints and pass-through.

type TurnView

type TurnView struct {
	Visible []kernel.Message
	Nudge   string // "" when no nudge is due
	Hints   []kernel.RecallHint
	Row     string // decision row of spec §8.3, for diagnostics
}

TurnView is one planned turn as the library returns it.

func PlanTurn

func PlanTurn(ctx context.Context, st *store.Store, sess *session.Session, sid string, cfg config.Config, tokenCount int) (TurnView, error)

PlanTurn runs the per-turn steps of compactTurn that follow Ingest: it applies the project settings, plans the trigger, reduces the window (CCR and crush) and writes the nudge text. The caller ingests the transcript first. The cache gate of the server is not applied: a library caller has no upstream cache to measure.

Jump to

Keyboard shortcuts

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