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`, Responses renders it as `max_output_tokens`,
30//! and Anthropic renders it as `max_tokens`. `None` omits the limit on the
31//! two OpenAI wires; Anthropic requires the field, so that renderer rejects
32//! a missing value before any HTTP.
33//! - **Reasoning effort**: a string, written as the host gave it to OpenAI
34//! Chat `reasoning_effort`, Responses `reasoning.effort` and Anthropic
35//! `output_config.effort`. No value is refused, mapped or combined with
36//! another field; which values a model accepts is the host's knowledge.
37//! - **Extra request fields**:
38//! [`causa_kernel::GenerationOptions::extra_body`] is merged into the
39//! body. Objects merge key by key with what the renderer wrote, so
40//! `reasoning.summary` joins `reasoning.effort`; any other value the
41//! renderer wrote is kept, and arrays are never merged. The rule goes by
42//! key name, and `output_schema` is kept whole: a key beside the schema
43//! is merged, a key inside it is not. An Anthropic `thinking` object is
44//! sent this way.
45//! - **Structured outputs**: `output_schema` renders as `response_format` on
46//! Chat and `text.format` on Responses; the Anthropic adapter rejects it.
47//! - **Prompt cache directives**: [`causa_kernel::CacheDirective::StablePrefix`]
48//! renders explicit `cache_control` breakpoints on Anthropic; OpenAI-family
49//! providers cache automatically server-side, treating the directive as a no-op.
50//! - **Stop values**: Anthropic maps wire `stop_reason`, Chat maps `finish_reason`,
51//! and Responses maps `status` (or `status.reason` when status is `incomplete`
52//! and carries a reason).
53
54#![deny(unsafe_code)]
55#![deny(missing_docs)]
56
57pub mod protocol;
58pub mod translation;
59
60pub use protocol::Protocol;
61pub use translation::sse_parser::{SseEvent, SseParser};