mockgateway

package
v0.154.0 Latest Latest
Warning

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

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

Documentation

Overview

Package mockgateway implements a hermetic, deterministic stand-in for the inference-gateway HTTP API surface the CLI consumes: GET /v1/models, POST /v1/chat/completions (sync JSON and SSE streaming) and GET /v1/health.

Scenario resolution is stateless: every request carries the full message history, so the scenario is chosen by matching each scenario's regex against the first user message, and the turn within the scenario is the count of assistant messages already present in the request. The only mutable state is the error-injection counter and the request recording, both guarded by one mutex, which makes the server safe for concurrent use.

Index

Constants

View Source
const (
	DefaultContextWindow   = 128000
	DefaultInputPrice      = "0.0000025"   // per token → $2.50 per MTok
	DefaultOutputPrice     = "0.00001"     // per token → $10.00 per MTok
	DefaultCachePrice      = "0.00000025"  // per token → $0.25 per MTok
	DefaultCacheWritePrice = "0.000003125" // per token → $3.125 per MTok (1.25x input)
)

Metadata advertised for DefaultModel on /v1/models. The real gateway only includes these fields when ?include=context_window,pricing is set; the mock always serves them (the CLI always asks).

View Source
const AnthropicModel = "anthropic/claude-sonnet-4-5"

AnthropicModel is the Anthropic model the mock advertises; the CLI routes it through POST /v1/messages (native Anthropic SSE) instead of /v1/chat/completions.

View Source
const DefaultModel = "openai/gpt-4o"

DefaultModel is the primary model the mock advertises on /v1/models. Model ids carry the provider prefix; request bodies arrive with it stripped.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorInject

type ErrorInject struct {
	Status int `yaml:"status"`
	Times  int `yaml:"times"`
}

ErrorInject makes a turn answer with HTTP Status for the first Times requests that resolve to it; Times -1 fails forever.

type Recorded

type Recorded struct {
	// Endpoint is the request path (/v1/chat/completions or /v1/messages).
	Endpoint string
	// Provider is the ?provider= query parameter sent by the SDK.
	Provider string
	// Model is the request-body model (provider prefix already stripped).
	Model string
	// Scenario is the matched scenario name; empty when only the fallback applied.
	Scenario string
	// Step is the assistant-message count at the time of the request.
	Step int
	// Stream reports whether the request asked for SSE.
	Stream bool
	// Body is the full decoded request for /v1/chat/completions.
	Body sdk.CreateChatCompletionRequest
	// MessagesBody is the full decoded request for /v1/messages.
	MessagesBody *sdk.CreateMessagesRequest
}

Recorded captures one LLM request for test assertions.

type Scenario

type Scenario struct {
	// Name uniquely identifies the scenario in recordings and logs.
	Name string `yaml:"name"`
	// Match is a Go regular expression tested (unanchored) against the latest
	// user message of each request that is not an injected <system-reminder>.
	Match string `yaml:"match"`
	// Turns are the scripted assistant responses, indexed by the number of
	// assistant messages following the matched user message.
	Turns []Turn `yaml:"turns"`
	// contains filtered or unexported fields
}

Scenario is one scripted conversation, selected by regex.

type ScenarioFile

type ScenarioFile struct {
	// Fallback is rendered when no scenario matches the prompt or when a
	// matched scenario has no turn left for the current step.
	Fallback Turn `yaml:"fallback"`
	// Scenarios are evaluated in file order; the first regex match wins.
	Scenarios []Scenario `yaml:"scenarios"`
}

ScenarioFile is the root of a scenarios YAML document.

func Default

func Default() *ScenarioFile

Default returns the embedded built-in scenario library.

func Load

func Load(b []byte) (*ScenarioFile, error)

Load parses and validates a scenarios YAML document. Unknown fields are rejected so typos in scenario files fail fast.

type Server

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

Server is an http.Handler implementing the inference-gateway API surface the CLI consumes.

func New

func New(defs *ScenarioFile) *Server

New returns a Server serving the given scenario definitions.

func (*Server) Requests

func (s *Server) Requests() []Recorded

Requests returns a copy of all recorded chat-completion requests.

func (*Server) ServeHTTP

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

type StallInject added in v0.142.0

type StallInject struct {
	Times   int  `yaml:"times"`
	Connect bool `yaml:"connect"`
}

StallInject makes a turn's stream hang for the first Times requests that resolve to it; Times -1 stalls forever. By default the stream stalls after the initial role delta; with Connect true it stalls before the response headers, simulating a TCP connect that never completes.

type ToolCall

type ToolCall struct {
	// Name is the tool name as registered in the CLI (e.g. Read, Grep, Bash).
	Name string `yaml:"name"`
	// Args is marshaled into the tool call's JSON arguments string.
	Args map[string]any `yaml:"args"`
	// contains filtered or unexported fields
}

ToolCall describes one function call the mock model requests.

type Turn

type Turn struct {
	// Content is the assistant text, streamed in ChunkSize-rune fragments.
	Content string `yaml:"content"`
	// Reasoning is streamed as reasoning_content deltas before Content.
	Reasoning string `yaml:"reasoning"`
	// ToolCalls all land in this single assistant turn.
	ToolCalls []ToolCall `yaml:"tool_calls"`
	// Usage defaults to 10 prompt / 5 completion tokens when nil.
	Usage *Usage `yaml:"usage"`
	// ChunkSize is the fragment size in runes for streamed text (default 16).
	ChunkSize int `yaml:"chunk_size"`
	// DelayMs sleeps before each SSE frame (streaming) or once before the
	// body (sync), aborting early when the client disconnects.
	DelayMs int `yaml:"delay_ms"`
	// Error, when set, replaces the turn with an HTTP error for the first
	// Times matching requests (-1 means every request).
	Error *ErrorInject `yaml:"error"`
	// Stall, when set, makes the first Times streaming requests hang after
	// the initial role delta: the connection stays open but no further
	// frames arrive until the client disconnects. Exercises the CLI's
	// stalled-stream reconnect path.
	Stall *StallInject `yaml:"stall"`
	// Malformed emits one non-JSON data: frame early in the stream.
	Malformed bool `yaml:"malformed"`
}

Turn is one scripted assistant response, rendered as SSE or as a sync JSON body depending on the request's stream flag.

type Usage

type Usage struct {
	PromptTokens     int64 `yaml:"prompt_tokens"`
	CompletionTokens int64 `yaml:"completion_tokens"`
	CachedTokens     int64 `yaml:"cached_tokens"`
	CacheWriteTokens int64 `yaml:"cache_write_tokens"`
}

Usage overrides the token usage reported for a turn. CacheWriteTokens is only surfaced on the /v1/messages endpoint (cache_creation_input_tokens); the OpenAI-shaped usage has no field for it.

Jump to

Keyboard shortcuts

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