forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-terminal-command.ts
More file actions
182 lines (156 loc) · 8.2 KB
/
run-terminal-command.ts
File metadata and controls
182 lines (156 loc) · 8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import z from 'zod/v4'
import { $getNativeToolCallExampleString, jsonToolResultSchema } from '../utils'
import type { $ToolParams } from '../../constants'
export const terminalCommandOutputSchema = z.union([
z.object({
command: z.string(),
startingCwd: z.string().optional(),
message: z.string().optional(),
stderr: z.string().optional(),
stdout: z.string().optional(),
exitCode: z.number().optional(),
}),
z.object({
command: z.string(),
startingCwd: z.string().optional(),
message: z.string().optional(),
stderr: z.string().optional(),
stdoutOmittedForLength: z.literal(true),
exitCode: z.number().optional(),
}),
z.object({
command: z.string(),
processId: z.number(),
backgroundProcessStatus: z.enum(['running', 'completed', 'error']),
}),
z.object({
command: z.string(),
errorMessage: z.string(),
}),
])
export const gitCommitGuidePrompt = `
### Using git to commit changes
When the user requests a new git commit, please follow these steps closely:
1. **Run two run_terminal_command tool calls:**
- Run \`git diff\` to review both staged and unstaged modifications.
- Run \`git log\` to check recent commit messages, ensuring consistency with this repository's style.
2. **Select relevant files to include in the commit:**
Use the git context established at the start of this conversation to decide which files are pertinent to the changes. Stage any new untracked files that are relevant, but avoid committing previously modified files (from the beginning of the conversation) unless they directly relate to this commit.
3. **Analyze the staged changes and compose a commit message:**
Enclose your analysis in <commit_analysis> tags. Within these tags, you should:
- Note which files have been altered or added.
- Categorize the nature of the changes (e.g., new feature, fix, refactor, documentation, etc.).
- Consider the purpose or motivation behind the alterations.
- Refrain from using tools to inspect code beyond what is presented in the git context.
- Evaluate the overall impact on the project.
- Check for sensitive details that should not be committed.
- Draft a concise, one- to two-sentence commit message focusing on the “why” rather than the “what.”
- Use precise, straightforward language that accurately represents the changes.
- Ensure the message provides clarity—avoid generic or vague terms like “Update” or “Fix” without context.
- Revisit your draft to confirm it truly reflects the changes and their intention.
4. **Create the commit, ending with this specific footer:**
\`\`\`
Generated with Codebuff 🤖
Co-Authored-By: Codebuff <noreply@codebuff.com>
\`\`\`
**For bash/PowerShell:** Use multi-line \`-m\` format:
\`\`\`
git commit -m "Your commit message here.
🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>"
\`\`\`
**For cmd.exe (Windows Command Prompt):** Use the file-based approach to avoid escaping issues:
1. Use \`write_file\` to create \`.codebuff-commit-msg.txt\` with the full commit message
2. Run \`git commit -F .codebuff-commit-msg.txt\`
3. Run \`del .codebuff-commit-msg.txt\` to clean up
This file-based approach completely avoids cmd.exe's complex escaping rules for quotes, newlines, and special characters.
**Important:** Do NOT use HEREDOC syntax (\`<<'EOF'\`) - it only works in bash and will fail on Windows.
**Important details**
- When feasible, use a single \`git commit -am\` command to add and commit together, but do not accidentally stage unrelated files.
- Never alter the git config.
- Do not push to the remote repository.
- Avoid using interactive flags (e.g., \`-i\`) that require unsupported interactive input.
- Do not create an empty commit if there are no changes.
- Make sure your commit message is concise yet descriptive, focusing on the intention behind the changes rather than merely describing them.
`
const toolName = 'run_terminal_command'
const endsAgentStep = true
const inputSchema = z
.object({
// Can be empty to use it for a timeout.
command: z
.string()
.min(1, 'Command cannot be empty')
.describe(`CLI command valid for user's OS.`),
process_type: z
.enum(['SYNC', 'BACKGROUND'])
.default('SYNC')
.describe(
`Either SYNC (waits, returns output) or BACKGROUND (runs in background). Default SYNC`,
),
cwd: z
.string()
.optional()
.describe(
`The working directory to run the command in. Default is the project root.`,
),
timeout_seconds: z
.number()
.default(30)
.optional()
.describe(
`Set to -1 for no timeout. Does not apply for BACKGROUND commands. Default 30`,
),
})
.describe(
`Execute a CLI command from the **project root** (different from the user's cwd).`,
)
const description = `
Stick to these use cases:
1. Typechecking the project or running build (e.g., "npm run build"). Reading the output can help you edit code to fix build errors. If possible, use an option that performs checks but doesn't emit files, e.g. \`tsc --noEmit\`.
2. Running tests (e.g., "npm test"). Reading the output can help you edit code to fix failing tests. Or, you could write new unit tests and then run them.
3. Moving, renaming, or deleting files and directories. These actions can be vital for refactoring requests. Use commands like \`mv\`/\`move\` or \`rm\`/\`del\`.
Most likely, you should ask for permission for any other type of command you want to run. If asking for permission, show the user the command you want to run using \`\`\` tags and *do not* use the tool call format, e.g.:
\`\`\`bash
git branch -D foo
\`\`\`
DO NOT do any of the following:
1. Run commands that can modify files outside of the project directory, install packages globally, install virtual environments, or have significant side effects outside of the project directory, unless you have explicit permission from the user. Treat anything outside of the project directory as read-only.
2. Run \`git push\` because it can break production (!) if the user was not expecting it. Don't run \`git commit\`, \`git rebase\`, or related commands unless you get explicit permission. If a user asks to commit changes, you can do so, but you should not invoke any further git commands beyond the git commit command.
3. Run scripts without asking. Especially don't run scripts that could run against the production environment or have permanent effects without explicit permission from the user.
4. Be careful with any command that has big or irreversible effects. Anything that touches a production environment, servers, the database, or other systems that could be affected by a command should be run with explicit permission from the user.
5. Use the run_terminal_command tool to create or edit files. Do not use \`cat\` or \`echo\` to create or edit files. You should instead use other tools for creating or editing files.
6. Use the wrong package manager for the project. For example, if the project uses \`pnpm\` or \`bun\` or \`yarn\`, you should not use \`npm\`. Similarly not everyone uses \`pip\` for python, etc.
Do:
- If there's an opportunity to use "-y" or "--yes" flags, use them. Any command that prompts for confirmation will hang if you don't use the flags.
Notes:
- If the user references a specific file, it could be either from their cwd or from the project root. You **must** determine which they are referring to (either infer or ask). Then, you must specify the path relative to the project root (or use the cwd parameter)
- Commands can succeed without giving any output, e.g. if no type errors were found.
${gitCommitGuidePrompt}
Example:
${$getNativeToolCallExampleString({
toolName,
inputSchema,
input: {
command: 'echo "hello world"',
},
endsAgentStep,
})}
${$getNativeToolCallExampleString({
toolName,
inputSchema,
input: {
command: `git commit -m "Your commit message here.
🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>"`,
},
endsAgentStep,
})}
`.trim()
export const runTerminalCommandParams = {
toolName,
endsAgentStep,
description,
inputSchema,
outputSchema: jsonToolResultSchema(terminalCommandOutputSchema),
} satisfies $ToolParams