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