hook

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package hook defines the in-process lifecycle contract for gateway extensions.

BeforeCall runs after auth on every modality and may mutate CallContext or deny. AfterCall runs after the upstream attempt finishes. BeforeChat mutates typed chat IR; AfterChat observes completed chat attempts.

Package hook is the stable lifecycle API for gateway extensions.

In-process Go hooks register on the dataplane HookChain from cmd/gateway. Sandboxed WASM guests use the same semantics via internal/adapters/wasm (see docs/development/hooks/wasm.md); they do not import this package.

BeforeCall receives a mutable CallContext (principal, route, tags, metadata, body) and may allow, enrich, or deny. AfterCall runs after the provider attempt.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AfterCallHook

type AfterCallHook interface {
	Name() string
	AfterCall(ctx context.Context, call *CallContext, info AfterCallInfo) error
}

AfterCallHook runs after the attempt completes (success or error).

type AfterCallInfo

type AfterCallInfo struct {
	Status           string
	LatencyMs        int64
	ProviderType     string
	TargetModel      string
	PromptTokens     int64
	CompletionTokens int64
}

AfterCallInfo is passed to AfterCallHook after an upstream attempt finishes.

type AfterChatHook

type AfterChatHook interface {
	Name() string
	AfterChat(ctx context.Context, info AfterChatInfo) error
}

AfterChatHook runs after a chat dialect attempt completes (success or error).

type AfterChatInfo

type AfterChatInfo struct {
	Model        string
	Status       string
	LatencyMs    int64
	ProviderType string
	TargetModel  string
	// Dialect is the client wire format: "openai", "anthropic", or "gemini".
	Dialect string
	// Modality is the usage modality: "chat", "messages", or "generate_content".
	Modality string
}

AfterChatInfo is passed to AfterChatHook after a chat attempt finishes.

type BeforeCallHook

type BeforeCallHook interface {
	Name() string
	BeforeCall(ctx context.Context, call *CallContext) (CallDecision, error)
}

BeforeCallHook runs after authentication and may mutate call or deny.

type CallContext

type CallContext struct {
	Principal Principal
	Route     RouteContext
	Tags      map[string]string
	// Headers are sanitized inbound HTTP headers (lowercased keys) for policy/CEL.
	Headers  map[string]string
	Metadata map[string]any
	Body     []byte
	// RequestHeaders are applied to the upstream provider HTTP request after BeforeCall.
	// Keys are canonicalized with http.CanonicalHeaderKey when applied.
	RequestHeaders map[string]string
	// ResponseHeaders are merged onto the client HTTP response (allow and deny paths).
	ResponseHeaders map[string]string
}

CallContext is the mutable request-scoped bag passed through BeforeCall / AfterCall.

func (*CallContext) DeleteRequestHeader

func (c *CallContext) DeleteRequestHeader(key string)

DeleteRequestHeader removes an outbound upstream header overlay.

func (*CallContext) DeleteResponseHeader

func (c *CallContext) DeleteResponseHeader(key string)

DeleteResponseHeader removes a client response header overlay.

func (*CallContext) SetRequestHeader

func (c *CallContext) SetRequestHeader(key, value string)

SetRequestHeader sets an outbound upstream header on the call (after BeforeCall).

func (*CallContext) SetResponseHeader

func (c *CallContext) SetResponseHeader(key, value string)

SetResponseHeader sets a header to merge onto the client HTTP response.

type CallDecision

type CallDecision struct {
	Allow   bool
	Status  int               // default 403 when deny and unset
	Reason  string            // machine code, e.g. policy_violation
	Message string            // human-readable; optional
	Headers map[string]string // optional response headers on deny
}

CallDecision is returned by BeforeCall. Allow=false stops the request.

func Allow

func Allow() CallDecision

Allow returns an allowing decision.

func Deny

func Deny(status int, reason, message string) CallDecision

Deny returns a denying decision with the given HTTP status and reason code.

type ChatHook

type ChatHook interface {
	Name() string
	BeforeChat(ctx context.Context, req chatir.Request) (chatir.Request, error)
}

ChatHook mutates a typed chat request before provider dispatch.

type Principal

type Principal struct {
	OrganizationID string
	ProjectID      string
	TeamID         string
	EnvironmentID  string
	APIKeyID       string
	SigningKeyID   string
	KeyID          string
	AuthMethod     string
	Kind           string
	OwnerUserID    string
	Name           string
}

Principal is the authenticated API key identity for a call.

type RouteContext

type RouteContext struct {
	Model    string
	Path     string
	Stream   bool
	Modality string
}

RouteContext describes the requested operation (before or after provider binding).

Jump to

Keyboard shortcuts

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