diff --git a/codex-rs/cli/examples/fibonacci.rs b/codex-rs/cli/examples/fibonacci.rs new file mode 100644 index 0000000000..4cd81803bd --- /dev/null +++ b/codex-rs/cli/examples/fibonacci.rs @@ -0,0 +1,53 @@ +//! Print the Fibonacci sequence. +//! +//! Usage: +//! cargo run -p codex-cli --example fibonacci -- [COUNT] +//! +//! If COUNT is omitted, the first 10 numbers are printed. + +use std::env; +use std::process; + +fn fibonacci(count: usize) -> Vec { + let mut seq = Vec::with_capacity(count); + if count == 0 { + return seq; + } + // Start with 0, 1 + let mut a: u128 = 0; + let mut b: u128 = 1; + for _ in 0..count { + seq.push(a); + let next = a.saturating_add(b); + a = b; + b = next; + } + seq +} + +fn parse_count_arg() -> Result { + let mut args = env::args().skip(1); + match args.next() { + None => Ok(10), // default + Some(s) => s + .parse::() + .map_err(|_| format!("Invalid COUNT: '{}' (expected a non-negative integer)", s)), + } +} + +fn main() { + let count = match parse_count_arg() { + Ok(n) => n, + Err(e) => { + eprintln!( + "{}\nUsage: cargo run -p codex-cli --example fibonacci -- [COUNT]", + e + ); + process::exit(2); + } + }; + + for n in fibonacci(count) { + println!("{}", n); + } +} diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index aaef73ded9..f461f4206e 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -65,6 +65,7 @@ use crate::models::ResponseItem; use crate::models::ShellToolCallParams; use crate::openai_tools::ToolsConfig; use crate::openai_tools::get_openai_tools; +use crate::parse_command::parse_command; use crate::plan_tool::handle_update_plan; use crate::project_doc::get_user_instructions; use crate::protocol::AgentMessageDeltaEvent; @@ -373,7 +374,7 @@ impl Session { } } - async fn on_exec_command_begin( + pub async fn on_exec_command_begin( &self, turn_diff_tracker: &mut TurnDiffTracker, exec_command_context: ExecCommandContext, @@ -402,6 +403,7 @@ impl Session { call_id, command: command_for_display.clone(), cwd, + parsed_cmd: parse_command(&command_for_display), }), }; let event = Event { diff --git a/codex-rs/core/src/lib.rs b/codex-rs/core/src/lib.rs index c728bd3125..b36689f057 100644 --- a/codex-rs/core/src/lib.rs +++ b/codex-rs/core/src/lib.rs @@ -28,6 +28,7 @@ mod mcp_connection_manager; mod mcp_tool_call; mod message_history; mod model_provider_info; +pub mod parse_command; pub use model_provider_info::BUILT_IN_OSS_MODEL_PROVIDER_ID; pub use model_provider_info::ModelProviderInfo; pub use model_provider_info::WireApi; diff --git a/codex-rs/core/src/parse_command.rs b/codex-rs/core/src/parse_command.rs new file mode 100644 index 0000000000..51810b8083 --- /dev/null +++ b/codex-rs/core/src/parse_command.rs @@ -0,0 +1,648 @@ +use crate::bash::try_parse_bash; +use crate::bash::try_parse_word_only_commands_sequence; +use serde::Deserialize; +use serde::Serialize; +use shlex::split as shlex_split; + +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] +pub enum ParsedCommand { + Read { + cmd: Vec, + name: String, + }, + Python { + cmd: Vec, + }, + GitStatus { + cmd: Vec, + }, + GitLog { + cmd: Vec, + }, + GitDiff { + cmd: Vec, + }, + Ls { + cmd: Vec, + path: Option, + }, + Rg { + cmd: Vec, + query: Option, + path: Option, + files_only: bool, + }, + Shell { + cmd: Vec, + display: String, + }, + Pnpm { + cmd: Vec, + pnpm_cmd: String, + }, + Unknown { + cmd: Vec, + }, +} + +pub fn parse_command(command: &[String]) -> Vec { + let main_cmd = extract_main_cmd_tokens(command); + + // 1) Try the "bash -lc