|
1 | 1 | (ns eca.features.tools.shell |
2 | 2 | (:require |
3 | 3 | [babashka.fs :as fs] |
4 | | - [clojure.java.shell :as shell] |
| 4 | + [babashka.process :as p] |
5 | 5 | [clojure.string :as string] |
6 | 6 | [eca.config :as config] |
7 | 7 | [eca.features.tools.util :as tools.util] |
8 | 8 | [eca.logger :as logger] |
9 | | - [eca.shared :as shared])) |
| 9 | + [eca.shared :as shared :refer [multi-str]])) |
10 | 10 |
|
11 | 11 | (set! *warn-on-reflection* true) |
12 | 12 |
|
13 | 13 | (def ^:private logger-tag "[TOOLS-SHELL]") |
14 | 14 |
|
15 | 15 | (defn ^:private shell-command [arguments {:keys [db config]}] |
16 | | - (let [command (get arguments "command") |
| 16 | + (let [command-args (get arguments "command") |
| 17 | + command (first (string/split command-args #"\s+")) |
17 | 18 | user-work-dir (get arguments "working_directory") |
18 | | - exclude-cmds (-> config :nativeTools :shell :excludeCommands set) |
19 | | - command-args (string/split command #" ")] |
| 19 | + exclude-cmds (-> config :nativeTools :shell :excludeCommands set)] |
20 | 20 | (or (tools.util/invalid-arguments arguments [["working_directory" #(or (nil? %) |
21 | 21 | (fs/exists? %)) "working directory $working_directory does not exist"] |
22 | | - ["commmand" (constantly (not (contains? exclude-cmds (first command-args)))) (format "Cannot run command '%s' because it is excluded by eca config." |
23 | | - (first command-args))]]) |
| 22 | + ["commmand" (constantly (not (contains? exclude-cmds command))) (format "Cannot run command '%s' because it is excluded by eca config." |
| 23 | + command)]]) |
24 | 24 | (let [work-dir (or (some-> user-work-dir fs/canonicalize str) |
25 | 25 | (some-> (:workspace-folders db) |
26 | 26 | first |
27 | 27 | :uri |
28 | 28 | shared/uri->filename) |
29 | 29 | (config/get-property "user.home")) |
30 | | - command-and-opts (concat [] command-args [:dir work-dir]) |
| 30 | + command-and-opts (concat ["bash -c"] command-args [:dir work-dir]) |
31 | 31 | _ (logger/debug logger-tag "Running command:" command-and-opts) |
32 | 32 | result (try |
33 | | - (apply shell/sh command-and-opts) |
| 33 | + (p/shell {:dir work-dir :out :string :err :string} "bash -c" command-args) |
34 | 34 | (catch Exception e |
35 | 35 | {:exit 1 :err (.getMessage e)})) |
36 | 36 | err (some-> (:err result) string/trim) |
|
51 | 51 |
|
52 | 52 | (def definitions |
53 | 53 | {"eca_shell_command" |
54 | | - {:description (str "Execute an arbitrary shell command and return the output. " |
55 | | - "Useful to run commands like `ls`, `git status`, etc.") |
| 54 | + {:description (multi-str "Execute an arbitrary shell command and return the output. |
| 55 | +1. Command Execution: |
| 56 | + - Always quote file paths that contain spaces with double quotes (e.g., cd \" path with spaces/file.txt \") |
| 57 | + - Examples of proper quoting: |
| 58 | + - cd \"/Users/name/My Documents\" (correct) |
| 59 | + - cd /Users/name/My Documents (incorrect - will fail) |
| 60 | + - python \"/path/with spaces/script.py\" (correct) |
| 61 | + - python /path/with spaces/script.py (incorrect - will fail) |
| 62 | + - After ensuring proper quoting, execute the command. |
| 63 | + - Capture the output of the command. |
| 64 | + - VERY IMPORTANT: You MUST avoid using search command `grep`. Instead use eca_grep to search. You MUST avoid read tools like `cat`, `head`, `tail`, and `ls`, and use eca_read_file or eca_directory_tree. |
| 65 | +
|
| 66 | +# Committing changes with git |
| 67 | +
|
| 68 | +When the user asks you to create a new git commit, follow these steps carefully: |
| 69 | +
|
| 70 | +1.: |
| 71 | + - Run a git status command to see all untracked files. |
| 72 | + - Run a git diff command to see both staged and unstaged changes that will be committed. |
| 73 | + - Run a git log command to see recent commit messages, so that you can follow this repository's commit message style. |
| 74 | +
|
| 75 | +2. Analyze all staged changes (both previously staged and newly added) and draft a commit message. Wrap your analysis process in <commit_analysis> tags: |
| 76 | +
|
| 77 | +<commit_analysis> |
| 78 | +- List the files that have been changed or added |
| 79 | +- Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.) |
| 80 | +- Brainstorm the purpose or motivation behind these changes |
| 81 | +- Assess the impact of these changes on the overall project |
| 82 | +- Check for any sensitive information that shouldn't be committed |
| 83 | +- Draft a concise (1-2 sentences) commit message that focuses on the \"why\" rather than the \"what\" |
| 84 | +- Ensure your language is clear, concise, and to the point |
| 85 | +- 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.) |
| 86 | +- Ensure the message is not generic (avoid words like \"Update\" or \"Fix\" without context) |
| 87 | +- Review the draft message to ensure it accurately reflects the changes and their purpose |
| 88 | +</commit_analysis> |
| 89 | +
|
| 90 | +3.: |
| 91 | + - Add relevant untracked files to the staging area. |
| 92 | + - Create the commit with a message ending with: |
| 93 | + 🤖 Generated with [eca](https://eca.dev) |
| 94 | +
|
| 95 | + Co-Authored-By: eca <[email protected]> |
| 96 | + - Run git status to make sure the commit succeeded. |
| 97 | +
|
| 98 | +4. 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. |
| 99 | +
|
| 100 | +Important notes: |
| 101 | +- Use the git context at the start of this conversation to determine which files are relevant to your commit. Be careful not to stage and commit files (e.g. with `git add .`) that aren't relevant to your commit. |
| 102 | +- NEVER update the git config |
| 103 | +- DO NOT run additional commands to read or explore code, beyond what is available in the git context |
| 104 | +- DO NOT push to the remote repository |
| 105 | +- 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. |
| 106 | +- If there are no changes to commit (i.e., no untracked files and no modifications), do not create an empty commit |
| 107 | +- Ensure your commit message is meaningful and concise. It should explain the purpose of the changes, not just describe them. |
| 108 | +- Return an empty response - the user will see the git output directly |
| 109 | +- In order to ensure good formatting, ALWAYS pass the commit message via a HEREDOC, a la this example: |
| 110 | +<example> |
| 111 | +git commit -m \"$(cat <<'EOF' |
| 112 | + Commit message here. |
| 113 | +
|
| 114 | + 🤖 Generated with [opencode](https://opencode.ai) |
| 115 | +
|
| 116 | + Co-Authored-By: opencode <[email protected]> |
| 117 | + EOF |
| 118 | + )\" |
| 119 | +</example> |
| 120 | +
|
| 121 | +# Creating pull requests |
| 122 | +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. |
| 123 | +
|
| 124 | +IMPORTANT: When the user asks you to create a pull request, follow these steps carefully: |
| 125 | +
|
| 126 | +1. In order to understand the current state of the branch since it diverged from the main branch: |
| 127 | + - Run a git status command to see all untracked files |
| 128 | + - Run a git diff command to see both staged and unstaged changes that will be committed |
| 129 | + - 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 |
| 130 | + - 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) |
| 131 | +
|
| 132 | +2. 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: |
| 133 | +
|
| 134 | +<pr_analysis> |
| 135 | +- List the commits since diverging from the main branch |
| 136 | +- Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.) |
| 137 | +- Brainstorm the purpose or motivation behind these changes |
| 138 | +- Assess the impact of these changes on the overall project |
| 139 | +- Do not use tools to explore code, beyond what is available in the git context |
| 140 | +- Check for any sensitive information that shouldn't be committed |
| 141 | +- Draft a concise (1-2 bullet points) pull request summary that focuses on the \"why\" rather than the \"what\" |
| 142 | +- Ensure the summary accurately reflects all changes since diverging from the main branch |
| 143 | +- Ensure your language is clear, concise, and to the point |
| 144 | +- 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.) |
| 145 | +- Ensure the summary is not generic (avoid words like \"Update\" or \"Fix\" without context) |
| 146 | +- Review the draft summary to ensure it accurately reflects the changes and their purpose |
| 147 | +</pr_analysis> |
| 148 | +
|
| 149 | +3. ALWAYS run the following commands in parallel: |
| 150 | + - Create new branch if needed |
| 151 | + - Push to remote with -u flag if needed |
| 152 | + - Create PR using gh pr create with the format below. Use a HEREDOC to pass the body to ensure correct formatting. |
| 153 | +<example> |
| 154 | +gh pr create --title \"the pr title\" --body \"$(cat <<'EOF' |
| 155 | +## Summary |
| 156 | +<1-3 bullet points> |
| 157 | +
|
| 158 | +## Test plan |
| 159 | +[Checklist of TODOs for testing the pull request...] |
| 160 | +
|
| 161 | +🤖 Generated with [opencode](https://opencode.ai) |
| 162 | +EOF |
| 163 | +)\" |
| 164 | +</example> |
| 165 | +
|
| 166 | +Important: |
| 167 | +- NEVER update the git config |
| 168 | +- Return the PR URL when you're done, so the user can see it |
| 169 | +
|
| 170 | +# Other common operations |
| 171 | +- View comments on a Github PR: gh api repos/foo/bar/pulls/123/comments |
| 172 | +") |
56 | 173 | :parameters {:type "object" |
57 | 174 | :properties {"command" {:type "string" |
58 | 175 | :description "The shell command to execute."} |
|
0 commit comments