lumiverse-spindle-types 0.4.14 → 0.4.16

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lumiverse-spindle-types",
3
- "version": "0.4.14",
3
+ "version": "0.4.16",
4
4
  "types": "./src/index.ts",
5
5
  "keywords": [
6
6
  "lumiverse",
package/src/api.ts CHANGED
@@ -755,6 +755,74 @@ export interface SpindleCommandContextDTO {
755
755
  isGroupChat?: boolean;
756
756
  }
757
757
 
758
+ // ─── Generation Event Payload DTOs ──────────────────────────────────────
759
+
760
+ /** Payload for `GENERATION_STARTED` events. */
761
+ export interface GenerationStartedPayloadDTO {
762
+ generationId: string;
763
+ chatId: string;
764
+ model: string;
765
+ targetMessageId?: string;
766
+ characterId?: string;
767
+ characterName?: string;
768
+ }
769
+
770
+ /** Payload for `STREAM_TOKEN_RECEIVED` events. */
771
+ export interface StreamTokenPayloadDTO {
772
+ generationId: string;
773
+ chatId: string;
774
+ /** The token text chunk. */
775
+ token: string;
776
+ /** Monotonic sequence number for deduplication on reconnect. */
777
+ seq: number;
778
+ /** Present and set to `"reasoning"` for chain-of-thought tokens. */
779
+ type?: "reasoning";
780
+ }
781
+
782
+ /** Payload for `GENERATION_ENDED` events. */
783
+ export interface GenerationEndedPayloadDTO {
784
+ generationId: string;
785
+ chatId: string;
786
+ /** ID of the saved message (absent on error). */
787
+ messageId?: string;
788
+ /** Final generated content (absent on error). */
789
+ content?: string;
790
+ /** Error message when the generation failed. */
791
+ error?: string;
792
+ }
793
+
794
+ /** Payload for `GENERATION_STOPPED` events (user-initiated stop). */
795
+ export interface GenerationStoppedPayloadDTO {
796
+ generationId: string;
797
+ chatId: string;
798
+ /** Partial content accumulated before the stop. */
799
+ content?: string;
800
+ }
801
+
802
+ /**
803
+ * Observer handle returned by `spindle.generate.observe()`.
804
+ * Provides a high-level API for watching an in-flight generation on a
805
+ * specific chat, with automatic token accumulation and lifecycle callbacks.
806
+ */
807
+ export interface GenerationObserver {
808
+ /** Register a callback for when a generation starts on the observed chat. */
809
+ onStart(handler: (info: GenerationStartedPayloadDTO) => void): void;
810
+ /** Register a callback for each streamed token (content or reasoning). */
811
+ onToken(handler: (token: StreamTokenPayloadDTO) => void): void;
812
+ /** Register a callback for when the generation completes (success or error). */
813
+ onEnd(handler: (result: GenerationEndedPayloadDTO) => void): void;
814
+ /** Register a callback for when the generation is stopped by the user. */
815
+ onStop(handler: (result: GenerationStoppedPayloadDTO) => void): void;
816
+ /** Accumulated content tokens so far. */
817
+ readonly content: string;
818
+ /** Accumulated reasoning tokens so far. */
819
+ readonly reasoning: string;
820
+ /** The active generation ID, or `null` if idle. */
821
+ readonly generationId: string | null;
822
+ /** Stop observing and unsubscribe from all events. */
823
+ dispose(): void;
824
+ }
825
+
758
826
  // ─── Worker → Host messages ──────────────────────────────────────────────
759
827
 
760
828
  export type WorkerToHost =
package/src/index.ts CHANGED
@@ -62,6 +62,11 @@ export type {
62
62
  SpindleModalItemDTO,
63
63
  SpindleCommandDTO,
64
64
  SpindleCommandContextDTO,
65
+ GenerationStartedPayloadDTO,
66
+ StreamTokenPayloadDTO,
67
+ GenerationEndedPayloadDTO,
68
+ GenerationStoppedPayloadDTO,
69
+ GenerationObserver,
65
70
  WorkerToHost,
66
71
  HostToWorker,
67
72
  } from "./api";
@@ -39,11 +39,24 @@ import type {
39
39
  SpindleModalItemDTO,
40
40
  SpindleCommandDTO,
41
41
  SpindleCommandContextDTO,
42
+ GenerationStartedPayloadDTO,
43
+ StreamTokenPayloadDTO,
44
+ GenerationEndedPayloadDTO,
45
+ GenerationStoppedPayloadDTO,
46
+ GenerationObserver,
42
47
  } from "./api";
43
48
 
44
49
  /** The global `spindle` object available in backend extension workers */
45
50
  export interface SpindleAPI {
46
- /** Subscribe to a Lumiverse event */
51
+ /** Subscribe to generation-started events (requires `generation` permission). */
52
+ on(event: "GENERATION_STARTED", handler: (payload: GenerationStartedPayloadDTO) => void): () => void;
53
+ /** Subscribe to streamed token events (requires `generation` permission). */
54
+ on(event: "STREAM_TOKEN_RECEIVED", handler: (payload: StreamTokenPayloadDTO) => void): () => void;
55
+ /** Subscribe to generation-ended events (requires `generation` permission). */
56
+ on(event: "GENERATION_ENDED", handler: (payload: GenerationEndedPayloadDTO) => void): () => void;
57
+ /** Subscribe to generation-stopped events (requires `generation` permission). */
58
+ on(event: "GENERATION_STOPPED", handler: (payload: GenerationStoppedPayloadDTO) => void): () => void;
59
+ /** Subscribe to a Lumiverse event. */
47
60
  on(event: string, handler: (payload: unknown) => void): () => void;
48
61
 
49
62
  /** Register a macro */
@@ -89,6 +102,27 @@ export interface SpindleAPI {
89
102
  batch(input: GenerationRequestDTO): Promise<unknown>;
90
103
  /** Run a dry-run prompt assembly without calling the LLM. */
91
104
  dryRun(input: DryRunRequestDTO, userId?: string): Promise<DryRunResultDTO>;
105
+ /**
106
+ * Observe an in-flight generation on a chat.
107
+ *
108
+ * Returns a {@link GenerationObserver} that subscribes to the generation
109
+ * lifecycle events for the given `chatId` and accumulates streamed
110
+ * content/reasoning automatically. Call `.dispose()` when done.
111
+ *
112
+ * Requires the `generation` permission.
113
+ *
114
+ * @example
115
+ * ```ts
116
+ * const obs = spindle.generate.observe("chat-uuid");
117
+ * obs.onStart((info) => console.log("Started:", info.model));
118
+ * obs.onToken((t) => console.log("Token:", t.token));
119
+ * obs.onEnd((r) => {
120
+ * console.log("Done:", obs.content);
121
+ * obs.dispose();
122
+ * });
123
+ * ```
124
+ */
125
+ observe(chatId: string): GenerationObserver;
92
126
  };
93
127
 
94
128
  /** Scoped storage (per-extension virtual disk) */
@@ -186,6 +220,13 @@ export interface SpindleAPI {
186
220
  role: "system" | "user" | "assistant";
187
221
  content: string;
188
222
  metadata?: Record<string, unknown>;
223
+ /** Index of the active swipe in `swipes`. `0` when the message has no alternates. */
224
+ swipe_id: number;
225
+ /**
226
+ * All swipe variants for this message, including the currently active one.
227
+ * `swipes[swipe_id]` always equals `content`.
228
+ */
229
+ swipes: string[];
189
230
  }>>;
190
231
  appendMessage(
191
232
  chatId: string,