oxi-ai 0.6.3

Unified LLM API — multi-provider streaming interface for AI coding assistants
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
//! High-level API for oxi-ai
//!
//! Provides convenient functions for common LLM interactions.

use crate::error::{Error, ProviderError};
use crate::{
    AssistantMessage, ContentBlock, Context, Model, ProviderEvent, StreamOptions, TextContent,
    ToolCall,
};
use futures::StreamExt;

/// High-level complete function that collects all streaming events
/// and returns the final assistant message.
///
/// # Arguments
/// * `model` - The model to use
/// * `context` - The conversation context
/// * `options` - Optional streaming options
///
/// # Returns
/// The final assistant message containing all content blocks
pub async fn complete(
    model: &Model,
    context: &Context,
    options: Option<StreamOptions>,
) -> std::result::Result<AssistantMessage, Error> {
    use crate::providers::stream;

    let mut stream = stream(model, context, options).await?;

    let mut final_message: Option<AssistantMessage> = None;
    let mut text_buffer = String::new();
    let mut current_text_index: Option<usize> = None;
    let mut tool_calls: Vec<(usize, ToolCall)> = Vec::new();

    while let Some(event) = stream.next().await {
        match event {
            ProviderEvent::Start { partial } => {
                final_message = Some(partial);
            }
            ProviderEvent::TextStart {
                content_index,
                partial,
            } => {
                if final_message.is_none() {
                    final_message = Some(partial);
                }
                current_text_index = Some(content_index);
                text_buffer.clear();
            }
            ProviderEvent::TextDelta {
                delta,
                content_index,
                ..
            } => {
                if current_text_index != Some(content_index) {
                    // New text block started — flush previous buffer first
                    if let Some(idx) = current_text_index {
                        if !text_buffer.is_empty() {
                            push_text_block(&mut final_message, idx, &text_buffer);
                        }
                    }
                    current_text_index = Some(content_index);
                    text_buffer.clear();
                }
                text_buffer.push_str(&delta);
            }
            ProviderEvent::TextEnd {
                content_index,
                content,
                ..
            } => {
                push_text_block(&mut final_message, content_index, &content);
            }
            ProviderEvent::ThinkingStart {
                content_index: _,
                partial,
            } => {
                if final_message.is_none() {
                    final_message = Some(partial);
                }
            }
            ProviderEvent::ThinkingDelta {
                delta,
                content_index,
                ..
            } => {
                // Append thinking content
                if let Some(ref mut msg) = final_message {
                    // Find or create thinking block
                    let content = ContentBlock::Thinking(crate::ThinkingContent {
                        content_type: crate::ThinkingContentType::Thinking,
                        thinking: delta,
                        thinking_signature: None,
                        redacted: None,
                    });
                    if content_index >= msg.content.len() {
                        msg.content.push(content);
                    }
                }
            }
            ProviderEvent::ThinkingEnd {
                content_index,
                content,
                ..
            } => {
                if let Some(ref mut msg) = final_message {
                    let thinking = ContentBlock::Thinking(crate::ThinkingContent {
                        content_type: crate::ThinkingContentType::Thinking,
                        thinking: content,
                        thinking_signature: None,
                        redacted: None,
                    });
                    if content_index >= msg.content.len() {
                        msg.content.push(thinking);
                    }
                }
            }
            ProviderEvent::ToolCallStart {
                content_index,
                tool_call_id,
                partial,
            } => {
                if final_message.is_none() {
                    final_message = Some(partial);
                }
                // Initialize tool call — use provider ID if available, otherwise generate
                let id = tool_call_id.unwrap_or_else(|| format!("tool_call_{}", content_index));
                let tc = ToolCall {
                    content_type: crate::ToolCallType::ToolCall,
                    id,
                    name: String::new(),
                    arguments: serde_json::json!({}),
                    thought_signature: None,
                };
                tool_calls.push((content_index, tc));
            }
            ProviderEvent::ToolCallDelta {
                delta,
                content_index,
                ..
            } => {
                // Accumulate tool call arguments
                if let Some((_, tc)) = tool_calls.iter_mut().find(|(idx, _)| *idx == content_index)
                {
                    // Parse the accumulated args
                    let current_args = tc.arguments.to_string() + &delta;
                    if let Ok(parsed) = serde_json::from_str(&current_args) {
                        tc.arguments = parsed;
                    }
                }
            }
            ProviderEvent::ToolCallEnd {
                content_index,
                tool_call,
                ..
            } => {
                // Update or add tool call
                if let Some((_, tc)) = tool_calls.iter_mut().find(|(idx, _)| *idx == content_index)
                {
                    *tc = tool_call.clone();
                }
                // Add to final message content
                push_tool_call(&mut final_message, content_index, tool_call.clone());
            }
            ProviderEvent::Done { message, .. } => {
                // Finalize any remaining text
                if let Some(idx) = current_text_index {
                    if !text_buffer.is_empty() {
                        push_text_block(&mut final_message, idx, &text_buffer);
                    }
                }

                // Add any pending tool calls
                for (content_index, tc) in &tool_calls {
                    push_tool_call(&mut final_message, *content_index, tc.clone());
                }

                final_message = Some(message);
                break;
            }
            ProviderEvent::Error { error, .. } => {
                return Err(Error::Provider(ProviderError::StreamError(
                    error
                        .error_message
                        .unwrap_or_else(|| "Unknown error".to_string()),
                )));
            }
        }
    }

    final_message.ok_or_else(|| {
        Error::Provider(ProviderError::StreamError(
            "Stream ended without message".to_string(),
        ))
    })
}

