bash.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import z from "zod"
  2. import { spawn } from "child_process"
  3. import { Tool } from "./tool"
  4. import path from "path"
  5. import DESCRIPTION from "./bash.txt"
  6. import { Log } from "../util/log"
  7. import { Instance } from "../project/instance"
  8. import { lazy } from "@/util/lazy"
  9. import { Language } from "web-tree-sitter"
  10. import { $ } from "bun"
  11. import { Filesystem } from "@/util/filesystem"
  12. import { fileURLToPath } from "url"
  13. import { Flag } from "@/flag/flag.ts"
  14. import { Shell } from "@/shell/shell"
  15. import { BashArity } from "@/permission/arity"
  16. import { Truncate } from "./truncation"
  17. import { Plugin } from "@/plugin"
  18. const MAX_METADATA_LENGTH = 30_000
  19. const DEFAULT_TIMEOUT = Flag.OPENCODE_EXPERIMENTAL_BASH_DEFAULT_TIMEOUT_MS || 2 * 60 * 1000
  20. export const log = Log.create({ service: "bash-tool" })
  21. const resolveWasm = (asset: string) => {
  22. if (asset.startsWith("file://")) return fileURLToPath(asset)
  23. if (asset.startsWith("/") || /^[a-z]:/i.test(asset)) return asset
  24. const url = new URL(asset, import.meta.url)
  25. return fileURLToPath(url)
  26. }
  27. const parser = lazy(async () => {
  28. const { Parser } = await import("web-tree-sitter")
  29. const { default: treeWasm } = await import("web-tree-sitter/tree-sitter.wasm" as string, {
  30. with: { type: "wasm" },
  31. })
  32. const treePath = resolveWasm(treeWasm)
  33. await Parser.init({
  34. locateFile() {
  35. return treePath
  36. },
  37. })
  38. const { default: bashWasm } = await import("tree-sitter-bash/tree-sitter-bash.wasm" as string, {
  39. with: { type: "wasm" },
  40. })
  41. const bashPath = resolveWasm(bashWasm)
  42. const bashLanguage = await Language.load(bashPath)
  43. const p = new Parser()
  44. p.setLanguage(bashLanguage)
  45. return p
  46. })
  47. // TODO: we may wanna rename this tool so it works better on other shells
  48. export const BashTool = Tool.define("bash", async () => {
  49. const shell = Shell.acceptable()
  50. log.info("bash tool using shell", { shell })
  51. return {
  52. description: DESCRIPTION.replaceAll("${directory}", Instance.directory)
  53. .replaceAll("${maxLines}", String(Truncate.MAX_LINES))
  54. .replaceAll("${maxBytes}", String(Truncate.MAX_BYTES)),
  55. parameters: z.object({
  56. command: z.string().describe("The command to execute"),
  57. timeout: z.number().describe("Optional timeout in milliseconds").optional(),
  58. workdir: z
  59. .string()
  60. .describe(
  61. `The working directory to run the command in. Defaults to ${Instance.directory}. Use this instead of 'cd' commands.`,
  62. )
  63. .optional(),
  64. description: z
  65. .string()
  66. .describe(
  67. "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'",
  68. ),
  69. }),
  70. async execute(params, ctx) {
  71. const cwd = params.workdir || Instance.directory
  72. if (params.timeout !== undefined && params.timeout < 0) {
  73. throw new Error(`Invalid timeout value: ${params.timeout}. Timeout must be a positive number.`)
  74. }
  75. const timeout = params.timeout ?? DEFAULT_TIMEOUT
  76. const tree = await parser().then((p) => p.parse(params.command))
  77. if (!tree) {
  78. throw new Error("Failed to parse command")
  79. }
  80. const directories = new Set<string>()
  81. if (!Instance.containsPath(cwd)) directories.add(cwd)
  82. const patterns = new Set<string>()
  83. const always = new Set<string>()
  84. for (const node of tree.rootNode.descendantsOfType("command")) {
  85. if (!node) continue
  86. // Get full command text including redirects if present
  87. let commandText = node.parent?.type === "redirected_statement" ? node.parent.text : node.text
  88. const command = []
  89. for (let i = 0; i < node.childCount; i++) {
  90. const child = node.child(i)
  91. if (!child) continue
  92. if (
  93. child.type !== "command_name" &&
  94. child.type !== "word" &&
  95. child.type !== "string" &&
  96. child.type !== "raw_string" &&
  97. child.type !== "concatenation"
  98. ) {
  99. continue
  100. }
  101. command.push(child.text)
  102. }
  103. // not an exhaustive list, but covers most common cases
  104. if (["cd", "rm", "cp", "mv", "mkdir", "touch", "chmod", "chown", "cat"].includes(command[0])) {
  105. for (const arg of command.slice(1)) {
  106. if (arg.startsWith("-") || (command[0] === "chmod" && arg.startsWith("+"))) continue
  107. const resolved = await $`realpath ${arg}`
  108. .cwd(cwd)
  109. .quiet()
  110. .nothrow()
  111. .text()
  112. .then((x) => x.trim())
  113. log.info("resolved path", { arg, resolved })
  114. if (resolved) {
  115. // Git Bash on Windows returns Unix-style paths like /c/Users/...
  116. const normalized =
  117. process.platform === "win32" && resolved.match(/^\/[a-z]\//)
  118. ? resolved.replace(/^\/([a-z])\//, (_, drive) => `${drive.toUpperCase()}:\\`).replace(/\//g, "\\")
  119. : resolved
  120. if (!Instance.containsPath(normalized)) {
  121. const dir = (await Filesystem.isDir(normalized)) ? normalized : path.dirname(normalized)
  122. directories.add(dir)
  123. }
  124. }
  125. }
  126. }
  127. // cd covered by above check
  128. if (command.length && command[0] !== "cd") {
  129. patterns.add(commandText)
  130. always.add(BashArity.prefix(command).join(" ") + " *")
  131. }
  132. }
  133. if (directories.size > 0) {
  134. const globs = Array.from(directories).map((dir) => path.join(dir, "*"))
  135. await ctx.ask({
  136. permission: "external_directory",
  137. patterns: globs,
  138. always: globs,
  139. metadata: {},
  140. })
  141. }
  142. if (patterns.size > 0) {
  143. await ctx.ask({
  144. permission: "bash",
  145. patterns: Array.from(patterns),
  146. always: Array.from(always),
  147. metadata: {},
  148. })
  149. }
  150. const shellEnv = await Plugin.trigger("shell.env", { cwd }, { env: {} })
  151. const proc = spawn(params.command, {
  152. shell,
  153. cwd,
  154. env: {
  155. ...process.env,
  156. ...shellEnv.env,
  157. },
  158. stdio: ["ignore", "pipe", "pipe"],
  159. detached: process.platform !== "win32",
  160. })
  161. let output = ""
  162. // Initialize metadata with empty output
  163. ctx.metadata({
  164. metadata: {
  165. output: "",
  166. description: params.description,
  167. },
  168. })
  169. const append = (chunk: Buffer) => {
  170. output += chunk.toString()
  171. ctx.metadata({
  172. metadata: {
  173. // truncate the metadata to avoid GIANT blobs of data (has nothing to do w/ what agent can access)
  174. output: output.length > MAX_METADATA_LENGTH ? output.slice(0, MAX_METADATA_LENGTH) + "\n\n..." : output,
  175. description: params.description,
  176. },
  177. })
  178. }
  179. proc.stdout?.on("data", append)
  180. proc.stderr?.on("data", append)
  181. let timedOut = false
  182. let aborted = false
  183. let exited = false
  184. const kill = () => Shell.killTree(proc, { exited: () => exited })
  185. if (ctx.abort.aborted) {
  186. aborted = true
  187. await kill()
  188. }
  189. const abortHandler = () => {
  190. aborted = true
  191. void kill()
  192. }
  193. ctx.abort.addEventListener("abort", abortHandler, { once: true })
  194. const timeoutTimer = setTimeout(() => {
  195. timedOut = true
  196. void kill()
  197. }, timeout + 100)
  198. await new Promise<void>((resolve, reject) => {
  199. const cleanup = () => {
  200. clearTimeout(timeoutTimer)
  201. ctx.abort.removeEventListener("abort", abortHandler)
  202. }
  203. proc.once("exit", () => {
  204. exited = true
  205. cleanup()
  206. resolve()
  207. })
  208. proc.once("error", (error) => {
  209. exited = true
  210. cleanup()
  211. reject(error)
  212. })
  213. })
  214. const resultMetadata: string[] = []
  215. if (timedOut) {
  216. resultMetadata.push(`bash tool terminated command after exceeding timeout ${timeout} ms`)
  217. }
  218. if (aborted) {
  219. resultMetadata.push("User aborted the command")
  220. }
  221. if (resultMetadata.length > 0) {
  222. output += "\n\n<bash_metadata>\n" + resultMetadata.join("\n") + "\n</bash_metadata>"
  223. }
  224. return {
  225. title: params.description,
  226. metadata: {
  227. output: output.length > MAX_METADATA_LENGTH ? output.slice(0, MAX_METADATA_LENGTH) + "\n\n..." : output,
  228. exit: proc.exitCode,
  229. description: params.description,
  230. },
  231. output,
  232. }
  233. },
  234. }
  235. })