oxi-ai 0.6.4

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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
#![allow(dead_code)]
//! Shared code between OpenAI Responses API and Codex providers.
//!
//! Ported from `openai-responses-shared.ts`. Contains:
//! - Message conversion to the Responses API `input` format
//! - Tool conversion to the Responses API `tools` format
//! - Streaming event processing helpers
//! - Tool call ID normalization for cross-provider compatibility
//! - Text signature encoding/decoding

use serde_json::Value as JsonValue;

use crate::{ContentBlock, Context, Message, MessageContent, Model, Tool};

// ---------------------------------------------------------------------------
// Text signatures
// ---------------------------------------------------------------------------

/// Encode a text signature v1 payload as JSON.
pub fn encode_text_signature_v1(id: &str, phase: Option<&str>) -> String {
    let mut payload = serde_json::json!({ "v": 1, "id": id });
    if let Some(p) = phase {
        payload["phase"] = serde_json::json!(p);
    }
    payload.to_string()
}

/// Parse a text signature, returning `(id, optional_phase)`.
pub fn parse_text_signature(
    signature: Option<&str>,
) -> Option<(String, Option<String>)> {
    let sig = signature?;
    if sig.starts_with('{') {
        if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(sig) {
            if parsed.get("v").and_then(|v| v.as_u64()) == Some(1) {
                if let Some(id) = parsed.get("id").and_then(|v| v.as_str()) {
                    let phase = parsed
                        .get("phase")
                        .and_then(|v| v.as_str())
                        .map(|s| s.to_string());
                    return Some((id.to_string(), phase));
                }
            }
        }
    }
    // Legacy plain-string handling
    Some((sig.to_string(), None))
}

// ---------------------------------------------------------------------------
// Short hash utility
// ---------------------------------------------------------------------------

/// Produce a short deterministic hash of a string (hex, first 12 chars).
pub fn short_hash(input: &str) -> String {
    use std::fmt::Write;
    let hash = simple_hash(input.as_bytes());
    let mut out = String::with_capacity(12);
    for byte in &hash[..6] {
        write!(&mut out, "{:02x}", byte).expect("writing to string");
    }
    out
}

/// Minimal deterministic hash for ID generation purposes.
/// NOT cryptographic, but sufficient for generating short unique IDs.
fn simple_hash(data: &[u8]) -> [u8; 32] {
    let mut result = [0u8; 32];
    for (i, &byte) in data.iter().enumerate() {
        result[i % 32] ^= byte;
        result[(i + 7) % 32] = result[(i + 7) % 32].wrapping_add(byte);
    }
    // Mix rounds
    for round in 0u8..4 {
        for i in 0..32usize {
            let prev = result[(i + 31) % 32];
            result[i] = result[i]
                .wrapping_add(prev)
                .wrapping_mul(17u8.wrapping_add(round));
        }
    }
    result
}

// ---------------------------------------------------------------------------
// ID normalization helpers
// ---------------------------------------------------------------------------

/// Normalize one part of a tool call ID (alphanumeric + `_-`, max 64 chars, no trailing `_`).
fn normalize_id_part(part: &str) -> String {
    let sanitized: String = part
        .chars()
        .map(|c| if c.is_alphanumeric() || c == '_' || c == '-' { c } else { '_' })
        .collect();
    let truncated = if sanitized.len() > 64 {
        &sanitized[..64]
    } else {
        &sanitized
    };
    truncated.trim_end_matches('_').to_string()
}

// ---------------------------------------------------------------------------
// Convert messages to Responses API input format
// ---------------------------------------------------------------------------

/// Options for message conversion.
#[derive(Debug, Clone, Default)]
pub struct ConvertResponsesMessagesOptions {
    /// Whether to include the system prompt as a developer/system message.
    pub include_system_prompt: bool,
}

