Elffuss BraidHub

kikocis/continue public

681d1de32c2d · 2.8 KiBRawPermalink
1import os from "os";
2import { Tool, ToolPolicy } from "../..";
3import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn";
4import { evaluateTerminalCommandSecurity } from "../security/terminalCommandSecurity";
5
6/**
7 * Get the preferred shell for the current platform
8 * @returns The preferred shell command or path
9 */
10function getPreferredShell(): string {
11 const platform = os.platform();
12
13 if (platform === "win32") {
14 return "powershell.exe";
15 } else if (platform === "darwin") {
16 return process.env.SHELL || "/bin/zsh";
17 } else {
18 // Linux and other Unix-like systems
19 return process.env.SHELL || "/bin/bash";
20 }
21}
22
23const PLATFORM_INFO = `Choose terminal commands and scripts optimized for ${os.platform()} and ${os.arch()} and shell ${getPreferredShell()}.`;
24
25const RUN_COMMAND_NOTES = `The shell is not stateful and will not remember any previous commands.\
26 When a command is run in the background ALWAYS suggest using shell commands to stop it; NEVER suggest using Ctrl+C.\
27 When suggesting subsequent shell commands ALWAYS format them in shell command blocks.\
28 Do NOT perform actions requiring special/admin privileges.\
29 ${PLATFORM_INFO}`;
30
31export const runTerminalCommandTool: Tool = {
32 type: "function",
33 displayTitle: "Run Terminal Command",
34 wouldLikeTo: "run the following terminal command:",
35 isCurrently: "running the following terminal command:",
36 hasAlready: "ran the following terminal command:",
37 readonly: false,
38 group: BUILT_IN_GROUP_NAME,
39 function: {
40 name: BuiltInToolNames.RunTerminalCommand,
41 description: `Run a terminal command in the current directory.\n${RUN_COMMAND_NOTES}`,
42 parameters: {
43 type: "object",
44 required: ["command"],
45 properties: {
46 command: {
47 type: "string",
48 description:
49 "The command to run. This will be passed directly into the IDE shell.",
50 },
51 waitForCompletion: {
52 type: "boolean",
53 description:
54 "Whether to wait for the command to complete before returning. Default is true. Set to false to run the command in the background. Set to true to run the command in the foreground and wait to collect the output.",
55 },
56 },
57 },
58 },
59 defaultToolPolicy: "allowedWithPermission",
60 evaluateToolCallPolicy: (
61 basePolicy: ToolPolicy,
62 parsedArgs: Record<string, unknown>,
63 ): ToolPolicy => {
64 return evaluateTerminalCommandSecurity(
65 basePolicy,
66 parsedArgs.command as string,
67 );
68 },
69 systemMessageDescription: {
70 prefix: `To run a terminal command, use the ${BuiltInToolNames.RunTerminalCommand} tool
71${RUN_COMMAND_NOTES}
72You can also optionally include the waitForCompletion argument set to false to run the command in the background.
73For example, to see the git log, you could respond with:`,
74 exampleArgs: [["command", "git log"]],
75 },
76};