@@ -23,11 +23,13 @@ import type {
2323 TerminalHandle ,
2424 TerminalOutputRequest ,
2525 TerminalOutputResponse ,
26+ ToolKind ,
2627 WaitForTerminalExitRequest ,
2728 WaitForTerminalExitResponse ,
2829 WriteTextFileRequest ,
2930 WriteTextFileResponse ,
3031} from "@agentclientprotocol/sdk" ;
32+ import type { PermissionMode } from "../../execution-mode" ;
3133import type { Logger } from "../../utils/logger" ;
3234import type { CodexSessionState } from "./session-state" ;
3335
@@ -36,6 +38,32 @@ export interface CodexClientCallbacks {
3638 onUsageUpdate ?: ( update : Record < string , unknown > ) => void ;
3739}
3840
41+ const AUTO_APPROVED_KINDS : Partial < Record < PermissionMode , Set < ToolKind > > > = {
42+ auto : new Set ( [ "read" , "search" , "fetch" , "think" ] ) ,
43+ "read-only" : new Set ( [ "read" , "search" , "fetch" , "think" ] ) ,
44+ "full-access" : new Set ( [
45+ "read" ,
46+ "edit" ,
47+ "delete" ,
48+ "move" ,
49+ "search" ,
50+ "execute" ,
51+ "think" ,
52+ "fetch" ,
53+ "switch_mode" ,
54+ "other" ,
55+ ] ) ,
56+ } ;
57+
58+ function shouldAutoApprove (
59+ mode : PermissionMode ,
60+ kind : ToolKind | null | undefined ,
61+ ) : boolean {
62+ if ( mode === "full-access" ) return true ;
63+ if ( ! kind ) return false ;
64+ return AUTO_APPROVED_KINDS [ mode ] ?. has ( kind ) ?? false ;
65+ }
66+
3967/**
4068 * Creates an ACP Client that delegates all requests from codex-acp
4169 * to the upstream PostHog Code client (via AgentSideConnection).
@@ -46,16 +74,31 @@ export function createCodexClient(
4674 sessionState : CodexSessionState ,
4775 callbacks ?: CodexClientCallbacks ,
4876) : Client {
49- // Track terminal handles for delegation
5077 const terminalHandles = new Map < string , TerminalHandle > ( ) ;
5178
5279 return {
5380 async requestPermission (
5481 params : RequestPermissionRequest ,
5582 ) : Promise < RequestPermissionResponse > {
56- logger . debug ( "Relaying permission request to upstream" , {
57- sessionId : params . sessionId ,
58- } ) ;
83+ const kind = params . toolCall ?. kind as ToolKind | null | undefined ;
84+
85+ if ( shouldAutoApprove ( sessionState . permissionMode , kind ) ) {
86+ logger . debug ( "Auto-approving permission" , {
87+ mode : sessionState . permissionMode ,
88+ kind,
89+ toolCallId : params . toolCall ?. toolCallId ,
90+ } ) ;
91+ const allowOption = params . options ?. find (
92+ ( o ) => o . kind === "allow_once" || o . kind === "allow_always" ,
93+ ) ;
94+ return {
95+ outcome : {
96+ outcome : "selected" ,
97+ optionId : allowOption ?. optionId ?? "allow" ,
98+ } ,
99+ } ;
100+ }
101+
59102 return upstreamClient . requestPermission ( params ) ;
60103 } ,
61104
0 commit comments