Documentation
¶
Index ¶
- Constants
- func BuildResponsesFromChat(chatReq *types.ChatRequest, stream bool) *types.ResponsesRequest
- func ConvertAnthropicLineToChat(state *AnthropicToChatState, line string) any
- func ConvertAnthropicLineToResponses(w SSEWriter, state *AnthropicToResponsesState, line string) bool
- func ConvertChatChunkToAnthropicSSE(w SSEWriter, state *AnthropicStreamState, data string) bool
- func ConvertChatChunkToResponsesSSE(w SSEWriter, state *ResponsesStreamState, data string) bool
- func ConvertResponsesEventToAnthropicSSE(w SSEWriter, state *ResponsesToAnthropicState, data string) bool
- func ConvertResponsesLineToChat(state *ResponsesToChatState, line string) any
- func EmitCompleted(w SSEWriter, state *AnthropicToResponsesState)
- func FormatSSEEvent(eventType string, data []byte) string
- func NormalizeRole(role string) string
- func ParseSSEDataLine(line string) string
- func StripThinkTag(text, tag string) string
- type AnthropicContentBlock
- type AnthropicMessage
- type AnthropicRequest
- type AnthropicResponse
- type AnthropicStreamState
- type AnthropicSystemBlock
- type AnthropicToChatState
- type AnthropicToResponsesState
- type AnthropicTool
- type AnthropicUsage
- type ConvertedRequest
- type Converter
- func (c *Converter) AnthropicResponseToChat(resp *AnthropicResponse) (*types.ChatResponse, error)
- func (c *Converter) AnthropicResponseToResponses(resp *AnthropicResponse, model, thinkTag string) (*types.ResponsesResponse, error)
- func (c *Converter) AnthropicToChat(req *AnthropicRequest) (*types.ChatRequest, error)
- func (c *Converter) AnthropicToResponses(req *AnthropicRequest) (*types.ResponsesRequest, error)
- func (c *Converter) ChatRequestToAnthropic(req *types.ChatRequest) (*AnthropicRequest, error)
- func (c *Converter) ChatToAnthropic(chatResp *types.ChatResponse, model, thinkTag string) (*AnthropicResponse, error)
- func (c *Converter) ChatToResponses(chatResp *types.ChatResponse, model, thinkTag string) (*types.ResponsesResponse, error)
- func (c *Converter) ConvertRequest(clientFormat, upstreamFormat string, body []byte, defaultModel string, ...) (*ConvertedRequest, error)
- func (c *Converter) ResponsesToAnthropic(req *types.ResponsesRequest) (*AnthropicRequest, error)
- func (c *Converter) ResponsesToChat(req *types.ResponsesRequest) (*types.ChatRequest, error)
- func (c *Converter) ResponsesToChatResponse(resp *types.ResponsesResponse) (*types.ChatResponse, error)
- type ResponsesStreamState
- type ResponsesToAnthropicState
- type ResponsesToChatState
- type SSEWriter
- type ThinkTagState
Constants ¶
const ( FormatChat = "chat" FormatResponses = "responses" FormatAnthropic = "anthropic" )
Format constants for protocol identification.
Variables ¶
This section is empty.
Functions ¶
func BuildResponsesFromChat ¶
func BuildResponsesFromChat(chatReq *types.ChatRequest, stream bool) *types.ResponsesRequest
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 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 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 FormatSSEEvent ¶
FormatSSEEvent formats an SSE event string.
func NormalizeRole ¶ added in v0.1.5
NormalizeRole maps unsupported roles to "user".
func ParseSSEDataLine ¶
ParseSSEDataLine extracts the data portion from an SSE line. Returns empty string if not a data line.
func StripThinkTag ¶
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 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
}
AnthropicStreamState tracks state across SSE chunks for Anthropic conversion.
type AnthropicSystemBlock ¶
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 AnthropicUsage ¶
type ConvertedRequest ¶
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) 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) 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) ConvertRequest ¶
func (c *Converter) ConvertRequest(clientFormat, upstreamFormat string, body []byte, defaultModel string, modelMap map[string]string) (*ConvertedRequest, error)
ConvertRequest converts a client request to upstream format. clientFormat is "responses", "anthropic", or "chat". upstreamFormat is from config. body is raw JSON.
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) ResponsesToChatResponse ¶ added in v0.1.4
func (c *Converter) ResponsesToChatResponse(resp *types.ResponsesResponse) (*types.ChatResponse, error)
ResponsesToChatResponse converts a Responses API response back to Chat Completions format.
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 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).