protocol

package
v1.2.8 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BatchToGatewayModelAlias

func BatchToGatewayModelAlias(rawModels []string) []string

func FromGatewayModelAlias

func FromGatewayModelAlias(alias string, availableModels []string) string

FromGatewayModelAlias restores the original model name from an obfuscated alias.

func GetAnthropicModels

func GetAnthropicModels(baseURL, key string) (string, error)

func GetAnthropicModelsWithAuth added in v1.2.5

func GetAnthropicModelsWithAuth(baseURL, key, authStyle string) (string, error)

GetAnthropicModelsWithAuth fetches Anthropic-compatible models using either the official x-api-key header or a Bearer token used by some routers.

func GetOpenAIModels

func GetOpenAIModels(baseURL, apiKey string) (string, error)

func MapModel

func MapModel(requestedModel string, configuredModel string, availableModels []string) string

MapModel intelligently translates the requested Anthropic model name into the best available OpenAI gateway counterpart. Uses the authoritative models registry for tier classification and selection when possible, falling back to heuristic keyword matching for models not in the registry.

func NormalizeAnthropicBaseURLForClaude added in v1.2.5

func NormalizeAnthropicBaseURLForClaude(baseURL string) string

NormalizeAnthropicBaseURLForClaude returns the base URL shape expected by Claude Code's Anthropic client. Claude appends /v1/messages itself, so a configured endpoint ending in /v1, /v1/messages, or /v1/models would otherwise become /v1/v1/messages at runtime.

func NormalizeAnthropicMessagesURL added in v1.2.4

func NormalizeAnthropicMessagesURL(baseURL string) string

func NormalizeAnthropicModelsURL added in v1.2.4

func NormalizeAnthropicModelsURL(baseURL string) string

func NormalizeOpenAIChatCompletionsURL added in v1.2.4

func NormalizeOpenAIChatCompletionsURL(baseURL string) string

func NormalizeOpenAIModelsURL added in v1.2.4

func NormalizeOpenAIModelsURL(baseURL string) string

func NormalizeOpenAIResponsesURL added in v1.2.4

func NormalizeOpenAIResponsesURL(baseURL string) string

func ProbeOpenAIResponsesSupport added in v1.2.4

func ProbeOpenAIResponsesSupport(endpoint, apiKey, model string, timeout time.Duration) bool

ProbeOpenAIResponsesSupport sends a minimal, real generation request to the /v1/responses endpoint to determine whether an OpenAI-compatible gateway implements the newer, agent-oriented Responses API ("openai(agent)") as opposed to only the legacy Chat Completions API ("openai(chat)"). Listing models alone (/v1/models) cannot distinguish these two, since both protocols commonly share the same model catalog — an actual call to /v1/responses is required. Returns true only when the upstream responds with a 2xx status.

func ToGatewayModelAlias

func ToGatewayModelAlias(model string) string

ToGatewayModelAlias translates a raw model name into an Anthropic-compliant name that avoids Claude Code's hardcoded blacklist.

Types

type AnthropicMessage

type AnthropicMessage struct {
	Role    string `json:"role"`
	Content any    `json:"content"` // Can be string or []ContentBlock
}

type AnthropicModelResponse

type AnthropicModelResponse struct {
	Data []struct {
		CreatedAt   time.Time `json:"created_at"`
		DisplayName string    `json:"display_name"`
		Id          string    `json:"id"`
		Type        string    `json:"type"`
	} `json:"data"`
	FirstId string `json:"firstId"`
	HasMore bool   `json:"hasMore"`
	LastId  string `json:"lastId"`
}

type AnthropicRequest

type AnthropicRequest struct {
	Model         string               `json:"model"`
	Messages      []AnthropicMessage   `json:"messages"`
	System        any                  `json:"system,omitempty"` // Can be string or []ContentBlock
	MaxTokens     int                  `json:"max_tokens,omitempty"`
	Metadata      map[string]any       `json:"metadata,omitempty"`
	StopSequences []string             `json:"stop_sequences,omitempty"`
	Stream        bool                 `json:"stream,omitempty"`
	Temperature   *float64             `json:"temperature,omitempty"`
	TopK          *int                 `json:"top_k,omitempty"`
	TopP          *float64             `json:"top_p,omitempty"`
	Tools         []AnthropicTool      `json:"tools,omitempty"`
	ToolChoice    *AnthropicToolChoice `json:"tool_choice,omitempty"`
	Thinking      *AnthropicThinking   `json:"thinking,omitempty"`
}

