feat: Phase1 核心——模型/状态机 + SQLite Store(守卫+审批闸) + REST/WS API + daemon

- src/model: 复杂度分级、16 状态状态机、实体类型
- src/store: better-sqlite3 接 schema,transition 受 canTransition 守卫,
  decide 审批闸(reject 必带改进意见),事件订阅广播,nextExecutable
- src/api: Fastify REST + ws 事件广播(/ws)
- src/daemon: maestrod 入口(env 配置,默认 ~/.maestro :4517)
- test: 9 个生命周期单测全过;typecheck/build 干净;REST+WS 端到端实跑验证

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-06-12 20:44:45 +08:00
commit 3172718a94
18 changed files with 2757 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
import { homedir } from 'node:os';
import { join } from 'node:path';
import { mkdirSync } from 'node:fs';
export interface DaemonConfig {
dataDir: string; // 数据库 + run 转录日志所在目录
dbFile: string;
host: string;
port: number;
}
/** 解析配置:环境变量覆盖默认值。首次访问时确保数据目录存在。 */
export function loadConfig(): DaemonConfig {
const dataDir = process.env.MAESTRO_DATA_DIR ?? join(homedir(), '.maestro');
mkdirSync(dataDir, { recursive: true });
return {
dataDir,
dbFile: join(dataDir, 'maestro.sqlite'),
host: process.env.MAESTRO_HOST ?? '127.0.0.1',
port: Number(process.env.MAESTRO_PORT ?? 4517),
};
}
+29
View File
@@ -0,0 +1,29 @@
import { Store } from '../store/index.js';
import { buildServer, attachWebSocket } from '../api/server.js';
import { loadConfig } from './config.js';
/** maestrod:核心 daemon。Phase 1 = Store + REST/WS API(手动驱动;编排器在 Phase 2 接入)。 */
async function main(): Promise<void> {
const cfg = loadConfig();
const store = new Store(cfg.dbFile);
const app = buildServer({ store, logger: true });
attachWebSocket(app, store);
await app.listen({ host: cfg.host, port: cfg.port });
app.log.info(`maestrod 就绪 · db=${cfg.dbFile} · http://${cfg.host}:${cfg.port} · ws ${cfg.host}:${cfg.port}/ws`);
const shutdown = async (): Promise<void> => {
app.log.info('收到退出信号,关闭中…');
await app.close();
store.close();
process.exit(0);
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
}
main().catch((err) => {
console.error('maestrod 启动失败:', err);
process.exit(1);
});