@powerhousedao/reactor-workflow 6.2.3-dev.84 → 6.2.3-dev.85

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,13 +7,13 @@ 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, TRIGGER_PORTS, checkTriggerStrategy, referenceDocuments, undeclaredPortEdges } from "@powerhousedao/pieces-framework/workflow";
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";
11
11
  import { Cron } from "croner";
12
- import { createHash, randomUUID } from "node:crypto";
12
+ import { createHash, randomBytes, randomUUID } from "node:crypto";
13
13
  import { ssrfIpClassifier } from "@powerhousedao/pieces-framework/host";
14
14
  import dns from "node:dns";
15
15
  import net, { isIP } from "node:net";
16
- import { AsyncLocalStorage } from "node:async_hooks";
16
+ import { AsyncLocalStorage, AsyncResource } from "node:async_hooks";
17
17
  import os, { tmpdir, userInfo } from "node:os";
18
18
  import { blockKey, isExactVersion } from "@powerhousedao/pieces-framework/block-type";
19
19
  import { SpanStatusCode, context, metrics, trace } from "@opentelemetry/api";
@@ -785,6 +785,8 @@ const OUTPUT_UPDATE = "output.update";
785
785
  const STORE_GET = "store.get";
786
786
  const STORE_PUT = "store.put";
787
787
  const STORE_DELETE = "store.delete";
788
+ const FILES_UPLOAD = "files.upload";
789
+ const WAITPOINT_CREATE = "waitpoint.create";
788
790
  //#endregion
789
791
  //#region src/pieces/activepieces/worker/host-call.ts
790
792
  const DEFAULT_HOST_CALL_TIMEOUT_MS = 1e4;
@@ -1072,11 +1074,11 @@ function createIpcTransport(worker, requestId) {
1072
1074
  });
1073
1075
  },
