protocol

package
v0.260224.1130 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MPL-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const ChatGPTBackendAPIBase = "https://chatgpt.com/backend-api"

ChatGPTBackendAPIBase is the API base URL for ChatGPT/Codex OAuth provider

Variables

This section is empty.

Functions

func AddTypeFieldToInputItems added in v0.260224.0

func AddTypeFieldToInputItems(data []byte) ([]byte, error)

AddTypeFieldToInputItems preprocesses the JSON to add "type": "message" to input items that don't have a type field. This is necessary because the OpenAI SDK's union deserializer requires the type field to correctly match variants.

func GetInputValue added in v0.260224.0

func GetInputValue(input responses.ResponseNewParamsInputUnion) any

GetInputValue extracts the raw input value from ResponseNewParamsInputUnion. Returns the underlying string, array, or nil.

func IsContextCanceled added in v0.260224.0

func IsContextCanceled(err error) bool

IsContextCanceled checks if the error is due to context cancellation.

Types

type APIStyle

type APIStyle string

APIStyle represents the API style/version for a provider

const (
	APIStyleOpenAI    APIStyle = "openai"
	APIStyleAnthropic APIStyle = "anthropic"
	APIStyleGoogle    APIStyle = "google"
)

type AnthropicBetaMessagesRequest

type AnthropicBetaMessagesRequest struct {
	Stream bool `json:"stream"`
	anthropic.BetaMessageNewParams
}

Use official Anthropic SDK types directly

func (*AnthropicBetaMessagesRequest) UnmarshalJSON

func (r *AnthropicBetaMessagesRequest) UnmarshalJSON(data []byte) error

type AnthropicMessagesRequest

type AnthropicMessagesRequest struct {
	Stream bool `json:"stream"`
	anthropic.MessageNewParams
}

Request types

func (*AnthropicMessagesRequest) UnmarshalJSON

func (r *AnthropicMessagesRequest) UnmarshalJSON(data []byte) error

type BetaRound

type BetaRound struct {
	Messages       []anthropic.BetaMessageParam
	IsCurrentRound bool
	Stats          *RoundStats // Optional metadata about the round structure
}

BetaRound represents a conversation round for v1beta API.

type Client

type Client interface {
	// APIStyle returns the type of provider this client implements
	APIStyle() APIStyle

	// Close closes any resources held by the client
	Close() error
}

Client is the unified interface for AI provider clients

type ErrorDetail added in v0.260224.0

type ErrorDetail struct {
	Message string `json:"message"`
	Type    string `json:"type"`
	Code    string `json:"code,omitempty"`
}

ErrorDetail represents error details

type ErrorResponse added in v0.260224.0

type ErrorResponse struct {
	Error ErrorDetail `json:"error"`
}

ErrorResponse represents an error response

type Grouper

type Grouper struct{}

Grouper provides methods to group messages into conversation rounds.

func NewGrouper

func NewGrouper() *Grouper

NewGrouper creates a new Grouper instance.

func (*Grouper) GroupBeta

func (g *Grouper) GroupBeta(messages []anthropic.BetaMessageParam) []BetaRound

GroupBeta groups beta messages into conversation rounds.

func (*Grouper) GroupV1

func (g *Grouper) GroupV1(messages []anthropic.MessageParam) []V1Round

GroupV1 groups v1 messages into conversation rounds. A round starts with a pure user message and includes all subsequent messages (assistant with tool use, tool results) until the next pure user message (exclusive).

func (*Grouper) IsPureBetaUserMessage

func (g *Grouper) IsPureBetaUserMessage(msg anthropic.BetaMessageParam) bool

IsPureBetaUserMessage checks if a beta message is a pure user instruction.

func (*Grouper) IsPureUserMessage

func (g *Grouper) IsPureUserMessage(msg anthropic.MessageParam) bool

IsPureUserMessage checks if a v1 message is a pure user instruction (not a tool result).

type HandleContext added in v0.260224.0

type HandleContext struct {
	// Gin context
	GinContext *gin.Context

	// Model info
	ResponseModel string

	// Hooks for stream processing (chainable - multiple hooks can be added)
	OnStreamEventHooks    []func(event interface{}) error
	OnStreamCompleteHooks []func()
	OnStreamErrorHooks    []func(err error)
}

HandleContext provides dependencies for handle functions. It uses the builder pattern for optional configuration and hooks.

func NewHandleContext added in v0.260224.0

func NewHandleContext(c *gin.Context, responseModel string) *HandleContext

NewHandleContext creates a new HandleContext with required dependencies.

func (*HandleContext) CallOnStreamComplete added in v0.260224.0

func (hc *HandleContext) CallOnStreamComplete()

CallOnStreamComplete calls all OnStreamComplete hooks. This is useful for non-streaming handlers that still need to invoke complete hooks.

func (*HandleContext) ProcessStream added in v0.260224.0

func (hc *HandleContext) ProcessStream(nextFunc func() (bool, error, interface{}), handleFunc func(interface{}) error) error

ProcessStream provides a generic framework for processing streaming responses. It handles context cancellation, error checking, and event processing.

