bash.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { z } from "zod"
  2. import { Tool } from "./tool"
  3. import DESCRIPTION from "./bash.txt"
  4. const MAX_OUTPUT_LENGTH = 30000
  5. const BANNED_COMMANDS = [
  6. "alias",
  7. "curl",
  8. "curlie",
  9. "wget",
  10. "axel",
  11. "aria2c",
  12. "nc",
  13. "telnet",
  14. "lynx",
  15. "w3m",
  16. "links",
  17. "httpie",
  18. "xh",
  19. "http-prompt",
  20. "chrome",
  21. "firefox",
  22. "safari",
  23. ]
  24. const DEFAULT_TIMEOUT = 1 * 60 * 1000
  25. const MAX_TIMEOUT = 10 * 60 * 1000
  26. export const BashTool = Tool.define({
  27. id: "opencode.bash",
  28. description: DESCRIPTION,
  29. parameters: z.object({
  30. command: z.string().describe("The command to execute"),
  31. timeout: z
  32. .number()
  33. .min(0)
  34. .max(MAX_TIMEOUT)
  35. .describe("Optional timeout in milliseconds")
  36. .optional(),
  37. description: z
  38. .string()
  39. .describe(
  40. "Clear, concise description of what this command does in 5-10 words. Examples:\nInput: ls\nOutput: Lists files in current directory\n\nInput: git status\nOutput: Shows working tree status\n\nInput: npm install\nOutput: Installs package dependencies\n\nInput: mkdir foo\nOutput: Creates directory 'foo'",
  41. ),
  42. }),
  43. async execute(params, ctx) {
  44. const timeout = Math.min(params.timeout ?? DEFAULT_TIMEOUT, MAX_TIMEOUT)
  45. if (BANNED_COMMANDS.some((item) => params.command.startsWith(item)))
  46. throw new Error(`Command '${params.command}' is not allowed`)
  47. const process = Bun.spawn({
  48. cmd: ["bash", "-c", params.command],
  49. maxBuffer: MAX_OUTPUT_LENGTH,
  50. signal: ctx.abort,
  51. timeout: timeout,
  52. stdout: "pipe",
  53. stderr: "pipe",
  54. })
  55. await process.exited
  56. const stdout = await new Response(process.stdout).text()
  57. const stderr = await new Response(process.stderr).text()
  58. return {
  59. metadata: {
  60. stderr,
  61. stdout,
  62. description: params.description,
  63. title: params.command,
  64. },
  65. output: stdout.replaceAll(/\x1b\[[0-9;]*m/g, ""),
  66. }
  67. },
  68. })