tt-shared 0.1.2

Shared types, errors, and Provider trait for TokenTrimmer.
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Capability and context-window guard for the routing / failover path.
//!
//! [`RequiredCapabilities`] is derived from a [`ChatCompletionRequest`] and
//! checked against a candidate model's [`ModelInfo`] before a route rewrite or
//! failover dispatch is committed.  The check is intentionally permissive:
//!
//! - When `ModelInfo` is **unknown** for a candidate (not in the registry
//!   catalog) we allow it through — we only skip when we *positively know* a
//!   capability is missing.
//! - A capability that the request needs but the model info does **not** list
//!   causes the candidate to be skipped (the caller emits a tracing event and
//!   tries the next candidate or falls back to the original model).
//!
//! # Token counting
//!
//! [`estimate_input_tokens`] concatenates all message text and delegates to
//! [`tt_tokenize::estimate_tokens`], keyed on `provider_id` so tiktoken is
//! used for OpenAI/Anthropic and the char/4 heuristic is used elsewhere.
//! Image/audio bytes are not measured — the guard is a best-effort floor, not
//! an exact window-packing count.

use crate::{
    messages::{ContentPart, Message, MessageContent},
    pricing::{Capability, ModelInfo},
    ChatCompletionRequest,
};

/// The set of capabilities a [`ChatCompletionRequest`] requires.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RequiredCapabilities {
    /// At least one message contains an image_url or input_audio content part.
    pub vision: bool,
    /// The request has non-empty `tools`, or any assistant message contains
    /// `tool_calls`.
    pub tools: bool,
    /// `response_format.type` is `"json_object"` or `"json_schema"`.
    pub json_mode: bool,
}

impl RequiredCapabilities {
    /// Derive the required capabilities from a chat completion request.
    pub fn from_request(req: &ChatCompletionRequest) -> Self {
        let mut caps = Self::default();

        // tools / function-calling
        if !req.tools.is_empty() {
            caps.tools = true;
        }

        // response_format → json mode
        if let Some(rf) = &req.response_format {
            if rf.r#type == "json_object" || rf.r#type == "json_schema" {
                caps.json_mode = true;
            }
        }

        // scan messages for vision content and tool_calls
        for msg in &req.messages {
            match msg {
                Message::User { content, .. } | Message::System { content } => {
                    if let MessageContent::Parts(parts) = content {
                        for part in parts {
                            match part {
                                ContentPart::ImageUrl { .. } | ContentPart::InputAudio { .. } => {
                                    caps.vision = true;
                                }
                                ContentPart::Text { .. } => {}
                            }
                        }
                    }
                }
                Message::Assistant { tool_calls, .. } => {
                    if !tool_calls.is_empty() {
                        caps.tools = true;
                    }
                }
                Message::Tool { .. } => {
                    // A Tool message in context means the conversation already
                    // used tool-calling; the next turn may need it too.
                    caps.tools = true;
                }
            }
        }

        caps
    }

    /// Returns `true` when all required capabilities are listed in
    /// `info.capabilities` **and** `max_input_tokens >= estimated_tokens`.
    ///
    /// Pass `estimated_tokens = 0` to skip the context-window check.
    #[must_use]
    pub fn satisfied_by(&self, info: &ModelInfo, estimated_tokens: u64) -> bool {
        if self.vision && !info.capabilities.contains(&Capability::Vision) {
            return false;
        }
        if self.tools && !info.capabilities.contains(&Capability::Tools) {
            return false;
        }
        if self.json_mode && !info.capabilities.contains(&Capability::JsonMode) {
            return false;
        }
        if estimated_tokens > 0 && info.max_input_tokens < estimated_tokens {
            return false;
        }
        true
    }

    /// Human-readable list of the reasons a candidate was skipped, for use in
    /// the `route_skipped_capability` tracing event.
    pub fn skip_reasons(&self, info: &ModelInfo, estimated_tokens: u64) -> Vec<&'static str> {
        let mut reasons = Vec::new();
        if self.vision && !info.capabilities.contains(&Capability::Vision) {
            reasons.push("vision_not_supported");
        }
        if self.tools && !info.capabilities.contains(&Capability::Tools) {
            reasons.push("tools_not_supported");
        }
        if self.json_mode && !info.capabilities.contains(&Capability::JsonMode) {
            reasons.push("json_mode_not_supported");
        }
        if estimated_tokens > 0 && info.max_input_tokens < estimated_tokens {
            reasons.push("context_window_too_small");
        }
        reasons
    }
}

/// Concatenate all message text parts from a request for token estimation.
///
/// Image/audio bytes are excluded — the result is passed to the caller's
/// tokenizer (e.g. `tt_tokenize::estimate_tokens`) so that `tt-shared` does
/// not need to depend on `tt-tokenize`.
pub fn message_text_for_estimation(req: &ChatCompletionRequest) -> String {
    req.messages
        .iter()
        .map(|m| match m {
            Message::User { content, .. } | Message::System { content } => extract_text(content),
            Message::Assistant { content, .. } => {
                content.as_ref().map(extract_text).unwrap_or_default()
            }
            Message::Tool { content, .. } => extract_text(content),
        })
        .collect()
}

