@upstash/workflow 0.2.22 → 0.2.23-rc

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,91 @@ 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 out = [userId, context.workflowRunId, getEventId()].join("/");
1643
+ return JSON.stringify({
1644
+ ...step,
1645
+ out
1646
+ });
1647
+ }
1648
+ safeParseOut(out) {
1649
+ const [userId, workflowRunId, eventId] = out.split("/");
1650
+ const qstashUrl = getQStashUrl(this.context.qstashClient);
1651
+ return {
1652
+ webhookUrl: `${qstashUrl}/v2/workflows/hooks/${userId}/${workflowRunId}/${eventId}`,
1653
+ eventId
1654
+ };
1655
+ }
1656
+ };
1657
+ var LazyWaitForWebhookStep = class extends LazyWaitEventStep {
1658
+ stepType = "WaitForWebhook";
1659
+ allowUndefinedOut = true;
1660
+ constructor(context, stepName, webhook, timeout) {
1661
+ super(context, stepName, webhook.eventId, timeout);
1662
+ }
1663
+ safeParseOut(out) {
1664
+ const eventData = decodeBase64(out);
1665
+ const parsedEventData = BaseLazyStep.tryParsing(eventData);
1666
+ const body = parsedEventData.body;
1667
+ const parsedBody = typeof body === "string" ? decodeBase64(body) : void 0;
1668
+ const request = new Request(
1669
+ `${parsedEventData.proto}://${parsedEventData.host}${parsedEventData.url}`,
1670
+ {
1671
+ method: parsedEventData.method,
1672
+ headers: parsedEventData.header,
1673
+ body: parsedBody
1674
+ }
1675
+ );
1676
+ return {
1677
+ request,
1678
+ timeout: false
1679
+ };
1680
+ }
1681
+ handleUndefinedOut() {
1682
+ return {
1683
+ timeout: true,
1684
+ request: void 0
1685
+ };
1686
+ }
1687
+ };
1688
+ var LazyWaitForEventStep = class extends LazyWaitEventStep {
1689
+ stepType = "Wait";
1690
+ allowUndefinedOut = true;
1691
+ parseWaitForEventOut(out, waitTimeout) {
1692
+ return {
1693
+ eventData: out ? BaseLazyStep.tryParsing(decodeBase64(out)) : void 0,
1694
+ timeout: waitTimeout ?? false
1695
+ };
1696
+ }
1697
+ safeParseOut(out, step) {
1698
+ return this.parseWaitForEventOut(out, step.waitTimeout);
1699
+ }
1700
+ handleUndefinedOut(step) {
1701
+ return this.parseWaitForEventOut(void 0, step.waitTimeout);
1702
+ }
1703
+ };
1589
1704
 
1590
1705
  // src/agents/constants.ts
1591
1706
  var AGENT_NAME_HEADER = "upstash-agent-name";
@@ -1990,7 +2105,7 @@ var AutoExecutor = class _AutoExecutor {
1990
2105
  step,
1991
2106
  stepCount: this.stepCount
1992
2107
  });
1993
- return lazyStep.parseOut(step.out);
2108
+ return lazyStep.parseOut(step);
1994
2109
  }
