@degreesign/chat 0.5.2 → 0.6.2
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/README.md +178 -14
- package/dist/browser/chat.min.mjs +1 -1
- package/dist/browser/chat.min.mjs.LICENSE.txt +2 -0
- package/dist/engines/litert.d.ts +22 -3
- package/dist/enums.d.ts +12 -1
- package/dist/index.d.ts +2 -2
- package/dist/models.d.ts +4 -2
- package/dist/node/chat.node.min.js +1 -1
- package/dist/node/chat.node.min.js.LICENSE.txt +2 -0
- package/dist/node/chat.node.min.mjs +1 -1
- package/dist/node/chat.node.min.mjs.LICENSE.txt +2 -0
- package/dist/tools/calculator.d.ts +9 -0
- package/dist/tools/currency.d.ts +9 -0
- package/dist/tools/index.d.ts +9 -0
- package/dist/tools/search.d.ts +9 -0
- package/dist/tools/text.d.ts +7 -0
- package/dist/types.d.ts +211 -9
- package/package.json +2 -1
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ChatModelId, ChatStatus, LlmBackend, LlmPhase, LlmRole, LlmStatus } from './enums';
|
|
1
|
+
import type { BuiltinTool, ChatModelId, ChatStatus, LlmBackend, LlmPhase, LlmRole, LlmStatus, WebSearchProvider } from './enums';
|
|
2
2
|
/** One chat message, owned by the host */
|
|
3
3
|
interface ChatMessage {
|
|
4
4
|
role: LlmRole;
|
|
@@ -8,6 +8,174 @@ interface ChatMessage {
|
|
|
8
8
|
/** Audio data URLs or URLs attached to the turn */
|
|
9
9
|
audio?: string[];
|
|
10
10
|
}
|
|
11
|
+
/** One JSON-safe value passed to or returned from a tool */
|
|
12
|
+
type ChatToolValue = string | number | boolean | null | ChatToolValue[] | {
|
|
13
|
+
[key: string]: ChatToolValue;
|
|
14
|
+
};
|
|
15
|
+
/** The JSON-schema subset the model reads for a tool parameter */
|
|
16
|
+
interface ChatToolSchema {
|
|
17
|
+
type?: `object` | `array` | `string` | `number` | `integer` | `boolean` | `null`;
|
|
18
|
+
description?: string;
|
|
19
|
+
properties?: Record<string, ChatToolSchema>;
|
|
20
|
+
required?: string[];
|
|
21
|
+
items?: ChatToolSchema;
|
|
22
|
+
enum?: string[];
|
|
23
|
+
}
|
|
24
|
+
/** One tool the model may call, run by the package or by the host */
|
|
25
|
+
interface ChatTool {
|
|
26
|
+
/** Unique tool name, the model calls it by this */
|
|
27
|
+
name: string;
|
|
28
|
+
/** What the tool does, read by the model before a call */
|
|
29
|
+
description?: string;
|
|
30
|
+
parameters?: ChatToolSchema;
|
|
31
|
+
execute: (args: Record<string, ChatToolValue>) => ChatToolValue | Promise<ChatToolValue>;
|
|
32
|
+
}
|
|
33
|
+
/** One call the model made, with its parsed arguments */
|
|
34
|
+
interface ChatToolCall {
|
|
35
|
+
id: number;
|
|
36
|
+
name: string;
|
|
37
|
+
arguments: Record<string, ChatToolValue>;
|
|
38
|
+
}
|
|
39
|
+
/** One finished tool call, carrying its value or its error */
|
|
40
|
+
interface ChatToolResult extends ChatToolCall {
|
|
41
|
+
result?: ChatToolValue;
|
|
42
|
+
error?: string;
|
|
43
|
+
}
|
|
44
|
+
/** Search provider settings of the built-in websearch tool */
|
|
45
|
+
interface WebSearchConfig {
|
|
46
|
+
provider?: WebSearchProvider;
|
|
47
|
+
/** Wikipedia language edition, two or three letters plus an optional region */
|
|
48
|
+
language?: string;
|
|
49
|
+
/** Results asked of the provider, 1 to 20 */
|
|
50
|
+
limit?: number;
|
|
51
|
+
/** Characters kept per snippet */
|
|
52
|
+
snippetLength?: number;
|
|
53
|
+
/** Request budget in milliseconds */
|
|
54
|
+
timeout?: number;
|
|
55
|
+
}
|
|
56
|
+
/** One search hit returned to the model */
|
|
57
|
+
interface WebSearchResult {
|
|
58
|
+
title: string;
|
|
59
|
+
url: string;
|
|
60
|
+
snippet: string;
|
|
61
|
+
}
|
|
62
|
+
/** Websearch settings with every default and clamp resolved */
|
|
63
|
+
interface WebSearchSettings {
|
|
64
|
+
provider: WebSearchProvider;
|
|
65
|
+
language: string;
|
|
66
|
+
limit: number;
|
|
67
|
+
snippetLength: number;
|
|
68
|
+
timeout: number;
|
|
69
|
+
}
|
|
70
|
+
/** Fiat and metal rates of one feed fetch, quoted per EUR */
|
|
71
|
+
interface FiatSnapshot {
|
|
72
|
+
/** Local fetch time, milliseconds */
|
|
73
|
+
fetchedAt: number;
|
|
74
|
+
/** Feed timestamp, milliseconds */
|
|
75
|
+
sourceAt: number;
|
|
76
|
+
rates: Record<string, number>;
|
|
77
|
+
yesterday: Record<string, number>;
|
|
78
|
+
}
|
|
79
|
+
/** Crypto prices of one feed fetch, quoted in EUR per unit */
|
|
80
|
+
interface CryptoSnapshot {
|
|
81
|
+
fetchedAt: number;
|
|
82
|
+
sourceAt: number;
|
|
83
|
+
prices: Record<string, number>;
|
|
84
|
+
yesterday: Record<string, number>;
|
|
85
|
+
}
|
|
86
|
+
/** The price sections held in memory and in localStorage */
|
|
87
|
+
interface PriceSnapshot {
|
|
88
|
+
fiat?: FiatSnapshot;
|
|
89
|
+
crypto?: CryptoSnapshot;
|
|
90
|
+
}
|
|
91
|
+
/** Price sections one request has to read */
|
|
92
|
+
interface PriceSections {
|
|
93
|
+
fiat: boolean;
|
|
94
|
+
crypto: boolean;
|
|
95
|
+
}
|
|
96
|
+
/** One conversion returned to the model */
|
|
97
|
+
interface CurrencyResult {
|
|
98
|
+
from: string;
|
|
99
|
+
to: string;
|
|
100
|
+
amount: number;
|
|
101
|
+
rate: number;
|
|
102
|
+
previousRate?: number;
|
|
103
|
+
change?: number;
|
|
104
|
+
changePercent?: number;
|
|
105
|
+
result: number;
|
|
106
|
+
updatedAt: string;
|
|
107
|
+
stale: boolean;
|
|
108
|
+
}
|
|
109
|
+
/** Tool fields shared by the host input and one generation */
|
|
110
|
+
interface ChatToolParams {
|
|
111
|
+
/** Extra host tools, appended after the built-ins */
|
|
112
|
+
tools?: ChatTool[];
|
|
113
|
+
/** Called before a host or built-in tool runs */
|
|
114
|
+
onToolCall?: (call: ChatToolCall) => void;
|
|
115
|
+
/** Called when a tool returns or throws */
|
|
116
|
+
onToolResult?: (result: ChatToolResult) => void;
|
|
117
|
+
/** Max tool rounds in one reply, defaults to 4 */
|
|
118
|
+
maxToolRounds?: number;
|
|
119
|
+
}
|
|
120
|
+
/** Host tools resolved against the built-ins for one request */
|
|
121
|
+
interface ResolvedTools {
|
|
122
|
+
/** Tools offered to the model, host tools replacing a built-in */
|
|
123
|
+
tools: ChatTool[];
|
|
124
|
+
/** Whether the host passed tools of its own */
|
|
125
|
+
explicit: boolean;
|
|
126
|
+
}
|
|
127
|
+
/** One evaluated arithmetic expression */
|
|
128
|
+
interface CalculationResult {
|
|
129
|
+
/** Normalized expression that was evaluated */
|
|
130
|
+
expression: string;
|
|
131
|
+
result: number;
|
|
132
|
+
}
|
|
133
|
+
/** One page of a Wikipedia search response */
|
|
134
|
+
interface WikipediaPage {
|
|
135
|
+
key?: string;
|
|
136
|
+
title?: string;
|
|
137
|
+
description?: string;
|
|
138
|
+
excerpt?: string;
|
|
139
|
+
}
|
|
140
|
+
/** One Wikipedia search response */
|
|
141
|
+
interface WikipediaResponse {
|
|
142
|
+
pages?: WikipediaPage[];
|
|
143
|
+
}
|
|
144
|
+
/** One DuckDuckGo topic, a group carries nested topics */
|
|
145
|
+
interface DuckDuckGoTopic {
|
|
146
|
+
FirstURL?: string;
|
|
147
|
+
Text?: string;
|
|
148
|
+
Topics?: DuckDuckGoTopic[];
|
|
149
|
+
}
|
|
150
|
+
/** One DuckDuckGo instant answer response */
|
|
151
|
+
interface DuckDuckGoResponse {
|
|
152
|
+
Heading?: string;
|
|
153
|
+
AbstractText?: string;
|
|
154
|
+
AbstractURL?: string;
|
|
155
|
+
Results?: DuckDuckGoTopic[];
|
|
156
|
+
RelatedTopics?: DuckDuckGoTopic[];
|
|
157
|
+
}
|
|
158
|
+
/** One crypto row of the price feed */
|
|
159
|
+
interface PriceCoin {
|
|
160
|
+
/** Price in EUR per unit */
|
|
161
|
+
pr?: number;
|
|
162
|
+
/** Price in EUR per unit, 24 hours earlier */
|
|
163
|
+
yst?: number;
|
|
164
|
+
}
|
|
165
|
+
/** The CoinExams price feed, validated before use */
|
|
166
|
+
interface PriceFeed {
|
|
167
|
+
rates?: {
|
|
168
|
+
/** Feed timestamp of the fiat and metal rates, milliseconds */
|
|
169
|
+
date?: number;
|
|
170
|
+
/** Units of each symbol per EUR */
|
|
171
|
+
current?: Record<string, number>;
|
|
172
|
+
/** The same rates, 24 hours earlier */
|
|
173
|
+
yesterday?: Record<string, number>;
|
|
174
|
+
};
|
|
175
|
+
coins?: Record<string, PriceCoin>;
|
|
176
|
+
/** Feed timestamp of the crypto prices, milliseconds */
|
|
177
|
+
date?: number;
|
|
178
|
+
}
|
|
11
179
|
/** One LiteRT-LM model file and its capabilities */
|
|
12
180
|
interface LitertArtifact {
|
|
13
181
|
/** File name inside the model repository */
|
|
@@ -16,6 +184,8 @@ interface LitertArtifact {
|
|
|
16
184
|
vision: boolean;
|
|
17
185
|
/** Whether the build accepts audio */
|
|
18
186
|
audio: boolean;
|
|
187
|
+
/** Whether the build accepts tool declarations */
|
|
188
|
+
tools: boolean;
|
|
19
189
|
}
|
|
20
190
|
/** One on-device model */
|
|
21
191
|
interface ChatModel {
|
|
@@ -70,6 +240,8 @@ interface ChatStatusSnapshot {
|
|
|
70
240
|
supportsImages: boolean;
|
|
71
241
|
/** Whether the requested model accepts audio on this backend */
|
|
72
242
|
supportsAudio: boolean;
|
|
243
|
+
/** Whether the requested model accepts tool declarations on this backend */
|
|
244
|
+
supportsTools: boolean;
|
|
73
245
|
ready: boolean;
|
|
74
246
|
loading: boolean;
|
|
75
247
|
/** Whether a reply is streaming right now */
|
|
@@ -81,7 +253,7 @@ interface ChatStatusSnapshot {
|
|
|
81
253
|
error?: string;
|
|
82
254
|
}
|
|
83
255
|
/** One chat request, invoked for every response */
|
|
84
|
-
interface ChatInput {
|
|
256
|
+
interface ChatInput extends ChatToolParams {
|
|
85
257
|
/** Model that answers, the GPU model when WebGPU is available, else the CPU one */
|
|
86
258
|
model?: ChatModelId;
|
|
87
259
|
/** Complete prompt, including the system and user turns */
|
|
@@ -92,6 +264,10 @@ interface ChatInput {
|
|
|
92
264
|
reasoning?: boolean;
|
|
93
265
|
/** Whether package and engine logs reach the console, defaults to off */
|
|
94
266
|
logging?: boolean;
|
|
267
|
+
/** Built-in tools to attach, all three by default */
|
|
268
|
+
builtins?: BuiltinTool[] | false;
|
|
269
|
+
/** Search provider settings */
|
|
270
|
+
websearch?: WebSearchConfig;
|
|
95
271
|
/** Accumulated reasoning text, streamed while the model thinks */
|
|
96
272
|
onThinking?: (text: string) => void;
|
|
97
273
|
/** Accumulated reply text, streamed while the model answers */
|
|
@@ -238,7 +414,7 @@ interface ModelStore {
|
|
|
238
414
|
purge: () => Promise<void>;
|
|
239
415
|
}
|
|
240
416
|
/** One generation request */
|
|
241
|
-
interface RuntimeChatParams {
|
|
417
|
+
interface RuntimeChatParams extends ChatToolParams {
|
|
242
418
|
messages: ChatMessage[];
|
|
243
419
|
limits: LlmLimits;
|
|
244
420
|
reasoning: boolean;
|
|
@@ -246,6 +422,8 @@ interface RuntimeChatParams {
|
|
|
246
422
|
logging?: boolean;
|
|
247
423
|
onReason: (text: string) => void;
|
|
248
424
|
onText: (text: string) => void;
|
|
425
|
+
/** Called before a rebuilt engine restarts the reply */
|
|
426
|
+
onRestart?: () => void;
|
|
249
427
|
/** Current generation speed in tokens per second */
|
|
250
428
|
onSpeed?: (tokensPerSecond: number) => void;
|
|
251
429
|
signal?: AbortSignal;
|
|
@@ -266,7 +444,7 @@ interface Runtime {
|
|
|
266
444
|
model: () => ChatModel | undefined;
|
|
267
445
|
}
|
|
268
446
|
/** Params for a single on-device chat generation */
|
|
269
|
-
interface EngineChatParams {
|
|
447
|
+
interface EngineChatParams extends ChatToolParams {
|
|
270
448
|
messages: ChatMessage[];
|
|
271
449
|
limits: LlmLimits;
|
|
272
450
|
/** User preference, off unless explicitly turned on */
|
|
@@ -277,6 +455,8 @@ interface EngineChatParams {
|
|
|
277
455
|
onReason: (text: string) => void;
|
|
278
456
|
/** Streamed reply delta */
|
|
279
457
|
onText: (text: string) => void;
|
|
458
|
+
/** Called before a rebuilt engine restarts the reply */
|
|
459
|
+
onRestart?: () => void;
|
|
280
460
|
/** Current generation speed in tokens per second */
|
|
281
461
|
onSpeed?: (tokensPerSecond: number) => void;
|
|
282
462
|
signal?: AbortSignal;
|
|
@@ -305,6 +485,8 @@ interface LlmRuntime {
|
|
|
305
485
|
contextSize: number;
|
|
306
486
|
/** Stream one reply, returning the final text or undefined when stopped */
|
|
307
487
|
chat: (params: EngineChatParams) => Promise<string | undefined>;
|
|
488
|
+
/** Whether the runtime cannot serve another turn */
|
|
489
|
+
fatal?: () => boolean;
|
|
308
490
|
/** Release the model from memory, keeping its cache */
|
|
309
491
|
unload: () => Promise<void>;
|
|
310
492
|
}
|
|
@@ -372,6 +554,23 @@ type LitertContentPart = import('@litert-lm/core').ContentPart;
|
|
|
372
554
|
type LitertMessage = import('@litert-lm/core').Message;
|
|
373
555
|
/** One LiteRT-LM engine */
|
|
374
556
|
type LitertEngine = import('@litert-lm/core').Engine;
|
|
557
|
+
/** WebGPU device the wasm module initialized */
|
|
558
|
+
type LitertDevice = NonNullable<import('@litert-lm/core').Wasm.LiteRtLmWasm[`preinitializedWebGPUDevice`]>;
|
|
559
|
+
/** One live LiteRT-LM engine and its device */
|
|
560
|
+
interface LitertGpu {
|
|
561
|
+
/** Live engine, rebuilt after a device loss */
|
|
562
|
+
engine: LitertEngine;
|
|
563
|
+
/** Wasm module the engine belongs to */
|
|
564
|
+
mod: LitertModule;
|
|
565
|
+
/** Device the engine runs on */
|
|
566
|
+
device?: LitertDevice;
|
|
567
|
+
/** Whether the device was lost */
|
|
568
|
+
lost: boolean;
|
|
569
|
+
/** Context window step of this engine */
|
|
570
|
+
contextStep: number;
|
|
571
|
+
/** Model file stream of a rebuild */
|
|
572
|
+
reopen: () => Promise<ReadableStream<Uint8Array>>;
|
|
573
|
+
}
|
|
375
574
|
/** The LiteRT-LM module namespace */
|
|
376
575
|
type LitertModule = typeof import('@litert-lm/core');
|
|
377
576
|
/** Settings handed to the LiteRT-LM engine */
|
|
@@ -386,23 +585,26 @@ interface LitertSettingsParams {
|
|
|
386
585
|
source: ReadableStream<Uint8Array>;
|
|
387
586
|
/** Loaded LiteRT module */
|
|
388
587
|
mod: LitertModule;
|
|
588
|
+
/** Context window of this engine */
|
|
589
|
+
contextTokens?: number;
|
|
389
590
|
}
|
|
390
591
|
/** One reused LiteRT-LM conversation and the turns it already holds */
|
|
391
592
|
interface LitertSession {
|
|
392
|
-
/** Live conversation, dropped when a turn cannot be continued */
|
|
393
|
-
conversation?: import('@litert-lm/core').
|
|
593
|
+
/** Live conversation or tool chat, dropped when a turn cannot be continued */
|
|
594
|
+
conversation?: import('@litert-lm/core').ChatInterface;
|
|
394
595
|
/** Built turns already sent to the conversation */
|
|
395
596
|
sent: EngineMessage[];
|
|
396
597
|
/** Reasoning flag the conversation was created with */
|
|
397
598
|
reasoning: boolean;
|
|
398
599
|
/** Output token budget the conversation was created with */
|
|
399
600
|
maxTokens: number;
|
|
601
|
+
/** Tools the conversation was created with */
|
|
602
|
+
tools?: ChatTool[];
|
|
400
603
|
}
|
|
401
604
|
/** Params of one LiteRT-LM chat call */
|
|
402
605
|
interface LitertChatParams {
|
|
403
|
-
|
|
606
|
+
gpu: LitertGpu;
|
|
404
607
|
model: ChatModel;
|
|
405
|
-
mod: LitertModule;
|
|
406
608
|
params: EngineChatParams;
|
|
407
609
|
session: LitertSession;
|
|
408
610
|
}
|
|
@@ -460,4 +662,4 @@ interface EngineRunParams {
|
|
|
460
662
|
/** Called when a slow stream keeps the text already delivered */
|
|
461
663
|
onStall?: () => void;
|
|
462
664
|
}
|
|
463
|
-
export type { ChatMessage, ChatModel, LitertArtifact, LlmLimits, ChatStatusSnapshot, ChatInput, ChatPreloadInput, ChatState, CancelChat, WllamaAssets, LitertAssets, GpuNavigator, MobileNavigator, ActiveCall, RuntimeDeps, RuntimeLoadParams, RuntimeLoadWaiter, RuntimeChatParams, StoredPart, StoredFilePart, StoredModel, StoredModelMeta, ResumePlan, StoreDownloadParams, ModelStore, Runtime, EngineChatParams, StartLlmParams, LlmRuntime, LlmEngine, EngineMessage, EngineContentPart, WllamaContentPart, WllamaMessage, LitertContentPart, LitertMessage, LitertEngine, LitertModule, LitertSettings, LitertSettingsParams, LitertSession, LitertChatParams, StreamLitertParams, EngineDelta, EngineChunk, StreamReplyParams, EngineRunParams, };
|
|
665
|
+
export type { ChatMessage, ChatModel, LitertArtifact, ChatTool, ChatToolCall, ChatToolResult, ChatToolSchema, ChatToolValue, ChatToolParams, ResolvedTools, CalculationResult, WebSearchConfig, WebSearchResult, WebSearchSettings, FiatSnapshot, CryptoSnapshot, PriceSnapshot, PriceSections, CurrencyResult, WikipediaPage, WikipediaResponse, DuckDuckGoTopic, DuckDuckGoResponse, PriceCoin, PriceFeed, LlmLimits, ChatStatusSnapshot, ChatInput, ChatPreloadInput, ChatState, CancelChat, WllamaAssets, LitertAssets, GpuNavigator, MobileNavigator, ActiveCall, RuntimeDeps, RuntimeLoadParams, RuntimeLoadWaiter, RuntimeChatParams, StoredPart, StoredFilePart, StoredModel, StoredModelMeta, ResumePlan, StoreDownloadParams, ModelStore, Runtime, EngineChatParams, StartLlmParams, LlmRuntime, LlmEngine, EngineMessage, EngineContentPart, WllamaContentPart, WllamaMessage, LitertContentPart, LitertMessage, LitertEngine, LitertModule, LitertDevice, LitertGpu, LitertSettings, LitertSettingsParams, LitertSession, LitertChatParams, StreamLitertParams, EngineDelta, EngineChunk, StreamReplyParams, EngineRunParams, };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@degreesign/chat",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "Headless on-device AI chat for browsers and Node with wllama CPU and LiteRT WebGPU engines",
|
|
5
5
|
"main": "dist/node/chat.node.min.js",
|
|
6
6
|
"module": "dist/browser/chat.min.mjs",
|
|
@@ -61,6 +61,7 @@
|
|
|
61
61
|
"dist/"
|
|
62
62
|
],
|
|
63
63
|
"dependencies": {
|
|
64
|
+
"@degreesign/utils": "^1.0.5",
|
|
64
65
|
"@litert-lm/core": "0.19.0-nightly.20261024",
|
|
65
66
|
"@wllama/wllama": "^3.8.1",
|
|
66
67
|
"@wllama/wllama-compat": "^3.8.1"
|