bash.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { z } from "zod"
  2. import { Tool } from "./tool"
  3. import DESCRIPTION from "./bash.txt"
  4. import { App } from "../app/app"
  5. const MAX_OUTPUT_LENGTH = 30000
  6. const DEFAULT_TIMEOUT = 1 * 60 * 1000
  7. const MAX_TIMEOUT = 10 * 60 * 1000
  8. export const BashTool = Tool.define("bash", {
  9. description: DESCRIPTION,
  10. parameters: z.object({
  11. command: z.string().describe("The command to execute"),
  12. timeout: z.number().min(0).max(MAX_TIMEOUT).describe("Optional timeout in milliseconds").optional(),
  13. description: z
  14. .string()
  15. .describe(
  16. "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'",
  17. ),
  18. }),
  19. async execute(params, ctx) {
  20. const timeout = Math.min(params.timeout ?? DEFAULT_TIMEOUT, MAX_TIMEOUT)
  21. const process = Bun.spawn({
  22. cmd: ["bash", "-c", params.command],
  23. cwd: App.info().path.cwd,
  24. maxBuffer: MAX_OUTPUT_LENGTH,
  25. signal: ctx.abort,
  26. timeout: timeout,
  27. stdout: "pipe",
  28. stderr: "pipe",
  29. })
  30. await process.exited
  31. const stdout = await new Response(process.stdout).text()
  32. const stderr = await new Response(process.stderr).text()
  33. return {
  34. title: params.command,
  35. metadata: {
  36. stderr,
  37. stdout,
  38. exit: process.exitCode,
  39. description: params.description,
  40. },
  41. output: [`<stdout>`, stdout ?? "", `</stdout>`, `<stderr>`, stderr ?? "", `</stderr>`].join("\n"),
  42. }
  43. },
  44. })