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
Local proxy context window: per-request snapshots, follow stored live windows, manual clears auto
Claude702a02cc3006
diff --git a/src/chrome/src/providers/manager.js b/src/chrome/src/providers/manager.jsindex a5136be0a..95f39d3f4 100644--- a/src/chrome/src/providers/manager.js+++ b/src/chrome/src/providers/manager.js@@ -2051,6 +2051,11 @@ export class ProviderManager {if (Object.hasOwn(updates, 'autoContextWindow') && updates.autoContextWindow == null) {delete merged.autoContextWindow;}+ // Explicitly re-entering the auto-filled number is still a user choice: it must stop being+ // replaceable by later detection (e.g. a deliberate 64k cap enlarged to 128k by model name).+ if (updates.manualContextWindow === true) {+ delete merged.autoContextWindow;+ }if (id === 'webgpu' && Object.hasOwn(updates, 'model')) {merged.model = normalizeWebgpuModelId(merged.model);const preset = webgpuModelPreset(merged.model);diff --git a/src/chrome/src/ui/settings.js b/src/chrome/src/ui/settings.jsindex 506e59baa..19cf24e3a 100644--- a/src/chrome/src/ui/settings.js+++ b/src/chrome/src/ui/settings.js@@ -3931,6 +3931,7 @@ function renderProviders() {markProviderDirty(input.dataset.provider);if (input.dataset.key === 'contextWindow') {input.dataset.manuallyEdited = 'true';+ input.dataset.editVersion = String((Number(input.dataset.editVersion) || 0) + 1);}});});@@ -4258,37 +4259,48 @@ function applyProviderBaseUrl(id, baseUrl) {const providerContextPendingSnapshots = new Map();+// What the context field looked like when a background request started. The+// request returns the window the background already stored, so the field must+// show it unless the user touched the field in the meantime. Each request keeps+// its own snapshot, so overlapping Test connection / Load models / model picks+// cannot consume or replace one another's.function markProviderContextDetectionStart(id) {const input = document.querySelector(`input[data-provider="${id}"][data-key="contextWindow"]`);- if (!input) return;- providerContextPendingSnapshots.set(id, {+ if (!input) return undefined;+ const snapshot = {value: input.value,manuallyEdited: input.dataset.manuallyEdited === 'true',- });+ editVersion: Number(input.dataset.editVersion) || 0,+ };+ providerContextPendingSnapshots.set(id, snapshot);+ return snapshot;+}++function clearProviderContextDetectionStart(id, snapshot) {+ if (snapshot === undefined || providerContextPendingSnapshots.get(id) === snapshot) {+ providerContextPendingSnapshots.delete(id);+ }}-function clearProviderContextDetectionStart(id) {- providerContextPendingSnapshots.delete(id);+function providerContextEditedSince(input, snapshot) {+ return input.value !== snapshot.value+ || (input.dataset.manuallyEdited === 'true') !== snapshot.manuallyEdited+ || (Number(input.dataset.editVersion) || 0) !== snapshot.editVersion;}-function applyProviderContextWindow(id, contextWindow) {+function applyProviderContextWindow(id, contextWindow, snapshot = providerContextPendingSnapshots.get(id)) {const n = Number(contextWindow);+ clearProviderContextDetectionStart(id, snapshot);if (!Number.isFinite(n) || n <= 0) return;const input = document.querySelector(`input[data-provider="${id}"][data-key="contextWindow"]`);- const snapshot = providerContextPendingSnapshots.get(id);- providerContextPendingSnapshots.delete(id);if (input) {- if (snapshot) {- const isCurrentlyEdited = input.dataset.manuallyEdited === 'true';- if (input.value !== snapshot.value || isCurrentlyEdited !== snapshot.manuallyEdited) {- return;- }- }- if (input.dataset.manuallyEdited === 'true') return;+ // Only an edit made after the request started wins. A manual value that+ // was saved earlier and left alone is still replaced by a window the+ // background decided to store (a live shrink, say), or the next Save would+ // write the stale larger value back.+ if (snapshot && providerContextEditedSince(input, snapshot)) return;if (input.value !== String(n)) input.value = String(n);input.dataset.manuallyEdited = 'false';- } else if (providersData[id]?.manualContextWindow === true) {- return;}if (providersData[id]) {providersData[id].contextWindow = n;@@ -4299,17 +4311,17 @@ function applyProviderContextWindow(id, contextWindow) {async function detectProviderContextWindowForModel(id, model) {const value = String(model || '').trim();if (!value) return;- if (typeof markProviderContextDetectionStart === 'function') {- markProviderContextDetectionStart(id);- }+ const snapshot = typeof markProviderContextDetectionStart === 'function'+ ? markProviderContextDetectionStart(id)+ : undefined;try {const res = await sendToBackground('detect_provider_context_window', { providerId: id, model: value });- if (res?.ok) applyProviderContextWindow(id, res.contextWindow);+ if (res?.ok) applyProviderContextWindow(id, res.contextWindow, snapshot);} catch {// Model picking should still succeed if the backend cannot report a window.} finally {if (typeof clearProviderContextDetectionStart === 'function') {- clearProviderContextDetectionStart(id);+ clearProviderContextDetectionStart(id, snapshot);}}}@@ -4374,29 +4386,30 @@ async function loadProviderModels(id) {if (!isCurrent()) return;setProviderLoadModelsStatus(id, t('st.providers.loading'));- if (typeof markProviderContextDetectionStart === 'function') markProviderContextDetectionStart(id);+ const contextSnapshot = typeof markProviderContextDetectionStart === 'function'+ ? markProviderContextDetectionStart(id)+ : undefined;+ const releaseContextSnapshot = () => {+ if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id, contextSnapshot);+ };let res;try {res = await sendToBackground('list_provider_models', { providerId: id });} catch (e) {- if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id);+ releaseContextSnapshot();if (!isCurrent()) return;setProviderLoadModelsStatus(id, providerModelLoadErrorMessage(e.message), 'var(--danger, #c33)');return;}- if (!isCurrent()) {- if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id);- return;- }+ // The snapshot travels with this request (passed to apply below); the shared slot is free again.+ releaseContextSnapshot();+ if (!isCurrent()) return;datalistEl = document.getElementById(`models-${id}`);- if (!datalistEl) {- if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id);- return;- }+ if (!datalistEl) return;if (res?.ok) {applyProviderBaseUrl(id, res.baseUrl);- applyProviderContextWindow(id, res.contextWindow);+ applyProviderContextWindow(id, res.contextWindow, contextSnapshot);const loadedDialogEl = document.querySelector(`.loaded-model-dialog[data-loaded-models-for="${id}"]`);datalistEl.innerHTML = res.models.map((m) => `<option value="${escapeHtml(m)}"></option>`)@@ -4545,12 +4558,14 @@ async function testProvider(id) {if (!setProviderTestResult(id, '', t('st.providers.testing'), 'var(--text2)')) return;- if (typeof markProviderContextDetectionStart === 'function') markProviderContextDetectionStart(id);+ const contextSnapshot = typeof markProviderContextDetectionStart === 'function'+ ? markProviderContextDetectionStart(id)+ : undefined;try {const res = await sendToBackground('test_provider', { providerId: id });if (res.ok) {applyProviderBaseUrl(id, res.baseUrl);- applyProviderContextWindow(id, res.contextWindow);+ applyProviderContextWindow(id, res.contextWindow, contextSnapshot);setProviderTestResult(id, 'ok', t('st.providers.connected', { model: res.model || t('st.providers.unknown_model') }));} else {setProviderTestResult(id, 'fail', t('st.providers.failed', { error: res.error }));@@ -4558,7 +4573,7 @@ async function testProvider(id) {} catch (e) {setProviderTestResult(id, 'fail', t('st.providers.failed', { error: e.message }));} finally {- if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id);+ if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id, contextSnapshot);}}diff --git a/src/firefox/src/providers/manager.js b/src/firefox/src/providers/manager.jsindex 0ccf0384e..ac96203c3 100644--- a/src/firefox/src/providers/manager.js+++ b/src/firefox/src/providers/manager.js@@ -1632,6 +1632,11 @@ export class ProviderManager {if (Object.hasOwn(updates, 'autoContextWindow') && updates.autoContextWindow == null) {delete merged.autoContextWindow;}+ // Explicitly re-entering the auto-filled number is still a user choice: it must stop being+ // replaceable by later detection (e.g. a deliberate 64k cap enlarged to 128k by model name).+ if (updates.manualContextWindow === true) {+ delete merged.autoContextWindow;+ }if (this._providerDefinitionId(id, current) === 'ollama') {merged.visionMode = OLLAMA_VISION_MODES.has(merged.visionMode) ? merged.visionMode : 'auto';delete merged.supportsVision;diff --git a/src/firefox/src/ui/settings.js b/src/firefox/src/ui/settings.jsindex cfa48376a..655afe4c6 100644--- a/src/firefox/src/ui/settings.js+++ b/src/firefox/src/ui/settings.js@@ -3426,6 +3426,7 @@ function renderProviders() {markProviderDirty(input.dataset.provider);if (input.dataset.key === 'contextWindow') {input.dataset.manuallyEdited = 'true';+ input.dataset.editVersion = String((Number(input.dataset.editVersion) || 0) + 1);}});});@@ -3722,37 +3723,48 @@ function applyProviderBaseUrl(id, baseUrl) {const providerContextPendingSnapshots = new Map();+// What the context field looked like when a background request started. The+// request returns the window the background already stored, so the field must+// show it unless the user touched the field in the meantime. Each request keeps+// its own snapshot, so overlapping Test connection / Load models / model picks+// cannot consume or replace one another's.function markProviderContextDetectionStart(id) {const input = document.querySelector(`input[data-provider="${id}"][data-key="contextWindow"]`);- if (!input) return;- providerContextPendingSnapshots.set(id, {+ if (!input) return undefined;+ const snapshot = {value: input.value,manuallyEdited: input.dataset.manuallyEdited === 'true',- });+ editVersion: Number(input.dataset.editVersion) || 0,+ };+ providerContextPendingSnapshots.set(id, snapshot);+ return snapshot;+}++function clearProviderContextDetectionStart(id, snapshot) {+ if (snapshot === undefined || providerContextPendingSnapshots.get(id) === snapshot) {+ providerContextPendingSnapshots.delete(id);+ }}-function clearProviderContextDetectionStart(id) {- providerContextPendingSnapshots.delete(id);+function providerContextEditedSince(input, snapshot) {+ return input.value !== snapshot.value+ || (input.dataset.manuallyEdited === 'true') !== snapshot.manuallyEdited+ || (Number(input.dataset.editVersion) || 0) !== snapshot.editVersion;}-function applyProviderContextWindow(id, contextWindow) {+function applyProviderContextWindow(id, contextWindow, snapshot = providerContextPendingSnapshots.get(id)) {const n = Number(contextWindow);+ clearProviderContextDetectionStart(id, snapshot);if (!Number.isFinite(n) || n <= 0) return;const input = document.querySelector(`input[data-provider="${id}"][data-key="contextWindow"]`);- const snapshot = providerContextPendingSnapshots.get(id);- providerContextPendingSnapshots.delete(id);if (input) {- if (snapshot) {- const isCurrentlyEdited = input.dataset.manuallyEdited === 'true';- if (input.value !== snapshot.value || isCurrentlyEdited !== snapshot.manuallyEdited) {- return;- }- }- if (input.dataset.manuallyEdited === 'true') return;+ // Only an edit made after the request started wins. A manual value that+ // was saved earlier and left alone is still replaced by a window the+ // background decided to store (a live shrink, say), or the next Save would+ // write the stale larger value back.+ if (snapshot && providerContextEditedSince(input, snapshot)) return;if (input.value !== String(n)) input.value = String(n);input.dataset.manuallyEdited = 'false';- } else if (providersData[id]?.manualContextWindow === true) {- return;}if (providersData[id]) {providersData[id].contextWindow = n;@@ -3763,17 +3775,17 @@ function applyProviderContextWindow(id, contextWindow) {async function detectProviderContextWindowForModel(id, model) {const value = String(model || '').trim();if (!value) return;- if (typeof markProviderContextDetectionStart === 'function') {- markProviderContextDetectionStart(id);- }+ const snapshot = typeof markProviderContextDetectionStart === 'function'+ ? markProviderContextDetectionStart(id)+ : undefined;try {const res = await sendToBackground('detect_provider_context_window', { providerId: id, model: value });- if (res?.ok) applyProviderContextWindow(id, res.contextWindow);+ if (res?.ok) applyProviderContextWindow(id, res.contextWindow, snapshot);} catch {// Model picking should still succeed if the backend cannot report a window.} finally {if (typeof clearProviderContextDetectionStart === 'function') {- clearProviderContextDetectionStart(id);+ clearProviderContextDetectionStart(id, snapshot);}}}@@ -3836,29 +3848,30 @@ async function loadProviderModels(id) {if (!isCurrent()) return;setProviderLoadModelsStatus(id, t('st.providers.loading'));- if (typeof markProviderContextDetectionStart === 'function') markProviderContextDetectionStart(id);+ const contextSnapshot = typeof markProviderContextDetectionStart === 'function'+ ? markProviderContextDetectionStart(id)+ : undefined;+ const releaseContextSnapshot = () => {+ if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id, contextSnapshot);+ };let res;try {res = await sendToBackground('list_provider_models', { providerId: id });} catch (e) {- if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id);+ releaseContextSnapshot();if (!isCurrent()) return;setProviderLoadModelsStatus(id, providerModelLoadErrorMessage(e.message), 'var(--danger, #c33)');return;}- if (!isCurrent()) {- if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id);- return;- }+ // The snapshot travels with this request (passed to apply below); the shared slot is free again.+ releaseContextSnapshot();+ if (!isCurrent()) return;datalistEl = document.getElementById(`models-${id}`);- if (!datalistEl) {- if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id);- return;- }+ if (!datalistEl) return;if (res?.ok) {applyProviderBaseUrl(id, res.baseUrl);- applyProviderContextWindow(id, res.contextWindow);+ applyProviderContextWindow(id, res.contextWindow, contextSnapshot);const loadedDialogEl = document.querySelector(`.loaded-model-dialog[data-loaded-models-for="${id}"]`);datalistEl.innerHTML = res.models.map((m) => `<option value="${escapeHtml(m)}"></option>`)@@ -3975,12 +3988,14 @@ async function testProvider(id) {if (!setProviderTestResult(id, '', t('st.providers.testing'), 'var(--text2)')) return;- if (typeof markProviderContextDetectionStart === 'function') markProviderContextDetectionStart(id);+ const contextSnapshot = typeof markProviderContextDetectionStart === 'function'+ ? markProviderContextDetectionStart(id)+ : undefined;try {const res = await sendToBackground('test_provider', { providerId: id });if (res.ok) {applyProviderBaseUrl(id, res.baseUrl);- applyProviderContextWindow(id, res.contextWindow);+ applyProviderContextWindow(id, res.contextWindow, contextSnapshot);setProviderTestResult(id, 'ok', t('st.providers.connected', { model: res.model || t('st.providers.unknown_model') }));} else {setProviderTestResult(id, 'fail', t('st.providers.failed', { error: res.error }));@@ -3988,7 +4003,7 @@ async function testProvider(id) {} catch (e) {setProviderTestResult(id, 'fail', t('st.providers.failed', { error: e.message }));} finally {- if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id);+ if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id, contextSnapshot);}}diff --git a/test/run.js b/test/run.jsindex 7b17f2ab6..1c828ccb6 100644--- a/test/run.js+++ b/test/run.js@@ -50062,7 +50062,7 @@ test('settings async test controls surface rejected background results', () => {);assert.match(settings,- /async function detectProviderContextWindowForModel\(id, model\) \{[\s\S]*?sendToBackground\('detect_provider_context_window', \{ providerId: id, model: value \}\);[\s\S]*?applyProviderContextWindow\(id, res\.contextWindow\);[\s\S]*?\}/,+ /async function detectProviderContextWindowForModel\(id, model\) \{[\s\S]*?sendToBackground\('detect_provider_context_window', \{ providerId: id, model: value \}\);[\s\S]*?applyProviderContextWindow\(id, res\.contextWindow, snapshot\);[\s\S]*?\}/,`${label}: selected-model context detection should update the provider context field`,);assert.match(@@ -73072,6 +73072,20 @@ test('Local OpenAI-compatible Proxy context window: model card, live props, modeassert.equal(config().contextWindow, 128000);}+ // 5e'. Re-entering the auto-filled number as a manual choice drops the auto provenance, so a+ // later name-based detection cannot enlarge the deliberate cap.+ {+ const { mgr, config } = setup(async () => new Response('missing', { status: 404 }), {+ model: 'claude-sonnet-4-5', contextWindow: 65536, autoContextWindow: 65536,+ });+ await mgr.updateProvider('local_openai_proxy', { contextWindow: 65536, manualContextWindow: true });+ assert.equal(config().manualContextWindow, true, `${label}: explicit manual flag is kept for an unchanged number`);+ assert.equal(config().autoContextWindow, undefined, `${label}: explicit manual flag clears the auto provenance`);+ const result = await mgr.detectProviderContextWindow('local_openai_proxy', 'claude-sonnet-4-5');+ assert.equal(result.contextWindow, undefined, `${label}: a deliberate 64k is not replaced by the name-based 128k`);+ assert.equal(config().contextWindow, 65536);+ }+// 5f. Settings UI: edits made while detection is pending are preserved{const settingsSource = fs.readFileSync(path.join(ROOT, label === 'chrome' ? 'src/chrome/src/ui/settings.js' : 'src/firefox/src/ui/settings.js'), 'utf8');@@ -73113,6 +73127,47 @@ test('Local OpenAI-compatible Proxy context window: model card, live props, modeassert.equal(inputEl.value, '128000', `${label}: untouched input is updated by detection`);assert.equal(inputEl.dataset.manuallyEdited, 'false');assert.equal(providersData.local_openai_proxy.contextWindow, 128000);++ // Case C: a manual value saved earlier and left alone while detection runs still takes the+ // window the background stored (a live shrink), so the next Save cannot write the old value back.+ inputEl.value = '131072';+ inputEl.dataset.manuallyEdited = 'true';+ providersData.local_openai_proxy = { contextWindow: 131072, manualContextWindow: true };+ markProviderContextDetectionStart('local_openai_proxy');+ applyProviderContextWindow('local_openai_proxy', 32768);+ assert.equal(inputEl.value, '32768', `${label}: an untouched saved manual value follows the stored live window`);+ assert.equal(inputEl.dataset.manuallyEdited, 'false');+ assert.equal(providersData.local_openai_proxy.contextWindow, 32768);+ assert.equal(providersData.local_openai_proxy.manualContextWindow, false);++ // Case D: overlapping requests keep separate snapshots; an edit made after the first one+ // started is still protected when the first one answers last.+ inputEl.value = '65536';+ inputEl.dataset.manuallyEdited = 'false';+ providersData.local_openai_proxy = { contextWindow: 65536, manualContextWindow: false };+ const first = markProviderContextDetectionStart('local_openai_proxy');+ inputEl.value = '24576';+ inputEl.dataset.manuallyEdited = 'true';+ inputEl.dataset.editVersion = '1';+ const second = markProviderContextDetectionStart('local_openai_proxy');+ applyProviderContextWindow('local_openai_proxy', 131072, second);+ assert.equal(inputEl.value, '131072', `${label}: the later request saw the edit as its starting point`);+ inputEl.value = '24576';+ inputEl.dataset.manuallyEdited = 'true';+ providersData.local_openai_proxy = { contextWindow: 65536, manualContextWindow: false };+ applyProviderContextWindow('local_openai_proxy', 131072, first);+ assert.equal(inputEl.value, '24576', `${label}: the earlier request cannot overwrite an edit made after it started`);+ assert.equal(providersData.local_openai_proxy.contextWindow, 65536);++ // Case E: re-typing the same number is still an edit (value unchanged, version bumped).+ inputEl.value = '65536';+ inputEl.dataset.manuallyEdited = 'false';+ inputEl.dataset.editVersion = '5';+ const retyped = markProviderContextDetectionStart('local_openai_proxy');+ inputEl.dataset.editVersion = '6';+ inputEl.dataset.manuallyEdited = 'true';+ applyProviderContextWindow('local_openai_proxy', 131072, retyped);+ assert.equal(inputEl.value, '65536', `${label}: re-entering the same number counts as a user edit`);}// 6. A manual override is never enlarged by a name or the fallback...