MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / prepare

Function prepare

graph-ui/src/browser-ai/browser-ai.worker.ts:65–114  ·  view source on GitHub ↗
(id: number, modelId: string, cacheOnly = false)

Source from the content-addressed store, hash-verified

63};
64
65async function prepare(id: number, modelId: string, cacheOnly = false): Promise<void> {
66 const next = getBrowserModel(modelId);
67 if (next.availability !== 'available') throw new Error(next.compatibilityNote);
68 if (model && tokenizer) {
69 if (selected.id !== modelId) throw new Error('Unload the current model before changing models.');
70 return;
71 }
72 selected = next;
73 const gpu = (navigator as unknown as { gpu?: { requestAdapter(): Promise<{ features: { has(feature: string): boolean } } | null> } }).gpu;
74 if (!gpu) throw new Error('WebGPU is unavailable in this browser. Use a browser with WebGPU enabled. No model was downloaded.');
75 const adapter = await gpu.requestAdapter();
76 if (!adapter?.features.has('shader-f16')) throw new Error('This model needs WebGPU with shader-f16 support. No model was downloaded.');
77 if (typeof caches === 'undefined') throw new Error('Browser model storage is unavailable. Use a secure localhost browser session.');
78 env.allowLocalModels = false;
79 env.allowRemoteModels = true;
80 // v4 tokenizer discovery drops revision options; even its metadata probes must stay pinned.
81 env.remotePathTemplate = `{model}/resolve/${selected.revision}/`;
82 env.useBrowserCache = false;
83 env.useFSCache = false;
84 // v4 otherwise wraps the local .mjs in a blob URL, which product CSP correctly rejects.
85 env.useWasmCache = false;
86 // v4 captures native fetch during import; explicitly retain our pinned download boundary.
87 env.fetch = globalThis.fetch;
88 env.useCustomCache = true;
89 env.customCache = await caches.open(selected.cacheName);
90 if (env.backends.onnx.wasm) {
91 env.backends.onnx.wasm.numThreads = 1;
92 env.backends.onnx.wasm.proxy = false;
93 env.backends.onnx.wasm.wasmPaths = { wasm: new URL(wasmUrl, self.location.href).href, mjs: new URL(wasmLoaderUrl, self.location.href).href };
94 }
95 const options = {
96 revision: selected.revision,
97 progress_callback: (progress: unknown) => {
98 const update = progress as BrowserAiProgress;
99 post({ id, kind: 'progress', progress: { file: update.file, loaded: update.loaded, total: update.total, progress: update.progress } });
100 },
101 };
102 // A resumed model loads from Cache Storage; a missing file fails instead of downloading.
103 downloadsAllowed = !cacheOnly;
104 try {
105 tokenizer = await AutoTokenizer.from_pretrained(selected.id, options);
106 // CausalLM loads only decoder + embeddings for Qwen3.5; the policy excludes all vision files.
107 model = await AutoModelForCausalLM.from_pretrained(selected.id, { ...options, device: 'webgpu', dtype: selected.dtype });
108 observeInferenceDevice();
109 } catch (error) {
110 tokenizer = undefined;
111 model = undefined;
112 throw error;
113 } finally { downloadsAllowed = false; }
114}
115
116/**
117 * Transformers.js counts prompt tokens as earlier n-grams. A name or path quoted

Callers 1

Calls 4

getBrowserModelFunction · 0.90
postFunction · 0.85
observeInferenceDeviceFunction · 0.85
openMethod · 0.80

Tested by

no test coverage detected