codex: route view_image through sandboxed fs options

This commit is contained in:
starr-openai
2026-04-06 17:01:56 -07:00
parent a19601e767
commit 689107592d
4 changed files with 35 additions and 2 deletions
+1
View File
@@ -2048,6 +2048,7 @@ dependencies = [
"base64 0.22.1",
"clap",
"codex-app-server-protocol",
"codex-protocol",
"codex-utils-absolute-path",
"codex-utils-cargo-bin",
"codex-utils-pty",
+31 -2
View File
@@ -1,3 +1,4 @@
use codex_exec_server::FileSystemOperationOptions;
use codex_protocol::models::FunctionCallOutputBody;
use codex_protocol::models::FunctionCallOutputContentItem;
use codex_protocol::models::FunctionCallOutputPayload;
@@ -8,6 +9,7 @@ use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_image::PromptImageMode;
use codex_utils_image::load_for_prompt_bytes;
use serde::Deserialize;
use std::path::Path;
use crate::function_tool::FunctionCallError;
use crate::original_image_detail::can_request_original_image_detail;
@@ -91,11 +93,13 @@ impl ToolHandler for ViewImageHandler {
AbsolutePathBuf::try_from(turn.resolve_path(Some(args.path))).map_err(|error| {
FunctionCallError::RespondToModel(format!("unable to resolve image path: {error}"))
})?;
let file_system_options =
view_image_file_system_options(turn.cwd.as_path(), turn.sandbox_policy.get().clone());
let metadata = turn
.environment
.get_filesystem()
.get_metadata(&abs_path)
.get_metadata_with_options(&abs_path, &file_system_options)
.await
.map_err(|error| {
FunctionCallError::RespondToModel(format!(
@@ -113,7 +117,7 @@ impl ToolHandler for ViewImageHandler {
let file_bytes = turn
.environment
.get_filesystem()
.read_file(&abs_path)
.read_file_with_options(&abs_path, &file_system_options)
.await
.map_err(|error| {
FunctionCallError::RespondToModel(format!(
@@ -160,6 +164,16 @@ impl ToolHandler for ViewImageHandler {
}
}
fn view_image_file_system_options(
cwd: &Path,
sandbox_policy: codex_protocol::protocol::SandboxPolicy,
) -> FileSystemOperationOptions {
FileSystemOperationOptions {
sandbox_policy: Some(sandbox_policy),
cwd: AbsolutePathBuf::from_absolute_path(cwd).ok(),
}
}
pub struct ViewImageOutput {
image_url: String,
image_detail: Option<ImageDetail>,
@@ -204,6 +218,7 @@ mod tests {
use super::*;
use pretty_assertions::assert_eq;
use serde_json::json;
use tempfile::tempdir;
#[test]
fn code_mode_result_returns_image_url_object() {
@@ -224,4 +239,18 @@ mod tests {
})
);
}
#[test]
fn view_image_file_system_options_include_sandbox_policy_and_cwd() {
let temp_dir = tempdir().expect("temp dir");
let sandbox_policy = codex_protocol::protocol::SandboxPolicy::new_workspace_write_policy();
let options = view_image_file_system_options(temp_dir.path(), sandbox_policy.clone());
assert_eq!(options.sandbox_policy, Some(sandbox_policy));
assert_eq!(
options.cwd,
AbsolutePathBuf::from_absolute_path(temp_dir.path()).ok()
);
}
}
+2
View File
@@ -20,6 +20,7 @@ async-trait = { workspace = true }
base64 = { workspace = true }
clap = { workspace = true, features = ["derive"] }
codex-app-server-protocol = { workspace = true }
codex-protocol = { workspace = true }
codex-utils-absolute-path = { workspace = true }
codex-utils-pty = { workspace = true }
futures = { workspace = true }
@@ -42,6 +43,7 @@ tracing = { workspace = true }
[dev-dependencies]
anyhow = { workspace = true }
codex-protocol = { workspace = true }
codex-utils-cargo-bin = { workspace = true }
pretty_assertions = { workspace = true }
tempfile = { workspace = true }
+1
View File
@@ -39,6 +39,7 @@ pub use file_system::CopyOptions;
pub use file_system::CreateDirectoryOptions;
pub use file_system::ExecutorFileSystem;
pub use file_system::FileMetadata;
pub use file_system::FileSystemOperationOptions;
pub use file_system::FileSystemResult;
pub use file_system::ReadDirectoryEntry;
pub use file_system::RemoveOptions;