AnthropicRequest matches the structure sent by Claude Code to /v1/messages.

type AnthropicResponse

type AnthropicResponse struct {
	ID           string         `json:"id"`
	Type         string         `json:"type"`
	Role         string         `json:"role"`
	Content      []ContentBlock `json:"content"`
	Model        string         `json:"model"`
	StopReason   string         `json:"stop_reason,omitempty"`
	StopSequence string         `json:"stop_sequence,omitempty"`
	Usage        AnthropicUsage `json:"usage"`
}

AnthropicResponse matches the structure expected by Claude Code.

func ConvertResponse

func ConvertResponse(oaResp *OpenAIResponse) (*AnthropicResponse, error)

ConvertResponse translates a standard OpenAI Chat Completions response to Anthropic style Messages.

func ConvertResponsesResponse added in v1.2.4

func ConvertResponsesResponse(resp *OpenAIResponsesResponse) (*AnthropicResponse, error)

type AnthropicThinking

type AnthropicThinking struct {
	Type         string `json:"type"` // e.g. "enabled"
	BudgetTokens int    `json:"budget_tokens,omitempty"`
}

type AnthropicTool

type AnthropicTool struct {
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	InputSchema json.RawMessage `json:"input_schema"` // JSON Schema
}

type AnthropicToolChoice

type AnthropicToolChoice struct {
	Type string `json:"type"`
	Name string `json:"name,omitempty"`
}

type AnthropicUsage

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

type ContentBlock

type ContentBlock struct {
	Type   string          `json:"type"`
	Text   string          `json:"text,omitempty"`
	Source *ImageSource    `json:"source,omitempty"` // for image
	ID     string          `json:"id,omitempty"`     // for tool_use
	Name   string          `json:"name,omitempty"`   // for tool_use
	Input  json.RawMessage `json:"input,omitempty"`  // for tool_use, can be arbitrary object

	Thinking  string `json:"thinking,omitempty"`  // for thinking
	Signature string `json:"signature,omitempty"` // for thinking_delta

	ToolUseID string `json:"tool_use_id,omitempty"` // for tool_result
	Content   any    `json:"content,omitempty"`     // for tool_result, can be string or []ContentBlock
	IsError   bool   `json:"is_error,omitempty"`    // for tool_result
}

type ImageSource added in v1.2.4

type ImageSource struct {
	Type      string `json:"type,omitempty"`
	MediaType string `json:"media_type,omitempty"`
	Data      string `json:"data,omitempty"`
	URL       string `json:"url,omitempty"`
}

type ModelResponse

type ModelResponse struct {
	Data []struct {
		Created  int    `json:"created"`
		Domain   string `json:"domain"`
		Features struct {
			StructuredOutputs struct {
				JsonObject bool `json:"json_object"`
				JsonSchema bool `json:"json_schema"`
			} `json:"structured_outputs,omitempty"`
			Tools struct {
				FunctionCalling bool `json:"function_calling"`
			} `json:"tools,omitempty"`
			Batch struct {
				BatchChat bool `json:"batch_chat"`
				BatchJob  bool `json:"batch_job"`
			} `json:"batch,omitempty"`
			Cache struct {
				PrefixCache  bool `json:"prefix_cache"`
				SessionCache bool `json:"session_cache"`
			} `json:"cache,omitempty"`
		} `json:"features"`
		Id         string `json:"id"`
		Name       string `json:"name"`
		Object     string `json:"object"`
		Status     string `json:"status,omitempty"`
		Version    string `json:"version"`
		Modalities struct {
			InputModalities  []string `json:"input_modalities,omitempty"`
			OutputModalities []string `json:"output_modalities,omitempty"`
		} `json:"modalities,omitempty"`
		TaskType    []string `json:"task_type,omitempty"`
		TokenLimits struct {
			ContextWindow           int `json:"context_window,omitempty"`
			MaxInputTokenLength     int `json:"max_input_token_length,omitempty"`
			MaxOutputTokenLength    int `json:"max_output_token_length,omitempty"`
			MaxReasoningTokenLength int `json:"max_reasoning_token_length,omitempty"`
		} `json:"token_limits,omitempty"`
	} `json:"data"`
	Object string `json:"object"`
}

