来源新增修改

This commit is contained in:
董宏凯 committed 2026-05-22 10:43:12 +08:00
1 parent 7652db4601
commit a01d7123d7
6 files changed
+862 -45

No files matched your search

@@ -37,6 +37,11 @@ import {
DialogTitle,
} from "@/components/ui/dialog";
import { useI18n } from "@/core/i18n/hooks";
import {
normalizeSearchResults,
resolveSearchQuery,
type SearchResultItem,
} from "@/core/messages/search-results";
import {
extractReasoningContentFromMessage,
findToolCallResult,
@@ -216,15 +221,21 @@ function ToolCall({
const { t } = useI18n();
const artifacts = useArtifactsOptional();
if (name === "web_search") {
const searchItems = normalizeSearchResults(result);
if (name === "web_search" || searchItems.length > 0) {
const query = resolveSearchQuery(args, result);
let label: React.ReactNode = t.toolCalls.searchForRelatedInfo;
if (typeof args.query === "string") {
label = t.toolCalls.searchOnWebFor(args.query);
if (query) {
label =
name === "web_search"
? t.toolCalls.searchOnWebFor(query)
: t.toolCalls.searchFor(query);
}
const items = normalizeSearchResults(result);
return (
<ChainOfThoughtStep key={id} label={label} icon={SearchIcon}>
{items.length > 0 && <WebSearchResultsList results={items} />}
{searchItems.length > 0 && (
<WebSearchResultsList results={searchItems} />
)}
</ChainOfThoughtStep>
);
} else if (name === "image_search") {
@@ -501,32 +512,7 @@ function convertToSteps(messages: Message[]): CoTStep[] {
return steps;
}
type WebSearchItem = {
title?: string;
url?: string;
snippet?: string;
content?: string;
published_date?: string;
publishedDate?: string;
date?: string;
};
function normalizeSearchResults(
result: string | Record<string, unknown> | undefined,
): WebSearchItem[] {
if (Array.isArray(result)) {
return result as WebSearchItem[];
}
if (result && typeof result === "object") {
const nested = (result as { results?: unknown }).results;
if (Array.isArray(nested)) {
return nested as WebSearchItem[];
}
}
return [];
}
function WebSearchResultsList({ results }: { results: WebSearchItem[] }) {
function WebSearchResultsList({ results }: { results: SearchResultItem[] }) {
const [open, setOpen] = useState(false);
return (
<Collapsible open={open} onOpenChange={setOpen} className="w-full">
@@ -548,16 +534,16 @@ function WebSearchResultsList({ results }: { results: WebSearchItem[] }) {
);
}
function SearchResultCard({ item }: { item: WebSearchItem }) {
function SearchResultCard({ item }: { item: SearchResultItem }) {
const [open, setOpen] = useState(false);
const content = item.snippet ?? item.content;
const date = item.published_date ?? item.publishedDate ?? item.date;
let source: string | undefined;
let sourceLabel: string | undefined = item.source;
if (item.url) {
try {
source = new URL(item.url).hostname.replace(/^www\./, "");
sourceLabel = new URL(item.url).hostname.replace(/^www\./, "");
} catch {
source = undefined;
sourceLabel = sourceLabel ?? item.url;
}
}
return (
@@ -572,10 +558,10 @@ function SearchResultCard({ item }: { item: WebSearchItem }) {
{item.title}
</div>
)}
{(source || date) && (
{(sourceLabel || date) && (
<div className="text-muted-foreground mt-1 flex items-center gap-2 text-xs">
{source && <span className="truncate">{source}</span>}
{source && date && <span>·</span>}
{sourceLabel && <span className="truncate">{sourceLabel}</span>}
{sourceLabel && date && <span>·</span>}
{date && <span>{date}</span>}
</div>
)}
@@ -591,10 +577,10 @@ function SearchResultCard({ item }: { item: WebSearchItem }) {
<DialogTitle className="pr-6 text-base leading-relaxed">
{item.title ?? "检索结果"}
</DialogTitle>
{(source || date) && (
{(sourceLabel || date) && (
<DialogDescription className="flex items-center gap-2 text-xs">
{source && <span>{source}</span>}
{source && date && <span>·</span>}
{sourceLabel && <span>{sourceLabel}</span>}
{sourceLabel && date && <span>·</span>}
{date && <span>{date}</span>}
</DialogDescription>
)}