mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-14 18:54:46 +08:00
feat: add inline citations and thread management features
Citations: - Add citations parsing utilities for extracting source references from AI responses - Render inline citations as hover card badges in message content - Display citation cards with title, URL, and description on hover - Add citation badge rendering in artifact markdown preview - Update prompt to guide AI to output citations in correct format Thread Management: - Add rename functionality for chat threads with dialog UI - Add share functionality to copy thread link to clipboard - Share links use Vercel URL for production accessibility - Add useRenameThread hook for thread title updates i18n: - Add translations for rename, share, cancel, save, and linkCopied Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
8
frontend/src/core/citations/index.ts
Normal file
8
frontend/src/core/citations/index.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export {
|
||||
parseCitations,
|
||||
buildCitationMap,
|
||||
extractDomainFromUrl,
|
||||
isCitationsBlockIncomplete,
|
||||
} from "./utils";
|
||||
|
||||
export type { Citation, ParseCitationsResult } from "./utils";
|
||||
124
frontend/src/core/citations/utils.ts
Normal file
124
frontend/src/core/citations/utils.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* 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 citations block from message content.
|
||||
*
|
||||
* 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 };
|
||||
}
|
||||
|
||||
// Match the citations block at the start of content (with possible leading whitespace)
|
||||
const citationsRegex = /^\s*<citations>([\s\S]*?)<\/citations>/;
|
||||
const match = citationsRegex.exec(content);
|
||||
|
||||
if (!match) {
|
||||
return { citations: [], cleanContent: content };
|
||||
}
|
||||
|
||||
const citationsBlock = match[1] ?? "";
|
||||
const citations: Citation[] = [];
|
||||
|
||||
// Parse each line as JSON
|
||||
const lines = citationsBlock.split("\n");
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (trimmed?.startsWith("{")) {
|
||||
try {
|
||||
const citation = JSON.parse(trimmed) as Citation;
|
||||
// Validate required fields
|
||||
if (citation.id && citation.url) {
|
||||
citations.push({
|
||||
id: citation.id,
|
||||
title: citation.title || "",
|
||||
url: citation.url,
|
||||
snippet: citation.snippet || "",
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
// Skip invalid JSON lines - this can happen during streaming
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the citations block from content
|
||||
const cleanContent = content.replace(citationsRegex, "").trim();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if content is still receiving the citations block (streaming)
|
||||
* This helps determine if we should wait before parsing
|
||||
*
|
||||
* @param content - The current content being streamed
|
||||
* @returns true if citations block appears to be incomplete
|
||||
*/
|
||||
export function isCitationsBlockIncomplete(content: string): boolean {
|
||||
if (!content) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if we have an opening tag but no closing tag
|
||||
const hasOpenTag = content.includes("<citations>");
|
||||
const hasCloseTag = content.includes("</citations>");
|
||||
|
||||
return hasOpenTag && !hasCloseTag;
|
||||
}
|
||||
@@ -11,6 +11,8 @@ export const enUS: Translations = {
|
||||
home: "Home",
|
||||
settings: "Settings",
|
||||
delete: "Delete",
|
||||
rename: "Rename",
|
||||
share: "Share",
|
||||
openInNewWindow: "Open in new window",
|
||||
close: "Close",
|
||||
more: "More",
|
||||
@@ -24,6 +26,8 @@ export const enUS: Translations = {
|
||||
loading: "Loading...",
|
||||
code: "Code",
|
||||
preview: "Preview",
|
||||
cancel: "Cancel",
|
||||
save: "Save",
|
||||
},
|
||||
|
||||
// Welcome
|
||||
@@ -38,6 +42,7 @@ export const enUS: Translations = {
|
||||
copyToClipboard: "Copy to clipboard",
|
||||
copiedToClipboard: "Copied to clipboard",
|
||||
failedToCopyToClipboard: "Failed to copy to clipboard",
|
||||
linkCopied: "Link copied to clipboard",
|
||||
},
|
||||
|
||||
// Input Box
|
||||
|
||||
@@ -9,6 +9,8 @@ export interface Translations {
|
||||
home: string;
|
||||
settings: string;
|
||||
delete: string;
|
||||
rename: string;
|
||||
share: string;
|
||||
openInNewWindow: string;
|
||||
close: string;
|
||||
more: string;
|
||||
@@ -22,6 +24,8 @@ export interface Translations {
|
||||
loading: string;
|
||||
code: string;
|
||||
preview: string;
|
||||
cancel: string;
|
||||
save: string;
|
||||
};
|
||||
|
||||
// Welcome
|
||||
@@ -35,6 +39,7 @@ export interface Translations {
|
||||
copyToClipboard: string;
|
||||
copiedToClipboard: string;
|
||||
failedToCopyToClipboard: string;
|
||||
linkCopied: string;
|
||||
};
|
||||
|
||||
// Input Box
|
||||
|
||||
@@ -11,6 +11,8 @@ export const zhCN: Translations = {
|
||||
home: "首页",
|
||||
settings: "设置",
|
||||
delete: "删除",
|
||||
rename: "重命名",
|
||||
share: "分享",
|
||||
openInNewWindow: "在新窗口打开",
|
||||
close: "关闭",
|
||||
more: "更多",
|
||||
@@ -24,6 +26,8 @@ export const zhCN: Translations = {
|
||||
loading: "加载中...",
|
||||
code: "代码",
|
||||
preview: "预览",
|
||||
cancel: "取消",
|
||||
save: "保存",
|
||||
},
|
||||
|
||||
// Welcome
|
||||
@@ -38,6 +42,7 @@ export const zhCN: Translations = {
|
||||
copyToClipboard: "复制到剪贴板",
|
||||
copiedToClipboard: "已复制到剪贴板",
|
||||
failedToCopyToClipboard: "复制到剪贴板失败",
|
||||
linkCopied: "链接已复制到剪贴板",
|
||||
},
|
||||
|
||||
// Input Box
|
||||
|
||||
@@ -186,3 +186,43 @@ export function useDeleteThread() {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useRenameThread() {
|
||||
const queryClient = useQueryClient();
|
||||
const apiClient = getAPIClient();
|
||||
return useMutation({
|
||||
mutationFn: async ({
|
||||
threadId,
|
||||
title,
|
||||
}: {
|
||||
threadId: string;
|
||||
title: string;
|
||||
}) => {
|
||||
await apiClient.threads.update(threadId, {
|
||||
metadata: { title },
|
||||
});
|
||||
},
|
||||
onSuccess(_, { threadId, title }) {
|
||||
queryClient.setQueriesData(
|
||||
{
|
||||
queryKey: ["threads", "search"],
|
||||
exact: false,
|
||||
},
|
||||
(oldData: Array<AgentThread>) => {
|
||||
return oldData.map((t) => {
|
||||
if (t.thread_id === threadId) {
|
||||
return {
|
||||
...t,
|
||||
metadata: {
|
||||
...t.metadata,
|
||||
title,
|
||||
},
|
||||
};
|
||||
}
|
||||
return t;
|
||||
});
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user