diff --git a/site/static/app.js b/site/static/app.js index 3954209..80a1ba2 100644 --- a/site/static/app.js +++ b/site/static/app.js @@ -497,6 +497,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', {