@zerodep-dev/flow-ts-engine 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/client/client.d.ts +112 -0
- package/dist/client/client.d.ts.map +1 -0
- package/dist/client/client.js +67 -0
- package/dist/client/client.js.map +1 -0
- package/dist/client/endpoint.d.ts +20 -0
- package/dist/client/endpoint.d.ts.map +1 -0
- package/dist/client/endpoint.js +153 -0
- package/dist/client/endpoint.js.map +1 -0
- package/dist/client/http.d.ts +30 -0
- package/dist/client/http.d.ts.map +1 -0
- package/dist/client/http.js +67 -0
- package/dist/client/http.js.map +1 -0
- package/dist/client/index.d.ts +12 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +11 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/port.d.ts +24 -0
- package/dist/client/port.d.ts.map +1 -0
- package/dist/client/port.js +53 -0
- package/dist/client/port.js.map +1 -0
- package/dist/client/protocol.d.ts +26 -0
- package/dist/client/protocol.d.ts.map +1 -0
- package/dist/client/protocol.js +13 -0
- package/dist/client/protocol.js.map +1 -0
- package/dist/commands/admin.d.ts +41 -0
- package/dist/commands/admin.d.ts.map +1 -0
- package/dist/commands/admin.js +228 -0
- package/dist/commands/admin.js.map +1 -0
- package/dist/commands/complete.d.ts +36 -0
- package/dist/commands/complete.d.ts.map +1 -0
- package/dist/commands/complete.js +289 -0
- package/dist/commands/complete.js.map +1 -0
- package/dist/commands/dispatch.d.ts +11 -0
- package/dist/commands/dispatch.d.ts.map +1 -0
- package/dist/commands/dispatch.js +77 -0
- package/dist/commands/dispatch.js.map +1 -0
- package/dist/commands/operate.d.ts +14 -0
- package/dist/commands/operate.d.ts.map +1 -0
- package/dist/commands/operate.js +72 -0
- package/dist/commands/operate.js.map +1 -0
- package/dist/commands/operations.d.ts +75 -0
- package/dist/commands/operations.d.ts.map +1 -0
- package/dist/commands/operations.js +417 -0
- package/dist/commands/operations.js.map +1 -0
- package/dist/commands/signal.d.ts +9 -0
- package/dist/commands/signal.d.ts.map +1 -0
- package/dist/commands/signal.js +78 -0
- package/dist/commands/signal.js.map +1 -0
- package/dist/commands/start.d.ts +41 -0
- package/dist/commands/start.d.ts.map +1 -0
- package/dist/commands/start.js +186 -0
- package/dist/commands/start.js.map +1 -0
- package/dist/commands/submission.d.ts +20 -0
- package/dist/commands/submission.d.ts.map +1 -0
- package/dist/commands/submission.js +29 -0
- package/dist/commands/submission.js.map +1 -0
- package/dist/core/channels.d.ts +13 -0
- package/dist/core/channels.d.ts.map +1 -0
- package/dist/core/channels.js +13 -0
- package/dist/core/channels.js.map +1 -0
- package/dist/core/context.d.ts +76 -0
- package/dist/core/context.d.ts.map +1 -0
- package/dist/core/context.js +148 -0
- package/dist/core/context.js.map +1 -0
- package/dist/core/entities.d.ts +392 -0
- package/dist/core/entities.d.ts.map +1 -0
- package/dist/core/entities.js +282 -0
- package/dist/core/entities.js.map +1 -0
- package/dist/core/errors.d.ts +35 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +18 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/engine.d.ts +146 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +85 -0
- package/dist/engine.js.map +1 -0
- package/dist/extensions.d.ts +198 -0
- package/dist/extensions.d.ts.map +1 -0
- package/dist/extensions.js +7 -0
- package/dist/extensions.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/jobs/jobs.d.ts +27 -0
- package/dist/jobs/jobs.d.ts.map +1 -0
- package/dist/jobs/jobs.js +299 -0
- package/dist/jobs/jobs.js.map +1 -0
- package/dist/notify/collect.d.ts +38 -0
- package/dist/notify/collect.d.ts.map +1 -0
- package/dist/notify/collect.js +100 -0
- package/dist/notify/collect.js.map +1 -0
- package/dist/notify/inbox.d.ts +26 -0
- package/dist/notify/inbox.d.ts.map +1 -0
- package/dist/notify/inbox.js +87 -0
- package/dist/notify/inbox.js.map +1 -0
- package/dist/org/directory.d.ts +74 -0
- package/dist/org/directory.d.ts.map +1 -0
- package/dist/org/directory.js +254 -0
- package/dist/org/directory.js.map +1 -0
- package/dist/org/entities.d.ts +69 -0
- package/dist/org/entities.d.ts.map +1 -0
- package/dist/org/entities.js +39 -0
- package/dist/org/entities.js.map +1 -0
- package/dist/org/index.d.ts +5 -0
- package/dist/org/index.d.ts.map +1 -0
- package/dist/org/index.js +5 -0
- package/dist/org/index.js.map +1 -0
- package/dist/proxy/proxies.d.ts +18 -0
- package/dist/proxy/proxies.d.ts.map +1 -0
- package/dist/proxy/proxies.js +102 -0
- package/dist/proxy/proxies.js.map +1 -0
- package/dist/publish/project.d.ts +45 -0
- package/dist/publish/project.d.ts.map +1 -0
- package/dist/publish/project.js +176 -0
- package/dist/publish/project.js.map +1 -0
- package/dist/publish/publish.d.ts +42 -0
- package/dist/publish/publish.d.ts.map +1 -0
- package/dist/publish/publish.js +274 -0
- package/dist/publish/publish.js.map +1 -0
- package/dist/queries/queries.d.ts +24 -0
- package/dist/queries/queries.d.ts.map +1 -0
- package/dist/queries/queries.js +326 -0
- package/dist/queries/queries.js.map +1 -0
- package/dist/queries/stats.d.ts +9 -0
- package/dist/queries/stats.d.ts.map +1 -0
- package/dist/queries/stats.js +189 -0
- package/dist/queries/stats.js.map +1 -0
- package/dist/runtime/assignees.d.ts +32 -0
- package/dist/runtime/assignees.d.ts.map +1 -0
- package/dist/runtime/assignees.js +93 -0
- package/dist/runtime/assignees.js.map +1 -0
- package/dist/runtime/calls.d.ts +18 -0
- package/dist/runtime/calls.d.ts.map +1 -0
- package/dist/runtime/calls.js +182 -0
- package/dist/runtime/calls.js.map +1 -0
- package/dist/runtime/countersign.d.ts +26 -0
- package/dist/runtime/countersign.d.ts.map +1 -0
- package/dist/runtime/countersign.js +63 -0
- package/dist/runtime/countersign.js.map +1 -0
- package/dist/runtime/execution.d.ts +150 -0
- package/dist/runtime/execution.d.ts.map +1 -0
- package/dist/runtime/execution.js +792 -0
- package/dist/runtime/execution.js.map +1 -0
- package/dist/runtime/next-steps.d.ts +56 -0
- package/dist/runtime/next-steps.d.ts.map +1 -0
- package/dist/runtime/next-steps.js +145 -0
- package/dist/runtime/next-steps.js.map +1 -0
- package/dist/runtime/state.d.ts +26 -0
- package/dist/runtime/state.d.ts.map +1 -0
- package/dist/runtime/state.js +32 -0
- package/dist/runtime/state.js.map +1 -0
- package/dist/testing/scenarios.d.ts +35 -0
- package/dist/testing/scenarios.d.ts.map +1 -0
- package/dist/testing/scenarios.js +63 -0
- package/dist/testing/scenarios.js.map +1 -0
- package/dist/testing/simulate.d.ts +55 -0
- package/dist/testing/simulate.d.ts.map +1 -0
- package/dist/testing/simulate.js +122 -0
- package/dist/testing/simulate.js.map +1 -0
- package/dist/testing/testing.d.ts +31 -0
- package/dist/testing/testing.d.ts.map +1 -0
- package/dist/testing/testing.js +54 -0
- package/dist/testing/testing.js.map +1 -0
- package/dist/types.d.ts +283 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/views.d.ts +317 -0
- package/dist/views.d.ts.map +1 -0
- package/dist/views.js +9 -0
- package/dist/views.js.map +1 -0
- package/package.json +76 -3
- package/src/client/client.ts +194 -0
- package/src/client/endpoint.ts +174 -0
- package/src/client/http.ts +99 -0
- package/src/client/index.ts +12 -0
- package/src/client/port.ts +78 -0
- package/src/client/protocol.ts +36 -0
- package/src/commands/admin.ts +308 -0
- package/src/commands/complete.ts +368 -0
- package/src/commands/dispatch.ts +106 -0
- package/src/commands/operate.ts +110 -0
- package/src/commands/operations.ts +524 -0
- package/src/commands/signal.ts +96 -0
- package/src/commands/start.ts +255 -0
- package/src/commands/submission.ts +51 -0
- package/src/core/channels.ts +14 -0
- package/src/core/context.ts +252 -0
- package/src/core/entities.ts +335 -0
- package/src/core/errors.ts +42 -0
- package/src/engine.ts +247 -0
- package/src/extensions.ts +231 -0
- package/src/index.ts +77 -0
- package/src/jobs/jobs.ts +320 -0
- package/src/notify/collect.ts +155 -0
- package/src/notify/inbox.ts +107 -0
- package/src/org/directory.ts +332 -0
- package/src/org/entities.ts +62 -0
- package/src/org/index.ts +21 -0
- package/src/proxy/proxies.ts +132 -0
- package/src/publish/project.ts +230 -0
- package/src/publish/publish.ts +314 -0
- package/src/queries/queries.ts +427 -0
- package/src/queries/stats.ts +207 -0
- package/src/runtime/assignees.ts +121 -0
- package/src/runtime/calls.ts +196 -0
- package/src/runtime/countersign.ts +83 -0
- package/src/runtime/execution.ts +915 -0
- package/src/runtime/next-steps.ts +213 -0
- package/src/runtime/state.ts +55 -0
- package/src/testing/scenarios.ts +95 -0
- package/src/testing/simulate.ts +190 -0
- package/src/testing/testing.ts +84 -0
- package/src/types.ts +286 -0
- package/src/views.ts +334 -0
- package/README.md +0 -3
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 处理人解析:定义里的来源(指定的人、角色、部门、发起人、同某节点、解析器)→ 候选。
|
|
3
|
+
* 只解析出人时直接落到人(一个人)或进以这些人为候选的收件箱;有角色、部门时进收件箱,由其中的人认领
|
|
4
|
+
*/
|
|
5
|
+
import { isRef, type AssigneeSource } from '@zerodep-dev/flow-ts-model';
|
|
6
|
+
import { FlowError } from '../core/errors.ts';
|
|
7
|
+
import type { Actor, ExtensionContext, FlowEngineOptions, Resolver } from '../types.ts';
|
|
8
|
+
|
|
9
|
+
/** 一个候选 */
|
|
10
|
+
export interface Candidate {
|
|
11
|
+
readonly kind: 'user' | 'role' | 'department' | 'group';
|
|
12
|
+
readonly value: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** 候选 → 存进待办 candidates 列的标记:`role:finance`、`group:岗位:p1` */
|
|
16
|
+
export const candidateToken = (candidate: Candidate): string =>
|
|
17
|
+
`${candidate.kind}:${candidate.value}`;
|
|
18
|
+
|
|
19
|
+
/** 标记 → 候选 */
|
|
20
|
+
export function parseCandidate(token: string): Candidate {
|
|
21
|
+
const colon = token.indexOf(':');
|
|
22
|
+
return { kind: token.slice(0, colon) as Candidate['kind'], value: token.slice(colon + 1) };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 这个人的标记:自己、角色、部门、扩展种类;和待办的 candidates 有交集就是候选 */
|
|
26
|
+
export function actorTokens(actor: Actor): string[] {
|
|
27
|
+
return [
|
|
28
|
+
`user:${actor.id}`,
|
|
29
|
+
...(actor.roles ?? []).map((role) => `role:${role}`),
|
|
30
|
+
...(actor.departments ?? []).map((department) => `department:${department}`),
|
|
31
|
+
...(actor.groups ?? []).map((group) => `group:${group}`),
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 解析时用到的:取值、最后的处理人、发起人、扩展 */
|
|
36
|
+
export interface ResolveScope {
|
|
37
|
+
readonly context: ExtensionContext;
|
|
38
|
+
readonly handledBy: Readonly<Record<string, string>>;
|
|
39
|
+
readonly options: FlowEngineOptions;
|
|
40
|
+
/** 按名字找处理人解析器(引擎选项里注册的,或流程工程的 `logic.名字`) */
|
|
41
|
+
readonly resolver: (name: string) => Resolver | undefined;
|
|
42
|
+
/** 上一步给这个节点选的处理人(`{ pick }` 的来源) */
|
|
43
|
+
readonly picked?: readonly string[] | undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 值或引用 → 文字编号;引用的值是数组时展开 */
|
|
47
|
+
function idsOf(value: unknown, scope: ResolveScope): string[] {
|
|
48
|
+
const raw = isRef(value) ? scope.context.read(value.ref) : value;
|
|
49
|
+
if (raw === undefined || raw === null || raw === '') return [];
|
|
50
|
+
return (Array.isArray(raw) ? raw : [raw]).map(String);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 解析处理人的来源,去重 */
|
|
54
|
+
export async function resolveCandidates(
|
|
55
|
+
sources: readonly AssigneeSource[],
|
|
56
|
+
scope: ResolveScope,
|
|
57
|
+
): Promise<Candidate[]> {
|
|
58
|
+
const found: Candidate[] = [];
|
|
59
|
+
const add = (kind: Candidate['kind'], values: readonly string[]) => {
|
|
60
|
+
for (const value of values) {
|
|
61
|
+
if (!found.some((c) => c.kind === kind && c.value === value)) found.push({ kind, value });
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
for (const source of sources) {
|
|
65
|
+
if ('user' in source) add('user', idsOf(source.user, scope));
|
|
66
|
+
else if ('role' in source) add('role', idsOf(source.role, scope));
|
|
67
|
+
else if ('department' in source) add('department', idsOf(source.department, scope));
|
|
68
|
+
else if ('initiator' in source) add('user', [scope.context.instance.startedBy]);
|
|
69
|
+
else if ('sameAs' in source) {
|
|
70
|
+
const user = scope.handledBy[source.sameAs];
|
|
71
|
+
if (user) add('user', [user]);
|
|
72
|
+
} else if ('resolver' in source) {
|
|
73
|
+
const resolver = scope.resolver(source.resolver);
|
|
74
|
+
if (!resolver) {
|
|
75
|
+
throw new FlowError('invalid-definition', `没有注册处理人解析器 ${source.resolver}`);
|
|
76
|
+
}
|
|
77
|
+
add('user', [...(await resolver({ ...scope.context, args: source.args ?? {} }))].map(String));
|
|
78
|
+
} else if ('group' in source) {
|
|
79
|
+
add(
|
|
80
|
+
'group',
|
|
81
|
+
idsOf(source.id, scope).map((id) => `${source.group}:${id}`),
|
|
82
|
+
);
|
|
83
|
+
} else {
|
|
84
|
+
// 上一步选的人:发起、处理时已经按 pick 的 from、max 查过(next-steps.ts)
|
|
85
|
+
add('user', [...(scope.picked ?? [])]);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return found;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 候选展开成人(会签的参与人、抄送):角色、部门、扩展种类经 org.members 展开 */
|
|
92
|
+
export async function expandToUsers(
|
|
93
|
+
candidates: readonly Candidate[],
|
|
94
|
+
options: FlowEngineOptions,
|
|
95
|
+
): Promise<string[]> {
|
|
96
|
+
const users: string[] = [];
|
|
97
|
+
for (const candidate of candidates) {
|
|
98
|
+
const members =
|
|
99
|
+
candidate.kind === 'user'
|
|
100
|
+
? [candidate.value]
|
|
101
|
+
: await requireOrg(options).members(
|
|
102
|
+
candidate.kind === 'role'
|
|
103
|
+
? { role: candidate.value }
|
|
104
|
+
: candidate.kind === 'department'
|
|
105
|
+
? { department: candidate.value }
|
|
106
|
+
: { group: candidate.value },
|
|
107
|
+
);
|
|
108
|
+
for (const user of members) if (!users.includes(user)) users.push(user);
|
|
109
|
+
}
|
|
110
|
+
return users;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function requireOrg(options: FlowEngineOptions) {
|
|
114
|
+
if (!options.org) {
|
|
115
|
+
throw new FlowError(
|
|
116
|
+
'invalid-definition',
|
|
117
|
+
'要把角色、部门展开成人(会签、抄送),引擎的选项里要给 org',
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
return options.org;
|
|
121
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 调用流程(call 节点):发起子实例、子实例结束后回到父实例、forEach 与异步的等待与汇总;每个命令写回之后由 afterFlush 接着处理。
|
|
3
|
+
*/
|
|
4
|
+
import { isRef, type CallNode } from '@zerodep-dev/flow-ts-model';
|
|
5
|
+
import { FlowInstance, type FlowDb } from '../core/entities.ts';
|
|
6
|
+
import { Execution } from './execution.ts';
|
|
7
|
+
import type { EngineCore } from '../core/context.ts';
|
|
8
|
+
import { variableName } from '../commands/start.ts';
|
|
9
|
+
|
|
10
|
+
/** forEach 的列表:不是数组时当没有 */
|
|
11
|
+
const listOf = (value: unknown): unknown[] => (Array.isArray(value) ? value : []);
|
|
12
|
+
|
|
13
|
+
/** 调用流程(call 节点):装进引擎核心(core.calls),别的模块经它调用 */
|
|
14
|
+
export function createCalls(core: EngineCore) {
|
|
15
|
+
const { definitionOf, execution, latestDefinition } = core;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 调用流程(同步、单个):在同一个事务里发起子实例;子实例当场就结束时直接按它的结果往下走,否则父实例在这个节点等,
|
|
19
|
+
* 子实例结束时 afterFlush 回到父实例。input 的键是子流程的变量,值是父流程这边的值(引用或直接写)
|
|
20
|
+
*/
|
|
21
|
+
async function startChild(parent: Execution, node: CallNode): Promise<void> {
|
|
22
|
+
const definition = await latestDefinition(parent.tx, node.process);
|
|
23
|
+
const round = parent.bumpRound(node.id);
|
|
24
|
+
const sync = node.mode === 'sync';
|
|
25
|
+
// forEach:列表里每一项一个子实例,input 里 `as.字段` 引用那一项
|
|
26
|
+
const items: unknown[] = node.forEach ? listOf(parent.read(node.forEach.ref)) : [undefined];
|
|
27
|
+
const valueOf = (value: unknown, item: unknown) => {
|
|
28
|
+
if (!isRef(value)) return value;
|
|
29
|
+
const as = node.as;
|
|
30
|
+
if (as && (value.ref === as || value.ref.startsWith(`${as}.`))) {
|
|
31
|
+
return value.ref
|
|
32
|
+
.slice(as.length)
|
|
33
|
+
.split('.')
|
|
34
|
+
.filter(Boolean)
|
|
35
|
+
.reduce<unknown>((at, key) => (at as Record<string, unknown> | null)?.[key], item);
|
|
36
|
+
}
|
|
37
|
+
return parent.read(value.ref);
|
|
38
|
+
};
|
|
39
|
+
if (sync) {
|
|
40
|
+
parent.state.calls[node.id] = items.length;
|
|
41
|
+
parent.current.add(node.id);
|
|
42
|
+
}
|
|
43
|
+
const finished: Execution[] = [];
|
|
44
|
+
for (const item of items) {
|
|
45
|
+
const input = Object.fromEntries(
|
|
46
|
+
Object.entries(node.input ?? {}).map(([name, value]) => [
|
|
47
|
+
variableName(name),
|
|
48
|
+
valueOf(value, item),
|
|
49
|
+
]),
|
|
50
|
+
);
|
|
51
|
+
const child = await core.starts.launch(
|
|
52
|
+
parent.tx,
|
|
53
|
+
definition,
|
|
54
|
+
input,
|
|
55
|
+
{ id: parent.instance.startedBy },
|
|
56
|
+
{
|
|
57
|
+
businessKey: parent.instance.businessKey,
|
|
58
|
+
title: null,
|
|
59
|
+
parentId: parent.instance.id,
|
|
60
|
+
parentNode: node.id,
|
|
61
|
+
parentRound: round,
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
const { instance } = await child.flush();
|
|
65
|
+
// 子实例开出的待办也算这个命令的结果(同 Java 版)
|
|
66
|
+
parent.created.push(...child.created);
|
|
67
|
+
parent.addEvent('call.started', {
|
|
68
|
+
node: node.id,
|
|
69
|
+
data: { child: instance.id, process: node.process, title: instance.title },
|
|
70
|
+
});
|
|
71
|
+
if (child.finished) finished.push(child);
|
|
72
|
+
}
|
|
73
|
+
// 异步:发起就往下走;同步:这一轮全在同一个命令里结束了(或列表是空的)就直接回来
|
|
74
|
+
if (!sync) return parent.leave(node.id, null);
|
|
75
|
+
for (const child of finished) await childReturned(parent, node, child);
|
|
76
|
+
if (items.length === 0) await callsSettled(parent, node, []);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** 同步调用的一个子实例结束了:少等一个;这一轮都结束了收 output、往下走 */
|
|
80
|
+
async function childReturned(parent: Execution, node: CallNode, child: Execution) {
|
|
81
|
+
const left = (parent.state.calls[node.id] ?? 1) - 1;
|
|
82
|
+
parent.state.calls[node.id] = left;
|
|
83
|
+
if (left > 0) return;
|
|
84
|
+
await callsSettled(parent, node, [child]);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 这一轮的子实例都结束了:output 收进父实例(forEach 时按发起的先后收成列表),几个子实例的结果都一样才按它,
|
|
89
|
+
* 否则走没写结果的出边。justEnded 是这个命令里刚结束、还没从库里读到的
|
|
90
|
+
*/
|
|
91
|
+
async function callsSettled(parent: Execution, node: CallNode, justEnded: readonly Execution[]) {
|
|
92
|
+
const round = parent.state.rounds[node.id] ?? 1;
|
|
93
|
+
const rows = await parent.tx
|
|
94
|
+
.queryable(FlowInstance)
|
|
95
|
+
.where((i) => [
|
|
96
|
+
i.parentId.eq(parent.instance.id),
|
|
97
|
+
i.parentNode.eq(node.id),
|
|
98
|
+
i.parentRound.eq(round),
|
|
99
|
+
])
|
|
100
|
+
.orderBy((i) => i.id)
|
|
101
|
+
.toList();
|
|
102
|
+
const children = rows.map((row) => {
|
|
103
|
+
const live = justEnded.find((c) => c.instance.id === row.id);
|
|
104
|
+
return {
|
|
105
|
+
id: row.id,
|
|
106
|
+
status: live?.status ?? row.status,
|
|
107
|
+
result: live ? live.result : row.result,
|
|
108
|
+
read: (name: string) => (live ? live.read(name) : row.variables[variableName(name)]),
|
|
109
|
+
};
|
|
110
|
+
});
|
|
111
|
+
for (const [variable, name] of Object.entries(node.output ?? {})) {
|
|
112
|
+
const values = children.map((c) => c.read(name) ?? null);
|
|
113
|
+
parent.setVariables({
|
|
114
|
+
[variableName(variable)]: node.forEach ? values : (values[0] ?? null),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
delete parent.state.calls[node.id];
|
|
118
|
+
parent.current.delete(node.id);
|
|
119
|
+
const first = children[0];
|
|
120
|
+
const same =
|
|
121
|
+
first && children.every((c) => c.status === 'completed' && c.result === first.result);
|
|
122
|
+
const outcome = same ? first.result : null;
|
|
123
|
+
parent.addEvent('call.finished', {
|
|
124
|
+
node: node.id,
|
|
125
|
+
outcome,
|
|
126
|
+
data: { children: children.map((c) => c.id) },
|
|
127
|
+
});
|
|
128
|
+
await parent.leave(node.id, outcome);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** 父实例恢复时:暂停期间这一轮的子实例都结束了的同步调用节点,补上往下走 */
|
|
132
|
+
async function resumeWaitingCalls(parent: Execution) {
|
|
133
|
+
for (const nodeId of Object.keys(parent.state.calls)) {
|
|
134
|
+
const node = parent.graph.node(nodeId);
|
|
135
|
+
if (node?.type !== 'call') continue;
|
|
136
|
+
const round = parent.state.rounds[nodeId] ?? 1;
|
|
137
|
+
const open = await parent.tx
|
|
138
|
+
.queryable(FlowInstance)
|
|
139
|
+
.where((i) => [
|
|
140
|
+
i.parentId.eq(parent.instance.id),
|
|
141
|
+
i.parentNode.eq(nodeId),
|
|
142
|
+
i.parentRound.eq(round),
|
|
143
|
+
i.status.in(['active', 'suspended']),
|
|
144
|
+
])
|
|
145
|
+
.count();
|
|
146
|
+
if (open === 0) await callsSettled(parent, node, []);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 命令写回之后:子实例结束了就锁住父实例、回到它(父实例在等这个调用节点时);
|
|
152
|
+
* 实例被终止、撤回时,还在进行的子实例一起终止
|
|
153
|
+
*/
|
|
154
|
+
async function afterFlush(tx: FlowDb, exec: Execution): Promise<void> {
|
|
155
|
+
if (exec.finished && exec.instance.parentId !== null && exec.instance.parentNode !== null) {
|
|
156
|
+
const parentId = exec.instance.parentId;
|
|
157
|
+
if (await tx.lock(FlowInstance, parentId)) {
|
|
158
|
+
const row = await tx.queryable(FlowInstance).findOrThrow(parentId);
|
|
159
|
+
const parent = await execution(tx, await definitionOf(tx, row.definitionId), row, null);
|
|
160
|
+
const node = parent.graph.node(exec.instance.parentNode);
|
|
161
|
+
// 暂停的父实例不推进(恢复时 resumeWaitingCalls 补上);旧一轮的子实例不算
|
|
162
|
+
if (
|
|
163
|
+
parent.status === 'active' &&
|
|
164
|
+
node?.type === 'call' &&
|
|
165
|
+
node.mode === 'sync' &&
|
|
166
|
+
parent.current.has(node.id) &&
|
|
167
|
+
exec.instance.parentRound === (parent.state.rounds[node.id] ?? 1)
|
|
168
|
+
) {
|
|
169
|
+
await childReturned(parent, node, exec);
|
|
170
|
+
await parent.flush();
|
|
171
|
+
// 父实例往下走开出的待办也算这个命令的结果
|
|
172
|
+
exec.created.push(...parent.created);
|
|
173
|
+
await afterFlush(tx, parent);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (exec.status === 'terminated' || exec.status === 'withdrawn') {
|
|
178
|
+
const children = await tx
|
|
179
|
+
.queryable(FlowInstance)
|
|
180
|
+
.where((i) => [i.parentId.eq(exec.instance.id), i.status.in(['active', 'suspended'])])
|
|
181
|
+
.toList();
|
|
182
|
+
for (const row of children) {
|
|
183
|
+
await tx.lock(FlowInstance, row.id);
|
|
184
|
+
const child = await execution(tx, await definitionOf(tx, row.definitionId), row, null);
|
|
185
|
+
await child.finish('terminated', 'parent-terminated', exec.endComment);
|
|
186
|
+
await child.flush();
|
|
187
|
+
await afterFlush(tx, child);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return { startChild, childReturned, callsSettled, resumeWaitingCalls, afterFlush };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** createCalls 给出的函数 */
|
|
196
|
+
export type Calls = ReturnType<typeof createCalls>;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会签的完成规则(docs/decisions/0001 第 9 节):每处理一份,按这一组的全部待办与排队的人判断结束了没有、结果是什么。
|
|
3
|
+
* - all、sequential:有人选了非通过的结果立即结束,按他的;全部通过才结束
|
|
4
|
+
* - any:第一个人处理完就结束
|
|
5
|
+
* - ratio:同一结果的人数占比达到门槛就结束;有一票否决的结果立即结束;都办完还没达到按 fallback
|
|
6
|
+
* - votes:同一结果的票数(每份待办的 weight)达到 votes 就结束;一票否决、都办完没达到同 ratio
|
|
7
|
+
*/
|
|
8
|
+
import type { MultiInstance, Outcome } from '@zerodep-dev/flow-ts-model';
|
|
9
|
+
import { FlowError } from '../core/errors.ts';
|
|
10
|
+
import type { CountersignRule } from '../types.ts';
|
|
11
|
+
|
|
12
|
+
/** 一组待办里的一份 */
|
|
13
|
+
export interface SignVote {
|
|
14
|
+
readonly state: 'open' | 'waiting' | 'done' | 'canceled';
|
|
15
|
+
readonly outcome: string | null;
|
|
16
|
+
/** 票数;没有时一票 */
|
|
17
|
+
readonly weight?: number | null;
|
|
18
|
+
readonly assignee?: string | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** 判断:还没结束是 undefined,结束了是结果 */
|
|
22
|
+
export function decideCountersign(
|
|
23
|
+
multi: MultiInstance,
|
|
24
|
+
outcomes: readonly Outcome[],
|
|
25
|
+
votes: readonly SignVote[],
|
|
26
|
+
queued: number,
|
|
27
|
+
/** 自定义规则(multi.complete 不是内置的名字时)与刚处理完的人选的结果 */
|
|
28
|
+
custom?: { readonly rule: CountersignRule; readonly node: string; readonly chosen: string },
|
|
29
|
+
): string | undefined {
|
|
30
|
+
const done = votes.filter((v) => v.state === 'done');
|
|
31
|
+
const total = votes.filter((v) => v.state !== 'canceled').length + queued;
|
|
32
|
+
const kindOf = (key: string | null) => outcomes.find((o) => o.key === key)?.kind ?? 'approve';
|
|
33
|
+
const allDone = done.length === total;
|
|
34
|
+
switch (multi.complete) {
|
|
35
|
+
case 'all':
|
|
36
|
+
case 'sequential': {
|
|
37
|
+
const objection = done.find((v) => kindOf(v.outcome) !== 'approve');
|
|
38
|
+
if (objection) return objection.outcome!;
|
|
39
|
+
return allDone ? (done.at(-1)?.outcome ?? undefined) : undefined;
|
|
40
|
+
}
|
|
41
|
+
case 'any':
|
|
42
|
+
return done[0]?.outcome ?? undefined;
|
|
43
|
+
case 'ratio':
|
|
44
|
+
case 'votes': {
|
|
45
|
+
const veto = done.find((v) => outcomes.find((o) => o.key === v.outcome)?.veto);
|
|
46
|
+
if (veto) return veto.outcome!;
|
|
47
|
+
const byVotes = multi.complete === 'votes';
|
|
48
|
+
const counts = new Map<string, number>();
|
|
49
|
+
for (const v of done)
|
|
50
|
+
counts.set(v.outcome!, (counts.get(v.outcome!) ?? 0) + (byVotes ? (v.weight ?? 1) : 1));
|
|
51
|
+
for (const [outcome, count] of counts) {
|
|
52
|
+
const reached = byVotes
|
|
53
|
+
? count >= (multi.votes ?? Infinity)
|
|
54
|
+
: total > 0 && count / total >= (multi.ratio ?? 1) - 1e-9;
|
|
55
|
+
if (reached) return outcome;
|
|
56
|
+
}
|
|
57
|
+
if (!allDone) return undefined;
|
|
58
|
+
if (!multi.fallback) {
|
|
59
|
+
throw new FlowError(
|
|
60
|
+
'invalid-definition',
|
|
61
|
+
'按比例、票数会签都办完了没达到门槛,又没写 fallback',
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
return multi.fallback;
|
|
65
|
+
}
|
|
66
|
+
default: {
|
|
67
|
+
if (!custom) throw new FlowError('invalid-definition', `没有注册会签规则 ${multi.complete}`);
|
|
68
|
+
const decided = custom.rule({
|
|
69
|
+
node: custom.node,
|
|
70
|
+
chosen: custom.chosen,
|
|
71
|
+
items: votes.map((v) => ({
|
|
72
|
+
assignee: v.assignee ?? null,
|
|
73
|
+
state: v.state,
|
|
74
|
+
outcome: v.outcome,
|
|
75
|
+
weight: v.weight ?? null,
|
|
76
|
+
})),
|
|
77
|
+
someoneOpen: !allDone,
|
|
78
|
+
});
|
|
79
|
+
if (decided !== undefined) return decided;
|
|
80
|
+
return allDone ? (multi.fallback ?? custom.chosen) : undefined;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|