virtualserver

package
v0.260411.2300-preview Latest Latest
Warning

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

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

Documentation

Overview

Package virtualserver provides the HTTP handler for virtual model endpoints. It serves OpenAI Chat and Anthropic Messages API formats backed by a virtualmodel.Registry of deterministic VirtualModel instances.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnthropicContent

type AnthropicContent struct {
	Type  string          `json:"type"`
	Text  string          `json:"text,omitempty"`
	ID    string          `json:"id,omitempty"`
	Name  string          `json:"name,omitempty"`
	Input json.RawMessage `json:"input,omitempty"`
}

AnthropicContent is a content block in an Anthropic response.

type AnthropicDelta

type AnthropicDelta struct {
	Type       string `json:"type,omitempty"`
	Text       string `json:"text,omitempty"`
	StopReason string `json:"stop_reason,omitempty"`
}

AnthropicDelta is a delta in an Anthropic streaming response.

type AnthropicMessage

type AnthropicMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

AnthropicMessage is a message in Anthropic format.

type AnthropicMessageRequest

type AnthropicMessageRequest struct {
	Model     string             `json:"model"`
	Messages  []AnthropicMessage `json:"messages"`
	MaxTokens int                `json:"max_tokens,omitempty"`
	Stream    bool               `json:"stream,omitempty"`
	Tools     []AnthropicTool    `json:"tools,omitempty"`
}

AnthropicMessageRequest is an Anthropic-compatible messages request.

type AnthropicMessageResponse

type AnthropicMessageResponse struct {
	ID         string             `json:"id"`
	Type       string             `json:"type"`
	Role       string             `json:"role"`
	Model      string             `json:"model"`
	Content    []AnthropicContent `json:"content"`
	StopReason string             `json:"stop_reason"`
	Usage      AnthropicUsage     `json:"usage"`
}

AnthropicMessageResponse is an Anthropic-compatible message response.

type AnthropicStreamEvent

type AnthropicStreamEvent struct {
	Type    string                    `json:"type"`
	Message *AnthropicMessageResponse `json:"message,omitempty"`
	Index   int                       `json:"index,omitempty"`
	Delta   *AnthropicDelta           `json:"delta,omitempty"`
	Usage   *AnthropicUsage           `json:"usage,omitempty"`
}

AnthropicStreamEvent is a streaming event in Anthropic format.

type AnthropicTool

type AnthropicTool struct {
	Name        string          `json:"name"`
	Description string          `json:"description"`
	InputSchema json.RawMessage `json:"input_schema"`
}

AnthropicTool is a tool definition in Anthropic format.

type AnthropicUsage

type AnthropicUsage struct {
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
}

AnthropicUsage holds token usage in Anthropic format.

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Messages    []Message `json:"messages"`
	Model       string    `json:"model"`
	Temperature *float64  `json:"temperature,omitempty"`
	MaxTokens   *int      `json:"max_tokens,omitempty"`
	Stream      bool      `json:"stream,omitempty"`
}

ChatCompletionRequest is an OpenAI-compatible chat completion request.

type ChatCompletionResponse

type ChatCompletionResponse struct {
	ID      string   `json:"id"`
	Object  string   `json:"object"`
	Created int64    `json:"created"`
	Model   string   `json:"model"`
	Choices []Choice `json:"choices"`
	Usage   Usage    `json:"usage"`
}

ChatCompletionResponse is an OpenAI-compatible chat completion response.

type ChatCompletionStreamResponse

type ChatCompletionStreamResponse struct {
	ID      string         `json:"id"`
	Object  string         `json:"object"`
	Created int64          `json:"created"`
	Model   string         `json:"model"`
	Choices []StreamChoice `json:"choices"`
}

ChatCompletionStreamResponse is a streaming chunk in OpenAI format.

type Choice

