/** * 流程包 DSL 与触发器配置(编码规范 §8.1;语义出处 OS 03)。 * * 流程包 = 调用接口 + 两种活动(声明)+ 审批点 + 两种网关;工作流 = 其运行时实例。 * 触发器与流程包**平级**:不入流程包结构、不随流程包版本,配置独立存放。 * * 编排铁律:**连线即映射,画图即校验,不闭合不得发布** —— 存在未完成边、未满足必填 * 或类型冲突的流程包可存草稿、不得发布。校验算法在 `process` 的 domain,本文件只定形状。 */ import { z } from 'zod'; import { activityIdSchema, nodeIdSchema, processPackageIdSchema, staffIdSchema, triggerIdSchema, } from './id.ts'; import { jsonSchemaSchema } from './contract7.ts'; /* ── 负责人四策略(OS 03) ──────────────────────────────────────────── */ export const OWNER_STRATEGIES = ['assigned', 'sole', 'rotation', 'follow-source'] as const; export const ownerStrategySchema = z.enum(OWNER_STRATEGIES); export type OwnerStrategy = z.infer; export const OWNER_STRATEGY_LABEL: Readonly> = { assigned: '指定人', sole: '岗位唯一人', rotation: '轮值', 'follow-source': '来源跟随', }; export const ownerSpecSchema = z.object({ /** 编排声明岗位,起单时确定性解析 */ position: z.string().min(1), strategy: ownerStrategySchema.default('sole'), /** strategy = assigned 时直接绑定员工(慎用) */ staffId: staffIdSchema.optional(), }); export type OwnerSpec = z.infer; /* ── 边与载荷映射 ────────────────────────────────────────────────────── */ /** * 映射来源四类:上游输出口载荷字段 / 流程变量 / 起单载荷 / 常量; * 允许 S-FEEL 表达式做转换(与触发器字段映射同一形式)。 */ export const mappingSourceSchema = z.discriminatedUnion('from', [ z.object({ from: z.literal('output-port'), port: z.string().min(1), path: z.string().min(1) }), z.object({ from: z.literal('workflow-var'), name: z.string().min(1) }), z.object({ from: z.literal('initiation'), path: z.string().min(1) }), z.object({ from: z.literal('constant'), value: z.unknown() }), z.object({ from: z.literal('expression'), sfeel: z.string().min(1) }), ]); export type MappingSource = z.infer; export const fieldMappingSchema = z.object({ /** 下游活动契约 data 的字段路径 */ target: z.string().min(1), source: mappingSourceSchema, }); export type FieldMapping = z.infer; export const edgeSchema = z.object({ id: z.string().min(1), from: nodeIdSchema, to: nodeIdSchema, /** XOR 网关出边的条件(S-FEEL);AND 出边留空 */ condition: z.string().optional(), /** 连一条边即须完成该边映射 —— 必填未映射 = 边未完成 */ mapping: z.array(fieldMappingSchema).default([]), /** 边标签:画布上的出口名(如「TYPE-A」「通过」) */ label: z.string().optional(), /** 回边标记:人裁决后重开新任务实例(同节点、新任务 ID、留痕关联) */ reopen: z.boolean().default(false), }); export type Edge = z.infer; /* ── 失败走向四选一(OS 03) ────────────────────────────────────────── */ export const failureDispositionSchema = z.discriminatedUnion('kind', [ z.object({ kind: z.literal('terminate') }), z.object({ kind: z.literal('reroute'), edgeId: z.string().min(1) }), z.object({ kind: z.literal('escalate'), approvalNodeId: nodeIdSchema }), /** 仅限 AND 分支;静态校验其下游映射不得依赖该分支的必填字段 */ z.object({ kind: z.literal('tolerate') }), ]); export type FailureDisposition = z.infer; export const FAILURE_DISPOSITION_LABEL: Readonly> = { terminate: '终止流', reroute: '改道', escalate: '升审批', tolerate: '容忍', }; /* ── 催办阶梯(时限机制复用) ────────────────────────────────────────── */ export const nudgeLadderSchema = z.object({ /** ISO-8601 duration */ deadline: z.string().min(2), steps: z.array(z.object({ after: z.string().min(2), action: z.enum(['nudge', 'escalate']) })).default([]), }); export type NudgeLadder = z.infer; /* ── 节点 ─────────────────────────────────────────────────────────────── */ export const GATEWAY_KINDS = ['parallel', 'exclusive'] as const; export const gatewayKindSchema = z.enum(GATEWAY_KINDS); export type GatewayKind = z.infer; export const GATEWAY_KIND_LABEL: Readonly> = { parallel: '并行网关', exclusive: '异或网关', }; const nodeBase = { id: nodeIdSchema, label: z.string().min(1), /** 画布坐标 —— 布局引擎的输入;不影响语义 */ layout: z.object({ row: z.number().int(), col: z.number().int() }).optional(), }; export const processNodeSchema = z.discriminatedUnion('kind', [ z.object({ ...nodeBase, kind: z.literal('start') }), z.object({ ...nodeBase, kind: z.literal('end'), /** 流程包显式终点的输出声明 */ output: jsonSchemaSchema.optional(), }), z.object({ ...nodeBase, kind: z.literal('activity'), /** 以声明入流:引用活动库中已安装的活动 */ activityId: activityIdSchema, /** 版本约束:留空 = 装配时取最新生效版 */ activityVersion: z.string().optional(), owner: ownerSpecSchema, failure: failureDispositionSchema.default({ kind: 'terminate' }), /** 委托消息模板,可插值载荷字段;缺省为空 */ message: z.string().optional(), }), z.object({ ...nodeBase, kind: z.literal('approval'), /** 审批点功能属性四件:审批岗位 / 输入装配映射 / 驳回走向 / 催办阶梯 */ approver: ownerSpecSchema, /** 驳回走向:声明式,不由临场决定 */ reject: z.discriminatedUnion('kind', [ z.object({ kind: z.literal('reopen'), nodeId: nodeIdSchema, mapping: z.array(fieldMappingSchema).default([]) }), z.object({ kind: z.literal('terminate') }), ]), nudge: nudgeLadderSchema.optional(), message: z.string().optional(), }), z.object({ ...nodeBase, kind: z.literal('gateway'), gateway: gatewayKindSchema, /** 分叉 / 汇合 */ mode: z.enum(['fork', 'join']), }), ]); export type ProcessNode = z.infer; /* ── 调用接口:流程包的唯一出入口 ───────────────────────────────────── */ export const callInterfaceSchema = z.object({ input: jsonSchemaSchema, /** 显式终点的输出声明 —— 末端输出载荷按此交付、按来源回执 */ output: jsonSchemaSchema, }); export type CallInterface = z.infer; /* ── 流程包 ───────────────────────────────────────────────────────────── */ export const PROCESS_PACKAGE_STATES = ['draft', 'published', 'retired'] as const; export const processPackageStateSchema = z.enum(PROCESS_PACKAGE_STATES); export type ProcessPackageState = z.infer; export const processPackageSchema = z.object({ id: processPackageIdSchema, name: z.string().min(1), version: z.number().int().min(1), state: processPackageStateSchema, callInterface: callInterfaceSchema, nodes: z.array(processNodeSchema).min(2), edges: z.array(edgeSchema), /** 流程变量声明:边映射的 workflow-var 来源 */ variables: z.array(z.object({ name: z.string().min(1), schema: jsonSchemaSchema })).default([]), description: z.string().optional(), }); export type ProcessPackage = z.infer; /* ── 编排校验的报告形状(前端画布即时预览与后端裁决同源) ───────────── */ export const CLOSURE_VIOLATION_KINDS = [ 'edge-unmapped', 'required-unmapped', 'type-conflict', 'dangling-node', 'gateway-unbalanced', 'join-ambiguous-source', 'tolerate-required-dependency', 'unknown-activity', 'missing-owner-position', ] as const; export const closureViolationKindSchema = z.enum(CLOSURE_VIOLATION_KINDS); export type ClosureViolationKind = z.infer; export const closureViolationSchema = z.object({ kind: closureViolationKindSchema, nodeId: nodeIdSchema.optional(), edgeId: z.string().optional(), field: z.string().optional(), message: z.string(), }); export type ClosureViolation = z.infer; export const closureReportSchema = z.object({ publishable: z.boolean(), violations: z.array(closureViolationSchema), }); export type ClosureReport = z.infer; /* ── 触发器三类(OS 3.1) ──────────────────────────────────────────── */ export const TRIGGER_KINDS = ['periodic', 'event', 'registration'] as const; export const triggerKindSchema = z.enum(TRIGGER_KINDS); export type TriggerKind = z.infer; export const TRIGGER_KIND_LABEL: Readonly> = { periodic: '周期触发', event: '事件触发', registration: '登记触发', }; export const triggerConfigSchema = z.object({ id: triggerIdSchema, name: z.string().min(1), enabled: z.boolean().default(true), /** 触发器只知道调用接口,不知流程内部 */ processPackageId: processPackageIdSchema, /** 起单载荷装配:事件信封与载荷字段 → 调用接口的 data 字段;缺必填即起单失败入死信 */ mapping: z.array(fieldMappingSchema).default([]), spec: z.discriminatedUnion('kind', [ z.object({ kind: z.literal('periodic'), /** 日历型:纯日历规则 */ mode: z.literal('calendar'), /** cron 风格的日历表达式 */ calendar: z.string().min(1), }), z.object({ kind: z.literal('periodic'), /** 数据日历型:按台账日期字段 + 偏移 + 过滤条件逐行起单 */ mode: z.literal('data-calendar'), domain: z.string().min(1), object: z.string().min(1), dateField: z.string().min(1), /** 偏移:如 -P3D 表示日期字段前 3 天 */ offset: z.string().min(2), filter: z.string().optional(), }), z.object({ kind: z.literal('event'), /** 命中规则:S-FEEL,对事件信封与载荷字段求值 */ rule: z.string().min(1), eventType: z.enum(['data', 'time', 'human', 'control']).default('data'), }), z.object({ kind: z.literal('registration'), /** 登记表单由调用接口 schema 自动渲染 */ formHint: z.string().optional(), }), ]), }); export type TriggerConfig = z.infer;