Module openai_compat

Module openai_compat 

Source
Expand description

The OpenAI-compatibility facade (DW-075).

The CLIENT-facing dialect: every AI route accepts an OpenAI chat-completions request and answers in the OpenAI response shape, regardless of which provider actually served the call. This module owns that dialect’s parse (parse_chat_request) and serialization (response_to_openai, stream_event_to_openai_chunk, error_body) — the inverse of crate::ai::adapters::openai, which speaks the SAME dialect but toward the provider (where the model identifier is the mapped provider model, not the alias).

Errors are emitted in an OpenAI-client-compatible shape ({"error":{"message","type","code","request_id"}}): the standard OpenAI SDKs parse error.message/type/code, and the extra request_id member carries the gateway’s correlation ID without breaking those parsers.

Functions§

error_body
Build the OpenAI-shaped error body every AI-route failure answers with. error_type follows the OpenAI taxonomy (invalid_request_error, model_error, api_error, …).
error_type_of
The OpenAI error-type token for an AiError, for pass-through error mapping.
parse_chat_request
Parse an OpenAI chat-completions request body into the canonical ChatRequest. Unknown top-level parameters are preserved in other (the OpenAI provider adapter re-emits them; other dialects drop them — see the types docs).
response_to_openai
Serialize a canonical ChatResponse into the OpenAI response shape. model_alias is echoed as the model (the provider’s internal identifier never leaks); request_id rides along for correlation.
stream_event_to_openai_chunk
Serialize one canonical stream event as an OpenAI chat.completion.chunk (DW-077 wires the gateway SSE pipeline; shape verified here and in the adapter stream tests).
unix_now
Current Unix seconds (response created fields).