bazis 0.130.0 → 0.132.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/cli/templates/process.ts +1 -1
- package/core/agent/AgentToolExecutor.ts +3 -0
- package/core/agent/Tool.contract.ts +1 -1
- package/core/agent/index.ts +1 -0
- package/core/agent/metadata.ts +9 -0
- package/core/agent/semantic.ts +8 -1
- package/core/infra/connectors/codex/CodexAppServerClient.ts +1 -1
- package/core/workflow/{runtime_module/config/Runtime.config.ts → contracts/Workflow.config.ts} +15 -6
- package/core/workflow/contracts/WorkflowExtensions.ts +40 -0
- package/core/workflow/contracts/WorkflowInputValidator.ts +6 -5
- package/core/workflow/contracts/types.ts +37 -1
- package/core/workflow/definitions_module/WorkflowDefinitions.module.ts +18 -3
- package/core/workflow/definitions_module/access/Definitions.permissions.ts +4 -3
- package/core/workflow/definitions_module/access/Folder.references.ts +37 -0
- package/core/workflow/definitions_module/access/Process.references.ts +1 -1
- package/core/workflow/definitions_module/access/ProcessFolder.lookup.ts +20 -0
- package/core/workflow/definitions_module/code/CatalogImport.ts +95 -0
- package/core/workflow/definitions_module/code/CodeProcessPublisher.ts +26 -16
- package/core/workflow/definitions_module/code/codeProcesses.ts +7 -3
- package/core/workflow/definitions_module/code/defineProcess.ts +10 -4
- package/core/workflow/definitions_module/events/Definitions.events.ts +16 -0
- package/core/workflow/definitions_module/http/Processes.controller.ts +72 -25
- package/core/workflow/definitions_module/model/Definitions.dbContext.ts +2 -2
- package/core/workflow/definitions_module/model/WfProcess.model.ts +19 -5
- package/core/workflow/definitions_module/model/WfVersion.model.ts +1 -1
- package/core/workflow/definitions_module/services/IWorkflowDefinitions.service.ts +49 -14
- package/core/workflow/definitions_module/services/WorkflowDefinitions.service.ts +128 -45
- package/core/workflow/generated/deps.ts +15 -5
- package/core/workflow/index.ts +17 -3
- package/core/workflow/internal/WorkflowEvents.ts +23 -0
- package/core/workflow/internal/WorkflowLanguages.ts +35 -0
- package/core/workflow/internal/canonicalJson.ts +8 -0
- package/core/workflow/internal/http.ts +53 -5
- package/core/workflow/runtime_module/WorkflowRuntime.module.ts +0 -2
- package/core/workflow/runtime_module/access/Runtime.permissions.ts +5 -3
- package/core/workflow/runtime_module/background/WorkflowRetention.ts +1 -1
- package/core/workflow/runtime_module/background/WorkflowWorker.ts +2 -2
- package/core/workflow/runtime_module/events/Runtime.events.ts +30 -3
- package/core/workflow/runtime_module/http/Events.controller.ts +2 -1
- package/core/workflow/runtime_module/http/Instances.controller.ts +32 -21
- package/core/workflow/runtime_module/internal/Schedules.ts +2 -2
- package/core/workflow/runtime_module/internal/ToolStep.ts +42 -10
- package/core/workflow/runtime_module/internal/WorkflowEngine.ts +83 -31
- package/core/workflow/runtime_module/internal/WorkflowJobs.ts +2 -4
- package/core/workflow/runtime_module/model/Runtime.dbContext.ts +2 -2
- package/core/workflow/runtime_module/model/WfHistory.model.ts +2 -2
- package/core/workflow/runtime_module/model/WfInstance.model.ts +28 -3
- package/core/workflow/runtime_module/model/WfJob.model.ts +17 -1
- package/core/workflow/runtime_module/model/WfSubscription.model.ts +1 -1
- package/core/workflow/runtime_module/model/WfToken.model.ts +11 -1
- package/core/workflow/runtime_module/services/IWorkflowRuntime.service.ts +19 -6
- package/core/workflow/runtime_module/services/WorkflowRuntime.service.ts +20 -17
- package/core/workflow/tasks_module/events/Tasks.events.ts +15 -0
- package/core/workflow/tasks_module/http/Tasks.controller.ts +21 -18
- package/core/workflow/tasks_module/model/Tasks.dbContext.ts +2 -2
- package/core/workflow/tasks_module/model/WfTask.model.ts +8 -4
- package/core/workflow/tasks_module/services/IWorkflowTasks.service.ts +4 -4
- package/core/workflow/tasks_module/services/WorkflowTasks.service.ts +11 -8
- package/library/openapi/codegen.ts +12 -0
- package/library/validation/types/ValidatorOptions.ts +12 -0
- package/library/workflow/ProcessDocument.ts +59 -23
- package/library/workflow/expressionSyntax.ts +243 -0
- package/library/workflow/expressions.ts +343 -0
- package/library/workflow/formFields.ts +120 -34
- package/library/workflow/index.ts +48 -9
- package/library/workflow/localized.ts +35 -0
- package/library/workflow/masking.ts +58 -0
- package/library/workflow/mermaid.ts +18 -8
- package/library/workflow/nodeKinds.ts +88 -17
- package/library/workflow/palette.ts +108 -0
- package/library/workflow/paths.ts +31 -6
- package/library/workflow/structure.ts +282 -0
- package/library/workflow/upgradeDocument.ts +84 -0
- package/library/workflow/validateProcess.ts +130 -48
- package/package.json +1 -1
- package/library/workflow/conditions.ts +0 -44
|
@@ -7,13 +7,14 @@ export type StartedVia = "manual" | "event" | "schedule";
|
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* One run of a process. Bound to the version it started with: a new publication
|
|
10
|
-
* does not touch it. Its data: the input, the variables
|
|
11
|
-
*
|
|
10
|
+
* does not touch it. Its data: the input, the variables and the result of each
|
|
11
|
+
* step by node id.
|
|
12
12
|
*/
|
|
13
13
|
@Index(["partition", "status"])
|
|
14
14
|
@Index(["processKey", "status"])
|
|
15
15
|
@Index(["status", "endedAt"])
|
|
16
|
-
@
|
|
16
|
+
@Index(["test", "createdAt"])
|
|
17
|
+
@Entity({ table: "bazis_wf2_run_instances" })
|
|
17
18
|
export class WfInstance {
|
|
18
19
|
@UUID({ version: "v7" })
|
|
19
20
|
id = "";
|
|
@@ -45,6 +46,30 @@ export class WfInstance {
|
|
|
45
46
|
@Column({ type: "json" })
|
|
46
47
|
nodes: Record<string, unknown> = {};
|
|
47
48
|
|
|
49
|
+
/** What the process gave back: its `out` variables when it completed. */
|
|
50
|
+
@Column({ type: "json", nullable: true })
|
|
51
|
+
output: Record<string, unknown> | null = null;
|
|
52
|
+
|
|
53
|
+
/** `$event`: the data of the event that started or last continued it. */
|
|
54
|
+
@Column({ type: "json", nullable: true })
|
|
55
|
+
event: unknown = null;
|
|
56
|
+
|
|
57
|
+
/** `$trigger`: how it was started. */
|
|
58
|
+
@Column({ type: "json", nullable: true })
|
|
59
|
+
trigger: Record<string, unknown> | null = null;
|
|
60
|
+
|
|
61
|
+
/** A trial run (WORKFLOW-002 §5.4, stage 5). */
|
|
62
|
+
@Column({ type: "boolean" })
|
|
63
|
+
test = false;
|
|
64
|
+
|
|
65
|
+
/** The instance whose `callProcess` started this one (stage 3). */
|
|
66
|
+
@Index()
|
|
67
|
+
@Column({ type: "text", nullable: true })
|
|
68
|
+
parentInstanceId: string | null = null;
|
|
69
|
+
|
|
70
|
+
@Column({ type: "text", nullable: true })
|
|
71
|
+
parentTokenId: string | null = null;
|
|
72
|
+
|
|
48
73
|
@Index()
|
|
49
74
|
@Column({ type: "text", nullable: true })
|
|
50
75
|
businessKey: string | null = null;
|
|
@@ -9,7 +9,7 @@ export type JobKind = "tool" | "timer" | "retry" | "schedule";
|
|
|
9
9
|
* token. Application instances claim it with `FOR UPDATE SKIP LOCKED`.
|
|
10
10
|
*/
|
|
11
11
|
@ForeignKey(() => WfInstance, { properties: ["instanceId"], onDelete: "cascade" })
|
|
12
|
-
@Entity({ table: "
|
|
12
|
+
@Entity({ table: "bazis_wf2_run_jobs" })
|
|
13
13
|
export class WfJob {
|
|
14
14
|
@UUID({ version: "v7" })
|
|
15
15
|
id = "";
|
|
@@ -34,6 +34,22 @@ export class WfJob {
|
|
|
34
34
|
@Column({ type: "datetime" })
|
|
35
35
|
dueAt = new Date(0);
|
|
36
36
|
|
|
37
|
+
/** What a job carries besides its token: an event to deliver (stage 4). */
|
|
38
|
+
@Column({ type: "json", nullable: true })
|
|
39
|
+
payload: unknown = null;
|
|
40
|
+
|
|
41
|
+
/** Jobs with one key merge into the last: `debounce` of an event start (stage 4). */
|
|
42
|
+
@Index()
|
|
43
|
+
@Column({ type: "text", nullable: true })
|
|
44
|
+
dedupeKey: string | null = null;
|
|
45
|
+
|
|
46
|
+
/** The partition and the start node of a `schedule` job (stage 4). */
|
|
47
|
+
@Column({ type: "text", nullable: true })
|
|
48
|
+
partition: string | null = null;
|
|
49
|
+
|
|
50
|
+
@Column({ type: "text", nullable: true })
|
|
51
|
+
nodeId: string | null = null;
|
|
52
|
+
|
|
37
53
|
@Column({ type: "integer" })
|
|
38
54
|
attempts = 0;
|
|
39
55
|
|
|
@@ -4,7 +4,7 @@ import { WfInstance } from "./WfInstance.model";
|
|
|
4
4
|
/** A token waiting for an event: `eventWait` or `boundaryEvent`. Matched by key and correlation. */
|
|
5
5
|
@ForeignKey(() => WfInstance, { properties: ["instanceId"], onDelete: "cascade" })
|
|
6
6
|
@Index(["eventKey", "correlation"])
|
|
7
|
-
@Entity({ table: "
|
|
7
|
+
@Entity({ table: "bazis_wf2_run_subscriptions" })
|
|
8
8
|
export class WfSubscription {
|
|
9
9
|
@UUID({ version: "v7" })
|
|
10
10
|
id = "";
|
|
@@ -10,7 +10,7 @@ export type TokenStatus = "active" | "waiting" | "done" | "cancelled" | "failed"
|
|
|
10
10
|
/** Where execution is. After a parallel gateway an instance has several. Its id is the idempotency key of a Tool call. */
|
|
11
11
|
@ForeignKey(() => WfInstance, { properties: ["instanceId"], onDelete: "cascade" })
|
|
12
12
|
@Index(["instanceId", "status"])
|
|
13
|
-
@Entity({ table: "
|
|
13
|
+
@Entity({ table: "bazis_wf2_run_tokens" })
|
|
14
14
|
export class WfToken {
|
|
15
15
|
@UUID({ version: "v7" })
|
|
16
16
|
id = "";
|
|
@@ -31,6 +31,16 @@ export class WfToken {
|
|
|
31
31
|
@Column({ type: "json", nullable: true })
|
|
32
32
|
output: unknown = null;
|
|
33
33
|
|
|
34
|
+
/** The loop token this one runs inside, with its iteration and what only the body sees (stage 3). */
|
|
35
|
+
@Column({ type: "text", nullable: true })
|
|
36
|
+
parentTokenId: string | null = null;
|
|
37
|
+
|
|
38
|
+
@Column({ type: "integer", nullable: true })
|
|
39
|
+
iteration: number | null = null;
|
|
40
|
+
|
|
41
|
+
@Column({ type: "json", nullable: true })
|
|
42
|
+
locals: Record<string, unknown> | null = null;
|
|
43
|
+
|
|
34
44
|
@Column({ type: "createdAt" })
|
|
35
45
|
createdAt = new Date(0);
|
|
36
46
|
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { AccessFilter } from "../../../access";
|
|
2
2
|
import type { FormIssue } from "../../../../library/workflow";
|
|
3
|
-
import type { WorkflowActor, WorkflowPage, WorkflowPaging } from "../../contracts/types";
|
|
3
|
+
import type { WorkflowActor, WorkflowAuditOptions, WorkflowPage, WorkflowPaging } from "../../contracts/types";
|
|
4
4
|
import type { HistoryType } from "../model/WfHistory.model";
|
|
5
5
|
import type { InstanceStatus, StartedVia } from "../model/WfInstance.model";
|
|
6
6
|
import type { TokenStatus } from "../model/WfToken.model";
|
|
7
7
|
|
|
8
|
-
export interface StartInstance {
|
|
8
|
+
export interface StartInstance extends WorkflowAuditOptions {
|
|
9
9
|
readonly input?: unknown;
|
|
10
10
|
/** The application's scope: a space, an organization. */
|
|
11
11
|
readonly partition?: string | null;
|
|
@@ -13,6 +13,17 @@ export interface StartInstance {
|
|
|
13
13
|
readonly businessKey?: string | null;
|
|
14
14
|
/** On whose behalf the steps run. */
|
|
15
15
|
readonly actor: WorkflowActor;
|
|
16
|
+
/** The language of the request, for the application's validator; `workflow.language` by default. */
|
|
17
|
+
readonly language?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface CancelInstance extends WorkflowAuditOptions {
|
|
21
|
+
readonly reason?: string | null;
|
|
22
|
+
readonly actor: WorkflowActor;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface RetryInstance extends WorkflowAuditOptions {
|
|
26
|
+
readonly actor: WorkflowActor;
|
|
16
27
|
}
|
|
17
28
|
|
|
18
29
|
export interface InstanceSummary {
|
|
@@ -43,6 +54,8 @@ export interface InstanceDetails extends InstanceSummary {
|
|
|
43
54
|
readonly input: Record<string, unknown>;
|
|
44
55
|
readonly vars: Record<string, unknown>;
|
|
45
56
|
readonly nodes: Record<string, unknown>;
|
|
57
|
+
/** What the process gave back: its `out` variables; `null` until it completes. */
|
|
58
|
+
readonly output: Record<string, unknown> | null;
|
|
46
59
|
/** Every token, oldest first. */
|
|
47
60
|
readonly tokens: readonly TokenDetails[];
|
|
48
61
|
}
|
|
@@ -57,7 +70,7 @@ export interface HistoryEntry {
|
|
|
57
70
|
readonly at: Date;
|
|
58
71
|
}
|
|
59
72
|
|
|
60
|
-
export interface EmitOptions {
|
|
73
|
+
export interface EmitOptions extends WorkflowAuditOptions {
|
|
61
74
|
/** Which waits it is for: `order:42`. Waits without a correlation take any event of the key. */
|
|
62
75
|
readonly correlation?: string | null;
|
|
63
76
|
/** Only the waits of this partition; it is also the partition of instances the event starts. */
|
|
@@ -92,11 +105,11 @@ export interface InstanceListQuery extends WorkflowPaging {
|
|
|
92
105
|
export abstract class IWorkflowRuntime {
|
|
93
106
|
/** Starts the latest version of a process from its manual start. */
|
|
94
107
|
abstract start(processKey: string, options: StartInstance): Promise<InstanceDetails>;
|
|
95
|
-
abstract cancel(instanceId: string, options:
|
|
108
|
+
abstract cancel(instanceId: string, options: CancelInstance): Promise<InstanceDetails>;
|
|
96
109
|
/** Retries the failed steps of a `failed` instance. */
|
|
97
|
-
abstract retry(instanceId: string, options:
|
|
110
|
+
abstract retry(instanceId: string, options: RetryInstance): Promise<InstanceDetails>;
|
|
98
111
|
/** Finishes a user task's wait with the form output and moves on; `wrong_status` when the token no longer waits. */
|
|
99
|
-
abstract completeToken(tokenId: string, output: unknown, actor: WorkflowActor): Promise<InstanceDetails>;
|
|
112
|
+
abstract completeToken(tokenId: string, output: unknown, actor: WorkflowActor, options?: { readonly language?: string }): Promise<InstanceDetails>;
|
|
100
113
|
/**
|
|
101
114
|
* Delivers an event: starts processes whose latest version has an
|
|
102
115
|
* `eventStart` for the key, and moves the instances waiting for it.
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { grantsToWhere } from "../../../access";
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
2
|
+
import { paginate } from "../../../orm";
|
|
3
|
+
import { inputSpecOf, type FormIssue, type FormSpec } from "../../../../library/workflow";
|
|
4
|
+
import { isUuid, pageQueryOf, type WorkflowActor, type WorkflowPage } from "../../contracts/types";
|
|
4
5
|
import { WorkflowError } from "../../contracts/WorkflowError";
|
|
5
6
|
import type { ServiceProvider } from "../../../di";
|
|
6
7
|
import { WorkflowInputValidator } from "../../contracts/WorkflowInputValidator";
|
|
7
8
|
import type { IWorkflowDefinitions } from "../../definitions_module/services/IWorkflowDefinitions.service";
|
|
9
|
+
import type { WorkflowLanguages } from "../../internal/WorkflowLanguages";
|
|
8
10
|
import type { WorkflowEngine } from "../internal/WorkflowEngine";
|
|
9
11
|
import type { RuntimeDbContext } from "../model/Runtime.dbContext";
|
|
10
12
|
import type { WfHistory } from "../model/WfHistory.model";
|
|
@@ -12,12 +14,14 @@ import type { WfInstance } from "../model/WfInstance.model";
|
|
|
12
14
|
import type { WfToken } from "../model/WfToken.model";
|
|
13
15
|
import {
|
|
14
16
|
IWorkflowRuntime,
|
|
17
|
+
type CancelInstance,
|
|
15
18
|
type EmitOptions,
|
|
16
19
|
type EmitResult,
|
|
17
20
|
type HistoryEntry,
|
|
18
21
|
type InstanceDetails,
|
|
19
22
|
type InstanceListQuery,
|
|
20
23
|
type InstanceSummary,
|
|
24
|
+
type RetryInstance,
|
|
21
25
|
type StartInstance,
|
|
22
26
|
} from "./IWorkflowRuntime.service";
|
|
23
27
|
|
|
@@ -31,6 +35,7 @@ export class WorkflowRuntime extends IWorkflowRuntime {
|
|
|
31
35
|
private readonly definitions: IWorkflowDefinitions,
|
|
32
36
|
/** The scope: the input validator is asked here, so that the application's own registration replaces the default. */
|
|
33
37
|
private readonly services: ServiceProvider,
|
|
38
|
+
private readonly languages: WorkflowLanguages,
|
|
34
39
|
) {
|
|
35
40
|
super();
|
|
36
41
|
}
|
|
@@ -42,38 +47,38 @@ export class WorkflowRuntime extends IWorkflowRuntime {
|
|
|
42
47
|
const version = await this.definitions.latest(processKey);
|
|
43
48
|
const start = Object.entries(version.document.nodes).find(([, node]) => node.kind === "manualStart");
|
|
44
49
|
if (!start) throw new WorkflowError("wrong_status", `Process ${processKey} has no manual start`);
|
|
45
|
-
const { value, issues } = await this.validator().validate(version.document
|
|
50
|
+
const { value, issues } = await this.validator().validate(inputSpecOf(version.document), options.input, { purpose: "input", processKey, partition, language: options.language ?? this.languages.default, actor: options.actor });
|
|
46
51
|
if (issues.length > 0) throw new WorkflowError("invalid_input", "The process input is invalid", issues);
|
|
47
|
-
const id = await this.engine.begin(this.engine.cache(version), start[0], { input: value, partition, businessKey, startedVia: "manual", actor: options.actor });
|
|
52
|
+
const id = await this.engine.begin(this.engine.cache(version), start[0], { input: value, partition, businessKey, startedVia: "manual", actor: options.actor, request: options.request ?? null });
|
|
48
53
|
return await this.instance(id);
|
|
49
54
|
}
|
|
50
55
|
|
|
51
|
-
async cancel(instanceId: string, options:
|
|
56
|
+
async cancel(instanceId: string, options: CancelInstance): Promise<InstanceDetails> {
|
|
52
57
|
actorOf(options.actor);
|
|
53
58
|
const reason = optionalText(options.reason, "reason", 1000);
|
|
54
59
|
if (!isUuid(instanceId)) throw notFound(instanceId);
|
|
55
60
|
const done = await this.engine.inStep(instanceId, async (step) => {
|
|
56
61
|
if (step.instance.status !== "running" && step.instance.status !== "failed") throw new WorkflowError("wrong_status", `Instance is ${step.instance.status}`);
|
|
57
|
-
await this.engine.cancel(step, reason, options.actor);
|
|
62
|
+
await this.engine.cancel(step, reason, options.actor, options.request ?? null);
|
|
58
63
|
return true;
|
|
59
64
|
});
|
|
60
65
|
if (done === null) throw notFound(instanceId);
|
|
61
66
|
return await this.instance(instanceId);
|
|
62
67
|
}
|
|
63
68
|
|
|
64
|
-
async retry(instanceId: string, options:
|
|
69
|
+
async retry(instanceId: string, options: RetryInstance): Promise<InstanceDetails> {
|
|
65
70
|
actorOf(options.actor);
|
|
66
71
|
if (!isUuid(instanceId)) throw notFound(instanceId);
|
|
67
72
|
const done = await this.engine.inStep(instanceId, async (step) => {
|
|
68
73
|
if (step.instance.status !== "failed") throw new WorkflowError("wrong_status", `Only a failed instance is retried; this one is ${step.instance.status}`);
|
|
69
|
-
await this.engine.retry(step, options.actor);
|
|
74
|
+
await this.engine.retry(step, options.actor, options.request ?? null);
|
|
70
75
|
return true;
|
|
71
76
|
});
|
|
72
77
|
if (done === null) throw notFound(instanceId);
|
|
73
78
|
return await this.instance(instanceId);
|
|
74
79
|
}
|
|
75
80
|
|
|
76
|
-
async completeToken(tokenId: string, output: unknown, actor: WorkflowActor): Promise<InstanceDetails> {
|
|
81
|
+
async completeToken(tokenId: string, output: unknown, actor: WorkflowActor, options: { readonly language?: string } = {}): Promise<InstanceDetails> {
|
|
77
82
|
actorOf(actor);
|
|
78
83
|
if (!isUuid(tokenId)) throw new WorkflowError("not_found", `Token ${tokenId} does not exist`);
|
|
79
84
|
const token = await this.db.tokens.asNoTracking().firstOrDefault((t) => t.id.eq(tokenId));
|
|
@@ -84,7 +89,7 @@ export class WorkflowRuntime extends IWorkflowRuntime {
|
|
|
84
89
|
if (node?.kind !== "userTask" || !current || current.status !== "waiting" || step.instance.status !== "running") {
|
|
85
90
|
throw new WorkflowError("wrong_status", "The token does not wait for a task");
|
|
86
91
|
}
|
|
87
|
-
const { value, issues } = await this.validator().validate(node.config?.form as FormSpec | undefined, output, { purpose: "task", processKey: step.instance.processKey, partition: step.instance.partition });
|
|
92
|
+
const { value, issues } = await this.validator().validate(node.config?.form as FormSpec | undefined, output, { purpose: "task", processKey: step.instance.processKey, partition: step.instance.partition, language: options.language ?? this.languages.default, actor });
|
|
88
93
|
if (issues.length > 0) throw new WorkflowError("invalid_input", "The task form is invalid", issues);
|
|
89
94
|
await this.engine.complete(step, tokenId, value, actor);
|
|
90
95
|
return true;
|
|
@@ -120,20 +125,19 @@ export class WorkflowRuntime extends IWorkflowRuntime {
|
|
|
120
125
|
for (const [nodeId, node] of Object.entries(version.document.nodes)) {
|
|
121
126
|
if (node.kind !== "eventStart" || node.config?.event !== eventKey) continue;
|
|
122
127
|
// An event carries more than a process needs: fields the input does not declare are dropped, not refused.
|
|
123
|
-
const { value, issues: all } = await this.validator().validate(version.document
|
|
128
|
+
const { value, issues: all } = await this.validator().validate(inputSpecOf(version.document), data, { purpose: "input", processKey: version.key, partition, language: this.languages.default, actor });
|
|
124
129
|
const issues = all.filter((issue) => issue.code !== "unknown");
|
|
125
130
|
if (issues.length > 0) {
|
|
126
131
|
rejected.push({ processKey: version.key, issues });
|
|
127
132
|
continue;
|
|
128
133
|
}
|
|
129
|
-
started.push(await this.engine.begin(this.engine.cache(version), nodeId, { input: value, partition, businessKey: correlation, startedVia: "event", actor }));
|
|
134
|
+
started.push(await this.engine.begin(this.engine.cache(version), nodeId, { input: value, partition, businessKey: correlation, startedVia: "event", actor, request: options.request ?? null, event: { key: eventKey, payload: data } }));
|
|
130
135
|
}
|
|
131
136
|
}
|
|
132
137
|
return { started, continued: [...continued], rejected };
|
|
133
138
|
}
|
|
134
139
|
|
|
135
140
|
async instances(query: InstanceListQuery = {}): Promise<WorkflowPage<InstanceSummary>> {
|
|
136
|
-
const { limit, offset } = pagingOf(query);
|
|
137
141
|
if (query.status !== undefined && !STATUSES.has(query.status)) throw new WorkflowError("invalid_input", "status must be running, completed, cancelled or failed");
|
|
138
142
|
const where = query.access === undefined ? "all" : grantsToWhere(query.access);
|
|
139
143
|
if (where === "none") return { items: [], total: 0 };
|
|
@@ -144,9 +148,8 @@ export class WorkflowRuntime extends IWorkflowRuntime {
|
|
|
144
148
|
if (query.processKey !== undefined) source = source.where((i) => i.processKey.eq(query.processKey!));
|
|
145
149
|
if (query.businessKey !== undefined) source = source.where((i) => i.businessKey.eq(query.businessKey!));
|
|
146
150
|
if (query.startedById !== undefined) source = source.where((i) => i.startedById.eq(query.startedById!));
|
|
147
|
-
const total = await source
|
|
148
|
-
|
|
149
|
-
return { items: rows.map(summaryOf), total };
|
|
151
|
+
const { items, total } = await paginate(source, pageQueryOf(query, { field: "id", dir: "desc" }));
|
|
152
|
+
return { items: items.map(summaryOf), total };
|
|
150
153
|
}
|
|
151
154
|
|
|
152
155
|
async instance(instanceId: string): Promise<InstanceDetails> {
|
|
@@ -154,7 +157,7 @@ export class WorkflowRuntime extends IWorkflowRuntime {
|
|
|
154
157
|
const row = await this.db.instances.asNoTracking().firstOrDefault((i) => i.id.eq(instanceId));
|
|
155
158
|
if (!row) throw notFound(instanceId);
|
|
156
159
|
const tokens = await this.db.tokens.asNoTracking().where((t) => t.instanceId.eq(instanceId)).orderBy((t) => t.id).toList();
|
|
157
|
-
return { ...summaryOf(row), input: row.input, vars: row.vars, nodes: row.nodes, tokens: tokens.map(tokenOf) };
|
|
160
|
+
return { ...summaryOf(row), input: row.input, vars: row.vars, nodes: row.nodes, output: row.output, tokens: tokens.map(tokenOf) };
|
|
158
161
|
}
|
|
159
162
|
|
|
160
163
|
async history(instanceId: string): Promise<readonly HistoryEntry[]> {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createEventToken } from "../../../kernel";
|
|
2
|
+
import type { WorkflowActor, WorkflowRequest } from "../../contracts/types";
|
|
3
|
+
|
|
4
|
+
/** A person filled a task's form, for the application's audit. Published after it is saved. */
|
|
5
|
+
export interface WorkflowTaskCompleted {
|
|
6
|
+
readonly taskId: string;
|
|
7
|
+
readonly instanceId: string;
|
|
8
|
+
readonly processKey: string;
|
|
9
|
+
readonly nodeId: string;
|
|
10
|
+
readonly partition: string | null;
|
|
11
|
+
readonly actor: WorkflowActor;
|
|
12
|
+
readonly request: WorkflowRequest | null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const WORKFLOW_TASK_COMPLETED = createEventToken<WorkflowTaskCompleted>("workflow.task-completed");
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { contextOnlyController } from "../../../http/Routing/contextOnlyControllers";
|
|
2
2
|
import { can } from "../../../access";
|
|
3
3
|
import { Authorize, Controller, Get, Post, type HttpContext } from "../../../http";
|
|
4
|
-
import type { WorkflowActor } from "../../contracts/types";
|
|
4
|
+
import type { WorkflowActor, WorkflowRequest } from "../../contracts/types";
|
|
5
5
|
import { WorkflowError } from "../../contracts/WorkflowError";
|
|
6
|
-
import {
|
|
6
|
+
import { buildListDocument } from "../../../../library/jsonapi";
|
|
7
|
+
import { body, callerOf, defaultLanguage, filterOf, listQuery, pagingOfList, param, withTitleText } from "../../internal/http";
|
|
7
8
|
import type { TaskStatus } from "../model/WfTask.model";
|
|
8
9
|
import { IWorkflowTasks, type TaskDetails } from "../services/IWorkflowTasks.service";
|
|
9
10
|
|
|
@@ -19,58 +20,60 @@ export class TasksController {
|
|
|
19
20
|
@Get()
|
|
20
21
|
@Authorize(can("workflow.tasks.work"))
|
|
21
22
|
async list(ctx: HttpContext) {
|
|
22
|
-
const
|
|
23
|
-
const {
|
|
24
|
-
const
|
|
23
|
+
const { access, actor, language } = await callerOf(ctx);
|
|
24
|
+
const list = listQuery(ctx, { filter: { partition: ["eq"], processKey: ["eq"], status: ["eq", "in"] }, sort: ["createdAt", "dueAt", "status", "processKey"] });
|
|
25
|
+
const partition = filterOf(list, "partition");
|
|
26
|
+
const status = filterOf(list, "status");
|
|
25
27
|
const statuses = status === undefined ? {} : { status: status.split(",") as TaskStatus[] };
|
|
26
28
|
const page = await this.tasks.list({
|
|
27
|
-
...
|
|
29
|
+
...pagingOfList(list),
|
|
28
30
|
...statuses,
|
|
29
31
|
// Addressees are per partition and counted now, not when the task was created.
|
|
30
32
|
addressees: await access.principals(partition === undefined ? await this.tasks.partitions(statuses) : [partition]),
|
|
31
33
|
actor,
|
|
32
34
|
access: await access.filter("workflow.tasks.work"),
|
|
33
35
|
partition,
|
|
34
|
-
processKey:
|
|
36
|
+
processKey: filterOf(list, "processKey"),
|
|
35
37
|
});
|
|
36
|
-
return
|
|
38
|
+
return buildListDocument(page.items.map((item) => withTitleText(item, language, defaultLanguage(ctx))), list, page.total, { basePath: ctx.path });
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
@Get(":id")
|
|
40
42
|
@Authorize(can("workflow.tasks.work"))
|
|
41
43
|
async get(ctx: HttpContext) {
|
|
42
|
-
|
|
44
|
+
const { task, language } = await this.visible(ctx);
|
|
45
|
+
return withTitleText(task, language, defaultLanguage(ctx));
|
|
43
46
|
}
|
|
44
47
|
|
|
45
48
|
@Post(":id/claim")
|
|
46
49
|
@Authorize(can("workflow.tasks.work"))
|
|
47
50
|
async claim(ctx: HttpContext) {
|
|
48
|
-
const { task, actor } = await this.visible(ctx);
|
|
49
|
-
return await this.tasks.claim(task.id, actor);
|
|
51
|
+
const { task, actor, language } = await this.visible(ctx);
|
|
52
|
+
return withTitleText(await this.tasks.claim(task.id, actor), language, defaultLanguage(ctx));
|
|
50
53
|
}
|
|
51
54
|
|
|
52
55
|
@Post(":id/release")
|
|
53
56
|
@Authorize(can("workflow.tasks.work"))
|
|
54
57
|
async release(ctx: HttpContext) {
|
|
55
|
-
const { task, actor } = await this.visible(ctx);
|
|
56
|
-
return await this.tasks.release(task.id, actor);
|
|
58
|
+
const { task, actor, language } = await this.visible(ctx);
|
|
59
|
+
return withTitleText(await this.tasks.release(task.id, actor), language, defaultLanguage(ctx));
|
|
57
60
|
}
|
|
58
61
|
|
|
59
62
|
@Post(":id/complete")
|
|
60
63
|
@Authorize(can("workflow.tasks.work"))
|
|
61
64
|
async complete(ctx: HttpContext) {
|
|
62
|
-
const { task, actor } = await this.visible(ctx);
|
|
65
|
+
const { task, actor, request: from, language } = await this.visible(ctx);
|
|
63
66
|
const request = await body(ctx);
|
|
64
|
-
return await this.tasks.complete(task.id, request.output ?? {}, actor);
|
|
67
|
+
return withTitleText(await this.tasks.complete(task.id, request.output ?? {}, actor, { request: from, language }), language, defaultLanguage(ctx));
|
|
65
68
|
}
|
|
66
69
|
|
|
67
70
|
/** The task if the caller may see it: addressed to one of their principals or claimed by them. */
|
|
68
|
-
private async visible(ctx: HttpContext): Promise<{ task: TaskDetails; actor: WorkflowActor }> {
|
|
71
|
+
private async visible(ctx: HttpContext): Promise<{ task: TaskDetails; actor: WorkflowActor; request: WorkflowRequest; language: string }> {
|
|
69
72
|
const task = await this.tasks.get(param(ctx, "id"));
|
|
70
|
-
const { access, actor } = await callerOf(ctx, "workflow.tasks.work", { scope: task.partition, process: task.processKey });
|
|
73
|
+
const { access, actor, request, language } = await callerOf(ctx, "workflow.tasks.work", { scope: task.partition, process: task.processKey });
|
|
71
74
|
const addressees = await access.principals([task.partition]);
|
|
72
75
|
if (!addressees.some(({ principals }) => principals.includes(task.assignee)) && task.claimedBy !== actor.id) throw new WorkflowError("not_found", `Task ${task.id} does not exist`);
|
|
73
|
-
return { task, actor };
|
|
76
|
+
return { task, actor, request, language };
|
|
74
77
|
}
|
|
75
78
|
}
|
|
76
79
|
contextOnlyController(TasksController);
|
|
@@ -8,7 +8,7 @@ export class TasksDbContext extends DbContext {
|
|
|
8
8
|
/** The table of the Tasks part: bazis creates and checks it in the application database. */
|
|
9
9
|
export const tasksStore = defineOrmOwnedStoreV1({
|
|
10
10
|
contract: "bazis.orm-owned-store/v1",
|
|
11
|
-
storeKey: "bazis.workflow.tasks",
|
|
11
|
+
storeKey: "bazis.workflow.tasks.v2",
|
|
12
12
|
formatVersion: 1,
|
|
13
|
-
ownedScope: { schema: "public", tablePrefix: "
|
|
13
|
+
ownedScope: { schema: "public", tablePrefix: "bazis_wf2_task_" },
|
|
14
14
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Column, Entity, Index, UUID } from "../../../orm";
|
|
2
|
-
import type { FormSpec } from "../../../../library/workflow";
|
|
2
|
+
import type { FormSpec, LocalizedText } from "../../../../library/workflow";
|
|
3
3
|
|
|
4
4
|
/** `open` waits for someone, `claimed` is taken by one person, `completed` and `cancelled` are done. */
|
|
5
5
|
export type TaskStatus = "open" | "claimed" | "completed" | "cancelled";
|
|
@@ -10,7 +10,7 @@ export type TaskStatus = "open" | "claimed" | "completed" | "cancelled";
|
|
|
10
10
|
*/
|
|
11
11
|
@Index(["assignee", "status"])
|
|
12
12
|
@Index(["partition", "status"])
|
|
13
|
-
@Entity({ table: "
|
|
13
|
+
@Entity({ table: "bazis_wf2_task_items" })
|
|
14
14
|
export class WfTask {
|
|
15
15
|
@UUID({ version: "v7" })
|
|
16
16
|
id = "";
|
|
@@ -33,8 +33,12 @@ export class WfTask {
|
|
|
33
33
|
@Column({ type: "text", nullable: true })
|
|
34
34
|
partition: string | null = null;
|
|
35
35
|
|
|
36
|
-
@Column({ type: "
|
|
37
|
-
title = "";
|
|
36
|
+
@Column({ type: "json" })
|
|
37
|
+
title: LocalizedText = "";
|
|
38
|
+
|
|
39
|
+
/** The task of a trial run (WORKFLOW-002 §5.4, stage 5). */
|
|
40
|
+
@Column({ type: "boolean" })
|
|
41
|
+
test = false;
|
|
38
42
|
|
|
39
43
|
/** `role:<code>` or `user:<id>`. */
|
|
40
44
|
@Column({ type: "text" })
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { FormSpec } from "../../../../library/workflow";
|
|
1
|
+
import type { FormSpec, LocalizedText } from "../../../../library/workflow";
|
|
2
2
|
import type { AccessFilter, ScopePrincipals } from "../../../access";
|
|
3
|
-
import type { WorkflowActor, WorkflowPage, WorkflowPaging } from "../../contracts/types";
|
|
3
|
+
import type { WorkflowActor, WorkflowAuditOptions, WorkflowPage, WorkflowPaging } from "../../contracts/types";
|
|
4
4
|
import type { TaskStatus } from "../model/WfTask.model";
|
|
5
5
|
|
|
6
6
|
export interface TaskDetails {
|
|
@@ -10,7 +10,7 @@ export interface TaskDetails {
|
|
|
10
10
|
readonly processKey: string;
|
|
11
11
|
readonly nodeId: string;
|
|
12
12
|
readonly partition: string | null;
|
|
13
|
-
readonly title:
|
|
13
|
+
readonly title: LocalizedText;
|
|
14
14
|
readonly assignee: string;
|
|
15
15
|
readonly form: FormSpec | null;
|
|
16
16
|
readonly dueAt: Date | null;
|
|
@@ -53,5 +53,5 @@ export abstract class IWorkflowTasks {
|
|
|
53
53
|
/** Gives a claimed task back. */
|
|
54
54
|
abstract release(taskId: string, actor: WorkflowActor): Promise<TaskDetails>;
|
|
55
55
|
/** Fills the form: the process moves on. */
|
|
56
|
-
abstract complete(taskId: string, output: unknown, actor: WorkflowActor): Promise<TaskDetails>;
|
|
56
|
+
abstract complete(taskId: string, output: unknown, actor: WorkflowActor, options?: WorkflowAuditOptions & { readonly language?: string }): Promise<TaskDetails>;
|
|
57
57
|
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { grantsToWhere, type ScopePrincipals } from "../../../access";
|
|
2
|
-
import {
|
|
2
|
+
import { paginate } from "../../../orm";
|
|
3
|
+
import { isUuid, pageQueryOf, type WorkflowActor, type WorkflowAuditOptions, type WorkflowPage } from "../../contracts/types";
|
|
3
4
|
import type { WorkflowClock } from "../../contracts/WorkflowClock";
|
|
4
5
|
import { WorkflowError } from "../../contracts/WorkflowError";
|
|
6
|
+
import type { WorkflowEvents } from "../../internal/WorkflowEvents";
|
|
5
7
|
import type { IWorkflowRuntime } from "../../runtime_module/services/IWorkflowRuntime.service";
|
|
8
|
+
import { WORKFLOW_TASK_COMPLETED } from "../events/Tasks.events";
|
|
6
9
|
import type { TasksDbContext } from "../model/Tasks.dbContext";
|
|
7
10
|
import { WfTask, type TaskStatus } from "../model/WfTask.model";
|
|
8
11
|
import { IWorkflowTasks, type TaskDetails, type TaskListQuery } from "./IWorkflowTasks.service";
|
|
@@ -14,12 +17,12 @@ export class WorkflowTasks extends IWorkflowTasks {
|
|
|
14
17
|
private readonly db: TasksDbContext,
|
|
15
18
|
private readonly runtime: IWorkflowRuntime,
|
|
16
19
|
private readonly clock: WorkflowClock,
|
|
20
|
+
private readonly events: WorkflowEvents,
|
|
17
21
|
) {
|
|
18
22
|
super();
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
async list(query: TaskListQuery): Promise<WorkflowPage<TaskDetails>> {
|
|
22
|
-
const { limit, offset } = pagingOf(query);
|
|
23
26
|
const statuses = statusesOf(query.status);
|
|
24
27
|
const groups = groupByPrincipals(query.addressees);
|
|
25
28
|
const mine = query.actor?.id;
|
|
@@ -38,14 +41,13 @@ export class WorkflowTasks extends IWorkflowTasks {
|
|
|
38
41
|
});
|
|
39
42
|
if (query.partition !== undefined) source = source.where((t) => t.partition.eq(query.partition!));
|
|
40
43
|
if (query.processKey !== undefined) source = source.where((t) => t.processKey.eq(query.processKey!));
|
|
41
|
-
const total = await source
|
|
42
|
-
|
|
43
|
-
return { items: rows.map(detailsOf), total };
|
|
44
|
+
const { items, total } = await paginate(source, pageQueryOf(query, { field: "id", dir: "asc" }));
|
|
45
|
+
return { items: items.map(detailsOf), total };
|
|
44
46
|
}
|
|
45
47
|
|
|
46
48
|
async partitions(query: { readonly status?: readonly TaskStatus[] } = {}): Promise<readonly (string | null)[]> {
|
|
47
49
|
const statuses = statusesOf(query.status);
|
|
48
|
-
const rows = await this.db.database.querySqlRaw(`SELECT DISTINCT "partition" FROM "
|
|
50
|
+
const rows = await this.db.database.querySqlRaw(`SELECT DISTINCT "partition" FROM "bazis_wf2_task_items" WHERE "status" IN (${statuses.map((_, index) => `{${index}}`).join(", ")})`, ...statuses);
|
|
49
51
|
return rows.map((row) => row.partition as string | null);
|
|
50
52
|
}
|
|
51
53
|
|
|
@@ -79,14 +81,14 @@ export class WorkflowTasks extends IWorkflowTasks {
|
|
|
79
81
|
});
|
|
80
82
|
}
|
|
81
83
|
|
|
82
|
-
async complete(taskId: string, output: unknown, actor: WorkflowActor): Promise<TaskDetails> {
|
|
84
|
+
async complete(taskId: string, output: unknown, actor: WorkflowActor, options: WorkflowAuditOptions & { readonly language?: string } = {}): Promise<TaskDetails> {
|
|
83
85
|
const task = await this.task(taskId, false);
|
|
84
86
|
if (task.status === "claimed" && task.claimedById !== actor.id) throw new WorkflowError("conflict", "The task is taken by someone else");
|
|
85
87
|
if (task.status !== "open" && task.status !== "claimed") throw new WorkflowError("wrong_status", `The task is ${task.status}`);
|
|
86
88
|
let instance;
|
|
87
89
|
try {
|
|
88
90
|
// The form is checked and the process moves on first; the task follows.
|
|
89
|
-
instance = await this.runtime.completeToken(task.tokenId, output, actor);
|
|
91
|
+
instance = await this.runtime.completeToken(task.tokenId, output, actor, options.language === undefined ? {} : { language: options.language });
|
|
90
92
|
} catch (error) {
|
|
91
93
|
if (error instanceof WorkflowError && error.code === "wrong_status") await this.close(task.tokenId, "cancelled");
|
|
92
94
|
throw error;
|
|
@@ -98,6 +100,7 @@ export class WorkflowTasks extends IWorkflowTasks {
|
|
|
98
100
|
completedAt: this.clock.now(),
|
|
99
101
|
output: typeof saved === "object" && saved !== null && !Array.isArray(saved) ? saved as Record<string, unknown> : null,
|
|
100
102
|
});
|
|
103
|
+
await this.events.publish(WORKFLOW_TASK_COMPLETED, { taskId: task.id, instanceId: task.instanceId, processKey: task.processKey, nodeId: task.nodeId, partition: task.partition, actor, request: options.request ?? null });
|
|
101
104
|
return await this.get(task.id);
|
|
102
105
|
}
|
|
103
106
|
|
|
@@ -686,6 +686,18 @@ function applyValidatorOptions(schema: OpenApiCodegenSchema, options: Record<str
|
|
|
686
686
|
out.type = "string";
|
|
687
687
|
out.pattern = options.pattern;
|
|
688
688
|
}
|
|
689
|
+
if (typeof options.ref === "string") {
|
|
690
|
+
out["x-ref"] = options.ref;
|
|
691
|
+
}
|
|
692
|
+
if (typeof options.refKeys === "string") {
|
|
693
|
+
out["x-ref-keys"] = options.refKeys;
|
|
694
|
+
}
|
|
695
|
+
if (typeof options.refScope === "string") {
|
|
696
|
+
out["x-ref-scope"] = options.refScope;
|
|
697
|
+
}
|
|
698
|
+
if (options.personal === true) {
|
|
699
|
+
out["x-personal"] = true;
|
|
700
|
+
}
|
|
689
701
|
return out;
|
|
690
702
|
}
|
|
691
703
|
|
|
@@ -150,4 +150,16 @@ export interface ValidatorOptions {
|
|
|
150
150
|
* enums are ignored).
|
|
151
151
|
*/
|
|
152
152
|
enumType?: Record<string, string | number>;
|
|
153
|
+
|
|
154
|
+
// ── Schema ───────────────────────────────────────────────────────────────
|
|
155
|
+
// Literals that go to the generated JSON Schema; they check nothing here.
|
|
156
|
+
|
|
157
|
+
/** The value is a reference of this kind (`x-ref`): `dm.table`. */
|
|
158
|
+
ref?: string;
|
|
159
|
+
/** The keys of the object are references of this kind (`x-ref-keys`): fields of a table. */
|
|
160
|
+
refKeys?: string;
|
|
161
|
+
/** JSON Pointer to the sibling input that sets their area (`x-ref-scope`): `/table`. */
|
|
162
|
+
refScope?: `/${string}`;
|
|
163
|
+
/** Personal data (`x-personal`): a process masks the value in its history. */
|
|
164
|
+
personal?: boolean;
|
|
153
165
|
}
|