Skip to content

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

fix(social-publish): enforce typed media branches

Barack Sokullue6c30974b9bf
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 24b1ab9c3..5db89779e 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -3095,12 +3095,11 @@ export class Agent extends LoopDetector {
const hasRawGif = RAW_GIF_NOUN_REGEX.test(text);
const hasRawOrdinaryVideo = RAW_VIDEO_NOUN_REGEX.test(text);
- const hasRawVideo = hasRawGif || hasRawOrdinaryVideo;
const hasRawImage = RAW_IMAGE_NOUN_REGEX.test(text);
const wantsGif = hasRawGif && !isGifNegated;
const wantsOrdinaryVideo = hasRawOrdinaryVideo && !isVideoNegated;
- const wantsVideo = (hasRawVideo && !isVideoNegated) || wantsGif;
+ const wantsVideo = wantsOrdinaryVideo || wantsGif;
const wantsImage = hasRawImage && !isImageNegated;
const parseCountWord = (str) => {
@@ -3360,8 +3359,18 @@ export class Agent extends LoopDetector {
.map(branch => branch.trim())
.filter(Boolean)
: [];
- const hasEnumeratedMediaChoice = oneOfMediaBranches.length > 1;
- const mediaAlternativeBranchTexts = hasEnumeratedMediaChoice ? oneOfMediaBranches : [];
+ const commaMediaDisjunction = new RegExp(`[,;]\\s*${COUNT_DISJUNCTION_PATTERN}\\s+`, 'iu');
+ const leadingMediaDisjunction = new RegExp(`^${COUNT_DISJUNCTION_PATTERN}\\s*`, 'iu');
+ const commaMediaBranches = isGeneric && commaMediaDisjunction.test(text)
+ ? text.replace(/^either\s+/iu, '').split(/\s*[,;]\s*/u)
+ .map(branch => branch.replace(leadingMediaDisjunction, '').trim())
+ .filter(Boolean)
+ : [];
+ const enumeratedMediaBranches = oneOfMediaBranches.length > 1
+ ? oneOfMediaBranches
+ : commaMediaBranches;
+ const hasEnumeratedMediaChoice = enumeratedMediaBranches.length > 1;
+ const mediaAlternativeBranchTexts = hasEnumeratedMediaChoice ? enumeratedMediaBranches : [];
if (!hasEnumeratedMediaChoice && hasUnscopedDisjunction) {
let branchStart = 0;
for (const match of unscopedDisjunctionMatches) {
@@ -3853,7 +3862,9 @@ export class Agent extends LoopDetector {
const typedVideoAlternativeCounts = parsed.wantsGif && !parsed.wantsOrdinaryVideo
? gifAlternativeCounts
: videoAlternativeCounts;
- const typedVideoCount = parsed.wantsGif && !parsed.wantsOrdinaryVideo ? gifCount : videoCount;
+ const typedVideoCount = parsed.wantsGif && !parsed.wantsOrdinaryVideo ? gifCount
+ : ordinaryVideoOnly ? ordinaryVideoCount
+ : videoCount;
const minImages = imageAlternativeCounts.length
? Math.min(...imageAlternativeCounts)
: (parsed.isImageMaximum ? 0 : (parsed.expectedImageCount > 0 ? parsed.expectedImageCount : 1));
@@ -3877,10 +3888,10 @@ export class Agent extends LoopDetector {
if (typedVideoAlternativeCounts.length && !typedVideoAlternativeCounts.includes(typedVideoCount)) {
return false;
}
- if (!typedVideoAlternativeCounts.length && parsed.hasExplicitVideoCount && !parsed.isVideoMinimum && !parsed.isVideoMaximum && videoCount !== parsed.expectedVideoCount) {
+ if (!typedVideoAlternativeCounts.length && parsed.hasExplicitVideoCount && !parsed.isVideoMinimum && !parsed.isVideoMaximum && typedVideoCount !== parsed.expectedVideoCount) {
return false;
}
- if (parsed.hasExplicitVideoCount && parsed.isVideoMaximum && videoCount > parsed.expectedVideoCount) {
+ if (parsed.hasExplicitVideoCount && parsed.isVideoMaximum && typedVideoCount > parsed.expectedVideoCount) {
return false;
}
if (parsed.hasExplicitImageCount && parsed.hasExplicitVideoCount
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 4e7237c5c..9eb3fdc4c 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -2810,12 +2810,11 @@ export class Agent extends LoopDetector {
const hasRawGif = RAW_GIF_NOUN_REGEX.test(text);
const hasRawOrdinaryVideo = RAW_VIDEO_NOUN_REGEX.test(text);
- const hasRawVideo = hasRawGif || hasRawOrdinaryVideo;
const hasRawImage = RAW_IMAGE_NOUN_REGEX.test(text);
const wantsGif = hasRawGif && !isGifNegated;
const wantsOrdinaryVideo = hasRawOrdinaryVideo && !isVideoNegated;
- const wantsVideo = (hasRawVideo && !isVideoNegated) || wantsGif;
+ const wantsVideo = wantsOrdinaryVideo || wantsGif;
const wantsImage = hasRawImage && !isImageNegated;
const parseCountWord = (str) => {
@@ -3075,8 +3074,18 @@ export class Agent extends LoopDetector {
.map(branch => branch.trim())
.filter(Boolean)
: [];
- const hasEnumeratedMediaChoice = oneOfMediaBranches.length > 1;
- const mediaAlternativeBranchTexts = hasEnumeratedMediaChoice ? oneOfMediaBranches : [];
+ const commaMediaDisjunction = new RegExp(`[,;]\\s*${COUNT_DISJUNCTION_PATTERN}\\s+`, 'iu');
+ const leadingMediaDisjunction = new RegExp(`^${COUNT_DISJUNCTION_PATTERN}\\s*`, 'iu');
+ const commaMediaBranches = isGeneric && commaMediaDisjunction.test(text)
+ ? text.replace(/^either\s+/iu, '').split(/\s*[,;]\s*/u)
+ .map(branch => branch.replace(leadingMediaDisjunction, '').trim())
+ .filter(Boolean)
+ : [];
+ const enumeratedMediaBranches = oneOfMediaBranches.length > 1
+ ? oneOfMediaBranches
+ : commaMediaBranches;
+ const hasEnumeratedMediaChoice = enumeratedMediaBranches.length > 1;
+ const mediaAlternativeBranchTexts = hasEnumeratedMediaChoice ? enumeratedMediaBranches : [];
if (!hasEnumeratedMediaChoice && hasUnscopedDisjunction) {
let branchStart = 0;
for (const match of unscopedDisjunctionMatches) {
@@ -3568,7 +3577,9 @@ export class Agent extends LoopDetector {
const typedVideoAlternativeCounts = parsed.wantsGif && !parsed.wantsOrdinaryVideo
? gifAlternativeCounts
: videoAlternativeCounts;
- const typedVideoCount = parsed.wantsGif && !parsed.wantsOrdinaryVideo ? gifCount : videoCount;
+ const typedVideoCount = parsed.wantsGif && !parsed.wantsOrdinaryVideo ? gifCount
+ : ordinaryVideoOnly ? ordinaryVideoCount
+ : videoCount;
const minImages = imageAlternativeCounts.length
? Math.min(...imageAlternativeCounts)
: (parsed.isImageMaximum ? 0 : (parsed.expectedImageCount > 0 ? parsed.expectedImageCount : 1));
@@ -3592,10 +3603,10 @@ export class Agent extends LoopDetector {
if (typedVideoAlternativeCounts.length && !typedVideoAlternativeCounts.includes(typedVideoCount)) {
return false;
}
- if (!typedVideoAlternativeCounts.length && parsed.hasExplicitVideoCount && !parsed.isVideoMinimum && !parsed.isVideoMaximum && videoCount !== parsed.expectedVideoCount) {
+ if (!typedVideoAlternativeCounts.length && parsed.hasExplicitVideoCount && !parsed.isVideoMinimum && !parsed.isVideoMaximum && typedVideoCount !== parsed.expectedVideoCount) {
return false;
}
- if (parsed.hasExplicitVideoCount && parsed.isVideoMaximum && videoCount > parsed.expectedVideoCount) {
+ if (parsed.hasExplicitVideoCount && parsed.isVideoMaximum && typedVideoCount > parsed.expectedVideoCount) {
return false;
}
if (parsed.hasExplicitImageCount && parsed.hasExplicitVideoCount
diff --git a/test/run.js b/test/run.js
index b302fa020..15c28bfaf 100644
--- a/test/run.js
+++ b/test/run.js
@@ -92929,6 +92929,15 @@ test('upper-bound attachment qualifiers verify as maximum counts', () => {
assert.equal(agent._workflowSocialPublishedAttachmentObserved(
{ value: 'up to two videos' }, { attachments: [] }), true,
AgentClass.name + ': zero videos should satisfy a standalone video maximum');
+ const imageWithoutGifs = agent._parseWorkflowAttachmentRequirement('one image and no GIFs');
+ assert.equal(imageWithoutGifs.wantsVideo, false,
+ AgentClass.name + ': a negated GIF mention created positive video intent');
+ assert.equal(agent._workflowSocialPublishedAttachmentObserved(
+ { value: 'one image and no GIFs' }, { attachments: [image(1)] }), true,
+ AgentClass.name + ': one image did not satisfy a no-GIF requirement');
+ assert.equal(agent._workflowSocialPublishedAttachmentObserved(
+ { value: 'one image and no GIFs' }, { attachments: [image(1), gif(1)] }), false,
+ AgentClass.name + ': a GIF satisfied a no-GIF requirement');
const boundedImages = agent._parseWorkflowAttachmentRequirement('between one and two images');
assert.equal(boundedImages.isGeneric, true,
@@ -94173,6 +94182,29 @@ test('attachment verification matches specific attachment names without substrin
[alternativeImage(1), alternativeImage(2)],
],
},
+ {
+ requirement: 'either one image and one video or one GIF',
+ valid: [[alternativeImage(1), alternativeVideo(1)], [alternativeGif(1)]],
+ invalid: [
+ [alternativeImage(1), alternativeGif(1)],
+ [alternativeImage(1)],
+ [alternativeVideo(1)],
+ ],
+ },
+ {
+ requirement: 'either one image, two videos, or three GIFs',
+ branchCount: 3,
+ valid: [
+ [alternativeImage(1)],
+ [alternativeVideo(1), alternativeVideo(2)],
+ [alternativeGif(1), alternativeGif(2), alternativeGif(3)],
+ ],
+ invalid: [
+ [alternativeGif(1)],
+ [alternativeImage(1), alternativeVideo(1), alternativeVideo(2)],
+ [alternativeVideo(1), alternativeVideo(2), alternativeVideo(3)],
+ ],
+ },
]) {
const parsedTypedAlternative = agent._parseWorkflowAttachmentRequirement({ value: requirement });
assert.equal(parsedTypedAlternative.isGeneric, true,