virtualmodel

package
v0.260430.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: MPL-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultAnthropicStream added in v0.260414.2000

func DefaultAnthropicStream(vm AnthropicVirtualModel, req *protocol.AnthropicBetaMessagesRequest, emit func(any)) error

DefaultAnthropicStream is a stream adapter for batch-only Anthropic models. It calls HandleAnthropic, chunks text content via token.SplitIntoChunks, and emits typed stream events. Batch-only models should delegate here.

func DefaultOpenAIChatStream added in v0.260414.2000

func DefaultOpenAIChatStream(vm OpenAIChatVirtualModel, req *protocol.OpenAIChatCompletionRequest, emit func(any)) error

DefaultOpenAIChatStream is a stream adapter for batch-only OpenAI Chat models. It calls HandleOpenAIChat, chunks text content via token.SplitIntoChunks, and emits typed stream events. Batch-only models should delegate here.

func NewClaudeCodeCompactTransform added in v0.260414.2000

func NewClaudeCodeCompactTransform() transform.Transform

NewClaudeCodeCompactTransform creates a new ClaudeCodeCompactTransform.

func ToolCallDisplayContent added in v0.260414.2000

func ToolCallDisplayContent(args map[string]interface{}) string

ToolCallDisplayContent extracts display text from tool call arguments. It checks for "message" and "question" keys, returning the first non-empty value found.

Types

type AnthropicDoneEvent added in v0.260414.2000

type AnthropicDoneEvent struct {
	StopReason string
}

AnthropicDoneEvent signals end of stream with stop reason.

type AnthropicMockResponse added in v0.260414.2000

type AnthropicMockResponse struct {
	Content    string
	ToolCall   *ToolCallConfig
	StopReason string // defaults to "stop" (or "tool_use" if ToolCall is set)
}

AnthropicMockResponse defines the Anthropic-protocol mock response for a scenario.

type AnthropicStreamStartEvent added in v0.260414.2000

type AnthropicStreamStartEvent struct {
	MsgID string
	Model string
}

AnthropicStreamStartEvent signals the start of a message stream.

type AnthropicTextDeltaEvent added in v0.260414.2000

type AnthropicTextDeltaEvent struct {
	Index int
	Text  string
}

AnthropicTextDeltaEvent carries a text content chunk.

type AnthropicToolUseEvent added in v0.260414.2000

type AnthropicToolUseEvent struct {
	Index int
	ID    string
	Name  string
	Input json.RawMessage
}

AnthropicToolUseEvent carries a complete tool_use block.

type AnthropicVirtualModel added in v0.260414.2000

type AnthropicVirtualModel interface {
	VirtualModel
	HandleAnthropic(req *protocol.AnthropicBetaMessagesRequest) (VModelResponse, error)
	HandleAnthropicStream(req *protocol.AnthropicBetaMessagesRequest, emit func(any)) error
}

AnthropicVirtualModel handles Anthropic Beta Messages requests. Models that support the Anthropic protocol implement this in addition to VirtualModel. HandleAnthropicStream must always be implemented; models that are batch-only may delegate to DefaultAnthropicStream.

type ClaudeCodeCompactTransform added in v0.260414.2000

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

ClaudeCodeCompactTransform conditionally applies XML compression for Claude Code. Only activates when: 1. Last user message contains "compact" (case-insensitive) 2. Request has tool definitions

func (*ClaudeCodeCompactTransform) Apply added in v0.260414.2000

Apply applies XML compression only if conditions are met.

func (*ClaudeCodeCompactTransform) Name added in v0.260414.2000

Name returns the transform identifier.

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 MockModel added in v0.260414.2000

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

MockModel implements VirtualModel, AnthropicVirtualModel, and OpenAIChatVirtualModel for static and tool-type virtual models. HandleAnthropic/HandleOpenAIChat are no-ops; ResponseAnthropic/ResponseOpenAIChat return fixed content from config.

func NewMockModel added in v0.260414.2000

func NewMockModel(cfg *MockModelConfig) *MockModel

NewMockModel creates a MockModel. FinishReason defaults to "stop" (or "tool_use" if ToolCall is set).

func (*MockModel) GetID added in v0.260414.2000

func (m *MockModel) GetID() string

func (*MockModel) HandleAnthropic added in v0.260414.2000

HandleAnthropic returns fixed content from config in Anthropic format.

func (*MockModel) HandleAnthropicStream added in v0.260414.2000

func (m *MockModel) HandleAnthropicStream(req *protocol.AnthropicBetaMessagesRequest, emit func(any)) error

HandleAnthropicStream streams fixed content using GetStreamChunks with simulated delay.

func (*MockModel) HandleOpenAIChat added in v0.260414.2000

