/** * 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 ); } });