mirror of
https://github.com/openai/codex.git
synced 2026-11-24 23:20:47 +00:00
Add sandboxed file transfer tools
Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
Generated
+19
@@ -1557,6 +1557,7 @@ version = "0.0.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"codex-apply-patch",
|
||||
"codex-file-transfer",
|
||||
"codex-linux-sandbox",
|
||||
"codex-shell-escalation",
|
||||
"codex-utils-home-dir",
|
||||
@@ -1844,6 +1845,7 @@ dependencies = [
|
||||
"codex-environment",
|
||||
"codex-execpolicy",
|
||||
"codex-file-search",
|
||||
"codex-file-transfer",
|
||||
"codex-git",
|
||||
"codex-hooks",
|
||||
"codex-keyring-store",
|
||||
@@ -2075,6 +2077,23 @@ dependencies = [
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "codex-file-transfer"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"codex-client",
|
||||
"mime_guess",
|
||||
"pretty_assertions",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tempfile",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"wiremock",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "codex-git"
|
||||
version = "0.0.0"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
[workspace]
|
||||
members = [
|
||||
"backend-client",
|
||||
"file-transfer",
|
||||
"ansi-escape",
|
||||
"async-utils",
|
||||
"app-server",
|
||||
@@ -109,6 +110,7 @@ codex-exec = { path = "exec" }
|
||||
codex-execpolicy = { path = "execpolicy" }
|
||||
codex-experimental-api-macros = { path = "codex-experimental-api-macros" }
|
||||
codex-feedback = { path = "feedback" }
|
||||
codex-file-transfer = { path = "file-transfer" }
|
||||
codex-file-search = { path = "file-search" }
|
||||
codex-git = { path = "utils/git" }
|
||||
codex-hooks = { path = "hooks" }
|
||||
|
||||
@@ -14,6 +14,7 @@ workspace = true
|
||||
[dependencies]
|
||||
anyhow = { workspace = true }
|
||||
codex-apply-patch = { workspace = true }
|
||||
codex-file-transfer = { workspace = true }
|
||||
codex-linux-sandbox = { workspace = true }
|
||||
codex-shell-escalation = { workspace = true }
|
||||
codex-utils-home-dir = { workspace = true }
|
||||
|
||||
@@ -4,6 +4,7 @@ use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use codex_apply_patch::CODEX_CORE_APPLY_PATCH_ARG1;
|
||||
use codex_file_transfer::CODEX_CORE_FILE_TRANSFER_ARG1;
|
||||
use codex_utils_home_dir::find_codex_home;
|
||||
#[cfg(unix)]
|
||||
use std::os::unix::fs::symlink;
|
||||
@@ -105,6 +106,43 @@ pub fn arg0_dispatch() -> Option<Arg0PathEntryGuard> {
|
||||
};
|
||||
std::process::exit(exit_code);
|
||||
}
|
||||
if argv1 == CODEX_CORE_FILE_TRANSFER_ARG1 {
|
||||
let request_arg = args.next().and_then(|s| s.to_str().map(str::to_owned));
|
||||
let runtime = match tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
{
|
||||
Ok(runtime) => runtime,
|
||||
Err(_) => std::process::exit(1),
|
||||
};
|
||||
let exit_code = match request_arg {
|
||||
Some(request_arg) => runtime.block_on(async move {
|
||||
match codex_file_transfer::run_from_arg(&request_arg).await {
|
||||
Ok(output) => match output.to_json() {
|
||||
Ok(json) => {
|
||||
println!("{json}");
|
||||
0
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("failed to serialize file transfer output: {err}");
|
||||
1
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
eprintln!("file transfer helper failed: {err}");
|
||||
1
|
||||
}
|
||||
}
|
||||
}),
|
||||
None => {
|
||||
eprintln!(
|
||||
"Error: {CODEX_CORE_FILE_TRANSFER_ARG1} requires a UTF-8 request argument."
|
||||
);
|
||||
1
|
||||
}
|
||||
};
|
||||
std::process::exit(exit_code);
|
||||
}
|
||||
|
||||
// This modifies the environment, which is not thread-safe, so do this
|
||||
// before creating any threads/the Tokio runtime.
|
||||
|
||||
@@ -38,6 +38,7 @@ codex-environment = { workspace = true }
|
||||
codex-shell-command = { workspace = true }
|
||||
codex-skills = { workspace = true }
|
||||
codex-execpolicy = { workspace = true }
|
||||
codex-file-transfer = { workspace = true }
|
||||
codex-file-search = { workspace = true }
|
||||
codex-git = { workspace = true }
|
||||
codex-hooks = { workspace = true }
|
||||
|
||||
+52
-13
File diff suppressed because one or more lines are too long
@@ -478,7 +478,17 @@ impl ModelsManager {
|
||||
.iter()
|
||||
.position(|existing| existing.slug == model.slug)
|
||||
{
|
||||
existing_models[existing_index] = model;
|
||||
let existing = &existing_models[existing_index];
|
||||
existing_models[existing_index] = if model.experimental_supported_tools.is_empty()
|
||||
&& !existing.experimental_supported_tools.is_empty()
|
||||
{
|
||||
ModelInfo {
|
||||
experimental_supported_tools: existing.experimental_supported_tools.clone(),
|
||||
..model
|
||||
}
|
||||
} else {
|
||||
model
|
||||
};
|
||||
} else {
|
||||
existing_models.push(model);
|
||||
}
|
||||
|
||||
@@ -351,6 +351,45 @@ async fn refresh_available_models_uses_cache_when_fresh() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn refresh_available_models_preserves_local_experimental_tools_when_remote_empty() {
|
||||
let server = MockServer::start().await;
|
||||
let remote_models = vec![remote_model("gpt-5.4", "gpt-5.4", 0)];
|
||||
let _models_mock = mount_models_once(
|
||||
&server,
|
||||
ModelsResponse {
|
||||
models: remote_models,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
let codex_home = tempdir().expect("temp dir");
|
||||
let auth_manager =
|
||||
AuthManager::from_auth_for_testing(CodexAuth::create_dummy_chatgpt_auth_for_testing());
|
||||
let provider = provider_for(server.uri());
|
||||
let manager = ModelsManager::with_provider_for_tests(
|
||||
codex_home.path().to_path_buf(),
|
||||
auth_manager,
|
||||
provider,
|
||||
);
|
||||
|
||||
manager
|
||||
.refresh_available_models(RefreshStrategy::OnlineIfUncached)
|
||||
.await
|
||||
.expect("refresh succeeds");
|
||||
|
||||
let gpt_54 = manager
|
||||
.get_remote_models()
|
||||
.await
|
||||
.into_iter()
|
||||
.find(|model| model.slug == "gpt-5.4")
|
||||
.expect("gpt-5.4 should exist");
|
||||
assert_eq!(
|
||||
gpt_54.experimental_supported_tools,
|
||||
vec!["upload_file".to_string(), "download_file".to_string()]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn refresh_available_models_refetches_when_cache_stale() {
|
||||
let server = MockServer::start().await;
|
||||
|
||||
@@ -0,0 +1,769 @@
|
||||
use crate::codex::Session;
|
||||
use crate::codex::TurnContext;
|
||||
use crate::default_client::get_codex_user_agent;
|
||||
use crate::error::CodexErr;
|
||||
use crate::error::SandboxErr;
|
||||
use crate::function_tool::FunctionCallError;
|
||||
use crate::sandboxing::SandboxPermissions;
|
||||
use crate::sandboxing::effective_file_system_sandbox_policy;
|
||||
use crate::sandboxing::merge_permission_profiles;
|
||||
use crate::sandboxing::normalize_additional_permissions;
|
||||
use crate::tools::context::FunctionToolOutput;
|
||||
use crate::tools::context::ToolInvocation;
|
||||
use crate::tools::context::ToolPayload;
|
||||
use crate::tools::handlers::parse_arguments;
|
||||
use crate::tools::handlers::resolve_workdir_base_path;
|
||||
use crate::tools::orchestrator::ToolOrchestrator;
|
||||
use crate::tools::registry::ToolHandler;
|
||||
use crate::tools::registry::ToolKind;
|
||||
use crate::tools::runtimes::file_transfer::FileTransferRuntime;
|
||||
use crate::tools::runtimes::file_transfer::InternalFileTransferRequest;
|
||||
use crate::tools::sandboxing::ToolCtx;
|
||||
use async_trait::async_trait;
|
||||
use codex_file_transfer::DownloadFileToolResult;
|
||||
use codex_file_transfer::FILE_TRANSFER_ACCOUNT_ID_ENV;
|
||||
use codex_file_transfer::FILE_TRANSFER_BASE_URL_ENV;
|
||||
use codex_file_transfer::FILE_TRANSFER_BEARER_TOKEN_ENV;
|
||||
use codex_file_transfer::FILE_TRANSFER_USER_AGENT_ENV;
|
||||
use codex_file_transfer::FileTransferRequest;
|
||||
use codex_file_transfer::UploadFileToolResult;
|
||||
use codex_protocol::models::FileSystemPermissions;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
use std::io::ErrorKind;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub struct FileTransferHandler;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct UploadFileArgs {
|
||||
path: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct DownloadFileArgs {
|
||||
file_id: String,
|
||||
path: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct EffectivePathAccess {
|
||||
base_allowed: bool,
|
||||
effective_allowed: bool,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ToolHandler for FileTransferHandler {
|
||||
type Output = FunctionToolOutput;
|
||||
|
||||
fn kind(&self) -> ToolKind {
|
||||
ToolKind::Function
|
||||
}
|
||||
|
||||
async fn is_mutating(&self, _invocation: &ToolInvocation) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
async fn handle(&self, invocation: ToolInvocation) -> Result<Self::Output, FunctionCallError> {
|
||||
let ToolInvocation {
|
||||
session,
|
||||
turn,
|
||||
call_id,
|
||||
tool_name,
|
||||
payload,
|
||||
..
|
||||
} = invocation;
|
||||
let arguments = match payload {
|
||||
ToolPayload::Function { arguments } => arguments,
|
||||
_ => {
|
||||
return Err(FunctionCallError::RespondToModel(
|
||||
"file transfer handler received unsupported payload".to_string(),
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
match tool_name.as_str() {
|
||||
"upload_file" => {
|
||||
let cwd = resolve_workdir_base_path(&arguments, turn.cwd.as_path())?;
|
||||
let args: UploadFileArgs = parse_arguments(&arguments)?;
|
||||
let path = crate::util::resolve_path(cwd.as_path(), &PathBuf::from(args.path));
|
||||
handle_upload(&session, &turn, &call_id, &tool_name, path).await
|
||||
}
|
||||
"download_file" => {
|
||||
let cwd = resolve_workdir_base_path(&arguments, turn.cwd.as_path())?;
|
||||
let args: DownloadFileArgs = parse_arguments(&arguments)?;
|
||||
let path = crate::util::resolve_path(cwd.as_path(), &PathBuf::from(args.path));
|
||||
let file_id = parse_file_id(&args.file_id).ok_or_else(|| {
|
||||
FunctionCallError::RespondToModel(
|
||||
"download_file.file_id must be a bare file id or openai-file://v1/{file_id}"
|
||||
.to_string(),
|
||||
)
|
||||
})?;
|
||||
handle_download(&session, &turn, &call_id, &tool_name, file_id, path).await
|
||||
}
|
||||
_ => Err(FunctionCallError::RespondToModel(format!(
|
||||
"unsupported file transfer tool `{tool_name}`"
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_upload(
|
||||
session: &std::sync::Arc<Session>,
|
||||
turn: &std::sync::Arc<TurnContext>,
|
||||
call_id: &str,
|
||||
tool_name: &str,
|
||||
path: PathBuf,
|
||||
) -> Result<FunctionToolOutput, FunctionCallError> {
|
||||
let access =
|
||||
effective_path_access(session.as_ref(), turn.as_ref(), &path, AccessKind::Read).await;
|
||||
if !access.effective_allowed {
|
||||
return json_output(
|
||||
&UploadFileToolResult {
|
||||
ok: false,
|
||||
file_id: None,
|
||||
uri: None,
|
||||
file_name: None,
|
||||
file_size_bytes: None,
|
||||
mime_type: None,
|
||||
error_code: Some("sandbox_path_denied".to_string()),
|
||||
message: Some(format!(
|
||||
"upload path `{}` is outside the current sandbox",
|
||||
path.display()
|
||||
)),
|
||||
retryable: Some(false),
|
||||
http_status_code: None,
|
||||
path: Some(path.display().to_string()),
|
||||
},
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
let metadata = match tokio::fs::metadata(&path).await {
|
||||
Ok(metadata) => metadata,
|
||||
Err(err) => {
|
||||
let error_code = match err.kind() {
|
||||
ErrorKind::NotFound => "path_not_found",
|
||||
ErrorKind::PermissionDenied => "sandbox_path_denied",
|
||||
_ => "upload_failed",
|
||||
};
|
||||
return json_output(
|
||||
&UploadFileToolResult {
|
||||
ok: false,
|
||||
file_id: None,
|
||||
uri: None,
|
||||
file_name: path
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.map(str::to_string),
|
||||
file_size_bytes: None,
|
||||
mime_type: None,
|
||||
error_code: Some(error_code.to_string()),
|
||||
message: Some(format!("failed to inspect `{}`: {err}", path.display())),
|
||||
retryable: Some(false),
|
||||
http_status_code: None,
|
||||
path: Some(path.display().to_string()),
|
||||
},
|
||||
false,
|
||||
);
|
||||
}
|
||||
};
|
||||
if metadata.is_dir() {
|
||||
return json_output(
|
||||
&UploadFileToolResult {
|
||||
ok: false,
|
||||
file_id: None,
|
||||
uri: None,
|
||||
file_name: None,
|
||||
file_size_bytes: None,
|
||||
mime_type: None,
|
||||
error_code: Some("path_is_directory".to_string()),
|
||||
message: Some(format!("upload path `{}` is a directory", path.display())),
|
||||
retryable: Some(false),
|
||||
http_status_code: None,
|
||||
path: Some(path.display().to_string()),
|
||||
},
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
let auth = session.services.auth_manager.auth().await;
|
||||
let Some(auth) = auth else {
|
||||
return json_output(&upload_auth_required(&path), false);
|
||||
};
|
||||
if !auth.is_chatgpt_auth() {
|
||||
return json_output(&upload_auth_required(&path), false);
|
||||
}
|
||||
let bearer_token = auth.get_token().map_err(|err| {
|
||||
FunctionCallError::RespondToModel(format!("failed to load chatgpt auth token: {err}"))
|
||||
})?;
|
||||
|
||||
let additional_permissions =
|
||||
additional_permissions_for_access(&path, AccessKind::Read, &access).map_err(|err| {
|
||||
FunctionCallError::RespondToModel(format!(
|
||||
"failed to derive file transfer permissions: {err}"
|
||||
))
|
||||
})?;
|
||||
let request = InternalFileTransferRequest {
|
||||
request: FileTransferRequest::Upload { path: path.clone() },
|
||||
cwd: turn.cwd.clone(),
|
||||
env: helper_env(
|
||||
turn.as_ref(),
|
||||
&bearer_token,
|
||||
auth.get_account_id().as_deref(),
|
||||
),
|
||||
network: turn.network.clone(),
|
||||
sandbox_permissions: SandboxPermissions::UseDefault,
|
||||
additional_permissions,
|
||||
codex_exe: turn.codex_linux_sandbox_exe.clone(),
|
||||
};
|
||||
run_transfer(
|
||||
request,
|
||||
session,
|
||||
turn,
|
||||
call_id,
|
||||
tool_name,
|
||||
TransferKind::Upload,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn handle_download(
|
||||
session: &std::sync::Arc<Session>,
|
||||
turn: &std::sync::Arc<TurnContext>,
|
||||
call_id: &str,
|
||||
tool_name: &str,
|
||||
file_id: String,
|
||||
path: PathBuf,
|
||||
) -> Result<FunctionToolOutput, FunctionCallError> {
|
||||
let path_is_directory = tokio::fs::metadata(&path)
|
||||
.await
|
||||
.map(|metadata| metadata.is_dir())
|
||||
.unwrap_or(false);
|
||||
let access_path = path.clone();
|
||||
if !path_is_directory {
|
||||
let Some(parent) = path.parent() else {
|
||||
return json_output(
|
||||
&DownloadFileToolResult {
|
||||
ok: false,
|
||||
file_id: Some(file_id.clone()),
|
||||
uri: Some(openai_file_uri(&file_id)),
|
||||
file_name: None,
|
||||
mime_type: None,
|
||||
destination_path: Some(path.display().to_string()),
|
||||
bytes_written: None,
|
||||
error_code: Some("destination_parent_missing".to_string()),
|
||||
message: Some(format!(
|
||||
"download destination `{}` has no parent directory",
|
||||
path.display()
|
||||
)),
|
||||
retryable: Some(false),
|
||||
http_status_code: None,
|
||||
},
|
||||
false,
|
||||
);
|
||||
};
|
||||
if !parent.exists() {
|
||||
return json_output(
|
||||
&DownloadFileToolResult {
|
||||
ok: false,
|
||||
file_id: Some(file_id.clone()),
|
||||
uri: Some(openai_file_uri(&file_id)),
|
||||
file_name: None,
|
||||
mime_type: None,
|
||||
destination_path: Some(path.display().to_string()),
|
||||
bytes_written: None,
|
||||
error_code: Some("destination_parent_missing".to_string()),
|
||||
message: Some(format!(
|
||||
"download destination parent `{}` does not exist",
|
||||
parent.display()
|
||||
)),
|
||||
retryable: Some(false),
|
||||
http_status_code: None,
|
||||
},
|
||||
false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let access = effective_path_access(
|
||||
session.as_ref(),
|
||||
turn.as_ref(),
|
||||
&access_path,
|
||||
AccessKind::Write,
|
||||
)
|
||||
.await;
|
||||
if !access.effective_allowed {
|
||||
return json_output(
|
||||
&DownloadFileToolResult {
|
||||
ok: false,
|
||||
file_id: Some(file_id.clone()),
|
||||
uri: Some(openai_file_uri(&file_id)),
|
||||
file_name: None,
|
||||
mime_type: None,
|
||||
destination_path: Some(path.display().to_string()),
|
||||
bytes_written: None,
|
||||
error_code: Some("sandbox_path_denied".to_string()),
|
||||
message: Some(format!(
|
||||
"download destination `{}` is outside the current sandbox",
|
||||
path.display()
|
||||
)),
|
||||
retryable: Some(false),
|
||||
http_status_code: None,
|
||||
},
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
let auth = session.services.auth_manager.auth().await;
|
||||
let Some(auth) = auth else {
|
||||
return json_output(&download_auth_required(&file_id, &path), false);
|
||||
};
|
||||
if !auth.is_chatgpt_auth() {
|
||||
return json_output(&download_auth_required(&file_id, &path), false);
|
||||
}
|
||||
let bearer_token = auth.get_token().map_err(|err| {
|
||||
FunctionCallError::RespondToModel(format!("failed to load chatgpt auth token: {err}"))
|
||||
})?;
|
||||
|
||||
let additional_permissions =
|
||||
additional_permissions_for_access(&access_path, AccessKind::Write, &access).map_err(
|
||||
|err| {
|
||||
FunctionCallError::RespondToModel(format!(
|
||||
"failed to derive file transfer permissions: {err}"
|
||||
))
|
||||
},
|
||||
)?;
|
||||
let request = InternalFileTransferRequest {
|
||||
request: FileTransferRequest::Download {
|
||||
file_id: file_id.clone(),
|
||||
path: path.clone(),
|
||||
path_is_directory,
|
||||
},
|
||||
cwd: turn.cwd.clone(),
|
||||
env: helper_env(
|
||||
turn.as_ref(),
|
||||
&bearer_token,
|
||||
auth.get_account_id().as_deref(),
|
||||
),
|
||||
network: turn.network.clone(),
|
||||
sandbox_permissions: SandboxPermissions::UseDefault,
|
||||
additional_permissions,
|
||||
codex_exe: turn.codex_linux_sandbox_exe.clone(),
|
||||
};
|
||||
run_transfer(
|
||||
request,
|
||||
session,
|
||||
turn,
|
||||
call_id,
|
||||
tool_name,
|
||||
TransferKind::Download,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn run_transfer(
|
||||
request: InternalFileTransferRequest,
|
||||
session: &std::sync::Arc<Session>,
|
||||
turn: &std::sync::Arc<TurnContext>,
|
||||
call_id: &str,
|
||||
tool_name: &str,
|
||||
kind: TransferKind,
|
||||
) -> Result<FunctionToolOutput, FunctionCallError> {
|
||||
let mut orchestrator = ToolOrchestrator::new();
|
||||
let mut runtime = FileTransferRuntime::new();
|
||||
let tool_ctx = ToolCtx {
|
||||
session: session.clone(),
|
||||
turn: turn.clone(),
|
||||
call_id: call_id.to_string(),
|
||||
tool_name: tool_name.to_string(),
|
||||
};
|
||||
let output = orchestrator
|
||||
.run(
|
||||
&mut runtime,
|
||||
&request,
|
||||
&tool_ctx,
|
||||
turn.as_ref(),
|
||||
turn.approval_policy.value(),
|
||||
)
|
||||
.await;
|
||||
|
||||
match output {
|
||||
Ok(result) => parse_helper_output(result.output, kind, &request),
|
||||
Err(crate::tools::sandboxing::ToolError::Codex(CodexErr::Sandbox(
|
||||
SandboxErr::Denied {
|
||||
network_policy_decision,
|
||||
..
|
||||
},
|
||||
))) => {
|
||||
let content = if network_policy_decision.is_some() {
|
||||
transfer_network_denied(kind, &request)
|
||||
} else {
|
||||
transfer_internal_error(
|
||||
kind,
|
||||
&request,
|
||||
"file transfer helper could not run inside the sandbox".to_string(),
|
||||
)
|
||||
};
|
||||
json_output_value(content, false)
|
||||
}
|
||||
Err(crate::tools::sandboxing::ToolError::Rejected(message)) => {
|
||||
json_output_value(transfer_internal_error(kind, &request, message), false)
|
||||
}
|
||||
Err(crate::tools::sandboxing::ToolError::Codex(err)) => json_output_value(
|
||||
transfer_internal_error(kind, &request, err.to_string()),
|
||||
false,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_helper_output(
|
||||
output: crate::exec::ExecToolCallOutput,
|
||||
kind: TransferKind,
|
||||
request: &InternalFileTransferRequest,
|
||||
) -> Result<FunctionToolOutput, FunctionCallError> {
|
||||
if output.exit_code != 0 {
|
||||
return json_output_value(
|
||||
transfer_internal_error(
|
||||
kind,
|
||||
request,
|
||||
format!(
|
||||
"file transfer helper exited with status {}: {}",
|
||||
output.exit_code, output.stderr.text
|
||||
),
|
||||
),
|
||||
false,
|
||||
);
|
||||
}
|
||||
let parsed: Value = serde_json::from_str(&output.stdout.text).map_err(|err| {
|
||||
FunctionCallError::RespondToModel(format!(
|
||||
"file transfer helper returned invalid JSON: {err}"
|
||||
))
|
||||
})?;
|
||||
let success = parsed.get("ok").and_then(Value::as_bool).unwrap_or(false);
|
||||
let content = serde_json::to_string(&parsed).map_err(|err| {
|
||||
FunctionCallError::RespondToModel(format!("failed to encode file transfer output: {err}"))
|
||||
})?;
|
||||
Ok(FunctionToolOutput::from_text(content, Some(success)))
|
||||
}
|
||||
|
||||
fn helper_env(
|
||||
turn: &TurnContext,
|
||||
bearer_token: &str,
|
||||
account_id: Option<&str>,
|
||||
) -> HashMap<String, String> {
|
||||
let mut env = HashMap::from([
|
||||
(
|
||||
FILE_TRANSFER_BASE_URL_ENV.to_string(),
|
||||
turn.config.chatgpt_base_url.clone(),
|
||||
),
|
||||
(
|
||||
FILE_TRANSFER_BEARER_TOKEN_ENV.to_string(),
|
||||
bearer_token.to_string(),
|
||||
),
|
||||
(
|
||||
FILE_TRANSFER_USER_AGENT_ENV.to_string(),
|
||||
get_codex_user_agent(),
|
||||
),
|
||||
]);
|
||||
if let Some(account_id) = account_id {
|
||||
env.insert(
|
||||
FILE_TRANSFER_ACCOUNT_ID_ENV.to_string(),
|
||||
account_id.to_string(),
|
||||
);
|
||||
}
|
||||
env
|
||||
}
|
||||
|
||||
async fn effective_path_access(
|
||||
session: &Session,
|
||||
turn: &TurnContext,
|
||||
path: &Path,
|
||||
kind: AccessKind,
|
||||
) -> EffectivePathAccess {
|
||||
let granted_permissions = merge_permission_profiles(
|
||||
session.granted_session_permissions().await.as_ref(),
|
||||
session.granted_turn_permissions().await.as_ref(),
|
||||
);
|
||||
let effective_policy = effective_file_system_sandbox_policy(
|
||||
&turn.file_system_sandbox_policy,
|
||||
granted_permissions.as_ref(),
|
||||
);
|
||||
let base_allowed = match kind {
|
||||
AccessKind::Read => turn
|
||||
.file_system_sandbox_policy
|
||||
.can_read_path_with_cwd(path, turn.cwd.as_path()),
|
||||
AccessKind::Write => turn
|
||||
.file_system_sandbox_policy
|
||||
.can_write_path_with_cwd(path, turn.cwd.as_path()),
|
||||
};
|
||||
let effective_allowed = match kind {
|
||||
AccessKind::Read => effective_policy.can_read_path_with_cwd(path, turn.cwd.as_path()),
|
||||
AccessKind::Write => effective_policy.can_write_path_with_cwd(path, turn.cwd.as_path()),
|
||||
};
|
||||
EffectivePathAccess {
|
||||
base_allowed,
|
||||
effective_allowed,
|
||||
}
|
||||
}
|
||||
|
||||
fn additional_permissions_for_access(
|
||||
path: &Path,
|
||||
kind: AccessKind,
|
||||
access: &EffectivePathAccess,
|
||||
) -> Result<Option<PermissionProfile>, String> {
|
||||
if access.base_allowed || !access.effective_allowed {
|
||||
return Ok(None);
|
||||
}
|
||||
let absolute_path = AbsolutePathBuf::from_absolute_path(path)
|
||||
.map_err(|err| format!("invalid absolute path `{}`: {err}", path.display()))?;
|
||||
let file_system = match kind {
|
||||
AccessKind::Read => FileSystemPermissions {
|
||||
read: Some(vec![absolute_path]),
|
||||
write: None,
|
||||
},
|
||||
AccessKind::Write => FileSystemPermissions {
|
||||
read: Some(vec![]),
|
||||
write: Some(vec![absolute_path]),
|
||||
},
|
||||
};
|
||||
normalize_additional_permissions(PermissionProfile {
|
||||
file_system: Some(file_system),
|
||||
..Default::default()
|
||||
})
|
||||
.map(Some)
|
||||
}
|
||||
|
||||
fn parse_file_id(value: &str) -> Option<String> {
|
||||
let trimmed = value.trim();
|
||||
trimmed
|
||||
.strip_prefix("openai-file://v1/")
|
||||
.unwrap_or(trimmed)
|
||||
.split('/')
|
||||
.next()
|
||||
.filter(|file_id| !file_id.is_empty())
|
||||
.map(str::to_string)
|
||||
}
|
||||
|
||||
fn upload_auth_required(path: &Path) -> UploadFileToolResult {
|
||||
UploadFileToolResult {
|
||||
ok: false,
|
||||
file_id: None,
|
||||
uri: None,
|
||||
file_name: None,
|
||||
file_size_bytes: None,
|
||||
mime_type: None,
|
||||
error_code: Some("chatgpt_auth_required".to_string()),
|
||||
message: Some("chatgpt authentication is required to upload files".to_string()),
|
||||
retryable: Some(false),
|
||||
http_status_code: None,
|
||||
path: Some(path.display().to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn download_auth_required(file_id: &str, path: &Path) -> DownloadFileToolResult {
|
||||
DownloadFileToolResult {
|
||||
ok: false,
|
||||
file_id: Some(file_id.to_string()),
|
||||
uri: Some(openai_file_uri(file_id)),
|
||||
file_name: None,
|
||||
mime_type: None,
|
||||
destination_path: Some(path.display().to_string()),
|
||||
bytes_written: None,
|
||||
error_code: Some("chatgpt_auth_required".to_string()),
|
||||
message: Some("chatgpt authentication is required to download files".to_string()),
|
||||
retryable: Some(false),
|
||||
http_status_code: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn transfer_network_denied(kind: TransferKind, request: &InternalFileTransferRequest) -> Value {
|
||||
match kind {
|
||||
TransferKind::Upload => serialize_output_value(UploadFileToolResult {
|
||||
ok: false,
|
||||
file_id: None,
|
||||
uri: None,
|
||||
file_name: None,
|
||||
file_size_bytes: None,
|
||||
mime_type: None,
|
||||
error_code: Some("network_denied".to_string()),
|
||||
message: Some(
|
||||
"network access for file transfer was denied by sandbox policy".to_string(),
|
||||
),
|
||||
retryable: Some(false),
|
||||
http_status_code: None,
|
||||
path: request_path(request),
|
||||
}),
|
||||
TransferKind::Download => serialize_output_value(DownloadFileToolResult {
|
||||
ok: false,
|
||||
file_id: request_file_id(request),
|
||||
uri: request_file_id(request)
|
||||
.as_ref()
|
||||
.map(|file_id| openai_file_uri(file_id)),
|
||||
file_name: None,
|
||||
mime_type: None,
|
||||
destination_path: request_path(request),
|
||||
bytes_written: None,
|
||||
error_code: Some("network_denied".to_string()),
|
||||
message: Some(
|
||||
"network access for file transfer was denied by sandbox policy".to_string(),
|
||||
),
|
||||
retryable: Some(false),
|
||||
http_status_code: None,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn transfer_internal_error(
|
||||
kind: TransferKind,
|
||||
request: &InternalFileTransferRequest,
|
||||
message: String,
|
||||
) -> Value {
|
||||
match kind {
|
||||
TransferKind::Upload => serialize_output_value(UploadFileToolResult {
|
||||
ok: false,
|
||||
file_id: request_file_id(request),
|
||||
uri: request_file_id(request)
|
||||
.as_ref()
|
||||
.map(|file_id| openai_file_uri(file_id)),
|
||||
file_name: None,
|
||||
file_size_bytes: None,
|
||||
mime_type: None,
|
||||
error_code: Some("internal_helper_failed".to_string()),
|
||||
message: Some(message),
|
||||
retryable: Some(false),
|
||||
http_status_code: None,
|
||||
path: request_path(request),
|
||||
}),
|
||||
TransferKind::Download => serialize_output_value(DownloadFileToolResult {
|
||||
ok: false,
|
||||
file_id: request_file_id(request),
|
||||
uri: request_file_id(request)
|
||||
.as_ref()
|
||||
.map(|file_id| openai_file_uri(file_id)),
|
||||
file_name: None,
|
||||
mime_type: None,
|
||||
destination_path: request_path(request),
|
||||
bytes_written: None,
|
||||
error_code: Some("internal_helper_failed".to_string()),
|
||||
message: Some(message),
|
||||
retryable: Some(false),
|
||||
http_status_code: None,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn request_path(request: &InternalFileTransferRequest) -> Option<String> {
|
||||
match &request.request {
|
||||
FileTransferRequest::Upload { path } => Some(path.display().to_string()),
|
||||
FileTransferRequest::Download { path, .. } => Some(path.display().to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn request_file_id(request: &InternalFileTransferRequest) -> Option<String> {
|
||||
match &request.request {
|
||||
FileTransferRequest::Upload { .. } => None,
|
||||
FileTransferRequest::Download { file_id, .. } => Some(file_id.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
fn json_output<T: Serialize>(
|
||||
value: &T,
|
||||
success: bool,
|
||||
) -> Result<FunctionToolOutput, FunctionCallError> {
|
||||
let content = serde_json::to_string(value).map_err(|err| {
|
||||
FunctionCallError::RespondToModel(format!("failed to encode file transfer output: {err}"))
|
||||
})?;
|
||||
Ok(FunctionToolOutput::from_text(content, Some(success)))
|
||||
}
|
||||
|
||||
fn json_output_value(value: Value, success: bool) -> Result<FunctionToolOutput, FunctionCallError> {
|
||||
let content = serde_json::to_string(&value).map_err(|err| {
|
||||
FunctionCallError::RespondToModel(format!("failed to encode file transfer output: {err}"))
|
||||
})?;
|
||||
Ok(FunctionToolOutput::from_text(content, Some(success)))
|
||||
}
|
||||
|
||||
fn serialize_output_value<T: Serialize>(value: T) -> Value {
|
||||
serde_json::to_value(value).unwrap_or_else(|err| {
|
||||
Value::String(format!("failed to serialize file transfer output: {err}"))
|
||||
})
|
||||
}
|
||||
|
||||
fn openai_file_uri(file_id: &str) -> String {
|
||||
format!("openai-file://v1/{file_id}")
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum AccessKind {
|
||||
Read,
|
||||
Write,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum TransferKind {
|
||||
Upload,
|
||||
Download,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::exec::ExecToolCallOutput;
|
||||
use crate::exec::StreamOutput;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
fn download_request() -> InternalFileTransferRequest {
|
||||
InternalFileTransferRequest {
|
||||
request: FileTransferRequest::Download {
|
||||
file_id: "file-123".to_string(),
|
||||
path: PathBuf::from("/tmp/output.txt"),
|
||||
path_is_directory: false,
|
||||
},
|
||||
cwd: PathBuf::from("/tmp"),
|
||||
env: HashMap::new(),
|
||||
network: None,
|
||||
sandbox_permissions: SandboxPermissions::UseDefault,
|
||||
additional_permissions: None,
|
||||
codex_exe: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_helper_output_preserves_request_context_on_nonzero_exit() {
|
||||
let output = ExecToolCallOutput {
|
||||
exit_code: 23,
|
||||
stdout: StreamOutput::new(String::new()),
|
||||
stderr: StreamOutput::new("boom".to_string()),
|
||||
aggregated_output: StreamOutput::new("boom".to_string()),
|
||||
duration: std::time::Duration::ZERO,
|
||||
timed_out: false,
|
||||
};
|
||||
|
||||
let result =
|
||||
parse_helper_output(output, TransferKind::Download, &download_request()).unwrap();
|
||||
let payload: DownloadFileToolResult =
|
||||
serde_json::from_str(&result.into_text()).expect("valid json");
|
||||
assert_eq!(payload.ok, false);
|
||||
assert_eq!(payload.file_id, Some("file-123".to_string()));
|
||||
assert_eq!(
|
||||
payload.destination_path,
|
||||
Some("/tmp/output.txt".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
payload.error_code,
|
||||
Some("internal_helper_failed".to_string())
|
||||
);
|
||||
assert!(
|
||||
payload
|
||||
.message
|
||||
.as_deref()
|
||||
.is_some_and(|message| message.contains("status 23"))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ pub(crate) mod agent_jobs;
|
||||
pub mod apply_patch;
|
||||
mod artifacts;
|
||||
mod dynamic;
|
||||
mod file_transfer;
|
||||
mod grep_files;
|
||||
mod js_repl;
|
||||
mod list_dir;
|
||||
@@ -38,6 +39,7 @@ pub use artifacts::ArtifactsHandler;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
pub use dynamic::DynamicToolHandler;
|
||||
pub use file_transfer::FileTransferHandler;
|
||||
pub use grep_files::GrepFilesHandler;
|
||||
pub use js_repl::JsReplHandler;
|
||||
pub use js_repl::JsReplResetHandler;
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
use crate::exec::ExecExpiration;
|
||||
use crate::exec::ExecToolCallOutput;
|
||||
use crate::sandboxing::CommandSpec;
|
||||
use crate::sandboxing::SandboxPermissions;
|
||||
use crate::sandboxing::execute_env;
|
||||
use crate::tools::network_approval::NetworkApprovalMode;
|
||||
use crate::tools::network_approval::NetworkApprovalSpec;
|
||||
use crate::tools::sandboxing::Approvable;
|
||||
use crate::tools::sandboxing::ApprovalCtx;
|
||||
use crate::tools::sandboxing::ExecApprovalRequirement;
|
||||
use crate::tools::sandboxing::SandboxAttempt;
|
||||
use crate::tools::sandboxing::Sandboxable;
|
||||
use crate::tools::sandboxing::SandboxablePreference;
|
||||
use crate::tools::sandboxing::ToolCtx;
|
||||
use crate::tools::sandboxing::ToolError;
|
||||
use crate::tools::sandboxing::ToolRuntime;
|
||||
use codex_file_transfer::CODEX_CORE_FILE_TRANSFER_ARG1;
|
||||
use codex_file_transfer::FileTransferRequest;
|
||||
use codex_network_proxy::NetworkProxy;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
use codex_protocol::protocol::ReviewDecision;
|
||||
use futures::future::BoxFuture;
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
const DEFAULT_FILE_TRANSFER_TIMEOUT_MS: u64 = 120_000;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct InternalFileTransferRequest {
|
||||
pub request: FileTransferRequest,
|
||||
pub cwd: PathBuf,
|
||||
pub env: HashMap<String, String>,
|
||||
pub network: Option<NetworkProxy>,
|
||||
pub sandbox_permissions: SandboxPermissions,
|
||||
pub additional_permissions: Option<PermissionProfile>,
|
||||
pub codex_exe: Option<PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FileTransferRuntime;
|
||||
|
||||
impl FileTransferRuntime {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
|
||||
fn build_command_spec(req: &InternalFileTransferRequest) -> Result<CommandSpec, ToolError> {
|
||||
let exe = if let Some(path) = &req.codex_exe {
|
||||
path.clone()
|
||||
} else {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
codex_windows_sandbox::resolve_current_exe_for_launch(&req.cwd, "codex.exe")
|
||||
}
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
{
|
||||
std::env::current_exe().map_err(|err| {
|
||||
ToolError::Rejected(format!("failed to determine codex exe: {err}"))
|
||||
})?
|
||||
}
|
||||
};
|
||||
let request_json = serde_json::to_string(&req.request).map_err(|err| {
|
||||
ToolError::Rejected(format!("failed to encode file transfer request: {err}"))
|
||||
})?;
|
||||
Ok(CommandSpec {
|
||||
program: exe.to_string_lossy().to_string(),
|
||||
args: vec![CODEX_CORE_FILE_TRANSFER_ARG1.to_string(), request_json],
|
||||
cwd: req.cwd.clone(),
|
||||
expiration: ExecExpiration::Timeout(std::time::Duration::from_millis(
|
||||
DEFAULT_FILE_TRANSFER_TIMEOUT_MS,
|
||||
)),
|
||||
env: req.env.clone(),
|
||||
sandbox_permissions: req.sandbox_permissions,
|
||||
additional_permissions: req.additional_permissions.clone(),
|
||||
justification: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn stdout_stream(ctx: &ToolCtx) -> Option<crate::exec::StdoutStream> {
|
||||
Some(crate::exec::StdoutStream {
|
||||
sub_id: ctx.turn.sub_id.clone(),
|
||||
call_id: ctx.call_id.clone(),
|
||||
tx_event: ctx.session.get_tx_event(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Sandboxable for FileTransferRuntime {
|
||||
fn sandbox_preference(&self) -> SandboxablePreference {
|
||||
SandboxablePreference::Auto
|
||||
}
|
||||
|
||||
fn escalate_on_failure(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl Approvable<InternalFileTransferRequest> for FileTransferRuntime {
|
||||
type ApprovalKey = ();
|
||||
|
||||
fn approval_keys(&self, _req: &InternalFileTransferRequest) -> Vec<Self::ApprovalKey> {
|
||||
vec![]
|
||||
}
|
||||
|
||||
fn start_approval_async<'a>(
|
||||
&'a mut self,
|
||||
_req: &'a InternalFileTransferRequest,
|
||||
_ctx: ApprovalCtx<'a>,
|
||||
) -> BoxFuture<'a, ReviewDecision> {
|
||||
Box::pin(async { ReviewDecision::Approved })
|
||||
}
|
||||
|
||||
fn wants_no_sandbox_approval(&self, policy: AskForApproval) -> bool {
|
||||
match policy {
|
||||
AskForApproval::Never => false,
|
||||
AskForApproval::Granular(granular_config) => granular_config.allows_sandbox_approval(),
|
||||
AskForApproval::OnFailure => true,
|
||||
AskForApproval::OnRequest => true,
|
||||
AskForApproval::UnlessTrusted => true,
|
||||
}
|
||||
}
|
||||
|
||||
fn exec_approval_requirement(
|
||||
&self,
|
||||
_req: &InternalFileTransferRequest,
|
||||
) -> Option<ExecApprovalRequirement> {
|
||||
Some(ExecApprovalRequirement::Skip {
|
||||
bypass_sandbox: false,
|
||||
proposed_execpolicy_amendment: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolRuntime<InternalFileTransferRequest, ExecToolCallOutput> for FileTransferRuntime {
|
||||
fn network_approval_spec(
|
||||
&self,
|
||||
req: &InternalFileTransferRequest,
|
||||
_ctx: &ToolCtx,
|
||||
) -> Option<NetworkApprovalSpec> {
|
||||
req.network.as_ref()?;
|
||||
Some(NetworkApprovalSpec {
|
||||
network: req.network.clone(),
|
||||
mode: NetworkApprovalMode::Deferred,
|
||||
})
|
||||
}
|
||||
|
||||
async fn run(
|
||||
&mut self,
|
||||
req: &InternalFileTransferRequest,
|
||||
attempt: &SandboxAttempt<'_>,
|
||||
ctx: &ToolCtx,
|
||||
) -> Result<ExecToolCallOutput, ToolError> {
|
||||
let spec = Self::build_command_spec(req)?;
|
||||
let env = attempt
|
||||
.env_for(spec, req.network.as_ref())
|
||||
.map_err(|err| ToolError::Codex(err.into()))?;
|
||||
execute_env(env, Self::stdout_stream(ctx))
|
||||
.await
|
||||
.map_err(ToolError::Codex)
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
|
||||
pub mod apply_patch;
|
||||
pub mod file_transfer;
|
||||
pub mod shell;
|
||||
pub mod unified_exec;
|
||||
|
||||
|
||||
@@ -98,6 +98,87 @@ fn unified_exec_output_schema() -> JsonValue {
|
||||
})
|
||||
}
|
||||
|
||||
fn file_transfer_error_properties() -> serde_json::Map<String, JsonValue> {
|
||||
serde_json::Map::from_iter([
|
||||
("error_code".to_string(), json!({"type": "string"})),
|
||||
("message".to_string(), json!({"type": "string"})),
|
||||
("retryable".to_string(), json!({"type": "boolean"})),
|
||||
("http_status_code".to_string(), json!({"type": "number"})),
|
||||
])
|
||||
}
|
||||
|
||||
fn upload_file_output_schema() -> JsonValue {
|
||||
let mut success_properties = serde_json::Map::from_iter([
|
||||
("ok".to_string(), json!({"type": "boolean", "enum": [true]})),
|
||||
("file_id".to_string(), json!({"type": "string"})),
|
||||
("uri".to_string(), json!({"type": "string"})),
|
||||
("file_name".to_string(), json!({"type": "string"})),
|
||||
("file_size_bytes".to_string(), json!({"type": "number"})),
|
||||
("mime_type".to_string(), json!({"type": "string"})),
|
||||
("path".to_string(), json!({"type": "string"})),
|
||||
]);
|
||||
let mut error_properties = file_transfer_error_properties();
|
||||
error_properties.insert(
|
||||
"ok".to_string(),
|
||||
json!({"type": "boolean", "enum": [false]}),
|
||||
);
|
||||
error_properties.insert("path".to_string(), json!({"type": "string"}));
|
||||
success_properties.extend(error_properties.clone());
|
||||
json!({
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": success_properties,
|
||||
"required": ["ok", "file_id", "uri", "file_name", "file_size_bytes", "mime_type", "path"],
|
||||
"additionalProperties": false
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": error_properties,
|
||||
"required": ["ok", "error_code", "message", "retryable"],
|
||||
"additionalProperties": false
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
fn download_file_output_schema() -> JsonValue {
|
||||
let mut success_properties = serde_json::Map::from_iter([
|
||||
("ok".to_string(), json!({"type": "boolean", "enum": [true]})),
|
||||
("file_id".to_string(), json!({"type": "string"})),
|
||||
("uri".to_string(), json!({"type": "string"})),
|
||||
("file_name".to_string(), json!({"type": "string"})),
|
||||
("mime_type".to_string(), json!({"type": "string"})),
|
||||
("destination_path".to_string(), json!({"type": "string"})),
|
||||
("bytes_written".to_string(), json!({"type": "number"})),
|
||||
]);
|
||||
let mut error_properties = file_transfer_error_properties();
|
||||
error_properties.insert(
|
||||
"ok".to_string(),
|
||||
json!({"type": "boolean", "enum": [false]}),
|
||||
);
|
||||
error_properties.insert("file_id".to_string(), json!({"type": "string"}));
|
||||
error_properties.insert("uri".to_string(), json!({"type": "string"}));
|
||||
error_properties.insert("destination_path".to_string(), json!({"type": "string"}));
|
||||
success_properties.extend(error_properties.clone());
|
||||
json!({
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": success_properties,
|
||||
"required": ["ok", "file_id", "uri", "file_name", "mime_type", "destination_path", "bytes_written"],
|
||||
"additionalProperties": false
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": error_properties,
|
||||
"required": ["ok", "file_id", "uri", "destination_path", "error_code", "message", "retryable"],
|
||||
"additionalProperties": false
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
fn agent_status_output_schema() -> JsonValue {
|
||||
json!({
|
||||
"oneOf": [
|
||||
@@ -1972,6 +2053,67 @@ fn create_read_file_tool() -> ToolSpec {
|
||||
})
|
||||
}
|
||||
|
||||
fn create_upload_file_tool() -> ToolSpec {
|
||||
let properties = BTreeMap::from([(
|
||||
"path".to_string(),
|
||||
JsonSchema::String {
|
||||
description: Some(
|
||||
"Path to a local file to upload. May be relative to the current working directory or absolute. Directories are not allowed."
|
||||
.to_string(),
|
||||
),
|
||||
},
|
||||
)]);
|
||||
|
||||
ToolSpec::Function(ResponsesApiTool {
|
||||
name: "upload_file".to_string(),
|
||||
description: "Uploads a local sandboxed file to OpenAI file storage and returns an `openai-file://v1/{file_id}` URI.".to_string(),
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: JsonSchema::Object {
|
||||
properties,
|
||||
required: Some(vec!["path".to_string()]),
|
||||
additional_properties: Some(false.into()),
|
||||
},
|
||||
output_schema: Some(upload_file_output_schema()),
|
||||
})
|
||||
}
|
||||
|
||||
fn create_download_file_tool() -> ToolSpec {
|
||||
let properties = BTreeMap::from([
|
||||
(
|
||||
"file_id".to_string(),
|
||||
JsonSchema::String {
|
||||
description: Some(
|
||||
"File id to download. Accepts either a bare id or `openai-file://v1/{file_id}`."
|
||||
.to_string(),
|
||||
),
|
||||
},
|
||||
),
|
||||
(
|
||||
"path".to_string(),
|
||||
JsonSchema::String {
|
||||
description: Some(
|
||||
"Destination path. If this path is an existing directory, the downloaded file is written into it using the remote file name; otherwise the file is written exactly to this path."
|
||||
.to_string(),
|
||||
),
|
||||
},
|
||||
),
|
||||
]);
|
||||
|
||||
ToolSpec::Function(ResponsesApiTool {
|
||||
name: "download_file".to_string(),
|
||||
description: "Downloads an OpenAI file into the local sandbox using strict `cp` destination semantics.".to_string(),
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: JsonSchema::Object {
|
||||
properties,
|
||||
required: Some(vec!["file_id".to_string(), "path".to_string()]),
|
||||
additional_properties: Some(false.into()),
|
||||
},
|
||||
output_schema: Some(download_file_output_schema()),
|
||||
})
|
||||
}
|
||||
|
||||
fn create_list_dir_tool() -> ToolSpec {
|
||||
let properties = BTreeMap::from([
|
||||
(
|
||||
@@ -2516,6 +2658,7 @@ pub(crate) fn build_specs_with_discoverable_tools(
|
||||
use crate::tools::handlers::CodeModeExecuteHandler;
|
||||
use crate::tools::handlers::CodeModeWaitHandler;
|
||||
use crate::tools::handlers::DynamicToolHandler;
|
||||
use crate::tools::handlers::FileTransferHandler;
|
||||
use crate::tools::handlers::GrepFilesHandler;
|
||||
use crate::tools::handlers::JsReplHandler;
|
||||
use crate::tools::handlers::JsReplResetHandler;
|
||||
@@ -2555,6 +2698,7 @@ pub(crate) fn build_specs_with_discoverable_tools(
|
||||
let request_user_input_handler = Arc::new(RequestUserInputHandler {
|
||||
default_mode_request_user_input: config.default_mode_request_user_input,
|
||||
});
|
||||
let file_transfer_handler = Arc::new(FileTransferHandler);
|
||||
let tool_suggest_handler = Arc::new(ToolSuggestHandler);
|
||||
let code_mode_handler = Arc::new(CodeModeExecuteHandler);
|
||||
let code_mode_wait_handler = Arc::new(CodeModeWaitHandler);
|
||||
@@ -2817,6 +2961,32 @@ pub(crate) fn build_specs_with_discoverable_tools(
|
||||
builder.register_handler("read_file", read_file_handler);
|
||||
}
|
||||
|
||||
if config
|
||||
.experimental_supported_tools
|
||||
.contains(&"upload_file".to_string())
|
||||
{
|
||||
push_tool_spec(
|
||||
&mut builder,
|
||||
create_upload_file_tool(),
|
||||
/*supports_parallel_tool_calls*/ true,
|
||||
config.code_mode_enabled,
|
||||
);
|
||||
builder.register_handler("upload_file", file_transfer_handler.clone());
|
||||
}
|
||||
|
||||
if config
|
||||
.experimental_supported_tools
|
||||
.contains(&"download_file".to_string())
|
||||
{
|
||||
push_tool_spec(
|
||||
&mut builder,
|
||||
create_download_file_tool(),
|
||||
/*supports_parallel_tool_calls*/ true,
|
||||
config.code_mode_enabled,
|
||||
);
|
||||
builder.register_handler("download_file", file_transfer_handler);
|
||||
}
|
||||
|
||||
if config
|
||||
.experimental_supported_tools
|
||||
.iter()
|
||||
|
||||
@@ -1504,6 +1504,8 @@ fn test_parallel_support_flags() {
|
||||
assert!(find_tool(&tools, "grep_files").supports_parallel_tool_calls);
|
||||
assert!(find_tool(&tools, "list_dir").supports_parallel_tool_calls);
|
||||
assert!(find_tool(&tools, "read_file").supports_parallel_tool_calls);
|
||||
assert!(find_tool(&tools, "upload_file").supports_parallel_tool_calls);
|
||||
assert!(find_tool(&tools, "download_file").supports_parallel_tool_calls);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1515,6 +1517,8 @@ fn test_test_model_info_includes_sync_tool() {
|
||||
"read_file".to_string(),
|
||||
"grep_files".to_string(),
|
||||
"list_dir".to_string(),
|
||||
"upload_file".to_string(),
|
||||
"download_file".to_string(),
|
||||
];
|
||||
let features = Features::with_defaults();
|
||||
let available_models = Vec::new();
|
||||
@@ -1539,6 +1543,16 @@ fn test_test_model_info_includes_sync_tool() {
|
||||
.iter()
|
||||
.any(|tool| tool_name(&tool.spec) == "read_file")
|
||||
);
|
||||
assert!(
|
||||
tools
|
||||
.iter()
|
||||
.any(|tool| tool_name(&tool.spec) == "upload_file")
|
||||
);
|
||||
assert!(
|
||||
tools
|
||||
.iter()
|
||||
.any(|tool| tool_name(&tool.spec) == "download_file")
|
||||
);
|
||||
assert!(
|
||||
tools
|
||||
.iter()
|
||||
@@ -1547,6 +1561,35 @@ fn test_test_model_info_includes_sync_tool() {
|
||||
assert!(tools.iter().any(|tool| tool_name(&tool.spec) == "list_dir"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_models_json_default_model_includes_file_transfer_tools() {
|
||||
let _config = test_config();
|
||||
let model_info = model_info_from_models_json("gpt-5.3-codex");
|
||||
let features = Features::with_defaults();
|
||||
let available_models = Vec::new();
|
||||
let tools_config = ToolsConfig::new(&ToolsConfigParams {
|
||||
model_info: &model_info,
|
||||
available_models: &available_models,
|
||||
features: &features,
|
||||
web_search_mode: Some(WebSearchMode::Cached),
|
||||
session_source: SessionSource::Cli,
|
||||
sandbox_policy: &SandboxPolicy::DangerFullAccess,
|
||||
windows_sandbox_level: WindowsSandboxLevel::Disabled,
|
||||
});
|
||||
let (tools, _) = build_specs(&tools_config, None, None, &[]).build();
|
||||
|
||||
assert!(
|
||||
tools
|
||||
.iter()
|
||||
.any(|tool| tool_name(&tool.spec) == "upload_file")
|
||||
);
|
||||
assert!(
|
||||
tools
|
||||
.iter()
|
||||
.any(|tool| tool_name(&tool.spec) == "download_file")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_specs_mcp_tools_converted() {
|
||||
let config = test_config();
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
load("//:defs.bzl", "codex_rust_crate")
|
||||
|
||||
codex_rust_crate(
|
||||
name = "file-transfer",
|
||||
crate_name = "codex_file_transfer",
|
||||
)
|
||||
@@ -0,0 +1,29 @@
|
||||
[package]
|
||||
name = "codex-file-transfer"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
publish = false
|
||||
|
||||
[lib]
|
||||
name = "codex_file_transfer"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow = { workspace = true }
|
||||
codex-client = { workspace = true }
|
||||
mime_guess = { workspace = true }
|
||||
reqwest = { workspace = true, features = ["json", "stream"] }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_json = { workspace = true }
|
||||
tokio = { workspace = true, features = ["fs", "io-util"] }
|
||||
tokio-util = { workspace = true, features = ["io"] }
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = { workspace = true }
|
||||
tempfile = { workspace = true }
|
||||
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
|
||||
wiremock = { workspace = true }
|
||||
@@ -0,0 +1,900 @@
|
||||
use anyhow::Context;
|
||||
use anyhow::Result;
|
||||
use codex_client::build_reqwest_client_with_custom_ca;
|
||||
use mime_guess::MimeGuess;
|
||||
use reqwest::header::AUTHORIZATION;
|
||||
use reqwest::header::CONTENT_DISPOSITION;
|
||||
use reqwest::header::CONTENT_TYPE;
|
||||
use reqwest::header::HeaderMap;
|
||||
use reqwest::header::HeaderName;
|
||||
use reqwest::header::HeaderValue;
|
||||
use reqwest::header::USER_AGENT;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use std::io::ErrorKind;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio_util::io::ReaderStream;
|
||||
|
||||
pub const CODEX_CORE_FILE_TRANSFER_ARG1: &str = "--codex-run-as-file-transfer";
|
||||
pub const FILE_TRANSFER_BASE_URL_ENV: &str = "CODEX_FILE_TRANSFER_BASE_URL";
|
||||
pub const FILE_TRANSFER_BEARER_TOKEN_ENV: &str = "CODEX_FILE_TRANSFER_BEARER_TOKEN";
|
||||
pub const FILE_TRANSFER_ACCOUNT_ID_ENV: &str = "CODEX_FILE_TRANSFER_ACCOUNT_ID";
|
||||
pub const FILE_TRANSFER_USER_AGENT_ENV: &str = "CODEX_FILE_TRANSFER_USER_AGENT";
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
#[serde(tag = "operation", rename_all = "snake_case")]
|
||||
pub enum FileTransferRequest {
|
||||
Upload {
|
||||
path: PathBuf,
|
||||
},
|
||||
Download {
|
||||
file_id: String,
|
||||
path: PathBuf,
|
||||
path_is_directory: bool,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct UploadFileToolResult {
|
||||
pub ok: bool,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub file_id: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub uri: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub file_name: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub file_size_bytes: Option<u64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub mime_type: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub error_code: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub message: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub retryable: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub http_status_code: Option<u16>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub path: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct DownloadFileToolResult {
|
||||
pub ok: bool,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub file_id: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub uri: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub file_name: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub mime_type: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub destination_path: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub bytes_written: Option<u64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub error_code: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub message: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub retryable: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub http_status_code: Option<u16>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FileTransferConfig {
|
||||
pub base_url: String,
|
||||
pub bearer_token: String,
|
||||
pub chatgpt_account_id: Option<String>,
|
||||
pub user_agent: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum FileTransferOutput {
|
||||
Upload(UploadFileToolResult),
|
||||
Download(DownloadFileToolResult),
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct CreateFileRequestBody {
|
||||
file_name: String,
|
||||
file_size: u64,
|
||||
use_case: &'static str,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct CreateFileResponseBody {
|
||||
file_id: String,
|
||||
upload_url: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct FinalizeUploadResponseBody {
|
||||
status: String,
|
||||
download_url: String,
|
||||
file_name: String,
|
||||
file_size_bytes: u64,
|
||||
mime_type: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct DownloadLinkResponseBody {
|
||||
status: String,
|
||||
download_url: Option<String>,
|
||||
#[serde(default)]
|
||||
file_name: Option<String>,
|
||||
#[serde(default)]
|
||||
mime_type: Option<String>,
|
||||
}
|
||||
|
||||
impl FileTransferOutput {
|
||||
pub fn to_json(&self) -> Result<String> {
|
||||
match self {
|
||||
Self::Upload(output) => serde_json::to_string(output).map_err(anyhow::Error::from),
|
||||
Self::Download(output) => serde_json::to_string(output).map_err(anyhow::Error::from),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FileTransferConfig {
|
||||
pub fn from_env() -> Result<Self> {
|
||||
let base_url = std::env::var(FILE_TRANSFER_BASE_URL_ENV)
|
||||
.with_context(|| format!("missing {FILE_TRANSFER_BASE_URL_ENV}"))?;
|
||||
let bearer_token = std::env::var(FILE_TRANSFER_BEARER_TOKEN_ENV)
|
||||
.with_context(|| format!("missing {FILE_TRANSFER_BEARER_TOKEN_ENV}"))?;
|
||||
let user_agent = std::env::var(FILE_TRANSFER_USER_AGENT_ENV)
|
||||
.with_context(|| format!("missing {FILE_TRANSFER_USER_AGENT_ENV}"))?;
|
||||
let chatgpt_account_id = std::env::var(FILE_TRANSFER_ACCOUNT_ID_ENV)
|
||||
.ok()
|
||||
.filter(|value| !value.is_empty());
|
||||
Ok(Self {
|
||||
base_url,
|
||||
bearer_token,
|
||||
chatgpt_account_id,
|
||||
user_agent,
|
||||
})
|
||||
}
|
||||
|
||||
fn headers(&self) -> Result<HeaderMap> {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
AUTHORIZATION,
|
||||
HeaderValue::from_str(&format!("Bearer {}", self.bearer_token))
|
||||
.context("invalid bearer token header")?,
|
||||
);
|
||||
headers.insert(
|
||||
USER_AGENT,
|
||||
HeaderValue::from_str(&self.user_agent).context("invalid user agent header")?,
|
||||
);
|
||||
if let Some(account_id) = &self.chatgpt_account_id {
|
||||
headers.insert(
|
||||
HeaderName::from_static("chatgpt-account-id"),
|
||||
HeaderValue::from_str(account_id).context("invalid account id header")?,
|
||||
);
|
||||
}
|
||||
Ok(headers)
|
||||
}
|
||||
|
||||
fn files_api_url(&self) -> String {
|
||||
let base_url = normalize_base_url(&self.base_url);
|
||||
if base_url.contains("/backend-api") {
|
||||
format!("{base_url}/files")
|
||||
} else {
|
||||
format!("{base_url}/api/files")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run_from_arg(request_arg: &str) -> Result<FileTransferOutput> {
|
||||
let request: FileTransferRequest =
|
||||
serde_json::from_str(request_arg).context("failed to decode file transfer request")?;
|
||||
let config = FileTransferConfig::from_env()?;
|
||||
run_request(&config, &request).await
|
||||
}
|
||||
|
||||
pub async fn run_request(
|
||||
config: &FileTransferConfig,
|
||||
request: &FileTransferRequest,
|
||||
) -> Result<FileTransferOutput> {
|
||||
match request {
|
||||
FileTransferRequest::Upload { path } => {
|
||||
Ok(FileTransferOutput::Upload(upload_file(config, path).await?))
|
||||
}
|
||||
FileTransferRequest::Download {
|
||||
file_id,
|
||||
path,
|
||||
path_is_directory,
|
||||
} => Ok(FileTransferOutput::Download(
|
||||
download_file(config, file_id, path, *path_is_directory).await?,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn upload_file(config: &FileTransferConfig, path: &Path) -> Result<UploadFileToolResult> {
|
||||
let file_name = path
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.map(str::to_owned)
|
||||
.context("upload path has no valid UTF-8 filename")?;
|
||||
let metadata = match tokio::fs::metadata(path).await {
|
||||
Ok(metadata) => metadata,
|
||||
Err(err) => return Ok(upload_path_error(path, err)),
|
||||
};
|
||||
let file_size = metadata.len();
|
||||
|
||||
let client = build_reqwest_client_with_custom_ca(reqwest::Client::builder())?;
|
||||
let create_url = config.files_api_url();
|
||||
let create_request = CreateFileRequestBody {
|
||||
file_name: file_name.clone(),
|
||||
file_size,
|
||||
use_case: "codex",
|
||||
};
|
||||
let create_response = client
|
||||
.post(&create_url)
|
||||
.headers(config.headers()?)
|
||||
.json(&create_request)
|
||||
.send()
|
||||
.await
|
||||
.context("failed to create file upload")?;
|
||||
if !create_response.status().is_success() {
|
||||
let status = create_response.status().as_u16();
|
||||
let body = create_response.text().await.unwrap_or_default();
|
||||
return Ok(UploadFileToolResult {
|
||||
ok: false,
|
||||
file_id: None,
|
||||
uri: None,
|
||||
file_name: Some(file_name),
|
||||
file_size_bytes: Some(file_size),
|
||||
mime_type: None,
|
||||
error_code: Some("backend_request_failed".to_string()),
|
||||
message: Some(format!(
|
||||
"file create request failed with status {status}: {body}"
|
||||
)),
|
||||
retryable: Some(status >= 500),
|
||||
http_status_code: Some(status),
|
||||
path: Some(path.display().to_string()),
|
||||
});
|
||||
}
|
||||
let create_payload: CreateFileResponseBody = create_response
|
||||
.json()
|
||||
.await
|
||||
.context("failed to decode file create response")?;
|
||||
|
||||
let mime_type = MimeGuess::from_path(path)
|
||||
.first_or_octet_stream()
|
||||
.to_string();
|
||||
let upload_file = match tokio::fs::File::open(path).await {
|
||||
Ok(file) => file,
|
||||
Err(err) => return Ok(upload_path_error(path, err)),
|
||||
};
|
||||
let upload_response = client
|
||||
.put(&create_payload.upload_url)
|
||||
.header("x-ms-blob-type", "BlockBlob")
|
||||
.header(CONTENT_TYPE, mime_type.as_str())
|
||||
.body(reqwest::Body::wrap_stream(ReaderStream::new(upload_file)))
|
||||
.send()
|
||||
.await
|
||||
.context("failed to upload file bytes")?;
|
||||
if !upload_response.status().is_success() {
|
||||
let status = upload_response.status().as_u16();
|
||||
let body = upload_response.text().await.unwrap_or_default();
|
||||
return Ok(UploadFileToolResult {
|
||||
ok: false,
|
||||
file_id: Some(create_payload.file_id.clone()),
|
||||
uri: Some(openai_file_uri(&create_payload.file_id)),
|
||||
file_name: Some(file_name),
|
||||
file_size_bytes: Some(file_size),
|
||||
mime_type: Some(mime_type),
|
||||
error_code: Some("upload_failed".to_string()),
|
||||
message: Some(format!("byte upload failed with status {status}: {body}")),
|
||||
retryable: Some(status >= 500),
|
||||
http_status_code: Some(status),
|
||||
path: Some(path.display().to_string()),
|
||||
});
|
||||
}
|
||||
|
||||
let finalize_url = format!("{}/{}/uploaded", create_url, create_payload.file_id);
|
||||
let finalize_response = client
|
||||
.post(&finalize_url)
|
||||
.headers(config.headers()?)
|
||||
.send()
|
||||
.await
|
||||
.context("failed to finalize uploaded file")?;
|
||||
if !finalize_response.status().is_success() {
|
||||
let status = finalize_response.status().as_u16();
|
||||
let body = finalize_response.text().await.unwrap_or_default();
|
||||
return Ok(UploadFileToolResult {
|
||||
ok: false,
|
||||
file_id: Some(create_payload.file_id.clone()),
|
||||
uri: Some(openai_file_uri(&create_payload.file_id)),
|
||||
file_name: Some(file_name),
|
||||
file_size_bytes: Some(file_size),
|
||||
mime_type: Some(mime_type),
|
||||
error_code: Some("finalize_failed".to_string()),
|
||||
message: Some(format!(
|
||||
"finalize request failed with status {status}: {body}"
|
||||
)),
|
||||
retryable: Some(status >= 500),
|
||||
http_status_code: Some(status),
|
||||
path: Some(path.display().to_string()),
|
||||
});
|
||||
}
|
||||
let finalize_payload: FinalizeUploadResponseBody = finalize_response
|
||||
.json()
|
||||
.await
|
||||
.context("failed to decode finalize response")?;
|
||||
let _ = &finalize_payload.status;
|
||||
let _ = &finalize_payload.download_url;
|
||||
|
||||
Ok(UploadFileToolResult {
|
||||
ok: true,
|
||||
file_id: Some(create_payload.file_id.clone()),
|
||||
uri: Some(openai_file_uri(&create_payload.file_id)),
|
||||
file_name: Some(finalize_payload.file_name),
|
||||
file_size_bytes: Some(finalize_payload.file_size_bytes),
|
||||
mime_type: Some(finalize_payload.mime_type),
|
||||
error_code: None,
|
||||
message: None,
|
||||
retryable: None,
|
||||
http_status_code: None,
|
||||
path: Some(path.display().to_string()),
|
||||
})
|
||||
}
|
||||
|
||||
async fn download_file(
|
||||
config: &FileTransferConfig,
|
||||
file_id: &str,
|
||||
path: &Path,
|
||||
path_is_directory: bool,
|
||||
) -> Result<DownloadFileToolResult> {
|
||||
let client = build_reqwest_client_with_custom_ca(reqwest::Client::builder())?;
|
||||
let url = format!("{}/download/{file_id}", config.files_api_url());
|
||||
let link_response = client
|
||||
.get(&url)
|
||||
.headers(config.headers()?)
|
||||
.send()
|
||||
.await
|
||||
.context("failed to start file download")?;
|
||||
|
||||
if !link_response.status().is_success() {
|
||||
let status = link_response.status().as_u16();
|
||||
let body = link_response.text().await.unwrap_or_default();
|
||||
return Ok(DownloadFileToolResult {
|
||||
ok: false,
|
||||
file_id: Some(file_id.to_string()),
|
||||
uri: Some(openai_file_uri(file_id)),
|
||||
file_name: None,
|
||||
mime_type: None,
|
||||
destination_path: Some(path.display().to_string()),
|
||||
bytes_written: None,
|
||||
error_code: Some("download_failed".to_string()),
|
||||
message: Some(format!(
|
||||
"download request failed with status {status}: {body}"
|
||||
)),
|
||||
retryable: Some(status >= 500),
|
||||
http_status_code: Some(status),
|
||||
});
|
||||
}
|
||||
|
||||
let link_payload: DownloadLinkResponseBody = link_response
|
||||
.json()
|
||||
.await
|
||||
.context("failed to decode download link response")?;
|
||||
if link_payload.status != "success" {
|
||||
return Ok(DownloadFileToolResult {
|
||||
ok: false,
|
||||
file_id: Some(file_id.to_string()),
|
||||
uri: Some(openai_file_uri(file_id)),
|
||||
file_name: link_payload.file_name,
|
||||
mime_type: link_payload.mime_type,
|
||||
destination_path: Some(path.display().to_string()),
|
||||
bytes_written: None,
|
||||
error_code: Some("download_failed".to_string()),
|
||||
message: Some(format!(
|
||||
"download link response reported status `{}`",
|
||||
link_payload.status
|
||||
)),
|
||||
retryable: Some(link_payload.status == "retry"),
|
||||
http_status_code: None,
|
||||
});
|
||||
}
|
||||
let Some(download_url) = link_payload.download_url else {
|
||||
return Ok(DownloadFileToolResult {
|
||||
ok: false,
|
||||
file_id: Some(file_id.to_string()),
|
||||
uri: Some(openai_file_uri(file_id)),
|
||||
file_name: link_payload.file_name,
|
||||
mime_type: link_payload.mime_type,
|
||||
destination_path: Some(path.display().to_string()),
|
||||
bytes_written: None,
|
||||
error_code: Some("download_failed".to_string()),
|
||||
message: Some("download link response did not include a download_url".to_string()),
|
||||
retryable: Some(false),
|
||||
http_status_code: None,
|
||||
});
|
||||
};
|
||||
let response = client
|
||||
.get(download_url)
|
||||
.headers(config.headers()?)
|
||||
.send()
|
||||
.await
|
||||
.context("failed to fetch downloaded file bytes")?;
|
||||
if !response.status().is_success() {
|
||||
let status = response.status().as_u16();
|
||||
let body = response.text().await.unwrap_or_default();
|
||||
return Ok(DownloadFileToolResult {
|
||||
ok: false,
|
||||
file_id: Some(file_id.to_string()),
|
||||
uri: Some(openai_file_uri(file_id)),
|
||||
file_name: link_payload.file_name,
|
||||
mime_type: link_payload.mime_type,
|
||||
destination_path: Some(path.display().to_string()),
|
||||
bytes_written: None,
|
||||
error_code: Some("download_failed".to_string()),
|
||||
message: Some(format!(
|
||||
"download bytes request failed with status {status}: {body}"
|
||||
)),
|
||||
retryable: Some(status >= 500),
|
||||
http_status_code: Some(status),
|
||||
});
|
||||
}
|
||||
|
||||
let file_name = response
|
||||
.headers()
|
||||
.get(CONTENT_DISPOSITION)
|
||||
.and_then(|value| value.to_str().ok())
|
||||
.and_then(parse_content_disposition_filename)
|
||||
.map(|file_name| sanitize_download_file_name(&file_name))
|
||||
.or_else(|| {
|
||||
link_payload
|
||||
.file_name
|
||||
.as_deref()
|
||||
.map(sanitize_download_file_name)
|
||||
})
|
||||
.unwrap_or_else(|| file_id.to_string());
|
||||
let mime_type = response
|
||||
.headers()
|
||||
.get(CONTENT_TYPE)
|
||||
.and_then(|value| value.to_str().ok())
|
||||
.map(str::to_owned)
|
||||
.or(link_payload.mime_type)
|
||||
.unwrap_or_else(|| "application/octet-stream".to_string());
|
||||
let destination_path = if path_is_directory {
|
||||
path.join(&file_name)
|
||||
} else {
|
||||
path.to_path_buf()
|
||||
};
|
||||
let mut destination_file = match tokio::fs::File::create(&destination_path).await {
|
||||
Ok(file) => file,
|
||||
Err(err) => return Ok(download_path_error(file_id, path, &destination_path, err)),
|
||||
};
|
||||
let mut bytes_written = 0u64;
|
||||
let mut response = response;
|
||||
while let Some(chunk) = response
|
||||
.chunk()
|
||||
.await
|
||||
.context("failed to read download bytes")?
|
||||
{
|
||||
if let Err(err) = destination_file.write_all(&chunk).await {
|
||||
return Ok(download_path_error(file_id, path, &destination_path, err));
|
||||
}
|
||||
bytes_written += chunk.len() as u64;
|
||||
}
|
||||
if let Err(err) = destination_file.flush().await {
|
||||
return Ok(download_path_error(file_id, path, &destination_path, err));
|
||||
}
|
||||
|
||||
Ok(DownloadFileToolResult {
|
||||
ok: true,
|
||||
file_id: Some(file_id.to_string()),
|
||||
uri: Some(openai_file_uri(file_id)),
|
||||
file_name: Some(file_name),
|
||||
mime_type: Some(mime_type),
|
||||
destination_path: Some(destination_path.display().to_string()),
|
||||
bytes_written: Some(bytes_written),
|
||||
error_code: None,
|
||||
message: None,
|
||||
retryable: None,
|
||||
http_status_code: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn normalize_base_url(base_url: &str) -> String {
|
||||
let trimmed = base_url.trim_end_matches('/');
|
||||
if (trimmed.starts_with("https://chatgpt.com")
|
||||
|| trimmed.starts_with("https://chat.openai.com"))
|
||||
&& !trimmed.contains("/backend-api")
|
||||
{
|
||||
format!("{trimmed}/backend-api")
|
||||
} else {
|
||||
trimmed.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
fn openai_file_uri(file_id: &str) -> String {
|
||||
format!("openai-file://v1/{file_id}")
|
||||
}
|
||||
|
||||
fn parse_content_disposition_filename(content_disposition: &str) -> Option<String> {
|
||||
for part in content_disposition.split(';').map(str::trim) {
|
||||
if let Some(value) = part.strip_prefix("filename*=") {
|
||||
let value = value.strip_prefix("UTF-8''").unwrap_or(value);
|
||||
return Some(percent_decode(value));
|
||||
}
|
||||
if let Some(value) = part.strip_prefix("filename=") {
|
||||
return Some(value.trim_matches('"').to_string());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn sanitize_download_file_name(file_name: &str) -> String {
|
||||
let sanitized = Path::new(file_name)
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.map(str::trim)
|
||||
.filter(|name| !name.is_empty())
|
||||
.unwrap_or("");
|
||||
if sanitized.is_empty() {
|
||||
"download".to_string()
|
||||
} else {
|
||||
sanitized.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
fn upload_path_error(path: &Path, err: std::io::Error) -> UploadFileToolResult {
|
||||
let error_code = match err.kind() {
|
||||
ErrorKind::NotFound => "path_not_found",
|
||||
ErrorKind::PermissionDenied => "sandbox_path_denied",
|
||||
_ => "upload_failed",
|
||||
};
|
||||
UploadFileToolResult {
|
||||
ok: false,
|
||||
file_id: None,
|
||||
uri: None,
|
||||
file_name: path
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.map(str::to_string),
|
||||
file_size_bytes: None,
|
||||
mime_type: None,
|
||||
error_code: Some(error_code.to_string()),
|
||||
message: Some(format!("failed to access `{}`: {err}", path.display())),
|
||||
retryable: Some(matches!(err.kind(), ErrorKind::Interrupted)),
|
||||
http_status_code: None,
|
||||
path: Some(path.display().to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn download_path_error(
|
||||
file_id: &str,
|
||||
requested_path: &Path,
|
||||
destination_path: &Path,
|
||||
err: std::io::Error,
|
||||
) -> DownloadFileToolResult {
|
||||
let error_code = match err.kind() {
|
||||
ErrorKind::NotFound => "destination_parent_missing",
|
||||
ErrorKind::PermissionDenied => "sandbox_path_denied",
|
||||
_ => "download_failed",
|
||||
};
|
||||
DownloadFileToolResult {
|
||||
ok: false,
|
||||
file_id: Some(file_id.to_string()),
|
||||
uri: Some(openai_file_uri(file_id)),
|
||||
file_name: None,
|
||||
mime_type: None,
|
||||
destination_path: Some(destination_path.display().to_string()),
|
||||
bytes_written: None,
|
||||
error_code: Some(error_code.to_string()),
|
||||
message: Some(format!(
|
||||
"failed to write download for `{}` to `{}`: {err}",
|
||||
requested_path.display(),
|
||||
destination_path.display()
|
||||
)),
|
||||
retryable: Some(matches!(err.kind(), ErrorKind::Interrupted)),
|
||||
http_status_code: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn percent_decode(value: &str) -> String {
|
||||
let mut decoded = String::with_capacity(value.len());
|
||||
let bytes = value.as_bytes();
|
||||
let mut index = 0usize;
|
||||
while index < bytes.len() {
|
||||
if bytes[index] == b'%'
|
||||
&& index + 2 < bytes.len()
|
||||
&& let (Some(high), Some(low)) =
|
||||
(hex_value(bytes[index + 1]), hex_value(bytes[index + 2]))
|
||||
{
|
||||
decoded.push((high * 16 + low) as char);
|
||||
index += 3;
|
||||
continue;
|
||||
}
|
||||
decoded.push(bytes[index] as char);
|
||||
index += 1;
|
||||
}
|
||||
decoded
|
||||
}
|
||||
|
||||
fn hex_value(byte: u8) -> Option<u8> {
|
||||
match byte {
|
||||
b'0'..=b'9' => Some(byte - b'0'),
|
||||
b'a'..=b'f' => Some(byte - b'a' + 10),
|
||||
b'A'..=b'F' => Some(byte - b'A' + 10),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tempfile::tempdir;
|
||||
use wiremock::Mock;
|
||||
use wiremock::MockServer;
|
||||
use wiremock::ResponseTemplate;
|
||||
use wiremock::matchers::body_json;
|
||||
use wiremock::matchers::header;
|
||||
use wiremock::matchers::method;
|
||||
use wiremock::matchers::path;
|
||||
|
||||
fn test_config(server: &MockServer) -> FileTransferConfig {
|
||||
FileTransferConfig {
|
||||
base_url: format!("{}/backend-api", server.uri()),
|
||||
bearer_token: "token".to_string(),
|
||||
chatgpt_account_id: Some("acct".to_string()),
|
||||
user_agent: "codex-test".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn upload_request_uses_expected_flow() -> Result<()> {
|
||||
let server = MockServer::start().await;
|
||||
let file_id = "file-123";
|
||||
let upload_url = format!("{}/upload-target", server.uri());
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/backend-api/files"))
|
||||
.and(header(AUTHORIZATION.as_str(), "Bearer token"))
|
||||
.and(header("chatgpt-account-id", "acct"))
|
||||
.and(body_json(serde_json::json!({
|
||||
"file_name": "hello.txt",
|
||||
"file_size": 5,
|
||||
"use_case": "codex"
|
||||
})))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
|
||||
"file_id": file_id,
|
||||
"upload_url": upload_url
|
||||
})))
|
||||
.mount(&server)
|
||||
.await;
|
||||
Mock::given(method("PUT"))
|
||||
.and(path("/upload-target"))
|
||||
.respond_with(ResponseTemplate::new(201))
|
||||
.mount(&server)
|
||||
.await;
|
||||
Mock::given(method("POST"))
|
||||
.and(path(format!("/backend-api/files/{file_id}/uploaded")))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
|
||||
"status": "success",
|
||||
"download_url": format!("{}/blob/file-123", server.uri()),
|
||||
"file_name": "hello.txt",
|
||||
"file_size_bytes": 5,
|
||||
"mime_type": "text/plain"
|
||||
})))
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
let dir = tempdir()?;
|
||||
let path = dir.path().join("hello.txt");
|
||||
tokio::fs::write(&path, b"hello").await?;
|
||||
|
||||
let output = upload_file(&test_config(&server), &path).await?;
|
||||
assert_eq!(
|
||||
output,
|
||||
UploadFileToolResult {
|
||||
ok: true,
|
||||
file_id: Some(file_id.to_string()),
|
||||
uri: Some(openai_file_uri(file_id)),
|
||||
file_name: Some("hello.txt".to_string()),
|
||||
file_size_bytes: Some(5),
|
||||
mime_type: Some("text/plain".to_string()),
|
||||
error_code: None,
|
||||
message: None,
|
||||
retryable: None,
|
||||
http_status_code: None,
|
||||
path: Some(path.display().to_string()),
|
||||
}
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn download_directory_uses_remote_file_name() -> Result<()> {
|
||||
let server = MockServer::start().await;
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/backend-api/files/download/file-456"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
|
||||
"status": "success",
|
||||
"download_url": format!("{}/content/file-456", server.uri()),
|
||||
"file_name": "ignored.txt",
|
||||
"mime_type": "text/plain"
|
||||
})))
|
||||
.mount(&server)
|
||||
.await;
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/content/file-456"))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(200)
|
||||
.insert_header(CONTENT_TYPE.as_str(), "text/plain")
|
||||
.insert_header(
|
||||
CONTENT_DISPOSITION.as_str(),
|
||||
"attachment; filename*=UTF-8''report%20final.txt",
|
||||
)
|
||||
.set_body_bytes(b"downloaded".to_vec()),
|
||||
)
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
let dir = tempdir()?;
|
||||
let output = download_file(&test_config(&server), "file-456", dir.path(), true).await?;
|
||||
let expected_path = dir.path().join("report final.txt");
|
||||
assert_eq!(tokio::fs::read(&expected_path).await?, b"downloaded");
|
||||
assert_eq!(
|
||||
output,
|
||||
DownloadFileToolResult {
|
||||
ok: true,
|
||||
file_id: Some("file-456".to_string()),
|
||||
uri: Some(openai_file_uri("file-456")),
|
||||
file_name: Some("report final.txt".to_string()),
|
||||
mime_type: Some("text/plain".to_string()),
|
||||
destination_path: Some(expected_path.display().to_string()),
|
||||
bytes_written: Some(10),
|
||||
error_code: None,
|
||||
message: None,
|
||||
retryable: None,
|
||||
http_status_code: None,
|
||||
}
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn download_directory_sanitizes_remote_file_name() -> Result<()> {
|
||||
let server = MockServer::start().await;
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/backend-api/files/download/file-456"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
|
||||
"status": "success",
|
||||
"download_url": format!("{}/content/file-456", server.uri()),
|
||||
"file_name": "../report.txt",
|
||||
"mime_type": "text/plain"
|
||||
})))
|
||||
.mount(&server)
|
||||
.await;
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/content/file-456"))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(200)
|
||||
.insert_header(CONTENT_TYPE.as_str(), "text/plain")
|
||||
.insert_header(
|
||||
CONTENT_DISPOSITION.as_str(),
|
||||
"attachment; filename=../report.txt",
|
||||
)
|
||||
.set_body_bytes(b"downloaded".to_vec()),
|
||||
)
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
let dir = tempdir()?;
|
||||
let output = download_file(&test_config(&server), "file-456", dir.path(), true).await?;
|
||||
let expected_path = dir.path().join("report.txt");
|
||||
assert_eq!(
|
||||
output,
|
||||
DownloadFileToolResult {
|
||||
ok: true,
|
||||
file_id: Some("file-456".to_string()),
|
||||
uri: Some(openai_file_uri("file-456")),
|
||||
file_name: Some("report.txt".to_string()),
|
||||
mime_type: Some("text/plain".to_string()),
|
||||
destination_path: Some(expected_path.display().to_string()),
|
||||
bytes_written: Some(10),
|
||||
error_code: None,
|
||||
message: None,
|
||||
retryable: None,
|
||||
http_status_code: None,
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
tokio::fs::read_to_string(expected_path).await?,
|
||||
"downloaded"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn download_uses_json_link_then_fetches_bytes() -> Result<()> {
|
||||
let server = MockServer::start().await;
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/backend-api/files/download/file-789"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
|
||||
"status": "success",
|
||||
"download_url": format!("{}/content/file-789", server.uri()),
|
||||
"file_name": "fallback.txt",
|
||||
"mime_type": "text/plain"
|
||||
})))
|
||||
.mount(&server)
|
||||
.await;
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/content/file-789"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_bytes(b"hello bytes".to_vec()))
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
let dir = tempdir()?;
|
||||
let path = dir.path().join("out.txt");
|
||||
let output = download_file(&test_config(&server), "file-789", &path, false).await?;
|
||||
assert_eq!(tokio::fs::read_to_string(path).await?, "hello bytes");
|
||||
assert_eq!(output.file_name, Some("fallback.txt".to_string()));
|
||||
assert_eq!(output.mime_type, Some("text/plain".to_string()));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn upload_missing_path_returns_structured_error() -> Result<()> {
|
||||
let server = MockServer::start().await;
|
||||
let dir = tempdir()?;
|
||||
let path = dir.path().join("missing.txt");
|
||||
|
||||
let output = upload_file(&test_config(&server), &path).await?;
|
||||
assert_eq!(output.ok, false);
|
||||
assert_eq!(output.file_id, None);
|
||||
assert_eq!(output.uri, None);
|
||||
assert_eq!(output.file_name, Some("missing.txt".to_string()));
|
||||
assert_eq!(output.file_size_bytes, None);
|
||||
assert_eq!(output.mime_type, None);
|
||||
assert_eq!(output.error_code, Some("path_not_found".to_string()));
|
||||
assert_eq!(output.retryable, Some(false));
|
||||
assert_eq!(output.http_status_code, None);
|
||||
assert_eq!(output.path, Some(path.display().to_string()));
|
||||
assert!(
|
||||
output
|
||||
.message
|
||||
.as_deref()
|
||||
.is_some_and(|message| message.contains(&path.display().to_string())),
|
||||
"message should mention missing path"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn files_api_url_uses_chatgpt_backend_style() {
|
||||
let config = FileTransferConfig {
|
||||
base_url: "https://chatgpt.com/backend-api/".to_string(),
|
||||
bearer_token: "token".to_string(),
|
||||
chatgpt_account_id: None,
|
||||
user_agent: "codex-test".to_string(),
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
config.files_api_url(),
|
||||
"https://chatgpt.com/backend-api/files"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn files_api_url_uses_non_chatgpt_api_style() {
|
||||
let config = FileTransferConfig {
|
||||
base_url: "https://example.test".to_string(),
|
||||
bearer_token: "token".to_string(),
|
||||
chatgpt_account_id: None,
|
||||
user_agent: "codex-test".to_string(),
|
||||
};
|
||||
|
||||
assert_eq!(config.files_api_url(), "https://example.test/api/files");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user