ferro-ai 0.2.63

AI structured classification and confirmation primitives for the Ferro framework
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
use crate::client::{
    CompletionRequest, CompletionResponse, LlmClient, Role, TokenStream, ToolChoice, ToolUseBlock,
};
use crate::error::Error;
use async_trait::async_trait;
use futures::{stream, StreamExt};
use reqwest_eventsource::{Event, RequestBuilderExt};

/// OpenAI Chat Completions API client.
///
/// Implements [`LlmClient`] against `{base_url}/v1/chat/completions`. Also
/// serves as the Groq client when `base_url` is set to
/// `https://api.groq.com/openai` — Groq exposes an OpenAI-compatible API.
///
/// # Authentication
///
/// Uses `Authorization: Bearer {api_key}` for all requests.
///
/// # Embeddings
///
/// `embed()` posts to `{base_url}/v1/embeddings` using model
/// `text-embedding-3-small` and extracts `data[0].embedding` as `Vec<f32>`.
pub struct OpenAiClient {
    client: reqwest::Client,
    api_key: String,
    model: Option<String>,
    base_url: String,
}

impl OpenAiClient {
    /// Create a new client.
    ///
    /// - `model`: optional model override; `None` resolves to `default_model()`.
    /// - `base_url`: optional base URL override; `None` defaults to
    ///   `https://api.openai.com`. Pass `Some("https://api.groq.com/openai")`
    ///   for Groq compatibility.
    ///
    /// The internal `reqwest::Client` uses a 60-second timeout (T-165-04).
    pub fn new(api_key: String, model: Option<String>, base_url: Option<String>) -> Self {
        let client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(60))
            .build()
            .expect("failed to build reqwest client");
        let base_url = base_url.unwrap_or_else(|| "https://api.openai.com".to_string());
        Self {
            client,
            api_key,
            model,
            base_url,
        }
    }

    /// The embedding model for `/v1/embeddings`.
    ///
    /// Reads `FERRO_AI_EMBED_MODEL`; falls back to `"text-embedding-3-small"`.
    /// Intentionally separate from `default_model()` (the chat model).
    pub(crate) fn embed_model() -> String {
        std::env::var("FERRO_AI_EMBED_MODEL")
            .unwrap_or_else(|_| "text-embedding-3-small".to_string())
    }

    /// Build the request body for the Chat Completions API.
    ///
    /// Includes `response_format.type = "json_schema"` only when
    /// `request.schema` is `Some`. Sets `"stream": stream`.
    pub(crate) fn build_body(
        &self,
        request: &CompletionRequest,
        stream: bool,
    ) -> serde_json::Value {
        let model = request
            .model_override
            .as_deref()
            .unwrap_or_else(|| self.default_model());

        let messages: Vec<serde_json::Value> = request
            .messages
            .iter()
            .map(|m| match m.role {
                Role::Tool => {
                    // OpenAI wire format: role "tool" with tool_call_id as a real field.
                    // tool_call_id must not be embedded in the content string.
                    let call_id = m.tool_call_id.as_deref().unwrap_or("");
                    serde_json::json!({
                        "role": "tool",
                        "tool_call_id": call_id,
                        "content": m.content,
                    })
                }
                Role::User => serde_json::json!({"role": "user", "content": m.content}),
                Role::Assistant => {
                    serde_json::json!({"role": "assistant", "content": m.content})
                }
            })
            .collect();

        let mut body = serde_json::json!({
            "model": model,
            "messages": messages,
            "max_tokens": request.max_tokens,
            "stream": stream,
        });

        if let Some(schema) = &request.schema {
            body["response_format"] = serde_json::json!({
                "type": "json_schema",
                "json_schema": {
                    "name": "output",
                    "schema": schema,
                    "strict": true,
                }
            });
        }

        if let Some(tools) = &request.tools {
            let tools_json: Vec<serde_json::Value> = tools
                .iter()
                .map(|t| {
                    serde_json::json!({
                        "type": "function",
                        "function": {
                            "name": t.name,
                            "description": t.description,
                            "parameters": t.parameters_schema,
                            "strict": true,
                        }
                    })
                })
                .collect();
            body["tools"] = serde_json::Value::Array(tools_json);
            // WR-01: honor request.tool_choice; default to "auto" when not specified.
            body["tool_choice"] = match request.tool_choice.as_ref() {
                Some(ToolChoice::None) => serde_json::json!("none"),
                Some(ToolChoice::Auto) | None => serde_json::json!("auto"),
            };
        }

        body
    }
}

