@nanobpm/workflow 0.11.0 → 0.13.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/README.md CHANGED
@@ -36,13 +36,24 @@ Both compile to the same engine durability; pick per workflow.
36
36
  **The recommended surface.** Describe the flow as a **tree of nodes**: `w.run` (a
37
37
  locally-hosted service task), `w.task` (an external-worker service task — job
38
38
  type `${flowId}:${name}` by default, or pass `w.task(name, { jobType })` to
39
- target an existing worker pool, e.g. a `senior:pr-review` agent token), and
39
+ target an existing worker pool, e.g. a `senior:pr-review` agent token),
40
40
  `w.signal` (a durable message catch that resumes via a correlated message — the
41
- human-in-the-loop path), composed with control-flow combinators:
41
+ human-in-the-loop path), and `w.human` (a BPMN **user task** — a human-in-the-loop
42
+ approval gate rendered on a task list), composed with control-flow combinators:
42
43
 
43
44
  - `w.switch(subject, cases)` — a multi-way exclusive choice; each case key routes
44
45
  when `subject = value`; an optional `default` case is the fallback.
45
46
  - `w.branch(condition, { then, else? })` — a two-way choice on a FEEL boolean.
47
+ - `w.human(name, { form, assignee?, candidateGroups?, io? })` — a durable **user
48
+ task** (`<bpmn:userTask>` with the Zeebe user-task marker). `form` binds a
49
+ `zeebe:formDefinition` (the form id shown to the assignee). Add an assignment
50
+ with `assignee` (a static user id or a FEEL expression like
51
+ `=escalationAssignee`) and/or `candidateGroups` (who may claim it) — supply
52
+ either or both. `io` adds a `zeebe:ioMapping` of `{ input?, output? }` entries
53
+ (`{ source, target }`: a FEEL `source` copied to/from the process variable
54
+ `target`), e.g. to stamp the reviewer's decision back onto a variable on
55
+ completion. The token waits durably until the task is completed from a task
56
+ list / the Zeebe user-task API.
46
57
  - `w.loop(body)` — a durable loop (back-edge to the loop head).
47
58
  - `w.break()` / `w.continue()` — exit the enclosing loop, or jump back to its head.
48
59
  - `w.parallel([blockA, blockB, …])` — a static fork/join: every block runs
@@ -88,6 +99,25 @@ const { processInstanceKey } = await client.start(onboarding, {});
88
99
  await client.signal(onboarding, "approved", userId, { by: "alice" });
