Elffuss BraidHub

kikocis/continue public

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