@hackbuild/ai-views 0.1.13 → 0.1.14

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.
@@ -6014,18 +6014,19 @@ var init_sha256 = __esmMin((() => {
6014
6014
  }
6015
6015
  };
6016
6016
  }));
6017
- // packages/weights/src/store.ts
6018
- function pickBackends(opts) {
6019
- const spec = opts.backends ?? "auto";
6020
- const onFile = (opts.protocol ?? globalThis.location?.protocol) === "file:";
6021
- const g = globalThis;
6022
- return (spec === "auto" ? [
6023
- !onFile && typeof g.navigator?.storage?.getDirectory === "function" ? "opfs" : null,
6024
- g.indexedDB ? "idb" : null,
6025
- !onFile && g.caches ? "cache" : null,
6026
- "memory"
6027
- ].filter(Boolean) : spec).map((n) => typeof n !== "string" ? n : n === "opfs" ? opfsBackend() : n === "idb" ? idbBackend() : n === "cache" ? cacheBackend() : memoryBackend());
6028
- }
6017
+ // packages/weights/src/types.ts
6018
+ var WeightError;
6019
+ var init_types$2 = __esmMin((() => {
6020
+ WeightError = class extends Error {
6021
+ code;
6022
+ constructor(code, message) {
6023
+ super(message);
6024
+ this.name = "WeightError";
6025
+ this.code = code;
6026
+ }
6027
+ };
6028
+ }));
6029
+ // packages/weights/src/download.ts
6029
6030
  function parseContentRange(h) {
6030
6031
  const m = h && /bytes\s+(\d+)-\d+\/(\d+|\*)/i.exec(h);
6031
6032
  if (!m) return null;
@@ -6056,6 +6057,175 @@ async function assemble(b, meta) {
6056
6057
  }
6057
6058
  return new Blob(parts);
6058
6059
  }
