Skip to content

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

Exclude image payloads from Clef Flash's text budget

Claudeba6aba565021
diff --git a/src/chrome/src/agent/systemone-judge.js b/src/chrome/src/agent/systemone-judge.js
index a25f1fd5f..af8fb63a7 100644
--- a/src/chrome/src/agent/systemone-judge.js
+++ b/src/chrome/src/agent/systemone-judge.js
@@ -151,7 +151,10 @@ export function estimateTextTokens(text) {
function assertStateFitsModel(model, state) {
const budget = SYSTEM_ONE_TEXT_TOKEN_BUDGETS[model];
if (!budget) return;
- if (estimateTextTokens(JSON.stringify(state)) > budget) {
+ // Screenshots ride inside the state as data URLs; the provider counts images
+ // separately, so only the text around them is measured.
+ const text = JSON.stringify(state).replace(/data:[\w.+-]+\/[\w.+-]+;base64,[A-Za-z0-9+/=]*/g, 'data:');
+ if (estimateTextTokens(text) > budget) {
throw systemOneError('JEV_STATE_EXCEEDS_MODEL_INPUT', `The decision state is longer than ${model} reads; the fallback answers instead.`);
}
}
diff --git a/src/firefox/src/agent/systemone-judge.js b/src/firefox/src/agent/systemone-judge.js
index cbb23013d..c670b8b4b 100644
--- a/src/firefox/src/agent/systemone-judge.js
+++ b/src/firefox/src/agent/systemone-judge.js
@@ -151,7 +151,10 @@ export function estimateTextTokens(text) {
function assertStateFitsModel(model, state) {
const budget = SYSTEM_ONE_TEXT_TOKEN_BUDGETS[model];
if (!budget) return;
- if (estimateTextTokens(JSON.stringify(state)) > budget) {
+ // Screenshots ride inside the state as data URLs; the provider counts images
+ // separately, so only the text around them is measured.
+ const text = JSON.stringify(state).replace(/data:[\w.+-]+\/[\w.+-]+;base64,[A-Za-z0-9+/=]*/g, 'data:');
+ if (estimateTextTokens(text) > budget) {
throw systemOneError('JEV_STATE_EXCEEDS_MODEL_INPUT', `The decision state is longer than ${model} reads; the fallback answers instead.`);
}
}
diff --git a/test/systemone.mjs b/test/systemone.mjs
index 75643bf6b..e0bbfefe2 100644
--- a/test/systemone.mjs
+++ b/test/systemone.mjs
@@ -53,8 +53,11 @@ for (const build of ['chrome', 'firefox']) {
error => error.code === 'JEV_STATE_EXCEEDS_MODEL_INPUT' && mod.systemOneFailureReason(error) === 'state_too_long', name);
}
assert.equal(requests.length, 1, 'a state past the budget is not sent');
+ // A screenshot inside the state is an image, not text, so it does not use up the budget.
+ await judge.evaluate({ config: { ...config, supportsVision: true }, state: [{ task: 'Is it done?' }, { type: 'image_url', image_url: { url: 'data:image/png;base64,' + 'iVBORw0KGgo'.repeat(50000) } }], questions: one });
+ assert.equal(requests.length, 2, 'an image payload is excluded from the text budget');
await judge.evaluate({ config: { ...config, model: 'another/publisher' }, state: dense.cjk, questions: one });
- assert.equal(requests.length, 2, 'other models keep the full state');
+ assert.equal(requests.length, 3, 'other models keep the full state');
assert.ok(mod.estimateTextTokens('x'.repeat(100)) >= 50 && mod.estimateTextTokens('æ—¥'.repeat(10)) >= 20);
});
test(`${build}: singleton Choice requests fail locally before a service call`, async () => {