Reply to selected text in Assistant answers

Selecting text in an Assistant answer shows a Reply button that quotes the selection in the chat input. The paragraph Ask button no longer shows in Assistant answers.
This commit is contained in:
Nolann Biron
2026-12-03 11:48:51 +01:00
parent 3c33ebe356
commit ed9505acde
44 changed files with 74 additions and 13 deletions
@@ -161,6 +161,8 @@ function AIChatAssistantGroup(props: {
onOpenChange={setManuallyOpened}
disabled={!hasFinalAnswer}
data-testid="ai-chat-message-assistant"
// Selecting text here offers "Reply" (see `AskAITextSelection`).
data-ai-chat-reply-root=""
id={`message-${firstAssistantIndex}`}
className={tcls(
'flex flex-col gap-2',
@@ -22,7 +22,8 @@ const MARGIN = 8;
/**
* Floating "Ask" button anchored above a text selection. Clicking it stages the selection as a
* reference and opens the AI chat. Only rendered in Assistant mode, on non-touch devices.
* reference and opens the AI chat. In an assistant answer, it reads "Reply" and quotes the
* selection in the open chat. Only rendered in Assistant mode, on non-touch devices.
*/
export function AskAITextSelection() {
const config = useAIConfig();
@@ -36,11 +37,13 @@ export function AskAITextSelection() {
const toolbarRef = React.useRef<HTMLDivElement>(null);
const { selection, clear } = useStableTextSelection({
rootSelector: '[data-content-ref-root]',
rootSelector: '[data-content-ref-root], [data-ai-chat-reply-root]',
enabled,
ignoreRef: toolbarRef,
});
const isReply = selection?.root.hasAttribute('data-ai-chat-reply-root') ?? false;
const [coords, setCoords] = React.useState<{ top: number; left: number } | null>(null);
// Position once the button has been measured, so it can be centered and clamped to the viewport.
@@ -73,7 +76,9 @@ export function AskAITextSelection() {
}, [selection]);
const onClick = () => {
trackEvent({ type: 'ask_trigger_click', trigger: 'selection' });
if (!isReply) {
trackEvent({ type: 'ask_trigger_click', trigger: 'selection' });
}
if (!selection) {
return;
}
@@ -87,7 +92,9 @@ export function AskAITextSelection() {
id: `text-${fnv1a(content, { size: 32 })}`,
content,
});
chatController.open('selection');
if (!isReply) {
chatController.open('selection');
}
chatController.focus();
clear();
};
@@ -109,7 +116,8 @@ export function AskAITextSelection() {
position: 'fixed',
top: coords?.top ?? 0,
left: coords?.left ?? 0,
zIndex: 40,
// Above the AI chat side sheet (z-41), for replies.
zIndex: 42,
}}
// Keep the selection alive: prevent the button from stealing focus on click.
onMouseDown={(event) => event.preventDefault()}
@@ -117,11 +125,17 @@ export function AskAITextSelection() {
<Button
size="small"
variant="primary"
icon={<AIChatIcon state="default" trademark={config.trademark} />}
icon={
isReply ? (
'reply'
) : (
<AIChatIcon state="default" trademark={config.trademark} />
)
}
onClick={onClick}
className="shadow-sm"
>
{t(language, 'ask')}
{t(language, isReply ? 'reply' : 'ask')}
</Button>
</motion.div>
) : null}
@@ -11,10 +11,12 @@ type SelectionAnchor = {
export type StableTextSelection = {
anchor: SelectionAnchor;
text: string;
/** The `rootSelector` match that holds the selection. */
root: Element;
};
type Options = {
/** Selector of the content root; both selection endpoints must be inside it. */
/** Selector of the content roots; both selection endpoints must be inside the same one. */
rootSelector: string;
enabled: boolean;
/** Pointer events inside this element are ignored, so clicking the UI keeps the selection. */
@@ -180,13 +182,15 @@ function readStableSelection(rootSelector: string): StableTextSelection | null {
return null;
}
const root = document.querySelector(rootSelector);
if (!root || !root.contains(sel.anchorNode) || !root.contains(sel.focusNode)) {
const anchorElement =
sel.anchorNode instanceof Element ? sel.anchorNode : sel.anchorNode?.parentElement;
const root = anchorElement?.closest(rootSelector);
if (!root || !root.contains(sel.focusNode)) {
return null;
}
const anchor = getSelectionAnchor(sel);
return anchor ? { anchor, text } : null;
return anchor ? { anchor, text, root } : null;
}
/** Box centered above the whole selection, or null if it has no size or is scrolled out of view. */
@@ -27,10 +27,11 @@ export function Paragraph(props: BlockProps<DocumentBlockParagraph>) {
</p>
);
// Offer to ask the assistant about any paragraph, in Assistant mode, on screen.
// Offer to ask the assistant about any paragraph of the page, in Assistant mode. Not in AI
// answers, where replying to a selection is the way to follow up.
const contentContext = context.contentContext;
const aiAssistantEnabled =
context.mode !== 'print' &&
context.isPageBody &&
// Table cells would clip the margin button, and big tables hold thousands of paragraphs.
!ancestorBlocks.some((ancestor) => ancestor.type === 'table') &&
contentContext != null &&
@@ -36,6 +36,7 @@ export const ar: TranslationLanguage = {
search_scope_variant_all: 'كل المحتوى',
search_scope_variant_all_description: 'كل نسخة متاحة',
ask: 'اسأل',
reply: 'رد',
search_ask: 'اسأل "${1}"',
search_ask_description: 'اعثر على الإجابة باستخدام ${1}',
search_ask_sources: 'المصادر',
@@ -36,6 +36,7 @@ export const bg: TranslationLanguage = {
search_scope_variant_all: 'Цялото съдържание',
search_scope_variant_all_description: 'Всеки наличен вариант',
ask: 'Попитайте',
reply: 'Отговор',
search_ask: 'Попитайте "${1}"',
search_ask_description: 'Намерете отговора с ${1}',
search_ask_sources: 'Източници',
@@ -36,6 +36,7 @@ export const cs: TranslationLanguage = {
search_scope_variant_all: 'Veškerý obsah',
search_scope_variant_all_description: 'Každá dostupná varianta',
ask: 'Zeptat se',
reply: 'Odpovědět',
search_ask: 'Zeptat se "${1}"',
search_ask_description: 'Najít odpověď pomocí ${1}',
search_ask_sources: 'Zdroje',
@@ -36,6 +36,7 @@ export const da: TranslationLanguage = {
search_scope_variant_all: 'Alt indhold',
search_scope_variant_all_description: 'Alle tilgængelige varianter',
ask: 'Spørg',
reply: 'Svar',
search_ask: 'Spørg "${1}"',
search_ask_description: 'Find svaret med ${1}',
search_ask_sources: 'Kilder',
@@ -36,6 +36,7 @@ export const de: TranslationLanguage = {
search_scope_variant_all: 'Alle Inhalte',
search_scope_variant_all_description: 'Jede verfügbare Variante',
ask: 'Fragen',
reply: 'Antworten',
ai_chat_ask_query: 'An ${1} fragen: "${2}"',
search_ask: 'Fragen "${1}"',
search_ask_description: 'Finden Sie die Antwort mit ${1}',
@@ -36,6 +36,7 @@ export const el: TranslationLanguage = {
search_scope_variant_all: 'Όλο το περιεχόμενο',
search_scope_variant_all_description: 'Κάθε διαθέσιμη παραλλαγή',
ask: 'Ρωτήστε',
reply: 'Απάντηση',
search_ask: 'Ρωτήστε "${1}"',
search_ask_description: 'Βρείτε την απάντηση με το ${1}',
search_ask_sources: 'Πηγές',
@@ -34,6 +34,7 @@ export const en = {
search_scope_variant_all: 'All content',
search_scope_variant_all_description: 'Every available variant',
ask: 'Ask',
reply: 'Reply',
search_ask: 'Ask "${1}"',
search_ask_description: 'Find the answer with ${1}',
search_ask_sources: 'Sources',
@@ -36,6 +36,7 @@ export const es: TranslationLanguage = {
search_scope_variant_all: 'Todo el contenido',
search_scope_variant_all_description: 'Cada variante disponible',
ask: 'Preguntar',
reply: 'Responder',
ai_chat_ask_query: 'Preguntar a ${1} "${2}"',
search_ask: 'Preguntar "${1}"',
search_ask_description: 'Encuentra la respuesta con ${1}',
@@ -36,6 +36,7 @@ export const et: TranslationLanguage = {
search_scope_variant_all: 'Kogu sisu',
search_scope_variant_all_description: 'Iga saadaolev variant',
ask: 'Küsi',
reply: 'Vasta',
search_ask: 'Küsi "${1}"',
search_ask_description: 'Leia vastus ${1} abil',
search_ask_sources: 'Allikad',
@@ -36,6 +36,7 @@ export const fi: TranslationLanguage = {
search_scope_variant_all: 'Kaikki sisältö',
search_scope_variant_all_description: 'Kaikki saatavilla olevat variantit',
ask: 'Kysy',
reply: 'Vastaa',
search_ask: 'Kysy "${1}"',
search_ask_description: 'Etsi vastaus palvelulla ${1}',
search_ask_sources: 'Lähteet',
@@ -36,6 +36,7 @@ export const fr: TranslationLanguage = {
search_scope_variant_all: 'Tout le contenu',
search_scope_variant_all_description: 'Chaque variante disponible',
ask: 'Une question ?',
reply: 'Répondre',
ai_chat_ask_query: 'Demander à ${1} « ${2} »',
search_ask: 'Demander « ${1} »',
search_ask_description: 'Trouvez la réponse avec ${1}',
@@ -36,6 +36,7 @@ export const he: TranslationLanguage = {
search_scope_variant_all: 'כל התוכן',
search_scope_variant_all_description: 'כל גרסה זמינה',
ask: 'שאל',
reply: 'השב',
search_ask: 'שאל "${1}"',
search_ask_description: 'מצא את התשובה בעזרת ${1}',
search_ask_sources: 'מקורות',
@@ -36,6 +36,7 @@ export const hi: TranslationLanguage = {
search_scope_variant_all: 'सभी सामग्री',
search_scope_variant_all_description: 'हर उपलब्ध वैरिएंट',
ask: 'पूछें',
reply: 'जवाब दें',
search_ask: '"${1}" पूछें',
search_ask_description: '${1} से उत्तर खोजें',
search_ask_sources: 'स्रोत',
@@ -36,6 +36,7 @@ export const hr: TranslationLanguage = {
search_scope_variant_all: 'Sav sadržaj',
search_scope_variant_all_description: 'Svaka dostupna varijanta',
ask: 'Pitaj',
reply: 'Odgovori',
search_ask: 'Pitaj "${1}"',
search_ask_description: 'Pronađi odgovor pomoću ${1}',
search_ask_sources: 'Izvori',
@@ -36,6 +36,7 @@ export const hu: TranslationLanguage = {
search_scope_variant_all: 'Minden tartalom',
search_scope_variant_all_description: 'Minden elérhető változat',
ask: 'Kérdezés',
reply: 'Válasz',
search_ask: '"${1}" kérdezése',
search_ask_description: 'Találja meg a választ ezzel: ${1}',
search_ask_sources: 'Források',
@@ -36,6 +36,7 @@ export const id: TranslationLanguage = {
search_scope_variant_all: 'Semua konten',
search_scope_variant_all_description: 'Setiap varian yang tersedia',
ask: 'Tanya',
reply: 'Balas',
search_ask: 'Tanya "${1}"',
search_ask_description: 'Temukan jawabannya dengan ${1}',
search_ask_sources: 'Sumber',
@@ -36,6 +36,7 @@ export const is: TranslationLanguage = {
search_scope_variant_all: 'Allt efni',
search_scope_variant_all_description: 'Allar tiltækar útgáfur',
ask: 'Spyrja',
reply: 'Svara',
search_ask: 'Spyrja "${1}"',
search_ask_description: 'Finndu svarið með ${1}',
search_ask_sources: 'Heimildir',
@@ -36,6 +36,7 @@ export const it: TranslationLanguage = {
search_scope_variant_all: 'Tutti i contenuti',
search_scope_variant_all_description: 'Ogni variante disponibile',
ask: 'Chiedi',
reply: 'Rispondi',
ai_chat_ask_query: 'Chiedi a ${1} "${2}"',
search_ask: 'Chiedi "${1}"',
search_ask_description: 'Trova la risposta con ${1}',
@@ -36,6 +36,7 @@ export const ja: TranslationLanguage = {
search_scope_variant_all: 'すべてのコンテンツ',
search_scope_variant_all_description: '利用可能なすべてのバリエーション',
ask: '質問する',
reply: '返信',
ai_chat_ask_query: '"${2}" を ${1} に質問する',
search_ask: '"${1}" を質問する',
search_ask_description: '${1}で答えを見つける',
@@ -36,6 +36,7 @@ export const ko: TranslationLanguage = {
search_scope_variant_all: '전체 콘텐츠',
search_scope_variant_all_description: '사용 가능한 모든 변형',
ask: '질문',
reply: '답장',
ai_chat_ask_query: '${1}에게 "${2}" 질문하기',
search_ask: '"${1}" 질문하기',
search_ask_description: '${1}로 답변 찾기',
@@ -36,6 +36,7 @@ export const lt: TranslationLanguage = {
search_scope_variant_all: 'Visas turinys',
search_scope_variant_all_description: 'Kiekvienas galimas variantas',
ask: 'Klausti',
reply: 'Atsakyti',
search_ask: 'Klausti "${1}"',
search_ask_description: 'Raskite atsakymą naudodami ${1}',
search_ask_sources: 'Šaltiniai',
@@ -36,6 +36,7 @@ export const lv: TranslationLanguage = {
search_scope_variant_all: 'Viss saturs',
search_scope_variant_all_description: 'Katrs pieejamais variants',
ask: 'Jautāt',
reply: 'Atbildēt',
search_ask: 'Jautāt "${1}"',
search_ask_description: 'Atrodiet atbildi ar ${1}',
search_ask_sources: 'Avoti',
@@ -36,6 +36,7 @@ export const ms: TranslationLanguage = {
search_scope_variant_all: 'Semua kandungan',
search_scope_variant_all_description: 'Setiap varian yang tersedia',
ask: 'Tanya',
reply: 'Balas',
search_ask: 'Tanya "${1}"',
search_ask_description: 'Cari jawapan dengan ${1}',
search_ask_sources: 'Sumber',
@@ -36,6 +36,7 @@ export const nl: TranslationLanguage = {
search_scope_variant_all: 'Alle inhoud',
search_scope_variant_all_description: 'Elke beschikbare variant',
ask: 'Vragen',
reply: 'Beantwoorden',
ai_chat_ask_query: 'Vraag ${1} "${2}"',
search_ask: 'Vraag "${1}"',
search_ask_description: 'Vind het antwoord met ${1}',
@@ -36,6 +36,7 @@ export const no: TranslationLanguage = {
search_scope_variant_all: 'Alt innhold',
search_scope_variant_all_description: 'Hver tilgjengelig variant',
ask: 'Spør',
reply: 'Svar',
ai_chat_ask_query: 'Spør ${1} "${2}"',
search_ask: 'Spør "${1}"',
search_ask_description: 'Finn svaret med ${1}',
@@ -36,6 +36,7 @@ export const pl: TranslationLanguage = {
search_scope_variant_all: 'Cała treść',
search_scope_variant_all_description: 'Każdy dostępny wariant',
ask: 'Zapytaj',
reply: 'Odpowiedz',
search_ask: 'Zapytaj "${1}"',
search_ask_description: 'Znajdź odpowiedź z ${1}',
search_ask_sources: 'Źródła',
@@ -36,6 +36,7 @@ export const pt_br: TranslationLanguage = {
search_scope_variant_all: 'Todo o conteúdo',
search_scope_variant_all_description: 'Cada variante disponível',
ask: 'Perguntar',
reply: 'Responder',
ai_chat_ask_query: 'Perguntar a ${1} "${2}"',
search_ask: 'Perguntar "${1}"',
search_ask_description: 'Encontre a resposta com ${1}',
@@ -36,6 +36,7 @@ export const pt: TranslationLanguage = {
search_scope_variant_all: 'Todo o conteúdo',
search_scope_variant_all_description: 'Todas as variantes disponíveis',
ask: 'Perguntar',
reply: 'Responder',
search_ask: 'Perguntar "${1}"',
search_ask_description: 'Encontre a resposta com ${1}',
search_ask_sources: 'Fontes',
@@ -36,6 +36,7 @@ export const ro: TranslationLanguage = {
search_scope_variant_all: 'Tot conținutul',
search_scope_variant_all_description: 'Fiecare variantă disponibilă',
ask: 'Întreabă',
reply: 'Răspunde',
search_ask: 'Întreabă "${1}"',
search_ask_description: 'Găsește răspunsul cu ${1}',
search_ask_sources: 'Surse',
@@ -36,6 +36,7 @@ export const ru: TranslationLanguage = {
search_scope_variant_all: 'Весь контент',
search_scope_variant_all_description: 'Каждый доступный вариант',
ask: 'Спросить',
reply: 'Ответить',
ai_chat_ask_query: 'Спросить у ${1} "${2}"',
search_ask: 'Спросить "${1}"',
search_ask_description: 'Найти ответ с помощью ${1}',
@@ -36,6 +36,7 @@ export const sk: TranslationLanguage = {
search_scope_variant_all: 'Všetok obsah',
search_scope_variant_all_description: 'Každý dostupný variant',
ask: 'Opýtať sa',
reply: 'Odpovedať',
search_ask: 'Opýtať sa "${1}"',
search_ask_description: 'Nájdite odpoveď pomocou ${1}',
search_ask_sources: 'Zdroje',
@@ -36,6 +36,7 @@ export const sl: TranslationLanguage = {
search_scope_variant_all: 'Vsa vsebina',
search_scope_variant_all_description: 'Vsaka razpoložljiva različica',
ask: 'Vprašaj',
reply: 'Odgovori',
search_ask: 'Vprašaj "${1}"',
search_ask_description: 'Poišči odgovor z ${1}',
search_ask_sources: 'Viri',
@@ -36,6 +36,7 @@ export const sv: TranslationLanguage = {
search_scope_variant_all: 'Allt innehåll',
search_scope_variant_all_description: 'Varje tillgänglig variant',
ask: 'Fråga',
reply: 'Svara',
search_ask: 'Fråga "${1}"',
search_ask_description: 'Hitta svaret med ${1}',
search_ask_sources: 'Källor',
@@ -36,6 +36,7 @@ export const th: TranslationLanguage = {
search_scope_variant_all: 'เนื้อหาทั้งหมด',
search_scope_variant_all_description: 'ทุกตัวแปรที่มี',
ask: 'ถาม',
reply: 'ตอบกลับ',
search_ask: 'ถาม "${1}"',
search_ask_description: 'ค้นหาคำตอบด้วย ${1}',
search_ask_sources: 'แหล่งที่มา',
@@ -36,6 +36,7 @@ export const tr: TranslationLanguage = {
search_scope_variant_all: 'Tüm içerik',
search_scope_variant_all_description: 'Kullanılabilir her varyant',
ask: 'Sor',
reply: 'Yanıtla',
search_ask: '"${1}" sor',
search_ask_description: '${1} ile cevabı bul',
search_ask_sources: 'Kaynaklar',
@@ -36,6 +36,7 @@ export const uk: TranslationLanguage = {
search_scope_variant_all: 'Увесь вміст',
search_scope_variant_all_description: 'Кожен доступний варіант',
ask: 'Запитати',
reply: 'Відповісти',
search_ask: 'Запитати "${1}"',
search_ask_description: 'Знайдіть відповідь за допомогою ${1}',
search_ask_sources: 'Джерела',
@@ -36,6 +36,7 @@ export const vi: TranslationLanguage = {
search_scope_variant_all: 'Tất cả nội dung',
search_scope_variant_all_description: 'Mọi biến thể có sẵn',
ask: 'Hỏi',
reply: 'Trả lời',
search_ask: 'Hỏi "${1}"',
search_ask_description: 'Tìm câu trả lời bằng ${1}',
search_ask_sources: 'Nguồn',
@@ -36,6 +36,7 @@ export const yue: TranslationLanguage = {
search_scope_variant_all: '所有內容',
search_scope_variant_all_description: '所有可用版本',
ask: '發問',
reply: '回覆',
search_ask: '問「${1}」',
search_ask_description: '用 ${1} 搵答案',
search_ask_sources: '來源',
@@ -36,6 +36,7 @@ export const zh_tw: TranslationLanguage = {
search_scope_variant_all: '所有內容',
search_scope_variant_all_description: '所有可用版本',
ask: '詢問',
reply: '回覆',
search_ask: '詢問「${1}」',
search_ask_description: '使用 ${1} 尋找答案',
search_ask_sources: '來源',
@@ -36,6 +36,7 @@ export const zh: TranslationLanguage = {
search_scope_variant_all: '所有内容',
search_scope_variant_all_description: '每个可用的变体',
ask: '询问',
reply: '回复',
ai_chat_ask_query: '向${1}询问"${2}"',
search_ask: '询问"${1}"',
search_ask_description: '利用${1}找到答案',