@sylphx/sdk 0.19.3 → 0.21.0

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.
@@ -654,6 +654,15 @@ type NativeStepContext = {
654
654
  * On replay: returns the cached result without re-running fn.
655
655
  */
656
656
  run<T>(name: string, fn: () => T | Promise<T>): Promise<T>;
657
+ /**
658
+ * Execute an external side effect with a durable idempotency key.
659
+ *
660
+ * First encounter: signals Platform to persist a side-effect receipt before
661
+ * the external call. Replay then invokes fn with that persisted key. If the
662
+ * handler crashes after the external call but before completion settles, the
663
+ * next replay reuses the same key so the downstream service suppresses duplicates.
664
+ */
665
+ sideEffect<T>(stepName: string, effectKey: string, externalService: string, fn: (idempotencyKey: string) => NativeSideEffectOutcome<T> | Promise<NativeSideEffectOutcome<T>>): Promise<T>;
657
666
  /**
658
667
  * Pause execution for the given duration.
659
668
  *
@@ -691,6 +700,10 @@ type NativeStepContext = {
691
700
  filter?: Record<string, unknown>;
692
701
  }): Promise<T | null>;
693
702
  };
703
+ interface NativeSideEffectOutcome<T = unknown> {
704
+ readonly handle?: string;
705
+ readonly result: T;
706
+ }
694
707
  /**
695
708
  * A named task definition registered with sylphx.tasks.define().
696
709
  */
@@ -714,6 +727,16 @@ interface NativeTaskDefinition<TPayload = unknown, TResult = unknown> {
714
727
  /**
715
728
  * The shape returned by GET/POST /tasks/:runId in the platform API.
716
729
  */
730
+ type TaskRunDispatchLeaseState = 'active' | 'expired' | 'legacy';
731
+ interface TaskRunDispatchLease {
732
+ /**
733
+ * Customer-safe dispatch claim state. Raw claim tokens and controller
734
+ * identities are intentionally not exposed through the public SDK.
735
+ */
736
+ state: TaskRunDispatchLeaseState;
737
+ claimedAt: string | null;
738
+ expiresAt: string | null;
739
+ }
717
740
  interface TaskRunStatus {
718
741
  id: string;
719
742
  taskName: string;
@@ -726,6 +749,13 @@ interface TaskRunStatus {
726
749
  scheduledFor?: string;
727
750
  startedAt?: string;
728
751
  completedAt?: string;
752
+ dispatchLease?: TaskRunDispatchLease | null;
753
+ definition?: {
754
+ id: string;
755
+ version: number | null;
756
+ hash: string | null;
757
+ source: string | null;
758
+ } | null;
729
759
  createdAt: string;
730
760
  updatedAt: string;
731
761
  }
@@ -7942,7 +7972,7 @@ interface UseTasksReturn {
7942
7972
  limit?: number;
7943
7973
  offset?: number;
7944
7974
  }) => Promise<Job[]>;
7945
- /** Cancel a pending/queued job */
7975
+ /** Cancel a pending, running, waiting, or failed task run */
7946
7976
  cancelJob: (jobId: string) => Promise<boolean>;
7947
7977
  /** Whether a job operation is in progress */
7948
7978
  isLoading: boolean;
@@ -13385,7 +13415,7 @@ declare class TasksClient {
13385
13415
  */
13386
13416
  getTaskRun(taskRunId: string): Promise<TaskRunStatus>;
13387
13417
  /**
13388
- * Cancel a pending or waiting task run.
13418
+ * Cancel a pending, running, waiting, or failed task run.
13389
13419
  */
13390
13420
  cancelTaskRun(taskRunId: string): Promise<boolean>;
13391
13421
  /**
@@ -14997,6 +14997,24 @@ async function putWithRetry(url, body, headers, signal, onProgress) {
14997
14997
  }
14998
14998
  throw lastErr;
14999
14999
  }
15000
+ function fileFromCompletion(completion, options, blob) {
15001
+ const now = /* @__PURE__ */ new Date();
15002
+ return {
15003
+ id: completion.fileId,
15004
+ filename: options.filename ?? (isFile(blob) ? blob.name : void 0) ?? "upload.bin",
15005
+ contentType: options.contentType ?? (blob.type || "application/octet-stream"),
15006
+ size: completion.size,
15007
+ checksumSha256: completion.checksumSha256,
15008
+ etag: completion.etag,
15009
+ visibility: options.visibility ?? "private",
15010
+ folder: options.folder ?? null,
15011
+ metadata: options.metadata ?? {},
15012
+ url: completion.url,
15013
+ isDeleted: false,
15014
+ createdAt: now,
15015
+ updatedAt: now
15016
+ };
15017
+ }
15000
15018
  async function uploadsCreate(config2, blob, options) {
15001
15019
  const signal = options.signal;
15002
15020
  if (signal?.aborted) throw toAbortError();
@@ -15063,7 +15081,7 @@ async function runSinglePart(config2, blob, session, options, idempotencyKey) {
15063
15081
  }
15064
15082
  );
15065
15083
  clearResume(String(session.uploadId));
15066
- return await fetchFile(config2, completion.fileId);
15084
+ return fileFromCompletion(completion, options, blob);
15067
15085
  }
15068
15086
  async function runMultipart(config2, blob, session, options, idempotencyKey) {
15069
15087
  const { onProgress, signal } = options;
@@ -15108,10 +15126,7 @@ async function runMultipart(config2, blob, session, options, idempotencyKey) {
15108
15126
  }
15109
15127
  );
15110
15128
  clearResume(String(session.uploadId));
15111
- return await fetchFile(config2, completion.fileId);
15112
- }
15113
- async function fetchFile(config2, fileId) {
15114
- return callApi(config2, PATHS.file(fileId), { method: "GET" });
15129
+ return fileFromCompletion(completion, options, blob);
15115
15130
  }
