feat(core): 扩展模板引擎支持函数调用和算术运算
- 新增模板函数调用语法 ${FUNC_NAME()}
- 新增除法算术运算 ${value/divisor}
- 添加 bash 工具配置常量 (CUSTOM_TIMEOUT_MS 等)
- 更新 bash 工具描述使用动态模板格式
- 添加 Git 操作指令文本 (commit/PR 说明)
- 添加模板渲染器单元测试 (31 个测试用例)
This commit is contained in:
@@ -0,0 +1,12 @@
|
|||||||
|
/**
|
||||||
|
* Bash 工具配置常量
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** 最大超时时间(毫秒) - 10 分钟 */
|
||||||
|
export const CUSTOM_TIMEOUT_MS = 600000;
|
||||||
|
|
||||||
|
/** 默认超时时间(毫秒) - 2 分钟 */
|
||||||
|
export const MAX_TIMEOUT_MS = 120000;
|
||||||
|
|
||||||
|
/** 输出截断限制(字符数) */
|
||||||
|
export const MAX_OUTPUT_CHARS = 30000;
|
||||||
@@ -1 +1,2 @@
|
|||||||
export * from './paths.js';
|
export * from './paths.js';
|
||||||
|
export * from './bash-tool.js';
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
* - ${variable} - 简单变量替换
|
* - ${variable} - 简单变量替换
|
||||||
* - ${obj.prop} - 嵌套属性访问
|
* - ${obj.prop} - 嵌套属性访问
|
||||||
* - ${condition ? "trueValue" : "falseValue"} - 条件表达式
|
* - ${condition ? "trueValue" : "falseValue"} - 条件表达式
|
||||||
|
* - ${FUNC_NAME()} - 函数调用(无参数)
|
||||||
|
* - ${value / divisor} - 算术运算(除法)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
@@ -14,9 +16,17 @@ import type {
|
|||||||
PromptContext,
|
PromptContext,
|
||||||
PromptTemplate,
|
PromptTemplate,
|
||||||
ToolNameMapping,
|
ToolNameMapping,
|
||||||
|
ExtendedTemplateContext,
|
||||||
|
TemplateFunctionRegistry,
|
||||||
} from './types.js';
|
} from './types.js';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
|
import {
|
||||||
|
CUSTOM_TIMEOUT_MS,
|
||||||
|
MAX_TIMEOUT_MS,
|
||||||
|
MAX_OUTPUT_CHARS,
|
||||||
|
} from '../constants/bash-tool.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取嵌套属性值
|
* 获取嵌套属性值
|
||||||
@@ -125,6 +135,29 @@ function evaluateExpression(
|
|||||||
: evaluateExpression(falseValue, context, options);
|
: evaluateExpression(falseValue, context, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理算术运算(除法): value / divisor 或 FUNC() / divisor
|
||||||
|
// 注意:必须在函数调用之前处理,因为 FUNC()/60000 需要先拆分
|
||||||
|
const divMatch = expr.match(/^(.+?)\s*\/\s*(\d+)$/);
|
||||||
|
if (divMatch) {
|
||||||
|
const left = evaluateExpression(divMatch[1].trim(), context, options);
|
||||||
|
const divisor = parseInt(divMatch[2], 10);
|
||||||
|
const leftNum = parseFloat(left);
|
||||||
|
if (!isNaN(leftNum) && divisor !== 0) {
|
||||||
|
return String(Math.floor(leftNum / divisor));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理函数调用: FUNC_NAME()
|
||||||
|
const funcMatch = expr.match(/^([A-Za-z_][A-Za-z0-9_]*)\(\)$/);
|
||||||
|
if (funcMatch) {
|
||||||
|
const funcName = funcMatch[1];
|
||||||
|
const functions = (context as ExtendedTemplateContext).__functions__;
|
||||||
|
if (functions && typeof functions[funcName] === 'function') {
|
||||||
|
return String(functions[funcName]());
|
||||||
|
}
|
||||||
|
// 未找到函数,继续尝试作为变量处理
|
||||||
|
}
|
||||||
|
|
||||||
// 处理字符串字面量: "string" 或 'string' 或 `string`
|
// 处理字符串字面量: "string" 或 'string' 或 `string`
|
||||||
const stringMatch = expr.match(/^["'`]([\s\S]*)["'`]$/);
|
const stringMatch = expr.match(/^["'`]([\s\S]*)["'`]$/);
|
||||||
if (stringMatch) {
|
if (stringMatch) {
|
||||||
@@ -399,6 +432,44 @@ function camelToUpperSnake(str: string): string {
|
|||||||
return str.replace(/[A-Z]/g, (c) => `_${c}`).toUpperCase();
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建工具描述渲染上下文
|
* 创建工具描述渲染上下文
|
||||||
*
|
*
|
||||||
@@ -408,8 +479,17 @@ function camelToUpperSnake(str: string): string {
|
|||||||
* 支持的变量格式:
|
* 支持的变量格式:
|
||||||
* - ${tools.grep} - 小写驼峰形式(项目内部风格)
|
* - ${tools.grep} - 小写驼峰形式(项目内部风格)
|
||||||
* - ${GREP_TOOL_NAME} - 大写下划线形式(Claude Code 官方风格)
|
* - ${GREP_TOOL_NAME} - 大写下划线形式(Claude Code 官方风格)
|
||||||
|
*
|
||||||
|
* 支持的函数调用:
|
||||||
|
* - ${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>): PromptContext {
|
export function createToolDescriptionContext(
|
||||||
|
overrides?: Partial<PromptContext>
|
||||||
|
): PromptContext & { __functions__: TemplateFunctionRegistry } {
|
||||||
const base = createDefaultContext(overrides);
|
const base = createDefaultContext(overrides);
|
||||||
|
|
||||||
// 将 tools 映射转为大写变量放入 custom
|
// 将 tools 映射转为大写变量放入 custom
|
||||||
@@ -421,8 +501,18 @@ export function createToolDescriptionContext(overrides?: Partial<PromptContext>)
|
|||||||
toolVars[upperKey] = value;
|
toolVars[upperKey] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 注册模板函数
|
||||||
|
const functions: TemplateFunctionRegistry = {
|
||||||
|
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 {
|
return {
|
||||||
...base,
|
...base,
|
||||||
custom: { ...base.custom, ...toolVars },
|
custom: { ...base.custom, ...toolVars },
|
||||||
|
__functions__: functions,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,18 @@
|
|||||||
* 支持动态变量替换和条件表达式的模板系统
|
* 支持动态变量替换和条件表达式的模板系统
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 模板函数类型
|
||||||
|
* 无参数函数,返回字符串或数字
|
||||||
|
*/
|
||||||
|
export type TemplateFunction = () => string | number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 模板函数注册表
|
||||||
|
* 用于存储可在模板中调用的函数
|
||||||
|
*/
|
||||||
|
export type TemplateFunctionRegistry = Record<string, TemplateFunction>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 模板上下文 - 运行时可用的变量
|
* 模板上下文 - 运行时可用的变量
|
||||||
*
|
*
|
||||||
@@ -11,6 +23,16 @@
|
|||||||
*/
|
*/
|
||||||
export type TemplateContext = Record<string, unknown>;
|
export type TemplateContext = Record<string, unknown>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 扩展的模板上下文 - 支持函数调用
|
||||||
|
*
|
||||||
|
* 除了普通变量外,还支持通过 __functions__ 注册可调用的函数
|
||||||
|
*/
|
||||||
|
export interface ExtendedTemplateContext extends Record<string, unknown> {
|
||||||
|
/** 注册的模板函数 */
|
||||||
|
__functions__?: TemplateFunctionRegistry;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 模板定义
|
* 模板定义
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1 +1,48 @@
|
|||||||
执行 bash 命令。可以用于运行系统命令、安装包、git 操作等。
|
Executes a given bash command in a persistent shell session with optional timeout, ensuring proper handling and security measures.
|
||||||
|
|
||||||
|
IMPORTANT: This tool is for terminal operations like git, npm, docker, etc. DO NOT use it for file operations (reading, writing, editing, searching, finding files) - use the specialized tools for this instead.
|
||||||
|
|
||||||
|
Before executing the command, please follow these steps:
|
||||||
|
|
||||||
|
1. Directory Verification:
|
||||||
|
- If the command will create new directories or files, first use `ls` to verify the parent directory exists and is the correct location
|
||||||
|
- For example, before running "mkdir foo/bar", first use `ls foo` to check that "foo" exists and is the intended parent directory
|
||||||
|
|
||||||
|
2. Command Execution:
|
||||||
|
- Always quote file paths that contain spaces with double quotes (e.g., cd "path with spaces/file.txt")
|
||||||
|
- Examples of proper quoting:
|
||||||
|
- cd "/Users/name/My Documents" (correct)
|
||||||
|
- cd /Users/name/My Documents (incorrect - will fail)
|
||||||
|
- python "/path/with spaces/script.py" (correct)
|
||||||
|
- python /path/with spaces/script.py (incorrect - will fail)
|
||||||
|
- After ensuring proper quoting, execute the command.
|
||||||
|
- Capture the output of the command.
|
||||||
|
|
||||||
|
Usage notes:
|
||||||
|
- The command argument is required.
|
||||||
|
- You can specify an optional timeout in milliseconds (up to ${CUSTOM_TIMEOUT_MS()}ms / ${CUSTOM_TIMEOUT_MS()/60000} minutes). If not specified, commands will timeout after ${MAX_TIMEOUT_MS()}ms (${MAX_TIMEOUT_MS()/60000} minutes).
|
||||||
|
- 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)
|
||||||
|
- Read files: Use ${READ_TOOL_NAME} (NOT cat/head/tail)
|
||||||
|
- Edit files: Use ${EDIT_TOOL_NAME} (NOT sed/awk)
|
||||||
|
- Write files: Use ${WRITE_TOOL_NAME} (NOT echo >/cat <<EOF)
|
||||||
|
- Communication: Output text directly (NOT echo/printf)
|
||||||
|
- When issuing multiple commands:
|
||||||
|
- If the commands are independent and can run in parallel, make multiple ${BASH_TOOL_NAME} tool calls in a single message. For example, if you need to run "git status" and "git diff", send a single message with two ${BASH_TOOL_NAME} tool calls in parallel.
|
||||||
|
- If the commands depend on each other and must run sequentially, use a single ${BASH_TOOL_NAME} call with '&&' to chain them together (e.g., `git add . && git commit -m "message" && git push`). For instance, if one operation must complete before another starts (like mkdir before cp, Write before Bash for git operations, or git add before git commit), run these operations sequentially instead.
|
||||||
|
- Use ';' only when you need to run commands sequentially but don't care if earlier commands fail
|
||||||
|
- DO NOT use newlines to separate commands (newlines are ok in quoted strings)
|
||||||
|
- 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.
|
||||||
|
<good-example>
|
||||||
|
pytest /foo/bar/tests
|
||||||
|
</good-example>
|
||||||
|
<bad-example>
|
||||||
|
cd /foo/bar && pytest tests
|
||||||
|
</bad-example>
|
||||||
|
|
||||||
|
${GIT_COMMIT_AND_PR_CREATION_INSTRUCTION()}
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
# 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
|
||||||
@@ -0,0 +1,268 @@
|
|||||||
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||||
|
import {
|
||||||
|
renderTemplate,
|
||||||
|
renderPromptTemplate,
|
||||||
|
createToolDescriptionContext,
|
||||||
|
clearGitInstructionsCache,
|
||||||
|
} from '../../../src/template/renderer.js';
|
||||||
|
import type { ExtendedTemplateContext } from '../../../src/template/types.js';
|
||||||
|
import {
|
||||||
|
CUSTOM_TIMEOUT_MS,
|
||||||
|
MAX_TIMEOUT_MS,
|
||||||
|
MAX_OUTPUT_CHARS,
|
||||||
|
} from '../../../src/constants/bash-tool.js';
|
||||||
|
|
||||||
|
describe('Template Renderer - 模板渲染器', () => {
|
||||||
|
describe('基础变量替换', () => {
|
||||||
|
it('替换简单变量', () => {
|
||||||
|
const result = renderTemplate('Hello ${name}!', { name: 'World' });
|
||||||
|
expect(result).toBe('Hello World!');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('替换嵌套属性', () => {
|
||||||
|
const result = renderTemplate('${user.name} is ${user.age}', {
|
||||||
|
user: { name: 'Alice', age: 30 },
|
||||||
|
});
|
||||||
|
expect(result).toBe('Alice is 30');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('处理未定义变量 - 默认空字符串', () => {
|
||||||
|
const result = renderTemplate('Value: ${undefined_var}', {});
|
||||||
|
expect(result).toBe('Value: ');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('处理未定义变量 - 自定义默认值', () => {
|
||||||
|
const result = renderTemplate('Value: ${undefined_var}', {}, { undefinedValue: 'N/A' });
|
||||||
|
expect(result).toBe('Value: N/A');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('三元表达式', () => {
|
||||||
|
it('条件为真时返回 trueValue', () => {
|
||||||
|
const result = renderTemplate('${isActive ? "Active" : "Inactive"}', { isActive: true });
|
||||||
|
expect(result).toBe('Active');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('条件为假时返回 falseValue', () => {
|
||||||
|
const result = renderTemplate('${isActive ? "Active" : "Inactive"}', { isActive: false });
|
||||||
|
expect(result).toBe('Inactive');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('空字符串被视为假', () => {
|
||||||
|
const result = renderTemplate('${value ? "yes" : "no"}', { value: '' });
|
||||||
|
expect(result).toBe('no');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('函数调用', () => {
|
||||||
|
it('调用注册的函数', () => {
|
||||||
|
const context: ExtendedTemplateContext = {
|
||||||
|
__functions__: {
|
||||||
|
GET_VALUE: () => 42,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const result = renderTemplate('Value: ${GET_VALUE()}', context);
|
||||||
|
expect(result).toBe('Value: 42');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('调用返回字符串的函数', () => {
|
||||||
|
const context: ExtendedTemplateContext = {
|
||||||
|
__functions__: {
|
||||||
|
GET_MESSAGE: () => 'Hello from function',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const result = renderTemplate('${GET_MESSAGE()}', context);
|
||||||
|
expect(result).toBe('Hello from function');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('未注册的函数返回空字符串', () => {
|
||||||
|
const context: ExtendedTemplateContext = {
|
||||||
|
__functions__: {},
|
||||||
|
};
|
||||||
|
const result = renderTemplate('Value: ${UNKNOWN_FUNC()}', context);
|
||||||
|
expect(result).toBe('Value: ');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('没有 __functions__ 时返回空字符串', () => {
|
||||||
|
const result = renderTemplate('Value: ${SOME_FUNC()}', {});
|
||||||
|
expect(result).toBe('Value: ');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('算术运算(除法)', () => {
|
||||||
|
it('数值除法', () => {
|
||||||
|
const result = renderTemplate('${value / 1000}', { value: 60000 });
|
||||||
|
expect(result).toBe('60');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('函数调用结果除法', () => {
|
||||||
|
const context: ExtendedTemplateContext = {
|
||||||
|
__functions__: {
|
||||||
|
GET_MS: () => 120000,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const result = renderTemplate('${GET_MS() / 60000} minutes', context);
|
||||||
|
expect(result).toBe('2 minutes');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('结果向下取整', () => {
|
||||||
|
const result = renderTemplate('${value / 1000}', { value: 12345 });
|
||||||
|
expect(result).toBe('12');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('除以零不进行除法运算', () => {
|
||||||
|
// 除以零时,divMatch 匹配但 divisor === 0,会跳过除法逻辑
|
||||||
|
// 然后尝试作为变量处理 "value / 0",找不到返回空
|
||||||
|
const result = renderTemplate('${value / 0}', { value: 100 });
|
||||||
|
expect(result).toBe('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('组合表达式', () => {
|
||||||
|
it('函数调用 + 除法 + 字符串', () => {
|
||||||
|
const context: ExtendedTemplateContext = {
|
||||||
|
__functions__: {
|
||||||
|
TIMEOUT_MS: () => 600000,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const result = renderTemplate(
|
||||||
|
'Timeout: ${TIMEOUT_MS()}ms (${TIMEOUT_MS()/60000} minutes)',
|
||||||
|
context
|
||||||
|
);
|
||||||
|
expect(result).toBe('Timeout: 600000ms (10 minutes)');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('多个函数调用', () => {
|
||||||
|
const context: ExtendedTemplateContext = {
|
||||||
|
__functions__: {
|
||||||
|
MIN_VAL: () => 10,
|
||||||
|
MAX_VAL: () => 100,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const result = renderTemplate('Range: ${MIN_VAL()} - ${MAX_VAL()}', context);
|
||||||
|
expect(result).toBe('Range: 10 - 100');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('函数调用 + 变量混合', () => {
|
||||||
|
const context: ExtendedTemplateContext = {
|
||||||
|
toolName: 'bash',
|
||||||
|
__functions__: {
|
||||||
|
GET_TIMEOUT: () => 120000,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const result = renderTemplate(
|
||||||
|
'Use ${toolName} with timeout ${GET_TIMEOUT()}ms',
|
||||||
|
context
|
||||||
|
);
|
||||||
|
expect(result).toBe('Use bash with timeout 120000ms');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('createToolDescriptionContext - 工具描述上下文', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
clearGitInstructionsCache();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
clearGitInstructionsCache();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('包含工具名称变量', () => {
|
||||||
|
const context = createToolDescriptionContext();
|
||||||
|
expect(context.custom?.BASH_TOOL_NAME).toBe('bash');
|
||||||
|
expect(context.custom?.GREP_TOOL_NAME).toBe('grep');
|
||||||
|
expect(context.custom?.GLOB_TOOL_NAME).toBe('glob');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('包含注册的模板函数', () => {
|
||||||
|
const context = createToolDescriptionContext();
|
||||||
|
expect(context.__functions__).toBeDefined();
|
||||||
|
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('函数返回正确的配置值', () => {
|
||||||
|
const context = createToolDescriptionContext();
|
||||||
|
expect(context.__functions__.CUSTOM_TIMEOUT_MS()).toBe(CUSTOM_TIMEOUT_MS);
|
||||||
|
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 - 提示词模板渲染', () => {
|
||||||
|
it('渲染带函数调用的模板', () => {
|
||||||
|
const context = createToolDescriptionContext();
|
||||||
|
const template = 'Timeout: ${CUSTOM_TIMEOUT_MS()}ms (${CUSTOM_TIMEOUT_MS()/60000} minutes)';
|
||||||
|
const result = renderPromptTemplate(template, context);
|
||||||
|
expect(result).toBe(`Timeout: ${CUSTOM_TIMEOUT_MS}ms (${Math.floor(CUSTOM_TIMEOUT_MS / 60000)} minutes)`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('渲染带工具名变量的模板', () => {
|
||||||
|
const context = createToolDescriptionContext();
|
||||||
|
const template = 'Use ${BASH_TOOL_NAME} or ${GREP_TOOL_NAME}';
|
||||||
|
const result = renderPromptTemplate(template, context);
|
||||||
|
expect(result).toBe('Use bash or grep');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('渲染完整的 bash 描述模板片段', () => {
|
||||||
|
const context = createToolDescriptionContext();
|
||||||
|
const template = `Commands timeout after \${MAX_TIMEOUT_MS()}ms (\${MAX_TIMEOUT_MS()/60000} minutes).
|
||||||
|
Output truncated at \${MAX_OUTPUT_CHARS()} chars.
|
||||||
|
Use \${GREP_TOOL_NAME} for search.`;
|
||||||
|
|
||||||
|
const result = renderPromptTemplate(template, context);
|
||||||
|
|
||||||
|
expect(result).toContain(`${MAX_TIMEOUT_MS}ms`);
|
||||||
|
expect(result).toContain(`${Math.floor(MAX_TIMEOUT_MS / 60000)} minutes`);
|
||||||
|
expect(result).toContain(`${MAX_OUTPUT_CHARS} chars`);
|
||||||
|
expect(result).toContain('grep for search');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('回归测试 - 确保现有功能正常', () => {
|
||||||
|
it('字符串字面量中的变量插值', () => {
|
||||||
|
const result = renderTemplate('${cond ? "Value is ${val}" : "No value"}', {
|
||||||
|
cond: true,
|
||||||
|
val: 42,
|
||||||
|
});
|
||||||
|
expect(result).toBe('Value is 42');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('布尔值处理', () => {
|
||||||
|
expect(renderTemplate('${flag}', { flag: true })).toBe('true');
|
||||||
|
expect(renderTemplate('${flag}', { flag: false })).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('数字值处理', () => {
|
||||||
|
expect(renderTemplate('${num}', { num: 123 })).toBe('123');
|
||||||
|
expect(renderTemplate('${num}', { num: 0 })).toBe('0');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('嵌套对象访问', () => {
|
||||||
|
const result = renderTemplate('${a.b.c}', { a: { b: { c: 'deep' } } });
|
||||||
|
expect(result).toBe('deep');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('多个模板变量', () => {
|
||||||
|
const result = renderTemplate('${a} + ${b} = ${c}', { a: 1, b: 2, c: 3 });
|
||||||
|
expect(result).toBe('1 + 2 = 3');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user