refactor(core,server): 统一模块职责并消除类型重复

Core 模块职责:
- 添加 inferPermissionType() 权限类型推断函数
- 添加 partToApiFormat()/partsToApiFormat() API 格式转换函数
- 添加 ApiPart/ApiTextPart/ApiToolPart/ApiReasoningPart 类型
- 统一导出 toolRegistry 供 Server 使用

Server 模块职责:
- 重命名 PermissionRequestContext 为 PermissionDisplayContext
- 移除本地 toolRegistry,直接使用 Core 的注册表
- 使用 Core 的 inferPermissionType 替代本地实现
- 使用 Core 的 partsToApiFormat 替代手动转换

文档更新:
- 在 gui-server-client.md 添加第11章「模块职责边界」
- 明确 Core 和 Server 的职责划分
This commit is contained in:
2025-12-16 21:28:19 +08:00
parent 0a26c3ab72
commit e53035ffc0
12 changed files with 274 additions and 185 deletions
+22 -26
View File
@@ -2,39 +2,30 @@
* Tools API Routes
*
* 工具管理相关的 REST API
* 直接使用 Core 的 toolRegistry,不再维护独立的注册表
*/
import { Hono } from 'hono';
import type { Tool } from '../types.js';
import { toolRegistry } from '@ai-assistant/core';
export const toolsRouter = new Hono();
// 工具注册表 (后续会从 core 模块获取)
const toolRegistry: Map<string, Tool> = new Map();
/**
* 注册工具 (内部使用)
*/
export function registerTool(tool: Tool): void {
toolRegistry.set(tool.name, tool);
}
/**
* 获取所有已注册的工具
*/
export function getRegisteredTools(): Tool[] {
return Array.from(toolRegistry.values());
}
/**
* GET /tools - 列出所有可用工具
*/
toolsRouter.get('/', (c) => {
const tools = getRegisteredTools();
const tools = toolRegistry.getAllTools();
// 转换为 API 格式(不包含 execute 函数)
const toolList = tools.map((t) => ({
name: t.name,
description: t.description,
parameters: t.parameters,
}));
return c.json({
success: true,
data: tools,
data: toolList,
});
});
@@ -43,7 +34,7 @@ toolsRouter.get('/', (c) => {
*/
toolsRouter.get('/:name', (c) => {
const name = c.req.param('name');
const tool = toolRegistry.get(name);
const tool = toolRegistry.getTool(name);
if (!tool) {
return c.json(
@@ -57,7 +48,11 @@ toolsRouter.get('/:name', (c) => {
return c.json({
success: true,
data: tool,
data: {
name: tool.name,
description: tool.description,
parameters: tool.parameters,
},
});
});
@@ -69,7 +64,7 @@ toolsRouter.get('/:name', (c) => {
*/
toolsRouter.post('/:name/execute', async (c) => {
const name = c.req.param('name');
const tool = toolRegistry.get(name);
const tool = toolRegistry.getTool(name);
if (!tool) {
return c.json(
@@ -85,8 +80,9 @@ toolsRouter.post('/:name/execute', async (c) => {
const body = await c.req.json();
const params = body.params || {};
// TODO: 实际调用 core 模块的工具执行逻辑
// const result = await executeTool(name, params);
// TODO: 实际调用工具执行逻辑
// 需要设置正确的上下文(workdir、权限回调等)
// const result = await tool.execute(params, context);
return c.json({
success: true,
@@ -94,7 +90,7 @@ toolsRouter.post('/:name/execute', async (c) => {
tool: name,
params,
result: null, // 占位,后续实现
message: 'Tool execution not yet implemented',
message: 'Tool execution not yet implemented - use WebSocket for tool execution',
},
});
} catch (error) {