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/api/src/app/seed-mvp-flow.ts
T
ChaiHaoandClaude Fable 5 6854524784 三面拆分⑫:网关去节点化 —— 分支即条件出边,汇合即多入边异或
模型(与 Mastra .branch 同构,条件挂在步与步之间):
- routing.selectEdges:多出边一律异或,不分节点种类 —— 条件命中优先(按声明序),
  无条件边作默认兜底,恰取一条;单出边直通保留(历史并行网关的遗留通道)
- closure:含网关即 gateway-deprecated(历史版本仅可读、不可激活);
  「隐式合并」禁令删除(多入边=异或汇合,上游互斥无歧义);
  新规:多出边节点的无条件边至多一条,超出报 multi-default-edge
- contracts:CLOSURE_VIOLATION_KINDS 去 gateway-unbalanced/join-ambiguous-source,
  进 multi-default-edge/gateway-deprecated(下游零引用,api-report 随行)

种子(按用户要求:只留一条流一个版本,图/节点/边全部对账):
- MVP_PACKAGE 重写:10 节点 14 边零网关 —— G1/G2 展开为 A1 的三条出边
  (选型/直接寻源/默认直接定价)+ A2 的两条,G2J/G3 整颗删除,
  A5 审批点的两个去向就是自己的两条 verdict 条件出边(驳回边与 reject 声明字字对齐)
- 手绘图:顶车道 y30 补画 A1→A3「直接寻源」—— 画布保存只认画出来的边,
  不画这条语义边会在首次存盘被静默丢掉(新增图边对账测试卡死这类漏画);
  「供应商底价表」标注钉进 y190 车道空档,不再被卡点压住
- 开发库外科手术:清运行数据 + 删 v1–v4,重种为唯一生效版 v1(闭合过闸)

UI 与文案同步:
- 元素盘/追加盘不再有网关件(画布仍认得历史图);图例删「判断/并行网关」「菱形=网关」
- 新建流程补激活一跳(保存不生效模型下,不激活「建出来就能起单」就是谎言)
- 总览四种编排要素、「画图不检查,激活才查完整」贯穿 blueprint/memory/process 文案

测试:closure 43 通过(gateway-deprecated + multi-default-edge 两用例),
seed-mvp-flow 8 通过(含图边对账),端到端八例真库全绿(网关从不建实例,断言原样存活);
五闸 typecheck/lint/depcruise/api:check/test 全过(退出码验)。

债:M5-4 AND 并行待「并行组」声明(Mastra .parallel);M5-5 网关运行时兼容三处,
含网关在途流清零后随 schema 一并摘除。

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

427 lines
19 KiB
TypeScript