HandleOpenAIChat returns fixed content from config in OpenAI Chat format.

func (*MockModel) HandleOpenAIChatStream added in v0.260414.2000

func (m *MockModel) HandleOpenAIChatStream(req *protocol.OpenAIChatCompletionRequest, emit func(any)) error

HandleOpenAIChatStream streams fixed content using GetStreamChunks with simulated delay.

func (*MockModel) Protocols added in v0.260414.2000

func (m *MockModel) Protocols() []protocol.APIType

Protocols declares that MockModel supports both Anthropic and OpenAI Chat.

func (*MockModel) SimulatedDelay added in v0.260414.2000

func (m *MockModel) SimulatedDelay() time.Duration

func (*MockModel) ToModel added in v0.260414.2000

func (m *MockModel) ToModel() Model

type MockModelConfig added in v0.260414.2000

type MockModelConfig struct {
	ID           string
	Name         string
	Description  string
	Content      string        // static: fixed response text
	FinishReason string        // default: "stop"
	Delay        time.Duration // simulated latency
	StreamChunks []string      // optional custom stream chunks

	// tool-type: if set, Response returns a tool_use block
	ToolCall *ToolCallConfig
}

MockModelConfig holds configuration for a mock virtual model (static or tool).

type MockScenario added in v0.260414.2000

type MockScenario struct {
	ID           string
	Name         string
	Delay        time.Duration
	StreamChunks []string

	Anthropic  *AnthropicMockResponse  // if set → implements AnthropicVirtualModel
	OpenAIChat *OpenAIChatMockResponse // if set → implements OpenAIChatVirtualModel
}

MockScenario describes a named test scenario with per-protocol mock responses. Fields Anthropic and OpenAIChat are optional; set at least one. The resulting model implements only the sub-interfaces for which a response is provided.

type Model

type Model struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Created int64  `json:"created"`
	OwnedBy string `json:"owned_by"`
}

Model represents a virtual model in the models list (OpenAI-compatible format).

type OpenAIChatDeltaEvent added in v0.260414.2000

type OpenAIChatDeltaEvent struct {
	Index   int
	Content string
}

OpenAIChatDeltaEvent carries a text content chunk.

type OpenAIChatDoneEvent added in v0.260414.2000

type OpenAIChatDoneEvent struct {
	FinishReason string
}

OpenAIChatDoneEvent signals end of stream with finish reason.

type OpenAIChatMockResponse added in v0.260414.2000

type OpenAIChatMockResponse struct {
	Content      string
	ToolCalls    []VToolCall
	FinishReason string // defaults to "stop" (or "tool_calls" if ToolCalls non-empty)
}

OpenAIChatMockResponse defines the OpenAI Chat-protocol mock response for a scenario.

type OpenAIChatToolEvent added in v0.260414.2000

type OpenAIChatToolEvent struct {
	Index    int
	ToolCall VToolCall
}

OpenAIChatToolEvent carries a tool call.

type OpenAIChatVModelResponse added in v0.260414.2000

type OpenAIChatVModelResponse struct {
	Content      string
	ToolCalls    []VToolCall
	FinishReason string
}

OpenAIChatVModelResponse describes what the virtual model returns for OpenAI Chat.

type OpenAIChatVirtualModel added in v0.260414.2000

type OpenAIChatVirtualModel interface {
	VirtualModel
	HandleOpenAIChat(req *protocol.OpenAIChatCompletionRequest) (OpenAIChatVModelResponse, error)
	HandleOpenAIChatStream(req *protocol.OpenAIChatCompletionRequest, emit func(any)) error
}

OpenAIChatVirtualModel handles OpenAI Chat Completions requests. Models that support the OpenAI Chat protocol implement this in addition to VirtualModel. HandleOpenAIChatStream must always be implemented; models that are batch-only may delegate to DefaultOpenAIChatStream.

type Registry

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

Registry manages virtual models indexed by ID and by protocol type. At Register time it reads vm.Protocols() to build a protocol index and validates that each declared APIType maps to the corresponding sub-interface.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new virtual model registry.

func (*Registry) Clear

func (r *Registry) Clear()

Clear clears all registered models from all indexes.

func (*Registry) Get

func (r *Registry) Get(id string) VirtualModel

Get retrieves a virtual model by ID (protocol-agnostic).

func (*Registry) GetAnthropicVM added in v0.260414.2000

func (r *Registry) GetAnthropicVM(id string) AnthropicVirtualModel

GetAnthropicVM returns the AnthropicVirtualModel for id, or nil if not found or if the model does not support the Anthropic protocol.

func (*Registry) GetOpenAIChatVM added in v0.260414.2000

