Elffuss BraidHub

kikocis/continue public

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