refactor(server): 消除与 Core 的重复类型定义

- 删除 Server 中 60+ 个与 Core 重复的类型定义
- 将动态导入 (await import) 改为静态类型导入 (import type)
- 保留必要的运行时静态导入
- 修复测试文件中的 mock 初始化问题
- 净删除约 960 行重复代码

重构文件:
- routes/checkpoints.ts: 删除 155 行重复类型
- routes/agents.ts: 删除 93 行重复类型
- routes/commands.ts: 删除 83 行重复类型
- routes/mcp.ts: 修复类型窄化
- routes/hooks.ts: 已使用静态导入
- routes/providers.ts: 删除 63 行重复类型
- session/manager.ts: 删除 41 行重复类型
- routes/sessions.ts: 添加类型导入
- permission/handler.ts: 添加类型导入
This commit is contained in:
2025-12-16 20:19:24 +08:00
parent 026429cb2f
commit 1b7d55848d
14 changed files with 283 additions and 1240 deletions
@@ -34,15 +34,7 @@ const mockAgentModule = vi.hoisted(() => ({
isPresetAgent: vi.fn((name: string) => name in mockPresetAgents),
}));
// Track if module should be available
let moduleAvailable = true;
vi.mock('@ai-assistant/core', () => {
if (!moduleAvailable) {
throw new Error('Module not found');
}
return mockAgentModule;
});
vi.mock('@ai-assistant/core', () => mockAgentModule);
vi.mock('../../../src/routes/config.js', () => ({
getConfig: vi.fn(() => ({ workdir: '/test/workdir' })),
@@ -57,7 +49,6 @@ app.route('/agents', agentsRouter);
describe('Agents Route', () => {
beforeEach(() => {
vi.clearAllMocks();
moduleAvailable = true;
mockAgentModule.loadAgentConfig.mockResolvedValue(null);
mockAgentModule.saveAgentConfig.mockResolvedValue(undefined);
});
@@ -35,15 +35,7 @@ const mockCommandModule = vi.hoisted(() => ({
createCommandManager: vi.fn(() => mockCommandManager),
}));
// Track if module should be available
let moduleAvailable = true;
vi.mock('@ai-assistant/core', () => {
if (!moduleAvailable) {
throw new Error('Module not found');
}
return mockCommandModule;
});
vi.mock('@ai-assistant/core', () => mockCommandModule);
vi.mock('../../../src/routes/config.js', () => ({
getConfig: vi.fn(() => ({ workdir: '/test/workdir' })),
@@ -58,7 +50,6 @@ app.route('/commands', commandsRouter);
describe('Commands Route', () => {
beforeEach(() => {
vi.clearAllMocks();
moduleAvailable = true;
mockCommandRegistry.list.mockReturnValue([]);
mockCommandRegistry.getStats.mockReturnValue({ total: 0, bySource: {} });
});
@@ -15,15 +15,7 @@ const mockHooksModule = vi.hoisted(() => ({
createDefaultConfig: vi.fn(),
}));
// Track if module should be available
let moduleAvailable = true;
vi.mock('@ai-assistant/core', () => {
if (!moduleAvailable) {
throw new Error('Module not found');
}
return mockHooksModule;
});
vi.mock('@ai-assistant/core', () => mockHooksModule);
vi.mock('../../../src/routes/config.js', () => ({
getConfig: vi.fn(() => ({ workdir: '/test/workdir' })),
@@ -44,7 +36,6 @@ app.route('/hooks', hooksRouter);
describe('Hooks Route', () => {
beforeEach(() => {
vi.clearAllMocks();
moduleAvailable = true;
mockHooksModule.loadHookConfig.mockResolvedValue(null);
mockHooksModule.getConfigFilePath.mockResolvedValue('/test/workdir/.ai-assistant.json');
});
@@ -26,15 +26,7 @@ const mockCoreModule = vi.hoisted(() => ({
getProviderRegistry: vi.fn(() => mockRegistry),
}));
// Track if core module should be available
let coreModuleAvailable = true;
vi.mock('@ai-assistant/core', () => {
if (!coreModuleAvailable) {
throw new Error('Module not found');
}
return mockCoreModule;
});
vi.mock('@ai-assistant/core', () => mockCoreModule);
import { providersRouter } from '../../../src/routes/providers.js';
@@ -45,7 +37,6 @@ app.route('/providers', providersRouter);
describe('Providers Route', () => {
beforeEach(() => {
vi.clearAllMocks();
coreModuleAvailable = true;
});
describe('GET /providers - 列出所有提供商', () => {