@sylphx/sdk 0.19.3 → 0.20.1

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
  /**
@@ -34733,6 +34733,23 @@ var StepCompleteSignal = class {
34733
34733
  }
34734
34734
  _isStepCompleteSignal = true;
34735
34735
  };
34736
+ var StepSideEffectStartSignal = class {
34737
+ constructor(stepName, effectKey, externalService) {
34738
+ this.stepName = stepName;
34739
+ this.effectKey = effectKey;
34740
+ this.externalService = externalService;
34741
+ }
34742
+ _isStepSideEffectStartSignal = true;
34743
+ };
34744
+ var StepSideEffectCompleteSignal = class {
34745
+ constructor(stepName, effectKey, handle, result) {
34746
+ this.stepName = stepName;
34747
+ this.effectKey = effectKey;
34748
+ this.handle = handle;
34749
+ this.result = result;
34750
+ }
34751
+ _isStepSideEffectCompleteSignal = true;
34752
+ };
34736
34753
  var StepSleepSignal = class {
34737
34754
  constructor(stepName, duration) {
34738
34755
  this.stepName = stepName;
@@ -34748,7 +34765,7 @@ var StepWaitEventSignal = class {
34748
34765
  }
34749
34766
  _isStepWaitEventSignal = true;
34750
34767
  };
34751
- function createStepContext(completedSteps, resolvedWaits) {
34768
+ function createStepContext(completedSteps, resolvedWaits, sideEffectReceipts = /* @__PURE__ */ new Map()) {
34752
34769
  return {
34753
34770
  /**
34754
34771
  * Execute a named step.
@@ -34763,6 +34780,27 @@ function createStepContext(completedSteps, resolvedWaits) {
34763
34780
  const result = await fn();
34764
34781
  throw new StepCompleteSignal(name, result);
34765
34782
  },
34783
+ async sideEffect(stepName, effectKey, externalService, fn) {
34784
+ const receipt = sideEffectReceipts.get(sideEffectReceiptKey(stepName, effectKey));
34785
+ if (!receipt) {
34786
+ throw new StepSideEffectStartSignal(stepName, effectKey, externalService);
34787
+ }
34788
+ if (receipt.externalService !== externalService) {
34789
+ throw new Error(
34790
+ `Side-effect external service mismatch for ${stepName}/${effectKey}: receipt=${receipt.externalService} handler=${externalService}`
34791
+ );
34792
+ }
34793
+ if (receipt.completedAt) {
34794
+ return receipt.result;
34795
+ }
34796
+ const outcome = await fn(receipt.idempotencyKey);
34797
+ throw new StepSideEffectCompleteSignal(
34798
+ stepName,
34799
+ effectKey,
34800
+ normalizeOptionalStepKey(outcome.handle),
34801
+ outcome.result
34802
+ );
34803
+ },
34766
34804
  /**
34767
34805
  * Sleep for the given duration.
34768
34806
  *
@@ -34803,6 +34841,13 @@ function createStepContext(completedSteps, resolvedWaits) {
34803
34841
  }
34804
34842
  };
34805
34843
  }
34844
+ function normalizeOptionalStepKey(value) {
34845
+ const trimmed = value?.trim();
34846
+ return trimmed ? trimmed.slice(0, 500) : void 0;
34847
+ }
34848
+ function sideEffectReceiptKey(stepName, effectKey) {
34849
+ return `${stepName}\0${effectKey}`;
34850
+ }
34806
34851
  function verifySignature(body, signature, secret) {
34807
34852
  try {
34808
34853
  const hmac = createHmac("sha256", secret).update(body, "utf8").digest("hex");
@@ -34883,7 +34928,11 @@ function createTasksHandler(taskDefs, options = {}) {
34883
34928
  for (const wait2 of context?.waits ?? []) {
34884
34929
  resolvedWaits.set(wait2.name, wait2.result ?? void 0);
34885
34930
  }
34886
- const stepCtx = createStepContext(completedSteps, resolvedWaits);
34931
+ const sideEffectReceipts = /* @__PURE__ */ new Map();
34932
+ for (const receipt of context?.sideEffects ?? []) {
34933
+ sideEffectReceipts.set(sideEffectReceiptKey(receipt.stepName, receipt.effectKey), receipt);
34934
+ }
34935
+ const stepCtx = createStepContext(completedSteps, resolvedWaits, sideEffectReceipts);
34887
34936
  try {
34888
34937
  const result = await taskDef.handler(payload, { step: stepCtx });
34889
34938
  return Response.json({ status: "complete", result });
@@ -34896,6 +34945,25 @@ function createTasksHandler(taskDefs, options = {}) {
34896
34945
  result: signal.result
34897
34946
  });
34898
34947
  }
34948
+ if (err instanceof StepSideEffectStartSignal || err?._isStepSideEffectStartSignal) {
34949
+ const signal = err;
34950
+ return Response.json({
34951
+ status: "side_effect_start",
34952
+ stepName: signal.stepName,
34953
+ effectKey: signal.effectKey,
34954
+ externalService: signal.externalService
34955
+ });
34956
+ }
34957
+ if (err instanceof StepSideEffectCompleteSignal || err?._isStepSideEffectCompleteSignal) {
34958
+ const signal = err;
34959
+ return Response.json({
34960
+ status: "side_effect_complete",
34961
+ stepName: signal.stepName,
34962
+ effectKey: signal.effectKey,
34963
+ handle: signal.handle ?? null,
34964
+ result: signal.result
34965
+ });
34966
+ }
34899
34967
  if (err instanceof StepSleepSignal || err?._isStepSleepSignal) {
34900
34968
  const signal = err;
34901
34969
  return Response.json({
@@ -35305,7 +35373,7 @@ var TasksClient = class {
35305
35373
  return this.fetch(`/tasks/${taskRunId}`);
35306
35374
  }
35307
35375
  /**
35308
- * Cancel a pending or waiting task run.
35376
+ * Cancel a pending, running, waiting, or failed task run.
35309
35377
  */
35310
35378
  async cancelTaskRun(taskRunId) {
35311
35379
  const response = await this.fetch(`/tasks/${taskRunId}/cancel`, {