oino-ai 1.0.1

Message, content, model and streaming types for the Oino agent stack
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! The `Model` and related catalogue types.

use std::fmt;

use indexmap::IndexMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::json::{JsonObject, JsonValue, js_f64, js_f64_opt};
use crate::primitives::{InputModality, ThinkingLevel};

/// Chat model.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Model {
    /// Model id within the provider.
    pub id: String,
    /// Display name.
    pub name: String,
    /// Wire API.
    pub api: String,
    /// Provider id.
    pub provider: String,
    /// Base URL.
    #[serde(default)]
    pub base_url: String,
    /// Accepted input modalities.
    #[serde(default)]
    pub input: Vec<InputModality>,
    /// Request size / image limits.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub input_limits: Option<InputLimits>,
    /// Pricing per million tokens.
    #[serde(default)]
    pub cost: ModelCost,
    /// Extra request headers.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub headers: Option<IndexMap<String, String>>,
    /// `"chat"` when present.
    #[serde(rename = "type", default, skip_serializing_if = "Option::is_none")]
    pub kind: Option<ModelKindChat>,
    /// Whether the model supports reasoning.
    #[serde(default)]
    pub reasoning: bool,
    /// Mapping of thinking levels to provider effort strings (`null` = unsupported).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub thinking_level_map: Option<ThinkingLevelMap>,
    /// Prompt cache pricing/retention hints.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub prompt_cache: Option<PromptCache>,
    /// Context window in tokens.
    #[serde(default)]
    pub context_window: u64,
    /// Maximum output tokens.
    #[serde(default)]
    pub max_tokens: u64,
    /// Default sampling parameters.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sampling_params: Option<JsonObject>,
    /// Sampling parameters per thinking level.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sampling_params_by_thinking_level: Option<IndexMap<ThinkingLevel, JsonObject>>,
    /// Provider compatibility flags (opaque to core).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub compat: Option<JsonValue>,
}

/// The literal `"chat"` model type.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum ModelKindChat {
    /// `"chat"`.
    #[serde(rename = "chat")]
    Chat,
}

/// `"provider/id"` model key.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ModelKey {
    /// Provider id.
    pub provider: String,
    /// Model id.
    pub id: String,
}

impl fmt::Display for ModelKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}/{}", self.provider, self.id)
    }
}

impl Model {
    /// `provider/id` key.
    #[must_use]
    pub fn key(&self) -> ModelKey {
        ModelKey {
            provider: self.provider.clone(),
            id: self.id.clone(),
        }
    }

    /// `[Off]` for non-reasoning models; otherwise every level
    /// not mapped to `null`, with `xhigh`/`max` only when explicitly mapped.
    #[must_use]
    pub fn available_thinking_levels(&self) -> Vec<ThinkingLevel> {
        if !self.reasoning {
            return vec![ThinkingLevel::Off];
        }
        ThinkingLevel::ALL
            .into_iter()
            .filter(|level| {
                let mapped = self
                    .thinking_level_map
                    .as_ref()
                    .map_or(MapEntry::Absent, |map| map.get(*level));
                match mapped {
                    MapEntry::Null => false,
                    MapEntry::Absent => !matches!(level, ThinkingLevel::Xhigh | ThinkingLevel::Max),
                    MapEntry::Value(_) => true,
                }
            })
            .collect()
    }
}

/// Pricing (USD per million tokens).
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ModelCost {
    /// Input price.
    #[serde(with = "js_f64")]
    pub input: f64,
    /// Output price.
    #[serde(with = "js_f64")]
    pub output: f64,
    /// Cache read price.
    #[serde(with = "js_f64")]
    pub cache_read: f64,
    /// Cache write price.
    #[serde(with = "js_f64")]
    pub cache_write: f64,
    /// Tiered prices above input-token thresholds.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tiers: Option<Vec<ModelCostTier>>,
}

/// One price tier.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ModelCostTier {
    /// Input price.
    #[serde(with = "js_f64")]
    pub input: f64,
    /// Output price.
    #[serde(with = "js_f64")]
    pub output: f64,
    /// Cache read price.
    #[serde(with = "js_f64")]
    pub cache_read: f64,
    /// Cache write price.
    #[serde(with = "js_f64")]
    pub cache_write: f64,
    /// Threshold in input tokens.
    pub input_tokens_above: u64,
}