fn extract_text(content: &MessageContent) -> String {
    match content {
        MessageContent::Text(s) => s.clone(),
        MessageContent::Parts(parts) => parts
            .iter()
            .filter_map(|p| match p {
                ContentPart::Text { text } => Some(text.as_str()),
                _ => None,
            })
            .collect::<Vec<_>>()
            .join(""),
    }
}

/// True when any message carries an image (`ContentPart::ImageUrl`) content part.
///
/// Distinct from [`RequiredCapabilities`], which collapses image **and** audio
/// into a single `vision` flag; routing needs to tell the two modalities apart.
pub fn request_has_images(req: &ChatCompletionRequest) -> bool {
    req.messages
        .iter()
        .any(|m| content_of(m).is_some_and(has_image_part))
}

/// True when any message carries an audio (`ContentPart::InputAudio`) content part.
pub fn request_has_audio(req: &ChatCompletionRequest) -> bool {
    req.messages
        .iter()
        .any(|m| content_of(m).is_some_and(has_audio_part))
}

/// The content of a message, if it has any (Assistant content is optional).
fn content_of(m: &Message) -> Option<&MessageContent> {
    match m {
        Message::User { content, .. }
        | Message::System { content }
        | Message::Tool { content, .. } => Some(content),
        Message::Assistant { content, .. } => content.as_ref(),
    }
}

fn has_image_part(c: &MessageContent) -> bool {
    matches!(c, MessageContent::Parts(parts)
        if parts.iter().any(|p| matches!(p, ContentPart::ImageUrl { .. })))
}

fn has_audio_part(c: &MessageContent) -> bool {
    matches!(c, MessageContent::Parts(parts)
        if parts.iter().any(|p| matches!(p, ContentPart::InputAudio { .. })))
}