/**
* 「统一报价」流程包 —— **无网关**:语义取自 `docs/MVP示例工作流.html`,结构按新模型展开。
*
* 分支 = 源节点的条件出边(恰取一条,无条件边作默认兜底);汇合 = 多入边异或。
* FIG.1 的 G1/G2/G4 各自消失成 A1/A2/A6 的条件出边;G2J 与 G3 整颗不再需要。
* 10 节点 14 边;审批点 A5 的两个去向就是它自己的两条条件出边(verdict 由引擎注入)。
*
* 几何:两带换行、零交叉;直达与回跳都用**卡点**表达(不画长线)——
* 卡点带 `to`,`mergeGraphIntoPackage` 只认语义边已存在的还原,编辑器保存不丢边。
*/
import { processPackageSchema, triggerConfigSchema } from '@contracts';
import type { ProcessPackage } from '@contracts';
import type { Container } from './container.ts';
/** 活动节点:岗位独任、失败即终止 —— 槽位期不谈容忍 */
const activityNode = (
id: string,
label: string,
activityId: string,
position: string,
col: number,
row: number,
x: number,
y: number,
) => ({
id,
kind: 'activity' as const,
label,
sub: position,
activityId,
owner: { position, strategy: 'sole' as const },
failure: { kind: 'terminate' as const },
layout: { row, col, x, y },
});
/** 受理口的两枚去向旗与记录 —— A4 重新入场(回边 / 驳回)时照此装配 */
const REENTER_A4 = [
{ target: 'records', source: { from: 'output-port' as const, port: 'intake', path: 'records' } },
{ target: 'customerCode', source: { from: 'initiation' as const, path: 'customerCode' } },
];
export const MVP_PACKAGE: ProcessPackage = processPackageSchema.parse({
id: 'unified-quote',
name: '统一报价',
version: 1,
state: 'active',
callInterface: {
input: {
type: 'object',
required: ['customerCode', 'rfqRef'],
properties: {
customerCode: { type: 'string' },
rfqRef: { type: 'string' },
subject: { type: 'string' },
body: { type: 'string' },
quotedAt: { type: 'string' },
},
},
output: { type: 'object', properties: { landingRate: { type: 'number' } } },
},
/*
* **无网关**(产品决策 2026-07-28):分支 = 源节点的多条条件出边(恰取一条,
* 无条件边作默认兜底),汇合 = 多入边异或(上游互斥,只会来一条)。
* 与 Mastra 同构:条件挂在步与步之间,步保持纯粹。
* FIG.1 的 G1/G2/G4 于是各自消失成 A1/A2/A6 的条件出边;
* G2J(当年为绕「隐式合并」禁令而生的显式汇合点)与 G3(审批结果)整颗不再需要 ——
* A4 的三条正向入边互斥即合法,A5 的裁决就是它自己的两条条件出边。
*
* 10 节点:start · A1 受理 · A2 选型 · A3 寻源 · A4 定价 · A5 审批 · A6 议价 · A7 归档 · 双终点。
*/
nodes: [
{ id: 'start', kind: 'start', label: '客户RFQ询价', layout: { row: 0, col: 0, x: 30, y: 70 } },
activityNode('A1', '客户RFQ受理', 'rfq-intake', 'BD', 1, 0, 110, 60),
activityNode('A2', '替代选型', 'alt-selection', 'FAE', 2, 0, 290, 60),
activityNode('A3', '寻源询价', 'sourcing-inquiry', 'PRM', 3, 0, 470, 60),
activityNode('A4', '成本定价', 'cost-pricing', 'BD', 4, 1, 110, 310),
{
id: 'A5',
kind: 'approval',
label: '定价审批',
sub: 'PM',
approver: { position: 'PM', strategy: 'sole' },
/*
* 驳回走向声明式,不由临场决定:退回 A4 重新定价。
* 图上由 A5 自己的 `verdict = "reject"` 条件出边**显式表达同一件事**,
* 引擎先看图(有边走边,没边才退回本声明),两处须字字对齐 —— 有测试守着。
* 本声明不能删:schema 必填,且它是通用机制(不是每条流都把驳回画成边)。
*/
reject: { kind: 'reopen', nodeId: 'A4', mapping: REENTER_A4 },
nudge: { steps: [{ after: 'PT20M', action: 'nudge' }], deadline: 'PT30M' },
layout: { row: 1, col: 5, x: 290, y: 310 },
},
activityNode('A6', '报价议价', 'quote-negotiation', 'BD', 6, 1, 470, 310),
activityNode('A7', '情报归档', 'intel-archive', 'BD', 7, 2, 630, 410),
{ id: 'end_won', kind: 'end', label: '转订单履约', layout: { row: 1, col: 7, x: 630, y: 320 } },
{ id: 'end_arch', kind: 'end', label: '归档闭环', layout: { row: 2, col: 8, x: 790, y: 420 } },
],
edges: [
{
id: 'e-start-a1',
from: 'start',
to: 'A1',
mapping: [
{ target: 'customerCode', source: { from: 'initiation', path: 'customerCode' } },
{ target: 'rfqRef', source: { from: 'initiation', path: 'rfqRef' } },
{ target: 'subject', source: { from: 'initiation', path: 'subject' } },
{ target: 'body', source: { from: 'initiation', path: 'body' } },
{ target: 'quotedAt', source: { from: 'initiation', path: 'quotedAt' } },
],
reopen: false,
},
/* A1 的三条出边 —— 原 G1「是否需要选型」+ G2「是否需要寻源」在直达路径上的合并展开 */
{
id: 'e-a1-a2',
from: 'A1',
to: 'A2',
condition: 'needAlternative = true',
label: '需要选型',
mapping: [
{ target: 'material', source: { from: 'output-port', port: 'intake', path: 'pn' } },
{ target: 'needSourcing', source: { from: 'output-port', port: 'intake', path: 'needSourcing' } },
{ target: 'rfqRef', source: { from: 'initiation', path: 'rfqRef' } },
],
reopen: false,
},
{
id: 'e-a1-a3',
from: 'A1',
to: 'A3',
condition: 'needAlternative = false and needSourcing = true',
label: '直接寻源',
mapping: [{ target: 'pn', source: { from: 'output-port', port: 'intake', path: 'pn' } }],
reopen: false,
},
// 默认兜底:不选型不寻源 → 直接定价(条件全不中时走它,旗齐时它就是第三种情形)
{ id: 'e-a1-a4', from: 'A1', to: 'A4', label: '直接定价', mapping: REENTER_A4, reopen: false },
/* A2 的两条出边 —— 原 G2 在选型路径上的那一问 */
{
id: 'e-a2-a3',
from: 'A2',
to: 'A3',
condition: 'needSourcing = true',
label: '仍需寻源',
mapping: [{ target: 'pn', source: { from: 'output-port', port: 'intake', path: 'pn' } }],
reopen: false,
},
{ id: 'e-a2-a4', from: 'A2', to: 'A4', label: '选型后定价', mapping: REENTER_A4, reopen: false },
{
id: 'e-a3-a4',
from: 'A3',
to: 'A4',
label: '供应商底价表',
mapping: [
...REENTER_A4,
// 底价只在寻源路径上有;A1/A2 直达的两条边不带它,A4 的 floors 非必填
{ target: 'floors', source: { from: 'output-port', port: 'compare', path: 'floors' } },
],
reopen: false,
},
{ id: 'e-a4-a5', from: 'A4', to: 'A5', mapping: [], label: '报价草案提审', reopen: false },
/* A5 审批点自己的两条条件出边 —— 裁决(verdict)由引擎在裁决时放进载荷 */
{
id: 'e-a5-a6',
from: 'A5',
to: 'A6',
condition: 'verdict = "pass"',
label: '通过',
tone: 'ok',
mapping: [
{ target: 'rows', source: { from: 'output-port', port: 'draft', path: 'rows' } },
{ target: 'quote_no', source: { from: 'output-port', port: 'draft', path: 'quote_no' } },
{ target: 'customerCode', source: { from: 'initiation', path: 'customerCode' } },
],
reopen: false,
},
{
id: 'e-a5-a4',
from: 'A5',
to: 'A4',
condition: 'verdict = "reject"',
label: '不通过',
tone: 'bad',
mapping: REENTER_A4,
// 回边:与 A5 的 reject 声明同一去向;reopen 必标,否则成环、拓扑序崩
reopen: true,
},
/* A6 的三条条件出边 —— outcome 由放行选口给出(选择组恰选一口) */
{
id: 'e-a6-won',
from: 'A6',
to: 'end_won',
condition: 'outcome = "won"',
label: '成交',
tone: 'ok',
mapping: [],
reopen: false,
},
{
id: 'e-a6-a4',
from: 'A6',
to: 'A4',
condition: 'outcome = "revise"',
label: '有异议',
tone: 'bad',
mapping: REENTER_A4,
reopen: true,
},
{
id: 'e-a6-a7',
from: 'A6',
to: 'A7',
condition: 'outcome = "lost"',
label: '拒绝/超时',
tone: 'bad',
mapping: [
// outcome 由放行选口注入落地事实,随每个发射口的载荷同行(不是 formal 声明的字段)
{ target: 'outcome', source: { from: 'output-port', port: 'formal', path: 'outcome' } },
{ target: 'quote_no', source: { from: 'output-port', port: 'formal', path: 'quote_no' } },
{ target: 'customer_code', source: { from: 'output-port', port: 'formal', path: 'customer_code' } },
{ target: 'quotedAt', source: { from: 'initiation', path: 'quotedAt' } },
],
reopen: false,
},
{ id: 'e-a7-end', from: 'A7', to: 'end_arch', mapping: [], label: '归档闭环', reopen: false },
],
variables: [],
/*
* 手绘几何存证 —— 呈现以它为真相。两带换行、零交叉:
* 顶车道 y30:A1「直接寻源」跨过 A2 直达 A3(带上方,不碰任何框)
* 带 0(y60) start → A1 → A2 → A3 受理 · 选型 · 寻源
* 换行车道 y190:A3 右探下折横扫回左,进 A4 北口
* 带 1(y310) A4 → A5 → A6 → end_won 定价 · 审批 · 议价 · 成交
* 带 2(y410) A7 → end_arch 未成交闭环
* 直达与回跳都用**卡点**表达(不画长线):A1/A2 的「直接定价」牌与 A5/A6 的回 A4 牌。
*/
diagram: {
manual: true,
rows: [],
anchors: [],
nodes: [
{ id: 'start', kind: 'start', label: '客户RFQ询价', x: 30, y: 70, lx: 44, ly: 104 },
{ id: 'A1', kind: 'activity', label: '客户RFQ受理', code: 'A1', owner: 'BD', x: 110, y: 60 },
{ id: 'A2', kind: 'activity', label: '替代选型', code: 'A2', owner: 'FAE', flavor: 'case', x: 290, y: 60 },
{ id: 'A3', kind: 'activity', label: '寻源询价', code: 'A3', owner: 'PRM', flavor: 'case', x: 470, y: 60 },
{ id: 'A4', kind: 'activity', label: '成本定价', code: 'A4', owner: 'BD', x: 110, y: 310 },
{ id: 'A5', kind: 'activity', label: '定价审批', code: 'A5', owner: 'PM', flavor: 'approval', x: 290, y: 310 },
{ id: 'A6', kind: 'activity', label: '报价议价', code: 'A6', owner: 'BD', flavor: 'case', x: 470, y: 310 },
{ id: 'A7', kind: 'activity', label: '情报归档', code: 'A7', owner: 'BD', x: 630, y: 410 },
{ id: 'end_won', kind: 'end', label: '转订单履约', x: 630, y: 320, lx: 640, ly: 352 },
{ id: 'end_arch', kind: 'end-bad', label: '归档闭环', x: 790, y: 420, lx: 800, ly: 452 },
/* 四枚卡点:直达两枚(正向,语义边真实存在)、回跳两枚 —— 全在各自源节点的上下车道 */
{ id: 'c_d1', kind: 'callout', label: '直接定价', to: 'A4', x: 110, y: 170 },
{ id: 'c_d2', kind: 'callout', label: '选型后定价', to: 'A4', x: 290, y: 170 },
{ id: 'c_reject', kind: 'callout', label: '退回调整', to: 'A4', x: 290, y: 240 },
{ id: 'c_revise', kind: 'callout', label: '返回调整', to: 'A4', x: 470, y: 240 },
],
edges: [
{ from: 'start', to: 'A1', points: [{ x: 50, y: 80 }, { x: 110, y: 80 }] },
{ from: 'A1', to: 'A2', label: '需要选型', points: [{ x: 210, y: 80 }, { x: 290, y: 80 }] },
// 直接寻源走顶车道 y30(A1 北口 → A3 北口):画布保存只认画出来的边,这条不画就会丢
{
from: 'A1',
to: 'A3',
label: '直接寻源',
points: [
{ x: 160, y: 60 },
{ x: 160, y: 30 },
{ x: 520, y: 30 },
{ x: 520, y: 60 },
],
},
{ from: 'A2', to: 'A3', label: '仍需寻源', points: [{ x: 390, y: 80 }, { x: 470, y: 80 }] },
// 换行那一笔:A3 右探 → 降到 190 车道 → 横扫回左 → 落 A4 北口
{
from: 'A3',
to: 'A4',
label: '供应商底价表',
// 标注钉在 y190 车道的空档(x390–610)—— 默认取折线中点,会被「选型后定价→A4」卡点压住
lx: 450,
ly: 183,
points: [
{ x: 570, y: 80 },
{ x: 610, y: 80 },
{ x: 610, y: 190 },
{ x: 160, y: 190 },
{ x: 160, y: 310 },
],
},
// 直达两枚卡点(正向):不选不寻 → 直接定价;选完不寻 → 定价
{ from: 'A1', to: 'c_d1', label: '不选不寻', points: [{ x: 160, y: 100 }, { x: 160, y: 170 }] },
{ from: 'A2', to: 'c_d2', label: '不再寻源', points: [{ x: 340, y: 100 }, { x: 340, y: 170 }] },
{ from: 'A4', to: 'A5', label: '报价草案提审', points: [{ x: 210, y: 330 }, { x: 290, y: 330 }] },
{ from: 'A5', to: 'A6', label: '通过', tone: 'ok', points: [{ x: 390, y: 330 }, { x: 470, y: 330 }] },
{ from: 'A5', to: 'c_reject', label: '不通过', tone: 'bad', points: [{ x: 340, y: 310 }, { x: 340, y: 270 }], lx: 310, ly: 292 },
{ from: 'A6', to: 'end_won', label: '成交', tone: 'ok', points: [{ x: 570, y: 330 }, { x: 630, y: 330 }] },
{ from: 'A6', to: 'c_revise', label: '有异议', tone: 'bad', points: [{ x: 520, y: 310 }, { x: 520, y: 270 }], lx: 545, ly: 292 },
{ from: 'A6', to: 'A7', label: '拒绝/超时', tone: 'bad', points: [{ x: 520, y: 350 }, { x: 520, y: 430 }, { x: 630, y: 430 }], lx: 545, ly: 388 },
{ from: 'A7', to: 'end_arch', label: '归档闭环', points: [{ x: 730, y: 430 }, { x: 790, y: 430 }] },
],
},
});
/** 事件触发器:投进公司询价邮箱且主题含询价/RFQ 的自动起单 */
const TRIGGER_EVENT = triggerConfigSchema.parse({
id: 'trg-rfq',
name: '客户RFQ询价',
enabled: true,
processPackageId: 'unified-quote',
mapping: [
{ target: 'customerCode', source: { from: 'expression', sfeel: 'keys.customerCode' } },
{ target: 'rfqRef', source: { from: 'expression', sfeel: 'keys.rfqRef' } },
{ target: 'subject', source: { from: 'expression', sfeel: 'subject' } },
{ target: 'body', source: { from: 'expression', sfeel: 'body' } },
// 单据日期取入站时刻:`intel.history_price` 的主键是 (pn, quoted_at),归档拿不到日期就写不进去
{ target: 'quotedAt', source: { from: 'expression', sfeel: 'occurredAt' } },
],
// 按**通道**收口:同一封信投到「询价待确认邮箱」不命中,落人工确认那条路
spec: {
kind: 'event',
rule: 'channelId = "ch-rfq-mail" and (contains(subject, "询价") or contains(subject, "RFQ"))',
eventType: 'data',
},
});
/** 登记触发器:人确认后起单,「登记人即来源」—— 表单由调用接口 schema 自动渲染 */
const TRIGGER_REGISTRATION = triggerConfigSchema.parse({
id: 'trg-rfq-manual',
name: '人工登记起单(统一报价)',
enabled: true,
processPackageId: 'unified-quote',
mapping: [],
spec: { kind: 'registration' },
});
export async function seedMvpFlow(container: Container): Promise<void> {
const log = (line: string) => process.stdout.write(line + '\n');
/*
* 保存即生效,且**内容没变就不再存新版**。
*
* 保存恒新增版本(这是「旧任务走完旧版本」的前提),所以种子不能无条件存 ——
* 那样每跑一次播种就多一个版本,库里堆一串一模一样的定义。
* 比对的是语义与呈现,不含 version/state(它们由保存本身决定)。
*/
/*
* 比对要**认内容不认键序**。jsonb 从 PG 读回来时键是重排过的
* (Postgres 对 jsonb 的对象键排序),直接 `JSON.stringify` 永远判不等 ——
* 于是每播一次种就多存一版一模一样的定义(实测:一口气堆到 v3)。
* 所以递归按键名排序后再序列化;version / state 由保存本身决定,不参与比对。
*/
const canonical = (value: unknown): unknown =>
Array.isArray(value)
? value.map(canonical)
: value !== null && typeof value === 'object'
? Object.fromEntries(
Object.entries(value as Record<string, unknown>)
.filter(([k]) => k !== 'version' && k !== 'state')
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
.map(([k, v]) => [k, canonical(v)]),
)
: value;
const stripped = (pkg: unknown) => JSON.stringify(canonical(pkg));
const current = await container.process.getPackage(MVP_PACKAGE.id);
if (current !== undefined && stripped(current) === stripped(MVP_PACKAGE)) {
log(`④ 流程包「统一报价」v${current.version} 已在库,内容未变 —— 不存新版`);
} else {
// 保存不生效 —— 激活才是上线闸(不闭合在这一步被拦,种子包若不闭合要吵出来)
const saved = await container.process.savePackage(MVP_PACKAGE);
await container.process.setPackageState(saved.id, saved.version, 'active');
log(
`④ 流程包「统一报价」v${saved.version} 已保存并激活 —— ${saved.nodes.length} 节点,` +
`闭合校验过(不闭合不得激活)${current === undefined ? '' : `;旧版 v${current.version} 原样留着,在途任务照它跑完`}`,
);
}
await container.trigger.upsert(TRIGGER_EVENT);
await container.trigger.upsert(TRIGGER_REGISTRATION);
log(' 触发器已配置:客户RFQ询价(事件)· 人工登记起单(登记)');
/* 邮件通道两条:自动起单的与人工确认的 —— 同一封 RFQ 两条路,起不起单由通道定 */
const { positions } = await container.org.listStaff();
const op = positions.find((p) => p.code === 'OP');
const pm = positions.find((p) => p.code === 'PM');
/*
* 归属键提取 —— 正则要能吃**完整**的单号。
*
* `RFQ-[A-Z0-9]+` 不含连字符:遇到 `RFQ-2607-501` 只抽到 `RFQ-2607` 就停(实测)。
* 而这个键会成为「受理结论」那一行的主键 —— 截断了,同一单的数据就静默分裂成两行,
* 网关读到哪一行全看运气。分段续接才能整个吃下:`RFQ-2607-501` / `RFQ-ABC123` 都对。
*/
const extract = [
{ key: 'customerCode', regex: '\\bC\\d{3}\\b', source: 'both' as const },
{ key: 'rfqRef', regex: 'RFQ-[A-Z0-9]+(?:-[A-Z0-9]+)*', source: 'both' as const },
];
await container.channel.upsert({
id: 'ch-rfq-mail',
name: '公司询价邮箱',
kind: 'mail',
address: 'rfq@we.com',
enabled: true,
extract: [...extract, { key: 'quoteNo', regex: 'Q\\d{6}', source: 'both' }],
mapping: [],
...(op === undefined ? {} : { fallbackPosition: op.id }),
addressWindowMs: 7 * 86_400_000,
});
await container.channel.upsert({
id: 'ch-rfq-review',
name: '询价待确认邮箱',
kind: 'mail',
address: 'rfq-review@we.com',
enabled: true,
extract,
mapping: [],
...(pm === undefined ? {} : { fallbackPosition: pm.id }),
addressWindowMs: 7 * 86_400_000,
});
log(' 邮件通道已配置:公司询价邮箱(OP 兜底)· 询价待确认邮箱(PM 兜底)');
}