sim_codec_chat/providers/
lemonade.rs1use std::sync::Arc;
8
9use sim_codec::{Decoder, DomainCodecLib, Encoder, Input, Output, ReadCx};
10use sim_kernel::{CodecId, Expr, Lib, LibManifest, Linker, LoadCx, Result, Symbol, WriteCx};
11
12use super::openai::OpenAiCodecOptions;
13use super::openai_compat::{
14 decode_request_for_provider, decode_response_for_provider, decode_stream_for_provider,
15 encode_request_for_provider, encode_response_for_codec, encode_response_for_provider,
16};
17
18const LEMONADE_CODEC_ID: CodecId = CodecId(0);
19const PROVIDER: &str = "lemonade";
20
21pub type LemonadeCodecOptions = OpenAiCodecOptions;
23
24pub type LemonadeRequestOptions = LemonadeCodecOptions;
26
27pub struct LemonadeCodec;
29
30impl Decoder for LemonadeCodec {
31 fn decode(&self, cx: &mut ReadCx<'_>, input: Input) -> Result<Expr> {
32 decode_request_for_provider(cx.codec, input, PROVIDER)
33 }
34}
35
36impl Encoder for LemonadeCodec {
37 fn encode(&self, cx: &mut WriteCx<'_>, expr: &Expr) -> Result<Output> {
38 encode_response_for_codec(cx.codec, expr).map(Output::Text)
39 }
40}
41
42pub struct LemonadeCodecLib {
44 symbol: Symbol,
45 codec_id: CodecId,
46}
47
48impl LemonadeCodecLib {
49 pub fn new(id: CodecId) -> Self {
51 Self {
52 symbol: lemonade_codec_symbol(),
53 codec_id: id,
54 }
55 }
56
57 fn domain_lib(&self) -> DomainCodecLib {
58 DomainCodecLib::new(
59 self.symbol.clone(),
60 self.codec_id,
61 Arc::new(LemonadeCodec),
62 Arc::new(LemonadeCodec),
63 Symbol::qualified("codec", "LemonadeTranscript"),
64 )
65 }
66}
67
68impl Lib for LemonadeCodecLib {
69 fn manifest(&self) -> LibManifest {
70 self.domain_lib().manifest()
71 }
72
73 fn load(&self, cx: &mut LoadCx, linker: &mut Linker<'_>) -> Result<()> {
74 self.domain_lib().load(cx, linker)
75 }
76}
77
78pub fn lemonade_codec_symbol() -> Symbol {
80 Symbol::qualified("codec", PROVIDER)
81}
82
83pub fn decode_lemonade_request(input: Input) -> Result<Expr> {
85 decode_request_for_provider(LEMONADE_CODEC_ID, input, PROVIDER)
86}
87
88pub fn encode_lemonade_request(expr: &Expr, options: &LemonadeRequestOptions) -> Result<Vec<u8>> {
90 encode_request_for_provider(expr, options)
91}
92
93pub fn decode_lemonade_response(
95 runner: Symbol,
96 model: &str,
97 body: &[u8],
98 include_raw: bool,
99) -> Result<Expr> {
100 decode_response_for_provider(runner, model, body, include_raw, PROVIDER)
101}
102
103pub fn decode_lemonade_stream(
105 runner: Symbol,
106 model: &str,
107 body: &[u8],
108 include_raw: bool,
109) -> Result<Expr> {
110 decode_stream_for_provider(runner, model, body, include_raw, PROVIDER)
111}
112
113pub fn encode_lemonade_response(expr: &Expr) -> Result<Vec<u8>> {
115 encode_response_for_provider(expr)
116}