@degreesign/chat 1.0.2 → 1.2.0

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/types.d.ts CHANGED
@@ -1,18 +1,18 @@
1
1
  import type { ChatModelId, ChatStatus, LlmBackend, LlmPhase, LlmRole, LlmStatus, WebSearchProvider } from './enums';
2
- /** One chat message, owned by the host */
2
+ /** Host-owned chat message */
3
3
  interface ChatMessage {
4
4
  role: LlmRole;
5
5
  content: string;
6
- /** Image data URLs or URLs attached to the turn */
6
+ /** Turn image URLs */
7
7
  images?: string[];
8
- /** Audio data URLs or URLs attached to the turn */
8
+ /** Turn audio URLs */
9
9
  audio?: string[];
10
10
  }
11
- /** One JSON-safe value passed to or returned from a tool */
11
+ /** JSON-safe tool value */
12
12
  type ChatToolValue = string | number | boolean | null | ChatToolValue[] | {
13
13
  [key: string]: ChatToolValue;
14
14
  };
15
- /** The JSON-schema subset the model reads for a tool parameter */
15
+ /** Tool parameter JSON schema */
16
16
  interface ChatToolSchema {
17
17
  type?: `object` | `array` | `string` | `number` | `integer` | `boolean` | `null`;
18
18
  description?: string;
@@ -21,7 +21,7 @@ interface ChatToolSchema {
21
21
  items?: ChatToolSchema;
22
22
  enum?: string[];
23
23
  }
24
- /** One tool a host may run itself, never handed to the model */
24
+ /** Host-run tool */
25
25
  interface ChatTool {
26
26
  /** Unique tool name */
27
27
  name: string;
@@ -30,36 +30,36 @@ interface ChatTool {
30
30
  parameters?: ChatToolSchema;
31
31
  execute: (args: Record<string, ChatToolValue>) => ChatToolValue | Promise<ChatToolValue>;
32
32
  }
33
- /** One tool call, with its parsed arguments */
33
+ /** Parsed tool call */
34
34
  interface ChatToolCall {
35
35
  id: number;
36
36
  name: string;
37
37
  arguments: Record<string, ChatToolValue>;
38
38
  }
39
- /** One finished tool call, carrying its value or its error */
39
+ /** Finished tool result */
40
40
  interface ChatToolResult extends ChatToolCall {
41
41
  result?: ChatToolValue;
42
42
  error?: string;
43
43
  }
44
- /** Search provider settings of the websearch tool */
44
+ /** Websearch provider settings */
45
45
  interface WebSearchConfig {
46
46
  provider?: WebSearchProvider;
47
- /** Wikipedia language edition, two or three letters plus an optional region */
47
+ /** Wikipedia language code */
48
48
  language?: string;
49
- /** Results asked of the provider, 1 to 20 */
49
+ /** Provider result count */
50
50
  limit?: number;
51
51
  /** Characters kept per snippet */
52
52
  snippetLength?: number;
53
53
  /** Request budget in milliseconds */
54
54
  timeout?: number;
55
55
  }
56
- /** One search hit returned by the websearch tool */
56
+ /** Websearch result hit */
57
57
  interface WebSearchResult {
58
58
  title: string;
59
59
  url: string;
60
60
  snippet: string;
61
61
  }
62
- /** Websearch settings with every default and clamp resolved */
62
+ /** Resolved websearch settings */
63
63
  interface WebSearchSettings {
64
64
  provider: WebSearchProvider;
65
65
  language: string;
@@ -67,7 +67,7 @@ interface WebSearchSettings {
67
67
  snippetLength: number;
68
68
  timeout: number;
69
69
  }
70
- /** Fiat and metal rates of one feed fetch, quoted per EUR */
70
+ /** Fiat and metal rates */
71
71
  interface FiatSnapshot {
72
72
  /** Local fetch time, milliseconds */
73
73
  fetchedAt: number;
@@ -76,24 +76,24 @@ interface FiatSnapshot {
76
76
  rates: Record<string, number>;
77
77
  yesterday: Record<string, number>;
78
78
  }
79
- /** Crypto prices of one feed fetch, quoted in EUR per unit */
79
+ /** Crypto prices in EUR */
80
80
  interface CryptoSnapshot {
81
81
  fetchedAt: number;
82
82
  sourceAt: number;
83
83
  prices: Record<string, number>;
84
84
  yesterday: Record<string, number>;
85
85
  }
86
- /** The price sections held in memory and in localStorage */
86
+ /** Cached price sections */
87
87
  interface PriceSnapshot {
88
88
  fiat?: FiatSnapshot;
89
89
  crypto?: CryptoSnapshot;
90
90
  }
91
- /** Price sections one request has to read */
91
+ /** Required price sections */
92
92
  interface PriceSections {
93
93
  fiat: boolean;
94
94
  crypto: boolean;
95
95
  }
96
- /** One conversion returned by the currency tool */
96
+ /** Currency conversion result */
97
97
  interface CurrencyResult {
98
98
  from: string;
99
99
  to: string;
@@ -112,7 +112,7 @@ interface CalculationResult {
112
112
  expression: string;
113
113
  result: number;
114
114
  }
115
- /** One page of a Wikipedia search response */
115
+ /** Wikipedia search page */
116
116
  interface WikipediaPage {
117
117
  key?: string;
118
118
  title?: string;
@@ -123,7 +123,7 @@ interface WikipediaPage {
123
123
  interface WikipediaResponse {
124
124
  pages?: WikipediaPage[];
125
125
  }
126
- /** One DuckDuckGo topic, a group carries nested topics */
126
+ /** DuckDuckGo topic group */
127
127
  interface DuckDuckGoTopic {
128
128
  FirstURL?: string;
129
129
  Text?: string;
@@ -137,50 +137,50 @@ interface DuckDuckGoResponse {
137
137
  Results?: DuckDuckGoTopic[];
138
138
  RelatedTopics?: DuckDuckGoTopic[];
139
139
  }
140
- /** One crypto row of the price feed */
140
+ /** Crypto price row */
141
141
  interface PriceCoin {
142
142
  /** Price in EUR per unit */
143
143
  pr?: number;
144
- /** Price in EUR per unit, 24 hours earlier */
144
+ /** Price 24 hours ago */
145
145
  yst?: number;
146
146
  }
147
- /** The CoinExams price feed, validated before use */
147
+ /** Validated CoinExams feed */
148
148
  interface PriceFeed {
149
149
  rates?: {
150
- /** Feed timestamp of the fiat and metal rates, milliseconds */
150
+ /** Fiat feed timestamp */
151
151
  date?: number;
152
- /** Units of each symbol per EUR */
152
+ /** Symbol units per EUR */
153
153
  current?: Record<string, number>;
154
- /** The same rates, 24 hours earlier */
154
+ /** Rates 24 hours ago */
155
155
  yesterday?: Record<string, number>;
156
156
  };
157
157
  coins?: Record<string, PriceCoin>;
158
- /** Feed timestamp of the crypto prices, milliseconds */
158
+ /** Crypto feed timestamp */
159
159
  date?: number;
160
160
  }
161
161
  /** One on-device model */
162
162
  interface ChatModel {
163
- /** Stable catalog id, passed by the host */
163
+ /** Stable catalog id */
164
164
  id: ChatModelId;
165
- /** Whether the model supports chain-of-thought reasoning */
165
+ /** Model reasoning support */
166
166
  reasoning: boolean;
167
167
  /** wllama (CPU) artifact */
168
168
  cpu?: {
169
169
  repo: string;
170
170
  file: string;
171
- /** Vision projector, present when the CPU build accepts images */
171
+ /** Optional CPU vision projector */
172
172
  mmproj?: {
173
- /** Repository of the projector, when it differs */
173
+ /** Projector repository override */
174
174
  repo?: string;
175
175
  file: string;
176
- /** Whether the projector also accepts audio */
176
+ /** Projector audio support */
177
177
  audio?: boolean;
178
178
  };
179
179
  };
180
180
  /** LiteRT-LM (WebGPU) artifact */
181
181
  litert?: {
182
182
  repo: string;
183
- /** File name inside the model repository */
183
+ /** Model repository file name */
184
184
  file: string;
185
185
  /** Whether the build accepts images */
186
186
  vision: boolean;
@@ -188,159 +188,192 @@ interface ChatModel {
188
188
  audio: boolean;
189
189
  };
190
190
  }
191
- /** Token, prompt and thread budgets of an on-device generation */
191
+ /** Generation budget settings */
192
192
  interface LlmLimits {
193
193
  /** Max answer tokens */
194
194
  maxReplyTokens: number;
195
- /** Max chain-of-thought tokens, used by the CPU engine when it loads */
195
+ /** Max reasoning tokens */
196
196
  maxThinkingTokens: number;
197
- /** Total output budget when reasoning is on, thought plus answer */
197
+ /** Reasoning output budget */
198
198
  maxReasoningTokens: number;
199
- /** Max images sent in one prompt */
199
+ /** Max prompt images */
200
200
  maxImages: number;
201
- /** Max audio clips sent in one prompt */
201
+ /** Vision tokens per image */
202
+ imageTokens: number;
203
+ /** Max prompt audio clips */
202
204
  maxAudio: number;
203
205
  /** Max characters kept per message */
204
206
  maxContentLength: number;
205
- /** Max characters of the whole prompt */
207
+ /** Max prompt characters */
206
208
  promptCharBudget: number;
207
- /** Max CPU threads, applied after the hardware maximum */
209
+ /** Max CPU threads */
208
210
  maxThreads: number;
209
211
  }
210
- /** Current model and request state reported to the host */
212
+ /** Reported model and request state */
211
213
  interface ChatStatusSnapshot {
212
- /** Build that runs on this device after the mobile swap */
214
+ /** Device-specific model build */
213
215
  model?: ChatModelId;
214
- /** Availability derived from the engine status */
216
+ /** Derived engine availability */
215
217
  availability: ChatStatus;
216
218
  status: LlmStatus;
217
219
  backend: LlmBackend;
218
- /** Whether the requested model accepts images on this backend */
220
+ /** Model image support */
219
221
  supportsImages: boolean;
220
- /** Whether the requested model accepts audio on this backend */
222
+ /** Model audio support */
221
223
  supportsAudio: boolean;
222
224
  ready: boolean;
223
225
  loading: boolean;
224
- /** Whether a reply is streaming right now */
226
+ /** Reply streaming flag */
225
227
  requesting: boolean;
226
228
  phase: LlmPhase;
227
229
  /** Download or load progress, 0..100 */
228
230
  percent: number;
229
- /** Last request error, cleared when a reply starts */
231
+ /** Last request error */
230
232
  error?: string;
231
233
  }
232
- /** One chat request, invoked for every response */
234
+ /** Chat request callback */
233
235
  interface ChatInput {
234
- /** Model that answers, the GPU model when WebGPU is available, else the CPU one */
236
+ /** Model that answers */
235
237
  model?: ChatModelId;
236
- /** Complete prompt, including the system and user turns */
238
+ /** Complete chat prompt */
237
239
  messages: ChatMessage[];
238
- /** Override any token, prompt or thread budget */
240
+ /** Budget overrides */
239
241
  limits?: Partial<LlmLimits>;
240
- /** Whether the model may use chain-of-thought reasoning, defaults to off */
242
+ /** Reasoning option, off */
241
243
  reasoning?: boolean;
242
- /** Whether package and engine logs reach the console, defaults to off */
244
+ /** Logging option, off */
243
245
  logging?: boolean;
244
- /** Accumulated reasoning text, streamed while the model thinks */
246
+ /** Streamed reasoning text */
245
247
  onThinking?: (text: string) => void;
246
- /** Accumulated reply text, streamed while the model answers */
248
+ /** Streamed reply text */
247
249
  onResponse: (text: string) => void;
248
- /** Current generation speed in tokens per second, streamed while the model answers */
250
+ /** Generation speed callback */
249
251
  onSpeed?: (tokensPerSecond: number) => void;
250
- /** Model and request state, reported on every change */
252
+ /** Status change callback */
251
253
  onStatus?: (snapshot: ChatStatusSnapshot) => void;
252
254
  }
253
- /** Model preload request, a subset of ChatInput */
255
+ /** Model preload request */
254
256
  type ChatPreloadInput = Pick<ChatInput, `model` | `limits` | `logging` | `onStatus`>;
255
- /** Stop the current request, its model load, or free the loaded model */
257
+ /** Stop request or model */
256
258
  type CancelChat = () => void;
257
- /** One image described in English and in the requested language */
259
+ /** Described image result */
258
260
  interface ImageDescription {
259
- /** Description produced by the vision model */
261
+ /** Vision description */
260
262
  descriptionEN: string;
261
- /** Translation of the English description, English itself without a GPU */
263
+ /** Description translation */
262
264
  description: string;
263
- /** Time the describer took for this image, milliseconds */
265
+ /** Description duration */
264
266
  durationMs: number;
265
267
  }
266
268
  /** One batch image description request */
267
269
  interface DescribeImagesInput {
268
- /** Language the description is translated into, English skips translation */
270
+ /** Target description language */
269
271
  targetLanguage: string;
270
- /** Image data URLs or URLs, described one by one in this order */
272
+ /** Ordered image URLs */
271
273
  images: string[];
272
- /** Index of the image being processed and each description once ready */
274
+ /** Stop batch, keep finished */
275
+ signal?: AbortSignal;
276
+ /** Description progress callback */
273
277
  currentIndex?: (index: number, descriptionEN?: string, description?: string, durationMs?: number) => void;
274
- /** Index of the image being described and its speed, tokens per second */
278
+ /** Image speed callback */
275
279
  onSpeed?: (index: number, tokensPerSecond: number) => void;
276
- /** Index of the image and its accumulated text while the model writes */
280
+ /** Image text callback */
277
281
  onText?: (index: number, text: string, stage: `describe` | `translate`) => void;
278
- /** Override any token, prompt or thread budget */
282
+ /** Budget overrides */
279
283
  limits?: Partial<LlmLimits>;
280
- /** Whether package and engine logs reach the console, defaults to off */
284
+ /** Logging option, off */
281
285
  logging?: boolean;
282
- /** Model and request state, reported on every change */
286
+ /** Status change callback */
283
287
  onStatus?: (snapshot: ChatStatusSnapshot) => void;
288
+ /** Additional rules */
289
+ additionalRules?: string;
284
290
  }
285
- /** One batch translation request for English image descriptions */
291
+ /** Batch translation request */
286
292
  interface DescribeTranslatedInput {
287
- /** English descriptions translated one by one in this order */
293
+ /** Ordered English descriptions */
288
294
  descriptions: string[];
289
- /** Language the descriptions are translated into, English skips translation */
295
+ /** Target translation language */
290
296
  targetLanguage: string;
291
- /** Index being translated and each description once ready */
297
+ /** Stop batch, keep finished */
298
+ signal?: AbortSignal;
299
+ /** Translation progress callback */
292
300
  currentIndex?: (index: number, descriptionEN?: string, description?: string) => void;
293
- /** Index of the description being generated and its speed, tokens per second */
301
+ /** Translation speed callback */
294
302
  onSpeed?: (index: number, tokensPerSecond: number) => void;
295
- /** Index of the description and its accumulated text while the model writes */
303
+ /** Translation text callback */
296
304
  onText?: (index: number, text: string, stage: `translate`) => void;
297
- /** Override any token, prompt or thread budget */
305
+ /** Budget overrides */
298
306
  limits?: Partial<LlmLimits>;
299
- /** Whether package and engine logs reach the console, defaults to off */
307
+ /** Logging option, off */
300
308
  logging?: boolean;
301
- /** Model and request state, reported on every change */
309
+ /** Status change callback */
302
310
  onStatus?: (snapshot: ChatStatusSnapshot) => void;
303
311
  }
304
- /** wllama wasm assets used by the CPU engine */
312
+ /** CPU wasm assets */
305
313
  interface WllamaAssets {
306
- /** URL of the wllama wasm binary */
314
+ /** wllama wasm URL */
307
315
  wasm: string;
308
- /** URL of the wllama compat wasm binary */
316
+ /** Compat wasm URL */
309
317
  compatWasm: string;
310
- /** URL of the wllama compat worker script */
318
+ /** Compat worker URL */
311
319
  compatWorker: string;
312
320
  }
313
- /** LiteRT-LM wasm assets used by the GPU engine */
321
+ /** GPU wasm assets */
314
322
  interface LitertAssets {
315
- /** URL of the LiteRT-LM wasm glue script */
323
+ /** LiteRT glue URL */
316
324
  script: string;
317
- /** URL of the LiteRT-LM wasm binary */
325
+ /** LiteRT wasm URL */
318
326
  wasm: string;
319
327
  }
320
- /** Navigator with the WebGPU entry point, absent in Node */
328
+ /** WebGPU navigator type */
321
329
  interface GpuNavigator extends Navigator {
322
330
  gpu?: {
323
- /** Ask the browser for a WebGPU adapter */
331
+ /** Request WebGPU adapter */
324
332
  requestAdapter: (options?: {
325
333
  powerPreference?: `low-power` | `high-performance`;
326
334
  }) => Promise<unknown>;
327
335
  };
328
336
  }
329
- /** Navigator fields that tell a phone or tablet from a desktop */
337
+ /** Minimal WebGPU buffer */
338
+ interface GpuBuffer {
339
+ /** Map buffer after work */
340
+ mapAsync: (...args: unknown[]) => Promise<void>;
341
+ }
342
+ /** Minimal WebGPU device */
343
+ interface GpuDevice {
344
+ /** Create engine buffer */
345
+ createBuffer?: (...args: unknown[]) => GpuBuffer;
346
+ /** Release device resources */
347
+ destroy?: () => void;
348
+ }
349
+ /** Reset wasm fields */
350
+ interface GpuWasm {
351
+ preinitializedWebGPUDevice?: GpuDevice;
352
+ WebGPU?: {
353
+ preinitializedDeviceId?: number;
354
+ };
355
+ }
356
+ /** GPU throttle state */
357
+ interface GpuThrottleParams {
358
+ device: GpuDevice;
359
+ /** Rest after one GPU readback */
360
+ pace: () => Promise<void>;
361
+ }
362
+ /** Mobile detection fields */
330
363
  type MobileNavigator = Pick<Navigator, `userAgent` | `maxTouchPoints`> & {
331
- /** Chromium client hints, the reliable mobile flag */
364
+ /** Chromium mobile hint */
332
365
  userAgentData?: {
333
366
  mobile?: boolean;
334
367
  };
335
368
  };
336
- /** Request state that builds one status snapshot */
369
+ /** Status snapshot state */
337
370
  interface ChatState {
338
- /** Whether a reply is streaming right now */
371
+ /** Reply streaming flag */
339
372
  requesting: boolean;
340
373
  phase: LlmPhase;
341
374
  /** Download or load progress, 0..100 */
342
375
  percent: number;
343
- /** Last request error, cleared when a reply starts */
376
+ /** Last request error */
344
377
  error?: string;
345
378
  }
346
379
  /** One live chat request */
@@ -352,171 +385,188 @@ interface ActiveCall extends ChatState {
352
385
  /** Model resolved for this call */
353
386
  model?: ChatModelId;
354
387
  }
355
- /** Engine and backend overrides used by tests */
388
+ /** Test engine overrides */
356
389
  interface RuntimeDeps {
357
390
  engines?: {
358
391
  cpu?: LlmEngine;
359
392
  litert?: LlmEngine;
360
393
  };
361
394
  backend?: LlmBackend;
395
+ /** Device class override, tests only */
396
+ mobile?: () => boolean;
362
397
  }
363
398
  /** One model load request */
364
399
  interface RuntimeLoadParams {
365
400
  model: ChatModel;
366
401
  limits: LlmLimits;
367
402
  assets?: Partial<WllamaAssets>;
368
- /** Whether engine logs reach the console */
403
+ /** Engine logging flag */
369
404
  logging?: boolean;
370
- /** Stop waiting for this load, the download continues while another caller waits */
405
+ /** Stop awaiting shared load */
371
406
  signal?: AbortSignal;
372
407
  onProgress: (progress: number, phase: LlmPhase) => void;
373
408
  }
374
- /** One caller waiting on an in-flight model load */
409
+ /** Waiting load caller */
375
410
  interface RuntimeLoadWaiter {
376
- /** Report load progress to this caller */
411
+ /** Report caller progress */
377
412
  report: RuntimeLoadParams[`onProgress`];
378
- /** Resolve this caller, early when it leaves the load */
413
+ /** Resolve waiting caller */
379
414
  settle: (loaded: boolean) => void;
380
415
  }
381
- /** Bytes of one model file already stored on disk */
416
+ /** Stored model file bytes */
382
417
  interface StoredPart {
383
418
  /** Bytes already stored */
384
419
  size: number;
385
- /** Full file size, zero while unknown */
420
+ /** File size, zero unknown */
386
421
  total: number;
387
- /** Entity tag of the stored bytes, empty when the host hides it */
422
+ /** Stored bytes entity tag */
388
423
  etag: string;
389
424
  }
390
- /** One part file of a model, at its byte offset */
425
+ /** Model part file */
391
426
  interface StoredFilePart {
392
- /** Byte offset of the part in the model file */
427
+ /** Part byte offset */
393
428
  start: number;
394
429
  /** Bytes stored in the part */
395
430
  size: number;
396
431
  }
397
- /** One stored model file, complete or still downloading */
432
+ /** Stored model file */
398
433
  interface StoredModel extends StoredPart {
399
- /** The stored bytes, a snapshot of the parts on disk */
434
+ /** Stored file snapshot */
400
435
  file: Blob;
401
- /** Whether every byte of the total is stored */
436
+ /** Complete file flag */
402
437
  complete: boolean;
403
438
  }
404
- /** Download metadata kept beside one stored model file */
439
+ /** Stored download metadata */
405
440
  interface StoredModelMeta {
406
441
  total: number;
407
442
  etag: string;
408
443
  complete: boolean;
409
444
  }
410
- /** Where one response continues a stored part */
445
+ /** Stored part continuation */
411
446
  interface ResumePlan {
412
- /** Byte the response body starts at */
447
+ /** Response start byte */
413
448
  start: number;
414
- /** Full file size, zero while unknown */
449
+ /** File size, zero unknown */
415
450
  total: number;
416
451
  etag: string;
417
452
  }
418
- /** One model file download into the store */
453
+ /** Model download into store */
419
454
  interface StoreDownloadParams {
420
455
  url: string;
421
- /** Stop the download, the bytes stored so far are kept */
456
+ /** Stop download, keep bytes */
422
457
  signal?: AbortSignal;
423
458
  onProgress: StartLlmParams[`onProgress`];
424
459
  }
425
- /** Resumable model file store owned by one engine */
460
+ /** Engine resumable file store */
426
461
  interface ModelStore {
427
- /** Stored bytes and metadata of one model file, undefined when absent */
462
+ /** Stored model file info */
428
463
  stored: (url: string) => Promise<StoredModel | undefined>;
429
- /** Download one model file, resuming the parts already stored */
464
+ /** Resume model file download */
430
465
  download: (params: StoreDownloadParams) => Promise<StoredModel | undefined>;
431
- /** Delete one stored model file and its metadata */
466
+ /** Delete model file */
432
467
  remove: (url: string) => Promise<void>;
433
- /** Delete the whole store directory and every model file in it */
468
+ /** Delete entire store */
434
469
  purge: () => Promise<void>;
435
470
  }
436
471
  /** One generation request */
437
472
  interface RuntimeChatParams {
473
+ /** Resolved answer model */
474
+ model?: ChatModelId;
438
475
  messages: ChatMessage[];
439
476
  limits: LlmLimits;
440
477
  reasoning: boolean;
441
- /** Whether engine logs reach the console */
478
+ /** Repetition penalty */
479
+ repeatPenalty?: number;
480
+ /** Sampling temperature */
481
+ temperature?: number;
482
+ /** Engine logging flag */
442
483
  logging?: boolean;
443
484
  onReason: (text: string) => void;
444
485
  onText: (text: string) => void;
445
- /** Current generation speed in tokens per second */
486
+ /** Generation speed */
446
487
  onSpeed?: (tokensPerSecond: number) => void;
447
488
  signal?: AbortSignal;
448
489
  }
449
- /** On-device runtime shared by every request */
490
+ /** Shared on-device runtime */
450
491
  interface Runtime {
451
- /** Load a model, or join a load of the same model already in flight */
492
+ /** Load or join model */
452
493
  load: (params: RuntimeLoadParams) => Promise<boolean>;
453
494
  chat: (params: RuntimeChatParams) => Promise<string | undefined>;
454
- unload: () => Promise<void>;
455
- /** Release the model and delete every cached model file */
495
+ /** Release one or all models */
496
+ unload: (model?: ChatModelId) => Promise<void>;
497
+ /** Release and delete caches */
456
498
  clear: () => Promise<void>;
457
- ready: () => boolean;
458
- loading: () => boolean;
459
- status: () => LlmStatus;
460
- backend: () => LlmBackend;
461
- /** Last model load error, cleared when a load starts */
462
- error: () => string | undefined;
463
- /** Model currently loaded on this runtime */
499
+ /** Model loaded check */
500
+ ready: (model?: ChatModelId) => boolean;
501
+ /** Model loading check */
502
+ loading: (model?: ChatModelId) => boolean;
503
+ /** Model load status */
504
+ status: (model?: ChatModelId) => LlmStatus;
505
+ /** Model backend */
506
+ backend: (model?: ChatModelId) => LlmBackend;
507
+ /** Latest model load error */
508
+ error: (model?: ChatModelId) => string | undefined;
509
+ /** Last active model */
464
510
  model: () => ChatModel | undefined;
465
511
  }
466
- /** Params for a single on-device chat generation */
512
+ /** Single chat generation params */
467
513
  interface EngineChatParams {
468
514
  messages: ChatMessage[];
469
515
  limits: LlmLimits;
470
- /** User preference, off unless explicitly turned on */
516
+ /** Opt-in reasoning preference */
471
517
  reasoning: boolean;
472
- /** Whether engine logs reach the console */
518
+ /** Repetition penalty */
519
+ repeatPenalty?: number;
520
+ /** Sampling temperature */
521
+ temperature?: number;
522
+ /** Engine logging flag */
473
523
  logging?: boolean;
474
524
  /** Streamed reasoning delta */
475
525
  onReason: (text: string) => void;
476
526
  /** Streamed reply delta */
477
527
  onText: (text: string) => void;
478
- /** Current generation speed in tokens per second */
528
+ /** Generation speed */
479
529
  onSpeed?: (tokensPerSecond: number) => void;
480
530
  signal?: AbortSignal;
481
531
  }
482
532
  /** Params of the engine startLLM */
483
533
  interface StartLlmParams {
484
534
  model: ChatModel;
485
- /** Engine-level budgets, read when the model loads */
535
+ /** Engine load budgets */
486
536
  limits: LlmLimits;
487
- /** Report download and load progress 0..1 */
537
+ /** Download progress callback */
488
538
  onProgress: (progress: number, phase?: LlmPhase) => void;
489
- /** Stop a download or load that is no longer wanted */
539
+ /** Cancel download or load */
490
540
  signal?: AbortSignal;
491
- /** Whether engine logs reach the console */
541
+ /** Engine logging flag */
492
542
  logging?: boolean;
493
543
  /** CPU engine asset override */
494
544
  assets?: Partial<WllamaAssets>;
495
545
  }
496
- /** Unified object returned by the engine startLLM */
546
+ /** Engine start result */
497
547
  interface LlmRuntime {
498
548
  /** Model this runtime serves */
499
549
  model: ChatModel;
500
550
  /** Backend that accepted the model */
501
551
  backend: LlmBackend;
502
- /** Context window requested from the engine */
552
+ /** Requested context window */
503
553
  contextSize: number;
504
- /** Stream one reply, returning the final text or undefined when stopped */
554
+ /** Stream reply text */
505
555
  chat: (params: EngineChatParams) => Promise<string | undefined>;
506
- /** Release the model from memory, keeping its cache */
556
+ /** Release model, keep cache */
507
557
  unload: () => Promise<void>;
508
558
  }
509
- /** A model runner backed by one inference engine */
559
+ /** Engine-backed model runner */
510
560
  interface LlmEngine {
511
561
  backend: LlmBackend;
512
- /** Download (if needed) and load a model, returning its runtime */
562
+ /** Load model runtime */
513
563
  startLLM: (params: StartLlmParams) => Promise<LlmRuntime>;
514
- /** Whether the model files are already fully stored on this device */
564
+ /** Model files stored check */
515
565
  cached: (model: ChatModel) => Promise<boolean>;
516
- /** Delete every on-device artifact this engine keeps, including orphans */
566
+ /** Delete engine artifacts */
517
567
  purge: () => Promise<void>;
518
568
  }
519
- /** One text, image or audio segment of a user turn */
569
+ /** User turn content part */
520
570
  type EngineContentPart = {
521
571
  type: `text`;
522
572
  text: string;
@@ -549,7 +599,7 @@ type WllamaMessage = {
549
599
  role: `assistant`;
550
600
  content: string;
551
601
  };
552
- /** One OpenAI-style message, a user turn may carry media parts */
602
+ /** OpenAI-style chat message */
553
603
  type EngineMessage = {
554
604
  role: `system`;
555
605
  content: string;
@@ -568,31 +618,33 @@ type LitertMessage = import('@litert-lm/core').Message;
568
618
  type LitertEngine = import('@litert-lm/core').Engine;
569
619
  /** The LiteRT-LM module namespace */
570
620
  type LitertModule = typeof import('@litert-lm/core');
571
- /** Settings handed to the LiteRT-LM engine */
621
+ /** LiteRT engine settings */
572
622
  type LitertSettings = import('@litert-lm/core').EngineSettings;
573
- /** Params of one LiteRT-LM settings build */
623
+ /** LiteRT settings params */
574
624
  interface LitertSettingsParams {
575
625
  /** Model with the artifact */
576
626
  model: ChatModel;
577
- /** Image and context budgets */
627
+ /** Image budgets */
578
628
  limits: LlmLimits;
629
+ /** Requested context window */
630
+ contextSize: number;
579
631
  /** Model file stream */
580
632
  source: ReadableStream<Uint8Array>;
581
633
  /** Loaded LiteRT module */
582
634
  mod: LitertModule;
583
635
  }
584
- /** One reused LiteRT-LM conversation and the turns it already holds */
636
+ /** Reused LiteRT conversation */
585
637
  interface LitertSession {
586
- /** Live conversation, dropped when a turn cannot be continued */
638
+ /** Live LiteRT conversation */
587
639
  conversation?: import('@litert-lm/core').Conversation;
588
- /** Built turns already sent to the conversation */
640
+ /** Sent conversation turns */
589
641
  sent: EngineMessage[];
590
- /** Reasoning flag the conversation was created with */
642
+ /** Conversation reasoning flag */
591
643
  reasoning: boolean;
592
- /** Output token budget the conversation was created with */
644
+ /** Conversation output budget */
593
645
  maxTokens: number;
594
646
  }
595
- /** Params of one LiteRT-LM chat call */
647
+ /** LiteRT chat params */
596
648
  interface LitertChatParams {
597
649
  engine: LitertEngine;
598
650
  model: ChatModel;
@@ -608,13 +660,13 @@ interface StreamLitertParams {
608
660
  messages: EngineMessage[];
609
661
  session: LitertSession;
610
662
  }
611
- /** Streamed delta, reasoning may be inline or structured */
663
+ /** Streamed reasoning delta */
612
664
  interface EngineDelta {
613
665
  content?: string;
614
666
  reasoning?: string;
615
667
  reasoning_content?: string;
616
668
  }
617
- /** Streamed chunk as the engine shapes it */
669
+ /** Engine stream chunk */
618
670
  interface EngineChunk {
619
671
  choices?: Array<{
620
672
  finish_reason?: string;
@@ -628,35 +680,35 @@ interface StreamReplyParams {
628
680
  onReason: (text: string) => void;
629
681
  /** Streamed reply delta */
630
682
  onText: (text: string) => void;
631
- /** Current generation speed in tokens per second */
683
+ /** Generation speed */
632
684
  onSpeed?: (tokensPerSecond: number) => void;
633
- /** Open the engine stream for the prepared messages */
685
+ /** Open engine stream */
634
686
  start: () => Promise<AsyncIterable<unknown>>;
635
- /** Whether the caller stopped the generation */
687
+ /** Generation stopped flag */
636
688
  stopped: () => boolean;
637
- /** Whether engine logs reach the console */
689
+ /** Engine logging flag */
638
690
  logging?: boolean;
639
- /** Budget before the first chunk arrives, milliseconds */
691
+ /** First chunk timeout */
640
692
  firstTimeoutMs?: number;
641
693
  /** Budget between two chunks, milliseconds */
642
694
  idleTimeoutMs?: number;
643
- /** Called when a slow stream keeps the text already delivered */
695
+ /** Slow stream callback */
644
696
  onStall?: () => void;
645
697
  }
646
- /** Params of one engine chat call */
698
+ /** Engine chat params */
647
699
  interface EngineRunParams {
648
700
  model: ChatModel;
649
701
  params: EngineChatParams;
650
- /** Open the engine stream for the prepared messages */
702
+ /** Open engine stream */
651
703
  start: (messages: EngineMessage[]) => Promise<AsyncIterable<unknown>>;
652
- /** Whether the caller stopped the generation */
704
+ /** Generation stopped flag */
653
705
  stopped: () => boolean;
654
- /** Called when a slow stream keeps the text already delivered */
706
+ /** Slow stream callback */
655
707
  onStall?: () => void;
656
708
  }
657
- /** Pixel size of one image sent to an engine */
709
+ /** Engine image pixel size */
658
710
  interface ImageSize {
659
711
  width: number;
660
712
  height: number;
661
713
  }
662
- export type { ChatMessage, ChatTool, ChatToolCall, ChatToolResult, ChatToolSchema, ChatToolValue, WebSearchConfig, WebSearchResult, WebSearchSettings, FiatSnapshot, CryptoSnapshot, PriceSnapshot, PriceSections, PriceCoin, PriceFeed, CurrencyResult, CalculationResult, WikipediaPage, WikipediaResponse, DuckDuckGoTopic, DuckDuckGoResponse, ChatModel, LlmLimits, ChatStatusSnapshot, ChatInput, ChatPreloadInput, ChatState, CancelChat, ImageDescription, DescribeImagesInput, DescribeTranslatedInput, WllamaAssets, LitertAssets, GpuNavigator, MobileNavigator, ActiveCall, RuntimeDeps, RuntimeLoadParams, RuntimeLoadWaiter, RuntimeChatParams, StoredPart, StoredFilePart, StoredModel, StoredModelMeta, ResumePlan, StoreDownloadParams, ModelStore, Runtime, EngineChatParams, StartLlmParams, LlmRuntime, LlmEngine, EngineMessage, ImageSize, EngineContentPart, WllamaContentPart, WllamaMessage, LitertContentPart, LitertMessage, LitertEngine, LitertModule, LitertSettings, LitertSettingsParams, LitertSession, LitertChatParams, StreamLitertParams, EngineDelta, EngineChunk, StreamReplyParams, EngineRunParams, };
714
+ export type { ChatMessage, ChatTool, ChatToolCall, ChatToolResult, ChatToolSchema, ChatToolValue, WebSearchConfig, WebSearchResult, WebSearchSettings, FiatSnapshot, CryptoSnapshot, PriceSnapshot, PriceSections, PriceCoin, PriceFeed, CurrencyResult, CalculationResult, WikipediaPage, WikipediaResponse, DuckDuckGoTopic, DuckDuckGoResponse, ChatModel, LlmLimits, ChatStatusSnapshot, ChatInput, ChatPreloadInput, ChatState, CancelChat, ImageDescription, DescribeImagesInput, DescribeTranslatedInput, WllamaAssets, LitertAssets, GpuBuffer, GpuDevice, GpuWasm, GpuThrottleParams, GpuNavigator, MobileNavigator, ActiveCall, RuntimeDeps, RuntimeLoadParams, RuntimeLoadWaiter, RuntimeChatParams, StoredPart, StoredFilePart, StoredModel, StoredModelMeta, ResumePlan, StoreDownloadParams, ModelStore, Runtime, EngineChatParams, StartLlmParams, LlmRuntime, LlmEngine, EngineMessage, ImageSize, EngineContentPart, WllamaContentPart, WllamaMessage, LitertContentPart, LitertMessage, LitertEngine, LitertModule, LitertSettings, LitertSettingsParams, LitertSession, LitertChatParams, StreamLitertParams, EngineDelta, EngineChunk, StreamReplyParams, EngineRunParams, };