feat: 重构为 Monorepo 架构并实现 HTTP Server

架构变更:
- 采用 pnpm workspaces 实现 Monorepo 结构
- 将现有代码迁移到 packages/core
- 新增 packages/server HTTP 服务层

Server 功能:
- REST API: 会话管理、工具管理、配置管理
- WebSocket: 实时双向通信支持
- SSE: 服务端事件推送
- Hono + Bun 作为运行时

API 端点:
- GET/POST /api/sessions - 会话 CRUD
- GET/POST /api/sessions/:id/messages - 消息管理
- GET /api/sessions/:id/events - SSE 事件流
- WS /api/ws/:sessionId - WebSocket 连接
- GET/POST /api/tools - 工具管理
- GET/PUT /api/config - 配置管理
This commit is contained in:
2025-12-12 10:42:20 +08:00
parent 59dbed926e
commit 5e32375f0e
301 changed files with 3281 additions and 43 deletions
+109
View File
@@ -0,0 +1,109 @@
/**
* Tools API Routes
*
* 工具管理相关的 REST API
*/
import { Hono } from 'hono';
import type { Tool } from '../types.js';
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();
return c.json({
success: true,
data: tools,
});
});
/**
* GET /tools/:name - 获取单个工具详情
*/
toolsRouter.get('/:name', (c) => {
const name = c.req.param('name');
const tool = toolRegistry.get(name);
if (!tool) {
return c.json(
{
success: false,
error: 'Tool not found',
},
404
);
}
return c.json({
success: true,
data: tool,
});
});
/**
* POST /tools/:name/execute - 执行工具
*
* 注意: 工具执行是同步的,对于长时间运行的工具,
* 建议通过 WebSocket 执行以获取实时反馈。
*/
toolsRouter.post('/:name/execute', async (c) => {
const name = c.req.param('name');
const tool = toolRegistry.get(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: 实际调用 core 模块的工具执行逻辑
// const result = await executeTool(name, params);
return c.json({
success: true,
data: {
tool: name,
params,
result: null, // 占位,后续实现
message: 'Tool execution not yet implemented',
},
});
} catch (error) {
return c.json(
{
success: false,
error: error instanceof Error ? error.message : 'Execution failed',
},
500
);
}
});