Skip to content

Commit d262244

Browse files
authored
fix: introduce codex-protocol crate (#2355)
1 parent 7c26c8e commit d262244

23 files changed

+244
-43
lines changed

codex-rs/Cargo.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codex-rs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ members = [
1515
"mcp-server",
1616
"mcp-types",
1717
"ollama",
18+
"protocol",
1819
"tui",
1920
]
2021
resolver = "2"

codex-rs/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ chrono = { version = "0.4", features = ["serde"] }
1919
codex-apply-patch = { path = "../apply-patch" }
2020
codex-login = { path = "../login" }
2121
codex-mcp-client = { path = "../mcp-client" }
22+
codex-protocol = { path = "../protocol" }
2223
dirs = "6"
2324
env-flags = "0.1.1"
2425
eventsource-stream = "0.2.3"

codex-rs/core/src/codex.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,10 @@ impl Session {
677677
call_id,
678678
command: command_for_display.clone(),
679679
cwd,
680-
parsed_cmd: parse_command(&command_for_display),
680+
parsed_cmd: parse_command(&command_for_display)
681+
.into_iter()
682+
.map(Into::into)
683+
.collect(),
681684
}),
682685
};
683686
let event = Event {
@@ -1031,8 +1034,8 @@ async fn submission_loop(
10311034
Arc::new(per_turn_config),
10321035
None,
10331036
provider,
1034-
effort,
1035-
summary,
1037+
effort.into(),
1038+
summary.into(),
10361039
sess.session_id,
10371040
);
10381041

@@ -1102,7 +1105,13 @@ async fn submission_loop(
11021105
crate::protocol::GetHistoryEntryResponseEvent {
11031106
offset,
11041107
log_id,
1105-
entry: entry_opt,
1108+
entry: entry_opt.map(|e| {
1109+
codex_protocol::message_history::HistoryEntry {
1110+
session_id: e.session_id,
1111+
ts: e.ts,
1112+
text: e.text,
1113+
}
1114+
}),
11061115
},
11071116
),
11081117
};
@@ -1160,6 +1169,9 @@ async fn submission_loop(
11601169
}
11611170
break;
11621171
}
1172+
_ => {
1173+
// Ignore unknown ops; enum is non_exhaustive to allow extensions.
1174+
}
11631175
}
11641176
}
11651177
debug!("Agent loop exited");

codex-rs/core/src/config_types.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,27 @@ pub enum ReasoningSummary {
228228
/// Option to disable reasoning summaries.
229229
None,
230230
}
231+
232+
// Conversions from protocol enums to core config enums used where protocol
233+
// values are supplied by clients and core needs its internal representations.
234+
impl From<codex_protocol::config_types::ReasoningEffort> for ReasoningEffort {
235+
fn from(v: codex_protocol::config_types::ReasoningEffort) -> Self {
236+
match v {
237+
codex_protocol::config_types::ReasoningEffort::Low => ReasoningEffort::Low,
238+
codex_protocol::config_types::ReasoningEffort::Medium => ReasoningEffort::Medium,
239+
codex_protocol::config_types::ReasoningEffort::High => ReasoningEffort::High,
240+
codex_protocol::config_types::ReasoningEffort::None => ReasoningEffort::None,
241+
}
242+
}
243+
}
244+
245+
impl From<codex_protocol::config_types::ReasoningSummary> for ReasoningSummary {
246+
fn from(v: codex_protocol::config_types::ReasoningSummary) -> Self {
247+
match v {
248+
codex_protocol::config_types::ReasoningSummary::Auto => ReasoningSummary::Auto,
249+
codex_protocol::config_types::ReasoningSummary::Concise => ReasoningSummary::Concise,
250+
codex_protocol::config_types::ReasoningSummary::Detailed => ReasoningSummary::Detailed,
251+
codex_protocol::config_types::ReasoningSummary::None => ReasoningSummary::None,
252+
}
253+
}
254+
}

codex-rs/core/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ mod openai_model_info;
4444
mod openai_tools;
4545
pub mod plan_tool;
4646
mod project_doc;
47-
pub mod protocol;
4847
mod rollout;
4948
pub(crate) mod safety;
5049
pub mod seatbelt;
@@ -56,3 +55,9 @@ mod user_notification;
5655
pub mod util;
5756
pub use apply_patch::CODEX_APPLY_PATCH_ARG1;
5857
pub use safety::get_platform_sandbox;
58+
// Re-export the protocol types from the standalone `codex-protocol` crate so existing
59+
// `codex_core::protocol::...` references continue to work across the workspace.
60+
pub use codex_protocol::protocol;
61+
// Re-export protocol config enums to ensure call sites can use the same types
62+
// as those in the protocol crate when constructing protocol messages.
63+
pub use codex_protocol::config_types as protocol_config_types;