89
100
  ```
90
101
 
102
+ A human-in-the-loop **approval gate** with a form, an assignment, and an output
103
+ mapping that stamps the decision back onto a process variable:
104
+
105
+ ```ts
106
+ const review = defineFlow("plan-review", (w) => {
107
+ w.human("plan-review-decision", {
108
+ form: "plan-review-decision", // zeebe:formDefinition formId
109
+ assignee: "=escalationAssignee", // a FEEL expression (or a static user id)
110
+ candidateGroups: "operators", // who may claim it
111
+ io: { output: [{ source: "=verdict", target: "planVerdict" }] },
112
+ });
113
+ w.switch("planVerdict", {
114
+ approve: (c) => c.run("apply", applyPlan),
115
+ default: (c) => c.run("revise", revisePlan),
116
+ });
117
+ });
118
+ ```
119
+
120
+
91
121
  A durable convergence loop (the shape `urban-pr-review` uses) — a loop wrapping a
92
122
  status switch with a nested guard:
93
123
 
@@ -206,6 +236,46 @@ Only `resourceId` is required; omit `append` and no `ioMapping` is emitted. A
206
236
  `w.task` **without** a `prompt` is unchanged — it emits no `linkedResources`.
207
237
  Data envelopes (via contracts) still lift alongside the prompt binding.
208
238
 
239
+ #### Service-task I/O mappings (`w.task`/`w.run` with `{ io }`)
240
+
241
+ Any service task — external (`w.task`) or locally-hosted (`w.run`) — can carry a
242
+ general `<zeebe:ioMapping>` of arbitrary **input** (applied on activation) and
243
+ **output** (applied on completion) variable mappings via an optional `io`. It
244
+ reuses the same `{ input?, output? }` shape as `w.human`'s `io` (a single
245
+ `HumanIoMapping` / `HumanIoEntry`, no parallel type):
246
+
247
+ ```ts
248
+ w.task("record-conformance-ack", {
249
+ jobType: "pr.conformance-ack",
250
+ io: {
251
+ input: [
252
+ { source: "=planKey", target: "planKey" },
253
+ { source: "=if (is defined(note)) then note else null", target: "note" },
254
+ ],
255
+ },
256
+ });
257
+ ```
258
+
259
+ derives:
260
+
261
+ ```xml
262
+ <bpmn:serviceTask id="record-conformance-ack" name="record-conformance-ack">
263
+ <bpmn:extensionElements>
264
+ <zeebe:taskDefinition type="pr.conformance-ack" />
265
+ <zeebe:ioMapping>
266
+ <zeebe:input source="=planKey" target="planKey" />
267
+ <zeebe:input source="=if (is defined(note)) then note else null" target="note" />
268
+ </zeebe:ioMapping>
269
+ </bpmn:extensionElements>
270
+ </bpmn:serviceTask>
271
+ ```
272
+
273
+ The `ioMapping` is emitted after `taskDefinition` (and after `linkedResources` /
274
+ envelope `properties` when present). When a task has **both** a `prompt.append`
275
+ and an explicit `io.input`, they merge into a **single** `<zeebe:ioMapping>` (the
276
+ `appendPrompt` input trails the explicit inputs) — never two. Omit `io` (or pass
277
+ an empty `{}`) and no `ioMapping` is emitted.
278
+
209
279
  ### Imperative (Temporal-style, engine-replayed) — experimental/internal
210
280
 
211
281
  Write the orchestration as a function. `ctx.run(name, fn)` is a durable step: its
@@ -1,5 +1,6 @@
1
1
  import type { DeclarativeFlow, FlowContracts, FlowNode, JsonObject, NodeEnvelopes, StepHandler, TimerStart } from "./types.js";
2
2
  import type { Envelope } from "./envelope.js";
3
+ import type { HumanIoMapping } from "./io-mapping.js";
3
4
  import "./nodes/index.js";
4
5
  /** The TS payload type of a contract's input envelope (untyped fallback). */
5
6
  export type InPayload<Ct> = Ct extends {
@@ -42,7 +43,9 @@ export interface FlowBuilder<C extends object = object> {
42
43
  * the flow's contracts, the handler's job variables and return value are typed
43
44
  * from that contract's `in`/`out` envelopes; otherwise they are `JsonObject`.
44
45
  */
45
- run<K extends string>(name: K, handler: TypedHandler<VarsOf<C, K>, ResultOf<C, K>>): FlowBuilder<C>;
46
+ run<K extends string>(name: K, handler: TypedHandler<VarsOf<C, K>, ResultOf<C, K>>, opts?: {
47
+ io?: HumanIoMapping;
48
+ }): FlowBuilder<C>;
46
49
  /**
47
50
  * A durable activity served by a worker OUTSIDE this program (a BPMN service
48
51
  * task, but no locally-hosted handler). Its job type defaults to the derived
@@ -57,6 +60,7 @@ export interface FlowBuilder<C extends object = object> {
57
60
  */
58
61
  task<K extends string>(name: K, opts?: {
59
62
  jobType?: string;
63
+ io?: HumanIoMapping;
60
64
  }): FlowBuilder<C>;
61
65
  /**
62
66
  * A durable wait for an external/human event, correlated on a process
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export type { Journal, ReplayStep } from "./imperative.js";
3
3
  export { defineFlow, declarativeToBpmn, externalJobTypes, walkNodes } from "./declarative.js";
4
4
  export type { FlowBuilder } from "./declarative.js";
5
5
  export type { BoundaryOptions } from "./nodes/boundary.js";
6
+ export type { HumanOptions, HumanIoMapping, HumanIoEntry } from "./nodes/human.js";
6
7
  export { layoutBpmn, declarativeToLayoutedBpmn } from "./layout.js";
7
8
  export { envelope } from "./envelope.js";
8
9
  export type { Envelope, EnvelopeType, EnvelopeField, FieldSpec, ScalarType, ScalarTs, FieldTs, } from "./envelope.js";
@@ -0,0 +1,32 @@
1
+ /** A single Zeebe I/O mapping entry — a FEEL `source` copied to/from a process
2
+ * variable `target`. */
3
+ export interface HumanIoEntry {
4
+ /** FEEL expression evaluated for the mapping (e.g. `=orderId`). */
5
+ source: string;
6
+ /** The process variable the value is written to. */
7
+ target: string;
8
+ }
9
+ /** A `<zeebe:ioMapping>`: input mappings applied on activation and output
10
+ * mappings applied on completion. */
11
+ export interface HumanIoMapping {
12
+ input?: HumanIoEntry[];
13
+ output?: HumanIoEntry[];
14
+ }
15
+ /** A present, non-empty, non-whitespace-only string — the shape every id /
16
+ * assignment / mapping-expression field must have. A blank value would emit a
17
+ * meaningless BPMN attribute (an empty ioMapping source/target, formId,
18
+ * assignee, …) that fails or misbehaves at deploy/run time, so we fail fast at
19
+ * build. Non-blank values are kept verbatim — FEEL expressions are never
20
+ * trimmed or normalized. */
21
+ export declare function isNonBlankString(v: unknown): v is string;
22
+ /** Validate an optional io mapping at authoring time. `label` is the node's
23
+ * self-description (e.g. `human("decide")` or `task("record")`) so the thrown
24
+ * message names the offending step. An `undefined` mapping is allowed (no
25
+ * ioMapping is emitted); a non-object / array value, or a malformed entry, fails
26
+ * fast. */
27
+ export declare function assertIoMapping(label: string, io: HumanIoMapping | undefined): void;
28
+ /** Emit a single `<zeebe:ioMapping>` (inputs then outputs) for the mapping, or
29
+ * the empty string when it has no entries. The 8-space element / 10-space entry
30
+ * indentation matches both the user-task and service-task emission, so a caller
31
+ * can splice the result straight into either `<bpmn:extensionElements>`. */
32
+ export declare function renderIoMapping(io: HumanIoMapping | undefined): string;
@@ -0,0 +1,67 @@
1
+ // The single source of truth for a Zeebe `<zeebe:ioMapping>` — its shape, its
2
+ // authoring-time validation, and its BPMN emission — shared by EVERY node kind
3
+ // that carries variable mappings (`human` user tasks, `task`/`run` service
4
+ // tasks). Derivation over duplication (AGENTS.md): one interface, one validator,
5
+ // one renderer, so the ioMapping surface can never drift between node kinds.
6
+ import { escapeXml } from "./xml.js";
7
+ /** A present, non-empty, non-whitespace-only string — the shape every id /
8
+ * assignment / mapping-expression field must have. A blank value would emit a
9
+ * meaningless BPMN attribute (an empty ioMapping source/target, formId,
10
+ * assignee, …) that fails or misbehaves at deploy/run time, so we fail fast at
11
+ * build. Non-blank values are kept verbatim — FEEL expressions are never
12
+ * trimmed or normalized. */
13
+ export function isNonBlankString(v) {
14
+ return typeof v === "string" && v.trim() !== "";
15
+ }
16
+ function isEntry(e) {
17
+ if (e === null || typeof e !== "object")
18
+ return false;
19
+ if (!("source" in e) || !("target" in e))
20
+ return false;
21
+ const { source, target } = e;
22
+ return isNonBlankString(source) && isNonBlankString(target);
23
+ }
24
+ function validateEntries(label, dir, entries) {
25
+ if (entries === undefined)
26
+ return;
27
+ if (!Array.isArray(entries))
28
+ throw new Error(`${label} io.${dir} must be an array of { source, target }`);
29
+ for (const e of entries) {
30
+ if (!isEntry(e)) {
31
+ throw new Error(`${label} io.${dir} entries must be { source: non-empty string, target: non-empty string }`);
32
+ }
33
+ }
34
+ }
35
+ /** Validate an optional io mapping at authoring time. `label` is the node's
36
+ * self-description (e.g. `human("decide")` or `task("record")`) so the thrown
37
+ * message names the offending step. An `undefined` mapping is allowed (no
38
+ * ioMapping is emitted); a non-object / array value, or a malformed entry, fails
39
+ * fast. */
40
+ export function assertIoMapping(label, io) {
41
+ if (io === undefined)
42
+ return;
43
+ if (io === null || typeof io !== "object" || Array.isArray(io)) {
44
+ throw new Error(`${label} { io } must be an object { input?, output? }`);
45
+ }
46
+ validateEntries(label, "input", io.input);
47
+ validateEntries(label, "output", io.output);
48
+ }
49
+ function renderMappings(entries, tag) {
50
+ if (!entries || entries.length === 0)
51
+ return "";
52
+ return entries
53
+ .map((e) => ` <zeebe:${tag} source="${escapeXml(e.source)}" target="${escapeXml(e.target)}" />\n`)
54
+ .join("");
55
+ }
56
+ /** Emit a single `<zeebe:ioMapping>` (inputs then outputs) for the mapping, or
57
+ * the empty string when it has no entries. The 8-space element / 10-space entry
58
+ * indentation matches both the user-task and service-task emission, so a caller
59
+ * can splice the result straight into either `<bpmn:extensionElements>`. */
60
+ export function renderIoMapping(io) {
61
+ if (!io || ((io.input?.length ?? 0) === 0 && (io.output?.length ?? 0) === 0))
62
+ return "";
63
+ return (` <zeebe:ioMapping>\n` +
64
+ renderMappings(io.input, "input") +
65
+ renderMappings(io.output, "output") +
66
+ ` </zeebe:ioMapping>\n`);
67
+ }
@@ -0,0 +1,44 @@
1
+ import type { HumanIoMapping } from "../io-mapping.js";
2
+ export type { HumanIoEntry, HumanIoMapping } from "../io-mapping.js";
3
+ /** Options for {@link FlowBuilder.human}. `form` is the `zeebe:formDefinition`
4
+ * form id; `assignee` / `candidateGroups` populate a `zeebe:assignmentDefinition`
5
+ * (supply either or both); `io` populates a `zeebe:ioMapping`. */
6
+ export interface HumanOptions {
7
+ /** The `zeebe:formDefinition` form id bound to this task. */
8
+ form: string;
9
+ /** A single assignee (a static user id or a FEEL expression like
10
+ * `=escalationAssignee`). */
11
+ assignee?: string;
12
+ /** The candidate groups allowed to claim the task (a static group id or a FEEL
13
+ * expression). */
14
+ candidateGroups?: string;
15
+ /** Input/output variable mappings. */
16
+ io?: HumanIoMapping;
17
+ }
18
+ /** The `human` node shape (its `FlowNode` variant). */
19
+ interface HumanNode {
20
+ kind: "human";
21
+ name: string;
22
+ form: string;
23
+ assignee?: string;
24
+ candidateGroups?: string;
25
+ io?: HumanIoMapping;
26
+ }
27
+ declare module "../types.js" {
28
+ interface FlowNodeRegistry {
29
+ human: HumanNode;
30
+ }
31
+ }
32
+ declare module "../declarative.js" {
33
+ interface FlowBuilder<C extends object> {
34
+ /**
35
+ * A human-in-the-loop approval step (a BPMN `<bpmn:userTask>` with the Zeebe
36
+ * user-task marker). `opts.form` binds a `zeebe:formDefinition`; the optional
37
+ * `assignee` / `candidateGroups` populate a `zeebe:assignmentDefinition`
38
+ * (either or both), and `io` populates a `zeebe:ioMapping`. Resume it from a
39
+ * task list / the Zeebe user-task API; the token waits durably until the task
40
+ * is completed.
41
+ */
42
+ human<K extends string>(name: K, opts: HumanOptions): FlowBuilder<C>;
43
+ }
44
+ }
@@ -0,0 +1,70 @@
1
+ // Built-in flow-node kind: `human` — a human-in-the-loop approval step (a BPMN
2
+ // `<bpmn:userTask>` with the Zeebe user-task marker). It carries a form binding
3
+ // and, optionally, an assignment (assignee / candidate groups) and an I/O
4
+ // mapping — the surface every nwf approval gate (answer escalation, plan review,
5
+ // trial-merge decision) is built from. Contributed from this module through the
6
+ // extension seam (epic #314, S3/#318) — no central union or dispatch is edited.
7
+ import { escapeXml } from "../xml.js";
8
+ import { incomingOutgoing } from "../declarative.js";
9
+ import { assertIoMapping, isNonBlankString, renderIoMapping } from "../io-mapping.js";
10
+ import { registerNodeKind } from "./registry.js";
11
+ /** A present, non-empty, non-whitespace-only string — the shape every human()
12
+ * id / assignment field must have. A blank value would emit a meaningless BPMN
13
+ * attribute (an empty formId, assignee, or candidateGroups) that fails or
14
+ * misbehaves at deploy/run time, so we fail fast at build. Shared with the
15
+ * ioMapping validator via `io-mapping`. */
16
+ function renderAssignment(node) {
17
+ if (node.assignee === undefined && node.candidateGroups === undefined)
18
+ return "";
19
+ const attrs = [
20
+ node.assignee !== undefined ? ` assignee="${escapeXml(node.assignee)}"` : "",
21
+ node.candidateGroups !== undefined ? ` candidateGroups="${escapeXml(node.candidateGroups)}"` : "",
22
+ ].join("");
23
+ return ` <zeebe:assignmentDefinition${attrs} />\n`;
24
+ }
25
+ function renderUserTask(node, inc, outg) {
26
+ const id = node.name;
27
+ const ext = ` <bpmn:extensionElements>\n` +
28
+ ` <zeebe:formDefinition formId="${escapeXml(node.form)}" />\n` +
29
+ ` <zeebe:userTask />\n` +
30
+ renderAssignment(node) +
31
+ renderIoMapping(node.io) +
32
+ ` </bpmn:extensionElements>`;
33
+ return (` <bpmn:userTask id="${escapeXml(id)}" name="${escapeXml(id)}">\n` +
34
+ ext +
35
+ "\n" +
36
+ incomingOutgoing(inc, outg) +
37
+ ` </bpmn:userTask>`);
38
+ }
39
+ registerNodeKind("human", {
40
+ build: (api) => (name, opts) => {
41
+ api.claim(name);
42
+ if (opts === null || typeof opts !== "object") {
43
+ throw new Error(`human("${name}") needs an options object { form, … }`);
44
+ }
45
+ if (!isNonBlankString(opts.form)) {
46
+ throw new Error(`human("${name}") needs a non-empty { form } (the zeebe:formDefinition form id)`);
47
+ }
48
+ if (opts.assignee !== undefined && !isNonBlankString(opts.assignee)) {
49
+ throw new Error(`human("${name}") { assignee } must be a non-empty string`);
50
+ }
51
+ if (opts.candidateGroups !== undefined && !isNonBlankString(opts.candidateGroups)) {
52
+ throw new Error(`human("${name}") { candidateGroups } must be a non-empty string`);
53
+ }
54
+ assertIoMapping(`human("${name}")`, opts.io);
55
+ api.out.push({
56
+ kind: "human",
57
+ name,
58
+ form: opts.form,
59
+ assignee: opts.assignee,
60
+ candidateGroups: opts.candidateGroups,
61
+ io: opts.io,
62
+ });
63
+ return api.self();
64
+ },
65
+ emit: (node, incoming, _loop, api) => {
66
+ api.addNode({ id: node.name, render: (inc, outg) => renderUserTask(node, inc, outg) });
67
+ api.connect(incoming, node.name);
68
+ return [api.newEdge(node.name)];
69
+ },
70
+ });
@@ -3,6 +3,7 @@ import "./branch.js";
3
3
  import "./break.js";
4
4
  import "./continue.js";
5
5
  import "./forEach.js";
6
+ import "./human.js";
6
7
  import "./loop.js";
7
8
  import "./parallel.js";
8
9
  import "./race.js";
@@ -10,6 +10,7 @@ import "./branch.js";
10
10
  import "./break.js";
11
11
  import "./continue.js";
12
12
  import "./forEach.js";
13
+ import "./human.js";
13
14
  import "./loop.js";
14
15
  import "./parallel.js";
15
16
  import "./race.js";
@@ -1,10 +1,11 @@
1
+ import type { HumanIoMapping } from "../io-mapping.js";
1
2
  declare module "../types.js" {
2
3
  interface FlowNodeRegistry {
3
4
  run: {
4
5
  kind: "run";
5
6
  name: string;
6
7
  envelopes?: NodeEnvelopes;
8
+ io?: HumanIoMapping;
7
9
  };
8
10
  }
9
11
  }
10
- export {};
package/dist/nodes/run.js CHANGED
@@ -3,18 +3,20 @@
3
3
  // entirely from this module through the extension seam (epic #314, S0/#315):
4
4
  // its `FlowNode` variant, its `w.run(...)` builder method, and its walk/emit
5
5
  // handlers — with NO edit to the shared union or a central dispatch switch.
6
+ import { assertIoMapping, renderIoMapping } from "../io-mapping.js";
6
7
  import { registerNodeKind } from "./registry.js";
7
8
  registerNodeKind("run", {
8
- build: (api) => (name, handler) => {
9
+ build: (api) => (name, handler, opts) => {
9
10
  api.claim(name);
10
11
  if (typeof handler !== "function")
11
12
  throw new Error(`run("${name}") needs a handler function`);
13
+ assertIoMapping(`run("${name}")`, opts?.io);
12
14
  api.handlers[name] = handler;
13
- api.out.push({ kind: "run", name, envelopes: api.contractEnvelopes(name) });
15
+ api.out.push({ kind: "run", name, envelopes: api.contractEnvelopes(name), io: opts?.io });
14
16
  return api.self();
15
17
  },
16
18
  emit: (node, incoming, _loop, api) => {
17
- api.addServiceTask(node);
19
+ api.addServiceTask(node, node.io !== undefined ? { extraExt: renderIoMapping(node.io) } : {});
18
20
  api.connect(incoming, node.name);
19
21
  return [api.newEdge(node.name)];
20
22
  },
@@ -1,3 +1,4 @@
1
+ import type { HumanIoMapping } from "../io-mapping.js";
1
2
  /** Binds an LLM prompt resource to an agent service task. Emits, alongside the
2
3
  * task's `zeebe:taskDefinition` capability token, a
3
4
  * `<zeebe:linkedResources><zeebe:linkedResource resourceId=… bindingType=…