/// Convert messages from a [`Context`] into the OpenAI Responses API `input` format.
///
/// This handles:
/// - System prompt → `developer` or `system` message based on model capabilities
/// - User messages → `input_text` / `input_image` content
/// - Assistant messages → `output_text`, `function_call`, `reasoning` items
/// - Tool results → `function_call_output` items
/// - Tool call ID normalization for cross-provider compatibility
pub fn convert_responses_messages(
    model: &Model,
    context: &Context,
    _allowed_tool_call_providers: &[&str],
    options: Option<ConvertResponsesMessagesOptions>,
) -> Vec<JsonValue> {
    let opts = options.unwrap_or_default();
    let mut messages: Vec<JsonValue> = Vec::new();

    // Transform messages for the target model using cross-provider transformation.
    // This handles image downgrades, thinking block conversion, tool call ID
    // normalization, synthetic tool results for orphaned tool calls, and skipping
    // error/aborted assistant messages.
    let transformed: Vec<crate::Message> =
        crate::transform::transform_messages_for_model(&context.messages, model);

    // System prompt
    if opts.include_system_prompt {
        if let Some(ref prompt) = context.system_prompt {
            let role = if model.reasoning { "developer" } else { "system" };
            messages.push(serde_json::json!({
                "role": role,
                "content": sanitize_surrogates(prompt),
            }));
        }
    }

    let mut msg_index: usize = 0;

    for msg in &transformed {
        match msg {
            Message::User(u) => {
                match &u.content {
                    MessageContent::Text(s) => {
                        messages.push(serde_json::json!({
                            "role": "user",
                            "content": [{ "type": "input_text", "text": sanitize_surrogates(s) }],
                        }));
                    }
                    MessageContent::Blocks(blocks) => {
                        let content: Vec<JsonValue> = blocks
                            .iter()
                            .filter_map(|block| match block {
                                ContentBlock::Text(t) => Some(serde_json::json!({
                                    "type": "input_text",
                                    "text": sanitize_surrogates(&t.text),
                                })),
                                ContentBlock::Image(img) => Some(serde_json::json!({
                                    "type": "input_image",
                                    "detail": "auto",
                                    "image_url": format!("data:{};base64,{}", img.mime_type, img.data),
                                })),
                                _ => None,
                            })
                            .collect();
                        if content.is_empty() {
                            msg_index += 1;
                            continue;
                        }
                        messages.push(serde_json::json!({
                            "role": "user",
                            "content": content,
                        }));
                    }
                }
            }

            Message::Assistant(a) => {
                let mut output: Vec<JsonValue> = Vec::new();
                let is_different_model = a.model != model.id
                    && a.provider == model.provider
                    && a.api == model.api;

                for block in &a.content {
                    match block {
                        ContentBlock::Thinking(th) => {
                            if let Some(ref sig) = th.thinking_signature {
                                if let Ok(reasoning_item) =
                                    serde_json::from_str::<JsonValue>(sig)
                                {
                                    output.push(reasoning_item);
                                }
                            }
                        }
                        ContentBlock::Text(t) => {
                            let parsed_sig = parse_text_signature(t.text_signature.as_deref());
                            let mut msg_id = parsed_sig
                                .as_ref()
                                .map(|(id, _)| id.clone())
                                .unwrap_or_else(|| format!("msg_{}", msg_index));

                            if msg_id.len() > 64 {
                                msg_id = format!("msg_{}", short_hash(&msg_id));
                            }

                            let phase = parsed_sig.and_then(|(_, p)| p);

                            output.push(serde_json::json!({
                                "type": "message",
                                "role": "assistant",
                                "content": [{
                                    "type": "output_text",
                                    "text": sanitize_surrogates(&t.text),
                                    "annotations": [],
                                }],
                                "status": "completed",
                                "id": msg_id,
                                "phase": phase,
                            }));
                        }
                        ContentBlock::ToolCall(tc) => {
                            let parts: Vec<&str> = tc.id.splitn(2, '|').collect();
                            let call_id = parts[0];
                            let item_id_raw = parts.get(1).copied();

                            let item_id: Option<String> = if is_different_model {
                                item_id_raw.and_then(|id| {
                                    if id.starts_with("fc_") {
                                        None
                                    } else {
                                        Some(normalize_id_part(id))
                                    }
                                })
                            } else {
                                item_id_raw.map(normalize_id_part)
                            };

                            output.push(serde_json::json!({
                                "type": "function_call",
                                "id": item_id,
                                "call_id": normalize_id_part(call_id),
                                "name": tc.name,
                                "arguments": tc.arguments.to_string(),
                            }));
                        }
                        _ => {}
                    }
                }

                if output.is_empty() {
                    msg_index += 1;
                    continue;
                }
                messages.extend(output);
            }

            Message::ToolResult(t) => {
                let text_parts: Vec<&str> = t
                    .content
                    .iter()
                    .filter_map(|b| b.as_text())
                    .collect();
                let text_result = text_parts.join("\n");
                let has_images = t.content.iter().any(|b| matches!(b, ContentBlock::Image(_)));
                let has_text = !text_result.is_empty();

                let parts: Vec<&str> = t.tool_call_id.splitn(2, '|').collect();
                let call_id = normalize_id_part(parts[0]);

                let output_val: JsonValue = if has_images
                    && model.input.contains(&crate::InputModality::Image)
                {
                    let mut content_parts: Vec<JsonValue> = Vec::new();
                    if has_text {
                        content_parts.push(serde_json::json!({
                            "type": "input_text",
                            "text": sanitize_surrogates(&text_result),
                        }));
                    }
                    for block in &t.content {
                        if let ContentBlock::Image(img) = block {
                            content_parts.push(serde_json::json!({
                                "type": "input_image",
                                "detail": "auto",
                                "image_url": format!("data:{};base64,{}", img.mime_type, img.data),
                            }));
                        }
                    }
                    serde_json::json!(content_parts)
                } else {
                    serde_json::json!(sanitize_surrogates(
                        if has_text { &text_result } else { "(see attached image)" }
                    ))
                };

                messages.push(serde_json::json!({
                    "type": "function_call_output",
                    "call_id": call_id,
                    "output": output_val,
                }));
            }
        }
        msg_index += 1;
    }

    messages
}