type Choice struct {
	Index        int        `json:"index"`
	Message      Message    `json:"message"`
	FinishReason string     `json:"finish_reason"`
	Delta        Delta      `json:"delta,omitempty"`
	ToolCalls    []ToolCall `json:"tool_calls,omitempty"`
}

Choice is a choice in the response.

type Delta

type Delta struct {
	Role      string     `json:"role,omitempty"`
	Content   string     `json:"content,omitempty"`
	ToolCalls []ToolCall `json:"tool_calls,omitempty"`
}

Delta is the delta in a streaming response.

type FunctionCall

type FunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

FunctionCall is a function call.

type Handler

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

Handler handles HTTP requests for virtual model endpoints.

func NewHandler

func NewHandler(registry *virtualmodel.Registry) *Handler

NewHandler creates a new Handler backed by the given registry.

func (*Handler) ChatCompletions

func (h *Handler) ChatCompletions(c *gin.Context)

ChatCompletions handles POST /virtual/v1/chat/completions.

func (*Handler) ListModels

func (h *Handler) ListModels(c *gin.Context)

ListModels handles GET /virtual/v1/models.

func (*Handler) Messages

func (h *Handler) Messages(c *gin.Context)

Messages handles POST /virtual/v1/messages (Anthropic format).

type HandlerInterface

type HandlerInterface interface {
	ListModels(c *gin.Context)
	ChatCompletions(c *gin.Context)
	Messages(c *gin.Context)
}

HandlerInterface is satisfied by virtualserver.Handler and allows virtualmodel to reference the handler without a circular import.

type Message

type Message struct {
	Role       string     `json:"role"`
	Content    string     `json:"content"`
	ToolCalls  []ToolCall `json:"tool_calls,omitempty"`
	ToolCallID string     `json:"tool_call_id,omitempty"`
}

Message is a chat message.

type OpenAIModelsResponse

type OpenAIModelsResponse struct {
	Object string               `json:"object"`
	Data   []virtualmodel.Model `json:"data"`
}

OpenAIModelsResponse is the OpenAI models list response format.

type Service

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

Service manages the virtual model service.

func NewService

func NewService() *Service

NewService creates a new Service. Call SetHandler after constructing the virtualserver.Handler to complete wiring.

func (*Service) GetHandler

func (s *Service) GetHandler() HandlerInterface

GetHandler returns the HTTP handler.

func (*Service) GetModel

func (s *Service) GetModel(id string) *virtualmodel.VirtualModel

GetModel retrieves a virtual model by ID.

func (*Service) GetRegistry

func (s *Service) GetRegistry() *virtualmodel.Registry

GetRegistry returns the model registry.

func (*Service) ListModels

func (s *Service) ListModels() []virtualmodel.Model

ListModels returns all registered models.

func (*Service) RegisterModel

func (s *Service) RegisterModel(vm *virtualmodel.VirtualModel) error

RegisterModel registers a new virtual model.

func (*Service) SetHandler

func (s *Service) SetHandler(h HandlerInterface)

SetHandler wires the HTTP handler (virtualserver.Handler) into the service.

func (*Service) SetupRoutes

func (s *Service) SetupRoutes(group *gin.RouterGroup)

SetupRoutes sets up virtual model routes on a Gin router group. Panics if SetHandler has not been called.

func (*Service) UnregisterModel

func (s *Service) UnregisterModel(id string)

UnregisterModel unregisters a virtual model.

type StreamChoice

type StreamChoice struct {
	Index        int        `json:"index"`
	Delta        Delta      `json:"delta"`
	FinishReason *string    `json:"finish_reason"`
	ToolCalls    []ToolCall `json:"tool_calls,omitempty"`
}

StreamChoice is a choice in a streaming response.

type ToolCall

type ToolCall struct {
	ID       string       `json:"id"`
	Type     string       `json:"type"`
	Function FunctionCall `json:"function"`
}

ToolCall is a tool call in the response.

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

Usage holds token usage information.

Jump to

Keyboard shortcuts

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