type OpenAIChoice

type OpenAIChoice struct {
	Index        int           `json:"index"`
	Message      OpenAIMessage `json:"message"`
	FinishReason string        `json:"finish_reason"`
}

type OpenAIFunctionCall

type OpenAIFunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"` // JSON string
}

type OpenAIFunctionDef

type OpenAIFunctionDef struct {
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	Parameters  json.RawMessage `json:"parameters,omitempty"` // JSON Schema
}

type OpenAIImageURL

type OpenAIImageURL struct {
	URL    string `json:"url"`
	Detail string `json:"detail,omitempty"`
}

type OpenAIMessage

type OpenAIMessage struct {
	Role             string              `json:"role"`
	Content          any                 `json:"content,omitempty"` // string or []OpenAIMessagePart
	Name             string              `json:"name,omitempty"`    // for tool result
	ToolCallID       string              `json:"tool_call_id,omitempty"`
	ToolCalls        []OpenAIToolCall    `json:"tool_calls,omitempty"`
	FunctionCall     *OpenAIFunctionCall `json:"function_call,omitempty"`
	Refusal          string              `json:"refusal,omitempty"`
	ReasoningContent string              `json:"reasoning_content,omitempty"`
}

type OpenAIMessagePart

type OpenAIMessagePart struct {
	Type     string          `json:"type"`
	Text     string          `json:"text,omitempty"`
	ImageURL *OpenAIImageURL `json:"image_url,omitempty"`
}

type OpenAIRequest

type OpenAIRequest struct {
	Model         string               `json:"model"`
	Messages      []OpenAIMessage      `json:"messages"`
	MaxTokens     int                  `json:"max_tokens,omitempty"`
	Stream        bool                 `json:"stream,omitempty"`
	StreamOptions *OpenAIStreamOptions `json:"stream_options,omitempty"`
	Temperature   *float64             `json:"temperature,omitempty"`
	TopP          *float64             `json:"top_p,omitempty"`
	Stop          []string             `json:"stop,omitempty"`
	Tools         []OpenAITool         `json:"tools,omitempty"`
	ToolChoice    any                  `json:"tool_choice,omitempty"` // "none", "auto", or OpenAIToolChoice
}

OpenAIRequest matches the Chat Completions endpoint payload format (/v1/chat/completions).

func ConvertRequest

func ConvertRequest(antReq *AnthropicRequest) (*OpenAIRequest, error)

ConvertRequest maps an Anthropic Messages request to an OpenAI Chat Completions request.

type OpenAIResponse

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

OpenAIResponse matches the Chat Completions response.

type OpenAIResponsesRequest added in v1.2.4

type OpenAIResponsesRequest struct {
	Model           string                `json:"model"`
	Input           []ResponsesInputItem  `json:"input"`
	Instructions    string                `json:"instructions,omitempty"`
	MaxOutputTokens int                   `json:"max_output_tokens,omitempty"`
	Stream          bool                  `json:"stream,omitempty"`
	Temperature     *float64              `json:"temperature,omitempty"`
	TopP            *float64              `json:"top_p,omitempty"`
	Tools           []OpenAIResponsesTool `json:"tools,omitempty"`
	ToolChoice      any                   `json:"tool_choice,omitempty"`
	Metadata        map[string]any        `json:"metadata,omitempty"`
	Store           *bool                 `json:"store,omitempty"`
}

OpenAIResponsesRequest matches the OpenAI Responses API payload shape.

func ConvertRequestToResponses added in v1.2.4

