@sylphx/sdk 0.19.2 → 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;
@@ -9978,9 +10008,10 @@ declare function sanitizeUrl$1(url: string): string | null;
9978
10008
  * 4. No silent fixes - Transparency over convenience (but warn + continue)
9979
10009
  * 5. Single Source of Truth - All key logic in one place
9980
10010
  *
9981
- * Key Formats (ADR-021):
9982
- * - Publishable key: pk_(dev|stg|prod|prev)_{ref}_{32hex} — client-safe (new)
9983
- * - Secret Key: sk_(dev|stg|prod|prev)_{ref}_{64hex} — server-side only
10011
+ * Key Formats (ADR-055):
10012
+ * - Publishable key: pk_(dev|stg|prod|prev)_{32hex} — client-safe
10013
+ * - Secret Key: sk_(dev|stg|prod|prev)_{64hex} — server-side only
10014
+ * - Legacy compatibility accepts embedded-ref keys from the superseded ADR-021 path.
9984
10015
  *
9985
10016
  * Legacy Key Formats (backward-compat):
9986
10017
  * - App ID (old): app_(dev|stg|prod|prev)_[identifier] — Public identifier
@@ -10012,7 +10043,7 @@ interface KeyValidationResult {
10012
10043
  /**
10013
10044
  * Validate a legacy App ID (app_*) and return detailed results.
10014
10045
  *
10015
- * @deprecated Use validatePublicKey() for new pk_* keys (ADR-021).
10046
+ * @deprecated Use validatePublicKey() for ADR-055 pk_* keys.
10016
10047
  *
10017
10048
  * @example
10018
10049
  * ```typescript
@@ -10029,7 +10060,7 @@ declare function validateAppId(key: string | undefined | null): KeyValidationRes
10029
10060
  /**
10030
10061
  * Validate and sanitize App ID, logging warnings.
10031
10062
  *
10032
- * @deprecated Use validateAndSanitizePublicKey() for new pk_* keys (ADR-021).
10063
+ * @deprecated Use validateAndSanitizePublicKey() for ADR-055 pk_* keys.
10033
10064
  * @throws Error if the key is invalid and cannot be sanitized
10034
10065
  * @returns The sanitized App ID
10035
10066
  */
@@ -13384,7 +13415,7 @@ declare class TasksClient {
13384
13415
  */
13385
13416
  getTaskRun(taskRunId: string): Promise<TaskRunStatus>;
13386
13417
  /**
13387
- * Cancel a pending or waiting task run.
13418
+ * Cancel a pending, running, waiting, or failed task run.
13388
13419
  */
13389
13420
  cancelTaskRun(taskRunId: string): Promise<boolean>;
13390
13421
  /**
@@ -121,12 +121,10 @@ var init_constants = __esm({
121
121
  // src/lib/pkce.ts
122
122
  var pkce_exports = {};
123
123
  __export(pkce_exports, {
124
- clearAllPKCEVerifiers: () => clearAllPKCEVerifiers,
125
124
  clearPKCEVerifier: () => clearPKCEVerifier,
126
125
  generateCodeChallenge: () => generateCodeChallenge,
127
126
  generateCodeVerifier: () => generateCodeVerifier,
128
127
  generatePKCE: () => generatePKCE,
129
- retrievePKCEVerifier: () => retrievePKCEVerifier,
130
128
  storePKCEVerifier: () => storePKCEVerifier,
131
129
  verifyPKCE: () => verifyPKCE
132
130
  });
@@ -231,46 +229,11 @@ function storePKCEVerifier(verifier, appId, nonce) {
231
229
  createdAt: Date.now()
232
230
  });
233
231
  }
234
- function retrievePKCEVerifier(appId, nonce) {
235
- if (typeof window === "undefined") return null;
236
- const key = buildKey(appId, nonce);
237
- const entry = getEntry(key);
238
- if (!entry) {
239
- return null;
240
- }
241
- deleteEntry(key);
242
- if (Date.now() - entry.createdAt > PKCE_TTL_MS) {
243
- return null;
244
- }
245
- return entry.verifier;
246
- }
247
232
  function clearPKCEVerifier(appId, nonce) {
248
233
  if (typeof window === "undefined") return;
249
234
  const key = buildKey(appId, nonce);
250
235
  deleteEntry(key);
251
236
  }
252
- function clearAllPKCEVerifiers(appId) {
253
- if (typeof window === "undefined") return;
254
- const prefix = PKCE_STORAGE_PREFIX + appId;
255
- if (isSessionStorageAvailable()) {
256
- const keysToRemove = [];
257
- for (let i2 = 0; i2 < sessionStorage.length; i2++) {
258
- const key = sessionStorage.key(i2);
259
- if (key?.startsWith(prefix)) {
260
- keysToRemove.push(key);
261
- }
262
- }
263
- for (const key of keysToRemove) {
264
- sessionStorage.removeItem(key);
265
- }
266
- } else {
267
- for (const key of pkceMemoryFallback.keys()) {
268
- if (key.startsWith(prefix)) {
269
- pkceMemoryFallback.delete(key);
270
- }
271
- }
272
- }
273
- }
274
237
  function timingSafeEqual(a2, b2) {
275
238
  if (a2.length !== b2.length) {
276
239
  let _result = 1;
@@ -34770,6 +34733,23 @@ var StepCompleteSignal = class {
34770
34733
  }
34771
34734
  _isStepCompleteSignal = true;
34772
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
+ };
34773
34753
  var StepSleepSignal = class {
34774
34754
  constructor(stepName, duration) {
34775
34755
  this.stepName = stepName;
@@ -34785,7 +34765,7 @@ var StepWaitEventSignal = class {
34785
34765
  }
34786
34766
  _isStepWaitEventSignal = true;
34787
34767
  };
34788
- function createStepContext(completedSteps, resolvedWaits) {
34768
+ function createStepContext(completedSteps, resolvedWaits, sideEffectReceipts = /* @__PURE__ */ new Map()) {
34789
34769
  return {
34790
34770
  /**
34791
34771
  * Execute a named step.
@@ -34800,6 +34780,27 @@ function createStepContext(completedSteps, resolvedWaits) {
34800
34780
  const result = await fn();
34801
34781
  throw new StepCompleteSignal(name, result);
34802
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
+ },
34803
34804
  /**
34804
34805
  * Sleep for the given duration.
34805
34806
  *
@@ -34840,6 +34841,13 @@ function createStepContext(completedSteps, resolvedWaits) {
34840
34841
  }
34841
34842
  };
34842
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
+ }
34843
34851
  function verifySignature(body, signature, secret) {
34844
34852
  try {
34845
34853
  const hmac = createHmac("sha256", secret).update(body, "utf8").digest("hex");
@@ -34920,7 +34928,11 @@ function createTasksHandler(taskDefs, options = {}) {
34920
34928
  for (const wait2 of context?.waits ?? []) {
34921
34929
  resolvedWaits.set(wait2.name, wait2.result ?? void 0);
34922
34930
  }
34923
- 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);
34924
34936
  try {
34925
34937
  const result = await taskDef.handler(payload, { step: stepCtx });
34926
34938
  return Response.json({ status: "complete", result });
@@ -34933,6 +34945,25 @@ function createTasksHandler(taskDefs, options = {}) {
34933
34945
  result: signal.result
34934
34946
  });
34935
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
+ }
34936
34967
  if (err instanceof StepSleepSignal || err?._isStepSleepSignal) {
34937
34968
  const signal = err;
34938
34969
  return Response.json({
@@ -35342,7 +35373,7 @@ var TasksClient = class {
35342
35373
  return this.fetch(`/tasks/${taskRunId}`);
35343
35374
  }
35344
35375
  /**
35345
- * Cancel a pending or waiting task run.
35376
+ * Cancel a pending, running, waiting, or failed task run.
35346
35377
  */
35347
35378
  async cancelTaskRun(taskRunId) {
35348
35379
  const response = await this.fetch(`/tasks/${taskRunId}/cancel`, {