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