Skip to main content

sim_codec_chat/providers/
openai.rs

1//! OpenAI-compatible provider wire codec.
2//!
3//! The free functions project OpenAI chat-completion JSON to and from the
4//! canonical chat transcript maps. `OpenAiCodecLib` installs the same mapping
5//! as a runtime codec under `codec:openai`.
6
7mod common;
8mod decode;
9mod encode;
10mod runtime;
11
12pub use decode::{
13    decode_openai_request, decode_openai_request_with_limits, decode_openai_response,
14    decode_openai_response_with_limits, decode_openai_stream, decode_openai_stream_with_limits,
15};
16pub(in crate::providers) use decode::{
17    decode_openai_request_for_codec_with_limits, decode_openai_response_for_codec_with_limits,
18    decode_openai_stream_for_codec_with_limits,
19};
20pub(in crate::providers) use encode::encode_openai_response_for_codec;
21pub use encode::{encode_openai_request, encode_openai_response};
22pub use runtime::{OpenAiCodec, OpenAiCodecLib};
23
24use sim_kernel::{CodecId, Symbol};
25
26pub(super) const OPENAI_CODEC_ID: CodecId = CodecId(0);
27
28/// Options for OpenAI-compatible provider request JSON generation.
29#[derive(Clone, Debug, PartialEq, Eq)]
30pub struct OpenAiCodecOptions {
31    /// Model identifier to place in the generated request.
32    pub model: String,
33    /// Whether to request a streamed SSE response.
34    pub stream: bool,
35    /// Whether to include the `tools` field in the generated request.
36    pub tools: bool,
37}
38
39impl OpenAiCodecOptions {
40    /// Builds codec options from a model id and the stream/tools flags.
41    pub fn new(model: impl Into<String>, stream: bool, tools: bool) -> Self {
42        Self {
43            model: model.into(),
44            stream,
45            tools,
46        }
47    }
48}
49
50/// Alias for [`OpenAiCodecOptions`] when used for provider request generation.
51pub type OpenAiRequestOptions = OpenAiCodecOptions;
52
53/// Returns the codec symbol `codec:openai`.
54pub fn openai_codec_symbol() -> Symbol {
55    Symbol::qualified("codec", "openai")
56}