@upstash/workflow 0.2.3 → 0.2.5-agents

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.
@@ -1,34 +1,23 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
8
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
9
- }) : x)(function(x) {
10
- if (typeof require !== "undefined") return require.apply(this, arguments);
11
- throw Error('Dynamic require of "' + x + '" is not supported');
12
- });
13
- var __commonJS = (cb, mod) => function __require2() {
14
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
15
- };
16
- var __copyProps = (to, from, except, desc) => {
17
- if (from && typeof from === "object" || typeof from === "function") {
18
- for (let key of __getOwnPropNames(from))
19
- if (!__hasOwnProp.call(to, key) && key !== except)
20
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
- }
22
- return to;
23
- };
24
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
25
- // If the importer is in node compatibility mode or this is not an ESM
26
- // file that has been converted to a CommonJS file using a Babel-
27
- // compatible transform (i.e. "__esModule" has not been set), then set
28
- // "default" to the CommonJS "module.exports" for node compatibility.
29
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
30
- mod
31
- ));
1
+ import {
2
+ WorkflowAgents
3
+ } from "./chunk-PU5J4TNC.mjs";
4
+
5
+ // src/constants.ts
6
+ var WORKFLOW_ID_HEADER = "Upstash-Workflow-RunId";
7
+ var WORKFLOW_INIT_HEADER = "Upstash-Workflow-Init";
8
+ var WORKFLOW_URL_HEADER = "Upstash-Workflow-Url";
9
+ var WORKFLOW_FAILURE_HEADER = "Upstash-Workflow-Is-Failure";
10
+ var WORKFLOW_FEATURE_HEADER = "Upstash-Feature-Set";
11
+ var WORKFLOW_PROTOCOL_VERSION = "1";
12
+ var WORKFLOW_PROTOCOL_VERSION_HEADER = "Upstash-Workflow-Sdk-Version";
13
+ var DEFAULT_CONTENT_TYPE = "application/json";
14
+ var NO_CONCURRENCY = 1;
15
+ var DEFAULT_RETRIES = 3;
16
+ var VERSION = "v0.2.3";
17
+ var SDK_TELEMETRY = `@upstash/workflow@${VERSION}`;
18
+ var TELEMETRY_HEADER_SDK = "Upstash-Telemetry-Sdk";
19
+ var TELEMETRY_HEADER_FRAMEWORK = "Upstash-Telemetry-Framework";
20
+ var TELEMETRY_HEADER_RUNTIME = "Upstash-Telemetry-Runtime";
32
21
 
33
22
  // src/error.ts
34
23
  import { QstashError } from "@upstash/qstash";
@@ -738,30 +727,23 @@ var Err = class {
738
727
  };
739
728
  var fromThrowable = Result.fromThrowable;
740
729
 
741
- // src/constants.ts
742
- var WORKFLOW_ID_HEADER = "Upstash-Workflow-RunId";
743
- var WORKFLOW_INIT_HEADER = "Upstash-Workflow-Init";
744
- var WORKFLOW_URL_HEADER = "Upstash-Workflow-Url";
745
- var WORKFLOW_FAILURE_HEADER = "Upstash-Workflow-Is-Failure";
746
- var WORKFLOW_FEATURE_HEADER = "Upstash-Feature-Set";
747
- var WORKFLOW_PROTOCOL_VERSION = "1";
748
- var WORKFLOW_PROTOCOL_VERSION_HEADER = "Upstash-Workflow-Sdk-Version";
749
- var DEFAULT_CONTENT_TYPE = "application/json";
750
- var NO_CONCURRENCY = 1;
751
- var DEFAULT_RETRIES = 3;
752
-
753
730
  // src/workflow-requests.ts
754
731
  import { QstashError as QstashError3 } from "@upstash/qstash";
