File size: 11,052 Bytes
1944112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
 * Drag-and-drop ingestion: turn a folder of MLC-compiled model artifacts into
 * populated Cache Storage entries, with no network involved at any point.
 *
 * Everything is validated before the first byte is written, so a folder that is
 * missing a shard fails immediately instead of after copying 2 GB.
 */
import { ERROR, EngineError } from "./errors.js";
import { CACHE_CONFIG, CACHE_MODEL, CACHE_WASM, SOURCE, baseUrlFor, toModelType } from "./model-store.js";

/** WebLLM asks for this name; older MLC exports ship `ndarray-cache.json`. */
export const TENSOR_MANIFEST = "tensor-cache.json";
export const LEGACY_TENSOR_MANIFEST = "ndarray-cache.json";
export const CHAT_CONFIG = "mlc-chat-config.json";

export const CONTENT_TYPES = {
  json: "application/json",
  wasm: "application/wasm",
  bin: "application/octet-stream",
};

/**
 * Walk a DataTransfer from a drop event into flat `{ path, file }` entries.
 * Uses the entries API so dropping a *folder* works, not just a file selection.
 */
export async function filesFromDataTransfer(dataTransfer) {
  // `Array.from` throughout, for the reason `filesFromInput` gives: a
  // DataTransferItemList and a FileList are array-like, and only sometimes
  // iterable. Spreading them threw from inside here, three frames from the drop
  // handler the caller actually wrote.
  const roots = Array.from(dataTransfer.items)
    .filter((item) => item.kind === "file")
    .map((item) => (item.webkitGetAsEntry ? item.webkitGetAsEntry() : null));

  if (roots.some((entry) => entry === null)) {
    // No entries API: fall back to the flat file list (a folder drop yields nothing).
    return Array.from(dataTransfer.files, (file) => ({
      path: file.webkitRelativePath || file.name,
      file,
    }));
  }

  const out = [];
  await Promise.all(roots.filter(Boolean).map((entry) => walkEntry(entry, "", out)));
  return out;
}

async function walkEntry(entry, prefix, out) {
  const path = prefix ? `${prefix}/${entry.name}` : entry.name;
  if (entry.isFile) {
    out.push({ path, file: await new Promise((res, rej) => entry.file(res, rej)) });
    return;
  }
  const reader = entry.createReader();
  // readEntries() returns at most ~100 entries per call; drain it.
  for (;;) {
    const batch = await new Promise((res, rej) => reader.readEntries(res, rej));
    if (batch.length === 0) break;
    await Promise.all(batch.map((child) => walkEntry(child, path, out)));
  }
}

/**
 * Turns `<input webkitdirectory>` output into the same `{ path, file }` shape.
 *
 * `Array.from`, not spread: a real `FileList` is iterable, but plenty of things
 * that behave like one are only array-like, and spreading those fails with
 * "fileList is not iterable" — an error that names none of the three places it
 * could have come from. Array.from accepts both.
 */
export function filesFromInput(fileList) {
  return Array.from(fileList, (file) => ({
    path: file.webkitRelativePath || file.name,
    file,
  }));
}

/**
 * @param {Array<{path: string, file: File}>} entries
 * @param {object} opts
 * @param {import("./model-store.js").ModelStore} opts.store where the registry entry lands
 * @param {string} [opts.modelId] overrides the id inferred from the folder name
 * @param {"llm"|"embedding"|"vlm"} [opts.modelType] declare a vision model, or WebLLM
 *   rejects every image sent to it
 * @param {(p: {phase: string, done: number, total: number, label: string}) => void} [opts.onProgress]
 * @returns {Promise<object>} the saved registry record
 */
