@powerhousedao/reactor-workflow 6.2.3-dev.87 → 6.2.3-dev.89

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/index.js CHANGED
@@ -1,10 +1,10 @@
1
- import { $ as containsRedactedMarker, A as AttachmentCache, B as SecretCreatorMismatchError, C as blockLabel, Ct as isIndeterminateError, D as describeBuiltinPiece, E as builtinPiece, F as shapeConnection, G as secretRefFromId, H as SecretNotFoundError, I as resolveStepInput, J as PieceWorkerPool, K as stepBlock, L as expressionsIn, N as declaredConnectionIds, O as CORE_PIECE_NAME, P as declaredReactorConnectionIds, Q as PieceWorkerTimeoutError, R as unavailableValue, S as storeHandlers, St as setPieceRegistryUrl, T as blockPorts, U as SecretOwnerMismatchError, V as SecretDeletedError, W as parseSecretRef, X as PieceWorkerDisposedError, Y as PieceWorker, Z as PieceWorkerError, _ as effectiveRetryPolicy, _t as unsupportedTrigger, a as PieceRegistry, at as isPrivateAddress, b as isHostBound, bt as npmRegistryBaseUrl, c as packagePieces, ct as FileTooLargeError, d as runWorkflow, dt as pieceModuleRef, et as redact, f as canonicalJson, ft as sourcedResolver, g as checkDynamicProperties, gt as unsupportedAuth, h as DynamicPropertiesError, ht as authMethodFor, i as tracedSteps, it as DEFAULT_EGRESS_POLICY, j as BoundConnectionResolver, k as CORE_PIECE_VERSION, lt as maxFileBytes, m as stepConfigHash, mt as UnsupportedPieceFeatureError, n as WorkflowTelemetry, nt as rememberSecrets, o as installedPiece, ot as parseAddress, p as hashOf, q as triggerBlock, r as tracedMethods, rt as secretsFor, s as installedPieces, st as extractDedupeKey, t as TELEMETRY_SCOPE, tt as redactMessage, u as errorNameOf, ut as bundleResolver, v as ActivepiecesBlockExecutor, w as unpinnedNote, x as resolvedBlock, xt as pieceRegistrySource, y as CompositeBlockExecutor } from "./telemetry-B5nuWvdf.js";
1
+ import { $ as PieceWorkerPool, A as builtinPiece, At as isIndeterminateError, B as shapeConnection, C as flowsHandlers, Ct as unsupportedAuth, D as blockLabel, Dt as npmRegistryBaseUrl, E as storeHandlers, F as AttachmentCache, G as SecretCreatorMismatchError, H as expressionsIn, I as BoundConnectionResolver, J as SecretOwnerMismatchError, K as SecretDeletedError, M as CORE_PIECE_NAME, N as CORE_PIECE_VERSION, O as unpinnedNote, Ot as pieceRegistrySource, P as loopItems, Q as triggerBlock, R as declaredConnectionIds, S as CompositeBlockExecutor, St as authMethodFor, T as resolvedBlock, U as unavailableValue, V as resolveStepInput, X as secretRefFromId, Y as parseSecretRef, Z as stepBlock, _ as stepConfigHash, _t as bundleResolver, a as PieceRegistry, at as redact, b as effectiveRetryPolicy, c as packagePieces, ct as secretsFor, d as runWorkflow, dt as parseAddress, et as PieceWorker, f as isLoopStep, ft as extractDedupeKey, g as hashOf, gt as HOST_ORIGIN, h as canonicalJson, ht as FLOWS_LIST, i as tracedSteps, it as containsRedactedMarker, j as describeBuiltinPiece, k as blockPorts, kt as setPieceRegistryUrl, lt as DEFAULT_EGRESS_POLICY, m as loopStructure, mt as maxFileBytes, n as WorkflowTelemetry, nt as PieceWorkerError, o as installedPiece, ot as redactMessage, p as loopConfigHash, pt as FileTooLargeError, q as SecretNotFoundError, r as tracedMethods, rt as PieceWorkerTimeoutError, s as installedPieces, st as rememberSecrets, t as TELEMETRY_SCOPE, tt as PieceWorkerDisposedError, u as errorNameOf, ut as isPrivateAddress, v as DynamicPropertiesError, vt as pieceModuleRef, w as isHostBound, wt as unsupportedTrigger, x as ActivepiecesBlockExecutor, xt as UnsupportedPieceFeatureError, y as checkDynamicProperties, yt as sourcedResolver, z as declaredReactorConnectionIds } from "./telemetry-Dg51lywn.js";
2
2
  import { DENIED_ACTION_INDEXES, ReactorAccessDeniedError, ReactorAccessDeniedError as ReactorAccessDeniedError$1, ReactorJobFailedError, ReactorJobPendingError as ReactorJobPendingError$1, WebhookHandshakeStrategy, assertActionsApplied } from "@powerhousedao/pieces-framework";
3
3
  import { mkdirSync, openAsBlob, readFileSync, writeFileSync } from "node:fs";
4
4
  import { open, readFile, readdir, rm, stat } from "node:fs/promises";
5
5
  import { dirname, join } from "node:path";
6
6
  import { childLogger, garbageCollect, generateId, hashDocumentStateForScope, isDenied, sortOperations } from "document-model";
7
- import { DEFAULT_TIMEZONE, DOCUMENT_REF_KEY, ScheduleConfigError, checkTriggerStrategy, coerceManualPayload, containsDocumentRef, documentRefsIn, expandDocumentRefs, isDocumentRefMarker, manualInputTypeName, parseManualInputs, parseScheduleConfig, referenceDocuments } from "@powerhousedao/pieces-framework/workflow";
7
+ import { CALL_FLOW_ACTION as CALL_FLOW_ACTION$1, DEFAULT_TIMEZONE, DOCUMENT_REF_KEY, SUBFLOWS_PIECE as SUBFLOWS_PIECE$1, ScheduleConfigError, checkTriggerStrategy, coerceManualPayload, containsDocumentRef, documentRefsIn, expandDocumentRefs, isCallableFlow, isDocumentRefMarker, manualInputTypeName, parseManualInputs, parseScheduleConfig, referenceDocuments } from "@powerhousedao/pieces-framework/workflow";
8
8
  import { Cron } from "croner";
9
9
  import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes, randomUUID } from "node:crypto";
10
10
  import dns from "node:dns";
@@ -829,8 +829,7 @@ const SERVER_ONLY_PIECES = /* @__PURE__ */ new Set([
829
829
  "@activepieces/piece-utility-ai",
830
830
  "@activepieces/piece-agent",
831
831
  "@activepieces/piece-todos",
832
- "@activepieces/piece-tables",
833
- "@activepieces/piece-subflows"
832
+ "@activepieces/piece-tables"
834
833
  ]);
835
834
  //#endregion
836
835
  //#region src/reactor/piece-catalog.ts
