Skip to content

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

fix(agent): restrict per-type attachment counts to generic phrases and fix delimiter character class

Emre Sokullu893badf4ec2b
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 07e41113a..fef86f0ee 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -2566,18 +2566,6 @@ export class Agent extends LoopDetector {
return explicitWords.has(s);
};
- const countPrefix = '(?:\\d+|a|an|one|two|three|four|five|six|seven|eight|nine|ten|single|both|multiple|un|une|deux|trois|quatre|cinq|uno|una|unos|unas|dos|tres|cuatro|cinco|due|tre|quattro|cinque|ein|eine|einen|einer|zwei|drei|vier|fünf|um|uma|dois|duas|três|один|одна|одно|два|две|три|четыре|пять|[一二两三四五六七八九十]|하나|둘|셋|넷|다섯|한(?=\\s*(?:장|개|건|편|개의|장의))|두(?=\\s*(?:장|개|건|편|개의|장의))|세(?=\\s*(?:장|개|건|편|개의|장의))|네(?=\\s*(?:장|개|건|편|개의|장의))|[일이삼사오](?=\\s*(?:장|개|건|편|개의|장의)))';
-
- const imageCountMatch = text.match(new RegExp(`(?:^|[\\s,;and+와과및])(${countPrefix})\\s*(?:images?|photos?|pictures?|pics?|foto|fotos|bild|bilder|imagen(?:es)?|imágenes|画像|写真|图片|照片|圖片|изображение|фото|사진|이미지|포토)(?:\\s*(?:장|개|건))?`, 'i'))
- || text.match(/(?:사진|이미지|포토)\s*([0-9하나둘셋넷다섯]+|[한두세네일이삼사오](?=\\s*(?:장|개|건|편|개의|장의)))\s*(?:장|개|건)?/i);
- const expectedImageCount = imageCountMatch ? parseCountWord(imageCountMatch[1]) : 0;
- const hasExplicitImageCount = imageCountMatch ? isExplicitCountWord(imageCountMatch[1]) : false;
-
- const videoCountMatch = text.match(new RegExp(`(?:^|[\\s,;and+와과및])(${countPrefix})\\s*(?:videos?|clips?|recordings?|gifs?|animated[-_ ]gifs?|vidéo|vidéos|動画|视频|影片|видео|동영상|비디오|영상)(?:\\s*(?:개|편|건))?`, 'i'))
- || text.match(/(?:동영상|비디오|영상)\s*([0-9하나둘셋넷다섯]+|[한두세네일이삼사오](?=\\s*(?:장|개|건|편|개의|장의)))\s*(?:개|편|건)?/i);
- const expectedVideoCount = videoCountMatch ? parseCountWord(videoCountMatch[1]) : 0;
- const hasExplicitVideoCount = videoCountMatch ? isExplicitCountWord(videoCountMatch[1]) : false;
-
let isGeneric = false;
if (CJK_GENERIC_ATTACHMENT_REGEX.test(text)) {
isGeneric = true;
@@ -2587,31 +2575,51 @@ export class Agent extends LoopDetector {
isGeneric = words.length > 0 && words.every(w => /^\d+$/.test(w) || GENERIC_ATTACHMENT_WORDS.has(w) || CJK_GENERIC_ATTACHMENT_REGEX.test(w));
}
+ const countPrefix = '(?:\\d+|a|an|one|two|three|four|five|six|seven|eight|nine|ten|single|both|multiple|un|une|deux|trois|quatre|cinq|uno|una|unos|unas|dos|tres|cuatro|cinco|due|tre|quattro|cinque|ein|eine|einen|einer|zwei|drei|vier|fünf|um|uma|dois|duas|três|один|одна|одно|два|две|три|четыре|пять|[一二两三四五六七八九十]|하나|둘|셋|넷|다섯|한(?=\\s*(?:장|개|건|편|개의|장의))|두(?=\\s*(?:장|개|건|편|개의|장의))|세(?=\\s*(?:장|개|건|편|개의|장의))|네(?=\\s*(?:장|개|건|편|개의|장의))|[일이삼사오](?=\\s*(?:장|개|건|편|개의|장의)))';
+ const countSeparator = '(?:^|[\\s,;+&/|]|(?:\\b(?:and|und|et|e|y)\\b\\s*)|[와과및])';
+
+ const imageCountMatch = isGeneric
+ ? (text.match(new RegExp(`${countSeparator}(${countPrefix})\\s*(?:images?|photos?|pictures?|pics?|foto|fotos|bild|bilder|imagen(?:es)?|imágenes|画像|写真|图片|照片|圖片|изображение|фото|사진|이미지|포토)(?:\\s*(?:장|개|건))?`, 'i'))
+ || text.match(/(?:사진|이미지|포토)\s*([0-9하나둘셋넷다섯]+|[한두세네일이삼사오](?=\\s*(?:장|개|건|편|개의|장의)))\s*(?:장|개|건)?/i))
+ : null;
+ const expectedImageCount = imageCountMatch ? parseCountWord(imageCountMatch[1]) : 0;
+ const hasExplicitImageCount = imageCountMatch ? isExplicitCountWord(imageCountMatch[1]) : false;
+
+ const videoCountMatch = isGeneric
+ ? (text.match(new RegExp(`${countSeparator}(${countPrefix})\\s*(?:videos?|clips?|recordings?|gifs?|animated[-_ ]gifs?|vidéo|vidéos|動画|视频|影片|видео|동영상|비디오|영상)(?:\\s*(?:개|편|건))?`, 'i'))
+ || text.match(/(?:동영상|비디오|영상)\s*([0-9하나둘셋넷다섯]+|[한두세네일이삼사오](?=\\s*(?:장|개|건|편|개의|장의)))\s*(?:개|편|건)?/i))
+ : null;
+ const expectedVideoCount = videoCountMatch ? parseCountWord(videoCountMatch[1]) : 0;
+ const hasExplicitVideoCount = videoCountMatch ? isExplicitCountWord(videoCountMatch[1]) : false;
+
let hasExplicitGenericCount = false;
let expectedCount = 1;
- if (expectedImageCount > 0 && expectedVideoCount > 0) {
- expectedCount = expectedImageCount + expectedVideoCount;
- } else if (expectedImageCount > 0) {
- expectedCount = expectedImageCount + (wantsVideo ? 1 : 0);
- } else if (expectedVideoCount > 0) {
- expectedCount = expectedVideoCount + (wantsImage ? 1 : 0);
- } else {
- const genericNounCountMatch = text.match(new RegExp(`(?:^|[\\s,;and+와과및])(${countPrefix})\\s+(?:attachments?|files?|media|uploads?|pieces?|items?|assets?|enclosures?|documents?|fichiers?|archivos?|dateien?|allegati?|anexos?|вложения?|вложение|첨부(?:파일)?|파일|미디어)`, 'i'))
- || text.match(new RegExp(`(?:^|[\\s,;and+와과및])(${countPrefix})\\s*(?:枚|つ|本|张|條|条|个|個|장|개|건|편)`, 'i'))
- || text.match(new RegExp(`(?:첨부(?:파일)?|파일|미디어)\\s*(${countPrefix})\\s*(?:장|개|건|편)?`, 'i'));
- const generalCountMatch = genericNounCountMatch
- || (isGeneric && (text.match(new RegExp(`\\b(${countPrefix})\\b`, 'i')) || text.match(/([一二两三四五六七八九十])/)));
- if (generalCountMatch) {
- const parsedNum = parseCountWord(generalCountMatch[1]);
- if (parsedNum > 0) {
- expectedCount = parsedNum;
- hasExplicitGenericCount = isExplicitCountWord(generalCountMatch[1]);
+ if (isGeneric) {
+ if (expectedImageCount > 0 && expectedVideoCount > 0) {
+ expectedCount = expectedImageCount + expectedVideoCount;
+ } else if (expectedImageCount > 0) {
+ expectedCount = expectedImageCount + (wantsVideo ? 1 : 0);
+ } else if (expectedVideoCount > 0) {
+ expectedCount = expectedVideoCount + (wantsImage ? 1 : 0);
+ } else {
+ const genericNounCountMatch = text.match(new RegExp(`${countSeparator}(${countPrefix})\\s+(?:attachments?|files?|media|uploads?|pieces?|items?|assets?|enclosures?|documents?|fichiers?|archivos?|dateien?|allegati?|anexos?|вложения?|вложение|첨부(?:파일)?|파일|미디어)`, 'i'))
+ || text.match(new RegExp(`${countSeparator}(${countPrefix})\\s*(?:枚|つ|本|张|條|条|个|個|장|개|건|편)`, 'i'))
+ || text.match(new RegExp(`(?:첨부(?:파일)?|파일|미디어)\\s*(${countPrefix})\\s*(?:장|개|건|편)?`, 'i'));
+ const generalCountMatch = genericNounCountMatch
+ || text.match(new RegExp(`\\b(${countPrefix})\\b`, 'i'))
+ || text.match(/([一二两三四五六七八九十])/);
+ if (generalCountMatch) {
+ const parsedNum = parseCountWord(generalCountMatch[1]);
+ if (parsedNum > 0) {
+ expectedCount = parsedNum;
+ hasExplicitGenericCount = isExplicitCountWord(generalCountMatch[1]);
+ }
}
}
- }
- if (wantsImage && wantsVideo && expectedCount < 2) {
- expectedCount = 2;
+ if (wantsImage && wantsVideo && expectedCount < 2) {
+ expectedCount = 2;
+ }
}
const hasExplicitCardinality = hasExplicitImageCount || hasExplicitVideoCount || hasExplicitGenericCount;
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index ed0d5d7c2..3b40e82f0 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -2280,18 +2280,6 @@ export class Agent extends LoopDetector {
return explicitWords.has(s);
};
- const countPrefix = '(?:\\d+|a|an|one|two|three|four|five|six|seven|eight|nine|ten|single|both|multiple|un|une|deux|trois|quatre|cinq|uno|una|unos|unas|dos|tres|cuatro|cinco|due|tre|quattro|cinque|ein|eine|einen|einer|zwei|drei|vier|fünf|um|uma|dois|duas|três|один|одна|одно|два|две|три|четыре|пять|[一二两三四五六七八九十]|하나|둘|셋|넷|다섯|한(?=\\s*(?:장|개|건|편|개의|장의))|두(?=\\s*(?:장|개|건|편|개의|장의))|세(?=\\s*(?:장|개|건|편|개의|장의))|네(?=\\s*(?:장|개|건|편|개의|장의))|[일이삼사오](?=\\s*(?:장|개|건|편|개의|장의)))';
-
- const imageCountMatch = text.match(new RegExp(`(?:^|[\\s,;and+와과및])(${countPrefix})\\s*(?:images?|photos?|pictures?|pics?|foto|fotos|bild|bilder|imagen(?:es)?|imágenes|画像|写真|图片|照片|圖片|изображение|фото|사진|이미지|포토)(?:\\s*(?:장|개|건))?`, 'i'))
- || text.match(/(?:사진|이미지|포토)\s*([0-9하나둘셋넷다섯]+|[한두세네일이삼사오](?=\\s*(?:장|개|건|편|개의|장의)))\s*(?:장|개|건)?/i);
- const expectedImageCount = imageCountMatch ? parseCountWord(imageCountMatch[1]) : 0;
- const hasExplicitImageCount = imageCountMatch ? isExplicitCountWord(imageCountMatch[1]) : false;
-
- const videoCountMatch = text.match(new RegExp(`(?:^|[\\s,;and+와과및])(${countPrefix})\\s*(?:videos?|clips?|recordings?|gifs?|animated[-_ ]gifs?|vidéo|vidéos|動画|视频|影片|видео|동영상|비디오|영상)(?:\\s*(?:개|편|건))?`, 'i'))
- || text.match(/(?:동영상|비디오|영상)\s*([0-9하나둘셋넷다섯]+|[한두세네일이삼사오](?=\\s*(?:장|개|건|편|개의|장의)))\s*(?:개|편|건)?/i);
- const expectedVideoCount = videoCountMatch ? parseCountWord(videoCountMatch[1]) : 0;
- const hasExplicitVideoCount = videoCountMatch ? isExplicitCountWord(videoCountMatch[1]) : false;
-
let isGeneric = false;
if (CJK_GENERIC_ATTACHMENT_REGEX.test(text)) {
isGeneric = true;
@@ -2301,31 +2289,51 @@ export class Agent extends LoopDetector {
isGeneric = words.length > 0 && words.every(w => /^\d+$/.test(w) || GENERIC_ATTACHMENT_WORDS.has(w) || CJK_GENERIC_ATTACHMENT_REGEX.test(w));
}
+ const countPrefix = '(?:\\d+|a|an|one|two|three|four|five|six|seven|eight|nine|ten|single|both|multiple|un|une|deux|trois|quatre|cinq|uno|una|unos|unas|dos|tres|cuatro|cinco|due|tre|quattro|cinque|ein|eine|einen|einer|zwei|drei|vier|fünf|um|uma|dois|duas|três|один|одна|одно|два|две|три|четыре|пять|[一二两三四五六七八九十]|하나|둘|셋|넷|다섯|한(?=\\s*(?:장|개|건|편|개의|장의))|두(?=\\s*(?:장|개|건|편|개의|장의))|세(?=\\s*(?:장|개|건|편|개의|장의))|네(?=\\s*(?:장|개|건|편|개의|장의))|[일이삼사오](?=\\s*(?:장|개|건|편|개의|장의)))';
+ const countSeparator = '(?:^|[\\s,;+&/|]|(?:\\b(?:and|und|et|e|y)\\b\\s*)|[와과및])';
+
+ const imageCountMatch = isGeneric
+ ? (text.match(new RegExp(`${countSeparator}(${countPrefix})\\s*(?:images?|photos?|pictures?|pics?|foto|fotos|bild|bilder|imagen(?:es)?|imágenes|画像|写真|图片|照片|圖片|изображение|фото|사진|이미지|포토)(?:\\s*(?:장|개|건))?`, 'i'))
+ || text.match(/(?:사진|이미지|포토)\s*([0-9하나둘셋넷다섯]+|[한두세네일이삼사오](?=\\s*(?:장|개|건|편|개의|장의)))\s*(?:장|개|건)?/i))
+ : null;
+ const expectedImageCount = imageCountMatch ? parseCountWord(imageCountMatch[1]) : 0;
+ const hasExplicitImageCount = imageCountMatch ? isExplicitCountWord(imageCountMatch[1]) : false;
+
+ const videoCountMatch = isGeneric
+ ? (text.match(new RegExp(`${countSeparator}(${countPrefix})\\s*(?:videos?|clips?|recordings?|gifs?|animated[-_ ]gifs?|vidéo|vidéos|動画|视频|影片|видео|동영상|비디오|영상)(?:\\s*(?:개|편|건))?`, 'i'))
+ || text.match(/(?:동영상|비디오|영상)\s*([0-9하나둘셋넷다섯]+|[한두세네일이삼사오](?=\\s*(?:장|개|건|편|개의|장의)))\s*(?:개|편|건)?/i))
+ : null;
+ const expectedVideoCount = videoCountMatch ? parseCountWord(videoCountMatch[1]) : 0;
+ const hasExplicitVideoCount = videoCountMatch ? isExplicitCountWord(videoCountMatch[1]) : false;
+
let hasExplicitGenericCount = false;
let expectedCount = 1;
- if (expectedImageCount > 0 && expectedVideoCount > 0) {
- expectedCount = expectedImageCount + expectedVideoCount;
- } else if (expectedImageCount > 0) {
- expectedCount = expectedImageCount + (wantsVideo ? 1 : 0);
- } else if (expectedVideoCount > 0) {
- expectedCount = expectedVideoCount + (wantsImage ? 1 : 0);
- } else {
- const genericNounCountMatch = text.match(new RegExp(`(?:^|[\\s,;and+와과및])(${countPrefix})\\s+(?:attachments?|files?|media|uploads?|pieces?|items?|assets?|enclosures?|documents?|fichiers?|archivos?|dateien?|allegati?|anexos?|вложения?|вложение|첨부(?:파일)?|파일|미디어)`, 'i'))
- || text.match(new RegExp(`(?:^|[\\s,;and+와과및])(${countPrefix})\\s*(?:枚|つ|本|张|條|条|个|個|장|개|건|편)`, 'i'))
- || text.match(new RegExp(`(?:첨부(?:파일)?|파일|미디어)\\s*(${countPrefix})\\s*(?:장|개|건|편)?`, 'i'));
- const generalCountMatch = genericNounCountMatch
- || (isGeneric && (text.match(new RegExp(`\\b(${countPrefix})\\b`, 'i')) || text.match(/([一二两三四五六七八九十])/)));
- if (generalCountMatch) {
- const parsedNum = parseCountWord(generalCountMatch[1]);
- if (parsedNum > 0) {
- expectedCount = parsedNum;
- hasExplicitGenericCount = isExplicitCountWord(generalCountMatch[1]);
+ if (isGeneric) {
+ if (expectedImageCount > 0 && expectedVideoCount > 0) {
+ expectedCount = expectedImageCount + expectedVideoCount;
+ } else if (expectedImageCount > 0) {
+ expectedCount = expectedImageCount + (wantsVideo ? 1 : 0);
+ } else if (expectedVideoCount > 0) {
+ expectedCount = expectedVideoCount + (wantsImage ? 1 : 0);
+ } else {
+ const genericNounCountMatch = text.match(new RegExp(`${countSeparator}(${countPrefix})\\s+(?:attachments?|files?|media|uploads?|pieces?|items?|assets?|enclosures?|documents?|fichiers?|archivos?|dateien?|allegati?|anexos?|вложения?|вложение|첨부(?:파일)?|파일|미디어)`, 'i'))
+ || text.match(new RegExp(`${countSeparator}(${countPrefix})\\s*(?:枚|つ|本|张|條|条|个|個|장|개|건|편)`, 'i'))
+ || text.match(new RegExp(`(?:첨부(?:파일)?|파일|미디어)\\s*(${countPrefix})\\s*(?:장|개|건|편)?`, 'i'));
+ const generalCountMatch = genericNounCountMatch
+ || text.match(new RegExp(`\\b(${countPrefix})\\b`, 'i'))
+ || text.match(/([一二两三四五六七八九十])/);
+ if (generalCountMatch) {
+ const parsedNum = parseCountWord(generalCountMatch[1]);
+ if (parsedNum > 0) {
+ expectedCount = parsedNum;
+ hasExplicitGenericCount = isExplicitCountWord(generalCountMatch[1]);
+ }
}
}
- }
- if (wantsImage && wantsVideo && expectedCount < 2) {
- expectedCount = 2;
+ if (wantsImage && wantsVideo && expectedCount < 2) {
+ expectedCount = 2;
+ }
}
const hasExplicitCardinality = hasExplicitImageCount || hasExplicitVideoCount || hasExplicitGenericCount;
diff --git a/test/run.js b/test/run.js
index 845e29ea4..64d666d0a 100644
--- a/test/run.js
+++ b/test/run.js
@@ -87613,6 +87613,41 @@ test('attachment requirement parser ignores numbers in specific attachment filen
const req4 = agent._parseWorkflowAttachmentRequirement('2枚');
assert.equal(req4.expectedCount, 2, AgentClass.name + ': 2枚 should expect 2 attachments');
assert.equal(req4.isGeneric, true, AgentClass.name + ': 2枚 should be generic');
+
+ const reqBrand = agent._parseWorkflowAttachmentRequirement('brand2images.png');
+ assert.equal(reqBrand.expectedCount, 1, AgentClass.name + ': brand2images.png should expect 1 attachment');
+ assert.equal(reqBrand.expectedImageCount, 0, AgentClass.name + ': brand2images.png should have 0 expectedImageCount');
+ assert.equal(reqBrand.hasExplicitImageCount, false, AgentClass.name + ': brand2images.png should not have explicit image count');
+ assert.equal(reqBrand.hasExplicitCardinality, false, AgentClass.name + ': brand2images.png should not have explicit cardinality');
+ assert.equal(reqBrand.isGeneric, false, AgentClass.name + ': brand2images.png should not be generic');
+ assert.equal(reqBrand.wantsImage, true, AgentClass.name + ': brand2images.png should want image');
+ assert.equal(reqBrand.wantsVideo, false, AgentClass.name + ': brand2images.png should not want video');
+
+ const brandVerified = agent._workflowSocialPublishedAttachmentObserved(
+ { value: 'brand2images.png' },
+ { attachments: [{ type: 'image', src: 'https://pbs.twimg.com/media/brand2images.png' }] },
+ );
+ assert.equal(brandVerified, true, AgentClass.name + ': single matching brand2images.png should pass');
+
+ const brandWrongFile = agent._workflowSocialPublishedAttachmentObserved(
+ { value: 'brand2images.png' },
+ { attachments: [{ type: 'image', src: 'https://pbs.twimg.com/media/other.png' }] },
+ );
+ assert.equal(brandWrongFile, false, AgentClass.name + ': non-matching file for brand2images.png should fail');
+
+ const reqBrandVid = agent._parseWorkflowAttachmentRequirement('brand2videos.mp4');
+ assert.equal(reqBrandVid.expectedCount, 1, AgentClass.name + ': brand2videos.mp4 should expect 1 attachment');
+ assert.equal(reqBrandVid.expectedVideoCount, 0, AgentClass.name + ': brand2videos.mp4 should have 0 expectedVideoCount');
+ assert.equal(reqBrandVid.hasExplicitVideoCount, false, AgentClass.name + ': brand2videos.mp4 should not have explicit video count');
+ assert.equal(reqBrandVid.isGeneric, false, AgentClass.name + ': brand2videos.mp4 should not be generic');
+ assert.equal(reqBrandVid.wantsVideo, true, AgentClass.name + ': brand2videos.mp4 should want video');
+ assert.equal(reqBrandVid.wantsImage, false, AgentClass.name + ': brand2videos.mp4 should not want image');
+
+ const brandVidVerified = agent._workflowSocialPublishedAttachmentObserved(
+ { value: 'brand2videos.mp4' },
+ { attachments: [{ type: 'video', src: 'https://video.twimg.com/media/brand2videos.mp4' }] },
+ );
+ assert.equal(brandVidVerified, true, AgentClass.name + ': single matching brand2videos.mp4 should pass');
}
});