Skip to content

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

fix(agent): pair quotation delimiters and support nested quotation styles in _maskQuotedPayload

Emre Sokullu4e6a4de2709d
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 2ebac1dad..b1b8f574e 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -15174,14 +15174,72 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
_maskQuotedPayload(text) {
if (!text) return '';
- let str = String(text);
- str = str.replace(/(["“「『«])([\s\S]*?)(["”」』»])/g, (_match, open, content, close) => {
- return open + ' '.repeat(content.length) + close;
- });
- str = str.replace(/(?<!\p{L})'([^']+)'(?!\p{L})/gu, (_match, content) => {
+ const chars = String(text).split('');
+ const pairs = {
+ '“': '”',
+ '「': '」',
+ '『': '』',
+ '«': '»',
+ };
+ let i = 0;
+ while (i < chars.length) {
+ const ch = chars[i];
+ if (pairs[ch]) {
+ const openChar = ch;
+ const closeChar = pairs[ch];
+ const start = i;
+ let depth = 1;
+ i++;
+ while (i < chars.length && depth > 0) {
+ if (chars[i] === '\\' && i + 1 < chars.length) {
+ i += 2;
+ continue;
+ }
+ if (chars[i] === openChar) depth++;
+ else if (chars[i] === closeChar) depth--;
+ i++;
+ }
+ if (depth === 0) {
+ for (let j = start + 1; j < i - 1; j++) {
+ chars[j] = ' ';
+ }
+ } else {
+ i = start + 1;
+ }
+ continue;
+ }
+ if (ch === '"') {
+ const start = i;
+ i++;
+ let closed = false;
+ while (i < chars.length) {
+ if (chars[i] === '\\' && i + 1 < chars.length) {
+ i += 2;
+ continue;
+ }
+ if (chars[i] === '"') {
+ closed = true;
+ i++;
+ break;
+ }
+ i++;
+ }
+ if (closed) {
+ for (let j = start + 1; j < i - 1; j++) {
+ chars[j] = ' ';
+ }
+ } else {
+ i = start + 1;
+ }
+ continue;
+ }
+ i++;
+ }
+ let res = chars.join('');
+ res = res.replace(/(?<!\p{L})'([^']+)'(?!\p{L})/gu, (_match, content) => {
return "'" + ' '.repeat(content.length) + "'";
});
- return str;
+ return res;
}
_socialPublicationClauses(text) {
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 298485142..1fae578e0 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -12974,14 +12974,72 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
_maskQuotedPayload(text) {
if (!text) return '';
- let str = String(text);
- str = str.replace(/(["“「『«])([\s\S]*?)(["”」』»])/g, (_match, open, content, close) => {
- return open + ' '.repeat(content.length) + close;
- });
- str = str.replace(/(?<!\p{L})'([^']+)'(?!\p{L})/gu, (_match, content) => {
+ const chars = String(text).split('');
+ const pairs = {
+ '“': '”',
+ '「': '」',
+ '『': '』',
+ '«': '»',
+ };
+ let i = 0;
+ while (i < chars.length) {
+ const ch = chars[i];
+ if (pairs[ch]) {
+ const openChar = ch;
+ const closeChar = pairs[ch];
+ const start = i;
+ let depth = 1;
+ i++;
+ while (i < chars.length && depth > 0) {
+ if (chars[i] === '\\' && i + 1 < chars.length) {
+ i += 2;
+ continue;
+ }
+ if (chars[i] === openChar) depth++;
+ else if (chars[i] === closeChar) depth--;
+ i++;
+ }
+ if (depth === 0) {
+ for (let j = start + 1; j < i - 1; j++) {
+ chars[j] = ' ';
+ }
+ } else {
+ i = start + 1;
+ }
+ continue;
+ }
+ if (ch === '"') {
+ const start = i;
+ i++;
+ let closed = false;
+ while (i < chars.length) {
+ if (chars[i] === '\\' && i + 1 < chars.length) {
+ i += 2;
+ continue;
+ }
+ if (chars[i] === '"') {
+ closed = true;
+ i++;
+ break;
+ }
+ i++;
+ }
+ if (closed) {
+ for (let j = start + 1; j < i - 1; j++) {
+ chars[j] = ' ';
+ }
+ } else {
+ i = start + 1;
+ }
+ continue;
+ }
+ i++;
+ }
+ let res = chars.join('');
+ res = res.replace(/(?<!\p{L})'([^']+)'(?!\p{L})/gu, (_match, content) => {
return "'" + ' '.repeat(content.length) + "'";
});
- return str;
+ return res;
}
_socialPublicationClauses(text) {
diff --git a/test/run.js b/test/run.js
index 290fe45a3..e9396b277 100644
--- a/test/run.js
+++ b/test/run.js
@@ -86396,6 +86396,14 @@ test('social publication workflow follows the live X or Bluesky destination and
'Portuguese na Bluesky destination preposition was not recognized'],
['Não publique no X: olá', [],
'negated Portuguese no X command was wrongly treated as a publish destination'],
+ ['Post “He said \\"hello; world\\" today” on X', ['twitter'],
+ 'nested quotation styles mispaired opening curly quote with inner ASCII quote'],
+ ['Post “He said "hello; world" today” on X', ['twitter'],
+ 'nested quotation styles mispaired opening curly quote with inner ASCII quote'],
+ ['Post «He said "hello; world" today» to https://bsky.app/', ['bluesky'],
+ 'guillemet quotation containing inner ASCII quote mispaired delimiters'],
+ ['Post 「He said 『hello; world』 today」 on X', ['twitter'],
+ 'nested CJK quotation marks mispaired delimiters'],
]) {
assert.deepEqual(
[...agent._trustedSocialPublishTargetAdapters({ taskText })],