@upstash/workflow 0.2.22 → 0.2.23

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/solidjs.js CHANGED
@@ -197,8 +197,8 @@ var NANOID_LENGTH = 21;
197
197
  function getRandomInt() {
198
198
  return Math.floor(Math.random() * NANOID_CHARS.length);
199
199
  }
200
- function nanoid() {
201
- return Array.from({ length: NANOID_LENGTH }).map(() => NANOID_CHARS[getRandomInt()]).join("");
200
+ function nanoid(length = NANOID_LENGTH) {
201
+ return Array.from({ length }).map(() => NANOID_CHARS[getRandomInt()]).join("");
202
202
  }
203
203
  function getWorkflowRunId(id) {
204
204
  return `wfr_${id ?? nanoid()}`;
@@ -215,6 +215,37 @@ function decodeBase64(base64) {
215
215
  return binString;
216
216
  }
217
217
  }
218
+ function getUserIdFromToken(qstashClient) {
219
+ try {
220
+ const token = qstashClient.token;
221
+ const decodedToken = decodeBase64(token);
222
+ const tokenPayload = JSON.parse(decodedToken);
223
+ const userId = tokenPayload.UserID;
224
+ if (!userId) {
225
+ throw new WorkflowError("QStash token payload does not contain userId");
226
+ }
227
+ return userId;
228
+ } catch (error) {
229
+ throw new WorkflowError(
230
+ `Failed to decode QStash token while running create webhook step: ${error.message}`
231
+ );
232
+ }
233
+ }
234
+ function getQStashUrl(qstashClient) {
235
+ try {
236
+ const requester = qstashClient.http;
237
+ const baseUrl = requester.baseUrl;
238
+ if (!baseUrl) {
239
+ throw new WorkflowError("QStash client does not have a baseUrl");
240
+ }
241
+ return baseUrl;
242
+ } catch (error) {
243
+ throw new WorkflowError(`Failed to get QStash URL from client: ${error.message}`);
244
+ }
245
+ }
246
+ function getEventId() {
247
+ return `evt_${nanoid(15)}`;
248
+ }
218
249
 
219
250
  // node_modules/neverthrow/dist/index.es.js
220
251
  var defaultErrorConfig = {
@@ -640,7 +671,9 @@ var StepTypes = [
640
671
  "Call",
641
672
  "Wait",
642
673
  "Notify",
643
- "Invoke"
674
+ "Invoke",
675
+ "CreateWebhook",
676
+ "WaitForWebhook"
644
677
  ];
645
678
 
646
679
  // src/workflow-requests.ts
@@ -952,7 +985,9 @@ If you want to disable QStash Verification, you should clear env variables QSTAS
952
985
  // src/context/steps.ts
953
986
  var BaseLazyStep = class _BaseLazyStep {
954
987
  stepName;
955
- constructor(stepName) {
988
+ context;
989
+ constructor(context, stepName) {
990
+ this.context = context;
956
991
  if (!stepName) {
957
992
  throw new WorkflowError(
958
993
  "A workflow step name cannot be undefined or an empty string. Please provide a name for your workflow step."
@@ -970,13 +1005,14 @@ var BaseLazyStep = class _BaseLazyStep {
970
1005
  *
971
1006
  * will be called when returning the steps to the context from auto executor
972
1007
  *
973
- * @param out field of the step
1008
+ * @param step step
974
1009
  * @returns parsed out field
975
1010
  */
976
- parseOut(out) {
1011
+ parseOut(step) {
1012
+ const out = step.out;
977
1013
  if (out === void 0) {
978
1014
  if (this.allowUndefinedOut) {
979
- return void 0;
1015
+ return this.handleUndefinedOut(step);
980
1016
  } else {
981
1017
  throw new WorkflowError(
982
1018
  `Error while parsing output of ${this.stepType} step. Expected a string, but got: undefined`
@@ -984,27 +1020,26 @@ var BaseLazyStep = class _BaseLazyStep {
984
1020
  }
985
1021
  }
986
1022
  if (typeof out === "object") {
987
- if (this.stepType !== "Wait") {
988
- console.warn(
989
- `Error while parsing ${this.stepType} step output. Expected a string, but got object. Please reach out to Upstash Support.`
990
- );
991
- return out;
992
- }
993
- return {
994
- ...out,
995
- eventData: _BaseLazyStep.tryParsing(out.eventData)
996
- };
1023
+ console.warn(
1024
+ `Error while parsing ${this.stepType} step output. Expected a string, but got object. Please reach out to Upstash Support.`
1025
+ );
1026
+ return out;
997
1027
  }
998
1028
  if (typeof out !== "string") {
999
1029
  throw new WorkflowError(
1000
1030
  `Error while parsing output of ${this.stepType} step. Expected a string or undefined, but got: ${typeof out}`
1001
1031
  );
1002
1032
  }
1003
- return this.safeParseOut(out);
1033
+ return this.safeParseOut(out, step);
1004
1034
  }
1005
- safeParseOut(out) {
1035
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1036
+ safeParseOut(out, step) {
1006
1037
  return _BaseLazyStep.tryParsing(out);
1007
1038
  }
1039
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1040
+ handleUndefinedOut(step) {
1041
+ return void 0;
1042
+ }
1008
1043
  static tryParsing(stepOut) {
1009
1044
  try {
1010
1045
  return JSON.parse(stepOut);
@@ -1055,8 +1090,8 @@ var LazyFunctionStep = class extends BaseLazyStep {
1055
1090
  stepFunction;
1056
1091
  stepType = "Run";
1057
1092
  allowUndefinedOut = true;
1058
- constructor(stepName, stepFunction) {
1059
- super(stepName);
1093
+ constructor(context, stepName, stepFunction) {
1094
+ super(context, stepName);
1060
1095
  this.stepFunction = stepFunction;
1061
1096
  }
1062
1097
  getPlanStep(concurrent, targetStep) {
@@ -1086,8 +1121,8 @@ var LazySleepStep = class extends BaseLazyStep {
1086
1121
  sleep;
1087
1122
  stepType = "SleepFor";
1088
1123
  allowUndefinedOut = true;
1089
- constructor(stepName, sleep) {
1090
- super(stepName);
1124
+ constructor(context, stepName, sleep) {
1125
+ super(context, stepName);
1091
1126
  this.sleep = sleep;
1092
1127
  }
1093
1128
  getPlanStep(concurrent, targetStep) {
@@ -1128,8 +1163,8 @@ var LazySleepUntilStep = class extends BaseLazyStep {
1128
1163
  sleepUntil;
1129
1164
  stepType = "SleepUntil";
1130
1165
  allowUndefinedOut = true;
1131
- constructor(stepName, sleepUntil) {
1132
- super(stepName);
1166
+ constructor(context, stepName, sleepUntil) {
1167
+ super(context, stepName);
1133
1168
  this.sleepUntil = sleepUntil;
1134
1169
  }
1135
1170
  getPlanStep(concurrent, targetStep) {
@@ -1181,8 +1216,8 @@ var LazyCallStep = class _LazyCallStep extends BaseLazyStep {
1181
1216
  stringifyBody;
1182
1217
  stepType = "Call";
1183
1218
  allowUndefinedOut = false;
1184
- constructor(stepName, url, method, body, headers, retries, retryDelay, timeout, flowControl, stringifyBody) {
1185
- super(stepName);
1219
+ constructor(context, stepName, url, method, body, headers, retries, retryDelay, timeout, flowControl, stringifyBody) {
1220
+ super(context, stepName);
1186
1221
  this.url = url;
1187
1222
  this.method = method;
1188
1223
  this.body = body;
@@ -1326,13 +1361,12 @@ var LazyCallStep = class _LazyCallStep extends BaseLazyStep {
1326
1361
  ]);
1327
1362
  }
1328
1363
  };
1329
- var LazyWaitForEventStep = class extends BaseLazyStep {
1364
+ var LazyWaitEventStep = class extends BaseLazyStep {
1330
1365
  eventId;
1331
1366
  timeout;
1332
- stepType = "Wait";
1333
1367
  allowUndefinedOut = false;
1334
- constructor(stepName, eventId, timeout) {
1335
- super(stepName);
1368
+ constructor(context, stepName, eventId, timeout) {
1369
+ super(context, stepName);
1336
1370
  this.eventId = eventId;
1337
1371
  this.timeout = timeout;
1338
1372
  }
@@ -1357,13 +1391,6 @@ var LazyWaitForEventStep = class extends BaseLazyStep {
1357
1391
  concurrent
1358
1392
  });
1359
1393
  }
1360
- safeParseOut(out) {
1361
- const result = JSON.parse(out);
1362
- return {
1363
- ...result,
1364
- eventData: BaseLazyStep.tryParsing(result.eventData)
1365
- };
1366
- }
1367
1394
  getHeaders({ context, telemetry, invokeCount, step }) {
1368
1395
  const headers = super.getHeaders({ context, telemetry, invokeCount, step });
1369
1396
  headers.headers["Upstash-Workflow-CallType"] = "step";
@@ -1397,7 +1424,7 @@ var LazyWaitForEventStep = class extends BaseLazyStep {
1397
1424
  timeoutHeaders,
1398
1425
  step: {
1399
1426
  stepId: step.stepId,
1400
- stepType: "Wait",
1427
+ stepType: this.stepType,
1401
1428
  stepName: step.stepName,
1402
1429
  concurrent: step.concurrent,
1403
1430
  targetStep: step.targetStep
@@ -1418,8 +1445,8 @@ var LazyWaitForEventStep = class extends BaseLazyStep {
1418
1445
  };
1419
1446
  var LazyNotifyStep = class extends LazyFunctionStep {
1420
1447
  stepType = "Notify";
1421
- constructor(stepName, eventId, eventData, requester) {
1422
- super(stepName, async () => {
1448
+ constructor(context, stepName, eventId, eventData, requester) {
1449
+ super(context, stepName, async () => {
1423
1450
  const notifyResponse = await makeNotifyRequest(requester, eventId, eventData);
1424
1451
  return {
1425
1452
  eventId,
@@ -1444,7 +1471,7 @@ var LazyInvokeStep = class extends BaseLazyStep {
1444
1471
  * workflow id of the invoked workflow
1445
1472
  */
1446
1473
  workflowId;
1447
- constructor(stepName, {
1474
+ constructor(context, stepName, {
1448
1475
  workflow,
1449
1476
  body,
1450
1477
  headers = {},
@@ -1454,7 +1481,7 @@ var LazyInvokeStep = class extends BaseLazyStep {
1454
1481
  flowControl,
1455
1482
  stringifyBody = true
1456
1483
  }) {
1457
- super(stepName);
1484
+ super(context, stepName);
1458
1485
  this.params = {
1459
1486
  workflow,
1460
1487
  body,
@@ -1515,6 +1542,9 @@ var LazyInvokeStep = class extends BaseLazyStep {
1515
1542
  userHeaders: context.headers,
1516
1543
  invokeCount
1517
1544
  });
1545
+ context.qstashClient.http.headers?.forEach((value, key) => {
1546
+ invokerHeaders[key] = value;
1547
+ });
1518
1548
  invokerHeaders["Upstash-Workflow-Runid"] = context.workflowRunId;
1519
1549
  let invokeBody;
1520
1550
  if (this.params.stringifyBody) {
@@ -1586,6 +1616,88 @@ var LazyInvokeStep = class extends BaseLazyStep {
1586
1616
  return [result];
1587
1617
  }
1588
1618
  };
1619
+ var LazyCreateWebhookStep = class extends BaseLazyStep {
1620
+ stepType = "CreateWebhook";
1621
+ allowUndefinedOut = false;
1622
+ getPlanStep(concurrent, targetStep) {
1623
+ return {
1624
+ stepId: 0,
1625
+ stepName: this.stepName,
1626
+ stepType: this.stepType,
1627
+ concurrent,
1628
+ targetStep
1629
+ };
1630
+ }
1631
+ async getResultStep(concurrent, stepId) {
1632
+ return {
1633
+ stepId,
1634
+ stepName: this.stepName,
1635
+ stepType: this.stepType,
1636
+ out: void 0,
1637
+ concurrent
1638
+ };
1639
+ }
1640
+ getBody({ step, context }) {
1641
+ const userId = getUserIdFromToken(context.qstashClient);
1642
+ const workflowRunId = context.workflowRunId;
1643
+ const eventId = getEventId();
1644
+ const qstashUrl = getQStashUrl(this.context.qstashClient);
1645
+ return JSON.stringify({
1646
+ ...step,
1647
+ out: JSON.stringify({
1648
+ webhookUrl: `${qstashUrl}/v2/workflows/hooks/${userId}/${workflowRunId}/${eventId}`,
1649
+ eventId
1650
+ })
1651
+ });
1652
+ }
1653
+ };
1654
+ var LazyWaitForWebhookStep = class extends LazyWaitEventStep {
1655
+ stepType = "WaitForWebhook";
1656
+ allowUndefinedOut = true;
1657
+ constructor(context, stepName, webhook, timeout) {
1658
+ super(context, stepName, webhook.eventId, timeout);
1659
+ }
1660
+ safeParseOut(out) {
1661
+ const eventData = decodeBase64(out);
1662
+ const parsedEventData = BaseLazyStep.tryParsing(eventData);
1663
+ const body = parsedEventData.body;
1664
+ const parsedBody = typeof body === "string" ? decodeBase64(body) : void 0;
1665
+ const request = new Request(
1666
+ `${parsedEventData.proto}://${parsedEventData.host}${parsedEventData.url}`,
1667
+ {
1668
+ method: parsedEventData.method,
1669
+ headers: parsedEventData.header,
1670
+ body: parsedBody
1671
+ }
1672
+ );
1673
+ return {
1674
+ request,
1675
+ timeout: false
1676
+ };
1677
+ }
1678
+ handleUndefinedOut() {
1679
+ return {
1680
+ timeout: true,
1681
+ request: void 0
1682
+ };
1683
+ }
1684
+ };
1685
+ var LazyWaitForEventStep = class extends LazyWaitEventStep {
1686
+ stepType = "Wait";
1687
+ allowUndefinedOut = true;
1688
+ parseWaitForEventOut(out, waitTimeout) {
1689
+ return {
1690
+ eventData: out ? BaseLazyStep.tryParsing(decodeBase64(out)) : void 0,
1691
+ timeout: waitTimeout ?? false
1692
+ };
1693
+ }
1694
+ safeParseOut(out, step) {
1695
+ return this.parseWaitForEventOut(out, step.waitTimeout);
1696
+ }
1697
+ handleUndefinedOut(step) {
1698
+ return this.parseWaitForEventOut(void 0, step.waitTimeout);
1699
+ }
1700
+ };
1589
1701
 
1590
1702
  // src/agents/constants.ts
1591
1703
  var AGENT_NAME_HEADER = "upstash-agent-name";
@@ -1990,7 +2102,7 @@ var AutoExecutor = class _AutoExecutor {
1990
2102
  step,
1991
2103
  stepCount: this.stepCount
1992
2104
  });
1993
- return lazyStep.parseOut(step.out);
2105
+ return lazyStep.parseOut(step);
1994
2106
  }
1995
2107
  const resultStep = await submitSingleStep({
1996
2108
  context: this.context,
@@ -2077,7 +2189,7 @@ var AutoExecutor = class _AutoExecutor {
2077
2189
  const parallelResultSteps = sortedSteps.filter((step) => step.stepId >= initialStepCount).slice(0, parallelSteps.length);
2078
2190
  validateParallelSteps(parallelSteps, parallelResultSteps);
2079
2191
  return parallelResultSteps.map(
2080
- (step, index) => parallelSteps[index].parseOut(step.out)
2192
+ (step, index) => parallelSteps[index].parseOut(step)
2081
2193
  );
2082
2194
  }
2083
2195
  }
@@ -2133,7 +2245,6 @@ var AutoExecutor = class _AutoExecutor {
2133
2245
  * @param index index of the current step
2134
2246
  * @returns result[index] if lazyStepList > 1, otherwise result
2135
2247
  */
2136
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
2137
2248
  static getResult(lazyStepList, result, index) {
2138
2249
  if (lazyStepList.length === 1) {
2139
2250
  return result;
@@ -2858,7 +2969,7 @@ var WorkflowContext = class {
2858
2969
  */
2859
2970
  async run(stepName, stepFunction) {
2860
2971
  const wrappedStepFunction = () => this.executor.wrapStep(stepName, stepFunction);
2861
- return await this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2972
+ return await this.addStep(new LazyFunctionStep(this, stepName, wrappedStepFunction));
2862
2973
  }
2863
2974
  /**
2864
2975
  * Stops the execution for the duration provided.
@@ -2872,7 +2983,7 @@ var WorkflowContext = class {
2872
2983
  * @returns undefined
2873
2984
  */
2874
2985
  async sleep(stepName, duration) {
2875
- await this.addStep(new LazySleepStep(stepName, duration));
2986
+ await this.addStep(new LazySleepStep(this, stepName, duration));
2876
2987
  }
2877
2988
  /**
2878
2989
  * Stops the execution until the date time provided.
@@ -2894,13 +3005,14 @@ var WorkflowContext = class {
2894
3005
  datetime = typeof datetime === "string" ? new Date(datetime) : datetime;
2895
3006
  time = Math.round(datetime.getTime() / 1e3);
2896
3007
  }
2897
- await this.addStep(new LazySleepUntilStep(stepName, time));
3008
+ await this.addStep(new LazySleepUntilStep(this, stepName, time));
2898
3009
  }
2899
3010
  async call(stepName, settings) {
2900
3011
  let callStep;
2901
3012
  if ("workflow" in settings) {
2902
3013
  const url = getNewUrlFromWorkflowId(this.url, settings.workflow.workflowId);
2903
3014
  callStep = new LazyCallStep(
3015
+ this,
2904
3016
  stepName,
2905
3017
  url,
2906
3018
  "POST",
@@ -2925,6 +3037,7 @@ var WorkflowContext = class {
2925
3037
  stringifyBody = true
2926
3038
  } = settings;
2927
3039
  callStep = new LazyCallStep(
3040
+ this,
2928
3041
  stepName,
2929
3042
  url,
2930
3043
  method,
@@ -2976,7 +3089,9 @@ var WorkflowContext = class {
2976
3089
  async waitForEvent(stepName, eventId, options = {}) {
2977
3090
  const { timeout = "7d" } = options;
2978
3091
  const timeoutStr = typeof timeout === "string" ? timeout : `${timeout}s`;
2979
- return await this.addStep(new LazyWaitForEventStep(stepName, eventId, timeoutStr));
3092
+ return await this.addStep(
3093
+ new LazyWaitForEventStep(this, stepName, eventId, timeoutStr)
3094
+ );
2980
3095
  }
2981
3096
  /**
2982
3097
  * Notify workflow runs waiting for an event
@@ -3001,11 +3116,19 @@ var WorkflowContext = class {
3001
3116
  */
3002
3117
  async notify(stepName, eventId, eventData) {
3003
3118
  return await this.addStep(
3004
- new LazyNotifyStep(stepName, eventId, eventData, this.qstashClient.http)
3119
+ new LazyNotifyStep(this, stepName, eventId, eventData, this.qstashClient.http)
3005
3120
  );
3006
3121
  }
3007
3122
  async invoke(stepName, settings) {
3008
- return await this.addStep(new LazyInvokeStep(stepName, settings));
3123
+ return await this.addStep(
3124
+ new LazyInvokeStep(this, stepName, settings)
3125
+ );
3126
+ }
3127
+ async createWebhook(stepName) {
3128
+ return await this.addStep(new LazyCreateWebhookStep(this, stepName));
3129
+ }
3130
+ async waitForWebhook(stepName, webhook, timeout) {
3131
+ return await this.addStep(new LazyWaitForWebhookStep(this, stepName, webhook, timeout));
3009
3132
  }
3010
3133
  /**
3011
3134
  * Cancel the current workflow run
@@ -3169,13 +3292,6 @@ var processRawSteps = (rawSteps) => {
3169
3292
  const stepsToDecode = encodedSteps.filter((step) => step.callType === "step");
3170
3293
  const otherSteps = stepsToDecode.map((rawStep) => {
3171
3294
  const step = JSON.parse(decodeBase64(rawStep.body));
3172
- if (step.waitEventId) {
3173
- const newOut = {
3174
- eventData: step.out ? decodeBase64(step.out) : void 0,
3175
- timeout: step.waitTimeout ?? false
3176
- };
3177
- step.out = newOut;
3178
- }
3179
3295
  return step;
3180
3296
  });
3181
3297
  const steps = [initialStep, ...otherSteps];
package/solidjs.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  SDK_TELEMETRY,
3
3
  serveBase
4
- } from "./chunk-BON2RKOR.mjs";
4
+ } from "./chunk-GZRDB6Z5.mjs";
5
5
 
6
6
  // platforms/solidjs.ts
7
7
  var serve = (routeFunction, options) => {
package/svelte.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as _sveltejs_kit from '@sveltejs/kit';
2
2
  import { RequestHandler } from '@sveltejs/kit';
3
- import { R as RouteFunction, o as PublicServeOptions, z as InvokableWorkflow } from './types-9nCq6bRP.mjs';
4
- import { s as serveManyBase } from './serve-many-CctdYIfB.mjs';
3
+ import { R as RouteFunction, o as PublicServeOptions, z as InvokableWorkflow } from './types-BD06btU6.mjs';
4
+ import { s as serveManyBase } from './serve-many-B5Vbacm6.mjs';
5
5
  import '@upstash/qstash';
6
6
  import 'zod';
7
7
  import 'ai';
package/svelte.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as _sveltejs_kit from '@sveltejs/kit';
2
2
  import { RequestHandler } from '@sveltejs/kit';
3
- import { R as RouteFunction, o as PublicServeOptions, z as InvokableWorkflow } from './types-9nCq6bRP.js';
4
- import { s as serveManyBase } from './serve-many-BXDr30rl.js';
3
+ import { R as RouteFunction, o as PublicServeOptions, z as InvokableWorkflow } from './types-BD06btU6.js';
4
+ import { s as serveManyBase } from './serve-many-BCV7INWe.js';
5
5
  import '@upstash/qstash';
6
6
  import 'zod';
7
7
  import 'ai';