Skip to main content

Module openai

Module openai 

Source
Expand description

Task 10A: trace wiring for the OpenAI-compatible transports — Chat Completions (broker-proxied) and Responses/Codex (broker-proxied xAI and direct ChatGPT-backend). Mirrors the Anthropic seam in trace::anthropic and runtime/api.rs:

  • Exact bytes only. The wire digest/length always come from the one buffer serialized by the transport. Broker-backed sends pass that same buffer through the typed ProxyRequest::body_bytes handoff, which LocalBroker sends verbatim. On a remote broker the daemon re-serializes the JSON value out of process, so those records carry wire: None and TransportKind::CloudProxy — never a digest of bytes this process did not send.
  • One record per actual HTTP send/attempt, sharing a request ID with strictly increasing attempt ordinals (rule documented in trace::emit).
  • Validated identity only. Error codes are static literals or http_<status>; provider request IDs pass TraceId validation; normalized stop reasons come from a closed mapping. Raw provider text never enters a record.
  • Correctness firewall. Everything degrades to no-ops when the context is disabled or identity is unrepresentable.

Structs§

StreamAttempt
Per-attempt trace state for one OpenAI-compatible request: the optional tracer plus this attempt’s monotonic clock. Every method is a no-op when tracing is disabled; nothing here can fail the request.

Functions§

attachment_translation_losses
Metadata-only attachment rewrites. Native image/file bytes survive, but origin labels and tool-result lifts synthesize user blocks; text documents are lowered from documents to ordinary input text. Never record filenames, tool outputs, URLs, or source payloads in this report.
begin_openai_tracer
Begin tracing one OpenAI-compatible request. Returns None when tracing is disabled or any structural identity is unrepresentable — tracing can never fail the request.
broker_error_status
Extract the upstream HTTP status from a redacted broker proxy error (… provider request failed: <status> <reason>). Returns only a parsed numeric status — never any provider-authored text.
capture_request_content
Text documents lose structural provenance on the OpenAI wire. Never let that lowering bypass attachment privacy: consume the optional content arm visibly without a bundle (metadata tracing still proceeds normally).
provider_request_id_from_headers
Validate a provider-assigned request ID from response headers into a bounded TraceId. Invalid or hostile values are omitted — never copied raw into a trace record.
provider_usage
Provider-reported usage for a trace record. None inputs stay absent — zeros are only recorded when the provider actually reported them.
renamed_tool_losses
Translation-report entries for the rewrites the OpenAI adapter is known to perform today: tool names sanitized for the OpenAI wire grammar. Element IDs are the original names when they fit the bounded safe grammar; otherwise the entry stands without an ID (never raw text). The full per-element OpenAI IR report is incremental follow-up work (Task 9 allows per-provider reports to land incrementally) — but a known rename is never silently claimed lossless.
retry_class_for_status
Map an OpenAI-compatible HTTP failure status to a coarse retry class.
stop_reason_from_finish_reason
Normalize an OpenAI Chat Completions finish_reason into the trace enum. Unknown values collapse to Other — the raw string is never stored.