755
- var triggerFirstInvocation = async (workflowContext, retries, useJSONContent, debug) => {
756
- const { headers } = getHeaders(
757
- "true",
758
- workflowContext.workflowRunId,
759
- workflowContext.url,
760
- workflowContext.headers,
761
- void 0,
762
- workflowContext.failureUrl,
763
- retries
764
- );
732
+ var triggerFirstInvocation = async ({
733
+ workflowContext,
734
+ useJSONContent,
735
+ telemetry,
736
+ debug
737
+ }) => {
738
+ const { headers } = getHeaders({
739
+ initHeaderValue: "true",
740
+ workflowRunId: workflowContext.workflowRunId,
741
+ workflowUrl: workflowContext.url,
742
+ userHeaders: workflowContext.headers,
743
+ failureUrl: workflowContext.failureUrl,
744
+ retries: workflowContext.retries,
745
+ telemetry
746
+ });
765
747
  if (useJSONContent) {
766
748
  headers["content-type"] = "application/json";
767
749
  }
@@ -867,7 +849,16 @@ var recreateUserHeaders = (headers) => {
867
849
  }
868
850
  return filteredHeaders;
869
851
  };
870
- var handleThirdPartyCallResult = async (request, requestPayload, client, workflowUrl, failureUrl, retries, debug) => {
852
+ var handleThirdPartyCallResult = async ({
853
+ request,
854
+ requestPayload,
855
+ client,
856
+ workflowUrl,
857
+ failureUrl,
858
+ retries,
859
+ telemetry,
860
+ debug
861
+ }) => {
871
862
  try {
872
863
  if (request.headers.get("Upstash-Workflow-Callback")) {
873
864
  let callbackPayload;
@@ -926,15 +917,15 @@ ${atob(callbackMessage.body ?? "")}`
926
917
  );
927
918
  }
928
919
  const userHeaders = recreateUserHeaders(request.headers);
929
- const { headers: requestHeaders } = getHeaders(
930
- "false",
920
+ const { headers: requestHeaders } = getHeaders({
921
+ initHeaderValue: "false",
931
922
  workflowRunId,
932
923
  workflowUrl,
933
924
  userHeaders,
934
- void 0,
935
925
  failureUrl,
936
- retries
937
- );
926
+ retries,
927
+ telemetry
928
+ });
938
929
  const callResponse = {
939
930
  status: callbackMessage.status,
940
931
  body: atob(callbackMessage.body ?? ""),
@@ -972,12 +963,31 @@ ${atob(callbackMessage.body ?? "")}`
972
963
  );
973
964
  }
974
965
  };
975
- var getHeaders = (initHeaderValue, workflowRunId, workflowUrl, userHeaders, step, failureUrl, retries, callRetries, callTimeout) => {
966
+ var getTelemetryHeaders = (telemetry) => {
967
+ return {
968
+ [TELEMETRY_HEADER_SDK]: telemetry.sdk,
969
+ [TELEMETRY_HEADER_FRAMEWORK]: telemetry.framework,
970
+ [TELEMETRY_HEADER_RUNTIME]: telemetry.runtime ?? "unknown"
971
+ };
972
+ };
973
+ var getHeaders = ({
974
+ initHeaderValue,
975
+ workflowRunId,
976
+ workflowUrl,
977
+ userHeaders,
978
+ failureUrl,
979
+ retries,
980
+ step,
981
+ callRetries,
982
+ callTimeout,
983
+ telemetry
984
+ }) => {
976
985
  const baseHeaders = {
977
986
  [WORKFLOW_INIT_HEADER]: initHeaderValue,
978
987
  [WORKFLOW_ID_HEADER]: workflowRunId,
979
988
  [WORKFLOW_URL_HEADER]: workflowUrl,
980
- [WORKFLOW_FEATURE_HEADER]: "LazyFetch,InitialBody"
989
+ [WORKFLOW_FEATURE_HEADER]: "LazyFetch,InitialBody",
990
+ ...telemetry ? getTelemetryHeaders(telemetry) : {}
981
991
  };
982
992
  if (!step?.callUrl) {
983
993
  baseHeaders[`Upstash-Forward-${WORKFLOW_PROTOCOL_VERSION_HEADER}`] = WORKFLOW_PROTOCOL_VERSION;
@@ -1051,6 +1061,13 @@ var getHeaders = (initHeaderValue, workflowRunId, workflowUrl, userHeaders, step
1051
1061
  ...Object.fromEntries(
1052
1062
  Object.entries(baseHeaders).map(([header, value]) => [header, [value]])
1053
1063
  ),
1064
+ // to include telemetry headers:
1065
+ ...telemetry ? Object.fromEntries(
1066
+ Object.entries(getTelemetryHeaders(telemetry)).map(([header, value]) => [
1067
+ header,
1068
+ [value]
1069
+ ])
1070
+ ) : {},
1054
1071
  // note: using WORKFLOW_ID_HEADER doesn't work, because Runid -> RunId:
1055
1072
  "Upstash-Workflow-Runid": [workflowRunId],
1056
1073
  [WORKFLOW_INIT_HEADER]: ["false"],
@@ -1089,6 +1106,7 @@ If you want to disable QStash Verification, you should clear env variables QSTAS
1089
1106
  };
1090
1107
 
1091
1108
  // src/context/auto-executor.ts
1109
+ import { QstashError as QstashError4 } from "@upstash/qstash";
1092
1110
  var AutoExecutor = class _AutoExecutor {
1093
1111
  context;
1094
1112
  promises = /* @__PURE__ */ new WeakMap();
@@ -1097,13 +1115,15 @@ var AutoExecutor = class _AutoExecutor {
1097
1115
  nonPlanStepCount;
1098
1116
  steps;
1099
1117
  indexInCurrentList = 0;
1118
+ telemetry;
1100
1119
  stepCount = 0;
1101
1120
  planStepCount = 0;
1102
1121
  executingStep = false;
1103
- constructor(context, steps, debug) {
1122
+ constructor(context, steps, telemetry, debug) {
1104
1123
  this.context = context;
1105
- this.debug = debug;
1106
1124
  this.steps = steps;
1125
+ this.telemetry = telemetry;
1126
+ this.debug = debug;
1107
1127
  this.nonPlanStepCount = this.steps.filter((step) => !step.targetStep).length;
1108
1128
  }
1109
1129
  /**
@@ -1246,7 +1266,7 @@ var AutoExecutor = class _AutoExecutor {
1246
1266
  );
1247
1267
  await this.submitStepsToQStash([resultStep], [parallelStep]);
1248
1268
  } catch (error) {
1249
- if (error instanceof WorkflowAbort) {
1269
+ if (error instanceof WorkflowAbort || error instanceof QstashError4 && error.status === 400) {
1250
1270
  throw error;
1251
1271
  }
1252
1272
  throw new WorkflowError(
@@ -1317,15 +1337,16 @@ var AutoExecutor = class _AutoExecutor {
1317
1337
  });
1318
1338
  if (steps[0].waitEventId && steps.length === 1) {
1319
1339
  const waitStep = steps[0];
1320
- const { headers, timeoutHeaders } = getHeaders(
1321
- "false",
1322
- this.context.workflowRunId,
1323
- this.context.url,
1324
- this.context.headers,
1325
- waitStep,
1326
- this.context.failureUrl,
1327
- this.context.retries
1328
- );
1340
+ const { headers, timeoutHeaders } = getHeaders({
1341
+ initHeaderValue: "false",
1342
+ workflowRunId: this.context.workflowRunId,
1343
+ workflowUrl: this.context.url,
1344
+ userHeaders: this.context.headers,
1345
+ step: waitStep,
1346
+ failureUrl: this.context.failureUrl,
1347
+ retries: this.context.retries,
1348
+ telemetry: this.telemetry
1349
+ });
1329
1350
  const waitBody = {
1330
1351
  url: this.context.url,
1331
1352
  timeout: waitStep.timeout,
@@ -1352,17 +1373,18 @@ var AutoExecutor = class _AutoExecutor {
1352
1373
  const result = await this.context.qstashClient.batchJSON(
1353
1374
  steps.map((singleStep, index) => {
1354
1375
  const lazyStep = lazySteps[index];
1355
- const { headers } = getHeaders(
1356
- "false",
1357
- this.context.workflowRunId,
1358
- this.context.url,
1359
- this.context.headers,
1360
- singleStep,
1361
- this.context.failureUrl,
1362
- this.context.retries,
1363
- lazyStep instanceof LazyCallStep ? lazyStep.retries : void 0,
1364
- lazyStep instanceof LazyCallStep ? lazyStep.timeout : void 0
1365
- );
1376
+ const { headers } = getHeaders({
1377
+ initHeaderValue: "false",
1378
+ workflowRunId: this.context.workflowRunId,
1379
+ workflowUrl: this.context.url,
1380
+ userHeaders: this.context.headers,
1381
+ step: singleStep,
1382
+ failureUrl: this.context.failureUrl,
1383
+ retries: this.context.retries,
1384
+ callRetries: lazyStep instanceof LazyCallStep ? lazyStep.retries : void 0,
1385
+ callTimeout: lazyStep instanceof LazyCallStep ? lazyStep.timeout : void 0,
1386
+ telemetry: this.telemetry
1387
+ });
1366
1388
  const willWait = singleStep.concurrent === NO_CONCURRENCY || singleStep.stepId === 0;
1367
1389
  singleStep.out = JSON.stringify(singleStep.out);
1368
1390
  return singleStep.callUrl ? (
@@ -1727,7 +1749,8 @@ var WorkflowContext = class {
1727
1749
  debug,
1728
1750
  initialPayload,
1729
1751
  env,
1730
- retries
1752
+ retries,
1753
+ telemetry
1731
1754
  }) {
1732
1755
  this.qstashClient = qstashClient;
1733
1756
  this.workflowRunId = workflowRunId;
@@ -1738,7 +1761,7 @@ var WorkflowContext = class {
1738
1761
  this.requestPayload = initialPayload;
1739
1762
  this.env = env ?? {};
1740
1763
  this.retries = retries ?? DEFAULT_RETRIES;
1741
- this.executor = new AutoExecutor(this, this.steps, debug);
1764
+ this.executor = new AutoExecutor(this, this.steps, telemetry, debug);
1742
1765
  }
1743
1766
  /**
1744
1767
  * Executes a workflow step
@@ -1979,6 +2002,11 @@ var WorkflowContext = class {
1979
2002
  context: this
1980
2003
  });
1981
2004
  }
2005
+ get agents() {
2006
+ return new WorkflowAgents({
2007
+ context: this
2008
+ });
2009
+ }
1982
2010
  };
1983
2011
 
1984
2012
  // src/logger.ts
@@ -2183,6 +2211,7 @@ var checkIfLastOneIsDuplicate = async (steps, debug) => {
2183
2211
  if (step.stepId === lastStepId && step.targetStep === lastTargetStepId) {
2184
2212
  const message = `Upstash Workflow: The step '${step.stepName}' with id '${step.stepId}' has run twice during workflow execution. Rest of the workflow will continue running as usual.`;
2185
2213
  await debug?.log("WARN", "RESPONSE_DEFAULT", message);
2214
+ console.log(steps);
2186
2215
  console.warn(message);
2187
2216
  return true;
2188
2217
  }
@@ -2251,7 +2280,7 @@ var parseRequest = async (requestPayload, isFirstInvocation, workflowRunId, requ
2251
2280
  };
2252
2281
  }
2253
2282
  };
2254
- var handleFailure = async (request, requestPayload, qstashClient, initialPayloadParser, routeFunction, failureFunction, debug) => {
2283
+ var handleFailure = async (request, requestPayload, qstashClient, initialPayloadParser, routeFunction, failureFunction, env, retries, debug) => {
2255
2284
  if (request.headers.get(WORKFLOW_FAILURE_HEADER) !== "true") {
2256
2285
  return ok("not-failure-callback");
2257
2286
  }
@@ -2276,7 +2305,11 @@ var handleFailure = async (request, requestPayload, qstashClient, initialPayload
2276
2305
  steps: [],
2277
2306
  url,
2278
2307
  failureUrl: url,
2279
- debug
2308
+ debug,
2309
+ env,
2310
+ retries,
2311
+ telemetry: void 0
2312
+ // not going to make requests in authentication check
2280
2313
  });
2281
2314
  const authCheck = await DisabledWorkflowContext.tryAuthentication(
2282
2315
  routeFunction,
@@ -2351,6 +2384,7 @@ var processOptions = (options) => {
2351
2384
  env: environment,
2352
2385
  retries: DEFAULT_RETRIES,
2353
2386
  useJSONContent: false,
2387
+ disableTelemetry: false,
2354
2388
  ...options
2355
2389
  };
2356
2390
  };
@@ -2385,7 +2419,7 @@ var determineUrls = async (request, url, baseUrl, failureFunction, failureUrl, d
2385
2419
  var AUTH_FAIL_MESSAGE = `Failed to authenticate Workflow request. If this is unexpected, see the caveat https://upstash.com/docs/workflow/basics/caveats#avoid-non-deterministic-code-outside-context-run`;
2386
2420
 
2387
2421
  // src/serve/index.ts
2388
- var serveBase = (routeFunction, options) => {
2422
+ var serveBase = (routeFunction, telemetry, options) => {
2389
2423
  const {
2390
2424
  qstashClient,
2391
2425
  onStepFinish,
@@ -2398,8 +2432,10 @@ var serveBase = (routeFunction, options) => {
2398
2432
  baseUrl,
2399
2433
  env,
2400
2434
  retries,
2401
- useJSONContent
2435
+ useJSONContent,
2436
+ disableTelemetry
2402
2437
  } = processOptions(options);
2438
+ telemetry = disableTelemetry ? void 0 : telemetry;
2403
2439
  const debug = WorkflowLogger.getLogger(verbose);
2404
2440
  const handler = async (request) => {
2405
2441
  await debug?.log("INFO", "ENDPOINT_START");
@@ -2435,7 +2471,10 @@ var serveBase = (routeFunction, options) => {
2435
2471
  qstashClient,
2436
2472
  initialPayloadParser,
2437
2473
  routeFunction,
2438
- failureFunction
2474
+ failureFunction,
2475
+ env,
2476
+ retries,
2477
+ debug
2439
2478
  );
2440
2479
  if (failureCheck.isErr()) {
2441
2480
  throw failureCheck.error;
@@ -2453,7 +2492,8 @@ var serveBase = (routeFunction, options) => {
2453
2492
  failureUrl: workflowFailureUrl,
2454
2493
  debug,
2455
2494
  env,
2456
- retries
2495
+ retries,
2496
+ telemetry
2457
2497
  });
2458
2498
  const authCheck = await DisabledWorkflowContext.tryAuthentication(
2459
2499
  routeFunction,
@@ -2469,22 +2509,23 @@ var serveBase = (routeFunction, options) => {
2469
2509
  "auth-fail"
2470
2510
  );
2471
2511
  }
2472
- const callReturnCheck = await handleThirdPartyCallResult(
2512
+ const callReturnCheck = await handleThirdPartyCallResult({
2473
2513
  request,
2474
- rawInitialPayload,
2475
- qstashClient,
2514
+ requestPayload: rawInitialPayload,
2515
+ client: qstashClient,
2476
2516
  workflowUrl,
2477
- workflowFailureUrl,
2517
+ failureUrl: workflowFailureUrl,
2478
2518
  retries,
2519
+ telemetry,
2479
2520
  debug
2480
- );
2521
+ });
2481
2522
  if (callReturnCheck.isErr()) {
2482
2523
  await debug?.log("ERROR", "SUBMIT_THIRD_PARTY_RESULT", {
2483
2524
  error: callReturnCheck.error.message
2484
2525
  });
2485
2526
  throw callReturnCheck.error;
2486
2527
  } else if (callReturnCheck.value === "continue-workflow") {
2487
- const result = isFirstInvocation ? await triggerFirstInvocation(workflowContext, retries, useJSONContent, debug) : await triggerRouteFunction({
2528
+ const result = isFirstInvocation ? await triggerFirstInvocation({ workflowContext, useJSONContent, telemetry, debug }) : await triggerRouteFunction({
2488
2529
  onStep: async () => routeFunction(workflowContext),
2489
2530
  onCleanup: async () => {
2490
2531
  await triggerWorkflowDelete(workflowContext, debug);
@@ -2519,18 +2560,22 @@ var serveBase = (routeFunction, options) => {
2519
2560
  return { handler: safeHandler };
2520
2561
  };
2521
2562
  var serve = (routeFunction, options) => {
2522
- return serveBase(routeFunction, options);
2563
+ return serveBase(
2564
+ routeFunction,
2565
+ {
2566
+ sdk: SDK_TELEMETRY,
2567
+ framework: "unknown"
2568
+ },
2569
+ options
2570
+ );
2523
2571
  };
2524
2572
 
2525
2573
  export {
2526
- __require,
2527
- __commonJS,
2528
- __toESM,
2529
2574
  makeNotifyRequest,
2530
2575
  makeGetWaitersRequest,
2576
+ SDK_TELEMETRY,
2531
2577
  WorkflowError,
2532
2578
  WorkflowAbort,
2533
- DEFAULT_RETRIES,
2534
2579
  StepTypes,
2535
2580
  triggerFirstInvocation,
2536
2581
  WorkflowContext,
package/cloudflare.d.mts CHANGED
@@ -1,5 +1,8 @@
1
- import { R as RouteFunction, j as PublicServeOptions } from './types-R9q4MUwl.mjs';
1
+ import { R as RouteFunction, j as PublicServeOptions } from './types-BEyIoCRe.mjs';
2
2
  import '@upstash/qstash';
3
+ import 'ai';
4
+ import '@ai-sdk/openai';
5
+ import 'langchain/tools';
3
6
 
4
7
  type WorkflowBindings = {
5
8
  QSTASH_TOKEN: string;
package/cloudflare.d.ts CHANGED
@@ -1,5 +1,8 @@
1
- import { R as RouteFunction, j as PublicServeOptions } from './types-R9q4MUwl.js';
1
+ import { R as RouteFunction, j as PublicServeOptions } from './types-BEyIoCRe.js';
2
2
  import '@upstash/qstash';
3
+ import 'ai';
4
+ import '@ai-sdk/openai';
5
+ import 'langchain/tools';
3
6
 
4
7
  type WorkflowBindings = {
5
8
  QSTASH_TOKEN: string;