converter

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

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

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

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

FormatSSEEvent formats an SSE event string.

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"`
	Text string `json:"text,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"`
}

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
	ThinkTag     string
	TagState     ThinkTagState
}

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
	Started      bool
}

AnthropicToChatState tracks state when converting Anthropic SSE to Chat SSE.

type AnthropicToResponsesState

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

AnthropicToResponsesState tracks state when converting Anthropic SSE to Responses SSE.

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 {
	UpstreamPath string
	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) AnthropicToChat

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

AnthropicToChat converts an Anthropic Messages request to a Chat Completions 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) ResponsesToChat

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

ResponsesToChat converts OpenAI Responses API request to Chat Completions API request

type ResponsesStreamState

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

StreamState tracks state across SSE chunks for Responses API conversion.

type ResponsesToChatState

type ResponsesToChatState struct {
	ID      string
	Model   string
	Created int64
	AccText string
	Started bool
}

ResponsesToChatState tracks state when converting Responses SSE to Chat SSE.

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