Skip to main content

agent_framework_openai/
responses.rs

1//! [`OpenAIChatClient`]: a [`ChatClient`] for the OpenAI Responses API
2//! (`POST /v1/responses`).
3//!
4//! The Responses API uses an item-based `input`/`output` shape rather than
5//! the `messages` array used by Chat Completions, and supports a dedicated
6//! `previous_response_id` for service-side conversation state. Wire framing
7//! (SSE parsing style, error handling) mirrors [`crate::OpenAIChatCompletionClient`].
8//!
9//! ```no_run
10//! use agent_framework_openai::responses::OpenAIChatClient;
11//! use agent_framework_core::prelude::*;
12//!
13//! # async fn demo() -> Result<()> {
14//! let client = OpenAIChatClient::new("sk-...", "gpt-4o-mini");
15//! let agent = Agent::builder(client)
16//!     .instructions("You are concise.")
17//!     .build();
18//! let reply = agent.run_once("Say hi").await?;
19//! println!("{}", reply.text());
20//! # Ok(())
21//! # }
22//! ```
23
24use std::collections::{HashMap, VecDeque};
25use std::sync::Arc;
26
27use agent_framework_core::client::{ChatClient, ChatStream};
28use agent_framework_core::error::{Error, Result};
29use agent_framework_core::streaming::Utf8StreamDecoder;
30use agent_framework_core::tools::ToolDefinition;
31use agent_framework_core::types::{
32    Annotation, ChatOptions, ChatResponse, ChatResponseUpdate, Content, DataContent, FinishReason,
33    FunctionApprovalRequestContent, FunctionArguments, FunctionCallContent, FunctionResultContent,
34    Message, ResponseFormat, Role, TextContent, TextReasoningContent, TextSpanRegion, ToolMode,
35    UriContent, UsageContent, UsageDetails,
36};
37use futures::StreamExt;
38use serde_json::{json, Map, Value};
39
40use crate::convert::{
41    audio_format, data_content_media_type, function_arguments_to_string, result_to_string,
42    top_level_media_type, DEFAULT_FILENAME,
43};
44use crate::{ByteStream, DEFAULT_BASE_URL};
45
46/// An OpenAI Responses API chat client (`POST /v1/responses`).
47#[derive(Clone)]
48pub struct OpenAIChatClient {
49    inner: Arc<Inner>,
50}
51
52#[derive(Clone)]
53struct Inner {
54    http: reqwest::Client,
55    api_key: String,
56    base_url: String,
57    model: String,
58    organization: Option<String>,
59}
60
61impl std::fmt::Debug for OpenAIChatClient {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        f.debug_struct("OpenAIChatClient")
64            .field("base_url", &self.inner.base_url)
65            .field("model", &self.inner.model)
66            .field("organization", &self.inner.organization)
67            .finish_non_exhaustive()
68    }
69}
70
71impl OpenAIChatClient {
72    /// Create a client for the given API key and default model.
73    pub fn new(api_key: impl Into<String>, model: impl Into<String>) -> Self {
74        Self {
75            inner: Arc::new(Inner {
76                http: reqwest::Client::new(),
77                api_key: api_key.into(),
78                base_url: DEFAULT_BASE_URL.to_string(),
79                model: model.into(),
80                organization: None,
81            }),
82        }
83    }
84
85    /// Build a client from the `OPENAI_API_KEY` (and optional
86    /// `OPENAI_BASE_URL`) environment variables.
87    pub fn from_env(model: impl Into<String>) -> Result<Self> {
88        let key = std::env::var("OPENAI_API_KEY")
89            .map_err(|_| Error::Configuration("OPENAI_API_KEY is not set".into()))?;
90        let mut client = Self::new(key, model);
91        if let Ok(base) = std::env::var("OPENAI_BASE_URL") {
92            client = client.with_base_url(base);
93        }
94        Ok(client)
95    }
96
97    /// Override the base URL (for Azure OpenAI or compatible servers).
98    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
99        Arc::make_mut(&mut self.inner).base_url = base_url.into();
100        self
101    }
102
103    /// Set the organization header.
104    pub fn with_organization(mut self, org: impl Into<String>) -> Self {
105        Arc::make_mut(&mut self.inner).organization = Some(org.into());
106        self
107    }
108
109    /// The default model id.
110    pub fn model(&self) -> &str {
111        &self.inner.model
112    }
113
114    fn build_body(&self, messages: &[Message], options: &ChatOptions, stream: bool) -> Value {
115        let mut body = Map::new();
116        let model = options
117            .model
118            .clone()
119            .unwrap_or_else(|| self.inner.model.clone());
120        body.insert("model".into(), json!(model));
121
122        let (instructions, rest) = extract_instructions(messages, options.instructions.as_deref());
123        if let Some(instructions) = instructions {
124            body.insert("instructions".into(), json!(instructions));
125        }
126        body.insert("input".into(), json!(messages_to_input(rest)));
127
128        if let Some(conversation_id) = &options.conversation_id {
129            body.insert("previous_response_id".into(), json!(conversation_id));
130        }
131        if let Some(t) = options.temperature {
132            body.insert("temperature".into(), json!(t));
133        }
134        if let Some(t) = options.top_p {
135            body.insert("top_p".into(), json!(t));
136        }
137        if let Some(mt) = options.max_tokens {
138            body.insert("max_output_tokens".into(), json!(mt));
139        }
140        if let Some(store) = options.store {
141            body.insert("store".into(), json!(store));
142        }
143        if let Some(user) = &options.user {
144            body.insert("user".into(), json!(user));
145        }
146        if let Some(metadata) = &options.metadata {
147            body.insert("metadata".into(), json!(metadata));
148        }
149
150        if !options.tools.is_empty() {
151            let tools: Vec<Value> = options.tools.iter().map(tool_to_responses_spec).collect();
152            body.insert("tools".into(), json!(tools));
153            if let Some(allow_multi) = options.allow_multiple_tool_calls {
154                body.insert("parallel_tool_calls".into(), json!(allow_multi));
155            }
156        }
157        if let Some(tool_choice) = &options.tool_choice {
158            body.insert("tool_choice".into(), tool_choice_to_responses(tool_choice));
159        }
160        if let Some(fmt) = &options.response_format {
161            body.insert(
162                "text".into(),
163                json!({ "format": response_format_to_text(fmt) }),
164            );
165        }
166
167        // Inserted before the `additional_properties` pass below, whose
168        // A caller's own `include` entries are already folded in here.
169        if let Some(include) = responses_include(options, true) {
170            body.insert("include".into(), include);
171        }
172
173        for (k, v) in &options.additional_properties {
174            // `include` belongs to `responses_include` alone. Letting it
175            // through would resurrect a value that function deliberately
176            // dropped — an explicit empty array would land as `include: []`,
177            // the one shape it exists to avoid.
178            if k == "include" {
179                continue;
180            }
181            body.entry(k.clone()).or_insert_with(|| v.clone());
182        }
183
184        if stream {
185            body.insert("stream".into(), json!(true));
186        }
187        Value::Object(body)
188    }
189
190    async fn post(&self, body: &Value) -> Result<reqwest::Response> {
191        let url = format!("{}/responses", self.inner.base_url.trim_end_matches('/'));
192        let mut req = self
193            .inner
194            .http
195            .post(&url)
196            .bearer_auth(&self.inner.api_key)
197            .json(body);
198        if let Some(org) = &self.inner.organization {
199            req = req.header("OpenAI-Organization", org);
200        }
201        let resp = req
202            .send()
203            .await
204            .map_err(|e| Error::service(format!("request failed: {e}")))?;
205        if !resp.status().is_success() {
206            let status = resp.status();
207            let retry_after = crate::parse_retry_after(resp.headers());
208            let text = resp.text().await.unwrap_or_default();
209            return Err(crate::classify_service_error(
210                status.as_u16(),
211                &text,
212                format!("OpenAI API error {status}: {text}"),
213                retry_after,
214            ));
215        }
216        Ok(resp)
217    }
218}
219
220#[async_trait::async_trait]
221impl ChatClient for OpenAIChatClient {
222    async fn get_response(
223        &self,
224        messages: Vec<Message>,
225        options: ChatOptions,
226    ) -> Result<ChatResponse> {
227        let body = self.build_body(&messages, &options, false);
228        let resp = self.post(&body).await?;
229        let value: Value = resp
230            .json()
231            .await
232            .map_err(|e| Error::service(format!("invalid response json: {e}")))?;
233        if let Some(err) = response_failure_error(&value) {
234            return Err(err);
235        }
236        Ok(parse_response(&value, options.store))
237    }
238
239    async fn get_streaming_response(
240        &self,
241        messages: Vec<Message>,
242        options: ChatOptions,
243    ) -> Result<ChatStream> {
244        let body = self.build_body(&messages, &options, true);
245        let resp = self.post(&body).await?;
246        Ok(parse_responses_sse_stream(resp, options.store).boxed())
247    }
248
249    fn model(&self) -> Option<&str> {
250        Some(&self.inner.model)
251    }
252}
253
254// region: request conversion
255
256/// The `include` entry that asks a reasoning model to return its *encrypted*
257/// reasoning item alongside the summary.
258pub const ENCRYPTED_REASONING_INCLUDE: &str = "reasoning.encrypted_content";
259
260/// Build the Responses request's `include` array.
261///
262/// A reasoning item must be replayed verbatim on the follow-up turn of a tool
263/// loop, and the service only accepts it when it carries its `id` *and*
264/// `encrypted_content`. When the conversation is held service-side the model
265/// already has that item, but on a stateless request nothing is stored — so
266/// unless the request asks for `reasoning.encrypted_content`, the item comes
267/// back without it and the replay in [`messages_to_input`] has nothing valid
268/// to re-send. This adds it implicitly in exactly that case, mirroring
269/// upstream's `_prepare_options`.
270///
271/// A caller's own `include` entries (passed through
272/// `ChatOptions::additional_properties`) are always preserved, and an explicit
273/// `reasoning.encrypted_content` is honored even when `implicit` is `false` —
274/// `implicit` gates only whether it is added on the caller's behalf. Foundry
275/// passes `false` here: it does not want encrypted reasoning unless asked for
276/// it by name (upstream #7536).
277///
278/// Returns `None` when the array would be empty, so a request that needs no
279/// `include` does not carry an empty one.
280///
281/// `pub` for the same reason as [`extract_instructions`]: `agent-framework-azure`'s
282/// Responses client builds its own body and reuses this step rather than
283/// reimplementing it.
284pub fn responses_include(options: &ChatOptions, implicit: bool) -> Option<Value> {
285    let mut include: Vec<Value> = options
286        .additional_properties
287        .get("include")
288        .and_then(Value::as_array)
289        .cloned()
290        .unwrap_or_default();
291
292    // Upstream keys this off the service-side-storage indicators, not `store`:
293    // a request continuing a stored conversation needs nothing echoed back.
294    let uses_service_side_storage = options
295        .conversation_id
296        .as_deref()
297        .is_some_and(|id| !id.is_empty());
298
299    let already_present = include
300        .iter()
301        .any(|v| v.as_str() == Some(ENCRYPTED_REASONING_INCLUDE));
302    if implicit && !uses_service_side_storage && !already_present {
303        include.push(json!(ENCRYPTED_REASONING_INCLUDE));
304    }
305
306    (!include.is_empty()).then(|| json!(include))
307}
308
309/// Split a leading system message (and/or `ChatOptions::instructions`) out
310/// into the Responses API's top-level `instructions` field, returning the
311/// remaining messages to convert into `input` items.
312///
313/// `pub` (rather than private) so `agent-framework-azure`'s Responses client
314/// can reuse this exact instructions-extraction step ahead of
315/// [`messages_to_input`] when building the Azure OpenAI Responses request
316/// body, instead of reimplementing it.
317pub fn extract_instructions<'a>(
318    messages: &'a [Message],
319    options_instructions: Option<&str>,
320) -> (Option<String>, &'a [Message]) {
321    let mut parts = Vec::new();
322    if let Some(instr) = options_instructions {
323        if !instr.is_empty() {
324            parts.push(instr.to_string());
325        }
326    }
327    let mut rest = messages;
328    if let Some(first) = messages.first() {
329        if first.role == Role::system() {
330            let text = first.text();
331            if !text.is_empty() {
332                parts.push(text);
333            }
334            rest = &messages[1..];
335        }
336    }
337    if parts.is_empty() {
338        (None, rest)
339    } else {
340        (Some(parts.join("\n\n")), rest)
341    }
342}
343
344/// Convert framework messages into the Responses API's `input` item array.
345///
346/// `pub` so `agent-framework-azure`'s Responses client can reuse this
347/// conversion verbatim rather than reimplementing it (Azure OpenAI's
348/// Responses API shares the exact same `input` item wire shape).
349pub fn messages_to_input(messages: &[Message]) -> Vec<Value> {
350    let mut out = Vec::new();
351    for msg in messages {
352        let role = msg.role.as_str();
353        if role == Role::TOOL {
354            for content in &msg.contents {
355                if let Content::FunctionResult(fr) = content {
356                    out.push(function_result_to_item(fr));
357                }
358            }
359            continue;
360        }
361
362        let mut buffered: Vec<Value> = Vec::new();
363        for content in &msg.contents {
364            match content {
365                // A refusal is its own output content part — `{"type":
366                // "refusal", "refusal": "..."}` — sitting beside `output_text`
367                // in an output message. Re-emitting it as `output_text` would
368                // replay a decline to the provider as an answer. Only
369                // meaningful on the assistant side: an *input* message's
370                // content list admits only input_text/input_image/input_file,
371                // so a refusal on any other role has no wire form and is
372                // dropped rather than mis-encoded.
373                Content::Text(t) if t.refusal => {
374                    if role == Role::ASSISTANT {
375                        buffered.push(json!({ "type": "refusal", "refusal": t.text }));
376                    }
377                }
378                Content::Text(t) => {
379                    let text_type = if role == Role::ASSISTANT {
380                        "output_text"
381                    } else {
382                        "input_text"
383                    };
384                    buffered.push(json!({ "type": text_type, "text": t.text }));
385                }
386                Content::Uri(u) => {
387                    if let Some(part) = content_to_input_part(&u.uri, Some(&u.media_type)) {
388                        buffered.push(part);
389                    }
390                }
391                Content::Data(d) => {
392                    if let Some(part) =
393                        content_to_input_part(&d.uri, data_content_media_type(d).as_deref())
394                    {
395                        buffered.push(part);
396                    }
397                }
398                Content::HostedFile(h) => {
399                    buffered.push(json!({ "type": "input_file", "file_id": h.file_id }));
400                }
401                Content::FunctionCall(fc) => {
402                    flush_text(&mut out, &mut buffered, role);
403                    out.push(json!({
404                        "type": "function_call",
405                        "call_id": fc.call_id,
406                        "name": fc.name,
407                        "arguments": function_arguments_to_string(&fc.arguments),
408                    }));
409                }
410                Content::FunctionResult(fr) => {
411                    flush_text(&mut out, &mut buffered, role);
412                    out.push(function_result_to_item(fr));
413                }
414                Content::FunctionApprovalResponse(r) => {
415                    flush_text(&mut out, &mut buffered, role);
416                    out.push(json!({
417                        "type": "mcp_approval_response",
418                        "approval_request_id": r.id,
419                        "approve": r.approved,
420                    }));
421                }
422                Content::FunctionApprovalRequest(r) => {
423                    flush_text(&mut out, &mut buffered, role);
424                    out.push(json!({
425                        "type": "mcp_approval_request",
426                        "id": r.id,
427                        "name": r.function_call.name,
428                        "arguments": function_arguments_to_string(&r.function_call.arguments),
429                    }));
430                }
431                Content::TextReasoning(tr) => {
432                    // Re-emit the original reasoning item verbatim (store:false
433                    // replay). A summary-only reasoning content with no
434                    // preserved item has no valid input form (it lacks the
435                    // required id/encrypted_content), so it is dropped.
436                    if let Some(raw) = &tr.raw_representation {
437                        flush_text(&mut out, &mut buffered, role);
438                        out.push(raw.clone());
439                    }
440                }
441                _ => {}
442            }
443        }
444        flush_text(&mut out, &mut buffered, role);
445    }
446    out
447}
448
449/// Map a URI/data content item to a Responses API input content part, or `None`
450/// when it has no wire mapping (mirrors upstream `_openai_content_parser`).
451/// Handles images (`input_image`), audio (`input_audio`), and `application/*`
452/// data (`input_file`).
453fn content_to_input_part(uri: &str, media_type: Option<&str>) -> Option<Value> {
454    let media_type = media_type?;
455    match top_level_media_type(media_type).as_str() {
456        // `detail` defaults to "auto"; the Rust content types carry no override.
457        "image" => Some(json!({
458            "type": "input_image",
459            "image_url": uri,
460            "detail": "auto",
461        })),
462        "audio" => {
463            let format = audio_format(media_type)?;
464            // `input_audio.data` is the raw base64 payload, not a data URI —
465            // same wire rule as the Chat Completions converter.
466            Some(json!({
467                "type": "input_audio",
468                "input_audio": {
469                    "data": crate::convert::strip_data_uri_prefix(uri),
470                    "format": format,
471                },
472            }))
473        }
474        "application" => Some(json!({
475            "type": "input_file",
476            "file_data": uri,
477            "filename": DEFAULT_FILENAME,
478        })),
479        _ => None,
480    }
481}
482
483fn flush_text(out: &mut Vec<Value>, buffered: &mut Vec<Value>, role: &str) {
484    if !buffered.is_empty() {
485        out.push(json!({ "type": "message", "role": role, "content": std::mem::take(buffered) }));
486    }
487}
488
489fn function_result_to_item(fr: &FunctionResultContent) -> Value {
490    json!({
491        "type": "function_call_output",
492        "call_id": fr.call_id,
493        "output": result_to_string(fr),
494    })
495}
496
497/// The flat Responses-API tool spec: `{"type":"function","name":...}`, unlike
498/// Chat Completions' `{"type":"function","function":{...}}` nesting.
499///
500/// `pub` so `agent-framework-azure`'s Responses client can reuse this
501/// mapping rather than reimplementing it.
502pub fn tool_to_responses_spec(tool: &ToolDefinition) -> Value {
503    use agent_framework_core::tools::ToolKind;
504    match &tool.kind {
505        ToolKind::HostedWebSearch => {
506            let mut spec = Map::new();
507            spec.insert("type".into(), json!("web_search"));
508            if let Some(loc) = tool.parameters.get("user_location") {
509                spec.insert("user_location".into(), user_location_to_responses(loc));
510            }
511            Value::Object(spec)
512        }
513        ToolKind::HostedCodeInterpreter => {
514            let mut spec = Map::new();
515            spec.insert("type".into(), json!("code_interpreter"));
516            // A caller-supplied `container` wins; otherwise default to `auto`
517            // and attach any `file_ids` (`_responses_client.py:264-278`).
518            if let Some(container) = tool.parameters.get("container") {
519                spec.insert("container".into(), container.clone());
520            } else {
521                let mut container = Map::new();
522                container.insert("type".into(), json!("auto"));
523                if let Some(file_ids) = tool.parameters.get("file_ids") {
524                    container.insert("file_ids".into(), file_ids.clone());
525                }
526                spec.insert("container".into(), Value::Object(container));
527            }
528            Value::Object(spec)
529        }
530        ToolKind::HostedFileSearch { max_results } => {
531            let mut spec = Map::new();
532            spec.insert("type".into(), json!("file_search"));
533            // The Responses API requires vector-store ids; the marker itself
534            // carries none, so honor ids supplied via the definition's
535            // parameters object when present.
536            if let Some(ids) = tool.parameters.get("vector_store_ids") {
537                spec.insert("vector_store_ids".into(), ids.clone());
538            }
539            // Prefer the marker's `max_results`; fall back to a parameters key.
540            let max = (*max_results)
541                .map(|n| json!(n))
542                .or_else(|| tool.parameters.get("max_results").cloned());
543            if let Some(n) = max {
544                spec.insert("max_num_results".into(), n);
545            }
546            Value::Object(spec)
547        }
548        ToolKind::HostedMcp { url, allowed_tools } => {
549            let mut spec = Map::new();
550            spec.insert("type".into(), json!("mcp"));
551            spec.insert("server_label".into(), json!(tool.name.replace(' ', "_")));
552            spec.insert("server_url".into(), json!(url));
553            if !tool.description.is_empty() {
554                spec.insert("server_description".into(), json!(tool.description));
555            }
556            if let Some(headers) = tool.parameters.get("headers") {
557                spec.insert("headers".into(), headers.clone());
558            }
559            if let Some(allowed) = allowed_tools {
560                spec.insert("allowed_tools".into(), json!(allowed));
561            }
562            spec.insert("require_approval".into(), mcp_require_approval(tool));
563            Value::Object(spec)
564        }
565        ToolKind::HostedImageGeneration => {
566            let mut spec = Map::new();
567            spec.insert("type".into(), json!("image_generation"));
568            // Pass through any caller-supplied generation options (size,
569            // quality, background, …) carried on the marker's parameters.
570            if let Value::Object(params) = &tool.parameters {
571                for (k, v) in params {
572                    if k != "type" && k != "properties" {
573                        spec.insert(k.clone(), v.clone());
574                    }
575                }
576            }
577            Value::Object(spec)
578        }
579        ToolKind::Function => json!({
580            "type": "function",
581            "name": tool.name,
582            "description": tool.description,
583            "parameters": tool.parameters,
584        }),
585    }
586}
587
588/// Build the Responses `web_search.user_location` object from a hosted-tool
589/// `user_location` parameter (`_responses_client.py:310-329`).
590fn user_location_to_responses(location: &Value) -> Value {
591    let mut loc = Map::new();
592    loc.insert("type".into(), json!("approximate"));
593    for key in ["city", "country", "region", "timezone"] {
594        if let Some(v) = location.get(key) {
595            loc.insert(key.into(), v.clone());
596        }
597    }
598    Value::Object(loc)
599}
600
601/// Build the Responses MCP `require_approval` value. A `parameters.approval_mode`
602/// override — either the string `"always_require"`/`"never_require"` or an
603/// object `{"always": [...], "never": [...]}` — takes precedence over the
604/// definition's [`ApprovalMode`]; mirrors `get_mcp_tool`
605/// (`_responses_client.py:365-386`).
606fn mcp_require_approval(tool: &ToolDefinition) -> Value {
607    use agent_framework_core::tools::ApprovalMode;
608    match tool.parameters.get("approval_mode") {
609        Some(Value::String(s)) => {
610            return json!(if s == "always_require" {
611                "always"
612            } else {
613                "never"
614            });
615        }
616        Some(Value::Object(modes)) => {
617            let mut req = Map::new();
618            if let Some(always) = modes.get("always") {
619                req.insert("always".into(), json!({ "tool_names": always }));
620            }
621            if let Some(never) = modes.get("never") {
622                req.insert("never".into(), json!({ "tool_names": never }));
623            }
624            if !req.is_empty() {
625                return Value::Object(req);
626            }
627        }
628        _ => {}
629    }
630    json!(match tool.approval_mode {
631        ApprovalMode::AlwaysRequire => "always",
632        ApprovalMode::NeverRequire => "never",
633    })
634}
635
636/// `pub` so `agent-framework-azure`'s Responses client can reuse this
637/// mapping rather than reimplementing it.
638pub fn tool_choice_to_responses(mode: &ToolMode) -> Value {
639    match mode {
640        ToolMode::Auto => json!("auto"),
641        ToolMode::None => json!("none"),
642        ToolMode::Required(Some(name)) => json!({ "type": "function", "name": name }),
643        ToolMode::Required(None) => json!("required"),
644    }
645}
646
647/// Convert a `ChatOptions::response_format` into a Responses API
648/// `text.format` object. Unlike Chat Completions (which nests the schema
649/// under `json_schema`), the Responses API uses a flat object.
650///
651/// `pub` so `agent-framework-azure`'s Responses client can reuse this
652/// mapping rather than reimplementing it.
653pub fn response_format_to_text(format: &ResponseFormat) -> Value {
654    match format {
655        ResponseFormat::Text => json!({ "type": "text" }),
656        ResponseFormat::JsonObject => json!({ "type": "json_object" }),
657        ResponseFormat::JsonSchema {
658            name,
659            description,
660            schema,
661            strict,
662        } => {
663            let mut obj = Map::new();
664            obj.insert("type".into(), json!("json_schema"));
665            obj.insert("name".into(), json!(name));
666            if let Some(d) = description {
667                obj.insert("description".into(), json!(d));
668            }
669            obj.insert("schema".into(), schema.clone());
670            if let Some(st) = strict {
671                obj.insert("strict".into(), json!(st));
672            }
673            Value::Object(obj)
674        }
675    }
676}
677
678// endregion
679
680// region: response conversion
681
682/// Parse a full (non-streaming) Responses API response.
683///
684/// Map a Responses body whose `status` is `"failed"` to a classified error,
685/// or `None` for a non-failed response. A failed run reports `status:
686/// "failed"` with a 2xx HTTP status, so the error is pulled from the body:
687/// `error.code == "content_filter"` becomes [`Error::ServiceContentFilter`]
688/// (matching the HTTP-level classification in `classify_service_error`),
689/// anything else a generic service error.
690///
691/// `pub` so `agent-framework-azure`'s Responses client can share the exact
692/// classification.
693pub fn response_failure_error(value: &Value) -> Option<Error> {
694    if value.get("status").and_then(Value::as_str) != Some("failed") {
695        return None;
696    }
697    let error = value.get("error");
698    let msg = error
699        .and_then(|e| e.get("message"))
700        .and_then(Value::as_str)
701        .unwrap_or("response failed")
702        .to_string();
703    let code = error.and_then(|e| e.get("code")).and_then(Value::as_str);
704    Some(match code {
705        Some("content_filter") => Error::service_content_filter(msg),
706        _ => Error::service(msg),
707    })
708}
709
710/// `pub` so `agent-framework-azure`'s Responses client (whose wire format is
711/// otherwise identical) can reuse this parser — including `parse_output_item`,
712/// `parse_annotations`, and usage/finish-reason handling — rather than
713/// reimplementing it.
714pub fn parse_response(value: &Value, store: Option<bool>) -> ChatResponse {
715    let mut response = ChatResponse {
716        response_id: value.get("id").and_then(Value::as_str).map(String::from),
717        model: value.get("model").and_then(Value::as_str).map(String::from),
718        ..Default::default()
719    };
720
721    let mut contents: Vec<Content> = Vec::new();
722    if let Some(items) = value.get("output").and_then(Value::as_array) {
723        for item in items {
724            parse_output_item(item, &mut contents);
725        }
726    }
727
728    let mut message = Message::with_contents(Role::assistant(), contents);
729    message.message_id = response.response_id.clone();
730    response.messages.push(message);
731
732    response.finish_reason = finish_reason_from_response(value);
733
734    if let Some(usage) = value.get("usage") {
735        response.usage_details = Some(parse_responses_usage(usage));
736    }
737    if store != Some(false) {
738        response.conversation_id = response.response_id.clone();
739    }
740    response
741}
742
743fn parse_output_item(item: &Value, contents: &mut Vec<Content>) {
744    match item.get("type").and_then(Value::as_str) {
745        Some("message") => {
746            if let Some(parts) = item.get("content").and_then(Value::as_array) {
747                for part in parts {
748                    match part.get("type").and_then(Value::as_str) {
749                        Some("output_text") => {
750                            if let Some(text) = part.get("text").and_then(Value::as_str) {
751                                let mut tc = TextContent::new(text);
752                                tc.annotations = parse_annotations(part);
753                                contents.push(Content::Text(tc));
754                            }
755                        }
756                        Some("refusal") => {
757                            if let Some(text) = part.get("refusal").and_then(Value::as_str) {
758                                // Marked, so `Message::text` withholds it
759                                // rather than presenting it as the answer.
760                                contents.push(Content::Text(TextContent::refusal(text)));
761                            }
762                        }
763                        _ => {}
764                    }
765                }
766            }
767        }
768        Some("function_call") => {
769            let call_id = item
770                .get("call_id")
771                .and_then(Value::as_str)
772                .unwrap_or_default()
773                .to_string();
774            let name = item
775                .get("name")
776                .and_then(Value::as_str)
777                .unwrap_or_default()
778                .to_string();
779            let args = item
780                .get("arguments")
781                .and_then(Value::as_str)
782                .unwrap_or("{}")
783                .to_string();
784            contents.push(Content::FunctionCall(FunctionCallContent::new(
785                call_id,
786                name,
787                Some(FunctionArguments::Raw(args)),
788            )));
789        }
790        Some("reasoning") => {
791            let summaries: Vec<String> = item
792                .get("summary")
793                .and_then(Value::as_array)
794                .map(|arr| {
795                    arr.iter()
796                        .filter_map(|s| s.get("text").and_then(Value::as_str))
797                        .map(str::to_string)
798                        .collect()
799                })
800                .unwrap_or_default();
801            // Preserve the raw reasoning item so a store:false tool-loop replay
802            // can re-send it verbatim — reasoning models require the original
803            // item (id + encrypted_content), not just the summary, on the
804            // follow-up turn. `messages_to_input` re-emits it from
805            // `raw_representation`; it is attached to exactly one content so a
806            // multi-summary item yields exactly one reasoning input item.
807            let raw = Some(item.clone());
808            if summaries.is_empty() {
809                // No summary (e.g. encrypted reasoning) — still carry the item.
810                contents.push(Content::TextReasoning(TextReasoningContent {
811                    text: String::new(),
812                    annotations: None,
813                    raw_representation: raw,
814                    protected_data: None,
815                }));
816            } else {
817                let n = summaries.len();
818                for (i, text) in summaries.into_iter().enumerate() {
819                    contents.push(Content::TextReasoning(TextReasoningContent {
820                        text,
821                        annotations: None,
822                        raw_representation: (i == n - 1).then(|| raw.clone()).flatten(),
823                        protected_data: None,
824                    }));
825                }
826            }
827        }
828        // Code-interpreter runs surface `logs` as text and `image` outputs as
829        // URIs; a bare `code` (no outputs) is a text fallback
830        // (`_create_response_content:748-764`).
831        Some("code_interpreter_call") => {
832            let outputs = item
833                .get("outputs")
834                .and_then(Value::as_array)
835                .filter(|a| !a.is_empty());
836            if let Some(outputs) = outputs {
837                for output in outputs {
838                    match output.get("type").and_then(Value::as_str) {
839                        Some("logs") => {
840                            if let Some(logs) = output.get("logs").and_then(Value::as_str) {
841                                contents.push(Content::Text(TextContent::new(logs)));
842                            }
843                        }
844                        Some("image") => {
845                            if let Some(url) = output.get("url").and_then(Value::as_str) {
846                                contents.push(Content::Uri(UriContent {
847                                    uri: url.to_string(),
848                                    media_type: "image".to_string(),
849                                }));
850                            }
851                        }
852                        _ => {}
853                    }
854                }
855            } else if let Some(code) = item.get("code").and_then(Value::as_str) {
856                contents.push(Content::Text(TextContent::new(code)));
857            }
858        }
859        // A generated image is returned as base64; default to image/png unless
860        // the result is a data URI that states its own type
861        // (`_create_response_content:788-811`).
862        Some("image_generation_call") => {
863            if let Some(result) = item.get("result").and_then(Value::as_str) {
864                let (uri, media_type) = if result.starts_with("data:") {
865                    let media_type = if result.contains(';') {
866                        result
867                            .strip_prefix("data:")
868                            .and_then(|r| r.split(';').next())
869                            .filter(|s| !s.is_empty())
870                            .map(String::from)
871                    } else {
872                        None
873                    };
874                    (result.to_string(), media_type)
875                } else {
876                    (
877                        format!("data:image/png;base64,{result}"),
878                        Some("image/png".to_string()),
879                    )
880                };
881                contents.push(Content::Data(DataContent { uri, media_type }));
882            }
883        }
884        // An MCP approval request round-trips its `id` as the call id so a
885        // later `FunctionApprovalResponse` refers back to it
886        // (`_create_response_content:775-787`).
887        Some("mcp_approval_request") => {
888            let id = item
889                .get("id")
890                .and_then(Value::as_str)
891                .unwrap_or_default()
892                .to_string();
893            let name = item
894                .get("name")
895                .and_then(Value::as_str)
896                .unwrap_or_default()
897                .to_string();
898            let args = item
899                .get("arguments")
900                .and_then(Value::as_str)
901                .unwrap_or("{}")
902                .to_string();
903            contents.push(Content::FunctionApprovalRequest(
904                FunctionApprovalRequestContent {
905                    id: id.clone(),
906                    function_call: FunctionCallContent::new(
907                        id,
908                        name,
909                        Some(FunctionArguments::Raw(args)),
910                    ),
911                },
912            ));
913        }
914        _ => {}
915    }
916}
917
918/// Parse the `annotations` on an `output_text` part into [`Annotation`]s
919/// (`_create_response_content:667-724`). The core annotation type has no free
920/// `additional_properties`, so upstream's `index`/`container_id` extras are
921/// dropped.
922fn parse_annotations(part: &Value) -> Option<Vec<Annotation>> {
923    let arr = part.get("annotations").and_then(Value::as_array)?;
924    let mut out = Vec::new();
925    for ann in arr {
926        let str_field = |k: &str| ann.get(k).and_then(Value::as_str).map(String::from);
927        let regions = || {
928            Some(vec![TextSpanRegion {
929                start_index: ann.get("start_index").and_then(Value::as_i64),
930                end_index: ann.get("end_index").and_then(Value::as_i64),
931            }])
932        };
933        match ann.get("type").and_then(Value::as_str) {
934            Some("file_path") => out.push(Annotation {
935                file_id: str_field("file_id"),
936                ..Default::default()
937            }),
938            Some("file_citation") => out.push(Annotation {
939                url: str_field("filename"),
940                file_id: str_field("file_id"),
941                ..Default::default()
942            }),
943            Some("url_citation") => out.push(Annotation {
944                title: str_field("title"),
945                url: str_field("url"),
946                annotated_regions: regions(),
947                ..Default::default()
948            }),
949            Some("container_file_citation") => out.push(Annotation {
950                file_id: str_field("file_id"),
951                url: str_field("filename"),
952                annotated_regions: regions(),
953                ..Default::default()
954            }),
955            _ => {}
956        }
957    }
958    if out.is_empty() {
959        None
960    } else {
961        Some(out)
962    }
963}
964
965fn finish_reason_from_response(value: &Value) -> Option<FinishReason> {
966    let has_function_call = value
967        .get("output")
968        .and_then(Value::as_array)
969        .map(|items| {
970            items
971                .iter()
972                .any(|i| i.get("type").and_then(Value::as_str) == Some("function_call"))
973        })
974        .unwrap_or(false);
975    if has_function_call {
976        return Some(FinishReason::tool_calls());
977    }
978    let status = value.get("status").and_then(Value::as_str)?;
979    Some(match status {
980        "completed" => FinishReason::stop(),
981        "incomplete" => match value
982            .get("incomplete_details")
983            .and_then(|d| d.get("reason"))
984            .and_then(Value::as_str)
985        {
986            Some("max_output_tokens") => FinishReason::new(FinishReason::LENGTH),
987            Some("content_filter") => FinishReason::new(FinishReason::CONTENT_FILTER),
988            Some(other) => FinishReason::new(other),
989            None => FinishReason::new("incomplete"),
990        },
991        other => FinishReason::new(other),
992    })
993}
994
995fn parse_responses_usage(usage: &Value) -> UsageDetails {
996    let mut details = UsageDetails {
997        input_token_count: usage.get("input_tokens").and_then(Value::as_u64),
998        output_token_count: usage.get("output_tokens").and_then(Value::as_u64),
999        total_token_count: usage.get("total_tokens").and_then(Value::as_u64),
1000        ..Default::default()
1001    };
1002    if let Some(cached) = usage
1003        .get("input_tokens_details")
1004        .and_then(|d| d.get("cached_tokens"))
1005        .and_then(Value::as_u64)
1006    {
1007        details
1008            .additional_counts
1009            .insert("openai.cached_input_tokens".into(), cached);
1010        // Mirror upstream: also surface as the typed, cross-language field.
1011        details.cache_read_input_token_count = Some(cached);
1012    }
1013    // Cache *writes* are reported separately from cache reads. Absent when the
1014    // provider does not report them (older API versions).
1015    if let Some(cache_write) = usage
1016        .get("input_tokens_details")
1017        .and_then(|d| d.get("cache_write_tokens"))
1018        .and_then(Value::as_u64)
1019    {
1020        details
1021            .additional_counts
1022            .insert("openai.cache_write_tokens".into(), cache_write);
1023        details.cache_creation_input_token_count = Some(cache_write);
1024    }
1025    if let Some(reasoning) = usage
1026        .get("output_tokens_details")
1027        .and_then(|d| d.get("reasoning_tokens"))
1028        .and_then(Value::as_u64)
1029    {
1030        details
1031            .additional_counts
1032            .insert("openai.reasoning_tokens".into(), reasoning);
1033        details.reasoning_output_token_count = Some(reasoning);
1034    }
1035    details
1036}
1037
1038// endregion
1039
1040// region: streaming
1041
1042/// Turn a Responses API SSE HTTP response into a stream of updates.
1043///
1044/// `pub` so `agent-framework-azure`'s Responses client can reuse this exact
1045/// SSE event parser (Azure OpenAI's Responses API streams the same event
1046/// shapes) rather than reimplementing it.
1047pub fn parse_responses_sse_stream(
1048    resp: reqwest::Response,
1049    store: Option<bool>,
1050) -> impl futures::Stream<Item = Result<ChatResponseUpdate>> + Send {
1051    let byte_stream: ByteStream = Box::pin(resp.bytes_stream());
1052    futures::stream::unfold(
1053        ResponsesSseState {
1054            byte_stream,
1055            buffer: String::new(),
1056            utf8: Utf8StreamDecoder::new(),
1057            queued: VecDeque::new(),
1058            call_ids: HashMap::new(),
1059            done: false,
1060            store,
1061        },
1062        |mut state| async move {
1063            loop {
1064                if let Some(update) = state.queued.pop_front() {
1065                    return Some((Ok(update), state));
1066                }
1067                if state.done {
1068                    return None;
1069                }
1070                match state.byte_stream.next().await {
1071                    Some(Ok(bytes)) => {
1072                        let decoded = state.utf8.push(&bytes);
1073                        state.buffer.push_str(&decoded);
1074                        while let Some(pos) = state.buffer.find('\n') {
1075                            let line = state.buffer[..pos].trim().to_string();
1076                            state.buffer.drain(..=pos);
1077                            let Some(data) = line.strip_prefix("data:") else {
1078                                continue;
1079                            };
1080                            let data = data.trim();
1081                            if data.is_empty() {
1082                                continue;
1083                            }
1084                            let Ok(value) = serde_json::from_str::<Value>(data) else {
1085                                continue;
1086                            };
1087                            match parse_responses_event(&value, &mut state.call_ids, state.store) {
1088                                EventOutcome::Update(update) => state.queued.push_back(update),
1089                                EventOutcome::Error(e) => {
1090                                    state.done = true;
1091                                    return Some((Err(e), state));
1092                                }
1093                                EventOutcome::None => {}
1094                            }
1095                        }
1096                    }
1097                    Some(Err(e)) => {
1098                        state.done = true;
1099                        return Some((Err(Error::service(format!("stream error: {e}"))), state));
1100                    }
1101                    None => return None,
1102                }
1103            }
1104        },
1105    )
1106}
1107
1108struct ResponsesSseState {
1109    byte_stream: ByteStream,
1110    buffer: String,
1111    utf8: Utf8StreamDecoder,
1112    queued: VecDeque<ChatResponseUpdate>,
1113    /// `output_index` -> `call_id`, resolved when the call is first announced
1114    /// via `response.output_item.added` and used to route later
1115    /// `response.function_call_arguments.delta` fragments.
1116    call_ids: HashMap<i64, String>,
1117    done: bool,
1118    store: Option<bool>,
1119}
1120
1121// A transient control-flow value: produced per SSE event and immediately
1122// destructured in the stream loop, never stored in bulk. Boxing the `Update`
1123// variant to equalize sizes would add a heap allocation on every streamed
1124// token, so the size skew is accepted here.
1125#[allow(clippy::large_enum_variant)]
1126enum EventOutcome {
1127    Update(ChatResponseUpdate),
1128    Error(Error),
1129    None,
1130}
1131
1132/// Wrap streamed reasoning text as a [`TextReasoningContent`] update, or
1133/// [`EventOutcome::None`] when empty.
1134fn reasoning_update(text: &str) -> EventOutcome {
1135    if text.is_empty() {
1136        return EventOutcome::None;
1137    }
1138    EventOutcome::Update(ChatResponseUpdate {
1139        contents: vec![Content::TextReasoning(TextReasoningContent {
1140            text: text.to_string(),
1141            annotations: None,
1142            ..Default::default()
1143        })],
1144        role: Some(Role::assistant()),
1145        ..Default::default()
1146    })
1147}
1148
1149/// Parse one Responses API SSE event (already-decoded JSON `data:` payload).
1150fn parse_responses_event(
1151    value: &Value,
1152    call_ids: &mut HashMap<i64, String>,
1153    store: Option<bool>,
1154) -> EventOutcome {
1155    let event_type = value.get("type").and_then(Value::as_str).unwrap_or("");
1156    match event_type {
1157        "response.created" => {
1158            let resp = value.get("response");
1159            let response_id = resp
1160                .and_then(|r| r.get("id"))
1161                .and_then(Value::as_str)
1162                .map(String::from);
1163            let model = resp
1164                .and_then(|r| r.get("model"))
1165                .and_then(Value::as_str)
1166                .map(String::from);
1167            if response_id.is_none() && model.is_none() {
1168                return EventOutcome::None;
1169            }
1170            EventOutcome::Update(ChatResponseUpdate {
1171                role: Some(Role::assistant()),
1172                response_id,
1173                model,
1174                ..Default::default()
1175            })
1176        }
1177        "response.output_text.delta" => {
1178            let text = value.get("delta").and_then(Value::as_str).unwrap_or("");
1179            if text.is_empty() {
1180                return EventOutcome::None;
1181            }
1182            EventOutcome::Update(ChatResponseUpdate {
1183                contents: vec![Content::Text(TextContent::new(text))],
1184                role: Some(Role::assistant()),
1185                ..Default::default()
1186            })
1187        }
1188        // A refusal streams on its own channel. Only the incremental `.delta`
1189        // is consumed: the terminal `response.refusal.done` repeats the whole
1190        // refusal, which aggregation would append to the fragments already
1191        // collected, doubling it — the same reason the text channel above
1192        // does not consume its `.done`. Dropping these events entirely, as
1193        // this parser used to, let a streamed decline reach the caller as an
1194        // empty response.
1195        "response.refusal.delta" => {
1196            let text = value.get("delta").and_then(Value::as_str).unwrap_or("");
1197            if text.is_empty() {
1198                return EventOutcome::None;
1199            }
1200            EventOutcome::Update(ChatResponseUpdate {
1201                contents: vec![Content::Text(TextContent::refusal(text))],
1202                role: Some(Role::assistant()),
1203                ..Default::default()
1204            })
1205        }
1206        // Reasoning (chain-of-thought / summary) streams as its own text
1207        // channel. Both the incremental `.delta` and the terminal `.done`
1208        // (full text) map to `TextReasoningContent`, mirroring upstream
1209        // `_create_streaming_response_content` (`_responses_client.py:917-928`).
1210        "response.reasoning_text.delta" | "response.reasoning_summary_text.delta" => {
1211            reasoning_update(value.get("delta").and_then(Value::as_str).unwrap_or(""))
1212        }
1213        // `.done` carries the *full* completed text, not another increment —
1214        // the deltas above already streamed it, so emitting it again would
1215        // duplicate the reasoning in the aggregated response (adjacent
1216        // reasoning contents coalesce by appending). Treated as terminal
1217        // metadata, like `response.output_text.done`.
1218        "response.reasoning_text.done" | "response.reasoning_summary_text.done" => {
1219            EventOutcome::None
1220        }
1221        "response.output_item.added" => {
1222            let item = value.get("item");
1223            if item.and_then(|i| i.get("type")).and_then(Value::as_str) != Some("function_call") {
1224                return EventOutcome::None;
1225            }
1226            let output_index = value
1227                .get("output_index")
1228                .and_then(Value::as_i64)
1229                .unwrap_or(0);
1230            let call_id = item
1231                .and_then(|i| i.get("call_id"))
1232                .and_then(Value::as_str)
1233                .unwrap_or_default()
1234                .to_string();
1235            let name = item
1236                .and_then(|i| i.get("name"))
1237                .and_then(Value::as_str)
1238                .unwrap_or_default()
1239                .to_string();
1240            call_ids.insert(output_index, call_id.clone());
1241            EventOutcome::Update(ChatResponseUpdate {
1242                contents: vec![Content::FunctionCall(FunctionCallContent::new(
1243                    call_id, name, None,
1244                ))],
1245                role: Some(Role::assistant()),
1246                ..Default::default()
1247            })
1248        }
1249        "response.function_call_arguments.delta" => {
1250            let output_index = value
1251                .get("output_index")
1252                .and_then(Value::as_i64)
1253                .unwrap_or(0);
1254            let delta = value.get("delta").and_then(Value::as_str).unwrap_or("");
1255            match call_ids.get(&output_index) {
1256                Some(call_id) => EventOutcome::Update(ChatResponseUpdate {
1257                    contents: vec![Content::FunctionCall(FunctionCallContent::new(
1258                        call_id.clone(),
1259                        "",
1260                        Some(FunctionArguments::Raw(delta.to_string())),
1261                    ))],
1262                    role: Some(Role::assistant()),
1263                    ..Default::default()
1264                }),
1265                None => EventOutcome::None,
1266            }
1267        }
1268        "response.completed" => {
1269            let resp = value.get("response");
1270            let response_id = resp
1271                .and_then(|r| r.get("id"))
1272                .and_then(Value::as_str)
1273                .map(String::from);
1274            let model = resp
1275                .and_then(|r| r.get("model"))
1276                .and_then(Value::as_str)
1277                .map(String::from);
1278            let mut contents = Vec::new();
1279            if let Some(usage) = resp.and_then(|r| r.get("usage")) {
1280                contents.push(Content::Usage(UsageContent {
1281                    details: parse_responses_usage(usage),
1282                }));
1283            }
1284            let finish_reason = resp.and_then(finish_reason_from_response);
1285            let conversation_id = if store != Some(false) {
1286                response_id.clone()
1287            } else {
1288                None
1289            };
1290            EventOutcome::Update(ChatResponseUpdate {
1291                contents,
1292                role: Some(Role::assistant()),
1293                response_id,
1294                model,
1295                conversation_id,
1296                finish_reason,
1297                ..Default::default()
1298            })
1299        }
1300        "response.failed" | "error" => {
1301            let resp = value.get("response");
1302            let err_obj = resp
1303                .and_then(|r| r.get("error"))
1304                .or_else(|| value.get("error"));
1305            let msg = err_obj
1306                .and_then(|e| e.get("message"))
1307                .and_then(Value::as_str)
1308                .unwrap_or("response failed")
1309                .to_string();
1310            // Same classification as the non-streaming `response_failure_error`
1311            // so callers branching on content filters see one behavior for
1312            // streamed and non-streamed Responses.
1313            let code = err_obj.and_then(|e| e.get("code")).and_then(Value::as_str);
1314            EventOutcome::Error(match code {
1315                Some("content_filter") => Error::service_content_filter(msg),
1316                _ => Error::service(msg),
1317            })
1318        }
1319        // Recognized but carry no additional content: the arguments are
1320        // already fully accumulated via `.delta` events, and item/part
1321        // lifecycle markers don't themselves map to a `Content`.
1322        "response.function_call_arguments.done"
1323        | "response.output_item.done"
1324        | "response.content_part.added"
1325        | "response.content_part.done"
1326        | "response.in_progress" => EventOutcome::None,
1327        _ => EventOutcome::None,
1328    }
1329}
1330
1331// endregion
1332
1333#[cfg(test)]
1334mod tests {
1335    use super::*;
1336    use agent_framework_core::tools::{ApprovalMode, ToolDefinition, ToolKind};
1337    use agent_framework_core::types::{
1338        FunctionApprovalResponseContent, FunctionResultContent, HostedFileContent,
1339    };
1340
1341    fn user(text: &str) -> Message {
1342        Message::user(text)
1343    }
1344
1345    fn user_with(contents: Vec<Content>) -> Message {
1346        Message::with_contents(Role::user(), contents)
1347    }
1348
1349    /// Parse a single Responses output item into its content list.
1350    fn parse_item(item: Value) -> Vec<Content> {
1351        let mut contents = Vec::new();
1352        parse_output_item(&item, &mut contents);
1353        contents
1354    }
1355
1356    fn client() -> OpenAIChatClient {
1357        OpenAIChatClient::new("test-key", "gpt-4o-mini")
1358    }
1359
1360    // region: request body building
1361
1362    #[test]
1363    fn build_body_simple_text() {
1364        let c = client();
1365        let body = c.build_body(&[user("Hello there")], &ChatOptions::new(), false);
1366        assert_eq!(
1367            body,
1368            json!({
1369                "model": "gpt-4o-mini",
1370                "input": [
1371                    { "type": "message", "role": "user", "content": [
1372                        { "type": "input_text", "text": "Hello there" }
1373                    ]}
1374                ],
1375                // Nothing is stored service-side on this request, so the
1376                // encrypted reasoning item is requested for the replay path.
1377                "include": ["reasoning.encrypted_content"],
1378            })
1379        );
1380    }
1381
1382    #[test]
1383    fn responses_usage_parses_cache_write_tokens() {
1384        // Cache *writes* are reported separately from cache reads (upstream #7369).
1385        let d = parse_responses_usage(&json!({
1386            "input_tokens": 2000,
1387            "output_tokens": 60,
1388            "total_tokens": 2060,
1389            "input_tokens_details": { "cached_tokens": 0, "cache_write_tokens": 1024 },
1390        }));
1391        assert_eq!(d.cache_creation_input_token_count, Some(1024));
1392        assert_eq!(d.cache_read_input_token_count, Some(0));
1393        assert_eq!(
1394            d.additional_counts.get("openai.cache_write_tokens"),
1395            Some(&1024)
1396        );
1397    }
1398
1399    #[test]
1400    fn responses_usage_omits_cache_write_tokens_when_not_reported() {
1401        let d = parse_responses_usage(&json!({
1402            "input_tokens": 100,
1403            "output_tokens": 20,
1404            "input_tokens_details": { "cached_tokens": 40 },
1405        }));
1406        assert_eq!(d.cache_creation_input_token_count, None);
1407        assert!(!d
1408            .additional_counts
1409            .contains_key("openai.cache_write_tokens"));
1410    }
1411
1412    #[test]
1413    fn build_body_extracts_leading_system_message_as_instructions() {
1414        let c = client();
1415        let messages = vec![Message::system("Be terse."), user("Hi")];
1416        let body = c.build_body(&messages, &ChatOptions::new(), false);
1417        assert_eq!(body["instructions"], json!("Be terse."));
1418        assert_eq!(
1419            body["input"],
1420            json!([
1421                { "type": "message", "role": "user", "content": [
1422                    { "type": "input_text", "text": "Hi" }
1423                ]}
1424            ])
1425        );
1426    }
1427
1428    #[test]
1429    fn build_body_combines_options_instructions_and_system_message() {
1430        let c = client();
1431        let messages = vec![Message::system("Also be nice."), user("Hi")];
1432        let options = ChatOptions::new().with_instructions("Be terse.");
1433        let body = c.build_body(&messages, &options, false);
1434        assert_eq!(body["instructions"], json!("Be terse.\n\nAlso be nice."));
1435    }
1436
1437    #[test]
1438    fn build_body_assistant_text_uses_output_text_type() {
1439        let c = client();
1440        let messages = vec![user("Hi"), Message::assistant("Hello!")];
1441        let body = c.build_body(&messages, &ChatOptions::new(), false);
1442        assert_eq!(
1443            body["input"][1],
1444            json!({ "type": "message", "role": "assistant", "content": [
1445                { "type": "output_text", "text": "Hello!" }
1446            ]})
1447        );
1448    }
1449
1450    #[test]
1451    fn build_body_function_call_round_trip() {
1452        let c = client();
1453        let call = FunctionCallContent::new(
1454            "call_1",
1455            "get_weather",
1456            Some(FunctionArguments::Raw(r#"{"city":"Paris"}"#.to_string())),
1457        );
1458        let assistant_msg =
1459            Message::with_contents(Role::assistant(), vec![Content::FunctionCall(call)]);
1460        let tool_msg = Message::with_contents(
1461            Role::tool(),
1462            vec![Content::FunctionResult(FunctionResultContent::new(
1463                "call_1",
1464                Some(json!("18C and sunny")),
1465            ))],
1466        );
1467        let body = c.build_body(
1468            &[user("weather?"), assistant_msg, tool_msg],
1469            &ChatOptions::new(),
1470            false,
1471        );
1472        assert_eq!(
1473            body["input"],
1474            json!([
1475                { "type": "message", "role": "user", "content": [
1476                    { "type": "input_text", "text": "weather?" }
1477                ]},
1478                { "type": "function_call", "call_id": "call_1", "name": "get_weather", "arguments": "{\"city\":\"Paris\"}" },
1479                { "type": "function_call_output", "call_id": "call_1", "output": "18C and sunny" },
1480            ])
1481        );
1482    }
1483
1484    #[test]
1485    fn build_body_tools_are_flat_not_nested() {
1486        let c = client();
1487        let tool = ToolDefinition {
1488            name: "get_weather".into(),
1489            description: "Get the weather".into(),
1490            parameters: json!({ "type": "object", "properties": {} }),
1491            kind: ToolKind::Function,
1492            approval_mode: ApprovalMode::NeverRequire,
1493            executor: None,
1494        };
1495        let options = ChatOptions::new().with_tool(tool);
1496        let body = c.build_body(&[user("hi")], &options, false);
1497        assert_eq!(
1498            body["tools"],
1499            json!([{
1500                "type": "function",
1501                "name": "get_weather",
1502                "description": "Get the weather",
1503                "parameters": { "type": "object", "properties": {} },
1504            }])
1505        );
1506    }
1507
1508    #[test]
1509    fn build_body_tool_choice_required_named() {
1510        let c = client();
1511        let options =
1512            ChatOptions::new().with_tool_choice(ToolMode::Required(Some("get_weather".into())));
1513        let body = c.build_body(&[user("hi")], &options, false);
1514        assert_eq!(
1515            body["tool_choice"],
1516            json!({ "type": "function", "name": "get_weather" })
1517        );
1518    }
1519
1520    #[test]
1521    fn stateless_request_asks_for_the_encrypted_reasoning_item() {
1522        // A reasoning item is only replayable on the next turn if it carries
1523        // `encrypted_content`, and the service only returns that when asked.
1524        let c = client();
1525        let body = c.build_body(&[user("hi")], &ChatOptions::new(), false);
1526        assert_eq!(body["include"], json!(["reasoning.encrypted_content"]));
1527    }
1528
1529    #[test]
1530    fn a_service_side_conversation_needs_no_encrypted_reasoning() {
1531        // The service already holds the reasoning item, so echoing it back is
1532        // pointless — and upstream keys this off the storage indicators, not
1533        // off `store`.
1534        let mut options = ChatOptions::new();
1535        options.conversation_id = Some("resp_abc123".into());
1536        let body = client().build_body(&[user("hi")], &options, false);
1537        assert!(
1538            body.get("include").is_none(),
1539            "no include expected, got: {}",
1540            body
1541        );
1542    }
1543
1544    #[test]
1545    fn an_empty_conversation_id_is_not_service_side_storage() {
1546        // An empty id is no id: it would not continue anything server-side, so
1547        // the request is still stateless and still needs the item echoed back.
1548        let mut options = ChatOptions::new();
1549        options.conversation_id = Some(String::new());
1550        let body = client().build_body(&[user("hi")], &options, false);
1551        assert_eq!(body["include"], json!(["reasoning.encrypted_content"]));
1552    }
1553
1554    #[test]
1555    fn a_callers_own_include_entries_survive_and_are_not_duplicated() {
1556        // The caller's entries are preserved alongside the implicit one...
1557        let mut options = ChatOptions::new();
1558        options
1559            .additional_properties
1560            .insert("include".into(), json!(["file_search_call.results"]));
1561        let body = client().build_body(&[user("hi")], &options, false);
1562        assert_eq!(
1563            body["include"],
1564            json!(["file_search_call.results", "reasoning.encrypted_content"])
1565        );
1566
1567        // ...and asking for the encrypted item by name does not get it twice.
1568        let mut options = ChatOptions::new();
1569        options
1570            .additional_properties
1571            .insert("include".into(), json!(["reasoning.encrypted_content"]));
1572        let body = client().build_body(&[user("hi")], &options, false);
1573        assert_eq!(body["include"], json!(["reasoning.encrypted_content"]));
1574    }
1575
1576    #[test]
1577    fn an_explicit_empty_include_is_omitted_not_sent_as_an_empty_array() {
1578        // `responses_include` drops an empty array, and the
1579        // `additional_properties` pass must not put it back: `include` is
1580        // that function's alone. Reached here via a stored conversation, which
1581        // suppresses the implicit entry that would otherwise fill the array.
1582        let mut options = ChatOptions::new();
1583        options.conversation_id = Some("resp_abc123".into());
1584        options
1585            .additional_properties
1586            .insert("include".into(), json!([]));
1587        let body = client().build_body(&[user("hi")], &options, false);
1588        assert!(
1589            body.get("include").is_none(),
1590            "an empty include should be omitted entirely, got: {}",
1591            body
1592        );
1593    }
1594
1595    #[test]
1596    fn other_additional_properties_still_pass_through() {
1597        // Only `include` is intercepted; everything else the caller sets still
1598        // reaches the body.
1599        let mut options = ChatOptions::new();
1600        options
1601            .additional_properties
1602            .insert("safety_identifier".into(), json!("user-123"));
1603        let body = client().build_body(&[user("hi")], &options, false);
1604        assert_eq!(body["safety_identifier"], json!("user-123"));
1605    }
1606
1607    #[test]
1608    fn build_body_conversation_id_becomes_previous_response_id() {
1609        let c = client();
1610        let mut options = ChatOptions::new();
1611        options.conversation_id = Some("resp_abc123".into());
1612        let body = c.build_body(&[user("hi")], &options, false);
1613        assert_eq!(body["previous_response_id"], json!("resp_abc123"));
1614    }
1615
1616    #[test]
1617    fn build_body_max_tokens_becomes_max_output_tokens() {
1618        let c = client();
1619        let options = ChatOptions::new().with_max_tokens(256);
1620        let body = c.build_body(&[user("hi")], &options, false);
1621        assert_eq!(body["max_output_tokens"], json!(256));
1622        assert!(body.get("max_tokens").is_none());
1623    }
1624
1625    #[test]
1626    fn build_body_response_format_becomes_text_format() {
1627        let c = client();
1628        let mut options = ChatOptions::new();
1629        options.response_format = Some(ResponseFormat::JsonSchema {
1630            name: "answer".into(),
1631            description: None,
1632            schema: json!({"type": "object"}),
1633            strict: Some(true),
1634        });
1635        let body = c.build_body(&[user("hi")], &options, false);
1636        assert_eq!(
1637            body["text"]["format"],
1638            json!({ "type": "json_schema", "name": "answer", "schema": {"type": "object"}, "strict": true })
1639        );
1640    }
1641
1642    #[test]
1643    fn build_body_stream_flag() {
1644        let c = client();
1645        let body = c.build_body(&[user("hi")], &ChatOptions::new(), true);
1646        assert_eq!(body["stream"], json!(true));
1647    }
1648
1649    // endregion
1650
1651    // region: response parsing
1652
1653    #[test]
1654    fn parse_response_text_and_usage() {
1655        let value = json!({
1656            "id": "resp_123",
1657            "model": "gpt-4o-mini",
1658            "status": "completed",
1659            "output": [
1660                { "type": "message", "role": "assistant", "content": [
1661                    { "type": "output_text", "text": "Hello!" }
1662                ]}
1663            ],
1664            "usage": { "input_tokens": 10, "output_tokens": 5, "total_tokens": 15 },
1665        });
1666        let resp = parse_response(&value, None);
1667        assert_eq!(resp.response_id.as_deref(), Some("resp_123"));
1668        assert_eq!(resp.conversation_id.as_deref(), Some("resp_123"));
1669        assert_eq!(resp.text(), "Hello!");
1670        assert_eq!(resp.finish_reason, Some(FinishReason::stop()));
1671        let usage = resp.usage_details.unwrap();
1672        assert_eq!(usage.input_token_count, Some(10));
1673        assert_eq!(usage.output_token_count, Some(5));
1674        assert_eq!(usage.total_token_count, Some(15));
1675    }
1676
1677    #[test]
1678    fn parse_response_store_false_omits_conversation_id() {
1679        let value = json!({
1680            "id": "resp_123",
1681            "status": "completed",
1682            "output": [],
1683        });
1684        let resp = parse_response(&value, Some(false));
1685        assert_eq!(resp.conversation_id, None);
1686    }
1687
1688    #[test]
1689    fn parse_response_function_call_sets_tool_calls_finish_reason() {
1690        let value = json!({
1691            "id": "resp_123",
1692            "status": "completed",
1693            "output": [
1694                { "type": "function_call", "call_id": "call_1", "name": "get_weather", "arguments": "{\"city\":\"Paris\"}" }
1695            ],
1696        });
1697        let resp = parse_response(&value, None);
1698        assert_eq!(resp.finish_reason, Some(FinishReason::tool_calls()));
1699        let calls = resp.function_calls();
1700        assert_eq!(calls.len(), 1);
1701        assert_eq!(calls[0].call_id, "call_1");
1702        assert_eq!(calls[0].name, "get_weather");
1703    }
1704
1705    #[test]
1706    fn parse_response_incomplete_max_output_tokens_is_length() {
1707        let value = json!({
1708            "id": "resp_123",
1709            "status": "incomplete",
1710            "incomplete_details": { "reason": "max_output_tokens" },
1711            "output": [],
1712        });
1713        let resp = parse_response(&value, None);
1714        assert_eq!(
1715            resp.finish_reason,
1716            Some(FinishReason::new(FinishReason::LENGTH))
1717        );
1718    }
1719
1720    #[test]
1721    fn parse_response_reasoning_becomes_text_reasoning() {
1722        let value = json!({
1723            "id": "resp_123",
1724            "status": "completed",
1725            "output": [
1726                { "type": "reasoning", "summary": [{ "type": "summary_text", "text": "thinking..." }] },
1727                { "type": "message", "role": "assistant", "content": [{ "type": "output_text", "text": "done" }] },
1728            ],
1729        });
1730        let resp = parse_response(&value, None);
1731        let contents = &resp.messages[0].contents;
1732        assert!(matches!(&contents[0], Content::TextReasoning(t) if t.text == "thinking..."));
1733        assert!(matches!(&contents[1], Content::Text(t) if t.text == "done"));
1734    }
1735
1736    #[test]
1737    fn reasoning_item_round_trips_through_input_for_store_false_replay() {
1738        // A store:false tool-loop replay must re-send the original reasoning
1739        // item (id + encrypted_content), not just its summary text.
1740        let reasoning_item = json!({
1741            "type": "reasoning",
1742            "id": "rs_abc",
1743            "encrypted_content": "ENC",
1744            "summary": [{ "type": "summary_text", "text": "thinking..." }],
1745        });
1746        let value = json!({
1747            "id": "resp_1",
1748            "status": "completed",
1749            "output": [
1750                reasoning_item,
1751                { "type": "function_call", "call_id": "c1", "name": "f", "arguments": "{}" },
1752            ],
1753        });
1754        let resp = parse_response(&value, Some(false));
1755        // The parsed reasoning content preserves the raw item.
1756        let reasoning = resp.messages[0]
1757            .contents
1758            .iter()
1759            .find_map(|c| match c {
1760                Content::TextReasoning(t) => Some(t),
1761                _ => None,
1762            })
1763            .expect("reasoning content");
1764        assert_eq!(
1765            reasoning.raw_representation.as_ref().unwrap()["id"],
1766            "rs_abc"
1767        );
1768
1769        // Replaying that assistant message back through the input mapper
1770        // re-emits the reasoning item verbatim, ahead of the function call.
1771        let input = messages_to_input(&resp.messages);
1772        let reasoning_pos = input
1773            .iter()
1774            .position(|i| i.get("type") == Some(&json!("reasoning")))
1775            .expect("reasoning item re-emitted");
1776        assert_eq!(input[reasoning_pos]["id"], "rs_abc");
1777        assert_eq!(input[reasoning_pos]["encrypted_content"], "ENC");
1778        let call_pos = input
1779            .iter()
1780            .position(|i| i.get("type") == Some(&json!("function_call")))
1781            .expect("function call present");
1782        assert!(reasoning_pos < call_pos, "reasoning must precede the call");
1783    }
1784
1785    #[test]
1786    fn summary_only_reasoning_is_not_re_emitted_as_input() {
1787        // A reasoning content with no preserved raw item (e.g. from streaming
1788        // display) has no valid input form and must be dropped, not sent as a
1789        // bogus reasoning item lacking id/encrypted_content.
1790        let msg = Message::with_contents(
1791            Role::assistant(),
1792            vec![Content::TextReasoning(TextReasoningContent {
1793                text: "just display".into(),
1794                annotations: None,
1795                raw_representation: None,
1796                protected_data: None,
1797            })],
1798        );
1799        let input = messages_to_input(&[msg]);
1800        assert!(input
1801            .iter()
1802            .all(|i| i.get("type") != Some(&json!("reasoning"))));
1803    }
1804
1805    // endregion
1806
1807    // region: streaming
1808
1809    fn sse_body(events: &[(&str, Value)]) -> String {
1810        let mut out = String::new();
1811        for (event, data) in events {
1812            out.push_str(&format!("event: {event}\ndata: {}\n\n", data));
1813        }
1814        out
1815    }
1816
1817    async fn collect_updates(text: String) -> Vec<ChatResponseUpdate> {
1818        // Build a fake reqwest::Response backed by the given SSE text using a
1819        // tiny local HTTP server would be overkill; instead we drive the
1820        // event parser directly through the same state machine by feeding
1821        // the byte stream via `futures::stream::once`.
1822        let stream =
1823            futures::stream::once(async move { Ok::<_, reqwest::Error>(bytes::Bytes::from(text)) });
1824        let byte_stream: ByteStream = Box::pin(stream);
1825        let mut state = ResponsesSseState {
1826            byte_stream,
1827            buffer: String::new(),
1828            utf8: Utf8StreamDecoder::new(),
1829            queued: VecDeque::new(),
1830            call_ids: HashMap::new(),
1831            done: false,
1832            store: None,
1833        };
1834        let mut updates = Vec::new();
1835        // Drain the single chunk manually (mirrors the real unfold body).
1836        if let Some(Ok(bytes)) = state.byte_stream.next().await {
1837            let decoded = state.utf8.push(&bytes);
1838            state.buffer.push_str(&decoded);
1839            while let Some(pos) = state.buffer.find('\n') {
1840                let line = state.buffer[..pos].trim().to_string();
1841                state.buffer.drain(..=pos);
1842                let Some(data) = line.strip_prefix("data:") else {
1843                    continue;
1844                };
1845                let data = data.trim();
1846                if data.is_empty() {
1847                    continue;
1848                }
1849                let value: Value = serde_json::from_str(data).unwrap();
1850                if let EventOutcome::Update(u) =
1851                    parse_responses_event(&value, &mut state.call_ids, state.store)
1852                {
1853                    updates.push(u);
1854                }
1855            }
1856        }
1857        updates
1858    }
1859
1860    #[tokio::test]
1861    async fn stream_text_only_accumulates() {
1862        let text = sse_body(&[
1863            (
1864                "response.created",
1865                json!({ "type": "response.created", "response": { "id": "resp_1", "model": "gpt-4o-mini" } }),
1866            ),
1867            (
1868                "response.output_text.delta",
1869                json!({ "type": "response.output_text.delta", "delta": "Hel" }),
1870            ),
1871            (
1872                "response.output_text.delta",
1873                json!({ "type": "response.output_text.delta", "delta": "lo!" }),
1874            ),
1875            (
1876                "response.completed",
1877                json!({ "type": "response.completed", "response": { "id": "resp_1", "model": "gpt-4o-mini", "status": "completed", "output": [], "usage": { "input_tokens": 3, "output_tokens": 2 } } }),
1878            ),
1879        ]);
1880        let updates = collect_updates(text).await;
1881        let resp = ChatResponse::from_updates(updates);
1882        assert_eq!(resp.text(), "Hello!");
1883        assert_eq!(resp.response_id.as_deref(), Some("resp_1"));
1884        assert_eq!(resp.finish_reason, Some(FinishReason::stop()));
1885        let usage = resp.usage_details.unwrap();
1886        assert_eq!(usage.input_token_count, Some(3));
1887        assert_eq!(usage.output_token_count, Some(2));
1888    }
1889
1890    #[tokio::test]
1891    async fn stream_tool_call_accumulates_arguments() {
1892        let text = sse_body(&[
1893            (
1894                "response.output_item.added",
1895                json!({ "type": "response.output_item.added", "output_index": 0, "item": { "type": "function_call", "call_id": "call_1", "name": "get_weather" } }),
1896            ),
1897            (
1898                "response.function_call_arguments.delta",
1899                json!({ "type": "response.function_call_arguments.delta", "output_index": 0, "delta": "{\"city\":" }),
1900            ),
1901            (
1902                "response.function_call_arguments.delta",
1903                json!({ "type": "response.function_call_arguments.delta", "output_index": 0, "delta": "\"Paris\"}" }),
1904            ),
1905            (
1906                "response.completed",
1907                json!({ "type": "response.completed", "response": { "id": "resp_2", "status": "completed", "output": [{"type":"function_call","call_id":"call_1","name":"get_weather","arguments":"{\"city\":\"Paris\"}"}] } }),
1908            ),
1909        ]);
1910        let updates = collect_updates(text).await;
1911        let resp = ChatResponse::from_updates(updates);
1912        let calls = resp.function_calls();
1913        assert_eq!(calls.len(), 1);
1914        assert_eq!(calls[0].call_id, "call_1");
1915        assert_eq!(calls[0].name, "get_weather");
1916        assert_eq!(
1917            calls[0].parse_arguments().unwrap().get("city").unwrap(),
1918            &json!("Paris")
1919        );
1920        assert_eq!(resp.finish_reason, Some(FinishReason::tool_calls()));
1921    }
1922
1923    #[tokio::test]
1924    async fn stream_failed_event_is_error() {
1925        let text = sse_body(&[(
1926            "response.failed",
1927            json!({ "type": "response.failed", "response": { "error": { "message": "boom" } } }),
1928        )]);
1929        let stream =
1930            futures::stream::once(async move { Ok::<_, reqwest::Error>(bytes::Bytes::from(text)) });
1931        let byte_stream: ByteStream = Box::pin(stream);
1932        let mut state = ResponsesSseState {
1933            byte_stream,
1934            buffer: String::new(),
1935            utf8: Utf8StreamDecoder::new(),
1936            queued: VecDeque::new(),
1937            call_ids: HashMap::new(),
1938            done: false,
1939            store: None,
1940        };
1941        let bytes = state.byte_stream.next().await.unwrap().unwrap();
1942        let decoded = state.utf8.push(&bytes);
1943        state.buffer.push_str(&decoded);
1944        let mut saw_error = false;
1945        while let Some(pos) = state.buffer.find('\n') {
1946            let line = state.buffer[..pos].trim().to_string();
1947            state.buffer.drain(..=pos);
1948            let Some(data) = line.strip_prefix("data:") else {
1949                continue;
1950            };
1951            let data = data.trim();
1952            if data.is_empty() {
1953                continue;
1954            }
1955            let value: Value = serde_json::from_str(data).unwrap();
1956            if let EventOutcome::Error(e) =
1957                parse_responses_event(&value, &mut state.call_ids, state.store)
1958            {
1959                assert!(e.to_string().contains("boom"));
1960                saw_error = true;
1961            }
1962        }
1963        assert!(
1964            saw_error,
1965            "expected a response.failed event to surface an error"
1966        );
1967    }
1968
1969    #[test]
1970    fn stream_failed_event_classifies_content_filter() {
1971        // Streamed and non-streamed Responses failures must classify alike
1972        // (mirrors `response_failure_error`).
1973        let mut call_ids = HashMap::new();
1974        let filtered = parse_responses_event(
1975            &json!({
1976                "type": "response.failed",
1977                "response": { "error": { "code": "content_filter", "message": "blocked" } }
1978            }),
1979            &mut call_ids,
1980            None,
1981        );
1982        let EventOutcome::Error(err) = filtered else {
1983            panic!("expected an error outcome");
1984        };
1985        assert!(matches!(err, Error::ServiceContentFilter { .. }));
1986
1987        let generic = parse_responses_event(
1988            &json!({
1989                "type": "response.failed",
1990                "response": { "error": { "code": "server_error", "message": "boom" } }
1991            }),
1992            &mut call_ids,
1993            None,
1994        );
1995        let EventOutcome::Error(err) = generic else {
1996            panic!("expected an error outcome");
1997        };
1998        assert!(matches!(err, Error::Service(_)));
1999    }
2000
2001    // endregion
2002
2003    // region: env-var constructor
2004
2005    /// Guards `OPENAI_API_KEY` / `OPENAI_BASE_URL` mutation: `cargo test` runs
2006    /// tests in the same process on multiple threads, and env vars are
2007    /// process-global, so concurrent set/remove across tests would be racy
2008    /// without serializing access.
2009    static ENV_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());
2010
2011    #[test]
2012    fn from_env_reads_api_key_and_base_url() {
2013        let _guard = ENV_MUTEX.lock().unwrap();
2014        // SAFETY: serialized by ENV_MUTEX against the other env-var test in
2015        // this module; no other test in this crate touches these variables.
2016        unsafe {
2017            std::env::set_var("OPENAI_API_KEY", "sk-test-123");
2018            std::env::set_var("OPENAI_BASE_URL", "https://example.test/v1");
2019        }
2020        let client = OpenAIChatClient::from_env("gpt-4o-mini").unwrap();
2021        assert_eq!(client.inner.api_key, "sk-test-123");
2022        assert_eq!(client.inner.base_url, "https://example.test/v1");
2023        unsafe {
2024            std::env::remove_var("OPENAI_API_KEY");
2025            std::env::remove_var("OPENAI_BASE_URL");
2026        }
2027    }
2028
2029    #[test]
2030    fn from_env_errors_when_api_key_missing() {
2031        let _guard = ENV_MUTEX.lock().unwrap();
2032        // SAFETY: serialized by ENV_MUTEX; see above.
2033        unsafe {
2034            std::env::remove_var("OPENAI_API_KEY");
2035            std::env::remove_var("OPENAI_BASE_URL");
2036        }
2037        let result = OpenAIChatClient::from_env("gpt-4o-mini");
2038        assert!(result.is_err());
2039    }
2040    #[test]
2041    fn build_body_maps_hosted_tools_to_responses_types() {
2042        use agent_framework_core::tools::{
2043            hosted_code_interpreter, hosted_file_search, hosted_mcp, hosted_web_search,
2044        };
2045        let c = client();
2046        let mut options = ChatOptions::new();
2047        options.tools = vec![
2048            hosted_web_search(),
2049            hosted_code_interpreter(),
2050            hosted_file_search(Some(7)),
2051            hosted_mcp(
2052                "docs",
2053                "https://mcp.example/sse",
2054                Some(vec!["search".into()]),
2055            ),
2056        ];
2057        let body = c.build_body(&[user("hi")], &options, false);
2058        let tools = body["tools"].as_array().unwrap();
2059        assert_eq!(tools[0], json!({ "type": "web_search" }));
2060        assert_eq!(
2061            tools[1],
2062            json!({ "type": "code_interpreter", "container": { "type": "auto" } })
2063        );
2064        assert_eq!(tools[2]["type"], "file_search");
2065        assert_eq!(tools[2]["max_num_results"], json!(7));
2066        assert_eq!(tools[3]["type"], "mcp");
2067        assert_eq!(tools[3]["server_url"], "https://mcp.example/sse");
2068        assert_eq!(tools[3]["allowed_tools"], json!(["search"]));
2069        assert_eq!(tools[3]["require_approval"], "never");
2070    }
2071
2072    // endregion
2073
2074    // region: multimodal input (Responses)
2075
2076    #[test]
2077    fn input_image_uri_becomes_input_image_part() {
2078        let msg = user_with(vec![Content::Uri(UriContent {
2079            uri: "https://example.com/cat.png".into(),
2080            media_type: "image/png".into(),
2081        })]);
2082        let input = messages_to_input(&[msg]);
2083        assert_eq!(
2084            input[0],
2085            json!({ "type": "message", "role": "user", "content": [
2086                { "type": "input_image", "image_url": "https://example.com/cat.png", "detail": "auto" }
2087            ]})
2088        );
2089    }
2090
2091    #[test]
2092    fn input_audio_file_and_hosted_file_parts() {
2093        let msg = user_with(vec![
2094            Content::Data(DataContent {
2095                uri: "data:audio/wav;base64,QQ".into(),
2096                media_type: Some("audio/wav".into()),
2097            }),
2098            Content::Data(DataContent {
2099                uri: "data:application/pdf;base64,JV".into(),
2100                media_type: Some("application/pdf".into()),
2101            }),
2102            Content::HostedFile(HostedFileContent {
2103                file_id: "file-123".into(),
2104            }),
2105        ]);
2106        let input = messages_to_input(&[msg]);
2107        // `input_audio.data` is the raw base64 payload (data-URI prefix
2108        // stripped), the same wire rule as Chat Completions; file inputs keep
2109        // the full data URI in `file_data`.
2110        assert_eq!(
2111            input[0]["content"],
2112            json!([
2113                { "type": "input_audio", "input_audio": { "data": "QQ", "format": "wav" } },
2114                { "type": "input_file", "file_data": "data:application/pdf;base64,JV", "filename": "file" },
2115                { "type": "input_file", "file_id": "file-123" },
2116            ])
2117        );
2118    }
2119
2120    #[test]
2121    fn approval_response_becomes_mcp_approval_response_item() {
2122        let resp = FunctionApprovalResponseContent {
2123            approved: true,
2124            id: "appr_1".into(),
2125            function_call: FunctionCallContent::new("appr_1", "search", None),
2126        };
2127        let msg = user_with(vec![Content::FunctionApprovalResponse(resp)]);
2128        let input = messages_to_input(&[msg]);
2129        assert_eq!(
2130            input[0],
2131            json!({
2132                "type": "mcp_approval_response",
2133                "approval_request_id": "appr_1",
2134                "approve": true,
2135            })
2136        );
2137    }
2138
2139    #[test]
2140    fn approval_request_becomes_mcp_approval_request_item() {
2141        let req = FunctionApprovalRequestContent {
2142            id: "appr_1".into(),
2143            function_call: FunctionCallContent::new(
2144                "appr_1",
2145                "search",
2146                Some(FunctionArguments::Raw(r#"{"q":"x"}"#.into())),
2147            ),
2148        };
2149        let msg = Message::with_contents(
2150            Role::assistant(),
2151            vec![Content::FunctionApprovalRequest(req)],
2152        );
2153        let input = messages_to_input(&[msg]);
2154        assert_eq!(
2155            input[0],
2156            json!({
2157                "type": "mcp_approval_request",
2158                "id": "appr_1",
2159                "name": "search",
2160                "arguments": r#"{"q":"x"}"#,
2161            })
2162        );
2163    }
2164
2165    // endregion
2166
2167    // region: output parsing (annotations, code interpreter, images, approvals)
2168
2169    #[test]
2170    fn output_text_url_citation_annotation() {
2171        let contents = parse_item(json!({
2172            "type": "message", "role": "assistant", "content": [{
2173                "type": "output_text", "text": "See source.",
2174                "annotations": [{
2175                    "type": "url_citation", "title": "Src", "url": "https://ex.com",
2176                    "start_index": 0, "end_index": 3,
2177                }],
2178            }],
2179        }));
2180        let Content::Text(t) = &contents[0] else {
2181            panic!("expected text content");
2182        };
2183        let ann = t.annotations.as_ref().unwrap();
2184        assert_eq!(ann[0].title.as_deref(), Some("Src"));
2185        assert_eq!(ann[0].url.as_deref(), Some("https://ex.com"));
2186        let region = &ann[0].annotated_regions.as_ref().unwrap()[0];
2187        assert_eq!(region.start_index, Some(0));
2188        assert_eq!(region.end_index, Some(3));
2189    }
2190
2191    #[test]
2192    fn output_text_file_and_container_citations() {
2193        let contents = parse_item(json!({
2194            "type": "message", "role": "assistant", "content": [{
2195                "type": "output_text", "text": "x",
2196                "annotations": [
2197                    { "type": "file_citation", "filename": "doc.pdf", "file_id": "file-1", "index": 2 },
2198                    { "type": "file_path", "file_id": "file-2", "index": 0 },
2199                    { "type": "container_file_citation", "filename": "c.txt", "file_id": "file-3",
2200                      "container_id": "cont-1", "start_index": 1, "end_index": 4 },
2201                ],
2202            }],
2203        }));
2204        let Content::Text(t) = &contents[0] else {
2205            panic!("expected text content");
2206        };
2207        let ann = t.annotations.as_ref().unwrap();
2208        assert_eq!(ann[0].url.as_deref(), Some("doc.pdf"));
2209        assert_eq!(ann[0].file_id.as_deref(), Some("file-1"));
2210        assert_eq!(ann[1].file_id.as_deref(), Some("file-2"));
2211        assert_eq!(ann[2].file_id.as_deref(), Some("file-3"));
2212        assert_eq!(ann[2].url.as_deref(), Some("c.txt"));
2213        assert_eq!(
2214            ann[2].annotated_regions.as_ref().unwrap()[0].end_index,
2215            Some(4)
2216        );
2217    }
2218
2219    #[test]
2220    fn code_interpreter_outputs_become_text_and_uri() {
2221        let contents = parse_item(json!({
2222            "type": "code_interpreter_call",
2223            "outputs": [
2224                { "type": "logs", "logs": "hello stdout" },
2225                { "type": "image", "url": "https://ex.com/plot.png" },
2226            ],
2227        }));
2228        assert!(matches!(&contents[0], Content::Text(t) if t.text == "hello stdout"));
2229        assert!(
2230            matches!(&contents[1], Content::Uri(u) if u.uri == "https://ex.com/plot.png" && u.media_type == "image")
2231        );
2232    }
2233
2234    #[test]
2235    fn code_interpreter_without_outputs_falls_back_to_code() {
2236        let contents = parse_item(json!({
2237            "type": "code_interpreter_call", "code": "print(1)",
2238        }));
2239        assert!(matches!(&contents[0], Content::Text(t) if t.text == "print(1)"));
2240    }
2241
2242    #[test]
2243    fn image_generation_raw_base64_becomes_png_data() {
2244        let contents = parse_item(json!({
2245            "type": "image_generation_call", "result": "AAAABBBB",
2246        }));
2247        let Content::Data(d) = &contents[0] else {
2248            panic!("expected data content");
2249        };
2250        assert_eq!(d.uri, "data:image/png;base64,AAAABBBB");
2251        assert_eq!(d.media_type.as_deref(), Some("image/png"));
2252    }
2253
2254    #[test]
2255    fn image_generation_data_uri_keeps_stated_media_type() {
2256        let contents = parse_item(json!({
2257            "type": "image_generation_call", "result": "data:image/webp;base64,ZZZ",
2258        }));
2259        let Content::Data(d) = &contents[0] else {
2260            panic!("expected data content");
2261        };
2262        assert_eq!(d.uri, "data:image/webp;base64,ZZZ");
2263        assert_eq!(d.media_type.as_deref(), Some("image/webp"));
2264    }
2265
2266    #[test]
2267    fn mcp_approval_request_output_round_trips_into_response() {
2268        let contents = parse_item(json!({
2269            "type": "mcp_approval_request",
2270            "id": "appr_9", "name": "search", "arguments": r#"{"q":"rust"}"#,
2271            "server_label": "docs",
2272        }));
2273        let Content::FunctionApprovalRequest(req) = &contents[0] else {
2274            panic!("expected approval request");
2275        };
2276        assert_eq!(req.id, "appr_9");
2277        assert_eq!(req.function_call.call_id, "appr_9");
2278        assert_eq!(req.function_call.name, "search");
2279
2280        // The id round-trips into the request's response item (item 5).
2281        let msg = user_with(vec![Content::FunctionApprovalResponse(
2282            req.create_response(true),
2283        )]);
2284        let input = messages_to_input(&[msg]);
2285        assert_eq!(input[0]["type"], json!("mcp_approval_response"));
2286        assert_eq!(input[0]["approval_request_id"], json!("appr_9"));
2287        assert_eq!(input[0]["approve"], json!(true));
2288    }
2289
2290    // endregion
2291
2292    // region: streaming reasoning
2293
2294    fn reasoning_event(value: Value) -> EventOutcome {
2295        let mut ids = HashMap::new();
2296        parse_responses_event(&value, &mut ids, None)
2297    }
2298
2299    #[test]
2300    fn a_replayed_refusal_becomes_a_refusal_content_part() {
2301        // Beside `output_text` in an output message, a refusal has its own
2302        // part type. Re-emitting it as `output_text` replays a decline to the
2303        // provider as an answer.
2304        let input = messages_to_input(&[Message::with_contents(
2305            Role::assistant(),
2306            vec![
2307                Content::Text(TextContent::new("here is half")),
2308                Content::Text(TextContent::refusal("I can't do the rest")),
2309            ],
2310        )]);
2311        let content = &input[0]["content"];
2312        assert_eq!(
2313            content[0],
2314            json!({"type": "output_text", "text": "here is half"})
2315        );
2316        assert_eq!(
2317            content[1],
2318            json!({"type": "refusal", "refusal": "I can't do the rest"})
2319        );
2320    }
2321
2322    #[test]
2323    fn a_refusal_on_a_non_assistant_role_is_dropped_rather_than_mis_encoded() {
2324        // An *input* message's content list admits only
2325        // input_text/input_image/input_file — there is no refusal part — so
2326        // emitting one would be rejected by the API. Dropping it is the only
2327        // correct option, and it cannot arise from a parsed provider response
2328        // anyway (refusals are assistant output).
2329        let input = messages_to_input(&[Message::with_contents(
2330            Role::user(),
2331            vec![
2332                Content::Text(TextContent::new("hello")),
2333                Content::Text(TextContent::refusal("stray")),
2334            ],
2335        )]);
2336        let content = &input[0]["content"];
2337        assert_eq!(content.as_array().map(Vec::len), Some(1));
2338        assert_eq!(content[0], json!({"type": "input_text", "text": "hello"}));
2339    }
2340
2341    #[test]
2342    fn a_streamed_refusal_is_marked_and_its_done_event_is_not_duplicated() {
2343        // Before this, `response.refusal.delta` fell through the catch-all
2344        // and a streamed decline reached the caller as an empty response —
2345        // the non-streaming path marked refusals while the streaming path
2346        // dropped them.
2347        let EventOutcome::Update(delta) =
2348            reasoning_event(json!({ "type": "response.refusal.delta", "delta": "I can't" }))
2349        else {
2350            panic!("expected update");
2351        };
2352        assert!(matches!(&delta.contents[0], Content::Text(t) if t.refusal && t.text == "I can't"));
2353
2354        // `.done` repeats the whole refusal; emitting it would double the
2355        // text already streamed, exactly as for the text channel.
2356        let done =
2357            reasoning_event(json!({ "type": "response.refusal.done", "refusal": "I can't help" }));
2358        assert!(matches!(done, EventOutcome::None));
2359    }
2360
2361    #[test]
2362    fn reasoning_text_delta_streams_and_done_is_terminal_metadata() {
2363        let EventOutcome::Update(delta) =
2364            reasoning_event(json!({ "type": "response.reasoning_text.delta", "delta": "Th" }))
2365        else {
2366            panic!("expected update");
2367        };
2368        assert!(matches!(&delta.contents[0], Content::TextReasoning(t) if t.text == "Th"));
2369
2370        // `.done` carries the full text the deltas already streamed — emitting
2371        // it again would duplicate the reasoning in the aggregate.
2372        let done =
2373            reasoning_event(json!({ "type": "response.reasoning_text.done", "text": "Think" }));
2374        assert!(matches!(done, EventOutcome::None));
2375    }
2376
2377    #[test]
2378    fn reasoning_summary_text_events_map_to_reasoning_content() {
2379        let EventOutcome::Update(delta) = reasoning_event(
2380            json!({ "type": "response.reasoning_summary_text.delta", "delta": "sum" }),
2381        ) else {
2382            panic!("expected update");
2383        };
2384        assert!(matches!(&delta.contents[0], Content::TextReasoning(t) if t.text == "sum"));
2385
2386        let done = reasoning_event(
2387            json!({ "type": "response.reasoning_summary_text.done", "text": "summary" }),
2388        );
2389        assert!(matches!(done, EventOutcome::None));
2390    }
2391
2392    #[test]
2393    fn response_failure_error_classifies_content_filter() {
2394        let filtered = json!({
2395            "status": "failed",
2396            "error": { "code": "content_filter", "message": "blocked" }
2397        });
2398        assert!(matches!(
2399            response_failure_error(&filtered),
2400            Some(Error::ServiceContentFilter { .. })
2401        ));
2402
2403        let generic = json!({
2404            "status": "failed",
2405            "error": { "code": "server_error", "message": "boom" }
2406        });
2407        assert!(matches!(
2408            response_failure_error(&generic),
2409            Some(Error::Service(_))
2410        ));
2411
2412        assert!(response_failure_error(&json!({ "status": "completed" })).is_none());
2413    }
2414
2415    #[test]
2416    fn input_audio_data_strips_data_uri_prefix() {
2417        let part = content_to_input_part("data:audio/wav;base64,QUJD", Some("audio/wav"))
2418            .expect("audio part");
2419        assert_eq!(part["input_audio"]["data"], "QUJD");
2420        assert_eq!(part["input_audio"]["format"], "wav");
2421    }
2422
2423    // endregion
2424
2425    // region: hosted-tool config passthrough
2426
2427    fn hosted(kind: ToolKind, name: &str, params: Value) -> ToolDefinition {
2428        ToolDefinition {
2429            name: name.into(),
2430            description: String::new(),
2431            parameters: params,
2432            kind,
2433            approval_mode: ApprovalMode::NeverRequire,
2434            executor: None,
2435        }
2436    }
2437
2438    #[test]
2439    fn web_search_passes_through_user_location() {
2440        let tool = hosted(
2441            ToolKind::HostedWebSearch,
2442            "web_search",
2443            json!({ "user_location": { "city": "Paris", "country": "FR" } }),
2444        );
2445        assert_eq!(
2446            tool_to_responses_spec(&tool),
2447            json!({
2448                "type": "web_search",
2449                "user_location": { "type": "approximate", "city": "Paris", "country": "FR" },
2450            })
2451        );
2452    }
2453
2454    #[test]
2455    fn file_search_passes_vector_store_ids_and_max_results_param() {
2456        let tool = hosted(
2457            ToolKind::HostedFileSearch { max_results: None },
2458            "file_search",
2459            json!({ "vector_store_ids": ["vs_1"], "max_results": 12 }),
2460        );
2461        let spec = tool_to_responses_spec(&tool);
2462        assert_eq!(spec["vector_store_ids"], json!(["vs_1"]));
2463        assert_eq!(spec["max_num_results"], json!(12));
2464    }
2465
2466    #[test]
2467    fn image_generation_maps_to_responses_tool_with_passthrough_params() {
2468        let tool = hosted(
2469            ToolKind::HostedImageGeneration,
2470            "image_generation",
2471            json!({ "size": "1024x1024", "quality": "high" }),
2472        );
2473        let spec = tool_to_responses_spec(&tool);
2474        assert_eq!(spec["type"], json!("image_generation"));
2475        assert_eq!(spec["size"], json!("1024x1024"));
2476        assert_eq!(spec["quality"], json!("high"));
2477    }
2478
2479    #[test]
2480    fn code_interpreter_passes_file_ids_and_container_override() {
2481        let with_files = hosted(
2482            ToolKind::HostedCodeInterpreter,
2483            "ci",
2484            json!({ "file_ids": ["file-1", "file-2"] }),
2485        );
2486        assert_eq!(
2487            tool_to_responses_spec(&with_files)["container"],
2488            json!({ "type": "auto", "file_ids": ["file-1", "file-2"] })
2489        );
2490        let with_container = hosted(
2491            ToolKind::HostedCodeInterpreter,
2492            "ci",
2493            json!({ "container": { "type": "secure", "id": "c1" } }),
2494        );
2495        assert_eq!(
2496            tool_to_responses_spec(&with_container)["container"],
2497            json!({ "type": "secure", "id": "c1" })
2498        );
2499    }
2500
2501    #[test]
2502    fn mcp_passes_headers_and_string_approval_mode_override() {
2503        let tool = hosted(
2504            ToolKind::HostedMcp {
2505                url: "https://mcp/sse".into(),
2506                allowed_tools: None,
2507            },
2508            "docs",
2509            json!({ "headers": { "Authorization": "Bearer x" }, "approval_mode": "always_require" }),
2510        );
2511        // The enum default is NeverRequire; the parameter overrides it.
2512        let spec = tool_to_responses_spec(&tool);
2513        assert_eq!(spec["headers"], json!({ "Authorization": "Bearer x" }));
2514        assert_eq!(spec["require_approval"], json!("always"));
2515    }
2516
2517    #[test]
2518    fn mcp_object_approval_mode_maps_to_tool_name_lists() {
2519        let tool = hosted(
2520            ToolKind::HostedMcp {
2521                url: "https://mcp/sse".into(),
2522                allowed_tools: None,
2523            },
2524            "docs",
2525            json!({ "approval_mode": { "always": ["delete"], "never": ["read"] } }),
2526        );
2527        assert_eq!(
2528            tool_to_responses_spec(&tool)["require_approval"],
2529            json!({ "always": { "tool_names": ["delete"] }, "never": { "tool_names": ["read"] } })
2530        );
2531    }
2532
2533    // endregion
2534}