Skip to main content

codex_codes/
protocol.rs

1//! App-server v2 protocol types for the Codex CLI.
2//!
3//! These types represent the JSON-RPC request parameters, response payloads,
4//! and notification bodies used by `codex app-server`. All wire types use
5//! camelCase field names via `#[serde(rename_all = "camelCase")]`.
6//!
7//! # Organization
8//!
9//! - **Request/Response pairs** — [`ThreadStartParams`]/[`ThreadStartResponse`],
10//!   [`TurnStartParams`]/[`TurnStartResponse`], etc.
11//! - **Server notifications** — Structs like [`TurnCompletedNotification`],
12//!   [`AgentMessageDeltaNotification`] that can be deserialized from the `params`
13//!   field of a [`ServerMessage::Notification`]
14//! - **Approval flow types** — [`CommandExecutionApprovalParams`] and
15//!   [`FileChangeApprovalParams`] for server-to-client requests that need a response
16//! - **Method constants** — The [`methods`] module contains all JSON-RPC method
17//!   name strings
18//!
19//! # Parsing notifications
20//!
21//! Prefer the typed dispatch in [`crate::messages`] over manual `method` checks:
22//!
23//! ```
24//! use codex_codes::{Notification, ServerMessage};
25//!
26//! fn handle(msg: ServerMessage) {
27//!     if let ServerMessage::Notification(Notification::TurnCompleted(c)) = msg {
28//!         println!("Turn {} on thread {} completed", c.turn.id, c.thread_id);
29//!     }
30//! }
31//! ```
32
33use crate::io::items::ThreadItem;
34use serde::{Deserialize, Serialize};
35use serde_json::Value;
36
37// ---------------------------------------------------------------------------
38// User input
39// ---------------------------------------------------------------------------
40
41/// User input sent as part of a [`TurnStartParams`].
42///
43/// # Example
44///
45/// ```
46/// use codex_codes::UserInput;
47///
48/// let text = UserInput::Text { text: "What is 2+2?".into() };
49/// let json = serde_json::to_string(&text).unwrap();
50/// assert!(json.contains(r#""type":"text""#));
51/// ```
52#[derive(Debug, Clone, Serialize, Deserialize)]
53#[serde(tag = "type", rename_all = "camelCase")]
54pub enum UserInput {
55    /// Text input from the user.
56    Text { text: String },
57    /// Pre-encoded image as a data URI (e.g., `data:image/png;base64,...`).
58    Image { data: String },
59}
60
61// ---------------------------------------------------------------------------
62// Initialization handshake
63// ---------------------------------------------------------------------------
64
65/// Client info sent during the `initialize` handshake.
66///
67/// Identifies the connecting client to the app-server.
68#[derive(Debug, Clone, Serialize, Deserialize)]
69#[serde(rename_all = "camelCase")]
70pub struct ClientInfo {
71    /// Client application name (e.g., `"my-codex-app"`).
72    pub name: String,
73    /// Client version string (e.g., `"1.0.0"`).
74    pub version: String,
75    /// Human-readable display name.
76    #[serde(skip_serializing_if = "Option::is_none")]
77    pub title: Option<String>,
78}
79
80/// Client capabilities negotiated during `initialize`.
81#[derive(Debug, Clone, Default, Serialize, Deserialize)]
82#[serde(rename_all = "camelCase")]
83pub struct InitializeCapabilities {
84    /// Opt into receiving experimental API methods and fields.
85    #[serde(default)]
86    pub experimental_api: bool,
87    /// Notification method names to suppress for this connection.
88    #[serde(skip_serializing_if = "Option::is_none")]
89    pub opt_out_notification_methods: Option<Vec<String>>,
90}
91
92/// Parameters for the `initialize` request.
93///
94/// Must be the first request sent after connecting to the app-server.
95#[derive(Debug, Clone, Serialize, Deserialize)]
96#[serde(rename_all = "camelCase")]
97pub struct InitializeParams {
98    /// Information about the connecting client.
99    pub client_info: ClientInfo,
100    /// Optional client capabilities.
101    #[serde(skip_serializing_if = "Option::is_none")]
102    pub capabilities: Option<InitializeCapabilities>,
103}
104
105/// Response from the `initialize` request.
106#[derive(Debug, Clone, Serialize, Deserialize)]
107#[serde(rename_all = "camelCase")]
108pub struct InitializeResponse {
109    /// The server's user-agent string.
110    pub user_agent: String,
111}
112
113// ---------------------------------------------------------------------------
114// Thread lifecycle requests
115// ---------------------------------------------------------------------------
116
117/// Parameters for `thread/start`.
118///
119/// Use `ThreadStartParams::default()` for a basic thread with no custom instructions.
120#[derive(Debug, Clone, Default, Serialize, Deserialize)]
121#[serde(rename_all = "camelCase")]
122pub struct ThreadStartParams {
123    /// Optional system instructions for the agent.
124    #[serde(skip_serializing_if = "Option::is_none")]
125    pub instructions: Option<String>,
126    /// Optional tool definitions to make available to the agent.
127    #[serde(skip_serializing_if = "Option::is_none")]
128    pub tools: Option<Vec<Value>>,
129}
130
131/// Thread metadata returned inside a [`ThreadStartResponse`].
132#[derive(Debug, Clone, Serialize, Deserialize)]
133#[serde(rename_all = "camelCase")]
134pub struct ThreadInfo {
135    /// Unique thread identifier.
136    pub id: String,
137    /// All other fields are captured but not typed.
138    #[serde(flatten)]
139    pub extra: Value,
140}
141
142/// Response from `thread/start`.
143#[derive(Debug, Clone, Serialize, Deserialize)]
144#[serde(rename_all = "camelCase")]
145pub struct ThreadStartResponse {
146    /// The created thread.
147    pub thread: ThreadInfo,
148    /// The model assigned to this thread.
149    #[serde(default)]
150    pub model: Option<String>,
151    /// All other fields are captured but not typed.
152    #[serde(flatten)]
153    pub extra: Value,
154}
155
156impl ThreadStartResponse {
157    /// Convenience accessor for the thread ID.
158    pub fn thread_id(&self) -> &str {
159        &self.thread.id
160    }
161}
162
163/// Parameters for `thread/archive`.
164#[derive(Debug, Clone, Serialize, Deserialize)]
165#[serde(rename_all = "camelCase")]
166pub struct ThreadArchiveParams {
167    pub thread_id: String,
168}
169
170/// Response from `thread/archive`.
171#[derive(Debug, Clone, Default, Serialize, Deserialize)]
172#[serde(rename_all = "camelCase")]
173pub struct ThreadArchiveResponse {}
174
175// ---------------------------------------------------------------------------
176// Turn lifecycle requests
177// ---------------------------------------------------------------------------
178
179/// Parameters for `turn/start`.
180///
181/// Starts a new agent turn within an existing thread. The agent processes the
182/// input and streams notifications until the turn completes.
183#[derive(Debug, Clone, Serialize, Deserialize)]
184#[serde(rename_all = "camelCase")]
185pub struct TurnStartParams {
186    /// The thread ID from [`ThreadStartResponse`].
187    pub thread_id: String,
188    /// One or more user inputs (text and/or images).
189    pub input: Vec<UserInput>,
190    /// Override the model for this turn (e.g., `"o4-mini"`).
191    #[serde(skip_serializing_if = "Option::is_none")]
192    pub model: Option<String>,
193    /// Override reasoning effort for this turn (e.g., `"low"`, `"medium"`, `"high"`).
194    #[serde(skip_serializing_if = "Option::is_none")]
195    pub reasoning_effort: Option<String>,
196    /// Override sandbox policy for this turn.
197    #[serde(skip_serializing_if = "Option::is_none")]
198    pub sandbox_policy: Option<Value>,
199}
200
201/// Response from `turn/start`.
202#[derive(Debug, Clone, Default, Serialize, Deserialize)]
203#[serde(rename_all = "camelCase")]
204pub struct TurnStartResponse {}
205
206/// Parameters for `turn/interrupt`.
207#[derive(Debug, Clone, Serialize, Deserialize)]
208#[serde(rename_all = "camelCase")]
209pub struct TurnInterruptParams {
210    pub thread_id: String,
211}
212
213/// Response from `turn/interrupt`.
214#[derive(Debug, Clone, Default, Serialize, Deserialize)]
215#[serde(rename_all = "camelCase")]
216pub struct TurnInterruptResponse {}
217
218// ---------------------------------------------------------------------------
219// Turn status & data types
220// ---------------------------------------------------------------------------
221
222/// Status of a turn within a [`Turn`].
223#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
224#[serde(rename_all = "camelCase")]
225pub enum TurnStatus {
226    /// The agent finished normally.
227    Completed,
228    /// The turn was interrupted by the client via `turn/interrupt`.
229    Interrupted,
230    /// The turn failed with an error (see [`Turn::error`]).
231    Failed,
232    /// The turn is still being processed.
233    InProgress,
234}
235
236/// Error information from a failed turn.
237#[derive(Debug, Clone, Serialize, Deserialize)]
238#[serde(rename_all = "camelCase")]
239pub struct TurnError {
240    pub message: String,
241    #[serde(skip_serializing_if = "Option::is_none")]
242    pub codex_error_info: Option<Value>,
243}
244
245/// A completed turn with its items and final status.
246///
247/// Included in [`TurnCompletedNotification`] when a turn finishes.
248#[derive(Debug, Clone, Serialize, Deserialize)]
249#[serde(rename_all = "camelCase")]
250pub struct Turn {
251    /// Unique turn identifier.
252    pub id: String,
253    /// All items produced during this turn (messages, commands, file changes, etc.).
254    #[serde(default)]
255    pub items: Vec<ThreadItem>,
256    /// Final status of the turn.
257    pub status: TurnStatus,
258    /// Error details if `status` is [`TurnStatus::Failed`].
259    #[serde(skip_serializing_if = "Option::is_none")]
260    pub error: Option<TurnError>,
261}
262
263// ---------------------------------------------------------------------------
264// Token usage
265// ---------------------------------------------------------------------------
266
267/// A snapshot of token counts within a single turn or aggregated across a
268/// thread. Sub-field of [`TokenUsage`].
269#[derive(Debug, Clone, Default, Serialize, Deserialize)]
270#[serde(rename_all = "camelCase")]
271pub struct TokenCounts {
272    /// Input tokens consumed.
273    #[serde(default)]
274    pub input_tokens: u64,
275    /// Output tokens generated.
276    #[serde(default)]
277    pub output_tokens: u64,
278    /// Input tokens served from cache.
279    #[serde(default)]
280    pub cached_input_tokens: u64,
281    /// Output tokens spent on chain-of-thought reasoning (model-dependent).
282    #[serde(default)]
283    pub reasoning_output_tokens: u64,
284    /// Sum total — may be redundant with the other counts.
285    #[serde(default)]
286    pub total_tokens: u64,
287}
288
289/// Cumulative token usage for a thread.
290///
291/// Sent via [`ThreadTokenUsageUpdatedNotification`] after each turn. Carries
292/// per-turn (`last`) and lifetime (`total`) counts plus the model's context
293/// window for client-side budget tracking.
294#[derive(Debug, Clone, Serialize, Deserialize)]
295#[serde(rename_all = "camelCase")]
296pub struct TokenUsage {
297    /// Counts for the most recently completed turn.
298    pub last: TokenCounts,
299    /// Cumulative counts for the entire thread.
300    pub total: TokenCounts,
301    /// The model's maximum context window in tokens.
302    pub model_context_window: u64,
303}
304
305// ---------------------------------------------------------------------------
306// Thread status
307// ---------------------------------------------------------------------------
308
309/// Status of a thread, sent via [`ThreadStatusChangedNotification`].
310///
311/// Wire format is internally tagged on `"type"`, with the `Active` variant
312/// carrying an `activeFlags` array of in-progress markers.
313#[derive(Debug, Clone, Serialize, Deserialize)]
314#[serde(tag = "type", rename_all = "camelCase")]
315pub enum ThreadStatus {
316    /// Thread is not yet loaded.
317    NotLoaded,
318    /// Thread is idle (no active turn).
319    Idle,
320    /// Thread has an active turn being processed.
321    Active {
322        /// Tags identifying what is in flight (e.g. running tools).
323        /// Shape is codex-version-dependent; preserved as raw JSON.
324        #[serde(default, skip_serializing_if = "Vec::is_empty")]
325        active_flags: Vec<Value>,
326    },
327    /// Thread encountered an unrecoverable error.
328    SystemError,
329}
330
331// ---------------------------------------------------------------------------
332// Server notifications
333// ---------------------------------------------------------------------------
334
335/// `thread/started` notification.
336///
337/// Sent once when a thread is created. Carries the full [`ThreadInfo`] for
338/// the new thread.
339#[derive(Debug, Clone, Serialize, Deserialize)]
340#[serde(rename_all = "camelCase")]
341pub struct ThreadStartedNotification {
342    pub thread: ThreadInfo,
343}
344
345/// `thread/status/changed` notification.
346#[derive(Debug, Clone, Serialize, Deserialize)]
347#[serde(rename_all = "camelCase")]
348pub struct ThreadStatusChangedNotification {
349    pub thread_id: String,
350    pub status: ThreadStatus,
351}
352
353/// `turn/started` notification.
354///
355/// Carries the freshly-created [`Turn`] (with `status: in_progress`).
356#[derive(Debug, Clone, Serialize, Deserialize)]
357#[serde(rename_all = "camelCase")]
358pub struct TurnStartedNotification {
359    pub thread_id: String,
360    pub turn: Turn,
361}
362
363/// `turn/completed` notification.
364///
365/// Carries the final [`Turn`] state with its full item list.
366#[derive(Debug, Clone, Serialize, Deserialize)]
367#[serde(rename_all = "camelCase")]
368pub struct TurnCompletedNotification {
369    pub thread_id: String,
370    pub turn: Turn,
371}
372
373/// `item/started` notification.
374#[derive(Debug, Clone, Serialize, Deserialize)]
375#[serde(rename_all = "camelCase")]
376pub struct ItemStartedNotification {
377    pub thread_id: String,
378    pub turn_id: String,
379    /// Server-side timestamp (milliseconds since Unix epoch) when the item
380    /// began. Optional — older codex builds omit it.
381    #[serde(default, skip_serializing_if = "Option::is_none")]
382    pub started_at_ms: Option<i64>,
383    pub item: ThreadItem,
384}
385
386/// `item/completed` notification.
387#[derive(Debug, Clone, Serialize, Deserialize)]
388#[serde(rename_all = "camelCase")]
389pub struct ItemCompletedNotification {
390    pub thread_id: String,
391    pub turn_id: String,
392    /// Server-side timestamp (milliseconds since Unix epoch) when the item
393    /// finished. Optional — older codex builds omit it.
394    #[serde(default, skip_serializing_if = "Option::is_none")]
395    pub completed_at_ms: Option<i64>,
396    pub item: ThreadItem,
397}
398
399/// `item/agentMessage/delta` notification.
400#[derive(Debug, Clone, Serialize, Deserialize)]
401#[serde(rename_all = "camelCase")]
402pub struct AgentMessageDeltaNotification {
403    pub thread_id: String,
404    pub item_id: String,
405    pub delta: String,
406}
407
408/// `item/commandExecution/outputDelta` notification.
409#[derive(Debug, Clone, Serialize, Deserialize)]
410#[serde(rename_all = "camelCase")]
411pub struct CmdOutputDeltaNotification {
412    pub thread_id: String,
413    pub item_id: String,
414    pub delta: String,
415}
416
417/// `item/fileChange/outputDelta` notification.
418#[derive(Debug, Clone, Serialize, Deserialize)]
419#[serde(rename_all = "camelCase")]
420pub struct FileChangeOutputDeltaNotification {
421    pub thread_id: String,
422    pub item_id: String,
423    pub delta: String,
424}
425
426/// `item/reasoning/summaryTextDelta` notification.
427#[derive(Debug, Clone, Serialize, Deserialize)]
428#[serde(rename_all = "camelCase")]
429pub struct ReasoningDeltaNotification {
430    pub thread_id: String,
431    pub item_id: String,
432    pub delta: String,
433}
434
435/// `error` notification.
436#[derive(Debug, Clone, Serialize, Deserialize)]
437#[serde(rename_all = "camelCase")]
438pub struct ErrorNotification {
439    pub error: String,
440    #[serde(skip_serializing_if = "Option::is_none")]
441    pub thread_id: Option<String>,
442    #[serde(skip_serializing_if = "Option::is_none")]
443    pub turn_id: Option<String>,
444    #[serde(default)]
445    pub will_retry: bool,
446}
447
448/// `thread/tokenUsage/updated` notification.
449///
450/// Emitted after each turn with cumulative and per-turn token counts.
451#[derive(Debug, Clone, Serialize, Deserialize)]
452#[serde(rename_all = "camelCase")]
453pub struct ThreadTokenUsageUpdatedNotification {
454    pub thread_id: String,
455    /// The turn that triggered this usage update. May be absent for
456    /// thread-level updates that aren't tied to a specific turn.
457    #[serde(default, skip_serializing_if = "Option::is_none")]
458    pub turn_id: Option<String>,
459    pub token_usage: TokenUsage,
460}
461
462/// A rate-limit window descriptor used inside [`RateLimits`].
463#[derive(Debug, Clone, Serialize, Deserialize)]
464#[serde(rename_all = "camelCase")]
465pub struct RateLimitWindow {
466    /// Unix timestamp (seconds) at which this rate-limit window resets.
467    pub resets_at: i64,
468    /// Percentage of the window already consumed (0-100).
469    pub used_percent: i32,
470    /// Length of the rate-limit window, in minutes.
471    pub window_duration_mins: i64,
472}
473
474/// Rate-limit envelope sent in [`AccountRateLimitsUpdatedNotification`].
475#[derive(Debug, Clone, Serialize, Deserialize)]
476#[serde(rename_all = "camelCase")]
477pub struct RateLimits {
478    /// Credit balance, if applicable for this plan. Shape is plan-dependent
479    /// so the payload is preserved as raw JSON.
480    #[serde(default, skip_serializing_if = "Option::is_none")]
481    pub credits: Option<Value>,
482    /// Stable machine identifier for the limit (e.g. `"codex"`).
483    pub limit_id: String,
484    /// Human-readable label, if the server provides one.
485    #[serde(default, skip_serializing_if = "Option::is_none")]
486    pub limit_name: Option<String>,
487    /// Plan tier (e.g. `"free"`, `"plus"`, `"team"`).
488    pub plan_type: String,
489    /// Primary (short-term) rate-limit window, if active.
490    #[serde(default, skip_serializing_if = "Option::is_none")]
491    pub primary: Option<RateLimitWindow>,
492    /// Secondary (longer-term) rate-limit window, if active.
493    #[serde(default, skip_serializing_if = "Option::is_none")]
494    pub secondary: Option<RateLimitWindow>,
495    /// Which window (if any) the account has already hit.
496    #[serde(default, skip_serializing_if = "Option::is_none")]
497    pub rate_limit_reached_type: Option<String>,
498}
499
500/// `account/rateLimits/updated` notification.
501#[derive(Debug, Clone, Serialize, Deserialize)]
502#[serde(rename_all = "camelCase")]
503pub struct AccountRateLimitsUpdatedNotification {
504    pub rate_limits: RateLimits,
505}
506
507/// `mcpServer/startupStatus/updated` notification.
508///
509/// Emitted by the app-server as each managed MCP server transitions through
510/// its startup lifecycle (e.g. `starting` → `ready` or `starting` → `failed`).
511#[derive(Debug, Clone, Serialize, Deserialize)]
512#[serde(rename_all = "camelCase")]
513pub struct McpServerStartupStatusUpdatedNotification {
514    /// MCP server identifier.
515    pub name: String,
516    /// Current lifecycle status string (e.g. `"starting"`, `"ready"`,
517    /// `"failed"`). Kept as `String` so new status values don't break parsing.
518    pub status: String,
519    /// Error message if startup failed.
520    #[serde(default, skip_serializing_if = "Option::is_none")]
521    pub error: Option<String>,
522}
523
524/// `remoteControl/status/changed` notification.
525#[derive(Debug, Clone, Serialize, Deserialize)]
526#[serde(rename_all = "camelCase")]
527pub struct RemoteControlStatusChangedNotification {
528    /// Status string (e.g. `"disabled"`, `"enabled"`).
529    pub status: String,
530    /// Connected environment id, if any.
531    #[serde(default, skip_serializing_if = "Option::is_none")]
532    pub environment_id: Option<String>,
533}
534
535// ---------------------------------------------------------------------------
536// Approval flow types (server-to-client requests)
537// ---------------------------------------------------------------------------
538
539/// Decision for command execution approval.
540///
541/// Sent as part of [`CommandExecutionApprovalResponse`] when responding to
542/// a command approval request from the server.
543#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
544#[serde(rename_all = "camelCase")]
545pub enum CommandApprovalDecision {
546    /// Allow this specific command to execute.
547    Accept,
548    /// Allow this command and similar future commands in this session.
549    AcceptForSession,
550    /// Reject this command.
551    Decline,
552    /// Cancel the entire turn.
553    Cancel,
554}
555
556/// Parameters for `item/commandExecution/requestApproval` (server → client).
557///
558/// The server sends this as a [`ServerMessage::Request`] when the agent wants
559/// to execute a command that requires user approval. Respond with
560/// [`CommandExecutionApprovalResponse`].
561#[derive(Debug, Clone, Serialize, Deserialize)]
562#[serde(rename_all = "camelCase")]
563pub struct CommandExecutionApprovalParams {
564    pub thread_id: String,
565    pub turn_id: String,
566    /// Unique identifier for this tool call.
567    pub call_id: String,
568    /// The shell command the agent wants to run.
569    pub command: String,
570    /// Working directory for the command.
571    pub cwd: String,
572    /// Human-readable explanation of why the command is needed.
573    #[serde(skip_serializing_if = "Option::is_none")]
574    pub reason: Option<String>,
575}
576
577/// Response for `item/commandExecution/requestApproval`.
578#[derive(Debug, Clone, Serialize, Deserialize)]
579#[serde(rename_all = "camelCase")]
580pub struct CommandExecutionApprovalResponse {
581    pub decision: CommandApprovalDecision,
582}
583
584/// Decision for file change approval.
585///
586/// Sent as part of [`FileChangeApprovalResponse`] when responding to
587/// a file change approval request from the server.
588#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
589#[serde(rename_all = "camelCase")]
590pub enum FileChangeApprovalDecision {
591    /// Allow this specific file change.
592    Accept,
593    /// Allow this and similar future file changes in this session.
594    AcceptForSession,
595    /// Reject this file change.
596    Decline,
597    /// Cancel the entire turn.
598    Cancel,
599}
600
601/// Parameters for `item/fileChange/requestApproval` (server → client).
602///
603/// The server sends this as a [`ServerMessage::Request`] when the agent wants
604/// to modify files and requires user approval. Respond with
605/// [`FileChangeApprovalResponse`].
606#[derive(Debug, Clone, Serialize, Deserialize)]
607#[serde(rename_all = "camelCase")]
608pub struct FileChangeApprovalParams {
609    pub thread_id: String,
610    pub turn_id: String,
611    /// Unique identifier for this tool call.
612    pub call_id: String,
613    /// The proposed file changes (structure varies by patch format).
614    pub changes: Value,
615    /// Human-readable explanation of why the changes are needed.
616    #[serde(skip_serializing_if = "Option::is_none")]
617    pub reason: Option<String>,
618}
619
620/// Response for `item/fileChange/requestApproval`.
621#[derive(Debug, Clone, Serialize, Deserialize)]
622#[serde(rename_all = "camelCase")]
623pub struct FileChangeApprovalResponse {
624    pub decision: FileChangeApprovalDecision,
625}
626
627// The [`ServerMessage`] enum that clients return now lives in
628// [`crate::messages`] alongside the typed [`crate::Notification`] and
629// [`crate::ServerRequest`] dispatch enums.
630
631// ---------------------------------------------------------------------------
632// Method name constants
633// ---------------------------------------------------------------------------
634
635/// JSON-RPC method names used by the app-server protocol.
636///
637/// Use these constants when matching on [`ServerMessage::Notification`] or
638/// [`ServerMessage::Request`] method fields to avoid typos.
639pub mod methods {
640    // Client → server requests
641    pub const INITIALIZE: &str = "initialize";
642    pub const INITIALIZED: &str = "initialized";
643    pub const THREAD_START: &str = "thread/start";
644    pub const THREAD_ARCHIVE: &str = "thread/archive";
645    pub const TURN_START: &str = "turn/start";
646    pub const TURN_INTERRUPT: &str = "turn/interrupt";
647    pub const TURN_STEER: &str = "turn/steer";
648
649    // Server → client notifications
650    pub const THREAD_STARTED: &str = "thread/started";
651    pub const THREAD_STATUS_CHANGED: &str = "thread/status/changed";
652    pub const THREAD_TOKEN_USAGE_UPDATED: &str = "thread/tokenUsage/updated";
653    pub const TURN_STARTED: &str = "turn/started";
654    pub const TURN_COMPLETED: &str = "turn/completed";
655    pub const ITEM_STARTED: &str = "item/started";
656    pub const ITEM_COMPLETED: &str = "item/completed";
657    pub const AGENT_MESSAGE_DELTA: &str = "item/agentMessage/delta";
658    pub const CMD_OUTPUT_DELTA: &str = "item/commandExecution/outputDelta";
659    pub const FILE_CHANGE_OUTPUT_DELTA: &str = "item/fileChange/outputDelta";
660    pub const REASONING_DELTA: &str = "item/reasoning/summaryTextDelta";
661    pub const ERROR: &str = "error";
662    pub const ACCOUNT_RATE_LIMITS_UPDATED: &str = "account/rateLimits/updated";
663    pub const MCP_SERVER_STARTUP_STATUS_UPDATED: &str = "mcpServer/startupStatus/updated";
664    pub const REMOTE_CONTROL_STATUS_CHANGED: &str = "remoteControl/status/changed";
665
666    // Server → client requests (approval)
667    pub const CMD_EXEC_APPROVAL: &str = "item/commandExecution/requestApproval";
668    pub const FILE_CHANGE_APPROVAL: &str = "item/fileChange/requestApproval";
669}
670
671#[cfg(test)]
672mod tests {
673    use super::*;
674
675    #[test]
676    fn test_initialize_params() {
677        let params = InitializeParams {
678            client_info: ClientInfo {
679                name: "my-app".to_string(),
680                version: "1.0.0".to_string(),
681                title: Some("My App".to_string()),
682            },
683            capabilities: None,
684        };
685        let json = serde_json::to_string(&params).unwrap();
686        assert!(json.contains("clientInfo"));
687        assert!(json.contains("my-app"));
688        assert!(!json.contains("capabilities"));
689    }
690
691    #[test]
692    fn test_initialize_response() {
693        let json = r#"{"userAgent":"codex-cli/0.104.0"}"#;
694        let resp: InitializeResponse = serde_json::from_str(json).unwrap();
695        assert_eq!(resp.user_agent, "codex-cli/0.104.0");
696    }
697
698    #[test]
699    fn test_initialize_capabilities() {
700        let params = InitializeParams {
701            client_info: ClientInfo {
702                name: "test".to_string(),
703                version: "0.1.0".to_string(),
704                title: None,
705            },
706            capabilities: Some(InitializeCapabilities {
707                experimental_api: true,
708                opt_out_notification_methods: Some(vec!["thread/started".to_string()]),
709            }),
710        };
711        let json = serde_json::to_string(&params).unwrap();
712        assert!(json.contains("experimentalApi"));
713        assert!(json.contains("optOutNotificationMethods"));
714    }
715
716    #[test]
717    fn test_user_input_text() {
718        let input = UserInput::Text {
719            text: "Hello".to_string(),
720        };
721        let json = serde_json::to_string(&input).unwrap();
722        assert!(json.contains(r#""type":"text""#));
723        let parsed: UserInput = serde_json::from_str(&json).unwrap();
724        assert!(matches!(parsed, UserInput::Text { text } if text == "Hello"));
725    }
726
727    #[test]
728    fn test_thread_start_params() {
729        let params = ThreadStartParams {
730            instructions: Some("Be helpful".to_string()),
731            tools: None,
732        };
733        let json = serde_json::to_string(&params).unwrap();
734        assert!(json.contains("instructions"));
735        assert!(!json.contains("tools"));
736    }
737
738    #[test]
739    fn test_thread_start_response() {
740        let json = r#"{"thread":{"id":"th_abc123"},"model":"gpt-4","approvalPolicy":"never","cwd":"/tmp","modelProvider":"openai","sandbox":{}}"#;
741        let resp: ThreadStartResponse = serde_json::from_str(json).unwrap();
742        assert_eq!(resp.thread_id(), "th_abc123");
743        assert_eq!(resp.model.as_deref(), Some("gpt-4"));
744    }
745
746    #[test]
747    fn test_turn_start_params() {
748        let params = TurnStartParams {
749            thread_id: "th_1".to_string(),
750            input: vec![UserInput::Text {
751                text: "What is 2+2?".to_string(),
752            }],
753            model: None,
754            reasoning_effort: None,
755            sandbox_policy: None,
756        };
757        let json = serde_json::to_string(&params).unwrap();
758        assert!(json.contains("threadId"));
759        assert!(json.contains("input"));
760    }
761
762    #[test]
763    fn test_turn_status() {
764        let json = r#""completed""#;
765        let status: TurnStatus = serde_json::from_str(json).unwrap();
766        assert_eq!(status, TurnStatus::Completed);
767    }
768
769    #[test]
770    fn test_turn_completed_notification() {
771        let json = r#"{
772            "threadId": "th_1",
773            "turnId": "t_1",
774            "turn": {
775                "id": "t_1",
776                "items": [],
777                "status": "completed"
778            }
779        }"#;
780        let notif: TurnCompletedNotification = serde_json::from_str(json).unwrap();
781        assert_eq!(notif.thread_id, "th_1");
782        assert_eq!(notif.turn.status, TurnStatus::Completed);
783    }
784
785    #[test]
786    fn test_agent_message_delta() {
787        let json = r#"{"threadId":"th_1","itemId":"msg_1","delta":"Hello "}"#;
788        let notif: AgentMessageDeltaNotification = serde_json::from_str(json).unwrap();
789        assert_eq!(notif.delta, "Hello ");
790    }
791
792    #[test]
793    fn test_command_approval_decision() {
794        let json = r#""accept""#;
795        let decision: CommandApprovalDecision = serde_json::from_str(json).unwrap();
796        assert_eq!(decision, CommandApprovalDecision::Accept);
797
798        let json = r#""acceptForSession""#;
799        let decision: CommandApprovalDecision = serde_json::from_str(json).unwrap();
800        assert_eq!(decision, CommandApprovalDecision::AcceptForSession);
801    }
802
803    #[test]
804    fn test_command_approval_params() {
805        let json = r#"{
806            "threadId": "th_1",
807            "turnId": "t_1",
808            "callId": "call_1",
809            "command": "rm -rf /tmp/test",
810            "cwd": "/home/user"
811        }"#;
812        let params: CommandExecutionApprovalParams = serde_json::from_str(json).unwrap();
813        assert_eq!(params.command, "rm -rf /tmp/test");
814    }
815
816    #[test]
817    fn test_error_notification() {
818        let json = r#"{"error":"something failed","willRetry":true}"#;
819        let notif: ErrorNotification = serde_json::from_str(json).unwrap();
820        assert_eq!(notif.error, "something failed");
821        assert!(notif.will_retry);
822    }
823
824    #[test]
825    fn test_thread_status_idle() {
826        let json = r#"{"type":"idle"}"#;
827        let status: ThreadStatus = serde_json::from_str(json).unwrap();
828        assert!(matches!(status, ThreadStatus::Idle));
829    }
830
831    #[test]
832    fn test_thread_status_active_with_flags() {
833        let json = r#"{"type":"active","activeFlags":[]}"#;
834        let status: ThreadStatus = serde_json::from_str(json).unwrap();
835        match status {
836            ThreadStatus::Active { active_flags } => assert!(active_flags.is_empty()),
837            other => panic!("expected Active, got {:?}", other),
838        }
839    }
840
841    #[test]
842    fn test_token_usage() {
843        let json = r#"{
844            "last":{"inputTokens":100,"outputTokens":200,"cachedInputTokens":50,"reasoningOutputTokens":0,"totalTokens":300},
845            "total":{"inputTokens":1000,"outputTokens":2000,"cachedInputTokens":500,"reasoningOutputTokens":10,"totalTokens":3000},
846            "modelContextWindow":200000
847        }"#;
848        let usage: TokenUsage = serde_json::from_str(json).unwrap();
849        assert_eq!(usage.last.input_tokens, 100);
850        assert_eq!(usage.last.output_tokens, 200);
851        assert_eq!(usage.last.cached_input_tokens, 50);
852        assert_eq!(usage.total.input_tokens, 1000);
853        assert_eq!(usage.model_context_window, 200000);
854    }
855}