Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
Keep the queued chat model until its download starts
Clauded6d2ab339222
diff --git a/src/chrome/src/background.js b/src/chrome/src/background.jsindex f9d30027f..c11fe0189 100644--- a/src/chrome/src/background.js+++ b/src/chrome/src/background.js@@ -199,11 +199,14 @@ const chatModelAfterD1 = createChatModelAfterD1({if (providerManager.providers.size === 0) await providerManager.load();return providerManager.startWebgpuDownload({ model });},+ onStartError: (error) => {+ console.warn('[WebBrain] The queued local chat-model download could not start yet:', error);+ },});chrome.alarms.onAlarm.addListener((alarm) => {if (alarm?.name !== CHAT_AFTER_D1_ALARM) return;chatModelAfterD1.advance().catch((error) => {- console.warn('[WebBrain] The queued local chat-model download could not start:', error);+ console.warn('[WebBrain] The queued local chat-model download could not be checked:', error);});});const apocalypseController = createApocalypseController(chrome);@@ -5041,7 +5044,7 @@ async function handleMessage(msg, sender) {else if (msg.command === 'stop') await chatModelAfterD1.pause();const chatModelQueued = msg.command === 'status'? await chatModelAfterD1.advance(result).catch((error) => {- console.warn('[WebBrain] The queued local chat-model download could not start:', error);+ console.warn('[WebBrain] The queued local chat-model download could not be checked:', error);return false;}): await chatModelAfterD1.queued();diff --git a/src/chrome/src/providers/install-download-queue.js b/src/chrome/src/providers/install-download-queue.jsindex fa613d618..c73a1d08a 100644--- a/src/chrome/src/providers/install-download-queue.js+++ b/src/chrome/src/providers/install-download-queue.js@@ -17,9 +17,10 @@ const CHAT_TRANSFER_STATUSES = new Set(['checking', 'starting', 'queued', 'downl/*** `d1Status()` reads D1's status, `chatStatus(model)` the chat model's- * download status and `startChat(model)` starts that download.+ * download status and `startChat(model)` starts that download. A start that+ * fails keeps the queue for the next check, and `onStartError` hears why.*/-export function createChatModelAfterD1({ api, d1Status, chatStatus, startChat, now = () => Date.now() }) {+export function createChatModelAfterD1({ api, d1Status, chatStatus, startChat, onStartError = () => {}, now = () => Date.now() }) {const storage = api.storage.local;const read = async () => (await storage.get(CHAT_AFTER_D1_KEY))?.[CHAT_AFTER_D1_KEY] || null;const clearAlarm = async () => { try { await api.alarms?.clear?.(CHAT_AFTER_D1_ALARM); } catch { /* best effort */ } };@@ -60,9 +61,17 @@ export function createChatModelAfterD1({ api, d1Status, chatStatus, startChat, n// Another model holds the WebGPU download slot: start when it is free.return true;}+ if (chat?.ready !== true && !CHAT_TRANSFER_STATUSES.has(chatState)) {+ try {+ await startChat(queued.model);+ } catch (error) {+ // Kept for the next check (the alarm, or the guide's next read) until+ // it starts or the queue is too old.+ onStartError(error);+ return true;+ }+ }await clear();- if (chat?.ready === true || CHAT_TRANSFER_STATUSES.has(chatState)) return false;- await startChat(queued.model);return false;}diff --git a/test/d1-decision.mjs b/test/d1-decision.mjsindex f4cd40172..ee03ddddd 100644--- a/test/d1-decision.mjs+++ b/test/d1-decision.mjs@@ -298,6 +298,7 @@ for (const build of ['chrome', 'firefox']) {assert.match(branch, /msg\.command === 'status'\s*\? await chatModelAfterD1\.advance\(result\)/);assert.match(branch, /result: \{ \.\.\.result, usedForDecisions: await d1UsedForDecisions\(api\), chatModelQueued \}/);assert.match(background, /chrome\.alarms\.onAlarm\.addListener\(\(alarm\) => \{\s*if \(alarm\?\.name !== CHAT_AFTER_D1_ALARM\) return;\s*chatModelAfterD1\.advance\(\)/);+ assert.match(background, /onStartError: \(error\) => \{\s*console\.warn\(/, 'a failed start is reported and retried');} else {// Firefox has no in-browser chat model to queue.assert.match(branch, /result: fromInstall \? \{ \.\.\.result, usedForDecisions: await d1UsedForDecisions\(api\) \} : result/);@@ -619,6 +620,36 @@ test('chrome: the install guide queues its chat model behind D1, and the backgroassert.equal(await env.queue.advance(), false);assert.deepEqual(env.starts, [model]);}+ {+ // A start that fails keeps the queue for the next check, until it starts.+ const env = make();+ const errors = [];+ let failures = 1;+ env.queue = queue.createChatModelAfterD1({+ api: { storage: { local: {+ get: async (key) => (key in env.area ? { [key]: structuredClone(env.area[key]) } : {}),+ set: async (values) => { Object.assign(env.area, structuredClone(values)); },+ remove: async (key) => { delete env.area[key]; },+ } }, alarms: { create: async () => {}, clear: async (name) => { env.cleared.push(name); return true; } } },+ d1Status: async () => env.d1,+ chatStatus: async () => env.chat,+ startChat: async (name) => {+ if (failures-- > 0) throw new Error('Offscreen document is starting.');+ env.starts.push(name); env.chat = { status: 'downloading' };+ },+ onStartError: (error) => errors.push(error.message),+ now: () => env.now,+ });+ await env.queue.queue(model);+ env.d1 = { status: 'ready', loaded: true };+ assert.equal(await env.queue.advance(), true, 'still queued after a failed start');+ assert.deepEqual(errors, ['Offscreen document is starting.']);+ assert.equal(env.area[queue.CHAT_AFTER_D1_KEY].model, model);+ assert.deepEqual(env.cleared, [], 'the alarm keeps checking');+ assert.equal(await env.queue.advance(), false);+ assert.deepEqual(env.starts, [model]);+ assert.equal(env.area[queue.CHAT_AFTER_D1_KEY], undefined);+ }{// D1's status cannot be read: try again later.const env = make({ d1: new Error('offscreen document closed') });