converter

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FormatChat      = "chat"
	FormatResponses = "responses"
	FormatAnthropic = "anthropic"
	FormatGemini    = "gemini"
)

Format constants for protocol identification.

Variables

This section is empty.

Functions

func BuildResponsesFromChat

func BuildResponsesFromChat(chatReq *types.ChatRequest, stream bool) *types.ResponsesRequest

func BuildSummarizationRequest added in v0.1.12

func BuildSummarizationRequest(conversation, model string) *types.ChatRequest

BuildSummarizationRequest creates a Chat API request for LLM-based summarization.

func ConvertAnthropicLineToChat

func ConvertAnthropicLineToChat(state *AnthropicToChatState, line string) any

ConvertAnthropicLineToChat processes a raw Anthropic SSE line and returns a ChatStreamResponse chunk, or nil if the line should be skipped. Returns the string "[DONE]" when the stream is complete.

func ConvertAnthropicLineToResponses

func ConvertAnthropicLineToResponses(w SSEWriter, state *AnthropicToResponsesState, line string) bool

ConvertAnthropicLineToResponses processes a raw Anthropic SSE line and writes corresponding Responses API SSE events via the writer. Returns true when done.

func ConvertChatChunkToAnthropicSSE

func ConvertChatChunkToAnthropicSSE(w SSEWriter, state *AnthropicStreamState, data string) bool

ConvertChatChunkToAnthropicSSE processes a single Chat SSE data line and emits corresponding Anthropic Messages SSE events. Returns true when stream is done.

message_start uses input_tokens: 0 as placeholder. message_delta is deferred until upstream usage arrives (or [DONE] as fallback), and includes both input_tokens and output_tokens from real upstream data.

Handles DeepSeek reasoning_content by emitting Anthropic thinking blocks.

func ConvertChatChunkToResponsesSSE

func ConvertChatChunkToResponsesSSE(w SSEWriter, state *ResponsesStreamState, data string) bool

ConvertChatChunkToResponsesSSE processes a single Chat SSE data line and emits corresponding Responses API SSE events. Returns true when stream is done.

func ConvertGeminiLineToAnthropicSSE added in v0.1.11

func ConvertGeminiLineToAnthropicSSE(w SSEWriter, state *GeminiToAnthropicState, data string) bool

ConvertGeminiLineToAnthropicSSE processes a Gemini SSE line and emits Anthropic SSE. Returns true when stream is done.

func ConvertGeminiLineToChat added in v0.1.11

func ConvertGeminiLineToChat(state *GeminiToChatState, line string) any

ConvertGeminiLineToChat processes a Gemini SSE line and buffers Chat SSE chunks. Returns the next buffered chunk, or nil if none available. Pass an empty string to drain the next buffered chunk without parsing new input. The handler must call this in a loop after each upstream line to emit all chunks.

func ConvertGeminiLineToResponsesSSE added in v0.1.11

func ConvertGeminiLineToResponsesSSE(w SSEWriter, state *GeminiToResponsesState, data string) bool

ConvertGeminiLineToResponsesSSE processes a Gemini SSE line and emits Responses SSE. Returns true when stream is done.

func ConvertResponsesEventToAnthropicSSE added in v0.1.4

func ConvertResponsesEventToAnthropicSSE(w SSEWriter, state *ResponsesToAnthropicState, data string) bool

ConvertResponsesEventToAnthropicSSE processes a raw Responses SSE data line and emits corresponding Anthropic Messages SSE events via the writer. Returns true when done.

func ConvertResponsesLineToChat

func ConvertResponsesLineToChat(state *ResponsesToChatState, line string) any

ConvertResponsesLineToChat processes a raw Responses SSE line and returns a ChatStreamResponse chunk, or nil if the line should be skipped. Returns the string "[DONE]" when the stream is complete.

func DecodeCompactionPayload added in v0.1.12

func DecodeCompactionPayload(encoded string) (*types.CompactionPayload, error)

DecodeCompactionPayload decodes an ai-switch fake encrypted_content back to the payload.

func EmitCompleted added in v0.1.4

func EmitCompleted(w SSEWriter, state *AnthropicToResponsesState)

EmitCompleted emits all terminal Responses API events and marks the stream as done. Idempotent: safe to call multiple times — only emits once.

func EncodeCompactionPayload added in v0.1.12

func EncodeCompactionPayload(payload *types.CompactionPayload) (string, error)

