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(vm VirtualModel, req *protocol.OpenAIChatCompletionRequest, emit func(any)) error
- func RegisterDefaults(r *Registry)
- func RegisterErrorMocks(r *Registry)
- func RegisterStreamTestMocks(r *Registry)
- type DelayConfig
- type DelayProvider
- type DeltaEvent
- type DoneEvent
- type MockModel
- type MockModelConfig
- type MockScenario
- type Registry
- 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(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 RegisterErrorMocks ¶ added in v0.260604.1
func RegisterErrorMocks(r *Registry)
RegisterErrorMocks registers the opt-in error-injection fixtures (virtual-fail-precontent-{429,500}, virtual-fail-midstream-{close,event}) into r. These always fail and exist so consumers can simulate a broken upstream by model name without standing up an ad-hoc httptest.Server. Intentionally separate from RegisterDefaults so production registries stay clean.
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(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 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(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.