@degreesign/chat 1.1.0 → 1.3.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/chat.d.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  import { ChatModelId } from './enums';
2
2
  import type { CancelChat, ChatInput, ChatPreloadInput, ChatState, ChatStatusSnapshot, Runtime } from './types';
3
- /** Build one model and request status snapshot */
3
+ /** Build one status snapshot */
4
4
  declare const statusSnapshot: (service: Runtime, model: ChatModelId | undefined, state: ChatState) => ChatStatusSnapshot;
5
- /** Build a one-shot chat function over one runtime */
5
+ /** Build chat over one runtime */
6
6
  declare const createChatOn: (service: Runtime) => ((input: ChatInput) => CancelChat);
7
- /** Build a model preload over one runtime */
7
+ /** Build preload over one runtime */
8
8
  declare const createPreloadOn: (service: Runtime) => (input?: ChatPreloadInput) => Promise<boolean>;
9
9
  declare const createChat: (input: ChatInput) => CancelChat;
10
10
  declare const preloadModel: (input?: ChatPreloadInput) => Promise<boolean>;
11
- /** Delete every model file cached on this device */
11
+ /** Delete every cached model file */
12
12
  declare const clearModelCache: () => Promise<void>;
13
13
  export { clearModelCache, createChat, createChatOn, createPreloadOn, preloadModel, statusSnapshot, };
@@ -1,13 +1,22 @@
1
1
  import { ChatModelId } from './enums';
2
2
  import type { DescribeImagesInput, DescribeTranslatedInput, ImageDescription, Runtime } from './types';
3
- /** Model that describes every image, the Q3 build keeps the CPU pass light */
4
- declare const describeModelId = ChatModelId.qwen3_vl_2b_instruct_q3;
5
- /** Build image descriptions over one runtime */
3
+ declare const
4
+ /** System message of the describer */
5
+ imageSystem: string,
6
+ /** System image rules */
7
+ imageRules: string,
8
+ /** Request for every image */
9
+ imageNote = "Describe all elements of the image.";
10
+ /** Reply budget, about 100 words */
11
+ declare const imageReplyTokens = 128;
12
+ /** Describer model, Q4 CPU build */
13
+ declare const describeModelId = ChatModelId.qwen3_vl_2b_instruct;
14
+ /** Build descriptions over one runtime */
6
15
  declare const createDescribeOn: (service: Runtime) => (input: DescribeImagesInput) => Promise<ImageDescription[]>;
7
- /** Build translations of English image descriptions over one runtime */
16
+ /** Build translations over one runtime */
8
17
  declare const createTranslateOn: (service: Runtime) => (input: DescribeTranslatedInput) => Promise<string[]>;
9
- /** Describe every image in English, then translate each description on a GPU */
18
+ /** Describe images in English, translate */
10
19
  declare const describeImages: (input: DescribeImagesInput) => Promise<ImageDescription[]>;
11
- /** Translate English image descriptions on a GPU */
20
+ /** Translate image descriptions on GPU */
12
21
  declare const describeTranslated: (input: DescribeTranslatedInput) => Promise<string[]>;
13
- export { createDescribeOn, createTranslateOn, describeImages, describeModelId, describeTranslated, };
22
+ export { createDescribeOn, createTranslateOn, describeImages, describeModelId, describeTranslated, imageNote, imageReplyTokens, imageRules, imageSystem, };
@@ -1,37 +1,37 @@
1
1
  import type { ChatMessage, ChatModel, EngineMessage, EngineRunParams, LlmLimits, StreamReplyParams } from '../types';
2
- /** Default token, prompt and thread budgets of the CPU engine */
2
+ /** Default CPU engine budgets */
3
3
  declare const defaultLlmLimits: LlmLimits;
4
- /** Merge developer budgets over the backend defaults */
4
+ /** Merge budgets over backend defaults */
5
5
  declare const resolveLlmLimits: (overrides?: Partial<LlmLimits>, gpu?: boolean) => LlmLimits;
