Skip to main content

oxicode_ai/utils/
mod.rs

1//! Utility modules for AI API handling
2//!
3//! These utilities provide robust handling for common AI API edge cases:
4//! - Unicode sanitization for safe JSON serialization
5//! - JSON parsing with repair for malformed streaming responses
6//! - Context overflow detection across providers
7//! - Tool call ID normalization for cross-provider compatibility
8
9pub mod json_parse;
10pub mod overflow;
11pub mod sanitize_unicode;
12pub mod secret_obfuscator;
13pub mod thinking_loop;
14pub mod tool_call_loop;
15
16/// Normalize a tool call ID for cross-provider compatibility.
17///
18/// OpenAI Responses API generates IDs that are 450+ chars with special characters like `|`.
19/// Anthropic APIs require IDs matching `^[a-zA-Z0-9_-]+$` (max 64 chars).
20///
21/// This replaces non-alphanumeric (except `_` and `-`) chars with `_` and
22/// truncates to 64 characters.
23pub fn normalize_tool_call_id(id: &str) -> String {
24    let sanitized: String = id
25        .chars()
26        .map(|c| {
27            if c.is_alphanumeric() || c == '_' || c == '-' {
28                c
29            } else {
30                '_'
31            }
32        })
33        .collect();
34    if sanitized.len() > 64 {
35        sanitized[..64].trim_end_matches('_').to_string()
36    } else {
37        sanitized.trim_end_matches('_').to_string()
38    }
39}