Elffuss BraidHub

kikocis/continue public

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