Elffuss BraidHub

kikocis/continue public

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