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
+308
View File
@@ -0,0 +1,308 @@
/**
* 治理台路由契约(编码规范 §8.1 / §9.1)。
* 治理台九功能不设后端 governance 模块:每个功能是对应模块的管理用例 + 本文件的路由组装(§3)。
*/
import { z } from 'zod';
export declare const listStaffRoute: {
readonly method: "GET";
readonly path: "/api/gov/staff";
readonly output: z.ZodObject<{
staff: z.ZodArray<z.ZodObject<{
id: z.core.$ZodBranded<z.ZodString, "StaffId", "out">;
account: z.ZodString;
name: z.ZodString;
positionId: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
state: z.ZodDefault<z.ZodEnum<{
active: "active";
disabled: "disabled";
}>>;
position: z.ZodObject<{
id: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
code: z.ZodString;
name: z.ZodString;
note: z.ZodOptional<z.ZodString>;
}, z.core.$strip>;
}, z.core.$strip>>;
positions: z.ZodArray<z.ZodObject<{
id: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
code: z.ZodString;
name: z.ZodString;
note: z.ZodOptional<z.ZodString>;
}, z.core.$strip>>;
}, z.core.$strip>;
};
export declare const createStaffRoute: {
readonly method: "POST";
readonly path: "/api/gov/staff";
readonly body: z.ZodObject<{
account: z.ZodString;
name: z.ZodString;
positionId: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
}, z.core.$strip>;
readonly output: z.ZodObject<{
id: z.core.$ZodBranded<z.ZodString, "StaffId", "out">;
account: z.ZodString;
name: z.ZodString;
positionId: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
state: z.ZodDefault<z.ZodEnum<{
active: "active";
disabled: "disabled";
}>>;
}, z.core.$strip>;
};
export declare const disableStaffCheckRoute: {
readonly method: "GET";
readonly path: "/api/gov/staff/:staffId/disable-check";
readonly params: z.ZodObject<{
staffId: z.core.$ZodBranded<z.ZodString, "StaffId", "out">;
}, z.core.$strip>;
readonly output: z.ZodObject<{
staffId: z.core.$ZodBranded<z.ZodString, "StaffId", "out">;
blocked: z.ZodBoolean;
openTasks: z.ZodDefault<z.ZodArray<z.ZodObject<{
taskId: z.ZodString;
title: z.ZodString;
}, z.core.$strip>>>;
openCards: z.ZodDefault<z.ZodArray<z.ZodObject<{
cardId: z.ZodString;
title: z.ZodString;
}, z.core.$strip>>>;
openApprovals: z.ZodDefault<z.ZodArray<z.ZodObject<{
approvalId: z.ZodString;
title: z.ZodString;
}, z.core.$strip>>>;
}, z.core.$strip>;
};
export declare const disableStaffRoute: {
readonly method: "POST";
readonly path: "/api/gov/staff/:staffId/disable";
readonly params: z.ZodObject<{
staffId: z.core.$ZodBranded<z.ZodString, "StaffId", "out">;
}, z.core.$strip>;
readonly output: z.ZodObject<{
ok: z.ZodBoolean;
check: z.ZodObject<{
staffId: z.core.$ZodBranded<z.ZodString, "StaffId", "out">;
blocked: z.ZodBoolean;
openTasks: z.ZodDefault<z.ZodArray<z.ZodObject<{
taskId: z.ZodString;
title: z.ZodString;
}, z.core.$strip>>>;
openCards: z.ZodDefault<z.ZodArray<z.ZodObject<{
cardId: z.ZodString;
title: z.ZodString;
}, z.core.$strip>>>;
openApprovals: z.ZodDefault<z.ZodArray<z.ZodObject<{
approvalId: z.ZodString;
title: z.ZodString;
}, z.core.$strip>>>;
}, z.core.$strip>;
}, z.core.$strip>;
};
export declare const listGrantsRoute: {
readonly method: "GET";
readonly path: "/api/gov/positions/:positionId/grants";
readonly params: z.ZodObject<{
positionId: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
}, z.core.$strip>;
readonly output: z.ZodObject<{
grants: z.ZodArray<z.ZodObject<{
positionId: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
object: z.ZodDiscriminatedUnion<[z.ZodObject<{
kind: z.ZodLiteral<"data">;
domain: z.ZodString;
object: z.ZodString;
field: z.ZodOptional<z.ZodString>;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"governance">;
function: z.ZodEnum<{
data: "data";
task: "task";
trigger: "trigger";
system: "system";
tool: "tool";
activity: "activity";
staff: "staff";
process: "process";
memory: "memory";
}>;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"task">;
activityId: z.ZodOptional<z.ZodString>;
}, z.core.$strip>], "kind">;
action: z.ZodEnum<{
read: "read";
write: "write";
govern: "govern";
}>;
}, z.core.$strip>>;
}, z.core.$strip>;
};
export declare const setGrantsRoute: {
readonly method: "POST";
readonly path: "/api/gov/positions/:positionId/grants";
readonly params: z.ZodObject<{
positionId: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
}, z.core.$strip>;
readonly body: z.ZodObject<{
grants: z.ZodArray<z.ZodObject<{
positionId: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
object: z.ZodDiscriminatedUnion<[z.ZodObject<{
kind: z.ZodLiteral<"data">;
domain: z.ZodString;
object: z.ZodString;
field: z.ZodOptional<z.ZodString>;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"governance">;
function: z.ZodEnum<{
data: "data";
task: "task";
trigger: "trigger";
system: "system";
tool: "tool";
activity: "activity";
staff: "staff";
process: "process";
memory: "memory";
}>;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"task">;
activityId: z.ZodOptional<z.ZodString>;
}, z.core.$strip>], "kind">;
action: z.ZodEnum<{
read: "read";
write: "write";
govern: "govern";
}>;
}, z.core.$strip>>;
}, z.core.$strip>;
readonly output: z.ZodObject<{
grants: z.ZodArray<z.ZodObject<{
positionId: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
object: z.ZodDiscriminatedUnion<[z.ZodObject<{
kind: z.ZodLiteral<"data">;
domain: z.ZodString;
object: z.ZodString;
field: z.ZodOptional<z.ZodString>;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"governance">;
function: z.ZodEnum<{
data: "data";
task: "task";
trigger: "trigger";
system: "system";
tool: "tool";
activity: "activity";
staff: "staff";
process: "process";
memory: "memory";
}>;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"task">;
activityId: z.ZodOptional<z.ZodString>;
}, z.core.$strip>], "kind">;
action: z.ZodEnum<{
read: "read";
write: "write";
govern: "govern";
}>;
}, z.core.$strip>>;
}, z.core.$strip>;
};
export declare const LLM_PROVIDERS: readonly ["openai-compatible", "openai", "deepseek", "anthropic", "qwen"];
export declare const llmProviderSchema: z.ZodEnum<{
"openai-compatible": "openai-compatible";
openai: "openai";
deepseek: "deepseek";
anthropic: "anthropic";
qwen: "qwen";
}>;
export type LlmProvider = z.infer<typeof llmProviderSchema>;
/** 活动只见自定义模型名;Provider / URL / API-Key / Model 四元在系统侧绑定,活动不触厂商与密钥 */
export declare const llmInstanceViewSchema: z.ZodObject<{
id: z.ZodString;
name: z.ZodString;
provider: z.ZodEnum<{
"openai-compatible": "openai-compatible";
openai: "openai";
deepseek: "deepseek";
anthropic: "anthropic";
qwen: "qwen";
}>;
baseUrl: z.ZodString;
model: z.ZodString;
apiKeyBound: z.ZodBoolean;
systemUse: z.ZodDefault<z.ZodBoolean>;
enabled: z.ZodDefault<z.ZodBoolean>;
}, z.core.$strip>;
export type LlmInstanceView = z.infer<typeof llmInstanceViewSchema>;
export declare const listLlmInstancesRoute: {
readonly method: "GET";
readonly path: "/api/gov/llm";
readonly output: z.ZodObject<{
instances: z.ZodArray<z.ZodObject<{
id: z.ZodString;
name: z.ZodString;
provider: z.ZodEnum<{
"openai-compatible": "openai-compatible";
openai: "openai";
deepseek: "deepseek";
anthropic: "anthropic";
qwen: "qwen";
}>;
baseUrl: z.ZodString;
model: z.ZodString;
apiKeyBound: z.ZodBoolean;
systemUse: z.ZodDefault<z.ZodBoolean>;
enabled: z.ZodDefault<z.ZodBoolean>;
}, z.core.$strip>>;
}, z.core.$strip>;
};
export declare const upsertLlmInstanceRoute: {
readonly method: "POST";
readonly path: "/api/gov/llm";
readonly body: z.ZodObject<{
id: z.ZodOptional<z.ZodString>;
name: z.ZodString;
provider: z.ZodEnum<{
"openai-compatible": "openai-compatible";
openai: "openai";
deepseek: "deepseek";
anthropic: "anthropic";
qwen: "qwen";
}>;
baseUrl: z.ZodString;
model: z.ZodString;
apiKey: z.ZodOptional<z.ZodString>;
systemUse: z.ZodDefault<z.ZodBoolean>;
enabled: z.ZodDefault<z.ZodBoolean>;
}, z.core.$strip>;
readonly output: z.ZodObject<{
id: z.ZodString;
name: z.ZodString;
provider: z.ZodEnum<{
"openai-compatible": "openai-compatible";
openai: "openai";
deepseek: "deepseek";
anthropic: "anthropic";
qwen: "qwen";
}>;
baseUrl: z.ZodString;
model: z.ZodString;
apiKeyBound: z.ZodBoolean;
systemUse: z.ZodDefault<z.ZodBoolean>;
enabled: z.ZodDefault<z.ZodBoolean>;
}, z.core.$strip>;
};
export declare const probeLlmInstanceRoute: {
readonly method: "POST";
readonly path: "/api/gov/llm/:id/probe";
readonly params: z.ZodObject<{
id: z.ZodString;
}, z.core.$strip>;
readonly output: z.ZodObject<{
ok: z.ZodBoolean;
latencyMs: z.ZodOptional<z.ZodNumber>;
error: z.ZodOptional<z.ZodString>;
}, z.core.$strip>;
};
+31
View File
@@ -0,0 +1,31 @@
/**
* 路由契约的形状(编码规范 §8.1 / §9.2)。
*
* 前端只经 `shared/typed-fetch` + 本目录的路由契约与服务端交互;组件内禁止裸 fetch。
* **禁止 Hono RPC 客户端** —— 它要求前端 import 后端类型,违反 §5.1。
*/
import type { z } from 'zod';
export declare const HTTP_METHODS: readonly ["GET", "POST", "PATCH", "DELETE"];
export type HttpMethod = (typeof HTTP_METHODS)[number];
/**
* 一条路由契约。`path` 用 `:name` 表示路径参数,与 `params` 的键一一对应。
* body 只在 POST/PATCH 上有意义;GET 用 query。
*/
export type RouteContract<TParams extends z.ZodTypeAny = z.ZodTypeAny, TQuery extends z.ZodTypeAny = z.ZodTypeAny, TBody extends z.ZodTypeAny = z.ZodTypeAny, TOutput extends z.ZodTypeAny = z.ZodTypeAny> = {
readonly method: HttpMethod;
readonly path: string;
readonly params?: TParams;
readonly query?: TQuery;
readonly body?: TBody;
readonly output: TOutput;
};
export type RouteOutput<R extends RouteContract> = z.infer<R['output']>;
export type RouteBody<R extends RouteContract> = R['body'] extends z.ZodTypeAny ? z.infer<R['body']> : never;
/** 结构化错误:前端按 code 分辨,不解析文案 */
export declare const API_ERROR_CODES: readonly ["unauthenticated", "forbidden", "not-found", "validation-failed", "conflict", "precondition-failed", "internal"];
export type ApiErrorCode = (typeof API_ERROR_CODES)[number];
export type ApiError = {
readonly code: ApiErrorCode;
readonly message: string;
readonly detail?: unknown;
};
+4
View File
@@ -0,0 +1,4 @@
export * from './http.ts';
export * from './workbench.ts';
export * from './governance.ts';
export * from './ingest.ts';
+71
View File
@@ -0,0 +1,71 @@
/**
* 入站路由契约(编码规范 §8.1)。
*
* 通道入站 → 归属解析 → 统一事件流(OS 06)。MVP 的邮件通道以**模拟通道**接入:
* mock 只允许出现在**外界**(邮件通道与外部系统),系统内部机制不得 mock(README MVP 完成判据)。
*/
import { z } from 'zod';
/** 一封入站邮件的归一形状 —— 真实邮件通道与模拟通道共用 */
export declare const inboundMailSchema: z.ZodObject<{
messageId: z.ZodString;
threadId: z.ZodOptional<z.ZodString>;
from: z.ZodString;
to: z.ZodArray<z.ZodString>;
cc: z.ZodDefault<z.ZodArray<z.ZodString>>;
subject: z.ZodString;
body: z.ZodString;
receivedAt: z.ZodISODateTime;
attachments: z.ZodDefault<z.ZodArray<z.ZodObject<{
name: z.ZodString;
contentType: z.ZodString;
size: z.ZodNumber;
ref: z.ZodString;
}, z.core.$strip>>>;
}, z.core.$strip>;
export type InboundMail = z.infer<typeof inboundMailSchema>;
export declare const ingestMailRoute: {
readonly method: "POST";
readonly path: "/api/ingest/channel/:channelId/mail";
readonly params: z.ZodObject<{
channelId: z.core.$ZodBranded<z.ZodString, "ChannelId", "out">;
}, z.core.$strip>;
readonly body: z.ZodObject<{
messageId: z.ZodString;
threadId: z.ZodOptional<z.ZodString>;
from: z.ZodString;
to: z.ZodArray<z.ZodString>;
cc: z.ZodDefault<z.ZodArray<z.ZodString>>;
subject: z.ZodString;
body: z.ZodString;
receivedAt: z.ZodISODateTime;
attachments: z.ZodDefault<z.ZodArray<z.ZodObject<{
name: z.ZodString;
contentType: z.ZodString;
size: z.ZodNumber;
ref: z.ZodString;
}, z.core.$strip>>>;
}, z.core.$strip>;
readonly output: z.ZodObject<{
eventId: z.core.$ZodBranded<z.ZodString, "EventId", "out">;
resolution: z.ZodDiscriminatedUnion<[z.ZodObject<{
outcome: z.ZodLiteral<"owned">;
rule: z.ZodEnum<{
thread: "thread";
"listener-key": "listener-key";
"address-window": "address-window";
}>;
taskId: z.core.$ZodBranded<z.ZodString, "TaskId", "out">;
}, z.core.$strip>, z.ZodObject<{
outcome: z.ZodLiteral<"unowned">;
reason: z.ZodEnum<{
"no-match": "no-match";
ambiguous: "ambiguous";
"extract-failed": "extract-failed";
}>;
fallbackPosition: z.ZodOptional<z.core.$ZodBranded<z.ZodString, "PositionId", "out">>;
}, z.core.$strip>], "outcome">;
listenersHit: z.ZodNumber;
triggersHit: z.ZodNumber;
fellBackToMessage: z.ZodBoolean;
}, z.core.$strip>;
};
+69
View File
@@ -0,0 +1,69 @@
/**
* 工作台路由契约(编码规范 §8.1 / §9.1)。
* 工作台五要素:任务 / 审批 / 消息 / 纪要 / 汇报 —— 同一份过程事实的五种投影。
*/
import { z } from 'zod';
export declare const sessionRoute: {
readonly method: "GET";
readonly path: "/api/session";
readonly output: z.ZodObject<{
staff: z.ZodObject<{
id: z.core.$ZodBranded<z.ZodString, "StaffId", "out">;
account: z.ZodString;
name: z.ZodString;
positionId: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
state: z.ZodDefault<z.ZodEnum<{
active: "active";
disabled: "disabled";
}>>;
}, z.core.$strip>;
position: z.ZodObject<{
id: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
code: z.ZodString;
name: z.ZodString;
note: z.ZodOptional<z.ZodString>;
}, z.core.$strip>;
grants: z.ZodArray<z.ZodObject<{
positionId: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
object: z.ZodDiscriminatedUnion<[z.ZodObject<{
kind: z.ZodLiteral<"data">;
domain: z.ZodString;
object: z.ZodString;
field: z.ZodOptional<z.ZodString>;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"governance">;
function: z.ZodEnum<{
data: "data";
task: "task";
trigger: "trigger";
system: "system";
tool: "tool";
activity: "activity";
staff: "staff";
process: "process";
memory: "memory";
}>;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"task">;
activityId: z.ZodOptional<z.ZodString>;
}, z.core.$strip>], "kind">;
action: z.ZodEnum<{
read: "read";
write: "write";
govern: "govern";
}>;
}, z.core.$strip>>;
}, z.core.$strip>;
};
export declare const healthRoute: {
readonly method: "GET";
readonly path: "/api/health";
readonly output: z.ZodObject<{
ok: z.ZodBoolean;
version: z.ZodString;
db: z.ZodEnum<{
up: "up";
down: "down";
}>;
}, z.core.$strip>;
};