/// Concatenated text of the **user + system** messages — the caller-controlled
/// input, used for content/topic routing. Assistant/tool turns are excluded so a
/// model's own output can't spuriously trigger a topic route.
pub fn request_input_text(req: &ChatCompletionRequest) -> String {
    req.messages
        .iter()
        .filter_map(|m| match m {
            Message::User { content, .. } | Message::System { content } => {
                Some(extract_text(content))
            }
            _ => None,
        })
        .collect::<Vec<_>>()
        .join("\n")
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use super::*;
    use crate::{
        messages::{
            ImageUrl, InputAudio, ResponseFormat, Tool, ToolCall, ToolCallFunction, ToolFunction,
        },
        pricing::Capability,
        ModelInfo,
    };

    fn text_model() -> ModelInfo {
        ModelInfo {
            id: "text-only".into(),
            provider: "mock".into(),
            capabilities: vec![Capability::Text],
            max_input_tokens: 4096,
            max_output_tokens: 1024,
        }
    }

    fn vision_model() -> ModelInfo {
        ModelInfo {
            id: "vision-model".into(),
            provider: "mock".into(),
            capabilities: vec![Capability::Text, Capability::Vision, Capability::Tools],
            max_input_tokens: 128_000,
            max_output_tokens: 4096,
        }
    }

    fn small_model() -> ModelInfo {
        ModelInfo {
            id: "small-ctx".into(),
            provider: "mock".into(),
            capabilities: vec![Capability::Text],
            max_input_tokens: 100,
            max_output_tokens: 100,
        }
    }

    fn base_req() -> ChatCompletionRequest {
        ChatCompletionRequest {
            model: "gpt-4o".into(),
            messages: vec![],
            temperature: None,
            top_p: None,
            max_tokens: None,
            stream: false,
            tools: vec![],
            tool_choice: None,
            response_format: None,
            stop: vec![],
            presence_penalty: None,
            frequency_penalty: None,
            n: None,
            seed: None,
            user: None,
            tt_extras: HashMap::new(),
            ..Default::default()
        }
    }

    #[test]
    fn plain_text_request_has_no_required_caps() {
        let req = base_req();
        let caps = RequiredCapabilities::from_request(&req);
        assert!(!caps.vision);
        assert!(!caps.tools);
        assert!(!caps.json_mode);
    }

    #[test]
    fn image_url_part_sets_vision() {
        let mut req = base_req();
        req.messages = vec![Message::User {
            content: MessageContent::Parts(vec![
                ContentPart::Text {
                    text: "describe this".into(),
                },
                ContentPart::ImageUrl {
                    image_url: ImageUrl {
                        url: "data:image/png;base64,abc".into(),
                        detail: None,
                    },
                },
            ]),
            name: None,
        }];
        let caps = RequiredCapabilities::from_request(&req);
        assert!(caps.vision);
        assert!(!caps.tools);
    }

    #[test]
    fn tools_field_sets_tools_cap() {
        let mut req = base_req();
        req.tools = vec![Tool {
            r#type: "function".into(),
            function: ToolFunction {
                name: "get_weather".into(),
                description: None,
                parameters: serde_json::json!({}),
            },
        }];
        let caps = RequiredCapabilities::from_request(&req);
        assert!(caps.tools);
    }

    #[test]
    fn assistant_tool_calls_in_history_sets_tools_cap() {
        let mut req = base_req();
        req.messages = vec![Message::Assistant {
            content: None,
            tool_calls: vec![ToolCall {
                id: "call_1".into(),
                r#type: "function".into(),
                function: ToolCallFunction {
                    name: "get_weather".into(),
                    arguments: "{}".into(),
                },
            }],
            name: None,
        }];
        let caps = RequiredCapabilities::from_request(&req);
        assert!(caps.tools);
    }

    #[test]
    fn json_object_response_format_sets_json_mode() {
        let mut req = base_req();
        req.response_format = Some(ResponseFormat {
            r#type: "json_object".into(),
            json_schema: None,
        });
        let caps = RequiredCapabilities::from_request(&req);
        assert!(caps.json_mode);
    }

    #[test]
    fn vision_request_not_satisfied_by_text_model() {
        let mut req = base_req();
        req.messages = vec![Message::User {
            content: MessageContent::Parts(vec![ContentPart::ImageUrl {
                image_url: ImageUrl {
                    url: "data:image/png;base64,abc".into(),
                    detail: None,
                },
            }]),
            name: None,
        }];
        let caps = RequiredCapabilities::from_request(&req);
        assert!(!caps.satisfied_by(&text_model(), 0));
    }

    #[test]
    fn vision_request_satisfied_by_vision_model() {
        let mut req = base_req();
        req.messages = vec![Message::User {
            content: MessageContent::Parts(vec![ContentPart::ImageUrl {
                image_url: ImageUrl {
                    url: "data:image/png;base64,abc".into(),
                    detail: None,
                },
            }]),
            name: None,
        }];
        let caps = RequiredCapabilities::from_request(&req);
        assert!(caps.satisfied_by(&vision_model(), 0));
    }

    #[test]
    fn exceeds_context_window_not_satisfied() {
        let caps = RequiredCapabilities::default();
        assert!(!caps.satisfied_by(&small_model(), 200));
    }

    #[test]
    fn within_context_window_satisfied() {
        let caps = RequiredCapabilities::default();
        assert!(caps.satisfied_by(&small_model(), 50));
    }

    #[test]
    fn zero_estimated_tokens_skips_window_check() {
        let caps = RequiredCapabilities::default();
        assert!(caps.satisfied_by(&small_model(), 0));
    }

    #[test]
    fn skip_reasons_lists_all_failures() {
        let caps = RequiredCapabilities {
            vision: true,
            tools: true,
            ..Default::default()
        };
        let reasons = caps.skip_reasons(&text_model(), 9999);
        assert!(reasons.contains(&"vision_not_supported"));
        assert!(reasons.contains(&"tools_not_supported"));
        assert!(reasons.contains(&"context_window_too_small"));
    }

    #[test]
    fn request_has_images_detects_image_part() {
        let mut req = base_req();
        req.messages = vec![Message::User {
            content: MessageContent::Parts(vec![
                ContentPart::Text {
                    text: "look".into(),
                },
                ContentPart::ImageUrl {
                    image_url: ImageUrl {
                        url: "data:image/png;base64,abc".into(),
                        detail: None,
                    },
                },
            ]),
            name: None,
        }];
        assert!(request_has_images(&req));
        assert!(!request_has_audio(&req));
    }

    #[test]
    fn request_has_audio_detects_audio_part() {
        let mut req = base_req();
        req.messages = vec![Message::User {
            content: MessageContent::Parts(vec![ContentPart::InputAudio {
                input_audio: InputAudio {
                    data: "abc".into(),
                    format: "wav".into(),
                },
            }]),
            name: None,
        }];
        assert!(request_has_audio(&req));
        assert!(!request_has_images(&req));
    }

    #[test]
    fn plain_text_request_has_no_modality() {
        let req = base_req();
        assert!(!request_has_images(&req));
        assert!(!request_has_audio(&req));
    }

    #[test]
    fn request_input_text_user_and_system_only() {
        let mut req = base_req();
        req.messages = vec![
            Message::System {
                content: MessageContent::Text("sys ctx".into()),
            },
            Message::User {
                content: MessageContent::Text("Confidential matter".into()),
                name: None,
            },
            Message::Assistant {
                content: Some(MessageContent::Text("legal advice".into())),
                tool_calls: vec![],
                name: None,
            },
        ];
        let t = request_input_text(&req);
        assert!(t.contains("sys ctx"));
        assert!(t.contains("Confidential matter"));
        assert!(
            !t.contains("legal advice"),
            "assistant output must be excluded"
        );
    }
}