/** * apps/api 唯一组装层入口(编码规范 §5.2)。 * 只有 app/ 能同时认识多个模块:new adapter、注入 port、注册路由 / worker / Mastra 实例。 */ import { serve } from '@hono/node-server'; import { loadConfig } from './config.ts'; import { createContainer } from './container.ts'; import { createServer } from './server.ts'; import { startDispatcher } from './workers/dispatcher.ts'; export async function boot(): Promise { const config = loadConfig(); const container = await createContainer(config); await container.org.ensureSeeded(); // 派发器:投递记录即队列,按目标 ID 取 advisory lock 串行消费(ADR-008) // 消费者随 M4(任务)/ M5(触发器)注册;此刻空表即「只标记送达」 const dispatcher = startDispatcher({ container, consumers: new Map() }, config.dispatchIntervalMs); const app = createServer(container); serve({ fetch: app.fetch, port: config.port }, (info) => { process.stdout.write(`JCAI api · http://localhost:${info.port} · env=${config.nodeEnv}\n`); }); const shutdown = async (): Promise => { dispatcher.stop(); await container.close(); process.exit(0); }; process.on('SIGINT', () => void shutdown()); process.on('SIGTERM', () => void shutdown()); } if (import.meta.url === `file://${process.argv[1]}`) { await boot(); }