This commit is contained in:
won
2026-05-12 09:20:50 -07:00
parent 5fe33443b0
commit 52b68b803d
14 changed files with 143 additions and 44 deletions
@@ -2728,6 +2728,12 @@
"id": {
"type": "string"
},
"output_hint": {
"type": [
"string",
"null"
]
},
"result": {
"type": "string"
},
@@ -13965,6 +13965,12 @@
"id": {
"type": "string"
},
"output_hint": {
"type": [
"string",
"null"
]
},
"result": {
"type": "string"
},
@@ -10514,6 +10514,12 @@
"id": {
"type": "string"
},
"output_hint": {
"type": [
"string",
"null"
]
},
"result": {
"type": "string"
},
@@ -683,6 +683,12 @@
"id": {
"type": "string"
},
"output_hint": {
"type": [
"string",
"null"
]
},
"result": {
"type": "string"
},
@@ -813,6 +813,12 @@
"id": {
"type": "string"
},
"output_hint": {
"type": [
"string",
"null"
]
},
"result": {
"type": "string"
},
@@ -14,4 +14,4 @@ export type ResponseItem = { "type": "message", role: string, content: Array<Con
/**
* Set when using the Responses API.
*/
call_id: string | null, status: LocalShellStatus, action: LocalShellAction, } | { "type": "function_call", name: string, namespace?: string, arguments: string, call_id: string, } | { "type": "tool_search_call", call_id: string | null, status?: string, execution: string, arguments: unknown, } | { "type": "function_call_output", call_id: string, output: FunctionCallOutputBody, } | { "type": "custom_tool_call", status?: string, call_id: string, name: string, input: string, } | { "type": "custom_tool_call_output", call_id: string, name?: string, output: FunctionCallOutputBody, } | { "type": "tool_search_output", call_id: string | null, status: string, execution: string, tools: unknown[], } | { "type": "web_search_call", status?: string, action?: WebSearchAction, } | { "type": "image_generation_call", id: string, status: string, revised_prompt?: string, result: string, } | { "type": "compaction", encrypted_content: string, } | { "type": "context_compaction", encrypted_content?: string, } | { "type": "other" };
call_id: string | null, status: LocalShellStatus, action: LocalShellAction, } | { "type": "function_call", name: string, namespace?: string, arguments: string, call_id: string, } | { "type": "tool_search_call", call_id: string | null, status?: string, execution: string, arguments: unknown, } | { "type": "function_call_output", call_id: string, output: FunctionCallOutputBody, } | { "type": "custom_tool_call", status?: string, call_id: string, name: string, input: string, } | { "type": "custom_tool_call_output", call_id: string, name?: string, output: FunctionCallOutputBody, } | { "type": "tool_search_output", call_id: string | null, status: string, execution: string, tools: unknown[], } | { "type": "web_search_call", status?: string, action?: WebSearchAction, } | { "type": "image_generation_call", id: string, status: string, revised_prompt?: string, result: string, output_hint?: string, } | { "type": "compaction", encrypted_content: string, } | { "type": "context_compaction", encrypted_content?: string, } | { "type": "other" };
@@ -521,6 +521,7 @@ fn for_prompt_preserves_image_generation_calls_when_images_are_supported() {
status: "generating".to_string(),
revised_prompt: Some("lobster".to_string()),
result: "Zm9v".to_string(),
output_hint: None,
},
ResponseItem::Message {
id: None,
@@ -540,6 +541,7 @@ fn for_prompt_preserves_image_generation_calls_when_images_are_supported() {
status: "generating".to_string(),
revised_prompt: Some("lobster".to_string()),
result: "Zm9v".to_string(),
output_hint: None,
},
ResponseItem::Message {
id: None,
@@ -569,6 +571,9 @@ fn for_prompt_clears_image_generation_result_when_images_are_unsupported() {
status: "completed".to_string(),
revised_prompt: Some("lobster".to_string()),
result: "Zm9v".to_string(),
output_hint: Some(
"Generated images are saved to /tmp as /tmp/ig_123.png by default.".to_string(),
),
},
]);
@@ -588,6 +593,9 @@ fn for_prompt_clears_image_generation_result_when_images_are_unsupported() {
status: "completed".to_string(),
revised_prompt: Some("lobster".to_string()),
result: String::new(),
output_hint: Some(
"Generated images are saved to /tmp as /tmp/ig_123.png by default.".to_string(),
),
},
]
);
+1
View File
@@ -195,6 +195,7 @@ pub fn parse_turn_item(item: &ResponseItem) -> Option<TurnItem> {
status,
revised_prompt,
result,
..
} => Some(TurnItem::ImageGeneration(
codex_protocol::items::ImageGenerationItem {
id: id.clone(),
+19 -14
View File
@@ -6333,6 +6333,10 @@ async fn build_initial_context_omits_default_image_save_location_with_image_hist
status: "completed".to_string(),
revised_prompt: Some("a tiny blue square".to_string()),
result: "Zm9v".to_string(),
output_hint: Some(
"Generated images are saved to /tmp as /tmp/ig-test.png by default."
.to_string(),
),
}],
/*reference_context_item*/ None,
)
@@ -6569,7 +6573,7 @@ async fn build_initial_context_emits_thread_start_skill_warning_on_repeated_buil
}
#[tokio::test]
async fn handle_output_item_done_records_image_save_history_message() {
async fn handle_output_item_done_records_image_save_history_hint() {
let (session, turn_context) = make_session_and_context().await;
let session = Arc::new(session);
let turn_context = Arc::new(turn_context);
@@ -6585,6 +6589,7 @@ async fn handle_output_item_done_records_image_save_history_message() {
status: "completed".to_string(),
revised_prompt: Some("a tiny blue square".to_string()),
result: "Zm9v".to_string(),
output_hint: None,
};
let mut ctx = HandleOutputCtx {
@@ -6598,21 +6603,20 @@ async fn handle_output_item_done_records_image_save_history_message() {
.expect("image generation item should succeed");
let history = session.clone_history().await;
let image_output_path = crate::stream_events_utils::image_generation_artifact_path(
&turn_context.config.codex_home,
&session.conversation_id.to_string(),
"<image_id>",
);
let image_output_dir = image_output_path
let image_output_dir = expected_saved_path
.parent()
.expect("generated image path should have a parent");
let image_message: ResponseItem = crate::context::ContextualUserFragment::into(
crate::context::ImageGenerationInstructions::new(
image_output_dir.display(),
image_output_path.display(),
),
);
assert_eq!(history.raw_items(), &[image_message, item]);
let mut expected_item = item.clone();
if let ResponseItem::ImageGenerationCall { output_hint, .. } = &mut expected_item {
*output_hint = Some(
crate::context::ImageGenerationInstructions::new(
image_output_dir.display(),
expected_saved_path.display(),
)
.body(),
);
}
assert_eq!(history.raw_items(), &[expected_item]);
assert_eq!(
std::fs::read(&expected_saved_path).expect("saved file"),
b"foo"
@@ -6637,6 +6641,7 @@ async fn handle_output_item_done_skips_image_save_message_when_save_fails() {
status: "completed".to_string(),
revised_prompt: Some("broken payload".to_string()),
result: "_-8".to_string(),
output_hint: None,
};
let mut ctx = HandleOutputCtx {
+2 -2
View File
@@ -1771,7 +1771,7 @@ async fn handle_assistant_item_done_in_plan_mode(
{
maybe_complete_plan_item_from_message(sess, turn_context, state, item).await;
if let Some(turn_item) =
if let Some((turn_item, _item_for_history)) =
handle_non_tool_response_item(sess, turn_context, item, /*plan_mode*/ true).await
{
emit_turn_item_in_plan_mode(
@@ -2015,7 +2015,7 @@ async fn try_run_sampling_request(
} else if matches!(&item, ResponseItem::FunctionCall { .. }) {
active_tool_argument_diff_consumer = None;
}
if let Some(turn_item) = handle_non_tool_response_item(
if let Some((turn_item, _item_for_history)) = handle_non_tool_response_item(
sess.as_ref(),
turn_context.as_ref(),
&item,
+30 -21
View File
@@ -258,14 +258,14 @@ pub(crate) async fn handle_output_item_done(
}
// No tool call: convert messages/reasoning into turn items and mark them as complete.
Ok(None) => {
let turn_item = handle_non_tool_response_item(
let parsed_items = handle_non_tool_response_item(
ctx.sess.as_ref(),
ctx.turn_context.as_ref(),
&item,
plan_mode,
)
.await;
if let Some(turn_item) = turn_item {
let modified_item = if let Some((turn_item, modified_item)) = parsed_items {
if previously_active_item.is_none() {
let mut started_item = turn_item.clone();
if let TurnItem::ImageGeneration(item) = &mut started_item {
@@ -282,9 +282,18 @@ pub(crate) async fn handle_output_item_done(
ctx.sess
.emit_turn_item_completed(&ctx.turn_context, turn_item)
.await;
}
record_completed_response_item(ctx.sess.as_ref(), ctx.turn_context.as_ref(), &item)
.await;
Some(modified_item)
} else {
None
};
let item_for_history = modified_item.as_ref().unwrap_or(&item);
record_completed_response_item(
ctx.sess.as_ref(),
ctx.turn_context.as_ref(),
item_for_history,
)
.await;
let last_agent_message = last_assistant_message_from_item(&item, plan_mode);
output.last_agent_message = last_agent_message;
@@ -353,7 +362,7 @@ pub(crate) async fn handle_non_tool_response_item(
turn_context: &TurnContext,
item: &ResponseItem,
plan_mode: bool,
) -> Option<TurnItem> {
) -> Option<(TurnItem, ResponseItem)> {
debug!(?item, "Output item");
match item {
@@ -361,6 +370,7 @@ pub(crate) async fn handle_non_tool_response_item(
| ResponseItem::Reasoning { .. }
| ResponseItem::WebSearchCall { .. }
| ResponseItem::ImageGenerationCall { .. } => {
let mut modified_item = item.clone();
let mut turn_item = parse_turn_item(item)?;
if let TurnItem::AgentMessage(agent_message) = &mut turn_item {
let combined = agent_message
@@ -387,22 +397,21 @@ pub(crate) async fn handle_non_tool_response_item(
.await
{
Ok(path) => {
image_item.saved_path = Some(path);
let image_output_path = image_generation_artifact_path(
&turn_context.config.codex_home,
&session_id,
"<image_id>",
);
let image_output_dir = image_output_path
let image_output_dir = path
.parent()
.unwrap_or_else(|| turn_context.config.codex_home.clone());
let message: ResponseItem =
ContextualUserFragment::into(ImageGenerationInstructions::new(
image_output_dir.display(),
image_output_path.display(),
));
sess.record_conversation_items(turn_context, &[message])
.await;
let saved_path_text = path.display().to_string();
let output_hint_text = ImageGenerationInstructions::new(
image_output_dir.display(),
saved_path_text,
)
.body();
image_item.saved_path = Some(path);
if let ResponseItem::ImageGenerationCall { output_hint, .. } =
&mut modified_item
{
*output_hint = Some(output_hint_text);
}
}
Err(err) => {
let output_path = image_generation_artifact_path(
@@ -421,7 +430,7 @@ pub(crate) async fn handle_non_tool_response_item(
}
}
}
Some(turn_item)
Some((turn_item, modified_item))
}
ResponseItem::FunctionCallOutput { .. }
| ResponseItem::CustomToolCallOutput { .. }
@@ -117,10 +117,11 @@ async fn handle_non_tool_response_item_strips_citations_from_assistant_message()
"hello<oai-mem-citation><citation_entries>\nMEMORY.md:1-2|note=[x]\n</citation_entries>\n<rollout_ids>\n019cc2ea-1dff-7902-8d40-c8f6e5d83cc4\n</rollout_ids></oai-mem-citation> world",
);
let turn_item =
let (turn_item, item_for_history) =
handle_non_tool_response_item(&session, &turn_context, &item, /*plan_mode*/ false)
.await
.expect("assistant message should parse");
assert_eq!(item_for_history, item);
let TurnItem::AgentMessage(agent_message) = turn_item else {
panic!("expected agent message");
@@ -201,6 +202,7 @@ fn completed_item_defers_mailbox_delivery_for_image_generation_calls() {
status: "completed".to_string(),
revised_prompt: None,
result: "Zm9v".to_string(),
output_hint: None,
};
assert!(completed_item_defers_mailbox_delivery_to_next_turn(
+16 -4
View File
@@ -593,12 +593,18 @@ async fn generated_image_is_replayed_for_image_capable_models() -> Result<()> {
Some("Zm9v"),
"expected the original generated image payload to be preserved"
);
assert!(
image_generation_calls[0]["output_hint"]
.as_str()
.is_some_and(|text| text.contains("Generated images are saved to")),
"second request should keep the saved-path note on the image_generation_call"
);
assert!(
second_request
.message_input_texts("developer")
.iter()
.any(|text| text.contains("Generated images are saved to")),
"second request should include the saved-path note in model-visible history"
.all(|text| !text.contains("Generated images are saved to")),
"second request should not include a separate developer saved-path note"
);
let _ = std::fs::remove_file(&saved_path);
@@ -710,6 +716,12 @@ async fn model_change_from_generated_image_to_text_preserves_prior_generated_ima
Some(""),
"second request should strip generated image bytes for text-only models"
);
assert!(
image_generation_calls[0]["output_hint"]
.as_str()
.is_some_and(|text| text.contains("Generated images are saved to")),
"second request should preserve the saved-path note on the image_generation_call"
);
assert!(
second_request
.message_input_texts("user")
@@ -721,8 +733,8 @@ async fn model_change_from_generated_image_to_text_preserves_prior_generated_ima
second_request
.message_input_texts("developer")
.iter()
.any(|text| text.contains("Generated images are saved to")),
"second request should include the saved-path note in model-visible history"
.all(|text| !text.contains("Generated images are saved to")),
"second request should not include a separate developer saved-path note"
);
let _ = std::fs::remove_file(&saved_path);
+33 -1
View File
@@ -869,7 +869,8 @@ pub enum ResponseItem {
// "type":"image_generation_call",
// "status":"completed",
// "revised_prompt":"A gray tabby cat hugging an otter...",
// "result":"..."
// "result":"...",
// "output_hint":"Generated images are saved to ..."
// }
ImageGenerationCall {
id: String,
@@ -878,6 +879,9 @@ pub enum ResponseItem {
#[ts(optional)]
revised_prompt: Option<String>,
result: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
output_hint: Option<String>,
},
#[serde(alias = "compaction_summary")]
Compaction { encrypted_content: String },
@@ -1741,6 +1745,7 @@ mod tests {
status: "completed".to_string(),
revised_prompt: Some("A small blue square".to_string()),
result: "Zm9v".to_string(),
output_hint: None,
}
);
}
@@ -1762,6 +1767,33 @@ mod tests {
status: "completed".to_string(),
revised_prompt: None,
result: "Zm9v".to_string(),
output_hint: None,
}
);
}
#[test]
fn response_item_parses_image_generation_call_with_output_hint() {
let item = serde_json::from_value::<ResponseItem>(serde_json::json!({
"id": "ig_123",
"type": "image_generation_call",
"status": "completed",
"result": "Zm9v",
"output_hint": "Generated images are saved to /tmp/generated_images/session as /tmp/generated_images/session/ig_123.png by default.",
}))
.expect("image generation item should deserialize");
assert_eq!(
item,
ResponseItem::ImageGenerationCall {
id: "ig_123".to_string(),
status: "completed".to_string(),
revised_prompt: None,
result: "Zm9v".to_string(),
output_hint: Some(
"Generated images are saved to /tmp/generated_images/session as /tmp/generated_images/session/ig_123.png by default."
.to_string(),
),
}
);
}