1074
1076
  onMessage(listener) {
1075
- const handler = (value) => {
1077
+ const handler = AsyncResource.bind((value) => {
1076
1078
  if (closed || !isReactorRpcEnvelope(value, requestId)) return;
1077
1079
  if (!isRpcMessage(value.message)) return;
1078
1080
  listener(value.message);
1079
- };
1081
+ });
1080
1082
  worker.on("message", handler);
1081
1083
  const detach = () => {
1082
1084
  worker.off("message", handler);
@@ -1411,6 +1413,9 @@ var PieceWorker = class {
1411
1413
  this.noteReply(response);
1412
1414
  if (response.type === "result") resolve({
1413
1415
  output: response.output,
1416
+ ...response.paused ? { paused: response.paused } : {},
1417
+ ...response.response ? { response: response.response } : {},
1418
+ ...response.stopped ? { stopped: true } : {},
1414
1419
  touched: response.touched,
1415
1420
  tlsPoisoned: response.tlsPoisoned,
1416
1421
  files: response.files,
@@ -1430,14 +1435,14 @@ var PieceWorker = class {
1430
1435
  cleanup();
1431
1436
  reject(new PieceWorkerExitError(code, signal));
1432
1437
  };
1433
- const onHostCall = (value) => {
1438
+ const onHostCall = AsyncResource.bind((value) => {
1434
1439
  const message = value;
1435
1440
  if (message.type === "host-call") {
1436
1441
  this.serveHostCall(worker, message, taps.hostCalls);
1437
1442
  return;
1438
1443
  }
1439
1444
  if (message.type === "host-notify") serveNotify(message, taps.notifications);
1440
- };
1445
+ });
1441
1446
  const cleanup = () => {
1442
1447
  reactor?.stop();
1443
1448
  reactor = void 0;
@@ -2223,7 +2228,7 @@ function asList(value, side) {
2223
2228
  if (Array.isArray(value)) return value;
2224
2229
  throw new BranchConfigError(`${side} must be a list, got ${describe(value)}`);
2225
2230
  }
2226
- const blank = (value) => value === void 0 || value === null || value === "";
2231
+ const blank$1 = (value) => value === void 0 || value === null || value === "";
2227
2232
  function textOp(label, test) {
2228
2233
  return {
2229
2234
  label,
@@ -2301,8 +2306,8 @@ const BRANCH_OPERATORS = {
2301
2306
  },
2302
2307
  LIST_IS_EMPTY: unary("List is empty", "list", (v) => asList(v, "left").length === 0),
2303
2308
  LIST_IS_NOT_EMPTY: unary("List is not empty", "list", (v) => asList(v, "left").length > 0),
2304
- EXISTS: unary("Exists", "any", (v) => !blank(v)),
2305
- DOES_NOT_EXIST: unary("Does not exist", "any", blank)
2309
+ EXISTS: unary("Exists", "any", (v) => !blank$1(v)),
2310
+ DOES_NOT_EXIST: unary("Does not exist", "any", blank$1)
2306
2311
  };
2307
2312
  const EXISTENCE_OPERATORS = /* @__PURE__ */ new Set(["EXISTS", "DOES_NOT_EXIST"]);
2308
2313
  const OPERATORS = BRANCH_OPERATORS;
@@ -2339,6 +2344,24 @@ function resolveStepInput(step, scope) {
2339
2344
  assertNoUnavailableValue(input);
2340
2345
  return input;
2341
2346
  }
2347
+ function resolveResumedCoreInput(step, scope) {
2348
+ try {
2349
+ return resolveStepInput(step, scope);
2350
+ } catch (error) {
2351
+ if (!(error instanceof UnavailableValueError)) throw error;
2352
+ }
2353
+ const { config } = step;
2354
+ if (config === null || typeof config !== "object" || Array.isArray(config)) return {};
2355
+ const input = {};
2356
+ for (const [key, value] of Object.entries(config)) try {
2357
+ const resolved = resolveExpressions(value, scope);
2358
+ assertNoUnavailableValue(resolved);
2359
+ input[key] = resolved;
2360
+ } catch (error) {
2361
+ if (!(error instanceof UnavailableValueError)) throw error;
2362
+ }
2363
+ return input;
2364
+ }
2342
2365
  function resolveStepInputRaw(step, scope) {
2343
2366
  const { config } = step;
2344
2367
  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);
@@ -2984,18 +3007,407 @@ const manualTrigger = createTrigger({
2984
3007
  ...hooks("manual")
2985
3008
  });
2986
3009
  //#endregion
3010
+ //#region src/pieces/core/context.ts
3011
+ var CoreCannotPauseError = class extends Error {
3012
+ constructor(block) {
3013
+ super(`${block} suspends the run, which only a workflow run can do, not a step test`);
3014
+ this.name = "RunCannotPauseError";
3015
+ }
3016
+ };
3017
+ //#endregion
3018
+ //#region src/pieces/core/waits.ts
3019
+ const UNIT_MS = {
3020
+ seconds: 1e3,
3021
+ minutes: 6e4,
3022
+ hours: 36e5,
3023
+ days: 864e5
3024
+ };
3025
+ const unitOptions = Object.keys(UNIT_MS).map((unit) => ({
3026
+ value: unit,
3027
+ label: unit[0].toUpperCase() + unit.slice(1)
3028
+ }));
3029
+ function durationMs(amount, unit, label) {
3030
+ if (amount === void 0 || amount === null || amount === "") return;
3031
+ const value = Number(amount);
3032
+ if (!Number.isFinite(value) || value < 0) throw new Error(`${label}: ${JSON.stringify(amount)} is not a duration`);
3033
+ if (typeof unit !== "string" || !Object.hasOwn(UNIT_MS, unit)) throw new Error(`${label}: unknown unit ${JSON.stringify(unit)}`);
3034
+ return value * UNIT_MS[unit];
3035
+ }
3036
+ function context$2(ctx) {
3037
+ return ctx;
3038
+ }
3039
+ function assertCanPause(ctx, block) {
3040
+ if (!ctx.run.canPause) throw new CoreCannotPauseError(block);
3041
+ }
3042
+ const delayAction = withPorts(createAction({
3043
+ name: "delay",
3044
+ displayName: "Delay",
3045
+ description: "Suspends the run for a while, or until a date, then carries on. The run holds no worker while it waits.",
3046
+ requireAuth: false,
3047
+ props: {
3048
+ mode: Property.StaticDropdown({
3049
+ displayName: "Mode",
3050
+ required: true,
3051
+ defaultValue: "for",
3052
+ options: { options: [{
3053
+ value: "for",
3054
+ label: "Delay for"
3055
+ }, {
3056
+ value: "until",
3057
+ label: "Delay until"
3058
+ }] }
3059
+ }),
3060
+ amount: withHints(Property.Number({
3061
+ displayName: "Amount",
3062
+ required: true
3063
+ }), shownWhen("mode", ["for"])),
3064
+ unit: withHints(Property.StaticDropdown({
3065
+ displayName: "Unit",
3066
+ required: true,
3067
+ defaultValue: "minutes",
3068
+ options: { options: unitOptions }
3069
+ }), shownWhen("mode", ["for"])),
3070
+ until: withHints(Property.DateTime({
3071
+ displayName: "Until",
3072
+ description: "ISO 8601 date and time; a past one does not wait.",
3073
+ required: true
3074
+ }), shownWhen("mode", ["until"]))
3075
+ },
3076
+ run: async (raw) => {
3077
+ const ctx = context$2(raw);
3078
+ if (ctx.executionType === "RESUME") return { resumedAt: (/* @__PURE__ */ new Date()).toISOString() };
3079
+ const { mode, amount, unit, until } = ctx.propsValue;
3080
+ let resumeAt;
3081
+ if (mode === "until") {
3082
+ resumeAt = typeof until === "string" ? Date.parse(until) : NaN;
3083
+ if (Number.isNaN(resumeAt)) throw new Error(`Delay: ${JSON.stringify(until)} is not a date and time`);
3084
+ } else {
3085
+ const ms = durationMs(amount, unit, "Delay");
3086
+ if (ms === void 0) throw new Error("Delay: an amount is required");
3087
+ resumeAt = Date.now() + ms;
3088
+ }
3089
+ if (resumeAt <= Date.now()) return { resumedAt: (/* @__PURE__ */ new Date()).toISOString() };
3090
+ assertCanPause(ctx, "Delay");
3091
+ const resumeDateTime = new Date(resumeAt).toISOString();
3092
+ const waitpoint = await ctx.run.createWaitpoint({
3093
+ type: "DELAY",
3094
+ resumeDateTime
3095
+ });
3096
+ ctx.run.waitForWaitpoint(waitpoint.id);
3097
+ return { resumeAt: resumeDateTime };
3098
+ }
3099
+ }), {
3100
+ ports: PIECE_ACTION_PORTS,
3101
+ portOf: () => NEXT_PORT
3102
+ });
3103
+ const REASON_RULES = [
3104
+ "none",
3105
+ "reject",
3106
+ "both"
3107
+ ];
3108
+ function text(value, fallback) {
3109
+ return typeof value === "string" && value.trim() ? value : fallback;
3110
+ }
3111
+ function approvers(value) {
3112
+ const list = Array.isArray(value) ? value : typeof value === "string" ? value.split(/[\s,]+/) : [];
3113
+ return [...new Set(list.map((entry) => String(entry).trim()).filter(Boolean))];
3114
+ }
3115
+ function approvalDecision(payload) {
3116
+ const record = payload ?? {};
3117
+ const body = record.body && typeof record.body === "object" ? record.body : {};
3118
+ const query = record.queryParams ?? {};
3119
+ return {
3120
+ approved: typeof body.approved === "boolean" ? body.approved : query.action === "approve",
3121
+ reason: [body.reason, query.reason].find((value) => typeof value === "string" && value !== "") ?? null,
3122
+ decidedBy: typeof body.decidedBy === "string" ? body.decidedBy : null,
3123
+ decidedAt: typeof body.decidedAt === "string" ? body.decidedAt : (/* @__PURE__ */ new Date()).toISOString()
3124
+ };
3125
+ }
3126
+ const approvalAction = withPorts(createAction({
3127
+ name: "approval",
3128
+ displayName: "Request approval",
3129
+ description: "Suspends the run until someone approves or rejects it in the workflow editor. The decision is the step's output.",
3130
+ requireAuth: false,
3131
+ props: {
3132
+ title: Property.ShortText({
3133
+ displayName: "Title",
3134
+ required: true
3135
+ }),
3136
+ body: Property.LongText({
3137
+ displayName: "Details",
3138
+ description: "Markdown shown to the approver; expressions are filled in when the run pauses.",
3139
+ required: false
3140
+ }),
3141
+ approveLabel: Property.ShortText({
3142
+ displayName: "Approve label",
3143
+ required: false,
3144
+ defaultValue: "Approve"
3145
+ }),
3146
+ rejectLabel: Property.ShortText({
3147
+ displayName: "Reject label",
3148
+ required: false,
3149
+ defaultValue: "Reject"
3150
+ }),
3151
+ reasonRequiredOn: Property.StaticDropdown({
3152
+ displayName: "Reason required",
3153
+ required: false,
3154
+ defaultValue: "none",
3155
+ options: { options: [
3156
+ {
3157
+ value: "none",
3158
+ label: "Never"
3159
+ },
3160
+ {
3161
+ value: "reject",
3162
+ label: "On reject"
3163
+ },
3164
+ {
3165
+ value: "both",
3166
+ label: "Always"
3167
+ }
3168
+ ] }
3169
+ }),
3170
+ approvers: Property.Array({
3171
+ displayName: "Approvers",
3172
+ description: "DIDs or addresses allowed to decide in the editor; empty lets anyone who can edit the workflow.",
3173
+ required: false
3174
+ }),
3175
+ expiresAfter: Property.Number({
3176
+ displayName: "Expires after",
3177
+ description: "Ends the run if nobody decides in time; the workflow's maxSuspensionDays still applies.",
3178
+ required: false
3179
+ }),
3180
+ expiresUnit: Property.StaticDropdown({
3181
+ displayName: "Expiry unit",
3182
+ required: false,
3183
+ defaultValue: "days",
3184
+ options: { options: unitOptions }
3185
+ })
3186
+ },
3187
+ run: async (raw) => {
3188
+ const ctx = context$2(raw);
3189
+ if (ctx.executionType === "RESUME") return approvalDecision(ctx.resumePayload);
3190
+ assertCanPause(ctx, "Request approval");
3191
+ const props = ctx.propsValue;
3192
+ const rule = REASON_RULES.includes(props.reasonRequiredOn) ? props.reasonRequiredOn : "none";
3193
+ const approval = {
3194
+ title: text(props.title, "Approval"),
3195
+ body: typeof props.body === "string" && props.body ? props.body : null,
3196
+ approveLabel: text(props.approveLabel, "Approve"),
3197
+ rejectLabel: text(props.rejectLabel, "Reject"),
3198
+ reasonRequiredOn: rule,
3199
+ approvers: approvers(props.approvers)
3200
+ };
3201
+ const expiresMs = durationMs(props.expiresAfter, props.expiresUnit ?? "days", "Request approval");
3202
+ const waitpoint = await ctx.run.createWaitpoint({
3203
+ type: "WEBHOOK",
3204
+ approval,
3205
+ ...expiresMs ? { expiresAt: new Date(Date.now() + expiresMs).toISOString() } : {}
3206
+ });
3207
+ ctx.run.waitForWaitpoint(waitpoint.id);
3208
+ return {};
3209
+ }
3210
+ }), {
3211
+ ports: PIECE_ACTION_PORTS,
3212
+ portOf: () => NEXT_PORT
3213
+ });
3214
+ //#endregion
3215
+ //#region src/pieces/core/responses.ts
3216
+ const RESUME_URL_HEADER = "x-resume-webhook-url";
3217
+ const REDIRECT_STATUS = 302;
3218
+ function responseProps(bodyRequired) {
3219
+ return {
3220
+ responseType: Property.StaticDropdown({
3221
+ displayName: "Response type",
3222
+ required: false,
3223
+ defaultValue: "json",
3224
+ options: { options: [
3225
+ {
3226
+ value: "json",
3227
+ label: "JSON"
3228
+ },
3229
+ {
3230
+ value: "raw",
3231
+ label: "Raw"
3232
+ },
3233
+ {
3234
+ value: "redirect",
3235
+ label: "Redirect"
3236
+ }
3237
+ ] }
3238
+ }),
3239
+ status: withHints(Property.Number({
3240
+ displayName: "Status",
3241
+ description: "HTTP status; 200 when blank.",
3242
+ required: false
3243
+ }), shownWhen("responseType", ["json", "raw"])),
3244
+ headers: withHints(Property.Object({
3245
+ displayName: "Headers",
3246
+ required: false
3247
+ }), shownWhen("responseType", ["json", "raw"])),
3248
+ body: withHints(Property.Json({
3249
+ displayName: "JSON body",
3250
+ required: bodyRequired
3251
+ }), shownWhen("responseType", ["json"])),
3252
+ text: withHints(Property.LongText({
3253
+ displayName: "Raw body",
3254
+ required: bodyRequired
3255
+ }), shownWhen("responseType", ["raw"])),
3256
+ redirectUrl: withHints(Property.ShortText({
3257
+ displayName: "Redirect URL",
3258
+ required: true
3259
+ }), shownWhen("responseType", ["redirect"]))
3260
+ };
3261
+ }
3262
+ const blank = (value) => value === void 0 || value === null || value === "" || typeof value === "object" && Object.keys(value).length === 0;
3263
+ function jsonBody(value, block) {
3264
+ if (typeof value !== "string") return value;
3265
+ try {
3266
+ return JSON.parse(value);
3267
+ } catch {
3268
+ throw new Error(`${block}: the JSON body is not valid JSON`);
3269
+ }
3270
+ }
3271
+ function headerMap(value) {
3272
+ if (!value || typeof value !== "object" || Array.isArray(value)) return {};
3273
+ const headers = {};
3274
+ for (const [name, entry] of Object.entries(value)) if (typeof entry === "string" || typeof entry === "number") headers[name] = String(entry);
3275
+ return headers;
3276
+ }
3277
+ function withProtocol(url) {
3278
+ return /^https?:\/\//i.test(url) ? url : `https://${url}`;
3279
+ }
3280
+ function configuredResponse(props, block) {
3281
+ const type = blank(props.responseType) ? "json" : props.responseType;
3282
+ if (type === "redirect") {
3283
+ if (typeof props.redirectUrl !== "string" || !props.redirectUrl.trim()) throw new Error(`${block}: a redirect URL is required`);
3284
+ const location = withProtocol(props.redirectUrl.trim());
3285
+ return {
3286
+ status: REDIRECT_STATUS,
3287
+ headers: { location },
3288
+ body: location
3289
+ };
3290
+ }
3291
+ const raw = type === "raw";
3292
+ const given = raw ? props.text : props.body;
3293
+ if (blank(given) && blank(props.status) && blank(props.headers)) return;
3294
+ return {
3295
+ status: blank(props.status) ? 200 : Number(props.status),
3296
+ headers: headerMap(props.headers),
3297
+ ...blank(given) ? {} : { body: raw ? String(given) : jsonBody(given, block) }
3298
+ };
3299
+ }
3300
+ function context$1(ctx) {
3301
+ return ctx;
3302
+ }
3303
+ const stopAction = withPorts(createAction({
3304
+ name: "stop",
3305
+ displayName: "Stop run",
3306
+ description: "Ends the run successfully without running further steps. In a run that holds a request (a sync webhook or a /sync resume) it answers with the response set here, or 200 with {} when none is.",
3307
+ requireAuth: false,
3308
+ props: responseProps(false),
3309
+ run: (raw) => {
3310
+ const ctx = context$1(raw);
3311
+ const response = configuredResponse(ctx.propsValue, "Stop run");
3312
+ ctx.run.stop(response ? { response } : void 0);
3313
+ return Promise.resolve(response ? { response } : {});
3314
+ }
3315
+ }), {
3316
+ ports: STOP_PORTS,
3317
+ portOf: () => NEXT_PORT
3318
+ });
3319
+ function withResumeUrl(response, url, inBody) {
3320
+ const body = response.body ?? {};
3321
+ const object = typeof body === "object" && !Array.isArray(body);
3322
+ return {
3323
+ ...response,
3324
+ headers: {
3325
+ ...response.headers,
3326
+ [RESUME_URL_HEADER]: url
3327
+ },
3328
+ ...inBody && object ? { body: {
3329
+ ...body,
3330
+ resumeUrl: url
3331
+ } } : {}
3332
+ };
3333
+ }
3334
+ function resumeRequest(payload) {
3335
+ const record = payload ?? {};
3336
+ return {
3337
+ body: record.body ?? null,
3338
+ headers: record.headers ?? {},
3339
+ queryParams: record.queryParams ?? {}
3340
+ };
3341
+ }
3342
+ const waitForWebhookAction = withPorts(createAction({
3343
+ name: "waitForWebhook",
3344
+ displayName: "Respond and wait for webhook",
3345
+ description: `Answers the request the run holds with this response and the step's resume URL (in the ${RESUME_URL_HEADER} header, and in a JSON body when asked), then suspends the run until that URL is called. The resuming request's body, headers and query are the step's output.`,
3346
+ requireAuth: false,
3347
+ props: {
3348
+ ...responseProps(false),
3349
+ includeResumeUrl: withHints(Property.Checkbox({
3350
+ displayName: "Add resumeUrl to the body",
3351
+ description: "Adds a resumeUrl field to a JSON object body. POST to it to resume; append /sync to wait for the run's answer.",
3352
+ required: false,
3353
+ defaultValue: true
3354
+ }), shownWhen("responseType", ["json"])),
3355
+ expiresAfter: Property.Number({
3356
+ displayName: "Expires after",
3357
+ description: "Ends the run if nobody calls the URL in time; the workflow's maxSuspensionDays still applies.",
3358
+ required: false
3359
+ }),
3360
+ expiresUnit: Property.StaticDropdown({
3361
+ displayName: "Expiry unit",
3362
+ required: false,
3363
+ defaultValue: "days",
3364
+ options: { options: unitOptions }
3365
+ })
3366
+ },
3367
+ run: async (raw) => {
3368
+ const ctx = context$1(raw);
3369
+ if (ctx.executionType === "RESUME") return resumeRequest(ctx.resumePayload);
3370
+ const block = "Respond and wait for webhook";
3371
+ if (!ctx.run.canPause) throw new CoreCannotPauseError(block);
3372
+ const props = ctx.propsValue;
3373
+ const response = configuredResponse(props, block) ?? {
3374
+ status: 200,
3375
+ headers: {}
3376
+ };
3377
+ const inBody = (blank(props.responseType) || props.responseType === "json") && props.includeResumeUrl !== false;
3378
+ const expiresMs = durationMs(props.expiresAfter, props.expiresUnit ?? "days", block);
3379
+ const waitpoint = await ctx.run.createWaitpoint({
3380
+ type: "WEBHOOK",
3381
+ responseToSend: (url) => withResumeUrl(response, url, inBody),
3382
+ ...expiresMs ? { expiresAt: new Date(Date.now() + expiresMs).toISOString() } : {}
3383
+ });
3384
+ ctx.run.waitForWaitpoint(waitpoint.id);
3385
+ return { resumeUrl: waitpoint.resumeUrl };
3386
+ }
3387
+ }), {
3388
+ ports: PIECE_ACTION_PORTS,
3389
+ portOf: () => NEXT_PORT
3390
+ });
3391
+ //#endregion
2987
3392
  //#region src/pieces/core/version.ts
2988
- const CORE_PIECE_VERSION = "6.2.3-dev.84";
3393
+ const CORE_PIECE_VERSION = "6.2.3-dev.85";
2989
3394
  //#endregion
2990
3395
  //#region src/pieces/core/index.ts
2991
3396
  const corePiece = createPiece({
2992
3397
  displayName: "Core",
2993
- description: "The engine's own blocks: branching, assertions, and the triggers that belong to no service.",
3398
+ description: "The engine's own blocks: branching, assertions, delays, approvals, stopping and webhook replies, and the triggers that belong to no service.",
2994
3399
  logoUrl: "",
2995
3400
  authors: ["powerhouse"],
2996
3401
  auth: void 0,
2997
3402
  categories: [PieceCategory.CORE],
2998
- actions: [branchAction, assertAction],
3403
+ actions: [
3404
+ branchAction,
3405
+ assertAction,
3406
+ delayAction,
3407
+ approvalAction,
3408
+ stopAction,
3409
+ waitForWebhookAction
3410
+ ],
2999
3411
  triggers: [
3000
3412
  scheduleTrigger,
3001
3413
  webhookTrigger,
@@ -3050,10 +3462,56 @@ var UnknownBuiltinActionError = class extends Error {
3050
3462
  this.name = "UnknownBuiltinActionError";
3051
3463
  }
3052
3464
  };
3053
- async function runBuiltinAction(builtin, actionName, propsValue) {
3465
+ function resolvedCall(call, resumeBaseUrl) {
3466
+ const { responseToSend, ...rest } = call;
3467
+ if (typeof responseToSend !== "function") return responseToSend ? {
3468
+ ...rest,
3469
+ responseToSend
3470
+ } : rest;
3471
+ if (!resumeBaseUrl) return rest;
3472
+ const token = randomBytes(32).toString("base64url");
3473
+ const url = `${resumeBaseUrl.replace(/\/+$/, "")}/${token}`;
3474
+ return {
3475
+ ...rest,
3476
+ responseToSend: responseToSend(url),
3477
+ token
3478
+ };
3479
+ }
3480
+ async function runBuiltinAction(builtin, actionName, propsValue, options = {}) {
3054
3481
  const action = getActions(builtin.piece)[actionName];
3055
3482
  if (!action || typeof action.run !== "function") throw new UnknownBuiltinActionError(builtin.name, actionName);
3056
- const output = await action.run({ propsValue });
3483
+ let paused;
3484
+ let stopped;
3485
+ const { waitpoints, resume } = options;
3486
+ const context = {
3487
+ propsValue,
3488
+ executionType: resume ? "RESUME" : "BEGIN",
3489
+ ...resume ? { resumePayload: resume.payload } : {},
3490
+ run: {
3491
+ canPause: waitpoints !== void 0,
3492
+ createWaitpoint: (call) => {
3493
+ if (!waitpoints) throw new CoreCannotPauseError(actionName);
3494
+ return waitpoints.create(resolvedCall(call, waitpoints.resumeBaseUrl));
3495
+ },
3496
+ waitForWaitpoint: (waitpointId) => {
3497
+ if (!waitpoints) throw new CoreCannotPauseError(actionName);
3498
+ paused = { waitpointId };
3499
+ },
3500
+ stop: (params) => {
3501
+ stopped = jsonSafe(params?.response ?? {}) ?? {};
3502
+ }
3503
+ }
3504
+ };
3505
+ const output = await action.run(context);
3506
+ if (paused) return {
3507
+ output,
3508
+ paused
3509
+ };
3510
+ if (stopped) return {
3511
+ output,
3512
+ response: stopped,
3513
+ stopped: true
3514
+ };
3057
3515
  return {
3058
3516
  output,
3059
3517
  ...isPorted(action) ? { port: action.portOf(output) } : {}
@@ -3137,6 +3595,27 @@ function storeHandlers(port) {
3137
3595
  };
3138
3596
  }
3139
3597
  const logger$1 = childLogger(["workflow", "piece-staging"]);
3598
+ function waitpointHandlers(port, execution, parsed, created) {
3599
+ return { [WAITPOINT_CREATE]: async (payload) => {
3600
+ const call = payload;
3601
+ const waitpoint = await port.create({
3602
+ ...waitpointRequest(execution, parsed),
3603
+ type: call.type,
3604
+ ...call.resumeDateTime !== void 0 ? { resumeDateTime: call.resumeDateTime } : {},
3605
+ ...call.responseToSend !== void 0 ? { responseToSend: call.responseToSend } : {}
3606
+ });
3607
+ created.add(waitpoint.id);
3608
+ return waitpoint;
3609
+ } };
3610
+ }
3611
+ function waitpointRequest(execution, parsed) {
3612
+ return {
3613
+ stepId: execution.step.id,
3614
+ stepKey: execution.step.key,
3615
+ pieceName: parsed.packageName,
3616
+ blockName: parsed.name
3617
+ };
3618
+ }
3140
3619
  function stepTaps(options, execution, values) {
3141
3620
  const { onPieceLog, onPartialOutput } = options;
3142
3621
  if (!onPieceLog && !onPartialOutput) return void 0;
@@ -3210,13 +3689,26 @@ var ActivepiecesBlockExecutor = class {
3210
3689
  try {
3211
3690
  const builtin = builtinPiece(parsed.packageName);
3212
3691
  return {
3213
- ...builtin ? await runBuiltinAction(builtin, parsed.name, execution.config) : await this.run(execution, parsed),
3692
+ ...builtin ? await runBuiltinAction(builtin, parsed.name, execution.config, this.builtinOptions(execution, parsed)) : await this.run(execution, parsed),
3214
3693
  resolution
3215
3694
  };
3216
3695
  } catch (error) {
3217
3696
  throw withResolution(error, resolution);
3218
3697
  }
3219
3698
  }
3699
+ builtinOptions(execution, parsed) {
3700
+ const port = this.options.stepTest?.() ?? false ? void 0 : this.options.waitpoints?.();
3701
+ return {
3702
+ ...port ? { waitpoints: {
3703
+ create: (call) => port.create({
3704
+ ...waitpointRequest(execution, parsed),
3705
+ ...call
3706
+ }),
3707
+ ...port.resumeBaseUrl ? { resumeBaseUrl: port.resumeBaseUrl } : {}
3708
+ } } : {},
3709
+ ...execution.resume ? { resume: execution.resume } : {}
3710
+ };
3711
+ }
3220
3712
  async run(execution, parsed) {
3221
3713
  const stagingDir = this.options.stagingRoot ? path.join(this.options.stagingRoot, randomUUID()) : void 0;
3222
3714
  const runSecrets = execution.redactValues ?? [];
@@ -3245,6 +3737,15 @@ var ActivepiecesBlockExecutor = class {
3245
3737
  const pieceStore = this.options.pieceStore;
3246
3738
  const notifications = stepTaps(this.options, execution, redactValues);
3247
3739
  const egress = this.options.egress === void 0 ? DEFAULT_EGRESS_POLICY : this.options.egress;
3740
+ const uploads = this.uploadHandlers(stagingDir, signal);
3741
+ const stepTest = this.options.stepTest?.() ?? false;
3742
+ const waitpoints = stepTest ? void 0 : this.options.waitpoints?.();
3743
+ const created = /* @__PURE__ */ new Set();
3744
+ const hostCalls = pieceStore || uploads || waitpoints ? {
3745
+ ...pieceStore ? storeHandlers(pieceStore) : {},
3746
+ ...uploads,
3747
+ ...waitpoints ? waitpointHandlers(waitpoints, execution, parsed, created) : {}
3748
+ } : void 0;
3248
3749
  const reactorTap = await this.options.reactorAccess?.({
3249
3750
  block: parsed,
3250
3751
  reactorConnectionId: execution.reactorConnectionId
@@ -3259,8 +3760,15 @@ var ActivepiecesBlockExecutor = class {
3259
3760
  stepName: execution.step.key
3260
3761
  },
3261
3762
  ...redactValues.length > 0 ? { redactValues } : {},
3262
- ...this.options.stepTest?.() ? { stepTest: true } : {},
3763
+ ...stepTest ? { stepTest: true } : {},
3764
+ ...waitpoints ? { canPause: true } : {},
3765
+ ...waitpoints?.resumeBaseUrl ? { resumeBaseUrl: waitpoints.resumeBaseUrl } : {},
3766
+ ...execution.resume ? {
3767
+ executionType: "RESUME",
3768
+ resumePayload: execution.resume.payload
3769
+ } : {},
3263
3770
  ...stagingDir ? { stagingDir } : {},
3771
+ ...uploads ? { uploadFiles: true } : {},
3264
3772
  ...stagedInputs ? { stagedInputs } : {},
3265
3773
  ...pieceStore ? { durableStore: true } : {},
3266
3774
  ...this.options.onPieceLog ? { captureLogs: true } : {},
@@ -3269,13 +3777,31 @@ var ActivepiecesBlockExecutor = class {
3269
3777
  }, {
3270
3778
  ...remainingMs ? { timeoutMs: remainingMs } : {},
3271
3779
  hostCallTimeoutMs: hostCallTimeoutForStep(timeoutMs),
3272
- ...pieceStore ? { hostCalls: storeHandlers(pieceStore) } : {},
3780
+ ...hostCalls ? { hostCalls } : {},
3273
3781
  ...notifications ? { notifications } : {},
3274
3782
  ...reactorTap ? { reactor: reactorTap } : {}
3275
3783
  });
3784
+ const output = await this.ingestFiles(result.output, result.files, stagingDir, signal);
3785
+ const answer = {
3786
+ ...result.response ? { response: result.response } : {},
3787
+ ...result.stopped ? { stopped: true } : {}
3788
+ };
3789
+ if (!result.paused) return {
3790
+ output,
3791
+ redactValues,
3792
+ ...answer
3793
+ };
3794
+ if (!waitpoints) throw new Error("The step paused a run that cannot pause");
3795
+ if ("waitpointId" in result.paused && !created.has(result.paused.waitpointId)) throw new Error(`waitForWaitpoint: the step waited on waitpoint "${result.paused.waitpointId}", which it did not create; a step can only wait on a waitpoint its own createWaitpoint returned`);
3796
+ const waitpointId = "waitpointId" in result.paused ? result.paused.waitpointId : (await waitpoints.create({
3797
+ ...waitpointRequest(execution, parsed),
3798
+ ...result.paused.pause,
3799
+ ...result.paused.token ? { token: result.paused.token } : {}
3800
+ })).id;
3276
3801
  return {
3277
- output: await this.ingestFiles(result.output, result.files, stagingDir, signal),
3278
- redactValues
3802
+ output,
3803
+ redactValues,
3804
+ paused: { waitpointId }
3279
3805
  };
3280
3806
  } catch (error) {
3281
3807
  throw redactThrown(error, redactValues);
@@ -3341,6 +3867,20 @@ var ActivepiecesBlockExecutor = class {
3341
3867
  });
3342
3868
  return meta;
3343
3869
  }
3870
+ uploadHandlers(stagingDir, signal) {
3871
+ const port = this.options.attachments;
3872
+ if (!stagingDir || !port) return void 0;
3873
+ return { [FILES_UPLOAD]: async (payload) => {
3874
+ const file = payload;
3875
+ const size = await this.checkStagedFile(file, stagingDir);
3876
+ return port.write({
3877
+ path: path.resolve(file.path),
3878
+ fileName: file.fileName,
3879
+ size,
3880
+ contentType: file.contentType
3881
+ }, signal);
3882
+ } };
3883
+ }
3344
3884
  async checkStagedFile(file, stagingDir) {
3345
3885
  if (!stagingDir) throw new Error(`Refusing to ingest "${file.fileName}": no staging directory`);
3346
3886
  const root = await realpath(stagingDir);
@@ -3531,6 +4071,20 @@ function actionRetryPolicy(retry) {
3531
4071
  ...declared
3532
4072
  });
3533
4073
  }
4074
+ function lazyEntry(read) {
4075
+ let value;
4076
+ let done = false;
4077
+ return Object.defineProperty({}, "output", {
4078
+ enumerable: true,
4079
+ get: () => {
4080
+ if (!done) {
4081
+ value = read();
4082
+ done = true;
4083
+ }
4084
+ return value;
4085
+ }
4086
+ });
4087
+ }
3534
4088
  function deadPortWarnings(definition, declaredPorts) {
3535
4089
  const blocks = new Map(definition.steps.map((step) => [step.id, {
3536
4090
  key: step.key,
@@ -3581,7 +4135,9 @@ async function runWorkflow(options) {
3581
4135
  let runFailed;
3582
4136
  let runCancelled;
3583
4137
  let runFailedName;
3584
- let executedCount = 0;
4138
+ let runWaiting;
4139
+ let runStopped = false;
4140
+ let executedCount = options.firstOrdinal ?? 0;
3585
4141
  const sleep = options.sleep ?? ((ms) => new Promise((resolve) => {
3586
4142
  setTimeout(resolve, ms).unref();
3587
4143
  }));
@@ -3658,7 +4214,27 @@ async function runWorkflow(options) {
3658
4214
  scope.steps[step.key] = { output: null };
3659
4215
  decideOutgoing(step.id, "next");
3660
4216
  };
4217
+ const carryStep = (step, carried) => {
4218
+ const failed = carried.status === "FAILED";
4219
+ const port = carried.port ?? (failed ? "error" : "next");
4220
+ records.set(step.id, {
4221
+ stepId: step.id,
4222
+ key: step.key,
4223
+ pieceName: step.pieceName,
4224
+ blockName: step.actionName,
4225
+ status: carried.status,
4226
+ port,
4227
+ carried: true
4228
+ });
4229
+ scope.steps[step.key] = failed ? { error: carried.error ?? "" } : lazyEntry(carried.output);
4230
+ decideOutgoing(step.id, port);
4231
+ };
3661
4232
  const executeStep = async (step) => {
4233
+ const carried = options.carriedSteps?.get(step.id);
4234
+ if (carried) {
4235
+ carryStep(step, carried);
4236
+ return;
4237
+ }
3662
4238
  if (step.skip === true) {
3663
4239
  await passStep(step);
3664
4240
  return;
@@ -3696,8 +4272,9 @@ async function runWorkflow(options) {
3696
4272
  const maxAttempts = retry?.maxAttempts ?? 1;
3697
4273
  const startedAt = (/* @__PURE__ */ new Date()).toISOString();
3698
4274
  let input;
4275
+ const resumedCore = options.resume?.stepId === step.id && step.pieceName === CORE_PIECE_NAME;
3699
4276
  try {
3700
- input = resolveStepInput(step, scope);
4277
+ input = resumedCore ? resolveResumedCoreInput(step, scope) : resolveStepInput(step, scope);
3701
4278
  checkDynamicProperties(input, step.propertySettings);
3702
4279
  } catch (error) {
3703
4280
  await endStep(step, {
@@ -3718,10 +4295,34 @@ async function runWorkflow(options) {
3718
4295
  reactorConnectionId: step.reactorConnectionId,
3719
4296
  step,
3720
4297
  ...runSecrets.length > 0 ? { redactValues: runSecrets } : {},
3721
- ...defaultTimeout
4298
+ ...defaultTimeout,
4299
+ ...options.resume?.stepId === step.id ? { resume: { payload: options.resume.payload } } : {}
3722
4300
  });
3723
4301
  const port = result.port ?? "next";
3724
4302
  const values = [...runSecrets, ...result.redactValues ?? []];
4303
+ if (result.response) options.onRespond?.(result.response);
4304
+ if (result.paused) {
4305
+ const record = {
4306
+ stepId: step.id,
4307
+ key: step.key,
4308
+ pieceName: step.pieceName,
4309
+ blockName: step.actionName,
4310
+ status: "WAITING",
4311
+ input: journaled(input, values),
4312
+ output: journaled(referenceDocuments(result.output), values),
4313
+ startedAt,
4314
+ ...withPiece(pieceRecord(result.resolution)),
4315
+ configHash: stepConfigHash(step),
4316
+ ...attempts
4317
+ };
4318
+ records.set(step.id, record);
4319
+ await journal(record);
4320
+ runWaiting = {
4321
+ stepId: step.id,
4322
+ waitpointId: result.paused.waitpointId
4323
+ };
4324
+ return;
4325
+ }
3725
4326
  const record = {
3726
4327
  stepId: step.id,
3727
4328
  key: step.key,
@@ -3740,6 +4341,10 @@ async function runWorkflow(options) {
3740
4341
  records.set(step.id, record);
3741
4342
  await journal(record);
3742
4343
  scope.steps[step.key] = { output: result.output };
4344
+ if (result.stopped) {
4345
+ runStopped = true;
4346
+ return;
4347
+ }
3743
4348
  decideOutgoing(step.id, port);
3744
4349
  return;
3745
4350
  } catch (error) {
@@ -3845,8 +4450,9 @@ async function runWorkflow(options) {
3845
4450
  runCancelled ??= outOfTime();
3846
4451
  return runCancelled !== void 0;
3847
4452
  };
4453
+ const stopped = () => Boolean(runFailed || runCancelled || runWaiting || runStopped);
3848
4454
  let progressed = true;
3849
- while (progressed && !runFailed && !runCancelled) {
4455
+ while (progressed && !stopped()) {
3850
4456
  progressed = false;
3851
4457
  for (const step of definition.steps) {
3852
4458
  if (records.has(step.id)) continue;
@@ -3855,7 +4461,7 @@ async function runWorkflow(options) {
3855
4461
  if (expiredBeforeStep()) break;
3856
4462
  await executeStep(step);
3857
4463
  progressed = true;
3858
- if (runFailed || runCancelled) break;
4464
+ if (stopped()) break;
3859
4465
  continue;
3860
4466
  }
3861
4467
  if (inbound.length === 0) continue;
@@ -3865,9 +4471,18 @@ async function runWorkflow(options) {
3865
4471
  await executeStep(step);
3866
4472
  } else skipStep(step);
3867
4473
  progressed = true;
3868
- if (runFailed || runCancelled) break;
4474
+ if (stopped()) break;
3869
4475
  }
3870
4476
  }
4477
+ if (runWaiting && !runFailed && !runCancelled) {
4478
+ const warnings = options.declaredPorts ? deadPortWarnings(definition, options.declaredPorts) : [];
4479
+ return {
4480
+ status: "WAITING",
4481
+ steps: definition.steps.filter((step) => records.has(step.id)).map((step) => records.get(step.id)),
4482
+ waiting: runWaiting,
4483
+ ...warnings.length > 0 ? { warnings } : {}
4484
+ };
4485
+ }
3871
4486
  for (const step of definition.steps) if (!records.has(step.id)) skipStep(step);
3872
4487
  const steps = definition.steps.map((step) => records.get(step.id));
3873
4488
  const warnings = options.declaredPorts ? deadPortWarnings(definition, options.declaredPorts) : [];
@@ -3949,6 +4564,7 @@ var WorkflowTelemetry = class {
3949
4564
  stepDuration;
3950
4565
  phaseDuration;
3951
4566
  dispatcherGauges;
4567
+ suspendedGauge;
3952
4568
  poolGauges;
3953
4569
  constructor(options = {}) {
3954
4570
  this.tracer = options.tracer ?? trace.getTracer("@powerhousedao/reactor-workflow");
@@ -4078,6 +4694,18 @@ var WorkflowTelemetry = class {
4078
4694
  this.meter.addBatchObservableCallback(callback, observables);
4079
4695
  return () => this.meter.removeBatchObservableCallback(callback, observables);
4080
4696
  }
4697
+ observeSuspended(count) {
4698
+ this.suspendedGauge ??= this.meter.createObservableGauge("workflow.runs.suspended", {
4699
+ description: "Runs suspended on a waitpoint",
4700
+ unit: "{run}"
4701
+ });
4702
+ const gauge = this.suspendedGauge;
4703
+ const callback = (result) => {
4704
+ result.observe(count());
4705
+ };
4706
+ gauge.addCallback(callback);
4707
+ return () => gauge.removeCallback(callback);
4708
+ }
4081
4709
  createPoolGauges() {
4082
4710
  const gauge = (name, description) => this.meter.createObservableGauge(name, {
4083
4711
  description,
@@ -4219,6 +4847,6 @@ function tracedMethods(target, telemetry, prefix, parent, nested = []) {
4219
4847
  } });
4220
4848
  }
4221
4849
  //#endregion
4222
- export { redact as $, AttachmentCache as A, SecretDeletedError as B, blockLabel as C, describeBuiltinPiece as D, builtinPiece as E, shapeConnection as F, stepBlock as G, SecretOwnerMismatchError as H, resolveStepInput as I, PieceWorker as J, triggerBlock as K, expressionsIn as L, StaticConnectionResolver as M, declaredConnectionIds as N, CORE_PIECE_NAME$1 as O, declaredReactorConnectionIds as P, containsRedactedMarker as Q, InMemorySecretProvider as R, storeHandlers as S, isIndeterminateError as St, blockPorts as T, parseSecretRef as U, SecretNotFoundError as V, secretRefFromId as W, PieceWorkerError as X, PieceWorkerDisposedError as Y, PieceWorkerTimeoutError as Z, effectiveRetryPolicy as _, ensurePieceBundle as _t, PieceRegistry as a, parseAddress as at, isHostBound as b, pieceRegistrySource as bt, packagePieces as c, maxFileBytes as ct, runWorkflow as d, sourcedResolver as dt, redactMessage as et, canonicalJson as f, buildDescriptor as ft, checkDynamicProperties as g, unsupportedTrigger as gt, DynamicPropertiesError as h, unsupportedAuth as ht, tracedSteps as i, isPrivateAddress as it, BoundConnectionResolver as j, CORE_PIECE_VERSION as k, RUN_DEADLINE_ERROR_NAME as l, bundleResolver as lt, stepConfigHash as m, authMethodFor as mt, WorkflowTelemetry as n, secretsFor as nt, installedPiece as o, extractDedupeKey as ot, hashOf as p, UnsupportedPieceFeatureError as pt, PieceWorkerPool as q, tracedMethods as r, DEFAULT_EGRESS_POLICY as rt, installedPieces as s, FileTooLargeError as st, TELEMETRY_SCOPE as t, rememberSecrets as tt, errorNameOf as u, pieceModuleRef as ut, ActivepiecesBlockExecutor as v, fetchPieceBundle as vt, unpinnedNote as w, resolvedBlock as x, setPieceRegistryUrl as xt, CompositeBlockExecutor as y, npmRegistryBaseUrl as yt, SecretCreatorMismatchError as z };
4850
+ 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 };
4223
4851
 
4224
- //# sourceMappingURL=telemetry-CSemNycC.js.map
4852
+ //# sourceMappingURL=telemetry-B5OOpfCS.js.map