Skip to main content

sim_codec_chat/providers/
anthropic.rs

1//! Anthropic Messages provider wire codec.
2//!
3//! The free functions project Anthropic Messages JSON to and from the
4//! canonical chat transcript maps. `AnthropicCodecLib` installs the same
5//! mapping as a runtime codec under `codec:anthropic`.
6
7mod common;
8mod decode;
9mod encode;
10mod runtime;
11
12pub use decode::{
13    decode_anthropic_request, decode_anthropic_request_with_limits, decode_anthropic_response,
14    decode_anthropic_response_with_limits, decode_anthropic_stream, decode_anthropic_stream_events,
15    decode_anthropic_stream_events_with_limits, decode_anthropic_stream_with_limits,
16};
17pub use encode::{encode_anthropic_request, encode_anthropic_response};
18pub use runtime::{AnthropicCodec, AnthropicCodecLib};
19
20use sim_kernel::{CodecId, Symbol};
21
22pub(super) const ANTHROPIC_CODEC_ID: CodecId = CodecId(0);
23
24/// Options for Anthropic Messages request JSON generation.
25#[derive(Clone, Debug, PartialEq, Eq)]
26pub struct AnthropicCodecOptions {
27    /// Model identifier to place in the generated request.
28    pub model: String,
29    /// Maximum output tokens for the request body.
30    pub max_tokens: u64,
31    /// Whether to request a streamed SSE response.
32    pub stream: bool,
33    /// Whether to include tool schemas from the request transcript.
34    pub tools: bool,
35}
36
37impl AnthropicCodecOptions {
38    /// Builds codec options from a model id, token budget, and stream/tools flags.
39    pub fn new(model: impl Into<String>, max_tokens: u64, stream: bool, tools: bool) -> Self {
40        Self {
41            model: model.into(),
42            max_tokens,
43            stream,
44            tools,
45        }
46    }
47}
48
49/// Alias for [`AnthropicCodecOptions`] when used for provider request generation.
50pub type AnthropicRequestOptions = AnthropicCodecOptions;
51
52/// Returns the codec symbol `codec:anthropic`.
53pub fn anthropic_codec_symbol() -> Symbol {
54    Symbol::qualified("codec", "anthropic")
55}