/// Value of one [`ThinkingLevelMap`] slot.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MapEntry<'a> {
    /// Key absent.
    Absent,
    /// Explicit `null`: level unsupported.
    Null,
    /// Provider effort string.
    Value(&'a str),
}

/// Each key is absent, `null`, or a provider string.
#[allow(
    clippy::option_option,
    reason = "absent, null and string are three distinct states"
)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ThinkingLevelMap {
    /// `off`.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "tri")]
    pub off: Option<Option<String>>,
    /// `minimal`.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "tri")]
    pub minimal: Option<Option<String>>,
    /// `low`.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "tri")]
    pub low: Option<Option<String>>,
    /// `medium`.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "tri")]
    pub medium: Option<Option<String>>,
    /// `high`.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "tri")]
    pub high: Option<Option<String>>,
    /// `xhigh`.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "tri")]
    pub xhigh: Option<Option<String>>,
    /// `max`.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "tri")]
    pub max: Option<Option<String>>,
}

impl ThinkingLevelMap {
    /// Slot for `level`.
    #[must_use]
    pub fn get(&self, level: ThinkingLevel) -> MapEntry<'_> {
        let slot = match level {
            ThinkingLevel::Off => &self.off,
            ThinkingLevel::Minimal => &self.minimal,
            ThinkingLevel::Low => &self.low,
            ThinkingLevel::Medium => &self.medium,
            ThinkingLevel::High => &self.high,
            ThinkingLevel::Xhigh => &self.xhigh,
            ThinkingLevel::Max => &self.max,
        };
        match slot {
            None => MapEntry::Absent,
            Some(None) => MapEntry::Null,
            Some(Some(value)) => MapEntry::Value(value),
        }
    }
}

/// Serde for `Option<Option<String>>`: absent / `null` / string.
#[allow(
    clippy::option_option,
    reason = "absent, null and string are three distinct states"
)]
mod tri {
    use super::{Deserialize, Deserializer, Serializer};

    #[allow(
        clippy::ref_option,
        reason = "signature required by serde(serialize_with)"
    )]
    pub(super) fn serialize<S: Serializer>(
        value: &Option<Option<String>>,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        match value {
            Some(Some(text)) => serializer.serialize_str(text),
            _ => serializer.serialize_none(),
        }
    }

    pub(super) fn deserialize<'de, D: Deserializer<'de>>(
        deserializer: D,
    ) -> Result<Option<Option<String>>, D::Error> {
        Option::<String>::deserialize(deserializer).map(Some)
    }
}

/// Prompt cache pricing hints.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct PromptCache {
    /// Short-retention multiplier.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "js_f64_opt")]
    pub short: Option<f64>,
    /// Long-retention multiplier.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "js_f64_opt")]
    pub long: Option<f64>,
}

/// Request limits.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InputLimits {
    /// Maximum request body size.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_request_bytes: Option<u64>,
    /// Image limits.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub images: Option<ImageInputLimits>,
}

/// Image limits.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ImageInputLimits {
    /// Resize policy.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub resize: Option<ImageResize>,
    /// Images per message.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_per_message: Option<u32>,
    /// Images per request.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_per_request: Option<u32>,
}

/// Image resize policy.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ImageResize {
    /// Maximum width.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_width: Option<u32>,
    /// Maximum height.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_height: Option<u32>,
    /// Maximum encoded size.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_bytes: Option<u64>,
    /// JPEG quality used when re-encoding.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub jpeg_quality: Option<u8>,
}

/// Image-generation model.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ImageModel {
    /// Model id.
    pub id: String,
    /// Display name.
    pub name: String,
    /// Wire API.
    pub api: String,
    /// Provider id.
    pub provider: String,
    /// Base URL.
    #[serde(default)]
    pub base_url: String,
    /// Input modalities.
    #[serde(default)]
    pub input: Vec<InputModality>,
    /// Output modalities (`text`, `image`).
    #[serde(default)]
    pub output: Vec<InputModality>,
    /// Pricing.
    #[serde(default)]
    pub cost: ModelCost,
    /// Extra headers.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub headers: Option<IndexMap<String, String>>,
}

