refactor(core): 移除 BASH_TOOL_EXTRA_NOTES 和 GIT_COMMIT_AND_PR_CREATION_INSTRUCTION
- 从模板引擎中移除这两个函数的注册 - 从 bash 工具描述中移除对应的模板变量 - 删除不再需要的 git-instructions.txt 文件 - 更新单元测试,移除相关测试用例
This commit is contained in:
@@ -21,7 +21,6 @@ import type {
|
||||
} from './types.js';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import {
|
||||
CUSTOM_TIMEOUT_MS,
|
||||
MAX_TIMEOUT_MS,
|
||||
@@ -432,44 +431,6 @@ function camelToUpperSnake(str: string): string {
|
||||
return str.replace(/[A-Z]/g, (c) => `_${c}`).toUpperCase();
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 模板函数加载器
|
||||
// ============================================
|
||||
|
||||
// 缓存 Git 指令内容
|
||||
let gitInstructionsCache: string | null = null;
|
||||
|
||||
/**
|
||||
* 加载 Git 操作指令
|
||||
* 从 git-instructions.txt 文件读取内容,使用缓存避免重复读取
|
||||
*/
|
||||
function loadGitInstructions(): string {
|
||||
if (gitInstructionsCache === null) {
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const filePath = path.join(
|
||||
__dirname,
|
||||
'..',
|
||||
'tools',
|
||||
'descriptions',
|
||||
'shell',
|
||||
'git-instructions.txt'
|
||||
);
|
||||
try {
|
||||
gitInstructionsCache = fs.readFileSync(filePath, 'utf-8').trim();
|
||||
} catch {
|
||||
gitInstructionsCache = '';
|
||||
}
|
||||
}
|
||||
return gitInstructionsCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除 Git 指令缓存(用于测试)
|
||||
*/
|
||||
export function clearGitInstructionsCache(): void {
|
||||
gitInstructionsCache = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建工具描述渲染上下文
|
||||
*
|
||||
@@ -484,8 +445,6 @@ export function clearGitInstructionsCache(): void {
|
||||
* - ${CUSTOM_TIMEOUT_MS()} - 最大超时时间(毫秒)
|
||||
* - ${MAX_TIMEOUT_MS()} - 默认超时时间(毫秒)
|
||||
* - ${MAX_OUTPUT_CHARS()} - 输出截断限制(字符数)
|
||||
* - ${BASH_TOOL_EXTRA_NOTES()} - Bash 工具额外说明
|
||||
* - ${GIT_COMMIT_AND_PR_CREATION_INSTRUCTION()} - Git 操作说明
|
||||
*/
|
||||
export function createToolDescriptionContext(
|
||||
overrides?: Partial<PromptContext>
|
||||
@@ -506,8 +465,6 @@ export function createToolDescriptionContext(
|
||||
CUSTOM_TIMEOUT_MS: () => CUSTOM_TIMEOUT_MS,
|
||||
MAX_TIMEOUT_MS: () => MAX_TIMEOUT_MS,
|
||||
MAX_OUTPUT_CHARS: () => MAX_OUTPUT_CHARS,
|
||||
BASH_TOOL_EXTRA_NOTES: () => '', // 暂时为空,可通过 overrides 自定义
|
||||
GIT_COMMIT_AND_PR_CREATION_INSTRUCTION: loadGitInstructions,
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
@@ -24,7 +24,6 @@ Usage notes:
|
||||
- It is very helpful if you write a clear, concise description of what this command does in 5-10 words.
|
||||
- If the output exceeds ${MAX_OUTPUT_CHARS()} characters, output will be truncated before being returned to you.
|
||||
- You can use the `run_in_background` parameter to run the command in the background, which allows you to continue working while the command runs. You can monitor the output using the ${BASH_TOOL_NAME} tool as it becomes available. You do not need to use '&' at the end of the command when using this parameter.
|
||||
${BASH_TOOL_EXTRA_NOTES()}
|
||||
- Avoid using Bash with the `find`, `grep`, `cat`, `head`, `tail`, `sed`, `awk`, or `echo` commands, unless explicitly instructed or when these commands are truly necessary for the task. Instead, always prefer using the dedicated tools for these commands:
|
||||
- File search: Use ${GLOB_TOOL_NAME} (NOT find or ls)
|
||||
- Content search: Use ${GREP_TOOL_NAME} (NOT grep or rg)
|
||||
@@ -44,5 +43,3 @@ Usage notes:
|
||||
<bad-example>
|
||||
cd /foo/bar && pytest tests
|
||||
</bad-example>
|
||||
|
||||
${GIT_COMMIT_AND_PR_CREATION_INSTRUCTION()}
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
# Committing changes with git
|
||||
|
||||
Only create commits when requested by the user. If unclear, ask first. When the user asks you to create a new git commit, follow these steps carefully:
|
||||
|
||||
Git Safety Protocol:
|
||||
- NEVER update the git config
|
||||
- NEVER run destructive/irreversible git commands (like push --force, hard reset, etc) unless the user explicitly requests them
|
||||
- NEVER skip hooks (--no-verify, --no-gpg-sign, etc) unless the user explicitly requests it
|
||||
- NEVER run force push to main/master, warn the user if they request it
|
||||
- Avoid git commit --amend. ONLY use --amend when either (1) user explicitly requested amend OR (2) adding edits from pre-commit hook (additional instructions below)
|
||||
- Before amending: ALWAYS check authorship (git log -1 --format='%an %ae')
|
||||
- NEVER commit changes unless the user explicitly asks you to. It is VERY IMPORTANT to only commit when explicitly asked, otherwise the user will feel that you are being too proactive.
|
||||
|
||||
1. You can call multiple tools in a single response. When multiple independent pieces of information are requested and all commands are likely to succeed, run multiple tool calls in parallel for optimal performance. run the following bash commands in parallel, each using the Bash tool:
|
||||
- Run a git status command to see all untracked files.
|
||||
- Run a git diff command to see both staged and unstaged changes that will be committed.
|
||||
- Run a git log command to see recent commit messages, so that you can follow this repository's commit message style.
|
||||
2. Analyze all staged changes (both previously staged and newly added) and draft a commit message:
|
||||
- Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.). 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.).
|
||||
- Do not commit files that likely contain secrets (.env, credentials.json, etc). Warn the user if they specifically request to commit those files
|
||||
- Draft a concise (1-2 sentences) commit message that focuses on the "why" rather than the "what"
|
||||
- Ensure it accurately reflects the changes and their purpose
|
||||
3. You can call multiple tools in a single response. When multiple independent pieces of information are requested and all commands are likely to succeed, run multiple tool calls in parallel for optimal performance. run the following commands:
|
||||
- Add relevant untracked files to the staging area.
|
||||
- Create the commit with a message ending with:
|
||||
- Run git status after the commit completes to verify success.
|
||||
Note: git status depends on the commit completing, so run it sequentially after the commit.
|
||||
4. If the commit fails due to pre-commit hook changes, retry ONCE. If it succeeds but files were modified by the hook, verify it's safe to amend:
|
||||
- Check HEAD commit: git log -1 --format='[%h] (%an <%ae>) %s'. VERIFY it matches your commit
|
||||
- Check not pushed: git status shows "Your branch is ahead"
|
||||
- If both true: amend your commit. Otherwise: create NEW commit (never amend other developers' commits)
|
||||
|
||||
Important notes:
|
||||
- NEVER run additional commands to read or explore code, besides git bash commands
|
||||
- NEVER use the TodoWrite or Task tools
|
||||
- DO NOT push to the remote repository unless the user explicitly asks you to do so
|
||||
- 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.
|
||||
- If there are no changes to commit (i.e., no untracked files and no modifications), do not create an empty commit
|
||||
- In order to ensure good formatting, ALWAYS pass the commit message via a HEREDOC, a la this example:
|
||||
<example>
|
||||
git commit -m "$(cat <<'EOF'
|
||||
Commit message here.
|
||||
EOF
|
||||
)"
|
||||
</example>
|
||||
|
||||
# Creating pull requests
|
||||
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.
|
||||
|
||||
IMPORTANT: When the user asks you to create a pull request, follow these steps carefully:
|
||||
|
||||
1. You can call multiple tools in a single response. When multiple independent pieces of information are requested and all commands are likely to succeed, run multiple tool calls in parallel for optimal performance. run the following bash commands in parallel using the Bash tool, in order to understand the current state of the branch since it diverged from the main branch:
|
||||
- Run a git status command to see all untracked files
|
||||
- Run a git diff command to see both staged and unstaged changes that will be committed
|
||||
- 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
|
||||
- Run a git log command and `git diff [base-branch]...HEAD` to understand the full commit history for the current branch (from the time it diverged from the base branch)
|
||||
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
|
||||
3. You can call multiple tools in a single response. When multiple independent pieces of information are requested and all commands are likely to succeed, run multiple tool calls in parallel for optimal performance. run the following commands in parallel:
|
||||
- Create new branch if needed
|
||||
- Push to remote with -u flag if needed
|
||||
- Create PR using gh pr create with the format below. Use a HEREDOC to pass the body to ensure correct formatting.
|
||||
<example>
|
||||
gh pr create --title "the pr title" --body "$(cat <<'EOF'
|
||||
## Summary
|
||||
<1-3 bullet points>
|
||||
|
||||
## Test plan
|
||||
[Bulleted markdown checklist of TODOs for testing the pull request...]
|
||||
EOF
|
||||
)"
|
||||
</example>
|
||||
|
||||
Important:
|
||||
- DO NOT use the TodoWrite or Task tools
|
||||
- Return the PR URL when you're done, so the user can see it
|
||||
|
||||
# Other common operations
|
||||
- View comments on a Github PR: gh api repos/foo/bar/pulls/123/comments
|
||||
@@ -1,9 +1,8 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
renderTemplate,
|
||||
renderPromptTemplate,
|
||||
createToolDescriptionContext,
|
||||
clearGitInstructionsCache,
|
||||
} from '../../../src/template/renderer.js';
|
||||
import type { ExtendedTemplateContext } from '../../../src/template/types.js';
|
||||
import {
|
||||
@@ -160,14 +159,6 @@ describe('Template Renderer - 模板渲染器', () => {
|
||||
});
|
||||
|
||||
describe('createToolDescriptionContext - 工具描述上下文', () => {
|
||||
beforeEach(() => {
|
||||
clearGitInstructionsCache();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
clearGitInstructionsCache();
|
||||
});
|
||||
|
||||
it('包含工具名称变量', () => {
|
||||
const context = createToolDescriptionContext();
|
||||
expect(context.custom?.BASH_TOOL_NAME).toBe('bash');
|
||||
@@ -181,8 +172,6 @@ describe('createToolDescriptionContext - 工具描述上下文', () => {
|
||||
expect(typeof context.__functions__.CUSTOM_TIMEOUT_MS).toBe('function');
|
||||
expect(typeof context.__functions__.MAX_TIMEOUT_MS).toBe('function');
|
||||
expect(typeof context.__functions__.MAX_OUTPUT_CHARS).toBe('function');
|
||||
expect(typeof context.__functions__.BASH_TOOL_EXTRA_NOTES).toBe('function');
|
||||
expect(typeof context.__functions__.GIT_COMMIT_AND_PR_CREATION_INSTRUCTION).toBe('function');
|
||||
});
|
||||
|
||||
it('函数返回正确的配置值', () => {
|
||||
@@ -191,20 +180,6 @@ describe('createToolDescriptionContext - 工具描述上下文', () => {
|
||||
expect(context.__functions__.MAX_TIMEOUT_MS()).toBe(MAX_TIMEOUT_MS);
|
||||
expect(context.__functions__.MAX_OUTPUT_CHARS()).toBe(MAX_OUTPUT_CHARS);
|
||||
});
|
||||
|
||||
it('BASH_TOOL_EXTRA_NOTES 默认返回空字符串', () => {
|
||||
const context = createToolDescriptionContext();
|
||||
expect(context.__functions__.BASH_TOOL_EXTRA_NOTES()).toBe('');
|
||||
});
|
||||
|
||||
it('GIT_COMMIT_AND_PR_CREATION_INSTRUCTION 返回 Git 指令', () => {
|
||||
const context = createToolDescriptionContext();
|
||||
const gitInstructions = context.__functions__.GIT_COMMIT_AND_PR_CREATION_INSTRUCTION();
|
||||
expect(typeof gitInstructions).toBe('string');
|
||||
// 验证包含关键内容
|
||||
expect(gitInstructions).toContain('Committing changes with git');
|
||||
expect(gitInstructions).toContain('Creating pull requests');
|
||||
});
|
||||
});
|
||||
|
||||
describe('renderPromptTemplate - 提示词模板渲染', () => {
|
||||
|
||||
Reference in New Issue
Block a user