@weaveintel/local-engines 0.0.0-stage → 0.1.1
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 +79 -2
- package/dist/canonical.d.ts +21 -0
- package/dist/canonical.d.ts.map +1 -0
- package/dist/canonical.js +98 -0
- package/dist/canonical.js.map +1 -0
- package/dist/constants.d.ts +31 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +68 -0
- package/dist/constants.js.map +1 -0
- package/dist/engine.d.ts +84 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +60 -0
- package/dist/engine.js.map +1 -0
- package/dist/engines/chrome-builtin.d.ts +33 -0
- package/dist/engines/chrome-builtin.d.ts.map +1 -0
- package/dist/engines/chrome-builtin.js +240 -0
- package/dist/engines/chrome-builtin.js.map +1 -0
- package/dist/engines/stub.d.ts +32 -0
- package/dist/engines/stub.d.ts.map +1 -0
- package/dist/engines/stub.js +81 -0
- package/dist/engines/stub.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/probe.d.ts +65 -0
- package/dist/probe.d.ts.map +1 -0
- package/dist/probe.js +89 -0
- package/dist/probe.js.map +1 -0
- package/dist/ticket.d.ts +116 -0
- package/dist/ticket.d.ts.map +1 -0
- package/dist/ticket.js +261 -0
- package/dist/ticket.js.map +1 -0
- package/dist/validate.d.ts +30 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.js +134 -0
- package/dist/validate.js.map +1 -0
- package/dist/walker.d.ts +137 -0
- package/dist/walker.d.ts.map +1 -0
- package/dist/walker.js +360 -0
- package/dist/walker.js.map +1 -0
- package/dist/weights.d.ts +16 -0
- package/dist/weights.d.ts.map +1 -0
- package/dist/weights.js +18 -0
- package/dist/weights.js.map +1 -0
- package/package.json +39 -4
package/README.md
CHANGED
|
@@ -1,3 +1,80 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @weaveintel/local-engines
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Run short AI tasks on the person's own device (a browser's built-in model, later small in-browser models) behind a
|
|
4
|
+
chain that a server issued, checked step by step, and always ending at a hosted model.**
|
|
5
|
+
|
|
6
|
+
## Why it exists
|
|
7
|
+
|
|
8
|
+
Some tasks are small enough to run on the device: detect a message's language, summarise one record for the person
|
|
9
|
+
reading it, suggest a category from a fixed list. Doing that locally saves tokens and keeps text on the device. But a
|
|
10
|
+
device is untrusted compute: the server must decide what may run where, and whatever the device answers is only a
|
|
11
|
+
suggestion. This package is the device half of that arrangement:
|
|
12
|
+
|
|
13
|
+
- **The engine contract** (`LocalEngine`): `supports`, `availability` (never downloads), `run`.
|
|
14
|
+
- **Routing tickets**: canonical hashes over the field list and the chain (`hashFieldList`, `hashChain`,
|
|
15
|
+
`sealTicket`), shared by the server that issues a ticket and the device that walks it; `verifyTicket` refuses a
|
|
16
|
+
ticket that was altered, expired, has no hosted end, lists an engine the shell may never use, or fails its signature.
|
|
17
|
+
- **The chain walker** (`walkChain`): tries each local step in order and records one reason per step that did not
|
|
18
|
+
answer (`unavailable`, `download_refused`, `over_budget`, `out_of_memory`, `timeout`, `schema_violation`,
|
|
19
|
+
`low_confidence`, `device_policy_blocked`, `offline`, `not_applicable`). It never runs a hosted step: it hands the
|
|
20
|
+
hosted end back to the caller, who makes the normal hosted call carrying the ticket id.
|
|
21
|
+
- **A capability probe** (`probeCapabilities`, `readDeviceState`): presence of features only, never a model call.
|
|
22
|
+
- **Output checks per task** (`validateTaskOutput`) and **pinned-file checks** (`verifyFileBytes`).
|
|
23
|
+
- **Engines**: `createStubEngine` (deterministic, for tests and demos) and `createChromeBuiltinEngine` (Chrome's
|
|
24
|
+
Language Detector, Summarizer, Translator and Prompt API; feature-detected).
|
|
25
|
+
|
|
26
|
+
Browser-safe: no Node built-ins, no dependencies, only WebCrypto and standard globals.
|
|
27
|
+
|
|
28
|
+
## When to reach for it
|
|
29
|
+
|
|
30
|
+
When a server you control decides, per tenant and task, whether a device may try a local model first, and you want
|
|
31
|
+
the device side to be small, testable and unable to widen what the server allowed. If you only ever call hosted
|
|
32
|
+
models, you do not need it.
|
|
33
|
+
|
|
34
|
+
## How to use it
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import {
|
|
38
|
+
createChromeBuiltinEngine,
|
|
39
|
+
probeCapabilities,
|
|
40
|
+
readDeviceState,
|
|
41
|
+
toHostedFallback,
|
|
42
|
+
toResultSubmission,
|
|
43
|
+
walkChain,
|
|
44
|
+
} from '@weaveintel/local-engines';
|
|
45
|
+
|
|
46
|
+
const capabilities = probeCapabilities(); // send with the routing request (a hint, no content)
|
|
47
|
+
const ticket = await askServerForTicket(capabilities); // your API: the server seals and signs the ticket
|
|
48
|
+
|
|
49
|
+
const result = await walkChain({
|
|
50
|
+
ticket,
|
|
51
|
+
engines: [createChromeBuiltinEngine()],
|
|
52
|
+
input: { text: description },
|
|
53
|
+
device: await readDeviceState(),
|
|
54
|
+
policy: tenantDevicePolicy, // from your server; default allows no downloads
|
|
55
|
+
confirmDownload: askThePerson, // absent: every download is refused
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
if (result.kind === 'local') await postSuggestion(toResultSubmission(result));
|
|
59
|
+
else if (result.kind === 'hosted') await callHosted(toHostedFallback(result));
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
On the server, build the ticket with `sealTicket(body)`, sign `ticketSigningPayload(sealed)` with your key, store the
|
|
63
|
+
two hashes, and check them again (with `hashChain` / `hashFieldList`) when a result comes back. Use `checkChain` when
|
|
64
|
+
an administrator saves a chain.
|
|
65
|
+
|
|
66
|
+
## Rules it keeps
|
|
67
|
+
|
|
68
|
+
- `availability()` never downloads. Chrome's `create()` (which can start a multi-gigabyte model download) is called
|
|
69
|
+
only when the browser says the model is available, or when the walker's download gate passed: policy budget, the
|
|
70
|
+
step's limit, an unmetered connection when required, and the person's yes.
|
|
71
|
+
- A chain must end at a hosted step unless the server sealed it local only.
|
|
72
|
+
- A web page never walks a `local-server` or operating-system engine step.
|
|
73
|
+
- Only the fields the ticket lists reach an engine. Output is checked per task; confidence below the step's floor
|
|
74
|
+
falls through.
|
|
75
|
+
- Provenance (`observedVersion`, `weightsSha256`) is recorded with `attested: false`. Nothing should trust it.
|
|
76
|
+
|
|
77
|
+
## Not in this version
|
|
78
|
+
|
|
79
|
+
In-browser encoders and small LLMs (`browser-wasm`, `browser-webgpu`), Edge's Prompt API, desktop and mobile bridges
|
|
80
|
+
(`os-*`, `local-server`): the contract covers them; their adapters need downloads and are not built.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical JSON and SHA-256 over WebCrypto, so the issuing server and the browser hash the same bytes.
|
|
3
|
+
*
|
|
4
|
+
* Browser-safe: only `globalThis.crypto.subtle` and `TextEncoder`, both present in every current browser and in
|
|
5
|
+
* Node 20 and later.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* JSON with object keys sorted at every depth and no whitespace. Refuses values JSON cannot carry faithfully
|
|
9
|
+
* (non-finite numbers, functions, symbols, bigints, `undefined` inside arrays), so two parties can never disagree
|
|
10
|
+
* about what was hashed. Object properties whose value is `undefined` are left out, as `JSON.stringify` does.
|
|
11
|
+
*/
|
|
12
|
+
export declare function canonicalJson(value: unknown): string;
|
|
13
|
+
/** Lower-case hex SHA-256 of a string (UTF-8) or of bytes. */
|
|
14
|
+
export declare function sha256Hex(input: string | ArrayBuffer | ArrayBufferView): Promise<string>;
|
|
15
|
+
/** Copy into a fresh `Uint8Array` backed by a plain `ArrayBuffer` (WebCrypto refuses shared buffers). */
|
|
16
|
+
export declare function toUint8(input: ArrayBuffer | ArrayBufferView): Uint8Array<ArrayBuffer>;
|
|
17
|
+
/** Base64url without padding (the form signatures travel in). */
|
|
18
|
+
export declare function base64UrlEncode(bytes: Uint8Array): string;
|
|
19
|
+
/** Decode base64url (padding optional). Throws on characters outside the alphabet. */
|
|
20
|
+
export declare function base64UrlDecode(text: string): Uint8Array<ArrayBuffer>;
|
|
21
|
+
//# sourceMappingURL=canonical.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"canonical.d.ts","sourceRoot":"","sources":["../src/canonical.ts"],"names":[],"mappings":"AACA;;;;;GAKG;AAEH;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAEpD;AA+CD,8DAA8D;AAC9D,wBAAsB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,GAAG,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAI9F;AAED,yGAAyG;AACzG,wBAAgB,OAAO,CAAC,KAAK,EAAE,WAAW,GAAG,eAAe,GAAG,UAAU,CAAC,WAAW,CAAC,CAQrF;AAQD,iEAAiE;AACjE,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAIzD;AAED,sFAAsF;AACtF,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAAC,WAAW,CAAC,CAQrE"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
/**
|
|
3
|
+
* Canonical JSON and SHA-256 over WebCrypto, so the issuing server and the browser hash the same bytes.
|
|
4
|
+
*
|
|
5
|
+
* Browser-safe: only `globalThis.crypto.subtle` and `TextEncoder`, both present in every current browser and in
|
|
6
|
+
* Node 20 and later.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* JSON with object keys sorted at every depth and no whitespace. Refuses values JSON cannot carry faithfully
|
|
10
|
+
* (non-finite numbers, functions, symbols, bigints, `undefined` inside arrays), so two parties can never disagree
|
|
11
|
+
* about what was hashed. Object properties whose value is `undefined` are left out, as `JSON.stringify` does.
|
|
12
|
+
*/
|
|
13
|
+
export function canonicalJson(value) {
|
|
14
|
+
return write(value, 'value');
|
|
15
|
+
}
|
|
16
|
+
function write(value, path) {
|
|
17
|
+
if (value === null)
|
|
18
|
+
return 'null';
|
|
19
|
+
switch (typeof value) {
|
|
20
|
+
case 'boolean':
|
|
21
|
+
return value ? 'true' : 'false';
|
|
22
|
+
case 'string':
|
|
23
|
+
return JSON.stringify(value);
|
|
24
|
+
case 'number':
|
|
25
|
+
if (!Number.isFinite(value))
|
|
26
|
+
throw new TypeError(`canonicalJson: ${path} is not a finite number`);
|
|
27
|
+
return JSON.stringify(value);
|
|
28
|
+
case 'object': {
|
|
29
|
+
if (Array.isArray(value)) {
|
|
30
|
+
return `[${value
|
|
31
|
+
.map((item, i) => {
|
|
32
|
+
if (item === undefined)
|
|
33
|
+
throw new TypeError(`canonicalJson: ${path}[${i}] is undefined`);
|
|
34
|
+
return write(item, `${path}[${i}]`);
|
|
35
|
+
})
|
|
36
|
+
.join(',')}]`;
|
|
37
|
+
}
|
|
38
|
+
const proto = Object.getPrototypeOf(value);
|
|
39
|
+
if (proto !== Object.prototype && proto !== null) {
|
|
40
|
+
throw new TypeError(`canonicalJson: ${path} is not a plain object`);
|
|
41
|
+
}
|
|
42
|
+
const record = value;
|
|
43
|
+
const keys = Object.keys(record)
|
|
44
|
+
.filter((k) => record[k] !== undefined)
|
|
45
|
+
.sort();
|
|
46
|
+
return `{${keys.map((k) => `${JSON.stringify(k)}:${write(record[k], `${path}.${k}`)}`).join(',')}}`;
|
|
47
|
+
}
|
|
48
|
+
default:
|
|
49
|
+
throw new TypeError(`canonicalJson: ${path} has unsupported type ${typeof value}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function subtle() {
|
|
53
|
+
const s = globalThis.crypto?.subtle;
|
|
54
|
+
if (!s)
|
|
55
|
+
throw new Error('local-engines: WebCrypto (crypto.subtle) is not available in this environment');
|
|
56
|
+
return s;
|
|
57
|
+
}
|
|
58
|
+
/** Lower-case hex SHA-256 of a string (UTF-8) or of bytes. */
|
|
59
|
+
export async function sha256Hex(input) {
|
|
60
|
+
const bytes = typeof input === 'string' ? new TextEncoder().encode(input) : toUint8(input);
|
|
61
|
+
const digest = await subtle().digest('SHA-256', bytes);
|
|
62
|
+
return hex(new Uint8Array(digest));
|
|
63
|
+
}
|
|
64
|
+
/** Copy into a fresh `Uint8Array` backed by a plain `ArrayBuffer` (WebCrypto refuses shared buffers). */
|
|
65
|
+
export function toUint8(input) {
|
|
66
|
+
const view = input instanceof ArrayBuffer
|
|
67
|
+
? new Uint8Array(input)
|
|
68
|
+
: new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
69
|
+
const out = new Uint8Array(new ArrayBuffer(view.byteLength));
|
|
70
|
+
out.set(view);
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
function hex(bytes) {
|
|
74
|
+
let out = '';
|
|
75
|
+
for (const b of bytes)
|
|
76
|
+
out += b.toString(16).padStart(2, '0');
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
/** Base64url without padding (the form signatures travel in). */
|
|
80
|
+
export function base64UrlEncode(bytes) {
|
|
81
|
+
let bin = '';
|
|
82
|
+
for (const b of bytes)
|
|
83
|
+
bin += String.fromCharCode(b);
|
|
84
|
+
return btoa(bin).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
85
|
+
}
|
|
86
|
+
/** Decode base64url (padding optional). Throws on characters outside the alphabet. */
|
|
87
|
+
export function base64UrlDecode(text) {
|
|
88
|
+
if (!/^[A-Za-z0-9_-]*={0,2}$/.test(text))
|
|
89
|
+
throw new TypeError('base64url: invalid characters');
|
|
90
|
+
const b64 = text.replace(/-/g, '+').replace(/_/g, '/').replace(/=+$/, '');
|
|
91
|
+
const padded = b64 + '='.repeat((4 - (b64.length % 4)) % 4);
|
|
92
|
+
const bin = atob(padded);
|
|
93
|
+
const out = new Uint8Array(new ArrayBuffer(bin.length));
|
|
94
|
+
for (let i = 0; i < bin.length; i++)
|
|
95
|
+
out[i] = bin.charCodeAt(i);
|
|
96
|
+
return out;
|
|
97
|
+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AAY;IAC1C,IAAI,CAAC,wBAAwB,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,MAAM,IAAI,SAAS,CAAC,+BAA+B,CAAC,CAAC;IAC/F,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAC1E,MAAM,MAAM,GAAG,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5D,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;IACzB,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,IAAI,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IACxD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE;QAAE,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;IAChE,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The closed lists every part of the package shares: engine classes, task keys, shells, fallback reasons.
|
|
3
|
+
*
|
|
4
|
+
* A host application keeps its own copy of these lists in its contract; the values here are the same strings, so a
|
|
5
|
+
* host type such as `'tenant-hosted' | 'stub' | ...` is assignable to {@link EngineClass} and back.
|
|
6
|
+
*/
|
|
7
|
+
/** Where one step of a chain runs. The two hosted classes are the end of a chain. */
|
|
8
|
+
export declare const ENGINE_CLASSES: readonly ["stub", "chrome-builtin", "edge-builtin", "browser-wasm", "browser-webgpu", "os-apple", "os-android", "os-windows", "local-server", "tenant-hosted", "platform-hosted"];
|
|
9
|
+
export type EngineClass = (typeof ENGINE_CLASSES)[number];
|
|
10
|
+
/** The hosted end of a chain: the walker hands these back to the caller and never runs them itself. */
|
|
11
|
+
export declare const HOSTED_ENGINE_CLASSES: ReadonlySet<EngineClass>;
|
|
12
|
+
/** True when the class runs on the person's device (everything that is not hosted). */
|
|
13
|
+
export declare function isLocalEngineClass(engineClass: EngineClass): boolean;
|
|
14
|
+
/** Short, reviewable tasks that may run on a device. Anything else stays hosted. */
|
|
15
|
+
export declare const LOCAL_TASKS: readonly ["text.language", "text.summary.short", "field.suggest", "record.classify", "pii.detect", "text.translate", "speech.text"];
|
|
16
|
+
export type LocalTask = (typeof LOCAL_TASKS)[number];
|
|
17
|
+
/** Where the chain runs: a web page, a desktop shell or a mobile app. */
|
|
18
|
+
export declare const SHELLS: readonly ["web", "desktop", "mobile"];
|
|
19
|
+
export type Shell = (typeof SHELLS)[number];
|
|
20
|
+
/**
|
|
21
|
+
* Engines a shell may never be offered. A web page never talks to a model server on the person's machine, and never
|
|
22
|
+
* to an operating-system model (it has no bridge to one). The issuing server enforces this; the client refuses a
|
|
23
|
+
* ticket that breaks it as a second layer.
|
|
24
|
+
*/
|
|
25
|
+
export declare const ENGINES_NEVER_ON_SHELL: Readonly<Record<Shell, ReadonlySet<EngineClass>>>;
|
|
26
|
+
/** Why a local step was skipped or failed. One reason per attempt; the walker never invents others. */
|
|
27
|
+
export declare const FALLBACK_REASONS: readonly ["unavailable", "download_refused", "over_budget", "out_of_memory", "timeout", "schema_violation", "low_confidence", "device_policy_blocked", "offline", "not_applicable"];
|
|
28
|
+
export type FallbackReason = (typeof FALLBACK_REASONS)[number];
|
|
29
|
+
/** The longest short summary a local engine may return (words). */
|
|
30
|
+
export declare const SHORT_SUMMARY_MAX_WORDS = 120;
|
|
31
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AACA;;;;;GAKG;AAEH,qFAAqF;AACrF,eAAO,MAAM,cAAc,mLAYjB,CAAC;AACX,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC;AAE1D,uGAAuG;AACvG,eAAO,MAAM,qBAAqB,EAAE,WAAW,CAAC,WAAW,CAGzD,CAAC;AAEH,uFAAuF;AACvF,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAEpE;AAED,oFAAoF;AACpF,eAAO,MAAM,WAAW,qIAQd,CAAC;AACX,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,CAAC,CAAC;AAErD,yEAAyE;AACzE,eAAO,MAAM,MAAM,uCAAwC,CAAC;AAC5D,MAAM,MAAM,KAAK,GAAG,CAAC,OAAO,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;AAE5C;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,EAAE,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,WAAW,CAAC,WAAW,CAAC,CAAC,CAIpF,CAAC;AAEF,uGAAuG;AACvG,eAAO,MAAM,gBAAgB,qLAWnB,CAAC;AACX,MAAM,MAAM,cAAc,GAAG,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,CAAC,CAAC;AAE/D,mEAAmE;AACnE,eAAO,MAAM,uBAAuB,MAAM,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
/**
|
|
3
|
+
* The closed lists every part of the package shares: engine classes, task keys, shells, fallback reasons.
|
|
4
|
+
*
|
|
5
|
+
* A host application keeps its own copy of these lists in its contract; the values here are the same strings, so a
|
|
6
|
+
* host type such as `'tenant-hosted' | 'stub' | ...` is assignable to {@link EngineClass} and back.
|
|
7
|
+
*/
|
|
8
|
+
/** Where one step of a chain runs. The two hosted classes are the end of a chain. */
|
|
9
|
+
export const ENGINE_CLASSES = [
|
|
10
|
+
'stub',
|
|
11
|
+
'chrome-builtin',
|
|
12
|
+
'edge-builtin',
|
|
13
|
+
'browser-wasm',
|
|
14
|
+
'browser-webgpu',
|
|
15
|
+
'os-apple',
|
|
16
|
+
'os-android',
|
|
17
|
+
'os-windows',
|
|
18
|
+
'local-server',
|
|
19
|
+
'tenant-hosted',
|
|
20
|
+
'platform-hosted',
|
|
21
|
+
];
|
|
22
|
+
/** The hosted end of a chain: the walker hands these back to the caller and never runs them itself. */
|
|
23
|
+
export const HOSTED_ENGINE_CLASSES = new Set([
|
|
24
|
+
'tenant-hosted',
|
|
25
|
+
'platform-hosted',
|
|
26
|
+
]);
|
|
27
|
+
/** True when the class runs on the person's device (everything that is not hosted). */
|
|
28
|
+
export function isLocalEngineClass(engineClass) {
|
|
29
|
+
return !HOSTED_ENGINE_CLASSES.has(engineClass);
|
|
30
|
+
}
|
|
31
|
+
/** Short, reviewable tasks that may run on a device. Anything else stays hosted. */
|
|
32
|
+
export const LOCAL_TASKS = [
|
|
33
|
+
'text.language',
|
|
34
|
+
'text.summary.short',
|
|
35
|
+
'field.suggest',
|
|
36
|
+
'record.classify',
|
|
37
|
+
'pii.detect',
|
|
38
|
+
'text.translate',
|
|
39
|
+
'speech.text',
|
|
40
|
+
];
|
|
41
|
+
/** Where the chain runs: a web page, a desktop shell or a mobile app. */
|
|
42
|
+
export const SHELLS = ['web', 'desktop', 'mobile'];
|
|
43
|
+
/**
|
|
44
|
+
* Engines a shell may never be offered. A web page never talks to a model server on the person's machine, and never
|
|
45
|
+
* to an operating-system model (it has no bridge to one). The issuing server enforces this; the client refuses a
|
|
46
|
+
* ticket that breaks it as a second layer.
|
|
47
|
+
*/
|
|
48
|
+
export const ENGINES_NEVER_ON_SHELL = {
|
|
49
|
+
web: new Set(['local-server', 'os-apple', 'os-android', 'os-windows']),
|
|
50
|
+
desktop: new Set(['os-apple', 'os-android']),
|
|
51
|
+
mobile: new Set(['local-server', 'os-windows']),
|
|
52
|
+
};
|
|
53
|
+
/** Why a local step was skipped or failed. One reason per attempt; the walker never invents others. */
|
|
54
|
+
export const FALLBACK_REASONS = [
|
|
55
|
+
'unavailable',
|
|
56
|
+
'download_refused',
|
|
57
|
+
'over_budget',
|
|
58
|
+
'out_of_memory',
|
|
59
|
+
'timeout',
|
|
60
|
+
'schema_violation',
|
|
61
|
+
'low_confidence',
|
|
62
|
+
'device_policy_blocked',
|
|
63
|
+
'offline',
|
|
64
|
+
'not_applicable',
|
|
65
|
+
];
|
|
66
|
+
/** The longest short summary a local engine may return (words). */
|
|
67
|
+
export const SHORT_SUMMARY_MAX_WORDS = 120;
|
|
68
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B;;;;;GAKG;AAEH,qFAAqF;AACrF,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,MAAM;IACN,gBAAgB;IAChB,cAAc;IACd,cAAc;IACd,gBAAgB;IAChB,UAAU;IACV,YAAY;IACZ,YAAY;IACZ,cAAc;IACd,eAAe;IACf,iBAAiB;CACT,CAAC;AAGX,uGAAuG;AACvG,MAAM,CAAC,MAAM,qBAAqB,GAA6B,IAAI,GAAG,CAAc;IAClF,eAAe;IACf,iBAAiB;CAClB,CAAC,CAAC;AAEH,uFAAuF;AACvF,MAAM,UAAU,kBAAkB,CAAC,WAAwB;IACzD,OAAO,CAAC,qBAAqB,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;AACjD,CAAC;AAED,oFAAoF;AACpF,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,eAAe;IACf,oBAAoB;IACpB,eAAe;IACf,iBAAiB;IACjB,YAAY;IACZ,gBAAgB;IAChB,aAAa;CACL,CAAC;AAGX,yEAAyE;AACzE,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,CAAU,CAAC;AAG5D;;;;GAIG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAsD;IACvF,GAAG,EAAE,IAAI,GAAG,CAAc,CAAC,cAAc,EAAE,UAAU,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC;IACnF,OAAO,EAAE,IAAI,GAAG,CAAc,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;IACzD,MAAM,EAAE,IAAI,GAAG,CAAc,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;CAC7D,CAAC;AAEF,uGAAuG;AACvG,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,aAAa;IACb,kBAAkB;IAClB,aAAa;IACb,eAAe;IACf,SAAS;IACT,kBAAkB;IAClB,gBAAgB;IAChB,uBAAuB;IACvB,SAAS;IACT,gBAAgB;CACR,CAAC;AAGX,mEAAmE;AACnE,MAAM,CAAC,MAAM,uBAAuB,GAAG,GAAG,CAAC"}
|
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The engine contract: what every local engine (a stub, Chrome's built-in AI, later an in-browser encoder or a
|
|
3
|
+
* desktop bridge) offers the chain walker.
|
|
4
|
+
*
|
|
5
|
+
* An engine is untrusted compute. Its output is checked like text a person typed, becomes a suggestion, and decides
|
|
6
|
+
* nothing. What it says about itself (version, weights hash) is recorded, never trusted.
|
|
7
|
+
*/
|
|
8
|
+
import type { EngineClass, FallbackReason } from './constants.js';
|
|
9
|
+
import type { TicketFile } from './ticket.js';
|
|
10
|
+
/** The four states Chrome's built-in AI reports, used by every engine. */
|
|
11
|
+
export declare const AVAILABILITY_STATES: readonly ["available", "downloadable", "downloading", "unavailable"];
|
|
12
|
+
export type AvailabilityState = (typeof AVAILABILITY_STATES)[number];
|
|
13
|
+
export interface EngineAvailability {
|
|
14
|
+
readonly state: AvailabilityState;
|
|
15
|
+
/** Size of the download still needed, when the engine knows it. */
|
|
16
|
+
readonly downloadMb?: number;
|
|
17
|
+
/** A short machine-readable note, recorded on the attempt (for example `api_missing`). */
|
|
18
|
+
readonly detail?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* What a task reads. The walker passes only the fields the ticket lists; `text` is the content the task works on
|
|
22
|
+
* (already assembled by the host from those fields).
|
|
23
|
+
*/
|
|
24
|
+
export interface EngineInput {
|
|
25
|
+
readonly text?: string;
|
|
26
|
+
readonly fields?: Readonly<Record<string, string>>;
|
|
27
|
+
/** `field.suggest`: the field a value is wanted for. */
|
|
28
|
+
readonly field?: string;
|
|
29
|
+
/** `field.suggest` and `record.classify`: the only values an answer may take. */
|
|
30
|
+
readonly choices?: readonly string[];
|
|
31
|
+
/** `text.translate`. */
|
|
32
|
+
readonly sourceLanguage?: string;
|
|
33
|
+
readonly targetLanguage?: string;
|
|
34
|
+
}
|
|
35
|
+
export interface EngineCheckRequest {
|
|
36
|
+
readonly task: string;
|
|
37
|
+
readonly modelKey: string;
|
|
38
|
+
readonly input: EngineInput;
|
|
39
|
+
readonly signal: AbortSignal;
|
|
40
|
+
}
|
|
41
|
+
export interface EngineRunRequest extends EngineCheckRequest {
|
|
42
|
+
/** Pinned files for this step; an engine that loads files checks them with `verifyFileBytes` before use. */
|
|
43
|
+
readonly files: readonly TicketFile[];
|
|
44
|
+
readonly maxOutputChars: number;
|
|
45
|
+
/**
|
|
46
|
+
* True only when the walker's download gate passed (policy, budget and the person's yes). An engine must not start
|
|
47
|
+
* a download when this is false.
|
|
48
|
+
*/
|
|
49
|
+
readonly allowDownload: boolean;
|
|
50
|
+
}
|
|
51
|
+
export interface EngineRunResult {
|
|
52
|
+
readonly output: unknown;
|
|
53
|
+
/** 0 to 1, when the engine has one. */
|
|
54
|
+
readonly confidence?: number;
|
|
55
|
+
/** What the engine says it ran (recorded, never trusted). */
|
|
56
|
+
readonly observedVersion?: string;
|
|
57
|
+
readonly weightsSha256?: string;
|
|
58
|
+
}
|
|
59
|
+
export interface LocalEngine {
|
|
60
|
+
readonly engineClass: EngineClass;
|
|
61
|
+
/** A stable id for logs (`stub`, `chrome-builtin`, ...). */
|
|
62
|
+
readonly id: string;
|
|
63
|
+
/** Whether the engine can do this task with this model key at all (no device check). */
|
|
64
|
+
supports(task: string, modelKey: string): boolean;
|
|
65
|
+
/** The device state for this task. Must never start a download. */
|
|
66
|
+
availability(req: EngineCheckRequest): Promise<EngineAvailability>;
|
|
67
|
+
run(req: EngineRunRequest): Promise<EngineRunResult>;
|
|
68
|
+
}
|
|
69
|
+
/** An engine failure that already knows its fallback reason. */
|
|
70
|
+
export declare class EngineError extends Error {
|
|
71
|
+
readonly reason: FallbackReason;
|
|
72
|
+
readonly detail: string | undefined;
|
|
73
|
+
constructor(reason: FallbackReason, message: string, detail?: string);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Map anything an engine throws to a fallback reason. Built-in browser APIs throw `DOMException`s: `AbortError` or
|
|
77
|
+
* `TimeoutError` (timeout), `QuotaExceededError` (out of memory or disk), `NotSupportedError` (not applicable),
|
|
78
|
+
* `NotAllowedError` (blocked by browser or device policy). Anything else is `unavailable`.
|
|
79
|
+
*/
|
|
80
|
+
export declare function classifyEngineError(err: unknown): {
|
|
81
|
+
reason: FallbackReason;
|
|
82
|
+
detail?: string;
|
|
83
|
+
};
|
|
84
|
+
//# sourceMappingURL=engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AACA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,0EAA0E;AAC1E,eAAO,MAAM,mBAAmB,sEAKtB,CAAC;AACX,MAAM,MAAM,iBAAiB,GAAG,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAC;AAErE,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,KAAK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C,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;CACjC;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;IAClC,4DAA4D;IAC5D,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,wFAAwF;IACxF,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC;IAClD,mEAAmE;IACnE,YAAY,CAAC,GAAG,EAAE,kBAAkB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC;IACnE,GAAG,CAAC,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;CACtD;AAED,gEAAgE;AAChE,qBAAa,WAAY,SAAQ,KAAK;IACpC,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC;IAChC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;gBACxB,MAAM,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM;CAMrE;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,OAAO,GAAG;IAAE,MAAM,EAAE,cAAc,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CA0B7F"}
|
package/dist/engine.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
/**
|
|
3
|
+
* The engine contract: what every local engine (a stub, Chrome's built-in AI, later an in-browser encoder or a
|
|
4
|
+
* desktop bridge) offers the chain walker.
|
|
5
|
+
*
|
|
6
|
+
* An engine is untrusted compute. Its output is checked like text a person typed, becomes a suggestion, and decides
|
|
7
|
+
* nothing. What it says about itself (version, weights hash) is recorded, never trusted.
|
|
8
|
+
*/
|
|
9
|
+
/** The four states Chrome's built-in AI reports, used by every engine. */
|
|
10
|
+
export const AVAILABILITY_STATES = [
|
|
11
|
+
'available',
|
|
12
|
+
'downloadable',
|
|
13
|
+
'downloading',
|
|
14
|
+
'unavailable',
|
|
15
|
+
];
|
|
16
|
+
/** An engine failure that already knows its fallback reason. */
|
|
17
|
+
export class EngineError extends Error {
|
|
18
|
+
reason;
|
|
19
|
+
detail;
|
|
20
|
+
constructor(reason, message, detail) {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = 'EngineError';
|
|
23
|
+
this.reason = reason;
|
|
24
|
+
this.detail = detail;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Map anything an engine throws to a fallback reason. Built-in browser APIs throw `DOMException`s: `AbortError` or
|
|
29
|
+
* `TimeoutError` (timeout), `QuotaExceededError` (out of memory or disk), `NotSupportedError` (not applicable),
|
|
30
|
+
* `NotAllowedError` (blocked by browser or device policy). Anything else is `unavailable`.
|
|
31
|
+
*/
|
|
32
|
+
export function classifyEngineError(err) {
|
|
33
|
+
if (err instanceof EngineError) {
|
|
34
|
+
return err.detail === undefined
|
|
35
|
+
? { reason: err.reason }
|
|
36
|
+
: { reason: err.reason, detail: err.detail };
|
|
37
|
+
}
|
|
38
|
+
if (typeof err !== 'object' || err === null)
|
|
39
|
+
return { reason: 'unavailable', detail: 'error' };
|
|
40
|
+
const name = String(err.name ?? '');
|
|
41
|
+
const message = String(err.message ?? '');
|
|
42
|
+
const detail = name || 'error';
|
|
43
|
+
switch (name) {
|
|
44
|
+
case 'AbortError':
|
|
45
|
+
case 'TimeoutError':
|
|
46
|
+
return { reason: 'timeout', detail };
|
|
47
|
+
case 'QuotaExceededError':
|
|
48
|
+
return { reason: 'out_of_memory', detail };
|
|
49
|
+
case 'NotSupportedError':
|
|
50
|
+
return { reason: 'not_applicable', detail };
|
|
51
|
+
case 'NotAllowedError':
|
|
52
|
+
return { reason: 'device_policy_blocked', detail };
|
|
53
|
+
default:
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
if (/out of memory|allocation failed|\boom\b/i.test(message))
|
|
57
|
+
return { reason: 'out_of_memory', detail };
|
|
58
|
+
return { reason: 'unavailable', detail };
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B;;;;;;GAMG;AAKH,0EAA0E;AAC1E,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,WAAW;IACX,cAAc;IACd,aAAa;IACb,aAAa;CACL,CAAC;AAiEX,gEAAgE;AAChE,MAAM,OAAO,WAAY,SAAQ,KAAK;IAC3B,MAAM,CAAiB;IACvB,MAAM,CAAqB;IACpC,YAAY,MAAsB,EAAE,OAAe,EAAE,MAAe;QAClE,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,GAAY;IAC9C,IAAI,GAAG,YAAY,WAAW,EAAE,CAAC;QAC/B,OAAO,GAAG,CAAC,MAAM,KAAK,SAAS;YAC7B,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE;YACxB,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC;IACjD,CAAC;IACD,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI;QAAE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;IAC/F,MAAM,IAAI,GAAG,MAAM,CAAE,GAA0B,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC;IAC5D,MAAM,OAAO,GAAG,MAAM,CAAE,GAA6B,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;IACrE,MAAM,MAAM,GAAG,IAAI,IAAI,OAAO,CAAC;IAC/B,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,YAAY,CAAC;QAClB,KAAK,cAAc;YACjB,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC;QACvC,KAAK,oBAAoB;YACvB,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,CAAC;QAC7C,KAAK,mBAAmB;YACtB,OAAO,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,EAAE,CAAC;QAC9C,KAAK,iBAAiB;YACpB,OAAO,EAAE,MAAM,EAAE,uBAAuB,EAAE,MAAM,EAAE,CAAC;QACrD;YACE,MAAM;IACV,CAAC;IACD,IAAI,0CAA0C,CAAC,IAAI,CAAC,OAAO,CAAC;QAC1D,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,CAAC;IAC7C,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,CAAC;AAC3C,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chrome's built-in AI as a local engine: the Language Detector, Summarizer and Translator APIs (stable since
|
|
3
|
+
* Chrome 138) and the Prompt API (`LanguageModel`).
|
|
4
|
+
*
|
|
5
|
+
* Feature-detected. Nothing here runs at import time, and `availability()` only asks the browser (it never downloads).
|
|
6
|
+
* `create()` is the call that can start the browser's model download, so the adapter calls it only when the browser
|
|
7
|
+
* says the model is `available`, or when the walker's download gate passed (`allowDownload`). In tests the globals are
|
|
8
|
+
* stubs; the real model is never triggered by this package's tests.
|
|
9
|
+
*
|
|
10
|
+
* Task → API (the step's model key picks the API; empty picks the default):
|
|
11
|
+
* - `text.language` → `language-detector`
|
|
12
|
+
* - `text.summary.short` → `summarizer` (default) or `prompt`
|
|
13
|
+
* - `text.translate` → `translator`
|
|
14
|
+
* - `field.suggest` → `prompt` (JSON output constrained to the field's choices)
|
|
15
|
+
*/
|
|
16
|
+
import type { EngineClass } from '../constants.js';
|
|
17
|
+
import { type LocalEngine } from '../engine.js';
|
|
18
|
+
export declare const CHROME_BUILTIN_IDS: readonly ["language-detector", "summarizer", "translator", "prompt"];
|
|
19
|
+
export type ChromeBuiltinId = (typeof CHROME_BUILTIN_IDS)[number];
|
|
20
|
+
/** The APIs each task may use, default first. */
|
|
21
|
+
export declare const CHROME_BUILTIN_TASKS: Readonly<Record<string, readonly ChromeBuiltinId[]>>;
|
|
22
|
+
export interface ChromeBuiltinOptions {
|
|
23
|
+
/** Where to look for the APIs. Default `globalThis`. Tests pass stubs. */
|
|
24
|
+
readonly globals?: object;
|
|
25
|
+
/** Default `chrome-builtin`. */
|
|
26
|
+
readonly engineClass?: EngineClass;
|
|
27
|
+
/** Language the Prompt API and Summarizer answer in. Default `en`. */
|
|
28
|
+
readonly outputLanguage?: string;
|
|
29
|
+
/** The download size to report when the browser says `downloadable` (the browser does not say). */
|
|
30
|
+
readonly downloadMb?: number;
|
|
31
|
+
}
|
|
32
|
+
export declare function createChromeBuiltinEngine(opts?: ChromeBuiltinOptions): LocalEngine;
|
|
33
|
+
//# sourceMappingURL=chrome-builtin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chrome-builtin.d.ts","sourceRoot":"","sources":["../../src/engines/chrome-builtin.ts"],"names":[],"mappings":"AACA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAOL,KAAK,WAAW,EACjB,MAAM,cAAc,CAAC;AAEtB,eAAO,MAAM,kBAAkB,sEAKrB,CAAC;AACX,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,kBAAkB,CAAC,CAAC,MAAM,CAAC,CAAC;AAElE,iDAAiD;AACjD,eAAO,MAAM,oBAAoB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,eAAe,EAAE,CAAC,CAMlF,CAAC;AAEL,MAAM,WAAW,oBAAoB;IACnC,0EAA0E;IAC1E,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,gCAAgC;IAChC,QAAQ,CAAC,WAAW,CAAC,EAAE,WAAW,CAAC;IACnC,sEAAsE;IACtE,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IACjC,mGAAmG;IACnG,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC9B;AAyDD,wBAAgB,yBAAyB,CAAC,IAAI,GAAE,oBAAyB,GAAG,WAAW,CA6HtF"}
|