/// Parse tool_calls from an OpenAI response into [`ToolUseBlock`]s.
pub(crate) fn parse_openai_tool_calls(json: &serde_json::Value) -> Vec<ToolUseBlock> {
    let Some(tool_calls) = json["choices"][0]["message"]["tool_calls"].as_array() else {
        return vec![];
    };
    tool_calls
        .iter()
        .filter_map(|c| {
            Some(ToolUseBlock {
                id: c["id"].as_str()?.to_string(),
                name: c["function"]["name"].as_str()?.to_string(),
                input: serde_json::from_str(c["function"]["arguments"].as_str()?).ok()?,
            })
        })
        .collect()
}

/// Result of parsing a single OpenAI SSE chunk data string.
#[derive(Debug, PartialEq)]
pub(crate) enum OpenAiDelta {
    /// The stream is terminated (`data: [DONE]`).
    Done,
    /// A non-empty text token.
    Token(String),
    /// Empty delta or role-only chunk — skip.
    Skip,
}

/// Parse one OpenAI SSE chunk data string.
///
/// Handles the `[DONE]` sentinel (Pitfall 4) before JSON-parsing.
/// Returns `Done` on termination, `Token(text)` for content, `Skip` otherwise.
pub(crate) fn parse_openai_delta(data: &str) -> OpenAiDelta {
    if data == "[DONE]" {
        return OpenAiDelta::Done;
    }
    let Ok(v) = serde_json::from_str::<serde_json::Value>(data) else {
        return OpenAiDelta::Skip;
    };
    // Terminate on finish_reason being non-null
    if !v["choices"][0]["finish_reason"].is_null() {
        if let Some(reason) = v["choices"][0]["finish_reason"].as_str() {
            if !reason.is_empty() {
                return OpenAiDelta::Done;
            }
        }
    }
    match v["choices"][0]["delta"]["content"].as_str() {
        Some(text) if !text.is_empty() => OpenAiDelta::Token(text.to_string()),
        _ => OpenAiDelta::Skip,
    }
}

/// Parse the embeddings response, extracting `data[0].embedding` as `Vec<f32>`.
pub(crate) fn parse_embedding(json: &serde_json::Value) -> Result<Vec<f32>, Error> {
    json["data"][0]["embedding"]
        .as_array()
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_f64().map(|f| f as f32))
                .collect()
        })
        .ok_or_else(|| Error::Deserialization("no embedding in response".into()))
}

#[async_trait]
impl LlmClient for OpenAiClient {
    fn default_model(&self) -> &str {
        self.model.as_deref().unwrap_or("gpt-4o")
    }

    async fn complete(&self, request: CompletionRequest) -> Result<String, Error> {
        let body = self.build_body(&request, false);

        let resp = self
            .client
            .post(format!("{}/v1/chat/completions", self.base_url))
            .bearer_auth(&self.api_key)
            .json(&body)
            .send()
            .await
            .map_err(|e| {
                if e.is_timeout() {
                    Error::Timeout
                } else {
                    Error::Provider {
                        status: None,
                        message: e.to_string(),
                    }
                }
            })?;

        let status = resp.status().as_u16();
        if !resp.status().is_success() {
            let text = resp.text().await.unwrap_or_default();
            return Err(Error::Provider {
                status: Some(status),
                message: text,
            });
        }

        let json: serde_json::Value = resp
            .json()
            .await
            .map_err(|e| Error::Deserialization(e.to_string()))?;

        json["choices"][0]["message"]["content"]
            .as_str()
            .map(|s| s.to_string())
            .ok_or_else(|| Error::Deserialization("no content in response".into()))
    }

