Documentation
¶
Overview ¶
Package openaicompat contains shared adapter-layer helpers for backend plugins that talk to OpenAI-compatible APIs through the openai-go SDK.
This package is intentionally under internal/plugins/backends: SDK and wire concerns belong at the backend adapter edge, not in core contracts or orchestration packages.
Index ¶
- Constants
- func BuildCompatible(instanceID, factoryKind string, n yaml.Node, upstream *http.Client, ...) (execbackend.Backend, error)
- func BuildCompatibleWithHeaders(instanceID, factoryKind string, n yaml.Node, upstream *http.Client, ...) (execbackend.Backend, error)
- func BuildOutboundHeaders(apiSecret string, streaming bool, extraHeaders http.Header) http.Header
- func ChatCompletionEvents(comp openai.ChatCompletion) []lipapi.Event
- func CompatibleTransportCaps(flavor Flavor) lipapi.BackendTransportCaps
- func LifecycleOpenAILegacyCompatible(instanceID string, n yaml.Node, upstream *http.Client, ...) (pluginreg.BackendBuildResult, error)
- func LifecycleOpenAIResponsesCompatible(instanceID string, n yaml.Node, upstream *http.Client, ...) (pluginreg.BackendBuildResult, error)
- func NewBackend(spec BackendSpec) execbackend.Backend
- func NewChatStream(provider string, s *ssestream.Stream[openai.ChatCompletionChunk], ...) lipapi.ManagedEventStream
- func NewOutboundRequest(ctx context.Context, targetURL string, body io.Reader, contentLength int64, ...) (*http.Request, error)
- func NewResponsesStream(provider string, s *ssestream.Stream[responses.ResponseStreamEventUnion], ...) lipapi.ManagedEventStream
- func OpenChat(ctx context.Context, cli openai.Client, req InvokeRequest) (lipapi.ManagedEventStream, error)
- func OpenResponses(ctx context.Context, cli openai.Client, req InvokeRequest) (lipapi.ManagedEventStream, error)
- func ParseStreamResponse(providerID string, resp *http.Response, flavor Flavor, maxPending int) (lipapi.ManagedEventStream, error)
- func PeekFirstEvent(ctx context.Context, es lipapi.ManagedEventStream) (lipapi.ManagedEventStream, error)
- func ReasoningTextFromChunkDelta(delta openai.ChatCompletionChunkChoiceDelta) string
- func ReasoningTextFromMessage(msg openai.ChatCompletionMessage) string
- func ResolveEndpoint(baseURL string, flavor Flavor) (string, error)
- func ResolveHTTPClient(client *http.Client) *http.Client
- func ResponseEvents(resp responses.Response) ([]lipapi.Event, error)
- type BackendSpec
- type Flavor
- type HTTPError
- type InvokeRequest
- type WireOpenPrimitives
- func (p WireOpenPrimitives) BuildHeaders(apiSecret string, streaming bool) http.Header
- func (p WireOpenPrimitives) BuildHeadersWithExtra(apiSecret string, streaming bool, extraHeaders http.Header) http.Header
- func (p WireOpenPrimitives) Client() *http.Client
- func (p WireOpenPrimitives) Execute(ctx context.Context, ...) (lipapi.ManagedEventStream, error)
- func (p WireOpenPrimitives) NewRequest(ctx context.Context, body io.Reader, contentLength int64, apiSecret string, ...) (*http.Request, error)
- func (p WireOpenPrimitives) OpenWire(ctx context.Context, req largebody.WireOpenRequest) (lipapi.ManagedEventStream, error)
- func (p WireOpenPrimitives) ParseAndPeekStream(ctx context.Context, resp *http.Response) (lipapi.ManagedEventStream, error)
- func (p WireOpenPrimitives) ResolveURL() (string, error)
Constants ¶
const OpenAIChatProfileID = "openai_chat_v1"
OpenAIChatProfileID is the certified profile identifier for OpenAI Chat (Requirements 4, 8, 17).
const OpenAIResponsesProfileID = "openai_responses_v1"
OpenAIResponsesProfileID is the certified profile identifier for OpenAI Responses (Requirement 4, 8).
const OpenResponsesProfileID = "openresponses_v1"
OpenResponsesProfileID is the certified profile identifier for OpenResponses (Requirements 4, 8, 17.2).
Variables ¶
This section is empty.
Functions ¶
func BuildCompatible ¶
func BuildCompatible( instanceID, factoryKind string, n yaml.Node, upstream *http.Client, flavor Flavor, transportCaps lipapi.BackendTransportCaps, ) (execbackend.Backend, error)
BuildCompatible constructs a built-in OpenAI-family compatible backend from strict compatible-mode YAML for one configured runtime instance.
func BuildCompatibleWithHeaders ¶
func BuildCompatibleWithHeaders( instanceID, factoryKind string, n yaml.Node, upstream *http.Client, flavor Flavor, transportCaps lipapi.BackendTransportCaps, headers map[string]string, ) (execbackend.Backend, error)
BuildCompatibleWithHeaders is the profile composition seam for the bounded static-header subset. Custom-compatible YAML remains on BuildCompatible and cannot inject arbitrary headers.
func BuildOutboundHeaders ¶
BuildOutboundHeaders constructs backend-owned outbound HTTP request headers. It sets required Content-Type, Accept, and Authorization (if secret is non-empty). Extra headers are sanitized to strip hop-by-hop framing, stale length/encoding, client auth, and frontend session/control headers (Requirement 12.2).
func ChatCompletionEvents ¶
func ChatCompletionEvents(comp openai.ChatCompletion) []lipapi.Event
ChatCompletionEvents converts a non-streaming ChatCompletion response into a canonical event slice. The caller owns the returned slice.
func CompatibleTransportCaps ¶
func CompatibleTransportCaps(flavor Flavor) lipapi.BackendTransportCaps
CompatibleTransportCaps exposes the existing family transport contract to profile composition without exposing a factory or provider registration.
func LifecycleOpenAILegacyCompatible ¶
func LifecycleOpenAILegacyCompatible(instanceID string, n yaml.Node, upstream *http.Client, _ pluginreg.BackendFactoryDeps) (pluginreg.BackendBuildResult, error)
LifecycleOpenAILegacyCompatible is the standardplugins lifecycle entrypoint.
func LifecycleOpenAIResponsesCompatible ¶
func LifecycleOpenAIResponsesCompatible(instanceID string, n yaml.Node, upstream *http.Client, _ pluginreg.BackendFactoryDeps) (pluginreg.BackendBuildResult, error)
LifecycleOpenAIResponsesCompatible is the standardplugins lifecycle entrypoint.
func NewBackend ¶
func NewBackend(spec BackendSpec) execbackend.Backend
func NewChatStream ¶
func NewChatStream(provider string, s *ssestream.Stream[openai.ChatCompletionChunk], maxPending int) lipapi.ManagedEventStream
NewChatStream maps an openai-go chat-completions stream into canonical events.
func NewOutboundRequest ¶
func NewOutboundRequest( ctx context.Context, targetURL string, body io.Reader, contentLength int64, apiSecret string, streaming bool, extraHeaders http.Header, ) (*http.Request, error)
NewOutboundRequest constructs a secured outbound HTTP request targeting targetURL for wire execution (Requirement 12). It applies BuildOutboundHeaders, sets exact ContentLength when known (or -1 for streaming/chunked framing), clears any request trailers, keeps connection reuse enabled (Close=false), and clears req.Host so transport routes cleanly to targetURL without forwarding client Host.
func NewResponsesStream ¶
func NewResponsesStream(provider string, s *ssestream.Stream[responses.ResponseStreamEventUnion], maxPending int) lipapi.ManagedEventStream
func OpenChat ¶
func OpenChat(ctx context.Context, cli openai.Client, req InvokeRequest) (lipapi.ManagedEventStream, error)
func OpenResponses ¶
func OpenResponses(ctx context.Context, cli openai.Client, req InvokeRequest) (lipapi.ManagedEventStream, error)
func ParseStreamResponse ¶
func ParseStreamResponse(providerID string, resp *http.Response, flavor Flavor, maxPending int) (lipapi.ManagedEventStream, error)
ParseStreamResponse parses an HTTP response into a managed event stream. On status >= 400, it reads a bounded error body and returns an *HTTPError.
func PeekFirstEvent ¶
func PeekFirstEvent(ctx context.Context, es lipapi.ManagedEventStream) (lipapi.ManagedEventStream, error)
PeekFirstEvent peeks the first event from a managed stream and returns a prepended stream. If the first Recv fails, the stream is closed and the error returned.
func ReasoningTextFromChunkDelta ¶
func ReasoningTextFromChunkDelta(delta openai.ChatCompletionChunkChoiceDelta) string
ReasoningTextFromChunkDelta extracts reasoning text from a streaming chunk delta's "reasoning", "reasoning_content", or "reasoning_summary" extra fields.
func ReasoningTextFromMessage ¶
func ReasoningTextFromMessage(msg openai.ChatCompletionMessage) string
ReasoningTextFromMessage extracts reasoning text from the "reasoning", "reasoning_content", or "reasoning_summary" extra fields of a ChatCompletionMessage.
func ResolveEndpoint ¶
ResolveEndpoint maps a base URL and flavor to an absolute provider target URL. It validates the base URL with endpoint.ParseBaseURL and applies the canonical suffix via endpoint.Descriptor.Join.
func ResolveHTTPClient ¶
ResolveHTTPClient returns client when non-nil; otherwise returns httpclient.Standard ensuring default TLS, proxy, HTTP/2, and timeout tuning (Requirement 12.1).
Types ¶
type BackendSpec ¶
type BackendSpec struct {
ID string
BaseURL string
APIKey string
APIKeys []string
Credentials []credpool.Credential
HTTPClient *http.Client
SDKMaxRetries *int
RateLimitFallback time.Duration
// CompatibleModeAuth enables optional credentials for built-in compatible
// modes: an empty resolved key set proceeds without Authorization headers.
// Native hosted backends must leave this false (dummy-credential policy).
CompatibleModeAuth bool
ClientOptions func(lipapi.Call, routing.AttemptCandidate) []option.RequestOption
RequestOptions func(lipapi.Call) []option.RequestOption
ResolveModel func(routing.AttemptCandidate, lipapi.Call) string
ResolveFlavor func(lipapi.Call) Flavor
Flavor Flavor
WireDomainAnyAcceptedModel *bool
Inventory modelinventory.Provider
}
type HTTPError ¶
type HTTPError struct {
StatusCode int
Status string
Header http.Header
Body []byte
ProviderID string
}
HTTPError captures an upstream HTTP response failure (>= 400). It implements openaicred.HTTPStatusError so error classification works uniformly.
func (*HTTPError) HTTPHeader ¶
func (*HTTPError) HTTPStatusCode ¶
type InvokeRequest ¶
type InvokeRequest struct {
ProviderID string
Call lipapi.Call
Candidate routing.AttemptCandidate
SDKOptions []option.RequestOption
}
type WireOpenPrimitives ¶
type WireOpenPrimitives struct {
ProviderID string
BaseURL string
Flavor Flavor
Pool *credpool.Pool
HTTPClient *http.Client
RateLimitFallback time.Duration
MaxPending int
}
WireOpenPrimitives bundles backend-owned transport configuration and helpers for executing wire requests without duplicating credential, endpoint, client, or response stream parsing logic (Requirements 10, 12).
func (WireOpenPrimitives) BuildHeaders ¶
func (p WireOpenPrimitives) BuildHeaders(apiSecret string, streaming bool) http.Header
BuildHeaders constructs the outbound headers for an attempt.
func (WireOpenPrimitives) BuildHeadersWithExtra ¶
func (p WireOpenPrimitives) BuildHeadersWithExtra(apiSecret string, streaming bool, extraHeaders http.Header) http.Header
BuildHeadersWithExtra constructs the outbound headers including sanitized extra headers.
func (WireOpenPrimitives) Client ¶
func (p WireOpenPrimitives) Client() *http.Client
Client returns the effective HTTP client.
func (WireOpenPrimitives) Execute ¶
func (p WireOpenPrimitives) Execute( ctx context.Context, openFn func(ctx context.Context, cred credpool.Credential) (lipapi.ManagedEventStream, error), ) (lipapi.ManagedEventStream, error)
Execute executes openFn within the credential-rotation loop.
func (WireOpenPrimitives) NewRequest ¶
func (p WireOpenPrimitives) NewRequest( ctx context.Context, body io.Reader, contentLength int64, apiSecret string, streaming bool, extraHeaders http.Header, ) (*http.Request, error)
NewRequest constructs a secured outbound HTTP request using the resolved provider endpoint.
func (WireOpenPrimitives) OpenWire ¶
func (p WireOpenPrimitives) OpenWire(ctx context.Context, req largebody.WireOpenRequest) (lipapi.ManagedEventStream, error)
OpenWire opens a backend attempt directly from a wire request (Requirement 8, 12).
func (WireOpenPrimitives) ParseAndPeekStream ¶
func (p WireOpenPrimitives) ParseAndPeekStream(ctx context.Context, resp *http.Response) (lipapi.ManagedEventStream, error)
ParseAndPeekStream parses an HTTP response stream and peeks the first event.
func (WireOpenPrimitives) ResolveURL ¶
func (p WireOpenPrimitives) ResolveURL() (string, error)
ResolveURL resolves the target endpoint URL.