@powerhousedao/reactor-workflow 6.2.3-dev.88 → 6.2.3-dev.90

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.
@@ -7,7 +7,7 @@ import { promisify } from "node:util";
7
7
  import { gunzip } from "node:zlib";
8
8
  import { childLogger } from "document-model";
9
9
  import { fileURLToPath } from "node:url";
10
- import { BRANCH_PORTS, CORE_PIECE_NAME, CORE_PIECE_NAME as CORE_PIECE_NAME$1, NEXT_PORT, PIECE_ACTION_PORTS, SCHEDULE_MODES, SCHEDULE_UNIT_MS, STOP_PORTS, TRIGGER_PORTS, checkTriggerStrategy, referenceDocuments, undeclaredPortEdges } from "@powerhousedao/pieces-framework/workflow";
10
+ import { BRANCH_PORTS, CALL_FLOW_ACTION, CORE_PIECE_NAME, CORE_PIECE_NAME as CORE_PIECE_NAME$1, LOOP_ACTION, LOOP_EACH_PORT, LOOP_PORTS, NEXT_PORT, OTHERWISE_PORT, PIECE_ACTION_PORTS, ROUTER_ACTION, SCHEDULE_MODES, SCHEDULE_UNIT_MS, STOP_PORTS, SUBFLOWS_PIECE, TRIGGER_PORTS, checkTriggerStrategy, referenceDocuments, routerBranchName, routerBranchPort, routerBranches, routerPorts, stepPorts, undeclaredPortEdges } from "@powerhousedao/pieces-framework/workflow";
11
11
  import { Cron } from "croner";
12
12
  import { createHash, randomBytes, randomUUID } from "node:crypto";
13
13
  import { ssrfIpClassifier } from "@powerhousedao/pieces-framework/host";
@@ -495,6 +495,18 @@ function describeOAuth2(raw) {
495
495
  ...extra && Object.keys(extra).length > 0 ? { extra } : {}
496
496
  };
497
497
  }
498
+ const NOOP_HEAD = /^(?:async)?(?:function[\w$]*)?\(\)(?:=>)?/;
499
+ const DOWNLEVELED = /^\{?(?:return)?(?:__awaiter|__async)\(this,[^{]*function\*\(\)(\{.*\})\);?\}?$/;
500
+ const NOOP_BODY = /^\{?(?:return)?(?:await|yield)?(?:Promise\.resolve\(\))?;?\}?$/;
501
+ function declaresOnStart(onStart) {
502
+ if (typeof onStart !== "function") return false;
503
+ if (onStart.length > 0) return true;
504
+ const source = Function.prototype.toString.call(onStart).replace(/\s+/g, "");
505
+ const head = NOOP_HEAD.exec(source);
506
+ if (!head) return true;
507
+ const body = source.slice(head[0].length);
508
+ return !NOOP_BODY.test(DOWNLEVELED.exec(body)?.[1] ?? body);
509
+ }
498
510
  function describeHandshake(trigger) {
499
511
  const strategy = trigger.handshakeConfiguration?.strategy;
500
512
  if (!strategy || strategy === "NONE") return void 0;
@@ -678,6 +690,7 @@ function buildDescriptor(piece, source, options = {}) {
678
690
  ...trigger.sampleData !== void 0 && trigger.sampleData !== null ? { sampleData: trigger.sampleData } : {},
679
691
  handshake: describeHandshake(trigger),
680
692
  ...optional("renew", triggerRenew(trigger)),
693
+ ...declaresOnStart(trigger.onStart) ? { onStart: true } : {},
681
694
  ...optional("propertyGroups", describeGroups(trigger.propertyGroups)),
682
695
  ...options.hostFed ? {} : withUnsupported(unsupportedTrigger(trigger))
683
696
  }));
@@ -787,6 +800,9 @@ const STORE_PUT = "store.put";
787
800
  const STORE_DELETE = "store.delete";
788
801
  const FILES_UPLOAD = "files.upload";
789
802
  const WAITPOINT_CREATE = "waitpoint.create";
803
+ const FLOWS_LIST = "flows.list";
804
+ const HOST_HTTP = "host.http";
805
+ const HOST_ORIGIN = "http://reactor.invalid";
790
806
  //#endregion
791
807
  //#region src/pieces/activepieces/worker/host-call.ts
792
808
  const DEFAULT_HOST_CALL_TIMEOUT_MS = 1e4;
@@ -2382,10 +2398,22 @@ function resolveResumedCoreInput(step, scope) {
2382
2398
  }
2383
2399
  function resolveStepInputRaw(step, scope) {
2384
2400
  const { config } = step;
2385
- if (step.pieceName !== CORE_PIECE_NAME || step.actionName !== "branch" || config === null || typeof config !== "object" || Array.isArray(config) || !Object.hasOwn(config, "left")) return resolveExpressions(config, scope);
2401
+ if (step.pieceName !== CORE_PIECE_NAME || config === null || typeof config !== "object" || Array.isArray(config)) return resolveExpressions(config, scope);
2386
2402
  const record = config;
2403
+ if (step.actionName === "branch") return resolveCondition(record, scope);
2404
+ if (step.actionName === ROUTER_ACTION && Array.isArray(record.branches)) {
2405
+ const branches = record.branches;
2406
+ return {
2407
+ ...resolveExpressions(Object.fromEntries(Object.entries(record).filter(([key]) => key !== "branches")), scope),
2408
+ branches: branches.map((branch) => branch !== null && typeof branch === "object" && !Array.isArray(branch) ? resolveCondition(branch, scope) : resolveExpressions(branch, scope))
2409
+ };
2410
+ }
2411
+ return resolveExpressions(config, scope);
2412
+ }
2413
+ function resolveCondition(record, scope) {
2414
+ if (!Object.hasOwn(record, "left")) return resolveExpressions(record, scope);
2387
2415
  const operator = resolveExpressions(record.operator, scope);
2388
- if (typeof operator !== "string" || !EXISTENCE_OPERATORS.has(operator)) return resolveExpressions(config, scope);
2416
+ if (typeof operator !== "string" || !EXISTENCE_OPERATORS.has(operator)) return resolveExpressions(record, scope);
2389
2417
  return Object.fromEntries(Object.entries(record).map(([key, value]) => [key, key === "left" ? resolveOrMissing(value, scope) : resolveExpressions(value, scope)]));
2390
2418
  }
