This repository has been archived on 2026-09-13. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
BPOS-TMP-REPO/apps/web/src/shared/gov/flow/pkg-to-graph.ts
T
ChaiHaoandClaude Fable 5 f145bde875 三面拆分⑲:审批点卸岗位 —— 负责人一律运营时分配,绑员工组,实例记 assignee
模型(用户定):审批点不绑岗位;节点负责人是运营时分配的,绑定到员工组。

- 契约:approval 节点删 approver(功能属性只剩驳回走向/催办/委托消息);
  CLOSURE_VIOLATION_KINDS 删 missing-owner-position(岗位不入编排,无岗可核);
  治理审批列表删 approverPosition 字段
- 运行时统一口径(活动点=审批点):工作流绑了员工组按组轮值(组员并集最闲者先),
  没绑组全员轮值;staffId 稳定排序保证确定性,组员全不在岗退回全员。
  ADR-011「绑定先于策略」随岗位退场 —— 工作组快照的成员集合成为轮值池
- 实例记 assignee(迁移 0014 node_instance.assignee_id):分派结果是流转事实 ——
  审批箱收件(i.assigneeId === me)、裁决权(同一判据)、员工面链条报实际承接人、
  治理台审批列表报人,全部按它认,不再有岗位比对
- closure 卸 positions 入参;desk/desk-approval/governance-ops 三路由改口;
  web 属性卡删「审批岗位」选择与警示、positions 管线拆除;种子 A5 卸 PM;
  导出依赖文件审批段只留编排语义

测试:closure 25、save-package 6、seed 8、graph-to-pkg 5、端到端八例真库全绿;
五闸退出码全过。库:迁移 + v4 就地同步(唯一生效版)。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KeDBx2PHLavbURXB8LJJ7m
2026-07-28 21:24:41 +08:00

175 lines
7.6 KiB
TypeScript