    async fn complete_stream(&self, request: CompletionRequest) -> Result<TokenStream, Error> {
        let body = self.build_body(&request, true);

        let builder = self
            .client
            .post(format!("{}/v1/chat/completions", self.base_url))
            .bearer_auth(&self.api_key)
            .json(&body);

        let es = builder.eventsource().map_err(|_| Error::Provider {
            status: None,
            message: "request not cloneable".into(),
        })?;

        let token_stream = stream::unfold(es, |mut es| async move {
            loop {
                match es.next().await {
                    None => return None,
                    Some(Ok(Event::Open)) => continue,
                    Some(Ok(Event::Message(msg))) => match parse_openai_delta(&msg.data) {
                        OpenAiDelta::Done => {
                            es.close();
                            return None;
                        }
                        OpenAiDelta::Token(text) => return Some((Ok(text), es)),
                        OpenAiDelta::Skip => continue,
                    },
                    Some(Err(e)) => {
                        es.close();
                        return Some((
                            Err(Error::Provider {
                                status: None,
                                message: e.to_string(),
                            }),
                            es,
                        ));
                    }
                }
            }
        });

        Ok(Box::pin(token_stream))
    }

    async fn embed(&self, text: &str) -> Result<Vec<f32>, Error> {
        let body = serde_json::json!({
            "model": Self::embed_model(),
            "input": text,
        });

        let resp = self
            .client
            .post(format!("{}/v1/embeddings", self.base_url))
            .bearer_auth(&self.api_key)
            .json(&body)
            .send()
            .await
            .map_err(|e| {
                if e.is_timeout() {
                    Error::Timeout
                } else {
                    Error::Provider {
                        status: None,
                        message: e.to_string(),
                    }
                }
            })?;

        let status = resp.status().as_u16();
        if !resp.status().is_success() {
            let text = resp.text().await.unwrap_or_default();
            return Err(Error::Provider {
                status: Some(status),
                message: text,
            });
        }

        let json: serde_json::Value = resp
            .json()
            .await
            .map_err(|e| Error::Deserialization(e.to_string()))?;

        parse_embedding(&json)
    }

