Files
deer-flow/frontend/src/core/citations/utils.ts
LofiSu 4f9d1d524e feat(frontend): unify citation logic and prevent half-finished citations
- Add SafeCitationContent as single component for citation-aware body:
  useParsedCitations + shouldShowCitationLoading; show loading until
  citations complete, then render body with createCitationMarkdownComponents.
  Supports optional remarkPlugins, rehypePlugins, isHuman, img.

- Refactor MessageListItem: assistant message body now uses
  SafeCitationContent only; remove duplicate useParsedCitations,
  shouldShowCitationLoading, createCitationMarkdownComponents and
  CitationsLoadingIndicator logic. Human messages keep plain
  AIElementMessageResponse (no citation parsing).

- Use SafeCitationContent for clarification, present-files (message-list),
  thinking steps and write_file loading (message-group), subtask result
  (subtask-card). Artifact markdown preview keeps same guard
  (shouldShowCitationLoading) with ArtifactFilePreview.

- Unify loading condition: shouldShowCitationLoading(rawContent,
  cleanContent, isLoading) is the single source of truth. Show loading when
  (isLoading && hasCitationsBlock(rawContent)) or when
  (hasCitationsBlock(rawContent) && hasUnreplacedCitationRefs(cleanContent))
  so Pro/Ultra modes also show "loading citations" and half-finished
  [cite-N] never appear.

- message-group write_file: replace hasCitationsBlock + threadIsLoading
  with shouldShowCitationLoading(fileContent, cleanContent,
  threadIsLoading && isLast) for consistency.

- citations/utils: parse incomplete <citations> during streaming;
  remove isCitationsBlockIncomplete; keep hasUnreplacedCitationRefs
  internal; document display rule in file header.

Co-authored-by: Cursor <cursoragent@cursor.com>

---
feat(前端): 统一引用逻辑并杜绝半成品引用

- 新增 SafeCitationContent 作为引用正文的唯一出口:内部使用
  useParsedCitations + shouldShowCitationLoading,在引用未就绪时只显示
  「正在整理引用」,就绪后用 createCitationMarkdownComponents 渲染正文;
  支持可选 remarkPlugins、rehypePlugins、isHuman、img。

- 重构 MessageListItem:助手消息正文仅通过 SafeCitationContent 渲染,
  删除重复的 useParsedCitations、shouldShowCitationLoading、
  createCitationMarkdownComponents、CitationsLoadingIndicator 等逻辑;
  用户消息仍用 AIElementMessageResponse,不做引用解析。

- 澄清、present-files(message-list)、思考步骤与 write_file 加载
  (message-group)、子任务结果(subtask-card)均使用
  SafeCitationContent;Artifact 的 markdown 预览仍用同一 guard
  shouldShowCitationLoading,正文由 ArtifactFilePreview 渲染。

- 统一加载条件:shouldShowCitationLoading(rawContent, cleanContent,
  isLoading) 为唯一判断。在「流式中且已有引用块」或「有引用块且
  cleanContent 中仍有未替换的 [cite-N]」时仅显示加载,从而在 Pro/Ultra
  下也能看到「正在整理引用」,且永不出现半成品 [cite-N]。

- message-group 的 write_file:用 shouldShowCitationLoading(
  fileContent, cleanContent, threadIsLoading && isLast) 替代
  hasCitationsBlock + threadIsLoading,与其他场景一致。

- citations/utils:流式时解析未闭合的 <citations>;移除
  isCitationsBlockIncomplete;hasUnreplacedCitationRefs 保持内部使用;
  在文件头注释中说明展示规则。
2026-02-09 15:01:51 +08:00

245 lines
7.6 KiB
TypeScript

