ext

package
v0.1.64 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package ext is the public extension API for building custom gateway binaries on top of GoModel. External modules register request rewriters, HTTP middleware, and extra routes on a Registry (usually ext.Default) before starting the gateway; core consumes an immutable snapshot of the registry at server construction. An empty registry adds zero request overhead.

Index

Constants

This section is empty.

Variables

View Source
var Default = &Registry{}

Default is the process-wide registry used by package-level helpers and, by default, by run.Run.

Functions

func AddPublicPaths

func AddPublicPaths(paths ...string)

AddPublicPaths registers auth-skip paths on the Default registry.

func RegisterRewriter

func RegisterRewriter(rw RequestRewriter)

RegisterRewriter registers a rewriter on the Default registry.

func RegisterRoutes

func RegisterRoutes(fn func(e *echo.Echo))

RegisterRoutes registers a route callback on the Default registry.

func UseMiddleware

func UseMiddleware(m echo.MiddlewareFunc)

UseMiddleware registers middleware on the Default registry.

Types

type Endpoint

type Endpoint string

Endpoint identifies an inference endpoint whose raw JSON body can be rewritten before core parses it.

const (
	EndpointChatCompletions Endpoint = "/v1/chat/completions"
	EndpointMessages        Endpoint = "/v1/messages"
	EndpointResponses       Endpoint = "/v1/responses"
)

Endpoints eligible for request rewriting. Subroutes (for example /v1/messages/count_tokens or /v1/responses/{id}) are never rewritten.

type Input

type Input struct {
	Endpoint Endpoint
	// Body is the raw JSON request body, already bounded by the server's
	// body-size limit.
	Body []byte
	// Header is a clone of the inbound request headers with credential
	// values (Authorization, cookies, API keys, ...) redacted. Rewriters
	// run post-auth; use UserPath for identity.
	Header http.Header
	// UserPath is the canonical authenticated user path, when present.
	UserPath string
	// RequestID is the request correlation ID (X-Request-ID).
	RequestID string
}

Input is the raw inbound request handed to a rewriter before core parses it. Body and Header are snapshots owned by the middleware; rewriters must treat them as read-only and return new values in Result when changing anything.

type Registry

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

Registry collects extensions to be consumed by the gateway at startup. Register everything before the server is constructed (before run.Run or app.New); core snapshots the registry once and never consults it again.

func (*Registry) AddPublicPaths

func (r *Registry) AddPublicPaths(paths ...string)

AddPublicPaths appends paths to the authentication skip list (for example OAuth callback endpoints). A trailing "/*" matches a prefix.

func (*Registry) Middleware

func (r *Registry) Middleware() []echo.MiddlewareFunc

Middleware returns a defensive copy of the registered middleware.

func (*Registry) PublicPaths

func (r *Registry) PublicPaths() []string

PublicPaths returns a defensive copy of the registered public paths.

func (*Registry) RegisterRewriter

func (r *Registry) RegisterRewriter(rw RequestRewriter)

RegisterRewriter adds a request rewriter. Rewriters run in registration order, each receiving the previous rewriter's output.

func (*Registry) RegisterRoutes

func (r *Registry) RegisterRoutes(fn func(e *echo.Echo))

RegisterRoutes adds a callback that registers extra routes after all core routes. Paths are relative to the server base path.

func (*Registry) Rewriters

func (r *Registry) Rewriters() []RequestRewriter

Rewriters returns a defensive copy of the registered rewriters.

func (*Registry) Routes

func (r *Registry) Routes() []func(*echo.Echo)

Routes returns a defensive copy of the registered route callbacks.

func (*Registry) UseMiddleware

func (r *Registry) UseMiddleware(m echo.MiddlewareFunc)

UseMiddleware adds an Echo middleware that runs after audit capture and before gateway authentication, so it can normalize credentials (for example an SSO session) before the gateway auth check.

type RejectionError

type RejectionError struct {
	Status  int
	Code    string
	Message string
}

RejectionError rejects the request with a client-visible status code and machine-readable error code, rendered in the endpoint's native error dialect (OpenAI or Anthropic envelope).

func (*RejectionError) Error

func (e *RejectionError) Error() string

type RequestRewriter

type RequestRewriter interface {
	Name() string
	Rewrite(ctx context.Context, in Input) (*Result, error)
}

RequestRewriter rewrites raw JSON request bodies at ingress, after authentication and before model resolution, so body changes (including the "model" field) affect routing, failover, guardrails, budgets, and caching.

Rewriters run once per request in registration order; each receives the previous rewriter's output. Implementations must be safe for concurrent use. Errors fail the request (fail-closed): return a *RejectionError for a client-visible status, any other error maps to HTTP 500.

type Result

type Result struct {
	Body []byte
	// ResponseHeader entries are merged into the HTTP response so rewriters
	// can annotate what they did (for example X-GoModel-Pro-Tokens-Saved).
	ResponseHeader http.Header
	// Detail optionally carries a JSON-serializable summary of what the
	// rewriter changed. It is recorded in the audit trail's request-revision
	// chain and never sent upstream; it must never contain secrets or
	// request credentials.
	Detail any
	// TokensSaved is the rewriter's estimate of prompt tokens its body
	// change removed from the request. When positive and the rewritten body
	// is applied, core adds it to the request's usage record together with
	// the input cost those tokens would have incurred, and the dashboard
	// aggregates both as rewrite savings. Leave zero when the rewrite does
	// not shrink the prompt.
	TokensSaved int
}

Result carries a rewritten body and response-header annotations. A nil Result (or nil Body) means the request is unchanged.

Jump to

Keyboard shortcuts

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