@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,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 命令入口:run 经拦截器后按命令类型分发;runMany 一条一个事务地批量执行。
|
|
3
|
+
*/
|
|
4
|
+
import { FlowError } from '../core/errors.ts';
|
|
5
|
+
import type { Actor, BatchResult, FlowCommand, RunResult } from '../types.ts';
|
|
6
|
+
import type { EngineCore } from '../core/context.ts';
|
|
7
|
+
|
|
8
|
+
/** 命令入口:装进引擎核心(core.commands),别的模块经它调用 */
|
|
9
|
+
export function createCommands(core: EngineCore) {
|
|
10
|
+
const { options, withInstance } = core;
|
|
11
|
+
|
|
12
|
+
/** 拦截之后执行命令 */
|
|
13
|
+
async function run(input: FlowCommand, actor: Actor): Promise<RunResult> {
|
|
14
|
+
const command = (await options.intercept?.before?.(input, actor)) ?? input;
|
|
15
|
+
return dispatch(command, actor);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function dispatch(command: FlowCommand, actor: Actor): Promise<RunResult> {
|
|
19
|
+
switch (command.type) {
|
|
20
|
+
case 'start':
|
|
21
|
+
return core.starts.start(command, actor);
|
|
22
|
+
case 'operate':
|
|
23
|
+
return core.ops.operate(command, actor);
|
|
24
|
+
case 'claim':
|
|
25
|
+
return core.ops.operate(
|
|
26
|
+
{ type: 'operate', workItem: command.workItem, operation: 'claim' },
|
|
27
|
+
actor,
|
|
28
|
+
command,
|
|
29
|
+
);
|
|
30
|
+
case 'complete':
|
|
31
|
+
return core.tasks.complete(command, actor);
|
|
32
|
+
case 'transfer':
|
|
33
|
+
case 'reassign':
|
|
34
|
+
return core.ops.operate(
|
|
35
|
+
{
|
|
36
|
+
type: 'operate',
|
|
37
|
+
workItem: command.workItem,
|
|
38
|
+
operation: command.type,
|
|
39
|
+
input: { to: command.to, comment: command.comment },
|
|
40
|
+
},
|
|
41
|
+
actor,
|
|
42
|
+
command,
|
|
43
|
+
);
|
|
44
|
+
case 'withdraw':
|
|
45
|
+
return core.admin.withdraw(command, actor);
|
|
46
|
+
case 'terminate':
|
|
47
|
+
return core.admin.terminate(command, actor);
|
|
48
|
+
case 'suspend':
|
|
49
|
+
case 'resume':
|
|
50
|
+
return core.admin.suspendOrResume(command, actor);
|
|
51
|
+
case 'skipJob':
|
|
52
|
+
return core.jobs.skipJob(command, actor);
|
|
53
|
+
case 'jump':
|
|
54
|
+
return core.admin.jump(command, actor);
|
|
55
|
+
case 'remind':
|
|
56
|
+
return core.admin.remind(command, actor);
|
|
57
|
+
case 'changeData':
|
|
58
|
+
return core.admin.changeData(command, actor);
|
|
59
|
+
case 'migrate':
|
|
60
|
+
return core.admin.migrate(command, actor);
|
|
61
|
+
case 'comment':
|
|
62
|
+
return withInstance(
|
|
63
|
+
command.instance,
|
|
64
|
+
actor,
|
|
65
|
+
async (exec) => {
|
|
66
|
+
exec.addEvent('instance.commented', { comment: command.text });
|
|
67
|
+
},
|
|
68
|
+
command,
|
|
69
|
+
);
|
|
70
|
+
case 'cc':
|
|
71
|
+
if (command.users.length === 0) {
|
|
72
|
+
throw new FlowError('invalid-input', '抄送要选人', { field: 'users', code: 'required' });
|
|
73
|
+
}
|
|
74
|
+
return withInstance(
|
|
75
|
+
command.instance,
|
|
76
|
+
actor,
|
|
77
|
+
async (exec) => {
|
|
78
|
+
await exec.sendCc(
|
|
79
|
+
null,
|
|
80
|
+
command.users.map((user) => ({ user: user as `${bigint}` })),
|
|
81
|
+
command.message,
|
|
82
|
+
);
|
|
83
|
+
},
|
|
84
|
+
command,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function runMany(commands: readonly FlowCommand[], actor: Actor): Promise<BatchResult[]> {
|
|
90
|
+
const results: BatchResult[] = [];
|
|
91
|
+
for (const command of commands) {
|
|
92
|
+
try {
|
|
93
|
+
results.push({ ok: true, result: await run(command, actor) });
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (!(error instanceof FlowError)) throw error;
|
|
96
|
+
results.push({ ok: false, code: error.code, message: error.message });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return results;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return { run, dispatch, runMany };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** createCommands 给出的函数 */
|
|
106
|
+
export type Commands = ReturnType<typeof createCommands>;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 待办上的动作(operations.ts 的插件):认领、转办、委托、加签……经 operate 命令执行,暂存与结束交回给处理。
|
|
3
|
+
*/
|
|
4
|
+
import { fieldStates, formManifest, type TaskNode } from '@zerodep-dev/flow-ts-model';
|
|
5
|
+
import { FlowWorkItem } from '../core/entities.ts';
|
|
6
|
+
import { FlowError } from '../core/errors.ts';
|
|
7
|
+
import { Execution, type WorkItemRow } from '../runtime/execution.ts';
|
|
8
|
+
import { type OperationScope } from './operations.ts';
|
|
9
|
+
import type { Actor, FlowCommand, RunResult } from '../types.ts';
|
|
10
|
+
import type { EngineCore } from '../core/context.ts';
|
|
11
|
+
import { submit, throwFormErrors } from './submission.ts';
|
|
12
|
+
|
|
13
|
+
/** 待办上的动作(operations.ts 的插件):装进引擎核心(core.ops),别的模块经它调用 */
|
|
14
|
+
export function createOps(core: EngineCore) {
|
|
15
|
+
const { options, operations, withInstance, instanceOfItem, isCandidate, itemsOf } = core;
|
|
16
|
+
|
|
17
|
+
/** 动作:重读待办、按 available 判断,再交给动作的 run(实例已锁住) */
|
|
18
|
+
async function operate(
|
|
19
|
+
command: Extract<FlowCommand, { type: 'operate' }>,
|
|
20
|
+
actor: Actor,
|
|
21
|
+
original: FlowCommand = command,
|
|
22
|
+
): Promise<RunResult> {
|
|
23
|
+
const operation = operations.find((o) => o.name === command.operation);
|
|
24
|
+
if (!operation) {
|
|
25
|
+
throw new FlowError('invalid-input', `没有动作 ${command.operation}`, { field: 'operation' });
|
|
26
|
+
}
|
|
27
|
+
return withInstance(
|
|
28
|
+
await instanceOfItem(command.workItem),
|
|
29
|
+
actor,
|
|
30
|
+
async (exec, tx) => {
|
|
31
|
+
const item = await tx.queryable(FlowWorkItem).findOrThrow(command.workItem);
|
|
32
|
+
const scope = scopeOf(exec, item, actor, await itemsOf(tx, item.instanceId));
|
|
33
|
+
if (!operation.available(scope)) {
|
|
34
|
+
const code =
|
|
35
|
+
operation.refusal?.(scope) ??
|
|
36
|
+
(exec.status !== 'active'
|
|
37
|
+
? 'instance-not-active'
|
|
38
|
+
: item.state !== 'open'
|
|
39
|
+
? 'already-handled'
|
|
40
|
+
: 'forbidden');
|
|
41
|
+
throw new FlowError(code, `${actor.id} 现在不能对待办 ${item.id} ${operation.label}`);
|
|
42
|
+
}
|
|
43
|
+
await operation.run(
|
|
44
|
+
{
|
|
45
|
+
...scope,
|
|
46
|
+
exec,
|
|
47
|
+
tx,
|
|
48
|
+
cancel: async (comment) => {
|
|
49
|
+
await tx
|
|
50
|
+
.update(FlowWorkItem)
|
|
51
|
+
.set({ state: 'canceled', completedAt: exec.now, completedBy: actor.id, comment })
|
|
52
|
+
.where((w) => [w.id.eq(item.id), w.state.eq('open')])
|
|
53
|
+
.expectRows(1, () => new FlowError('already-handled', `待办 ${item.id} 已经处理过`))
|
|
54
|
+
.execute();
|
|
55
|
+
},
|
|
56
|
+
finish: (outcome, comment) =>
|
|
57
|
+
core.tasks.finishItem(exec, tx, item, scope.node, outcome, {
|
|
58
|
+
actor: actor.id,
|
|
59
|
+
comment,
|
|
60
|
+
}),
|
|
61
|
+
settle: () => core.tasks.settleParent(exec, tx, item.id, scope.node),
|
|
62
|
+
draftValues: (submitted) => {
|
|
63
|
+
const manifest = formManifest(exec.definition);
|
|
64
|
+
const states = fieldStates(manifest, { access: scope.node.form, read: exec.read });
|
|
65
|
+
const editable = Object.fromEntries(
|
|
66
|
+
Object.entries(submitted).filter(([name]) => states.get(name)?.editable),
|
|
67
|
+
);
|
|
68
|
+
const { errors } = submit(
|
|
69
|
+
manifest,
|
|
70
|
+
states,
|
|
71
|
+
exec.variables,
|
|
72
|
+
editable,
|
|
73
|
+
options.extensions?.fieldTypes,
|
|
74
|
+
);
|
|
75
|
+
throwFormErrors(
|
|
76
|
+
manifest,
|
|
77
|
+
errors.filter((e) => e.code !== 'required'),
|
|
78
|
+
);
|
|
79
|
+
return editable;
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
command.input ?? {},
|
|
83
|
+
);
|
|
84
|
+
},
|
|
85
|
+
original,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 判断动作能不能做时看的:实例的样子取推进现场的(锁住后的) */
|
|
90
|
+
function scopeOf(
|
|
91
|
+
exec: Execution,
|
|
92
|
+
item: WorkItemRow,
|
|
93
|
+
actor: Actor,
|
|
94
|
+
items: readonly WorkItemRow[],
|
|
95
|
+
): OperationScope {
|
|
96
|
+
return {
|
|
97
|
+
item,
|
|
98
|
+
node: exec.node(item.node) as TaskNode,
|
|
99
|
+
instance: { ...exec.instance, status: exec.status },
|
|
100
|
+
actor,
|
|
101
|
+
candidate: isCandidate(item, actor),
|
|
102
|
+
items,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return { operate, scopeOf };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** createOps 给出的函数 */
|
|
110
|
+
export type Ops = ReturnType<typeof createOps>;
|