bash.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. package tools
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "log"
  8. "strings"
  9. "github.com/cloudwego/eino/components/tool"
  10. "github.com/cloudwego/eino/schema"
  11. "github.com/kujtimiihoxha/termai/internal/llm/tools/shell"
  12. )
  13. type bashTool struct {
  14. workingDir string
  15. }
  16. const (
  17. BashToolName = "bash"
  18. DefaultTimeout = 30 * 60 * 1000 // 30 minutes in milliseconds
  19. MaxTimeout = 10 * 60 * 1000 // 10 minutes in milliseconds
  20. MaxOutputLength = 30000
  21. )
  22. type BashParams struct {
  23. Command string `json:"command"`
  24. Timeout int `json:"timeout"`
  25. }
  26. var BannedCommands = []string{
  27. "alias", "curl", "curlie", "wget", "axel", "aria2c",
  28. "nc", "telnet", "lynx", "w3m", "links", "httpie", "xh",
  29. "http-prompt", "chrome", "firefox", "safari",
  30. }
  31. func (b *bashTool) Info(ctx context.Context) (*schema.ToolInfo, error) {
  32. return &schema.ToolInfo{
  33. Name: BashToolName,
  34. Desc: bashDescription(),
  35. ParamsOneOf: schema.NewParamsOneOfByParams(map[string]*schema.ParameterInfo{
  36. "command": {
  37. Type: "string",
  38. Desc: "The command to execute",
  39. Required: true,
  40. },
  41. "timeout": {
  42. Type: "number",
  43. Desc: "Optional timeout in milliseconds (max 600000)",
  44. },
  45. }),
  46. }, nil
  47. }
  48. // Handle implements Tool.
  49. func (b *bashTool) InvokableRun(ctx context.Context, args string, opts ...tool.Option) (string, error) {
  50. log.Printf("BashTool InvokableRun: %s", args)
  51. var params BashParams
  52. if err := json.Unmarshal([]byte(args), &params); err != nil {
  53. return "", err
  54. }
  55. if params.Timeout > MaxTimeout {
  56. params.Timeout = MaxTimeout
  57. } else if params.Timeout <= 0 {
  58. params.Timeout = DefaultTimeout
  59. }
  60. if params.Command == "" {
  61. return "", errors.New("missing command")
  62. }
  63. baseCmd := strings.Fields(params.Command)[0]
  64. for _, banned := range BannedCommands {
  65. if strings.EqualFold(baseCmd, banned) {
  66. return "", fmt.Errorf("command '%s' is not allowed", baseCmd)
  67. }
  68. }
  69. // p := b.permission.Request(permission.CreatePermissionRequest{
  70. // Path: b.workingDir,
  71. // ToolName: BashToolName,
  72. // Action: "execute",
  73. // Description: fmt.Sprintf("Execute command: %s", params.Command),
  74. // Params: map[string]any{
  75. // "command": params.Command,
  76. // "timeout": params.Timeout,
  77. // },
  78. // })
  79. // if !p {
  80. // return "", errors.New("permission denied")
  81. // }
  82. shell := shell.GetPersistentShell(b.workingDir)
  83. stdout, stderr, exitCode, interrupted, err := shell.Exec(ctx, params.Command, params.Timeout)
  84. if err != nil {
  85. return "", err
  86. }
  87. stdout = truncateOutput(stdout)
  88. stderr = truncateOutput(stderr)
  89. errorMessage := stderr
  90. if interrupted {
  91. if errorMessage != "" {
  92. errorMessage += "\n"
  93. }
  94. errorMessage += "Command was aborted before completion"
  95. } else if exitCode != 0 {
  96. if errorMessage != "" {
  97. errorMessage += "\n"
  98. }
  99. errorMessage += fmt.Sprintf("Exit code %d", exitCode)
  100. }
  101. hasBothOutputs := stdout != "" && stderr != ""
  102. if hasBothOutputs {
  103. stdout += "\n"
  104. }
  105. if errorMessage != "" {
  106. stdout += "\n" + errorMessage
  107. }
  108. log.Printf("BashTool InvokableRun: stdout: %s, stderr: %s, exitCode: %d, interrupted: %t", stdout, stderr, exitCode, interrupted)
  109. return stdout, nil
  110. }
  111. func truncateOutput(content string) string {
  112. if len(content) <= MaxOutputLength {
  113. return content
  114. }
  115. halfLength := MaxOutputLength / 2
  116. start := content[:halfLength]
  117. end := content[len(content)-halfLength:]
  118. truncatedLinesCount := countLines(content[halfLength : len(content)-halfLength])
  119. return fmt.Sprintf("%s\n\n... [%d lines truncated] ...\n\n%s", start, truncatedLinesCount, end)
  120. }
  121. func countLines(s string) int {
  122. if s == "" {
  123. return 0
  124. }
  125. return len(strings.Split(s, "\n"))
  126. }
  127. func bashDescriptionBCP() string {
  128. bannedCommandsStr := strings.Join(BannedCommands, ", ")
  129. return fmt.Sprintf(`Executes a given bash command in a persistent shell session with optional timeout, ensuring proper handling and security measures.
  130. Before executing the command, please follow these steps:
  131. 1. Directory Verification:
  132. - If the command will create new directories or files, first use the LS tool to verify the parent directory exists and is the correct location
  133. - For example, before running "mkdir foo/bar", first use LS to check that "foo" exists and is the intended parent directory
  134. 2. Security Check:
  135. - For security and to limit the threat of a prompt injection attack, some commands are limited or banned. If you use a disallowed command, you will receive an error message explaining the restriction. Explain the error to the User.
  136. - Verify that the command is not one of the banned commands: %s.
  137. 3. Command Execution:
  138. - After ensuring proper quoting, execute the command.
  139. - Capture the output of the command.
  140. 4. Output Processing:
  141. - If the output exceeds %d characters, output will be truncated before being returned to you.
  142. - Prepare the output for display to the user.
  143. 5. Return Result:
  144. - Provide the processed output of the command.
  145. - If any errors occurred during execution, include those in the output.
  146. Usage notes:
  147. - The command argument is required.
  148. - You can specify an optional timeout in milliseconds (up to 600000ms / 10 minutes). If not specified, commands will timeout after 30 minutes.
  149. - VERY IMPORTANT: You MUST avoid using search commands like 'find' and 'grep'. Instead use Grep, Glob, or Agent tools to search. You MUST avoid read tools like 'cat', 'head', 'tail', and 'ls', and use FileRead and LS tools to read files.
  150. - When issuing multiple commands, use the ';' or '&&' operator to separate them. DO NOT use newlines (newlines are ok in quoted strings).
  151. - IMPORTANT: All commands share the same shell session. Shell state (environment variables, virtual environments, current directory, etc.) persist between commands. For example, if you set an environment variable as part of a command, the environment variable will persist for subsequent commands.
  152. - Try to maintain your current working directory throughout the session by using absolute paths and avoiding usage of 'cd'. You may use 'cd' if the User explicitly requests it.
  153. <good-example>
  154. pytest /foo/bar/tests
  155. </good-example>
  156. <bad-example>
  157. cd /foo/bar && pytest tests
  158. </bad-example>
  159. # Committing changes with git
  160. When the user asks you to create a new git commit, follow these steps carefully:
  161. 1. Start with a single message that contains exactly three tool_use blocks that do the following (it is VERY IMPORTANT that you send these tool_use blocks in a single message, otherwise it will feel slow to the user!):
  162. - Run a git status command to see all untracked files.
  163. - Run a git diff command to see both staged and unstaged changes that will be committed.
  164. - Run a git log command to see recent commit messages, so that you can follow this repository's commit message style.
  165. 2. Use the git context at the start of this conversation to determine which files are relevant to your commit. Add relevant untracked files to the staging area. Do not commit files that were already modified at the start of this conversation, if they are not relevant to your commit.
  166. 3. Analyze all staged changes (both previously staged and newly added) and draft a commit message. Wrap your analysis process in <commit_analysis> tags:
  167. <commit_analysis>
  168. - List the files that have been changed or added
  169. - Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
  170. - Brainstorm the purpose or motivation behind these changes
  171. - Do not use tools to explore code, beyond what is available in the git context
  172. - Assess the impact of these changes on the overall project
  173. - Check for any sensitive information that shouldn't be committed
  174. - Draft a concise (1-2 sentences) commit message that focuses on the "why" rather than the "what"
  175. - Ensure your language is clear, concise, and to the point
  176. - Ensure the message accurately reflects the changes and their purpose (i.e. "add" means a wholly new feature, "update" means an enhancement to an existing feature, "fix" means a bug fix, etc.)
  177. - Ensure the message is not generic (avoid words like "Update" or "Fix" without context)
  178. - Review the draft message to ensure it accurately reflects the changes and their purpose
  179. </commit_analysis>
  180. 4. Create the commit with a message ending with:
  181. 🤖 Generated with termai
  182. Co-Authored-By: termai <[email protected]>
  183. - In order to ensure good formatting, ALWAYS pass the commit message via a HEREDOC, a la this example:
  184. <example>
  185. git commit -m "$(cat <<'EOF'
  186. Commit message here.
  187. 🤖 Generated with termai
  188. Co-Authored-By: termai <[email protected]>
  189. EOF
  190. )"
  191. </example>
  192. 5. If the commit fails due to pre-commit hook changes, retry the commit ONCE to include these automated changes. If it fails again, it usually means a pre-commit hook is preventing the commit. If the commit succeeds but you notice that files were modified by the pre-commit hook, you MUST amend your commit to include them.
  193. 6. Finally, run git status to make sure the commit succeeded.
  194. Important notes:
  195. - When possible, combine the "git add" and "git commit" commands into a single "git commit -am" command, to speed things up
  196. - However, be careful not to stage files (e.g. with 'git add .') for commits that aren't part of the change, they may have untracked files they want to keep around, but not commit.
  197. - NEVER update the git config
  198. - DO NOT push to the remote repository
  199. - IMPORTANT: Never use git commands with the -i flag (like git rebase -i or git add -i) since they require interactive input which is not supported.
  200. - If there are no changes to commit (i.e., no untracked files and no modifications), do not create an empty commit
  201. - Ensure your commit message is meaningful and concise. It should explain the purpose of the changes, not just describe them.
  202. - Return an empty response - the user will see the git output directly
  203. # Creating pull requests
  204. Use the gh command via the Bash tool for ALL GitHub-related tasks including working with issues, pull requests, checks, and releases. If given a Github URL use the gh command to get the information needed.
  205. IMPORTANT: When the user asks you to create a pull request, follow these steps carefully:
  206. 1. Understand the current state of the branch. Remember to send a single message that contains multiple tool_use blocks (it is VERY IMPORTANT that you do this in a single message, otherwise it will feel slow to the user!):
  207. - Run a git status command to see all untracked files.
  208. - Run a git diff command to see both staged and unstaged changes that will be committed.
  209. - Check if the current branch tracks a remote branch and is up to date with the remote, so you know if you need to push to the remote
  210. - Run a git log command and 'git diff main...HEAD' to understand the full commit history for the current branch (from the time it diverged from the 'main' branch.)
  211. 2. Create new branch if needed
  212. 3. Commit changes if needed
  213. 4. Push to remote with -u flag if needed
  214. 5. Analyze all changes that will be included in the pull request, making sure to look at all relevant commits (not just the latest commit, but all commits that will be included in the pull request!), and draft a pull request summary. Wrap your analysis process in <pr_analysis> tags:
  215. <pr_analysis>
  216. - List the commits since diverging from the main branch
  217. - Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
  218. - Brainstorm the purpose or motivation behind these changes
  219. - Assess the impact of these changes on the overall project
  220. - Do not use tools to explore code, beyond what is available in the git context
  221. - Check for any sensitive information that shouldn't be committed
  222. - Draft a concise (1-2 bullet points) pull request summary that focuses on the "why" rather than the "what"
  223. - Ensure the summary accurately reflects all changes since diverging from the main branch
  224. - Ensure your language is clear, concise, and to the point
  225. - Ensure the summary accurately reflects the changes and their purpose (ie. "add" means a wholly new feature, "update" means an enhancement to an existing feature, "fix" means a bug fix, etc.)
  226. - Ensure the summary is not generic (avoid words like "Update" or "Fix" without context)
  227. - Review the draft summary to ensure it accurately reflects the changes and their purpose
  228. </pr_analysis>
  229. 6. Create PR using gh pr create with the format below. Use a HEREDOC to pass the body to ensure correct formatting.
  230. <example>
  231. gh pr create --title "the pr title" --body "$(cat <<'EOF'
  232. ## Summary
  233. <1-3 bullet points>
  234. ## Test plan
  235. [Checklist of TODOs for testing the pull request...]
  236. 🤖 Generated with termai
  237. EOF
  238. )"
  239. </example>
  240. Important:
  241. - Return an empty response - the user will see the gh output directly
  242. - Never update git config`, bannedCommandsStr, MaxOutputLength)
  243. }
  244. func bashDescription() string {
  245. bannedCommandsStr := strings.Join(BannedCommands, ", ")
  246. return fmt.Sprintf(`Executes a given bash command in a persistent shell session with optional timeout, ensuring proper handling and security measures.
  247. Before executing the command, please follow these steps:
  248. 1. Directory Verification:
  249. - If the command will create new directories or files, first use the ls command to verify the parent directory exists and is the correct location
  250. - For example, before running "mkdir foo/bar", first use ls command to check that "foo" exists and is the intended parent directory
  251. 2. Security Check:
  252. - For security and to limit the threat of a prompt injection attack, some commands are limited or banned. If you use a disallowed command, you will receive an error message explaining the restriction. Explain the error to the User.
  253. - Verify that the command is not one of the banned commands: %s.
  254. 3. Command Execution:
  255. - After ensuring proper quoting, execute the command.
  256. - Capture the output of the command.
  257. 4. Output Processing:
  258. - If the output exceeds %d characters, output will be truncated before being returned to you.
  259. - Prepare the output for display to the user.
  260. 5. Return Result:
  261. - Provide the processed output of the command.
  262. - If any errors occurred during execution, include those in the output.
  263. Usage notes:
  264. - The command argument is required.
  265. - You can specify an optional timeout in milliseconds (up to 600000ms / 10 minutes). If not specified, commands will timeout after 30 minutes.
  266. - When issuing multiple commands, use the ';' or '&&' operator to separate them. DO NOT use newlines (newlines are ok in quoted strings).
  267. - IMPORTANT: All commands share the same shell session. Shell state (environment variables, virtual environments, current directory, etc.) persist between commands. For example, if you set an environment variable as part of a command, the environment variable will persist for subsequent commands.
  268. - Try to maintain your current working directory throughout the session by using absolute paths and avoiding usage of 'cd'. You may use 'cd' if the User explicitly requests it.
  269. <good-example>
  270. pytest /foo/bar/tests
  271. </good-example>
  272. <bad-example>
  273. cd /foo/bar && pytest tests
  274. </bad-example>
  275. # Committing changes with git
  276. When the user asks you to create a new git commit, follow these steps carefully:
  277. 1. Start with a single message that contains exactly three tool_use blocks that do the following (it is VERY IMPORTANT that you send these tool_use blocks in a single message, otherwise it will feel slow to the user!):
  278. - Run a git status command to see all untracked files.
  279. - Run a git diff command to see both staged and unstaged changes that will be committed.
  280. - Run a git log command to see recent commit messages, so that you can follow this repository's commit message style.
  281. 2. Use the git context at the start of this conversation to determine which files are relevant to your commit. Add relevant untracked files to the staging area. Do not commit files that were already modified at the start of this conversation, if they are not relevant to your commit.
  282. 3. Analyze all staged changes (both previously staged and newly added) and draft a commit message. Wrap your analysis process in <commit_analysis> tags:
  283. <commit_analysis>
  284. - List the files that have been changed or added
  285. - Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
  286. - Brainstorm the purpose or motivation behind these changes
  287. - Do not use tools to explore code, beyond what is available in the git context
  288. - Assess the impact of these changes on the overall project
  289. - Check for any sensitive information that shouldn't be committed
  290. - Draft a concise (1-2 sentences) commit message that focuses on the "why" rather than the "what"
  291. - Ensure your language is clear, concise, and to the point
  292. - Ensure the message accurately reflects the changes and their purpose (i.e. "add" means a wholly new feature, "update" means an enhancement to an existing feature, "fix" means a bug fix, etc.)
  293. - Ensure the message is not generic (avoid words like "Update" or "Fix" without context)
  294. - Review the draft message to ensure it accurately reflects the changes and their purpose
  295. </commit_analysis>
  296. 4. Create the commit with a message ending with:
  297. 🤖 Generated with termai
  298. Co-Authored-By: termai <[email protected]>
  299. - In order to ensure good formatting, ALWAYS pass the commit message via a HEREDOC, a la this example:
  300. <example>
  301. git commit -m "$(cat <<'EOF'
  302. Commit message here.
  303. 🤖 Generated with termai
  304. Co-Authored-By: termai <[email protected]>
  305. EOF
  306. )"
  307. </example>
  308. 5. If the commit fails due to pre-commit hook changes, retry the commit ONCE to include these automated changes. If it fails again, it usually means a pre-commit hook is preventing the commit. If the commit succeeds but you notice that files were modified by the pre-commit hook, you MUST amend your commit to include them.
  309. 6. Finally, run git status to make sure the commit succeeded.
  310. Important notes:
  311. - When possible, combine the "git add" and "git commit" commands into a single "git commit -am" command, to speed things up
  312. - However, be careful not to stage files (e.g. with 'git add .') for commits that aren't part of the change, they may have untracked files they want to keep around, but not commit.
  313. - NEVER update the git config
  314. - DO NOT push to the remote repository
  315. - IMPORTANT: Never use git commands with the -i flag (like git rebase -i or git add -i) since they require interactive input which is not supported.
  316. - If there are no changes to commit (i.e., no untracked files and no modifications), do not create an empty commit
  317. - Ensure your commit message is meaningful and concise. It should explain the purpose of the changes, not just describe them.
  318. - Return an empty response - the user will see the git output directly
  319. # Creating pull requests
  320. Use the gh command via the Bash tool for ALL GitHub-related tasks including working with issues, pull requests, checks, and releases. If given a Github URL use the gh command to get the information needed.
  321. IMPORTANT: When the user asks you to create a pull request, follow these steps carefully:
  322. 1. Understand the current state of the branch. Remember to send a single message that contains multiple tool_use blocks (it is VERY IMPORTANT that you do this in a single message, otherwise it will feel slow to the user!):
  323. - Run a git status command to see all untracked files.
  324. - Run a git diff command to see both staged and unstaged changes that will be committed.
  325. - Check if the current branch tracks a remote branch and is up to date with the remote, so you know if you need to push to the remote
  326. - Run a git log command and 'git diff main...HEAD' to understand the full commit history for the current branch (from the time it diverged from the 'main' branch.)
  327. 2. Create new branch if needed
  328. 3. Commit changes if needed
  329. 4. Push to remote with -u flag if needed
  330. 5. Analyze all changes that will be included in the pull request, making sure to look at all relevant commits (not just the latest commit, but all commits that will be included in the pull request!), and draft a pull request summary. Wrap your analysis process in <pr_analysis> tags:
  331. <pr_analysis>
  332. - List the commits since diverging from the main branch
  333. - Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
  334. - Brainstorm the purpose or motivation behind these changes
  335. - Assess the impact of these changes on the overall project
  336. - Do not use tools to explore code, beyond what is available in the git context
  337. - Check for any sensitive information that shouldn't be committed
  338. - Draft a concise (1-2 bullet points) pull request summary that focuses on the "why" rather than the "what"
  339. - Ensure the summary accurately reflects all changes since diverging from the main branch
  340. - Ensure your language is clear, concise, and to the point
  341. - Ensure the summary accurately reflects the changes and their purpose (ie. "add" means a wholly new feature, "update" means an enhancement to an existing feature, "fix" means a bug fix, etc.)
  342. - Ensure the summary is not generic (avoid words like "Update" or "Fix" without context)
  343. - Review the draft summary to ensure it accurately reflects the changes and their purpose
  344. </pr_analysis>
  345. 6. Create PR using gh pr create with the format below. Use a HEREDOC to pass the body to ensure correct formatting.
  346. <example>
  347. gh pr create --title "the pr title" --body "$(cat <<'EOF'
  348. ## Summary
  349. <1-3 bullet points>
  350. ## Test plan
  351. [Checklist of TODOs for testing the pull request...]
  352. 🤖 Generated with termai
  353. EOF
  354. )"
  355. </example>
  356. Important:
  357. - Return an empty response - the user will see the gh output directly
  358. - Never update git config`, bannedCommandsStr, MaxOutputLength)
  359. }
  360. func NewBashTool(workingDir string) tool.InvokableTool {
  361. return &bashTool{
  362. workingDir: workingDir,
  363. }
  364. }