func ConvertRequestToResponses(antReq *AnthropicRequest) (*OpenAIResponsesRequest, error)

type OpenAIResponsesResponse added in v1.2.4

type OpenAIResponsesResponse struct {
	ID     string                `json:"id"`
	Object string                `json:"object"`
	Model  string                `json:"model"`
	Status string                `json:"status"`
	Output []ResponsesOutputItem `json:"output"`
	Usage  OpenAIResponsesUsage  `json:"usage"`
}

type OpenAIResponsesTool added in v1.2.4

type OpenAIResponsesTool struct {
	Type        string          `json:"type"`
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	Parameters  json.RawMessage `json:"parameters,omitempty"`
}

type OpenAIResponsesUsage added in v1.2.4

type OpenAIResponsesUsage struct {
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
	TotalTokens  int `json:"total_tokens"`
}

type OpenAIStreamChoice

type OpenAIStreamChoice struct {
	Index        int               `json:"index"`
	Delta        OpenAIStreamDelta `json:"delta"`
	FinishReason *string           `json:"finish_reason"`
}

type OpenAIStreamChunk

type OpenAIStreamChunk struct {
	ID      string               `json:"id"`
	Object  string               `json:"object"`
	Created int64                `json:"created"`
	Model   string               `json:"model"`
	Choices []OpenAIStreamChoice `json:"choices"`
	Usage   *OpenAIUsage         `json:"usage,omitempty"`
}

OpenAIStreamChunk matches SSE lines returned from OpenAI stream.

type OpenAIStreamDelta

type OpenAIStreamDelta struct {
	Role             string           `json:"role,omitempty"`
	Content          string           `json:"content,omitempty"`
	ToolCalls        []OpenAIToolCall `json:"tool_calls,omitempty"`
	ReasoningContent string           `json:"reasoning_content,omitempty"`
}

type OpenAIStreamOptions added in v1.2.4

type OpenAIStreamOptions struct {
	IncludeUsage bool `json:"include_usage"`
}

type OpenAITool

type OpenAITool struct {
	Type     string            `json:"type"`
	Function OpenAIFunctionDef `json:"function"`
}

type OpenAIToolCall

type OpenAIToolCall struct {
	Index    int                `json:"index,omitempty"`
	ID       string             `json:"id"`
	Type     string             `json:"type"`
	Function OpenAIFunctionCall `json:"function"`
}

type OpenAIUsage

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

type ResponsesContentPart added in v1.2.4

type ResponsesContentPart struct {
	Type     string `json:"type"`
	Text     string `json:"text,omitempty"`
	ImageURL string `json:"image_url,omitempty"`
}

type ResponsesInputItem added in v1.2.4

type ResponsesInputItem struct {
	Type      string `json:"type"`
	Role      string `json:"role,omitempty"`
	Content   any    `json:"content,omitempty"`
	CallID    string `json:"call_id,omitempty"`
	Name      string `json:"name,omitempty"`
	Arguments string `json:"arguments,omitempty"`
	Output    string `json:"output,omitempty"`
	Status    string `json:"status,omitempty"`
}

type ResponsesOutputItem added in v1.2.4

type ResponsesOutputItem struct {
	ID        string                 `json:"id"`
	Type      string                 `json:"type"`
	Role      string                 `json:"role,omitempty"`
	Status    string                 `json:"status,omitempty"`
	Content   []ResponsesOutputPart  `json:"content,omitempty"`
	CallID    string                 `json:"call_id,omitempty"`
	Name      string                 `json:"name,omitempty"`
	Arguments string                 `json:"arguments,omitempty"`
	Summary   []ResponsesSummaryPart `json:"summary,omitempty"`
}

type ResponsesOutputPart added in v1.2.4

type ResponsesOutputPart struct {
	Type string `json:"type"`
	Text string `json:"text,omitempty"`
}

type ResponsesSummaryPart added in v1.2.4

type ResponsesSummaryPart struct {
	Type string `json:"type"`
	Text string `json:"text,omitempty"`
}

Jump to

Keyboard shortcuts

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