@starcite/sdk 0.0.5 → 0.0.7

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.d.cts CHANGED
@@ -265,6 +265,62 @@ type TailEvent = z.infer<typeof TailEventSchema>;
265
265
  * Canonical session event surfaced by the SDK.
266
266
  */
267
267
  type SessionEvent = TailEvent;
268
+ /**
269
+ * Listener dispatch phase for `session.on("event")`.
270
+ */
271
+ type SessionEventPhase = "replay" | "live";
272
+ /**
273
+ * Context passed to `session.on("event")` listeners.
274
+ */
275
+ interface SessionEventContext {
276
+ /**
277
+ * Indicates whether this event originated from retained replay or live tail sync.
278
+ */
279
+ phase: SessionEventPhase;
280
+ /**
281
+ * Convenience flag for `phase === "replay"`.
282
+ */
283
+ replayed: boolean;
284
+ }
285
+ /**
286
+ * Session event listener signature.
287
+ */
288
+ type SessionEventListener<TEvent extends SessionEvent = SessionEvent> = (event: TEvent, context: SessionEventContext) => void | Promise<void>;
289
+ /**
290
+ * Item yielded by `session.tail(...)` async iterators.
291
+ */
292
+ interface SessionTailItem<TEvent extends SessionEvent = SessionEvent> {
293
+ /**
294
+ * Canonical ordered event.
295
+ */
296
+ event: TEvent;
297
+ /**
298
+ * Replay/live classification for this event.
299
+ */
300
+ context: SessionEventContext;
301
+ }
302
+ /**
303
+ * Listener options for `session.on("event", ...)`.
304
+ */
305
+ interface SessionOnEventOptions<TEvent extends SessionEvent = SessionEvent> {
306
+ /**
307
+ * Whether retained in-memory events should be replayed to this listener.
308
+ *
309
+ * Defaults to `true`.
310
+ */
311
+ replay?: boolean;
312
+ /**
313
+ * Optional schema used to validate and narrow events before dispatch.
314
+ *
315
+ * Schema validation failures are surfaced as session `error` events.
316
+ */
317
+ schema?: z.ZodType<TEvent>;
318
+ }
319
+ /**
320
+ * Options for async iterator tails.
321
+ */
322
+ interface SessionTailIteratorOptions<TEvent extends SessionEvent = SessionEvent> extends SessionTailOptions, SessionOnEventOptions<TEvent> {
323
+ }
268
324
  /**
269
325
  * Raw tail event batch grouped by a single WebSocket frame.
270
326
  */
