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(in crate::providers) use decode::decode_openai_request_for_codec;
13pub use decode::{decode_openai_request, decode_openai_response, decode_openai_stream};
14pub(in crate::providers) use encode::encode_openai_response_for_codec;
15pub use encode::{encode_openai_request, encode_openai_response};
16pub use runtime::{OpenAiCodec, OpenAiCodecLib};
17
18use sim_kernel::{CodecId, Symbol};
19
20pub(super) const OPENAI_CODEC_ID: CodecId = CodecId(0);
21
22/// Options for OpenAI-compatible provider request JSON generation.
23#[derive(Clone, Debug, PartialEq, Eq)]
24pub struct OpenAiCodecOptions {
25    /// Model identifier to place in the generated request.
26    pub model: String,
27    /// Whether to request a streamed SSE response.
28    pub stream: bool,
29    /// Whether to include the `tools` field in the generated request.
30    pub tools: bool,
31}
32
33impl OpenAiCodecOptions {
34    /// Builds codec options from a model id and the stream/tools flags.
35    pub fn new(model: impl Into<String>, stream: bool, tools: bool) -> Self {
36        Self {
37            model: model.into(),
38            stream,
39            tools,
40        }
41    }
42}
43
44/// Alias for [`OpenAiCodecOptions`] when used for provider request generation.
45pub type OpenAiRequestOptions = OpenAiCodecOptions;
46
47/// Returns the codec symbol `codec:openai`.
48pub fn openai_codec_symbol() -> Symbol {
49    Symbol::qualified("codec", "openai")
50}