mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
56 lines
1.6 KiB
Rust
56 lines
1.6 KiB
Rust
use std::error::Error;
|
|
use std::path::PathBuf;
|
|
|
|
use tokio::io;
|
|
use tokio::io::AsyncReadExt;
|
|
use tokio::io::AsyncWriteExt;
|
|
|
|
use crate::fs_helper::FsHelperRequest;
|
|
use crate::fs_helper::FsHelperResponse;
|
|
use crate::fs_helper::run_direct_request;
|
|
|
|
pub fn main() -> ! {
|
|
let exit_code = match tokio::runtime::Builder::new_current_thread()
|
|
.enable_all()
|
|
.build()
|
|
{
|
|
Ok(runtime) => match runtime.block_on(run_main()) {
|
|
Ok(()) => 0,
|
|
Err(err) => {
|
|
eprintln!("fs sandbox helper failed: {err}");
|
|
1
|
|
}
|
|
},
|
|
Err(err) => {
|
|
eprintln!("failed to start fs sandbox helper runtime: {err}");
|
|
1
|
|
}
|
|
};
|
|
std::process::exit(exit_code);
|
|
}
|
|
|
|
async fn run_main() -> Result<(), Box<dyn Error + Send + Sync>> {
|
|
let input = read_request_input().await?;
|
|
let request: FsHelperRequest = serde_json::from_slice(&input)?;
|
|
let response = match run_direct_request(request).await {
|
|
Ok(payload) => FsHelperResponse::Ok(payload),
|
|
Err(error) => FsHelperResponse::Error(error),
|
|
};
|
|
let mut stdout = io::stdout();
|
|
stdout
|
|
.write_all(serde_json::to_string(&response)?.as_bytes())
|
|
.await?;
|
|
stdout.write_all(b"\n").await?;
|
|
Ok(())
|
|
}
|
|
|
|
async fn read_request_input() -> Result<Vec<u8>, Box<dyn Error + Send + Sync>> {
|
|
if let Some(path) = std::env::args_os().nth(2) {
|
|
return Ok(tokio::fs::read(PathBuf::from(path)).await?);
|
|
}
|
|
|
|
let mut input = Vec::new();
|
|
io::stdin().read_to_end(&mut input).await?;
|
|
Ok(input)
|
|
}
|