@uptimizr/agent-core 0.2.0 → 0.2.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/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/loop.d.ts +36 -0
- package/dist/loop.d.ts.map +1 -1
- package/dist/loop.js +76 -3
- package/dist/loop.js.map +1 -1
- package/dist/providers/anthropic.d.ts +2 -1
- package/dist/providers/anthropic.d.ts.map +1 -1
- package/dist/providers/anthropic.js +11 -5
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/hosted.js +6 -2
- package/dist/providers/hosted.js.map +1 -1
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +1 -1
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/webllm.d.ts +88 -2
- package/dist/providers/webllm.d.ts.map +1 -1
- package/dist/providers/webllm.js +215 -13
- package/dist/providers/webllm.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { createCollectorClient, CollectorError, type CollectorClient, type CollectorClientConfig, type QueryParams, } from "./client.js";
|
|
2
2
|
export { readTools, type ReadTool, type ReadToolRequest } from "./tools.js";
|
|
3
3
|
export type { AgentMessage, AgentToolCall, AgentToolSchema, LlmProvider, ProviderRequest, ProviderResponse, } from "./provider.js";
|
|
4
|
-
export { runAgent, toToolSchemas, DEFAULT_MAX_STEPS, type RunAgentOptions, type RunAgentResult, } from "./loop.js";
|
|
4
|
+
export { runAgent, toToolSchemas, truncateToolResult, DEFAULT_MAX_STEPS, DEFAULT_MAX_TOOL_RESULT_CHARS, type RunAgentOptions, type RunAgentResult, } from "./loop.js";
|
|
5
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,cAAc,EACd,KAAK,eAAe,EACpB,KAAK,qBAAqB,EAC1B,KAAK,WAAW,GACjB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,SAAS,EAAE,KAAK,QAAQ,EAAE,KAAK,eAAe,EAAE,MAAM,YAAY,CAAC;AAC5E,YAAY,EACV,YAAY,EACZ,aAAa,EACb,eAAe,EACf,WAAW,EACX,eAAe,EACf,gBAAgB,GACjB,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,QAAQ,EACR,aAAa,EACb,iBAAiB,EACjB,KAAK,eAAe,EACpB,KAAK,cAAc,GACpB,MAAM,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,cAAc,EACd,KAAK,eAAe,EACpB,KAAK,qBAAqB,EAC1B,KAAK,WAAW,GACjB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,SAAS,EAAE,KAAK,QAAQ,EAAE,KAAK,eAAe,EAAE,MAAM,YAAY,CAAC;AAC5E,YAAY,EACV,YAAY,EACZ,aAAa,EACb,eAAe,EACf,WAAW,EACX,eAAe,EACf,gBAAgB,GACjB,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,QAAQ,EACR,aAAa,EACb,kBAAkB,EAClB,iBAAiB,EACjB,6BAA6B,EAC7B,KAAK,eAAe,EACpB,KAAK,cAAc,GACpB,MAAM,WAAW,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { createCollectorClient, CollectorError, } from "./client.js";
|
|
2
2
|
export { readTools } from "./tools.js";
|
|
3
|
-
export { runAgent, toToolSchemas, DEFAULT_MAX_STEPS, } from "./loop.js";
|
|
3
|
+
export { runAgent, toToolSchemas, truncateToolResult, DEFAULT_MAX_STEPS, DEFAULT_MAX_TOOL_RESULT_CHARS, } from "./loop.js";
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,cAAc,GAIf,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,SAAS,EAAuC,MAAM,YAAY,CAAC;AAS5E,OAAO,EACL,QAAQ,EACR,aAAa,EACb,iBAAiB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,cAAc,GAIf,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,SAAS,EAAuC,MAAM,YAAY,CAAC;AAS5E,OAAO,EACL,QAAQ,EACR,aAAa,EACb,kBAAkB,EAClB,iBAAiB,EACjB,6BAA6B,GAG9B,MAAM,WAAW,CAAC"}
|
package/dist/loop.d.ts
CHANGED
|
@@ -3,6 +3,21 @@ import type { AgentMessage, AgentToolSchema, LlmProvider } from "./provider.js";
|
|
|
3
3
|
import { type ReadTool } from "./tools.js";
|
|
4
4
|
/** Default cap on provider turns before the loop gives up (guards against loops). */
|
|
5
5
|
export declare const DEFAULT_MAX_STEPS = 8;
|
|
6
|
+
/**
|
|
7
|
+
* Default cap on the number of characters of a single tool result fed back to
|
|
8
|
+
* the model. Large analytics JSON blobs can blow a small local model's context
|
|
9
|
+
* window (8192 tokens for the curated Hermes records) and degrade it into
|
|
10
|
+
* empty/looping output, so results above this length are truncated with a clear
|
|
11
|
+
* marker. Chosen to comfortably fit several tool results plus the prompt.
|
|
12
|
+
*/
|
|
13
|
+
export declare const DEFAULT_MAX_TOOL_RESULT_CHARS = 8000;
|
|
14
|
+
/**
|
|
15
|
+
* Truncate an over-long tool result to `max` characters, appending a clear
|
|
16
|
+
* marker noting how many characters were dropped. Below the cap the content is
|
|
17
|
+
* returned untouched (full fidelity). Plain string slicing only — no regex, so
|
|
18
|
+
* there is no ReDoS surface on model-influenced tool output.
|
|
19
|
+
*/
|
|
20
|
+
export declare function truncateToolResult(content: string, max: number): string;
|
|
6
21
|
/**
|
|
7
22
|
* Convert the read-only tool catalog into provider-facing tool schemas. Each
|
|
8
23
|
* tool's Zod raw shape becomes a JSON-Schema object the model can reason about.
|
|
@@ -20,6 +35,21 @@ export interface RunAgentOptions {
|
|
|
20
35
|
tools?: readonly ReadTool[];
|
|
21
36
|
/** Maximum provider turns before returning. Defaults to {@link DEFAULT_MAX_STEPS}. */
|
|
22
37
|
maxSteps?: number;
|
|
38
|
+
/**
|
|
39
|
+
* When the model ends a run without a usable answer — either an empty `final`
|
|
40
|
+
* or by exhausting `maxSteps` while still tool-calling — make ONE extra
|
|
41
|
+
* `provider.complete()` with tools disabled, forcing it to synthesize a plain
|
|
42
|
+
* prose answer from the tool results it already gathered. At most one such
|
|
43
|
+
* forced turn per run. Defaults to `true`. Set `false` to keep the old
|
|
44
|
+
* empty-answer behavior. Benefits every consumer (dashboard assistant, MCP).
|
|
45
|
+
*/
|
|
46
|
+
forceFinalAnswer?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Maximum characters of a single tool result fed back to the model; longer
|
|
49
|
+
* results are truncated with a marker to protect small models' context.
|
|
50
|
+
* Defaults to {@link DEFAULT_MAX_TOOL_RESULT_CHARS}.
|
|
51
|
+
*/
|
|
52
|
+
maxToolResultChars?: number;
|
|
23
53
|
/** Optional cancellation signal forwarded to the provider. */
|
|
24
54
|
signal?: AbortSignal;
|
|
25
55
|
}
|
|
@@ -42,6 +72,12 @@ export interface RunAgentResult {
|
|
|
42
72
|
*
|
|
43
73
|
* The loop only ever issues collector `GET`s through the catalog's
|
|
44
74
|
* `buildRequest` — it can neither ingest nor mutate data (ADR 0003 / 0017).
|
|
75
|
+
*
|
|
76
|
+
* When a run would otherwise end without a usable answer (an empty `final`, or
|
|
77
|
+
* the step cap reached while still tool-calling) and `forceFinalAnswer` is on
|
|
78
|
+
* (default), it makes ONE extra tools-disabled `provider.complete()` so the
|
|
79
|
+
* model synthesizes a prose answer from the tool results it already gathered —
|
|
80
|
+
* at most one such forced turn per run.
|
|
45
81
|
*/
|
|
46
82
|
export declare function runAgent(options: RunAgentOptions): Promise<RunAgentResult>;
|
|
47
83
|
//# sourceMappingURL=loop.d.ts.map
|
package/dist/loop.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loop.d.ts","sourceRoot":"","sources":["../src/loop.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,KAAK,EAAE,YAAY,EAAiB,eAAe,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC/F,OAAO,EAAa,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtD,qFAAqF;AACrF,eAAO,MAAM,iBAAiB,IAAI,CAAC;AAEnC;;;GAGG;AACH,wBAAgB,aAAa,CAAC,KAAK,GAAE,SAAS,QAAQ,EAAc,GAAG,eAAe,EAAE,CAMvF;AAED,wDAAwD;AACxD,MAAM,WAAW,eAAe;IAC9B,iDAAiD;IACjD,QAAQ,EAAE,WAAW,CAAC;IACtB,6DAA6D;IAC7D,MAAM,EAAE,eAAe,CAAC;IACxB,4EAA4E;IAC5E,QAAQ,EAAE,YAAY,EAAE,CAAC;IACzB,wEAAwE;IACxE,KAAK,CAAC,EAAE,SAAS,QAAQ,EAAE,CAAC;IAC5B,sFAAsF;IACtF,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,8DAA8D;IAC9D,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,4CAA4C;AAC5C,MAAM,WAAW,cAAc;IAC7B,kFAAkF;IAClF,OAAO,EAAE,MAAM,CAAC;IAChB,4EAA4E;IAC5E,QAAQ,EAAE,YAAY,EAAE,CAAC;IACzB,0CAA0C;IA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AC5E,QAAQ,EAAE,YAAY,EAAE,CAAC;IACzB,0CAA0C;IAC1C,KAAK,EAAE,MAAM,CAAC;IACd,uFAAuF;IACvF,iBAAiB,EAAE,OAAO,CAAC;CAC5B;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,QAAQ,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,cAAc,CAAC,CA0EhF"}
|
package/dist/loop.js
CHANGED
|
@@ -2,6 +2,34 @@ import { z } from "zod";
|
|
|
2
2
|
import { readTools } from "./tools.js";
|
|
3
3
|
/** Default cap on provider turns before the loop gives up (guards against loops). */
|
|
4
4
|
export const DEFAULT_MAX_STEPS = 8;
|
|
5
|
+
/**
|
|
6
|
+
* Default cap on the number of characters of a single tool result fed back to
|
|
7
|
+
* the model. Large analytics JSON blobs can blow a small local model's context
|
|
8
|
+
* window (8192 tokens for the curated Hermes records) and degrade it into
|
|
9
|
+
* empty/looping output, so results above this length are truncated with a clear
|
|
10
|
+
* marker. Chosen to comfortably fit several tool results plus the prompt.
|
|
11
|
+
*/
|
|
12
|
+
export const DEFAULT_MAX_TOOL_RESULT_CHARS = 8000;
|
|
13
|
+
/**
|
|
14
|
+
* Directive sent — as a trailing `user` message — on the forced, tools-disabled
|
|
15
|
+
* synthesis turn (see {@link runAgent}). It tells the model to stop calling
|
|
16
|
+
* tools and answer in plain prose from the data it already gathered. Kept out of
|
|
17
|
+
* the returned transcript: only the resulting answer is persisted.
|
|
18
|
+
*/
|
|
19
|
+
const FORCE_FINAL_DIRECTIVE = "Now answer the user's question directly in plain text, using the tool results already " +
|
|
20
|
+
"gathered above. Do not call any tools. If the data is insufficient, say so briefly.";
|
|
21
|
+
/**
|
|
22
|
+
* Truncate an over-long tool result to `max` characters, appending a clear
|
|
23
|
+
* marker noting how many characters were dropped. Below the cap the content is
|
|
24
|
+
* returned untouched (full fidelity). Plain string slicing only — no regex, so
|
|
25
|
+
* there is no ReDoS surface on model-influenced tool output.
|
|
26
|
+
*/
|
|
27
|
+
export function truncateToolResult(content, max) {
|
|
28
|
+
if (content.length <= max)
|
|
29
|
+
return content;
|
|
30
|
+
const dropped = content.length - max;
|
|
31
|
+
return `${content.slice(0, max)}…[truncated ${dropped} chars]`;
|
|
32
|
+
}
|
|
5
33
|
/**
|
|
6
34
|
* Convert the read-only tool catalog into provider-facing tool schemas. Each
|
|
7
35
|
* tool's Zod raw shape becomes a JSON-Schema object the model can reason about.
|
|
@@ -21,19 +49,54 @@ export function toToolSchemas(tools = readTools) {
|
|
|
21
49
|
*
|
|
22
50
|
* The loop only ever issues collector `GET`s through the catalog's
|
|
23
51
|
* `buildRequest` — it can neither ingest nor mutate data (ADR 0003 / 0017).
|
|
52
|
+
*
|
|
53
|
+
* When a run would otherwise end without a usable answer (an empty `final`, or
|
|
54
|
+
* the step cap reached while still tool-calling) and `forceFinalAnswer` is on
|
|
55
|
+
* (default), it makes ONE extra tools-disabled `provider.complete()` so the
|
|
56
|
+
* model synthesizes a prose answer from the tool results it already gathered —
|
|
57
|
+
* at most one such forced turn per run.
|
|
24
58
|
*/
|
|
25
59
|
export async function runAgent(options) {
|
|
26
60
|
const { provider, client, signal } = options;
|
|
27
61
|
const tools = options.tools ?? readTools;
|
|
28
62
|
const maxSteps = options.maxSteps ?? DEFAULT_MAX_STEPS;
|
|
63
|
+
const forceFinalAnswer = options.forceFinalAnswer ?? true;
|
|
64
|
+
const maxToolResultChars = options.maxToolResultChars ?? DEFAULT_MAX_TOOL_RESULT_CHARS;
|
|
29
65
|
const toolsByName = new Map(tools.map((t) => [t.name, t]));
|
|
30
66
|
const toolSchemas = toToolSchemas(tools);
|
|
31
67
|
const messages = [...options.messages];
|
|
32
68
|
let steps = 0;
|
|
69
|
+
let alreadyForced = false;
|
|
70
|
+
/**
|
|
71
|
+
* One tools-disabled synthesis turn: ask the provider to answer in prose from
|
|
72
|
+
* the tool results already in `messages`. Sends no tools (so adapters omit
|
|
73
|
+
* function-calling entirely) plus a trailing directive that is NOT persisted
|
|
74
|
+
* into the transcript. Returns the answer text (possibly empty). Runs at most
|
|
75
|
+
* once per {@link runAgent} call via the shared `alreadyForced` guard.
|
|
76
|
+
*/
|
|
77
|
+
async function forceFinalSynthesis() {
|
|
78
|
+
alreadyForced = true;
|
|
79
|
+
const forcedMessages = [
|
|
80
|
+
...messages,
|
|
81
|
+
{ role: "user", content: FORCE_FINAL_DIRECTIVE },
|
|
82
|
+
];
|
|
83
|
+
const response = await provider.complete({ messages: forcedMessages, tools: [], signal });
|
|
84
|
+
return response.kind === "final" ? response.content : (response.content ?? "");
|
|
85
|
+
}
|
|
33
86
|
while (steps < maxSteps) {
|
|
34
87
|
steps += 1;
|
|
35
88
|
const response = await provider.complete({ messages, tools: toolSchemas, signal });
|
|
36
89
|
if (response.kind === "final") {
|
|
90
|
+
// The model may emit an empty final early — the reported failure where the
|
|
91
|
+
// user sees no answer. Force one tools-disabled synthesis pass to recover a
|
|
92
|
+
// real answer from any gathered tool results before returning empty.
|
|
93
|
+
if (response.content.trim() === "" && forceFinalAnswer && !alreadyForced) {
|
|
94
|
+
const forced = await forceFinalSynthesis();
|
|
95
|
+
if (forced.trim() !== "") {
|
|
96
|
+
messages.push({ role: "assistant", content: forced });
|
|
97
|
+
return { content: forced, messages, steps, stoppedOnMaxSteps: false };
|
|
98
|
+
}
|
|
99
|
+
}
|
|
37
100
|
messages.push({ role: "assistant", content: response.content });
|
|
38
101
|
return { content: response.content, messages, steps, stoppedOnMaxSteps: false };
|
|
39
102
|
}
|
|
@@ -43,7 +106,17 @@ export async function runAgent(options) {
|
|
|
43
106
|
toolCalls: response.toolCalls,
|
|
44
107
|
});
|
|
45
108
|
for (const call of response.toolCalls) {
|
|
46
|
-
messages.push(await executeToolCall(client, toolsByName.get(call.name), call));
|
|
109
|
+
messages.push(await executeToolCall(client, toolsByName.get(call.name), call, maxToolResultChars));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// Hit the step cap while still tool-calling: force one tools-disabled synthesis
|
|
113
|
+
// pass so the model composes an answer from what it gathered instead of the
|
|
114
|
+
// loop returning nothing.
|
|
115
|
+
if (forceFinalAnswer && !alreadyForced) {
|
|
116
|
+
const forced = await forceFinalSynthesis();
|
|
117
|
+
if (forced.trim() !== "") {
|
|
118
|
+
messages.push({ role: "assistant", content: forced });
|
|
119
|
+
return { content: forced, messages, steps, stoppedOnMaxSteps: true };
|
|
47
120
|
}
|
|
48
121
|
}
|
|
49
122
|
return { content: "", messages, steps, stoppedOnMaxSteps: true };
|
|
@@ -54,7 +127,7 @@ export async function runAgent(options) {
|
|
|
54
127
|
* can see the failure and recover on the next turn. Collector errors are
|
|
55
128
|
* likewise surfaced as text.
|
|
56
129
|
*/
|
|
57
|
-
async function executeToolCall(client, tool, call) {
|
|
130
|
+
async function executeToolCall(client, tool, call, maxToolResultChars) {
|
|
58
131
|
const result = (content) => ({
|
|
59
132
|
role: "tool",
|
|
60
133
|
toolCallId: call.id,
|
|
@@ -74,7 +147,7 @@ async function executeToolCall(client, tool, call) {
|
|
|
74
147
|
try {
|
|
75
148
|
const { path, params } = tool.buildRequest(parsed.data);
|
|
76
149
|
const data = await client.get(path, params);
|
|
77
|
-
return result(JSON.stringify(data));
|
|
150
|
+
return result(truncateToolResult(JSON.stringify(data), maxToolResultChars));
|
|
78
151
|
}
|
|
79
152
|
catch (err) {
|
|
80
153
|
const message = err instanceof Error ? err.message : String(err);
|
package/dist/loop.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loop.js","sourceRoot":"","sources":["../src/loop.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EAAE,SAAS,EAAiB,MAAM,YAAY,CAAC;AAEtD,qFAAqF;AACrF,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAEnC;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,QAA6B,SAAS;IAClE,OAAO,KAAK,CAAC,GAAG,CAAC,CAA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E,CAAC,CAAC;IAC1E,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACpB,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM;aAC/B,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,QAAQ,KAAK,CAAC,CAAC,OAAO,EAAE,CAAC;aAC3D,IAAI,CAAC,IAAI,CAAC,CAAC;QACd,OAAO,MAAM,CAAC,iCAAiC,IAAI,CAAC,IAAI,MAAM,MAAM,EAAE,CAAC,CAAC;IAC1E,CAAC;IAED,IAAI,CAAC;QACH,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,IAA+B,CAAC,CAAC;QACnF,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAC5C,OAAO,MAAM,CAAC,kBAAkB,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAAC;IAC9E,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,MAAM,OAAO,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACjE,OAAO,MAAM,CAAC,UAAU,OAAO,EAAE,CAAC,CAAC;IACrC,CAAC;AACH,CAAC"}
|
|
@@ -36,7 +36,8 @@ export interface AnthropicTool {
|
|
|
36
36
|
export interface AnthropicRequestBody {
|
|
37
37
|
system?: string;
|
|
38
38
|
messages: AnthropicMessage[];
|
|
39
|
-
tools
|
|
39
|
+
/** Omitted when no tools are advertised (e.g. a forced plain-text turn). */
|
|
40
|
+
tools?: AnthropicTool[];
|
|
40
41
|
}
|
|
41
42
|
/** The subset of an Anthropic Messages response this module reads. */
|
|
42
43
|
export interface AnthropicCompletion {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,YAAY,EAEZ,eAAe,EACf,gBAAgB,EACjB,MAAM,gBAAgB,CAAC;AAExB,yDAAyD;AACzD,MAAM,MAAM,qBAAqB,GAC7B;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE,GAC9E;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAElE,oCAAoC;AACpC,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,GAAG,WAAW,CAAC;IAC3B,OAAO,EAAE,qBAAqB,EAAE,CAAC;CAClC;AAED,uCAAuC;AACvC,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACvC;AAED,4DAA4D;AAC5D,MAAM,WAAW,oBAAoB;IACnC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,gBAAgB,EAAE,CAAC;IAC7B,KAAK,EAAE,aAAa,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,YAAY,EAEZ,eAAe,EACf,gBAAgB,EACjB,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ages, tools) {
|
|
|
43
43
|
return {
|
|
44
44
|
...(systemParts.length > 0 ? { system: systemParts.join("\n\n") } : {}),
|
|
45
45
|
messages: out,
|
|
46
|
-
tools
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
// Omit `tools` when there are none (e.g. the loop's forced synthesis pass)
|
|
47
|
+
// so the model answers in plain text instead of being offered functions.
|
|
48
|
+
...(tools.length > 0
|
|
49
|
+
? {
|
|
50
|
+
tools: tools.map((tool) => ({
|
|
51
|
+
name: tool.name,
|
|
52
|
+
description: tool.description,
|
|
53
|
+
input_schema: tool.parameters,
|
|
54
|
+
})),
|
|
55
|
+
}
|
|
56
|
+
: {}),
|
|
51
57
|
};
|
|
52
58
|
}
|
|
53
59
|
function toToolUse(call) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;
|
|
1
|
+
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAyCH;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAChC,QAAiC,EACjC,KAAiC;IAEjC,MAAM,WAAW,GAAa,EAAE,CAAC;IACjC,MAAM,GAAG,GAAuB,EAAE,CAAC;IAEnC,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;QAC/B,QAAQ,OAAO,CAAC,IAAI,EAAE,CAAC;YACrB,KAAK,QAAQ;gBACX,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;gBAClC,MAAM;YACR,KAAK,MAAM;gBACT,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC;gB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CAAC"}
|
package/dist/providers/hosted.js
CHANGED
|
@@ -51,11 +51,15 @@ export function createHostedProvider(config) {
|
|
|
51
51
|
}
|
|
52
52
|
async function completeOpenAi(config, fetchImpl, request) {
|
|
53
53
|
const url = joinUrl(config.endpoint, "/chat/completions");
|
|
54
|
+
// No tools this turn (e.g. the loop's forced synthesis pass) → omit `tools`
|
|
55
|
+
// and `tool_choice` entirely so the model answers in plain text rather than
|
|
56
|
+
// being nudged to keep calling functions.
|
|
54
57
|
const body = {
|
|
55
58
|
model: config.model,
|
|
56
59
|
messages: toOpenAiMessages(request.messages),
|
|
57
|
-
|
|
58
|
-
|
|
60
|
+
...(request.tools.length > 0
|
|
61
|
+
? { tools: toOpenAiTools(request.tools), tool_choice: "auto" }
|
|
62
|
+
: {}),
|
|
59
63
|
};
|
|
60
64
|
const res = await fetchImpl(url, {
|
|
61
65
|
method: "POST",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hosted.js","sourceRoot":"","sources":["../../src/providers/hosted.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAIH,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EAChB,aAAa,GAEd,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,wBAAwB,EACxB,kBAAkB,GAEnB,MAAM,gBAAgB,CAAC;AAuBxB,sEAAsE;AACtE,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAGjC;IAFX,YACE,OAAe,EACN,MAAc;QAEvB,KAAK,CAAC,OAAO,CAAC,CAAC;QAFN,WAAM,GAAN,MAAM,CAAQ;QAGvB,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF;AAED,MAAM,kBAAkB,GAAG,IAAI,CAAC;AAChC,MAAM,yBAAyB,GAAG,YAAY,CAAC;AAE/C,SAAS,OAAO,CAAC,IAAY,EAAE,MAAc;IAC3C,IAAI,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC;IACtB,OAAO,GAAG,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AS,CAAC,GAAG,EAAE;QAC/B,MAAM,EAAE,MAAM;QACd,OAAO,EAAE;YACP,cAAc,EAAE,kBAAkB;YAClC,WAAW,EAAE,MAAM,CAAC,MAAM;YAC1B,mBAAmB,EAAE,MAAM,CAAC,gBAAgB,IAAI,yBAAyB;YACzE,sEAAsE;YACtE,2CAA2C,EAAE,MAAM;SACpD;QACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;QAC1B,MAAM,EAAE,OAAO,CAAC,MAAM;KACvB,CAAC,CAAC;IACH,MAAM,UAAU,GAAG,CAAC,MAAM,QAAQ,CAAC,GAAG,CAAC,CAAwB,CAAC;IAChE,OAAO,wBAAwB,CAAC,UAAU,CAAC,CAAC;AAC9C,CAAC;AAED,KAAK,UAAU,QAAQ,CAAC,GAAa;IACnC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;QACZ,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;QAChD,MAAM,IAAI,mBAAmB,CAAC,MAAM,IAAI,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACtE,CAAC;IACD,OAAO,GAAG,CAAC,IAAI,EAAE,CAAC;AACpB,CAAC"}
|
|
@@ -12,5 +12,5 @@
|
|
|
12
12
|
*/
|
|
13
13
|
export { type AssistantBackendConfig, type BackendKind, type HostedApi, type HostedBackendConfig, type KeyValueStorage, type WebLlmBackendConfig, BACKEND_CONFIG_STORAGE_KEY, clearBackendConfig, defaultBackendKind, isWebGpuAvailable, loadBackendConfig, saveBackendConfig, } from "./config.js";
|
|
14
14
|
export { type HostedProviderConfig, HostedProviderError, createHostedProvider } from "./hosted.js";
|
|
15
|
-
export { type CuratedModel, type InitProgress, type WebLlmEngine, type WebLlmProvider, type WebLlmProviderOptions, type WebLlmRuntime, CURATED_MODELS, SUPPORTED_TOOL_CALLING_MODELS, UnsupportedToolCallingModelError, WebGpuUnavailableError, WebLlmConsentError, createWebLlmProvider, } from "./webllm.js";
|
|
15
|
+
export { type CuratedModel, type InitProgress, type WebLlmEngine, type WebLlmProvider, type WebLlmProviderOptions, type WebLlmRuntime, CURATED_MODELS, SUPPORTED_TOOL_CALLING_MODELS, UnsupportedToolCallingModelError, WebGpuUnavailableError, WebLlmConsentError, WebLlmStorageError, createWebLlmProvider, isQuotaExceededError, } from "./webllm.js";
|
|
16
16
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EACL,KAAK,sBAAsB,EAC3B,KAAK,WAAW,EAChB,KAAK,SAAS,EACd,KAAK,mBAAmB,EACxB,KAAK,eAAe,EACpB,KAAK,mBAAmB,EACxB,0BAA0B,EAC1B,kBAAkB,EAClB,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,KAAK,oBAAoB,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAEnG,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,cAAc,EACnB,KAAK,qBAAqB,EAC1B,KAAK,aAAa,EAClB,cAAc,EACd,6BAA6B,EAC7B,gCAAgC,EAChC,sBAAsB,EACtB,kBAAkB,EAClB,oBAAoB,GACrB,MAAM,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EACL,KAAK,sBAAsB,EAC3B,KAAK,WAAW,EAChB,KAAK,SAAS,EACd,KAAK,mBAAmB,EACxB,KAAK,eAAe,EACpB,KAAK,mBAAmB,EACxB,0BAA0B,EAC1B,kBAAkB,EAClB,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,KAAK,oBAAoB,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAEnG,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,cAAc,EACnB,KAAK,qBAAqB,EAC1B,KAAK,aAAa,EAClB,cAAc,EACd,6BAA6B,EAC7B,gCAAgC,EAChC,sBAAsB,EACtB,kBAAkB,EAClB,kBAAkB,EAClB,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,aAAa,CAAC"}
|
package/dist/providers/index.js
CHANGED
|
@@ -12,5 +12,5 @@
|
|
|
12
12
|
*/
|
|
13
13
|
export { BACKEND_CONFIG_STORAGE_KEY, clearBackendConfig, defaultBackendKind, isWebGpuAvailable, loadBackendConfig, saveBackendConfig, } from "./config.js";
|
|
14
14
|
export { HostedProviderError, createHostedProvider } from "./hosted.js";
|
|
15
|
-
export { CURATED_MODELS, SUPPORTED_TOOL_CALLING_MODELS, UnsupportedToolCallingModelError, WebGpuUnavailableError, WebLlmConsentError, createWebLlmProvider, } from "./webllm.js";
|
|
15
|
+
export { CURATED_MODELS, SUPPORTED_TOOL_CALLING_MODELS, UnsupportedToolCallingModelError, WebGpuUnavailableError, WebLlmConsentError, WebLlmStorageError, createWebLlmProvider, isQuotaExceededError, } from "./webllm.js";
|
|
16
16
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAOL,0BAA0B,EAC1B,kBAAkB,EAClB,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAErB,OAAO,EAA6B,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAEnG,OAAO,EAOL,cAAc,EACd,6BAA6B,EAC7B,gCAAgC,EAChC,sBAAsB,EACtB,kBAAkB,EAClB,oBAAoB,GACrB,MAAM,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAOL,0BAA0B,EAC1B,kBAAkB,EAClB,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAErB,OAAO,EAA6B,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAEnG,OAAO,EAOL,cAAc,EACd,6BAA6B,EAC7B,gCAAgC,EAChC,sBAAsB,EACtB,kBAAkB,EAClB,kBAAkB,EAClB,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,aAAa,CAAC"}
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* (behind explicit consent) and cached by the runtime in the browser's Cache
|
|
10
10
|
* Storage — never part of any precache.
|
|
11
11
|
*/
|
|
12
|
-
import type { LlmProvider } from "../provider.js";
|
|
12
|
+
import type { AgentMessage, LlmProvider } from "../provider.js";
|
|
13
13
|
import { type OpenAiCompletion, type OpenAiMessage, type OpenAiTool } from "./openai.js";
|
|
14
14
|
/** A curated model the user may install (ADR 0050 §4). */
|
|
15
15
|
export interface CuratedModel {
|
|
@@ -19,6 +19,12 @@ export interface CuratedModel {
|
|
|
19
19
|
label: string;
|
|
20
20
|
/** Approximate download size, for the consent disclosure. */
|
|
21
21
|
downloadSize: string;
|
|
22
|
+
/**
|
|
23
|
+
* Approximate download size in **bytes** — the numeric counterpart to
|
|
24
|
+
* {@link downloadSize}. Used by the storage preflight to compare against
|
|
25
|
+
* `navigator.storage.estimate()` without parsing the human string.
|
|
26
|
+
*/
|
|
27
|
+
downloadBytes: number;
|
|
22
28
|
/** Approximate GPU memory (VRAM) required. */
|
|
23
29
|
vram: string;
|
|
24
30
|
/** One-line description of the trade-off. */
|
|
@@ -45,6 +51,33 @@ export declare const SUPPORTED_TOOL_CALLING_MODELS: readonly string[];
|
|
|
45
51
|
* before any download.
|
|
46
52
|
*/
|
|
47
53
|
export declare const CURATED_MODELS: readonly CuratedModel[];
|
|
54
|
+
/**
|
|
55
|
+
* Context window (in tokens) the adapter requests when initialising a local
|
|
56
|
+
* model, overriding WebLLM's model-record default (which is 4096 for the curated
|
|
57
|
+
* Hermes records). The analytics assistant's prompt — its system instructions
|
|
58
|
+
* plus the tool JSON schemas and any tool results — runs well past 4096 tokens
|
|
59
|
+
* (~5.9k observed on the demo), so the model errored with
|
|
60
|
+
* "Prompt tokens exceed context window size". Every curated Hermes 7–8B model
|
|
61
|
+
* natively supports 8k+ context, so 8192 comfortably fits the prompt with
|
|
62
|
+
* headroom while staying within the models' real limits. Passed through as
|
|
63
|
+
* `chatOpts.context_window_size` (see {@link WebLlmChatOptions}); the
|
|
64
|
+
* mutually-exclusive `sliding_window_size` keeps its model-record default (-1).
|
|
65
|
+
*/
|
|
66
|
+
export declare const DEFAULT_LOCAL_CONTEXT_WINDOW = 8192;
|
|
67
|
+
/**
|
|
68
|
+
* The subset of `@mlc-ai/web-llm`'s `ChatOptions` (`Partial<ChatConfig>`) this
|
|
69
|
+
* adapter sets to override a model record's defaults. Verified against
|
|
70
|
+
* `@mlc-ai/web-llm` v0.2.84: `CreateMLCEngine(modelId, engineConfig?, chatOpts?)`
|
|
71
|
+
* and `ChatConfig` exposes `context_window_size` / `sliding_window_size`. We only
|
|
72
|
+
* ever set `context_window_size`; the field is declared optional so the object is
|
|
73
|
+
* a valid partial override.
|
|
74
|
+
*/
|
|
75
|
+
export interface WebLlmChatOptions {
|
|
76
|
+
/** Token context window to load the model with. */
|
|
77
|
+
context_window_size?: number;
|
|
78
|
+
/** Sliding-window size; mutually exclusive with `context_window_size` in mlc. */
|
|
79
|
+
sliding_window_size?: number;
|
|
80
|
+
}
|
|
48
81
|
/** Progress report emitted while weights download / the engine initialises. */
|
|
49
82
|
export interface InitProgress {
|
|
50
83
|
/** Fraction complete in [0, 1]. */
|
|
@@ -73,7 +106,7 @@ export interface WebLlmRuntime {
|
|
|
73
106
|
progress: number;
|
|
74
107
|
text: string;
|
|
75
108
|
}) => void;
|
|
76
|
-
}): Promise<WebLlmEngine>;
|
|
109
|
+
}, chatOpts?: WebLlmChatOptions): Promise<WebLlmEngine>;
|
|
77
110
|
}
|
|
78
111
|
/** Options for {@link createWebLlmProvider}. */
|
|
79
112
|
export interface WebLlmProviderOptions {
|
|
@@ -87,6 +120,13 @@ export interface WebLlmProviderOptions {
|
|
|
87
120
|
confirmDownload?: (model: CuratedModel) => boolean | Promise<boolean>;
|
|
88
121
|
/** Called with download/initialise progress. */
|
|
89
122
|
onInitProgress?: (progress: InitProgress) => void;
|
|
123
|
+
/**
|
|
124
|
+
* Token context window to load the local model with, overriding the model
|
|
125
|
+
* record's default. Defaults to {@link DEFAULT_LOCAL_CONTEXT_WINDOW} (8192),
|
|
126
|
+
* which fits the assistant's prompt on every curated Hermes model. Raise it
|
|
127
|
+
* only up to a value the selected model actually supports.
|
|
128
|
+
*/
|
|
129
|
+
contextWindowSize?: number;
|
|
90
130
|
/**
|
|
91
131
|
* Injectable runtime loader (for tests / custom hosting). Defaults to a lazy
|
|
92
132
|
* `import("@mlc-ai/web-llm")`.
|
|
@@ -118,6 +158,52 @@ export declare class WebLlmConsentError extends Error {
|
|
|
118
158
|
export declare class UnsupportedToolCallingModelError extends Error {
|
|
119
159
|
constructor(modelId: string);
|
|
120
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* Thrown when the browser is out of storage for the local model. WebLLM caches
|
|
163
|
+
* each curated model's ~4 GB of weights in the origin's **Cache Storage**;
|
|
164
|
+
* loading or switching among several curated models accumulates multiple copies
|
|
165
|
+
* until the per-origin storage quota is exceeded, at which point the Cache API
|
|
166
|
+
* `put` throws a `QuotaExceededError` DOMException. That is a **browser storage**
|
|
167
|
+
* limit — never an LLM API quota (the local backend has zero network egress), so
|
|
168
|
+
* the raw "Quota exceeded." message is misleading. This typed error carries an
|
|
169
|
+
* actionable remedy instead. See {@link isQuotaExceededError} for classification
|
|
170
|
+
* (by `instanceof`/`.name` only — never a regex over the message, per ADR/CodeQL).
|
|
171
|
+
*/
|
|
172
|
+
export declare class WebLlmStorageError extends Error {
|
|
173
|
+
constructor(message?: string);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Classify a thrown value as a browser storage-quota error **without** parsing
|
|
177
|
+
* its message (no regex — avoids CodeQL ReDoS on attacker-influenced text). A
|
|
178
|
+
* quota failure surfaces as a `DOMException` named `"QuotaExceededError"`; some
|
|
179
|
+
* non-DOM runtimes throw a plain object with the same `name`, so accept that too.
|
|
180
|
+
*/
|
|
181
|
+
export declare function isQuotaExceededError(err: unknown): boolean;
|
|
182
|
+
/**
|
|
183
|
+
* Fold any `system` message into the first `user` turn for WebLLM's Hermes
|
|
184
|
+
* function-calling path.
|
|
185
|
+
*
|
|
186
|
+
* WebLLM injects its OWN tool-calling system prompt for the Hermes-2-Pro /
|
|
187
|
+
* Hermes-3 family and **forbids** a caller-supplied `system` message whenever
|
|
188
|
+
* `tools` are present — it throws `CustomSystemPromptError`
|
|
189
|
+
* ("…cannot specify customized system prompt.") at runtime (verified against
|
|
190
|
+
* `@mlc-ai/web-llm` v0.2.79: the check is gated on both `request.tools` being
|
|
191
|
+
* present and the model id starting with `Hermes-2-Pro-`/`Hermes-3-`). Our
|
|
192
|
+
* assistant always sends `[{ role: "system", … }, userMessage]` plus tools, so
|
|
193
|
+
* the local backend hit that error.
|
|
194
|
+
*
|
|
195
|
+
* This pure helper removes every `system` message and prepends their content —
|
|
196
|
+
* in order, joined by a blank line — to the first `user` message, so the model
|
|
197
|
+
* still receives our analytics instructions, just not as a `system` role. If
|
|
198
|
+
* there is no `user` message yet, the folded system text becomes a `user`
|
|
199
|
+
* message. All other turns (assistant / tool) keep their relative order.
|
|
200
|
+
*
|
|
201
|
+
* Plain string/array operations only (no regex — avoids CodeQL ReDoS), and the
|
|
202
|
+
* input array is never mutated. It is idempotent per call because it always
|
|
203
|
+
* derives from the messages passed in, so re-folding the full transcript on
|
|
204
|
+
* every loop step never reintroduces a `system` role.
|
|
205
|
+
*/
|
|
206
|
+
export declare function foldSystemPromptForHermes(messages: readonly AgentMessage[]): AgentMessage[];
|
|
121
207
|
/**
|
|
122
208
|
* Create a local WebLLM provider. The runtime and model weights load lazily on
|
|
123
209
|
* the first {@link LlmProvider.complete} call — construction is cheap and has no
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webllm.d.ts","sourceRoot":"","sources":["../../src/providers/webllm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAqC,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"webllm.d.ts","sourceRoot":"","sources":["../../src/providers/webllm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAqC,MAAM,gBAAgB,CAAC;AAEnG,OAAO,EAIL,KAAK,gBAAgB,EACrB,KAAK,aAAa,EAClB,KAAK,UAAU,EAChB,MAAM,aAAa,CAAC;AAErB,0DAA0D;AAC1D,MAAM,WAAW,YAAY;IAC3B,0CAA0C;IAC1C,EAAE,EAAE,MAAM,CAAC;IACX,0CAA0C;IAC1C,KAAK,EAAE,MAAM,CAAC;IACd,6DAA6D;IAC7D,YAAY,EAAE,MAAM,CAAC;IACrB;;;;OAIG;IACH,aAAa,EAAE,MAAM,CAAC;IACtB,8CAA8C;IAC9C,IAAI,EAAE,MAAM,CAAC;IACb,6CAA6C;IAC7C,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,6BAA6B,EAAE,SAAS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_1-MLC",
|
|
29
29
|
"Hermes-3-Llama-3.1-8B-q4f16_1-MLC",
|
|
30
30
|
];
|
|
31
|
+
/** Bytes per binary gigabyte (GiB) — the unit browsers report cache usage in. */
|
|
32
|
+
const BYTES_PER_GIB = 1024 ** 3;
|
|
33
|
+
/** Extra free space (beyond the raw download) the storage preflight insists on. */
|
|
34
|
+
const STORAGE_HEADROOM_BYTES = Math.round(0.5 * BYTES_PER_GIB);
|
|
31
35
|
/**
|
|
32
36
|
* A small, curated set of **tool-calling-capable** models (ADR 0050 §4). WebLLM
|
|
33
37
|
* only supports function calling on the 7–8B Hermes family (see
|
|
@@ -42,6 +46,7 @@ export const CURATED_MODELS = [
|
|
|
42
46
|
id: "Hermes-2-Pro-Mistral-7B-q4f16_1-MLC",
|
|
43
47
|
label: "Hermes 2 Pro (Mistral 7B)",
|
|
44
48
|
downloadSize: "~3.9 GB",
|
|
49
|
+
downloadBytes: Math.round(3.9 * BYTES_PER_GIB),
|
|
45
50
|
vram: "~4.0 GB",
|
|
46
51
|
description: "Smallest tool-calling model; the least-friction default.",
|
|
47
52
|
},
|
|
@@ -49,6 +54,7 @@ export const CURATED_MODELS = [
|
|
|
49
54
|
id: "Hermes-2-Pro-Llama-3-8B-q4f16_1-MLC",
|
|
50
55
|
label: "Hermes 2 Pro (Llama 3 8B)",
|
|
51
56
|
downloadSize: "~4.6 GB",
|
|
57
|
+
downloadBytes: Math.round(4.6 * BYTES_PER_GIB),
|
|
52
58
|
vram: "~5.0 GB",
|
|
53
59
|
description: "Stronger Llama-3 base; needs a capable GPU.",
|
|
54
60
|
},
|
|
@@ -56,10 +62,24 @@ export const CURATED_MODELS = [
|
|
|
56
62
|
id: "Hermes-3-Llama-3.1-8B-q4f16_1-MLC",
|
|
57
63
|
label: "Hermes 3 (Llama 3.1 8B)",
|
|
58
64
|
downloadSize: "~4.5 GB",
|
|
65
|
+
downloadBytes: Math.round(4.5 * BYTES_PER_GIB),
|
|
59
66
|
vram: "~4.9 GB",
|
|
60
67
|
description: "Highest quality; needs a capable GPU (~5 GB VRAM).",
|
|
61
68
|
},
|
|
62
69
|
];
|
|
70
|
+
/**
|
|
71
|
+
* Context window (in tokens) the adapter requests when initialising a local
|
|
72
|
+
* model, overriding WebLLM's model-record default (which is 4096 for the curated
|
|
73
|
+
* Hermes records). The analytics assistant's prompt — its system instructions
|
|
74
|
+
* plus the tool JSON schemas and any tool results — runs well past 4096 tokens
|
|
75
|
+
* (~5.9k observed on the demo), so the model errored with
|
|
76
|
+
* "Prompt tokens exceed context window size". Every curated Hermes 7–8B model
|
|
77
|
+
* natively supports 8k+ context, so 8192 comfortably fits the prompt with
|
|
78
|
+
* headroom while staying within the models' real limits. Passed through as
|
|
79
|
+
* `chatOpts.context_window_size` (see {@link WebLlmChatOptions}); the
|
|
80
|
+
* mutually-exclusive `sliding_window_size` keeps its model-record default (-1).
|
|
81
|
+
*/
|
|
82
|
+
export const DEFAULT_LOCAL_CONTEXT_WINDOW = 8192;
|
|
63
83
|
/** Thrown when the local backend is used on a browser without WebGPU. */
|
|
64
84
|
export class WebGpuUnavailableError extends Error {
|
|
65
85
|
constructor() {
|
|
@@ -88,9 +108,160 @@ export class UnsupportedToolCallingModelError extends Error {
|
|
|
88
108
|
this.name = "UnsupportedToolCallingModelError";
|
|
89
109
|
}
|
|
90
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* Thrown when the browser is out of storage for the local model. WebLLM caches
|
|
113
|
+
* each curated model's ~4 GB of weights in the origin's **Cache Storage**;
|
|
114
|
+
* loading or switching among several curated models accumulates multiple copies
|
|
115
|
+
* until the per-origin storage quota is exceeded, at which point the Cache API
|
|
116
|
+
* `put` throws a `QuotaExceededError` DOMException. That is a **browser storage**
|
|
117
|
+
* limit — never an LLM API quota (the local backend has zero network egress), so
|
|
118
|
+
* the raw "Quota exceeded." message is misleading. This typed error carries an
|
|
119
|
+
* actionable remedy instead. See {@link isQuotaExceededError} for classification
|
|
120
|
+
* (by `instanceof`/`.name` only — never a regex over the message, per ADR/CodeQL).
|
|
121
|
+
*/
|
|
122
|
+
export class WebLlmStorageError extends Error {
|
|
123
|
+
constructor(message) {
|
|
124
|
+
super(message ??
|
|
125
|
+
"Your browser is out of storage for the local model (each model needs ~4 GB of cache). " +
|
|
126
|
+
"Free up disk space, clear this site's cached data, or switch to a hosted backend, then retry.");
|
|
127
|
+
this.name = "WebLlmStorageError";
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Classify a thrown value as a browser storage-quota error **without** parsing
|
|
132
|
+
* its message (no regex — avoids CodeQL ReDoS on attacker-influenced text). A
|
|
133
|
+
* quota failure surfaces as a `DOMException` named `"QuotaExceededError"`; some
|
|
134
|
+
* non-DOM runtimes throw a plain object with the same `name`, so accept that too.
|
|
135
|
+
*/
|
|
136
|
+
export function isQuotaExceededError(err) {
|
|
137
|
+
if (typeof DOMException !== "undefined" && err instanceof DOMException) {
|
|
138
|
+
return err.name === "QuotaExceededError";
|
|
139
|
+
}
|
|
140
|
+
return (typeof err === "object" &&
|
|
141
|
+
err !== null &&
|
|
142
|
+
"name" in err &&
|
|
143
|
+
err.name === "QuotaExceededError");
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Map a caught value to a {@link WebLlmStorageError} when — and only when — it is
|
|
147
|
+
* a browser storage-quota failure; otherwise return it unchanged so genuine
|
|
148
|
+
* WebGPU/network/model errors propagate. Never swallows non-quota errors.
|
|
149
|
+
*/
|
|
150
|
+
function mapStorageError(err) {
|
|
151
|
+
return isQuotaExceededError(err) ? new WebLlmStorageError() : err;
|
|
152
|
+
}
|
|
153
|
+
function getStorageEstimator() {
|
|
154
|
+
const storage = typeof navigator !== "undefined"
|
|
155
|
+
? navigator.storage
|
|
156
|
+
: undefined;
|
|
157
|
+
if (storage && typeof storage.estimate === "function") {
|
|
158
|
+
return storage;
|
|
159
|
+
}
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Best-effort, **soft** preflight: fail fast with a {@link WebLlmStorageError}
|
|
164
|
+
* *before* a multi-GB download when free storage is unambiguously too small,
|
|
165
|
+
* which otherwise produces a corrupt half-download (silent "no response").
|
|
166
|
+
*
|
|
167
|
+
* `navigator.storage.estimate()` is imprecise and often reports a large origin
|
|
168
|
+
* budget even when the disk is nearly full, so this only throws when the numbers
|
|
169
|
+
* clearly show insufficient headroom, and is skipped entirely when the API is
|
|
170
|
+
* unavailable — the real quota error is still caught and classified at write time.
|
|
171
|
+
*/
|
|
172
|
+
async function preflightStorage(model) {
|
|
173
|
+
const estimator = getStorageEstimator();
|
|
174
|
+
if (!estimator)
|
|
175
|
+
return;
|
|
176
|
+
let estimate;
|
|
177
|
+
try {
|
|
178
|
+
estimate = await estimator.estimate();
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
// An estimate failure is non-fatal — proceed and let the write-time
|
|
182
|
+
// classifier handle a real quota error.
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
const quota = estimate.quota;
|
|
186
|
+
const usage = estimate.usage ?? 0;
|
|
187
|
+
if (typeof quota !== "number" || quota <= 0)
|
|
188
|
+
return;
|
|
189
|
+
const free = quota - usage;
|
|
190
|
+
// Require the download plus a small headroom margin; only throw when the
|
|
191
|
+
// estimate is confident there is not enough room.
|
|
192
|
+
const needed = model.downloadBytes + STORAGE_HEADROOM_BYTES;
|
|
193
|
+
if (free < needed) {
|
|
194
|
+
const neededGib = (model.downloadBytes / BYTES_PER_GIB).toFixed(1);
|
|
195
|
+
throw new WebLlmStorageError(`Not enough browser storage for ${model.label}: it needs about ${neededGib} GB of free ` +
|
|
196
|
+
"cache space. Free up disk space or clear this site's cached data, then retry — " +
|
|
197
|
+
"or switch to a hosted backend.");
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/** A separator between the folded system instructions and the user's question. */
|
|
201
|
+
const SYSTEM_FOLD_SEPARATOR = "\n\n";
|
|
202
|
+
/**
|
|
203
|
+
* Fold any `system` message into the first `user` turn for WebLLM's Hermes
|
|
204
|
+
* function-calling path.
|
|
205
|
+
*
|
|
206
|
+
* WebLLM injects its OWN tool-calling system prompt for the Hermes-2-Pro /
|
|
207
|
+
* Hermes-3 family and **forbids** a caller-supplied `system` message whenever
|
|
208
|
+
* `tools` are present — it throws `CustomSystemPromptError`
|
|
209
|
+
* ("…cannot specify customized system prompt.") at runtime (verified against
|
|
210
|
+
* `@mlc-ai/web-llm` v0.2.79: the check is gated on both `request.tools` being
|
|
211
|
+
* present and the model id starting with `Hermes-2-Pro-`/`Hermes-3-`). Our
|
|
212
|
+
* assistant always sends `[{ role: "system", … }, userMessage]` plus tools, so
|
|
213
|
+
* the local backend hit that error.
|
|
214
|
+
*
|
|
215
|
+
* This pure helper removes every `system` message and prepends their content —
|
|
216
|
+
* in order, joined by a blank line — to the first `user` message, so the model
|
|
217
|
+
* still receives our analytics instructions, just not as a `system` role. If
|
|
218
|
+
* there is no `user` message yet, the folded system text becomes a `user`
|
|
219
|
+
* message. All other turns (assistant / tool) keep their relative order.
|
|
220
|
+
*
|
|
221
|
+
* Plain string/array operations only (no regex — avoids CodeQL ReDoS), and the
|
|
222
|
+
* input array is never mutated. It is idempotent per call because it always
|
|
223
|
+
* derives from the messages passed in, so re-folding the full transcript on
|
|
224
|
+
* every loop step never reintroduces a `system` role.
|
|
225
|
+
*/
|
|
226
|
+
export function foldSystemPromptForHermes(messages) {
|
|
227
|
+
const systemContents = [];
|
|
228
|
+
const rest = [];
|
|
229
|
+
for (const message of messages) {
|
|
230
|
+
if (message.role === "system") {
|
|
231
|
+
systemContents.push(message.content);
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
rest.push(message);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (systemContents.length === 0)
|
|
238
|
+
return [...messages];
|
|
239
|
+
const systemText = systemContents.join(SYSTEM_FOLD_SEPARATOR);
|
|
240
|
+
const firstUserIndex = rest.findIndex((message) => message.role === "user");
|
|
241
|
+
if (firstUserIndex === -1) {
|
|
242
|
+
// No user turn yet: carry the instructions in as a user message so the
|
|
243
|
+
// model still receives them without a forbidden `system` role.
|
|
244
|
+
return [{ role: "user", content: systemText }, ...rest];
|
|
245
|
+
}
|
|
246
|
+
return rest.map((message, index) => {
|
|
247
|
+
if (index !== firstUserIndex)
|
|
248
|
+
return message;
|
|
249
|
+
return {
|
|
250
|
+
role: "user",
|
|
251
|
+
content: systemText + SYSTEM_FOLD_SEPARATOR + message.content,
|
|
252
|
+
};
|
|
253
|
+
});
|
|
254
|
+
}
|
|
91
255
|
function resolveModel(id) {
|
|
92
256
|
const model = id ? CURATED_MODELS.find((m) => m.id === id) : CURATED_MODELS[0];
|
|
93
|
-
return model ?? {
|
|
257
|
+
return (model ?? {
|
|
258
|
+
id: id ?? "",
|
|
259
|
+
label: id ?? "",
|
|
260
|
+
downloadSize: "?",
|
|
261
|
+
downloadBytes: 0,
|
|
262
|
+
vram: "?",
|
|
263
|
+
description: "",
|
|
264
|
+
});
|
|
94
265
|
}
|
|
95
266
|
const defaultLoadRuntime = () => import("@mlc-ai/web-llm");
|
|
96
267
|
/**
|
|
@@ -108,6 +279,7 @@ export function createWebLlmProvider(options = {}) {
|
|
|
108
279
|
}
|
|
109
280
|
const loadRuntime = options.loadRuntime ?? defaultLoadRuntime;
|
|
110
281
|
const hasWebGpu = options.hasWebGpu ?? (() => isWebGpuAvailable());
|
|
282
|
+
const contextWindowSize = options.contextWindowSize ?? DEFAULT_LOCAL_CONTEXT_WINDOW;
|
|
111
283
|
let enginePromise;
|
|
112
284
|
function ensureEngine() {
|
|
113
285
|
if (!enginePromise) {
|
|
@@ -127,23 +299,53 @@ export function createWebLlmProvider(options = {}) {
|
|
|
127
299
|
if (!ok)
|
|
128
300
|
throw new WebLlmConsentError();
|
|
129
301
|
}
|
|
302
|
+
// Fail fast if free browser storage is unambiguously too small for the
|
|
303
|
+
// multi-GB download, avoiding a corrupt half-download (silent no-response).
|
|
304
|
+
await preflightStorage(model);
|
|
130
305
|
const runtime = await loadRuntime();
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
306
|
+
// Override the model record's default context window (4096 for the curated
|
|
307
|
+
// Hermes records) so the assistant's system prompt + tool schemas + results
|
|
308
|
+
// fit. `sliding_window_size` is left at its model-record default (-1); the
|
|
309
|
+
// two are mutually exclusive in mlc.
|
|
310
|
+
try {
|
|
311
|
+
return await runtime.CreateMLCEngine(model.id, {
|
|
312
|
+
initProgressCallback: options.onInitProgress
|
|
313
|
+
? (report) => options.onInitProgress?.({ progress: report.progress, text: report.text })
|
|
314
|
+
: undefined,
|
|
315
|
+
}, { context_window_size: contextWindowSize });
|
|
316
|
+
}
|
|
317
|
+
catch (err) {
|
|
318
|
+
// Weights are written to Cache Storage during init; a full origin quota
|
|
319
|
+
// surfaces as a QuotaExceededError. Reclassify only that — everything else
|
|
320
|
+
// (WebGPU, network, model errors) propagates unchanged.
|
|
321
|
+
throw mapStorageError(err);
|
|
322
|
+
}
|
|
136
323
|
}
|
|
137
324
|
return {
|
|
138
325
|
async complete(request) {
|
|
139
326
|
const engine = await ensureEngine();
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
327
|
+
const hasTools = request.tools.length > 0;
|
|
328
|
+
// WebLLM's Hermes tool-calling path injects its own system prompt and
|
|
329
|
+
// rejects a caller-supplied `system` message when `tools` are present, so
|
|
330
|
+
// fold our system instructions into the first user turn for that case.
|
|
331
|
+
// Without tools (e.g. the loop's forced synthesis pass) a custom `system`
|
|
332
|
+
// message is allowed, so we keep it and send no function-calling at all.
|
|
333
|
+
const messages = hasTools ? foldSystemPromptForHermes(request.messages) : request.messages;
|
|
334
|
+
try {
|
|
335
|
+
const completion = await engine.chat.completions.create({
|
|
336
|
+
messages: toOpenAiMessages(messages),
|
|
337
|
+
...(hasTools
|
|
338
|
+
? { tools: toOpenAiTools(request.tools), tool_choice: "auto" }
|
|
339
|
+
: {}),
|
|
340
|
+
stream: false,
|
|
341
|
+
});
|
|
342
|
+
return parseOpenAiCompletion(completion);
|
|
343
|
+
}
|
|
344
|
+
catch (err) {
|
|
345
|
+
// Generation can also write to Cache Storage (e.g. lazily fetched shards),
|
|
346
|
+
// so a quota failure here maps to the same actionable storage error.
|
|
347
|
+
throw mapStorageError(err);
|
|
348
|
+
}
|
|
147
349
|
},
|
|
148
350
|
async unload() {
|
|
149
351
|
const promise = enginePromise;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webllm.js","sourceRoot":"","sources":["../../src/providers/webllm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EAChB,aAAa,GAId,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"webllm.js","sourceRoot":"","sources":["../../src/providers/webllm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EAChB,aAAa,GAId,MAAM,aAAa,CAAC;AAsBrB;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAsB;IAC9D,qCAAqC;IACrC,qCAAqC;IACrC,qCAAqC;IACrC,mCAAmC;IACnC,mCAAmC;CACpC,CAAC;AAEF,iFAAiF;AACjF,MAAM,aAAa,GAAG,IAAI,IAAI,CAAC,CAAC;AAEhC,mFAAmF;AACnF,MAAM,sBAAsB,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,GAAG,aAAa,CAAC,CAAC;AAE/D;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,cAAc,GAA4B;IACrD;QACE,EAAE,EAAE,qCAAqC;QACzC,KAAK,EAAE,2BAA2B;QAClC,YAAY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iBACd,CAAC,CAAC;gBACH,OAAO,qBAAqB,CAAC,UAAU,CAAC,CAAC;YAC3C,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,2EAA2E;gBAC3E,qEAAqE;gBACrE,MAAM,eAAe,CAAC,GAAG,CAAC,CAAC;YAC7B,CAAC;QACH,CAAC;QACD,KAAK,CAAC,MAAM;YACV,MAAM,OAAO,GAAG,aAAa,CAAC;YAC9B,aAAa,GAAG,SAAS,CAAC;YAC1B,IAAI,CAAC,OAAO;gBAAE,OAAO;YACrB,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;YACpD,MAAM,MAAM,EAAE,MAAM,EAAE,EAAE,CAAC;QAC3B,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uptimizr/agent-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Framework-agnostic, browser-safe core for Uptimizr analytics agents — the single read-only tool catalog over the collector query API, a headless LLM provider-adapter interface, and the tool-calling loop.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"uptimizr",
|