@@ -26,6 +27,11 @@ declare module "../types.js" {
26
27
  jobType?: string;
27
28
  /** The agent prompt binding, when the task is an LLM-agent service task. */
28
29
  prompt?: PromptBinding;
30
+ /** An explicit `<zeebe:ioMapping>` on the service task — arbitrary input
31
+ * (and output) variable mappings, using the shared ioMapping shape. When a
32
+ * `prompt.append` is ALSO present it is folded into this single mapping as
33
+ * an extra `appendPrompt` input (no duplicate `ioMapping` element). */
34
+ io?: HumanIoMapping;
29
35
  };
30
36
  }
31
37
  }
@@ -37,11 +43,13 @@ declare module "../declarative.js" {
37
43
  * (`resourceType="GenericScript" linkName="prompt"`). Pair it with a
38
44
  * `{ jobType }` capability token (e.g. `senior:retro`) so an agent pool
39
45
  * services it. `prompt.append` (optional) feeds a FEEL expression to the
40
- * worker through a `zeebe:ioMapping` `appendPrompt` input.
46
+ * worker through a `zeebe:ioMapping` `appendPrompt` input. `io` (optional)
47
+ * adds arbitrary input/output variable mappings to the SAME `zeebe:ioMapping`.
41
48
  */
42
49
  task<K extends string>(name: K, opts: {
43
50
  jobType?: string;
44
51
  prompt: PromptBinding;
52
+ io?: HumanIoMapping;
45
53
  }): FlowBuilder<C>;
46
54
  }
