stream

package
v0.260321.1800-preview Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: MPL-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// OpenAI extra field names that map to Anthropic content blocks
	OpenaiFieldReasoningContent = "reasoning_content"
)

Variables

This section is empty.

Functions

func BuildErrorEvent

func BuildErrorEvent(message, errorType, code string) map[string]interface{}

BuildErrorEvent builds a standard error event map

func FilterSpecialFields

func FilterSpecialFields(extras map[string]interface{}) map[string]interface{}

FilterSpecialFields removes special fields that have dedicated content blocks e.g., reasoning_content is handled as thinking block, not merged into text_delta

func GenerateObfuscationString

func GenerateObfuscationString() string

GenerateObfuscationString generates a random string similar to "KOJz1A"

func HandleAnthropicToGoogleStreamResponse

func HandleAnthropicToGoogleStreamResponse(c *gin.Context, stream *anthropicstream.Stream[anthropic.MessageStreamEventUnion], responseModel string) error

HandleAnthropicToGoogleStreamResponse processes Anthropic streaming events and converts them to Google format

func HandleAnthropicToOpenAIResponsesStream

func HandleAnthropicToOpenAIResponsesStream(
	hc *protocol.HandleContext,
	stream *anthropicstream.Stream[anthropic.MessageStreamEventUnion],
	responseModel string,
) (*protocol.TokenUsage, error)

HandleAnthropicToOpenAIResponsesStream converts Anthropic streaming events to OpenAI Responses API format.

Returns (UsageStat, error) for usage tracking and error handling.

func HandleAnthropicToOpenAIStreamResponse added in v0.260124.900

func HandleAnthropicToOpenAIStreamResponse(c *gin.Context, req *anthropic.MessageNewParams, stream *anthropicstream.Stream[anthropic.MessageStreamEventUnion], responseModel string, disableStreamUsage bool) (int, int, error)

HandleAnthropicToOpenAIStreamResponse processes Anthropic streaming events and converts them to OpenAI format Returns inputTokens, outputTokens, and error for usage tracking

func HandleAnthropicV1BetaStream

HandleAnthropicV1BetaStream handles Anthropic v1 beta streaming response. Returns (UsageStat, error)

func HandleAnthropicV1Stream

HandleAnthropicV1Stream handles Anthropic v1 streaming response. Returns (UsageStat, error)

func HandleGoogleToAnthropicBetaStreamResponse

func HandleGoogleToAnthropicBetaStreamResponse(c *gin.Context, stream iter.Seq2[*genai.GenerateContentResponse, error], responseModel string) (*protocol.TokenUsage, error)

HandleGoogleToAnthropicBetaStreamResponse processes Google streaming events and converts them to Anthropic beta format. Returns UsageStat containing token usage information for tracking.

func HandleGoogleToAnthropicStreamResponse

func HandleGoogleToAnthropicStreamResponse(c *gin.Context, stream iter.Seq2[*genai.GenerateContentResponse, error], responseModel string) (*protocol.TokenUsage, error)

HandleGoogleToAnthropicStreamResponse processes Google streaming events and converts them to Anthropic format. Returns UsageStat containing token usage information for tracking.

func HandleGoogleToOpenAIStreamResponse

func HandleGoogleToOpenAIStreamResponse(c *gin.Context, stream iter.Seq2[*genai.GenerateContentResponse, error], responseModel string) error

HandleGoogleToOpenAIStreamResponse processes Google streaming events and converts them to OpenAI format

func HandleOpenAIChatStream

HandleOpenAIChatStream handles OpenAI chat streaming response. Returns (UsageStat, error)

func HandleOpenAIChatToResponsesStream

func HandleOpenAIChatToResponsesStream(c *gin.Context, stream *openaistream.Stream[openai.ChatCompletionChunk], responseModel string) (*protocol.TokenUsage, error)

HandleOpenAIChatToResponsesStream converts OpenAI Chat Completions streaming to Responses API format. Returns UsageStat containing token usage information for tracking.

func HandleOpenAIResponsesStream

func HandleOpenAIResponsesStream(hc *protocol.HandleContext, stream *openaistream.Stream[responses.ResponseStreamEventUnion], responseModel string) (*protocol.TokenUsage, error)

HandleOpenAIResponsesStream handles OpenAI Responses API streaming response. Returns (UsageStat, error)

func HandleOpenAIResponsesStreamToAnthropic

func HandleOpenAIResponsesStreamToAnthropic(c *gin.Context, stream *openaistream.Stream[responses.ResponseStreamEventUnion], responseModel string, useV1Format bool) (*protocol.TokenUsage, error)

HandleOpenAIResponsesStreamToAnthropic handles OpenAI Responses API streaming response and transforms it to Anthropic message format. This is used for ChatGPT backend API providers when the original request was in Anthropic format. Returns (TokenUsage, error)

func HandleOpenAIToAnthropicBetaStream

