bash.ts 5.0 KB

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