/// Push a text block to the message content
fn push_text_block(msg: &mut Option<AssistantMessage>, index: usize, text: &str) {
    if let Some(ref mut m) = msg {
        let content = ContentBlock::Text(TextContent {
            content_type: crate::TextContentType::Text,
            text: text.to_string(),
            text_signature: None,
        });

        // Ensure the content array is large enough
        while m.content.len() <= index {
            m.content.push(ContentBlock::Text(TextContent {
                content_type: crate::TextContentType::Text,
                text: String::new(),
                text_signature: None,
            }));
        }

        // Append text to existing block
        if let ContentBlock::Text(t) = &mut m.content[index] {
            if t.text.is_empty() {
                *t = TextContent::new(text);
            } else {
                t.text.push_str(text);
            }
        } else {
            m.content[index] = content;
        }
    }
}

/// Push a tool call block to the message content
fn push_tool_call(msg: &mut Option<AssistantMessage>, index: usize, tool_call: ToolCall) {
    if let Some(ref mut m) = msg {
        while m.content.len() <= index {
            m.content.push(ContentBlock::Text(TextContent::new("")));
        }
        m.content[index] = ContentBlock::ToolCall(tool_call);
    }
}

/// Token estimation utilities
pub mod tokens {
    /// Estimate token count using a hybrid algorithm that combines
    /// character-based and word-based heuristics.
    ///
    /// The estimator accounts for:
    /// - **CJK characters** (1 token per character – ideographic languages
    ///   tokenize nearly 1:1 with modern BPE tokenizers)
    /// - **Punctuation & symbols** (~1.5 tokens per character – they tend
    ///   to form short, independent tokens)
    /// - **Common ASCII** (~0.25 tokens per character, i.e. ~4 chars/token)
    /// - **Whitespace** overhead (~1 token per whitespace-separated word)
    ///
    /// For typical mixed English source code and prose this gives results
    /// within ±10% of tiktoken outputs for GPT-4-class tokenizers.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxi_ai::estimate_tokens;
    /// let text = "Hello, world! This is a test.";
    /// let tokens = estimate_tokens(text);
    /// assert!(tokens > 0);
    /// ```
    ///
    /// # Arguments
    /// * `text` - The text to estimate tokens for
    ///
    /// # Returns
    /// Estimated token count
    pub fn estimate(text: &str) -> usize {
        if text.is_empty() {
            return 0;
        }

        let mut cjk_chars: usize = 0;
        let mut ascii_or_latin_chars: usize = 0;
        let mut punct_chars: usize = 0;
        let mut whitespace_words: usize = 0;
        let mut in_word = false;

        for ch in text.chars() {
            if ch.is_whitespace() {
                if in_word {
                    whitespace_words += 1;
                    in_word = false;
                }
            } else {
                in_word = true;
                if is_cjk(ch) {
                    cjk_chars += 1;
                } else if is_punctuation(ch) {
                    punct_chars += 1;
                } else {
                    ascii_or_latin_chars += 1;
                }
            }
        }
        // Count trailing word if text doesn't end with whitespace
        if in_word {
            whitespace_words += 1;
        }

        // CJK: ~1 token per character
        let cjk_tokens = cjk_chars;
        // Punctuation & symbols: ~1.5 tokens per char (round to 3 per 2)
        let punct_tokens = (punct_chars * 3 + 1) / 2;
        // ASCII / Latin: ~4 chars per token
        let ascii_tokens = (ascii_or_latin_chars + 3) / 4;
        // Whitespace word-boundary tokens (BPE adds ~1 overhead per word)
        let ws_tokens = whitespace_words / 8;

        cjk_tokens + punct_tokens + ascii_tokens + ws_tokens
    }