func (r *Registry) GetOpenAIChatVM(id string) OpenAIChatVirtualModel

GetOpenAIChatVM returns the OpenAIChatVirtualModel for id, or nil if not found or if the model does not support the OpenAI Chat protocol.

func (*Registry) List

func (r *Registry) List() []VirtualModel

List returns all registered virtual models.

func (*Registry) ListModels

func (r *Registry) ListModels() []Model

ListModels returns all registered models as Model slices.

func (*Registry) Register

func (r *Registry) Register(vm VirtualModel) error

Register registers a virtual model. It reads vm.Protocols() to populate the protocol index and validates that each declared APIType corresponds to an implemented sub-interface:

  • TypeAnthropicV1 / TypeAnthropicBeta → AnthropicVirtualModel
  • TypeOpenAIChat / TypeOpenAIResponses → OpenAIChatVirtualModel

func (*Registry) RegisterDefaults

func (r *Registry) RegisterDefaults()

RegisterDefaults registers default virtual models.

func (*Registry) Unregister

func (r *Registry) Unregister(id string)

Unregister removes a virtual model from all indexes.

type ToolCallConfig

type ToolCallConfig struct {
	Name      string                 `json:"name" yaml:"name"`
	Arguments map[string]interface{} `json:"arguments" yaml:"arguments"`
}

ToolCallConfig defines a tool call to be returned by the virtual model.

type TransformModel added in v0.260414.2000

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

TransformModel implements AnthropicVirtualModel for proxy/transform virtual models. HandleAnthropic applies Chain and Transformer in-place on req; ResponseAnthropic reads the result. It does NOT implement OpenAIChatVirtualModel — transform models are Anthropic-only.

func NewTransformModel added in v0.260414.2000

func NewTransformModel(cfg *TransformModelConfig) *TransformModel

NewTransformModel creates a TransformModel.

func (*TransformModel) GetID added in v0.260414.2000

func (m *TransformModel) GetID() string

func (*TransformModel) HandleAnthropic added in v0.260414.2000

HandleAnthropic applies Chain then Transformer to req in-place and returns the response.

func (*TransformModel) HandleAnthropicStream added in v0.260414.2000

func (m *TransformModel) HandleAnthropicStream(req *protocol.AnthropicBetaMessagesRequest, emit func(any)) error

HandleAnthropicStream delegates to DefaultAnthropicStream since TransformModel is batch-only.

func (*TransformModel) Protocols added in v0.260414.2000

func (m *TransformModel) Protocols() []protocol.APIType

Protocols declares that TransformModel is Anthropic-only.

func (*TransformModel) SimulatedDelay added in v0.260414.2000

func (m *TransformModel) SimulatedDelay() time.Duration

SimulatedDelay is always 0 — transform models don't simulate latency.

func (*TransformModel) ToModel added in v0.260414.2000

func (m *TransformModel) ToModel() Model

type TransformModelConfig added in v0.260414.2000

type TransformModelConfig struct {
	ID          string
	Name        string
	Description string
	Transformer transform.Transform       // applied after Chain
	Chain       *transform.TransformChain // applied first
}

TransformModelConfig holds configuration for a transform virtual model (proxy).

type VModelResponse added in v0.260414.2000

type VModelResponse struct {
	Content    []anthropic.BetaContentBlockParamUnion
	StopReason anthropic.BetaStopReason
}

VModelResponse describes what the virtual model wants to respond (Anthropic protocol).

type VToolCall added in v0.260414.2000

type VToolCall struct {
	ID        string
	Name      string
	Arguments string // JSON-encoded arguments
}

VToolCall is a protocol-agnostic tool call used in OpenAI Chat responses. Using an intermediate type avoids coupling to the OpenAI SDK here.

type VirtualModel

type VirtualModel interface {
	GetID() string
	SimulatedDelay() time.Duration
	ToModel() Model

	// Protocols returns the set of API types this model supports.
	// Each declared type must correspond to an implemented sub-interface.
	Protocols() []protocol.APIType
}

VirtualModel is the protocol-agnostic base interface. All virtual model types implement this.

Protocols() declares which API types this model supports. Registry uses this at registration time to build a protocol index and validate that declared protocols match the implemented sub-interfaces (AnthropicVirtualModel, OpenAIChatVirtualModel). Callers use Registry typed getters instead of performing type assertions themselves.

func NewMockModelFromScenario added in v0.260414.2000

func NewMockModelFromScenario(s *MockScenario) VirtualModel

NewMockModelFromScenario creates a VirtualModel from a MockScenario. The returned model implements AnthropicVirtualModel if Anthropic is set, and OpenAIChatVirtualModel if OpenAIChat is set.

Jump to

Keyboard shortcuts

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