@trigger.dev/sdk 2.1.7 → 2.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
9
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
9
10
  var __export = (target, all) => {
10
11
  for (var name in all)
@@ -23,6 +24,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
24
  mod
24
25
  ));
25
26
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var __publicField = (obj, key, value) => {
28
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
29
+ return value;
30
+ };
26
31
  var __accessCheck = (obj, member, msg) => {
27
32
  if (!member.has(obj))
28
33
  throw TypeError("Cannot " + msg);
@@ -189,17 +194,21 @@ var ApiClient = class {
189
194
  }
190
195
  return await response.json();
191
196
  }
192
- async runTask(runId, task) {
197
+ async runTask(runId, task, options = {}) {
193
198
  const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
194
199
  __privateGet(this, _logger).debug("Running Task", {
195
200
  task
196
201
  });
197
- return await zodfetch(import_core.ServerTaskSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/tasks`, {
202
+ return await zodfetchWithVersions({
203
+ [import_core.API_VERSIONS.LAZY_LOADED_CACHED_TASKS]: import_core.RunTaskResponseWithCachedTasksBodySchema
204
+ }, import_core.ServerTaskSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/tasks`, {
198
205
  method: "POST",
199
206
  headers: {
200
207
  "Content-Type": "application/json",
201
208
  Authorization: `Bearer ${apiKey}`,
202
- "Idempotency-Key": task.idempotencyKey
209
+ "Idempotency-Key": task.idempotencyKey,
210
+ "X-Cached-Tasks-Cursor": options.cachedTasksCursor ?? "",
211
+ "Trigger-Version": import_core.API_VERSIONS.LAZY_LOADED_CACHED_TASKS
203
212
  },
204
213
  body: JSON.stringify(task)
205
214
  });
@@ -385,6 +394,19 @@ var ApiClient = class {
385
394
  }
386
395
  });
387
396
  }
