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
Use a plan translation only when it is in the requested language
Emre Sokullu42afcb427414
diff --git a/src/chrome/src/agent/planner.js b/src/chrome/src/agent/planner.jsindex ca543699d..94286872e 100644--- a/src/chrome/src/agent/planner.js+++ b/src/chrome/src/agent/planner.js@@ -543,6 +543,11 @@ export function normalizePlannerLocale(value) {return normalizedLocaleOrEmpty(value) || 'en';}+// The language of a locale tag ("tr" for "tr-TR"), or '' when it has none.+function localeLanguage(value) {+ return normalizedLocaleOrEmpty(value).split('-')[0];+}+export function fallbackResponseLanguagePolicy(locale = 'en') {return {framing_locale: normalizePlannerLocale(locale),@@ -1192,8 +1197,10 @@ export function normalizePlan(obj, opts = {}) {: [];const requestedLocale = normalizePlannerLocale(opts.locale || localizedInput.locale);const englishLocale = requestedLocale === 'en' || requestedLocale.startsWith('en-');- // The copy covers the summary, every step and every risk.+ // The copy is in the requested language and covers the summary, every step+ // and every risk.const localizedCopyComplete = Boolean(localizedSummary)+ && (!localizedInput.locale || localeLanguage(localizedInput.locale) === localeLanguage(requestedLocale))&& steps.every(step => localizedStepsById.get(step.id)?.action)&& riskEntries.every(entry => sanitizeText(providedLocalizedRisks[entry.sourceIndex], 200));if (opts.requireIntent) {@@ -1494,12 +1501,15 @@ export function buildPlanLocalizationMessages(plan) {}// The plan with the translation in `content` as its localized copy, or null-// unless `content` translates the summary, every step (matched by id) and-// every risk: the user reads the plan in one language, never a mix.+// unless `content` is in the requested language and translates the summary,+// every step (matched by id) and every risk: the user reads the plan in one+// language, never a mix.export function localizePlanFromContent(plan, content) {const obj = extractFirstJsonObject(content);const summary = sanitizeText(obj?.summary, 400);if (!plan || !summary) return null;+ const language = localeLanguage(obj.locale);+ if (!language || language !== localeLanguage(plan.localized?.locale)) return null;const translatedSteps = new Map((Array.isArray(obj.steps) ? obj.steps : []).map(step => [sanitizeText(step?.id, 20), sanitizeText(step?.action, 300)]));const translatedRisks = Array.isArray(obj.risks) ? obj.risks : [];diff --git a/src/firefox/src/agent/planner.js b/src/firefox/src/agent/planner.jsindex ca543699d..94286872e 100644--- a/src/firefox/src/agent/planner.js+++ b/src/firefox/src/agent/planner.js@@ -543,6 +543,11 @@ export function normalizePlannerLocale(value) {return normalizedLocaleOrEmpty(value) || 'en';}+// The language of a locale tag ("tr" for "tr-TR"), or '' when it has none.+function localeLanguage(value) {+ return normalizedLocaleOrEmpty(value).split('-')[0];+}+export function fallbackResponseLanguagePolicy(locale = 'en') {return {framing_locale: normalizePlannerLocale(locale),@@ -1192,8 +1197,10 @@ export function normalizePlan(obj, opts = {}) {: [];const requestedLocale = normalizePlannerLocale(opts.locale || localizedInput.locale);const englishLocale = requestedLocale === 'en' || requestedLocale.startsWith('en-');- // The copy covers the summary, every step and every risk.+ // The copy is in the requested language and covers the summary, every step+ // and every risk.const localizedCopyComplete = Boolean(localizedSummary)+ && (!localizedInput.locale || localeLanguage(localizedInput.locale) === localeLanguage(requestedLocale))&& steps.every(step => localizedStepsById.get(step.id)?.action)&& riskEntries.every(entry => sanitizeText(providedLocalizedRisks[entry.sourceIndex], 200));if (opts.requireIntent) {@@ -1494,12 +1501,15 @@ export function buildPlanLocalizationMessages(plan) {}// The plan with the translation in `content` as its localized copy, or null-// unless `content` translates the summary, every step (matched by id) and-// every risk: the user reads the plan in one language, never a mix.+// unless `content` is in the requested language and translates the summary,+// every step (matched by id) and every risk: the user reads the plan in one+// language, never a mix.export function localizePlanFromContent(plan, content) {const obj = extractFirstJsonObject(content);const summary = sanitizeText(obj?.summary, 400);if (!plan || !summary) return null;+ const language = localeLanguage(obj.locale);+ if (!language || language !== localeLanguage(plan.localized?.locale)) return null;const translatedSteps = new Map((Array.isArray(obj.steps) ? obj.steps : []).map(step => [sanitizeText(step?.id, 20), sanitizeText(step?.action, 300)]));const translatedRisks = Array.isArray(obj.risks) ? obj.risks : [];diff --git a/test/plan-when-needed.mjs b/test/plan-when-needed.mjsindex c004ba2d0..0345abb8c 100644--- a/test/plan-when-needed.mjs+++ b/test/plan-when-needed.mjs@@ -309,6 +309,9 @@ for (const build of BUILDS) {assert.equal(parse(englishPlan({ localized: { ...copy, steps: copy.steps.slice(0, 1) } }), { locale: 'tr' }).localized.missing, true);assert.equal(parse(englishPlan({ risks: ['The form is submitted.'], localized: copy }), { locale: 'tr' }).localized.missing, true);assert.equal('missing' in parse(englishPlan({ risks: ['The form is submitted.'], localized: { ...copy, risks: ['Form gönderilir.'] } }), { locale: 'tr' }).localized, false);+ // A copy in another language is no copy in the user's.+ assert.equal(parse(englishPlan({ localized: { ...copy, locale: 'en' } }), { locale: 'tr' }).localized.missing, true);+ assert.equal('missing' in parse(englishPlan({ localized: { ...copy, locale: 'tr-TR' } }), { locale: 'tr' }).localized, false);});test(`${build}: the translation request carries only the display fields, and its answer replaces them`, () => {@@ -325,9 +328,11 @@ for (const build of BUILDS) {steps: [{ id: '1', action: 'E-posta alanını doldur.' }, { id: '2', action: 'Gönder ve onayı oku.' }], risks: ['Form gönderilir.'] });assert.deepEqual([translated.summary, translated.steps, translated.risks], [plan.summary, plan.steps, plan.risks], 'execution keeps the English plan');// An incomplete translation is not used: the plan is read in one language.- const complete = { summary: 'Adresi abone et.', steps: [{ id: '1', action: 'Doldur.' }, { id: '2', action: 'Gönder.' }], risks: ['Form gönderilir.'] };- assert.ok(planner.localizePlanFromContent(plan, JSON.stringify(complete)));+ const complete = { locale: 'tr-TR', summary: 'Adresi abone et.', steps: [{ id: '1', action: 'Doldur.' }, { id: '2', action: 'Gönder.' }], risks: ['Form gönderilir.'] };+ assert.ok(planner.localizePlanFromContent(plan, JSON.stringify(complete)), 'a regional tag of the same language counts');for (const incomplete of [+ { ...complete, locale: 'en' },+ { ...complete, locale: undefined },{ ...complete, steps: [{ id: '1', action: 'Doldur.' }] },{ ...complete, steps: [{ id: '1', action: 'Doldur.' }, { id: '3', action: 'Gönder.' }] },{ ...complete, steps: [{ id: '1', action: 'Doldur.' }, { id: '2', action: ' ' }] },