codex-rs/core/src/models.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ impl From<Vec<InputItem>> for ResponseInputItem {
183183
None
184184
}
185185
},
186+
_ => None,
186187
})
187188
.collect::<Vec<ContentItem>>(),
188189
}

codex-rs/core/src/parse_command.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ pub enum ParsedCommand {
4141
},
4242
}
4343

44+
// Convert core's parsed command enum into the protocol's simplified type so
45+
// events can carry the canonical representation across process boundaries.
46+
impl From<ParsedCommand> for codex_protocol::parse_command::ParsedCommand {
47+
fn from(v: ParsedCommand) -> Self {
48+
use codex_protocol::parse_command::ParsedCommand as P;
49+
match v {
50+
ParsedCommand::Read { cmd, name } => P::Read { cmd, name },
51+
ParsedCommand::ListFiles { cmd, path } => P::ListFiles { cmd, path },
52+
ParsedCommand::Search { cmd, query, path } => P::Search { cmd, query, path },
53+
ParsedCommand::Format { cmd, tool, targets } => P::Format { cmd, tool, targets },
54+
ParsedCommand::Test { cmd } => P::Test { cmd },
55+
ParsedCommand::Lint { cmd, tool, targets } => P::Lint { cmd, tool, targets },
56+
ParsedCommand::Noop { cmd } => P::Noop { cmd },
57+
ParsedCommand::Unknown { cmd } => P::Unknown { cmd },
58+
}
59+
}
60+
}
61+
4462
fn shlex_join(tokens: &[String]) -> String {
4563
shlex_try_join(tokens.iter().map(|s| s.as_str()))
4664
.unwrap_or_else(|_| "<command included NUL byte>".to_string())

codex-rs/core/src/plan_tool.rs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use std::collections::BTreeMap;
22
use std::sync::LazyLock;
33

4-
use serde::Deserialize;
5-
use serde::Serialize;
6-
74
use crate::codex::Session;
85
use crate::models::FunctionCallOutputPayload;
96
use crate::models::ResponseInputItem;
@@ -13,29 +10,13 @@ use crate::openai_tools::ResponsesApiTool;
1310
use crate::protocol::Event;
1411
use crate::protocol::EventMsg;
1512

16-
// Types for the TODO tool arguments matching codex-vscode/todo-mcp/src/main.rs
17-
#[derive(Debug, Clone, Serialize, Deserialize)]
18-
#[serde(rename_all = "snake_case")]
19-
pub enum StepStatus {
20-
Pending,
21-
InProgress,
22-
Completed,
23-
}
13+
// Use the canonical plan tool types from the protocol crate to ensure
14+
// type-identity matches events transported via `codex_protocol`.
15+
pub use codex_protocol::plan_tool::PlanItemArg;
16+
pub use codex_protocol::plan_tool::StepStatus;
17+
pub use codex_protocol::plan_tool::UpdatePlanArgs;
2418

25-
#[derive(Debug, Clone, Serialize, Deserialize)]
26-
#[serde(deny_unknown_fields)]
27-
pub struct PlanItemArg {
28-
pub step: String,
29-
pub status: StepStatus,
30-
}
31-
32-
#[derive(Debug, Clone, Serialize, Deserialize)]
33-
#[serde(deny_unknown_fields)]
34-
pub struct UpdatePlanArgs {
35-
#[serde(default)]
36-
pub explanation: Option<String>,
37-
pub plan: Vec<PlanItemArg>,
38-
}
19+
// Types for the TODO tool arguments matching codex-vscode/todo-mcp/src/main.rs
3920

4021
pub(crate) static PLAN_TOOL: LazyLock<OpenAiTool> = LazyLock::new(|| {
4122
let mut plan_item_props = BTreeMap::new();

codex-rs/mcp-server/src/wire_format.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use std::collections::HashMap;
22
use std::fmt::Display;
33
use std::path::PathBuf;
44

5-
use codex_core::config_types::ReasoningEffort;
6-
use codex_core::config_types::ReasoningSummary;
75
use codex_core::protocol::AskForApproval;
86
use codex_core::protocol::FileChange;
97
use codex_core::protocol::ReviewDecision;
108
use codex_core::protocol::SandboxPolicy;
9+
use codex_core::protocol_config_types::ReasoningEffort;
10+
use codex_core::protocol_config_types::ReasoningSummary;
1111
use mcp_types::RequestId;
1212
use serde::Deserialize;
1313
use serde::Serialize;

0 commit comments

Comments
 (0)