@nanobpm/nano-agent-graph-engine 0.17.1 → 0.18.1

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/dist/guide.md CHANGED
@@ -124,7 +124,7 @@ export const triage = defineFlow("triage", { Start: { in: Input }, fetch: { in:
124
124
  - **First of**: `w.race({ timeout: { timer: { after: "PT1H" }, do: (b) => … }, reply: { signal: { correlationKey: "prKey" }, do: (b) => … } })` waits for whichever event comes first. A timer + signal race is a poll delay that a message can cut short.
125
125
  - **SLA on a step**: `w.task(…).boundary({ timer: "PT50M", onTimeout: (b) => b.run("sla", …) })` cancels the step when the timer fires and runs `onTimeout`, which then continues with the rest of the flow.
126
126
  - **Fixed branches**: `w.parallel([(b) => …, (b) => …])` runs every branch and joins them before continuing.
127
- - **Fan-out**: `w.forEach("items", "item", (b) => …, { outputCollection: "results", outputElement: "notes" })` runs one branch per list item, with results collected in order. Options: `sequential`, `completionCondition`. Up to 4 headless agent branches run at once.
127
+ - **Fan-out**: `w.forEach("items", "item", (b) => …, { outputCollection: "results", outputElement: "notes" })` runs one branch per list item, with results collected in order. Options: `sequential`, `completionCondition`, and `name` to label a multi-step body (it's drawn as an expanded sub-process). Up to 4 headless agent branches run at once.
128
128
  - **Waits**:
129
129
  - `w.timer(name, { after: "PT1H" })` waits for a duration.
130
130
  - `w.startOn({ cycle: "R/PT24H" })` starts the flow on a timer.
package/dist/index.js CHANGED
@@ -464,6 +464,12 @@ registerNodeKind("forEach", {
464
464
  }
465
465
  node.completionCondition = compCond;
466
466
  }
467
+ if (opts?.name !== void 0) {
468
+ if (typeof opts.name !== "string" || opts.name.trim() === "") {
469
+ throw new Error(`forEach("${itemVar}") name must be a non-empty string`);
470
+ }
471
+ node.name = opts.name.trim();
472
+ }
467
473
  if (node.outputElement && !node.outputCollection) {
468
474
  throw new Error(`forEach("${itemVar}") outputElement needs an outputCollection to collect into`);
469
475
  }
@@ -480,7 +486,7 @@ registerNodeKind("forEach", {
480
486
  return [api.newEdge(only.name)];
481
487
  }
482
488
  const subId = `Sub_${api.nextGw()}`;
483
- api.addSubProcess(subId, mi);
489
+ api.addSubProcess(subId, mi, node.name);
484
490
  api.connect(incoming, subId);
485
491
  api.pushScope(subId);
486
492
  const startId = `${subId}_start`;
@@ -12797,9 +12797,60 @@ async function layoutBpmn(bpmnXml) {
12797
12797
  }
12798
12798
  throw new Error('layoutBpmn requires the optional peer dependency "bpmn-auto-layout". Install it to generate diagram layout: npm i bpmn-auto-layout', { cause });
12799
12799
  }
12800
- const result = await layoutProcess2(bpmnXml);
12800
+ const result = await layoutProcess2(seedExpandedSubProcesses(bpmnXml));
12801
12801
  return typeof result === "string" ? result : result.xml;
12802
12802
  }
12803
+ var DI_NS = {
12804
+ bpmndi: "http://www.omg.org/spec/BPMN/20100524/DI",
12805
+ dc: "http://www.omg.org/spec/DD/20100524/DC"
12806
+ };
12807
+ function xmlAttrs(tag, allowSpacedEquals = false) {
12808
+ const attrs = /* @__PURE__ */ new Map();
12809
+ const body = tag.replace(/^<[^\s/>]+/, "");
12810
+ for (const m of body.matchAll(/([^\s=/>]+)(\s*)=(\s*)(?:"([^"]*)"|'([^']*)')/g)) {
12811
+ if (!allowSpacedEquals && (m[2] !== "" || m[3] !== ""))
12812
+ continue;
12813
+ const name2 = m[1];
12814
+ const value = m[4] ?? m[5];
12815
+ if (name2 !== void 0 && value !== void 0 && !attrs.has(name2))
12816
+ attrs.set(name2, value);
12817
+ }
12818
+ return attrs;
12819
+ }
12820
+ function xmlId(tag) {
12821
+ const id = xmlAttrs(tag).get("id");
12822
+ return id === void 0 || id === "" ? void 0 : id;
12823
+ }
12824
+ function maskNonMarkup(xml3) {
12825
+ return xml3.replace(/<!--[\s\S]*?-->|<!\[CDATA\[[\s\S]*?\]\]>|<\?[\s\S]*?\?>/g, (m) => " ".repeat(m.length));
12826
+ }
12827
+ function seedExpandedSubProcesses(bpmnXml) {
12828
+ const scan = maskNonMarkup(bpmnXml);
12829
+ if (/<(?:\w+:)?BPMNDiagram\b/.test(scan))
12830
+ return bpmnXml;
12831
+ const planes = [];
12832
+ for (const proc of scan.matchAll(/<bpmn:process\b[^>]*>[\s\S]*?<\/bpmn:process>/g)) {
12833
+ const procId = xmlId(proc[0].slice(0, proc[0].indexOf(">") + 1));
12834
+ if (procId === void 0)
12835
+ continue;
12836
+ const subs = [...proc[0].matchAll(/<bpmn:subProcess\b[^>]*>/g)].filter((m) => xmlAttrs(m[0]).get("triggeredByEvent") !== "true").map((m) => xmlId(m[0])).filter((id) => id !== void 0);
12837
+ if (subs.length === 0)
12838
+ continue;
12839
+ const shapes = subs.map((id, i) => `<bpmndi:BPMNShape id="Seed_${i}_${id}_di" bpmnElement="${id}" isExpanded="true"><dc:Bounds x="0" y="0" width="100" height="80"/></bpmndi:BPMNShape>`);
12840
+ planes.push(`<bpmndi:BPMNDiagram id="Seed_${procId}_diagram"><bpmndi:BPMNPlane id="Seed_${procId}_plane" bpmnElement="${procId}">` + shapes.join("") + `</bpmndi:BPMNPlane></bpmndi:BPMNDiagram>`);
12841
+ }
12842
+ if (planes.length === 0)
12843
+ return bpmnXml;
12844
+ const end = /<\/bpmn:definitions>\s*$/.exec(scan);
12845
+ const defs = /<bpmn:definitions\b[^>]*>/.exec(scan);
12846
+ if (end === null || defs === null)
12847
+ return bpmnXml;
12848
+ const defsAttrs = xmlAttrs(defs[0], true);
12849
+ const missing = Object.entries(DI_NS).filter(([prefix2]) => !defsAttrs.has(`xmlns:${prefix2}`)).map(([prefix2, uri2]) => ` xmlns:${prefix2}="${uri2}"`).join("");
12850
+ const insertAt = defs.index + "<bpmn:definitions".length;
12851
+ const xml3 = bpmnXml.slice(0, insertAt) + missing + bpmnXml.slice(insertAt, end.index) + planes.join("") + bpmnXml.slice(end.index);
12852
+ return xml3;
12853
+ }
12803
12854
 
12804
12855
  // src/layout-thread.ts
12805
12856
  var xml2 = await layoutBpmn(workerData.xml);
@@ -1,6 +1,7 @@
1
1
  import type { DeclarativeFlow, FlowContracts, FlowNode, JsonObject, NodeEnvelopes, StepHandler, TimerStart } from "./types.js";
2
2
  import type { Envelope } from "./envelope.js";
3
3
  import type { HumanIoMapping } from "./io-mapping.js";
4
+ import type { ForEachOpts } from "./nodes/forEach.js";
4
5
  import "./nodes/index.js";
5
6
  /** The TS payload type of a contract's input envelope (untyped fallback). */
6
7
  export type InPayload<Ct> = Ct extends {
@@ -149,6 +150,8 @@ export interface FlowBuilder<C extends object = object> {
149
150
  * item bound to the `itemVar` variable in the child's scope. A single-activity
150
151
  * body attaches the multi-instance characteristics to that activity; a
151
152
  * multi-step body is wrapped in an embedded multi-instance sub-process. Options:
153
+ * - `name` — label the multi-step body's sub-process on the diagram (e.g.
154
+ * "Review & challenge each file"); a single-step body keeps its step's name.
152
155
  * - `sequential` — run children one at a time (a sequential MI) instead of all
153
156
  * at once (the default: a parallel MI).
154
157
  * - `outputCollection` / `outputElement` — collect each child's `outputElement`
@@ -158,12 +161,7 @@ export interface FlowBuilder<C extends object = object> {
158
161
  * after a child completes, the remaining children are cancelled and the body
159
162
  * completes early.
160
163
  */
161
- forEach(collection: string, itemVar: string, body: Block<C>, opts?: {
162
- sequential?: boolean;
163
- outputCollection?: string;
164
- outputElement?: string;
165
- completionCondition?: string;
166
- }): FlowBuilder<C>;
164
+ forEach(collection: string, itemVar: string, body: Block<C>, opts?: ForEachOpts): FlowBuilder<C>;
167
165
  /** Exit the enclosing loop (routes to whatever follows it). Only valid inside
168
166
  * a `loop`. */
169
167
  break(): FlowBuilder<C>;
@@ -334,8 +332,9 @@ export interface EmitApi {
334
332
  addGateway(id: string, name?: string): RenderNode;
335
333
  /** Place a `<bpmn:parallelGateway>`. */
336
334
  addParallelGateway(id: string): RenderNode;
337
- /** Place an embedded `<bpmn:subProcess>` whose body renders in its own scope. */
338
- addSubProcess(id: string, mi: string): RenderNode;
335
+ /** Place an embedded `<bpmn:subProcess>` whose body renders in its own scope,
336
+ * optionally labelled with `name`. */
337
+ addSubProcess(id: string, mi: string, name?: string): RenderNode;
339
338
  /** Place a plain none `<bpmn:startEvent>` (for an embedded sub-process). */
340
339
  addPlainStart(id: string): void;
341
340
  /** Place a plain `<bpmn:endEvent>` (for an embedded sub-process). */
@@ -10,6 +10,19 @@ import type { DeclarativeFlow } from "./types.js";
10
10
  * clear error is thrown if it is missing.
11
11
  */
12
12
  export declare function layoutBpmn(bpmnXml: string): Promise<string>;
13
+ /**
14
+ * bpmn-auto-layout 2 draws a sub-process EXPANDED (its steps inline, on the
15
+ * main diagram) only when the input's own DI says `isExpanded="true"`; otherwise
16
+ * it collapses it onto a drill-down plane, which hides the steps — e.g. a
17
+ * multi-step `forEach` body — from anyone reading the diagram. A code-first
18
+ * model has no DI, so seed a minimal diagram that marks each embedded
19
+ * sub-process expanded (one plane per process). Input that already has a
20
+ * diagram is returned untouched: its expanded/collapsed choices are the author's.
21
+ * Only the `bpmn:` prefix our emitters write is recognised; other input passes
22
+ * through and lays out as before. Exported for tests only (not re-exported
23
+ * from the package index).
24
+ */
25
+ export declare function seedExpandedSubProcesses(bpmnXml: string): string;
13
26
  /**
14
27
  * Derive an executable BPMN model from a declarative flow AND auto-generate its
15
28
  * diagram (DI), so the model opens rendered in a modeller/viewer. Convenience
@@ -15,10 +15,23 @@ interface ForEachNode {
15
15
  outputElement?: string;
16
16
  /** FEEL boolean that, once true, completes the body early. */
17
17
  completionCondition?: string;
18
+ /** Label for the multi-step body's sub-process (what the box does, on the diagram). */
19
+ name?: string;
18
20
  }
19
21
  declare module "../types.js" {
20
22
  interface FlowNodeRegistry {
21
23
  forEach: ForEachNode;
22
24
  }
23
25
  }
26
+ /** `forEach` options — the single definition `FlowBuilder.forEach` uses too. */
27
+ export interface ForEachOpts {
28
+ /** Label the multi-step body's sub-process, e.g. "Review & challenge each file".
29
+ * A single-step body has no box of its own (the MI is lifted onto the step,
30
+ * which keeps its own name), so the label does not apply there. */
31
+ name?: string;
32
+ sequential?: boolean;
33
+ outputCollection?: string;
34
+ outputElement?: string;
35
+ completionCondition?: string;
36
+ }
24
37
  export {};
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nanobpm/workflow",
3
- "version": "0.18.0",
3
+ "version": "0.19.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",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nanobpm/nano-agent-graph-engine",
3
- "version": "0.17.1",
3
+ "version": "0.18.1",
4
4
  "description": "Durable BPMN process engine for Claude Code: code-first flows (defineFlow) that orchestrate scripts, Claude Code agents and people.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -63,7 +63,7 @@
63
63
  "@nanobpm/agentic": "^0.16.0",
64
64
  "@nanobpm/engine-wasm": "^0.11.0",
65
65
  "@nanobpm/nano-app-schema": "^0.11.0",
66
- "@nanobpm/workflow": "^0.18.0",
66
+ "@nanobpm/workflow": "^0.19.0",
67
67
  "@tailwindcss/cli": "^4.3.5",
68
68
  "@tanstack/react-query": "^5.107.1",
69
69
  "@types/node": "^26.6.6",
@@ -124,7 +124,7 @@ export const triage = defineFlow("triage", { Start: { in: Input }, fetch: { in:
124
124
  - **First of**: `w.race({ timeout: { timer: { after: "PT1H" }, do: (b) => … }, reply: { signal: { correlationKey: "prKey" }, do: (b) => … } })` waits for whichever event comes first. A timer + signal race is a poll delay that a message can cut short.
125
125
  - **SLA on a step**: `w.task(…).boundary({ timer: "PT50M", onTimeout: (b) => b.run("sla", …) })` cancels the step when the timer fires and runs `onTimeout`, which then continues with the rest of the flow.
126
126
  - **Fixed branches**: `w.parallel([(b) => …, (b) => …])` runs every branch and joins them before continuing.
127
- - **Fan-out**: `w.forEach("items", "item", (b) => …, { outputCollection: "results", outputElement: "notes" })` runs one branch per list item, with results collected in order. Options: `sequential`, `completionCondition`. Up to 4 headless agent branches run at once.
127
+ - **Fan-out**: `w.forEach("items", "item", (b) => …, { outputCollection: "results", outputElement: "notes" })` runs one branch per list item, with results collected in order. Options: `sequential`, `completionCondition`, and `name` to label a multi-step body (it's drawn as an expanded sub-process). Up to 4 headless agent branches run at once.
128
128
  - **Waits**:
129
129
  - `w.timer(name, { after: "PT1H" })` waits for a duration.
130
130
  - `w.startOn({ cycle: "R/PT24H" })` starts the flow on a timer.