@@ -1564,6 +1563,9 @@ function currentRunUser() {
1564
1563
  function currentWaitpoints() {
1565
1564
  return storage.getStore()?.waitpoints;
1566
1565
  }
1566
+ function currentFlows() {
1567
+ return storage.getStore()?.flows;
1568
+ }
1567
1569
  //#endregion
1568
1570
  //#region src/reactor/piece-store-port.ts
1569
1571
  const logger$5 = childLogger(["workflow", "piece-store"]);
@@ -1760,6 +1762,7 @@ function createBlockExecutor(host, secrets, attachments, pieceStore, resolveBloc
1760
1762
  worker: currentPieceWorker,
1761
1763
  stepTest: currentStepTest,
1762
1764
  waitpoints: currentWaitpoints,
1765
+ flows: currentFlows,
1763
1766
  identity: () => ({
1764
1767
  runId: currentRunId(),
1765
1768
  flowId: currentWorkflowId(),
@@ -2708,6 +2711,8 @@ const MANUAL_BLOCK = trigger("manual");
2708
2711
  const SCHEDULE_BLOCK = trigger("schedule");
2709
2712
  const WEBHOOK_BLOCK = trigger("webhook");
2710
2713
  const BRANCH_BLOCK = action("branch");
2714
+ const ROUTER_BLOCK = action("router");
2715
+ const LOOP_BLOCK = action("loop");
2711
2716
  const ASSERT_BLOCK = action("assert");
2712
2717
  const DELAY_BLOCK = action("delay");
2713
2718
  const APPROVAL_BLOCK = action("approval");
@@ -7729,6 +7734,7 @@ function triggerBlockColumns(block) {
7729
7734
  }
7730
7735
  const FIRED_WITHOUT_RUN_ID = "fired-without-run";
7731
7736
  const DEDUPE_CLAIM_MIGRATION = "trigger_dedupe.run_id-null-is-unclaimed";
7737
+ const STEP_PATH_MIGRATION = "step_execution.step_path";
7732
7738
  function abandonedAdmissionError(attempts) {
7733
7739
  return `This run was admitted at ${attempts} boots and the reactor stopped each time before it started, so it is no longer admitted on boot. Fix the cause, then rerun it to fire the workflow with the same payload.`;
7734
7740
  }
@@ -7833,7 +7839,9 @@ async function up(db) {
7833
7839
  for (const column of ["started_at", "ended_at"]) try {
7834
7840
  await db.schema.alterTable("step_execution").addColumn(column, "text").execute();
7835
7841
  } catch {}
7836
- try {
7842
+ await db.schema.createTable("migration_mark").addColumn("name", "text", (col) => col.primaryKey()).addColumn("applied_at", "text", (col) => col.notNull()).ifNotExists().execute();
7843
+ const stepPathMigrated = await db.selectFrom("migration_mark").select("name").where("name", "=", STEP_PATH_MIGRATION).executeTakeFirst();
7844
+ if (!stepPathMigrated) try {
7837
7845
  await db.schema.alterTable("step_execution").addUniqueConstraint("step_execution_run_step", ["run_id", "step_id"]).execute();
7838
7846
  } catch (error) {
7839
7847
  if (!isDuplicateObject(error)) throw new Error(`Could not add the step_execution (run_id, step_id) unique constraint, which per-step journaling upserts against: ${errorMessage(error)}`, { cause: error });
@@ -7850,7 +7858,7 @@ async function up(db) {
7850
7858
  try {
7851
7859
  await db.schema.alterTable("trigger_dedupe").addColumn("attempts", "integer", (col) => col.notNull().defaultTo(1)).execute();
7852
7860
  } catch {}
7853
- await db.schema.createTable("migration_mark").addColumn("name", "text", (col) => col.primaryKey()).addColumn("applied_at", "text", (col) => col.notNull()).ifNotExists().execute();
7861
+ if (!stepPathMigrated) await migrateStepPath(db);
7854
7862
  await db.transaction().execute(async (trx) => {
7855
7863
  if (await trx.selectFrom("migration_mark").select("name").where("name", "=", DEDUPE_CLAIM_MIGRATION).executeTakeFirst()) return;
7856
7864
  await trx.updateTable("trigger_dedupe").set({ run_id: FIRED_WITHOUT_RUN_ID }).where("run_id", "is", null).execute();
@@ -7914,7 +7922,9 @@ async function up(db) {
7914
7922
  ["definition_hash", "text"],
7915
7923
  ["definition_op_index", "integer"],
7916
7924
  ["definition_scope_hash", "text"],
7917
- ["waiting_on", "text"]
7925
+ ["waiting_on", "text"],
7926
+ ["parent_run_id", "text"],
7927
+ ["parent_waitpoint_id", "text"]
7918
7928
  ]) try {
7919
7929
  await db.schema.alterTable("run").addColumn(column, type).execute();
7920
7930
  } catch {}
@@ -7956,6 +7966,21 @@ function legacyBlockIdentity(blockType) {
7956
7966
  name
7957
7967
  };
7958
7968
  }
7969
+ async function migrateStepPath(db) {
7970
+ await db.schema.alterTable("step_execution").addColumn("step_path", "text", (col) => col.ifNotExists()).execute();
7971
+ await db.updateTable("step_execution").set({ step_path: sql.ref("step_id") }).where("step_path", "is", null).execute();
7972
+ await db.schema.alterTable("step_execution").alterColumn("step_path", (col) => col.setNotNull()).execute();
7973
+ try {
7974
+ await db.schema.alterTable("step_execution").addUniqueConstraint("step_execution_run_path", ["run_id", "step_path"]).execute();
7975
+ } catch (error) {
7976
+ if (!isDuplicateObject(error)) throw error;
7977
+ }
7978
+ await db.schema.alterTable("step_execution").dropConstraint("step_execution_run_step").ifExists().execute();
7979
+ await db.insertInto("migration_mark").values({
7980
+ name: STEP_PATH_MIGRATION,
7981
+ applied_at: (/* @__PURE__ */ new Date()).toISOString()
7982
+ }).onConflict((oc) => oc.column("name").doNothing()).execute();
7983
+ }
7959
7984
  async function migrateBlockType(db, table, nameColumn) {
7960
7985
  const legacy = db;
7961
7986
  try {
@@ -8180,6 +8205,7 @@ function stepValues(runId, ordinal, step) {
8180
8205
  run_id: runId,
8181
8206
  ordinal,
8182
8207
  step_id: step.stepId,
8208
+ step_path: step.path ?? step.stepId,
8183
8209
  step_key: step.key,
8184
8210
  piece_name: step.pieceName,
8185
8211
  block_name: step.blockName,
@@ -8271,6 +8297,7 @@ const STEP_COLUMNS_WITHOUT_DATA = [
8271
8297
  "run_id",
8272
8298
  "ordinal",
8273
8299
  "step_id",
8300
+ "step_path",
8274
8301
  "step_key",
8275
8302
  "piece_name",
8276
8303
  "block_name",
@@ -8292,6 +8319,7 @@ var WorkflowRunStore = class WorkflowRunStore {
8292
8319
  db;
8293
8320
  unmigrated;
8294
8321
  runsInFlight = /* @__PURE__ */ new Set();
8322
+ orphanedRunIds = [];
8295
8323
  journaledSteps = /* @__PURE__ */ new Map();
8296
8324
  pausingRuns = /* @__PURE__ */ new Set();
8297
8325
  resumedLegs = /* @__PURE__ */ new Set();
@@ -8325,10 +8353,11 @@ var WorkflowRunStore = class WorkflowRunStore {
8325
8353
  const ids = (await query.returning("id").execute()).map((row) => row.id);
8326
8354
  await failWaitingSteps(trx, ids, ORPHANED_RUN_ERROR, null, endedAt);
8327
8355
  await closeLiveWaitpoints(trx, ids, "CANCELLED", endedAt);
8328
- return ids.length;
8356
+ return ids;
8329
8357
  });
8330
- if (recovered > 0) logger$4.warn(`Recovered ${recovered} workflow run(s) left RUNNING by a stopped reactor; they are now FAILED and rerunnable`);
8331
- return recovered;
8358
+ this.orphanedRunIds = recovered;
8359
+ if (recovered.length > 0) logger$4.warn(`Recovered ${recovered.length} workflow run(s) left RUNNING by a stopped reactor; they are now FAILED and rerunnable`);
8360
+ return recovered.length;
8332
8361
  }
8333
8362
  async admitAbandonedRuns(options = {}) {
8334
8363
  const admitted = await this.db.transaction().execute(async (trx) => {
@@ -8610,9 +8639,21 @@ var WorkflowRunStore = class WorkflowRunStore {
8610
8639
  ended_at: null,
8611
8640
  rerun_of: null,
8612
8641
  warnings: 0,
8613
- warning_notes: null
8642
+ warning_notes: null,
8643
+ ...options.parent ? {
8644
+ parent_run_id: options.parent.runId,
8645
+ parent_waitpoint_id: options.parent.waitpointId
8646
+ } : {}
8614
8647
  }).execute();
8615
8648
  }
8649
+ async getRunParent(runId) {
8650
+ const row = await this.db.selectFrom("run").select(["parent_run_id", "parent_waitpoint_id"]).where("id", "=", runId).executeTakeFirst();
8651
+ if (!row?.parent_run_id) return void 0;
8652
+ return {
8653
+ runId: row.parent_run_id,
8654
+ waitpointId: row.parent_waitpoint_id ?? null
8655
+ };
8656
+ }
8616
8657
  async beginRun(runId, details) {
8617
8658
  this.runsInFlight.add(runId);
8618
8659
  this.journaledSteps.set(runId, /* @__PURE__ */ new Map());
@@ -8652,12 +8693,12 @@ var WorkflowRunStore = class WorkflowRunStore {
8652
8693
  async recordStep(runId, ordinal, step) {
8653
8694
  if (erasedRuns.has(runId)) return;
8654
8695
  const values = stepValues(runId, ordinal, step);
8655
- const { run_id: _run, step_id: _step, ...mutable } = values;
8696
+ const { run_id: _run, step_id: _step, step_path: _path, ...mutable } = values;
8656
8697
  if (!await this.withRunRow(runId, (trx) => trx.insertInto("step_execution").values({
8657
8698
  id: randomUUID(),
8658
8699
  ...values
8659
- }).onConflict((oc) => oc.columns(["run_id", "step_id"]).doUpdateSet(mutable)).execute())) return;
8660
- this.journaledSteps.get(runId)?.set(step.stepId, {
8700
+ }).onConflict((oc) => oc.columns(["run_id", "step_path"]).doUpdateSet(mutable)).execute())) return;
8701
+ this.journaledSteps.get(runId)?.set(values.step_path, {
8661
8702
  ordinal,
8662
8703
  hash: rowHash(values)
8663
8704
  });
@@ -8728,7 +8769,7 @@ var WorkflowRunStore = class WorkflowRunStore {
8728
8769
  return waiting || paused ? this.db.transaction().execute(close) : close(this.db);
8729
8770
  }
8730
8771
  async sweepSteps(runId, result, executionOrder) {
8731
- const journaled = await this.db.selectFrom("step_execution").select(["step_id", "ordinal"]).where("run_id", "=", runId).execute();
8772
+ const journaled = await this.db.selectFrom("step_execution").select(["step_id", "ordinal"]).where("run_id", "=", runId).whereRef("step_path", "=", "step_id").execute();
8732
8773
  const ordinals = new Map(journaled.map((row) => [row.step_id, row.ordinal]));
8733
8774
  let nextOrdinal = journaled.reduce((max, row) => Math.max(max, row.ordinal + 1), 0);
8734
8775
  for (const step of result.steps) {
@@ -8741,7 +8782,7 @@ var WorkflowRunStore = class WorkflowRunStore {
8741
8782
  await this.withRunRow(runId, (trx) => trx.insertInto("step_execution").values(result.steps.map((step) => ({
8742
8783
  id: randomUUID(),
8743
8784
  ...stepValues(runId, ordinalFor(step), step)
8744
- }))).onConflict((oc) => oc.columns(["run_id", "step_id"]).doUpdateSet((eb) => ({
8785
+ }))).onConflict((oc) => oc.columns(["run_id", "step_path"]).doUpdateSet((eb) => ({
8745
8786
  ordinal: eb.ref("excluded.ordinal"),
8746
8787
  step_key: eb.ref("excluded.step_key"),
8747
8788
  piece_name: eb.ref("excluded.piece_name"),
@@ -8782,13 +8823,22 @@ var WorkflowRunStore = class WorkflowRunStore {
8782
8823
  async getRun(id) {
8783
8824
  return this.db.selectFrom("run").selectAll().where("id", "=", id).executeTakeFirst();
8784
8825
  }
8785
- async getSteps(runId) {
8786
- return this.db.selectFrom("step_execution").selectAll().where("run_id", "=", runId).orderBy("ordinal", "asc").execute();
8826
+ async getSteps(runId, options = {}) {
8827
+ const base = this.db.selectFrom("step_execution").selectAll().where("run_id", "=", runId).orderBy("ordinal", "asc");
8828
+ const own = base.whereRef("step_path", "=", "step_id");
8829
+ if (!options.withIterations) return own.execute();
8830
+ if (options.iterationLimit === void 0) return base.execute();
8831
+ const [steps, iterations] = await Promise.all([own.execute(), base.whereRef("step_path", "!=", "step_id").limit(options.iterationLimit).execute()]);
8832
+ return [...steps, ...iterations].sort((a, b) => a.ordinal - b.ordinal);
8833
+ }
8834
+ async countIterations(runId) {
8835
+ const row = await this.db.selectFrom("step_execution").select((eb) => eb.fn.countAll().as("count")).where("run_id", "=", runId).whereRef("step_path", "!=", "step_id").executeTakeFirst();
8836
+ return Number(row?.count ?? 0);
8787
8837
  }
8788
8838
  async getStepsForRuns(runIds, options = {}) {
8789
8839
  const byRun = new Map(runIds.map((id) => [id, []]));
8790
8840
  if (runIds.length === 0) return byRun;
8791
- const base = this.db.selectFrom("step_execution").where("run_id", "in", [...new Set(runIds)]).orderBy("run_id").orderBy("ordinal", "asc");
8841
+ const base = this.db.selectFrom("step_execution").where("run_id", "in", [...new Set(runIds)]).whereRef("step_path", "=", "step_id").orderBy("run_id").orderBy("ordinal", "asc");
8792
8842
  const rows = options.withData === false ? (await base.select(STEP_COLUMNS_WITHOUT_DATA).execute()).map((row) => ({
8793
8843
  ...row,
8794
8844
  input: null,
@@ -9259,6 +9309,77 @@ var ParkState = class {
9259
9309
  }
9260
9310
  };
9261
9311
  //#endregion
9312
+ //#region src/reactor/subflows.ts
9313
+ const PARENT_RUN_HEADER = "ap-parent-run-id";
9314
+ const WAITPOINTS_PATH = "/v1/waitpoints/";
9315
+ const HOST_WAITPOINTS_URL = `${HOST_ORIGIN}/v1/waitpoints`;
9316
+ function hostRoute(request) {
9317
+ if (request.method.toUpperCase() !== "POST") return void 0;
9318
+ const { pathname } = new URL(request.path, HOST_ORIGIN);
9319
+ const [version, kind, id, ...rest] = pathname.split("/").filter(Boolean).map(decodeURIComponent);
9320
+ if (version !== "v1" || !id || rest.length > 0) return void 0;
9321
+ if (kind === "webhooks") return {
9322
+ kind: "webhook",
9323
+ workflowId: id
9324
+ };
9325
+ if (kind === "waitpoints") return {
9326
+ kind: "callback",
9327
+ token: id
9328
+ };
9329
+ }
9330
+ function hostBody(request) {
9331
+ if (request.body === void 0 || request.body === "") return void 0;
9332
+ if (!(request.headers["content-type"] ?? "").includes("json")) return request.body;
9333
+ try {
9334
+ return JSON.parse(request.body);
9335
+ } catch {
9336
+ return request.body;
9337
+ }
9338
+ }
9339
+ function jsonReply(status, body) {
9340
+ return {
9341
+ status,
9342
+ headers: { "content-type": "application/json" },
9343
+ body: JSON.stringify(body)
9344
+ };
9345
+ }
9346
+ function localCallback(callbackUrl, resumeBaseUrl) {
9347
+ const base = callbackUrl.startsWith(`http://reactor.invalid${WAITPOINTS_PATH}`) ? `${HOST_ORIGIN}${WAITPOINTS_PATH}` : resumeBaseUrl ? `${resumeBaseUrl.replace(/\/+$/, "")}/` : void 0;
9348
+ if (!base || !callbackUrl.startsWith(base)) return void 0;
9349
+ const token = callbackUrl.slice(base.length).split(/[/?#]/)[0];
9350
+ if (!token) return void 0;
9351
+ return {
9352
+ url: `${HOST_ORIGIN}${WAITPOINTS_PATH}${token}`,
9353
+ token
9354
+ };
9355
+ }
9356
+ function populatedFlow(workflowId, state, documentName) {
9357
+ const runnable = runnableDefinition(state);
9358
+ const trigger = runnable.trigger;
9359
+ const config = trigger?.config;
9360
+ return {
9361
+ id: workflowId,
9362
+ externalId: workflowId,
9363
+ status: state.status === "ENABLED" && runnable.published ? "ENABLED" : "DISABLED",
9364
+ version: {
9365
+ id: `${workflowId}@${runnable.version}`,
9366
+ displayName: state.name || documentName || workflowId,
9367
+ trigger: trigger ? {
9368
+ type: "PIECE_TRIGGER",
9369
+ settings: {
9370
+ pieceName: trigger.pieceName,
9371
+ pieceVersion: trigger.pieceVersion,
9372
+ triggerName: trigger.triggerName,
9373
+ input: config && typeof config === "object" && !Array.isArray(config) ? config : {}
9374
+ }
9375
+ } : {
9376
+ type: "EMPTY",
9377
+ settings: { input: {} }
9378
+ }
9379
+ }
9380
+ };
9381
+ }
9382
+ //#endregion
9262
9383
  //#region src/reactor/run-cursor.ts
9263
9384
  var InvalidRunCursorError = class extends Error {
9264
9385
  constructor() {
@@ -9877,6 +9998,8 @@ var TriggerSupervisor = class {
9877
9998
  if (!store) return false;
9878
9999
  const row = await store.getTriggerState(workflowId);
9879
10000
  if (!row || row.status !== "ENABLED") return false;
10001
+ const binding = this.bindings.get(workflowId);
10002
+ if (binding && !isSchedule(binding) && isCallableFlow(binding)) return false;
9880
10003
  await store.upsertTriggerState({
9881
10004
  ...row,
9882
10005
  next_poll_at: this.now().toISOString()
@@ -9985,12 +10108,12 @@ var TriggerSupervisor = class {
9985
10108
  handshake(binding, payload) {
9986
10109
  return this.enqueue(binding.workflowId, () => this.hook(binding, "onHandshake", { payload }));
9987
10110
  }
9988
- deliverWebhook(workflowId, payload) {
10111
+ deliverWebhook(workflowId, payload, parent) {
9989
10112
  return this.enqueue(workflowId, async () => {
9990
10113
  const binding = this.bindings.get(workflowId);
9991
10114
  if (!binding || isSchedule(binding)) {
9992
10115
  logger$2.warn(`Webhook delivery for unknown workflow ${workflowId}`);
9993
- return;
10116
+ return [];
9994
10117
  }
9995
10118
  const store = await this.store();
9996
10119
  if (!store) throw new MissingJournalError("Webhook delivery");
@@ -10009,13 +10132,23 @@ var TriggerSupervisor = class {
10009
10132
  storeState: VESTIGIAL_STORE_STATE,
10010
10133
  nextPollAtIso: new Date(now.getTime() + row.interval_ms).toISOString()
10011
10134
  } : void 0;
10012
- await this.fireItems(store, binding, result.output, now, rewind, success);
10135
+ return await this.fireItems(store, binding, result.output, now, rewind, success, parent);
10013
10136
  } catch (error) {
10014
10137
  if (!refused) await rewind();
10015
10138
  throw error;
10016
10139
  }
10017
10140
  });
10018
10141
  }
10142
+ pieceBinding(workflowId) {
10143
+ const binding = this.bindings.get(workflowId);
10144
+ return binding && !isSchedule(binding) ? binding : void 0;
10145
+ }
10146
+ onStart(binding, runId, payload) {
10147
+ return this.hook(binding, "onStart", {
10148
+ payload,
10149
+ runId
10150
+ });
10151
+ }
10019
10152
  async cursorRewind(store, workflowId) {
10020
10153
  const before = await store.listPieceStore("FLOW", workflowId);
10021
10154
  let rewound;
@@ -10044,6 +10177,11 @@ var TriggerSupervisor = class {
10044
10177
  const auth = await this.options.resolveAuth(binding.connectionId, { piecePackage: binding.packageName });
10045
10178
  const redactValues = secretsFor(auth);
10046
10179
  const reactor = await this.reactorFor(binding, hook, options.runUser);
10180
+ const flows = hook === "test" || hook === "onDisable" ? void 0 : this.options.flowsFor?.(binding.workflowId, options.runId);
10181
+ const hostCalls = pieceStore || flows ? {
10182
+ ...pieceStore ? storeHandlers(pieceStore) : {},
10183
+ ...flows ? flowsHandlers(flows) : {}
10184
+ } : void 0;
10047
10185
  this.throwIfStopped();
10048
10186
  return this.worker.runTriggerHook({
10049
10187
  ...pieceModuleRef(piece),
@@ -10055,15 +10193,17 @@ var TriggerSupervisor = class {
10055
10193
  ...pieceStore ? { durableStore: true } : {},
10056
10194
  identity: {
10057
10195
  flowId: binding.workflowId,
10058
- projectId: PROJECT_SCOPE_KEY
10196
+ projectId: PROJECT_SCOPE_KEY,
10197
+ ...options.runId ? { runId: options.runId } : {}
10059
10198
  },
10060
10199
  isRepublish: options.isRepublish,
10061
10200
  payload: options.payload,
10062
10201
  webhookUrl: options.webhookUrl ?? `http://localhost:0/v1/webhooks/${binding.workflowId}`,
10063
- ...this.egress ? { egress: this.egress } : {}
10202
+ ...this.egress ? { egress: this.egress } : {},
10203
+ ...flows ? { flows: flows.publicUrl ? { publicUrl: flows.publicUrl } : {} } : {}
10064
10204
  }, {
10065
10205
  timeoutMs: this.hookTimeoutMs,
10066
- ...pieceStore ? { hostCalls: storeHandlers(pieceStore) } : {},
10206
+ ...hostCalls ? { hostCalls } : {},
10067
10207
  ...reactor ? { reactor } : {}
10068
10208
  });
10069
10209
  }
@@ -10138,7 +10278,8 @@ var TriggerSupervisor = class {
10138
10278
  let reachedProvider = false;
10139
10279
  try {
10140
10280
  const runtime = await this.deliveryFor(binding);
10141
- const webhook = runtime.delivery === "webhook";
10281
+ const inProcess = isCallableFlow(binding);
10282
+ const webhook = runtime.delivery === "webhook" && !inProcess;
10142
10283
  const webhookUrl = webhook ? await this.webhookUrlOrThrow(binding.workflowId) : void 0;
10143
10284
  if (webhook && !webhookUrl) throw new Error("This trigger delivers by webhook, but no public webhook endpoint is configured for the reactor");
10144
10285
  reachedProvider = true;
@@ -10156,7 +10297,7 @@ var TriggerSupervisor = class {
10156
10297
  status: await this.armedStatus(store, binding.workflowId),
10157
10298
  store_state: VESTIGIAL_STORE_STATE,
10158
10299
  interval_ms: intervalMs,
10159
- next_poll_at: new Date(now.getTime() + intervalMs).toISOString(),
10300
+ next_poll_at: inProcess ? null : new Date(now.getTime() + intervalMs).toISOString(),
10160
10301
  last_poll_at: null,
10161
10302
  last_error: null,
10162
10303
  consecutive_failures: 0,
@@ -10502,10 +10643,10 @@ var TriggerSupervisor = class {
10502
10643
  logger$2.warn(`Poll failed for workflow ${row.workflow_id} (${failures}x): @error`, message);
10503
10644
  }
10504
10645
  }
10505
- async fireItems(store, binding, items, now, rewind, success) {
10646
+ async fireItems(store, binding, items, now, rewind, success, parent) {
10506
10647
  if (items.length === 0) {
10507
10648
  if (success) await store.recordPollSuccess(success.workflowId, success.storeState, now.toISOString(), success.nextPollAtIso);
10508
- return;
10649
+ return [];
10509
10650
  }
10510
10651
  const kind = pieceTriggerKind(binding.block);
10511
10652
  const journaled = (await store.journalTriggerItems(items.map((item) => ({
@@ -10513,7 +10654,8 @@ var TriggerSupervisor = class {
10513
10654
  options: {
10514
10655
  workflowId: binding.workflowId,
10515
10656
  triggerKind: kind,
10516
- triggerPayload: item
10657
+ triggerPayload: item,
10658
+ ...parent ? { parent } : {}
10517
10659
  }
10518
10660
  })), DEDUPE_TTL_MS, now.toISOString(), success)).flatMap((runId, index) => runId ? [{
10519
10661
  runId,
@@ -10525,6 +10667,7 @@ var TriggerSupervisor = class {
10525
10667
  throw error;
10526
10668
  }
10527
10669
  for (const { runId, item } of journaled) this.fire(binding.workflowId, item, kind, runId);
10670
+ return journaled.map(({ runId }) => runId);
10528
10671
  }
10529
10672
  async redeliver(runIds, rewind, error) {
10530
10673
  if (!await rewind(runIds)) await this.failRefused(runIds, error);
@@ -10962,6 +11105,8 @@ function bindingTarget(binding) {
10962
11105
  ...binding.source ? { source: binding.source } : {}
10963
11106
  };
10964
11107
  }
11108
+ /** A journal row with the steps that belong to it, as the subgraph serves it. */
11109
+ const RUN_ITERATIONS_LIMIT = 1e3;
10965
11110
  const RUNS_SCAN_LIMIT = 1e3;
10966
11111
  const WEBHOOK_TEST_TIMEOUT_MS = 3e5;
10967
11112
  const PENDING_SWEEP_INTERVAL_MS = 6e4;
@@ -11293,12 +11438,13 @@ var WorkflowRuntimeService = class {
11293
11438
  openStore() {
11294
11439
  this.storeOpening = true;
11295
11440
  const opening = WorkflowRunStore.create(this.host.relationalDb);
11296
- opening.then(() => {
11441
+ opening.then((store) => {
11297
11442
  this.storeOpening = false;
11298
11443
  if (this.storeError !== void 0) this.logger.info("Workflow run store opened after failing to");
11299
11444
  this.storeError = void 0;
11300
11445
  this.storeReopenMs = STORE_REOPEN_MS;
11301
11446
  this.retentionSweep?.();
11447
+ for (const runId of store.orphanedRunIds) this.endSubflow(runId);
11302
11448
  }, (error) => {
11303
11449
  this.storeOpening = false;
11304
11450
  this.storeError = error;
@@ -12119,6 +12265,7 @@ var WorkflowRuntimeService = class {
12119
12265
  },
12120
12266
  pollHeld: (workflowId) => this.pollHeld(workflowId),
12121
12267
  webhookUrlFor: async (workflowId) => (await this.mintWebhookEndpoint(workflowId))?.url,
12268
+ flowsFor: (workflowId, runId) => this.flowsPort(workflowId, runId ?? null),
12122
12269
  reactorAccess: (binding, requireReactor, runUser) => this.triggerReactorAccess(binding, requireReactor, runUser),
12123
12270
  models: this.models,
12124
12271
  worker: this.workers().session("trigger"),
@@ -12469,6 +12616,7 @@ var WorkflowRuntimeService = class {
12469
12616
  }
12470
12617
  }
12471
12618
  async deliverToPiece(binding, request) {
12619
+ if (isCallableFlow(binding)) return UNAUTHORIZED;
12472
12620
  const probe = await this.pieceHandshake(binding, request);
12473
12621
  if (probe) return probe;
12474
12622
  const payload = webhookPayload(request);
@@ -12603,14 +12751,17 @@ var WorkflowRuntimeService = class {
12603
12751
  }
12604
12752
  async driveWorkflowIds(driveId, ctx) {
12605
12753
  await this.assertCanReadDocument(driveId, ctx);
12754
+ const ids = await this.workflowNodeIds(driveId);
12755
+ return this.readableRows(ids, (id) => id, ctx);
12756
+ }
12757
+ async workflowNodeIds(driveId) {
12606
12758
  let page = await this.host.reactorClient.drives.listNodes(driveId);
12607
12759
  const nodes = [...page.results];
12608
12760
  while (page.next) {
12609
12761
  page = await page.next();
12610
12762
  nodes.push(...page.results);
12611
12763
  }
12612
- const ids = nodes.filter((node) => "documentType" in node && node.documentType === WORKFLOW_DOCUMENT_TYPE).map((node) => node.id);
12613
- return this.readableRows(ids, (id) => id, ctx);
12764
+ return nodes.filter((node) => "documentType" in node && node.documentType === WORKFLOW_DOCUMENT_TYPE).map((node) => node.id);
12614
12765
  }
12615
12766
  async runs(args, ctx) {
12616
12767
  return (await this.runsPage(args, ctx)).records;
@@ -12687,9 +12838,16 @@ var WorkflowRuntimeService = class {
12687
12838
  if (!row) return null;
12688
12839
  if (!await this.canReadDocument(row.workflow_id, ctx)) return null;
12689
12840
  if ((await this.servedRuns([row], ctx)).length === 0) return null;
12841
+ const steps = await store.getSteps(row.id, {
12842
+ withIterations: true,
12843
+ iterationLimit: RUN_ITERATIONS_LIMIT
12844
+ });
12845
+ const shown = steps.filter((step) => step.step_path !== step.step_id);
12846
+ const total = shown.length < 1e3 ? shown.length : await store.countIterations(row.id);
12690
12847
  return {
12691
12848
  row,
12692
- steps: await store.getSteps(row.id)
12849
+ steps,
12850
+ ...total > shown.length ? { iterationsTotal: total } : {}
12693
12851
  };
12694
12852
  }
12695
12853
  async connections(ctx) {
@@ -13114,9 +13272,10 @@ var WorkflowRuntimeService = class {
13114
13272
  if (!ctx) throw new Error("Connection access requires an authenticated request");
13115
13273
  await this.host.assertCanWrite(documentId, ctx);
13116
13274
  }
13117
- async blockOptions(block, propName, input, connectionId, ctx, searchValue, reactorConnectionId) {
13275
+ async blockOptions(block, propName, input, connectionId, ctx, searchValue, reactorConnectionId, editedWorkflowId) {
13118
13276
  if (connectionId) await this.assertCanReadDocument(connectionId, ctx);
13119
13277
  if (reactorConnectionId) await this.assertCanReadDocument(reactorConnectionId, ctx);
13278
+ const workflowId = editedWorkflowId && await this.assertCanReadDocument(editedWorkflowId, ctx).then(() => true, () => false) ? editedWorkflowId : void 0;
13120
13279
  const resolution = await this.resolveBlock(block);
13121
13280
  const parsed = resolvedBlock(resolution);
13122
13281
  if (!parsed) throw new Error(resolution.note ?? `${blockLabel(block)} does not resolve`);
@@ -13135,8 +13294,12 @@ var WorkflowRuntimeService = class {
13135
13294
  ...searchValue !== void 0 ? { searchValue } : {},
13136
13295
  auth,
13137
13296
  projectId: PROJECT_SCOPE_KEY,
13138
- ...this.designEgress ? { egress: this.designEgress } : {}
13139
- }, reactor ? { reactor } : {})).output;
13297
+ ...this.designEgress ? { egress: this.designEgress } : {},
13298
+ ...workflowId ? { flows: true } : {}
13299
+ }, {
13300
+ ...reactor ? { reactor } : {},
13301
+ ...workflowId ? { hostCalls: { [FLOWS_LIST]: (payload) => this.listFlows(workflowId, payload?.externalIds, ctx) } } : {}
13302
+ })).output;
13140
13303
  }
13141
13304
  async blockOutputTree(block, config) {
13142
13305
  const record = config ?? {};
@@ -13177,6 +13340,57 @@ var WorkflowRuntimeService = class {
13177
13340
  }
13178
13341
  ]
13179
13342
  };
13343
+ case ROUTER_BLOCK: return {
13344
+ source: "static",
13345
+ nodes: [{
13346
+ name: "branch",
13347
+ type: "String",
13348
+ description: "The matched branch's name; null for otherwise"
13349
+ }, {
13350
+ name: "index",
13351
+ type: "Int",
13352
+ description: "The matched branch, from 1; null for otherwise"
13353
+ }]
13354
+ };
13355
+ case LOOP_BLOCK: return {
13356
+ source: "static",
13357
+ nodes: [
13358
+ {
13359
+ name: "item",
13360
+ type: "value",
13361
+ description: "The current item; the last one after the loop"
13362
+ },
13363
+ {
13364
+ name: "index",
13365
+ type: "Int!",
13366
+ description: "From 1"
13367
+ },
13368
+ {
13369
+ name: "count",
13370
+ type: "Int",
13371
+ description: "Set after the loop"
13372
+ },
13373
+ {
13374
+ name: "iterations",
13375
+ type: "array",
13376
+ description: "Set after the loop: each item's step outputs",
13377
+ children: [
13378
+ {
13379
+ name: "index",
13380
+ type: "Int!"
13381
+ },
13382
+ {
13383
+ name: "item",
13384
+ type: "value"
13385
+ },
13386
+ {
13387
+ name: "steps",
13388
+ type: "JSONObject!"
13389
+ }
13390
+ ]
13391
+ }
13392
+ ]
13393
+ };
13180
13394
  case ASSERT_BLOCK: return {
13181
13395
  source: "static",
13182
13396
  nodes: [{
@@ -13555,6 +13769,7 @@ var WorkflowRuntimeService = class {
13555
13769
  return await traced;
13556
13770
  } finally {
13557
13771
  this.settleFiring(workflowId);
13772
+ if (enqueuedRunId) this.endSubflow(enqueuedRunId);
13558
13773
  }
13559
13774
  }
13560
13775
  async runFired(run, workflowId, triggerPayload, triggerKind, resume, ctx, enqueuedRunId) {
@@ -13751,6 +13966,7 @@ var WorkflowRuntimeService = class {
13751
13966
  runId = journaled.runId;
13752
13967
  if (runId) run.span.setAttribute("run.id", runId);
13753
13968
  if (this.closed) throw new WorkflowRuntimeClosedError(workflowId);
13969
+ if (runId && !resume) await this.startPieceTrigger(workflowId, runId, triggerPayload);
13754
13970
  session = this.workers().session();
13755
13971
  const { result, handed } = await this.executeLeg({
13756
13972
  workflowId,
@@ -13766,6 +13982,7 @@ var WorkflowRuntimeService = class {
13766
13982
  triggerPayload,
13767
13983
  executor,
13768
13984
  completedSteps: resume?.completedSteps,
13985
+ loopItems: resume?.loopItems,
13769
13986
  replayed: resume?.completedSteps,
13770
13987
  pin: () => ({
13771
13988
  definition_hash: this.definitionHash(workflowId, state),
@@ -13822,7 +14039,8 @@ var WorkflowRuntimeService = class {
13822
14039
  for (const documentId of documentIds) handed.add(documentId);
13823
14040
  if (store && runId) await store.recordRunDocuments(runId, documentIds);
13824
14041
  },
13825
- ...waitpoints ? { waitpoints: waitpoints.port } : {}
14042
+ ...waitpoints ? { waitpoints: waitpoints.port } : {},
14043
+ flows: this.flowsPort(workflowId, runId)
13826
14044
  }, () => runWorkflow({
13827
14045
  definition: {
13828
14046
  ...leg.definition,
@@ -13834,6 +14052,7 @@ var WorkflowRuntimeService = class {
13834
14052
  ...secretValues.length > 0 ? { redactValues: secretValues } : {},
13835
14053
  triggerPayload: leg.triggerPayload,
13836
14054
  completedSteps: leg.completedSteps,
14055
+ ...leg.loopItems ? { loopItems: leg.loopItems } : {},
13837
14056
  ...leg.carriedSteps ? { carriedSteps: leg.carriedSteps } : {},
13838
14057
  ...leg.resume ? { resume: leg.resume } : {},
13839
14058
  ...leg.firstOrdinal !== void 0 ? { firstOrdinal: leg.firstOrdinal } : {},
@@ -13841,7 +14060,7 @@ var WorkflowRuntimeService = class {
13841
14060
  ...policy.defaultRetry ? { defaultRetry: policy.defaultRetry } : {},
13842
14061
  ...leg.deadline !== void 0 ? { deadline: leg.deadline } : {},
13843
14062
  onStep: store && runId ? async (record, ordinal) => {
13844
- executionOrder.set(record.stepId, ordinal);
14063
+ if (!record.path) executionOrder.set(record.stepId, ordinal);
13845
14064
  try {
13846
14065
  await this.telemetry.phase("journal.step", {}, () => store.recordStep(runId, ordinal, record));
13847
14066
  } catch (error) {
@@ -14084,31 +14303,62 @@ var WorkflowRuntimeService = class {
14084
14303
  const triggerPayload = run.trigger_payload === null ? void 0 : JSON.parse(run.trigger_payload);
14085
14304
  if (isTruncatedStepPayload(triggerPayload)) throw new Error(`Trigger payload of run "${runId}" was truncated by the journal and cannot be replayed; fire the workflow again instead of rerunning it`);
14086
14305
  if (containsRedactedMarker(triggerPayload)) throw new Error(`Trigger payload of run "${runId}" was redacted and cannot be replayed; fire the workflow again instead of rerunning it`);
14087
- const document = await this.host.reactorClient.get(run.workflow_id);
14088
- const currentSteps = new Map(runnableDefinition(document.state.global).steps.map((step) => [step.id, stepDefinition(step)]));
14306
+ const runnable = runnableDefinition((await this.host.reactorClient.get(run.workflow_id)).state.global);
14307
+ const currentSteps = new Map(runnable.steps.map((step) => [step.id, stepDefinition(step)]));
14308
+ const current = {
14309
+ steps: [...currentSteps.values()],
14310
+ edges: runnable.edges
14311
+ };
14312
+ const structure = loopStructure(current);
14313
+ const hashes = /* @__PURE__ */ new Map();
14314
+ const hashNow = (step) => {
14315
+ let hash = hashes.get(step.id);
14316
+ if (hash === void 0) {
14317
+ hash = isLoopStep(step) ? loopConfigHash(step, current, structure) : stepConfigHash(step);
14318
+ hashes.set(step.id, hash);
14319
+ }
14320
+ return hash;
14321
+ };
14322
+ const readers = /* @__PURE__ */ new Map();
14323
+ const rereads = (step) => {
14324
+ let reads = readers.get(step.id);
14325
+ if (!reads) readers.set(step.id, reads = this.rereads(step));
14326
+ return reads;
14327
+ };
14089
14328
  const completedSteps = /* @__PURE__ */ new Map();
14090
- for (const row of await store.getSteps(runId)) {
14329
+ const loopItemsByPath = /* @__PURE__ */ new Map();
14330
+ const rows = await store.getSteps(runId, { withIterations: true });
14331
+ for (const row of rows) {
14332
+ const step = currentSteps.get(row.step_id);
14333
+ if (!step || step.key !== row.step_key || row.config_hash === null || hashNow(step) !== row.config_hash) continue;
14334
+ if (isLoopStep(step)) {
14335
+ const input = row.input === null ? void 0 : JSON.parse(row.input);
14336
+ if (input === void 0 || isTruncatedStepPayload(input)) continue;
14337
+ try {
14338
+ loopItemsByPath.set(row.step_path, loopItems(input));
14339
+ } catch {}
14340
+ continue;
14341
+ }
14091
14342
  if (row.status !== "SUCCEEDED" && row.status !== "REPLAYED") continue;
14092
- const current = currentSteps.get(row.step_id);
14093
- if (!current || current.key !== row.step_key || row.config_hash === null || stepConfigHash(current) !== row.config_hash) continue;
14094
14343
  const output = row.output === null ? void 0 : JSON.parse(row.output);
14095
14344
  if (isTruncatedStepPayload(output)) {
14096
- if (await this.rereads(current)) continue;
14097
- completedSteps.set(row.step_id, {
14345
+ if (await rereads(step)) continue;
14346
+ completedSteps.set(row.step_path, {
14098
14347
  port: row.port,
14099
14348
  outputTruncated: true,
14100
14349
  truncatedOutput: output
14101
14350
  });
14102
14351
  continue;
14103
14352
  }
14104
- if (containsDocumentRef(output) && await this.rereads(current)) continue;
14105
- completedSteps.set(row.step_id, {
14353
+ if (containsDocumentRef(output) && await rereads(step)) continue;
14354
+ completedSteps.set(row.step_path, {
14106
14355
  output,
14107
14356
  port: row.port
14108
14357
  });
14109
14358
  }
14110
14359
  return this.fire(run.workflow_id, triggerPayload, "rerun", {
14111
14360
  completedSteps,
14361
+ loopItems: loopItemsByPath,
14112
14362
  rerunOf: runId
14113
14363
  }, ctx);
14114
14364
  }
@@ -14116,6 +14366,151 @@ var WorkflowRuntimeService = class {
14116
14366
  const parsed = resolvedBlock(await this.resolveBlock(stepBlock(step)));
14117
14367
  return parsed ? await this.declaredReactor(parsed) === "read" : false;
14118
14368
  }
14369
+ flowsPort(workflowId, runId) {
14370
+ const base = this.resumeBaseUrl();
14371
+ return {
14372
+ list: (params) => this.listFlows(workflowId, params.externalIds),
14373
+ request: (request) => this.hostRequest(workflowId, runId, request),
14374
+ ...base ? { publicUrl: new URL(base).origin } : {}
14375
+ };
14376
+ }
14377
+ resumeBaseUrl() {
14378
+ const configured = this.host.waitpointBaseUrl?.replace(/\/+$/, "");
14379
+ return configured && URL.canParse(configured) ? configured : void 0;
14380
+ }
14381
+ async drivesOf(workflowId) {
14382
+ try {
14383
+ return (await this.host.reactorClient.getIncomingRelationships(workflowId, CHILD_RELATIONSHIP)).results.filter((parent) => parent.header.documentType === DRIVE_DOCUMENT_TYPE).map((parent) => parent.header.id);
14384
+ } catch (error) {
14385
+ this.logger.debug("Could not read the drives of workflow @workflow: @error", workflowId, error);
14386
+ return [];
14387
+ }
14388
+ }
14389
+ async callableWorkflowIds(workflowId) {
14390
+ const ids = /* @__PURE__ */ new Set([workflowId]);
14391
+ for (const driveId of await this.drivesOf(workflowId)) for (const id of await this.workflowNodeIds(driveId)) ids.add(id);
14392
+ return [...ids];
14393
+ }
14394
+ async callableFlow(id, reads) {
14395
+ try {
14396
+ const document = await this.host.reactorClient.get(id);
14397
+ if (document.header.documentType !== WORKFLOW_DOCUMENT_TYPE) return;
14398
+ const trigger = runnableDefinition(document.state.global).trigger;
14399
+ if (!trigger || !isCallableFlow(trigger) || !await reads(id)) return;
14400
+ return populatedFlow(id, document.state.global, document.header.name);
14401
+ } catch {
14402
+ return;
14403
+ }
14404
+ }
14405
+ /** `ctx.flows.list`, as Activepieces' PopulatedFlow records: the Callable
14406
+ * Flow workflows the caller (else the calling workflow's run user) reads. */
14407
+ async listFlows(workflowId, externalIds, ctx) {
14408
+ const ids = (await this.callableWorkflowIds(workflowId)).filter((id) => !externalIds || externalIds.includes(id));
14409
+ const reads = await this.flowReader(workflowId, ctx);
14410
+ return { data: (await Promise.all(ids.map((id) => this.callableFlow(id, reads)))).filter((flow) => flow !== void 0) };
14411
+ }
14412
+ async flowReader(workflowId, ctx) {
14413
+ if (ctx) return (id) => this.canReadDocument(id, ctx);
14414
+ const runUser = currentRunUser() ?? await publishRunUser(this.host.reactorClient, workflowId, this.host.hostIdentity).catch(() => null);
14415
+ if (!runUser) {
14416
+ const open = !authEnforced(this.host);
14417
+ return () => Promise.resolve(open);
14418
+ }
14419
+ return (id) => this.host.reactorClient.isServed(id, { subject: runUser.subject }).catch(() => false);
14420
+ }
14421
+ async hostRequest(workflowId, runId, request) {
14422
+ const route = hostRoute(request);
14423
+ if (!route) return jsonReply(404, { message: `Nothing is served at ${request.method} ${request.path}` });
14424
+ const body = hostBody(request);
14425
+ if (route.kind === "callback") {
14426
+ const { status } = await this.resumeByToken(route.token, {
14427
+ body,
14428
+ headers: request.headers,
14429
+ queryParams: {}
14430
+ });
14431
+ return jsonReply(200, {
14432
+ status,
14433
+ discarded: status === "gone"
14434
+ });
14435
+ }
14436
+ return this.dispatchSubflow(workflowId, runId, route.workflowId, request.headers, body);
14437
+ }
14438
+ async flowCallableFrom(callerId, targetId) {
14439
+ if (targetId !== callerId) {
14440
+ const [callerDrives, targetDrives] = await Promise.all([this.drivesOf(callerId), this.drivesOf(targetId)]);
14441
+ if (!targetDrives.some((drive) => callerDrives.includes(drive))) return;
14442
+ }
14443
+ return this.callableFlow(targetId, await this.flowReader(callerId, void 0));
14444
+ }
14445
+ async dispatchSubflow(callerId, runId, targetId, headers, body) {
14446
+ const flow = await this.flowCallableFrom(callerId, targetId);
14447
+ if (!flow) return jsonReply(404, { message: `No workflow "${targetId}" is callable from workflow "${callerId}"` });
14448
+ const registration = this.registry.get(targetId);
14449
+ const binding = registration?.kind === "piece-webhook" ? registration.binding : void 0;
14450
+ if (!binding || !isCallableFlow(binding)) return jsonReply(409, { message: `Workflow "${flow.version.displayName}" is not enabled with a Callable Flow trigger` });
14451
+ const claimed = headers[PARENT_RUN_HEADER];
14452
+ if (runId && claimed && claimed !== runId) return jsonReply(403, { message: `Run "${runId}" cannot call on behalf of run "${claimed}"` });
14453
+ const request = body && typeof body === "object" ? body : { data: body };
14454
+ const callback = typeof request.callbackUrl === "string" ? localCallback(request.callbackUrl, this.resumeBaseUrl()) : void 0;
14455
+ let waitpointId = null;
14456
+ if (runId && callback && headers["ap-fail-parent-on-failure"] === "true") {
14457
+ const waitpoint = await (await this.store())?.getWaitpointByTokenHash(tokenHash(callback.token));
14458
+ if (waitpoint?.run_id === runId) waitpointId = waitpoint.id;
14459
+ }
14460
+ const payload = {
14461
+ method: "POST",
14462
+ path: `/v1/webhooks/${targetId}`,
14463
+ headers,
14464
+ queryParams: {},
14465
+ body: {
14466
+ data: request.data ?? null,
14467
+ ...request.callbackUrl !== void 0 ? { callbackUrl: callback?.url ?? request.callbackUrl } : {}
14468
+ }
14469
+ };
14470
+ try {
14471
+ return jsonReply(200, { runId: (await this.supervisor().deliverWebhook(targetId, payload, runId ? {
14472
+ runId,
14473
+ waitpointId
14474
+ } : void 0)).at(0) ?? null });
14475
+ } catch (error) {
14476
+ return jsonReply(500, { message: error instanceof Error ? error.message : String(error) });
14477
+ }
14478
+ }
14479
+ async startPieceTrigger(workflowId, runId, payload) {
14480
+ const registration = this.registry.get(workflowId);
14481
+ const binding = registration?.kind === "piece-webhook" ? registration.binding : registration?.kind === "piece" ? this.supervisor().pieceBinding(workflowId) : void 0;
14482
+ if (!binding) return;
14483
+ if (!(await this.pieceDescriptor(bindingTarget(binding))).triggers.find((entry) => entry.name === binding.triggerName)?.onStart) return;
14484
+ await this.supervisor().onStart(binding, runId, payload);
14485
+ }
14486
+ async endSubflow(runId) {
14487
+ try {
14488
+ const store = await this.store();
14489
+ if (!store) return;
14490
+ const parent = await store.getRunParent(runId);
14491
+ if (!parent) return;
14492
+ const run = await store.getRun(runId);
14493
+ if (!run) return;
14494
+ const failed = FAILED_SUBFLOW_STATUSES.has(run.status);
14495
+ if (!failed && run.status !== "SUCCEEDED") return;
14496
+ await store.deletePieceStoreValue("FLOW", run.workflow_id, `${CALLABLE_FLOW_KEY_PREFIX}${runId}`);
14497
+ if (!parent.waitpointId || !failed) return;
14498
+ const completed = await store.completeWaitpoint(parent.waitpointId, { payload: {
14499
+ body: {
14500
+ status: "error",
14501
+ data: {
14502
+ message: run.error ?? `The called workflow's run ended ${run.status}`,
14503
+ runId
14504
+ }
14505
+ },
14506
+ headers: {},
14507
+ queryParams: {}
14508
+ } });
14509
+ if (completed) await this.deliverHeld(completed.run_id);
14510
+ } catch (error) {
14511
+ this.logger.warn("Could not fail the caller of run @run: @error", runId, error);
14512
+ }
14513
+ }
14119
14514
  resuming = /* @__PURE__ */ new Set();
14120
14515
  definitionHashes = /* @__PURE__ */ new Map();
14121
14516
  /** Our hash of the runnable definition: computed once per published version. */
@@ -14133,13 +14528,13 @@ var WorkflowRuntimeService = class {
14133
14528
  }
14134
14529
  waitpointPort(store, workflowId, runId, suspendMs) {
14135
14530
  const responses = /* @__PURE__ */ new Map();
14136
- const tokens = /* @__PURE__ */ new Map();
14137
- const configured = this.host.waitpointBaseUrl?.replace(/\/+$/, "");
14138
- const baseUrl = configured && URL.canParse(configured) ? configured : void 0;
14531
+ const urls = /* @__PURE__ */ new Map();
14532
+ const resumeBaseUrl = this.resumeBaseUrl();
14139
14533
  return {
14140
14534
  port: {
14141
- ...baseUrl ? { resumeBaseUrl: baseUrl } : {},
14535
+ ...resumeBaseUrl ? { resumeBaseUrl } : {},
14142
14536
  create: async (request) => {
14537
+ const baseUrl = request.pieceName === SUBFLOWS_PIECE$1 && request.blockName === CALL_FLOW_ACTION$1 ? HOST_WAITPOINTS_URL : resumeBaseUrl;
14143
14538
  if (!baseUrl && request.type === "WEBHOOK" && !request.approval) throw new Error("This host serves no resume URLs: resume URLs need an HTTP host");
14144
14539
  const createdAt = /* @__PURE__ */ new Date();
14145
14540
  const resumeAt = request.resumeDateTime ? new Date(Date.parse(request.resumeDateTime)).toISOString() : null;
@@ -14173,20 +14568,21 @@ var WorkflowRuntimeService = class {
14173
14568
  response_to_send: request.responseToSend ? JSON.stringify(request.responseToSend) : null
14174
14569
  });
14175
14570
  if (!inserted) {
14176
- const known = tokens.get(waitpoint.id);
14571
+ const known = urls.get(waitpoint.id);
14177
14572
  if (waitpoint.token_hash !== null && known === void 0) throw new Error(`Step "${request.stepKey}" has a waitpoint from an earlier leg, whose resume URL cannot be issued again`);
14178
14573
  return {
14179
14574
  id: waitpoint.id,
14180
- resumeUrl: known ? `${baseUrl}/${known}` : ""
14575
+ resumeUrl: known ?? ""
14181
14576
  };
14182
14577
  }
14183
- if (token) tokens.set(id, token);
14578
+ const resumeUrl = token ? `${baseUrl}/${token}` : "";
14579
+ if (token) urls.set(id, resumeUrl);
14184
14580
  if (request.responseToSend) responses.set(id, request.responseToSend);
14185
14581
  const due = request.type === "DELAY" ? resumeAt : expiresAt;
14186
14582
  if (due) this.waitpointPoller.nudge(due);
14187
14583
  return {
14188
14584
  id,
14189
- resumeUrl: token ? `${baseUrl}/${token}` : ""
14585
+ resumeUrl
14190
14586
  };
14191
14587
  }
14192
14588
  },
@@ -14221,10 +14617,13 @@ var WorkflowRuntimeService = class {
14221
14617
  waitpointPoller = new WaitpointPoller({
14222
14618
  store: () => this.store(),
14223
14619
  resume: (runId) => this.deliverHeld(runId),
14224
- ended: (end) => Promise.resolve(this.answerClosed(end.closed, end.runId, {
14225
- status: CANCELLED_RUN_STATUS,
14226
- error: end.reason
14227
- })),
14620
+ ended: (end) => {
14621
+ this.answerClosed(end.closed, end.runId, {
14622
+ status: CANCELLED_RUN_STATUS,
14623
+ error: end.reason
14624
+ });
14625
+ return this.endSubflow(end.runId);
14626
+ },
14228
14627
  logger: { warn: (message, ...args) => this.logger.warn(message, ...args) }
14229
14628
  });
14230
14629
  unobserveSuspended;
@@ -14291,6 +14690,7 @@ var WorkflowRuntimeService = class {
14291
14690
  let status = "REFUSED";
14292
14691
  try {
14293
14692
  const result = await this.runResumed(runId, workflowId);
14693
+ this.endSubflow(runId);
14294
14694
  if (result) status = result.status;
14295
14695
  if (status === "FAILED") span.setStatus({
14296
14696
  code: SpanStatusCode.ERROR,
@@ -14767,6 +15167,7 @@ var WorkflowRuntimeService = class {
14767
15167
  const reason = by ? `Cancelled by ${by}` : "Cancelled";
14768
15168
  if (!await store.cancelIdleRun(runId, reason)) throw refuse((await store.getRun(runId))?.status ?? "gone");
14769
15169
  await this.answerHeldResumes(store, runId, reason);
15170
+ await this.endSubflow(runId);
14770
15171
  const row = await store.getRun(runId);
14771
15172
  this.queuedReplies.get(runId)?.({
14772
15173
  status: row.status,
@@ -15098,6 +15499,12 @@ var WorkflowRuntimeService = class {
15098
15499
  function scopeHash(state) {
15099
15500
  return hashDocumentStateForScope({ state: { global: state } }, "global");
15100
15501
  }
15502
+ const CALLABLE_FLOW_KEY_PREFIX = "callableFlow_";
15503
+ const FAILED_SUBFLOW_STATUSES = /* @__PURE__ */ new Set([
15504
+ "FAILED",
15505
+ "CANCELLED",
15506
+ "INDETERMINATE"
15507
+ ]);
15101
15508
  function tokenHash(token) {
15102
15509
  return createHash("sha256").update(token).digest("hex");
15103
15510
  }