refactor(core): 实现类型安全的工具定义系统

- 新增 defineTool 函数,使用 Zod schema 定义参数并自动推断 TypeScript 类型
- 重构文件系统工具 (read_file, write_file, edit_file, glob, grep, multi_edit) 使用 Zod 类型推断
- 重构 shell 工具 (bash, kill_shell) 使用新的类型安全系统
- 重构 task 工具 (task, task_output) 使用 Zod 验证
- 兼容 Zod v4 API (处理 _zod.def vs _def, error.issues vs error.errors)
- 导出参数类型供外部使用 (ReadFileParams, BashParams 等)
- 统一参数命名: path -> file_path
- 修复相关测试以适配新的参数结构和输出格式
- 移除不存在工具的测试文件
This commit is contained in:
2025-12-18 15:46:11 +08:00
parent b2bb26a92b
commit 2c8a95daeb
21 changed files with 769 additions and 623 deletions
@@ -8,8 +8,10 @@ vi.mock('@tavily/core', () => ({
})),
}));
// Mock environment variable for Tavily API Key
vi.stubEnv('TAVILY_API_KEY', 'test-api-key');
// Mock getServiceApiKey 来返回 API Key
vi.mock('../../../../src/provider/index.js', () => ({
getServiceApiKey: vi.fn().mockResolvedValue('test-api-key'),
}));
// Mock permission manager
vi.mock('../../../../src/permission/index.js', () => ({
@@ -28,6 +30,7 @@ vi.mock('../../../../src/tools/load_description.js', () => ({
import { webExtractTool } from '../../../../src/tools/web/web_extract.js';
import { getPermissionManager } from '../../../../src/permission/index.js';
import { getServiceApiKey } from '../../../../src/provider/index.js';
describe('webExtractTool - 网页内容提取工具', () => {
beforeEach(() => {
@@ -193,7 +196,8 @@ describe('webExtractTool - 网页内容提取工具', () => {
});
it('无 API Key 返回错误', async () => {
vi.unstubAllEnvs();
// Mock getServiceApiKey 返回 null
vi.mocked(getServiceApiKey).mockResolvedValueOnce(null);
const result = await webExtractTool.execute({
urls: ['https://example.com'],
@@ -201,8 +205,6 @@ describe('webExtractTool - 网页内容提取工具', () => {
expect(result.success).toBe(false);
expect(result.error).toContain('未配置 Tavily API Key');
vi.stubEnv('TAVILY_API_KEY', 'test-api-key');
});
it('权限被拒绝时返回错误', async () => {
@@ -8,8 +8,10 @@ vi.mock('@tavily/core', () => ({
})),
}));
// Mock environment variable for Tavily API Key
vi.stubEnv('TAVILY_API_KEY', 'test-api-key');
// Mock getServiceApiKey 来返回 API Key
vi.mock('../../../../src/provider/index.js', () => ({
getServiceApiKey: vi.fn().mockResolvedValue('test-api-key'),
}));
// Mock permission manager
vi.mock('../../../../src/permission/index.js', () => ({
@@ -28,6 +30,7 @@ vi.mock('../../../../src/tools/load_description.js', () => ({
import { webSearchTool } from '../../../../src/tools/web/web_search.js';
import { getPermissionManager } from '../../../../src/permission/index.js';
import { getServiceApiKey } from '../../../../src/provider/index.js';
describe('webSearchTool - 网络搜索工具', () => {
beforeEach(() => {
@@ -126,14 +129,13 @@ describe('webSearchTool - 网络搜索工具', () => {
});
it('无 API Key 返回错误', async () => {
vi.unstubAllEnvs();
// Mock getServiceApiKey 返回 null
vi.mocked(getServiceApiKey).mockResolvedValueOnce(null);
const result = await webSearchTool.execute({ query: 'test' });
expect(result.success).toBe(false);
expect(result.error).toContain('未配置 Tavily API Key');
vi.stubEnv('TAVILY_API_KEY', 'test-api-key');
});
it('权限被拒绝时返回错误', async () => {