Files
ai-terminal-assistant/packages/server/src/routes/tools.ts
T
kurihada e53035ffc0 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 的职责划分
2025-12-16 21:28:19 +08:00

106 lines
2.2 KiB
TypeScript

/**
* Tools API Routes
*
* 工具管理相关的 REST API
* 直接使用 Core 的 toolRegistry,不再维护独立的注册表
*/
import { Hono } from 'hono';
import { toolRegistry } from '@ai-assistant/core';
export const toolsRouter = new Hono();
/**
* GET /tools - 列出所有可用工具
*/
toolsRouter.get('/', (c) => {
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: toolList,
});
});
/**
* GET /tools/:name - 获取单个工具详情
*/
toolsRouter.get('/:name', (c) => {
const name = c.req.param('name');
const tool = toolRegistry.getTool(name);
if (!tool) {
return c.json(
{
success: false,
error: 'Tool not found',
},
404
);
}
return c.json({
success: true,
data: {
name: tool.name,
description: tool.description,
parameters: tool.parameters,
},
});
});
/**
* POST /tools/:name/execute - 执行工具
*
* 注意: 工具执行是同步的,对于长时间运行的工具,
* 建议通过 WebSocket 执行以获取实时反馈。
*/
toolsRouter.post('/:name/execute', async (c) => {
const name = c.req.param('name');
const tool = toolRegistry.getTool(name);
if (!tool) {
return c.json(
{
success: false,
error: 'Tool not found',
},
404
);
}
try {
const body = await c.req.json();
const params = body.params || {};
// TODO: 实际调用工具执行逻辑
// 需要设置正确的上下文(workdir、权限回调等)
// const result = await tool.execute(params, context);
return c.json({
success: true,
data: {
tool: name,
params,
result: null, // 占位,后续实现
message: 'Tool execution not yet implemented - use WebSocket for tool execution',
},
});
} catch (error) {
return c.json(
{
success: false,
error: error instanceof Error ? error.message : 'Execution failed',
},
500
);
}
});