1use serde::Serialize;
7use serde_json::{Value, json};
8
9use crate::LlmResponseChunk;
10use crate::codecs::common::{
11 collect_responses_reasoning_text, encrypted_reasoning_data, encrypted_reasoning_item_id,
12};
13use crate::codecs::stream::{
14 StreamCodec, StreamTranslationState, record_source_identity,
15 target_message_id_or_source_message_id, target_model_or_source_model,
16};
17use crate::format::{FormatId, WireFormat};
18use crate::llm::Usage;
19
20use super::buffered::encode_responses_usage;
21
22pub struct OpenAiResponsesStreamCodec;
24
25impl StreamCodec for OpenAiResponsesStreamCodec {
26 fn format(&self) -> FormatId {
27 WireFormat::OpenAiResponses.into()
28 }
29
30 fn is_terminal_event(&self, event: &Value) -> bool {
31 matches!(
32 event
33 .get("type")
34 .or_else(|| event.get("event"))
35 .and_then(Value::as_str),
36 Some("response.completed" | "response.incomplete" | "response.failed")
37 )
38 }
39
40 fn stops_at_terminal_event(&self) -> bool {
41 true
42 }
43
44 fn sse_done_is_terminal(&self) -> bool {
45 true
46 }
47
48 fn stamp_streamed_response_model(&self, event: &mut Value, served_model: &str) {
49 if let Some(response) = event.get_mut("response").and_then(Value::as_object_mut) {
50 response.insert("model".to_string(), Value::String(served_model.to_string()));
51 }
52 }
53
54 fn decode_event(
55 &self,
56 state: &mut StreamTranslationState,
57 event: &Value,
58 ) -> Vec<LlmResponseChunk> {
59 decode_responses_stream(state, event)
60 }
61
62 fn encode_event(
63 &self,
64 state: &mut StreamTranslationState,
65 event: LlmResponseChunk,
66 ) -> Vec<Value> {
67 let events = encode_responses_stream(state, event);
68 add_sequence_numbers(state, events)
69 }
70
71 fn observe_replayed_event(
72 &self,
73 state: &mut StreamTranslationState,
74 raw: &Value,
75 normalized: Vec<LlmResponseChunk>,
76 ) {
77 let replayed_terminal = normalized
78 .iter()
79 .any(|chunk| matches!(chunk, LlmResponseChunk::MessageStop { .. }));
80 for chunk in normalized {
83 drop(encode_responses_stream(state, chunk));
84 }
85 state.response_sequence_number = raw
86 .get("sequence_number")
87 .and_then(Value::as_u64)
88 .map_or(state.response_sequence_number.saturating_add(1), |number| {
89 number.saturating_add(1)
90 });
91 if replayed_terminal {
92 state.finished = true;
93 }
94 }
95
96 fn finish(&self, state: &mut StreamTranslationState) -> Vec<Value> {
97 let events = finish_responses_stream(state);
98 add_sequence_numbers(state, events)
99 }
100}
101
102#[derive(Serialize)]
104struct ResponsesStreamResponse {
105 id: String,
106 object: &'static str,
107 created_at: u64,
108 completed_at: Option<u64>,
109 error: Option<Value>,
110 incomplete_details: Option<Value>,
111 instructions: Option<Value>,
112 metadata: Option<Value>,
113 model: String,
114 output: Vec<Value>,
115 parallel_tool_calls: bool,
116 frequency_penalty: Option<f64>,
117 presence_penalty: Option<f64>,
118 status: &'static str,
119 temperature: Option<f64>,
120 tool_choice: &'static str,
121 tools: Vec<Value>,
122 top_p: Option<f64>,
123 usage: Value,
124}
125
126fn decode_responses_stream(
128 state: &mut StreamTranslationState,
129 event: &Value,
130) -> Vec<LlmResponseChunk> {
131 tracing::trace!(target: "switchyard_translation::responses::raw", raw = %event);
134 let event_type = event
135 .get("type")
136 .or_else(|| event.get("event"))
137 .and_then(Value::as_str);
138 match event_type {
139 Some("response.created") => {
140 state.saw_message_start = true;
141 let response = event.get("response").and_then(Value::as_object);
142 if let Some(model) = response
143 .and_then(|response| response.get("model"))
144 .and_then(Value::as_str)
145 {
146 state.model = Some(model.to_string());
147 }
148 if let Some(id) = response
149 .and_then(|response| response.get("id"))
150 .and_then(Value::as_str)
151 {
152 state.message_id = Some(id.to_string());
153 }
154 vec![LlmResponseChunk::MessageStart {
155 id: state.message_id.clone(),
156 model: state.model.clone(),
157 }]
158 }
159 Some("response.output_text.delta" | "response.refusal.delta") => event
160 .get("delta")
161 .and_then(Value::as_str)
162 .map(|text| {
163 if event_type == Some("response.refusal.delta") {
164 state.stop_reason = Some("refusal".to_string());
165 }
166 let index = event
167 .get("output_index")
168 .and_then(Value::as_u64)
169 .unwrap_or(0) as usize;
170 let content_index = event
171 .get("content_index")
172 .and_then(Value::as_u64)
173 .unwrap_or(0) as usize;
174 state
175 .decoded_response_text
176 .entry(index)
177 .or_default()
178 .entry(content_index)
179 .or_default()
180 .push_str(text);
181 vec![LlmResponseChunk::TextDelta {
182 index,
183 text: text.to_string(),
184 }]
185 })
186 .unwrap_or_default(),
187 Some("response.refusal.done") => {
188 let Some(text) = event.get("refusal").and_then(Value::as_str) else {
189 return Vec::new();
190 };
191 let index = event
192 .get("output_index")
193 .and_then(Value::as_u64)
194 .unwrap_or(0) as usize;
195 let content_index = event
196 .get("content_index")
197 .and_then(Value::as_u64)
198 .unwrap_or(0) as usize;
199 decode_responses_refusal(state, index, content_index, text)
200 }
201 Some("response.reasoning_text.delta") | Some("response.reasoning_summary_text.delta") => {
202 event
203 .get("delta")
204 .or_else(|| event.get("text"))
205 .and_then(Value::as_str)
206 .map(|text| {
207 let index = event
208 .get("output_index")
209 .and_then(Value::as_u64)
210 .unwrap_or(0) as usize;
211 state
214 .decoded_reasoning
215 .entry(index)
216 .or_default()
217 .push_str(text);
218 vec![LlmResponseChunk::ReasoningDelta {
219 index,
220 text: text.to_string(),
221 }]
222 })
223 .unwrap_or_default()
224 }
225 Some("response.output_item.added") => decode_responses_output_item_added(event, state),
226 Some("response.function_call_arguments.delta") => {
227 let output_index = event
228 .get("output_index")
229 .and_then(Value::as_u64)
230 .unwrap_or(0);
231 event
232 .get("delta")
233 .and_then(Value::as_str)
234 .filter(|_| {
236 !state
237 .tool_states
238 .get(&(output_index as usize))
239 .is_some_and(|tool| tool.decoded_custom_call)
240 })
241 .map(|delta| {
242 state.decoded_tool_call = true;
243 state
246 .tool_states
247 .entry(output_index as usize)
248 .or_default()
249 .decoded_arguments
250 .push_str(delta);
251 vec![LlmResponseChunk::ToolCallDelta {
252 index: output_index as usize,
253 id: None,
254 name: None,
255 arguments_delta: Some(delta.to_string()),
256 }]
257 })
258 .unwrap_or_default()
259 }
260 Some("response.output_item.done") => decode_responses_output_item_done(event, state),
261 Some("response.reasoning_text.done") | Some("response.reasoning_summary_text.done") => {
262 let index = event
263 .get("output_index")
264 .and_then(Value::as_u64)
265 .unwrap_or(0) as usize;
266 event
267 .get("text")
268 .and_then(Value::as_str)
269 .filter(|text| !text.is_empty())
270 .filter(|_| {
271 state
272 .decoded_reasoning
273 .get(&index)
274 .is_none_or(String::is_empty)
275 })
276 .map(|text| {
277 state.decoded_reasoning.insert(index, text.to_string());
278 vec![LlmResponseChunk::ReasoningDelta {
279 index,
280 text: text.to_string(),
281 }]
282 })
283 .unwrap_or_default()
284 }
285 Some("response.reasoning_summary_part.done") => {
286 let index = event
287 .get("output_index")
288 .and_then(Value::as_u64)
289 .unwrap_or(0) as usize;
290 event
291 .get("part")
292 .and_then(|part| part.get("text"))
293 .and_then(Value::as_str)
294 .filter(|text| !text.is_empty())
295 .filter(|_| {
296 state
297 .decoded_reasoning
298 .get(&index)
299 .is_none_or(String::is_empty)
300 })
301 .map(|text| {
302 state.decoded_reasoning.insert(index, text.to_string());
303 vec![LlmResponseChunk::ReasoningDelta {
304 index,
305 text: text.to_string(),
306 }]
307 })
308 .unwrap_or_default()
309 }
310 Some("response.completed" | "response.incomplete") => {
311 let mut out = Vec::new();
312 if let Some(items) = event
315 .get("response")
316 .and_then(|response| response.get("output"))
317 .and_then(Value::as_array)
318 {
319 for (position, item) in items.iter().enumerate() {
320 if let Some(item) = item.as_object() {
321 out.extend(decode_responses_completed_item(item, position, state));
322 if matches!(out.last(), Some(LlmResponseChunk::DecodeError { .. })) {
323 return out;
324 }
325 }
326 }
327 }
328 decode_terminal_usage(state, event, &mut out);
329 let reason = if event_type == Some("response.incomplete") {
335 let reason = match event
336 .pointer("/response/incomplete_details/reason")
337 .and_then(Value::as_str)
338 {
339 Some("content_filter") => "content_filter",
340 _ => "max_tokens",
341 };
342 Some(reason.to_string())
343 } else {
344 state
345 .stop_reason
346 .clone()
347 .or_else(|| state.decoded_tool_call.then(|| "tool_use".to_string()))
348 };
349 out.push(LlmResponseChunk::MessageStop { reason });
350 out
351 }
352 Some("response.failed") => vec![LlmResponseChunk::StreamError {
353 message: event
354 .get("response")
355 .and_then(|response| response.get("error"))
356 .and_then(|error| error.get("message"))
357 .and_then(Value::as_str)
358 .unwrap_or("unknown Responses stream error")
359 .to_string(),
360 }],
361 Some("error") => vec![LlmResponseChunk::StreamError {
362 message: event
363 .get("message")
364 .and_then(Value::as_str)
365 .unwrap_or("unknown Responses stream error")
366 .to_string(),
367 }],
368 _ => Vec::new(),
369 }
370}
371
372fn encode_responses_stream(
374 state: &mut StreamTranslationState,
375 event: LlmResponseChunk,
376) -> Vec<Value> {
377 if state.errored {
379 return Vec::new();
380 }
381 match event {
382 LlmResponseChunk::MessageStart { id, model } => {
383 record_source_identity(state, id, model);
384 ensure_responses_created(state)
385 }
386 LlmResponseChunk::TextDelta { text, .. } => encode_responses_text_delta(state, text),
387 LlmResponseChunk::ReasoningDelta { index, text } => {
388 encode_responses_reasoning_delta(state, index, text)
389 }
390 LlmResponseChunk::ReasoningDetailsDelta {
391 index,
392 details,
393 text,
394 } => {
395 let data = encrypted_reasoning_data(&details);
398 let item = state.response_reasoning.entry(index).or_default();
399 for detail in &details {
400 if detail.get("type").and_then(Value::as_str) == Some("anthropic.signature_delta")
401 && let Some(signature) = detail.get("signature").and_then(Value::as_str)
402 {
403 item.anthropic_signature
404 .get_or_insert_default()
405 .push_str(signature);
406 }
407 }
408 match reasoning_details_item_id(&details) {
409 Some(id) if item.started && item.item_id.as_deref() != Some(id.as_str()) => {
410 tracing::warn!(
413 item_id = %id,
414 "encrypted reasoning arrived after its item opened under a different id; dropping payload"
415 );
416 }
417 id => {
418 if id.is_some() {
419 item.item_id = id;
420 }
421 if data.is_some() {
422 item.encrypted = data;
423 }
424 }
425 }
426 if !item.started && item.item_id.is_none() && summary_lacks_item_id(&details) {
430 item.pending_text.push_str(&text);
431 return ensure_responses_created(state);
432 }
433 let mut out = ensure_responses_reasoning_started(state, index);
434 if !text.is_empty() {
435 out.extend(encode_responses_reasoning_delta(state, index, text));
436 }
437 out
438 }
439 LlmResponseChunk::ToolCallDelta {
440 index,
441 id,
442 name,
443 arguments_delta,
444 } => encode_responses_tool_delta(state, index, id, name, arguments_delta),
445 LlmResponseChunk::Usage(usage) => {
446 state.usage = usage;
447 state.saw_backend_usage = true;
448 Vec::new()
449 }
450 LlmResponseChunk::MessageStop { reason } => {
451 state.stop_reason = reason.or_else(|| state.stop_reason.clone());
452 Vec::new()
453 }
454 LlmResponseChunk::DecodeError { message } | LlmResponseChunk::StreamError { message } => {
455 state.finished = true; state.errored = true; vec![json!({"type": "error", "message": message})]
459 }
460 }
461}
462
463fn finish_responses_stream(state: &mut StreamTranslationState) -> Vec<Value> {
465 if state.finished {
466 return Vec::new();
467 }
468 let is_truncated = matches!(
469 state.stop_reason.as_deref(),
470 Some("length") | Some("max_tokens")
471 );
472 let (event_type, status) = if is_truncated {
473 ("response.incomplete", "incomplete")
474 } else {
475 ("response.completed", "completed")
476 };
477 let incomplete_details = is_truncated.then(|| json!({ "reason": "max_output_tokens" }));
478 let mut out = open_held_reasoning(state, None);
479 out.extend(ensure_responses_created(state));
480 if state.stop_reason.as_deref() == Some("refusal") {
481 out.extend(ensure_responses_message_started(state));
482 }
483 let mut content = Vec::new();
484 if state.response_text_started
485 && let Some(output_index) = state.response_text_output_index
486 {
487 let item_id = responses_item_id(state, "msg", output_index);
488 out.push(json!({
489 "type": "response.output_text.done",
490 "item_id": item_id,
491 "output_index": output_index,
492 "content_index": 0,
493 "text": state.response_text,
494 "logprobs": [],
495 }));
496 out.push(json!({
497 "type": "response.content_part.done",
498 "item_id": item_id,
499 "output_index": output_index,
500 "content_index": 0,
501 "part": {"type": "output_text", "text": state.response_text, "annotations": [], "logprobs": []},
502 }));
503 content.push(json!({"type": "output_text", "text": state.response_text, "annotations": [], "logprobs": []}));
504 }
505 if state.stop_reason.as_deref() == Some("refusal") {
506 let refusal = "";
508 let output_index = state.response_text_output_index.unwrap_or(0);
509 let item_id = responses_item_id(state, "msg", output_index);
510 let content_index = content.len();
511 let part = json!({"type": "refusal", "refusal": refusal});
512 out.push(json!({
513 "type": "response.content_part.added",
514 "item_id": item_id,
515 "output_index": output_index,
516 "content_index": content_index,
517 "part": {"type": "refusal", "refusal": ""},
518 }));
519 out.push(json!({
520 "type": "response.refusal.delta",
521 "item_id": item_id,
522 "output_index": output_index,
523 "content_index": content_index,
524 "delta": refusal,
525 }));
526 out.push(json!({
527 "type": "response.refusal.done",
528 "item_id": item_id,
529 "output_index": output_index,
530 "content_index": content_index,
531 "refusal": refusal,
532 }));
533 out.push(json!({
534 "type": "response.content_part.done",
535 "item_id": item_id,
536 "output_index": output_index,
537 "content_index": content_index,
538 "part": part,
539 }));
540 content.push(part);
541 }
542
543 let mut final_items: Vec<(usize, Value)> = Vec::new();
544 if let Some(output_index) = state.response_text_output_index {
545 let item = json!({
546 "type": "message",
547 "id": responses_item_id(state, "msg", output_index),
548 "role": "assistant",
549 "status": status,
550 "content": content,
551 });
552 out.push(json!({
553 "type": "response.output_item.done",
554 "output_index": output_index,
555 "item": item,
556 }));
557 final_items.push((output_index, item));
558 }
559 let mut reasoning_items: Vec<(usize, usize)> = state
560 .response_reasoning
561 .iter()
562 .filter(|(_, item)| item.started)
563 .filter_map(|(index, item)| item.output_index.map(|output| (*index, output)))
564 .collect();
565 reasoning_items.sort_by_key(|(_, output_index)| *output_index);
566 for (index, output_index) in reasoning_items {
567 let item_id = responses_reasoning_item_id(state, index);
570 let reasoning = &state.response_reasoning[&index];
571 let mut summary = Vec::new();
572 if !reasoning.text.is_empty() {
573 out.push(json!({
574 "type": "response.reasoning_summary_text.done",
575 "item_id": item_id,
576 "output_index": output_index,
577 "summary_index": 0,
578 "text": reasoning.text,
579 }));
580 out.push(json!({
581 "type": "response.reasoning_summary_part.done",
582 "item_id": item_id,
583 "output_index": output_index,
584 "summary_index": 0,
585 "part": {"type": "summary_text", "text": reasoning.text},
586 }));
587 summary.push(json!({
588 "type": "summary_text",
589 "text": reasoning.text,
590 }));
591 }
592 let mut item = json!({
593 "type": "reasoning",
594 "id": item_id,
595 "status": "completed",
596 "summary": summary,
597 });
598 if let Some(signature) = &reasoning.anthropic_signature {
599 item["encrypted_content"] =
600 Value::String(super::encode_anthropic_thinking(&reasoning.text, signature));
601 } else if let Some(encrypted) = &reasoning.encrypted {
602 item["encrypted_content"] = Value::String(encrypted.clone());
603 }
604 out.push(json!({
605 "type": "response.output_item.done",
606 "output_index": output_index,
607 "item": item,
608 }));
609 final_items.push((output_index, item));
610 }
611 for tool in state.tool_states.values() {
612 if !tool.started {
613 continue;
614 }
615 let output_index = tool.response_output_index.unwrap_or(0);
616 let item_id = tool
617 .response_item_id
618 .clone()
619 .unwrap_or_else(|| responses_item_id(state, "fc", output_index));
620 out.push(json!({
621 "type": "response.function_call_arguments.done",
622 "item_id": item_id,
623 "output_index": output_index,
624 "name": tool.name.clone().unwrap_or_default(),
625 "arguments": tool.arguments,
626 }));
627 let item = json!({
628 "type": "function_call",
629 "id": item_id,
630 "call_id": tool.id.clone().unwrap_or_else(|| format!("call_{output_index}")),
631 "name": tool.name.clone().unwrap_or_default(),
632 "arguments": tool.arguments,
633 "status": "completed",
634 });
635 out.push(json!({
636 "type": "response.output_item.done",
637 "output_index": output_index,
638 "item": item,
639 }));
640 final_items.push((output_index, item));
641 }
642
643 final_items.sort_by_key(|(index, _)| *index);
644 let output = final_items
645 .into_iter()
646 .map(|(_, item)| item)
647 .collect::<Vec<_>>();
648
649 out.push(json!({
650 "type": event_type,
651 "response": responses_stream_response(state, status, incomplete_details, output),
652 }));
653 state.finished = true;
654 out
655}
656
657fn decode_responses_reasoning_item(
664 item: &serde_json::Map<String, Value>,
665 index: usize,
666 state: &mut StreamTranslationState,
667) -> Vec<LlmResponseChunk> {
668 let mut out = Vec::new();
669 let has_encrypted = item
670 .get("encrypted_content")
671 .and_then(Value::as_str)
672 .is_some_and(|data| !data.is_empty());
673 if let Some(id) = item
677 .get("id")
678 .and_then(Value::as_str)
679 .filter(|id| !id.is_empty())
680 && !state.decoded_reasoning_ids.contains(&index)
681 {
682 state.decoded_reasoning_ids.insert(index);
683 if !has_encrypted {
684 out.push(LlmResponseChunk::ReasoningDetailsDelta {
685 index,
686 details: vec![json!({"type": "reasoning.encrypted", "id": id})],
687 text: String::new(),
688 });
689 }
690 }
691 let mut parts = Vec::new();
692 collect_responses_reasoning_text(item.get("content"), &mut parts);
693 collect_responses_reasoning_text(item.get("summary"), &mut parts);
694 collect_responses_reasoning_text(item.get("text"), &mut parts);
695 let text = parts.join("\n");
696 let already = state
697 .decoded_reasoning
698 .get(&index)
699 .map(String::as_str)
700 .unwrap_or("");
701 if !text.is_empty() && already.is_empty() {
702 state.decoded_reasoning.insert(index, text.clone());
703 out.push(LlmResponseChunk::ReasoningDelta { index, text });
704 }
705 if let Some(data) = item
706 .get("encrypted_content")
707 .and_then(Value::as_str)
708 .filter(|data| !data.is_empty())
709 && !state.decoded_reasoning_encrypted.contains(&index)
710 {
711 state.decoded_reasoning_encrypted.insert(index);
712 let mut detail = json!({"type": "reasoning.encrypted", "data": data});
714 if let Some(id) = item
715 .get("id")
716 .and_then(Value::as_str)
717 .filter(|id| !id.is_empty())
718 {
719 detail["id"] = Value::String(id.to_string());
720 }
721 out.push(LlmResponseChunk::ReasoningDetailsDelta {
722 index,
723 details: vec![detail],
724 text: String::new(),
725 });
726 }
727 out
728}
729
730fn decode_responses_output_item_added(
731 event: &Value,
732 state: &mut StreamTranslationState,
733) -> Vec<LlmResponseChunk> {
734 let Some(item) = event.get("item").and_then(Value::as_object) else {
735 return Vec::new();
736 };
737 let index = event
738 .get("output_index")
739 .and_then(Value::as_u64)
740 .unwrap_or(0) as usize;
741 if item.get("type").and_then(Value::as_str) == Some("reasoning") {
742 return decode_responses_reasoning_item(item, index, state);
743 }
744 let item_type = item.get("type").and_then(Value::as_str);
745 if item_type != Some("function_call") && item_type != Some("custom_tool_call") {
746 return Vec::new();
747 }
748 state.decoded_tool_call = true;
749 let id = item
750 .get("call_id")
751 .or_else(|| item.get("id"))
752 .and_then(Value::as_str);
753 let name = item.get("name").and_then(Value::as_str);
754 let tool = state.tool_states.entry(index).or_default();
755 tool.has_decoded_identity |=
756 id.is_some_and(|id| !id.is_empty()) && name.is_some_and(|name| !name.is_empty());
757 let custom = item_type == Some("custom_tool_call");
758 tool.decoded_custom_call |= custom;
759 let arguments_delta = if custom {
762 None
763 } else {
764 item.get("arguments")
765 .and_then(Value::as_str)
766 .filter(|arguments| !arguments.is_empty())
767 .map(ToOwned::to_owned)
768 };
769 if let Some(arguments) = arguments_delta.as_deref() {
770 tool.decoded_arguments.push_str(arguments);
771 }
772 vec![LlmResponseChunk::ToolCallDelta {
773 index,
774 id: id.map(ToOwned::to_owned),
775 name: name.map(ToOwned::to_owned),
776 arguments_delta,
777 }]
778}
779
780fn decode_responses_output_item_done(
781 event: &Value,
782 state: &mut StreamTranslationState,
783) -> Vec<LlmResponseChunk> {
784 let Some(item) = event.get("item").and_then(Value::as_object) else {
785 return Vec::new();
786 };
787 let index = event
788 .get("output_index")
789 .and_then(Value::as_u64)
790 .unwrap_or(0) as usize;
791 decode_responses_completed_item(item, index, state)
792}
793
794fn decode_responses_completed_item(
795 item: &serde_json::Map<String, Value>,
796 index: usize,
797 state: &mut StreamTranslationState,
798) -> Vec<LlmResponseChunk> {
799 if item.get("type").and_then(Value::as_str) == Some("reasoning") {
800 return decode_responses_reasoning_item(item, index, state);
801 }
802 let item_type = item.get("type").and_then(Value::as_str);
803 if item_type == Some("message") {
804 let mut out = Vec::new();
805 if let Some(content) = item.get("content").and_then(Value::as_array) {
806 for (content_index, part) in content.iter().enumerate() {
807 let text = match part.get("type").and_then(Value::as_str) {
808 Some("output_text") => part.get("text"),
809 Some("refusal") => {
810 if let Some(text) = part.get("refusal").and_then(Value::as_str) {
811 out.extend(decode_responses_refusal(state, index, content_index, text));
812 if matches!(out.last(), Some(LlmResponseChunk::DecodeError { .. })) {
813 return out;
814 }
815 }
816 continue;
817 }
818 _ => continue,
819 };
820 if let Some(text) = text.and_then(Value::as_str) {
821 let decoded = state
822 .decoded_response_text
823 .entry(index)
824 .or_default()
825 .entry(content_index)
826 .or_default();
827 match snapshot_suffix(decoded, text) {
828 Ok(Some(text)) => out.push(LlmResponseChunk::TextDelta { index, text }),
829 Ok(None) => {}
830 Err(error) => return vec![error],
831 }
832 }
833 }
834 }
835 return out;
836 }
837 if item_type != Some("function_call") && item_type != Some("custom_tool_call") {
838 return Vec::new();
839 }
840 state.decoded_tool_call = true;
841 let custom_arguments = (item_type == Some("custom_tool_call")).then(|| {
842 json!({
843 crate::codex_custom_tools::INPUT_ARGUMENT:
844 item.get("input").and_then(Value::as_str).unwrap_or_default()
845 })
846 .to_string()
847 });
848 let arguments = custom_arguments
849 .as_deref()
850 .or_else(|| item.get("arguments").and_then(Value::as_str));
851 let tool = state.tool_states.entry(index).or_default();
852 let arguments_delta =
853 match arguments.map(|arguments| snapshot_suffix(&mut tool.decoded_arguments, arguments)) {
854 Some(Ok(delta)) => delta,
855 Some(Err(error)) => return vec![error],
856 None => None,
857 };
858 let id = item
859 .get("call_id")
860 .or_else(|| item.get("id"))
861 .and_then(Value::as_str);
862 let name = item.get("name").and_then(Value::as_str);
863 let needs_identity = !tool.has_decoded_identity;
864 tool.has_decoded_identity |=
865 id.is_some_and(|id| !id.is_empty()) && name.is_some_and(|name| !name.is_empty());
866 if needs_identity || arguments_delta.is_some() {
867 return vec![LlmResponseChunk::ToolCallDelta {
868 index,
869 id: id.filter(|_| needs_identity).map(ToOwned::to_owned),
870 name: name.filter(|_| needs_identity).map(ToOwned::to_owned),
871 arguments_delta,
872 }];
873 }
874 Vec::new()
875}
876
877fn decode_responses_refusal(
878 state: &mut StreamTranslationState,
879 index: usize,
880 content_index: usize,
881 text: &str,
882) -> Vec<LlmResponseChunk> {
883 state.stop_reason = Some("refusal".to_string());
884 let decoded = state
885 .decoded_response_text
886 .entry(index)
887 .or_default()
888 .entry(content_index)
889 .or_default();
890 match snapshot_suffix(decoded, text) {
891 Ok(Some(text)) => vec![LlmResponseChunk::TextDelta { index, text }],
892 Ok(None) => Vec::new(),
893 Err(error) => vec![error],
894 }
895}
896
897fn snapshot_suffix(
900 decoded: &mut String,
901 snapshot: &str,
902) -> Result<Option<String>, LlmResponseChunk> {
903 let Some(suffix) = snapshot.strip_prefix(decoded.as_str()) else {
904 return Err(LlmResponseChunk::DecodeError {
905 message: "Responses snapshot conflicts with streamed content".to_string(),
906 });
907 };
908 if suffix.is_empty() {
909 return Ok(None);
910 }
911 decoded.push_str(suffix);
912 Ok(Some(suffix.to_owned()))
913}
914
915fn ensure_responses_created(state: &mut StreamTranslationState) -> Vec<Value> {
917 if state.response_created {
918 return Vec::new();
919 }
920 if state.target_message_id.is_none() {
922 state.target_message_id = Some(responses_id(state));
923 }
924 state.response_created = true;
925 vec![json!({
926 "type": "response.created",
927 "response": responses_stream_response(state, "in_progress", None, Vec::new()),
928 })]
929}
930
931fn responses_stream_response(
933 state: &StreamTranslationState,
934 status: &'static str,
935 incomplete_details: Option<Value>,
936 output: Vec<Value>,
937) -> ResponsesStreamResponse {
938 ResponsesStreamResponse {
939 id: responses_id(state),
940 object: "response",
941 created_at: 0,
942 completed_at: None,
943 error: None,
944 incomplete_details,
945 instructions: None,
946 metadata: None,
947 model: target_model_or_source_model(state),
948 output,
949 parallel_tool_calls: true,
950 frequency_penalty: None,
951 presence_penalty: None,
952 status,
953 temperature: None,
954 tool_choice: "auto",
955 tools: Vec::new(),
956 top_p: None,
957 usage: encode_responses_usage(&state.usage),
958 }
959}
960
961fn add_sequence_numbers(state: &mut StreamTranslationState, mut events: Vec<Value>) -> Vec<Value> {
963 for event in &mut events {
964 if let Some(object) = event.as_object_mut() {
965 object.insert(
966 "sequence_number".to_string(),
967 Value::from(state.response_sequence_number),
968 );
969 state.response_sequence_number = state.response_sequence_number.saturating_add(1);
970 }
971 }
972 events
973}
974
975fn ensure_responses_message_started(state: &mut StreamTranslationState) -> Vec<Value> {
977 let mut out = open_held_reasoning(state, None);
978 out.extend(ensure_responses_created(state));
979 if state.response_text_output_index.is_none() {
980 let output_index = state.next_response_output_index;
981 state.next_response_output_index += 1;
982 state.response_text_output_index = Some(output_index);
983 out.push(json!({
984 "type": "response.output_item.added",
985 "output_index": output_index,
986 "item": {
987 "type": "message",
988 "id": responses_item_id(state, "msg", output_index),
989 "role": "assistant",
990 "status": "in_progress",
991 "content": [],
992 },
993 }));
994 }
995 out
996}
997
998fn encode_responses_text_delta(state: &mut StreamTranslationState, text: String) -> Vec<Value> {
1000 let mut out = ensure_responses_message_started(state);
1001 if !state.response_text_started {
1002 state.response_text_started = true;
1003 let output_index = state.response_text_output_index.unwrap_or(0);
1004 out.push(json!({
1005 "type": "response.content_part.added",
1006 "item_id": responses_item_id(state, "msg", output_index),
1007 "output_index": output_index,
1008 "content_index": 0,
1009 "part": {"type": "output_text", "text": "", "annotations": [], "logprobs": []},
1010 }));
1011 }
1012 state.response_text.push_str(&text);
1013 out.push(json!({
1014 "type": "response.output_text.delta",
1015 "item_id": responses_item_id(state, "msg", state.response_text_output_index.unwrap_or(0)),
1016 "output_index": state.response_text_output_index.unwrap_or(0),
1017 "content_index": 0,
1018 "delta": text,
1019 "logprobs": [],
1020 }));
1021 out
1022}
1023
1024fn reasoning_details_item_id(details: &[Value]) -> Option<String> {
1028 encrypted_reasoning_item_id(details).or_else(|| {
1029 details
1030 .iter()
1031 .filter_map(Value::as_object)
1032 .filter(|detail| {
1033 matches!(
1034 detail.get("type").and_then(Value::as_str),
1035 Some("reasoning.summary" | "reasoning.text")
1036 )
1037 })
1038 .find_map(|detail| {
1039 detail
1040 .get("id")
1041 .and_then(Value::as_str)
1042 .filter(|id| !id.is_empty())
1043 })
1044 .map(ToOwned::to_owned)
1045 })
1046}
1047
1048fn summary_lacks_item_id(details: &[Value]) -> bool {
1050 details.iter().filter_map(Value::as_object).any(|detail| {
1051 detail.get("type").and_then(Value::as_str) == Some("reasoning.summary")
1052 && detail
1053 .get("id")
1054 .and_then(Value::as_str)
1055 .is_none_or(str::is_empty)
1056 })
1057}
1058
1059fn open_held_reasoning(state: &mut StreamTranslationState, before: Option<usize>) -> Vec<Value> {
1064 let held: Vec<usize> = state
1065 .response_reasoning
1066 .iter()
1067 .filter(|(index, item)| {
1068 before.is_none_or(|bound| **index < bound)
1069 && !item.started
1070 && !item.pending_text.is_empty()
1071 })
1072 .map(|(index, _)| *index)
1073 .collect();
1074 let mut out = Vec::new();
1075 for index in held {
1076 out.extend(ensure_responses_reasoning_started(state, index));
1077 }
1078 out
1079}
1080
1081fn responses_reasoning_item_id(state: &StreamTranslationState, index: usize) -> String {
1084 let item = state.response_reasoning.get(&index);
1085 item.and_then(|item| item.item_id.clone())
1086 .unwrap_or_else(|| {
1087 let output_index = item.and_then(|item| item.output_index).unwrap_or(0);
1088 responses_item_id(state, "rs", output_index)
1089 })
1090}
1091
1092fn ensure_responses_reasoning_started(
1094 state: &mut StreamTranslationState,
1095 index: usize,
1096) -> Vec<Value> {
1097 let mut out = ensure_responses_created(state);
1098 if state
1099 .response_reasoning
1100 .get(&index)
1101 .is_some_and(|item| item.started)
1102 {
1103 return out;
1104 }
1105 out.extend(open_held_reasoning(state, Some(index)));
1107 let output_index = state.next_response_output_index;
1108 state.next_response_output_index += 1;
1109 let item = state.response_reasoning.entry(index).or_default();
1110 item.started = true;
1111 item.output_index = Some(output_index);
1112 let pending = std::mem::take(&mut item.pending_text);
1113 let item_id = responses_reasoning_item_id(state, index);
1114 out.push(json!({
1117 "type": "response.output_item.added",
1118 "output_index": output_index,
1119 "item": {
1120 "type": "reasoning",
1121 "id": item_id,
1122 "status": "in_progress",
1123 "summary": [],
1124 },
1125 }));
1126 if !pending.is_empty() {
1128 out.extend(encode_responses_reasoning_delta(state, index, pending));
1129 }
1130 out
1131}
1132
1133fn ensure_responses_reasoning_summary_started(
1137 state: &mut StreamTranslationState,
1138 index: usize,
1139) -> Vec<Value> {
1140 let mut out = ensure_responses_reasoning_started(state, index);
1141 let item_id = responses_reasoning_item_id(state, index);
1142 let item = state.response_reasoning.entry(index).or_default();
1143 if item.summary_started {
1144 return out;
1145 }
1146 item.summary_started = true;
1147 out.push(json!({
1148 "type": "response.reasoning_summary_part.added",
1149 "item_id": item_id,
1150 "output_index": item.output_index.unwrap_or(0),
1151 "summary_index": 0,
1152 "part": {"type": "summary_text", "text": ""},
1153 }));
1154 out
1155}
1156
1157fn encode_responses_reasoning_delta(
1159 state: &mut StreamTranslationState,
1160 index: usize,
1161 text: String,
1162) -> Vec<Value> {
1163 if let Some(item) = state.response_reasoning.get_mut(&index)
1165 && !item.started
1166 && !item.pending_text.is_empty()
1167 {
1168 item.pending_text.push_str(&text);
1169 return Vec::new();
1170 }
1171 if text.is_empty() {
1173 return ensure_responses_reasoning_started(state, index);
1174 }
1175 let mut out = ensure_responses_reasoning_summary_started(state, index);
1176 let item = state.response_reasoning.entry(index).or_default();
1177 item.text.push_str(&text);
1178 let output_index = item.output_index.unwrap_or(0);
1179 out.push(json!({
1180 "type": "response.reasoning_summary_text.delta",
1181 "item_id": responses_reasoning_item_id(state, index),
1182 "output_index": output_index,
1183 "summary_index": 0,
1184 "delta": text,
1185 }));
1186 out
1187}
1188
1189fn encode_responses_tool_delta(
1191 state: &mut StreamTranslationState,
1192 index: usize,
1193 id: Option<String>,
1194 name: Option<String>,
1195 arguments_delta: Option<String>,
1196) -> Vec<Value> {
1197 let mut out = ensure_responses_created(state);
1198 let resp_id = responses_id(state);
1199 let tool = state.tool_states.entry(index).or_default();
1200 if id.is_some() {
1201 tool.id = id;
1202 }
1203 if name.is_some() {
1204 tool.name = name;
1205 }
1206 if let Some(delta) = arguments_delta {
1207 tool.arguments.push_str(&delta);
1208 tool.pending_arguments.push_str(&delta);
1209 }
1210
1211 if !tool.started {
1212 let Some(name) = tool.name.clone() else {
1213 return out;
1214 };
1215 out.extend(open_held_reasoning(state, None));
1217 let output_index = state.next_response_output_index;
1218 state.next_response_output_index += 1;
1219 let tool = state.tool_states.entry(index).or_default();
1220 tool.response_output_index = Some(output_index);
1221 tool.response_item_id = Some(responses_item_id_from(&resp_id, "fc", output_index));
1222 tool.started = true;
1223 out.push(json!({
1224 "type": "response.output_item.added",
1225 "output_index": output_index,
1226 "item": {
1227 "type": "function_call",
1228 "id": tool.response_item_id.clone().unwrap_or_else(|| responses_item_id_from(&resp_id, "fc", output_index)),
1229 "call_id": tool.id.clone().unwrap_or_else(|| format!("call_{index}")),
1230 "name": name,
1231 "arguments": "",
1232 "status": "in_progress",
1233 },
1234 }));
1235 if !tool.pending_arguments.is_empty() {
1236 out.push(json!({
1237 "type": "response.function_call_arguments.delta",
1238 "item_id": tool.response_item_id,
1239 "output_index": output_index,
1240 "delta": tool.pending_arguments,
1241 }));
1242 tool.pending_arguments.clear();
1243 }
1244 return out;
1245 }
1246
1247 if let Some(output_index) = tool.response_output_index
1248 && !tool.pending_arguments.is_empty()
1249 {
1250 out.push(json!({
1251 "type": "response.function_call_arguments.delta",
1252 "item_id": tool.response_item_id,
1253 "output_index": output_index,
1254 "delta": tool.pending_arguments,
1255 }));
1256 tool.pending_arguments.clear();
1257 }
1258 out
1259}
1260
1261fn decode_terminal_usage(
1263 state: &mut StreamTranslationState,
1264 event: &Value,
1265 out: &mut Vec<LlmResponseChunk>,
1266) {
1267 if let Some(usage) = event
1268 .get("response")
1269 .and_then(Value::as_object)
1270 .and_then(|response| response.get("usage"))
1271 .and_then(Value::as_object)
1272 {
1273 let usage = responses_usage(usage);
1274 state.usage = usage.clone();
1275 state.saw_backend_usage = true;
1276 out.push(LlmResponseChunk::Usage(usage));
1277 }
1278}
1279
1280fn responses_usage(usage: &serde_json::Map<String, Value>) -> Usage {
1282 let aggregate_input_tokens = usage.get("input_tokens").and_then(Value::as_u64);
1283 let cached_input_tokens = usage
1284 .get("input_tokens_details")
1285 .and_then(|details| details.get("cached_tokens"))
1286 .and_then(Value::as_u64);
1287 let cache_creation_input_tokens = usage
1288 .get("input_tokens_details")
1289 .and_then(|details| details.get("cache_write_tokens"))
1290 .and_then(Value::as_u64);
1291 let input_tokens = aggregate_input_tokens.map(|tokens| {
1292 tokens
1293 .saturating_sub(cached_input_tokens.unwrap_or(0))
1294 .saturating_sub(cache_creation_input_tokens.unwrap_or(0))
1295 });
1296 let output_tokens = usage.get("output_tokens").and_then(Value::as_u64);
1297 Usage {
1298 provider_cost: crate::codecs::common::provider_cost(usage),
1299 input_tokens,
1300 cache: crate::codecs::common::cache_usage_details(
1301 usage,
1302 cached_input_tokens,
1303 cache_creation_input_tokens,
1304 ),
1305 output_tokens,
1306 total_tokens: usage
1307 .get("total_tokens")
1308 .and_then(Value::as_u64)
1309 .or_else(|| Some(aggregate_input_tokens.unwrap_or(0) + output_tokens.unwrap_or(0))),
1310 reasoning_tokens: usage
1311 .get("output_tokens_details")
1312 .and_then(|details| details.get("reasoning_tokens"))
1313 .or_else(|| {
1314 usage
1315 .get("completion_tokens_details")
1316 .and_then(|details| details.get("reasoning_tokens"))
1317 })
1318 .and_then(Value::as_u64),
1319 }
1320}
1321
1322const ITEM_ID_DISCRIMINATOR_CHARS: usize = 40;
1326
1327fn responses_item_id(state: &StreamTranslationState, prefix: &str, output_index: usize) -> String {
1331 responses_item_id_from(&responses_id(state), prefix, output_index)
1332}
1333
1334fn responses_item_id_from(resp_id: &str, prefix: &str, output_index: usize) -> String {
1337 let resp = resp_id.strip_prefix("resp_").unwrap_or(resp_id);
1338 if resp.chars().count() <= ITEM_ID_DISCRIMINATOR_CHARS {
1339 format!("{prefix}_{resp}_{output_index}")
1340 } else {
1341 format!("{prefix}_{:016x}_{output_index}", fnv1a_64(resp))
1342 }
1343}
1344
1345fn fnv1a_64(text: &str) -> u64 {
1348 text.bytes().fold(0xcbf2_9ce4_8422_2325_u64, |hash, byte| {
1349 (hash ^ u64::from(byte)).wrapping_mul(0x0000_0100_0000_01b3)
1350 })
1351}
1352
1353fn responses_id(state: &StreamTranslationState) -> String {
1355 target_message_id_or_source_message_id(state)
1356 .unwrap_or("resp_switchyard")
1357 .to_string()
1358}
1359
1360#[cfg(test)]
1361mod tests {
1362 use super::*;
1363
1364 #[test]
1365 fn recognizes_stream_completion() {
1366 let codec = OpenAiResponsesStreamCodec;
1367 assert!(codec.sse_done_is_terminal());
1368 for kind in [
1369 "response.completed",
1370 "response.incomplete",
1371 "response.failed",
1372 ] {
1373 assert!(codec.is_terminal_event(&json!({"type": kind})));
1374 assert!(codec.is_terminal_event(&json!({"event": kind})));
1375 }
1376 assert!(!codec.is_terminal_event(&json!({"type": "response.created"})));
1377 }
1378
1379 #[test]
1380 fn responses_replay_stamps_the_served_model_inside_response() {
1381 let mut event = json!({
1382 "type": "response.created",
1383 "response": {
1384 "id": "resp_1",
1385 "model": "provider/model",
1386 "provider_extension": true,
1387 },
1388 });
1389
1390 OpenAiResponsesStreamCodec.stamp_streamed_response_model(&mut event, "served/model");
1391
1392 assert_eq!(event["response"]["model"], "served/model");
1393 assert_eq!(event["response"]["provider_extension"], true);
1394 }
1395}