(setToolUseConfirmQueue, setToolPermissionContext)
| 26 | import { logPermissionDecision } from './toolPermission/permissionLogging.js'; |
| 27 | export type CanUseToolFn<Input extends Record<string, unknown> = Record<string, unknown>> = (tool: ToolType, input: Input, toolUseContext: ToolUseContext, assistantMessage: AssistantMessage, toolUseID: string, forceDecision?: PermissionDecision<Input>) => Promise<PermissionDecision<Input>>; |
| 28 | function useCanUseTool(setToolUseConfirmQueue, setToolPermissionContext) { |
| 29 | const $ = _c(3); |
| 30 | let t0; |
| 31 | if ($[0] !== setToolPermissionContext || $[1] !== setToolUseConfirmQueue) { |
| 32 | t0 = async (tool, input, toolUseContext, assistantMessage, toolUseID, forceDecision) => new Promise(resolve => { |
| 33 | const ctx = createPermissionContext(tool, input, toolUseContext, assistantMessage, toolUseID, setToolPermissionContext, createPermissionQueueOps(setToolUseConfirmQueue)); |
| 34 | if (ctx.resolveIfAborted(resolve)) { |
| 35 | return; |
| 36 | } |
| 37 | const decisionPromise = forceDecision !== undefined ? Promise.resolve(forceDecision) : hasPermissionsToUseTool(tool, input, toolUseContext, assistantMessage, toolUseID); |
| 38 | return decisionPromise.then(async result => { |
| 39 | if (result.behavior === "allow") { |
| 40 | if (ctx.resolveIfAborted(resolve)) { |
| 41 | return; |
| 42 | } |
| 43 | if (feature("TRANSCRIPT_CLASSIFIER") && result.decisionReason?.type === "classifier" && result.decisionReason.classifier === "auto-mode") { |
| 44 | setYoloClassifierApproval(toolUseID, result.decisionReason.reason); |
| 45 | } |
| 46 | ctx.logDecision({ |
| 47 | decision: "accept", |
| 48 | source: "config" |
| 49 | }); |
| 50 | resolve(ctx.buildAllow(result.updatedInput ?? input, { |
| 51 | decisionReason: result.decisionReason |
| 52 | })); |
| 53 | return; |
| 54 | } |
| 55 | const appState = toolUseContext.getAppState(); |
| 56 | const description = await tool.description(input as never, { |
| 57 | isNonInteractiveSession: toolUseContext.options.isNonInteractiveSession, |
| 58 | toolPermissionContext: appState.toolPermissionContext, |
| 59 | tools: toolUseContext.options.tools |
| 60 | }); |
| 61 | if (ctx.resolveIfAborted(resolve)) { |
| 62 | return; |
| 63 | } |
| 64 | switch (result.behavior) { |
| 65 | case "deny": |
| 66 | { |
| 67 | logPermissionDecision({ |
| 68 | tool, |
| 69 | input, |
| 70 | toolUseContext, |
| 71 | messageId: ctx.messageId, |
| 72 | toolUseID |
| 73 | }, { |
| 74 | decision: "reject", |
| 75 | source: "config" |
| 76 | }); |
| 77 | if (feature("TRANSCRIPT_CLASSIFIER") && result.decisionReason?.type === "classifier" && result.decisionReason.classifier === "auto-mode") { |
| 78 | recordAutoModeDenial({ |
| 79 | toolName: tool.name, |
| 80 | display: description, |
| 81 | reason: result.decisionReason.reason ?? "", |
| 82 | timestamp: Date.now() |
| 83 | }); |
| 84 | toolUseContext.addNotification?.({ |
| 85 | key: "auto-mode-denied", |
no test coverage detected