1- use std:: collections:: HashMap ;
21use std:: path:: Path ;
3- use std:: path:: PathBuf ;
42
53use crate :: JSONRPCNotification ;
64use crate :: JSONRPCRequest ;
@@ -9,12 +7,6 @@ use crate::export::GeneratedSchema;
97use crate :: export:: write_json_schema;
108use crate :: protocol:: v1;
119use crate :: protocol:: v2;
12- use codex_protocol:: ConversationId ;
13- use codex_protocol:: parse_command:: ParsedCommand ;
14- use codex_protocol:: protocol:: FileChange ;
15- use codex_protocol:: protocol:: ReviewDecision ;
16- use codex_protocol:: protocol:: SandboxCommandAssessment ;
17- use paste:: paste;
1810use schemars:: JsonSchema ;
1911use serde:: Deserialize ;
2012use serde:: Serialize ;
@@ -277,44 +269,46 @@ macro_rules! server_request_definitions {
277269 (
278270 $(
279271 $( #[ $variant_meta: meta] ) *
280- $variant: ident
272+ $variant: ident $( => $wire: literal) ? {
273+ params: $params: ty,
274+ response: $response: ty,
275+ }
281276 ) ,* $( , ) ?
282277 ) => {
283- paste! {
284- /// Request initiated from the server and sent to the client.
285- # [ derive ( Serialize , Deserialize , Debug , Clone , PartialEq , JsonSchema , TS ) ]
286- # [ serde ( tag = "method" , rename_all = "camelCase" ) ]
287- pub enum ServerRequest {
288- $(
289- $ ( # [ $variant_meta ] ) *
290- $variant {
291- #[ serde( rename = "id" ) ]
292- request_id: RequestId ,
293- params: [ <$variant Params > ] ,
294- } ,
295- ) *
296- }
278+ /// Request initiated from the server and sent to the client.
279+ # [ derive ( Serialize , Deserialize , Debug , Clone , PartialEq , JsonSchema , TS ) ]
280+ # [ serde ( tag = "method" , rename_all = "camelCase" ) ]
281+ pub enum ServerRequest {
282+ $ (
283+ $( # [ $variant_meta ] ) *
284+ $ ( # [ serde ( rename = $wire ) ] # [ ts ( rename = $wire ) ] ) ?
285+ $variant {
286+ #[ serde( rename = "id" ) ]
287+ request_id: RequestId ,
288+ params: $params ,
289+ } ,
290+ ) *
291+ }
297292
298- #[ derive( Debug , Clone , PartialEq , JsonSchema ) ]
299- pub enum ServerRequestPayload {
300- $( $variant( [ <$variant Params > ] ) , ) *
301- }
293+ #[ derive( Debug , Clone , PartialEq , JsonSchema ) ]
294+ pub enum ServerRequestPayload {
295+ $( $variant( $params ) , ) *
296+ }
302297
303- impl ServerRequestPayload {
304- pub fn request_with_id( self , request_id: RequestId ) -> ServerRequest {
305- match self {
306- $( Self :: $variant( params) => ServerRequest :: $variant { request_id, params } , ) *
307- }
298+ impl ServerRequestPayload {
299+ pub fn request_with_id( self , request_id: RequestId ) -> ServerRequest {
300+ match self {
301+ $( Self :: $variant( params) => ServerRequest :: $variant { request_id, params } , ) *
308302 }
309303 }
310304 }
311305
312306 pub fn export_server_responses(
313307 out_dir: & :: std:: path:: Path ,
314308 ) -> :: std:: result:: Result <( ) , :: ts_rs:: ExportError > {
315- paste! {
316- $ ( < [ <$variant Response > ] as :: ts_rs:: TS >:: export_all_to( out_dir) ?; ) *
317- }
309+ $ (
310+ <$response as :: ts_rs:: TS >:: export_all_to( out_dir) ?;
311+ ) *
318312 Ok ( ( ) )
319313 }
320314
@@ -323,9 +317,12 @@ macro_rules! server_request_definitions {
323317 out_dir: & Path ,
324318 ) -> :: anyhow:: Result <Vec <GeneratedSchema >> {
325319 let mut schemas = Vec :: new( ) ;
326- paste! {
327- $( schemas. push( crate :: export:: write_json_schema:: <[ <$variant Response >] >( out_dir, stringify!( [ <$variant Response >] ) ) ?) ; ) *
328- }
320+ $(
321+ schemas. push( crate :: export:: write_json_schema:: <$response>(
322+ out_dir,
323+ concat!( stringify!( $variant) , "Response" ) ,
324+ ) ?) ;
325+ ) *
329326 Ok ( schemas)
330327 }
331328
@@ -334,9 +331,12 @@ macro_rules! server_request_definitions {
334331 out_dir: & Path ,
335332 ) -> :: anyhow:: Result <Vec <GeneratedSchema >> {
336333 let mut schemas = Vec :: new( ) ;
337- paste! {
338- $( schemas. push( crate :: export:: write_json_schema:: <[ <$variant Params >] >( out_dir, stringify!( [ <$variant Params >] ) ) ?) ; ) *
339- }
334+ $(
335+ schemas. push( crate :: export:: write_json_schema:: <$params>(
336+ out_dir,
337+ concat!( stringify!( $variant) , "Params" ) ,
338+ ) ?) ;
339+ ) *
340340 Ok ( schemas)
341341 }
342342 } ;
@@ -426,49 +426,24 @@ impl TryFrom<JSONRPCRequest> for ServerRequest {
426426}
427427
428428server_request_definitions ! {
429+ /// NEW APIs
430+ /// Sent when approval is requested for a specific item (e.g. file edit, command execution).
431+ ItemRequestApproval => "item/requestApproval" {
432+ params: v2:: ItemRequestApprovalParams ,
433+ response: v2:: ItemRequestApprovalResponse ,
434+ } ,
435+
436+ /// DEPRECATED APIs below
429437 /// Request to approve a patch.
430- ApplyPatchApproval ,
438+ ApplyPatchApproval {
439+ params: v1:: ApplyPatchApprovalParams ,
440+ response: v1:: ApplyPatchApprovalResponse ,
441+ } ,
431442 /// Request to exec a command.
432- ExecCommandApproval ,
433- }
434-
435- #[ derive( Serialize , Deserialize , Debug , Clone , PartialEq , JsonSchema , TS ) ]
436- #[ serde( rename_all = "camelCase" ) ]
437- pub struct ApplyPatchApprovalParams {
438- pub conversation_id : ConversationId ,
439- /// Use to correlate this with [codex_core::protocol::PatchApplyBeginEvent]
440- /// and [codex_core::protocol::PatchApplyEndEvent].
441- pub call_id : String ,
442- pub file_changes : HashMap < PathBuf , FileChange > ,
443- /// Optional explanatory reason (e.g. request for extra write access).
444- pub reason : Option < String > ,
445- /// When set, the agent is asking the user to allow writes under this root
446- /// for the remainder of the session (unclear if this is honored today).
447- pub grant_root : Option < PathBuf > ,
448- }
449-
450- #[ derive( Serialize , Deserialize , Debug , Clone , PartialEq , JsonSchema , TS ) ]
451- #[ serde( rename_all = "camelCase" ) ]
452- pub struct ExecCommandApprovalParams {
453- pub conversation_id : ConversationId ,
454- /// Use to correlate this with [codex_core::protocol::ExecCommandBeginEvent]
455- /// and [codex_core::protocol::ExecCommandEndEvent].
456- pub call_id : String ,
457- pub command : Vec < String > ,
458- pub cwd : PathBuf ,
459- pub reason : Option < String > ,
460- pub risk : Option < SandboxCommandAssessment > ,
461- pub parsed_cmd : Vec < ParsedCommand > ,
462- }
463-
464- #[ derive( Serialize , Deserialize , Debug , Clone , PartialEq , JsonSchema , TS ) ]
465- pub struct ExecCommandApprovalResponse {
466- pub decision : ReviewDecision ,
467- }
468-
469- #[ derive( Serialize , Deserialize , Debug , Clone , PartialEq , JsonSchema , TS ) ]
470- pub struct ApplyPatchApprovalResponse {
471- pub decision : ReviewDecision ,
443+ ExecCommandApproval {
444+ params: v1:: ExecCommandApprovalParams ,
445+ response: v1:: ExecCommandApprovalResponse ,
446+ } ,
472447}
473448
474449#[ derive( Serialize , Deserialize , Debug , Clone , PartialEq , JsonSchema , TS ) ]
@@ -530,10 +505,13 @@ client_notification_definitions! {
530505mod tests {
531506 use super :: * ;
532507 use anyhow:: Result ;
508+ use codex_protocol:: ConversationId ;
533509 use codex_protocol:: account:: PlanType ;
510+ use codex_protocol:: parse_command:: ParsedCommand ;
534511 use codex_protocol:: protocol:: AskForApproval ;
535512 use pretty_assertions:: assert_eq;
536513 use serde_json:: json;
514+ use std:: path:: PathBuf ;
537515
538516 #[ test]
539517 fn serialize_new_conversation ( ) -> Result < ( ) > {
@@ -613,7 +591,7 @@ mod tests {
613591 #[ test]
614592 fn serialize_server_request ( ) -> Result < ( ) > {
615593 let conversation_id = ConversationId :: from_string ( "67e55044-10b1-426f-9247-bb680e5fe0c8" ) ?;
616- let params = ExecCommandApprovalParams {
594+ let params = v1 :: ExecCommandApprovalParams {
617595 conversation_id,
618596 call_id : "call-42" . to_string ( ) ,
619597 command : vec ! [ "echo" . to_string( ) , "hello" . to_string( ) ] ,
0 commit comments