@@ -300,7 +356,20 @@ interface SessionSnapshot {
300
356
  /**
301
357
  * Serializable persisted state for one session log.
302
358
  */
303
- interface SessionStoreState {
359
+ interface SessionStoreMetadata {
360
+ /**
361
+ * Store payload schema version.
362
+ */
363
+ schemaVersion: 1;
364
+ /**
365
+ * Unix epoch milliseconds when this snapshot was written.
366
+ */
367
+ updatedAtMs: number;
368
+ }
369
+ /**
370
+ * Serializable persisted state for one session log.
371
+ */
372
+ interface SessionStoreState<TEvent extends TailEvent = TailEvent> {
304
373
  /**
305
374
  * Highest contiguous sequence applied for this session.
306
375
  */
@@ -308,14 +377,18 @@ interface SessionStoreState {
308
377
  /**
309
378
  * Retained events snapshot used for immediate replay.
310
379
  */
311
- events: TailEvent[];
380
+ events: TEvent[];
381
+ /**
382
+ * Optional metadata for versioning and operational introspection.
383
+ */
384
+ metadata?: SessionStoreMetadata;
312
385
  }
313
386
  /**
314
387
  * Persistence interface for session cursor + retained events.
315
388
  */
316
- interface SessionStore {
317
- load(sessionId: string): SessionStoreState | undefined;
318
- save(sessionId: string, state: SessionStoreState): void;
389
+ interface SessionStore<TEvent extends TailEvent = TailEvent> {
390
+ load(sessionId: string): SessionStoreState<TEvent> | undefined;
391
+ save(sessionId: string, state: SessionStoreState<TEvent>): void;
319
392
  clear?(sessionId: string): void;
320
393
  }
321
394
  /**
@@ -436,7 +509,7 @@ interface SessionTailOptions {
436
509
  /**
437
510
  * Maximum time to wait for websocket handshake/open before reconnecting or failing.
438
511
  *
439
- * Defaults to `4000`.
512
+ * Defaults to `12000`.
440
513
  */
441
514
  connectionTimeoutMs?: number;
442
515
  /**
@@ -513,38 +586,6 @@ type TailLifecycleEvent = {
513
586
  sessionId: string;
514
587
  reason: "aborted" | "caught_up" | "graceful";
515
588
  };
516
- /**
517
- * Storage adapter used by `session.consume*()` to persist the last processed cursor.
518
- */
519
- interface SessionCursorStore {
520
- /**
521
- * Loads the last processed cursor for a session.
522
- *
523
- * Return `undefined` when no cursor has been stored yet.
524
- */
525
- load(sessionId: string): number | undefined | Promise<number | undefined>;
526
- /**
527
- * Persists the last processed cursor for a session.
528
- */
529
- save(sessionId: string, cursor: number): void | Promise<void>;
530
- }
531
- /**
532
- * Durable tail consumption options with automatic cursor checkpointing.
533
- */
534
- interface SessionConsumeOptions extends Omit<SessionTailOptions, "cursor"> {
535
- /**
536
- * Optional explicit starting cursor. When omitted, the SDK loads it from `cursorStore`.
537
- */
538
- cursor?: number;
539
- /**
540
- * Cursor storage adapter used for resume-safe processing.
541
- */
542
- cursorStore: SessionCursorStore;
543
- /**
544
- * Event handler. The cursor is checkpointed only after this handler succeeds.
545
- */
546
- handler: (event: TailEvent) => void | Promise<void>;
547
- }
548
589
  /**
549
590
  * Options for listing sessions.
550
591
  */
@@ -627,9 +668,9 @@ interface StarciteOptions {
627
668
  */
628
669
  websocketFactory?: StarciteWebSocketFactory;
629
670
  /**
630
- * Session store used for cursor + retained event persistence.
671
+ * Optional session store used for cursor + retained event persistence.
631
672
  *
632
- * Defaults to an in-memory store.
673
+ * When omitted, fresh attaches start from cursor `0` and replay from server tail.
633
674
  */
634
675
  store?: SessionStore;
635
676
  }
@@ -747,7 +788,7 @@ declare class SessionLog {
747
788
  replay?: boolean;
748
789
  }): () => void;
749
790
  private apply;
750
- getSnapshot(syncing: boolean): SessionSnapshot;
791
+ state(syncing: boolean): SessionSnapshot;
751
792
  get events(): readonly TailEvent[];
752
793
  get cursor(): number;
753
794
  get lastSeq(): number;
@@ -777,11 +818,10 @@ interface StarciteSessionOptions {
777
818
  token: string;
778
819
  identity: StarciteIdentity;
779
820
  transport: TransportConfig;
780
- store: SessionStore;
821
+ store?: SessionStore;
781
822
  record?: SessionRecord;
782
823
  logOptions?: SessionLogOptions;
783
824
  }
784
- type SessionEventListener = (event: SessionEvent) => void;
785
825
  /**
786
826
  * Session-scoped client bound to a specific identity and session token.
787
827
  *
@@ -803,8 +843,10 @@ declare class StarciteSession {
803
843
  private readonly store;
804
844
  private readonly lifecycle;
805
845
  private readonly eventSubscriptions;
846
+ private appendTask;
806
847
  private liveSyncController;
807
848
  private liveSyncTask;
849
+ private liveSyncCatchUpActive;
808
850
  constructor(options: StarciteSessionOptions);
809
851
  /**
810
852
  * Appends an event to this session.
@@ -820,6 +862,7 @@ declare class StarciteSession {
820
862
  * Subscribes to canonical session events and lifecycle errors.
821
863
  */
822
864
  on(eventName: "event", listener: SessionEventListener): () => void;
865
+ on<TEvent extends SessionEvent>(eventName: "event", listener: SessionEventListener<TEvent>, options: SessionOnEventOptions<TEvent>): () => void;
823
866
  on(eventName: "error", listener: (error: Error) => void): () => void;
824
867
  /**
825
868
  * Removes a previously registered listener.
@@ -839,25 +882,32 @@ declare class StarciteSession {
839
882
  */
840
883
  setLogOptions(options: SessionLogOptions): void;
841
884
  /**
842
- * Returns a stable snapshot of the current canonical in-memory log.
885
+ * Returns a stable view of the current canonical in-memory log state.
843
886
  */
844
- getSnapshot(): SessionSnapshot;
887
+ state(): SessionSnapshot;
845
888
  /**
846
- * Streams tail events one at a time via callback.
889
+ * Returns the retained canonical event list.
847
890
  */
848
- tail(onEvent: (event: TailEvent) => void | Promise<void>, options?: SessionTailOptions): Promise<void>;
891
+ events(): readonly SessionEvent[];
849
892
  /**
850
- * Streams tail event batches grouped by incoming frame via callback.
851
- */
852
- tailBatches(onBatch: (batch: TailEvent[]) => void | Promise<void>, options?: SessionTailOptions): Promise<void>;
853
- /**
854
- * Durably consumes events and checkpoints `event.seq` after each successful handler invocation.
893
+ * Streams canonical events as an async iterator.
894
+ *
895
+ * Replay semantics and schema validation mirror `session.on("event", ...)`.
855
896
  */
856
- consume(options: SessionConsumeOptions): Promise<void>;
897
+ tail<TEvent extends SessionEvent = SessionEvent>(options?: SessionTailIteratorOptions<TEvent>): AsyncIterable<SessionTailItem<TEvent>>;
898
+ private parseOnEvent;
899
+ private parseTailEvent;
900
+ private resolveEventContext;
901
+ private observeEventListenerResult;
902
+ private createTailAbortController;
903
+ private createTailRuntime;
904
+ private iterateLiveTail;
857
905
  private emitStreamError;
858
906
  private ensureLiveSync;
859
907
  private runLiveSync;
908
+ private subscribeLiveSyncPass;
860
909
  private persistLogState;
910
+ private waitForLiveSyncRetry;
861
911
  }
862
912
 
863
913
  /**
@@ -890,7 +940,7 @@ declare class Starcite {
890
940
  * Creates or binds to a session.
891
941
  *
892
942
  * **With identity** (backend): creates a new session and/or mints a session
893
- * token for the given identity. Pass `id` to bind to an existing session.
943
+ * token for the given identity. Pass `id` to create-or-bind that session.
894
944
  *
895
945
  * **With token** (frontend): wraps an existing session token. The identity
896
946
  * and session id are decoded from the JWT.
@@ -919,16 +969,17 @@ declare class Starcite {
919
969
  }
920
970
 
921
971
  /**
922
- * Minimal Web Storage contract used by {@link WebStorageCursorStore}.
972
+ * Minimal Web Storage contract used by {@link WebStorageSessionStore}.
923
973
  */
924
974
  interface StarciteWebStorage {
925
975
  getItem(key: string): string | null;
926
976
  setItem(key: string, value: string): void;
977
+ removeItem?(key: string): void;
927
978
  }
928
979
  /**
929
- * Key customization options for storage-backed cursor stores.
980
+ * Key customization options for storage-backed session stores.
930
981
  */
931
- interface CursorStoreOptions {
982
+ interface SessionStoreOptions {
932
983
  /**
933
984
  * Prefix used when deriving storage keys.
934
985
  *
@@ -941,42 +992,45 @@ interface CursorStoreOptions {
941
992
  keyForSession?: (sessionId: string) => string;
942
993
  }
943
994
  /**
944
- * In-memory cursor store (useful for workers/tests).
995
+ * Construction options for {@link WebStorageSessionStore}.
945
996
  */
946
- declare class InMemoryCursorStore implements SessionCursorStore {
947
- private readonly cursors;
948
- constructor(initial?: Record<string, number>);
949
- load(sessionId: string): number | undefined;
950
- save(sessionId: string, cursor: number): void;
951
- }
952
- /**
953
- * Cursor store backed by a Web Storage-compatible object.
954
- */
955
- declare class WebStorageCursorStore implements SessionCursorStore {
956
- private readonly storage;
957
- private readonly keyForSession;
958
- constructor(storage: StarciteWebStorage, options?: CursorStoreOptions);
959
- load(sessionId: string): number | undefined;
960
- save(sessionId: string, cursor: number): void;
997
+ interface WebStorageSessionStoreOptions<TEvent extends TailEvent = TailEvent> extends SessionStoreOptions {
998
+ /**
999
+ * Optional schema for validating persisted state payloads.
1000
+ *
1001
+ * When omitted, a default schema validates canonical TailEvent snapshots.
1002
+ */
1003
+ stateSchema?: z.ZodType<SessionStoreState<TEvent>>;
961
1004
  }
962
- /**
963
- * Cursor store backed by `globalThis.localStorage`.
964
- */
965
- declare class LocalStorageCursorStore extends WebStorageCursorStore {
966
- constructor(options?: CursorStoreOptions);
967
- }
968
-
969
1005
  /**
970
1006
  * Default in-memory session store.
971
1007
  *
972
1008
  * Persists both cursor and retained events for each session so late subscribers
973
1009
  * can replay immediately after process-local reconnect/rebind.
974
1010
  */
975
- declare class MemoryStore implements SessionStore {
1011
+ declare class MemoryStore<TEvent extends TailEvent = TailEvent> implements SessionStore<TEvent> {
976
1012
  private readonly sessions;
977
- load(sessionId: string): SessionStoreState | undefined;
978
- save(sessionId: string, state: SessionStoreState): void;
1013
+ load(sessionId: string): SessionStoreState<TEvent> | undefined;
1014
+ save(sessionId: string, state: SessionStoreState<TEvent>): void;
1015
+ clear(sessionId: string): void;
1016
+ }
1017
+ /**
1018
+ * Session store backed by a Web Storage-compatible object.
1019
+ */
1020
+ declare class WebStorageSessionStore<TEvent extends TailEvent = TailEvent> implements SessionStore<TEvent> {
1021
+ private readonly storage;
1022
+ private readonly keyForSession;
1023
+ private readonly stateSchema;
1024
+ constructor(storage: StarciteWebStorage, options?: WebStorageSessionStoreOptions<TEvent>);
1025
+ load(sessionId: string): SessionStoreState<TEvent> | undefined;
1026
+ save(sessionId: string, state: SessionStoreState<TEvent>): void;
979
1027
  clear(sessionId: string): void;
980
1028
  }
1029
+ /**
1030
+ * Session store backed by `globalThis.localStorage`.
1031
+ */
1032
+ declare class LocalStorageSessionStore<TEvent extends TailEvent = TailEvent> extends WebStorageSessionStore<TEvent> {
1033
+ constructor(options?: WebStorageSessionStoreOptions<TEvent>);
1034
+ }
981
1035
 
982
- export { type AppendEventRequest, type AppendEventResponse, type AppendResult, type CursorStoreOptions, InMemoryCursorStore, LocalStorageCursorStore, MemoryStore, type PrincipalType, type RequestOptions, type SessionAppendInput, type SessionConsumeOptions, type SessionCursorStore, type SessionEvent, type SessionListItem, type SessionListOptions, type SessionListPage, SessionLogConflictError, SessionLogGapError, type SessionLogOptions, type SessionRecord, type SessionSnapshot, type SessionStore, type SessionStoreState, type SessionTailOptions, type SessionTokenScope, Starcite, StarciteApiError, StarciteBackpressureError, StarciteConnectionError, StarciteError, StarciteIdentity, type StarciteOptions, StarciteRetryLimitError, StarciteSession, StarciteTailError, type StarciteTailErrorStage, StarciteTokenExpiredError, type StarciteWebSocket, type StarciteWebSocketCloseEvent, type StarciteWebSocketEventMap, type StarciteWebSocketFactory, type StarciteWebSocketMessageEvent, type StarciteWebStorage, type TailEvent, type TailEventBatch, type TailLifecycleEvent, type TailReconnectPolicy, WebStorageCursorStore };
1036
+ export { type AppendEventRequest, type AppendEventResponse, type AppendResult, LocalStorageSessionStore, MemoryStore, type PrincipalType, type RequestOptions, type SessionAppendInput, type SessionEvent, type SessionEventContext, type SessionEventListener, type SessionEventPhase, type SessionListItem, type SessionListOptions, type SessionListPage, SessionLogConflictError, SessionLogGapError, type SessionLogOptions, type SessionOnEventOptions, type SessionRecord, type SessionSnapshot, type SessionStore, type SessionStoreMetadata, type SessionStoreOptions, type SessionStoreState, type SessionTailItem, type SessionTailIteratorOptions, type SessionTailOptions, type SessionTokenScope, Starcite, StarciteApiError, StarciteBackpressureError, StarciteConnectionError, StarciteError, StarciteIdentity, type StarciteOptions, StarciteRetryLimitError, StarciteSession, StarciteTailError, type StarciteTailErrorStage, StarciteTokenExpiredError, type StarciteWebSocket, type StarciteWebSocketCloseEvent, type StarciteWebSocketEventMap, type StarciteWebSocketFactory, type StarciteWebSocketMessageEvent, type StarciteWebStorage, type TailEvent, type TailEventBatch, type TailLifecycleEvent, type TailReconnectPolicy, WebStorageSessionStore, type WebStorageSessionStoreOptions };