/**
* 流程包 → 画布图模型:**连线即映射**的可视一侧。
*
* 画布件(`flow-layout` / `flow-node-paint` / `FlowDiagram`)是框架无关的纯几何与画笔,
* 它认的是 `FlowGraph`;我们的权威模型是 `ProcessPackage`。这里做且只做一次形状转换,
* **不产生任何新语义** —— 节点仍是编排层五要素,审批点仍是系统内置人裁节点(共用方框几何而已)。
*/
import type { ProcessPackage } from '@contracts';
import type { FlowEdge, FlowGraph, FlowNode } from './flow-layout.ts';
/** 节点实例落位:running / waiting / terminal 决定描边高亮,不改几何 */
export type InstanceMark = {
readonly nodeId: string;
readonly running: boolean;
readonly waiting: boolean;
readonly activated: boolean;
readonly terminal?: string | undefined;
};
/** 每行最多几个方框 —— 蛇形折行的行宽;取原型 business 预设的观感 */
const ROW_WIDTH = 4;
export function packageToGraph(pkg: ProcessPackage, marks: readonly InstanceMark[] = []): FlowGraph {
const markOf = new Map(marks.map((m) => [m.nodeId, m]));
/*
* 手绘图存证优先:编辑器保存过,**手绘几何即呈现真相**。
* 运行标注仍要每次现算(它是运行态,不入存证),合到存证节点的 note 上。
*/
const stored = readStoredDiagram(pkg.diagram);
if (stored !== undefined) {
/*
* **语义里有、存证里没有的节点要补画出来** —— 不补,画布就看不见它:
* 异常看板指着一个图上不存在的节点,点了也定位不到(实测:API 直存的包
* 语义层加了新节点、diagram 还是旧的,picker 里根本没有那个节点)。
* 编辑器保存时两层同步,不会走到这;这是给「语义先行」的数据兜底。
* 补的节点没有几何,按 60px 一档排在图纸下缘 —— 位置丑没关系,看得见才能修。
*/
const drawn = new Set(stored.nodes.map((n) => n.id));
const missing = pkg.nodes.filter((n) => !drawn.has(n.id));
const bottom = stored.nodes.reduce((max, n) => Math.max(max, (n.y ?? 0)), 0) + 90;
const extras: FlowNode[] = missing.map((n, i) => ({
id: n.id,
kind: n.kind === 'start' ? ('start' as const) : n.kind === 'end' ? ('end' as const) : ('activity' as const),
label: n.label,
...(n.kind === 'approval' ? { flavor: 'approval' as const } : {}),
...(n.kind === 'activity' ? { code: n.id, act: n.activityId } : {}),
x: 30 + (i % 6) * 160,
y: bottom + Math.floor(i / 6) * 60,
}));
const semanticById = new Map(pkg.nodes.map((n) => [n.id as string, n]));
const withState = (list: readonly FlowNode[]) =>
list.map((n) => {
const state = stateOf(markOf.get(n.id));
const sem = semanticById.get(n.id);
// 链接的实现(装载面)以语义为准回填 —— 存证里的 act 只是画布工作痕迹
const linked = sem?.kind === 'activity' ? { act: sem.activityId as string } : {};
const base = { ...n, ...linked };
return state === undefined ? base : { ...base, note: state };
});
/*
* 边属性(编号/条件/映射)从语义边回填到画布边:属性卡编辑的是画布工作副本,
* 保存时 graph-to-pkg 又以它为准 —— 回填缺席的话,打开即保存会把契约冲掉。
* 顺序流全是真边(卡点画法已废),按 from→to 直接认亲;code 即边号 E1、E2…
*/
const semEdge = new Map(pkg.edges.map((e) => [`${e.from}→${e.to}`, e]));
const edges = stored.edges.map((e) => {
const sem = semEdge.get(`${e.from}→${e.to}`);
if (sem === undefined) return e;
return {
...e,
code: sem.id as string,
...(sem.condition === undefined ? {} : { condition: sem.condition }),
mapping: sem.mapping as unknown[],
};
});
return { ...stored, edges, nodes: [...withState(stored.nodes), ...withState(extras)] };
}
const nodes: FlowNode[] = pkg.nodes.map((n) => {
const state = stateOf(markOf.get(n.id));
if (n.kind === 'start') return { id: n.id, kind: 'start', label: n.label };
if (n.kind === 'end') return { id: n.id, kind: 'end', label: n.label };
const base: FlowNode = {
id: n.id,
kind: 'activity',
label: n.label,
code: n.id,
...(n.sub === undefined ? {} : { sub: n.sub }),
...(state === undefined ? {} : { note: state }),
};
if (n.kind === 'approval') {
// approval **不是一种活动**:它挂在 kind:"activity" 下仅因共用同一套方框几何
return { ...base, flavor: 'approval' };
}
return {
...base,
// 契约不带负责人岗位 —— 角标不再标岗位,链接的实现(装载面)以 act 携带
act: n.activityId,
...(n.in === undefined ? {} : { in: [...n.in] }),
...(n.out === undefined ? {} : { out: [...n.out] }),
};
});
/** 蛇形折行:偶数行 L→R、奇数行 R→L —— 行的划分交给拓扑序 */
const order = topologicalOrder(pkg);
const rows: string[][] = [];
for (let i = 0; i < order.length; i += ROW_WIDTH) rows.push(order.slice(i, i + ROW_WIDTH));
const edges: FlowEdge[] = pkg.edges.map((e) => ({
from: e.from,
to: e.to,
code: e.id as string,
// 回边按声明走「回 A4 重开新实例」,视觉上标红以区分正向流转
...(e.tone !== undefined ? { tone: e.tone } : e.reopen ? { tone: 'bad' as const } : {}),
...(e.label === undefined ? (e.condition === undefined ? {} : { label: e.condition }) : { label: e.label }),
}));
return { nodes, rows, anchors: [], edges };
}
/**
* 手绘图存证的在场校验:`diagram` 由本仓 `graph-to-pkg` 序列化而来,结构即 FlowGraph;
* 这里只验骨架在不在(nodes/edges 数组、非空),不逐字段复检 —— 写入侧是唯一生产者。
*/
function readStoredDiagram(d: Record<string, unknown> | undefined): FlowGraph | undefined {
if (d === undefined) return undefined;
const nodes = d['nodes'];
const edges = d['edges'];
if (!Array.isArray(nodes) || nodes.length === 0 || !Array.isArray(edges)) return undefined;
return {
nodes,
edges,
rows: Array.isArray(d['rows']) ? d['rows'] : [],
anchors: Array.isArray(d['anchors']) ? d['anchors'] : [],
manual: d['manual'] === true,
} as FlowGraph;
}
/** 运行标注:节点实例态 → 画布角注文字;未激活不标 */
function stateOf(mark: InstanceMark | undefined): string | undefined {
if (mark === undefined || !mark.activated) return undefined;
if (mark.terminal !== undefined) return mark.terminal === 'released' ? '已放行' : '已终止';
return mark.running ? '运行中' : mark.waiting ? '等待中' : '已激活';
}
/** 拓扑序:回边不参与定序(否则成环),同层按声明顺序稳定排列 */
function topologicalOrder(pkg: ProcessPackage): string[] {
const ids = pkg.nodes.map((n) => n.id as string);
const forward = pkg.edges.filter((e) => !e.reopen);
const indegree = new Map<string, number>(ids.map((id) => [id, 0]));
for (const e of forward) indegree.set(e.to, (indegree.get(e.to) ?? 0) + 1);
const ready = ids.filter((id) => indegree.get(id) === 0);
const out: string[] = [];
const seen = new Set<string>();
while (ready.length > 0) {
const id = ready.shift()!;
if (seen.has(id)) continue;
seen.add(id);
out.push(id);
for (const e of forward.filter((x) => x.from === id)) {
const left = (indegree.get(e.to) ?? 0) - 1;
indegree.set(e.to, left);
if (left === 0) ready.push(e.to);
}
}
// 环(理论上闭合校验已挡住)或孤点:兜底补齐,画布不因数据异常而空白
for (const id of ids) if (!seen.has(id)) out.push(id);
return out;
}