EncodeCompactionPayload serializes a CompactionPayload into a self-contained encrypted_content string.

func ExtractConversationText added in v0.1.12

func ExtractConversationText(input any) string

ExtractConversationText formats Responses API input items into a readable conversation string. Compaction items are skipped.

func FormatSSEEvent

func FormatSSEEvent(eventType string, data []byte) string

FormatSSEEvent formats an SSE event string.

func IsFakeCompaction added in v0.1.12

func IsFakeCompaction(encryptedContent string) bool

IsFakeCompaction returns true if the encrypted_content was produced by ai-switch.

func NormalizeRole added in v0.1.5

func NormalizeRole(role string) string

NormalizeRole maps unsupported roles to "user".

func ParseSSEDataLine

func ParseSSEDataLine(line string) string

ParseSSEDataLine extracts the data portion from an SSE line. Returns empty string if not a data line.

func StripThinkTag

func StripThinkTag(text, tag string) string

StripThinkTag removes all <{tag}>...</{tag}> blocks from text. Returns text unchanged if tag is empty.

Types

type AnthropicContentBlock

type AnthropicContentBlock struct {
	Type      string `json:"type"`
	Thinking  string `json:"thinking,omitempty"`
	Text      string `json:"text,omitempty"`
	ID        string `json:"id,omitempty"`
	Name      string `json:"name,omitempty"`
	Input     any    `json:"input,omitempty"`
	ToolUseID string `json:"tool_use_id,omitempty"`
	Content   any    `json:"content,omitempty"`
}

type AnthropicMessage

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

type AnthropicRequest

type AnthropicRequest struct {
	Model       string             `json:"model"`
	Messages    []AnthropicMessage `json:"messages"`
	System      any                `json:"system,omitempty"` // string or []AnthropicSystemBlock
	MaxTokens   int                `json:"max_tokens,omitempty"`
	Temperature float64            `json:"temperature,omitempty"`
	TopP        float64            `json:"top_p,omitempty"`
	Stream      bool               `json:"stream,omitempty"`
	Metadata    map[string]any     `json:"metadata,omitempty"`
	Tools       []AnthropicTool    `json:"tools,omitempty"`
	ToolChoice  any                `json:"tool_choice,omitempty"`
}

AnthropicRequest represents an Anthropic Messages API request.

type AnthropicResponse

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

AnthropicResponse represents an Anthropic Messages API response.

type AnthropicStreamState

type AnthropicStreamState struct {
	MessageID         string
	Model             string
	BlockIndex        int
	ContentSent       bool
	AccText           string
	InputTokens       int
	OutputTokens      int
	CacheCreateTokens int
	CacheReadTokens   int
	ThinkTag          string
	TagState          ThinkTagState
	FinishReason      string
	DeltaSent         bool
	TextBlockClosed   bool
	TextBlockStarted  bool
	TextBlockIdx      int
	ReasoningStarted  bool
	ReasoningClosed   bool
	ToolBlocks        map[int]*toolBlockState
	SawToolCall       bool
}

AnthropicStreamState tracks state across SSE chunks for Anthropic conversion.

type AnthropicSystemBlock

type AnthropicSystemBlock struct {
	Type string `json:"type"`
	Text string `json:"text"`
}

type AnthropicToChatState

type AnthropicToChatState struct {
	ID                string
	Model             string
	Created           int64
	AccText           string
	StopReason        string
	InputTokens       int
	OutputTokens      int
	CacheCreateTokens int
	CacheReadTokens   int
	Started           bool

	// Tool use tracking: Anthropic block index → tool block info
	ToolBlocks      map[int]*anthropicToolBlock
	ToolCallCounter int
}

AnthropicToChatState tracks state when converting Anthropic SSE to Chat SSE.

func (*AnthropicToChatState) ChatStreamUsage added in v0.1.4

func (s *AnthropicToChatState) ChatStreamUsage() (id, model string, input, output int)

ChatStreamUsage returns token usage for the final Chat SSE usage chunk.

type AnthropicToResponsesState

type AnthropicToResponsesState struct {
	ResponseID        string
	ItemID            string
	Model             string
	Created           int64
	AccText           string
	CreatedSent       bool
	ItemSent          bool
	CompletedSent     bool
	SeqNum            int
	InputTokens       int
	OutputTokens      int
	CacheCreateTokens int
	CacheReadTokens   int
	ThinkTag          string
	TagState          ThinkTagState

	// Tool use tracking
	CurrentBlockType string
	CurrentBlockIdx  int
	FuncArgsBuf      map[int]*strings.Builder
	FuncNames        map[int]string
	FuncCallIDs      map[int]string
	TextItemID       string
	TextDoneSent     bool
	OutputIndex      int
}