    async fn complete_with_tools(
        &self,
        request: CompletionRequest,
    ) -> Result<CompletionResponse, Error> {
        let body = self.build_body(&request, false);

        let resp = self
            .client
            .post(format!("{}/v1/chat/completions", self.base_url))
            .bearer_auth(&self.api_key)
            .json(&body)
            .send()
            .await
            .map_err(|e| {
                if e.is_timeout() {
                    Error::Timeout
                } else {
                    Error::Provider {
                        status: None,
                        message: e.to_string(),
                    }
                }
            })?;

        let status = resp.status().as_u16();
        if !resp.status().is_success() {
            let text = resp.text().await.unwrap_or_default();
            return Err(Error::Provider {
                status: Some(status),
                message: text,
            });
        }

        let json: serde_json::Value = resp
            .json()
            .await
            .map_err(|e| Error::Deserialization(e.to_string()))?;

        let finish_reason = json["choices"][0]["finish_reason"].as_str().unwrap_or("");
        if finish_reason == "tool_calls" {
            let blocks = parse_openai_tool_calls(&json);
            let assistant_content = json["choices"][0]["message"]["tool_calls"].to_string();
            return Ok(CompletionResponse::ToolUse {
                blocks,
                assistant_content,
            });
        }

        // stop or any other finish_reason → extract text content
        let text = json["choices"][0]["message"]["content"]
            .as_str()
            .map(|s| s.to_string())
            .ok_or_else(|| Error::Deserialization("no content in response".into()))?;

        Ok(CompletionResponse::Text(text))
    }
}

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

    #[test]
    fn test_openai_default_model() {
        let client = OpenAiClient::new("k".into(), None, None);
        assert_eq!(client.default_model(), "gpt-4o");
    }

    #[test]
    fn test_openai_default_base_url() {
        let client = OpenAiClient::new("k".into(), None, None);
        assert_eq!(client.base_url, "https://api.openai.com");
    }

    #[test]
    fn test_openai_groq_base_url() {
        let client =
            OpenAiClient::new("k".into(), None, Some("https://api.groq.com/openai".into()));
        assert_eq!(client.base_url, "https://api.groq.com/openai");
    }

    #[test]
    fn test_build_body_response_format_with_schema() {
        let client = OpenAiClient::new("k".into(), None, None);
        let schema = serde_json::json!({"type": "object", "properties": {"x": {"type": "string"}}});
        let request = CompletionRequest {
            system: None,
            messages: vec![Message {
                role: Role::User,
                content: "hi".into(),
                tool_call_id: None,
            }],
            max_tokens: 100,
            model_override: None,
            schema: Some(schema.clone()),
            tools: None,
            tool_choice: None,
        };
        let body = client.build_body(&request, false);

        assert_eq!(body["response_format"]["type"], "json_schema");
        assert_eq!(body["response_format"]["json_schema"]["name"], "output");
        assert_eq!(body["response_format"]["json_schema"]["schema"], schema);
        assert_eq!(body["response_format"]["json_schema"]["strict"], true);
    }

    #[test]
    fn test_build_body_no_response_format_without_schema() {
        let client = OpenAiClient::new("k".into(), None, None);
        let request = CompletionRequest {
            system: None,
            messages: vec![Message {
                role: Role::User,
                content: "hi".into(),
                tool_call_id: None,
            }],
            max_tokens: 100,
            model_override: None,
            schema: None,
            tools: None,
            tool_choice: None,
        };
        let body = client.build_body(&request, false);
        assert!(body.get("response_format").is_none());
    }

    #[test]
    fn test_parse_openai_delta_done() {
        assert_eq!(parse_openai_delta("[DONE]"), OpenAiDelta::Done);
    }

    #[test]
    fn test_parse_openai_delta_token() {
        // Fixture from RESEARCH line 635
        let data = r#"{"id":"chatcmpl-1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}"#;
        assert_eq!(
            parse_openai_delta(data),
            OpenAiDelta::Token("Hello".to_string())
        );
    }

    #[test]
    fn test_parse_openai_delta_skip_empty_content() {
        let data = r#"{"choices":[{"index":0,"delta":{"role":"assistant","content":null},"finish_reason":null}]}"#;
        assert_eq!(parse_openai_delta(data), OpenAiDelta::Skip);
    }

    #[test]
    fn test_parse_openai_delta_finish_reason() {
        let data = r#"{"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}"#;
        assert_eq!(parse_openai_delta(data), OpenAiDelta::Done);
    }

    #[test]
    fn test_parse_embedding() {
        let json = serde_json::json!({
            "data": [{"embedding": [0.1, -0.2, 0.3], "index": 0}],
            "usage": {}
        });
        let result = parse_embedding(&json).unwrap();
        assert_eq!(result.len(), 3);
        assert!((result[0] - 0.1f32).abs() < 1e-6);
        assert!((result[1] - (-0.2f32)).abs() < 1e-6);
        assert!((result[2] - 0.3f32).abs() < 1e-6);
    }

    #[test]
    fn test_parse_embedding_missing() {
        let json = serde_json::json!({"data": []});
        assert!(matches!(
            parse_embedding(&json),
            Err(Error::Deserialization(_))
        ));
    }

    #[test]
    fn test_openai_is_object_safe() {
        let _: Box<dyn LlmClient> = Box::new(OpenAiClient::new("k".into(), None, None));
    }

    /// CR-03 regression: OpenAI tool result messages must include `tool_call_id` as a
    /// real top-level field. The id must not be embedded inside the content string.
    #[test]
    fn test_build_body_tool_result_wire_format() {
        let client = OpenAiClient::new("k".into(), None, None);
        let request = CompletionRequest {
            system: None,
            messages: vec![
                Message {
                    role: Role::User,
                    content: "what is 2+2?".into(),
                    tool_call_id: None,
                },
                Message {
                    role: Role::Tool,
                    content: "4".into(),
                    tool_call_id: Some("call_abc123".into()),
                },
            ],
            max_tokens: 100,
            model_override: None,
            schema: None,
            tools: None,
            tool_choice: None,
        };
        let body = client.build_body(&request, false);
        let msgs = body["messages"].as_array().expect("messages must be array");
        assert_eq!(msgs.len(), 2);

        let tool_msg = &msgs[1];
        assert_eq!(tool_msg["role"], "tool");
        assert_eq!(
            tool_msg["tool_call_id"], "call_abc123",
            "tool_call_id must be a real top-level field"
        );
        assert_eq!(tool_msg["content"], "4");
        // The id must not also appear embedded in the content string.
        assert!(
            !tool_msg["content"]
                .as_str()
                .unwrap_or("")
                .contains("call_abc123"),
            "tool_call_id must not be embedded in content"
        );
    }

    /// WR-01 regression: OpenAI build_body must honor request.tool_choice.
    #[test]
    fn test_build_body_tool_choice_none() {
        use crate::client::{ToolChoice, ToolRequest};

        let client = OpenAiClient::new("k".into(), None, None);
        let request = CompletionRequest {
            system: None,
            messages: vec![Message {
                role: Role::User,
                content: "hi".into(),
                tool_call_id: None,
            }],
            max_tokens: 100,
            model_override: None,
            schema: None,
            tools: Some(vec![ToolRequest {
                name: "my_tool".into(),
                description: "does stuff".into(),
                parameters_schema: serde_json::json!({"type": "object"}),
            }]),
            tool_choice: Some(ToolChoice::None),
        };
        let body = client.build_body(&request, false);
        assert_eq!(
            body["tool_choice"], "none",
            "ToolChoice::None must emit tool_choice: 'none'"
        );
    }

    /// WR-01: Auto tool_choice (explicit) and default (None) both emit "auto".
    #[test]
    fn test_build_body_tool_choice_auto() {
        use crate::client::{ToolChoice, ToolRequest};

        let client = OpenAiClient::new("k".into(), None, None);
        let tools = Some(vec![ToolRequest {
            name: "my_tool".into(),
            description: "does stuff".into(),
            parameters_schema: serde_json::json!({"type": "object"}),
        }]);

        // Explicit Auto.
        let req_auto = CompletionRequest {
            system: None,
            messages: vec![Message {
                role: Role::User,
                content: "hi".into(),
                tool_call_id: None,
            }],
            max_tokens: 100,
            model_override: None,
            schema: None,
            tools: tools.clone(),
            tool_choice: Some(ToolChoice::Auto),
        };
        let body = client.build_body(&req_auto, false);
        assert_eq!(body["tool_choice"], "auto");

        // Default None → also "auto".
        let req_default = CompletionRequest {
            tool_choice: None,
            ..req_auto
        };
        let body2 = client.build_body(&req_default, false);
        assert_eq!(body2["tool_choice"], "auto");
    }

    #[test]
    fn embed_model_default_is_text_embedding_3_small() {
        let _g = crate::ENV_LOCK.lock().unwrap();
        std::env::remove_var("FERRO_AI_EMBED_MODEL");
        assert_eq!(OpenAiClient::embed_model(), "text-embedding-3-small");
    }

    #[test]
    fn embed_model_from_env() {
        let _g = crate::ENV_LOCK.lock().unwrap();
        std::env::set_var("FERRO_AI_EMBED_MODEL", "text-embedding-ada-002");
        assert_eq!(OpenAiClient::embed_model(), "text-embedding-ada-002");
        std::env::remove_var("FERRO_AI_EMBED_MODEL");
    }
}