mirror of
https://github.com/openai/codex.git
synced 2026-11-30 00:10:49 +00:00
Merge branch 'main' into fchen/imagegen-continue-unfinished-tasks
This commit is contained in:
@@ -20,6 +20,7 @@ pub use codex_tools::JsonToolOutput;
|
||||
pub use codex_tools::NoopTurnItemEmitter;
|
||||
pub use codex_tools::ResponsesApiTool;
|
||||
pub use codex_tools::ToolCall;
|
||||
pub use codex_tools::ToolEnvironment;
|
||||
pub use codex_tools::ToolExecutor;
|
||||
pub use codex_tools::ToolExecutorFuture;
|
||||
pub use codex_tools::ToolName;
|
||||
|
||||
@@ -1341,6 +1341,7 @@ fn tool_call(tool_name: &str, call_id: &str, arguments: serde_json::Value) -> To
|
||||
truncation_policy: TruncationPolicy::Bytes(1024),
|
||||
conversation_history: codex_extension_api::ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
environments: Vec::new(),
|
||||
payload: ToolPayload::Function {
|
||||
arguments: arguments.to_string(),
|
||||
},
|
||||
|
||||
@@ -23,6 +23,7 @@ codex-protocol = { workspace = true }
|
||||
codex-tools = { workspace = true }
|
||||
codex-utils-absolute-path = { workspace = true }
|
||||
codex-utils-image = { workspace = true }
|
||||
codex-utils-path-uri = { workspace = true }
|
||||
http = { workspace = true }
|
||||
schemars = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
@@ -30,3 +31,4 @@ serde_json = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = { workspace = true }
|
||||
tokio = { workspace = true, features = ["macros", "rt"] }
|
||||
|
||||
@@ -21,7 +21,7 @@ use super::GeneratedImageOutput;
|
||||
use super::ImageRequest;
|
||||
use super::ImagegenArgs;
|
||||
use super::imagegen_tool_spec;
|
||||
use super::request_for_args;
|
||||
use super::request_for_call_args;
|
||||
use crate::IMAGE_GEN_NAMESPACE;
|
||||
use crate::IMAGEGEN_TOOL_NAME;
|
||||
|
||||
@@ -64,14 +64,16 @@ fn description_allows_continuing_unfinished_requests() {
|
||||
#[test]
|
||||
fn omitted_references_generate_with_fixed_defaults() {
|
||||
assert_eq!(
|
||||
request_for_args(
|
||||
request_for_call_args(
|
||||
&ImagegenArgs {
|
||||
prompt: "paint a moonlit lake".to_string(),
|
||||
referenced_image_paths: None,
|
||||
num_last_images_to_include: None,
|
||||
},
|
||||
&[]
|
||||
&[],
|
||||
&[],
|
||||
)
|
||||
.await
|
||||
.expect("generation request should build"),
|
||||
ImageRequest::Generate(ImageGenerationRequest {
|
||||
prompt: "paint a moonlit lake".to_string(),
|
||||
@@ -84,8 +86,8 @@ fn omitted_references_generate_with_fixed_defaults() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recent_image_fallback_selects_newest_images_in_chronological_order() {
|
||||
#[tokio::test]
|
||||
async fn recent_image_fallback_selects_newest_images_in_chronological_order() {
|
||||
let history = vec![
|
||||
ResponseItem::Message {
|
||||
id: None,
|
||||
@@ -135,14 +137,16 @@ fn recent_image_fallback_selects_newest_images_in_chronological_order() {
|
||||
];
|
||||
|
||||
assert_eq!(
|
||||
request_for_args(
|
||||
request_for_call_args(
|
||||
&ImagegenArgs {
|
||||
prompt: "change the lighting".to_string(),
|
||||
referenced_image_paths: None,
|
||||
num_last_images_to_include: Some(4),
|
||||
},
|
||||
&history,
|
||||
&[],
|
||||
)
|
||||
.await
|
||||
.expect("history-backed edit request should build"),
|
||||
ImageRequest::Edit(expected_edit_request(
|
||||
"change the lighting",
|
||||
@@ -151,9 +155,9 @@ fn recent_image_fallback_selects_newest_images_in_chronological_order() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conflicting_image_selectors_return_tool_error() {
|
||||
let error = request_for_args(
|
||||
#[tokio::test]
|
||||
async fn conflicting_image_selectors_return_tool_error() {
|
||||
let error = request_for_call_args(
|
||||
&ImagegenArgs {
|
||||
prompt: "change the lighting".to_string(),
|
||||
referenced_image_paths: Some(vec![
|
||||
@@ -164,7 +168,9 @@ fn conflicting_image_selectors_return_tool_error() {
|
||||
num_last_images_to_include: Some(1),
|
||||
},
|
||||
&[],
|
||||
&[],
|
||||
)
|
||||
.await
|
||||
.expect_err("conflicting selectors should fail");
|
||||
|
||||
assert_eq!(
|
||||
@@ -173,9 +179,9 @@ fn conflicting_image_selectors_return_tool_error() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn too_many_referenced_image_paths_return_tool_error() {
|
||||
let error = request_for_args(
|
||||
#[tokio::test]
|
||||
async fn too_many_referenced_image_paths_return_tool_error() {
|
||||
let error = request_for_call_args(
|
||||
&ImagegenArgs {
|
||||
prompt: "change the lighting".to_string(),
|
||||
referenced_image_paths: Some(
|
||||
@@ -190,7 +196,9 @@ fn too_many_referenced_image_paths_return_tool_error() {
|
||||
num_last_images_to_include: None,
|
||||
},
|
||||
&[],
|
||||
&[],
|
||||
)
|
||||
.await
|
||||
.expect_err("too many paths should fail before reading files");
|
||||
|
||||
assert_eq!(
|
||||
@@ -199,9 +207,9 @@ fn too_many_referenced_image_paths_return_tool_error() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recent_image_fallback_requires_requested_count() {
|
||||
let error = request_for_args(
|
||||
#[tokio::test]
|
||||
async fn recent_image_fallback_requires_requested_count() {
|
||||
let error = request_for_call_args(
|
||||
&ImagegenArgs {
|
||||
prompt: "change the lighting".to_string(),
|
||||
referenced_image_paths: None,
|
||||
@@ -213,7 +221,9 @@ fn recent_image_fallback_requires_requested_count() {
|
||||
content: vec![input_image("only-image")],
|
||||
phase: None,
|
||||
}],
|
||||
&[],
|
||||
)
|
||||
.await
|
||||
.expect_err("history-backed edit should require the requested image count");
|
||||
|
||||
assert_eq!(
|
||||
|
||||
@@ -10,6 +10,7 @@ use codex_core::image_generation_artifact_path;
|
||||
use codex_extension_api::ExtensionTurnItem;
|
||||
use codex_extension_api::FunctionCallError;
|
||||
use codex_extension_api::ToolCall;
|
||||
use codex_extension_api::ToolEnvironment;
|
||||
use codex_extension_api::ToolExecutor;
|
||||
use codex_extension_api::ToolName;
|
||||
use codex_extension_api::ToolOutput;
|
||||
@@ -32,6 +33,7 @@ use codex_tools::default_namespace_description;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use codex_utils_image::PromptImageMode;
|
||||
use codex_utils_image::load_for_prompt_bytes;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use schemars::JsonSchema;
|
||||
use schemars::r#gen::SchemaSettings;
|
||||
use serde::Deserialize;
|
||||
@@ -103,7 +105,9 @@ impl ToolExecutor<ToolCall> for ImageGenerationTool {
|
||||
impl ImageGenerationTool {
|
||||
async fn handle_call(&self, call: ToolCall) -> Result<Box<dyn ToolOutput>, FunctionCallError> {
|
||||
let args = parse_args(&call)?;
|
||||
let request = request_for_args(&args, call.conversation_history.items())?;
|
||||
let request =
|
||||
request_for_call_args(&args, call.conversation_history.items(), &call.environments)
|
||||
.await?;
|
||||
call.turn_item_emitter
|
||||
.emit_started(ExtensionTurnItem::ImageGeneration(ImageGenerationItem {
|
||||
id: call.call_id.clone(),
|
||||
@@ -154,10 +158,10 @@ enum ImageRequest {
|
||||
Edit(ImageEditRequest),
|
||||
}
|
||||
|
||||
/// Builds a generation or edit request from the mutually exclusive image selectors.
|
||||
fn request_for_args(
|
||||
async fn request_for_call_args(
|
||||
args: &ImagegenArgs,
|
||||
history: &[ResponseItem],
|
||||
environments: &[ToolEnvironment],
|
||||
) -> Result<ImageRequest, FunctionCallError> {
|
||||
let paths = args.referenced_image_paths.as_deref().unwrap_or_default();
|
||||
if paths.len() > MAX_EDIT_IMAGES {
|
||||
@@ -176,7 +180,18 @@ fn request_for_args(
|
||||
size: Some("auto".to_string()),
|
||||
}));
|
||||
}
|
||||
(false, None) => paths.iter().map(image_url).collect::<Result<Vec<_>, _>>()?,
|
||||
(false, None) => {
|
||||
let Some(environment) = environments.first() else {
|
||||
return Err(FunctionCallError::RespondToModel(
|
||||
"referenced image paths are unavailable in this session".to_string(),
|
||||
));
|
||||
};
|
||||
let mut images = Vec::with_capacity(paths.len());
|
||||
for path in paths {
|
||||
images.push(image_url(path, environment).await?);
|
||||
}
|
||||
images
|
||||
}
|
||||
(true, Some(count)) => {
|
||||
if !(1..=MAX_EDIT_IMAGES).contains(&count) {
|
||||
return Err(FunctionCallError::RespondToModel(format!(
|
||||
@@ -214,7 +229,6 @@ fn request_for_args(
|
||||
}))
|
||||
}
|
||||
|
||||
/// Selects the newest requested images while preserving their conversation order.
|
||||
fn recent_images(history: &[ResponseItem], count: usize) -> Vec<ImageUrl> {
|
||||
let mut function_call_ids = HashSet::new();
|
||||
let mut custom_tool_call_ids = HashSet::new();
|
||||
@@ -307,13 +321,26 @@ fn output_image_urls(output: &FunctionCallOutputPayload) -> impl Iterator<Item =
|
||||
})
|
||||
}
|
||||
|
||||
fn image_url(path: &AbsolutePathBuf) -> Result<ImageUrl, FunctionCallError> {
|
||||
let bytes = std::fs::read(path).map_err(|error| {
|
||||
async fn image_url(
|
||||
path: &AbsolutePathBuf,
|
||||
environment: &ToolEnvironment,
|
||||
) -> Result<ImageUrl, FunctionCallError> {
|
||||
let path_uri = PathUri::from_abs_path(path).map_err(|error| {
|
||||
FunctionCallError::RespondToModel(format!(
|
||||
"unable to read referenced image at `{}`: {error}",
|
||||
path.display()
|
||||
))
|
||||
})?;
|
||||
let bytes = environment
|
||||
.file_system
|
||||
.read_file(&path_uri, Some(&environment.file_system_sandbox_context))
|
||||
.await
|
||||
.map_err(|error| {
|
||||
FunctionCallError::RespondToModel(format!(
|
||||
"unable to read referenced image at `{}`: {error}",
|
||||
path.display()
|
||||
))
|
||||
})?;
|
||||
let image = load_for_prompt_bytes(path.as_path(), bytes, PromptImageMode::Original).map_err(
|
||||
|error| {
|
||||
FunctionCallError::RespondToModel(format!(
|
||||
|
||||
@@ -215,6 +215,7 @@ async fn add_ad_hoc_note_tool_creates_note_file() {
|
||||
truncation_policy: TruncationPolicy::Bytes(1024),
|
||||
conversation_history: codex_extension_api::ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
environments: Vec::new(),
|
||||
payload: payload.clone(),
|
||||
})
|
||||
.await
|
||||
@@ -258,6 +259,7 @@ async fn add_ad_hoc_note_tool_rejects_paths_as_filenames() {
|
||||
truncation_policy: TruncationPolicy::Bytes(1024),
|
||||
conversation_history: codex_extension_api::ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
environments: Vec::new(),
|
||||
payload,
|
||||
})
|
||||
.await;
|
||||
@@ -302,6 +304,7 @@ async fn read_tool_reads_memory_file() {
|
||||
truncation_policy: TruncationPolicy::Bytes(1024),
|
||||
conversation_history: codex_extension_api::ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
environments: Vec::new(),
|
||||
payload: payload.clone(),
|
||||
})
|
||||
.await
|
||||
@@ -349,6 +352,7 @@ async fn search_tool_accepts_multiple_queries() {
|
||||
truncation_policy: TruncationPolicy::Bytes(1024),
|
||||
conversation_history: codex_extension_api::ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
environments: Vec::new(),
|
||||
payload: payload.clone(),
|
||||
})
|
||||
.await
|
||||
@@ -422,6 +426,7 @@ async fn search_tool_accepts_windowed_all_match_mode() {
|
||||
truncation_policy: TruncationPolicy::Bytes(1024),
|
||||
conversation_history: codex_extension_api::ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
environments: Vec::new(),
|
||||
payload: payload.clone(),
|
||||
})
|
||||
.await
|
||||
@@ -475,6 +480,7 @@ async fn search_tool_rejects_legacy_single_query() {
|
||||
truncation_policy: TruncationPolicy::Bytes(1024),
|
||||
conversation_history: codex_extension_api::ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
environments: Vec::new(),
|
||||
payload,
|
||||
})
|
||||
.await;
|
||||
|
||||
Reference in New Issue
Block a user