Skip to content

An AI-powered search engine with a generative UI

Fix citation mapping for message copy

Yoshiki Miura27ff22bd1134
diff --git a/components/answer-section.tsx b/components/answer-section.tsx
index 5236a26a..8d6eb84e 100644
--- a/components/answer-section.tsx
+++ b/components/answer-section.tsx
@@ -65,7 +65,7 @@ export function AnswerSection({
<div className="flex flex-col gap-1">
<MarkdownMessage message={content} citationMaps={citationMaps} />
<MessageActions
- message={content} // Keep original message content for copy
+ message={content} // Provide original message; copy path remaps citations
messageId={messageId}
traceId={metadata?.traceId}
feedbackScore={metadata?.feedbackScore}
@@ -74,6 +74,7 @@ export function AnswerSection({
reload={handleReload}
status={status}
visible={showActions}
+ citationMaps={citationMaps}
/>
</div>
)}
diff --git a/components/message-actions.tsx b/components/message-actions.tsx
index 6cc8be37..1f4f921a 100644
--- a/components/message-actions.tsx
+++ b/components/message-actions.tsx
@@ -1,13 +1,15 @@
'use client'
-import { useState } from 'react'
+import { useMemo, useState } from 'react'
import { UseChatHelpers } from '@ai-sdk/react'
import { Copy, ThumbsDown, ThumbsUp } from 'lucide-react'
import { toast } from 'sonner'
+import type { SearchResultItem } from '@/lib/types'
import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
import { cn } from '@/lib/utils'
+import { processCitations } from '@/lib/utils/citation'
import { Button } from './ui/button'
import { ChatShare } from './chat-share'
@@ -24,6 +26,7 @@ interface MessageActionsProps {
className?: string
status?: UseChatHelpers<UIMessage<unknown, UIDataTypes, UITools>>['status']
visible?: boolean
+ citationMaps?: Record<string, Record<number, SearchResultItem>>
}
export function MessageActions({
@@ -36,7 +39,8 @@ export function MessageActions({
enableShare,
className,
status,
- visible = true
+ visible = true,
+ citationMaps
}: MessageActionsProps) {
const [feedbackScore, setFeedbackScore] = useState<number | null>(
initialFeedbackScore ?? null
@@ -44,6 +48,11 @@ export function MessageActions({
const [isSubmittingFeedback, setIsSubmittingFeedback] = useState(false)
const isLoading = status === 'submitted' || status === 'streaming'
+ const mappedMessage = useMemo(() => {
+ if (!message) return ''
+ return processCitations(message, citationMaps || {})
+ }, [message, citationMaps])
+
// Do not render at all when actions should be hidden.
// Rendering while loading is allowed so previous messages keep their actions
// visible even during a new message's streaming.
@@ -52,7 +61,7 @@ export function MessageActions({
}
async function handleCopy() {
- await navigator.clipboard.writeText(message)
+ await navigator.clipboard.writeText(mappedMessage)
toast.success('Message copied to clipboard')
}