diff --git a/packages/server/src/server/agent/providers/omp/rpc-ui-permission-mapper.test.ts b/packages/server/src/server/agent/providers/omp/rpc-ui-permission-mapper.test.ts index 913596c69..9772158ce 100644 --- a/packages/server/src/server/agent/providers/omp/rpc-ui-permission-mapper.test.ts +++ b/packages/server/src/server/agent/providers/omp/rpc-ui-permission-mapper.test.ts @@ -136,6 +136,94 @@ describe("OMP rpc-ui permission mapper", () => { ).toEqual({ kind: "passthrough" }); }); + test("maps approvals that offer session and grant choices beside the one-call approval", () => { + const session = classifyOmpRpcUiPermissionRequest({ + type: "extension_ui_request", + id: "session-write", + method: "select", + title: "Allow tool: write\nPath: cap.txt\nContent:\nhello", + options: ["Approve", "Approve write for this session", "Deny"], + }); + if (session.kind !== "tool") throw new Error("Expected session approval to map as a tool"); + expect( + session.request.actions?.map((action) => [action.id, action.label, action.behavior]), + ).toEqual([ + ["deny", "Deny", "deny"], + ["approve", "Approve", "allow"], + ["approve-option-0", "Approve write for this session", "allow"], + ]); + expect(buildOmpRpcUiPermissionResponse(session.request, { behavior: "allow" })).toEqual({ + value: "Approve", + }); + expect( + buildOmpRpcUiPermissionResponse(session.request, { + behavior: "allow", + selectedActionId: "approve-option-0", + }), + ).toEqual({ value: "Approve write for this session" }); + expect( + buildOmpRpcUiPermissionResponse(session.request, { + behavior: "deny", + selectedActionId: "approve-option-0", + }), + ).toEqual({ value: "Deny" }); + + const grants = classifyOmpRpcUiPermissionRequest({ + type: "extension_ui_request", + id: "grant-edit", + method: "select", + title: "Allow tool: edit\nFile: src/a.ts", + options: ["Approve File", "Always Approve File", "Approve Once", "Deny"], + }); + if (grants.kind !== "tool") throw new Error("Expected grant approval to map as a tool"); + expect(buildOmpRpcUiPermissionResponse(grants.request, { behavior: "allow" })).toEqual({ + value: "Approve Once", + }); + expect( + buildOmpRpcUiPermissionResponse(grants.request, { + behavior: "allow", + selectedActionId: "approve-option-1", + }), + ).toEqual({ value: "Always Approve File" }); + }); + + test("allows one call when a grant is labeled Approve beside Approve Once", () => { + // OMP names the one-call choice "Approve Once" whenever it offers grants. A grant for a + // target that is not a file is then labeled "Approve", which remembers more than one call. + const grants = classifyOmpRpcUiPermissionRequest({ + type: "extension_ui_request", + id: "grant-target", + method: "select", + title: "Allow tool: edit\nFile: src/a.ts", + options: ["Approve", "Always Approve", "Approve Once", "Deny"], + }); + if (grants.kind !== "tool") throw new Error("Expected grant approval to map as a tool"); + expect(buildOmpRpcUiPermissionResponse(grants.request, { behavior: "allow" })).toEqual({ + value: "Approve Once", + }); + }); + + test("passes through selects whose extra choices are not approvals", () => { + expect( + classifyOmpRpcUiPermissionRequest({ + type: "extension_ui_request", + id: "odd-choice", + method: "select", + title: "Allow tool: bash\nCommand: echo hi", + options: ["Approve", "Edit command", "Deny"], + }), + ).toEqual({ kind: "passthrough" }); + expect( + classifyOmpRpcUiPermissionRequest({ + type: "extension_ui_request", + id: "deny-first", + method: "select", + title: "Allow tool: bash\nCommand: echo hi", + options: ["Deny", "Approve"], + }), + ).toEqual({ kind: "passthrough" }); + }); + test("responds to tool approvals with exact select values", () => { const request = toolApproval("bash"); diff --git a/packages/server/src/server/agent/providers/omp/rpc-ui-permission-mapper.ts b/packages/server/src/server/agent/providers/omp/rpc-ui-permission-mapper.ts index 2e1b60fd7..91236f7d1 100644 --- a/packages/server/src/server/agent/providers/omp/rpc-ui-permission-mapper.ts +++ b/packages/server/src/server/agent/providers/omp/rpc-ui-permission-mapper.ts @@ -10,8 +10,13 @@ import type { OmpRuntimeEvent } from "./rpc-types.js"; const OMP_PROVIDER = "omp"; const OMP_RPC_UI_TOOL_APPROVAL_METADATA = "omp_rpc_ui_tool_approval"; const TOOL_APPROVAL_APPROVE_VALUE = "Approve"; +// OMP labels the approve-this-call choice "Approve Once" when it also offers grants that remember +// more. A grant can then be labeled "Approve", so "Approve" is the one-call choice only without it. +const TOOL_APPROVAL_ONCE_VALUE = "Approve Once"; const TOOL_APPROVAL_DENY_VALUE = "Deny"; -const TOOL_APPROVAL_OPTIONS = [TOOL_APPROVAL_APPROVE_VALUE, TOOL_APPROVAL_DENY_VALUE] as const; +// Session and grant choices OMP offers beside the one-call approval, e.g. "Approve write for this session". +const TOOL_APPROVAL_EXTRA_CHOICE = /^(?:Always )?Approve\b/; +const TOOL_APPROVAL_EXTRA_ACTION_PREFIX = "approve-option-"; const TOOL_TITLE_PREFIX = "Allow tool: "; export type OmpRpcUiPermissionClassification = @@ -25,6 +30,12 @@ interface ToolApprovalDescriptor { args: AgentMetadata; detail: ToolCallDetail; description?: string; + choices: ToolApprovalChoices; +} + +interface ToolApprovalChoices { + approveValue: string; + extraValues: string[]; } export function classifyOmpRpcUiPermissionRequest( @@ -37,6 +48,13 @@ export function classifyOmpRpcUiPermissionRequest( } const provider = options.provider ?? OMP_PROVIDER; + const { approveValue, extraValues } = descriptor.choices; + const extraActions = extraValues.map((value, index) => ({ + id: `${TOOL_APPROVAL_EXTRA_ACTION_PREFIX}${index}`, + label: value, + behavior: "allow" as const, + variant: "secondary" as const, + })); return { kind: "tool", request: { @@ -57,17 +75,18 @@ export function classifyOmpRpcUiPermissionRequest( }, { id: "approve", - label: TOOL_APPROVAL_APPROVE_VALUE, + label: approveValue, behavior: "allow", variant: "primary", }, + ...extraActions, ], metadata: { extensionUiMethod: "select", toolApproval: OMP_RPC_UI_TOOL_APPROVAL_METADATA, toolName: descriptor.toolName, toolArgs: descriptor.args, - approveValue: TOOL_APPROVAL_APPROVE_VALUE, + approveValue, denyValue: TOOL_APPROVAL_DENY_VALUE, }, }, @@ -95,13 +114,23 @@ export function buildOmpRpcUiPermissionResponse( const approveValue = readString(request.metadata.approveValue) ?? TOOL_APPROVAL_APPROVE_VALUE; const denyValue = readString(request.metadata.denyValue) ?? TOOL_APPROVAL_DENY_VALUE; - return { value: response.behavior === "allow" ? approveValue : denyValue }; + if (response.behavior !== "allow") { + return { value: denyValue }; + } + // Each extra choice's action label is the exact select value OMP offered. + const selectedExtraChoice = request.actions?.find( + (action) => + action.id === response.selectedActionId && + action.id.startsWith(TOOL_APPROVAL_EXTRA_ACTION_PREFIX), + ); + return { value: selectedExtraChoice?.label ?? approveValue }; } function parseToolApprovalDescriptor( event: ExtensionUiRequestEvent, ): ToolApprovalDescriptor | null { - if (event.method !== "select" || !hasExactToolApprovalOptions(event.options)) { + const choices = event.method === "select" ? readToolApprovalChoices(event.options) : null; + if (!choices) { return null; } const title = readString(event.title); @@ -118,6 +147,15 @@ function parseToolApprovalDescriptor( const body = firstLineBreak < 0 ? "" : title.slice(firstLineBreak).replace(/^\r?\n/, ""); const bodyLines = body.split(/\r?\n/); + const parsed = parseToolApprovalBody(rawToolName, body, bodyLines); + return parsed ? { ...parsed, choices } : null; +} + +function parseToolApprovalBody( + rawToolName: string, + body: string, + bodyLines: string[], +): Omit | null { switch (rawToolName) { case "bash": return parseBashApproval(body); @@ -130,7 +168,7 @@ function parseToolApprovalDescriptor( } } -function parseBashApproval(body: string): ToolApprovalDescriptor | null { +function parseBashApproval(body: string): Omit | null { const match = /(?:^|\r?\n)[\t ]*Command:[\t ]?(.*)$/s.exec(body); const command = match?.[1]; if (!command) { @@ -144,7 +182,7 @@ function parseBashApproval(body: string): ToolApprovalDescriptor | null { }; } -function parseEditApproval(lines: string[]): ToolApprovalDescriptor | null { +function parseEditApproval(lines: string[]): Omit | null { const filePath = readPrefixedValue(lines, "File:"); if (!filePath) { return null; @@ -157,7 +195,7 @@ function parseEditApproval(lines: string[]): ToolApprovalDescriptor | null { }; } -function parseWriteApproval(lines: string[]): ToolApprovalDescriptor | null { +function parseWriteApproval(lines: string[]): Omit | null { const pathLineIndex = lines.findIndex((line) => line.trim().startsWith("Path:")); if (pathLineIndex < 0) { return null; @@ -182,12 +220,28 @@ function parseWriteApproval(lines: string[]): ToolApprovalDescriptor | null { }; } -function hasExactToolApprovalOptions(options: unknown): boolean { - return ( - Array.isArray(options) && - options.length === TOOL_APPROVAL_OPTIONS.length && - options.every((option, index) => option === TOOL_APPROVAL_OPTIONS[index]) +function readToolApprovalChoices(options: unknown): ToolApprovalChoices | null { + if (!Array.isArray(options) || !options.every((option) => typeof option === "string")) { + return null; + } + const labels = options as string[]; + if ( + labels.at(-1) !== TOOL_APPROVAL_DENY_VALUE || + labels.indexOf(TOOL_APPROVAL_DENY_VALUE) !== labels.length - 1 + ) { + return null; + } + const approveValue = [TOOL_APPROVAL_ONCE_VALUE, TOOL_APPROVAL_APPROVE_VALUE].find((value) => + labels.includes(value), ); + if (!approveValue) { + return null; + } + const extraValues = labels.slice(0, -1).filter((label) => label !== approveValue); + if (!extraValues.every((label) => TOOL_APPROVAL_EXTRA_CHOICE.test(label))) { + return null; + } + return { approveValue, extraValues }; } function readPrefixedValue(lines: readonly string[], prefix: string): string | null {