Elffuss BraidHub

kikocis/continue public

main · 2.0 KiBRawPermalink
1import { streamLines } from "../../../diff/util.js";
2import { SlashCommand } from "../../../index.js";
3import { removeQuotesAndEscapes } from "../../../util/index.js";
4
5function commandIsPotentiallyDangerous(command: string) {
6 return (
7 command.includes("rm -rf") ||
8 command.includes("sudo") ||
9 command.includes("cd / ")
10 );
11}
12
13const GenerateTerminalCommand: SlashCommand = {
14 name: "cmd",
15 description: "Generate a shell command",
16 run: async function* ({ ide, llm, input }) {
17 if (input.trim() === "") {
18 yield "Please provide a description of the shell command you want to generate. For example, '/cmd List all files in the current directory'.";
19 return;
20 }
21
22 const gen = llm.streamComplete(
23 `The user has made a request to run a shell command. Their description of what it should do is:
24
25"${input}"
26
27Please write a shell command that will do what the user requested. Your output should consist of only the command itself, without any explanation or example output. Do not use any newlines. Only output the command that when inserted into the terminal will do precisely what was requested. Here is the command:`,
28 new AbortController().signal,
29 );
30
31 const lines = streamLines(gen);
32 let cmd = "";
33 for await (const line of lines) {
34 console.log(line);
35 if (line.startsWith("```") && line.endsWith("```")) {
36 cmd = line.split(" ").slice(1).join(" ").slice(0, -3);
37 break;
38 }
39
40 if (
41 line.startsWith(">") ||
42 line.startsWith("``") ||
43 line.startsWith("\\begin{") ||
44 line.trim() === ""
45 ) {
46 continue;
47 }
48
49 cmd = removeQuotesAndEscapes(line.trim());
50 break;
51 }
52
53 yield `Generated shell command: ${cmd}`;
54 if (commandIsPotentiallyDangerous(cmd)) {
55 yield "\n\nWarning: This command may be potentially dangerous. Please double-check before pasting it in your terminal.";
56 } else {
57 await ide.runCommand(cmd);
58 }
59 },
60};
61
62export default GenerateTerminalCommand;