| 1 | import { Box, Text, useInput } from "ink"; |
| 2 | import React, { useState } from "react"; |
| 3 | |
| 4 | import { ToolCallTitle } from "src/tools/ToolCallTitle.js"; |
| 5 | |
| 6 | import { ToolCallPreview } from "../../tools/types.js"; |
| 7 | |
| 8 | import { ToolPreview } from "./ToolPreview.js"; |
| 9 | |
| 10 | interface PermissionOption { |
| 11 | id: string; |
| 12 | name: string; |
| 13 | color: string; |
| 14 | approved: boolean; |
| 15 | createPolicy?: boolean; |
| 16 | stopStream?: boolean; |
| 17 | } |
| 18 | |
| 19 | interface ToolPermissionSelectorProps { |
| 20 | toolName: string; |
| 21 | toolArgs: any; |
| 22 | requestId: string; |
| 23 | toolCallPreview?: ToolCallPreview[]; |
| 24 | hasDynamicEvaluation?: boolean; |
| 25 | onResponse: ( |
| 26 | requestId: string, |
| 27 | approved: boolean, |
| 28 | createPolicy?: boolean, |
| 29 | stopStream?: boolean, |
| 30 | ) => void; |
| 31 | } |
| 32 | |
| 33 | const getPermissionOptions = (): PermissionOption[] => { |
| 34 | return [ |
| 35 | { id: "approve", name: "Continue", color: "green", approved: true }, |
| 36 | { |
| 37 | id: "approve_policy", |
| 38 | name: "Continue + don't ask again", |
| 39 | color: "cyan", |
| 40 | approved: true, |
| 41 | createPolicy: true, |
| 42 | }, |
| 43 | { |
| 44 | id: "deny_stop", |
| 45 | name: "No, and tell Continue what to do differently", |
| 46 | color: "yellow", |
| 47 | approved: false, |
| 48 | stopStream: true, |
| 49 | }, |
| 50 | ]; |
| 51 | }; |
| 52 | |
| 53 | export const ToolPermissionSelector: React.FC<ToolPermissionSelectorProps> = ({ |
| 54 | toolName, |
| 55 | toolArgs, |
| 56 | requestId, |
| 57 | toolCallPreview, |
| 58 | hasDynamicEvaluation = false, |
| 59 | onResponse, |
| 60 | }) => { |
| 61 | const permissionOptions = getPermissionOptions(); |
| 62 | const [selectedIndex, setSelectedIndex] = useState(0); |
| 63 | |
| 64 | useInput((input, key) => { |
| 65 | if (key.return) { |
| 66 | const selectedOption = permissionOptions[selectedIndex]; |
| 67 | onResponse( |
| 68 | requestId, |
| 69 | selectedOption.approved, |
| 70 | selectedOption.createPolicy, |
| 71 | selectedOption.stopStream, |
| 72 | ); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // Tab to continue (approve) |
| 77 | if (key.tab && !key.shift) { |
| 78 | onResponse(requestId, true, false, false); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | // Shift+Tab to continue with policy creation |
| 83 | if (key.tab && key.shift) { |
| 84 | onResponse(requestId, true, true, false); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | // Escape to reject with stop stream |
| 89 | if (key.escape) { |
| 90 | onResponse(requestId, false, false, true); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | if (key.upArrow) { |
| 95 | setSelectedIndex(Math.max(0, selectedIndex - 1)); |
| 96 | } else if (key.downArrow) { |
| 97 | setSelectedIndex( |
| 98 | Math.min(permissionOptions.length - 1, selectedIndex + 1), |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | // Also support y/n for quick responses |
| 103 | if (input === "y" || input === "Y") { |
| 104 | onResponse(requestId, true, false, false); |
| 105 | } else if (input === "n" || input === "N") { |
| 106 | onResponse(requestId, false, false, true); |
| 107 | } |
| 108 | }); |
| 109 | |
| 110 | return ( |
| 111 | <Box |
| 112 | flexDirection="column" |
| 113 | padding={1} |
| 114 | borderStyle="round" |
| 115 | borderColor="magenta" |
| 116 | > |
| 117 | <Text color="magenta" bold> |
| 118 | <ToolCallTitle toolName={toolName} args={toolArgs} /> |
| 119 | </Text> |
| 120 | |
| 121 | {/* Show preview of what the tool will do */} |
| 122 | <Box> |
| 123 | <ToolPreview |
| 124 | toolName={toolName} |
| 125 | toolArgs={toolArgs} |
| 126 | toolCallPreview={toolCallPreview} |
| 127 | /> |
| 128 | </Box> |
| 129 | |
| 130 | <Box marginTop={1} flexDirection="column"> |
| 131 | <Text color="dim">Would you like to continue?</Text> |
| 132 | {hasDynamicEvaluation && ( |
| 133 | <Box marginTop={1}> |
| 134 | <Text color="yellow" dimColor> |
| 135 | Note: Dangerous commands will be blocked regardless of your |
| 136 | preference. |
| 137 | </Text> |
| 138 | </Box> |
| 139 | )} |
| 140 | {permissionOptions.map((option, index) => { |
| 141 | const isSelected = index === selectedIndex; |
| 142 | let shortcut = ""; |
| 143 | if (option.id === "approve") shortcut = "(tab)"; |
| 144 | else if (option.id === "approve_policy") shortcut = "(shift+tab)"; |
| 145 | else if (option.id === "deny_stop") shortcut = "(esc)"; |
| 146 | |
| 147 | return ( |
| 148 | <Box key={option.id} marginTop={index === 0 ? 1 : 0}> |
| 149 | <Text |
| 150 | color={isSelected ? option.color : "white"} |
| 151 | bold={isSelected} |
| 152 | > |
| 153 | {isSelected ? "> " : " "} |
| 154 | {option.name} |
| 155 | </Text> |
| 156 | <Text color="gray" dimColor> |
| 157 | {" "} |
| 158 | {shortcut} |
| 159 | </Text> |
| 160 | </Box> |
| 161 | ); |
| 162 | })} |
| 163 | </Box> |
| 164 | </Box> |
| 165 | ); |
| 166 | }; |