|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { |
| 4 | + InlineCitation, |
| 5 | + InlineCitationText, |
| 6 | + InlineCitationCard, |
| 7 | + InlineCitationCardTrigger, |
| 8 | + InlineCitationCardBody, |
| 9 | + InlineCitationCarousel, |
| 10 | + InlineCitationCarouselHeader, |
| 11 | + InlineCitationCarouselContent, |
| 12 | + InlineCitationCarouselItem, |
| 13 | + InlineCitationCarouselPrev, |
| 14 | + InlineCitationCarouselNext, |
| 15 | + InlineCitationCarouselIndex, |
| 16 | + InlineCitationSource, |
| 17 | + InlineCitationQuote, |
| 18 | +} from "@/src/components/ai-elements/inline-citation" |
| 19 | +import type { ReactNode } from "react" |
| 20 | + |
| 21 | +export interface Citation { |
| 22 | + id: string |
| 23 | + number: string |
| 24 | + title: string |
| 25 | + url: string |
| 26 | + description?: string |
| 27 | + quote?: string |
| 28 | +} |
| 29 | + |
| 30 | +interface AgentInlineCitationProps { |
| 31 | + citations: Citation[] |
| 32 | + text: string |
| 33 | +} |
| 34 | + |
| 35 | +export function AgentInlineCitation({ citations, text }: AgentInlineCitationProps) { |
| 36 | + const citation = citations[0] |
| 37 | + if (!citation) return <span>{text}</span> |
| 38 | + |
| 39 | + return ( |
| 40 | + <InlineCitation> |
| 41 | + <InlineCitationText>{text}</InlineCitationText> |
| 42 | + <InlineCitationCard> |
| 43 | + <InlineCitationCardTrigger sources={citations.map((c) => c.url)} /> |
| 44 | + <InlineCitationCardBody> |
| 45 | + <InlineCitationCarousel> |
| 46 | + <InlineCitationCarouselHeader> |
| 47 | + <InlineCitationCarouselPrev /> |
| 48 | + <InlineCitationCarouselNext /> |
| 49 | + <InlineCitationCarouselIndex /> |
| 50 | + </InlineCitationCarouselHeader> |
| 51 | + <InlineCitationCarouselContent> |
| 52 | + {citations.map((c) => ( |
| 53 | + <InlineCitationCarouselItem key={c.id}> |
| 54 | + <InlineCitationSource |
| 55 | + title={c.title} |
| 56 | + url={c.url} |
| 57 | + description={c.description} |
| 58 | + /> |
| 59 | + {c.quote && ( |
| 60 | + <InlineCitationQuote>{c.quote}</InlineCitationQuote> |
| 61 | + )} |
| 62 | + </InlineCitationCarouselItem> |
| 63 | + ))} |
| 64 | + </InlineCitationCarouselContent> |
| 65 | + </InlineCitationCarousel> |
| 66 | + </InlineCitationCardBody> |
| 67 | + </InlineCitationCard> |
| 68 | + </InlineCitation> |
| 69 | + ) |
| 70 | +} |
| 71 | + |
| 72 | +export function parseInlineCitations( |
| 73 | + content: string, |
| 74 | + sources: { url: string; title: string }[] |
| 75 | +): ReactNode[] { |
| 76 | + if (!sources.length) return [content] |
| 77 | + |
| 78 | + const parts: ReactNode[] = [] |
| 79 | + const citationRegex = /\[(\d+)\]/g |
| 80 | + let lastIndex = 0 |
| 81 | + let match |
| 82 | + |
| 83 | + while ((match = citationRegex.exec(content)) !== null) { |
| 84 | + if (match.index > lastIndex) { |
| 85 | + parts.push(content.slice(lastIndex, match.index)) |
| 86 | + } |
| 87 | + |
| 88 | + const citationNumber = match[1] |
| 89 | + const sourceIndex = parseInt(citationNumber, 10) - 1 |
| 90 | + const source = sources[sourceIndex] |
| 91 | + |
| 92 | + if (source) { |
| 93 | + parts.push( |
| 94 | + <AgentInlineCitation |
| 95 | + key={`citation-${match.index}`} |
| 96 | + text={match[0]} |
| 97 | + citations={[ |
| 98 | + { |
| 99 | + id: `cite-${sourceIndex}`, |
| 100 | + number: citationNumber, |
| 101 | + title: source.title, |
| 102 | + url: source.url, |
| 103 | + }, |
| 104 | + ]} |
| 105 | + /> |
| 106 | + ) |
| 107 | + } else { |
| 108 | + parts.push(match[0]) |
| 109 | + } |
| 110 | + |
| 111 | + lastIndex = match.index + match[0].length |
| 112 | + } |
| 113 | + |
| 114 | + if (lastIndex < content.length) { |
| 115 | + parts.push(content.slice(lastIndex)) |
| 116 | + } |
| 117 | + |
| 118 | + return parts.length ? parts : [content] |
| 119 | +} |
0 commit comments