2025-05-31 18:41:00 +00:00
import { z } from "zod"
import { Tool } from "./tool"
2025-06-04 17:12:13 +00:00
import DESCRIPTION from "./bash.txt"
2025-06-30 19:28:47 +00:00
import { App } from "../app/app"
2025-05-19 23:29:38 +00:00
2025-05-31 18:41:00 +00:00
const MAX_OUTPUT_LENGTH = 30000
const DEFAULT_TIMEOUT = 1 * 60 * 1000
const MAX_TIMEOUT = 10 * 60 * 1000
2025-05-19 23:29:38 +00:00
2025-05-31 21:12:16 +00:00
export const BashTool = Tool . define ( {
2025-06-19 13:59:12 +00:00
id : "bash" ,
2025-05-19 23:29:38 +00:00
description : DESCRIPTION ,
parameters : z.object ( {
2025-06-04 17:12:13 +00:00
command : z.string ( ) . describe ( "The command to execute" ) ,
2025-05-19 23:29:38 +00:00
timeout : z
. number ( )
. min ( 0 )
. max ( MAX_TIMEOUT )
. describe ( "Optional timeout in milliseconds" )
2025-06-17 15:27:07 +00:00
. optional ( ) ,
2025-06-04 17:12:13 +00:00
description : z
. string ( )
. describe (
"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'" ,
) ,
2025-05-19 23:29:38 +00:00
} ) ,
2025-06-10 21:56:05 +00:00
async execute ( params , ctx ) {
2025-05-31 18:41:00 +00:00
const timeout = Math . min ( params . timeout ? ? DEFAULT_TIMEOUT , MAX_TIMEOUT )
2025-05-19 23:29:38 +00:00
2025-06-03 17:08:47 +00:00
const process = Bun . spawn ( {
2025-05-19 23:29:38 +00:00
cmd : [ "bash" , "-c" , params . command ] ,
2025-06-30 19:28:47 +00:00
cwd : App.info ( ) . path . cwd ,
2025-05-19 23:29:38 +00:00
maxBuffer : MAX_OUTPUT_LENGTH ,
2025-06-10 21:56:05 +00:00
signal : ctx.abort ,
2025-05-19 23:29:38 +00:00
timeout : timeout ,
2025-06-03 18:48:05 +00:00
stdout : "pipe" ,
stderr : "pipe" ,
2025-05-31 18:41:00 +00:00
} )
2025-06-03 17:08:47 +00:00
await process . exited
const stdout = await new Response ( process . stdout ) . text ( )
2025-06-03 18:48:05 +00:00
const stderr = await new Response ( process . stderr ) . text ( )
2025-06-03 17:08:47 +00:00
2025-05-19 23:29:38 +00:00
return {
2025-06-03 17:08:47 +00:00
metadata : {
stderr ,
stdout ,
2025-06-22 18:24:35 +00:00
exit : process.exitCode ,
2025-06-04 17:12:13 +00:00
description : params.description ,
2025-06-11 16:44:17 +00:00
title : params.command ,
2025-06-03 17:08:47 +00:00
} ,
2025-06-22 18:24:35 +00:00
output : [
` <stdout> ` ,
stdout ? ? "" ,
` </stdout> ` ,
` <stderr> ` ,
stderr ? ? "" ,
` </stderr> ` ,
] . join ( "\n" ) ,
2025-05-31 18:41:00 +00:00
}
2025-05-19 23:29:38 +00:00
} ,
2025-05-31 18:41:00 +00:00
} )