Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Download D1 first when the guide resumes a paused chat model

Claude5275cf57e720
diff --git a/docs/providers-and-models.md b/docs/providers-and-models.md
index 1110b9c16..df253c174 100644
--- a/docs/providers-and-models.md
+++ b/docs/providers-and-models.md
@@ -132,7 +132,9 @@ this GPU cannot run D1. The guide's download queues Bonsai in the background
(`providers/install-download-queue.js`, `installChatModelAfterD1` in storage),
so it follows even after the guide is closed: the guide's reads of D1's
status and an alarm move it along, and a queue older than D1's own
-thirty-minute limit allows is dropped. The guide reads D1's status, checks the
+thirty-minute limit allows is dropped. Every click on the guide's button
+before Bonsai is ready stands for both models: resuming a paused Bonsai
+download also downloads D1 first when D1 is still missing. The guide reads D1's status, checks the
GPU and starts or stops the download (the only `d1_control` commands it may
send); it writes no decision settings, so Auto, the default, then uses D1 as
described above, and the guide says so. The package stores FP16 weights and computes in FP32; it is the
diff --git a/src/chrome/src/providers/install-download-queue.js b/src/chrome/src/providers/install-download-queue.js
index 2d5ee0586..aedadf813 100644
--- a/src/chrome/src/providers/install-download-queue.js
+++ b/src/chrome/src/providers/install-download-queue.js
@@ -55,14 +55,16 @@ export function createChatModelAfterD1({ api, d1Status, chatStatus, startChat, o
let chat;
try { chat = await chatStatus(queued.model); } catch { return true; }
const chatState = String(chat?.status || '');
+ // Not started yet, or failed; or paused when the guide's Resume queued it.
+ const startable = chat?.ready !== true
+ && (!CHAT_TRANSFER_STATUSES.has(chatState) || (chatState === 'paused' && queued.resumeChat === true));
const other = chat?.activeTransfer;
const otherModel = String(other?.modelId || other?.model || '').trim();
- if (chat?.ready !== true && !CHAT_TRANSFER_STATUSES.has(chatState)
- && otherModel && otherModel !== queued.model && CHAT_TRANSFER_STATUSES.has(String(other?.status || ''))) {
+ if (startable && otherModel && otherModel !== queued.model && CHAT_TRANSFER_STATUSES.has(String(other?.status || ''))) {
// Another model holds the WebGPU download slot: start when it is free.
return true;
}
- if (chat?.ready !== true && !CHAT_TRANSFER_STATUSES.has(chatState)) {
+ if (startable) {
try {
await startChat(queued.model);
} catch (error) {
@@ -77,9 +79,13 @@ export function createChatModelAfterD1({ api, d1Status, chatStatus, startChat, o
}
return {
- // The guide asked for both models: the chat model follows D1.
+ // The guide asked for both models: the chat model follows D1. A chat
+ // download that is paused now was paused before this click, so it resumes
+ // after D1; one paused later, elsewhere, is left paused.
queue: (model) => serial(async () => {
- await storage.set({ [CHAT_AFTER_D1_KEY]: { model, queuedAt: now(), paused: false } });
+ let resumeChat = false;
+ try { resumeChat = String((await chatStatus(model))?.status || '') === 'paused'; } catch { /* not known to be paused */ }
+ await storage.set({ [CHAT_AFTER_D1_KEY]: { model, queuedAt: now(), paused: false, ...(resumeChat ? { resumeChat: true } : {}) } });
try { await api.alarms?.create?.(CHAT_AFTER_D1_ALARM, { periodInMinutes: 0.5 }); } catch { /* the guide's reads still advance it */ }
}),
// The guide pauses D1: the chat model waits for its Resume. Called before
diff --git a/src/chrome/src/ui/install.js b/src/chrome/src/ui/install.js
index 020bc1062..b80241933 100644
--- a/src/chrome/src/ui/install.js
+++ b/src/chrome/src/ui/install.js
@@ -350,9 +350,11 @@ export function initializeD1Download({
}
// `chatModel`: the chat model to download once D1's download is over.
+ // Resolves to whether this call queued it behind a D1 download it started.
async function start({ chatModel = '' } = {}) {
- if (starting || ['checking', 'downloading', 'ready', 'saved'].includes(state.status)) return;
+ if (starting || ['checking', 'downloading', 'ready', 'saved'].includes(state.status)) return false;
starting = true;
+ let queued = false;
try {
setState({ status: 'checking' });
try {
@@ -360,19 +362,21 @@ export function initializeD1Download({
} catch (error) {
// No GPU D1 can run on: the chat model may still fit.
setState({ status: 'unsupported', error: error?.message || String(error) });
- return;
+ return false;
}
try {
const response = await command('download', { consent: true, consentVersion: D1_CONSENT_VERSION, ...(chatModel ? { chatModel } : {}) });
setState(normalizeD1Snapshot({ status: 'checking', ...response?.result }));
+ queued = !!chatModel && response?.result?.chatModelQueued === true;
} catch (error) {
setState({ status: 'error', error: error?.message || String(error) });
- return;
+ return false;
}
} finally {
starting = false;
}
await refresh();
+ return queued;
}
async function pause() {
@@ -499,12 +503,12 @@ export function initializeLocalModelDownload({
inFlight = true;
if (view.action === 'd1-start' || view.action === 'd1-pause') {
// Only D1 is downloaded, paused or resumed: before the chat model while
- // D1 goes first, or after it once the chat model is ready. Resuming a D1
- // the chat model was queued behind keeps it queued.
+ // D1 goes first, which keeps the chat model queued behind it, or after
+ // it once the chat model is ready.
render();
try {
if (view.action === 'd1-pause') await d1.pause();
- else await d1.start(d1.getState().chatModelQueued && state.status !== 'ready' ? { chatModel: modelId } : {});
+ else await d1.start(state.status !== 'ready' ? { chatModel: modelId } : {});
} finally {
inFlight = false;
render();
@@ -518,7 +522,11 @@ export function initializeLocalModelDownload({
await backgroundCommand(runtime, 'pause_webgpu_download');
} else if (view.action === 'resume') {
render();
- await backgroundCommand(runtime, 'start_webgpu_download', { model: modelId });
+ // D1 still to download goes first; the paused chat model resumes
+ // after it. A D1 that is done, or cannot download here, leaves the
+ // chat model to resume now.
+ const queued = d1 && ['idle', 'error'].includes(d1.getState().status) ? await d1.start({ chatModel: modelId }) : false;
+ if (!queued) await backgroundCommand(runtime, 'start_webgpu_download', { model: modelId });
} else {
state = { status: 'checking', progress: 0, error: '' };
render();
@@ -539,12 +547,13 @@ export function initializeLocalModelDownload({
// the chat model once D1's download is over. The button stays busy
// until D1's download has started, so a Pause always finds D1's
// transfer to stop.
+ let queued = false;
if (d1) {
state = { status: 'not-downloaded', progress: 0, error: '' };
render();
- await d1.start({ chatModel: modelId });
+ queued = await d1.start({ chatModel: modelId });
}
- if (d1?.getState().chatModelQueued !== true) {
+ if (!queued) {
inFlight = false;
await startChatModel();
return;
diff --git a/src/firefox/src/ui/install.js b/src/firefox/src/ui/install.js
index 020bc1062..b80241933 100644
--- a/src/firefox/src/ui/install.js
+++ b/src/firefox/src/ui/install.js
@@ -350,9 +350,11 @@ export function initializeD1Download({
}
// `chatModel`: the chat model to download once D1's download is over.
+ // Resolves to whether this call queued it behind a D1 download it started.
async function start({ chatModel = '' } = {}) {
- if (starting || ['checking', 'downloading', 'ready', 'saved'].includes(state.status)) return;
+ if (starting || ['checking', 'downloading', 'ready', 'saved'].includes(state.status)) return false;
starting = true;
+ let queued = false;
try {
setState({ status: 'checking' });
try {
@@ -360,19 +362,21 @@ export function initializeD1Download({
} catch (error) {
// No GPU D1 can run on: the chat model may still fit.
setState({ status: 'unsupported', error: error?.message || String(error) });
- return;
+ return false;
}
try {
const response = await command('download', { consent: true, consentVersion: D1_CONSENT_VERSION, ...(chatModel ? { chatModel } : {}) });
setState(normalizeD1Snapshot({ status: 'checking', ...response?.result }));
+ queued = !!chatModel && response?.result?.chatModelQueued === true;
} catch (error) {
setState({ status: 'error', error: error?.message || String(error) });
- return;
+ return false;
}
} finally {
starting = false;
}
await refresh();
+ return queued;
}
async function pause() {
@@ -499,12 +503,12 @@ export function initializeLocalModelDownload({
inFlight = true;
if (view.action === 'd1-start' || view.action === 'd1-pause') {
// Only D1 is downloaded, paused or resumed: before the chat model while
- // D1 goes first, or after it once the chat model is ready. Resuming a D1
- // the chat model was queued behind keeps it queued.
+ // D1 goes first, which keeps the chat model queued behind it, or after
+ // it once the chat model is ready.
render();
try {
if (view.action === 'd1-pause') await d1.pause();
- else await d1.start(d1.getState().chatModelQueued && state.status !== 'ready' ? { chatModel: modelId } : {});
+ else await d1.start(state.status !== 'ready' ? { chatModel: modelId } : {});
} finally {
inFlight = false;
render();
@@ -518,7 +522,11 @@ export function initializeLocalModelDownload({
await backgroundCommand(runtime, 'pause_webgpu_download');
} else if (view.action === 'resume') {
render();
- await backgroundCommand(runtime, 'start_webgpu_download', { model: modelId });
+ // D1 still to download goes first; the paused chat model resumes
+ // after it. A D1 that is done, or cannot download here, leaves the
+ // chat model to resume now.
+ const queued = d1 && ['idle', 'error'].includes(d1.getState().status) ? await d1.start({ chatModel: modelId }) : false;
+ if (!queued) await backgroundCommand(runtime, 'start_webgpu_download', { model: modelId });
} else {
state = { status: 'checking', progress: 0, error: '' };
render();
@@ -539,12 +547,13 @@ export function initializeLocalModelDownload({
// the chat model once D1's download is over. The button stays busy
// until D1's download has started, so a Pause always finds D1's
// transfer to stop.
+ let queued = false;
if (d1) {
state = { status: 'not-downloaded', progress: 0, error: '' };
render();
- await d1.start({ chatModel: modelId });
+ queued = await d1.start({ chatModel: modelId });
}
- if (d1?.getState().chatModelQueued !== true) {
+ if (!queued) {
inFlight = false;
await startChatModel();
return;
diff --git a/test/d1-decision.mjs b/test/d1-decision.mjs
index c3007dfcd..8c49cf28a 100644
--- a/test/d1-decision.mjs
+++ b/test/d1-decision.mjs
@@ -613,14 +613,27 @@ test('chrome: the install guide queues its chat model behind D1, and the backgro
assert.deepEqual(env.starts, [model], d1.status);
}
for (const chat of [{ status: 'downloading' }, { status: 'paused' }, { status: 'ready', ready: true }, { status: 'loading' }]) {
- // A chat model already under way, paused elsewhere or ready is left alone.
- const env = make({ chat });
+ // A chat model started, paused elsewhere or downloaded since the guide
+ // queued it is left alone.
+ const env = make();
await env.queue.queue(model);
+ env.chat = chat;
env.d1 = { status: 'ready', loaded: true };
assert.equal(await env.queue.advance(), false, chat.status);
assert.deepEqual(env.starts, [], chat.status);
assert.equal(env.area[queue.CHAT_AFTER_D1_KEY], undefined, chat.status);
}
+ {
+ // A chat download already paused when the guide's Resume queued it
+ // resumes after D1.
+ const env = make({ chat: { status: 'paused', progress: 40 } });
+ await env.queue.queue(model);
+ assert.equal(env.area[queue.CHAT_AFTER_D1_KEY].resumeChat, true);
+ assert.equal(await env.queue.advance(), true, 'D1 first');
+ env.d1 = { status: 'ready', loaded: true };
+ assert.equal(await env.queue.advance(), false);
+ assert.deepEqual(env.starts, [model]);
+ }
{
// Another model holds the WebGPU download slot: start when it is free.
const env = make({ chat: { status: 'not-downloaded', activeTransfer: { modelId: 'other/model', status: 'downloading' } } });
diff --git a/test/run.js b/test/run.js
index 64849ec01..10c97a00c 100644
--- a/test/run.js
+++ b/test/run.js
@@ -45946,26 +45946,70 @@ test('chrome install page downloads D1 first, then the chat model, and says what
{
// A guide opened while D1 downloads with nothing queued (started on the
- // Providers page, say) follows that download, but only a click here
- // downloads the chat model.
- const state = { bonsai: { status: 'not-downloaded', modelId }, d1: { status: 'downloading', progress: 70 } };
+ // Providers page, say) follows that download; only a click here, which
+ // stands for both models, queues the chat model.
+ for (const clicked of [false, true]) {
+ const state = { bonsai: { status: 'not-downloaded', modelId }, d1: { status: 'downloading', progress: 70 } };
+ const runtime = fakeRuntime({ ...bonsai(state), ...queueBackground(state) });
+ const documentLike = fakeDocument();
+ const controller = install.initializeLocalModelDownload({ documentLike, runtime, translate, pollMs: 60_000 });
+ await settle();
+ assert.equal(documentLike.nodes['local-model-label'].textContent, 'install.local_model.pause');
+ assert.equal(documentLike.nodes['local-model-d1'].textContent, 'install.local_model.d1_downloading|70');
+ if (clicked) {
+ await documentLike.nodes['local-model-button'].fire('click');
+ await settle();
+ await documentLike.nodes['local-model-button'].fire('click');
+ await settle();
+ assert.equal(d1Downloads(runtime)[0].chatModel, modelId, 'the guide\'s Resume queues the chat model behind D1');
+ }
+ state.d1 = { status: 'ready', loaded: true };
+ await controller.d1.refresh();
+ await settle();
+ assert.equal(state.backgroundStarts || 0, clicked ? 1 : 0, clicked ? 'the chat model follows D1' : 'no click, no chat model download');
+ assert.equal(chatStarts(runtime), 0);
+ assert.equal(documentLike.nodes['local-model-label'].textContent, clicked ? 'install.local_model.pause' : 'install.local_model.download');
+ controller.dispose();
+ }
+ }
+
+ {
+ // A paused chat download with D1 still to download: Resume downloads D1
+ // first and leaves the chat model queued to resume after it.
+ const state = { bonsai: { status: 'paused', modelId, progress: 40 }, d1: { status: 'idle' } };
const runtime = fakeRuntime({ ...bonsai(state), ...queueBackground(state) });
const documentLike = fakeDocument();
const controller = install.initializeLocalModelDownload({ documentLike, runtime, translate, pollMs: 60_000 });
await settle();
- assert.equal(documentLike.nodes['local-model-label'].textContent, 'install.local_model.pause');
- assert.equal(documentLike.nodes['local-model-d1'].textContent, 'install.local_model.d1_downloading|70');
- await documentLike.nodes['local-model-button'].fire('click');
- await settle();
+ assert.equal(documentLike.nodes['local-model-label'].textContent, 'install.local_model.resume');
await documentLike.nodes['local-model-button'].fire('click');
await settle();
- assert.equal(d1Downloads(runtime)[0].chatModel, undefined, 'resuming a download started elsewhere queues nothing');
+ assert.deepEqual(d1Commands(runtime), ['probe', 'download']);
+ assert.equal(d1Downloads(runtime)[0].chatModel, modelId);
+ assert.equal(chatStarts(runtime), 0, 'the chat model waits for D1');
+ assert.equal(documentLike.nodes['local-model-label'].textContent, 'install.local_model.pause', 'the button follows D1');
+ assert.equal(documentLike.nodes['local-model-status'].textContent, 'install.local_model.paused|40', 'the chat line keeps its own progress');
state.d1 = { status: 'ready', loaded: true };
await controller.d1.refresh();
await settle();
- assert.equal(state.backgroundStarts || 0, 0, 'no click, no chat model download');
- assert.equal(chatStarts(runtime), 0);
- assert.equal(documentLike.nodes['local-model-label'].textContent, 'install.local_model.download');
+ assert.equal(state.backgroundStarts, 1, 'the chat model resumes after D1');
+ controller.dispose();
+ }
+
+ {
+ // A paused chat download with D1 done: Resume resumes the chat model now.
+ const state = { bonsai: { status: 'paused', modelId, progress: 40 } };
+ const runtime = fakeRuntime({
+ ...bonsai(state),
+ 'd1_control:status': { success: true, result: { status: 'cached', usedForDecisions: true, chatModelQueued: false } },
+ });
+ const documentLike = fakeDocument();
+ const controller = install.initializeLocalModelDownload({ documentLike, runtime, translate, pollMs: 60_000 });
+ await settle();
+ await documentLike.nodes['local-model-button'].fire('click');
+ await settle();
+ assert.deepEqual(d1Commands(runtime), []);
+ assert.equal(chatStarts(runtime), 1);
controller.dispose();
}