Documentation
¶
Overview ¶
Package openai implements the model_protocol adapter for the OpenAI protocol family (OpenAI chat/completions, DeepSeek, Groq and the OpenAI Responses API).
Index ¶
- type Adapter
- func (a *Adapter) CredentialHeader() string
- func (a *Adapter) ErrorNormalizer() utils.ErrorNormalizer
- func (a *Adapter) ErrorParser() utils.ErrorParser
- func (a *Adapter) ExtraHeaders() map[string]string
- func (a *Adapter) ExtractUsageFields(data []byte) utils.UsageFields
- func (a *Adapter) InjectAuth(outreq *bfe_http.Request, key string) error
- func (a *Adapter) IsFinalUsageEvent(ev utils.StreamEvent) bool
- func (a *Adapter) IsStreamTerminal(ev utils.StreamEvent) bool
- func (a *Adapter) Key() string
- func (a *Adapter) NativeStreamErrorEvent(message string) []byte
- func (a *Adapter) ParseError(statusCode int, body []byte, header http.Header) *utils.ProtocolError
- func (a *Adapter) ParseStreamError(ev utils.StreamEvent) *utils.ProtocolError
- func (a *Adapter) StreamErrorParser() utils.StreamErrorParser
- func (a *Adapter) StreamTerminalMarkers() [][]byte
- func (a *Adapter) WrapStreamErrorPayload(payload string) []byte
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Adapter ¶
type Adapter struct{}
Adapter is the OpenAI-family protocol adapter. It is stateless.
func (*Adapter) CredentialHeader ¶ added in v1.8.9
CredentialHeader returns the header InjectAuth writes, in canonical form.
func (*Adapter) ErrorNormalizer ¶
func (a *Adapter) ErrorNormalizer() utils.ErrorNormalizer
ErrorNormalizer returns the phase-1 default normalizer (never recognizes errors, callers keep their status-code whitelist). The client-facing parser for normalization (AIConf.NormalizeUpstreamError) lives in ErrorParser below; the two seams stay decoupled on purpose.
func (*Adapter) ErrorParser ¶ added in v1.8.9
func (a *Adapter) ErrorParser() utils.ErrorParser
ErrorParser returns the OpenAI-family error parser for client-facing normalization (AIConf.NormalizeUpstreamError).
func (*Adapter) ExtraHeaders ¶
ExtraHeaders returns nil: the OpenAI protocol needs no supplementary headers.
func (*Adapter) ExtractUsageFields ¶
func (a *Adapter) ExtractUsageFields(data []byte) utils.UsageFields
ExtractUsageFields extracts usage fields via the OpenAI main chain plus the DeepSeek and Responses API cache-read fallbacks (see utils.ParseOpenAIUsageFields).
func (*Adapter) InjectAuth ¶
InjectAuth writes the upstream credential as an Authorization: Bearer header. An empty key is a no-op.
func (*Adapter) IsFinalUsageEvent ¶
func (a *Adapter) IsFinalUsageEvent(ev utils.StreamEvent) bool
IsFinalUsageEvent carries the legacy hard-coded final-usage check: an Anthropic message_delta event, the Anthropic non-streaming top-level type "message", or a protocol-agnostic final usage chunk (an OpenAI stream_options.include_usage chunk has no top-level type). The Responses API response.completed event carries the final usage under response.usage (issue #1381). Without "message_delta" a cross-protocol stream (Bearer request detected as openai while the backend answers with an Anthropic body) would lose the final usage and be undercharged; without "message" the final usage of a non-streaming Anthropic JSON body would never be recognized (issue #1364).
func (*Adapter) IsStreamTerminal ¶
func (a *Adapter) IsStreamTerminal(ev utils.StreamEvent) bool
IsStreamTerminal carries the legacy hard-coded termination check that lived in mod_body_process.SSEEvent.GetQuotaUsage: an Anthropic-style message_stop event or the OpenAI [DONE] marker ends the stream. The Responses API terminal event response.completed (issue #1381) joins the rule; its usage arrives in the same event, so the stream is both terminated and finally billed there. The OpenAI adapter is also the registry fallback (unknown/empty auth styles), so it keeps the full combined rule to preserve the previous behavior for every request that does not identify a concrete protocol.
func (*Adapter) NativeStreamErrorEvent ¶ added in v1.9.0
NativeStreamErrorEvent builds an OpenAI-style stream error event: the data payload is the {"error":{message,type,param,code}} envelope with type "server_error" and no event: line (OpenAI chat streams are untyped).
func (*Adapter) ParseError ¶ added in v1.8.9
ParseError parses an OpenAI-style error envelope ({"error":{message,type,param,code}}) into a normalized ProtocolError. code may be a string or a nested object (some providers nest it); a nested object is ignored and the mapping falls back to type/status. Returning nil means the body is not a recognizable OpenAI error envelope and the caller applies its unrecognized-action.
func (*Adapter) ParseStreamError ¶ added in v1.8.9
func (a *Adapter) ParseStreamError(ev utils.StreamEvent) *utils.ProtocolError
ParseStreamError inspects one SSE event. OpenAI signals in-stream errors with an event whose data is an {"error":{...}} object (the OpenAI adapter is also the registry fallback, so this check doubles as the generic rule for unknown protocols). The HTTP status of a stream is 200; the mapping is driven by the envelope's code/type fields.
func (*Adapter) StreamErrorParser ¶ added in v1.8.9
func (a *Adapter) StreamErrorParser() utils.StreamErrorParser
StreamErrorParser returns the per-event SSE error parser.
func (*Adapter) StreamTerminalMarkers ¶ added in v1.9.0
StreamTerminalMarkers returns the wire-level markers that terminate an OpenAI-family stream (issue #1474 truncation detection).
func (*Adapter) WrapStreamErrorPayload ¶ added in v1.9.0
WrapStreamErrorPayload wraps an already-serialized error JSON payload (e.g. the unified AiError body) into an OpenAI stream error event.