@trigger.dev/sdk 3.0.0-beta.37 → 3.0.0-beta.39

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/v3/index.mjs CHANGED
@@ -1,8 +1,9 @@
1
- import { TriggerTracer, SemanticInternalAttributes, runtime, accessoryAttributes, apiClientManager, taskCatalog, taskContext, defaultRetryOptions, calculateNextRetryDelay, defaultFetchRetryOptions, calculateResetAt, eventFilterMatches, flattenAttributes, stringifyIO, logger, conditionallyImportPacket, parsePacket, createErrorTaskError } from '@trigger.dev/core/v3';
1
+ import { TriggerTracer, SemanticInternalAttributes, runtime, accessoryAttributes, usage as usage$1, taskContext, apiClientManager, taskCatalog, TimezonesResult, defaultRetryOptions, calculateNextRetryDelay, defaultFetchRetryOptions, calculateResetAt, eventFilterMatches, flattenAttributes, stringifyIO, logger, conditionallyImportPacket, parsePacket, createErrorTaskError } from '@trigger.dev/core/v3';
2
2
  export { ApiError, AuthenticationError, BadRequestError, ConflictError, InternalServerError, NotFoundError, PermissionDeniedError, RateLimitError, UnprocessableEntityError, logger } from '@trigger.dev/core/v3';
3
3
  import { trace, context, SpanStatusCode, SpanKind } from '@opentelemetry/api';
4
4
  import { SEMATTRS_HTTP_STATUS_CODE, SEMATTRS_HTTP_METHOD, SEMATTRS_HTTP_URL, SEMATTRS_HTTP_HOST, SEMATTRS_HTTP_SCHEME, SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH, SEMATTRS_MESSAGING_OPERATION, SEMATTRS_MESSAGING_DESTINATION, SEMATTRS_MESSAGING_SYSTEM } from '@opentelemetry/semantic-conventions';
5
5
  import { AsyncLocalStorage } from 'node:async_hooks';
6
+ import { zodfetch } from '@trigger.dev/core/v3/zodfetch';
6
7
 
7
8
  var __defProp = Object.defineProperty;
8
9
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -17,7 +18,7 @@ var __publicField = (obj, key, value) => {
17
18
  };
18
19
 
19
20
  // package.json
20
- var version = "3.0.0-beta.37";
21
+ var version = "3.0.0-beta.39";
21
22
 
22
23
  // src/v3/tracer.ts
