-
Notifications
You must be signed in to change notification settings - Fork 14.1k
[core] Support interleaved response items #30876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -341,8 +341,11 @@ pub fn process_responses_event( | |
| } | ||
| } | ||
| "response.reasoning_summary_text.delta" => { | ||
| if let (Some(delta), Some(summary_index)) = (event.delta, event.summary_index) { | ||
| if let (Some(item_id), Some(delta), Some(summary_index)) = | ||
| (event.item_id, event.delta, event.summary_index) | ||
|
Comment on lines
+344
to
+345
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For streams that emit Useful? React with 👍 / 👎. |
||
| { | ||
| return Ok(Some(ResponseEvent::ReasoningSummaryDelta { | ||
| item_id, | ||
| delta, | ||
| summary_index, | ||
| })); | ||
|
|
@@ -434,8 +437,9 @@ pub fn process_responses_event( | |
| } | ||
| } | ||
| "response.reasoning_summary_part.added" => { | ||
| if let Some(summary_index) = event.summary_index { | ||
| if let (Some(item_id), Some(summary_index)) = (event.item_id, event.summary_index) { | ||
| return Ok(Some(ResponseEvent::ReasoningSummaryPartAdded { | ||
| item_id, | ||
| summary_index, | ||
| })); | ||
| } | ||
|
|
@@ -788,6 +792,44 @@ mod tests { | |
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn preserves_reasoning_summary_item_ids() { | ||
| let events = run_sse(vec![ | ||
| json!({ | ||
| "type": "response.reasoning_summary_part.added", | ||
| "item_id": "reasoning-1", | ||
| "summary_index": 0 | ||
| }), | ||
| json!({ | ||
| "type": "response.reasoning_summary_text.delta", | ||
| "item_id": "reasoning-1", | ||
| "summary_index": 0, | ||
| "delta": "Checking" | ||
| }), | ||
| json!({ | ||
| "type": "response.completed", | ||
| "response": { "id": "resp1" } | ||
| }), | ||
| ]) | ||
| .await; | ||
|
|
||
| assert_matches!( | ||
| &events[0], | ||
| ResponseEvent::ReasoningSummaryPartAdded { | ||
| item_id, | ||
| summary_index: 0, | ||
| } if item_id == "reasoning-1" | ||
| ); | ||
| assert_matches!( | ||
| &events[1], | ||
| ResponseEvent::ReasoningSummaryDelta { | ||
| item_id, | ||
| delta, | ||
| summary_index: 0, | ||
| } if item_id == "reasoning-1" && delta == "Checking" | ||
| ); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn error_when_missing_completed() { | ||
| let item1 = json!({ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1928,6 +1928,7 @@ async fn try_run_sampling_request( | |
| let mut needs_follow_up = false; | ||
| let mut last_agent_message: Option<String> = None; | ||
| let mut active_item: Option<TurnItem> = None; | ||
| let mut streamed_items = HashMap::<String, TurnItem>::new(); | ||
| let mut active_tool_argument_diff_consumer: Option<( | ||
| String, | ||
| Box<dyn ToolArgumentDiffConsumer>, | ||
|
|
@@ -1986,18 +1987,44 @@ async fn try_run_sampling_request( | |
| match event { | ||
| ResponseEvent::Created => {} | ||
| ResponseEvent::OutputItemDone(item) => { | ||
| if let Some((_, mut consumer)) = active_tool_argument_diff_consumer.take() | ||
| let completes_active_tool_call = active_tool_argument_diff_consumer | ||
| .as_ref() | ||
| .is_some_and(|(active_call_id, _)| { | ||
| matches!( | ||
| &item, | ||
| ResponseItem::CustomToolCall { call_id, .. } | ||
| if call_id == active_call_id | ||
| ) | ||
| }); | ||
| if completes_active_tool_call | ||
| && let Some((_, mut consumer)) = active_tool_argument_diff_consumer.take() | ||
| && let Ok(Some(event)) = consumer.finish() | ||
| { | ||
| sess.send_event(&turn_context, event).await; | ||
| } | ||
| let previously_active_item = active_item.take(); | ||
| let previously_streamed_item = if active_item_is_streaming_to_client { | ||
| previously_active_item | ||
| } else { | ||
| None | ||
| let completed_item_id = item.id().map(str::to_owned); | ||
| let is_tool_call = matches!( | ||
| &item, | ||
| ResponseItem::LocalShellCall { .. } | ||
| | ResponseItem::FunctionCall { .. } | ||
| | ResponseItem::ToolSearchCall { .. } | ||
| | ResponseItem::CustomToolCall { .. } | ||
| ); | ||
| if completed_item_id.is_none() && !is_tool_call { | ||
| warn!("dropping non-tool output_item.done event without item ID"); | ||
| continue; | ||
| } | ||
| let completes_active_item = match (&completed_item_id, active_item.as_ref()) { | ||
| (Some(completed_item_id), Some(active)) => completed_item_id == &active.id(), | ||
| _ => false, | ||
| }; | ||
|
Comment on lines
+2005
to
2020
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a custom tool call is streaming input deltas and another response item completes before that tool call's own Useful? React with 👍 / 👎. |
||
| active_item_is_streaming_to_client = false; | ||
| let previously_streamed_item = completed_item_id | ||
| .as_deref() | ||
| .and_then(|item_id| streamed_items.remove(item_id)); | ||
| if completes_active_item { | ||
| active_item = None; | ||
| active_item_is_streaming_to_client = false; | ||
| } | ||
| if let Some(previous) = previously_streamed_item.as_ref() | ||
| && matches!(previous, TurnItem::AgentMessage(_)) | ||
| { | ||
|
|
@@ -2152,6 +2179,7 @@ async fn try_run_sampling_request( | |
| ) | ||
| .await; | ||
| } | ||
| streamed_items.insert(turn_item.id(), turn_item.clone()); | ||
| } | ||
| active_item = Some(turn_item); | ||
| active_item_is_streaming_to_client = stream_item_to_client; | ||
|
|
@@ -2288,39 +2316,47 @@ async fn try_run_sampling_request( | |
| } | ||
| } | ||
| ResponseEvent::ReasoningSummaryDelta { | ||
| item_id, | ||
| delta, | ||
| summary_index, | ||
| } => { | ||
| if let Some(active) = active_item.as_ref() { | ||
| if !active_item_is_streaming_to_client { | ||
| continue; | ||
| } | ||
| if defer_streamed_turn_items_for_contributors { | ||
| continue; | ||
| } | ||
| if streamed_items.contains_key(&item_id) { | ||
| let event = ReasoningContentDeltaEvent { | ||
| thread_id: sess.thread_id.to_string(), | ||
| turn_id: turn_context.sub_id.clone(), | ||
| item_id: active.id(), | ||
| item_id, | ||
| delta, | ||
| summary_index, | ||
| }; | ||
| sess.send_event(&turn_context, EventMsg::ReasoningContentDelta(event)) | ||
| .await; | ||
| } else { | ||
| error_or_panic("ReasoningSummaryDelta without active item".to_string()); | ||
| error_or_panic(format!( | ||
| "ReasoningSummaryDelta without streamed item {item_id}" | ||
| )); | ||
| } | ||
| } | ||
| ResponseEvent::ReasoningSummaryPartAdded { summary_index } => { | ||
| if let Some(active) = active_item.as_ref() { | ||
| if !active_item_is_streaming_to_client { | ||
| continue; | ||
| } | ||
| ResponseEvent::ReasoningSummaryPartAdded { | ||
| item_id, | ||
| summary_index, | ||
| } => { | ||
| if defer_streamed_turn_items_for_contributors { | ||
| continue; | ||
| } | ||
| if streamed_items.contains_key(&item_id) { | ||
| let event = | ||
| EventMsg::AgentReasoningSectionBreak(AgentReasoningSectionBreakEvent { | ||
| item_id: active.id(), | ||
| item_id, | ||
| summary_index, | ||
| }); | ||
| sess.send_event(&turn_context, event).await; | ||
| } else { | ||
| error_or_panic("ReasoningSummaryPartAdded without active item".to_string()); | ||
| error_or_panic(format!( | ||
| "ReasoningSummaryPartAdded without streamed item {item_id}" | ||
| )); | ||
| } | ||
| } | ||
| ResponseEvent::ReasoningContentDelta { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.