M1: 底座 —— @contracts 首版 + event / ledger / org / llm

@contracts 首版(§8.1 九个文件):
- id 品牌 ID / terminal 终态五名与归侧四标签 / envelope 信封六字段与四类事件
- card 工单两模式 + 对话卡族(形状对齐 2026-07-26 原型定稿)
- contract7 契约七件 / ledger 三级寻址与写三分 / process-package 流程包 DSL 与触发器三类
- manifest 活动包清单六区(含 CMMN·DMN·步序声明的资产形状)/ org / api 路由契约

模块(L1):
- event   信封存储、归属解析三级短路、投递五诺、监听注册表、三级消费裁定
- ledger  schema 自生长(最大化合并/最宽类型/只增不破坏)、任务视图叠加、写三分、冲突检查
- org     岗位授权(客体三类×动作三种)、停用前置检查、会话、种子播种
- llm     四元绑定、ModelGateway 三模式(live/record/replay)—— 两缝之一

app 装配:Hono 服务 + 鉴权中间件 + 治理台/工作台路由;drizzle 迁移;AES-256-GCM 凭证密封

ADR-007 工具链与门禁 · ADR-008 投递队列 · ADR-009 凭证加密
(ADR 编号自 007 续:编码规范 §14 已用到 006,README 写「从 005 续」是笔误)

