@degreesign/chat 0.5.2 → 0.6.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,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;
@@ -266,7 +442,7 @@ interface Runtime {
266
442
  model: () => ChatModel | undefined;
267
443
  }
268
444
  /** Params for a single on-device chat generation */
269
- interface EngineChatParams {
445
+ interface EngineChatParams extends ChatToolParams {
270
446
  messages: ChatMessage[];
271
447
  limits: LlmLimits;
272
448
  /** User preference, off unless explicitly turned on */
@@ -389,14 +565,16 @@ interface LitertSettingsParams {
389
565
  }
390
566
  /** One reused LiteRT-LM conversation and the turns it already holds */
391
567
  interface LitertSession {
392
- /** Live conversation, dropped when a turn cannot be continued */
393
- conversation?: import('@litert-lm/core').Conversation;
568
+ /** Live conversation or tool chat, dropped when a turn cannot be continued */
569
+ conversation?: import('@litert-lm/core').ChatInterface;
394
570
  /** Built turns already sent to the conversation */
395
571
  sent: EngineMessage[];
396
572
  /** Reasoning flag the conversation was created with */
397
573
  reasoning: boolean;
398
574
  /** Output token budget the conversation was created with */
399
575
  maxTokens: number;
576
+ /** Tools the conversation was created with */
577
+ tools?: ChatTool[];
400
578
  }
401
579
  /** Params of one LiteRT-LM chat call */
402
580
  interface LitertChatParams {
@@ -460,4 +638,4 @@ interface EngineRunParams {
460
638
  /** Called when a slow stream keeps the text already delivered */
461
639
  onStall?: () => void;
462
640
  }
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, };
641
+ 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, 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.5.2",
3
+ "version": "0.6.0",
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"