AnthropicToResponsesState tracks state when converting Anthropic SSE to Responses SSE.

func (*AnthropicToResponsesState) EnsureInit added in v0.1.5

func (s *AnthropicToResponsesState) EnsureInit()

func (*AnthropicToResponsesState) NextSeq added in v0.1.4

func (s *AnthropicToResponsesState) NextSeq() int

type AnthropicTool added in v0.1.5

type AnthropicTool struct {
	Name        string         `json:"name"`
	Description string         `json:"description,omitempty"`
	InputSchema map[string]any `json:"input_schema,omitempty"`
}

type AnthropicUsage

type AnthropicUsage struct {
	InputTokens              int `json:"input_tokens"`
	OutputTokens             int `json:"output_tokens"`
	CacheCreationInputTokens int `json:"cache_creation_input_tokens,omitempty"`
	CacheReadInputTokens     int `json:"cache_read_input_tokens,omitempty"`
}

type ConvertedRequest

type ConvertedRequest struct {
	UpstreamBody []byte
	Model        string
	IsStreaming  bool
}

ConvertedRequest holds the result of request conversion.

type Converter

type Converter struct{}

func NewConverter

func NewConverter() *Converter

func (*Converter) AnthropicResponseToChat

func (c *Converter) AnthropicResponseToChat(resp *AnthropicResponse) (*types.ChatResponse, error)

AnthropicResponseToChat converts an Anthropic Messages response to a Chat Completions response.

func (*Converter) AnthropicResponseToResponses added in v0.1.5

func (c *Converter) AnthropicResponseToResponses(resp *AnthropicResponse, model, thinkTag string) (*types.ResponsesResponse, error)

AnthropicResponseToResponses converts an Anthropic Messages response directly to a Responses API response.

func (*Converter) AnthropicToChat

func (c *Converter) AnthropicToChat(req *AnthropicRequest) (*types.ChatRequest, error)

AnthropicToChat converts an Anthropic Messages request to a Chat Completions request.

func (*Converter) AnthropicToGeminiRequest added in v0.1.11

func (c *Converter) AnthropicToGeminiRequest(req *AnthropicRequest) (*GeminiRequest, error)

AnthropicToGeminiRequest converts an Anthropic request to a Gemini request.

func (*Converter) AnthropicToResponses added in v0.1.5

func (c *Converter) AnthropicToResponses(req *AnthropicRequest) (*types.ResponsesRequest, error)

AnthropicToResponses converts an Anthropic Messages request directly to a Responses API request.

func (*Converter) ChatRequestToAnthropic

func (c *Converter) ChatRequestToAnthropic(req *types.ChatRequest) (*AnthropicRequest, error)

ChatRequestToAnthropic converts a Chat Completions request to an Anthropic Messages request.

func (*Converter) ChatToAnthropic

func (c *Converter) ChatToAnthropic(chatResp *types.ChatResponse, model, thinkTag string) (*AnthropicResponse, error)

ChatToAnthropic converts a Chat Completions response to an Anthropic Messages response.

func (*Converter) ChatToGeminiRequest added in v0.1.11

func (c *Converter) ChatToGeminiRequest(req *types.ChatRequest) (*GeminiRequest, error)

ChatToGeminiRequest converts a Chat Completions request to a Gemini generateContent request.

func (*Converter) ChatToResponses

func (c *Converter) ChatToResponses(chatResp *types.ChatResponse, model, thinkTag string) (*types.ResponsesResponse, error)

ChatToResponses converts Chat Completions API response back to OpenAI Responses API format

func (*Converter) GeminiResponseToAnthropic added in v0.1.11

func (c *Converter) GeminiResponseToAnthropic(gemResp *GeminiResponse, model, thinkTag string) (*AnthropicResponse, error)

GeminiResponseToAnthropic converts a Gemini response to an Anthropic response.

func (*Converter) GeminiResponseToChat added in v0.1.11

func (c *Converter) GeminiResponseToChat(gemResp *GeminiResponse, model string) (*types.ChatResponse, error)

GeminiResponseToChat converts a Gemini response to a Chat Completions response.