47
55
  }
@@ -11,6 +11,7 @@
11
11
  // its option type — WITHOUT re-opening the shared `FlowNode` union, the central
12
12
  // `walkNodes`/`emitNode` dispatch, or the generated barrel.
13
13
  import { assertJobType, escapeXml } from "../xml.js";
14
+ import { assertIoMapping, renderIoMapping } from "../io-mapping.js";
14
15
  import { registerNodeKind } from "./registry.js";
15
16
  /** Normalize + validate a prompt binding at authoring time, applying the
16
17
  * `bindingType` default. Emitted string fields are trimmed before storing (in
@@ -42,24 +43,37 @@ function resolvePrompt(name, prompt) {
42
43
  }
43
44
  return resolved;
44
45
  }
45
- /** Build the prompt-specific `<bpmn:extensionElements>` delta appended to the
46
- * shared service-task emission: the `zeebe:linkedResources` prompt binding and
47
- * (optionally) a `zeebe:ioMapping` `appendPrompt` input. The task shell
46
+ /** Build the `<bpmn:extensionElements>` delta appended to the shared
47
+ * service-task emission: the optional `zeebe:linkedResources` prompt binding
48
+ * followed by a single, correctly-ordered `zeebe:ioMapping`. The task shell
48
49
  * (taskDefinition + envelope properties) is rendered by the built-in
49
50
  * `EmitApi.addServiceTask`, so this variant only contributes its delta and the
50
- * envelope serialization can never drift from the plain service task. */
51
- function promptExtensions(prompt) {
52
- const { resourceId, bindingType, append } = prompt;
53
- const linked = ` <zeebe:linkedResources>\n` +
54
- ` <zeebe:linkedResource resourceId="${escapeXml(resourceId)}" ` +
55
- `bindingType="${escapeXml(bindingType ?? "latest")}" resourceType="GenericScript" linkName="prompt" />\n` +
56
- ` </zeebe:linkedResources>\n`;
57
- const io = append !== undefined
58
- ? ` <zeebe:ioMapping>\n` +
59
- ` <zeebe:input source="${escapeXml(append)}" target="appendPrompt" />\n` +
60
- ` </zeebe:ioMapping>\n`
51
+ * envelope serialization can never drift from the plain service task.
52
+ *
53
+ * The `ioMapping` is the SINGLE source for all variable mappings on the task:
54
+ * the explicit `io.input`/`io.output` plus — when `prompt.append` is present —
55
+ * the `appendPrompt` input, folded in as one more input entry so a task with
56
+ * both a `prompt.append` and an explicit `io.input` emits exactly one
57
+ * `<zeebe:ioMapping>` (never two). Element order matches the golden:
58
+ * `linkedResources` (when any) then `ioMapping` (when any), the latter after
59
+ * `taskDefinition`/`properties`. */
60
+ function taskExtensions(prompt, io) {
61
+ const linked = prompt !== undefined
62
+ ? ` <zeebe:linkedResources>\n` +
63
+ ` <zeebe:linkedResource resourceId="${escapeXml(prompt.resourceId)}" ` +
64
+ `bindingType="${escapeXml(prompt.bindingType ?? "latest")}" resourceType="GenericScript" linkName="prompt" />\n` +
65
+ ` </zeebe:linkedResources>\n`
61
66
  : "";
62
- return linked + io;
67
+ // Merge the explicit io mapping and the prompt's `appendPrompt` input into one
68
+ // mapping. The append input trails the explicit inputs — it appends runtime
69
+ // context to the bound prompt, so it reads naturally after the task's own
70
+ // inputs — and outputs follow, matching `renderIoMapping`'s input-then-output
71
+ // ordering.
72
+ const inputs = [...(io?.input ?? [])];
73
+ if (prompt?.append !== undefined)
74
+ inputs.push({ source: prompt.append, target: "appendPrompt" });
75
+ const merged = { input: inputs, output: io?.output ?? [] };
76
+ return linked + renderIoMapping(merged);
63
77
  }
64
78
  registerNodeKind("task", {
65
79
  build: (api) => (name, opts) => {
@@ -68,29 +82,32 @@ registerNodeKind("task", {
68
82
  if (override !== undefined)
69
83
  assertJobType("task jobType", override);
70
84
  const prompt = opts?.prompt !== undefined ? resolvePrompt(name, opts.prompt) : undefined;
85
+ assertIoMapping(`task("${name}")`, opts?.io);
71
86
  api.out.push({
72
87
  kind: "task",
73
88
  name,
74
89
  envelopes: api.contractEnvelopes(name),
75
90
  jobType: override,
76
91
  prompt,
92
+ io: opts?.io,
77
93
  });
78
94
  return api.self();
79
95
  },
80
96
  emit: (node, incoming, _loop, api) => {
81
- // No prompt → the standard service-task emission is preserved unchanged (no
82
- // `linkedResources` are emitted).
83
- if (node.prompt === undefined) {
97
+ // Neither a prompt binding nor an explicit io mapping → the standard
98
+ // service-task emission is preserved unchanged (no `linkedResources`, no
99
+ // `ioMapping`).
100
+ if (node.prompt === undefined && node.io === undefined) {
84
101
  api.addServiceTask(node);
85
102
  api.connect(incoming, node.name);
86
103
  return [api.newEdge(node.name)];
87
104
  }
88
- // Prompt binding present → the shared `addServiceTask` renders the task
89
- // shell (taskDefinition + envelope properties, recording envelopes); we only
90
- // supply the `zeebe:linkedResource` (and optional `appendPrompt` ioMapping)
91
- // delta, so the envelope/taskDefinition serialization has a single source.
92
- const prompt = node.prompt;
93
- api.addServiceTask(node, { extraExt: promptExtensions(prompt) });
105
+ // A prompt binding and/or an explicit io mapping present → the shared
106
+ // `addServiceTask` renders the task shell (taskDefinition + envelope
107
+ // properties, recording envelopes); we only supply the `linkedResource`
108
+ // and/or `ioMapping` delta, so the envelope/taskDefinition serialization has
109
+ // a single source.
110
+ api.addServiceTask(node, { extraExt: taskExtensions(node.prompt, node.io) });
94
111
  api.connect(incoming, node.name);
95
112
  return [api.newEdge(node.name)];
96
113
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nanobpm/workflow",
3
- "version": "0.11.0",
3
+ "version": "0.13.0",
4
4
  "description": "Code-first durable orchestration for nanobpmn (ADR 0044/0045). Author a durable workflow as a declarative flow — `defineFlow(id, w => { w.run(...); w.signal(...); w.task(...) })` — and the SDK derives the executable BPMN model, the job types, and the message/correlation wiring, then hosts a generic worker. `w.run` is a local step, `w.signal` is a durable human-in-the-loop wait, `w.task` is a step serviced by an external worker. `defineFlow` is the single code-first surface; the imperative `defineWorkflow` replay function is experimental/internal, not a recommended API. No diagram, no task-type wiring, no correlation plumbing authored by hand — and you can optionally auto-generate a diagram (DI) for the derived model with `declarativeToLayoutedBpmn` via the optional `bpmn-auto-layout` peer dependency. Talks to a running nanobpmn gateway through @nanobpm/nano-sdk (the shared engine-transport spine — ADR 0055), which negotiates the Falcon protocol on a Nano server and REST elsewhere.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -48,6 +48,7 @@ import type {
48
48
  TimerStart,
49
49
  } from "./types.js";
50
50
  import type { Envelope, EnvelopeField } from "./envelope.js";
51
+ import type { HumanIoMapping } from "./io-mapping.js";
51
52
  import { assertIdent, assertTimerCycle, assertTimerDate, assertTimerDuration, escapeXml, jobType, messageName } from "./xml.js";
52
53
  import { eachNodeKind, requireNodeKind } from "./nodes/registry.js";
53
54
  // Import the generated barrel purely for its registration side effects: every
@@ -98,7 +99,11 @@ export interface FlowBuilder<C extends object = object> {
98
99
  * the flow's contracts, the handler's job variables and return value are typed
99
100
  * from that contract's `in`/`out` envelopes; otherwise they are `JsonObject`.
100
101
  */
101
- run<K extends string>(name: K, handler: TypedHandler<VarsOf<C, K>, ResultOf<C, K>>): FlowBuilder<C>;
102
+ run<K extends string>(
103
+ name: K,
104
+ handler: TypedHandler<VarsOf<C, K>, ResultOf<C, K>>,
105
+ opts?: { io?: HumanIoMapping },
106
+ ): FlowBuilder<C>;
102
107
  /**
103
108
  * A durable activity served by a worker OUTSIDE this program (a BPMN service
104
109
  * task, but no locally-hosted handler). Its job type defaults to the derived
@@ -111,7 +116,7 @@ export interface FlowBuilder<C extends object = object> {
111
116
  * workers must poll. Its contract envelopes (if any) type the model, not a
112
117
  * local handler.
113
118
  */
114
- task<K extends string>(name: K, opts?: { jobType?: string }): FlowBuilder<C>;
119
+ task<K extends string>(name: K, opts?: { jobType?: string; io?: HumanIoMapping }): FlowBuilder<C>;
115
120
  /**
116
121
  * A durable wait for an external/human event, correlated on a process
117
122
  * variable (a BPMN message intermediate catch event). Resume it with
package/src/index.ts CHANGED
@@ -46,6 +46,7 @@ export type { FlowBuilder } from "./declarative.js";
46
46
  // The `.boundary(...)` option type (its builder method augments FlowBuilder from
47
47
  // the boundary kind module — S2/#317).
48
48
  export type { BoundaryOptions } from "./nodes/boundary.js";
49
+ export type { HumanOptions, HumanIoMapping, HumanIoEntry } from "./nodes/human.js";
49
50
  export { layoutBpmn, declarativeToLayoutedBpmn } from "./layout.js";
50
51
  export { envelope } from "./envelope.js";
51
52
  export type {
@@ -0,0 +1,85 @@
1
+ // The single source of truth for a Zeebe `<zeebe:ioMapping>` — its shape, its
2
+ // authoring-time validation, and its BPMN emission — shared by EVERY node kind
3
+ // that carries variable mappings (`human` user tasks, `task`/`run` service
4
+ // tasks). Derivation over duplication (AGENTS.md): one interface, one validator,
5
+ // one renderer, so the ioMapping surface can never drift between node kinds.
6
+
7
+ import { escapeXml } from "./xml.js";
8
+
9
+ /** A single Zeebe I/O mapping entry — a FEEL `source` copied to/from a process
10
+ * variable `target`. */
11
+ export interface HumanIoEntry {
12
+ /** FEEL expression evaluated for the mapping (e.g. `=orderId`). */
13
+ source: string;
14
+ /** The process variable the value is written to. */
15
+ target: string;
16
+ }
17
+
18
+ /** A `<zeebe:ioMapping>`: input mappings applied on activation and output
19
+ * mappings applied on completion. */
20
+ export interface HumanIoMapping {
21
+ input?: HumanIoEntry[];
22
+ output?: HumanIoEntry[];
23
+ }
24
+
25
+ /** A present, non-empty, non-whitespace-only string — the shape every id /
26
+ * assignment / mapping-expression field must have. A blank value would emit a
27
+ * meaningless BPMN attribute (an empty ioMapping source/target, formId,
28
+ * assignee, …) that fails or misbehaves at deploy/run time, so we fail fast at
29
+ * build. Non-blank values are kept verbatim — FEEL expressions are never
30
+ * trimmed or normalized. */
31
+ export function isNonBlankString(v: unknown): v is string {
32
+ return typeof v === "string" && v.trim() !== "";
33
+ }
34
+
35
+ function isEntry(e: unknown): e is HumanIoEntry {
36
+ if (e === null || typeof e !== "object") return false;
37
+ if (!("source" in e) || !("target" in e)) return false;
38
+ const { source, target } = e;
39
+ return isNonBlankString(source) && isNonBlankString(target);
40
+ }
41
+
42
+ function validateEntries(label: string, dir: "input" | "output", entries: HumanIoEntry[] | undefined): void {
43
+ if (entries === undefined) return;
44
+ if (!Array.isArray(entries)) throw new Error(`${label} io.${dir} must be an array of { source, target }`);
45
+ for (const e of entries) {
46
+ if (!isEntry(e)) {
47
+ throw new Error(`${label} io.${dir} entries must be { source: non-empty string, target: non-empty string }`);
48
+ }
49
+ }
50
+ }
51
+
52
+ /** Validate an optional io mapping at authoring time. `label` is the node's
53
+ * self-description (e.g. `human("decide")` or `task("record")`) so the thrown
54
+ * message names the offending step. An `undefined` mapping is allowed (no
55
+ * ioMapping is emitted); a non-object / array value, or a malformed entry, fails
56
+ * fast. */
57
+ export function assertIoMapping(label: string, io: HumanIoMapping | undefined): void {
58
+ if (io === undefined) return;
59
+ if (io === null || typeof io !== "object" || Array.isArray(io)) {
60
+ throw new Error(`${label} { io } must be an object { input?, output? }`);
61
+ }
62
+ validateEntries(label, "input", io.input);
63
+ validateEntries(label, "output", io.output);
64
+ }
65
+
66
+ function renderMappings(entries: HumanIoEntry[] | undefined, tag: "input" | "output"): string {
67
+ if (!entries || entries.length === 0) return "";
68
+ return entries
69
+ .map((e) => ` <zeebe:${tag} source="${escapeXml(e.source)}" target="${escapeXml(e.target)}" />\n`)
70
+ .join("");
71
+ }
72
+
73
+ /** Emit a single `<zeebe:ioMapping>` (inputs then outputs) for the mapping, or
74
+ * the empty string when it has no entries. The 8-space element / 10-space entry
75
+ * indentation matches both the user-task and service-task emission, so a caller
76
+ * can splice the result straight into either `<bpmn:extensionElements>`. */
77
+ export function renderIoMapping(io: HumanIoMapping | undefined): string {
78
+ if (!io || ((io.input?.length ?? 0) === 0 && (io.output?.length ?? 0) === 0)) return "";
79
+ return (
80
+ ` <zeebe:ioMapping>\n` +
81
+ renderMappings(io.input, "input") +
82
+ renderMappings(io.output, "output") +
83
+ ` </zeebe:ioMapping>\n`
84
+ );
85
+ }
@@ -0,0 +1,129 @@
1
+ // Built-in flow-node kind: `human` — a human-in-the-loop approval step (a BPMN
2
+ // `<bpmn:userTask>` with the Zeebe user-task marker). It carries a form binding
3
+ // and, optionally, an assignment (assignee / candidate groups) and an I/O
4
+ // mapping — the surface every nwf approval gate (answer escalation, plan review,
5
+ // trial-merge decision) is built from. Contributed from this module through the
6
+ // extension seam (epic #314, S3/#318) — no central union or dispatch is edited.
7
+
8
+ import { escapeXml } from "../xml.js";
9
+ import { incomingOutgoing } from "../declarative.js";
10
+ import { assertIoMapping, isNonBlankString, renderIoMapping } from "../io-mapping.js";
11
+ import type { HumanIoMapping } from "../io-mapping.js";
12
+ import { registerNodeKind } from "./registry.js";
13
+
14
+ // The ioMapping shape lives in the shared `io-mapping` module (single source of
15
+ // truth across node kinds); re-export it so existing `@nanobpm/workflow`
16
+ // consumers keep importing `HumanIoMapping` / `HumanIoEntry` unchanged.
17
+ export type { HumanIoEntry, HumanIoMapping } from "../io-mapping.js";
18
+
19
+ /** Options for {@link FlowBuilder.human}. `form` is the `zeebe:formDefinition`
20
+ * form id; `assignee` / `candidateGroups` populate a `zeebe:assignmentDefinition`
21
+ * (supply either or both); `io` populates a `zeebe:ioMapping`. */
22
+ export interface HumanOptions {
23
+ /** The `zeebe:formDefinition` form id bound to this task. */
24
+ form: string;
25
+ /** A single assignee (a static user id or a FEEL expression like
26
+ * `=escalationAssignee`). */
27
+ assignee?: string;
28
+ /** The candidate groups allowed to claim the task (a static group id or a FEEL
29
+ * expression). */
30
+ candidateGroups?: string;
31
+ /** Input/output variable mappings. */
32
+ io?: HumanIoMapping;
33
+ }
34
+
35
+ /** The `human` node shape (its `FlowNode` variant). */
36
+ interface HumanNode {
37
+ kind: "human";
38
+ name: string;
39
+ form: string;
40
+ assignee?: string;
41
+ candidateGroups?: string;
42
+ io?: HumanIoMapping;
43
+ }
44
+
45
+ declare module "../types.js" {
46
+ interface FlowNodeRegistry {
47
+ human: HumanNode;
48
+ }
49
+ }
50
+
51
+ declare module "../declarative.js" {
52
+ interface FlowBuilder<C extends object> {
53
+ /**
54
+ * A human-in-the-loop approval step (a BPMN `<bpmn:userTask>` with the Zeebe
55
+ * user-task marker). `opts.form` binds a `zeebe:formDefinition`; the optional
56
+ * `assignee` / `candidateGroups` populate a `zeebe:assignmentDefinition`
57
+ * (either or both), and `io` populates a `zeebe:ioMapping`. Resume it from a
58
+ * task list / the Zeebe user-task API; the token waits durably until the task
59
+ * is completed.
60
+ */
61
+ human<K extends string>(name: K, opts: HumanOptions): FlowBuilder<C>;
62
+ }
63
+ }
64
+
65
+ /** A present, non-empty, non-whitespace-only string — the shape every human()
66
+ * id / assignment field must have. A blank value would emit a meaningless BPMN
67
+ * attribute (an empty formId, assignee, or candidateGroups) that fails or
68
+ * misbehaves at deploy/run time, so we fail fast at build. Shared with the
69
+ * ioMapping validator via `io-mapping`. */
70
+
71
+ function renderAssignment(node: HumanNode): string {
72
+ if (node.assignee === undefined && node.candidateGroups === undefined) return "";
73
+ const attrs = [
74
+ node.assignee !== undefined ? ` assignee="${escapeXml(node.assignee)}"` : "",
75
+ node.candidateGroups !== undefined ? ` candidateGroups="${escapeXml(node.candidateGroups)}"` : "",
76
+ ].join("");
77
+ return ` <zeebe:assignmentDefinition${attrs} />\n`;
78
+ }
79
+
80
+ function renderUserTask(node: HumanNode, inc: string[], outg: string[]): string {
81
+ const id = node.name;
82
+ const ext =
83
+ ` <bpmn:extensionElements>\n` +
84
+ ` <zeebe:formDefinition formId="${escapeXml(node.form)}" />\n` +
85
+ ` <zeebe:userTask />\n` +
86
+ renderAssignment(node) +
87
+ renderIoMapping(node.io) +
88
+ ` </bpmn:extensionElements>`;
89
+ return (
90
+ ` <bpmn:userTask id="${escapeXml(id)}" name="${escapeXml(id)}">\n` +
91
+ ext +
92
+ "\n" +
93
+ incomingOutgoing(inc, outg) +
94
+ ` </bpmn:userTask>`
95
+ );
96
+ }
97
+
98
+ registerNodeKind("human", {
99
+ build: (api) => (name: string, opts: HumanOptions) => {
100
+ api.claim(name);
101
+ if (opts === null || typeof opts !== "object") {
102
+ throw new Error(`human("${name}") needs an options object { form, … }`);
103
+ }
104
+ if (!isNonBlankString(opts.form)) {
105
+ throw new Error(`human("${name}") needs a non-empty { form } (the zeebe:formDefinition form id)`);
106
+ }
107
+ if (opts.assignee !== undefined && !isNonBlankString(opts.assignee)) {
108
+ throw new Error(`human("${name}") { assignee } must be a non-empty string`);
109
+ }
110
+ if (opts.candidateGroups !== undefined && !isNonBlankString(opts.candidateGroups)) {
111
+ throw new Error(`human("${name}") { candidateGroups } must be a non-empty string`);
112
+ }
113
+ assertIoMapping(`human("${name}")`, opts.io);
114
+ api.out.push({
115
+ kind: "human",
116
+ name,
117
+ form: opts.form,
118
+ assignee: opts.assignee,
119
+ candidateGroups: opts.candidateGroups,
120
+ io: opts.io,
121
+ });
122
+ return api.self();
123
+ },
124
+ emit: (node, incoming, _loop, api) => {
125
+ api.addNode({ id: node.name, render: (inc, outg) => renderUserTask(node, inc, outg) });
126
+ api.connect(incoming, node.name);
127
+ return [api.newEdge(node.name)];
128
+ },
129
+ });
@@ -11,6 +11,7 @@ import "./branch.js";
11
11
  import "./break.js";
12
12
  import "./continue.js";
13
13
  import "./forEach.js";
14
+ import "./human.js";
14
15
  import "./loop.js";
15
16
  import "./parallel.js";
16
17
  import "./race.js";
package/src/nodes/run.ts CHANGED
@@ -5,24 +5,27 @@
5
5
  // handlers — with NO edit to the shared union or a central dispatch switch.
6
6
 
7
7
  import type { NodeEnvelopes, StepHandler } from "../types.js";
8
+ import { assertIoMapping, renderIoMapping } from "../io-mapping.js";
9
+ import type { HumanIoMapping } from "../io-mapping.js";
8
10
  import { registerNodeKind } from "./registry.js";
9
11
 
10
12
  declare module "../types.js" {
11
13
  interface FlowNodeRegistry {
12
- run: { kind: "run"; name: string; envelopes?: NodeEnvelopes };
14
+ run: { kind: "run"; name: string; envelopes?: NodeEnvelopes; io?: HumanIoMapping };
13
15
  }
14
16
  }
15
17
 
16
18
  registerNodeKind("run", {
17
- build: (api) => (name: string, handler: StepHandler) => {
19
+ build: (api) => (name: string, handler: StepHandler, opts?: { io?: HumanIoMapping }) => {
18
20
  api.claim(name);
19
21
  if (typeof handler !== "function") throw new Error(`run("${name}") needs a handler function`);
22
+ assertIoMapping(`run("${name}")`, opts?.io);
20
23
  api.handlers[name] = handler;
21
- api.out.push({ kind: "run", name, envelopes: api.contractEnvelopes(name) });
24
+ api.out.push({ kind: "run", name, envelopes: api.contractEnvelopes(name), io: opts?.io });
22
25
  return api.self();
23
26
  },
24
27
  emit: (node, incoming, _loop, api) => {
25
- api.addServiceTask(node);
28
+ api.addServiceTask(node, node.io !== undefined ? { extraExt: renderIoMapping(node.io) } : {});
26
29
  api.connect(incoming, node.name);
27
30
  return [api.newEdge(node.name)];
28
31
  },
package/src/nodes/task.ts CHANGED
@@ -13,6 +13,8 @@
13
13
 
14
14
  import type { NodeEnvelopes } from "../types.js";
15
15
  import { assertJobType, escapeXml } from "../xml.js";
16
+ import { assertIoMapping, renderIoMapping } from "../io-mapping.js";
17
+ import type { HumanIoEntry, HumanIoMapping } from "../io-mapping.js";
16
18
  import { registerNodeKind } from "./registry.js";
17
19
 
18
20
  /** Binds an LLM prompt resource to an agent service task. Emits, alongside the
@@ -44,6 +46,11 @@ declare module "../types.js" {
44
46
  jobType?: string;
45
47
  /** The agent prompt binding, when the task is an LLM-agent service task. */
46
48
  prompt?: PromptBinding;
49
+ /** An explicit `<zeebe:ioMapping>` on the service task — arbitrary input
50
+ * (and output) variable mappings, using the shared ioMapping shape. When a
51
+ * `prompt.append` is ALSO present it is folded into this single mapping as
52
+ * an extra `appendPrompt` input (no duplicate `ioMapping` element). */
53
+ io?: HumanIoMapping;
47
54
  };
48
55
  }
49
56
  }
@@ -56,9 +63,13 @@ declare module "../declarative.js" {
56
63
  * (`resourceType="GenericScript" linkName="prompt"`). Pair it with a
57
64
  * `{ jobType }` capability token (e.g. `senior:retro`) so an agent pool
58
65
  * services it. `prompt.append` (optional) feeds a FEEL expression to the
59
- * worker through a `zeebe:ioMapping` `appendPrompt` input.
66
+ * worker through a `zeebe:ioMapping` `appendPrompt` input. `io` (optional)
67
+ * adds arbitrary input/output variable mappings to the SAME `zeebe:ioMapping`.
60
68
  */
61
- task<K extends string>(name: K, opts: { jobType?: string; prompt: PromptBinding }): FlowBuilder<C>;
69
+ task<K extends string>(
70
+ name: K,
71
+ opts: { jobType?: string; prompt: PromptBinding; io?: HumanIoMapping },
72
+ ): FlowBuilder<C>;
62
73
  }
63
74
  }
64
75
 
@@ -93,57 +104,71 @@ function resolvePrompt(name: string, prompt: PromptBinding): PromptBinding {
93
104
  return resolved;
94
105
  }
95
106
 
96
- /** Build the prompt-specific `<bpmn:extensionElements>` delta appended to the
97
- * shared service-task emission: the `zeebe:linkedResources` prompt binding and
98
- * (optionally) a `zeebe:ioMapping` `appendPrompt` input. The task shell
107
+ /** Build the `<bpmn:extensionElements>` delta appended to the shared
108
+ * service-task emission: the optional `zeebe:linkedResources` prompt binding
109
+ * followed by a single, correctly-ordered `zeebe:ioMapping`. The task shell
99
110
  * (taskDefinition + envelope properties) is rendered by the built-in
100
111
  * `EmitApi.addServiceTask`, so this variant only contributes its delta and the
101
- * envelope serialization can never drift from the plain service task. */
102
- function promptExtensions(prompt: PromptBinding): string {
103
- const { resourceId, bindingType, append } = prompt;
112
+ * envelope serialization can never drift from the plain service task.
113
+ *
114
+ * The `ioMapping` is the SINGLE source for all variable mappings on the task:
115
+ * the explicit `io.input`/`io.output` plus — when `prompt.append` is present —
116
+ * the `appendPrompt` input, folded in as one more input entry so a task with
117
+ * both a `prompt.append` and an explicit `io.input` emits exactly one
118
+ * `<zeebe:ioMapping>` (never two). Element order matches the golden:
119
+ * `linkedResources` (when any) then `ioMapping` (when any), the latter after
120
+ * `taskDefinition`/`properties`. */
121
+ function taskExtensions(prompt: PromptBinding | undefined, io: HumanIoMapping | undefined): string {
104
122
  const linked =
105
- ` <zeebe:linkedResources>\n` +
106
- ` <zeebe:linkedResource resourceId="${escapeXml(resourceId)}" ` +
107
- `bindingType="${escapeXml(bindingType ?? "latest")}" resourceType="GenericScript" linkName="prompt" />\n` +
108
- ` </zeebe:linkedResources>\n`;
109
- const io =
110
- append !== undefined
111
- ? ` <zeebe:ioMapping>\n` +
112
- ` <zeebe:input source="${escapeXml(append)}" target="appendPrompt" />\n` +
113
- ` </zeebe:ioMapping>\n`
123
+ prompt !== undefined
124
+ ? ` <zeebe:linkedResources>\n` +
125
+ ` <zeebe:linkedResource resourceId="${escapeXml(prompt.resourceId)}" ` +
126
+ `bindingType="${escapeXml(prompt.bindingType ?? "latest")}" resourceType="GenericScript" linkName="prompt" />\n` +
127
+ ` </zeebe:linkedResources>\n`
114
128
  : "";
115
- return linked + io;
129
+ // Merge the explicit io mapping and the prompt's `appendPrompt` input into one
130
+ // mapping. The append input trails the explicit inputs — it appends runtime
131
+ // context to the bound prompt, so it reads naturally after the task's own
132
+ // inputs — and outputs follow, matching `renderIoMapping`'s input-then-output
133
+ // ordering.
134
+ const inputs: HumanIoEntry[] = [...(io?.input ?? [])];
135
+ if (prompt?.append !== undefined) inputs.push({ source: prompt.append, target: "appendPrompt" });
136
+ const merged: HumanIoMapping = { input: inputs, output: io?.output ?? [] };
137
+ return linked + renderIoMapping(merged);
116
138
  }
117
139
 
118
140
  registerNodeKind("task", {
119
- build: (api) => (name: string, opts?: { jobType?: string; prompt?: PromptBinding }) => {
141
+ build: (api) => (name: string, opts?: { jobType?: string; prompt?: PromptBinding; io?: HumanIoMapping }) => {
120
142
  api.claim(name);
121
143
  const override = opts?.jobType;
122
144
  if (override !== undefined) assertJobType("task jobType", override);
123
145
  const prompt = opts?.prompt !== undefined ? resolvePrompt(name, opts.prompt) : undefined;
146
+ assertIoMapping(`task("${name}")`, opts?.io);
124
147
  api.out.push({
125
148
  kind: "task",
126
149
  name,
127
150
  envelopes: api.contractEnvelopes(name),
128
151
  jobType: override,
129
152
  prompt,
153
+ io: opts?.io,
130
154
  });
131
155
  return api.self();
132
156
  },
133
157
  emit: (node, incoming, _loop, api) => {
134
- // No prompt → the standard service-task emission is preserved unchanged (no
135
- // `linkedResources` are emitted).
136
- if (node.prompt === undefined) {
158
+ // Neither a prompt binding nor an explicit io mapping → the standard
159
+ // service-task emission is preserved unchanged (no `linkedResources`, no
160
+ // `ioMapping`).
161
+ if (node.prompt === undefined && node.io === undefined) {
137
162
  api.addServiceTask(node);
138
163
  api.connect(incoming, node.name);
139
164
  return [api.newEdge(node.name)];
140
165
  }
141
- // Prompt binding present → the shared `addServiceTask` renders the task
142
- // shell (taskDefinition + envelope properties, recording envelopes); we only
143
- // supply the `zeebe:linkedResource` (and optional `appendPrompt` ioMapping)
144
- // delta, so the envelope/taskDefinition serialization has a single source.
145
- const prompt = node.prompt;
146
- api.addServiceTask(node, { extraExt: promptExtensions(prompt) });
166
+ // A prompt binding and/or an explicit io mapping present → the shared
167
+ // `addServiceTask` renders the task shell (taskDefinition + envelope
168
+ // properties, recording envelopes); we only supply the `linkedResource`
169
+ // and/or `ioMapping` delta, so the envelope/taskDefinition serialization has
170
+ // a single source.
171
+ api.addServiceTask(node, { extraExt: taskExtensions(node.prompt, node.io) });
147
172
  api.connect(incoming, node.name);
148
173
  return [api.newEdge(node.name)];
149
174
  },