mirror of
https://github.com/openai/codex.git
synced 2026-04-24 14:45:27 +00:00
moved image_save logic to handler
This commit is contained in:
@@ -6217,7 +6217,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) = handle_non_tool_response_item(item, true) {
|
||||
if let Some(turn_item) = handle_non_tool_response_item(item, true, None).await {
|
||||
emit_turn_item_in_plan_mode(
|
||||
sess,
|
||||
turn_context,
|
||||
@@ -6396,7 +6396,8 @@ async fn try_run_sampling_request(
|
||||
needs_follow_up |= output_result.needs_follow_up;
|
||||
}
|
||||
ResponseEvent::OutputItemAdded(item) => {
|
||||
if let Some(turn_item) = handle_non_tool_response_item(&item, plan_mode) {
|
||||
if let Some(turn_item) = handle_non_tool_response_item(&item, plan_mode, None).await
|
||||
{
|
||||
let mut turn_item = turn_item;
|
||||
let mut seeded_parsed: Option<ParsedAssistantTextDelta> = None;
|
||||
let mut seeded_item_id: Option<String> = None;
|
||||
|
||||
@@ -189,32 +189,9 @@ pub(crate) async fn handle_output_item_done(
|
||||
}
|
||||
// No tool call: convert messages/reasoning into turn items and mark them as complete.
|
||||
Ok(None) => {
|
||||
if let Some(turn_item) = handle_non_tool_response_item(&item, plan_mode) {
|
||||
let turn_item = match turn_item {
|
||||
TurnItem::ImageGeneration(mut image_item) => {
|
||||
match save_image_generation_result_to_cwd(
|
||||
&ctx.turn_context.cwd,
|
||||
&image_item.id,
|
||||
&image_item.result,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(path) => {
|
||||
image_item.saved_path = Some(path.to_string_lossy().into_owned());
|
||||
}
|
||||
Err(err) => {
|
||||
tracing::warn!(
|
||||
call_id = %image_item.id,
|
||||
cwd = %ctx.turn_context.cwd.display(),
|
||||
"failed to save generated image: {err}"
|
||||
);
|
||||
}
|
||||
}
|
||||
TurnItem::ImageGeneration(image_item)
|
||||
}
|
||||
other => other,
|
||||
};
|
||||
|
||||
if let Some(turn_item) =
|
||||
handle_non_tool_response_item(&item, plan_mode, Some(&ctx.turn_context.cwd)).await
|
||||
{
|
||||
if previously_active_item.is_none() {
|
||||
let mut started_item = turn_item.clone();
|
||||
if let TurnItem::ImageGeneration(item) = &mut started_item {
|
||||
@@ -298,9 +275,10 @@ pub(crate) async fn handle_output_item_done(
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
pub(crate) fn handle_non_tool_response_item(
|
||||
pub(crate) async fn handle_non_tool_response_item(
|
||||
item: &ResponseItem,
|
||||
plan_mode: bool,
|
||||
image_output_cwd: Option<&Path>,
|
||||
) -> Option<TurnItem> {
|
||||
debug!(?item, "Output item");
|
||||
|
||||
@@ -322,6 +300,24 @@ pub(crate) fn handle_non_tool_response_item(
|
||||
agent_message.content =
|
||||
vec![codex_protocol::items::AgentMessageContent::Text { text: stripped }];
|
||||
}
|
||||
if let TurnItem::ImageGeneration(image_item) = &mut turn_item
|
||||
&& let Some(cwd) = image_output_cwd
|
||||
{
|
||||
match save_image_generation_result_to_cwd(cwd, &image_item.id, &image_item.result)
|
||||
.await
|
||||
{
|
||||
Ok(path) => {
|
||||
image_item.saved_path = Some(path.to_string_lossy().into_owned());
|
||||
}
|
||||
Err(err) => {
|
||||
tracing::warn!(
|
||||
call_id = %image_item.id,
|
||||
cwd = %cwd.display(),
|
||||
"failed to save generated image: {err}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(turn_item)
|
||||
}
|
||||
ResponseItem::FunctionCallOutput { .. } | ResponseItem::CustomToolCallOutput { .. } => {
|
||||
@@ -404,12 +400,13 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handle_non_tool_response_item_strips_citations_from_assistant_message() {
|
||||
#[tokio::test]
|
||||
async fn handle_non_tool_response_item_strips_citations_from_assistant_message() {
|
||||
let item = assistant_output_text("hello<oai-mem-citation>doc1</oai-mem-citation> world");
|
||||
|
||||
let turn_item =
|
||||
handle_non_tool_response_item(&item, false).expect("assistant message should parse");
|
||||
let turn_item = handle_non_tool_response_item(&item, false, None)
|
||||
.await
|
||||
.expect("assistant message should parse");
|
||||
|
||||
let TurnItem::AgentMessage(agent_message) = turn_item else {
|
||||
panic!("expected agent message");
|
||||
|
||||
Reference in New Issue
Block a user