func (*Converter) GeminiResponseToResponses added in v0.1.11

func (c *Converter) GeminiResponseToResponses(gemResp *GeminiResponse, model, thinkTag string) (*types.ResponsesResponse, error)

GeminiResponseToResponses converts a Gemini response to a Responses API response.

func (*Converter) ResponsesToAnthropic added in v0.1.5

func (c *Converter) ResponsesToAnthropic(req *types.ResponsesRequest) (*AnthropicRequest, error)

ResponsesToAnthropic converts a Responses API request directly to an Anthropic Messages request.

func (*Converter) ResponsesToChat

func (c *Converter) ResponsesToChat(req *types.ResponsesRequest) (*types.ChatRequest, error)

ResponsesToChat converts OpenAI Responses API request to Chat Completions API request

func (*Converter) ResponsesToGeminiRequest added in v0.1.11

func (c *Converter) ResponsesToGeminiRequest(req *types.ResponsesRequest) (*GeminiRequest, error)

ResponsesToGeminiRequest converts a Responses API request to a Gemini request.

type GeminiCandidate added in v0.1.11

type GeminiCandidate struct {
	Content      *GeminiContent `json:"content,omitempty"`
	FinishReason string         `json:"finishReason,omitempty"`
}

type GeminiContent added in v0.1.11

type GeminiContent struct {
	Role  string       `json:"role,omitempty"`
	Parts []GeminiPart `json:"parts"`
}

type GeminiFuncCall added in v0.1.11

type GeminiFuncCall struct {
	Name string         `json:"name"`
	Args map[string]any `json:"args,omitempty"`
}

type GeminiFuncCallingConfig added in v0.1.11

type GeminiFuncCallingConfig struct {
	Mode string `json:"mode,omitempty"`
}

type GeminiFuncDecl added in v0.1.11

type GeminiFuncDecl struct {
	Name        string         `json:"name"`
	Description string         `json:"description,omitempty"`
	Parameters  map[string]any `json:"parameters,omitempty"`
}

type GeminiFuncResp added in v0.1.11

type GeminiFuncResp struct {
	Name     string         `json:"name"`
	Response map[string]any `json:"response,omitempty"`
}

type GeminiGenerationConfig added in v0.1.11

type GeminiGenerationConfig struct {
	MaxOutputTokens int     `json:"maxOutputTokens,omitempty"`
	Temperature     float64 `json:"temperature,omitempty"`
	TopP            float64 `json:"topP,omitempty"`
}

type GeminiPart added in v0.1.11

type GeminiPart struct {
	Text         string          `json:"text,omitempty"`
	FunctionCall *GeminiFuncCall `json:"functionCall,omitempty"`
	FunctionResp *GeminiFuncResp `json:"functionResponse,omitempty"`
}

type GeminiRequest added in v0.1.11

type GeminiRequest struct {
	Contents          []GeminiContent         `json:"contents,omitempty"`
	SystemInstruction *GeminiContent          `json:"system_instruction,omitempty"`
	Tools             []GeminiToolDeclaration `json:"tools,omitempty"`
	ToolConfig        *GeminiToolConfig       `json:"tool_config,omitempty"`
	GenerationConfig  *GeminiGenerationConfig `json:"generationConfig,omitempty"`
}

type GeminiResponse added in v0.1.11

type GeminiResponse struct {
	Candidates    []GeminiCandidate `json:"candidates,omitempty"`
	UsageMetadata *GeminiUsageMeta  `json:"usageMetadata,omitempty"`
	ModelVersion  string            `json:"modelVersion,omitempty"`
}

type GeminiStreamState added in v0.1.11

type GeminiStreamState struct {
	Model        string
	InputTokens  int
	OutputTokens int
	Started      bool
	Done         bool
	// Tool call accumulation for Gemini → Chat: index → accumulated args
	ToolCallArgs map[int]*strings.Builder
	ToolCallIDs  map[int]string
	ToolCallSeq  int
	AccText      string
}

GeminiStreamState tracks state when converting between Gemini SSE and other SSE formats.

type GeminiToAnthropicState added in v0.1.11

type GeminiToAnthropicState struct {
	MessageID         string
	Model             string
	BlockIndex        int
	ContentSent       bool
	InputTokens       int
	OutputTokens      int
	CacheCreateTokens int
	CacheReadTokens   int
	ThinkTag          string
	TagState          ThinkTagState
	HasToolUse        bool
	AccText           string
}