397
+ async cancelRun(runId) {
398
+ const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
399
+ __privateGet(this, _logger).debug("Cancelling Run", {
400
+ runId
401
+ });
402
+ return await zodfetch(import_core.GetRunSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/cancel`, {
403
+ method: "POST",
404
+ headers: {
405
+ "Content-Type": "application/json",
406
+ Authorization: `Bearer ${apiKey}`
407
+ }
408
+ });
409
+ }
388
410
  async getRunStatuses(runId) {
389
411
  const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
390
412
  __privateGet(this, _logger).debug("Getting Run statuses", {
@@ -444,6 +466,36 @@ function getApiKey(key) {
444
466
  };
445
467
  }
446
468
  __name(getApiKey, "getApiKey");
469
+ async function zodfetchWithVersions(versionedSchemaMap, unversionedSchema, url, requestInit, options) {
470
+ const response = await (0, import_node_fetch.default)(url, requestInit);
471
+ if ((!requestInit || requestInit.method === "GET") && response.status === 404 && options?.optional) {
472
+ return;
473
+ }
474
+ if (response.status >= 400 && response.status < 500) {
475
+ const body = await response.json();
476
+ throw new Error(body.error);
477
+ }
478
+ if (response.status !== 200) {
479
+ throw new Error(options?.errorMessage ?? `Failed to fetch ${url}, got status code ${response.status}`);
480
+ }
481
+ const jsonBody = await response.json();
482
+ const version = response.headers.get("trigger-version");
483
+ if (!version) {
484
+ return {
485
+ version: "unversioned",
486
+ body: unversionedSchema.parse(jsonBody)
487
+ };
488
+ }
489
+ const versionedSchema = versionedSchemaMap[version];
490
+ if (!versionedSchema) {
491
+ throw new Error(`Unknown version ${version}`);
492
+ }
493
+ return {
494
+ version,
495
+ body: versionedSchema.parse(jsonBody)
496
+ };
497
+ }
498
+ __name(zodfetchWithVersions, "zodfetchWithVersions");
447
499
  async function zodfetch(schema, url, requestInit, options) {
448
500
  const response = await (0, import_node_fetch.default)(url, requestInit);
449
501
  if ((!requestInit || requestInit.method === "GET") && response.status === 404 && options?.optional) {
@@ -482,6 +534,12 @@ var CanceledWithTaskError = class {
482
534
  }
483
535
  };
484
536
  __name(CanceledWithTaskError, "CanceledWithTaskError");
537
+ var YieldExecutionError = class {
538
+ constructor(key) {
539
+ this.key = key;
540
+ }
541
+ };
542
+ __name(YieldExecutionError, "YieldExecutionError");
485
543
  var ParsedPayloadSchemaError = class {
486
544
  constructor(schemaErrors) {
487
545
  this.schemaErrors = schemaErrors;
@@ -489,7 +547,7 @@ var ParsedPayloadSchemaError = class {
489
547
  };
490
548
  __name(ParsedPayloadSchemaError, "ParsedPayloadSchemaError");
491
549
  function isTriggerError(err) {
492
- return err instanceof ResumeWithTaskError || err instanceof RetryWithTaskError || err instanceof CanceledWithTaskError;
550
+ return err instanceof ResumeWithTaskError || err instanceof RetryWithTaskError || err instanceof CanceledWithTaskError || err instanceof YieldExecutionError;
493
551
  }
494
552
  __name(isTriggerError, "isTriggerError");
495
553
 
@@ -549,6 +607,7 @@ var _addToCachedTasks, addToCachedTasks_fn;
549
607
  var IO = class {
550
608
  constructor(options) {
551
609
  __privateAdd(this, _addToCachedTasks);
610
+ __publicField(this, "brb", this.yield.bind(this));
552
611
  this._id = options.id;
553
612
  this._apiClient = options.apiClient;
554
613
  this._triggerClient = options.client;
@@ -556,13 +615,32 @@ var IO = class {
556
615
  this._cachedTasks = /* @__PURE__ */ new Map();
557
616
  this._jobLogger = options.jobLogger;
558
617
  this._jobLogLevel = options.jobLogLevel;
618
+ this._stats = {
619
+ initialCachedTasks: 0,
620
+ lazyLoadedCachedTasks: 0,
621
+ executedTasks: 0,
622
+ cachedTaskHits: 0,
623
+ cachedTaskMisses: 0,
624
+ noopCachedTaskHits: 0,
625
+ noopCachedTaskMisses: 0
626
+ };
559
627
  if (options.cachedTasks) {
560
628
  options.cachedTasks.forEach((task) => {
561
629
  this._cachedTasks.set(task.idempotencyKey, task);
562
630
  });
631
+ this._stats.initialCachedTasks = options.cachedTasks.length;
563
632
  }
564
633
  this._taskStorage = new import_node_async_hooks.AsyncLocalStorage();
565
634
  this._context = options.context;
635
+ this._yieldedExecutions = options.yieldedExecutions ?? [];
636
+ if (options.noopTasksSet) {
637
+ this._noopTasksBloomFilter = import_core3.BloomFilter.deserialize(options.noopTasksSet, import_core3.BloomFilter.NOOP_TASK_SET_SIZE);
638
+ }
639
+ this._cachedTasksCursor = options.cachedTasksCursor;
640
+ this._serverVersion = options.serverVersion ?? "unversioned";
641
+ }
642
+ get stats() {
643
+ return this._stats;
566
644
  }
567
645
  get runId() {
568
646
  return this._id;
@@ -573,38 +651,38 @@ var IO = class {
573
651
  get logger() {
574
652
  return new IOLogger(async (level, message, data) => {
575
653
  let logLevel = "info";
576
- switch (level) {
577
- case "LOG": {
578
- this._jobLogger?.log(message, data);
579
- logLevel = "log";
580
- break;
581
- }
582
- case "DEBUG": {
583
- this._jobLogger?.debug(message, data);
584
- logLevel = "debug";
585
- break;
586
- }
587
- case "INFO": {
588
- this._jobLogger?.info(message, data);
589
- logLevel = "info";
590
- break;
591
- }
592
- case "WARN": {
593
- this._jobLogger?.warn(message, data);
594
- logLevel = "warn";
595
- break;
596
- }
597
- case "ERROR": {
598
- this._jobLogger?.error(message, data);
599
- logLevel = "error";
600
- break;
601
- }
602
- }
603
654
  if (import_core3.Logger.satisfiesLogLevel(logLevel, this._jobLogLevel)) {
604
655
  await this.runTask([
605
656
  message,
606
657
  level
607
658
  ], async (task) => {
659
+ switch (level) {
660
+ case "LOG": {
661
+ this._jobLogger?.log(message, data);
662
+ logLevel = "log";
663
+ break;
664
+ }
665
+ case "DEBUG": {
666
+ this._jobLogger?.debug(message, data);
667
+ logLevel = "debug";
668
+ break;
669
+ }
670
+ case "INFO": {
671
+ this._jobLogger?.info(message, data);
672
+ logLevel = "info";
673
+ break;
674
+ }
675
+ case "WARN": {
676
+ this._jobLogger?.warn(message, data);
677
+ logLevel = "warn";
678
+ break;
679
+ }
680
+ case "ERROR": {
681
+ this._jobLogger?.error(message, data);
682
+ logLevel = "error";
683
+ break;
684
+ }
685
+ }
608
686
  }, {
609
687
  name: "log",
610
688
  icon: "log",
@@ -889,18 +967,42 @@ var IO = class {
889
967
  const cachedTask = this._cachedTasks.get(idempotencyKey);
890
968
  if (cachedTask && cachedTask.status === "COMPLETED") {
891
969
  this._logger.debug("Using completed cached task", {
892
- idempotencyKey,
893
- cachedTask
970
+ idempotencyKey
894
971
  });
972
+ this._stats.cachedTaskHits++;
895
973
  return cachedTask.output;
896
974
  }
897
- const task = await this._apiClient.runTask(this._id, {
975
+ if (options?.noop && this._noopTasksBloomFilter) {
976
+ if (this._noopTasksBloomFilter.test(idempotencyKey)) {
977
+ this._logger.debug("task idempotency key exists in noopTasksBloomFilter", {
978
+ idempotencyKey
979
+ });
980
+ this._stats.noopCachedTaskHits++;
981
+ return {};
982
+ }
983
+ }
984
+ const response = await this._apiClient.runTask(this._id, {
898
985
  idempotencyKey,
899
- displayKey: typeof key === "string" ? key : key.join("."),
986
+ displayKey: typeof key === "string" ? key : void 0,
900
987
  noop: false,
901
988
  ...options ?? {},
902
989
  parentId
990
+ }, {
991
+ cachedTasksCursor: this._cachedTasksCursor
903
992
  });
993
+ const task = response.version === import_core3.API_VERSIONS.LAZY_LOADED_CACHED_TASKS ? response.body.task : response.body;
994
+ if (response.version === import_core3.API_VERSIONS.LAZY_LOADED_CACHED_TASKS) {
995
+ this._cachedTasksCursor = response.body.cachedTasks?.cursor;
996
+ for (const cachedTask2 of response.body.cachedTasks?.tasks ?? []) {
997
+ if (!this._cachedTasks.has(cachedTask2.idempotencyKey)) {
998
+ this._cachedTasks.set(cachedTask2.idempotencyKey, cachedTask2);
999
+ this._logger.debug("Injecting lazy loaded task into task cache", {
1000
+ idempotencyKey: cachedTask2.idempotencyKey
1001
+ });
1002
+ this._stats.lazyLoadedCachedTasks++;
1003
+ }
1004
+ }
1005
+ }
904
1006
  if (task.status === "CANCELED") {
905
1007
  this._logger.debug("Task canceled", {
906
1008
  idempotencyKey,
@@ -909,11 +1011,18 @@ var IO = class {
909
1011
  throw new CanceledWithTaskError(task);
910
1012
  }
911
1013
  if (task.status === "COMPLETED") {
912
- this._logger.debug("Using task output", {
913
- idempotencyKey,
914
- task
915
- });
916
- __privateMethod(this, _addToCachedTasks, addToCachedTasks_fn).call(this, task);
1014
+ if (task.noop) {
1015
+ this._logger.debug("Noop Task completed", {
1016
+ idempotencyKey
1017
+ });
1018
+ this._noopTasksBloomFilter?.add(task.idempotencyKey);
1019
+ } else {
1020
+ this._logger.debug("Cache miss", {
1021
+ idempotencyKey
1022
+ });
1023
+ this._stats.cachedTaskMisses++;
1024
+ __privateMethod(this, _addToCachedTasks, addToCachedTasks_fn).call(this, task);
1025
+ }
917
1026
  return task.output;
918
1027
  }
919
1028
  if (task.status === "ERRORED") {
@@ -923,23 +1032,16 @@ var IO = class {
923
1032
  });
924
1033
  throw new Error(task.error ?? task?.output ? JSON.stringify(task.output) : "Task errored");
925
1034
  }
926
- if (task.status === "WAITING") {
927
- this._logger.debug("Task waiting", {
928
- idempotencyKey,
929
- task
930
- });
931
- throw new ResumeWithTaskError(task);
932
- }
933
- if (task.status === "RUNNING" && typeof task.operation === "string") {
934
- this._logger.debug("Task running operation", {
935
- idempotencyKey,
936
- task
937
- });
938
- throw new ResumeWithTaskError(task);
939
- }
940
1035
  const executeTask = /* @__PURE__ */ __name(async () => {
941
1036
  try {
942
1037
  const result = await callback(task, this);
1038
+ if (task.status === "WAITING" && task.callbackUrl) {
1039
+ this._logger.debug("Waiting for remote callback", {
1040
+ idempotencyKey,
1041
+ task
1042
+ });
1043
+ return {};
1044
+ }
943
1045
  const output = import_core3.SerializableJsonSchema.parse(result);
944
1046
  this._logger.debug("Completing using output", {
945
1047
  idempotencyKey,
@@ -949,6 +1051,7 @@ var IO = class {
949
1051
  output: output ?? void 0,
950
1052
  properties: task.outputProperties ?? void 0
951
1053
  });
1054
+ this._stats.executedTasks++;
952
1055
  if (completedTask.status === "CANCELED") {
953
1056
  throw new CanceledWithTaskError(completedTask);
954
1057
  }
@@ -1005,10 +1108,39 @@ var IO = class {
1005
1108
  throw error;
1006
1109
  }
1007
1110
  }, "executeTask");
1111
+ if (task.status === "WAITING") {
1112
+ this._logger.debug("Task waiting", {
1113
+ idempotencyKey,
1114
+ task
1115
+ });
1116
+ if (task.callbackUrl) {
1117
+ await this._taskStorage.run({
1118
+ taskId: task.id
1119
+ }, executeTask);
1120
+ }
1121
+ throw new ResumeWithTaskError(task);
1122
+ }
1123
+ if (task.status === "RUNNING" && typeof task.operation === "string") {
1124
+ this._logger.debug("Task running operation", {
1125
+ idempotencyKey,
1126
+ task
1127
+ });
1128
+ throw new ResumeWithTaskError(task);
1129
+ }
1008
1130
  return this._taskStorage.run({
1009
1131
  taskId: task.id
1010
1132
  }, executeTask);
1011
1133
  }
1134
+ yield(key) {
1135
+ if (!(0, import_core3.supportsFeature)("yieldExecution", this._serverVersion)) {
1136
+ console.warn("[trigger.dev] io.yield() is not support by the version of the Trigger.dev server you are using, you will need to upgrade your self-hosted Trigger.dev instance.");
1137
+ return;
1138
+ }
1139
+ if (this._yieldedExecutions.includes(key)) {
1140
+ return;
1141
+ }
1142
+ throw new YieldExecutionError(key);
1143
+ }
1012
1144
  async try(tryCallback, catchCallback) {
1013
1145
  try {
1014
1146
  return await tryCallback();
@@ -1483,7 +1615,7 @@ var registerSourceEvent = {
1483
1615
  icon: "register-source",
1484
1616
  parsePayload: import_core7.RegisterSourceEventSchemaV2.parse
1485
1617
  };
1486
- var _options4, _registeredJobs, _registeredSources, _registeredHttpSourceHandlers, _registeredDynamicTriggers, _jobMetadataByDynamicTriggers, _registeredSchedules, _authResolvers, _client2, _internalLogger, _preprocessRun, preprocessRun_fn, _executeJob, executeJob_fn, _createRunContext, createRunContext_fn, _createPreprocessRunContext, createPreprocessRunContext_fn, _handleHttpSourceRequest, handleHttpSourceRequest_fn, _resolveConnections, resolveConnections_fn, _resolveConnection, resolveConnection_fn, _buildJobsIndex, buildJobsIndex_fn, _buildJobIndex, buildJobIndex_fn, _buildJobIntegrations, buildJobIntegrations_fn, _buildJobIntegration, buildJobIntegration_fn;
1618
+ var _options4, _registeredJobs, _registeredSources, _registeredHttpSourceHandlers, _registeredDynamicTriggers, _jobMetadataByDynamicTriggers, _registeredSchedules, _authResolvers, _client2, _internalLogger, _preprocessRun, preprocessRun_fn, _executeJob, executeJob_fn, _createRunContext, createRunContext_fn, _createPreprocessRunContext, createPreprocessRunContext_fn, _handleHttpSourceRequest, handleHttpSourceRequest_fn, _resolveConnections, resolveConnections_fn, _resolveConnection, resolveConnection_fn, _buildJobsIndex, buildJobsIndex_fn, _buildJobIndex, buildJobIndex_fn, _buildJobIntegrations, buildJobIntegrations_fn, _buildJobIntegration, buildJobIntegration_fn, _logIOStats, logIOStats_fn, _standardResponseHeaders, standardResponseHeaders_get;
1487
1619
  var TriggerClient = class {
1488
1620
  constructor(options) {
1489
1621
  __privateAdd(this, _preprocessRun);
@@ -1497,6 +1629,8 @@ var TriggerClient = class {
1497
1629
  __privateAdd(this, _buildJobIndex);
1498
1630
  __privateAdd(this, _buildJobIntegrations);
1499
1631
  __privateAdd(this, _buildJobIntegration);
1632
+ __privateAdd(this, _logIOStats);
1633
+ __privateAdd(this, _standardResponseHeaders);
1500
1634
  __privateAdd(this, _options4, void 0);
1501
1635
  __privateAdd(this, _registeredJobs, {});
1502
1636
  __privateAdd(this, _registeredSources, {});
@@ -1510,7 +1644,10 @@ var TriggerClient = class {
1510
1644
  this.id = options.id;
1511
1645
  __privateSet(this, _options4, options);
1512
1646
  __privateSet(this, _client2, new ApiClient(__privateGet(this, _options4)));
1513
- __privateSet(this, _internalLogger, new import_core7.Logger("trigger.dev", __privateGet(this, _options4).verbose ? "debug" : "log"));
1647
+ __privateSet(this, _internalLogger, new import_core7.Logger("trigger.dev", __privateGet(this, _options4).verbose ? "debug" : "log", [
1648
+ "output",
1649
+ "noopTasksSet"
1650
+ ]));
1514
1651
  }
1515
1652
  async handleRequest(request) {
1516
1653
  __privateGet(this, _internalLogger).debug("handling request", {
@@ -1519,6 +1656,7 @@ var TriggerClient = class {
1519
1656
  method: request.method
1520
1657
  });
1521
1658
  const apiKey = request.headers.get("x-trigger-api-key");
1659
+ const triggerVersion = request.headers.get("x-trigger-version");
1522
1660
  const authorization = this.authorized(apiKey);
1523
1661
  switch (authorization) {
1524
1662
  case "authorized": {
@@ -1529,7 +1667,8 @@ var TriggerClient = class {
1529
1667
  status: 401,
1530
1668
  body: {
1531
1669
  message: "Unauthorized: client missing apiKey"
1532
- }
1670
+ },
1671
+ headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
1533
1672
  };
1534
1673
  }
1535
1674
  case "missing-header": {
@@ -1537,7 +1676,8 @@ var TriggerClient = class {
1537
1676
  status: 401,
1538
1677
  body: {
1539
1678
  message: "Unauthorized: missing x-trigger-api-key header"
1540
- }
1679
+ },
1680
+ headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
1541
1681
  };
1542
1682
  }
1543
1683
  case "unauthorized": {
@@ -1545,7 +1685,8 @@ var TriggerClient = class {
1545
1685
  status: 401,
1546
1686
  body: {
1547
1687
  message: `Forbidden: client apiKey mismatch: Make sure you are using the correct API Key for your environment`
1548
- }
1688
+ },
1689
+ headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
1549
1690
  };
1550
1691
  }
1551
1692
  }
@@ -1554,7 +1695,8 @@ var TriggerClient = class {
1554
1695
  status: 405,
1555
1696
  body: {
1556
1697
  message: "Method not allowed (only POST is allowed)"
1557
- }
1698
+ },
1699
+ headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
1558
1700
  };
1559
1701
  }
1560
1702
  const action = request.headers.get("x-trigger-action");
@@ -1563,7 +1705,8 @@ var TriggerClient = class {
1563
1705
  status: 400,
1564
1706
  body: {
1565
1707
  message: "Missing x-trigger-action header"
1566
- }
1708
+ },
1709
+ headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
1567
1710
  };
1568
1711
  }
1569
1712
  switch (action) {
@@ -1575,7 +1718,8 @@ var TriggerClient = class {
1575
1718
  body: {
1576
1719
  ok: false,
1577
1720
  error: "Missing endpoint ID"
1578
- }
1721
+ },
1722
+ headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
1579
1723
  };
1580
1724
  }
1581
1725
  if (this.id !== endpointId) {
@@ -1584,14 +1728,16 @@ var TriggerClient = class {
1584
1728
  body: {
1585
1729
  ok: false,
1586
1730
  error: `Endpoint ID mismatch error. Expected ${this.id}, got ${endpointId}`
1587
- }
1731
+ },
1732
+ headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
1588
1733
  };
1589
1734
  }
1590
1735
  return {
1591
1736
  status: 200,
1592
1737
  body: {
1593
1738
  ok: true
1594
- }
1739
+ },
1740
+ headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
1595
1741
  };
1596
1742
  }
1597
1743
  case "INDEX_ENDPOINT": {
@@ -1613,7 +1759,8 @@ var TriggerClient = class {
1613
1759
  };
1614
1760
  return {
1615
1761
  status: 200,
1616
- body
1762
+ body,
1763
+ headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
1617
1764
  };
1618
1765
  }
1619
1766
  case "INITIALIZE_TRIGGER": {
@@ -1638,7 +1785,8 @@ var TriggerClient = class {
1638
1785
  }
1639
1786
  return {
1640
1787
  status: 200,
1641
- body: dynamicTrigger.registeredTriggerForParams(body.data.params)
1788
+ body: dynamicTrigger.registeredTriggerForParams(body.data.params),
1789
+ headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
1642
1790
  };
1643
1791
  }
1644
1792
  case "EXECUTE_JOB": {
@@ -1661,10 +1809,11 @@ var TriggerClient = class {
1661
1809
  }
1662
1810
  };
1663
1811
  }
1664
- const results = await __privateMethod(this, _executeJob, executeJob_fn).call(this, execution.data, job);
1812
+ const results = await __privateMethod(this, _executeJob, executeJob_fn).call(this, execution.data, job, triggerVersion);
1665
1813
  return {
1666
1814
  status: 200,
1667
- body: results
1815
+ body: results,
1816
+ headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
1668
1817
  };
1669
1818
  }
1670
1819
  case "PREPROCESS_RUN": {
@@ -1693,7 +1842,8 @@ var TriggerClient = class {
1693
1842
  body: {
1694
1843
  abort: results.abort,
1695
1844
  properties: results.properties
1696
- }
1845
+ },
1846
+ headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
1697
1847
  };
1698
1848
  }
1699
1849
  case "DELIVER_HTTP_SOURCE_REQUEST": {
@@ -1742,7 +1892,8 @@ var TriggerClient = class {
1742
1892
  events,
1743
1893
  response,
1744
1894
  metadata
1745
- }
1895
+ },
1896
+ headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
1746
1897
  };
1747
1898
  }
1748
1899
  case "VALIDATE": {
@@ -1751,7 +1902,8 @@ var TriggerClient = class {
1751
1902
  body: {
1752
1903
  ok: true,
1753
1904
  endpointId: this.id
1754
- }
1905
+ },
1906
+ headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
1755
1907
  };
1756
1908
  }
1757
1909
  }
@@ -1759,7 +1911,8 @@ var TriggerClient = class {
1759
1911
  status: 405,
1760
1912
  body: {
1761
1913
  message: "Method not allowed"
1762
- }
1914
+ },
1915
+ headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
1763
1916
  };
1764
1917
  }
1765
1918
  defineJob(options) {
@@ -1916,6 +2069,9 @@ var TriggerClient = class {
1916
2069
  async getRun(runId, options) {
1917
2070
  return __privateGet(this, _client2).getRun(runId, options);
1918
2071
  }
2072
+ async cancelRun(runId) {
2073
+ return __privateGet(this, _client2).cancelRun(runId);
2074
+ }
1919
2075
  async getRuns(jobSlug, options) {
1920
2076
  return __privateGet(this, _client2).getRuns(jobSlug, options);
1921
2077
  }
@@ -1958,22 +2114,27 @@ preprocessRun_fn = /* @__PURE__ */ __name(async function(body, job) {
1958
2114
  };
1959
2115
  }, "#preprocessRun");
1960
2116
  _executeJob = new WeakSet();
1961
- executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1) {
2117
+ executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1, triggerVersion) {
1962
2118
  __privateGet(this, _internalLogger).debug("executing job", {
1963
2119
  execution: body1,
1964
2120
  job: job1.id,
1965
- version: job1.version
2121
+ version: job1.version,
2122
+ triggerVersion
1966
2123
  });
1967
2124
  const context = __privateMethod(this, _createRunContext, createRunContext_fn).call(this, body1);
1968
2125
  const io = new IO({
1969
2126
  id: body1.run.id,
1970
2127
  cachedTasks: body1.tasks,
2128
+ cachedTasksCursor: body1.cachedTaskCursor,
2129
+ yieldedExecutions: body1.yieldedExecutions ?? [],
2130
+ noopTasksSet: body1.noopTasksSet,
1971
2131
  apiClient: __privateGet(this, _client2),
1972
2132
  logger: __privateGet(this, _internalLogger),
1973
2133
  client: this,
1974
2134
  context,
1975
2135
  jobLogLevel: job1.logLevel ?? __privateGet(this, _options4).logLevel ?? "info",
1976
- jobLogger: __privateGet(this, _options4).ioLogLocalEnabled ? new import_core7.Logger(job1.id, job1.logLevel ?? __privateGet(this, _options4).logLevel ?? "info") : void 0
2136
+ jobLogger: __privateGet(this, _options4).ioLogLocalEnabled ? new import_core7.Logger(job1.id, job1.logLevel ?? __privateGet(this, _options4).logLevel ?? "info") : void 0,
2137
+ serverVersion: triggerVersion
1977
2138
  });
1978
2139
  const resolvedConnections = await __privateMethod(this, _resolveConnections, resolveConnections_fn).call(this, context, job1.options.integrations, body1.connections);
1979
2140
  if (!resolvedConnections.ok) {
@@ -1990,11 +2151,23 @@ executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1) {
1990
2151
  }, () => {
1991
2152
  return job1.options.run(job1.trigger.event.parsePayload(body1.event.payload ?? {}), ioWithConnections, context);
1992
2153
  });
2154
+ if (__privateGet(this, _options4).verbose) {
2155
+ __privateMethod(this, _logIOStats, logIOStats_fn).call(this, io.stats);
2156
+ }
1993
2157
  return {
1994
2158
  status: "SUCCESS",
1995
2159
  output
1996
2160
  };
1997
2161
  } catch (error) {
2162
+ if (__privateGet(this, _options4).verbose) {
2163
+ __privateMethod(this, _logIOStats, logIOStats_fn).call(this, io.stats);
2164
+ }
2165
+ if (error instanceof YieldExecutionError) {
2166
+ return {
2167
+ status: "YIELD_EXECUTION",
2168
+ key: error.key
2169
+ };
2170
+ }
1998
2171
  if (error instanceof ParsedPayloadSchemaError) {
1999
2172
  return {
2000
2173
  status: "INVALID_PAYLOAD",
@@ -2318,6 +2491,18 @@ buildJobIntegration_fn = /* @__PURE__ */ __name(function(integration1) {
2318
2491
  authSource
2319
2492
  };
2320
2493
  }, "#buildJobIntegration");
2494
+ _logIOStats = new WeakSet();
2495
+ logIOStats_fn = /* @__PURE__ */ __name(function(stats) {
2496
+ __privateGet(this, _internalLogger).debug("IO stats", {
2497
+ stats
2498
+ });
2499
+ }, "#logIOStats");
2500
+ _standardResponseHeaders = new WeakSet();
2501
+ standardResponseHeaders_get = /* @__PURE__ */ __name(function() {
2502
+ return {
2503
+ "Trigger-Version": import_core7.API_VERSIONS.LAZY_LOADED_CACHED_TASKS
2504
+ };
2505
+ }, "#standardResponseHeaders");
2321
2506
  function dynamicTriggerRegisterSourceJobId(id) {
2322
2507
  return `register-dynamic-trigger-${id}`;
2323
2508
  }