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:
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env bun
|
||||
/**
|
||||
* AI Assistant Server Entry Point
|
||||
*
|
||||
* 独立运行的服务器入口,用于开发和测试
|
||||
*
|
||||
* Usage:
|
||||
* bun run packages/server/src/bin/server.ts
|
||||
* bun run packages/server/src/bin/server.ts --port 8080
|
||||
* bun run packages/server/src/bin/server.ts --host 0.0.0.0 --port 3000
|
||||
*/
|
||||
|
||||
import { app, websocket, startServer } from '../index.js';
|
||||
|
||||
// 解析命令行参数
|
||||
function parseArgs(): { port: number; host: string } {
|
||||
const args = process.argv.slice(2);
|
||||
let port = 3000;
|
||||
let host = '127.0.0.1';
|
||||
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] === '--port' || args[i] === '-p') {
|
||||
port = parseInt(args[i + 1], 10) || 3000;
|
||||
i++;
|
||||
} else if (args[i] === '--host' || args[i] === '-h') {
|
||||
host = args[i + 1] || '127.0.0.1';
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return { port, host };
|
||||
}
|
||||
|
||||
// 主函数
|
||||
async function main() {
|
||||
const { port, host } = parseArgs();
|
||||
|
||||
// 打印启动信息
|
||||
startServer({ port, host });
|
||||
|
||||
// 启动 Bun 服务器
|
||||
const server = Bun.serve({
|
||||
port,
|
||||
hostname: host,
|
||||
fetch: app.fetch,
|
||||
websocket,
|
||||
});
|
||||
|
||||
console.log(`Server started at http://${host}:${port}`);
|
||||
|
||||
// 优雅关闭
|
||||
process.on('SIGINT', () => {
|
||||
console.log('\nShutting down server...');
|
||||
server.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
process.on('SIGTERM', () => {
|
||||
console.log('\nShutting down server...');
|
||||
server.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user