Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions codex-rs/app-server-protocol/src/protocol/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1719,14 +1719,16 @@ mod tests {
request_id: RequestId::Integer(9),
params: v2::FsGetMetadataParams {
path: absolute_path("tmp/example"),
environment_id: None,
},
};
assert_eq!(
json!({
"method": "fs/getMetadata",
"id": 9,
"params": {
"path": absolute_path_string("tmp/example")
"path": absolute_path_string("tmp/example"),
"environmentId": null
}
}),
serde_json::to_value(&request)?,
Expand All @@ -1741,6 +1743,7 @@ mod tests {
params: v2::FsWatchParams {
watch_id: "watch-git".to_string(),
path: absolute_path("tmp/repo/.git"),
environment_id: None,
},
};
assert_eq!(
Expand All @@ -1749,7 +1752,8 @@ mod tests {
"id": 10,
"params": {
"watchId": "watch-git",
"path": absolute_path_string("tmp/repo/.git")
"path": absolute_path_string("tmp/repo/.git"),
"environmentId": null
}
}),
serde_json::to_value(&request)?,
Expand Down
32 changes: 32 additions & 0 deletions codex-rs/app-server-protocol/src/protocol/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2333,6 +2333,9 @@ pub struct EnvironmentListResponse {
pub struct FsReadFileParams {
/// Absolute path to read.
pub path: AbsolutePathBuf,
/// Optional environment selection. Omit to use the default environment.
#[ts(optional = nullable)]
pub environment_id: Option<String>,
}

/// Base64-encoded file contents returned by `fs/readFile`.
Expand All @@ -2353,6 +2356,9 @@ pub struct FsWriteFileParams {
pub path: AbsolutePathBuf,
/// File contents encoded as base64.
pub data_base64: String,
/// Optional environment selection. Omit to use the default environment.
#[ts(optional = nullable)]
pub environment_id: Option<String>,
}

/// Successful response for `fs/writeFile`.
Expand All @@ -2371,6 +2377,9 @@ pub struct FsCreateDirectoryParams {
/// Whether parent directories should also be created. Defaults to `true`.
#[ts(optional = nullable)]
pub recursive: Option<bool>,
/// Optional environment selection. Omit to use the default environment.
#[ts(optional = nullable)]
pub environment_id: Option<String>,
}

/// Successful response for `fs/createDirectory`.
Expand All @@ -2386,6 +2395,9 @@ pub struct FsCreateDirectoryResponse {}
pub struct FsGetMetadataParams {
/// Absolute path to inspect.
pub path: AbsolutePathBuf,
/// Optional environment selection. Omit to use the default environment.
#[ts(optional = nullable)]
pub environment_id: Option<String>,
}

/// Metadata returned by `fs/getMetadata`.
Expand Down Expand Up @@ -2414,6 +2426,9 @@ pub struct FsGetMetadataResponse {
pub struct FsReadDirectoryParams {
/// Absolute directory path to read.
pub path: AbsolutePathBuf,
/// Optional environment selection. Omit to use the default environment.
#[ts(optional = nullable)]
pub environment_id: Option<String>,
}

/// A directory entry returned by `fs/readDirectory`.
Expand Down Expand Up @@ -2451,6 +2466,9 @@ pub struct FsRemoveParams {
/// Whether missing paths should be ignored. Defaults to `true`.
#[ts(optional = nullable)]
pub force: Option<bool>,
/// Optional environment selection. Omit to use the default environment.
#[ts(optional = nullable)]
pub environment_id: Option<String>,
}

/// Successful response for `fs/remove`.
Expand All @@ -2471,6 +2489,9 @@ pub struct FsCopyParams {
/// Required for directory copies; ignored for file copies.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub recursive: bool,
/// Optional environment selection. Omit to use the default environment.
#[ts(optional = nullable)]
pub environment_id: Option<String>,
}

/// Successful response for `fs/copy`.
Expand All @@ -2488,6 +2509,9 @@ pub struct FsWatchParams {
pub watch_id: String,
/// Absolute file or directory path to watch.
pub path: AbsolutePathBuf,
/// Optional environment selection. Omit to use the default environment.
#[ts(optional = nullable)]
pub environment_id: Option<String>,
}

/// Successful response for `fs/watch`.
Expand Down Expand Up @@ -6953,13 +6977,15 @@ mod tests {
fn fs_read_file_params_round_trip() {
let params = FsReadFileParams {
path: absolute_path("tmp/example.txt"),
environment_id: Some("dev".to_string()),
};

let value = serde_json::to_value(&params).expect("serialize fs/readFile params");
assert_eq!(
value,
json!({
"path": absolute_path_string("tmp/example.txt"),
"environmentId": "dev",
})
);

Expand All @@ -6973,6 +6999,7 @@ mod tests {
let params = FsCreateDirectoryParams {
path: absolute_path("tmp/example"),
recursive: None,
environment_id: Some("dev".to_string()),
};

let value = serde_json::to_value(&params).expect("serialize fs/createDirectory params");
Expand All @@ -6981,6 +7008,7 @@ mod tests {
json!({
"path": absolute_path_string("tmp/example"),
"recursive": null,
"environmentId": "dev",
})
);

Expand All @@ -6994,6 +7022,7 @@ mod tests {
let params = FsWriteFileParams {
path: absolute_path("tmp/example.bin"),
data_base64: "AAE=".to_string(),
environment_id: Some("dev".to_string()),
};

let value = serde_json::to_value(&params).expect("serialize fs/writeFile params");
Expand All @@ -7002,6 +7031,7 @@ mod tests {
json!({
"path": absolute_path_string("tmp/example.bin"),
"dataBase64": "AAE=",
"environmentId": "dev",
})
);

Expand All @@ -7016,6 +7046,7 @@ mod tests {
source_path: absolute_path("tmp/source"),
destination_path: absolute_path("tmp/destination"),
recursive: true,
environment_id: Some("dev".to_string()),
};

let value = serde_json::to_value(&params).expect("serialize fs/copy params");
Expand All @@ -7025,6 +7056,7 @@ mod tests {
"sourcePath": absolute_path_string("tmp/source"),
"destinationPath": absolute_path_string("tmp/destination"),
"recursive": true,
"environmentId": "dev",
})
);

Expand Down
50 changes: 33 additions & 17 deletions codex-rs/app-server/src/fs_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,44 @@ use codex_app_server_protocol::FsWriteFileResponse;
use codex_app_server_protocol::JSONRPCErrorError;
use codex_exec_server::CopyOptions;
use codex_exec_server::CreateDirectoryOptions;
use codex_exec_server::Environment;
use codex_exec_server::EnvironmentManager;
use codex_exec_server::ExecutorFileSystem;
use codex_exec_server::RemoveOptions;
use std::io;
use std::sync::Arc;

#[derive(Clone)]
pub(crate) struct FsApi {
file_system: Arc<dyn ExecutorFileSystem>,
environment_manager: Arc<EnvironmentManager>,
}

impl Default for FsApi {
fn default() -> Self {
impl FsApi {
pub(crate) fn new(environment_manager: Arc<EnvironmentManager>) -> Self {
Self {
file_system: Environment::default().get_filesystem(),
environment_manager,
}
}
}

impl FsApi {
async fn file_system(
&self,
environment_id: Option<&str>,
) -> Result<Arc<dyn ExecutorFileSystem>, JSONRPCErrorError> {
let environment = self
.environment_manager
.environment(environment_id)
.await
.map_err(|err| invalid_request(format!("failed to resolve environment: {err}")))?;
let environment =
environment.ok_or_else(|| invalid_request("the selected environment is disabled"))?;
Ok(environment.get_filesystem())
}

pub(crate) async fn read_file(
&self,
params: FsReadFileParams,
) -> Result<FsReadFileResponse, JSONRPCErrorError> {
let bytes = self
.file_system
let file_system = self.file_system(params.environment_id.as_deref()).await?;
let bytes = file_system
.read_file(&params.path, /*sandbox*/ None)
.await
.map_err(map_fs_error)?;
Expand All @@ -58,12 +70,13 @@ impl FsApi {
&self,
params: FsWriteFileParams,
) -> Result<FsWriteFileResponse, JSONRPCErrorError> {
let file_system = self.file_system(params.environment_id.as_deref()).await?;
let bytes = STANDARD.decode(params.data_base64).map_err(|err| {
invalid_request(format!(
"fs/writeFile requires valid base64 dataBase64: {err}"
))
})?;
self.file_system
file_system
.write_file(&params.path, bytes, /*sandbox*/ None)
.await
.map_err(map_fs_error)?;
Expand All @@ -74,7 +87,8 @@ impl FsApi {
&self,
params: FsCreateDirectoryParams,
) -> Result<FsCreateDirectoryResponse, JSONRPCErrorError> {
self.file_system
let file_system = self.file_system(params.environment_id.as_deref()).await?;
file_system
.create_directory(
&params.path,
CreateDirectoryOptions {
Expand All @@ -91,8 +105,8 @@ impl FsApi {
&self,
params: FsGetMetadataParams,
) -> Result<FsGetMetadataResponse, JSONRPCErrorError> {
let metadata = self
.file_system
let file_system = self.file_system(params.environment_id.as_deref()).await?;
let metadata = file_system
.get_metadata(&params.path, /*sandbox*/ None)
.await
.map_err(map_fs_error)?;
Expand All @@ -109,8 +123,8 @@ impl FsApi {
&self,
params: FsReadDirectoryParams,
) -> Result<FsReadDirectoryResponse, JSONRPCErrorError> {
let entries = self
.file_system
let file_system = self.file_system(params.environment_id.as_deref()).await?;
let entries = file_system
.read_directory(&params.path, /*sandbox*/ None)
.await
.map_err(map_fs_error)?;
Expand All @@ -130,7 +144,8 @@ impl FsApi {
&self,
params: FsRemoveParams,
) -> Result<FsRemoveResponse, JSONRPCErrorError> {
self.file_system
let file_system = self.file_system(params.environment_id.as_deref()).await?;
file_system
.remove(
&params.path,
RemoveOptions {
Expand All @@ -148,7 +163,8 @@ impl FsApi {
&self,
params: FsCopyParams,
) -> Result<FsCopyResponse, JSONRPCErrorError> {
self.file_system
let file_system = self.file_system(params.environment_id.as_deref()).await?;
file_system
.copy(
&params.source_path,
&params.destination_path,
Expand Down
Loading
Loading