|
| 1 | +import * as acp from "@agentclientprotocol/sdk"; |
| 2 | +import type { SessionState } from "./CodexAcpServer"; |
| 3 | +import type { ElicitationHandler } from "./CodexAppServerClient"; |
| 4 | +import type { |
| 5 | + McpServerElicitationRequestParams, |
| 6 | + McpServerElicitationRequestResponse, |
| 7 | +} from "./app-server/v2"; |
| 8 | +import { logger } from "./Logger"; |
| 9 | +import type { PendingMcpApprovals } from "./PendingMcpApprovals"; |
| 10 | + |
| 11 | +// Standard elicitation options (non-tool-call approval). |
| 12 | +const ELICITATION_OPTIONS: acp.PermissionOption[] = [ |
| 13 | + { optionId: "accept", name: "Accept", kind: "allow_once" }, |
| 14 | + { optionId: "decline", name: "Decline", kind: "reject_once" }, |
| 15 | +]; |
| 16 | + |
| 17 | +// Option IDs used for MCP tool call approval persist choices. |
| 18 | +const OPTION_ALLOW_ONCE = "allow_once"; |
| 19 | +const OPTION_ALLOW_SESSION = "allow_session"; |
| 20 | +const OPTION_ALLOW_ALWAYS = "allow_always"; |
| 21 | + |
| 22 | +type PersistValue = "session" | "always"; |
| 23 | + |
| 24 | +/** |
| 25 | + * Parses the `persist` field from the elicitation request `_meta`. |
| 26 | + * Codex advertises which persistence options the client should show. |
| 27 | + * Returns a set of supported persist values. |
| 28 | + */ |
| 29 | +function parsePersistOptions(meta: unknown): Set<PersistValue> { |
| 30 | + const result = new Set<PersistValue>(); |
| 31 | + if (!meta || typeof meta !== "object") return result; |
| 32 | + const persist = (meta as Record<string, unknown>)["persist"]; |
| 33 | + if (persist === "session") { |
| 34 | + result.add("session"); |
| 35 | + } else if (persist === "always") { |
| 36 | + result.add("always"); |
| 37 | + } else if (Array.isArray(persist)) { |
| 38 | + if (persist.includes("session")) result.add("session"); |
| 39 | + if (persist.includes("always")) result.add("always"); |
| 40 | + } |
| 41 | + return result; |
| 42 | +} |
| 43 | + |
| 44 | +function isMcpToolCallApproval(meta: unknown): boolean { |
| 45 | + return ( |
| 46 | + meta !== null && |
| 47 | + typeof meta === "object" && |
| 48 | + (meta as Record<string, unknown>)["codex_approval_kind"] === "mcp_tool_call" |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Builds the ACP permission options for an MCP tool call approval elicitation. |
| 54 | + * Always includes "Allow Once"; adds session/always persist options when advertised. |
| 55 | + */ |
| 56 | +function buildToolApprovalOptions(persistOptions: Set<PersistValue>): acp.PermissionOption[] { |
| 57 | + const options: acp.PermissionOption[] = [ |
| 58 | + { optionId: OPTION_ALLOW_ONCE, name: "Allow", kind: "allow_once" }, |
| 59 | + ]; |
| 60 | + if (persistOptions.has("session")) { |
| 61 | + options.push({ optionId: OPTION_ALLOW_SESSION, name: "Allow for This Session", kind: "allow_always" }); |
| 62 | + } |
| 63 | + if (persistOptions.has("always")) { |
| 64 | + options.push({ optionId: OPTION_ALLOW_ALWAYS, name: "Allow and Don't Ask Again", kind: "allow_always" }); |
| 65 | + } |
| 66 | + options.push({ optionId: "decline", name: "Decline", kind: "reject_once" }); |
| 67 | + return options; |
| 68 | +} |
| 69 | + |
| 70 | +export class CodexElicitationHandler implements ElicitationHandler { |
| 71 | + private readonly connection: acp.AgentSideConnection; |
| 72 | + private readonly sessionState: SessionState; |
| 73 | + private readonly pendingMcpApprovals: PendingMcpApprovals | undefined; |
| 74 | + |
| 75 | + constructor( |
| 76 | + connection: acp.AgentSideConnection, |
| 77 | + sessionState: SessionState, |
| 78 | + pendingMcpApprovals?: PendingMcpApprovals |
| 79 | + ) { |
| 80 | + this.connection = connection; |
| 81 | + this.sessionState = sessionState; |
| 82 | + this.pendingMcpApprovals = pendingMcpApprovals; |
| 83 | + } |
| 84 | + |
| 85 | + async handleElicitation( |
| 86 | + params: McpServerElicitationRequestParams |
| 87 | + ): Promise<McpServerElicitationRequestResponse> { |
| 88 | + try { |
| 89 | + const request = this.buildPermissionRequest(params); |
| 90 | + const response = await this.connection.requestPermission(request); |
| 91 | + return this.convertResponse(response); |
| 92 | + } catch (error) { |
| 93 | + logger.error("Error handling MCP elicitation request", error); |
| 94 | + return { action: "cancel", content: null, _meta: null }; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private buildPermissionRequest( |
| 99 | + params: McpServerElicitationRequestParams |
| 100 | + ): acp.RequestPermissionRequest { |
| 101 | + const sessionId = this.sessionState.sessionId; |
| 102 | + const messageContent: acp.ToolCallContent = { |
| 103 | + type: "content", |
| 104 | + content: { type: "text", text: params.message }, |
| 105 | + }; |
| 106 | + |
| 107 | + const meta = params._meta; |
| 108 | + const isToolApproval = isMcpToolCallApproval(meta); |
| 109 | + const options = isToolApproval |
| 110 | + ? buildToolApprovalOptions(parsePersistOptions(meta)) |
| 111 | + : ELICITATION_OPTIONS; |
| 112 | + |
| 113 | + if (params.mode === "form") { |
| 114 | + const correlatedCallId = isToolApproval |
| 115 | + ? this.pendingMcpApprovals?.pop(params.threadId, params.serverName) |
| 116 | + : undefined; |
| 117 | + if (correlatedCallId !== undefined) { |
| 118 | + // The tool call item is already visible in the IDE conversation history because |
| 119 | + // item/started was emitted before the elicitation request. Sending content or |
| 120 | + // rawInput here would duplicate that information in the approval widget. |
| 121 | + return { |
| 122 | + sessionId, |
| 123 | + toolCall: { |
| 124 | + toolCallId: correlatedCallId, |
| 125 | + kind: "execute", |
| 126 | + status: "pending", |
| 127 | + // content: [messageContent], — omitted: already rendered via item/started |
| 128 | + // rawInput: { ... } — omitted: same reason |
| 129 | + }, |
| 130 | + options, |
| 131 | + }; |
| 132 | + } |
| 133 | + return { |
| 134 | + sessionId, |
| 135 | + toolCall: { |
| 136 | + toolCallId: `elicitation-${params.serverName}`, |
| 137 | + kind: isToolApproval ? "execute" : "other", |
| 138 | + status: "pending", |
| 139 | + content: [messageContent], |
| 140 | + rawInput: { serverName: params.serverName, schema: params.requestedSchema }, |
| 141 | + }, |
| 142 | + options, |
| 143 | + }; |
| 144 | + } else { |
| 145 | + return { |
| 146 | + sessionId, |
| 147 | + toolCall: { |
| 148 | + toolCallId: `elicitation-${params.elicitationId}`, |
| 149 | + kind: "fetch", |
| 150 | + status: "pending", |
| 151 | + content: [messageContent], |
| 152 | + rawInput: { serverName: params.serverName, url: params.url }, |
| 153 | + }, |
| 154 | + options, |
| 155 | + }; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private convertResponse( |
| 160 | + response: acp.RequestPermissionResponse |
| 161 | + ): McpServerElicitationRequestResponse { |
| 162 | + if (response.outcome.outcome === "cancelled") { |
| 163 | + return { action: "cancel", content: null, _meta: null }; |
| 164 | + } |
| 165 | + |
| 166 | + const optionId = response.outcome.optionId; |
| 167 | + if (optionId === OPTION_ALLOW_SESSION) { |
| 168 | + return { action: "accept", content: null, _meta: { persist: "session" } }; |
| 169 | + } |
| 170 | + if (optionId === OPTION_ALLOW_ALWAYS) { |
| 171 | + return { action: "accept", content: null, _meta: { persist: "always" } }; |
| 172 | + } |
| 173 | + if (optionId === OPTION_ALLOW_ONCE || optionId === "accept") { |
| 174 | + return { action: "accept", content: null, _meta: null }; |
| 175 | + } |
| 176 | + return { action: "decline", content: null, _meta: null }; |
| 177 | + } |
| 178 | +} |
0 commit comments