1995
2110
  const resultStep = await submitSingleStep({
1996
2111
  context: this.context,
@@ -2077,7 +2192,7 @@ var AutoExecutor = class _AutoExecutor {
2077
2192
  const parallelResultSteps = sortedSteps.filter((step) => step.stepId >= initialStepCount).slice(0, parallelSteps.length);
2078
2193
  validateParallelSteps(parallelSteps, parallelResultSteps);
2079
2194
  return parallelResultSteps.map(
2080
- (step, index) => parallelSteps[index].parseOut(step.out)
2195
+ (step, index) => parallelSteps[index].parseOut(step)
2081
2196
  );
2082
2197
  }
2083
2198
  }
@@ -2133,7 +2248,6 @@ var AutoExecutor = class _AutoExecutor {
2133
2248
  * @param index index of the current step
2134
2249
  * @returns result[index] if lazyStepList > 1, otherwise result
2135
2250
  */
2136
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
2137
2251
  static getResult(lazyStepList, result, index) {
2138
2252
  if (lazyStepList.length === 1) {
2139
2253
  return result;
@@ -2858,7 +2972,7 @@ var WorkflowContext = class {
2858
2972
  */
2859
2973
  async run(stepName, stepFunction) {
2860
2974
  const wrappedStepFunction = () => this.executor.wrapStep(stepName, stepFunction);
2861
- return await this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2975
+ return await this.addStep(new LazyFunctionStep(this, stepName, wrappedStepFunction));
2862
2976
  }
2863
2977
  /**
2864
2978
  * Stops the execution for the duration provided.
@@ -2872,7 +2986,7 @@ var WorkflowContext = class {
2872
2986
  * @returns undefined
2873
2987
  */
2874
2988
  async sleep(stepName, duration) {
2875
- await this.addStep(new LazySleepStep(stepName, duration));
2989
+ await this.addStep(new LazySleepStep(this, stepName, duration));
2876
2990
  }
2877
2991
  /**
2878
2992
  * Stops the execution until the date time provided.
@@ -2894,13 +3008,14 @@ var WorkflowContext = class {
2894
3008
  datetime = typeof datetime === "string" ? new Date(datetime) : datetime;
2895
3009
  time = Math.round(datetime.getTime() / 1e3);
2896
3010
  }
2897
- await this.addStep(new LazySleepUntilStep(stepName, time));
3011
+ await this.addStep(new LazySleepUntilStep(this, stepName, time));
2898
3012
  }
2899
3013
  async call(stepName, settings) {
2900
3014
  let callStep;
2901
3015
  if ("workflow" in settings) {
2902
3016
  const url = getNewUrlFromWorkflowId(this.url, settings.workflow.workflowId);
2903
3017
  callStep = new LazyCallStep(
3018
+ this,
2904
3019
  stepName,
2905
3020
  url,
2906
3021
  "POST",
@@ -2925,6 +3040,7 @@ var WorkflowContext = class {
2925
3040
  stringifyBody = true
2926
3041
  } = settings;
2927
3042
  callStep = new LazyCallStep(
3043
+ this,
2928
3044
  stepName,
2929
3045
  url,
2930
3046
  method,
@@ -2976,7 +3092,9 @@ var WorkflowContext = class {
2976
3092
  async waitForEvent(stepName, eventId, options = {}) {
2977
3093
  const { timeout = "7d" } = options;
2978
3094
  const timeoutStr = typeof timeout === "string" ? timeout : `${timeout}s`;
2979
- return await this.addStep(new LazyWaitForEventStep(stepName, eventId, timeoutStr));
3095
+ return await this.addStep(
3096
+ new LazyWaitForEventStep(this, stepName, eventId, timeoutStr)
3097
+ );
2980
3098
  }
2981
3099
  /**
2982
3100
  * Notify workflow runs waiting for an event
@@ -3001,11 +3119,19 @@ var WorkflowContext = class {
3001
3119
  */
3002
3120
  async notify(stepName, eventId, eventData) {
3003
3121
  return await this.addStep(
3004
- new LazyNotifyStep(stepName, eventId, eventData, this.qstashClient.http)
3122
+ new LazyNotifyStep(this, stepName, eventId, eventData, this.qstashClient.http)
3005
3123
  );
3006
3124
  }
3007
3125
  async invoke(stepName, settings) {
3008
- return await this.addStep(new LazyInvokeStep(stepName, settings));
3126
+ return await this.addStep(
3127
+ new LazyInvokeStep(this, stepName, settings)
3128
+ );
3129
+ }
3130
+ async createWebhook(stepName) {
3131
+ return await this.addStep(new LazyCreateWebhookStep(this, stepName));
3132
+ }
3133
+ async waitForWebhook(stepName, webhook, timeout) {
3134
+ return await this.addStep(new LazyWaitForWebhookStep(this, stepName, webhook, timeout));
3009
3135
  }
3010
3136
  /**
3011
3137
  * Cancel the current workflow run
@@ -3169,13 +3295,6 @@ var processRawSteps = (rawSteps) => {
3169
3295
  const stepsToDecode = encodedSteps.filter((step) => step.callType === "step");
3170
3296
  const otherSteps = stepsToDecode.map((rawStep) => {
3171
3297
  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
3298
  return step;
3180
3299
  });
3181
3300
  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-KAGTWBLF.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';