23
24
  var tracer = new TriggerTracer({
@@ -930,6 +931,116 @@ function calculateDurationInMs(options) {
930
931
  throw new Error("Invalid options");
931
932
  }
932
933
  __name(calculateDurationInMs, "calculateDurationInMs");
934
+ var usage = {
935
+ /**
936
+ * Get the current running usage of this task run.
937
+ *
938
+ * @example
939
+ *
940
+ * ```typescript
941
+ * import { usage, task } from "@trigger.dev/sdk/v3";
942
+ *
943
+ * export const myTask = task({
944
+ * id: "my-task",
945
+ * run: async (payload, { ctx }) => {
946
+ * // ... Do a bunch of work
947
+ *
948
+ * const currentUsage = usage.getCurrent();
949
+ *
950
+ * // You have access to the current compute cost and duration up to this point
951
+ * console.log("Current attempt compute cost and duration", {
952
+ * cost: currentUsage.compute.attempt.costInCents,
953
+ * duration: currentUsage.compute.attempt.durationMs,
954
+ * });
955
+ *
956
+ * // You also can see the total compute cost and duration up to this point in the run, across all attempts
957
+ * console.log("Current total compute cost and duration", {
958
+ * cost: currentUsage.compute.total.costInCents,
959
+ * duration: currentUsage.compute.total.durationMs,
960
+ * });
961
+ *
962
+ * // You can see the base cost of the run, which is the cost of the run before any compute costs
963
+ * console.log("Total cost", {
964
+ * cost: currentUsage.totalCostInCents,
965
+ * baseCost: currentUsage.baseCostInCents,
966
+ * });
967
+ * },
968
+ * });
969
+ * ```
970
+ */
971
+ getCurrent: () => {
972
+ const sample = usage$1.sample();
973
+ const machine = taskContext.ctx?.machine;
974
+ const run = taskContext.ctx?.run;
975
+ if (!sample) {
976
+ return {
977
+ compute: {
978
+ attempt: {
979
+ costInCents: 0,
980
+ durationMs: 0
981
+ },
982
+ total: {
983
+ costInCents: run?.costInCents ?? 0,
984
+ durationMs: run?.durationMs ?? 0
985
+ }
986
+ },
987
+ baseCostInCents: run?.baseCostInCents ?? 0,
988
+ totalCostInCents: (run?.costInCents ?? 0) + (run?.baseCostInCents ?? 0)
989
+ };
990
+ }
991
+ const currentCostInCents = machine?.centsPerMs ? sample.cpuTime * machine.centsPerMs : 0;
992
+ return {
993
+ compute: {
994
+ attempt: {
995
+ costInCents: currentCostInCents,
996
+ durationMs: sample.cpuTime
997
+ },
998
+ total: {
999
+ costInCents: (run?.costInCents ?? 0) + currentCostInCents,
1000
+ durationMs: (run?.durationMs ?? 0) + sample.cpuTime
1001
+ }
1002
+ },
1003
+ baseCostInCents: run?.baseCostInCents ?? 0,
1004
+ totalCostInCents: (run?.costInCents ?? 0) + currentCostInCents + (run?.baseCostInCents ?? 0)
1005
+ };
1006
+ },
1007
+ /**
1008
+ * Measure the cost and duration of a function.
1009
+ *
1010
+ * @example
1011
+ *
1012
+ * ```typescript
1013
+ * import { usage } from "@trigger.dev/sdk/v3";
1014
+ *
1015
+ * export const myTask = task({
1016
+ * id: "my-task",
1017
+ * run: async (payload, { ctx }) => {
1018
+ * const { result, compute } = await usage.measure(async () => {
1019
+ * // Do some work
1020
+ * return "result";
1021
+ * });
1022
+ *
1023
+ * console.log("Result", result);
1024
+ * console.log("Cost and duration", { cost: compute.costInCents, duration: compute.durationMs });
1025
+ * },
1026
+ * });
1027
+ * ```
1028
+ */
1029
+ measure: async (cb) => {
1030
+ const measurement = usage$1.start();
1031
+ const result = await cb();
1032
+ const sample = usage$1.stop(measurement);
1033
+ const machine = taskContext.ctx?.machine;
1034
+ const costInCents = machine?.centsPerMs ? sample.cpuTime * machine.centsPerMs : 0;
1035
+ return {
1036
+ result,
1037
+ compute: {
1038
+ costInCents,
1039
+ durationMs: sample.cpuTime
1040
+ }
1041
+ };
1042
+ }
1043
+ };
933
1044
  var runs = {
934
1045
  replay: replayRun,
935
1046
  cancel: cancelRun,
@@ -982,6 +1093,7 @@ __export(schedules_exports, {
982
1093
  list: () => list,
983
1094
  retrieve: () => retrieve,
984
1095
  task: () => task2,
1096
+ timezones: () => timezones,
985
1097
  update: () => update
986
1098
  });
987
1099
  function task2(params) {
@@ -1048,6 +1160,19 @@ function list(options) {
1048
1160
  return apiClient.listSchedules(options);
1049
1161
  }
1050
1162
  __name(list, "list");
1163
+ function timezones(options) {
1164
+ const baseUrl = apiClientManager.baseURL;
1165
+ if (!baseUrl) {
1166
+ throw apiClientMissingError();
1167
+ }
1168
+ return zodfetch(TimezonesResult, `${baseUrl}/api/v1/timezones${options?.excludeUtc === true ? "?excludeUtc=true" : ""}`, {
1169
+ method: "GET",
1170
+ headers: {
1171
+ "Content-Type": "application/json"
1172
+ }
1173
+ });
1174
+ }
1175
+ __name(timezones, "timezones");
1051
1176
 
1052
1177
  // src/v3/envvars.ts
1053
1178
  var envvars_exports = {};
@@ -1252,6 +1377,6 @@ function configure(options) {
1252
1377
  }
1253
1378
  __name(configure, "configure");
1254
1379
 
1255
- export { InMemoryCache, configure, createCache, envvars_exports as envvars, queue, retry, runs, schedules_exports as schedules, task, wait };
1380
+ export { InMemoryCache, configure, createCache, envvars_exports as envvars, queue, retry, runs, schedules_exports as schedules, task, usage, wait };
1256
1381
  //# sourceMappingURL=out.js.map
1257
1382
  //# sourceMappingURL=index.mjs.map