6
- /** Sampling options shared by every engine stream */
7
- declare const replyOptions: (limits: LlmLimits, reasoning: boolean) => {
6
+ /** Sampling options for every stream */
7
+ declare const replyOptions: (limits: LlmLimits, reasoning: boolean, temperature?: number) => {
8
8
  stream: true;
9
9
  temperature: number;
10
10
  max_tokens: number;
11
11
  };
12
12
  declare const
13
- /** Resolve one model file on the model host */
13
+ /** Resolve one model file URL */
14
14
  modelUrl: (repo: string, file: string) => string,
15
- /** Declared size of one download, zero when the host hides it */
15
+ /** Declared download size or zero */
16
16
  contentLength: (response: Response) => number,
17
- /** Whether any turn carries an image */
17
+ /** Any turn carries an image */
18
18
  hasImages: (messages: EngineMessage[]) => boolean,
19
19
  /** Whether any turn carries audio */
20
20
  hasAudio: (messages: EngineMessage[]) => boolean;
21
21
  declare const
22
- /** Map the host messages to OpenAI-style turns, bounded for local context */
22
+ /** Map host messages to turns */
23
23
  buildMessages: (messages: ChatMessage[], limits?: LlmLimits) => EngineMessage[],
24
- /** Split inline reasoning markers out of the streamed content */
24
+ /** Split reasoning markers from content */
25
25
  createThinkSplitter: (onReason: (text: string) => void, onText: (text: string) => void) => {
26
26
  feed: (text: string) => void;
27
27
  flush: () => void;
28
28
  };
29
29
  declare const
30
- /** Log an engine chat call with all message text censored */
30
+ /** Log chat call, text censored */
31
31
  logChatCall: (model: ChatModel, messages: EngineMessage[]) => void,
32
- /** Stream one reply, logging it and returning undefined when stopped */
32
+ /** Stream reply, undefined when stopped */
33
33
  streamReply: ({ model, logging, onReason, onText, onSpeed, start, stopped, firstTimeoutMs, idleTimeoutMs, onStall, }: StreamReplyParams) => Promise<string | undefined>;
34
34
  declare const
35
- /** Build the prompt and stream one reply through an engine */
35
+ /** Build prompt, stream through engine */
36
36
  runEngineChat: ({ model, params, start, stopped, onStall, }: EngineRunParams) => Promise<string | undefined>;
37
37
  export { buildMessages, createThinkSplitter, logChatCall, runEngineChat, streamReply, replyOptions, defaultLlmLimits, resolveLlmLimits, hasImages, hasAudio, modelUrl, contentLength, };
@@ -6,14 +6,14 @@ imageTokens = 128,
6
6
  tokenCell = 32,
7
7
  /** Tokens allowed above the target */
8
8
  tokenSlack = 8;
9
- /** Square size the WebGPU engine encodes */
9
+ /** Square size for WebGPU encoding */
10
10
  declare const gpuImageSize: ImageSize;
11
- /** Decode a data URL or fetch a URL into bytes for an engine */
11
+ /** Decode URL into engine bytes */
12
12
  declare const contentBuffer: (url: string) => Promise<ArrayBuffer>;
13
- /** Full-frame size near the token target */
14
- declare const visionSize: (width: number, height: number) => ImageSize;
15
- /** Fit one image URL to the token grid for an engine */
16
- declare const resizeImageUrl: (url: string, size?: ImageSize) => Promise<string>;
17
- /** Fit every image part before an engine sees it */
18
- declare const resizeImages: (messages: EngineMessage[], size?: ImageSize) => Promise<EngineMessage[]>;
13
+ /** Full-frame size near token target */
14
+ declare const visionSize: (width: number, height: number, tokens?: number) => ImageSize;
15
+ /** Fit image to token grid */
16
+ declare const resizeImageUrl: (url: string, size?: ImageSize, tokens?: number) => Promise<string>;
17
+ /** Fit image parts before engine */
18
+ declare const resizeImages: (messages: EngineMessage[], size?: ImageSize, tokens?: number) => Promise<EngineMessage[]>;
19
19
  export { gpuImageSize, imageTokens, tokenCell, tokenSlack, visionSize, contentBuffer, resizeImageUrl, resizeImages, };
@@ -1,16 +1,28 @@
1
- import type { EngineChunk, EngineMessage, LitertMessage, LitertSettings, LitertSettingsParams, LlmEngine, StartLlmParams, StreamLitertParams } from '../types';
1
+ import { loadLitert } from './litertLoader';
2
+ import type { EngineChunk, EngineMessage, GpuDevice, GpuThrottleParams, LitertMessage, LitertSettings, LitertSettingsParams, LlmEngine, StartLlmParams, StreamLitertParams } from '../types';
2
3
  declare const
3
- /** Smallest Gemma 4 vision budget in tokens */
4
- visionTokens = 70;
5
- /** Fetch one model file into the cache, once per URL */
4
+ /** Smallest Gemma 4 vision budget */
5
+ visionTokens = 70,
6
+ /** GPU busy share of time */
7
+ gpuShare = 0.7,
8
+ /** Most rest per readback */
9
+ maxRestMs = 40;
10
+ declare const
11
+ /** Rest owed for busy interval */
12
+ restFor: (busyMs: number) => number;
13
+ /** WebGPU device resting after readback */
14
+ declare const throttledDevice: ({ device, pace, }: GpuThrottleParams) => GpuDevice;
15
+ /** Drop device and wasm module */
16
+ declare const releaseDevice: (mod: Awaited<ReturnType<typeof loadLitert>>) => void;
17
+ /** Cache one file per URL */
6
18
  declare const cacheModel: (url: string, cache: Cache, signal: AbortSignal | undefined, onProgress: StartLlmParams[`onProgress`]) => Promise<void>;
7
- /** Map built messages to LiteRT turns */
19
+ /** Map messages to LiteRT turns */
8
20
  declare const toLitertMessages: (messages: EngineMessage[], keepMedia?: boolean) => Promise<LitertMessage[]>;
9
- /** Whether the live conversation already holds the incoming history */
21
+ /** Whether conversation holds incoming history */
10
22
  declare const continuesHistory: (sent: EngineMessage[], history: EngineMessage[]) => boolean;
11
- /** Stream one LiteRT reply as engine chunks */
23
+ /** Stream LiteRT reply as chunks */
12
24
  declare const streamLitert: ({ engine, mod, params, messages, session, }: StreamLitertParams) => AsyncGenerator<EngineChunk>;
13
- /** Build one engine settings object with the small vision budget */
14
- declare const litertSettings: ({ model, limits, source, mod, }: LitertSettingsParams) => LitertSettings;
25
+ /** Engine settings with vision budget */
26
+ declare const litertSettings: ({ model, limits, contextSize, source, mod, }: LitertSettingsParams) => LitertSettings;
15
27
  declare const litertEngine: LlmEngine;
16
- export { cacheModel, continuesHistory, litertEngine, litertSettings, streamLitert, toLitertMessages, visionTokens, };
28
+ export { cacheModel, continuesHistory, gpuShare, litertEngine, litertSettings, maxRestMs, releaseDevice, restFor, streamLitert, throttledDevice, toLitertMessages, visionTokens, };
@@ -1,11 +1,11 @@
1
1
  import type { ModelStore, ResumePlan, StoredFilePart, StoredPart } from '../types';
2
2
  declare const
3
- /** Contiguous parts from offset zero, stopping at the first gap */
3
+ /** Contiguous parts until first gap */
4
4
  chainParts: (parts: StoredFilePart[]) => StoredFilePart[],
5
- /** Where one response continues the parts, undefined restarts them */
5
+ /** Where response continues parts */
6
6
  resumePlan: (response: Response, part: StoredPart) => ResumePlan | undefined,
7
- /** Whether a failed store write means this host cannot use a file store */
7
+ /** Whether store failure disables storage */
8
8
  isStoreError: (error: unknown) => boolean;
9
- /** Create the resumable file store of one engine */
9
+ /** Create engine resumable file store */
10
10
  declare const createModelStore: (dirName: string) => ModelStore;
11
11
  export { chainParts, createModelStore, isStoreError, resumePlan, };
@@ -1,3 +1,3 @@
1
- /** Logical cores minus a fifth, one kept free for the page */
1
+ /** Reserve one fifth for page */
2
2
  declare const heuristicThreads: (cores: number) => number;
3
3
  export { heuristicThreads };
@@ -8,19 +8,19 @@ declare const visionLimits: (model: ChatModel) => {
8
8
  image_max_tokens?: undefined;
9
9
  };
10
10
  declare const
11
- /** Every CPU file of one model, its name and remote URL */
11
+ /** CPU file names and URLs */
12
12
  cpuFiles: (model: ChatModel) => Array<{
13
13
  name: string;
14
14
  url: string;
15
15
  }>,
16
- /** Every CPU file as a Blob, from the store or a resumable download */
16
+ /** CPU files as stored blobs */
17
17
  storedBlobs: ({ model, signal, onProgress, }: {
18
18
  model: ChatModel;
19
19
  signal?: AbortSignal;
20
20
  onProgress: StartLlmParams[`onProgress`];
21
21
  }) => Promise<Blob[] | undefined>,
22
- /** Map built messages to wllama turns */
22
+ /** Map messages to wllama turns */
23
23
  toWllamaMessages: (messages: EngineMessage[]) => Promise<WllamaMessage[]>,
24
- /** Start the CPU runtime, the unified engine entry */
24
+ /** Start CPU runtime engine entry */
25
25
  startLLM: ({ model, limits, onProgress, signal, logging, assets: overrides, }: StartLlmParams) => Promise<LlmRuntime>, wllamaEngine: LlmEngine;
26
26
  export { cpuFiles, startLLM, storedBlobs, toWllamaMessages, visionLimits, wllamaEngine, };
package/dist/enums.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- /** Who a chat message is from */
1
+ /** Author of a chat message */
2
2
  declare enum LlmRole {
3
3
  system = "system",
4
4
  user = "user",
5
5
  assistant = "assistant"
6
6
  }
7
- /** Assistant availability derived from the engine */
7
+ /** Assistant availability from engine */
8
8
  declare enum ChatStatus {
9
9
  online = 0,
10
10
  away = 1,
@@ -18,23 +18,22 @@ declare enum LlmStatus {
18
18
  error = 3,
19
19
  unsupported = 4
20
20
  }
21
- /** Which engine runs the model on this device */
21
+ /** Engine running the model */
22
22
  declare enum LlmBackend {
23
23
  cpu = "cpu",
24
24
  gpu = "gpu",
25
25
  none = "none"
26
26
  }
27
- /** Download or load-into-memory phase of a model load */
27
+ /** Download or load phase */
28
28
  declare enum LlmPhase {
29
29
  download = "download",
30
30
  load = "load"
31
31
  }
32
- /** Catalog ids of the on-device models */
32
+ /** Catalog ids of on-device models */
33
33
  declare enum ChatModelId {
34
34
  qwen3_vl_2b = "qwen3-vl-2b",
35
35
  qwen3_vl_2b_instruct = "qwen3-vl-2b-instruct",
36
36
  qwen3_vl_2b_instruct_q3 = "qwen3-vl-2b-instruct-q3",
37
- gemma4_e2b = "gemma4-e2b",
38
37
  gemma4_e2b_limited = "gemma4-e2b-limited"
39
38
  }
40
39
  /** Built-in tools the package ships */
@@ -43,7 +42,7 @@ declare enum BuiltinTool {
43
42
  calculate = "calculate",
44
43
  currency = "currency"
45
44
  }
46
- /** Search provider behind the websearch tool */
45
+ /** Provider behind the websearch tool */
47
46
  declare enum WebSearchProvider {
48
47
  wikipedia = "wikipedia",
49
48
  duckduckgo = "duckduckgo"
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { clearModelCache, createChat, preloadModel } from './chat';
2
- export { describeImages, describeModelId, describeTranslated } from './describe';
2
+ export { describeImages, describeModelId, describeTranslated, imageReplyTokens } from './describe';
3
+ export { gpuAdapter } from './models';
3
4
  export { builtinTools, calculateTool, currencyTool, webSearchTool, } from './tools';
4
5
  export { LlmRole, ChatStatus, LlmStatus, LlmBackend, LlmPhase, ChatModelId, BuiltinTool, WebSearchProvider, } from './enums';
5
6
  export type { ChatMessage, LlmLimits, ChatStatusSnapshot, ChatInput, ChatPreloadInput, CancelChat, DescribeImagesInput, DescribeTranslatedInput, ImageDescription, WllamaAssets, ChatTool, ChatToolCall, ChatToolResult, ChatToolSchema, ChatToolValue, WebSearchConfig, WebSearchResult, } from './types';
package/dist/log.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- /** Write one package log line when logging is on */
1
+ /** Write log line when enabled */
2
2
  declare const log: (logging: boolean | undefined, ...args: unknown[]) => void;
3
3
  export { log };
package/dist/models.d.ts CHANGED
@@ -1,26 +1,26 @@
1
1
  import { ChatModelId } from './enums';
2
2
  import type { ChatModel, MobileNavigator } from './types';
3
3
  declare const llmModels: ChatModel[],
4
- /** Context window requested from the CPU engine, a 32k KV cache needs 3.5 GB */
4
+ /** CPU context window, 8k tokens */
5
5
  cpuContextWindow = 8192, // max 262_400
6
- /** CPU window on a phone, the smaller cache keeps the tab alive */
6
+ /** Phone CPU window, 4096 tokens */
7
7
  mobileCpuContextWindow = 4096, // max 32_768
8
- /** Context window requested from the GPU engine, the wasm heap holds the model */
9
- litertContextWindow = 4096, // max 32_768
10
- /** Whether this client is a phone or tablet */
8
+ /** GPU context window, 32k tokens */
9
+ litertContextWindow = 32768, // max 32_768
10
+ /** Phone GPU window, 16k tokens */
11
+ mobileLitertContextWindow = 16384, // max 32_768
12
+ /** Whether this client is mobile */
11
13
  mobileDevice: (nav?: MobileNavigator) => boolean,
12
- /** Build of a Gemma request that this device loads */
13
- deviceModelId: (id?: ChatModelId, mobile?: boolean) => ChatModelId | undefined,
14
- /** Look up a catalog entry by id */
14
+ /** Find catalog entry by id */
15
15
  findLlmModel: (id?: string) => ChatModel | undefined,
16
- /** Whether this browser provides a WebGPU adapter */
16
+ /** Whether browser has WebGPU adapter */
17
17
  gpuAdapter: () => Promise<boolean>,
18
- /** Default model, the GPU one when a WebGPU adapter exists */
18
+ /** Default model, GPU when available */
19
19
  defaultLlmModel: () => Promise<ChatModelId | undefined>,
20
- /** Resolve the requested model or the default of this device */
20
+ /** Resolve requested model or default */
21
21
  resolveLlmModel: (id?: ChatModelId) => Promise<ChatModel | undefined>,
22
22
  /** Whether a model accepts images */
23
23
  supportsImages: (model: ChatModel) => boolean,
24
24
  /** Whether a model accepts audio */
25
25
  supportsAudio: (model: ChatModel) => boolean;
26
- export { llmModels, cpuContextWindow, mobileCpuContextWindow, litertContextWindow, mobileDevice, deviceModelId, findLlmModel, gpuAdapter, defaultLlmModel, resolveLlmModel, supportsImages, supportsAudio, };
26
+ export { llmModels, cpuContextWindow, mobileCpuContextWindow, litertContextWindow, mobileLitertContextWindow, mobileDevice, findLlmModel, gpuAdapter, defaultLlmModel, resolveLlmModel, supportsImages, supportsAudio, };