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
fix: bind Discord DM and send evidence
Emre Sokullua1e4ccc3952d
diff --git a/src/chrome/src/agent/chat-workflow.js b/src/chrome/src/agent/chat-workflow.jsindex 570aa52f6..c860ad319 100644--- a/src/chrome/src/agent/chat-workflow.js+++ b/src/chrome/src/agent/chat-workflow.js@@ -81,6 +81,10 @@ export function canonicalChatText(value) {try { return text.normalize('NFKC').toLocaleLowerCase(); } catch { return text.toLowerCase(); }}+function messageTextForMatching(message) {+ return message?.authoredText ?? message?.text ?? '';+}+function hashText(value) {let hash = 2166136261;for (const char of String(value || '')) {@@ -149,6 +153,7 @@ export function normalizeChatMessage(value, { threadKey = '', occurrence = 0 } =const direction = normalizeDirection(value.direction ?? value.authorRole ?? value.role);const text = normalizeChatText(value.text ?? value.content ?? value.message);if (!text) return null;+ const authoredText = normalizeChatText(value.authoredText ?? value.primaryText);const author = bounded(value.author ?? value.authorName ?? value.sender, 240);const timestamp = bounded(value.timestamp ?? value.time ?? value.createdAt, 80);const explicitId = bounded(value.id ?? value.messageId ?? value.message_id, MAX_ID);@@ -156,6 +161,7 @@ export function normalizeChatMessage(value, { threadKey = '', occurrence = 0 } =id: explicitId || stableChatMessageId({ threadKey, direction, text, author, timestamp, occurrence }),direction,text,+ ...(authoredText ? { authoredText } : {}),...(author ? { author } : {}),...(timestamp ? { timestamp } : {}),...(value.verified === true ? { verified: true } : {}),@@ -281,7 +287,7 @@ function outgoingMessageKeys(snapshot) {let replyAnchor = 'chat_start';for (const message of snapshot.messages) {if (message.direction === 'incoming') replyAnchor = message.id;- if (message.direction === 'outgoing') keys.add(messageKey(snapshot.threadKey, message.text, replyAnchor));+ if (message.direction === 'outgoing') keys.add(messageKey(snapshot.threadKey, messageTextForMatching(message), replyAnchor));}return keys;}@@ -380,7 +386,7 @@ export function advanceChatSession(value, rawSnapshot, now = Date.now()) {// pending text is the same proof of delivery without depending on the anchor.const matchedPending = session.pendingOutbound&& (visibleOutgoingKeys.has(session.pendingOutbound.key)- || newOutgoing.some(message => canonicalChatText(message.text) === canonicalChatText(session.pendingOutbound.text))+ || newOutgoing.some(message => canonicalChatText(messageTextForMatching(message)) === canonicalChatText(session.pendingOutbound.text))|| !!pendingDiscordDelivery|| (!session.pendingOutbound.replyAnchor&& session.pendingOutbound.key === legacyMessageKey(snapshot.threadKey, session.pendingOutbound.text)));@@ -454,7 +460,7 @@ export function decideChatSend(value, rawSnapshot, text, now = Date.now()) {const sameReplyAlreadyVisible = snapshot.messages.some((message, index) => (index > latestIncomingIndex&& message.direction === 'outgoing'- && canonicalChatText(message.text) === canonicalChatText(body)+ && canonicalChatText(messageTextForMatching(message)) === canonicalChatText(body)));if (session.sentMessageKeys.includes(key)|| sameReplyAlreadyVisible) {@@ -508,7 +514,7 @@ function discordPendingDeliveryMatch(pending, snapshot, newMessages) {if (!Number.isFinite(dispatchedAt) || !Number.isFinite(observedAt)|| observedAt < dispatchedAt - 5_000) return null;const matchingMessages = newMessages.filter(message => (- canonicalChatText(message.text) === canonicalChatText(pending.text)+ canonicalChatText(messageTextForMatching(message)) === canonicalChatText(pending.text)));// Discord default avatars and server nicknames do not identify an author.// Correlate delivery only after a persisted successful dispatch, with onediff --git a/src/chrome/src/content/chat-observation.js b/src/chrome/src/content/chat-observation.jsindex 0651e6f90..2cfc82aa4 100644--- a/src/chrome/src/content/chat-observation.js+++ b/src/chrome/src/content/chat-observation.js@@ -167,20 +167,24 @@&& !!selectedDmChannelId && selectedDmChannelId !== channelId;} catch { return false; }});+ const transcriptDmRows = directMessageRoute+ ? query(transcript, '[role="article"]').filter(visible)+ : [];const transcriptDmChannelIds = directMessageRoute- ? query(transcript, '[role="article"][data-list-item-id]')- .filter(visible)+ ? transcriptDmRows.map(node => /^chat-messages___chat-messages-(\d+)-\d+$/.exec(attribute(node, 'data-list-item-id'))?.[1]).filter(Boolean): [];- const transcriptMatchesDmRoute = transcriptDmChannelIds.length > 0+ const transcriptMatchesDmRoute = transcriptDmRows.length > 0+ && transcriptDmChannelIds.length === transcriptDmRows.length&& transcriptDmChannelIds.every(id => id === channelId);const name = /^Message #(.+)$/.exec(attribute(composer, 'aria-label'))?.[1]|| /^Messages in (.+)$/.exec(attribute(transcript, 'aria-label'))?.[1]|| /^(.+) \(channel\)$/.exec(attribute(root, 'aria-label'))?.[1]|| (directMessageRoute ? `direct-message-${channelId}` : `channel-${channelId}`);return transcript && (directMessageRoute- ? !selectedDmRouteMismatch && (channelLink || transcriptMatchesDmRoute)+ ? !selectedDmRouteMismatch+ && (transcriptDmRows.length > 0 ? transcriptMatchesDmRoute : channelLink): channelLink)? {root, transcript, name, channelId,@@ -431,6 +435,7 @@id: `discord:${channel.channelId}:${messageId}`,direction,text: body,+ ...(text ? { authoredText: text } : {}),...(author ? { author } : {}),...(timestamp ? { timestamp } : {}),});diff --git a/src/firefox/src/agent/chat-workflow.js b/src/firefox/src/agent/chat-workflow.jsindex 570aa52f6..c860ad319 100644--- a/src/firefox/src/agent/chat-workflow.js+++ b/src/firefox/src/agent/chat-workflow.js@@ -81,6 +81,10 @@ export function canonicalChatText(value) {try { return text.normalize('NFKC').toLocaleLowerCase(); } catch { return text.toLowerCase(); }}+function messageTextForMatching(message) {+ return message?.authoredText ?? message?.text ?? '';+}+function hashText(value) {let hash = 2166136261;for (const char of String(value || '')) {@@ -149,6 +153,7 @@ export function normalizeChatMessage(value, { threadKey = '', occurrence = 0 } =const direction = normalizeDirection(value.direction ?? value.authorRole ?? value.role);const text = normalizeChatText(value.text ?? value.content ?? value.message);if (!text) return null;+ const authoredText = normalizeChatText(value.authoredText ?? value.primaryText);const author = bounded(value.author ?? value.authorName ?? value.sender, 240);const timestamp = bounded(value.timestamp ?? value.time ?? value.createdAt, 80);const explicitId = bounded(value.id ?? value.messageId ?? value.message_id, MAX_ID);@@ -156,6 +161,7 @@ export function normalizeChatMessage(value, { threadKey = '', occurrence = 0 } =id: explicitId || stableChatMessageId({ threadKey, direction, text, author, timestamp, occurrence }),direction,text,+ ...(authoredText ? { authoredText } : {}),...(author ? { author } : {}),...(timestamp ? { timestamp } : {}),...(value.verified === true ? { verified: true } : {}),@@ -281,7 +287,7 @@ function outgoingMessageKeys(snapshot) {let replyAnchor = 'chat_start';for (const message of snapshot.messages) {if (message.direction === 'incoming') replyAnchor = message.id;- if (message.direction === 'outgoing') keys.add(messageKey(snapshot.threadKey, message.text, replyAnchor));+ if (message.direction === 'outgoing') keys.add(messageKey(snapshot.threadKey, messageTextForMatching(message), replyAnchor));}return keys;}@@ -380,7 +386,7 @@ export function advanceChatSession(value, rawSnapshot, now = Date.now()) {// pending text is the same proof of delivery without depending on the anchor.const matchedPending = session.pendingOutbound&& (visibleOutgoingKeys.has(session.pendingOutbound.key)- || newOutgoing.some(message => canonicalChatText(message.text) === canonicalChatText(session.pendingOutbound.text))+ || newOutgoing.some(message => canonicalChatText(messageTextForMatching(message)) === canonicalChatText(session.pendingOutbound.text))|| !!pendingDiscordDelivery|| (!session.pendingOutbound.replyAnchor&& session.pendingOutbound.key === legacyMessageKey(snapshot.threadKey, session.pendingOutbound.text)));@@ -454,7 +460,7 @@ export function decideChatSend(value, rawSnapshot, text, now = Date.now()) {const sameReplyAlreadyVisible = snapshot.messages.some((message, index) => (index > latestIncomingIndex&& message.direction === 'outgoing'- && canonicalChatText(message.text) === canonicalChatText(body)+ && canonicalChatText(messageTextForMatching(message)) === canonicalChatText(body)));if (session.sentMessageKeys.includes(key)|| sameReplyAlreadyVisible) {@@ -508,7 +514,7 @@ function discordPendingDeliveryMatch(pending, snapshot, newMessages) {if (!Number.isFinite(dispatchedAt) || !Number.isFinite(observedAt)|| observedAt < dispatchedAt - 5_000) return null;const matchingMessages = newMessages.filter(message => (- canonicalChatText(message.text) === canonicalChatText(pending.text)+ canonicalChatText(messageTextForMatching(message)) === canonicalChatText(pending.text)));// Discord default avatars and server nicknames do not identify an author.// Correlate delivery only after a persisted successful dispatch, with onediff --git a/src/firefox/src/content/chat-observation.js b/src/firefox/src/content/chat-observation.jsindex 0651e6f90..2cfc82aa4 100644--- a/src/firefox/src/content/chat-observation.js+++ b/src/firefox/src/content/chat-observation.js@@ -167,20 +167,24 @@&& !!selectedDmChannelId && selectedDmChannelId !== channelId;} catch { return false; }});+ const transcriptDmRows = directMessageRoute+ ? query(transcript, '[role="article"]').filter(visible)+ : [];const transcriptDmChannelIds = directMessageRoute- ? query(transcript, '[role="article"][data-list-item-id]')- .filter(visible)+ ? transcriptDmRows.map(node => /^chat-messages___chat-messages-(\d+)-\d+$/.exec(attribute(node, 'data-list-item-id'))?.[1]).filter(Boolean): [];- const transcriptMatchesDmRoute = transcriptDmChannelIds.length > 0+ const transcriptMatchesDmRoute = transcriptDmRows.length > 0+ && transcriptDmChannelIds.length === transcriptDmRows.length&& transcriptDmChannelIds.every(id => id === channelId);const name = /^Message #(.+)$/.exec(attribute(composer, 'aria-label'))?.[1]|| /^Messages in (.+)$/.exec(attribute(transcript, 'aria-label'))?.[1]|| /^(.+) \(channel\)$/.exec(attribute(root, 'aria-label'))?.[1]|| (directMessageRoute ? `direct-message-${channelId}` : `channel-${channelId}`);return transcript && (directMessageRoute- ? !selectedDmRouteMismatch && (channelLink || transcriptMatchesDmRoute)+ ? !selectedDmRouteMismatch+ && (transcriptDmRows.length > 0 ? transcriptMatchesDmRoute : channelLink): channelLink)? {root, transcript, name, channelId,@@ -431,6 +435,7 @@id: `discord:${channel.channelId}:${messageId}`,direction,text: body,+ ...(text ? { authoredText: text } : {}),...(author ? { author } : {}),...(timestamp ? { timestamp } : {}),});diff --git a/test/discord-guard.mjs b/test/discord-guard.mjsindex 12bd9bf2d..399f51cc6 100644--- a/test/discord-guard.mjs+++ b/test/discord-guard.mjs@@ -257,7 +257,11 @@ for (const [kind, engine, AgentClass, policy] of [await page.locator('#channel').evaluate(el=>el.remove());assert.equal((await observe()).success,true,'matching message-row channel IDs bind the DM pane without an active rail link');+ await page.locator('nav').evaluate(el=>el.insertAdjacentHTML('beforeend',+ '<a aria-current="page" data-list-item-id="private-channels-uid_11___789" href="/channels/@me/789">Current DM</a>'));await page.evaluate(()=>history.replaceState(null,'','/channels/@me/789'));+ assert.equal((await observe()).success,false,'a matching current rail link cannot override stale transcript rows');+ await page.locator('nav [data-list-item-id="private-channels-uid_11___789"]').evaluate(el=>el.remove());assert.equal((await observe()).success,false,'stale transcript rows cannot bind to a different DM route without an active rail link');await page.evaluate(()=>history.replaceState(null,'','/channels/@me/456/1001'));assert.equal((await observe()).success,true,'the route succeeds again when the transcript IDs match it');@@ -297,6 +301,7 @@ for (const [kind, engine, AgentClass, policy] of [assert.equal(result.conversationIdentity,'#general');assert.deepEqual(result.messages,[{id:'discord:456:1001', direction:'incoming', text:'Hello from the fixture',+ authoredText:'Hello from the fixture',author:'Ficsit', timestamp:'2026-09-29T01:00:00.000Z',}]);await page.evaluate(()=>history.replaceState(null,'','/channels/123/456/1001'));@@ -350,7 +355,7 @@ for (const [kind, engine, AgentClass, policy] of [await page.locator('[data-list-id=chat-messages]').evaluate(el=>el.insertAdjacentHTML('beforeend',`<li><div role="article" data-list-item-id="chat-messages___chat-messages-456-1002"><div class="contents"><img src="/assets/embed/avatars/3.png"><h3><span id="message-username-1002"><span data-text="WebBrain">WebBrain</span></span><time id="message-timestamp-1002" datetime="${new Date().toISOString()}"></time></h3>- <div id="message-content-1002">My answer</div></div></div></li>+ <div id="message-content-1002">My answer</div></div><div id="message-accessories-1002"><div class="embedTitle_fixture">My answer link preview</div><div class="embedDescription_fixture">Preview description</div></div></div></li><li><div role="article" data-list-item-id="chat-messages___chat-messages-999-1003"><div id="message-content-1003">Wrong channel</div></div></li><li><div role="article" class="isSystemMessage_fixture" data-list-item-id="chat-messages___chat-messages-456-1004"><div id="message-content-1004">A member joined</div></div></li>`));const afterDraft=await observe();@@ -365,6 +370,8 @@ for (const [kind, engine, AgentClass, policy] of [const afterSend=await observe();assert.deepEqual(afterSend.messages.map(item=>item.id),['discord:456:1001','discord:456:1002']);assert.equal(afterSend.messages[1].direction,'unknown','nickname equality never asserts message authorship');+ assert.equal(afterSend.messages[1].authoredText,'My answer');+ assert.match(afterSend.messages[1].text,/Attachment: My answer link preview/);const withoutDispatchEvidence=advanceChatSession(pending,afterSend,Date.parse(afterSend.observedAt));assert.equal(withoutDispatchEvidence.pendingDeliveryVerified,false,'an ordinary observation cannot infer Discord message authorship');advanced=advanceChatSession(dispatchedPending,afterSend,Date.parse(afterSend.observedAt));@@ -435,6 +442,26 @@ for (const [kind, engine, AgentClass, policy] of [<div id="message-content-1007">A member with the same display name</div></div></div></li>`));const collidingNickname=await observe();assert.equal(collidingNickname.messages.at(-1).direction,'unknown','a member sharing the account display name is not called outgoing');+ const previewBody='https://example.com/release';+ const previewTimestamp=new Date().toISOString();+ const previewAttemptedAt=new Date(Date.parse(previewTimestamp)-1000).toISOString();+ await page.locator('[data-list-id=chat-messages]').evaluate((el,{previewBody,previewTimestamp})=>el.insertAdjacentHTML('beforeend',`+ <li><div role="article" data-list-item-id="chat-messages___chat-messages-456-1009"><div class="contents">+ <img src="https://cdn.discordapp.com/avatars/11/self.webp?size=160"><h3><span id="message-username-1009"><span data-text="WebBrain">WebBrain</span></span><time id="message-timestamp-1009" datetime="${previewTimestamp}"></time></h3>+ <div id="message-content-1009">${previewBody}</div></div><div id="message-accessories-1009"><div class="embedTitle_fixture">Release notes</div><div class="embedDescription_fixture">Preview description</div></div></div></li>`),{previewBody,previewTimestamp});+ const afterPreview=await observe();+ const previewMessage=afterPreview.messages.at(-1);+ assert.equal(previewMessage.id,'discord:456:1009');+ assert.equal(previewMessage.direction,'outgoing');+ assert.equal(previewMessage.authoredText,previewBody);+ assert.match(previewMessage.text,/Attachment: Release notes/);+ const previewPending=markChatSendPending(createChatSession({threadKey:afterPreview.threadKey}),{+ ok:true,messageKey:'preview-send',threadKey:afterPreview.threadKey,text:previewBody,+ attemptedAt:previewAttemptedAt,replyAnchor:'chat_start',+ },Date.parse(previewAttemptedAt));+ const previewDispatched={...previewPending,pendingOutbound:{...previewPending.pendingOutbound,dispatchedAt:previewAttemptedAt}};+ const previewAdvance=workflow(previewDispatched,afterPreview,Date.parse(afterPreview.observedAt));+ assert.equal(previewAdvance.session.pendingOutbound,null,'ordinary outgoing verification matches authored text despite accessory text');await page.locator('[data-list-id=chat-messages]').evaluate(el=>el.replaceChildren());assert.equal((await observe()).success,true);await page.locator('#channel').evaluate(el=>el.setAttribute('href','/channels/123/999'));