openaicompat

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: Apache-2.0 Imports: 39 Imported by: 0

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

View Source
const OpenAIChatProfileID = "openai_chat_v1"

OpenAIChatProfileID is the certified profile identifier for OpenAI Chat (Requirements 4, 8, 17).

View Source
const OpenAIResponsesProfileID = "openai_responses_v1"

OpenAIResponsesProfileID is the certified profile identifier for OpenAI Responses (Requirement 4, 8).

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

func BuildOutboundHeaders(apiSecret string, streaming bool, extraHeaders http.Header) http.Header

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

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

func ResolveEndpoint(baseURL string, flavor Flavor) (string, error)

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

func ResolveHTTPClient(client *http.Client) *http.Client

ResolveHTTPClient returns client when non-nil; otherwise returns httpclient.Standard ensuring default TLS, proxy, HTTP/2, and timeout tuning (Requirement 12.1).

func ResponseEvents

func ResponseEvents(resp responses.Response) ([]lipapi.Event, error)

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 Flavor

type Flavor string
const (
	FlavorChat      Flavor = "chat"
	FlavorResponses Flavor = "responses"
)

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) Error

func (e *HTTPError) Error() string

func (*HTTPError) HTTPHeader

func (e *HTTPError) HTTPHeader() http.Header

func (*HTTPError) HTTPStatusCode

func (e *HTTPError) HTTPStatusCode() int

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

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

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.

Jump to

Keyboard shortcuts

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