门禁五道全绿;96 个测试通过,含对真实 PG 的 ledger 集成测试

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KeDBx2PHLavbURXB8LJJ7m
This commit is contained in:
JCAI Main Agent
2026-07-27 00:59:14 +08:00
co-authored by Claude Opus 5
parent 7cd664af3a
commit 14d4fa046a
120 changed files with 15236 additions and 78 deletions
+124
View File
@@ -0,0 +1,124 @@
/**
* 治理台路由组装(编码规范 §3:治理台九功能不设后端 governance 模块 ——
* 每个功能是对应模块的管理用例 + 本文件的路由组装)。
*/
import type { Hono } from 'hono';
import type { ApiError, GovernanceFunction } from '@contracts';
import {
createStaffRoute,
disableStaffCheckRoute,
disableStaffRoute,
listGrantsRoute,
listLlmInstancesRoute,
listStaffRoute,
positionIdSchema,
probeLlmInstanceRoute,
setGrantsRoute,
staffIdSchema,
upsertLlmInstanceRoute,
} from '@contracts';
import { DisableBlockedError } from '@modules/org';
import type { Container } from '../container.ts';
import type { AppEnv } from '../http-env.ts';
/** 治理动作要过岗位授权(客体 = 治理功能,动作 = 治理) */
function requireGovern(container: Container, fn: GovernanceFunction) {
return async (account: string): Promise<ApiError | undefined> => {
const staff = await container.org.findByAccount(account);
if (staff === undefined) return { code: 'unauthenticated', message: '会话无效' };
const ok = await container.org.authorize(staff.id, { kind: 'governance', function: fn }, 'govern');
return ok ? undefined : { code: 'forbidden', message: `岗位 ${staff.position.code} 无「${fn}」治理权` };
};
}
export function registerGovernanceRoutes(app: Hono<AppEnv>, container: Container): void {
const guardStaff = requireGovern(container, 'staff');
const guardSystem = requireGovern(container, 'system');
/* ── 员工管理 ─────────────────────────────────────────────────────── */
app.get(listStaffRoute.path, async (c) => {
const denied = await guardStaff(c.get('account'));
if (denied) return c.json(denied, denied.code === 'forbidden' ? 403 : 401);
return c.json(await container.org.listStaff());
});
app.post(createStaffRoute.path, async (c) => {
const denied = await guardStaff(c.get('account'));
if (denied) return c.json(denied, denied.code === 'forbidden' ? 403 : 401);
const parsed = createStaffRoute.body.safeParse(await c.req.json());
if (!parsed.success) {
const body: ApiError = { code: 'validation-failed', message: '入参不合法', detail: parsed.error.issues };
return c.json(body, 400);
}
return c.json(await container.org.createStaff(parsed.data));
});
app.get(disableStaffCheckRoute.path, async (c) => {
const denied = await guardStaff(c.get('account'));
if (denied) return c.json(denied, denied.code === 'forbidden' ? 403 : 401);
const staffId = staffIdSchema.parse(c.req.param('staffId'));
return c.json(await container.org.checkDisable(staffId));
});
app.post(disableStaffRoute.path, async (c) => {
const denied = await guardStaff(c.get('account'));
if (denied) return c.json(denied, denied.code === 'forbidden' ? 403 : 401);
const staffId = staffIdSchema.parse(c.req.param('staffId'));
try {
await container.org.disableStaff(staffId);
return c.json({ ok: true, check: await container.org.checkDisable(staffId) });
} catch (error) {
if (error instanceof DisableBlockedError) {
// 停用前置检查未过:在途任务与待答卡须先改派完毕(OS 02)
const body: ApiError = { code: 'precondition-failed', message: error.message, detail: error.check };
return c.json(body, 412);
}
throw error;
}
});
app.get(listGrantsRoute.path, async (c) => {
const denied = await guardStaff(c.get('account'));
if (denied) return c.json(denied, denied.code === 'forbidden' ? 403 : 401);
const positionId = positionIdSchema.parse(c.req.param('positionId'));
return c.json({ grants: await container.org.listGrants(positionId) });
});
app.post(setGrantsRoute.path, async (c) => {
const denied = await guardStaff(c.get('account'));
if (denied) return c.json(denied, denied.code === 'forbidden' ? 403 : 401);
const positionId = positionIdSchema.parse(c.req.param('positionId'));
const parsed = setGrantsRoute.body.safeParse(await c.req.json());
if (!parsed.success) {
const body: ApiError = { code: 'validation-failed', message: '入参不合法', detail: parsed.error.issues };
return c.json(body, 400);
}
return c.json({ grants: await container.org.setGrants(positionId, parsed.data.grants) });
});
/* ── 系统管理 · LLM 实例 ──────────────────────────────────────────── */
app.get(listLlmInstancesRoute.path, async (c) => {
const denied = await guardSystem(c.get('account'));
if (denied) return c.json(denied, denied.code === 'forbidden' ? 403 : 401);
return c.json({ instances: await container.llm.list() });
});
app.post(upsertLlmInstanceRoute.path, async (c) => {
const denied = await guardSystem(c.get('account'));
if (denied) return c.json(denied, denied.code === 'forbidden' ? 403 : 401);
const parsed = upsertLlmInstanceRoute.body.safeParse(await c.req.json());
if (!parsed.success) {
const body: ApiError = { code: 'validation-failed', message: '入参不合法', detail: parsed.error.issues };
return c.json(body, 400);
}
return c.json(await container.llm.upsert(parsed.data));
});
app.post(probeLlmInstanceRoute.path, async (c) => {
const denied = await guardSystem(c.get('account'));
if (denied) return c.json(denied, denied.code === 'forbidden' ? 403 : 401);
return c.json(await container.llm.probe(c.req.param('id')));
});
}
+30
View File
@@ -0,0 +1,30 @@
/**
* 工作台路由组装 —— 路由契约在 @contracts/api,实现在此(编码规范 §3 / §9.2)。
*/
import type { Hono } from 'hono';
import type { ApiError } from '@contracts';
import { CONTRACTS_VERSION, healthRoute, sessionRoute } from '@contracts';
import type { Container } from '../container.ts';
import type { AppEnv } from '../http-env.ts';
export function registerWorkbenchRoutes(app: Hono<AppEnv>, container: Container): void {
app.get(healthRoute.path, async (c) => {
let db: 'up' | 'down' = 'down';
try {
await container.pool.query('select 1');
db = 'up';
} catch {
db = 'down';
}
return c.json({ ok: db === 'up', version: CONTRACTS_VERSION, db });
});
app.get(sessionRoute.path, async (c) => {
const session = await container.org.loadSession(c.get('account'));
if (session === undefined) {
const body: ApiError = { code: 'unauthenticated', message: '会话无效或员工已停用' };
return c.json(body, 401);
}
return c.json(session);
});
}