/**
* Citation parsing and display helpers.
* Display rule: never show half-finished citations. Use shouldShowCitationLoading
* and show only the loading indicator until the block is complete and all
* [cite-N] refs are replaced.
*/
/**
* Citation data structure representing a source reference
*/
export interface Citation {
id: string;
title: string;
url: string;
snippet: string;
}
/**
* Result of parsing citations from content
*/
export interface ParseCitationsResult {
citations: Citation[];
cleanContent: string;
}
/**
* Parse citation lines (one JSON object per line) into Citation array.
* Deduplicates by URL. Used for both complete and incomplete (streaming) blocks.
*/
function parseCitationLines(
blockContent: string,
seenUrls: Set<string>,
): Citation[] {
const out: Citation[] = [];
const lines = blockContent.split("\n");
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed?.startsWith("{")) continue;
try {
const citation = JSON.parse(trimmed) as Citation;
if (citation.id && citation.url && !seenUrls.has(citation.url)) {
seenUrls.add(citation.url);
out.push({
id: citation.id,
title: citation.title || "",
url: citation.url,
snippet: citation.snippet || "",
});
}
} catch {
// Skip invalid JSON lines - can happen during streaming
}
}
return out;
}
/**
* Parse citations block from message content.
* Shared by all modes (Flash / Thinking / Pro / Ultra); supports incomplete
* <citations> blocks during SSE streaming (parses whatever complete JSON lines
* have arrived so far so [cite-N] can be linked progressively).
*
* The citations block format:
* <citations>
* {"id": "cite-1", "title": "Page Title", "url": "https://example.com", "snippet": "Description"}
* {"id": "cite-2", "title": "Another Page", "url": "https://example2.com", "snippet": "Description"}
* </citations>
*
* @param content - The raw message content that may contain a citations block
* @returns Object containing parsed citations array and content with citations block removed
*/
export function parseCitations(content: string): ParseCitationsResult {
if (!content) {
return { citations: [], cleanContent: content };
}
const citations: Citation[] = [];
const seenUrls = new Set<string>();
// 1) Complete blocks: <citations>...</citations>
const citationsRegex = /<citations>([\s\S]*?)<\/citations>/g;
let match;
while ((match = citationsRegex.exec(content)) !== null) {
citations.push(...parseCitationLines(match[1] ?? "", seenUrls));
}
// 2) Incomplete block during streaming: <citations>... (no closing tag yet)
if (content.includes("<citations>") && !content.includes("</citations>")) {
const openMatch = content.match(/<citations>([\s\S]*)$/);
if (openMatch?.[1] != null) {
citations.push(...parseCitationLines(openMatch[1], seenUrls));
}
}
let cleanContent = removeCitationsBlocks(content);
// Convert [cite-N] references to markdown links
// Example: [cite-1] -> [Title](url)
if (citations.length > 0) {
// Build a map from citation id to citation object
const idMap = new Map<string, Citation>();
for (const citation of citations) {
idMap.set(citation.id, citation);
}
// Replace all [cite-N] patterns with markdown links
cleanContent = cleanContent.replace(/\[cite-(\d+)\]/g, (match, num) => {
const citeId = `cite-${num}`;
const citation = idMap.get(citeId);
if (citation) {
// Use title if available, otherwise use domain
const linkText = citation.title || extractDomainFromUrl(citation.url);
return `[${linkText}](${citation.url})`;
}
// If citation not found, keep the original text
return match;
});
}
return { citations, cleanContent };
}
/**
* Build a map from URL to Citation for quick lookup
*
* @param citations - Array of citations
* @returns Map with URL as key and Citation as value
*/
export function buildCitationMap(
citations: Citation[],
): Map<string, Citation> {
const map = new Map<string, Citation>();
for (const citation of citations) {
map.set(citation.url, citation);
}
return map;
}
/**
* Whether the URL is external (http/https).
*/
export function isExternalUrl(url: string): boolean {
return url.startsWith("http://") || url.startsWith("https://");
}
/**
* Build a synthetic Citation from a link (e.g. in artifact markdown without <citations> block).
*/
export function syntheticCitationFromLink(href: string, title: string): Citation {
return {
id: `artifact-cite-${href}`,
title: title || href,
url: href,
snippet: "",
};
}
/**
* Extract the domain name from a URL for display
*
* @param url - Full URL string
* @returns Domain name or the original URL if parsing fails
*/
export function extractDomainFromUrl(url: string): string {
try {
const urlObj = new URL(url);
// Remove 'www.' prefix if present
return urlObj.hostname.replace(/^www\./, "");
} catch {
return url;
}
}
/**
* Remove all <citations> blocks from content (complete and incomplete).
* Does not remove [cite-N] or markdown links; use removeAllCitations for that.
*/
export function removeCitationsBlocks(content: string): string {
if (!content) return content;
let result = content.replace(/<citations>[\s\S]*?<\/citations>/g, "").trim();
if (result.includes("<citations>")) {
result = result.replace(/<citations>[\s\S]*$/g, "").trim();
}
return result;
}
/**
* Whether content contains a <citations> block (open tag).
*/
export function hasCitationsBlock(content: string): boolean {
return Boolean(content?.includes("<citations>"));
}
/** Pattern for [cite-1], [cite-2], ... that should be replaced by parseCitations. */
const UNREPLACED_CITE_REF = /\[cite-\d+\]/;
/**
* Whether cleanContent still contains unreplaced [cite-N] refs (half-finished citations).
* When true, callers must not render this content and should show loading instead.
*/
export function hasUnreplacedCitationRefs(cleanContent: string): boolean {
return Boolean(cleanContent && UNREPLACED_CITE_REF.test(cleanContent));
}
/**
* Single source of truth: true when body must not be rendered (show loading instead).
* Use after parseCitations: pass raw content, parsed cleanContent, and isLoading.
* When streaming and any citation block is present, show loading so the indicator
* is visible in all modes (Pro/Ultra often receive complete blocks in one chunk).
*/
export function shouldShowCitationLoading(
rawContent: string,
cleanContent: string,
isLoading: boolean,
): boolean {
return (
(isLoading && hasCitationsBlock(rawContent)) ||
(hasCitationsBlock(rawContent) && hasUnreplacedCitationRefs(cleanContent))
);
}
/**
* Strip citation markdown links from already-cleaned content (from parseCitations).
* Use when you already have ParseCitationsResult to avoid parsing twice.
*/
export function contentWithoutCitationsFromParsed(
parsed: ParseCitationsResult,
): string {
const citationUrls = new Set(parsed.citations.map((c) => c.url));
const withoutLinks = parsed.cleanContent.replace(
/\[([^\]]+)\]\(([^)]+)\)/g,
(fullMatch, _text, url) => (citationUrls.has(url) ? "" : fullMatch),
);
return withoutLinks.replace(/\n{3,}/g, "\n\n").trim();
}
/**
* Remove ALL citations from content (blocks, [cite-N], and citation links).
* Used for copy/download. For display you typically use parseCitations/useParsedCitations.
*/
export function removeAllCitations(content: string): string {
if (!content) return content;
return contentWithoutCitationsFromParsed(parseCitations(content));
}