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