lumiverse-spindle-types 0.4.13 → 0.4.15

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.13",
3
+ "version": "0.4.15",
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 =
@@ -933,6 +1001,11 @@ export type WorkerToHost =
933
1001
  | { type: "vars_delete_global"; requestId: string; key: string; userId?: string }
934
1002
  | { type: "vars_list_global"; requestId: string; userId?: string }
935
1003
  | { type: "vars_has_global"; requestId: string; key: string; userId?: string }
1004
+ | { type: "vars_get_chat"; requestId: string; chatId: string; key: string }
1005
+ | { type: "vars_set_chat"; requestId: string; chatId: string; key: string; value: string }
1006
+ | { type: "vars_delete_chat"; requestId: string; chatId: string; key: string }
1007
+ | { type: "vars_list_chat"; requestId: string; chatId: string }
1008
+ | { type: "vars_has_chat"; requestId: string; chatId: string; key: string }
936
1009
  // ─── Characters (gated: "characters") ──────────────────────────────
937
1010
  | { type: "characters_list"; requestId: string; limit?: number; offset?: number; userId?: string }
938
1011
  | { type: "characters_get"; requestId: string; characterId: string; userId?: string }
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) */
@@ -307,8 +341,9 @@ export interface SpindleAPI {
307
341
  };
308
342
 
309
343
  /**
310
- * Local (chat-scoped) and global variable access (free tier — no permission needed).
311
- * Uses the same storage as built-in {{getvar}}/{{setgvar}} macros.
344
+ * Local (transient), global (user-scoped), and chat (persisted) variable access
345
+ * (free tier no permission needed).
346
+ * Uses the same storage as built-in {{getvar}}/{{setgvar}}/{{getchatvar}} macros.
312
347
  */
313
348
  variables: {
314
349
  local: {
@@ -325,6 +360,15 @@ export interface SpindleAPI {
325
360
  list(userId?: string): Promise<Record<string, string>>;
326
361
  has(key: string, userId?: string): Promise<boolean>;
327
362
  };
363
+ /** Chat-scoped persisted variables — stored in chat.metadata.chat_variables.
364
+ * Persists across generations within the same chat. */
365
+ chat: {
366
+ get(chatId: string, key: string): Promise<string>;
367
+ set(chatId: string, key: string, value: string): Promise<void>;
368
+ delete(chatId: string, key: string): Promise<void>;
369
+ list(chatId: string): Promise<Record<string, string>>;
370
+ has(chatId: string, key: string): Promise<boolean>;
371
+ };
328
372
  };
329
373
 
330
374
  /**