// ---------------------------------------------------------------------------
// Tool conversion
// ---------------------------------------------------------------------------

/// Options for tool conversion.
#[derive(Debug, Clone, Default)]
pub struct ConvertResponsesToolsOptions {
    /// Whether to use strict mode for function tools.
    pub strict: Option<bool>,
}

/// Convert tools to the Responses API format.
pub fn convert_responses_tools(
    tools: &[Tool],
    options: Option<ConvertResponsesToolsOptions>,
) -> Vec<JsonValue> {
    let strict = options.and_then(|o| o.strict).unwrap_or(false);

    tools
        .iter()
        .map(|tool| {
            serde_json::json!({
                "type": "function",
                "name": tool.name,
                "description": tool.description,
                "parameters": tool.parameters,
                "strict": strict,
            })
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Stream processing helpers
// ---------------------------------------------------------------------------

/// Map a Responses API status to a [`crate::StopReason`].
pub fn map_responses_stop_reason(status: Option<&str>) -> crate::StopReason {
    match status {
        Some("completed") => crate::StopReason::Stop,
        Some("incomplete") => crate::StopReason::Length,
        Some("failed") | Some("cancelled") => crate::StopReason::Error,
        Some("in_progress") | Some("queued") => crate::StopReason::Stop,
        _ => crate::StopReason::Stop,
    }
}

// ---------------------------------------------------------------------------
// Unicode sanitization
// ---------------------------------------------------------------------------

/// Replace lone surrogates and invalid Unicode with the replacement character.
pub fn sanitize_surrogates(s: &str) -> String {
    s.chars()
        .map(|c| {
            if c == '\u{FFFD}' || (c.is_control() && c != '\n' && c != '\r' && c != '\t') {
                ' '
            } else {
                c
            }
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Streaming JSON parser
// ---------------------------------------------------------------------------

/// Parse a partial JSON string, returning the best-effort parsed value.
/// Returns an empty JSON object for completely invalid input.
pub fn parse_streaming_json(input: &str) -> JsonValue {
    if input.is_empty() {
        return serde_json::json!({});
    }

    // Try full parse first
    if let Ok(val) = serde_json::from_str::<JsonValue>(input) {
        return val;
    }

    // Try to complete the JSON by closing open brackets/braces
    let mut open_braces = 0i32;
    let mut open_brackets = 0i32;
    let mut in_string = false;
    let mut escape = false;

    for ch in input.chars() {
        if escape {
            escape = false;
            continue;
        }
        if ch == '\\' && in_string {
            escape = true;
            continue;
        }
        if ch == '"' {
            in_string = !in_string;
            continue;
        }
        if in_string {
            continue;
        }
        match ch {
            '{' => open_braces += 1,
            '}' => open_braces -= 1,
            '[' => open_brackets += 1,
            ']' => open_brackets -= 1,
            _ => {}
        }
    }

    let mut completed = input.to_string();
    if in_string {
        completed.push('"');
    }
    for _ in 0..open_brackets {
        completed.push(']');
    }
    for _ in 0..open_braces {
        completed.push('}');
    }

    serde_json::from_str::<JsonValue>(&completed).unwrap_or(serde_json::json!({}))
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Message, Api};

    #[test]
    fn test_encode_text_signature_v1() {
        let sig = encode_text_signature_v1("msg_123", None);
        assert!(sig.contains("\"v\":1"));
        assert!(sig.contains("\"id\":\"msg_123\""));
    }

    #[test]
    fn test_encode_text_signature_v1_with_phase() {
        let sig = encode_text_signature_v1("msg_456", Some("commentary"));
        assert!(sig.contains("\"phase\":\"commentary\""));
    }

    #[test]
    fn test_parse_text_signature_v1() {
        let sig = encode_text_signature_v1("msg_abc", Some("final_answer"));
        let result = parse_text_signature(Some(&sig));
        assert_eq!(
            result,
            Some(("msg_abc".to_string(), Some("final_answer".to_string())))
        );
    }

    #[test]
    fn test_parse_text_signature_legacy() {
        let result = parse_text_signature(Some("plain_id"));
        assert_eq!(result, Some(("plain_id".to_string(), None)));
    }

    #[test]
    fn test_parse_text_signature_none() {
        assert_eq!(parse_text_signature(None), None);
    }

    #[test]
    fn test_normalize_id_part() {
        assert_eq!(normalize_id_part("abc123"), "abc123");
        assert_eq!(normalize_id_part("a|b|c"), "a_b_c");
        assert_eq!(normalize_id_part(&"x".repeat(100)).len(), 64);
    }

    #[test]
    fn test_short_hash_deterministic() {
        let h1 = short_hash("hello");
        let h2 = short_hash("hello");
        assert_eq!(h1, h2);
        assert_eq!(h1.len(), 12);
    }

    #[test]
    fn test_short_hash_different_inputs() {
        let h1 = short_hash("hello");
        let h2 = short_hash("world");
        assert_ne!(h1, h2);
    }

    #[test]
    fn test_map_responses_stop_reason() {
        assert_eq!(
            map_responses_stop_reason(Some("completed")),
            crate::StopReason::Stop
        );
        assert_eq!(
            map_responses_stop_reason(Some("incomplete")),
            crate::StopReason::Length
        );
        assert_eq!(
            map_responses_stop_reason(Some("failed")),
            crate::StopReason::Error
        );
        assert_eq!(
            map_responses_stop_reason(Some("cancelled")),
            crate::StopReason::Error
        );
        assert_eq!(map_responses_stop_reason(None), crate::StopReason::Stop);
    }

    #[test]
    fn test_sanitize_surrogates() {
        assert_eq!(sanitize_surrogates("hello"), "hello");
        assert_eq!(sanitize_surrogates("hello\nworld"), "hello\nworld");
    }

    #[test]
    fn test_parse_streaming_json_empty() {
        assert_eq!(parse_streaming_json(""), serde_json::json!({}));
    }

    #[test]
    fn test_parse_streaming_json_complete() {
        let result = parse_streaming_json(r#"{"key": "value"}"#);
        assert_eq!(result["key"], "value");
    }

    #[test]
    fn test_parse_streaming_json_partial() {
        let result = parse_streaming_json(r#"{"key": "val"#);
        assert_eq!(result["key"], "val");
    }

    #[test]
    fn test_convert_responses_tools() {
        let tools = vec![Tool {
            name: "get_weather".to_string(),
            description: "Get weather".to_string(),
            parameters: serde_json::json!({"type": "object"}),
        }];
        let result = convert_responses_tools(&tools, None);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0]["type"], "function");
        assert_eq!(result[0]["name"], "get_weather");
        assert_eq!(result[0]["strict"], false);
    }

    #[test]
    fn test_convert_responses_tools_strict() {
        let tools = vec![Tool {
            name: "test".to_string(),
            description: "Test".to_string(),
            parameters: serde_json::json!({}),
        }];
        let opts = ConvertResponsesToolsOptions { strict: Some(true) };
        let result = convert_responses_tools(&tools, Some(opts));
        assert_eq!(result[0]["strict"], true);
    }

    #[test]
    fn test_convert_responses_messages_basic() {
        let model = crate::Model::new(
            "gpt-4o",
            "GPT-4o",
            Api::OpenAiResponses,
            "openai-responses",
            "https://api.openai.com/v1",
        );
        let mut context = Context::new();
        context.add_message(Message::user("Hello"));

        let result = convert_responses_messages(
            &model,
            &context,
            &[],
            Some(ConvertResponsesMessagesOptions {
                include_system_prompt: false,
            }),
        );

        assert_eq!(result.len(), 1);
        assert_eq!(result[0]["role"], "user");
    }

    #[test]
    fn test_convert_responses_messages_with_system_prompt() {
        let model = crate::Model::new(
            "gpt-4o",
            "GPT-4o",
            Api::OpenAiResponses,
            "openai-responses",
            "https://api.openai.com/v1",
        );
        let mut context = Context::new();
        context.set_system_prompt("You are helpful");

        let result = convert_responses_messages(
            &model,
            &context,
            &[],
            Some(ConvertResponsesMessagesOptions {
                include_system_prompt: true,
            }),
        );

        assert!(result.len() >= 1);
        assert_eq!(result[0]["role"], "system");
    }

    #[test]
    fn test_convert_responses_messages_reasoning_model() {
        let mut model = crate::Model::new(
            "o3",
            "o3",
            Api::OpenAiResponses,
            "openai-responses",
            "https://api.openai.com/v1",
        );
        model.reasoning = true;

        let mut context = Context::new();
        context.set_system_prompt("Think carefully");

        let result = convert_responses_messages(
            &model,
            &context,
            &[],
            Some(ConvertResponsesMessagesOptions {
                include_system_prompt: true,
            }),
        );

        assert_eq!(result[0]["role"], "developer");
    }
}