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_response, decode_anthropic_stream,
14    decode_anthropic_stream_events,
15};
16pub use encode::{encode_anthropic_request, encode_anthropic_response};
17pub use runtime::{AnthropicCodec, AnthropicCodecLib};
18
19use sim_kernel::{CodecId, Symbol};
20
21pub(super) const ANTHROPIC_CODEC_ID: CodecId = CodecId(0);
22
23/// Options for Anthropic Messages request JSON generation.
24#[derive(Clone, Debug, PartialEq, Eq)]
25pub struct AnthropicCodecOptions {
26    /// Model identifier to place in the generated request.
27    pub model: String,
28    /// Maximum output tokens for the request body.
29    pub max_tokens: u64,
30    /// Whether to request a streamed SSE response.
31    pub stream: bool,
32    /// Whether to include tool schemas from the request transcript.
33    pub tools: bool,
34}
35
36impl AnthropicCodecOptions {
37    /// Builds codec options from a model id, token budget, and stream/tools flags.
38    pub fn new(model: impl Into<String>, max_tokens: u64, stream: bool, tools: bool) -> Self {
39        Self {
40            model: model.into(),
41            max_tokens,
42            stream,
43            tools,
44        }
45    }
46}
47
48/// Alias for [`AnthropicCodecOptions`] when used for provider request generation.
49pub type AnthropicRequestOptions = AnthropicCodecOptions;
50
51/// Returns the codec symbol `codec:anthropic`.
52pub fn anthropic_codec_symbol() -> Symbol {
53    Symbol::qualified("codec", "anthropic")
54}