GeminiToAnthropicState tracks state when converting Gemini SSE to Anthropic SSE.

type GeminiToChatState added in v0.1.11

type GeminiToChatState struct {
	ID           string
	Model        string
	Created      int64
	Started      bool
	InputTokens  int
	OutputTokens int
	AccText      string
	HasToolCalls bool
	ToolCallSeq  int
	// contains filtered or unexported fields
}

GeminiToChatState tracks state when converting Gemini SSE to Chat SSE.

func (*GeminiToChatState) ChatStreamUsage added in v0.1.11

func (s *GeminiToChatState) ChatStreamUsage() (id, model string, input, output int)

ChatStreamUsage returns token usage for the final Chat SSE usage chunk.

type GeminiToResponsesState added in v0.1.11

type GeminiToResponsesState struct {
	ResponseID    string
	ItemID        string
	Model         string
	Created       int64
	SeqNum        int
	CreatedSent   bool
	ItemSent      bool
	InputTokens   int
	OutputTokens  int
	AccText       string
	ThinkTag      string
	TagState      ThinkTagState
	TextOutputIdx int
	FuncOutputIdx int
}

GeminiToResponsesState tracks state when converting Gemini SSE to Responses SSE.

type GeminiToolConfig added in v0.1.11

type GeminiToolConfig struct {
	FunctionCallingConfig *GeminiFuncCallingConfig `json:"function_calling_config,omitempty"`
}

type GeminiToolDeclaration added in v0.1.11

type GeminiToolDeclaration struct {
	FunctionDeclarations []GeminiFuncDecl `json:"functionDeclarations,omitempty"`
}

type GeminiUsageMeta added in v0.1.11

type GeminiUsageMeta struct {
	PromptTokenCount     int `json:"promptTokenCount,omitempty"`
	CandidatesTokenCount int `json:"candidatesTokenCount,omitempty"`
	TotalTokenCount      int `json:"totalTokenCount,omitempty"`
}

type ResponsesStreamState

type ResponsesStreamState struct {
	ResponseID        string
	Created           int64
	OutputIndex       int
	ContentIndex      int
	ItemID            string
	CreatedSent       bool
	AccText           string
	SeqNum            int
	Model             string
	InputTokens       int
	OutputTokens      int
	CacheCreateTokens int
	CacheReadTokens   int
	ThinkTag          string
	TagState          ThinkTagState

	// Tool call tracking for Chat→Responses streaming
	ToolCalls     map[int]*chatToolCallState
	TextDoneSent  bool
	TextItemID    string
	FuncOutputIdx int
}

ResponsesStreamState tracks state across SSE chunks for Responses API conversion.

type ResponsesToAnthropicState added in v0.1.4

type ResponsesToAnthropicState struct {
	MessageID         string
	Model             string
	InputTokens       int
	OutputTokens      int
	CacheCreateTokens int
	CacheReadTokens   int
	AccText           string
	ContentSent       bool
	MessageStarted    bool
	CurrentBlockIdx   int
	HasToolUse        bool
}

ResponsesToAnthropicState tracks state when converting Responses SSE to Anthropic SSE.

type ResponsesToChatState

type ResponsesToChatState struct {
	ID                string
	Model             string
	Created           int64
	AccText           string
	Started           bool
	InputTokens       int
	OutputTokens      int
	CacheCreateTokens int
	CacheReadTokens   int
	HasToolCalls      bool

	// Tool call tracking: item_id -> call info
	ToolCallItems map[string]*responsesToChatTC
	ToolCallSeq   int
}

ResponsesToChatState tracks state when converting Responses SSE to Chat SSE.

func (*ResponsesToChatState) ChatStreamUsage added in v0.1.4

func (s *ResponsesToChatState) ChatStreamUsage() (id, model string, input, output int)

ChatStreamUsage returns token usage for the final Chat SSE usage chunk.

type SSEWriter

type SSEWriter interface {
	WriteEvent(eventType string, data any)
}

SSEWriter abstracts SSE event output, decoupling converters from HTTP frameworks.

type ThinkTagState

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

ThinkTagState tracks whether we are inside a think tag across stream chunks.

func (*ThinkTagState) FilterChunk

func (s *ThinkTagState) FilterChunk(chunk, tag string) string

FilterChunk strips think-tag content from a streaming chunk. Tags are assumed to be complete within a chunk boundary (common case).

Jump to

Keyboard shortcuts

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