Enhance citation handling and UI components
Browse files- Updated citation handling in chat messages to include citation numbers, improving clarity and usability.
- Introduced citation chips in Markdown rendering for better visual representation of citations.
- Refactored CitationRow and TextBlock components to support new citation number functionality, ensuring consistent display across the application.
- Enhanced citation normalization and numbering logic to ensure accurate citation tracking in messages.
- app/prompts.py +4 -2
- frontend/app/globals.css +22 -0
- frontend/components/chat-message.tsx +34 -4
- frontend/components/markdown-block.tsx +49 -7
- frontend/lib/chat-ui.ts +41 -0
app/prompts.py
CHANGED
|
@@ -140,8 +140,10 @@ ANSWERING_RULES = """## Answering rules
|
|
| 140 |
introduces or explains the code instead.
|
| 141 |
- Do not put all citations only in a final "Sources" section. A short sources
|
| 142 |
recap is okay only if the answer already has inline citations.
|
| 143 |
-
-
|
| 144 |
-
|
|
|
|
|
|
|
| 145 |
- If retrieval results are weak or missing, say the topic is not well
|
| 146 |
covered by the current knowledge base rather than guessing.
|
| 147 |
- Synthesize retrieved material into a clear teaching explanation. Do not
|
|
|
|
| 140 |
introduces or explains the code instead.
|
| 141 |
- Do not put all citations only in a final "Sources" section. A short sources
|
| 142 |
recap is okay only if the answer already has inline citations.
|
| 143 |
+
- Never cite `wiki/` or `generated/` KB paths: they are navigation indexes,
|
| 144 |
+
not sources, and they do not resolve to citable source cards. When a wiki
|
| 145 |
+
page led you to a claim, cite the underlying `raw/` page it references
|
| 146 |
+
(open it with `run_kb_command` if you have not already).
|
| 147 |
- If retrieval results are weak or missing, say the topic is not well
|
| 148 |
covered by the current knowledge base rather than guessing.
|
| 149 |
- Synthesize retrieved material into a clear teaching explanation. Do not
|
frontend/app/globals.css
CHANGED
|
@@ -386,6 +386,28 @@ textarea {
|
|
| 386 |
text-underline-offset: 0.14em;
|
| 387 |
}
|
| 388 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
.markdown-block hr {
|
| 390 |
border: 0;
|
| 391 |
border-top: 1px solid var(--hr-color);
|
|
|
|
| 386 |
text-underline-offset: 0.14em;
|
| 387 |
}
|
| 388 |
|
| 389 |
+
.markdown-block a.citation-chip {
|
| 390 |
+
display: inline-flex;
|
| 391 |
+
align-items: center;
|
| 392 |
+
justify-content: center;
|
| 393 |
+
min-width: 1.05rem;
|
| 394 |
+
height: 1.05rem;
|
| 395 |
+
margin-left: 0.18rem;
|
| 396 |
+
padding: 0 0.28rem;
|
| 397 |
+
border-radius: 9999px;
|
| 398 |
+
background: var(--accent-faint);
|
| 399 |
+
color: var(--accent);
|
| 400 |
+
font-size: 10px;
|
| 401 |
+
font-weight: 650;
|
| 402 |
+
line-height: 1;
|
| 403 |
+
text-decoration: none;
|
| 404 |
+
vertical-align: 0.32em;
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
.markdown-block a.citation-chip:hover {
|
| 408 |
+
background: var(--surface-strong);
|
| 409 |
+
}
|
| 410 |
+
|
| 411 |
.markdown-block hr {
|
| 412 |
border: 0;
|
| 413 |
border-top: 1px solid var(--hr-color);
|
frontend/components/chat-message.tsx
CHANGED
|
@@ -37,6 +37,8 @@ import type {
|
|
| 37 |
} from "@/lib/chat-ui";
|
| 38 |
import {
|
| 39 |
buildActivityItems,
|
|
|
|
|
|
|
| 40 |
getMessageCitations,
|
| 41 |
getOrderedMessageBlocks,
|
| 42 |
prettifyToolName,
|
|
@@ -77,6 +79,7 @@ export function ChatMessage({
|
|
| 77 |
const isAssistant = message.role === "assistant";
|
| 78 |
const contentBlocks = getOrderedMessageBlocks(message);
|
| 79 |
const citations = isAssistant && !isStreaming ? getMessageCitations(message) : [];
|
|
|
|
| 80 |
const editTextareaRef = useRef<HTMLTextAreaElement>(null);
|
| 81 |
|
| 82 |
useEffect(() => {
|
|
@@ -145,10 +148,16 @@ export function ChatMessage({
|
|
| 145 |
key={block.key}
|
| 146 |
block={block}
|
| 147 |
isActive={isActive}
|
|
|
|
| 148 |
/>
|
| 149 |
);
|
| 150 |
})}
|
| 151 |
-
{citations.length > 0 ?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
</div>
|
| 153 |
)}
|
| 154 |
</article>
|
|
@@ -280,14 +289,16 @@ function MessageActionButton({
|
|
| 280 |
function ContentBlock({
|
| 281 |
block,
|
| 282 |
isActive = false,
|
|
|
|
| 283 |
}: {
|
| 284 |
block: TutorMessageBlock;
|
| 285 |
isActive?: boolean;
|
|
|
|
| 286 |
}) {
|
| 287 |
if (block.kind === "activity") {
|
| 288 |
return <ActivityPanel parts={block.parts} isActive={isActive} />;
|
| 289 |
}
|
| 290 |
-
return <TextBlock parts={block.parts} />;
|
| 291 |
}
|
| 292 |
|
| 293 |
function ActivityPanel({
|
|
@@ -601,13 +612,20 @@ function formatToolResultSummary({
|
|
| 601 |
return "";
|
| 602 |
}
|
| 603 |
|
| 604 |
-
function TextBlock({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 605 |
return (
|
| 606 |
<div className="space-y-3 text-[15px] leading-[1.72] tracking-[-0.012em] text-[var(--ink)]">
|
| 607 |
{parts.map((part, index) => (
|
| 608 |
<MarkdownBlock
|
| 609 |
key={`text-${index}`}
|
| 610 |
className="text-[15px] leading-[1.72] tracking-[-0.012em] text-[var(--ink)]"
|
|
|
|
| 611 |
>
|
| 612 |
{part.text ?? ""}
|
| 613 |
</MarkdownBlock>
|
|
@@ -651,7 +669,13 @@ const CITATION_KIND_META: Record<
|
|
| 651 |
doc: { icon: BookOpen, label: "Docs" },
|
| 652 |
};
|
| 653 |
|
| 654 |
-
function CitationRow({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 655 |
return (
|
| 656 |
<section className="mt-2 space-y-2 border-t border-[var(--line)] pt-3">
|
| 657 |
<div className="inline-flex items-center gap-1.5 text-[10px] font-semibold uppercase tracking-[0.12em] text-[var(--muted)]">
|
|
@@ -665,6 +689,7 @@ function CitationRow({ citations }: { citations: MessageCitation[] }) {
|
|
| 665 |
{citations.map((citation, index) => {
|
| 666 |
const meta = CITATION_KIND_META[citation.kind];
|
| 667 |
const Icon = meta.icon;
|
|
|
|
| 668 |
return (
|
| 669 |
<a
|
| 670 |
key={`${citation.kind}-${citation.url}-${index}`}
|
|
@@ -674,6 +699,11 @@ function CitationRow({ citations }: { citations: MessageCitation[] }) {
|
|
| 674 |
title={`${meta.label}${citation.sublabel ? ` · ${citation.sublabel}` : ""}`}
|
| 675 |
className="group inline-flex max-w-full items-center gap-1.5 rounded-full border border-[var(--line-strong)] bg-[var(--accent-faint)] px-2.5 py-1.5 text-xs font-medium text-[var(--accent)] transition hover:border-[var(--accent)] hover:bg-[var(--surface-strong)]"
|
| 676 |
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 677 |
<Icon className="h-3.5 w-3.5 shrink-0" />
|
| 678 |
<span className="truncate">{citation.label}</span>
|
| 679 |
<ExternalLink className="h-3 w-3 shrink-0 opacity-60 transition group-hover:opacity-100" />
|
|
|
|
| 37 |
} from "@/lib/chat-ui";
|
| 38 |
import {
|
| 39 |
buildActivityItems,
|
| 40 |
+
buildCitationNumbers,
|
| 41 |
+
citationNumberFor,
|
| 42 |
getMessageCitations,
|
| 43 |
getOrderedMessageBlocks,
|
| 44 |
prettifyToolName,
|
|
|
|
| 79 |
const isAssistant = message.role === "assistant";
|
| 80 |
const contentBlocks = getOrderedMessageBlocks(message);
|
| 81 |
const citations = isAssistant && !isStreaming ? getMessageCitations(message) : [];
|
| 82 |
+
const citationNumbers = isAssistant ? buildCitationNumbers(message) : undefined;
|
| 83 |
const editTextareaRef = useRef<HTMLTextAreaElement>(null);
|
| 84 |
|
| 85 |
useEffect(() => {
|
|
|
|
| 148 |
key={block.key}
|
| 149 |
block={block}
|
| 150 |
isActive={isActive}
|
| 151 |
+
citationNumbers={citationNumbers}
|
| 152 |
/>
|
| 153 |
);
|
| 154 |
})}
|
| 155 |
+
{citations.length > 0 ? (
|
| 156 |
+
<CitationRow
|
| 157 |
+
citations={citations}
|
| 158 |
+
citationNumbers={citationNumbers}
|
| 159 |
+
/>
|
| 160 |
+
) : null}
|
| 161 |
</div>
|
| 162 |
)}
|
| 163 |
</article>
|
|
|
|
| 289 |
function ContentBlock({
|
| 290 |
block,
|
| 291 |
isActive = false,
|
| 292 |
+
citationNumbers,
|
| 293 |
}: {
|
| 294 |
block: TutorMessageBlock;
|
| 295 |
isActive?: boolean;
|
| 296 |
+
citationNumbers?: Map<string, number>;
|
| 297 |
}) {
|
| 298 |
if (block.kind === "activity") {
|
| 299 |
return <ActivityPanel parts={block.parts} isActive={isActive} />;
|
| 300 |
}
|
| 301 |
+
return <TextBlock parts={block.parts} citationNumbers={citationNumbers} />;
|
| 302 |
}
|
| 303 |
|
| 304 |
function ActivityPanel({
|
|
|
|
| 612 |
return "";
|
| 613 |
}
|
| 614 |
|
| 615 |
+
function TextBlock({
|
| 616 |
+
parts,
|
| 617 |
+
citationNumbers,
|
| 618 |
+
}: {
|
| 619 |
+
parts: TutorMessagePart[];
|
| 620 |
+
citationNumbers?: Map<string, number>;
|
| 621 |
+
}) {
|
| 622 |
return (
|
| 623 |
<div className="space-y-3 text-[15px] leading-[1.72] tracking-[-0.012em] text-[var(--ink)]">
|
| 624 |
{parts.map((part, index) => (
|
| 625 |
<MarkdownBlock
|
| 626 |
key={`text-${index}`}
|
| 627 |
className="text-[15px] leading-[1.72] tracking-[-0.012em] text-[var(--ink)]"
|
| 628 |
+
citationNumbers={citationNumbers}
|
| 629 |
>
|
| 630 |
{part.text ?? ""}
|
| 631 |
</MarkdownBlock>
|
|
|
|
| 669 |
doc: { icon: BookOpen, label: "Docs" },
|
| 670 |
};
|
| 671 |
|
| 672 |
+
function CitationRow({
|
| 673 |
+
citations,
|
| 674 |
+
citationNumbers,
|
| 675 |
+
}: {
|
| 676 |
+
citations: MessageCitation[];
|
| 677 |
+
citationNumbers?: Map<string, number>;
|
| 678 |
+
}) {
|
| 679 |
return (
|
| 680 |
<section className="mt-2 space-y-2 border-t border-[var(--line)] pt-3">
|
| 681 |
<div className="inline-flex items-center gap-1.5 text-[10px] font-semibold uppercase tracking-[0.12em] text-[var(--muted)]">
|
|
|
|
| 689 |
{citations.map((citation, index) => {
|
| 690 |
const meta = CITATION_KIND_META[citation.kind];
|
| 691 |
const Icon = meta.icon;
|
| 692 |
+
const number = citationNumberFor(citationNumbers, citation.url);
|
| 693 |
return (
|
| 694 |
<a
|
| 695 |
key={`${citation.kind}-${citation.url}-${index}`}
|
|
|
|
| 699 |
title={`${meta.label}${citation.sublabel ? ` · ${citation.sublabel}` : ""}`}
|
| 700 |
className="group inline-flex max-w-full items-center gap-1.5 rounded-full border border-[var(--line-strong)] bg-[var(--accent-faint)] px-2.5 py-1.5 text-xs font-medium text-[var(--accent)] transition hover:border-[var(--accent)] hover:bg-[var(--surface-strong)]"
|
| 701 |
>
|
| 702 |
+
{number !== undefined ? (
|
| 703 |
+
<span className="shrink-0 rounded-full bg-[var(--surface)] px-1.5 text-[10px] font-semibold text-[var(--accent)]">
|
| 704 |
+
{number}
|
| 705 |
+
</span>
|
| 706 |
+
) : null}
|
| 707 |
<Icon className="h-3.5 w-3.5 shrink-0" />
|
| 708 |
<span className="truncate">{citation.label}</span>
|
| 709 |
<ExternalLink className="h-3 w-3 shrink-0 opacity-60 transition group-hover:opacity-100" />
|
frontend/components/markdown-block.tsx
CHANGED
|
@@ -1,15 +1,38 @@
|
|
|
|
|
| 1 |
import ReactMarkdown from "react-markdown";
|
| 2 |
import rehypeHighlight from "rehype-highlight";
|
| 3 |
import remarkGfm from "remark-gfm";
|
|
|
|
| 4 |
|
| 5 |
type MarkdownBlockProps = {
|
| 6 |
children: string;
|
| 7 |
className?: string;
|
|
|
|
| 8 |
};
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
export function MarkdownBlock({
|
| 11 |
children,
|
| 12 |
className = "",
|
|
|
|
| 13 |
}: MarkdownBlockProps) {
|
| 14 |
return (
|
| 15 |
<div className={`markdown-block ${className}`.trim()}>
|
|
@@ -17,13 +40,32 @@ export function MarkdownBlock({
|
|
| 17 |
remarkPlugins={[remarkGfm]}
|
| 18 |
rehypePlugins={[[rehypeHighlight, { detect: true, ignoreMissing: true }]]}
|
| 19 |
components={{
|
| 20 |
-
a: ({ ...props }) =>
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
}}
|
| 28 |
>
|
| 29 |
{children}
|
|
|
|
| 1 |
+
import type { ReactNode } from "react";
|
| 2 |
import ReactMarkdown from "react-markdown";
|
| 3 |
import rehypeHighlight from "rehype-highlight";
|
| 4 |
import remarkGfm from "remark-gfm";
|
| 5 |
+
import { citationNumberFor } from "@/lib/chat-ui";
|
| 6 |
|
| 7 |
type MarkdownBlockProps = {
|
| 8 |
children: string;
|
| 9 |
className?: string;
|
| 10 |
+
citationNumbers?: Map<string, number>;
|
| 11 |
};
|
| 12 |
|
| 13 |
+
// Above this length the link text is a claim the model wrapped in a link, not
|
| 14 |
+
// a source title; keep the text as prose and append the chip after it.
|
| 15 |
+
const CITATION_TEXT_KEEP_THRESHOLD = 48;
|
| 16 |
+
|
| 17 |
+
function nodeToText(node: ReactNode): string {
|
| 18 |
+
if (typeof node === "string" || typeof node === "number") {
|
| 19 |
+
return String(node);
|
| 20 |
+
}
|
| 21 |
+
if (Array.isArray(node)) {
|
| 22 |
+
return node.map(nodeToText).join("");
|
| 23 |
+
}
|
| 24 |
+
if (node && typeof node === "object" && "props" in node) {
|
| 25 |
+
return nodeToText(
|
| 26 |
+
(node as { props: { children?: ReactNode } }).props.children,
|
| 27 |
+
);
|
| 28 |
+
}
|
| 29 |
+
return "";
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
export function MarkdownBlock({
|
| 33 |
children,
|
| 34 |
className = "",
|
| 35 |
+
citationNumbers,
|
| 36 |
}: MarkdownBlockProps) {
|
| 37 |
return (
|
| 38 |
<div className={`markdown-block ${className}`.trim()}>
|
|
|
|
| 40 |
remarkPlugins={[remarkGfm]}
|
| 41 |
rehypePlugins={[[rehypeHighlight, { detect: true, ignoreMissing: true }]]}
|
| 42 |
components={{
|
| 43 |
+
a: ({ children: linkChildren, href, ...props }) => {
|
| 44 |
+
const number = citationNumberFor(citationNumbers, href);
|
| 45 |
+
if (number !== undefined) {
|
| 46 |
+
const label = nodeToText(linkChildren).trim();
|
| 47 |
+
const keepText = label.length > CITATION_TEXT_KEEP_THRESHOLD;
|
| 48 |
+
return (
|
| 49 |
+
<>
|
| 50 |
+
{keepText ? <span>{label}</span> : null}
|
| 51 |
+
<a
|
| 52 |
+
href={href}
|
| 53 |
+
target="_blank"
|
| 54 |
+
rel="noreferrer"
|
| 55 |
+
title={label}
|
| 56 |
+
className="citation-chip"
|
| 57 |
+
>
|
| 58 |
+
{number}
|
| 59 |
+
</a>
|
| 60 |
+
</>
|
| 61 |
+
);
|
| 62 |
+
}
|
| 63 |
+
return (
|
| 64 |
+
<a {...props} href={href} target="_blank" rel="noreferrer">
|
| 65 |
+
{linkChildren}
|
| 66 |
+
</a>
|
| 67 |
+
);
|
| 68 |
+
},
|
| 69 |
}}
|
| 70 |
>
|
| 71 |
{children}
|
frontend/lib/chat-ui.ts
CHANGED
|
@@ -180,6 +180,47 @@ export function getMessageCitations(message: TutorMessage): MessageCitation[] {
|
|
| 180 |
return citations;
|
| 181 |
}
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
function cleanTitle(raw: string | undefined) {
|
| 184 |
const trimmed = raw?.trim();
|
| 185 |
if (!trimmed) {
|
|
|
|
| 180 |
return citations;
|
| 181 |
}
|
| 182 |
|
| 183 |
+
const MARKDOWN_LINK_PATTERN = /(!?)\[([^\]]*)\]\(\s*<?([^)\s>]+)>?(?:\s+"[^"]*")?\s*\)/g;
|
| 184 |
+
|
| 185 |
+
function normalizeCitationUrl(url: string) {
|
| 186 |
+
// Mirror the server's normalize_url so inline links and data-source parts
|
| 187 |
+
// resolve to the same key.
|
| 188 |
+
return url.replace(/#.*$/, "").replace(/\/+$/, "");
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
// Number the citation links in a message's answer text by order of first
|
| 192 |
+
// appearance, deduped by normalized URL. The same map drives the inline
|
| 193 |
+
// citation chips and the numbers on the sources row.
|
| 194 |
+
export function buildCitationNumbers(message: TutorMessage): Map<string, number> {
|
| 195 |
+
const numbers = new Map<string, number>();
|
| 196 |
+
for (const part of message.parts) {
|
| 197 |
+
if (!("type" in part) || part.type !== "text") {
|
| 198 |
+
continue;
|
| 199 |
+
}
|
| 200 |
+
const text = (part as TutorMessagePart).text ?? "";
|
| 201 |
+
for (const match of text.matchAll(MARKDOWN_LINK_PATTERN)) {
|
| 202 |
+
if (match[1] === "!") {
|
| 203 |
+
continue;
|
| 204 |
+
}
|
| 205 |
+
const key = normalizeCitationUrl(match[3] ?? "");
|
| 206 |
+
if (key && !numbers.has(key)) {
|
| 207 |
+
numbers.set(key, numbers.size + 1);
|
| 208 |
+
}
|
| 209 |
+
}
|
| 210 |
+
}
|
| 211 |
+
return numbers;
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
export function citationNumberFor(
|
| 215 |
+
numbers: Map<string, number> | undefined,
|
| 216 |
+
url: string | undefined,
|
| 217 |
+
) {
|
| 218 |
+
if (!numbers || !url) {
|
| 219 |
+
return undefined;
|
| 220 |
+
}
|
| 221 |
+
return numbers.get(normalizeCitationUrl(url));
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
function cleanTitle(raw: string | undefined) {
|
| 225 |
const trimmed = raw?.trim();
|
| 226 |
if (!trimmed) {
|