/// Classifier model.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ClassifierModel {
    /// Model id.
    pub id: String,
    /// Display name.
    pub name: String,
    /// Wire API.
    pub api: String,
    /// Provider id.
    pub provider: String,
    /// Base URL.
    #[serde(default)]
    pub base_url: String,
    /// Input modalities.
    #[serde(default)]
    pub input: Vec<InputModality>,
    /// Pricing.
    #[serde(default)]
    pub cost: ModelCost,
    /// Extra headers.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub headers: Option<IndexMap<String, String>>,
    /// Context window.
    #[serde(default)]
    pub context_window: u64,
    /// Provider compatibility flags.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub compat: Option<JsonValue>,
}

/// Any registry model, discriminated by `type` (absent ⇒ chat).
#[derive(Clone, Debug, PartialEq)]
pub enum AnyModel {
    /// Chat model (`type` absent or `"chat"`).
    Chat(Box<Model>),
    /// `type: "image"`.
    Image(Box<ImageModel>),
    /// `type: "classifier"`.
    Classifier(Box<ClassifierModel>),
}

impl Serialize for AnyModel {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::Error;
        let (mut value, tag) = match self {
            Self::Chat(model) => return model.serialize(serializer),
            Self::Image(model) => (
                serde_json::to_value(model).map_err(S::Error::custom)?,
                "image",
            ),
            Self::Classifier(model) => (
                serde_json::to_value(model).map_err(S::Error::custom)?,
                "classifier",
            ),
        };
        if let Some(object) = value.as_object_mut() {
            object.insert("type".into(), JsonValue::String(tag.into()));
        }
        value.serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for AnyModel {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        use serde::de::Error;
        let value = JsonValue::deserialize(deserializer)?;
        let kind = value
            .get("type")
            .and_then(JsonValue::as_str)
            .map(str::to_owned);
        match kind.as_deref() {
            None | Some("chat") => serde_json::from_value(value)
                .map(|m| Self::Chat(Box::new(m)))
                .map_err(D::Error::custom),
            Some("image") => serde_json::from_value(value)
                .map(|m| Self::Image(Box::new(m)))
                .map_err(D::Error::custom),
            Some("classifier") => serde_json::from_value(value)
                .map(|m| Self::Classifier(Box::new(m)))
                .map_err(D::Error::custom),
            Some(other) => Err(D::Error::custom(format!("unknown model type: {other}"))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn model(raw: JsonValue) -> Option<Model> {
        serde_json::from_value(raw).ok()
    }

    #[test]
    fn thinking_levels_follow_map() {
        let base = json!({"id":"m","name":"M","api":"a","provider":"p","baseUrl":"","input":["text"],
            "cost":{"input":3,"output":15,"cacheRead":0.3,"cacheWrite":3.75},"reasoning":true,
            "contextWindow":200_000,"maxTokens":8192});
        let plain = model(base.clone());
        assert_eq!(plain.map(|m| m.available_thinking_levels().len()), Some(5));
        let mut mapped = base;
        mapped["thinkingLevelMap"] = json!({"minimal": null, "xhigh": "max"});
        let levels = model(mapped.clone()).map(|m| m.available_thinking_levels());
        assert_eq!(
            levels,
            Some(vec![
                ThinkingLevel::Off,
                ThinkingLevel::Low,
                ThinkingLevel::Medium,
                ThinkingLevel::High,
                ThinkingLevel::Xhigh
            ])
        );
        assert_eq!(
            model(mapped.clone()).and_then(|m| serde_json::to_value(m).ok()),
            Some(mapped)
        );
    }

    #[test]
    fn any_model_dispatches_on_type() {
        let image: Option<AnyModel> = serde_json::from_value(json!({"type":"image","id":"i","name":"I","api":"x",
            "provider":"p","baseUrl":"","input":["text"],"output":["image"],"cost":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0}})).ok();
        assert!(matches!(image, Some(AnyModel::Image(_))));
        let raw = image
            .and_then(|m| serde_json::to_value(m).ok())
            .unwrap_or_default();
        assert_eq!(raw["type"], json!("image"));
    }
}