bash.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { z } from "zod"
  2. import { exec } from "child_process"
  3. import { text } from "stream/consumers"
  4. import { Tool } from "./tool"
  5. import DESCRIPTION from "./bash.txt"
  6. import { App } from "../app/app"
  7. import { Permission } from "../permission"
  8. import { Config } from "../config/config"
  9. import { Filesystem } from "../util/filesystem"
  10. import { lazy } from "../util/lazy"
  11. import { Log } from "../util/log"
  12. import { Wildcard } from "../util/wildcard"
  13. import { $ } from "bun"
  14. const MAX_OUTPUT_LENGTH = 30000
  15. const DEFAULT_TIMEOUT = 1 * 60 * 1000
  16. const MAX_TIMEOUT = 10 * 60 * 1000
  17. const log = Log.create({ service: "bash-tool" })
  18. const parser = lazy(async () => {
  19. const { default: Parser } = await import("tree-sitter")
  20. const Bash = await import("tree-sitter-bash")
  21. const p = new Parser()
  22. p.setLanguage(Bash.language as any)
  23. return p
  24. })
  25. export const BashTool = Tool.define("bash", {
  26. description: DESCRIPTION,
  27. parameters: z.object({
  28. command: z.string().describe("The command to execute"),
  29. timeout: z.number().describe("Optional timeout in milliseconds").optional(),
  30. description: z
  31. .string()
  32. .describe(
  33. "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'",
  34. ),
  35. }),
  36. async execute(params, ctx) {
  37. const timeout = Math.min(params.timeout ?? DEFAULT_TIMEOUT, MAX_TIMEOUT)
  38. const app = App.info()
  39. const cfg = await Config.get()
  40. const tree = await parser().then((p) => p.parse(params.command))
  41. const permissions = (() => {
  42. const value = cfg.permission?.bash
  43. if (!value)
  44. return {
  45. "*": "allow",
  46. }
  47. if (typeof value === "string")
  48. return {
  49. "*": value,
  50. }
  51. return value
  52. })()
  53. let needsAsk = false
  54. for (const node of tree.rootNode.descendantsOfType("command")) {
  55. const command = []
  56. for (let i = 0; i < node.childCount; i++) {
  57. const child = node.child(i)
  58. if (!child) continue
  59. if (
  60. child.type !== "command_name" &&
  61. child.type !== "word" &&
  62. child.type !== "string" &&
  63. child.type !== "raw_string" &&
  64. child.type !== "concatenation"
  65. ) {
  66. continue
  67. }
  68. command.push(child.text)
  69. }
  70. // not an exhaustive list, but covers most common cases
  71. if (["cd", "rm", "cp", "mv", "mkdir", "touch", "chmod", "chown"].includes(command[0])) {
  72. for (const arg of command.slice(1)) {
  73. if (arg.startsWith("-") || (command[0] === "chmod" && arg.startsWith("+"))) continue
  74. const resolved = await $`realpath ${arg}`
  75. .quiet()
  76. .nothrow()
  77. .text()
  78. .then((x) => x.trim())
  79. log.info("resolved path", { arg, resolved })
  80. if (resolved && !Filesystem.contains(app.path.cwd, resolved)) {
  81. throw new Error(
  82. `This command references paths outside of ${app.path.cwd} so it is not allowed to be executed.`,
  83. )
  84. }
  85. }
  86. }
  87. // always allow cd if it passes above check
  88. if (!needsAsk && command[0] !== "cd") {
  89. const action = (() => {
  90. for (const [pattern, value] of Object.entries(permissions)) {
  91. const match = Wildcard.match(node.text, pattern)
  92. log.info("checking", { text: node.text.trim(), pattern, match })
  93. if (match) return value
  94. }
  95. return "ask"
  96. })()
  97. if (action === "deny") {
  98. throw new Error(
  99. "The user has specifically restricted access to this command, you are not allowed to execute it.",
  100. )
  101. }
  102. if (action === "ask") needsAsk = true
  103. }
  104. }
  105. if (needsAsk) {
  106. await Permission.ask({
  107. type: "bash",
  108. sessionID: ctx.sessionID,
  109. messageID: ctx.messageID,
  110. callID: ctx.callID,
  111. title: params.command,
  112. metadata: {
  113. command: params.command,
  114. },
  115. })
  116. }
  117. const process = exec(params.command, {
  118. cwd: app.path.cwd,
  119. signal: ctx.abort,
  120. maxBuffer: MAX_OUTPUT_LENGTH,
  121. timeout,
  122. })
  123. const stdoutPromise = text(process.stdout!)
  124. const stderrPromise = text(process.stderr!)
  125. await new Promise<void>((resolve) => {
  126. process.on("close", () => {
  127. resolve()
  128. })
  129. })
  130. const stdout = await stdoutPromise
  131. const stderr = await stderrPromise
  132. return {
  133. title: params.command,
  134. metadata: {
  135. stderr,
  136. stdout,
  137. exit: process.exitCode,
  138. description: params.description,
  139. },
  140. output: [`<stdout>`, stdout ?? "", `</stdout>`, `<stderr>`, stderr ?? "", `</stderr>`].join("\n"),
  141. }
  142. },
  143. })