export async function ingestModelFolder(entries, { store, modelId, modelType, onProgress = () => {} } = {}) {
  if (!store) {
    throw new EngineError(ERROR.BAD_REQUEST, "ingestModelFolder needs a `store` to save the registry entry into.");
  }
  if (!entries?.length) {
    throw new EngineError(ERROR.INVALID_MODEL_FOLDER, "Nothing was dropped — expected a model folder.", {
      reason: "empty",
    });
  }

  const byPath = new Map();
  const byName = new Map();
  for (const { path, file } of entries) {
    const relative = stripRoot(path);
    byPath.set(relative, file);
    // Last writer wins; `find()` prefers the exact relative path anyway.
    byName.set(basename(relative), file);
  }

  const find = (name) => byPath.get(name) ?? byName.get(basename(name));

  onProgress({ phase: "validating", done: 0, total: 1, label: "Reading manifests" });

  const configFile = find(CHAT_CONFIG);
  if (!configFile) {
    throw new EngineError(
      ERROR.INVALID_MODEL_FOLDER,
      `Missing ${CHAT_CONFIG}. Use the folder produced by \`mlc_llm convert_weights\` + \`gen_config\`, not a raw HuggingFace checkpoint.`,
      { reason: "missing-config", missing: [CHAT_CONFIG] },
    );
  }
  const chatConfig = await readJson(configFile, CHAT_CONFIG);

  const tensorFile = find(TENSOR_MANIFEST) ?? find(LEGACY_TENSOR_MANIFEST);
  if (!tensorFile) {
    throw new EngineError(
      ERROR.INVALID_MODEL_FOLDER,
      `Missing ${TENSOR_MANIFEST} (or legacy ${LEGACY_TENSOR_MANIFEST}) — the weight shard index.`,
      { reason: "missing-manifest", missing: [TENSOR_MANIFEST] },
    );
  }
  const isLegacyManifest = !find(TENSOR_MANIFEST);
  const tensorManifest = await readJson(tensorFile, tensorFile.name);

  const records = tensorManifest.records;
  if (!Array.isArray(records) || records.length === 0) {
    throw new EngineError(
      ERROR.INVALID_MODEL_FOLDER,
      `${tensorFile.name} has no "records" array — it is not an MLC weight index.`,
      { reason: "malformed-manifest" },
    );
  }

  const shardPaths = records.map((r) => r.dataPath).filter(Boolean);
  if (shardPaths.length !== records.length) {
    throw new EngineError(
      ERROR.INVALID_MODEL_FOLDER,
      `${tensorFile.name} has records without a "dataPath".`,
      { reason: "malformed-manifest" },
    );
  }
  const missingShards = shardPaths.filter((p) => !find(p));
  if (missingShards.length) {
    throw new EngineError(
      ERROR.INVALID_MODEL_FOLDER,
      `${missingShards.length} weight shard(s) missing from the folder: ${missingShards.slice(0, 5).join(", ")}${missingShards.length > 5 ? ", …" : ""}`,
      { reason: "missing-shards", missing: missingShards },
    );
  }

  const tokenizerNames = Array.isArray(chatConfig.tokenizer_files) ? chatConfig.tokenizer_files : [];
  const tokenizerName = ["tokenizer.json", "tokenizer.model"].find(
    (name) => tokenizerNames.includes(name) && find(name),
  );
  if (!tokenizerName) {
    throw new EngineError(
      ERROR.INVALID_MODEL_FOLDER,
      `No usable tokenizer. ${CHAT_CONFIG} lists [${tokenizerNames.join(", ") || "nothing"}], and neither tokenizer.json nor tokenizer.model is present in the folder.`,
      { reason: "missing-tokenizer", expected: tokenizerNames },
    );
  }

  const wasmEntries = [...byPath.entries()].filter(([p]) => p.endsWith(".wasm"));
  if (wasmEntries.length === 0) {
    throw new EngineError(
      ERROR.INVALID_MODEL_FOLDER,
      "No .wasm model library found. Add the matching `*-webgpu.wasm` from mlc-ai/binary-mlc-llm-libs to the folder.",
      { reason: "missing-wasm" },
    );
  }
  if (wasmEntries.length > 1) {
    throw new EngineError(
      ERROR.INVALID_MODEL_FOLDER,
      `Found ${wasmEntries.length} .wasm files (${wasmEntries.map(([p]) => p).join(", ")}); the folder must contain exactly one model library.`,
      { reason: "ambiguous-wasm", found: wasmEntries.map(([p]) => p) },
    );
  }
  const [wasmPath, wasmFile] = wasmEntries[0];
  const wasmName = basename(wasmPath);

  const id = (modelId || inferModelId(entries) || wasmName.replace(/(-webgpu)?\.wasm$/, "")).trim();
  if (!id) {
    throw new EngineError(
      ERROR.INVALID_MODEL_FOLDER,
      "Could not determine a model id — name the folder after the model, or pass `modelId`.",
      { reason: "no-model-id" },
    );
  }

  const base = baseUrlFor(id);

  // Everything validated: build the write plan.
  const plan = [
    { scope: CACHE_CONFIG, url: base + CHAT_CONFIG, file: configFile, type: CONTENT_TYPES.json },
    { scope: CACHE_MODEL, url: base + TENSOR_MANIFEST, file: tensorFile, type: CONTENT_TYPES.json },
    {
      scope: CACHE_MODEL,
      url: base + tokenizerName,
      file: find(tokenizerName),
      type: tokenizerName.endsWith(".json") ? CONTENT_TYPES.json : CONTENT_TYPES.bin,
    },
    { scope: CACHE_WASM, url: base + wasmName, file: wasmFile, type: CONTENT_TYPES.wasm },
    ...shardPaths.map((p) => ({
      scope: CACHE_MODEL,
      url: new URL(p, base).href,
      file: find(p),
      type: CONTENT_TYPES.bin,
    })),
  ];
  if (isLegacyManifest) {
    // Keep the original name addressable too, so a future WebLLM that reverts
    // to `ndarray-cache.json` still hits cache.
    plan.push({
      scope: CACHE_MODEL,
      url: base + LEGACY_TENSOR_MANIFEST,
      file: tensorFile,
      type: CONTENT_TYPES.json,
    });
  }

  const openCaches = new Map();
  let done = 0;
  for (const item of plan) {
    if (!openCaches.has(item.scope)) openCaches.set(item.scope, await caches.open(item.scope));
    onProgress({ phase: "writing", done, total: plan.length, label: basename(item.url) });
    await openCaches.get(item.scope).put(
      new Request(item.url),
      new Response(item.file, { status: 200, headers: { "Content-Type": item.type } }),
    );
    done += 1;
  }
  onProgress({ phase: "writing", done, total: plan.length, label: "done" });

  const keys = { [CACHE_CONFIG]: [], [CACHE_MODEL]: [], [CACHE_WASM]: [] };
  for (const item of plan) keys[item.scope].push(item.url);

  return store.save({
    model_id: id,
    model: base,
    model_lib: base + wasmName,
    source: SOURCE.INJECTED,
    ...(toModelType(modelType) !== undefined ? { model_type: toModelType(modelType) } : {}),
    ...(chatConfig.context_window_size > 0
      ? { overrides: { context_window_size: chatConfig.context_window_size } }
      : {}),
    keys,
    sizeBytes: plan.reduce((sum, item) => sum + item.file.size, 0),
    fileCount: plan.length,
    shardCount: shardPaths.length,
    tokenizer: tokenizerName,
    wasm: wasmName,
    addedAt: new Date().toISOString(),
  });
}

async function readJson(file, label) {
  try {
    return JSON.parse(await file.text());
  } catch (err) {
    throw new EngineError(ERROR.INVALID_MODEL_FOLDER, `${label} is not valid JSON: ${err.message}`, {
      reason: "malformed-json",
      file: label,
    });
  }
}

/** Drops the dropped-folder name so `Qwen3-4B/tokenizer.json` keys as `tokenizer.json`. */
function stripRoot(path) {
  const parts = path.split("/");
  return parts.length > 1 ? parts.slice(1).join("/") : path;
}

function basename(path) {
  return path.split("/").pop();
}

/**
 * A dropped *folder* gives every entry the same first path segment; a flat
 * multi-file selection gives each entry a bare filename. Only the former names
 * the model.
 */
function inferModelId(entries) {
  const roots = new Set(
    entries.filter((e) => e.path.includes("/")).map((e) => e.path.split("/")[0]),
  );
  return roots.size === 1 ? [...roots][0] : "";
}