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
+19 -200
View File
@@ -9,38 +9,18 @@ import * as fs from 'fs/promises';
import * as path from 'path';
import { spawn } from 'child_process';
import { getConfig } from './config.js';
// Core Hooks 模块类型
interface HooksModule {
loadProjectConfig: (directory: string) => Promise<ProjectConfig | null>;
loadHookConfig: (directory: string) => Promise<HookConfig | null>;
getConfigFilePath: (directory: string) => Promise<string | null>;
createDefaultConfig: (directory: string) => Promise<void>;
}
interface ShellCommandConfig {
command: string[];
environment?: Record<string, string>;
timeout?: number;
cwd?: string;
}
interface FileHookConfig {
[pattern: string]: ShellCommandConfig[];
}
interface HookConfig {
file_edited?: FileHookConfig;
file_created?: FileHookConfig;
file_deleted?: FileHookConfig;
session_completed?: ShellCommandConfig[];
}
interface ProjectConfig {
hooks?: HookConfig;
plugins?: string[];
[key: string]: unknown;
}
import type {
HookConfig,
ProjectConfig,
ShellCommandConfig,
FileHookConfig,
} from '@ai-assistant/core';
import {
loadProjectConfig,
loadHookConfig,
getConfigFilePath,
createDefaultConfig,
} from '@ai-assistant/core';
interface HookTestResult {
success: boolean;
@@ -52,44 +32,6 @@ interface HookTestResult {
export const hooksRouter = new Hono();
// Core 模块缓存
let hooksModule: HooksModule | null = null;
/**
* 初始化 Hooks 模块
*/
async function initHooksModule(): Promise<HooksModule | null> {
if (hooksModule) return hooksModule;
try {
const corePath = '@ai-assistant/core';
const core = (await import(corePath)) as Record<string, unknown>;
if (
typeof core.loadProjectConfig !== 'function' ||
typeof core.loadHookConfig !== 'function' ||
typeof core.getConfigFilePath !== 'function' ||
typeof core.createDefaultConfig !== 'function'
) {
console.warn('[Hooks] Core module missing Hooks exports');
return null;
}
hooksModule = {
loadProjectConfig: core.loadProjectConfig as HooksModule['loadProjectConfig'],
loadHookConfig: core.loadHookConfig as HooksModule['loadHookConfig'],
getConfigFilePath: core.getConfigFilePath as HooksModule['getConfigFilePath'],
createDefaultConfig: core.createDefaultConfig as HooksModule['createDefaultConfig'],
};
console.log('[Hooks] Hooks module initialized');
return hooksModule;
} catch (error) {
console.warn('[Hooks] Failed to load Hooks module:', error);
return null;
}
}
/**
* 移除 JSON 中的注释(支持 JSONC 格式)
*/
@@ -123,11 +65,8 @@ async function writeConfigFile(configPath: string, config: ProjectConfig): Promi
* 获取或创建配置文件路径
*/
async function getOrCreateConfigPath(workdir: string): Promise<string> {
const module = await initHooksModule();
if (module) {
const existingPath = await module.getConfigFilePath(workdir);
if (existingPath) return existingPath;
}
const existingPath = await getConfigFilePath(workdir);
if (existingPath) return existingPath;
return path.join(workdir, '.ai-assistant.json');
}
@@ -135,20 +74,8 @@ async function getOrCreateConfigPath(workdir: string): Promise<string> {
* GET /hooks/config - 获取完整钩子配置
*/
hooksRouter.get('/config', async (c) => {
const module = await initHooksModule();
if (!module) {
return c.json(
{
success: false,
error: 'Hooks module not available',
},
503
);
}
const config = getConfig();
const hookConfig = await module.loadHookConfig(config.workdir);
const hookConfig = await loadHookConfig(config.workdir);
return c.json({
success: true,
@@ -165,18 +92,6 @@ hooksRouter.get('/config', async (c) => {
* PUT /hooks/config - 更新完整钩子配置
*/
hooksRouter.put('/config', async (c) => {
const module = await initHooksModule();
if (!module) {
return c.json(
{
success: false,
error: 'Hooks module not available',
},
503
);
}
try {
const newHookConfig = await c.req.json<HookConfig>();
const config = getConfig();
@@ -213,20 +128,8 @@ hooksRouter.put('/config', async (c) => {
* GET /hooks/file-edited - 获取 file_edited 钩子
*/
hooksRouter.get('/file-edited', async (c) => {
const module = await initHooksModule();
if (!module) {
return c.json(
{
success: false,
error: 'Hooks module not available',
},
503
);
}
const config = getConfig();
const hookConfig = await module.loadHookConfig(config.workdir);
const hookConfig = await loadHookConfig(config.workdir);
return c.json({
success: true,
@@ -238,18 +141,6 @@ hooksRouter.get('/file-edited', async (c) => {
* PUT /hooks/file-edited - 更新 file_edited 钩子
*/
hooksRouter.put('/file-edited', async (c) => {
const module = await initHooksModule();
if (!module) {
return c.json(
{
success: false,
error: 'Hooks module not available',
},
503
);
}
try {
const newFileEditedHooks = await c.req.json<FileHookConfig>();
const config = getConfig();
@@ -285,20 +176,8 @@ hooksRouter.put('/file-edited', async (c) => {
* GET /hooks/file-created - 获取 file_created 钩子
*/
hooksRouter.get('/file-created', async (c) => {
const module = await initHooksModule();
if (!module) {
return c.json(
{
success: false,
error: 'Hooks module not available',
},
503
);
}
const config = getConfig();
const hookConfig = await module.loadHookConfig(config.workdir);
const hookConfig = await loadHookConfig(config.workdir);
return c.json({
success: true,
@@ -310,18 +189,6 @@ hooksRouter.get('/file-created', async (c) => {
* PUT /hooks/file-created - 更新 file_created 钩子
*/
hooksRouter.put('/file-created', async (c) => {
const module = await initHooksModule();
if (!module) {
return c.json(
{
success: false,
error: 'Hooks module not available',
},
503
);
}
try {
const newFileCreatedHooks = await c.req.json<FileHookConfig>();
const config = getConfig();
@@ -357,20 +224,8 @@ hooksRouter.put('/file-created', async (c) => {
* GET /hooks/file-deleted - 获取 file_deleted 钩子
*/
hooksRouter.get('/file-deleted', async (c) => {
const module = await initHooksModule();
if (!module) {
return c.json(
{
success: false,
error: 'Hooks module not available',
},
503
);
}
const config = getConfig();
const hookConfig = await module.loadHookConfig(config.workdir);
const hookConfig = await loadHookConfig(config.workdir);
return c.json({
success: true,
@@ -382,18 +237,6 @@ hooksRouter.get('/file-deleted', async (c) => {
* PUT /hooks/file-deleted - 更新 file_deleted 钩子
*/
hooksRouter.put('/file-deleted', async (c) => {
const module = await initHooksModule();
if (!module) {
return c.json(
{
success: false,
error: 'Hooks module not available',
},
503
);
}
try {
const newFileDeletedHooks = await c.req.json<FileHookConfig>();
const config = getConfig();
@@ -429,20 +272,8 @@ hooksRouter.put('/file-deleted', async (c) => {
* GET /hooks/session-completed - 获取 session_completed 钩子
*/
hooksRouter.get('/session-completed', async (c) => {
const module = await initHooksModule();
if (!module) {
return c.json(
{
success: false,
error: 'Hooks module not available',
},
503
);
}
const config = getConfig();
const hookConfig = await module.loadHookConfig(config.workdir);
const hookConfig = await loadHookConfig(config.workdir);
return c.json({
success: true,
@@ -454,18 +285,6 @@ hooksRouter.get('/session-completed', async (c) => {
* PUT /hooks/session-completed - 更新 session_completed 钩子
*/
hooksRouter.put('/session-completed', async (c) => {
const module = await initHooksModule();
if (!module) {
return c.json(
{
success: false,
error: 'Hooks module not available',
},
503
);
}
try {
const newSessionCompletedHooks = await c.req.json<ShellCommandConfig[]>();
const config = getConfig();