636 lines
24 KiB
TypeScript
636 lines
24 KiB
TypeScript
"use client";
|
|
|
|
import {
|
|
Check,
|
|
Database,
|
|
Folder,
|
|
Globe2,
|
|
Loader2,
|
|
Search,
|
|
} from "lucide-react";
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuCheckboxItem,
|
|
DropdownMenuContent,
|
|
DropdownMenuLabel,
|
|
DropdownMenuSeparator,
|
|
DropdownMenuTrigger,
|
|
} from "@/components/ui/dropdown-menu";
|
|
import { Input } from "@/components/ui/input";
|
|
import { useAssistantKnowledgeAccess } from "@/core/llmwiki/assistant-knowledge-access";
|
|
import { ASSISTANT_KNOWLEDGE_UI_VISIBLE } from "@/core/llmwiki/assistant-knowledge-ui";
|
|
import { userFacingKnowledgeSuggestedQuestions } from "@/core/llmwiki/knowledge-suggested-questions";
|
|
import { cn } from "@/lib/utils";
|
|
import {
|
|
type AssistantKnowledgeBase,
|
|
useAssistantKnowledgeBases,
|
|
} from "@/strategy-components/api/assistant-knowledge";
|
|
import {
|
|
type LlmWikiKnowledgeBase,
|
|
useAgentLlmWikiKnowledgeBases,
|
|
useLlmWikiKnowledgeBases,
|
|
useLlmWikiRuntime,
|
|
} from "@/strategy-components/api/llmwiki";
|
|
|
|
type SelectorSource = "all" | "agent";
|
|
type SelectorVariant = "popover" | "panel";
|
|
const EMPTY_IDS: string[] = [];
|
|
const IGNORE_ASSISTANT_CHANGE = () => undefined;
|
|
|
|
export function buildKnowledgeSpaceQuestions(names: string[]): string[] {
|
|
const cleanNames = names.map((name) => name.trim()).filter(Boolean);
|
|
if (cleanNames.length === 0) return [];
|
|
if (cleanNames.length === 1) {
|
|
const name = cleanNames[0]!;
|
|
return [
|
|
`请概括「${name}」知识空间的主要内容、适用场景和资料边界。`,
|
|
`「${name}」包含哪些核心主题?请按重要性说明。`,
|
|
`请梳理「${name}」中的关键实体、概念及它们之间的关系。`,
|
|
`根据「${name}」,有哪些值得关注的问题、风险或行动建议?`,
|
|
];
|
|
}
|
|
return [
|
|
`请综合所选 ${cleanNames.length} 个知识空间,总结共同主题、差异和互补信息。`,
|
|
`请对比「${cleanNames.slice(0, 3).join("」「")}」中的关键观点。`,
|
|
"请梳理所选知识空间中的关键实体、概念及跨库关系。",
|
|
"所选知识空间中有哪些相互冲突或证据不足的结论?",
|
|
];
|
|
}
|
|
|
|
function groupRows(rows: LlmWikiKnowledgeBase[]) {
|
|
return [
|
|
{
|
|
key: "personal",
|
|
label: "我的知识空间",
|
|
rows: rows.filter((item) => item.publication_status !== "published" || item.is_owner),
|
|
},
|
|
{
|
|
key: "public",
|
|
label: "公共知识空间",
|
|
rows: rows.filter((item) => item.publication_status === "published" && !item.is_owner),
|
|
},
|
|
].filter((group) => group.rows.length > 0);
|
|
}
|
|
|
|
function SpaceIcon({ item }: { item: LlmWikiKnowledgeBase }) {
|
|
return (
|
|
<span
|
|
className={cn(
|
|
"flex size-7 shrink-0 items-center justify-center rounded-md",
|
|
item.publication_status === "published"
|
|
? "bg-emerald-500/10 text-emerald-600 dark:text-emerald-400"
|
|
: "bg-primary/10 text-primary",
|
|
)}
|
|
>
|
|
{item.publication_status === "published" ? (
|
|
<Globe2 className="size-3.5" />
|
|
) : (
|
|
<Folder className="size-3.5" />
|
|
)}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
function AssistantSpaceIcon({ item }: { item: AssistantKnowledgeBase }) {
|
|
return (
|
|
<span
|
|
className={cn(
|
|
"flex size-7 shrink-0 items-center justify-center rounded-md",
|
|
item.is_global ? "bg-amber-500/10 text-amber-600 dark:text-amber-400" : "bg-sky-500/10 text-sky-600 dark:text-sky-400",
|
|
)}
|
|
>
|
|
<Database className="size-3.5" />
|
|
</span>
|
|
);
|
|
}
|
|
|
|
function SelectionContent({
|
|
rows,
|
|
assistantRows,
|
|
value,
|
|
assistantValue,
|
|
onChange,
|
|
onAssistantChange,
|
|
search,
|
|
onSearchChange,
|
|
loading,
|
|
error,
|
|
source,
|
|
variant,
|
|
readOnly,
|
|
}: {
|
|
rows: LlmWikiKnowledgeBase[];
|
|
assistantRows: AssistantKnowledgeBase[];
|
|
value: string[];
|
|
assistantValue: string[];
|
|
onChange: (value: string[]) => void;
|
|
onAssistantChange: (value: string[]) => void;
|
|
search: string;
|
|
onSearchChange: (value: string) => void;
|
|
loading: boolean;
|
|
error: unknown;
|
|
source: SelectorSource;
|
|
variant: SelectorVariant;
|
|
readOnly: boolean;
|
|
}) {
|
|
const selected = useMemo(() => new Set(value), [value]);
|
|
const selectedAssistant = useMemo(() => new Set(assistantValue), [assistantValue]);
|
|
const filtered = useMemo(() => {
|
|
const query = search.trim().toLocaleLowerCase();
|
|
if (!query) return rows;
|
|
return rows.filter((item) =>
|
|
`${item.name} ${item.description}`.toLocaleLowerCase().includes(query),
|
|
);
|
|
}, [rows, search]);
|
|
const filteredAssistant = useMemo(() => {
|
|
const query = search.trim().toLocaleLowerCase();
|
|
if (!query) return assistantRows;
|
|
return assistantRows.filter((item) =>
|
|
`${item.name} ${item.description ?? ""}`.toLocaleLowerCase().includes(query),
|
|
);
|
|
}, [assistantRows, search]);
|
|
const groups = useMemo(() => groupRows(filtered), [filtered]);
|
|
const toggle = (id: string) => {
|
|
if (readOnly) return;
|
|
onChange(selected.has(id) ? value.filter((item) => item !== id) : [...value, id]);
|
|
};
|
|
const toggleAssistant = (id: string) => {
|
|
if (readOnly) return;
|
|
onAssistantChange(
|
|
selectedAssistant.has(id)
|
|
? assistantValue.filter((item) => item !== id)
|
|
: [...assistantValue, id],
|
|
);
|
|
};
|
|
const selectedCount = value.length + assistantValue.length;
|
|
const hasRows = groups.length > 0 || filteredAssistant.length > 0;
|
|
|
|
return (
|
|
<>
|
|
<div className="border-b p-2.5">
|
|
<div className="relative">
|
|
<Search className="pointer-events-none absolute left-2.5 top-1/2 size-3.5 -translate-y-1/2 text-muted-foreground" />
|
|
<Input
|
|
value={search}
|
|
onChange={(event) => onSearchChange(event.target.value)}
|
|
onKeyDown={(event) => event.stopPropagation()}
|
|
placeholder="搜索知识空间"
|
|
className="h-8 bg-muted/40 pl-8 text-xs"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className={cn("overflow-y-auto p-1.5", variant === "popover" ? "max-h-64" : "max-h-80")}>
|
|
{loading ? (
|
|
<div className="flex h-28 items-center justify-center text-xs text-muted-foreground">
|
|
<Loader2 className="mr-2 size-4 animate-spin" />正在读取知识空间
|
|
</div>
|
|
) : error ? (
|
|
<div className="m-2 rounded-lg border border-destructive/30 bg-destructive/5 p-3 text-xs text-destructive">
|
|
{error instanceof Error ? error.message : "知识空间读取失败"}
|
|
</div>
|
|
) : !hasRows ? (
|
|
<div className="flex h-28 items-center justify-center px-4 text-center text-xs text-muted-foreground">
|
|
{search
|
|
? "没有匹配的知识空间"
|
|
: source === "agent"
|
|
? "该智能体尚未绑定可用知识空间"
|
|
: "当前账号暂无可用知识空间"}
|
|
</div>
|
|
) : (
|
|
<>
|
|
{groups.map((group, groupIndex) => (
|
|
<div key={group.key}>
|
|
{groupIndex > 0 && (
|
|
variant === "popover"
|
|
? <DropdownMenuSeparator className="my-1.5" />
|
|
: <div className="my-1.5 border-t" />
|
|
)}
|
|
{variant === "popover" ? (
|
|
<DropdownMenuLabel className="flex items-center gap-1.5 px-2 py-1 text-[11px] font-semibold text-muted-foreground">
|
|
{group.key === "personal" ? <Folder className="size-3" /> : <Globe2 className="size-3" />}{group.label}
|
|
</DropdownMenuLabel>
|
|
) : (
|
|
<div className="flex items-center gap-1.5 px-2 py-1 text-[11px] font-semibold text-muted-foreground">
|
|
{group.key === "personal" ? <Folder className="size-3" /> : <Globe2 className="size-3" />}{group.label}
|
|
</div>
|
|
)}
|
|
{group.rows.map((item) => {
|
|
const rowContent = (
|
|
<>
|
|
<SpaceIcon item={item} />
|
|
<div className="min-w-0 flex-1">
|
|
<div className="truncate text-xs font-medium">{item.name}</div>
|
|
<div className="mt-0.5 truncate text-[10px] text-muted-foreground">
|
|
{item.type === "faq" ? "FAQ" : "文档库"} · {item.knowledge_count ?? 0} 项知识
|
|
</div>
|
|
</div>
|
|
{item.publication_status === "published" && (
|
|
<Badge variant="outline" className="h-4 px-1 text-[9px]">公共</Badge>
|
|
)}
|
|
</>
|
|
);
|
|
return variant === "popover" ? (
|
|
<DropdownMenuCheckboxItem
|
|
key={item.id}
|
|
checked={selected.has(item.id)}
|
|
disabled={readOnly}
|
|
onCheckedChange={() => toggle(item.id)}
|
|
onSelect={(event) => event.preventDefault()}
|
|
className={cn(
|
|
"my-0.5 min-h-10 rounded-md py-1.5 pr-2 pl-7",
|
|
selected.has(item.id) && "bg-primary/8 focus:bg-primary/12",
|
|
)}
|
|
>
|
|
{rowContent}
|
|
</DropdownMenuCheckboxItem>
|
|
) : (
|
|
<button
|
|
key={item.id}
|
|
type="button"
|
|
role="checkbox"
|
|
aria-checked={selected.has(item.id)}
|
|
disabled={readOnly}
|
|
onClick={() => toggle(item.id)}
|
|
className={cn(
|
|
"my-0.5 flex min-h-11 w-full items-center gap-2 rounded-md px-2 py-1.5 text-left outline-none transition-colors hover:bg-muted/60 focus-visible:ring-2 focus-visible:ring-primary",
|
|
selected.has(item.id) && "bg-primary/8",
|
|
)}
|
|
>
|
|
<span className={cn(
|
|
"flex size-4 shrink-0 items-center justify-center rounded border",
|
|
selected.has(item.id) && "border-primary bg-primary text-primary-foreground",
|
|
)}>
|
|
{selected.has(item.id) && <Check className="size-3" />}
|
|
</span>
|
|
{rowContent}
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
))}
|
|
{filteredAssistant.length > 0 && (
|
|
<div>
|
|
{(groups.length > 0) && (
|
|
variant === "popover"
|
|
? <DropdownMenuSeparator className="my-1.5" />
|
|
: <div className="my-1.5 border-t" />
|
|
)}
|
|
{variant === "popover" ? (
|
|
<DropdownMenuLabel className="flex items-center gap-1.5 px-2 py-1 text-[11px] font-semibold text-muted-foreground">
|
|
<Database className="size-3" />助手知识库
|
|
</DropdownMenuLabel>
|
|
) : (
|
|
<div className="flex items-center gap-1.5 px-2 py-1 text-[11px] font-semibold text-muted-foreground">
|
|
<Database className="size-3" />助手知识库
|
|
</div>
|
|
)}
|
|
{filteredAssistant.map((item) => {
|
|
const rowContent = (
|
|
<>
|
|
<AssistantSpaceIcon item={item} />
|
|
<div className="min-w-0 flex-1">
|
|
<div className="truncate text-xs font-medium">{item.name}</div>
|
|
<div className="mt-0.5 truncate text-[10px] text-muted-foreground">
|
|
Wiki · {item.page_count ?? 0} 页 · {item.entity_count ?? 0} 实体
|
|
</div>
|
|
</div>
|
|
<Badge variant="outline" className="h-4 px-1 text-[9px]">
|
|
{item.is_global ? "总库" : "助手"}
|
|
</Badge>
|
|
</>
|
|
);
|
|
return variant === "popover" ? (
|
|
<DropdownMenuCheckboxItem
|
|
key={`assistant:${item.id}`}
|
|
checked={selectedAssistant.has(item.id)}
|
|
disabled={readOnly}
|
|
onCheckedChange={() => toggleAssistant(item.id)}
|
|
onSelect={(event) => event.preventDefault()}
|
|
className={cn(
|
|
"my-0.5 min-h-10 rounded-md py-1.5 pr-2 pl-7",
|
|
selectedAssistant.has(item.id) && "bg-primary/8 focus:bg-primary/12",
|
|
)}
|
|
>
|
|
{rowContent}
|
|
</DropdownMenuCheckboxItem>
|
|
) : (
|
|
<button
|
|
key={`assistant:${item.id}`}
|
|
type="button"
|
|
role="checkbox"
|
|
aria-checked={selectedAssistant.has(item.id)}
|
|
disabled={readOnly}
|
|
onClick={() => toggleAssistant(item.id)}
|
|
className={cn(
|
|
"my-0.5 flex min-h-11 w-full items-center gap-2 rounded-md px-2 py-1.5 text-left outline-none transition-colors hover:bg-muted/60 focus-visible:ring-2 focus-visible:ring-primary",
|
|
selectedAssistant.has(item.id) && "bg-primary/8",
|
|
)}
|
|
>
|
|
<span className={cn(
|
|
"flex size-4 shrink-0 items-center justify-center rounded border",
|
|
selectedAssistant.has(item.id) && "border-primary bg-primary text-primary-foreground",
|
|
)}>
|
|
{selectedAssistant.has(item.id) && <Check className="size-3" />}
|
|
</span>
|
|
{rowContent}
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
</>
|
|
)}
|
|
</div>
|
|
|
|
<div className="flex items-center justify-between gap-2 border-t bg-muted/20 px-2.5 py-2">
|
|
<div>
|
|
<div className="text-[11px] text-muted-foreground">
|
|
{readOnly ? `本对话已选 ${selectedCount} 个` : `已选择 ${selectedCount} 个`}
|
|
</div>
|
|
{selectedCount > 0 && (
|
|
<div className="mt-0.5 text-[10px] text-primary">仅检索已选知识空间,不联网</div>
|
|
)}
|
|
</div>
|
|
{readOnly ? (
|
|
<span className="text-[11px] text-muted-foreground">对话开始后不可更改</span>
|
|
) : (
|
|
<div className="flex items-center gap-1">
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="sm"
|
|
className="h-7 px-2 text-xs"
|
|
onClick={() => {
|
|
onChange(filtered.map((item) => item.id));
|
|
onAssistantChange(filteredAssistant.map((item) => item.id));
|
|
}}
|
|
>
|
|
全选
|
|
</Button>
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="sm"
|
|
className="h-7 px-2 text-xs"
|
|
onClick={() => {
|
|
onChange([]);
|
|
onAssistantChange([]);
|
|
}}
|
|
>
|
|
清空
|
|
</Button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export function LlmWikiKnowledgeSelector({
|
|
value,
|
|
assistantValue = EMPTY_IDS,
|
|
onChange,
|
|
onAssistantChange,
|
|
onSuggestedQuestionsChange,
|
|
source = "all",
|
|
agentId,
|
|
autoSelectAll = false,
|
|
disabled = false,
|
|
readOnly = false,
|
|
className,
|
|
label = "知识空间",
|
|
panelTitle = "绑定知识空间",
|
|
panelDescription = "新建该智能体对话时,只能从已绑定空间中选择本次检索范围。",
|
|
variant = "popover",
|
|
}: {
|
|
value: string[];
|
|
assistantValue?: string[];
|
|
onChange: (value: string[]) => void;
|
|
onAssistantChange?: (value: string[]) => void;
|
|
onSuggestedQuestionsChange?: (questions: string[]) => void;
|
|
source?: SelectorSource;
|
|
agentId?: string | null;
|
|
autoSelectAll?: boolean;
|
|
disabled?: boolean;
|
|
/** Keep the selected knowledge spaces visible without allowing the thread scope to change. */
|
|
readOnly?: boolean;
|
|
className?: string;
|
|
label?: string;
|
|
panelTitle?: string;
|
|
panelDescription?: string;
|
|
variant?: SelectorVariant;
|
|
}) {
|
|
const [open, setOpen] = useState(false);
|
|
const [search, setSearch] = useState("");
|
|
const assistantAccess = useAssistantKnowledgeAccess({
|
|
enabled:
|
|
ASSISTANT_KNOWLEDGE_UI_VISIBLE && onAssistantChange !== undefined,
|
|
});
|
|
const assistantAllowed =
|
|
ASSISTANT_KNOWLEDGE_UI_VISIBLE &&
|
|
onAssistantChange !== undefined &&
|
|
assistantAccess.canAccess;
|
|
const runtime = useLlmWikiRuntime();
|
|
const weknoraEnabled = runtime.data?.provider === "weknora";
|
|
const shouldLoadOptions =
|
|
variant === "panel" ||
|
|
open ||
|
|
autoSelectAll ||
|
|
value.length > 0 ||
|
|
assistantValue.length > 0;
|
|
const all = useLlmWikiKnowledgeBases(
|
|
"all",
|
|
weknoraEnabled && shouldLoadOptions,
|
|
);
|
|
const agent = useAgentLlmWikiKnowledgeBases(
|
|
agentId,
|
|
weknoraEnabled && shouldLoadOptions && source === "agent" && !!agentId,
|
|
);
|
|
const assistantBases = useAssistantKnowledgeBases(
|
|
assistantAllowed &&
|
|
(shouldLoadOptions ||
|
|
runtime.isError ||
|
|
(!runtime.isLoading && !weknoraEnabled)),
|
|
);
|
|
// Stable empty results are essential: suggested questions are reported to
|
|
// the parent, so a fresh [] on every render creates an effect/update loop
|
|
// before lazy knowledge queries have loaded (or when they are disabled).
|
|
const rows = useMemo(() => weknoraEnabled ? (all.data ?? []) : [], [all.data, weknoraEnabled]);
|
|
const assistantRows = useMemo(
|
|
() => assistantAllowed ? (assistantBases.data ?? []) : [],
|
|
[assistantAllowed, assistantBases.data],
|
|
);
|
|
const effectiveAssistantValue = assistantAllowed ? assistantValue : EMPTY_IDS;
|
|
const defaultIds = useMemo(() => {
|
|
if (source !== "agent") {
|
|
return rows.map((item) => item.id);
|
|
}
|
|
return (agent.data ?? []).map((item) => item.id);
|
|
}, [agent.data, rows, source]);
|
|
const loading = runtime.isLoading || (weknoraEnabled && all.isLoading) || (assistantAllowed && assistantBases.isLoading) || (source === "agent" && !!agentId && agent.isLoading);
|
|
const error = (weknoraEnabled ? all.error ?? (source === "agent" ? agent.error : null) : null) ?? (assistantAllowed ? assistantBases.error : null);
|
|
const availableIds = useMemo(() => new Set(rows.map((item) => item.id)), [rows]);
|
|
const assistantAvailableIds = useMemo(
|
|
() => new Set(assistantRows.map((item) => item.id)),
|
|
[assistantRows],
|
|
);
|
|
const initializationKey = `${source}:${agentId ?? "all"}:${defaultIds.join(",")}`;
|
|
const initialized = useRef<string | null>(null);
|
|
const selectedNames = useMemo(
|
|
() => value.map((id) => rows.find((item) => item.id === id)?.name).filter((name): name is string => !!name),
|
|
[rows, value],
|
|
);
|
|
const selectedAssistantNames = useMemo(
|
|
() => effectiveAssistantValue.map((id) => assistantRows.find((item) => item.id === id)?.name).filter((name): name is string => !!name),
|
|
[assistantRows, effectiveAssistantValue],
|
|
);
|
|
const allSelectedNames = useMemo(
|
|
() => [...selectedNames, ...selectedAssistantNames],
|
|
[selectedAssistantNames, selectedNames],
|
|
);
|
|
const selectedConfiguredQuestions = useMemo(
|
|
() =>
|
|
userFacingKnowledgeSuggestedQuestions(
|
|
value.flatMap(
|
|
(id) => rows.find((item) => item.id === id)?.test_questions ?? [],
|
|
),
|
|
),
|
|
[rows, value],
|
|
);
|
|
const suggestedQuestions = useMemo(
|
|
() => {
|
|
if (value.length === 1 && effectiveAssistantValue.length === 0 && selectedConfiguredQuestions.length > 0) {
|
|
return selectedConfiguredQuestions.slice(0, 12);
|
|
}
|
|
const general = buildKnowledgeSpaceQuestions(allSelectedNames);
|
|
return Array.from(new Set([...selectedConfiguredQuestions, ...general])).slice(0, 12);
|
|
},
|
|
[allSelectedNames, effectiveAssistantValue.length, selectedConfiguredQuestions, value.length],
|
|
);
|
|
|
|
useEffect(() => {
|
|
onSuggestedQuestionsChange?.(suggestedQuestions);
|
|
}, [onSuggestedQuestionsChange, suggestedQuestions]);
|
|
|
|
useEffect(() => {
|
|
if (!runtime.isLoading && !weknoraEnabled) {
|
|
if (value.length > 0) onChange([]);
|
|
return;
|
|
}
|
|
if (!all.data || (source === "agent" && agentId && !agent.data) || initialized.current === initializationKey) return;
|
|
initialized.current = initializationKey;
|
|
const valid = value.filter((id) => availableIds.has(id));
|
|
const next = autoSelectAll && valid.length === 0 && defaultIds.length > 0
|
|
? defaultIds.filter((id) => availableIds.has(id))
|
|
: valid;
|
|
if (next.length !== value.length || next.some((id, index) => id !== value[index])) {
|
|
onChange(next);
|
|
}
|
|
}, [agent.data, agentId, all.data, autoSelectAll, availableIds, defaultIds, initializationKey, onChange, runtime.isLoading, source, value, weknoraEnabled]);
|
|
|
|
useEffect(() => {
|
|
if (!assistantAllowed || !assistantBases.data || !onAssistantChange) return;
|
|
const valid = effectiveAssistantValue.filter((id) => assistantAvailableIds.has(id));
|
|
if (valid.length !== effectiveAssistantValue.length || valid.some((id, index) => id !== effectiveAssistantValue[index])) {
|
|
onAssistantChange(valid);
|
|
}
|
|
}, [assistantAllowed, assistantAvailableIds, assistantBases.data, effectiveAssistantValue, onAssistantChange]);
|
|
|
|
useEffect(() => {
|
|
if (!open) setSearch("");
|
|
}, [open]);
|
|
|
|
if (
|
|
!runtime.isLoading &&
|
|
(runtime.isError || !weknoraEnabled) &&
|
|
(!assistantAllowed || !assistantBases.isLoading) &&
|
|
assistantRows.length === 0
|
|
) return null;
|
|
|
|
const content = (
|
|
<SelectionContent
|
|
rows={rows}
|
|
assistantRows={assistantRows}
|
|
value={value}
|
|
assistantValue={effectiveAssistantValue}
|
|
onChange={onChange}
|
|
onAssistantChange={
|
|
assistantAllowed
|
|
? (onAssistantChange ?? IGNORE_ASSISTANT_CHANGE)
|
|
: IGNORE_ASSISTANT_CHANGE
|
|
}
|
|
search={search}
|
|
onSearchChange={setSearch}
|
|
loading={loading}
|
|
error={error}
|
|
source={source}
|
|
variant={variant}
|
|
readOnly={readOnly}
|
|
/>
|
|
);
|
|
|
|
if (variant === "panel") {
|
|
return (
|
|
<div className={cn("overflow-hidden rounded-xl border bg-background shadow-sm", className)}>
|
|
<div className="flex items-start justify-between gap-4 border-b px-4 py-3">
|
|
<div>
|
|
<div className="flex items-center gap-2 text-sm font-semibold">
|
|
<Database className="size-4 text-primary" />{panelTitle}
|
|
</div>
|
|
<p className="mt-1 text-xs text-muted-foreground">
|
|
{panelDescription}
|
|
</p>
|
|
</div>
|
|
<Badge variant="secondary" className="shrink-0">{value.length + effectiveAssistantValue.length} 个</Badge>
|
|
</div>
|
|
{content}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const selectedCount = value.length + effectiveAssistantValue.length;
|
|
const selectedLabel = allSelectedNames.length > 0
|
|
? allSelectedNames.join("、")
|
|
: selectedCount > 0
|
|
? `已选 ${selectedCount} 个知识空间`
|
|
: label;
|
|
|
|
return (
|
|
<DropdownMenu open={open} onOpenChange={setOpen}>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="sm"
|
|
className={cn(
|
|
"agent-composer-tool h-8 max-w-52 gap-1.5 rounded-full border-0 bg-transparent px-2.5 text-muted-foreground shadow-none hover:bg-muted/70 hover:text-foreground",
|
|
selectedCount > 0 && "bg-primary/8 text-primary hover:bg-primary/12 hover:text-primary",
|
|
className,
|
|
)}
|
|
disabled={disabled || loading}
|
|
title={selectedCount > 0 ? selectedLabel : undefined}
|
|
>
|
|
{loading ? <Loader2 className="size-3.5 animate-spin" /> : <Database className="size-3.5" />}
|
|
<span className="truncate text-xs font-normal">{selectedLabel}</span>
|
|
{selectedCount > 0 && (
|
|
<span className="flex size-4 items-center justify-center rounded-full bg-primary text-[9px] font-semibold text-primary-foreground">
|
|
{selectedCount}
|
|
</span>
|
|
)}
|
|
{selectedCount > 0 && selectedCount === rows.length + assistantRows.length && <Check className="size-3" />}
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="start" sideOffset={8} className="w-80 overflow-hidden rounded-xl p-0 shadow-xl">
|
|
{content}
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
);
|
|
}
|
|
|
|
export default LlmWikiKnowledgeSelector;
|