Documentation
¶
Overview ¶
Package openai provides OpenAI-protocol virtual models.
All concrete virtual models in this package implement VirtualModel — a base vmodel.VirtualModel extended with HandleOpenAIChat / HandleOpenAIChatStream. Models are stored in a Registry that is scoped to this protocol; lookups never cross over to Anthropic.
Index ¶
- func DefaultStream(ctx context.Context, vm VirtualModel, ...) error
- func RegisterDefaults(r *Registry)
- func RegisterExtendedErrorMocks(r *Registry)
- func RegisterStreamTestMocks(r *Registry)
- type DelayConfig
- type DelayProvider
- type DeltaEvent
- type DoneEvent
- type MockModel
- type MockModelConfig
- type MockScenario
- type Registry
- type SequenceModel
- type Snapshotter
- type ToolEvent
- type UsageEvent
- type VModelResponse
- type VToolCall
- type VirtualModel
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultStream ¶
func DefaultStream(ctx context.Context, vm VirtualModel, req *protocol.OpenAIChatCompletionRequest, emit func(any)) error
DefaultStream is a stream adapter for batch-only OpenAI Chat models. It calls HandleOpenAIChat, chunks text content, and emits typed stream events. Batch-only models should delegate here.
func RegisterDefaults ¶
func RegisterDefaults(r *Registry)
RegisterDefaults registers the default OpenAI-protocol virtual models into r: the OpenAI-only "virtual-gpt-4" mock plus the shared mocks ("echo-model", "ask-user-question", "ask-confirmation", "web-search-example").
Compact-style transform models are Anthropic-only and live in the anthropic sub-package; they are intentionally not registered here.
func RegisterExtendedErrorMocks ¶ added in v0.260611.1
func RegisterExtendedErrorMocks(r *Registry)
RegisterExtendedErrorMocks registers additional error scenarios beyond the basic four: authentication failures, various upstream errors, network issues, and timeout scenarios. Like RegisterErrorMocks, this is opt-in and separate from RegisterDefaults.
func RegisterStreamTestMocks ¶ added in v0.260604.1
func RegisterStreamTestMocks(r *Registry)
RegisterStreamTestMocks registers the opt-in stream-test fixtures (virtual-stream-test, virtual-stream-test-tool) into r. These advertise deterministic, fully-populated usage (prompt / completion / cached input / reasoning) so streaming converters can be exercised end-to-end without a real upstream. Intentionally separate from RegisterDefaults so production registries stay clean.
Types ¶
type DelayConfig ¶
type DelayConfig struct {
// Delay before the first SSE chunk is emitted (controls TTFT).
MinFirstTokenDelayMs int
MaxFirstTokenDelayMs int
// Delay between the last content chunk and [DONE] (controls stream duration / TPS).
MinEndDelayMs int
MaxEndDelayMs int
}
DelayConfig configures the random delay ranges for a DelayProvider.
func DefaultDelayConfig ¶
func DefaultDelayConfig() DelayConfig
DefaultDelayConfig returns sensible defaults: 50–500ms TTFT, 100–1000ms end delay.
type DelayProvider ¶
type DelayProvider struct {
// contains filtered or unexported fields
}
DelayProvider is an embedded OpenAI-compatible HTTP server with configurable random delays. It accepts any chat completions request, streams the fixed response "I am testing and random sleep", and produces realistic TTFT / TPS / latency values for metrics E2E testing.
Use it as a typ.Provider in routing rules so requests flow through the full proxy pipeline (routing → OpenAI client → metrics tracking).
func NewDelayProvider ¶
func NewDelayProvider() *DelayProvider
NewDelayProvider creates and starts a DelayProvider with default config.
func NewDelayProviderWithConfig ¶
func NewDelayProviderWithConfig(cfg DelayConfig) *DelayProvider
NewDelayProviderWithConfig creates and starts a DelayProvider with a custom config.
func (*DelayProvider) Close ¶
func (dp *DelayProvider) Close()
Close shuts down the embedded HTTP server.
func (*DelayProvider) Provider ¶
func (dp *DelayProvider) Provider(name string) *typ.Provider
Provider returns a *typ.Provider configured to route to this delay server. name is used as both UUID and Name; it must be unique within the test config.
func (*DelayProvider) URL ¶
func (dp *DelayProvider) URL() string
URL returns the base URL of the embedded server.
type DeltaEvent ¶
DeltaEvent carries a text content chunk.
type DoneEvent ¶
type DoneEvent struct {
FinishReason string
}
DoneEvent signals end of stream with finish reason.
type MockModel ¶
type MockModel struct {
vmodel.BaseMockModel
// contains filtered or unexported fields
}
MockModel is an OpenAI-Chat-only mock virtual model.
func NewMockModel ¶
func NewMockModel(cfg *MockModelConfig) *MockModel
NewMockModel creates a MockModel. FinishReason defaults to "stop" (or "tool_calls" if ToolCall is set).
func (*MockModel) ErrorInjection ¶ added in v0.260604.1
func (m *MockModel) ErrorInjection() *vmodel.ErrorInjection
ErrorInjection implements vmodel.ErrorInjectingModel.
func (*MockModel) HandleOpenAIChat ¶
func (m *MockModel) HandleOpenAIChat(_ *protocol.OpenAIChatCompletionRequest) (VModelResponse, error)
HandleOpenAIChat returns fixed content from config in OpenAI Chat format.
func (*MockModel) HandleOpenAIChatStream ¶
func (m *MockModel) HandleOpenAIChatStream(ctx context.Context, req *protocol.OpenAIChatCompletionRequest, emit func(any)) error
HandleOpenAIChatStream streams fixed content using configured chunks with simulated delay.
type MockModelConfig ¶
type MockModelConfig struct {
ID string
Name string
Description string
Content string // static: fixed response text
FinishReason string // default: "stop" (or "tool_calls" if ToolCall is set)
Delay time.Duration // simulated latency
StreamChunks []string // optional custom stream chunks
// tool-type: if set, the response includes tool calls.
ToolCall *vmodel.ToolCallConfig
// Usage, when set, is emitted as a UsageEvent immediately before
// DoneEvent so streaming consumers can be exercised against a
// deterministic, fully-populated usage shape.
Usage *vmodel.MockUsage
// Error, when non-nil, makes this mock simulate a failure. See
// vmodel.ErrorInjection for the two supported stages.
Error *vmodel.ErrorInjection
}
MockModelConfig holds configuration for an OpenAI mock virtual model (static or tool).
type MockScenario ¶
type MockScenario struct {
ID string
Name string
Delay time.Duration
StreamChunks []string
Content string
ToolCalls []VToolCall
FinishReason string // defaults to "stop" (or "tool_calls" if ToolCalls non-empty)
// Error, when non-nil, makes the scenario simulate a failure instead of
// returning a normal response. See vmodel.ErrorInjection for the two
// supported stages (pre-content vs mid-stream).
Error *vmodel.ErrorInjection
}
MockScenario describes a named test scenario with an OpenAI Chat-protocol mock response.
type Registry ¶
type Registry = vmodel.GenericRegistry[VirtualModel]
Registry holds OpenAI Chat-protocol virtual models. It is a thin alias around vmodel.GenericRegistry instantiated for the OpenAI VirtualModel sub-interface.
func NewRegistry ¶
func NewRegistry() *Registry
NewRegistry creates a new OpenAI virtual model registry.
type SequenceModel ¶ added in v0.260723.1
type SequenceModel struct {
vmodel.BaseMockModel
// contains filtered or unexported fields
}
SequenceModel walks a configured program of per-request outcomes (e.g. 200, 200, 429) to simulate a flaky upstream. Each request atomically advances a shared cursor; the resolved step is materialised as a plain MockModel snapshot (a success returns content; an error step carries a pre-content ErrorInjection), so the handler needs no sequence-specific code.
func NewSequenceModel ¶ added in v0.260723.1
func NewSequenceModel(cfg *vmodel.SequenceConfig) *SequenceModel
NewSequenceModel constructs an OpenAI-protocol sequence model from cfg.
func NewStatusSequence ¶ added in v0.260723.1
func NewStatusSequence(id, name string, statuses ...int) *SequenceModel
NewStatusSequence is the shorthand for the common case: a sequence model driven purely by a list of status codes, e.g. NewStatusSequence("flaky", "Flaky", 200, 200, 429). Success content and error type/message come from module defaults; build a SequenceConfig and call NewSequenceModel when you need per-step content, repeats, or a non-default OnExhaust policy.
func (*SequenceModel) HandleOpenAIChat ¶ added in v0.260723.1
func (m *SequenceModel) HandleOpenAIChat(req *protocol.OpenAIChatCompletionRequest) (VModelResponse, error)
HandleOpenAIChat delegates to a freshly resolved snapshot so direct (non- handler) consumers still get one step per call. The production handler resolves via Snapshot first and never reaches this path.
func (*SequenceModel) HandleOpenAIChatStream ¶ added in v0.260723.1
func (m *SequenceModel) HandleOpenAIChatStream(ctx context.Context, req *protocol.OpenAIChatCompletionRequest, emit func(any)) error
HandleOpenAIChatStream mirrors HandleOpenAIChat for the streaming path.
func (*SequenceModel) Snapshot ¶ added in v0.260723.1
func (m *SequenceModel) Snapshot() VirtualModel
Snapshot advances the sequence and returns the MockModel snapshot for this request. This is the single point at which the cursor advances.
type Snapshotter ¶ added in v0.260723.1
type Snapshotter interface {
Snapshot() VirtualModel
}
Snapshotter is implemented by virtual models whose served behaviour varies per request (e.g. SequenceModel). The virtualserver handler calls Snapshot exactly once per request to obtain the concrete, stateless model that serves it — after which all existing dispatch machinery (ErrorInjection, HandleOpenAIChat, the mid-stream gate) works unchanged.
type UsageEvent ¶ added in v0.260604.1
UsageEvent carries explicit token usage that the model wants the wire stream to advertise (typically just before DoneEvent).
type VModelResponse ¶
VModelResponse describes what the virtual model wants to respond for OpenAI Chat.
type VToolCall ¶
VToolCall is a protocol-agnostic tool call returned by OpenAI Chat models. Using an intermediate type avoids coupling to the OpenAI SDK in this package; callers (e.g. virtualserver) translate it to the SDK form when serializing.
type VirtualModel ¶
type VirtualModel interface {
vmodel.VirtualModel
HandleOpenAIChat(req *protocol.OpenAIChatCompletionRequest) (VModelResponse, error)
HandleOpenAIChatStream(ctx context.Context, req *protocol.OpenAIChatCompletionRequest, emit func(any)) error
}
VirtualModel handles OpenAI Chat Completions requests.
func NewMockFromScenario ¶
func NewMockFromScenario(s *MockScenario) VirtualModel
NewMockFromScenario creates an OpenAI Chat VirtualModel from a MockScenario.
func Snapshot ¶ added in v0.260723.1
func Snapshot(vm VirtualModel) VirtualModel
Snapshot returns the concrete model to serve a single request with. If vm advances per request it is resolved to a stateless snapshot; otherwise vm is returned unchanged. Callers must invoke this once per request.