@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.
Files changed (219) hide show
  1. package/LICENSE +21 -0
  2. package/dist/.tsbuildinfo +1 -0
  3. package/dist/client/client.d.ts +112 -0
  4. package/dist/client/client.d.ts.map +1 -0
  5. package/dist/client/client.js +67 -0
  6. package/dist/client/client.js.map +1 -0
  7. package/dist/client/endpoint.d.ts +20 -0
  8. package/dist/client/endpoint.d.ts.map +1 -0
  9. package/dist/client/endpoint.js +153 -0
  10. package/dist/client/endpoint.js.map +1 -0
  11. package/dist/client/http.d.ts +30 -0
  12. package/dist/client/http.d.ts.map +1 -0
  13. package/dist/client/http.js +67 -0
  14. package/dist/client/http.js.map +1 -0
  15. package/dist/client/index.d.ts +12 -0
  16. package/dist/client/index.d.ts.map +1 -0
  17. package/dist/client/index.js +11 -0
  18. package/dist/client/index.js.map +1 -0
  19. package/dist/client/port.d.ts +24 -0
  20. package/dist/client/port.d.ts.map +1 -0
  21. package/dist/client/port.js +53 -0
  22. package/dist/client/port.js.map +1 -0
  23. package/dist/client/protocol.d.ts +26 -0
  24. package/dist/client/protocol.d.ts.map +1 -0
  25. package/dist/client/protocol.js +13 -0
  26. package/dist/client/protocol.js.map +1 -0
  27. package/dist/commands/admin.d.ts +41 -0
  28. package/dist/commands/admin.d.ts.map +1 -0
  29. package/dist/commands/admin.js +228 -0
  30. package/dist/commands/admin.js.map +1 -0
  31. package/dist/commands/complete.d.ts +36 -0
  32. package/dist/commands/complete.d.ts.map +1 -0
  33. package/dist/commands/complete.js +289 -0
  34. package/dist/commands/complete.js.map +1 -0
  35. package/dist/commands/dispatch.d.ts +11 -0
  36. package/dist/commands/dispatch.d.ts.map +1 -0
  37. package/dist/commands/dispatch.js +77 -0
  38. package/dist/commands/dispatch.js.map +1 -0
  39. package/dist/commands/operate.d.ts +14 -0
  40. package/dist/commands/operate.d.ts.map +1 -0
  41. package/dist/commands/operate.js +72 -0
  42. package/dist/commands/operate.js.map +1 -0
  43. package/dist/commands/operations.d.ts +75 -0
  44. package/dist/commands/operations.d.ts.map +1 -0
  45. package/dist/commands/operations.js +417 -0
  46. package/dist/commands/operations.js.map +1 -0
  47. package/dist/commands/signal.d.ts +9 -0
  48. package/dist/commands/signal.d.ts.map +1 -0
  49. package/dist/commands/signal.js +78 -0
  50. package/dist/commands/signal.js.map +1 -0
  51. package/dist/commands/start.d.ts +41 -0
  52. package/dist/commands/start.d.ts.map +1 -0
  53. package/dist/commands/start.js +186 -0
  54. package/dist/commands/start.js.map +1 -0
  55. package/dist/commands/submission.d.ts +20 -0
  56. package/dist/commands/submission.d.ts.map +1 -0
  57. package/dist/commands/submission.js +29 -0
  58. package/dist/commands/submission.js.map +1 -0
  59. package/dist/core/channels.d.ts +13 -0
  60. package/dist/core/channels.d.ts.map +1 -0
  61. package/dist/core/channels.js +13 -0
  62. package/dist/core/channels.js.map +1 -0
  63. package/dist/core/context.d.ts +76 -0
  64. package/dist/core/context.d.ts.map +1 -0
  65. package/dist/core/context.js +148 -0
  66. package/dist/core/context.js.map +1 -0
  67. package/dist/core/entities.d.ts +392 -0
  68. package/dist/core/entities.d.ts.map +1 -0
  69. package/dist/core/entities.js +282 -0
  70. package/dist/core/entities.js.map +1 -0
  71. package/dist/core/errors.d.ts +35 -0
  72. package/dist/core/errors.d.ts.map +1 -0
  73. package/dist/core/errors.js +18 -0
  74. package/dist/core/errors.js.map +1 -0
  75. package/dist/engine.d.ts +146 -0
  76. package/dist/engine.d.ts.map +1 -0
  77. package/dist/engine.js +85 -0
  78. package/dist/engine.js.map +1 -0
  79. package/dist/extensions.d.ts +198 -0
  80. package/dist/extensions.d.ts.map +1 -0
  81. package/dist/extensions.js +7 -0
  82. package/dist/extensions.js.map +1 -0
  83. package/dist/index.d.ts +19 -0
  84. package/dist/index.d.ts.map +1 -0
  85. package/dist/index.js +15 -0
  86. package/dist/index.js.map +1 -0
  87. package/dist/jobs/jobs.d.ts +27 -0
  88. package/dist/jobs/jobs.d.ts.map +1 -0
  89. package/dist/jobs/jobs.js +299 -0
  90. package/dist/jobs/jobs.js.map +1 -0
  91. package/dist/notify/collect.d.ts +38 -0
  92. package/dist/notify/collect.d.ts.map +1 -0
  93. package/dist/notify/collect.js +100 -0
  94. package/dist/notify/collect.js.map +1 -0
  95. package/dist/notify/inbox.d.ts +26 -0
  96. package/dist/notify/inbox.d.ts.map +1 -0
  97. package/dist/notify/inbox.js +87 -0
  98. package/dist/notify/inbox.js.map +1 -0
  99. package/dist/org/directory.d.ts +74 -0
  100. package/dist/org/directory.d.ts.map +1 -0
  101. package/dist/org/directory.js +254 -0
  102. package/dist/org/directory.js.map +1 -0
  103. package/dist/org/entities.d.ts +69 -0
  104. package/dist/org/entities.d.ts.map +1 -0
  105. package/dist/org/entities.js +39 -0
  106. package/dist/org/entities.js.map +1 -0
  107. package/dist/org/index.d.ts +5 -0
  108. package/dist/org/index.d.ts.map +1 -0
  109. package/dist/org/index.js +5 -0
  110. package/dist/org/index.js.map +1 -0
  111. package/dist/proxy/proxies.d.ts +18 -0
  112. package/dist/proxy/proxies.d.ts.map +1 -0
  113. package/dist/proxy/proxies.js +102 -0
  114. package/dist/proxy/proxies.js.map +1 -0
  115. package/dist/publish/project.d.ts +45 -0
  116. package/dist/publish/project.d.ts.map +1 -0
  117. package/dist/publish/project.js +176 -0
  118. package/dist/publish/project.js.map +1 -0
  119. package/dist/publish/publish.d.ts +42 -0
  120. package/dist/publish/publish.d.ts.map +1 -0
  121. package/dist/publish/publish.js +274 -0
  122. package/dist/publish/publish.js.map +1 -0
  123. package/dist/queries/queries.d.ts +24 -0
  124. package/dist/queries/queries.d.ts.map +1 -0
  125. package/dist/queries/queries.js +326 -0
  126. package/dist/queries/queries.js.map +1 -0
  127. package/dist/queries/stats.d.ts +9 -0
  128. package/dist/queries/stats.d.ts.map +1 -0
  129. package/dist/queries/stats.js +189 -0
  130. package/dist/queries/stats.js.map +1 -0
  131. package/dist/runtime/assignees.d.ts +32 -0
  132. package/dist/runtime/assignees.d.ts.map +1 -0
  133. package/dist/runtime/assignees.js +93 -0
  134. package/dist/runtime/assignees.js.map +1 -0
  135. package/dist/runtime/calls.d.ts +18 -0
  136. package/dist/runtime/calls.d.ts.map +1 -0
  137. package/dist/runtime/calls.js +182 -0
  138. package/dist/runtime/calls.js.map +1 -0
  139. package/dist/runtime/countersign.d.ts +26 -0
  140. package/dist/runtime/countersign.d.ts.map +1 -0
  141. package/dist/runtime/countersign.js +63 -0
  142. package/dist/runtime/countersign.js.map +1 -0
  143. package/dist/runtime/execution.d.ts +150 -0
  144. package/dist/runtime/execution.d.ts.map +1 -0
  145. package/dist/runtime/execution.js +792 -0
  146. package/dist/runtime/execution.js.map +1 -0
  147. package/dist/runtime/next-steps.d.ts +56 -0
  148. package/dist/runtime/next-steps.d.ts.map +1 -0
  149. package/dist/runtime/next-steps.js +145 -0
  150. package/dist/runtime/next-steps.js.map +1 -0
  151. package/dist/runtime/state.d.ts +26 -0
  152. package/dist/runtime/state.d.ts.map +1 -0
  153. package/dist/runtime/state.js +32 -0
  154. package/dist/runtime/state.js.map +1 -0
  155. package/dist/testing/scenarios.d.ts +35 -0
  156. package/dist/testing/scenarios.d.ts.map +1 -0
  157. package/dist/testing/scenarios.js +63 -0
  158. package/dist/testing/scenarios.js.map +1 -0
  159. package/dist/testing/simulate.d.ts +55 -0
  160. package/dist/testing/simulate.d.ts.map +1 -0
  161. package/dist/testing/simulate.js +122 -0
  162. package/dist/testing/simulate.js.map +1 -0
  163. package/dist/testing/testing.d.ts +31 -0
  164. package/dist/testing/testing.d.ts.map +1 -0
  165. package/dist/testing/testing.js +54 -0
  166. package/dist/testing/testing.js.map +1 -0
  167. package/dist/types.d.ts +283 -0
  168. package/dist/types.d.ts.map +1 -0
  169. package/dist/types.js +2 -0
  170. package/dist/types.js.map +1 -0
  171. package/dist/views.d.ts +317 -0
  172. package/dist/views.d.ts.map +1 -0
  173. package/dist/views.js +9 -0
  174. package/dist/views.js.map +1 -0
  175. package/package.json +76 -3
  176. package/src/client/client.ts +194 -0
  177. package/src/client/endpoint.ts +174 -0
  178. package/src/client/http.ts +99 -0
  179. package/src/client/index.ts +12 -0
  180. package/src/client/port.ts +78 -0
  181. package/src/client/protocol.ts +36 -0
  182. package/src/commands/admin.ts +308 -0
  183. package/src/commands/complete.ts +368 -0
  184. package/src/commands/dispatch.ts +106 -0
  185. package/src/commands/operate.ts +110 -0
  186. package/src/commands/operations.ts +524 -0
  187. package/src/commands/signal.ts +96 -0
  188. package/src/commands/start.ts +255 -0
  189. package/src/commands/submission.ts +51 -0
  190. package/src/core/channels.ts +14 -0
  191. package/src/core/context.ts +252 -0
  192. package/src/core/entities.ts +335 -0
  193. package/src/core/errors.ts +42 -0
  194. package/src/engine.ts +247 -0
  195. package/src/extensions.ts +231 -0
  196. package/src/index.ts +77 -0
  197. package/src/jobs/jobs.ts +320 -0
  198. package/src/notify/collect.ts +155 -0
  199. package/src/notify/inbox.ts +107 -0
  200. package/src/org/directory.ts +332 -0
  201. package/src/org/entities.ts +62 -0
  202. package/src/org/index.ts +21 -0
  203. package/src/proxy/proxies.ts +132 -0
  204. package/src/publish/project.ts +230 -0
  205. package/src/publish/publish.ts +314 -0
  206. package/src/queries/queries.ts +427 -0
  207. package/src/queries/stats.ts +207 -0
  208. package/src/runtime/assignees.ts +121 -0
  209. package/src/runtime/calls.ts +196 -0
  210. package/src/runtime/countersign.ts +83 -0
  211. package/src/runtime/execution.ts +915 -0
  212. package/src/runtime/next-steps.ts +213 -0
  213. package/src/runtime/state.ts +55 -0
  214. package/src/testing/scenarios.ts +95 -0
  215. package/src/testing/simulate.ts +190 -0
  216. package/src/testing/testing.ts +84 -0
  217. package/src/types.ts +286 -0
  218. package/src/views.ts +334 -0
  219. 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>;