2391
2419
  function resolveOrMissing(value, scope) {
@@ -2668,9 +2696,9 @@ function withDisplay(trigger, display) {
2668
2696
  }
2669
2697
  //#endregion
2670
2698
  //#region src/pieces/core/actions.ts
2671
- const operators = Object.entries(BRANCH_OPERATORS);
2672
- const BINARY_OPERATORS = operators.filter(([, spec]) => spec.binary).map(([name]) => name);
2673
- const TEXT_OPERATORS = operators.filter(([, spec]) => spec.text).map(([name]) => name);
2699
+ const operators$1 = Object.entries(BRANCH_OPERATORS);
2700
+ const BINARY_OPERATORS$1 = operators$1.filter(([, spec]) => spec.binary).map(([name]) => name);
2701
+ const TEXT_OPERATORS$1 = operators$1.filter(([, spec]) => spec.text).map(([name]) => name);
2674
2702
  const branchAction = withPorts(createAction({
2675
2703
  name: "branch",
2676
2704
  displayName: "Branch",
@@ -2688,7 +2716,7 @@ const branchAction = withPorts(createAction({
2688
2716
  description: "How the value is tested.",
2689
2717
  required: true,
2690
2718
  defaultValue: DEFAULT_BRANCH_OPERATOR,
2691
- options: { options: operators.map(([value, spec]) => ({
2719
+ options: { options: operators$1.map(([value, spec]) => ({
2692
2720
  value,
2693
2721
  label: spec.label
2694
2722
  })) }
@@ -2697,12 +2725,12 @@ const branchAction = withPorts(createAction({
2697
2725
  displayName: "Compared with",
2698
2726
  description: "The value the condition compares to.",
2699
2727
  required: true
2700
- }), shownWhen("operator", BINARY_OPERATORS)),
2728
+ }), shownWhen("operator", BINARY_OPERATORS$1)),
2701
2729
  caseSensitive: withHints(Property.Checkbox({
2702
2730
  displayName: "Case sensitive",
2703
2731
  description: "Match letter case exactly.",
2704
2732
  required: false
2705
- }), shownWhen("operator", TEXT_OPERATORS))
2733
+ }), shownWhen("operator", TEXT_OPERATORS$1))
2706
2734
  },
2707
2735
  run: (ctx) => Promise.resolve(evaluateBranch(ctx.propsValue))
2708
2736
  }), {
@@ -2764,6 +2792,122 @@ const assertAction = withPorts(createAction({
2764
2792
  portOf: () => NEXT_PORT
2765
2793
  });
2766
2794
  //#endregion
2795
+ //#region src/pieces/core/flow.ts
2796
+ const operators = Object.entries(BRANCH_OPERATORS);
2797
+ const BINARY_OPERATORS = operators.filter(([, spec]) => spec.binary).map(([name]) => name);
2798
+ const TEXT_OPERATORS = operators.filter(([, spec]) => spec.text).map(([name]) => name);
2799
+ function evaluateRouter(config) {
2800
+ const branches = routerBranches(config);
2801
+ for (const [i, branch] of branches.entries()) {
2802
+ const name = routerBranchName(branch, i + 1);
2803
+ let matched;
2804
+ try {
2805
+ const { left, operator, right, caseSensitive } = branch;
2806
+ matched = evaluateBranch({
2807
+ left,
2808
+ operator,
2809
+ right,
2810
+ caseSensitive
2811
+ }).result;
2812
+ } catch (error) {
2813
+ const reason = error instanceof BranchConfigError ? error.message.replace(/^Branch: /, "") : String(error);
2814
+ throw new Error(`Router: branch "${name}": ${reason}`, { cause: error });
2815
+ }
2816
+ if (matched) return {
2817
+ branch: name,
2818
+ index: i + 1
2819
+ };
2820
+ }
2821
+ return {
2822
+ branch: null,
2823
+ index: null
2824
+ };
2825
+ }
2826
+ const routerAction = withPorts(createAction({
2827
+ name: ROUTER_ACTION,
2828
+ displayName: "Router",
2829
+ description: "Continues on the first branch whose condition holds, else on Otherwise. Each branch tests one value, as Branch does.",
2830
+ requireAuth: false,
2831
+ props: { branches: Property.Array({
2832
+ displayName: "Branches",
2833
+ description: "Tested in order; the first that holds is taken.",
2834
+ required: true,
2835
+ properties: {
2836
+ name: Property.ShortText({
2837
+ displayName: "Name",
2838
+ description: "Labels the branch's port.",
2839
+ required: false
2840
+ }),
2841
+ left: Property.ShortText({
2842
+ displayName: "Value",
2843
+ description: "What to test, usually an expression.",
2844
+ required: true,
2845
+ placeholder: "{{steps.fetch.output.status}}"
2846
+ }),
2847
+ operator: Property.StaticDropdown({
2848
+ displayName: "Condition",
2849
+ description: "How the value is tested.",
2850
+ required: true,
2851
+ defaultValue: DEFAULT_BRANCH_OPERATOR,
2852
+ options: { options: operators.map(([value, spec]) => ({
2853
+ value,
2854
+ label: spec.label
2855
+ })) }
2856
+ }),
2857
+ right: withHints(Property.ShortText({
2858
+ displayName: "Compared with",
2859
+ description: "The value the condition compares to.",
2860
+ required: true
2861
+ }), shownWhen("operator", BINARY_OPERATORS)),
2862
+ caseSensitive: withHints(Property.Checkbox({
2863
+ displayName: "Case sensitive",
2864
+ description: "Match letter case exactly.",
2865
+ required: false
2866
+ }), shownWhen("operator", TEXT_OPERATORS))
2867
+ }
2868
+ }) },
2869
+ run: (ctx) => Promise.resolve(evaluateRouter(ctx.propsValue))
2870
+ }), {
2871
+ ports: routerPorts(void 0),
2872
+ portOf: (output) => {
2873
+ const { index } = output;
2874
+ return index === null ? OTHERWISE_PORT : routerBranchPort(index);
2875
+ }
2876
+ });
2877
+ var LoopItemsError = class extends Error {
2878
+ constructor(message) {
2879
+ super(`Loop: ${message}`);
2880
+ this.name = "LoopItemsError";
2881
+ }
2882
+ };
2883
+ function loopItems(config) {
2884
+ const items = config !== null && typeof config === "object" ? config.items : void 0;
2885
+ if (Array.isArray(items)) return items;
2886
+ throw new LoopItemsError(`items must be a list, got ${items === void 0 || items === null ? "nothing" : `${typeof items} ${JSON.stringify(items).slice(0, 60)}`}`);
2887
+ }
2888
+ const loopAction = withPorts(createAction({
2889
+ name: LOOP_ACTION,
2890
+ displayName: "Loop on items",
2891
+ description: "Runs the steps under For each item once per item, in order, then continues After loop. Those steps read the current item and its index (from 1) from this step's output.",
2892
+ requireAuth: false,
2893
+ props: { items: Property.ShortText({
2894
+ displayName: "Items",
2895
+ description: "An expression that resolves to a list.",
2896
+ required: true,
2897
+ placeholder: "{{steps.fetch.output.rows}}"
2898
+ }) },
2899
+ run: (ctx) => {
2900
+ const items = loopItems(ctx.propsValue);
2901
+ return Promise.resolve({
2902
+ count: items.length,
2903
+ items
2904
+ });
2905
+ }
2906
+ }), {
2907
+ ports: LOOP_PORTS,
2908
+ portOf: () => NEXT_PORT
2909
+ });
2910
+ //#endregion
2767
2911
  //#region src/pieces/core/triggers.ts
2768
2912
  function hostFed(name) {
2769
2913
  return () => Promise.reject(/* @__PURE__ */ new Error(`The "${name}" trigger is fired by the host, not by its hooks`));
@@ -3408,18 +3552,20 @@ const waitForWebhookAction = withPorts(createAction({
3408
3552
  });
3409
3553
  //#endregion
3410
3554
  //#region src/pieces/core/version.ts
3411
- const CORE_PIECE_VERSION = "6.2.3-dev.88";
3555
+ const CORE_PIECE_VERSION = "6.2.3-dev.90";
3412
3556
  //#endregion
3413
3557
  //#region src/pieces/core/index.ts
3414
3558
  const corePiece = createPiece({
3415
3559
  displayName: "Core",
3416
- description: "The engine's own blocks: branching, assertions, delays, approvals, stopping and webhook replies, and the triggers that belong to no service.",
3560
+ description: "The engine's own blocks: branching, routing, loops, assertions, delays, approvals, stopping and webhook replies, and the triggers that belong to no service.",
3417
3561
  logoUrl: "",
3418
3562
  authors: ["powerhouse"],
3419
3563
  auth: void 0,
3420
3564
  categories: [PieceCategory.CORE],
3421
3565
  actions: [
3422
3566
  branchAction,
3567
+ routerAction,
3568
+ loopAction,
3423
3569
  assertAction,
3424
3570
  delayAction,
3425
3571
  approvalAction,
@@ -3626,6 +3772,12 @@ function waitpointHandlers(port, execution, parsed, created) {
3626
3772
  return waitpoint;
3627
3773
  } };
3628
3774
  }
3775
+ function flowsHandlers(port) {
3776
+ return {
3777
+ [FLOWS_LIST]: (payload) => port.list(payload ?? {}),
3778
+ [HOST_HTTP]: (payload) => port.request(payload)
3779
+ };
3780
+ }
3629
3781
  function waitpointRequest(execution, parsed) {
3630
3782
  return {
3631
3783
  stepId: execution.step.id,
@@ -3758,11 +3910,13 @@ var ActivepiecesBlockExecutor = class {
3758
3910
  const uploads = this.uploadHandlers(stagingDir, signal);
3759
3911
  const stepTest = this.options.stepTest?.() ?? false;
3760
3912
  const waitpoints = stepTest ? void 0 : this.options.waitpoints?.();
3913
+ const flows = this.options.flows?.();
3761
3914
  const created = /* @__PURE__ */ new Set();
3762
- const hostCalls = pieceStore || uploads || waitpoints ? {
3915
+ const hostCalls = pieceStore || uploads || waitpoints || flows ? {
3763
3916
  ...pieceStore ? storeHandlers(pieceStore) : {},
3764
3917
  ...uploads,
3765
- ...waitpoints ? waitpointHandlers(waitpoints, execution, parsed, created) : {}
3918
+ ...waitpoints ? waitpointHandlers(waitpoints, execution, parsed, created) : {},
3919
+ ...flows ? flowsHandlers(flows) : {}
3766
3920
  } : void 0;
3767
3921
  const reactorTap = await this.options.reactorAccess?.({
3768
3922
  block: parsed,
@@ -3781,6 +3935,7 @@ var ActivepiecesBlockExecutor = class {
3781
3935
  ...stepTest ? { stepTest: true } : {},
3782
3936
  ...waitpoints ? { canPause: true } : {},
3783
3937
  ...waitpoints?.resumeBaseUrl ? { resumeBaseUrl: waitpoints.resumeBaseUrl } : {},
3938
+ ...flows ? { flows: flows.publicUrl ? { publicUrl: flows.publicUrl } : {} } : {},
3784
3939
  ...execution.resume ? {
3785
3940
  executionType: "RESUME",
3786
3941
  resumePayload: execution.resume.payload
@@ -4071,7 +4226,82 @@ function stepConfigHash(step) {
4071
4226
  }));
4072
4227
  }
4073
4228
  //#endregion
4229
+ //#region src/pieces/engine/loops.ts
4230
+ function isLoopStep(step) {
4231
+ return step.pieceName === CORE_PIECE_NAME && step.actionName === LOOP_ACTION;
4232
+ }
4233
+ function waitsForCallee(step, input) {
4234
+ return step.pieceName === SUBFLOWS_PIECE && step.actionName === CALL_FLOW_ACTION && typeof input === "object" && input !== null && input.waitForResponse === true;
4235
+ }
4236
+ function loopStructure(definition) {
4237
+ const { steps, edges } = definition;
4238
+ const keyOf = new Map(steps.map((step) => [step.id, step.key]));
4239
+ const key = (id) => keyOf.get(id) ?? id;
4240
+ const bodies = /* @__PURE__ */ new Map();
4241
+ let violation;
4242
+ for (const loop of steps.filter(isLoopStep)) {
4243
+ const body = /* @__PURE__ */ new Set();
4244
+ const queue = edges.filter((edge) => edge.from === loop.id && edge.port === LOOP_EACH_PORT).map((edge) => edge.to);
4245
+ while (queue.length > 0) {
4246
+ const id = queue.shift();
4247
+ if (body.has(id) || id === loop.id) continue;
4248
+ body.add(id);
4249
+ for (const edge of edges) {
4250
+ if (edge.from !== id) continue;
4251
+ queue.push(edge.to);
4252
+ if (edge.to === loop.id) violation ??= `Step "${key(id)}" in the body of loop "${loop.key}" leads back to the loop; the loop runs its body again for each item by itself`;
4253
+ }
4254
+ }
4255
+ bodies.set(loop.id, body);
4256
+ if (violation) continue;
4257
+ for (const edge of edges) {
4258
+ const entry = edge.from === loop.id && edge.port === LOOP_EACH_PORT;
4259
+ if (!body.has(edge.to) || body.has(edge.from) || entry) continue;
4260
+ const inside = edges.find((other) => other.to === edge.to && body.has(other.from));
4261
+ violation = inside ? `Step "${key(edge.to)}" is reached both from "${key(inside.from)}" in the body of loop "${loop.key}" and from "${key(edge.from)}" outside it; a loop body can only continue through the loop's "next" port` : `Step "${key(edge.to)}" in the body of loop "${loop.key}" is also reached from "${key(edge.from)}" outside it`;
4262
+ break;
4263
+ }
4264
+ }
4265
+ const owner = /* @__PURE__ */ new Map();
4266
+ for (const [loopId, body] of bodies) for (const id of body) {
4267
+ const current = owner.get(id);
4268
+ if (!current || bodies.get(current).size > body.size) owner.set(id, loopId);
4269
+ }
4270
+ return {
4271
+ bodies,
4272
+ owner,
4273
+ ...violation ? { violation } : {}
4274
+ };
4275
+ }
4276
+ function loopConfigHash(loop, definition, structure = loopStructure(definition)) {
4277
+ const body = structure.bodies.get(loop.id) ?? /* @__PURE__ */ new Set();
4278
+ const edges = definition.edges.filter((edge) => body.has(edge.from) || edge.from === loop.id && edge.port === LOOP_EACH_PORT).map((edge) => canonicalJson({
4279
+ from: edge.from,
4280
+ to: edge.to,
4281
+ port: edge.port,
4282
+ condition: edge.condition ?? null
4283
+ })).sort();
4284
+ return hashOf(stepConfigHash(loop), canonicalJson({
4285
+ body: [...body].sort(),
4286
+ edges
4287
+ }));
4288
+ }
4289
+ //#endregion
4074
4290
  //#region src/pieces/engine/coordinator.ts
4291
+ const LOOP_ROW_MAX_BYTES = 245760;
4292
+ const CORE_WAITS = /* @__PURE__ */ new Set([
4293
+ "delay",
4294
+ "approval",
4295
+ "waitForWebhook"
4296
+ ]);
4297
+ const utf8 = new TextEncoder();
4298
+ function jsonBytes(value) {
4299
+ try {
4300
+ return utf8.encode(JSON.stringify(value ?? null)).length;
4301
+ } catch {
4302
+ return Number.POSITIVE_INFINITY;
4303
+ }
4304
+ }
4075
4305
  const LOOKUP_FAILED = Object.freeze({});
4076
4306
  const ACTION_RETRY_DELAYS = {
4077
4307
  backoff: "EXPONENTIAL",
@@ -4106,7 +4336,8 @@ function lazyEntry(read) {
4106
4336
  function deadPortWarnings(definition, declaredPorts) {
4107
4337
  const blocks = new Map(definition.steps.map((step) => [step.id, {
4108
4338
  key: step.key,
4109
- block: stepBlock(step)
4339
+ block: stepBlock(step),
4340
+ config: step.config
4110
4341
  }]));
4111
4342
  if (definition.trigger) blocks.set(definition.trigger.id, {
4112
4343
  key: "trigger",
@@ -4114,7 +4345,8 @@ function deadPortWarnings(definition, declaredPorts) {
4114
4345
  });
4115
4346
  return undeclaredPortEdges(definition.edges, (id) => {
4116
4347
  const source = blocks.get(id);
4117
- return source ? declaredPorts(source.block) : void 0;
4348
+ const wired = definition.edges.filter((edge) => edge.from === id).map((edge) => edge.port);
4349
+ return source ? stepPorts(source.block, source.config, declaredPorts, wired) : void 0;
4118
4350
  }).map((edge) => {
4119
4351
  const source = blocks.get(edge.from);
4120
4352
  const target = blocks.get(edge.to)?.key ?? edge.to;
@@ -4149,6 +4381,11 @@ async function runWorkflow(options) {
4149
4381
  variables: Object.fromEntries((definition.variables ?? []).map((v) => [v.key, v.value ?? null]))
4150
4382
  };
4151
4383
  const records = /* @__PURE__ */ new Map();
4384
+ const stepSecrets = /* @__PURE__ */ new Map();
4385
+ const loops = loopStructure(definition);
4386
+ const configHashOf = (step) => isLoopStep(step) ? loopConfigHash(step, definition, loops) : stepConfigHash(step);
4387
+ let pathPrefix = "";
4388
+ let replaying = true;
4152
4389
  const edgeDecisions = /* @__PURE__ */ new Map();
4153
4390
  let runFailed;
4154
4391
  let runCancelled;
@@ -4176,12 +4413,16 @@ async function runWorkflow(options) {
4176
4413
  if (!options.onStep) return;
4177
4414
  const ordinal = executedCount++;
4178
4415
  try {
4179
- await options.onStep(record, ordinal);
4416
+ await options.onStep(pathPrefix ? {
4417
+ ...record,
4418
+ path: pathPrefix + record.stepId
4419
+ } : record, ordinal);
4180
4420
  } catch {}
4181
4421
  };
4182
- const decideOutgoing = (sourceId, port) => {
4422
+ const decideOutgoing = (sourceId, port, only) => {
4183
4423
  for (const edge of definition.edges) {
4184
4424
  if (edge.from !== sourceId) continue;
4425
+ if (only !== void 0 && edge.port !== only) continue;
4185
4426
  let taken = port !== void 0 && edge.port === port;
4186
4427
  if (taken && edge.condition) try {
4187
4428
  taken = evaluateCondition(edge.condition, scope);
@@ -4201,7 +4442,8 @@ async function runWorkflow(options) {
4201
4442
  key: step.key,
4202
4443
  pieceName: step.pieceName,
4203
4444
  blockName: step.actionName,
4204
- status: "SKIPPED"
4445
+ status: "SKIPPED",
4446
+ configHash: configHashOf(step)
4205
4447
  });
4206
4448
  decideOutgoing(step.id, void 0);
4207
4449
  };
@@ -4225,7 +4467,8 @@ async function runWorkflow(options) {
4225
4467
  blockName: step.actionName,
4226
4468
  status: "SKIPPED",
4227
4469
  output: null,
4228
- port: "next"
4470
+ port: "next",
4471
+ configHash: configHashOf(step)
4229
4472
  };
4230
4473
  records.set(step.id, record);
4231
4474
  await journal(record);
@@ -4247,39 +4490,59 @@ async function runWorkflow(options) {
4247
4490
  scope.steps[step.key] = failed ? { error: carried.error ?? "" } : lazyEntry(carried.output);
4248
4491
  decideOutgoing(step.id, port);
4249
4492
  };
4493
+ const settleBody = async (loop, settle) => {
4494
+ const body = loops.bodies.get(loop.id);
4495
+ for (const step of definition.steps) {
4496
+ if (!body?.has(step.id) || records.has(step.id)) continue;
4497
+ if (!await settle(step)) skipStep(step);
4498
+ }
4499
+ };
4500
+ const replayStep = async (step, replay) => {
4501
+ if (containsRedactedMarker(replay.output)) {
4502
+ refuseReplay(step);
4503
+ return;
4504
+ }
4505
+ const port = replay.port ?? "next";
4506
+ const output = replay.outputTruncated ? unavailableValue(`Output of step "${step.key}" is unavailable: it succeeded in the run being rerun, but the run journal truncated its output past the payload cap. The step is not re-run, because it had side effects. Fire the workflow again instead of rerunning it.`) : replay.output;
4507
+ const record = {
4508
+ stepId: step.id,
4509
+ key: step.key,
4510
+ pieceName: step.pieceName,
4511
+ blockName: step.actionName,
4512
+ status: "REPLAYED",
4513
+ output: replay.outputTruncated ? void 0 : replay.output,
4514
+ ...replay.outputTruncated && replay.truncatedOutput !== void 0 ? { journaledOutput: replay.truncatedOutput } : {},
4515
+ port,
4516
+ configHash: configHashOf(step)
4517
+ };
4518
+ records.set(step.id, record);
4519
+ await journal(record);
4520
+ scope.steps[step.key] = { output };
4521
+ decideOutgoing(step.id, port);
4522
+ };
4250
4523
  const executeStep = async (step) => {
4251
- const carried = options.carriedSteps?.get(step.id);
4524
+ const outer = pathPrefix === "";
4525
+ const carried = outer ? options.carriedSteps?.get(step.id) : void 0;
4252
4526
  if (carried) {
4253
4527
  carryStep(step, carried);
4528
+ if (isLoopStep(step)) await settleBody(step, (inner) => {
4529
+ const entry = options.carriedSteps?.get(inner.id);
4530
+ if (entry) carryStep(inner, entry);
4531
+ return entry !== void 0;
4532
+ });
4254
4533
  return;
4255
4534
  }
4256
4535
  if (step.skip === true) {
4257
4536
  await passStep(step);
4258
4537
  return;
4259
4538
  }
4260
- const replay = options.completedSteps?.get(step.id);
4539
+ const replay = replaying && !isLoopStep(step) ? options.completedSteps?.get(pathPrefix + step.id) : void 0;
4261
4540
  if (replay) {
4262
- if (containsRedactedMarker(replay.output)) {
4263
- refuseReplay(step);
4264
- return;
4265
- }
4266
- const port = replay.port ?? "next";
4267
- const output = replay.outputTruncated ? unavailableValue(`Output of step "${step.key}" is unavailable: it succeeded in the run being rerun, but the run journal truncated its output past the payload cap. The step is not re-run, because it had side effects. Fire the workflow again instead of rerunning it.`) : replay.output;
4268
- const record = {
4269
- stepId: step.id,
4270
- key: step.key,
4271
- pieceName: step.pieceName,
4272
- blockName: step.actionName,
4273
- status: "REPLAYED",
4274
- output: replay.outputTruncated ? void 0 : replay.output,
4275
- ...replay.outputTruncated && replay.truncatedOutput !== void 0 ? { journaledOutput: replay.truncatedOutput } : {},
4276
- port,
4277
- configHash: stepConfigHash(step)
4278
- };
4279
- records.set(step.id, record);
4280
- await journal(record);
4281
- scope.steps[step.key] = { output };
4282
- decideOutgoing(step.id, port);
4541
+ await replayStep(step, replay);
4542
+ return;
4543
+ }
4544
+ if (!outer && CORE_WAITS.has(step.actionName) && step.pieceName === CORE_PIECE_NAME) {
4545
+ await refuseWaitInLoop(step);
4283
4546
  return;
4284
4547
  }
4285
4548
  const ownRetry = step.retry !== void 0 && step.retry !== null;
@@ -4303,6 +4566,26 @@ async function runWorkflow(options) {
4303
4566
  });
4304
4567
  return;
4305
4568
  }
4569
+ if (!outer && waitsForCallee(step, input)) {
4570
+ await refuseWaitInLoop(step);
4571
+ return;
4572
+ }
4573
+ if (isLoopStep(step)) {
4574
+ let items;
4575
+ try {
4576
+ items = loopItems(input);
4577
+ } catch (error) {
4578
+ await endStep(step, {
4579
+ error,
4580
+ input,
4581
+ startedAt,
4582
+ attempt: 1
4583
+ });
4584
+ return;
4585
+ }
4586
+ await runLoop(step, input, items, startedAt);
4587
+ return;
4588
+ }
4306
4589
  for (let attempt = 1;; attempt++) {
4307
4590
  const attempts = attempt > 1 ? { attempts: attempt } : {};
4308
4591
  try {
@@ -4318,7 +4601,12 @@ async function runWorkflow(options) {
4318
4601
  });
4319
4602
  const port = result.port ?? "next";
4320
4603
  const values = [...runSecrets, ...result.redactValues ?? []];
4604
+ if (result.redactValues?.length) stepSecrets.set(step.id, result.redactValues);
4321
4605
  if (result.response) options.onRespond?.(result.response);
4606
+ if (result.paused && !outer) {
4607
+ await refuseWaitInLoop(step);
4608
+ return;
4609
+ }
4322
4610
  if (result.paused) {
4323
4611
  const record = {
4324
4612
  stepId: step.id,
@@ -4330,7 +4618,7 @@ async function runWorkflow(options) {
4330
4618
  output: journaled(referenceDocuments(result.output), values),
4331
4619
  startedAt,
4332
4620
  ...withPiece(pieceRecord(result.resolution)),
4333
- configHash: stepConfigHash(step),
4621
+ configHash: configHashOf(step),
4334
4622
  ...attempts
4335
4623
  };
4336
4624
  records.set(step.id, record);
@@ -4353,7 +4641,7 @@ async function runWorkflow(options) {
4353
4641
  startedAt,
4354
4642
  endedAt: (/* @__PURE__ */ new Date()).toISOString(),
4355
4643
  ...withPiece(pieceRecord(result.resolution)),
4356
- configHash: stepConfigHash(step),
4644
+ configHash: configHashOf(step),
4357
4645
  ...attempts
4358
4646
  };
4359
4647
  records.set(step.id, record);
@@ -4442,7 +4730,7 @@ async function runWorkflow(options) {
4442
4730
  startedAt,
4443
4731
  endedAt: (/* @__PURE__ */ new Date()).toISOString(),
4444
4732
  ...withPiece(pieceRecord(resolutionOf(error))),
4445
- configHash: stepConfigHash(step),
4733
+ configHash: configHashOf(step),
4446
4734
  ...attempt > 1 ? { attempts: attempt } : {}
4447
4735
  };
4448
4736
  records.set(step.id, record);
@@ -4469,29 +4757,155 @@ async function runWorkflow(options) {
4469
4757
  return runCancelled !== void 0;
4470
4758
  };
4471
4759
  const stopped = () => Boolean(runFailed || runCancelled || runWaiting || runStopped);
4472
- let progressed = true;
4473
- while (progressed && !stopped()) {
4474
- progressed = false;
4475
- for (const step of definition.steps) {
4476
- if (records.has(step.id)) continue;
4477
- const inbound = inboundEdges(step);
4478
- if (isEntryStep(step)) {
4479
- if (expiredBeforeStep()) break;
4480
- await executeStep(step);
4760
+ const ownedSteps = (loopId) => definition.steps.filter((step) => loops.owner.get(step.id) === loopId);
4761
+ async function walk(steps) {
4762
+ let progressed = true;
4763
+ while (progressed && !stopped()) {
4764
+ progressed = false;
4765
+ for (const step of steps) {
4766
+ if (records.has(step.id)) continue;
4767
+ const inbound = inboundEdges(step);
4768
+ if (isEntryStep(step)) {
4769
+ if (expiredBeforeStep()) break;
4770
+ await executeStep(step);
4771
+ progressed = true;
4772
+ if (stopped()) break;
4773
+ continue;
4774
+ }
4775
+ if (inbound.length === 0) continue;
4776
+ if (!inbound.every((edge) => edgeDecisions.has(edge.id))) continue;
4777
+ if (inbound.some((edge) => edgeDecisions.get(edge.id))) {
4778
+ if (expiredBeforeStep()) break;
4779
+ await executeStep(step);
4780
+ } else skipStep(step);
4481
4781
  progressed = true;
4482
4782
  if (stopped()) break;
4483
- continue;
4484
4783
  }
4485
- if (inbound.length === 0) continue;
4486
- if (!inbound.every((edge) => edgeDecisions.has(edge.id))) continue;
4487
- if (inbound.some((edge) => edgeDecisions.get(edge.id))) {
4488
- if (expiredBeforeStep()) break;
4489
- await executeStep(step);
4490
- } else skipStep(step);
4491
- progressed = true;
4492
- if (stopped()) break;
4493
4784
  }
4494
4785
  }
4786
+ async function refuseWaitInLoop(step) {
4787
+ const error = `Step "${step.key}" cannot wait inside a loop yet`;
4788
+ const record = {
4789
+ stepId: step.id,
4790
+ key: step.key,
4791
+ pieceName: step.pieceName,
4792
+ blockName: step.actionName,
4793
+ status: "FAILED",
4794
+ error,
4795
+ configHash: configHashOf(step)
4796
+ };
4797
+ records.set(step.id, record);
4798
+ await journal(record);
4799
+ runFailed ??= error;
4800
+ }
4801
+ async function runLoop(loop, input, items, startedAt) {
4802
+ const body = loops.bodies.get(loop.id) ?? /* @__PURE__ */ new Set();
4803
+ const bodySteps = definition.steps.filter((step) => body.has(step.id));
4804
+ const owned = ownedSteps(loop.id);
4805
+ const prefix = pathPrefix;
4806
+ const iterations = [];
4807
+ const last = items.length > 0 ? items[items.length - 1] : null;
4808
+ let iterationBytes = jsonBytes({
4809
+ item: last,
4810
+ index: items.length,
4811
+ count: items.length,
4812
+ iterations: [],
4813
+ iterationsTruncated: true
4814
+ });
4815
+ let iterationsTruncated = false;
4816
+ let interrupted;
4817
+ const bodySecrets = /* @__PURE__ */ new Set();
4818
+ const outerReplaying = replaying;
4819
+ const priorItems = replaying ? options.loopItems?.get(prefix + loop.id) : void 0;
4820
+ const itemKey = (item) => {
4821
+ try {
4822
+ return canonicalJson(JSON.parse(JSON.stringify(journaled(item, runSecrets) ?? null)));
4823
+ } catch {
4824
+ return;
4825
+ }
4826
+ };
4827
+ for (const [i, item] of items.entries()) {
4828
+ if (expiredBeforeStep()) break;
4829
+ for (const step of bodySteps) {
4830
+ records.delete(step.id);
4831
+ stepSecrets.delete(step.id);
4832
+ delete scope.steps[step.key];
4833
+ }
4834
+ for (const edge of definition.edges) if (body.has(edge.from) || edge.from === loop.id && edge.port === LOOP_EACH_PORT) edgeDecisions.delete(edge.id);
4835
+ scope.steps[loop.key] = { output: {
4836
+ item,
4837
+ index: i + 1
4838
+ } };
4839
+ pathPrefix = `${prefix}${loop.id}[${i + 1}]/`;
4840
+ const key = priorItems && i < priorItems.length ? itemKey(item) : void 0;
4841
+ replaying = key !== void 0 && key === itemKey(priorItems[i]);
4842
+ try {
4843
+ decideOutgoing(loop.id, LOOP_EACH_PORT, LOOP_EACH_PORT);
4844
+ await walk(owned);
4845
+ } finally {
4846
+ pathPrefix = prefix;
4847
+ replaying = outerReplaying;
4848
+ }
4849
+ const outputs = {};
4850
+ for (const step of bodySteps) {
4851
+ const entry = scope.steps[step.key];
4852
+ if (entry && "output" in entry) outputs[step.key] = entry.output;
4853
+ for (const value of stepSecrets.get(step.id) ?? []) bodySecrets.add(value);
4854
+ }
4855
+ const iteration = {
4856
+ index: i + 1,
4857
+ item,
4858
+ steps: outputs
4859
+ };
4860
+ const bytes = jsonBytes(iteration) + 1;
4861
+ if (!iterationsTruncated && iterationBytes + bytes <= LOOP_ROW_MAX_BYTES) {
4862
+ iterations.push(iteration);
4863
+ iterationBytes += bytes;
4864
+ } else iterationsTruncated = true;
4865
+ if (runFailed || runCancelled || runWaiting) {
4866
+ interrupted = `Item ${i + 1}: ${runFailed ?? runCancelled ?? "suspended"}`;
4867
+ break;
4868
+ }
4869
+ if (runStopped) break;
4870
+ }
4871
+ if (runCancelled && interrupted === void 0) interrupted = runCancelled;
4872
+ if (bodySecrets.size > 0) stepSecrets.set(loop.id, [...bodySecrets]);
4873
+ const values = [...runSecrets, ...bodySecrets];
4874
+ for (const step of bodySteps) if (!records.has(step.id)) skipStep(step);
4875
+ const output = {
4876
+ item: last,
4877
+ index: items.length,
4878
+ count: items.length,
4879
+ iterations,
4880
+ ...iterationsTruncated ? { iterationsTruncated: true } : {}
4881
+ };
4882
+ const record = {
4883
+ stepId: loop.id,
4884
+ key: loop.key,
4885
+ pieceName: loop.pieceName,
4886
+ blockName: loop.actionName,
4887
+ status: interrupted === void 0 ? "SUCCEEDED" : "FAILED",
4888
+ input: journaled(input, runSecrets),
4889
+ ...interrupted === void 0 ? {
4890
+ output: journaled(referenceDocuments(output), values),
4891
+ port: "next"
4892
+ } : { error: interrupted },
4893
+ startedAt,
4894
+ endedAt: (/* @__PURE__ */ new Date()).toISOString(),
4895
+ configHash: configHashOf(loop)
4896
+ };
4897
+ records.set(loop.id, record);
4898
+ await journal(record);
4899
+ if (prefix === "") for (const step of bodySteps) {
4900
+ const settled = records.get(step.id);
4901
+ if (settled && settled.status !== "SKIPPED") await journal(settled);
4902
+ }
4903
+ if (interrupted !== void 0) return;
4904
+ scope.steps[loop.key] = { output };
4905
+ if (!runStopped) decideOutgoing(loop.id, "next");
4906
+ }
4907
+ if (loops.violation) runFailed = loops.violation;
4908
+ else await walk(ownedSteps(void 0));
4495
4909
  if (runWaiting && !runFailed && !runCancelled) {
4496
4910
  const warnings = options.declaredPorts ? deadPortWarnings(definition, options.declaredPorts) : [];
4497
4911
  return {
@@ -4865,6 +5279,6 @@ function tracedMethods(target, telemetry, prefix, parent, nested = []) {
4865
5279
  } });
4866
5280
  }
4867
5281
  //#endregion
4868
- export { containsRedactedMarker as $, AttachmentCache as A, SecretCreatorMismatchError as B, blockLabel as C, isIndeterminateError as Ct, describeBuiltinPiece as D, builtinPiece as E, shapeConnection as F, secretRefFromId as G, SecretNotFoundError as H, resolveStepInput as I, PieceWorkerPool as J, stepBlock as K, expressionsIn as L, StaticConnectionResolver as M, declaredConnectionIds as N, CORE_PIECE_NAME$1 as O, declaredReactorConnectionIds as P, PieceWorkerTimeoutError as Q, unavailableValue as R, storeHandlers as S, setPieceRegistryUrl as St, blockPorts as T, SecretOwnerMismatchError as U, SecretDeletedError as V, parseSecretRef as W, PieceWorkerDisposedError as X, PieceWorker as Y, PieceWorkerError as Z, effectiveRetryPolicy as _, unsupportedTrigger as _t, PieceRegistry as a, isPrivateAddress as at, isHostBound as b, npmRegistryBaseUrl as bt, packagePieces as c, FileTooLargeError as ct, runWorkflow as d, pieceModuleRef as dt, redact as et, canonicalJson as f, sourcedResolver as ft, checkDynamicProperties as g, unsupportedAuth as gt, DynamicPropertiesError as h, authMethodFor as ht, tracedSteps as i, DEFAULT_EGRESS_POLICY as it, BoundConnectionResolver as j, CORE_PIECE_VERSION as k, RUN_DEADLINE_ERROR_NAME as l, maxFileBytes as lt, stepConfigHash as m, UnsupportedPieceFeatureError as mt, WorkflowTelemetry as n, rememberSecrets as nt, installedPiece as o, parseAddress as ot, hashOf as p, buildDescriptor as pt, triggerBlock as q, tracedMethods as r, secretsFor as rt, installedPieces as s, extractDedupeKey as st, TELEMETRY_SCOPE as t, redactMessage as tt, errorNameOf as u, bundleResolver as ut, ActivepiecesBlockExecutor as v, ensurePieceBundle as vt, unpinnedNote as w, resolvedBlock as x, pieceRegistrySource as xt, CompositeBlockExecutor as y, fetchPieceBundle as yt, InMemorySecretProvider as z };
5282
+ export { PieceWorkerPool as $, builtinPiece as A, isIndeterminateError as At, shapeConnection as B, flowsHandlers as C, unsupportedAuth as Ct, blockLabel as D, npmRegistryBaseUrl as Dt, storeHandlers as E, fetchPieceBundle as Et, AttachmentCache as F, SecretCreatorMismatchError as G, expressionsIn as H, BoundConnectionResolver as I, SecretOwnerMismatchError as J, SecretDeletedError as K, StaticConnectionResolver as L, CORE_PIECE_NAME$1 as M, CORE_PIECE_VERSION as N, unpinnedNote as O, pieceRegistrySource as Ot, loopItems as P, triggerBlock as Q, declaredConnectionIds as R, CompositeBlockExecutor as S, authMethodFor as St, resolvedBlock as T, ensurePieceBundle as Tt, unavailableValue as U, resolveStepInput as V, InMemorySecretProvider as W, secretRefFromId as X, parseSecretRef as Y, stepBlock as Z, stepConfigHash as _, bundleResolver as _t, PieceRegistry as a, redact as at, effectiveRetryPolicy as b, buildDescriptor as bt, packagePieces as c, secretsFor as ct, runWorkflow as d, parseAddress as dt, PieceWorker as et, isLoopStep as f, extractDedupeKey as ft, hashOf as g, HOST_ORIGIN as gt, canonicalJson as h, FLOWS_LIST as ht, tracedSteps as i, containsRedactedMarker as it, describeBuiltinPiece as j, blockPorts as k, setPieceRegistryUrl as kt, RUN_DEADLINE_ERROR_NAME as l, DEFAULT_EGRESS_POLICY as lt, loopStructure as m, maxFileBytes as mt, WorkflowTelemetry as n, PieceWorkerError as nt, installedPiece as o, redactMessage as ot, loopConfigHash as p, FileTooLargeError as pt, SecretNotFoundError as q, tracedMethods as r, PieceWorkerTimeoutError as rt, installedPieces as s, rememberSecrets as st, TELEMETRY_SCOPE as t, PieceWorkerDisposedError as tt, errorNameOf as u, isPrivateAddress as ut, DynamicPropertiesError as v, pieceModuleRef as vt, isHostBound as w, unsupportedTrigger as wt, ActivepiecesBlockExecutor as x, UnsupportedPieceFeatureError as xt, checkDynamicProperties as y, sourcedResolver as yt, declaredReactorConnectionIds as z };
4869
5283
 
4870
- //# sourceMappingURL=telemetry-BYDhITFO.js.map
5284
+ //# sourceMappingURL=telemetry-BOS6FOtP.js.map