nextFunc should return (true, nil, event) to continue, (false, nil, nil) to stop, or (false, err, nil) on error. handleFunc is called for each event after OnStreamEventHooks are invoked. It can be used to send the event to the client.

func (*HandleContext) SendError added in v0.260224.0

func (hc *HandleContext) SendError(err error, errorType, code string)

SendError sends an error response to the client.

func (*HandleContext) SetupSSEHeaders added in v0.260224.0

func (hc *HandleContext) SetupSSEHeaders()

SetupSSEHeaders sets the standard SSE (Server-Sent Events) headers.

func (*HandleContext) WithOnStreamComplete added in v0.260224.0

func (hc *HandleContext) WithOnStreamComplete(hook func()) *HandleContext

WithOnStreamComplete adds a hook that is called when stream completes successfully. Multiple hooks can be added and will be called in order.

func (*HandleContext) WithOnStreamError added in v0.260224.0

func (hc *HandleContext) WithOnStreamError(hook func(error)) *HandleContext

WithOnStreamError adds a hook that is called when stream encounters an error. Multiple hooks can be added and will be called in order.

func (*HandleContext) WithOnStreamEvent added in v0.260224.0

func (hc *HandleContext) WithOnStreamEvent(hook func(interface{}) error) *HandleContext

WithOnStreamEvent adds a hook that is called for each stream event. Multiple hooks can be added and will be called in order.

type OpenAIChatCompletionRequest

type OpenAIChatCompletionRequest struct {
	openai.ChatCompletionNewParams
	Stream bool `json:"stream"`
}

OpenAIChatCompletionRequest is a type alias for OpenAI chat completion request with extra fields.

func (*OpenAIChatCompletionRequest) UnmarshalJSON

func (r *OpenAIChatCompletionRequest) UnmarshalJSON(data []byte) error

type Response added in v0.260224.0

type Response = responses.Response

Response is an alias to the native OpenAI SDK type

type ResponseCreateRequest added in v0.260224.0

type ResponseCreateRequest struct {
	// Stream indicates whether to stream the response
	// This is not part of ResponseNewParams as streaming is controlled
	// by using NewStreaming() method on the SDK client
	Stream bool `json:"stream"`

	// Embed the native SDK type for all other fields
	responses.ResponseNewParams
}

ResponseCreateRequest wraps the native ResponseNewParams with additional fields for proxy-specific handling like the `stream` parameter.

func (*ResponseCreateRequest) UnmarshalJSON added in v0.260224.0

func (r *ResponseCreateRequest) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling for ResponseCreateRequest It handles both the custom Stream field and the embedded ResponseNewParams

type ResponseInputItemUnionParam added in v0.260224.0

type ResponseInputItemUnionParam = responses.ResponseInputItemUnionParam

ResponseInputItemUnionParam is an alias to the native OpenAI SDK type

type ResponseNewParams added in v0.260224.0

type ResponseNewParams = responses.ResponseNewParams

ResponseNewParams is an alias to the native OpenAI SDK type

type ResponseNewParamsInputUnion added in v0.260224.0

type ResponseNewParamsInputUnion = responses.ResponseNewParamsInputUnion

ResponseNewParamsInputUnion is an alias to the native OpenAI SDK type

type RoundStats

type RoundStats struct {
	UserMessageCount int  // Number of pure user messages in this round (should be 1)
	AssistantCount   int  // Number of assistant messages
	ToolResultCount  int  // Number of tool result messages
	TotalMessages    int  // Total messages in the round
	HasThinking      bool // Whether any assistant message contains thinking blocks
}

RoundStats contains metadata about a round's message composition.

type Transformer

type Transformer interface {
	// HandleV1 handles compacting for Anthropic v1 requests.
	HandleV1(req *anthropic.MessageNewParams) error

	// HandleV1Beta handles compacting for Anthropic v1beta requests.
	HandleV1Beta(req *anthropic.BetaMessageNewParams) error
}

Transformer defines the interface for request compacting transformations. Each handler method is responsible for a different request model type.

type UsageStat added in v0.260224.0

type UsageStat struct {
	// InputTokens is the number of input/prompt tokens consumed
	InputTokens int

	// OutputTokens is the number of output/completion tokens consumed
	OutputTokens int
}

UsageStat represents token usage statistics returned by stream handlers. This is used to propagate usage information from protocol conversion handlers back to the server layer for tracking.

func NewUsageStat added in v0.260224.0

func NewUsageStat(inputTokens, outputTokens int) UsageStat

NewUsageStat creates a new UsageStat with the given token counts.

func ZeroUsageStat added in v0.260224.0

func ZeroUsageStat() UsageStat

ZeroUsageStat returns a UsageStat with zero values.

func (*UsageStat) HasUsage added in v0.260224.0

func (u *UsageStat) HasUsage() bool

HasUsage returns true if either input or output tokens are non-zero.

func (*UsageStat) TotalTokens added in v0.260224.0

func (u *UsageStat) TotalTokens() int

TotalTokens returns the sum of input and output tokens.

type V1Round

type V1Round struct {
	Messages       []anthropic.MessageParam
	IsCurrentRound bool
	Stats          *RoundStats // Optional metadata about the round structure
}

V1Round represents a conversation round for v1 API.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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