Skip to content

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

fix(agent): keep authored post permalinks and recognize Korean generic attachments

Emre Sokullu8615be6f917f
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index f7d4c311d..e7524da60 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -289,9 +289,18 @@ const GENERIC_ATTACHMENT_WORDS = new Set([
'один', 'одна', 'одно', 'два', 'две', 'три', 'четыре', 'пять',
'вложения', 'вложение', 'фотографии', 'фотография', 'изображения', 'изображение', 'видео',
'и', 'или', 'плюс',
+ '사진', '이미지', '포토',
+ '동영상', '비디오', '영상', '클립',
+ '움짤',
+ '첨부', '첨부파일', '미디어', '파일',
+ '하나', '둘', '셋', '넷', '다섯',
+ '한', '두', '세', '네',
+ '일', '이', '삼', '사', '오',
+ '장', '개', '건', '편',
+ '와', '과', '및', '그리고', '하고', '도',
]);
-const CJK_GENERIC_ATTACHMENT_REGEX = /^[0-9一二两三四五六七八九十添付画像写真動画メディア已上传附件图片照片视频媒体枚つの本张條条个個장での\s\-_,.:;!?/\\()]+$/u;
+const CJK_GENERIC_ATTACHMENT_REGEX = /^[0-9一二两三四五六七八九十添付画像写真動画メディア已上传附件图片照片视频媒体枚つの本张條条个個장에서의사진이미지포토동영상비디오영상클립움짤첨부파일미디어하나둘셋넷다섯한두세네일이삼사오개건편와과및그리고하고도\s\-_,.:;!?/\\()&+/]+$/u;
// "On <url>, publish this" names a destination just as plainly as
// "publish this on <url>", but only when the URL is presented as a place.
@@ -2346,9 +2355,9 @@ export class Agent extends LoopDetector {
const text = String(value || '').trim().toLowerCase();
if (!text) return { isGeneric: true, expectedCount: 1, expectedImageCount: 0, expectedVideoCount: 0, wantsImage: false, wantsVideo: false, wantsGif: false, normalized: '' };
- const wantsGif = /\b(?:gif|gifs)\b|\.gif(?:[?#]|$)|(?:animated[-_ ]gif|動圖|动图)/i.test(text);
- const wantsVideo = wantsGif || /\b(?:video|videos|mp4|mov|webm|mkv|clip|clips|recording|recordings|vidéo|vidéos)\b|(?:動画|视频|影片|видео)/i.test(text);
- const wantsImage = /\b(?:image|images|photo|photos|picture|pictures|pic|pics|png|jpg|jpeg|webp|foto|fotos|bild|bilder|imagen(?:es)?|imágenes)\b|(?:画像|写真|图片|照片|圖片|изображение|фото)/i.test(text);
+ const wantsGif = /\b(?:gif|gifs)\b|\.gif(?:[?#]|$)|(?:animated[-_ ]gif|動圖|动图|움짤)/i.test(text);
+ const wantsVideo = wantsGif || /\b(?:video|videos|mp4|mov|webm|mkv|clip|clips|recording|recordings|vidéo|vidéos)\b|(?:動画|视频|影片|видео|동영상|비디오|영상)/i.test(text);
+ const wantsImage = /\b(?:image|images|photo|photos|picture|pictures|pic|pics|png|jpg|jpeg|webp|foto|fotos|bild|bilder|imagen(?:es)?|imágenes)\b|(?:画像|写真|图片|照片|圖片|изображение|фото|사진|이미지|포토)/i.test(text);
const parseCountWord = (str) => {
if (!str) return 0;
@@ -2357,22 +2366,29 @@ export class Agent extends LoopDetector {
const wordToNum = {
a: 1, an: 1, one: 1, single: 1, un: 1, une: 1, uno: 1, una: 1, ein: 1, eine: 1, einen: 1, einer: 1, um: 1, uma: 1,
'一': 1, один: 1, одна: 1, одно: 1,
+ '하나': 1, '한': 1, '일': 1,
two: 2, both: 2, dos: 2, due: 2, deux: 2, zwei: 2, dois: 2, duas: 2, '两': 2, '二': 2, два: 2, две: 2,
+ '둘': 2, '두': 2, '이': 2,
three: 3, tres: 3, tre: 3, trois: 3, drei: 3, 'três': 3, '三': 3, три: 3,
+ '셋': 3, '세': 3, '삼': 3,
four: 4, cuatro: 4, quattro: 4, quatre: 4, vier: 4, quatro: 4, '四': 4, четыре: 4,
+ '넷': 4, '네': 4, '사': 4,
five: 5, cinco: 5, cinque: 5, cinq: 5, 'fünf': 5, '五': 5, пять: 5,
+ '다섯': 5, '오': 5,
six: 6, '六': 6, seven: 7, '七': 7, eight: 8, '八': 8, nine: 9, '九': 9, ten: 10, '十': 10,
multiple: 2,
};
return wordToNum[s] || 0;
};
- 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|один|одна|одно|два|две|три|четыре|пять|[一二两三四五六七八九十])';
+ 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|画像|写真|图片|照片|圖片|изображение|фото)`, 'i'));
+ 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 videoCountMatch = text.match(new RegExp(`(?:^|[\\s,;and+])(${countPrefix})\\s+(?:videos?|clips?|recordings?|gifs?|animated[-_ ]gifs?|vidéo|vidéos|動画|视频|影片|видео)`, 'i'));
+ 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;
let isGeneric = false;
@@ -2392,8 +2408,9 @@ export class Agent extends LoopDetector {
} else if (expectedVideoCount > 0) {
expectedCount = expectedVideoCount;
} 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'));
+ 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) {
@@ -2402,6 +2419,10 @@ export class Agent extends LoopDetector {
}
}
+ if (expectedImageCount === 0 && expectedVideoCount === 0 && wantsImage && wantsVideo && expectedCount < 2) {
+ expectedCount = 2;
+ }
+
return { isGeneric, expectedCount, expectedImageCount, expectedVideoCount, wantsImage, wantsVideo, wantsGif, normalized: text };
}
diff --git a/src/chrome/src/agent/completion-invariant.js b/src/chrome/src/agent/completion-invariant.js
index 3ac96772d..80e1a6919 100644
--- a/src/chrome/src/agent/completion-invariant.js
+++ b/src/chrome/src/agent/completion-invariant.js
@@ -276,6 +276,11 @@ export function publicationResourceRecordRoot(link, identity, publicationResourc
: value.startsWith('bluesky:')
? '[data-testid="postText"]'
: '';
+ const embedSelector = value.startsWith('twitter:')
+ ? '[data-testid="quoteTweet"],[role="blockquote"],[data-testid*="quote"],[data-testid*="card.layout"]'
+ : value.startsWith('bluesky:')
+ ? '[data-testid^="postQuote-"],[data-testid="embeddedPost"],[data-testid*="quote"],[data-testid^="embed-"]'
+ : '';
const identityOf = (candidate) => {
try {
return publicationResourceIdentity(candidate.getAttribute('href') || candidate.href || '') || '';
@@ -290,6 +295,30 @@ export function publicationResourceRecordRoot(link, identity, publicationResourc
return null;
}
};
+ const isInsideAuthoredText = (node) => {
+ if (!bodySelector) return false;
+ try {
+ if (typeof node.closest === 'function') {
+ const bodyEl = node.closest(bodySelector);
+ if (bodyEl) {
+ if (!embedSelector || typeof node.closest !== 'function' || !node.closest(embedSelector)) {
+ return true;
+ }
+ }
+ }
+ } catch {}
+ let curr = node.parentElement;
+ for (let i = 0; curr && i < 5; i++, curr = curr.parentElement) {
+ try {
+ if (typeof curr.matches === 'function' && curr.matches(bodySelector)) {
+ if (!embedSelector || typeof node.closest !== 'function' || !node.closest(embedSelector)) {
+ return true;
+ }
+ }
+ } catch {}
+ }
+ return false;
+ };
// The largest subtree around a foreign permalink that this post's own
// permalink never reaches into is the embedded post.
const embeddedResourcesIn = (card) => {
@@ -300,13 +329,29 @@ export function publicationResourceRecordRoot(link, identity, publicationResourc
if (excluded.length >= 8) break;
const found = identityOf(candidate);
if (!found || found === value) continue;
- if (excluded.some(node => node === candidate || node.contains?.(candidate))) continue;
- let best = candidate;
- let node = candidate.parentElement;
- for (let depth = 0; node && node !== card && depth < 9; depth++, node = node.parentElement) {
- const inner = linksIn(node);
- if (!inner || inner.some(entry => identityOf(entry) === value)) break;
- best = node;
+ if (isInsideAuthoredText(candidate)) continue;
+ if (excluded.some(node => node === candidate || node.contains?.(node))) continue;
+ let embedContainer = null;
+ if (embedSelector) {
+ try {
+ if (typeof candidate.closest === 'function') {
+ embedContainer = candidate.closest(embedSelector);
+ }
+ } catch {}
+ }
+ let best = embedContainer || candidate;
+ if (!embedContainer) {
+ let node = candidate.parentElement;
+ for (let depth = 0; node && node !== card && depth < 9; depth++, node = node.parentElement) {
+ if (bodySelector) {
+ try {
+ if (typeof node.matches === 'function' && node.matches(bodySelector)) break;
+ } catch {}
+ }
+ const inner = linksIn(node);
+ if (!inner || inner.some(entry => identityOf(entry) === value)) break;
+ best = node;
+ }
}
excluded.push(best);
}
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 08119e176..d8251002a 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -222,9 +222,18 @@ const GENERIC_ATTACHMENT_WORDS = new Set([
'один', 'одна', 'одно', 'два', 'две', 'три', 'четыре', 'пять',
'вложения', 'вложение', 'фотографии', 'фотография', 'изображения', 'изображение', 'видео',
'и', 'или', 'плюс',
+ '사진', '이미지', '포토',
+ '동영상', '비디오', '영상', '클립',
+ '움짤',
+ '첨부', '첨부파일', '미디어', '파일',
+ '하나', '둘', '셋', '넷', '다섯',
+ '한', '두', '세', '네',
+ '일', '이', '삼', '사', '오',
+ '장', '개', '건', '편',
+ '와', '과', '및', '그리고', '하고', '도',
]);
-const CJK_GENERIC_ATTACHMENT_REGEX = /^[0-9一二两三四五六七八九十添付画像写真動画メディア已上传附件图片照片视频媒体枚つの本张條条个個장での\s\-_,.:;!?/\\()]+$/u;
+const CJK_GENERIC_ATTACHMENT_REGEX = /^[0-9一二两三四五六七八九十添付画像写真動画メディア已上传附件图片照片视频媒体枚つの本张條条个個장에서의사진이미지포토동영상비디오영상클립움짤첨부파일미디어하나둘셋넷다섯한두세네일이삼사오개건편와과및그리고하고도\s\-_,.:;!?/\\()&+/]+$/u;
// "On <url>, publish this" names a destination just as plainly as
// "publish this on <url>", but only when the URL is presented as a place.
@@ -2060,9 +2069,9 @@ export class Agent extends LoopDetector {
const text = String(value || '').trim().toLowerCase();
if (!text) return { isGeneric: true, expectedCount: 1, expectedImageCount: 0, expectedVideoCount: 0, wantsImage: false, wantsVideo: false, wantsGif: false, normalized: '' };
- const wantsGif = /\b(?:gif|gifs)\b|\.gif(?:[?#]|$)|(?:animated[-_ ]gif|動圖|动图)/i.test(text);
- const wantsVideo = wantsGif || /\b(?:video|videos|mp4|mov|webm|mkv|clip|clips|recording|recordings|vidéo|vidéos)\b|(?:動画|视频|影片|видео)/i.test(text);
- const wantsImage = /\b(?:image|images|photo|photos|picture|pictures|pic|pics|png|jpg|jpeg|webp|foto|fotos|bild|bilder|imagen(?:es)?|imágenes)\b|(?:画像|写真|图片|照片|圖片|изображение|фото)/i.test(text);
+ const wantsGif = /\b(?:gif|gifs)\b|\.gif(?:[?#]|$)|(?:animated[-_ ]gif|動圖|动图|움짤)/i.test(text);
+ const wantsVideo = wantsGif || /\b(?:video|videos|mp4|mov|webm|mkv|clip|clips|recording|recordings|vidéo|vidéos)\b|(?:動画|视频|影片|видео|동영상|비디오|영상)/i.test(text);
+ const wantsImage = /\b(?:image|images|photo|photos|picture|pictures|pic|pics|png|jpg|jpeg|webp|foto|fotos|bild|bilder|imagen(?:es)?|imágenes)\b|(?:画像|写真|图片|照片|圖片|изображение|фото|사진|이미지|포토)/i.test(text);
const parseCountWord = (str) => {
if (!str) return 0;
@@ -2071,22 +2080,29 @@ export class Agent extends LoopDetector {
const wordToNum = {
a: 1, an: 1, one: 1, single: 1, un: 1, une: 1, uno: 1, una: 1, ein: 1, eine: 1, einen: 1, einer: 1, um: 1, uma: 1,
'一': 1, один: 1, одна: 1, одно: 1,
+ '하나': 1, '한': 1, '일': 1,
two: 2, both: 2, dos: 2, due: 2, deux: 2, zwei: 2, dois: 2, duas: 2, '两': 2, '二': 2, два: 2, две: 2,
+ '둘': 2, '두': 2, '이': 2,
three: 3, tres: 3, tre: 3, trois: 3, drei: 3, 'três': 3, '三': 3, три: 3,
+ '셋': 3, '세': 3, '삼': 3,
four: 4, cuatro: 4, quattro: 4, quatre: 4, vier: 4, quatro: 4, '四': 4, четыре: 4,
+ '넷': 4, '네': 4, '사': 4,
five: 5, cinco: 5, cinque: 5, cinq: 5, 'fünf': 5, '五': 5, пять: 5,
+ '다섯': 5, '오': 5,
six: 6, '六': 6, seven: 7, '七': 7, eight: 8, '八': 8, nine: 9, '九': 9, ten: 10, '十': 10,
multiple: 2,
};
return wordToNum[s] || 0;
};
- 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|один|одна|одно|два|две|три|четыре|пять|[一二两三四五六七八九十])';
+ 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|画像|写真|图片|照片|圖片|изображение|фото)`, 'i'));
+ 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 videoCountMatch = text.match(new RegExp(`(?:^|[\\s,;and+])(${countPrefix})\\s+(?:videos?|clips?|recordings?|gifs?|animated[-_ ]gifs?|vidéo|vidéos|動画|视频|影片|видео)`, 'i'));
+ 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;
let isGeneric = false;
@@ -2106,8 +2122,9 @@ export class Agent extends LoopDetector {
} else if (expectedVideoCount > 0) {
expectedCount = expectedVideoCount;
} 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'));
+ 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) {
@@ -2116,6 +2133,10 @@ export class Agent extends LoopDetector {
}
}
+ if (expectedImageCount === 0 && expectedVideoCount === 0 && wantsImage && wantsVideo && expectedCount < 2) {
+ expectedCount = 2;
+ }
+
return { isGeneric, expectedCount, expectedImageCount, expectedVideoCount, wantsImage, wantsVideo, wantsGif, normalized: text };
}
diff --git a/src/firefox/src/agent/completion-invariant.js b/src/firefox/src/agent/completion-invariant.js
index 3ac96772d..80e1a6919 100644
--- a/src/firefox/src/agent/completion-invariant.js
+++ b/src/firefox/src/agent/completion-invariant.js
@@ -276,6 +276,11 @@ export function publicationResourceRecordRoot(link, identity, publicationResourc
: value.startsWith('bluesky:')
? '[data-testid="postText"]'
: '';
+ const embedSelector = value.startsWith('twitter:')
+ ? '[data-testid="quoteTweet"],[role="blockquote"],[data-testid*="quote"],[data-testid*="card.layout"]'
+ : value.startsWith('bluesky:')
+ ? '[data-testid^="postQuote-"],[data-testid="embeddedPost"],[data-testid*="quote"],[data-testid^="embed-"]'
+ : '';
const identityOf = (candidate) => {
try {
return publicationResourceIdentity(candidate.getAttribute('href') || candidate.href || '') || '';
@@ -290,6 +295,30 @@ export function publicationResourceRecordRoot(link, identity, publicationResourc
return null;
}
};
+ const isInsideAuthoredText = (node) => {
+ if (!bodySelector) return false;
+ try {
+ if (typeof node.closest === 'function') {
+ const bodyEl = node.closest(bodySelector);
+ if (bodyEl) {
+ if (!embedSelector || typeof node.closest !== 'function' || !node.closest(embedSelector)) {
+ return true;
+ }
+ }
+ }
+ } catch {}
+ let curr = node.parentElement;
+ for (let i = 0; curr && i < 5; i++, curr = curr.parentElement) {
+ try {
+ if (typeof curr.matches === 'function' && curr.matches(bodySelector)) {
+ if (!embedSelector || typeof node.closest !== 'function' || !node.closest(embedSelector)) {
+ return true;
+ }
+ }
+ } catch {}
+ }
+ return false;
+ };
// The largest subtree around a foreign permalink that this post's own
// permalink never reaches into is the embedded post.
const embeddedResourcesIn = (card) => {
@@ -300,13 +329,29 @@ export function publicationResourceRecordRoot(link, identity, publicationResourc
if (excluded.length >= 8) break;
const found = identityOf(candidate);
if (!found || found === value) continue;
- if (excluded.some(node => node === candidate || node.contains?.(candidate))) continue;
- let best = candidate;
- let node = candidate.parentElement;
- for (let depth = 0; node && node !== card && depth < 9; depth++, node = node.parentElement) {
- const inner = linksIn(node);
- if (!inner || inner.some(entry => identityOf(entry) === value)) break;
- best = node;
+ if (isInsideAuthoredText(candidate)) continue;
+ if (excluded.some(node => node === candidate || node.contains?.(node))) continue;
+ let embedContainer = null;
+ if (embedSelector) {
+ try {
+ if (typeof candidate.closest === 'function') {
+ embedContainer = candidate.closest(embedSelector);
+ }
+ } catch {}
+ }
+ let best = embedContainer || candidate;
+ if (!embedContainer) {
+ let node = candidate.parentElement;
+ for (let depth = 0; node && node !== card && depth < 9; depth++, node = node.parentElement) {
+ if (bodySelector) {
+ try {
+ if (typeof node.matches === 'function' && node.matches(bodySelector)) break;
+ } catch {}
+ }
+ const inner = linksIn(node);
+ if (!inner || inner.some(entry => identityOf(entry) === value)) break;
+ best = node;
+ }
}
excluded.push(best);
}
diff --git a/test/run.js b/test/run.js
index 24224b1c8..02082cc6e 100644
--- a/test/run.js
+++ b/test/run.js
@@ -24622,6 +24622,33 @@ test('publication resource records keep the owning social card when it embeds an
assert.equal(mediaRecord.root, mediaCard);
assert.deepEqual(mediaRecord.attachments, [photoNode, videoComponentNode],
`${label}: media attachments failed to deduplicate nested videoNode or failed to include photo`);
+
+ // Authored post body containing a foreign permalink keeps the postText in authored nodes.
+ const foreignPermalink = { getAttribute: () => '/other/status/888', href: '/other/status/888' };
+ const foreignBodyNode = {
+ innerText: 'Check this out https://x.com/other/status/888',
+ querySelectorAll: selector => (selector === 'a[href]' ? [foreignPermalink] : []),
+ contains: node => node === foreignPermalink,
+ };
+ foreignPermalink.parentElement = foreignBodyNode;
+ foreignPermalink.closest = selector => (selector.includes('Text') ? foreignBodyNode : null);
+ const foreignCardPermalink = {
+ innerText: '2m',
+ getAttribute: () => '/me/status/222',
+ href: '/me/status/222',
+ closest: () => foreignCard,
+ };
+ const foreignCard = {
+ innerText: 'Author\nCheck this out https://x.com/other/status/888',
+ querySelectorAll: selector => (String(selector).includes('Text') ? [foreignBodyNode] : [foreignCardPermalink, foreignPermalink]),
+ contains: node => node === foreignCardPermalink || node === foreignCard || node === foreignBodyNode || node === foreignPermalink,
+ };
+ foreignCardPermalink.parentElement = foreignCard;
+ foreignBodyNode.parentElement = foreignCard;
+ const foreignRecord = invariant.publicationResourceRecordRoot(foreignCardPermalink, identity, identityOf);
+ assert.equal(foreignRecord.root, foreignCard, `${label}: authored permalink discarded the owning card`);
+ assert.deepEqual(foreignRecord.excluded, [], `${label}: authored foreign permalink was treated as an embedded post`);
+ assert.deepEqual(foreignRecord.authored, [foreignBodyNode], `${label}: authored body containing foreign permalink was lost`);
}
for (const [label, rel] of [
@@ -87456,6 +87483,56 @@ test('attachment requirement parser treats conjunction-based requirements as gen
}
});
+test('attachment requirement parser recognizes Korean generic media nouns and counts', () => {
+ for (const AgentClass of [AgentCh, AgentFx]) {
+ const agent = new AgentClass({});
+ const reqImg = agent._parseWorkflowAttachmentRequirement('사진');
+ assert.equal(reqImg.isGeneric, true, AgentClass.name + ': 사진 should be generic');
+ assert.equal(reqImg.wantsImage, true, AgentClass.name + ': 사진 should want image');
+ assert.equal(reqImg.expectedCount, 1, AgentClass.name + ': 사진 should expect 1 attachment');
+
+ const reqVid = agent._parseWorkflowAttachmentRequirement('동영상');
+ assert.equal(reqVid.isGeneric, true, AgentClass.name + ': 동영상 should be generic');
+ assert.equal(reqVid.wantsVideo, true, AgentClass.name + ': 동영상 should want video');
+ assert.equal(reqVid.expectedCount, 1, AgentClass.name + ': 동영상 should expect 1 attachment');
+
+ const reqImg2 = agent._parseWorkflowAttachmentRequirement('사진 2장');
+ assert.equal(reqImg2.isGeneric, true, AgentClass.name + ': 사진 2장 should be generic');
+ assert.equal(reqImg2.expectedCount, 2, AgentClass.name + ': 사진 2장 should expect 2 attachments');
+ assert.equal(reqImg2.expectedImageCount, 2, AgentClass.name + ': 사진 2장 should expect 2 images');
+
+ const reqBoth = agent._parseWorkflowAttachmentRequirement('이미지와 동영상');
+ assert.equal(reqBoth.isGeneric, true, AgentClass.name + ': 이미지와 동영상 should be generic');
+ assert.equal(reqBoth.wantsImage, true, AgentClass.name + ': 이미지와 동영상 should want image');
+ assert.equal(reqBoth.wantsVideo, true, AgentClass.name + ': 이미지와 동영상 should want video');
+ assert.equal(reqBoth.expectedCount, 2, AgentClass.name + ': 이미지와 동영상 should expect 2 attachments');
+
+ const verifiedImg = agent._workflowSocialPublishedAttachmentObserved(
+ { value: '사진' },
+ { attachments: [{ type: 'image', src: 'https://pbs.twimg.com/media/pic.jpg' }] }
+ );
+ assert.equal(verifiedImg, true, AgentClass.name + ': valid image should satisfy 사진 requirement');
+
+ const rejectedImg = agent._workflowSocialPublishedAttachmentObserved(
+ { value: '사진' },
+ { attachments: [{ type: 'video', src: 'https://video.twimg.com/media/clip.mp4' }] }
+ );
+ assert.equal(rejectedImg, false, AgentClass.name + ': video attachment should not satisfy 사진 requirement');
+
+ const verifiedVid = agent._workflowSocialPublishedAttachmentObserved(
+ { value: '동영상' },
+ { attachments: [{ type: 'video', src: 'https://video.twimg.com/media/clip.mp4' }] }
+ );
+ assert.equal(verifiedVid, true, AgentClass.name + ': valid video should satisfy 동영상 requirement');
+
+ const rejectedVid = agent._workflowSocialPublishedAttachmentObserved(
+ { value: '동영상' },
+ { attachments: [{ type: 'image', src: 'https://pbs.twimg.com/media/pic.jpg' }] }
+ );
+ assert.equal(rejectedVid, false, AgentClass.name + ': image attachment should not satisfy 동영상 requirement');
+ }
+});
+
test('post body extraction preserves nested quotation marks and does not overwrite full body with prefix', async () => {
for (const [index, AgentClass] of [AgentCh, AgentFx].entries()) {
const agent = new AgentClass({});