15116
15131
  async function uploadsAbort(config2, uploadId) {
15117
15132
  await withRetry(() => callApi(config2, PATHS.upload(uploadId), { method: "DELETE" }), {
@@ -29846,7 +29861,7 @@ var DEFAULT_CONFIG = {
29846
29861
  uploadInterval: SESSION_REPLAY_UPLOAD_INTERVAL_MS
29847
29862
  };
29848
29863
 
29849
- // ../../node_modules/web-vitals/dist/web-vitals.js
29864
+ // ../../../../.bun-cache/links/web-vitals@5.1.0-8d3498403ac6cf73/node_modules/web-vitals/dist/web-vitals.js
29850
29865
  var e = -1;
29851
29866
  var t = (t2) => {
29852
29867
  addEventListener("pageshow", ((n2) => {
@@ -34733,6 +34748,23 @@ var StepCompleteSignal = class {
34733
34748
  }
34734
34749
  _isStepCompleteSignal = true;
34735
34750
  };
34751
+ var StepSideEffectStartSignal = class {
34752
+ constructor(stepName, effectKey, externalService) {
34753
+ this.stepName = stepName;
34754
+ this.effectKey = effectKey;
34755
+ this.externalService = externalService;
34756
+ }
34757
+ _isStepSideEffectStartSignal = true;
34758
+ };
34759
+ var StepSideEffectCompleteSignal = class {
34760
+ constructor(stepName, effectKey, handle, result) {
34761
+ this.stepName = stepName;
34762
+ this.effectKey = effectKey;
34763
+ this.handle = handle;
34764
+ this.result = result;
34765
+ }
34766
+ _isStepSideEffectCompleteSignal = true;
34767
+ };
34736
34768
  var StepSleepSignal = class {
34737
34769
  constructor(stepName, duration) {
34738
34770
  this.stepName = stepName;
@@ -34748,7 +34780,7 @@ var StepWaitEventSignal = class {
34748
34780
  }
34749
34781
  _isStepWaitEventSignal = true;
34750
34782
  };
34751
- function createStepContext(completedSteps, resolvedWaits) {
34783
+ function createStepContext(completedSteps, resolvedWaits, sideEffectReceipts = /* @__PURE__ */ new Map()) {
34752
34784
  return {
34753
34785
  /**
34754
34786
  * Execute a named step.
@@ -34763,6 +34795,27 @@ function createStepContext(completedSteps, resolvedWaits) {
34763
34795
  const result = await fn();
34764
34796
  throw new StepCompleteSignal(name, result);
34765
34797
  },
34798
+ async sideEffect(stepName, effectKey, externalService, fn) {
34799
+ const receipt = sideEffectReceipts.get(sideEffectReceiptKey(stepName, effectKey));
34800
+ if (!receipt) {
34801
+ throw new StepSideEffectStartSignal(stepName, effectKey, externalService);
34802
+ }
34803
+ if (receipt.externalService !== externalService) {
34804
+ throw new Error(
34805
+ `Side-effect external service mismatch for ${stepName}/${effectKey}: receipt=${receipt.externalService} handler=${externalService}`
34806
+ );
34807
+ }
34808
+ if (receipt.completedAt) {
34809
+ return receipt.result;
34810
+ }
34811
+ const outcome = await fn(receipt.idempotencyKey);
34812
+ throw new StepSideEffectCompleteSignal(
34813
+ stepName,
34814
+ effectKey,
34815
+ normalizeOptionalStepKey(outcome.handle),
34816
+ outcome.result
34817
+ );
34818
+ },
34766
34819
  /**
34767
34820
  * Sleep for the given duration.
34768
34821
  *
@@ -34803,6 +34856,13 @@ function createStepContext(completedSteps, resolvedWaits) {
34803
34856
  }
34804
34857
  };
34805
34858
  }
34859
+ function normalizeOptionalStepKey(value) {
34860
+ const trimmed = value?.trim();
34861
+ return trimmed ? trimmed.slice(0, 500) : void 0;
34862
+ }
34863
+ function sideEffectReceiptKey(stepName, effectKey) {
34864
+ return `${stepName}\0${effectKey}`;
34865
+ }
34806
34866
  function verifySignature(body, signature, secret) {
34807
34867
  try {
34808
34868
  const hmac = createHmac("sha256", secret).update(body, "utf8").digest("hex");
@@ -34883,7 +34943,11 @@ function createTasksHandler(taskDefs, options = {}) {
34883
34943
  for (const wait2 of context?.waits ?? []) {
34884
34944
  resolvedWaits.set(wait2.name, wait2.result ?? void 0);
34885
34945
  }
34886
- const stepCtx = createStepContext(completedSteps, resolvedWaits);
34946
+ const sideEffectReceipts = /* @__PURE__ */ new Map();
34947
+ for (const receipt of context?.sideEffects ?? []) {
34948
+ sideEffectReceipts.set(sideEffectReceiptKey(receipt.stepName, receipt.effectKey), receipt);
34949
+ }
34950
+ const stepCtx = createStepContext(completedSteps, resolvedWaits, sideEffectReceipts);
34887
34951
  try {
34888
34952
  const result = await taskDef.handler(payload, { step: stepCtx });
34889
34953
  return Response.json({ status: "complete", result });
@@ -34896,6 +34960,25 @@ function createTasksHandler(taskDefs, options = {}) {
34896
34960
  result: signal.result
34897
34961
  });
34898
34962
  }
34963
+ if (err instanceof StepSideEffectStartSignal || err?._isStepSideEffectStartSignal) {
34964
+ const signal = err;
34965
+ return Response.json({
34966
+ status: "side_effect_start",
34967
+ stepName: signal.stepName,
34968
+ effectKey: signal.effectKey,
34969
+ externalService: signal.externalService
34970
+ });
34971
+ }
34972
+ if (err instanceof StepSideEffectCompleteSignal || err?._isStepSideEffectCompleteSignal) {
34973
+ const signal = err;
34974
+ return Response.json({
34975
+ status: "side_effect_complete",
34976
+ stepName: signal.stepName,
34977
+ effectKey: signal.effectKey,
34978
+ handle: signal.handle ?? null,
34979
+ result: signal.result
34980
+ });
34981
+ }
34899
34982
  if (err instanceof StepSleepSignal || err?._isStepSleepSignal) {
34900
34983
  const signal = err;
34901
34984
  return Response.json({
@@ -35305,7 +35388,7 @@ var TasksClient = class {
35305
35388
  return this.fetch(`/tasks/${taskRunId}`);
35306
35389
  }
35307
35390
  /**
35308
- * Cancel a pending or waiting task run.
35391
+ * Cancel a pending, running, waiting, or failed task run.
35309
35392
  */
35310
35393
  async cancelTaskRun(taskRunId) {
35311
35394
  const response = await this.fetch(`/tasks/${taskRunId}/cancel`, {