protocol

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: MIT Imports: 7 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)

GetAnthropicModels 通过 API Key 和 BaseURL 获取 Anthropic 模型列表

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 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.

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"`
	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 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"`
	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"`
	Temperature *float64        `json:"temperature,omitempty"`
	TopP        *float64        `json:"top_p,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 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 OpenAITool

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

type OpenAIToolCall

type OpenAIToolCall struct {
	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"`
}

Jump to

Keyboard shortcuts

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