6060
+ async function download(d, ref, key, o) {
6061
+ const { backend: be, partBytes, fetch: doFetch, now, estimate } = d;
6062
+ let meta = await be.getMeta(key);
6063
+ if (meta?.complete) {
6064
+ const blob = await assemble(be, meta);
6065
+ if (blob && (!ref.sha256 || meta.sha256 === ref.sha256.toLowerCase())) return blob;
6066
+ await be.remove(key);
6067
+ meta = null;
6068
+ }
6069
+ const resume = o.resume !== false;
6070
+ if (meta && (!resume || meta.url !== ref.url)) {
6071
+ await be.remove(key);
6072
+ meta = null;
6073
+ }
6074
+ const hash = new Sha256();
6075
+ let offset = 0;
6076
+ if (meta && meta.parts.length) for (let n = 0; n < meta.parts.length; n++) {
6077
+ const p = await be.getPart(key, n);
6078
+ if (!p || p.size !== meta.parts[n]) {
6079
+ meta.parts = meta.parts.slice(0, n);
6080
+ break;
6081
+ }
6082
+ hash.update(new Uint8Array(await p.arrayBuffer()));
6083
+ offset += p.size;
6084
+ o.onProgress?.({
6085
+ key,
6086
+ loaded: offset,
6087
+ total: meta.total,
6088
+ fraction: meta.total ? offset / meta.total : null,
6089
+ resuming: true
6090
+ });
6091
+ }
6092
+ if (!meta) meta = {
6093
+ key,
6094
+ url: ref.url,
6095
+ licence: ref.licence,
6096
+ total: null,
6097
+ parts: [],
6098
+ complete: false,
6099
+ etag: null,
6100
+ lastModified: null,
6101
+ sha256: null,
6102
+ updated: now()
6103
+ };
6104
+ const headers = {};
6105
+ if (offset > 0) {
6106
+ headers["range"] = `bytes=${offset}-`;
6107
+ const validator = meta.etag ?? meta.lastModified;
6108
+ if (validator) headers["if-range"] = validator;
6109
+ }
6110
+ const init = { headers };
6111
+ if (o.signal) init.signal = o.signal;
6112
+ let res;
6113
+ try {
6114
+ res = await doFetch(ref.url, init);
6115
+ } catch (e) {
6116
+ if (o.signal?.aborted) throw new WeightError("abort", `download of ${key} aborted`);
6117
+ throw new WeightError("http", `fetch ${ref.url} failed: ${e instanceof Error ? e.message : String(e)}`);
6118
+ }
6119
+ if (offset > 0 && res.status === 416 && meta.total !== null && offset >= meta.total) {} else if (offset > 0 && res.status === 416 && meta.total === null) meta.total = offset;
6120
+ else if (offset > 0 && res.status === 206 && !sameFile(meta, res, !!ref.sha256)) {
6121
+ await res.body?.cancel().catch(() => void 0);
6122
+ await be.remove(key);
6123
+ return download(d, ref, key, {
6124
+ ...o,
6125
+ resume: false
6126
+ });
6127
+ } else if (offset > 0 && res.status === 206) {
6128
+ const cr = parseContentRange(res.headers.get("content-range"));
6129
+ if (!cr || cr.start !== offset) throw new WeightError("range", `server resumed ${key} at ${cr?.start ?? "unknown"}, expected ${offset}`);
6130
+ if (cr.total !== null) meta.total = cr.total;
6131
+ } else if (res.ok) {
6132
+ if (offset > 0) {
6133
+ await res.body?.cancel().catch(() => void 0);
6134
+ await be.remove(key);
6135
+ return download(d, ref, key, {
6136
+ ...o,
6137
+ resume: false
6138
+ });
6139
+ }
6140
+ const len = res.headers.get("content-length");
6141
+ meta.total = len !== null && /^\d+$/.test(len) && !res.headers.get("content-encoding") ? Number(len) : null;
6142
+ } else throw new WeightError("http", `http ${res.status} for ${ref.url}`);
6143
+ meta.etag = res.headers.get("etag") ?? meta.etag;
6144
+ meta.lastModified = res.headers.get("last-modified") ?? meta.lastModified;
6145
+ meta.updated = now();
6146
+ await be.putMeta(meta);
6147
+ if (meta.total !== null && offset === 0) {
6148
+ const est = await estimate();
6149
+ if (!est.fits(meta.total)) throw new WeightError("quota", `not enough storage for ${key}: needs ${meta.total} bytes plus a 20 percent margin, ${(est.quota ?? 0) - (est.used ?? 0)} free`);
6150
+ }
6151
+ let cur = [];
6152
+ let curBytes = 0;
6153
+ let loaded = offset;
6154
+ const flush = async () => {
6155
+ if (!curBytes) return;
6156
+ const n = meta.parts.length;
6157
+ await be.putPart(key, n, new Blob(cur));
6158
+ meta.parts.push(curBytes);
6159
+ meta.updated = now();
6160
+ await be.putMeta(meta);
6161
+ cur = [];
6162
+ curBytes = 0;
6163
+ };
6164
+ if (res.status !== 416 && res.body) {
6165
+ const reader = res.body.getReader();
6166
+ try {
6167
+ for (;;) {
6168
+ const { done, value } = await reader.read();
6169
+ if (done) break;
6170
+ if (o.signal?.aborted) throw new Error("aborted");
6171
+ hash.update(value);
6172
+ loaded += value.byteLength;
6173
+ let rest = value;
6174
+ while (rest.byteLength) {
6175
+ const take = Math.min(rest.byteLength, partBytes - curBytes);
6176
+ cur.push(rest.subarray(0, take));
6177
+ curBytes += take;
6178
+ rest = rest.subarray(take);
6179
+ if (curBytes >= partBytes) await flush();
6180
+ }
6181
+ o.onProgress?.({
6182
+ key,
6183
+ loaded,
6184
+ total: meta.total,
6185
+ fraction: meta.total ? Math.min(1, loaded / meta.total) : null,
6186
+ resuming: false
6187
+ });
6188
+ }
6189
+ } catch (e) {
6190
+ await flush().catch(() => void 0);
6191
+ if (o.signal?.aborted) throw new WeightError("abort", `download of ${key} aborted at ${loaded} bytes; parts kept for resume`);
6192
+ throw new WeightError("http", `download of ${key} broke at ${loaded} bytes, parts kept for resume: ${e instanceof Error ? e.message : String(e)}`);
6193
+ }
6194
+ }
6195
+ await flush();
6196
+ if (meta.total !== null && loaded !== meta.total) throw new WeightError("http", `download of ${key} ended at ${loaded} of ${meta.total} bytes; parts kept for resume`);
6197
+ const digest = hash.digestHex();
6198
+ if (ref.sha256 && digest !== ref.sha256.toLowerCase()) {
6199
+ await be.remove(key);
6200
+ throw new WeightError("sha256", `sha256 of ${key} is ${digest}, expected ${ref.sha256.toLowerCase()}; the parts were removed`);
6201
+ }
6202
+ meta.total = loaded;
6203
+ meta.sha256 = digest;
6204
+ meta.complete = true;
6205
+ meta.updated = now();
6206
+ await be.putMeta(meta);
6207
+ const blob = await assemble(be, meta);
6208
+ if (!blob) throw new WeightError("http", `parts of ${key} went missing after the download`);
6209
+ return blob;
6210
+ }
6211
+ var bareTag;
6212
+ var init_download = __esmMin((() => {
6213
+ init_sha256();
6214
+ init_types$2();
6215
+ bareTag = (t) => t ? t.replace(/^W\//, "").replace(/^"|"$/g, "") : null;
6216
+ }));
6217
+ // packages/weights/src/store.ts
6218
+ function pickBackends(opts) {
6219
+ const spec = opts.backends ?? "auto";
6220
+ const onFile = (opts.protocol ?? globalThis.location?.protocol) === "file:";
6221
+ const g = globalThis;
6222
+ return (spec === "auto" ? [
6223
+ !onFile && typeof g.navigator?.storage?.getDirectory === "function" ? "opfs" : null,
6224
+ g.indexedDB ? "idb" : null,
6225
+ !onFile && g.caches ? "cache" : null,
6226
+ "memory"
6227
+ ].filter(Boolean) : spec).map((n) => typeof n !== "string" ? n : n === "opfs" ? opfsBackend() : n === "idb" ? idbBackend() : n === "cache" ? cacheBackend() : memoryBackend());
6228
+ }
6059
6229
  /**
6060
6230
  * Open a store. The first backend whose meta read works is used; a backend
6061
6231
  * that throws on open (OPFS on file://, a blocked IndexedDB) is skipped.
@@ -6119,156 +6289,13 @@ async function openWeightStore(opts = {}) {
6119
6289
  fits: (bytes) => quota === null || quota - (used ?? 0) >= 1.2 * bytes
6120
6290
  };
6121
6291
  };
6122
- async function download(ref, key, o) {
6123
- let meta = await be.getMeta(key);
6124
- if (meta?.complete) {
6125
- const blob = await assemble(be, meta);
6126
- if (blob && (!ref.sha256 || meta.sha256 === ref.sha256.toLowerCase())) return blob;
6127
- await be.remove(key);
6128
- meta = null;
6129
- }
6130
- const resume = o.resume !== false;
6131
- if (meta && (!resume || meta.url !== ref.url)) {
6132
- await be.remove(key);
6133
- meta = null;
6134
- }
6135
- const hash = new Sha256();
6136
- let offset = 0;
6137
- if (meta && meta.parts.length) for (let n = 0; n < meta.parts.length; n++) {
6138
- const p = await be.getPart(key, n);
6139
- if (!p || p.size !== meta.parts[n]) {
6140
- meta.parts = meta.parts.slice(0, n);
6141
- break;
6142
- }
6143
- hash.update(new Uint8Array(await p.arrayBuffer()));
6144
- offset += p.size;
6145
- o.onProgress?.({
6146
- key,
6147
- loaded: offset,
6148
- total: meta.total,
6149
- fraction: meta.total ? offset / meta.total : null,
6150
- resuming: true
6151
- });
6152
- }
6153
- if (!meta) meta = {
6154
- key,
6155
- url: ref.url,
6156
- licence: ref.licence,
6157
- total: null,
6158
- parts: [],
6159
- complete: false,
6160
- etag: null,
6161
- lastModified: null,
6162
- sha256: null,
6163
- updated: now()
6164
- };
6165
- const headers = {};
6166
- if (offset > 0) {
6167
- headers["range"] = `bytes=${offset}-`;
6168
- const validator = meta.etag ?? meta.lastModified;
6169
- if (validator) headers["if-range"] = validator;
6170
- }
6171
- const init = { headers };
6172
- if (o.signal) init.signal = o.signal;
6173
- let res;
6174
- try {
6175
- res = await doFetch(ref.url, init);
6176
- } catch (e) {
6177
- if (o.signal?.aborted) throw new WeightError("abort", `download of ${key} aborted`);
6178
- throw new WeightError("http", `fetch ${ref.url} failed: ${e instanceof Error ? e.message : String(e)}`);
6179
- }
6180
- if (offset > 0 && res.status === 416 && meta.total !== null && offset >= meta.total) {} else if (offset > 0 && res.status === 416 && meta.total === null) meta.total = offset;
6181
- else if (offset > 0 && res.status === 206 && !sameFile(meta, res, !!ref.sha256)) {
6182
- await res.body?.cancel().catch(() => void 0);
6183
- await be.remove(key);
6184
- return download(ref, key, {
6185
- ...o,
6186
- resume: false
6187
- });
6188
- } else if (offset > 0 && res.status === 206) {
6189
- const cr = parseContentRange(res.headers.get("content-range"));
6190
- if (!cr || cr.start !== offset) throw new WeightError("range", `server resumed ${key} at ${cr?.start ?? "unknown"}, expected ${offset}`);
6191
- if (cr.total !== null) meta.total = cr.total;
6192
- } else if (res.ok) {
6193
- if (offset > 0) {
6194
- await res.body?.cancel().catch(() => void 0);
6195
- await be.remove(key);
6196
- return download(ref, key, {
6197
- ...o,
6198
- resume: false
6199
- });
6200
- }
6201
- const len = res.headers.get("content-length");
6202
- meta.total = len !== null && /^\d+$/.test(len) && !res.headers.get("content-encoding") ? Number(len) : null;
6203
- } else throw new WeightError("http", `http ${res.status} for ${ref.url}`);
6204
- meta.etag = res.headers.get("etag") ?? meta.etag;
6205
- meta.lastModified = res.headers.get("last-modified") ?? meta.lastModified;
6206
- meta.updated = now();
6207
- await be.putMeta(meta);
6208
- if (meta.total !== null && offset === 0) {
6209
- const est = await estimate();
6210
- if (!est.fits(meta.total)) throw new WeightError("quota", `not enough storage for ${key}: needs ${meta.total} bytes plus a 20 percent margin, ${(est.quota ?? 0) - (est.used ?? 0)} free`);
6211
- }
6212
- let cur = [];
6213
- let curBytes = 0;
6214
- let loaded = offset;
6215
- const flush = async () => {
6216
- if (!curBytes) return;
6217
- const n = meta.parts.length;
6218
- await be.putPart(key, n, new Blob(cur));
6219
- meta.parts.push(curBytes);
6220
- meta.updated = now();
6221
- await be.putMeta(meta);
6222
- cur = [];
6223
- curBytes = 0;
6224
- };
6225
- if (res.status !== 416 && res.body) {
6226
- const reader = res.body.getReader();
6227
- try {
6228
- for (;;) {
6229
- const { done, value } = await reader.read();
6230
- if (done) break;
6231
- if (o.signal?.aborted) throw new Error("aborted");
6232
- hash.update(value);
6233
- loaded += value.byteLength;
6234
- let rest = value;
6235
- while (rest.byteLength) {
6236
- const take = Math.min(rest.byteLength, partBytes - curBytes);
6237
- cur.push(rest.subarray(0, take));
6238
- curBytes += take;
6239
- rest = rest.subarray(take);
6240
- if (curBytes >= partBytes) await flush();
6241
- }
6242
- o.onProgress?.({
6243
- key,
6244
- loaded,
6245
- total: meta.total,
6246
- fraction: meta.total ? Math.min(1, loaded / meta.total) : null,
6247
- resuming: false
6248
- });
6249
- }
6250
- } catch (e) {
6251
- await flush().catch(() => void 0);
6252
- if (o.signal?.aborted) throw new WeightError("abort", `download of ${key} aborted at ${loaded} bytes; parts kept for resume`);
6253
- throw new WeightError("http", `download of ${key} broke at ${loaded} bytes, parts kept for resume: ${e instanceof Error ? e.message : String(e)}`);
6254
- }
6255
- }
6256
- await flush();
6257
- if (meta.total !== null && loaded !== meta.total) throw new WeightError("http", `download of ${key} ended at ${loaded} of ${meta.total} bytes; parts kept for resume`);
6258
- const digest = hash.digestHex();
6259
- if (ref.sha256 && digest !== ref.sha256.toLowerCase()) {
6260
- await be.remove(key);
6261
- throw new WeightError("sha256", `sha256 of ${key} is ${digest}, expected ${ref.sha256.toLowerCase()}; the parts were removed`);
6262
- }
6263
- meta.total = loaded;
6264
- meta.sha256 = digest;
6265
- meta.complete = true;
6266
- meta.updated = now();
6267
- await be.putMeta(meta);
6268
- const blob = await assemble(be, meta);
6269
- if (!blob) throw new WeightError("http", `parts of ${key} went missing after the download`);
6270
- return blob;
6271
- }
6292
+ const downloader = {
6293
+ backend: be,
6294
+ partBytes,
6295
+ fetch: doFetch,
6296
+ now,
6297
+ estimate
6298
+ };
6272
6299
  const store = {
6273
6300
  backend: be,
6274
6301
  get(ref, o = {}) {
@@ -6305,7 +6332,7 @@ async function openWeightStore(opts = {}) {
6305
6332
  progress,
6306
6333
  row
6307
6334
  };
6308
- flight.p = before.then(() => locked(key, ac.signal, () => download(ref, key, dl))).finally(() => {
6335
+ flight.p = before.then(() => locked(key, ac.signal, () => download(downloader, ref, key, dl))).finally(() => {
6309
6336
  if (inflight.get(key) === flight) inflight.delete(key);
6310
6337
  tell();
6311
6338
  });
@@ -6452,19 +6479,12 @@ function firstInSession(model) {
6452
6479
  seen.add(model);
6453
6480
  return true;
6454
6481
  }
6455
- var WeightError, bareTag, HF_REPO, SEEN;
6482
+ var HF_REPO, SEEN;
6456
6483
  var init_store = __esmMin((() => {
6457
6484
  init_backends();
6458
6485
  init_sha256();
6459
- WeightError = class extends Error {
6460
- code;
6461
- constructor(code, message) {
6462
- super(message);
6463
- this.name = "WeightError";
6464
- this.code = code;
6465
- }
6466
- };
6467
- bareTag = (t) => t ? t.replace(/^W\//, "").replace(/^"|"$/g, "") : null;
6486
+ init_download();
6487
+ init_types$2();
6468
6488
  HF_REPO = /^https:\/\/huggingface\.co\/([^/]+\/[^/]+)\/resolve\//;
6469
6489
  SEEN = Symbol.for("hackbuild.licence.seen");
6470
6490
  }));