From 45e49dda4f0cd7a9c2dc8b4a02e08312da443b84 Mon Sep 17 00:00:00 2001 From: djg Date: Wed, 23 Sep 2026 19:34:28 +0000 Subject: [PATCH] feat(ai): integrate Tier 3 WebLLM engine onto canonical codebase --- site/static/app.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/site/static/app.js b/site/static/app.js index 0a4d96f..4b01559 100644 --- a/site/static/app.js +++ b/site/static/app.js @@ -454,6 +454,28 @@ // Tier 2: BYOK (Bring Your Own Key) Fallback const key = store('byok_key'); if (!key) { + // Try Tier 3 (WebLLM) as the last resort fallback + if (store('ai_webllm_enabled') === 'true') { + if (!window.webllmEngine) { + const { CreateMLCEngine } = await import("https://cdn.jsdelivr.net/npm/@mlc-ai/web-llm/+esm"); + window.webllmEngine = await CreateMLCEngine("Llama-3.2-1B-Instruct-q4f16_1-MLC", { + initProgressCallback: (progress) => { + console.log("WebLLM Progress:", progress); + const el = document.getElementById('webllm-progress'); + if(el) el.innerText = Math.round(progress.progress * 100) + '% - ' + progress.text; + } + }); + } + try { + const reply = await window.webllmEngine.chat.completions.create({ + messages: [{ role: 'user', content: prompt }] + }); + return reply.choices[0].message.content.split('\n').filter(s=>s.trim().length>0); + } catch(e) { + console.error("WebLLM Context/OOM crash:", e); + throw new Error("NO_AI_AVAILABLE"); + } + } throw new Error("NO_AI_AVAILABLE"); // Let UI prompt for key or let Tier 3 try } const resp = await fetch('https://api.openai.com/v1/chat/completions', {