func HandleOpenAIToAnthropicBetaStream(c *gin.Context, req *openai.ChatCompletionNewParams, stream *openaistream.Stream[openai.ChatCompletionChunk], responseModel string) (*protocol.TokenUsage, error)

HandleOpenAIToAnthropicBetaStream processes OpenAI streaming events and converts them to Anthropic beta format. Returns UsageStat containing token usage information for tracking.

func HandleOpenAIToAnthropicStreamResponse

func HandleOpenAIToAnthropicStreamResponse(c *gin.Context, req *openai.ChatCompletionNewParams, stream *openaistream.Stream[openai.ChatCompletionChunk], responseModel string) (*protocol.TokenUsage, error)

HandleOpenAIToAnthropicStreamResponse processes OpenAI streaming events and converts them to Anthropic format. Returns UsageStat containing token usage information for tracking.

func HandleOpenAIToGoogleStreamResponse

func HandleOpenAIToGoogleStreamResponse(c *gin.Context, stream *openaistream.Stream[openai.ChatCompletionChunk], responseModel string) error

HandleOpenAIToGoogleStreamResponse processes OpenAI streaming events and converts them to Google format This handler writes Google-format streaming responses to the gin.Context

func HandleResponsesToAnthropicBetaAssembly

func HandleResponsesToAnthropicBetaAssembly(c *gin.Context, stream *openaistream.Stream[responses.ResponseStreamEventUnion], responseModel string) (*protocol.TokenUsage, error)

func HandleResponsesToAnthropicBetaStream

func HandleResponsesToAnthropicBetaStream(c *gin.Context, stream *openaistream.Stream[responses.ResponseStreamEventUnion], responseModel string) (*protocol.TokenUsage, error)

HandleResponsesToAnthropicBetaStream processes OpenAI Responses API streaming events and converts them to Anthropic beta format. This is a thin wrapper that uses the shared core logic with beta event senders. Returns UsageStat containing token usage information for tracking.

func HandleResponsesToAnthropicV1Stream

func HandleResponsesToAnthropicV1Stream(c *gin.Context, stream *openaistream.Stream[responses.ResponseStreamEventUnion], responseModel string) (*protocol.TokenUsage, error)

HandleResponsesToAnthropicV1Stream processes OpenAI Responses API streaming events and converts them to Anthropic v1 format. This is a thin wrapper that uses the shared core logic with v1 event senders. Returns UsageStat containing token usage information for tracking.

func MarshalAndSendErrorEvent

func MarshalAndSendErrorEvent(c *gin.Context, message, errorType, code string)

MarshalAndSendErrorEvent marshals and sends an error event

func SendFinishEvent

func SendFinishEvent(c *gin.Context)

SendFinishEvent sends a message_stop event to indicate completion

func SendForwardingError

func SendForwardingError(c *gin.Context, err error)

SendForwardingError sends an error response for request forwarding failures

func SendInternalError

func SendInternalError(c *gin.Context, errMsg string)

SendInternalError sends an error response for internal errors

func SendInvalidRequestBodyError

func SendInvalidRequestBodyError(c *gin.Context, err error)

SendInvalidRequestBodyError sends an error response for invalid request body

func SendSSErrorEvent

func SendSSErrorEvent(c *gin.Context, message, errorType string)

SendSSErrorEvent sends an error event through SSE

func SendSSErrorEventJSON

func SendSSErrorEventJSON(c *gin.Context, errorJSON []byte)

SendSSErrorEventJSON sends a JSON error event through SSE

func SendStreamingError

func SendStreamingError(c *gin.Context, err error)

SendStreamingError sends an error response for streaming request failures

Types

type AnthropicStreamAssembler

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

AnthropicStreamAssembler assembles Anthropic streaming responses It is a pure assembler that doesn't depend on recording logic

func NewAnthropicStreamAssembler

func NewAnthropicStreamAssembler() *AnthropicStreamAssembler

NewAnthropicStreamAssembler creates a new assembler for Anthropic streams

func (*AnthropicStreamAssembler) Finish

func (a *AnthropicStreamAssembler) Finish(model string, inputTokens, outputTokens int) *anthropic.Message

Finish assembles the final response and returns it as anthropic.Message

func (*AnthropicStreamAssembler) RecordV1BetaEvent

RecordV1BetaEvent processes a v1 beta stream event

func (*AnthropicStreamAssembler) RecordV1Event

RecordV1Event processes a v1 stream event

func (*AnthropicStreamAssembler) SetUsage

func (a *AnthropicStreamAssembler) SetUsage(inputTokens, outputTokens int)

SetUsage sets the usage data

type StreamEventRecorder

type StreamEventRecorder interface {
	RecordRawMapEvent(eventType string, event map[string]interface{})
}

StreamEventRecorder is an interface for recording stream events during protocol conversion

Jump to

Keyboard shortcuts

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