    /// Check if a character is a CJK ideograph.
    fn is_cjk(ch: char) -> bool {
        matches!(ch,
            '\u{4E00}'..='\u{9FFF}'   |  // CJK Unified Ideographs
            '\u{3400}'..='\u{4DBF}'   |  // CJK Unified Ideographs Extension A
            '\u{20000}'..='\u{2A6DF}' |  // CJK Unified Ideographs Extension B
            '\u{2A700}'..='\u{2B73F}' |  // CJK Unified Ideographs Extension C
            '\u{2B740}'..='\u{2B81F}' |  // CJK Unified Ideographs Extension D
            '\u{F900}'..='\u{FAFF}'   |  // CJK Compatibility Ideographs
            '\u{2F800}'..='\u{2FA1F}' |  // CJK Compatibility Ideographs Supplement
            '\u{3000}'..='\u{303F}'   |  // CJK Symbols and Punctuation
            '\u{3040}'..='\u{309F}'   |  // Hiragana
            '\u{30A0}'..='\u{30FF}'   |  // Katakana
            '\u{AC00}'..='\u{D7AF}'      // Hangul Syllables
        )
    }

    /// Check if a character is punctuation or a symbol that tends to
    /// tokenize into short, separate tokens.
    fn is_punctuation(ch: char) -> bool {
        ch.is_ascii_punctuation()
            || matches!(
                ch,
                '\u{201C}'
                    | '\u{201D}'
                    | '\u{2018}'
                    | '\u{2019}'
                    | '\u{2026}'
                    | '\u{2013}'
                    | '\u{2014}'
                    | '\u{00AB}'
                    | '\u{00BB}'
                    | '\u{00B7}'
                    | '\u{2022}'
                    | '\u{203B}'
                    | '\u{2192}'
                    | '\u{2190}'
                    | '\u{21D2}'
                    | '\u{2194}'
                    | '\\'
                    | '|'
                    | '~'
                    | '^'
                    | '`'
            )
    }

    /// Estimate tokens based on word count.
    ///
    /// Uses the improved hybrid estimator internally, but provided
    /// as a simpler word-based fallback.
    ///
    /// # Arguments
    /// * `text` - The text to estimate tokens for
    ///
    /// # Returns
    /// Estimated token count
    pub fn estimate_words(text: &str) -> usize {
        let word_count = text.split_whitespace().count();
        // ~1.3 tokens per word for English, higher for mixed content
        let per_word = if text.chars().any(is_cjk) { 1.6 } else { 1.3 };
        (word_count as f64 * per_word) as usize
    }

    /// Calculate context length usage percentage.
    ///
    /// # Arguments
    /// * `text` - The text to measure
    /// * `context_window` - The model's context window size
    ///
    /// # Returns
    /// Percentage of context window used (0.0 to 1.0)
    pub fn context_usage(text: &str, context_window: usize) -> f64 {
        if context_window == 0 {
            return 0.0;
        }
        (estimate(text) as f64 / context_window as f64).min(1.0)
    }

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

        #[test]
        fn estimate_empty_string() {
            assert_eq!(estimate(""), 0);
        }

        #[test]
        fn estimate_plain_english() {
            // "Hello world, this is a test." ≈ 8 tokens (GPT-4 tiktoken)
            let tokens = estimate("Hello world, this is a test.");
            // Should be in a reasonable range (5–12)
            assert!(
                tokens >= 4 && tokens <= 14,
                "expected 4–14 tokens for plain English sentence, got {}",
                tokens
            );
        }

        #[test]
        fn estimate_cjk() {
            // Each CJK char ≈ 1 token
            let tokens = estimate("\u{4F60}\u{597D}\u{4E16}\u{754C}\u{6D4B}\u{8BD5}");
            assert!(
                tokens >= 4,
                "expected >= 4 tokens for 5 CJK chars, got {}",
                tokens
            );
        }

        #[test]
        fn estimate_code() {
            let code = "fn main() { println!(\"hello\"); }";
            let tokens = estimate(code);
            // Code is punctuation-heavy; expect reasonable estimate
            assert!(
                tokens >= 4 && tokens <= 20,
                "expected 4–20 tokens for code snippet, got {}",
                tokens
            );
        }

        #[test]
        fn estimate_longer_than_naive() {
            // The hybrid estimator should give higher (more accurate) counts
            // than the old `text.len() / 4` for punctuation-heavy text.
            let text = "{ \"key\": \"value\" }";
            let hybrid = estimate(text);
            let naive = text.len() / 4;
            // Hybrid should be positive and in a reasonable range
            assert!(hybrid > 0);
            // For this short punctuation-heavy string, hybrid will be higher
            // than naive but should not exceed 10x
            assert!(hybrid <= naive * 10, "hybrid={} naive={}", hybrid, naive);
        }

        #[test]
        fn context_usage_clamped() {
            assert_eq!(context_usage("short", 0), 0.0);
            assert!(context_usage("hello", 100000) < 1.0);
        }
    }
}

// Re-export `estimate` as the main token-estimation function.
pub use tokens::estimate as estimate_tokens;