Elffuss BraidHub

kikocis/continue public

a367fdbffe58 · 4.6 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 [hasShownDangerousCommandWarningOnce] = useState(() => {
68 const prev = hasShownDangerousCommandWarning;
69 hasShownDangerousCommandWarning = true;
70 return prev;
71 });
72 const showDynamicWarning =
73 hasDynamicEvaluation && !hasShownDangerousCommandWarningOnce;
74
75 useInput((input, key) => {
76 if (key.return) {
77 const selectedOption = permissionOptions[selectedIndex];
78 onResponse(
79 requestId,
80 selectedOption.approved,
81 selectedOption.createPolicy,
82 selectedOption.stopStream,
83 );
84 return;
85 }
86
87 // Tab to continue (approve)
88 if (key.tab && !key.shift) {
89 onResponse(requestId, true, false, false);
90 return;
91 }
92
93 // Shift+Tab to continue with policy creation
94 if (key.tab && key.shift) {
95 onResponse(requestId, true, true, false);
96 return;
97 }
98
99 // Escape or Ctrl+C to reject with stop stream
100 if (key.escape || (key.ctrl && input === "c")) {
101 onResponse(requestId, false, false, true);
102 return;
103 }
104
105 if (key.upArrow) {
106 setSelectedIndex(Math.max(0, selectedIndex - 1));
107 } else if (key.downArrow) {
108 setSelectedIndex(
109 Math.min(permissionOptions.length - 1, selectedIndex + 1),
110 );
111 }
112
113 // Also support y/n for quick responses
114 if (input === "y" || input === "Y") {
115 onResponse(requestId, true, false, false);
116 } else if (input === "n" || input === "N") {
117 onResponse(requestId, false, false, true);
118 }
119 });
120
121 return (
122 <Box {...defaultBoxStyles("magenta")}>
123 <Text color="magenta" bold>
124 <ToolCallTitle toolName={toolName} args={toolArgs} />
125 </Text>
126
127 {/* Show preview of what the tool will do */}
128 <Box>
129 <ToolPreview
130 toolName={toolName}
131 toolArgs={toolArgs}
132 toolCallPreview={toolCallPreview}
133 />
134 </Box>
135
136 <Box marginTop={1} flexDirection="column">
137 <Text color="dim">Would you like to continue?</Text>
138 {showDynamicWarning && (
139 <Box marginTop={1}>
140 <Text color="yellow" dimColor>
141 Note: Dangerous commands will be blocked regardless of your
142 preference.
143 </Text>
144 </Box>
145 )}
146 {permissionOptions.map((option, index) => {
147 const isSelected = index === selectedIndex;
148 let shortcut = "";
149 if (option.id === "approve") shortcut = "(tab)";
150 else if (option.id === "approve_policy") shortcut = "(shift+tab)";
151 else if (option.id === "deny_stop") shortcut = "(esc)";
152
153 return (
154 <Box key={option.id} marginTop={index === 0 ? 1 : 0}>
155 <Text
156 color={isSelected ? option.color : "white"}
157 bold={isSelected}
158 >
159 {isSelected ? "> " : " "}
160 {option.name}
161 </Text>
162 <Text color="gray" dimColor>
163 {" "}
164 {shortcut}
165 </Text>
166 </Box>
167 );
168 })}
169 </Box>
170 </Box>
171 );
172};