browser-model-manager 0.0.0-stage → 0.1.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/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/PROVENANCE.json +16 -0
- package/README.md +607 -2
- package/THIRD_PARTY_NOTICES +4 -0
- package/dist/acquisition/grants.d.ts +31 -0
- package/dist/acquisition/grants.js +75 -0
- package/dist/acquisition/index.d.ts +4 -0
- package/dist/acquisition/index.js +6 -0
- package/dist/acquisition/output.d.ts +12 -0
- package/dist/acquisition/output.js +19 -0
- package/dist/acquisition/phases.d.ts +3 -0
- package/dist/acquisition/phases.js +64 -0
- package/dist/acquisition/progress.d.ts +29 -0
- package/dist/acquisition/progress.js +124 -0
- package/dist/acquisition/slots.d.ts +35 -0
- package/dist/acquisition/slots.js +296 -0
- package/dist/cache/index.d.ts +3 -0
- package/dist/cache/index.js +4 -0
- package/dist/cache/model-cache.d.ts +152 -0
- package/dist/cache/model-cache.js +1585 -0
- package/dist/cache/sha256.d.ts +11 -0
- package/dist/cache/sha256.js +97 -0
- package/dist/cache/shard-store.d.ts +89 -0
- package/dist/cache/shard-store.js +204 -0
- package/dist/contracts/budgets.d.ts +41 -0
- package/dist/contracts/budgets.js +48 -0
- package/dist/contracts/errors.d.ts +43 -0
- package/dist/contracts/errors.js +2 -0
- package/dist/contracts/grant.d.ts +47 -0
- package/dist/contracts/grant.js +44 -0
- package/dist/contracts/index.d.ts +6 -0
- package/dist/contracts/index.js +7 -0
- package/dist/contracts/leases.d.ts +20 -0
- package/dist/contracts/leases.js +14 -0
- package/dist/contracts/lifecycle.d.ts +88 -0
- package/dist/contracts/lifecycle.js +12 -0
- package/dist/contracts/manifest.d.ts +95 -0
- package/dist/contracts/manifest.js +174 -0
- package/dist/handles/cross-tab.d.ts +16 -0
- package/dist/handles/cross-tab.js +29 -0
- package/dist/handles/handle.d.ts +24 -0
- package/dist/handles/handle.js +93 -0
- package/dist/handles/index.d.ts +3 -0
- package/dist/handles/index.js +4 -0
- package/dist/handles/leases.d.ts +45 -0
- package/dist/handles/leases.js +103 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +7 -0
- package/package.json +43 -4
- package/schema/model-manifest.v1.schema.json +215 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
// The aggregate acquisition budget (fm-fr9s): every worker and tab of one origin shares a fixed number of chunk
|
|
2
|
+
// slots, so read-ahead, hashing and write buffering stay inside one bound however many workers acquire at once.
|
|
3
|
+
// The candidate read ahead two (or four) chunks per worker, which bounds nothing once two workers overlap.
|
|
4
|
+
//
|
|
5
|
+
// A slot is one Web Lock (`<name>:slot-<i>`); holding it admits one chunk (at most ACQUISITION_CHUNK_MAX_BYTES)
|
|
6
|
+
// from the first byte fetched until it is committed to storage. Priority: inside one manager instance waiters are
|
|
7
|
+
// served synthesis first, then detector, then the rest of the required work. Waiters announce their tier through
|
|
8
|
+
// a unique Web Lock, so another worker handed a slot can yield it to a higher-priority waiter before transferring.
|
|
9
|
+
// Existing transfers keep their slots. Optional preparation never queues on a slot or passes required waiters.
|
|
10
|
+
import { ACQUISITION_BUDGETS, ACQUISITION_CHUNK_MAX_BYTES, } from '../contracts/budgets.js';
|
|
11
|
+
/** Highest first. */
|
|
12
|
+
export const PRIORITY_ORDER = Object.freeze([
|
|
13
|
+
'synthesis-prerequisite',
|
|
14
|
+
'detector-prerequisite',
|
|
15
|
+
'foreground-required',
|
|
16
|
+
'optional',
|
|
17
|
+
'speculative-warming',
|
|
18
|
+
]);
|
|
19
|
+
const rank = (p) => PRIORITY_ORDER.indexOf(p);
|
|
20
|
+
/** Optional tiers never wait in a lock queue: they take a free slot or wait their turn locally. */
|
|
21
|
+
export const queuesOnLocks = (p) => rank(p) < rank('optional');
|
|
22
|
+
/** Slots per memory class: the aggregate read-ahead budget in whole chunks. */
|
|
23
|
+
export function slotCount(memory) {
|
|
24
|
+
return ACQUISITION_BUDGETS[memory].readAheadBytes / ACQUISITION_CHUNK_MAX_BYTES;
|
|
25
|
+
}
|
|
26
|
+
/** How long optional work waits between looks for a free slot. */
|
|
27
|
+
export const OPTIONAL_RETRY_MS = 100;
|
|
28
|
+
const aborted = () => new DOMException('Asset acquisition cancelled', 'AbortError');
|
|
29
|
+
/** Holds one lock until the returned release is called; null when `ifAvailable` found it taken. */
|
|
30
|
+
function hold(locks, name, options) {
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
locks
|
|
33
|
+
.request(name, options, (lock) => {
|
|
34
|
+
if (!lock)
|
|
35
|
+
return resolve(null);
|
|
36
|
+
return new Promise((release) => resolve(() => release()));
|
|
37
|
+
})
|
|
38
|
+
.catch(reject);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
export function createAcquisitionSlots({ locks, count, name = 'facemorph-acquisition', }) {
|
|
42
|
+
if (!Number.isInteger(count) || count < 1)
|
|
43
|
+
throw new TypeError('Acquisition slots: count must be a positive integer');
|
|
44
|
+
const slot = (i) => `${name}:slot-${i}`;
|
|
45
|
+
const priorityPrefix = `${name}:waiting-`;
|
|
46
|
+
const instance = Math.random().toString(36).slice(2);
|
|
47
|
+
let sequence = 0;
|
|
48
|
+
const waiting = [];
|
|
49
|
+
let next = 0;
|
|
50
|
+
let busy = false;
|
|
51
|
+
/** The waiter whose attempt is in flight; its own signal ends that attempt. */
|
|
52
|
+
let current = null;
|
|
53
|
+
/** Queues on every slot at once; keeps the first granted and lets any other grant go at once. */
|
|
54
|
+
function queueOnAll(w) {
|
|
55
|
+
const race = new AbortController();
|
|
56
|
+
const relay = () => race.abort(w.signal.reason ?? aborted());
|
|
57
|
+
w.signal.addEventListener('abort', relay, { once: true });
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
let won = false;
|
|
60
|
+
let failed = 0;
|
|
61
|
+
for (let i = 0; i < count; i++)
|
|
62
|
+
hold(locks, slot(i), { signal: race.signal }).then((release) => {
|
|
63
|
+
if (!release)
|
|
64
|
+
return;
|
|
65
|
+
if (won)
|
|
66
|
+
return release();
|
|
67
|
+
won = true;
|
|
68
|
+
w.signal.removeEventListener('abort', relay);
|
|
69
|
+
race.abort(aborted()); // the other queued requests leave
|
|
70
|
+
resolve(release);
|
|
71
|
+
}, (error) => {
|
|
72
|
+
if (++failed === count && !won) {
|
|
73
|
+
w.signal.removeEventListener('abort', relay);
|
|
74
|
+
reject(w.signal.reason ?? error);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/** Whether a slot request is queued anywhere on this origin (unknown when the platform cannot say). */
|
|
80
|
+
async function othersWaiting() {
|
|
81
|
+
if (!locks.query)
|
|
82
|
+
return false;
|
|
83
|
+
const { pending = [] } = await locks.query();
|
|
84
|
+
return pending.some((p) => p.name?.startsWith(`${name}:slot-`) === true);
|
|
85
|
+
}
|
|
86
|
+
/** Required tiers must also keep their order across independent workers' native FIFO lock queues. */
|
|
87
|
+
async function higherWaiting(w) {
|
|
88
|
+
if (waiting.some((other) => !other.signal.aborted && rank(other.priority) < rank(w.priority)))
|
|
89
|
+
return true;
|
|
90
|
+
if (!locks.query)
|
|
91
|
+
return false;
|
|
92
|
+
const { held = [] } = await locks.query();
|
|
93
|
+
return held.some((lock) => {
|
|
94
|
+
if (!lock.name?.startsWith(priorityPrefix))
|
|
95
|
+
return false;
|
|
96
|
+
const tier = Number(lock.name.slice(priorityPrefix.length).split(':')[0]);
|
|
97
|
+
return tier < rank(w.priority);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
async function pause(w) {
|
|
101
|
+
await new Promise((resolve, reject) => {
|
|
102
|
+
const timer = setTimeout(() => {
|
|
103
|
+
w.signal.removeEventListener('abort', stop);
|
|
104
|
+
resolve();
|
|
105
|
+
}, OPTIONAL_RETRY_MS);
|
|
106
|
+
const stop = () => {
|
|
107
|
+
clearTimeout(timer);
|
|
108
|
+
reject(w.signal.reason ?? aborted());
|
|
109
|
+
};
|
|
110
|
+
w.signal.addEventListener('abort', stop, { once: true });
|
|
111
|
+
if (w.signal.aborted)
|
|
112
|
+
stop();
|
|
113
|
+
});
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
/** A platform query can reject too: never leak a newly granted slot during arbitration. */
|
|
117
|
+
async function admitTurn(w, release) {
|
|
118
|
+
try {
|
|
119
|
+
if (w.signal.aborted)
|
|
120
|
+
throw w.signal.reason ?? aborted();
|
|
121
|
+
if (await higherWaiting(w)) {
|
|
122
|
+
release();
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
return release;
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
release();
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* One attempt for the head waiter: any free slot, else (required work) the queues of all of them. Optional work
|
|
134
|
+
* that finds none free, or finds required work queued anywhere, pauses and returns null, so a higher-priority
|
|
135
|
+
* waiter that arrived meanwhile goes first.
|
|
136
|
+
*/
|
|
137
|
+
async function grantHead(w) {
|
|
138
|
+
if (locks.query)
|
|
139
|
+
await w.announced;
|
|
140
|
+
if (w.signal.aborted)
|
|
141
|
+
throw w.signal.reason ?? aborted();
|
|
142
|
+
if (locks.query && (await higherWaiting(w)))
|
|
143
|
+
return pause(w);
|
|
144
|
+
const required = queuesOnLocks(w.priority);
|
|
145
|
+
if (required || !(await othersWaiting()))
|
|
146
|
+
for (let k = 0; k < count; k++) {
|
|
147
|
+
if (w.signal.aborted)
|
|
148
|
+
throw w.signal.reason ?? aborted();
|
|
149
|
+
const release = await hold(locks, slot((next + k) % count), { ifAvailable: true });
|
|
150
|
+
if (w.signal.aborted) {
|
|
151
|
+
release?.();
|
|
152
|
+
throw w.signal.reason ?? aborted();
|
|
153
|
+
}
|
|
154
|
+
if (release) {
|
|
155
|
+
next = (next + k + 1) % count;
|
|
156
|
+
return admitTurn(w, release);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// A cancellation during the lookups above has already fired, so the queue's relay would never see it.
|
|
160
|
+
if (w.signal.aborted)
|
|
161
|
+
throw w.signal.reason ?? aborted();
|
|
162
|
+
if (required) {
|
|
163
|
+
return admitTurn(w, await queueOnAll(w));
|
|
164
|
+
}
|
|
165
|
+
return pause(w);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Platform queries and ifAvailable callbacks need not answer before cancellation. Let the caller and the local
|
|
169
|
+
* queue move on immediately; an outstanding attempt still owns its answer and returns any grant that arrives late.
|
|
170
|
+
*/
|
|
171
|
+
function attemptHead(w) {
|
|
172
|
+
return new Promise((resolve, reject) => {
|
|
173
|
+
let settled = false;
|
|
174
|
+
const cancel = () => {
|
|
175
|
+
if (settled)
|
|
176
|
+
return;
|
|
177
|
+
settled = true;
|
|
178
|
+
w.signal.removeEventListener('abort', cancel);
|
|
179
|
+
reject(w.signal.reason ?? aborted());
|
|
180
|
+
};
|
|
181
|
+
w.signal.addEventListener('abort', cancel, { once: true });
|
|
182
|
+
if (w.signal.aborted)
|
|
183
|
+
return cancel();
|
|
184
|
+
void grantHead(w).then((release) => {
|
|
185
|
+
if (settled)
|
|
186
|
+
return release?.();
|
|
187
|
+
settled = true;
|
|
188
|
+
w.signal.removeEventListener('abort', cancel);
|
|
189
|
+
resolve(release);
|
|
190
|
+
}, (error) => {
|
|
191
|
+
if (settled)
|
|
192
|
+
return;
|
|
193
|
+
settled = true;
|
|
194
|
+
w.signal.removeEventListener('abort', cancel);
|
|
195
|
+
reject(error);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
async function pump() {
|
|
200
|
+
if (busy)
|
|
201
|
+
return;
|
|
202
|
+
busy = true;
|
|
203
|
+
try {
|
|
204
|
+
while (waiting.length) {
|
|
205
|
+
waiting.sort((a, b) => rank(a.priority) - rank(b.priority));
|
|
206
|
+
const w = waiting[0];
|
|
207
|
+
current = w;
|
|
208
|
+
try {
|
|
209
|
+
const release = await attemptHead(w);
|
|
210
|
+
current = null;
|
|
211
|
+
if (!release)
|
|
212
|
+
continue;
|
|
213
|
+
waiting.splice(waiting.indexOf(w), 1);
|
|
214
|
+
// Cancelled while its attempt was in flight (the abort listener leaves the current waiter to the attempt):
|
|
215
|
+
// the slot goes back and the caller is told. Releasing without rejecting left the take pending forever,
|
|
216
|
+
// and with it the whole acquisition (hosted macOS WebKit, fm-fy2.13: a sibling chunk's reset aborted a lane
|
|
217
|
+
// whose `ifAvailable` lookup then found a free slot).
|
|
218
|
+
if (w.signal.aborted) {
|
|
219
|
+
release();
|
|
220
|
+
w.reject(w.signal.reason ?? aborted());
|
|
221
|
+
}
|
|
222
|
+
else
|
|
223
|
+
w.resolve(release);
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
current = null;
|
|
227
|
+
const at = waiting.indexOf(w);
|
|
228
|
+
if (at >= 0)
|
|
229
|
+
waiting.splice(at, 1);
|
|
230
|
+
w.reject(error);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
finally {
|
|
235
|
+
busy = false;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return Object.freeze({
|
|
239
|
+
count,
|
|
240
|
+
take(priority, signal) {
|
|
241
|
+
if (signal.aborted)
|
|
242
|
+
return Promise.reject(signal.reason ?? aborted());
|
|
243
|
+
return new Promise((resolve, reject) => {
|
|
244
|
+
let once = false;
|
|
245
|
+
let finished = false;
|
|
246
|
+
let unannounce = null;
|
|
247
|
+
const announced = locks.query
|
|
248
|
+
? hold(locks, `${priorityPrefix}${rank(priority)}:${instance}:${++sequence}`, {
|
|
249
|
+
signal,
|
|
250
|
+
}).then((release) => {
|
|
251
|
+
if (finished)
|
|
252
|
+
release?.();
|
|
253
|
+
else
|
|
254
|
+
unannounce = release;
|
|
255
|
+
})
|
|
256
|
+
: Promise.resolve();
|
|
257
|
+
void announced.catch(() => { }); // a waiter cancelled before its local turn still owns its rejection
|
|
258
|
+
const release = (r) => () => {
|
|
259
|
+
if (once)
|
|
260
|
+
return;
|
|
261
|
+
once = true;
|
|
262
|
+
r();
|
|
263
|
+
};
|
|
264
|
+
const cleanup = () => {
|
|
265
|
+
finished = true;
|
|
266
|
+
unannounce?.();
|
|
267
|
+
signal.removeEventListener('abort', cancel);
|
|
268
|
+
};
|
|
269
|
+
const w = {
|
|
270
|
+
priority,
|
|
271
|
+
signal,
|
|
272
|
+
announced,
|
|
273
|
+
resolve: (r) => {
|
|
274
|
+
cleanup();
|
|
275
|
+
resolve(release(r));
|
|
276
|
+
},
|
|
277
|
+
reject: (reason) => {
|
|
278
|
+
cleanup();
|
|
279
|
+
reject(reason);
|
|
280
|
+
},
|
|
281
|
+
};
|
|
282
|
+
// A waiter cancelled before its turn leaves at once; an attempt in flight is ended by its signal.
|
|
283
|
+
const cancel = () => {
|
|
284
|
+
const at = waiting.indexOf(w);
|
|
285
|
+
if (at >= 0 && current !== w) {
|
|
286
|
+
waiting.splice(at, 1);
|
|
287
|
+
w.reject(signal.reason ?? aborted());
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
signal.addEventListener('abort', cancel, { once: true });
|
|
291
|
+
waiting.push(w);
|
|
292
|
+
void pump();
|
|
293
|
+
});
|
|
294
|
+
},
|
|
295
|
+
});
|
|
296
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { type AcquisitionSlots } from '../acquisition/slots.ts';
|
|
2
|
+
import type { ManagerError, Result } from '../contracts/errors.ts';
|
|
3
|
+
import type { AcquisitionGrant, GrantOwnerId } from '../contracts/grant.ts';
|
|
4
|
+
import type { AssetDescriptor } from '../contracts/manifest.ts';
|
|
5
|
+
import { type ShardStore, type StorageLike } from './shard-store.ts';
|
|
6
|
+
export declare const MODEL_CACHE_NAME = "checkface-model-blobs-v1";
|
|
7
|
+
/** An asset as a manifest pins it: lowercase SHA-256, exact size, HTTPS URL, optional chunks. */
|
|
8
|
+
export interface AssetInput {
|
|
9
|
+
readonly sha256: string;
|
|
10
|
+
readonly size: number;
|
|
11
|
+
readonly url: string;
|
|
12
|
+
readonly chunks?: readonly AssetInput[] | undefined;
|
|
13
|
+
}
|
|
14
|
+
export interface Asset {
|
|
15
|
+
readonly sha256: string;
|
|
16
|
+
readonly size: number;
|
|
17
|
+
readonly url: string;
|
|
18
|
+
readonly chunks?: readonly Asset[];
|
|
19
|
+
}
|
|
20
|
+
/** What observers see: hashes and status only (no URLs or authentication material). */
|
|
21
|
+
export interface CacheEvent {
|
|
22
|
+
readonly status: string;
|
|
23
|
+
readonly sha256: string;
|
|
24
|
+
readonly [field: string]: unknown;
|
|
25
|
+
}
|
|
26
|
+
/** Store contract: get(hash) -> fresh Response|undefined; put(hash, Response) MUST consume the stream and commit
|
|
27
|
+
* atomically only after successful EOF; remove(hash) removes only that corrupt identity. */
|
|
28
|
+
export interface BlobStore {
|
|
29
|
+
get(hash: string): Promise<Response | undefined>;
|
|
30
|
+
put(hash: string, response: Response): Promise<void>;
|
|
31
|
+
remove(hash: string): Promise<unknown>;
|
|
32
|
+
}
|
|
33
|
+
export interface LockManagerLike {
|
|
34
|
+
request<T>(name: string, run: () => Promise<T>): Promise<T>;
|
|
35
|
+
request<T>(name: string, options: {
|
|
36
|
+
signal: AbortSignal;
|
|
37
|
+
}, run: () => Promise<T>): Promise<T>;
|
|
38
|
+
}
|
|
39
|
+
export type Fetcher = (url: string, init: RequestInit) => Promise<Response>;
|
|
40
|
+
export interface ModelCacheOptions {
|
|
41
|
+
readonly store: BlobStore;
|
|
42
|
+
readonly shards?: ShardStore | null | undefined;
|
|
43
|
+
readonly fetcher?: Fetcher | undefined;
|
|
44
|
+
readonly locks?: LockManagerLike | undefined;
|
|
45
|
+
/** A pending network fetch/read that receives nothing for this long fails (TimeoutError). */
|
|
46
|
+
readonly timeoutMs?: number | undefined;
|
|
47
|
+
/** A chunk attempt that receives nothing for this long is abandoned and retried (CHUNK_STALL_MS). */
|
|
48
|
+
readonly stallMs?: number | undefined;
|
|
49
|
+
readonly maxConcurrent?: number | undefined;
|
|
50
|
+
readonly readAhead?: number | undefined;
|
|
51
|
+
readonly retryDelaysMs?: readonly number[] | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* HTTPS origins that serve the same files under the same paths. Every other retry of a chunk goes to the next
|
|
54
|
+
* one, in order. Empty by default: then no request leaves the asset's own origin.
|
|
55
|
+
*/
|
|
56
|
+
readonly alternateOrigins?: readonly string[] | undefined;
|
|
57
|
+
/** The aggregate chunk budget shared by every worker; null admits every chunk at once (one-worker tests only). */
|
|
58
|
+
readonly slots?: AcquisitionSlots | null | undefined;
|
|
59
|
+
readonly report?: ((event: CacheEvent) => void) | undefined;
|
|
60
|
+
}
|
|
61
|
+
/** Who an acquisition is for: an owner the grant names. Bytes are transferred only under the grant. */
|
|
62
|
+
export interface AcquireOptions {
|
|
63
|
+
readonly grant: AcquisitionGrant;
|
|
64
|
+
readonly owner: GrantOwnerId;
|
|
65
|
+
readonly signal?: AbortSignal | undefined;
|
|
66
|
+
}
|
|
67
|
+
export interface AssetHandle {
|
|
68
|
+
readonly sha256: string;
|
|
69
|
+
readonly size: number;
|
|
70
|
+
bytes(): Promise<Uint8Array<ArrayBuffer>>;
|
|
71
|
+
open(): Promise<Response>;
|
|
72
|
+
}
|
|
73
|
+
/** Verified local bytes only: never a fetch, never a repair (contracts/lifecycle.ts VerifiedLocalReader). */
|
|
74
|
+
export interface LocalReader {
|
|
75
|
+
readonly allowRepair: false;
|
|
76
|
+
read(asset: AssetDescriptor): Promise<Result<Uint8Array<ArrayBuffer>, ManagerError>>;
|
|
77
|
+
}
|
|
78
|
+
/** A real storage-quota refusal, separate from execution/provider capability.
|
|
79
|
+
* The DOM name and cause remain available to existing browser-storage callers;
|
|
80
|
+
* `error` is the structured-clone-safe manager result for app/worker boundaries.
|
|
81
|
+
*/
|
|
82
|
+
export declare class CacheQuotaExceeded extends Error {
|
|
83
|
+
readonly error: Extract<ManagerError, {
|
|
84
|
+
code: 'quota';
|
|
85
|
+
}>;
|
|
86
|
+
constructor(requiredBytes: number, cause: unknown);
|
|
87
|
+
}
|
|
88
|
+
export interface ModelCache {
|
|
89
|
+
/** Verified local reads with typed misses (fm-eulc). */
|
|
90
|
+
readonly local: LocalReader;
|
|
91
|
+
/** Rejects with GrantRefused before unauthorized requests, or CacheQuotaExceeded when storage is full. */
|
|
92
|
+
acquire(input: AssetInput, options: AcquireOptions): Promise<AssetHandle>;
|
|
93
|
+
readonly records: RecordAccess;
|
|
94
|
+
has(input: AssetInput | string): Promise<boolean>;
|
|
95
|
+
/**
|
|
96
|
+
* Bytes of this asset already stored on the device, counting each stored chunk of a partly downloaded asset
|
|
97
|
+
* (fm-9kr8): what an interrupted download kept, so an inventory can say what is still to transfer.
|
|
98
|
+
*/
|
|
99
|
+
heldBytes(input: AssetInput): Promise<number>;
|
|
100
|
+
peek(input: AssetInput | string): Promise<Response | null>;
|
|
101
|
+
}
|
|
102
|
+
export declare function validateAsset(asset: AssetInput): Asset;
|
|
103
|
+
export declare const CHUNK_RETRY_DELAYS_MS: readonly number[];
|
|
104
|
+
export declare const CHUNK_STALL_MS = 30000;
|
|
105
|
+
/** Explicit download for immediate execution when durable model storage cannot be used.
|
|
106
|
+
* This does not populate a cache or manufacture a local reader/asset handle.
|
|
107
|
+
*/
|
|
108
|
+
export type UncachedAcquireOptions = Pick<ModelCacheOptions, 'fetcher' | 'timeoutMs' | 'stallMs' | 'retryDelaysMs' | 'alternateOrigins' | 'slots' | 'report'>;
|
|
109
|
+
/** Returns one verified asset without reading or writing any persistent storage.
|
|
110
|
+
* The caller owns its lifetime. Explicit JS model buffers are one final buffer plus one
|
|
111
|
+
* sequential verified chunk, never an asset map or the encoder's complete shard set. Platform
|
|
112
|
+
* SHA-256 may additionally use native workspace for its input (a whole unchunked asset).
|
|
113
|
+
* All bytes, including
|
|
114
|
+
* failed attempts, spend the supplied grant; a cache failure does not replenish that grant.
|
|
115
|
+
*/
|
|
116
|
+
export declare function acquireUncachedBytes(input: AssetInput, options: AcquireOptions, { fetcher, timeoutMs, stallMs, retryDelaysMs, alternateOrigins, slots, report, }?: UncachedAcquireOptions): Promise<Uint8Array<ArrayBuffer>>;
|
|
117
|
+
/**
|
|
118
|
+
* Counts bytes on their way past so an observer can drive a real progress bar. The 158 MB
|
|
119
|
+
* asset used to report {loaded:0} and then {loaded:size}: the bar sat at zero for minutes and
|
|
120
|
+
* then jumped. Ticks are coalesced so a large download costs a bounded number of events.
|
|
121
|
+
*/
|
|
122
|
+
export declare const PROGRESS_INTERVAL_BYTES: number;
|
|
123
|
+
export interface RecordAccess {
|
|
124
|
+
get(key: string): Promise<Record<string, unknown> | undefined>;
|
|
125
|
+
put(key: string, record: unknown): Promise<void>;
|
|
126
|
+
remove(key: string): Promise<void>;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Small JSON records sharing the asset store's namespace but keyed by their own identity rather
|
|
130
|
+
* than asset bytes: verified-asset markers (C-05) and canary qualification records (C-03). A
|
|
131
|
+
* record that fails to parse or read is dropped, never fatal — the caller re-proves instead.
|
|
132
|
+
*/
|
|
133
|
+
export declare function createRecordAccess(store: BlobStore): RecordAccess;
|
|
134
|
+
/** Store contract: get(hash) -> fresh Response|undefined; put(hash, Response)
|
|
135
|
+
* MUST consume the stream and commit atomically only after successful EOF;
|
|
136
|
+
* remove(hash) removes only that corrupt identity. Never expose partial writes.
|
|
137
|
+
* Observers receive hashes/status only (no URLs or authentication material).
|
|
138
|
+
*/
|
|
139
|
+
export declare function createModelCache({ store, shards, fetcher, locks, timeoutMs, stallMs, maxConcurrent, readAhead, retryDelaysMs, alternateOrigins, slots, report, }: ModelCacheOptions): ModelCache;
|
|
140
|
+
export declare function createBrowserModelCache(options?: Partial<ModelCacheOptions> & {
|
|
141
|
+
readonly readOnly?: boolean;
|
|
142
|
+
}): Promise<ModelCache>;
|
|
143
|
+
export interface StorageStatus {
|
|
144
|
+
readonly persistence: 'granted' | 'not-granted' | 'unavailable';
|
|
145
|
+
readonly usage?: number | undefined;
|
|
146
|
+
readonly quota?: number | undefined;
|
|
147
|
+
readonly eviction: 'unknown';
|
|
148
|
+
}
|
|
149
|
+
export declare function storageStatus({ requestPersistence, storage, }?: {
|
|
150
|
+
requestPersistence?: boolean;
|
|
151
|
+
storage?: StorageLike | undefined;
|
|
152
|
+
}): Promise<StorageStatus>;
|