Skip to main content

causa_protocol/
lib.rs

1//! Wire-protocol translation for LLM providers.
2//!
3//! `causa-protocol` renders `ContextFrame` → provider wire bodies and
4//! parses wire responses → kernel `ModelOutput`.
5//! `translation::context_frame` is the shared policy walk; the three
6//! renderers (`translation::anthropic`, `openai_chat`,
7//! `openai_responses`) are thin emitters over it. The crate serves the
8//! `causa_kernel::ModelGateway` seam and is consumed by `causa-provider`'s
9//! gateways. It is transport-free.
10//!
11//! [`translation::streaming::StreamDecoder`] incrementally decodes raw SSE bytes
12//! into kernel observations and a complete terminal output. [`SseParser`] is
13//! the lower-level bounded byte framer. Neither owns HTTP, execution control,
14//! retries, event history or a background task.
15//!
16//! Layering:
17//!
18//! ```text
19//! causa-kernel (facts, ModelGateway seam)
20//!   <- causa-protocol (render/parse)   <- causa-provider
21//! ```
22//!
23//! `causa-protocol` must not depend on reqwest, Axum, or any concrete
24//! provider SDK.
25//!
26//! # Protocol options and wire rendering
27//!
28//! - **Generation options**: OpenAI Chat renders `max_tokens` as
29//!   `max_completion_tokens` (or `max_tokens`, by its render option),
30//!   Responses renders it as `max_output_tokens`, and Anthropic renders it
31//!   as `max_tokens`. `None` omits the limit on the
32//!   two OpenAI wires; Anthropic requires the field, so that renderer rejects
33//!   a missing value before any HTTP.
34//! - **Reasoning effort**: a string, written as the host gave it to OpenAI
35//!   Chat `reasoning_effort`, Responses `reasoning.effort` and Anthropic
36//!   `output_config.effort`. No value is refused, mapped or combined with
37//!   another field; which values a model accepts is the host's knowledge.
38//! - **Extra request fields**:
39//!   [`causa_kernel::GenerationOptions::extra_body`] is merged into the
40//!   body. Objects merge key by key with what the renderer wrote, so
41//!   `reasoning.summary` joins `reasoning.effort`; any other value the
42//!   renderer wrote is kept, and arrays are never merged. The rule goes by
43//!   key name, and `output_schema` is kept whole: a key beside the schema
44//!   is merged, a key inside it is not. An Anthropic `thinking` object is
45//!   sent this way.
46//! - **Structured outputs**: `output_schema` renders as `response_format` on
47//!   Chat and `text.format` on Responses; the Anthropic adapter rejects it.
48//! - **Prompt cache directives**: [`causa_kernel::CacheDirective::StablePrefix`]
49//!   renders explicit `cache_control` breakpoints on Anthropic; OpenAI-family
50//!   providers cache automatically server-side, treating the directive as a no-op.
51//! - **Stop values**: Anthropic maps wire `stop_reason`, Chat maps `finish_reason`,
52//!   and Responses maps `status` (or `status.reason` when status is `incomplete`
53//!   and carries a reason).
54
55#![deny(unsafe_code)]
56#![deny(missing_docs)]
57
58pub mod protocol;
59pub mod translation;
60
61pub use protocol::Protocol;
62pub use translation::sse_parser::{SseEvent, SseParser};