Skip to content

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

Preserve context window edits made while detection is pending

Emre Sokulluf6d121f7322f
diff --git a/src/chrome/src/ui/settings.js b/src/chrome/src/ui/settings.js
index e50695e37..506e59baa 100644
--- a/src/chrome/src/ui/settings.js
+++ b/src/chrome/src/ui/settings.js
@@ -4256,28 +4256,61 @@ function applyProviderBaseUrl(id, baseUrl) {
refreshVisionStatus(id);
}
+const providerContextPendingSnapshots = new Map();
+
+function markProviderContextDetectionStart(id) {
+ const input = document.querySelector(`input[data-provider="${id}"][data-key="contextWindow"]`);
+ if (!input) return;
+ providerContextPendingSnapshots.set(id, {
+ value: input.value,
+ manuallyEdited: input.dataset.manuallyEdited === 'true',
+ });
+}
+
+function clearProviderContextDetectionStart(id) {
+ providerContextPendingSnapshots.delete(id);
+}
+
function applyProviderContextWindow(id, contextWindow) {
const n = Number(contextWindow);
if (!Number.isFinite(n) || n <= 0) return;
- if (providersData[id]) {
- providersData[id].contextWindow = n;
- providersData[id].manualContextWindow = false;
- }
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;
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;
+ providersData[id].manualContextWindow = false;
}
}
async function detectProviderContextWindowForModel(id, model) {
const value = String(model || '').trim();
if (!value) return;
+ if (typeof markProviderContextDetectionStart === 'function') {
+ markProviderContextDetectionStart(id);
+ }
try {
const res = await sendToBackground('detect_provider_context_window', { providerId: id, model: value });
if (res?.ok) applyProviderContextWindow(id, res.contextWindow);
} catch {
// Model picking should still succeed if the backend cannot report a window.
+ } finally {
+ if (typeof clearProviderContextDetectionStart === 'function') {
+ clearProviderContextDetectionStart(id);
+ }
}
}
@@ -4341,18 +4374,26 @@ async function loadProviderModels(id) {
if (!isCurrent()) return;
setProviderLoadModelsStatus(id, t('st.providers.loading'));
+ if (typeof markProviderContextDetectionStart === 'function') markProviderContextDetectionStart(id);
let res;
try {
res = await sendToBackground('list_provider_models', { providerId: id });
} catch (e) {
+ if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id);
if (!isCurrent()) return;
setProviderLoadModelsStatus(id, providerModelLoadErrorMessage(e.message), 'var(--danger, #c33)');
return;
}
- if (!isCurrent()) return;
+ if (!isCurrent()) {
+ if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id);
+ return;
+ }
datalistEl = document.getElementById(`models-${id}`);
- if (!datalistEl) return;
+ if (!datalistEl) {
+ if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id);
+ return;
+ }
if (res?.ok) {
applyProviderBaseUrl(id, res.baseUrl);
applyProviderContextWindow(id, res.contextWindow);
@@ -4504,6 +4545,7 @@ async function testProvider(id) {
if (!setProviderTestResult(id, '', t('st.providers.testing'), 'var(--text2)')) return;
+ if (typeof markProviderContextDetectionStart === 'function') markProviderContextDetectionStart(id);
try {
const res = await sendToBackground('test_provider', { providerId: id });
if (res.ok) {
@@ -4515,6 +4557,8 @@ async function testProvider(id) {
}
} catch (e) {
setProviderTestResult(id, 'fail', t('st.providers.failed', { error: e.message }));
+ } finally {
+ if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id);
}
}
diff --git a/src/firefox/src/ui/settings.js b/src/firefox/src/ui/settings.js
index 0dc7f63a5..cfa48376a 100644
--- a/src/firefox/src/ui/settings.js
+++ b/src/firefox/src/ui/settings.js
@@ -3720,28 +3720,61 @@ function applyProviderBaseUrl(id, baseUrl) {
refreshVisionStatus(id);
}
+const providerContextPendingSnapshots = new Map();
+
+function markProviderContextDetectionStart(id) {
+ const input = document.querySelector(`input[data-provider="${id}"][data-key="contextWindow"]`);
+ if (!input) return;
+ providerContextPendingSnapshots.set(id, {
+ value: input.value,
+ manuallyEdited: input.dataset.manuallyEdited === 'true',
+ });
+}
+
+function clearProviderContextDetectionStart(id) {
+ providerContextPendingSnapshots.delete(id);
+}
+
function applyProviderContextWindow(id, contextWindow) {
const n = Number(contextWindow);
if (!Number.isFinite(n) || n <= 0) return;
- if (providersData[id]) {
- providersData[id].contextWindow = n;
- providersData[id].manualContextWindow = false;
- }
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;
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;
+ providersData[id].manualContextWindow = false;
}
}
async function detectProviderContextWindowForModel(id, model) {
const value = String(model || '').trim();
if (!value) return;
+ if (typeof markProviderContextDetectionStart === 'function') {
+ markProviderContextDetectionStart(id);
+ }
try {
const res = await sendToBackground('detect_provider_context_window', { providerId: id, model: value });
if (res?.ok) applyProviderContextWindow(id, res.contextWindow);
} catch {
// Model picking should still succeed if the backend cannot report a window.
+ } finally {
+ if (typeof clearProviderContextDetectionStart === 'function') {
+ clearProviderContextDetectionStart(id);
+ }
}
}
@@ -3803,18 +3836,26 @@ async function loadProviderModels(id) {
if (!isCurrent()) return;
setProviderLoadModelsStatus(id, t('st.providers.loading'));
+ if (typeof markProviderContextDetectionStart === 'function') markProviderContextDetectionStart(id);
let res;
try {
res = await sendToBackground('list_provider_models', { providerId: id });
} catch (e) {
+ if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id);
if (!isCurrent()) return;
setProviderLoadModelsStatus(id, providerModelLoadErrorMessage(e.message), 'var(--danger, #c33)');
return;
}
- if (!isCurrent()) return;
+ if (!isCurrent()) {
+ if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id);
+ return;
+ }
datalistEl = document.getElementById(`models-${id}`);
- if (!datalistEl) return;
+ if (!datalistEl) {
+ if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id);
+ return;
+ }
if (res?.ok) {
applyProviderBaseUrl(id, res.baseUrl);
applyProviderContextWindow(id, res.contextWindow);
@@ -3934,6 +3975,7 @@ async function testProvider(id) {
if (!setProviderTestResult(id, '', t('st.providers.testing'), 'var(--text2)')) return;
+ if (typeof markProviderContextDetectionStart === 'function') markProviderContextDetectionStart(id);
try {
const res = await sendToBackground('test_provider', { providerId: id });
if (res.ok) {
@@ -3945,6 +3987,8 @@ async function testProvider(id) {
}
} catch (e) {
setProviderTestResult(id, 'fail', t('st.providers.failed', { error: e.message }));
+ } finally {
+ if (typeof clearProviderContextDetectionStart === 'function') clearProviderContextDetectionStart(id);
}
}
diff --git a/test/run.js b/test/run.js
index 46f035dee..7b17f2ab6 100644
--- a/test/run.js
+++ b/test/run.js
@@ -73072,6 +73072,49 @@ test('Local OpenAI-compatible Proxy context window: model card, live props, mode
assert.equal(config().contextWindow, 128000);
}
+ // 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');
+ const start = settingsSource.indexOf('const providerContextPendingSnapshots = new Map();');
+ const end = settingsSource.indexOf('\nasync function detectProviderContextWindowForModel', start);
+ assert.ok(start >= 0 && end > start, `${label}: context snapshot runtime missing`);
+
+ const inputEl = {
+ value: '65536',
+ dataset: { provider: 'local_openai_proxy', key: 'contextWindow', manuallyEdited: 'false' },
+ };
+ const providersData = {
+ local_openai_proxy: { contextWindow: 65536, manualContextWindow: false },
+ };
+ const document = {
+ querySelector: (sel) => (sel.includes('contextWindow') ? inputEl : null),
+ };
+
+ const { markProviderContextDetectionStart, clearProviderContextDetectionStart, applyProviderContextWindow } = Function(
+ 'document', 'providersData',
+ `${settingsSource.slice(start, end)}\nreturn { markProviderContextDetectionStart, clearProviderContextDetectionStart, applyProviderContextWindow };`,
+ )(document, providersData);
+
+ // Case A: Detection starts with default 65536. User edits to 32768 while pending.
+ markProviderContextDetectionStart('local_openai_proxy');
+ inputEl.value = '32768';
+ inputEl.dataset.manuallyEdited = 'true';
+ // Detected window 128000 arrives:
+ applyProviderContextWindow('local_openai_proxy', 128000);
+ assert.equal(inputEl.value, '32768', `${label}: user edit made while pending is not overwritten`);
+ assert.equal(inputEl.dataset.manuallyEdited, 'true');
+ assert.equal(providersData.local_openai_proxy.contextWindow, 65536, `${label}: providersData was not overwritten with detected window`);
+
+ // Case B: Detection starts untouched, user does not edit. Detected window applies.
+ inputEl.value = '65536';
+ inputEl.dataset.manuallyEdited = 'false';
+ markProviderContextDetectionStart('local_openai_proxy');
+ applyProviderContextWindow('local_openai_proxy', 128000);
+ assert.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);
+ }
+
// 6. A manual override is never enlarged by a name or the fallback...
{
const { mgr, config } = setup(async () => new Response('missing', { status: 404 }), {