智能体列表
This commit is contained in:
1 parent
6cf4404243
commit
45eb46c3f3
11 files changed
+1457
-171
No files matched your search
@@ -1,7 +1,16 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { CheckIcon, Loader2Icon, RefreshCwIcon, SparklesIcon } from "lucide-react";
|
||||
import {
|
||||
CheckIcon,
|
||||
ChevronDownIcon,
|
||||
ChevronUpIcon,
|
||||
DotIcon,
|
||||
LightbulbIcon,
|
||||
Loader2Icon,
|
||||
RefreshCwIcon,
|
||||
SparklesIcon,
|
||||
} from "lucide-react";
|
||||
|
||||
import {
|
||||
Dialog,
|
||||
@@ -12,7 +21,15 @@ import {
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
ChainOfThought,
|
||||
ChainOfThoughtContent,
|
||||
ChainOfThoughtHeader,
|
||||
ChainOfThoughtStep,
|
||||
} from "@/components/ai-elements/chain-of-thought";
|
||||
import { MarkdownContent } from "@/components/workspace/messages/markdown-content";
|
||||
import { listAgents, type Agent } from "@/core/agents";
|
||||
import { useRehypeSplitWordsIntoSpans } from "@/core/rehype";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
import type { IntentSummary } from "../api/intent";
|
||||
@@ -43,7 +60,7 @@ export interface RecommendAgentsDialogProps {
|
||||
model?: string;
|
||||
onCancel: () => void;
|
||||
/** Called with the user's final selection of agent ids (in display order). */
|
||||
onConfirm: (agentIds: string[]) => void;
|
||||
onConfirm: (agents: Array<{ agent_id: string; name: string }>) => void;
|
||||
}
|
||||
|
||||
const MIN_SELECTED = 2;
|
||||
@@ -71,12 +88,21 @@ type DialogPhase =
|
||||
| "fallback"
|
||||
| "error";
|
||||
|
||||
interface RecommendStep {
|
||||
id: string;
|
||||
key: string;
|
||||
label: string;
|
||||
ts: string;
|
||||
}
|
||||
|
||||
interface InternalState {
|
||||
phase: DialogPhase;
|
||||
/** Streaming + final rationale text from the recommender. */
|
||||
rationale: string;
|
||||
/** Streaming SSE phase from the recommender helper. */
|
||||
streamPhase: RecommendStreamPhase | null;
|
||||
/** 推荐流程阶段时间线 —— 调度 / 已连接 / 流式生成 …… 用 ChainOfThought 折叠展示。 */
|
||||
steps: RecommendStep[];
|
||||
/** Pre-filtered candidate pool fetched from `/api/agents`. */
|
||||
candidates: RecommendCandidate[];
|
||||
/** Reason-by-agent_id, only for picks returned by the recommender. */
|
||||
@@ -93,6 +119,7 @@ const INITIAL_STATE: InternalState = {
|
||||
phase: "idle",
|
||||
rationale: "",
|
||||
streamPhase: null,
|
||||
steps: [],
|
||||
candidates: [],
|
||||
reasons: {},
|
||||
recommendedIds: [],
|
||||
@@ -100,6 +127,24 @@ const INITIAL_STATE: InternalState = {
|
||||
errorMessage: null,
|
||||
};
|
||||
|
||||
/** 推荐流程的 phase → 中文标签;与 RoundtablePlanningPage::phaseLabel 风格保持一致。 */
|
||||
function recommendPhaseLabel(phase: RecommendStreamPhase): string {
|
||||
switch (phase) {
|
||||
case "connecting":
|
||||
return "调度推荐智能体";
|
||||
case "connected":
|
||||
return "已连接,等待响应";
|
||||
case "streaming":
|
||||
return "正在生成推荐";
|
||||
default:
|
||||
return phase;
|
||||
}
|
||||
}
|
||||
|
||||
function nowTimeStr(): string {
|
||||
return new Date().toLocaleTimeString("zh-CN", { hour12: false });
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve which candidate ids should be checked by default given a set of
|
||||
* recommended ids. The result preserves recommendation order and silently
|
||||
@@ -193,6 +238,10 @@ export function RecommendAgentsDialog({
|
||||
candidates,
|
||||
rationale: "",
|
||||
streamPhase: "connecting",
|
||||
// 推荐开始,清空上一轮的步骤时间线并打第一条点。
|
||||
steps: [
|
||||
{ id: "0", key: "connecting", label: recommendPhaseLabel("connecting"), ts: nowTimeStr() },
|
||||
],
|
||||
}));
|
||||
|
||||
try {
|
||||
@@ -227,7 +276,24 @@ export function RecommendAgentsDialog({
|
||||
},
|
||||
onPhaseChange: (phase) => {
|
||||
if (myGen !== generationRef.current) return;
|
||||
setState((prev) => ({ ...prev, streamPhase: phase }));
|
||||
setState((prev) => {
|
||||
const key = phase;
|
||||
const last = prev.steps[prev.steps.length - 1];
|
||||
// 连续重复阶段不重复 push,只在阶段切换时打点。
|
||||
const steps =
|
||||
last && last.key === key
|
||||
? prev.steps
|
||||
: [
|
||||
...prev.steps,
|
||||
{
|
||||
id: String(prev.steps.length),
|
||||
key,
|
||||
label: recommendPhaseLabel(phase),
|
||||
ts: nowTimeStr(),
|
||||
},
|
||||
];
|
||||
return { ...prev, streamPhase: phase, steps };
|
||||
});
|
||||
},
|
||||
});
|
||||
if (myGen !== generationRef.current) return;
|
||||
@@ -346,12 +412,15 @@ export function RecommendAgentsDialog({
|
||||
// Order: recommended-and-selected first (in recommender order), then
|
||||
// user-added picks (in pool order). Keeps the leader's "可调度智能体"
|
||||
// list visually stable.
|
||||
const ordered: string[] = [];
|
||||
//
|
||||
// 传完整对象(含 name)而不是只传 agent_id —— 非内置 agent 的 id 是 UUID,
|
||||
// 上游若只拿 id,在左侧席位列表就会显示 UUID 而非中文名。
|
||||
const ordered: Array<{ agent_id: string; name: string }> = [];
|
||||
for (const c of recommendedCards) {
|
||||
if (selectedIds.has(c.agent_id)) ordered.push(c.agent_id);
|
||||
if (selectedIds.has(c.agent_id)) ordered.push({ agent_id: c.agent_id, name: c.name });
|
||||
}
|
||||
for (const c of otherCards) {
|
||||
if (selectedIds.has(c.agent_id)) ordered.push(c.agent_id);
|
||||
if (selectedIds.has(c.agent_id)) ordered.push({ agent_id: c.agent_id, name: c.name });
|
||||
}
|
||||
onConfirm(ordered);
|
||||
}, [canConfirm, recommendedCards, otherCards, selectedIds, onConfirm]);
|
||||
@@ -390,7 +459,7 @@ export function RecommendAgentsDialog({
|
||||
) : state.phase === "recommending" ? (
|
||||
<>
|
||||
{recommendedCards.length > 0 ? (
|
||||
<Section title={`推荐参会 (${recommendedCards.length})`}>
|
||||
<Section title={`推荐参会 (${recommendedCards.length})`} collapsible defaultOpen>
|
||||
{recommendedCards.map((c) => (
|
||||
<AgentCard
|
||||
key={c.agent_id}
|
||||
@@ -419,7 +488,7 @@ export function RecommendAgentsDialog({
|
||||
)}
|
||||
|
||||
{recommendedCards.length > 0 && (
|
||||
<Section title={`推荐参会 (${recommendedCards.length})`}>
|
||||
<Section title={`推荐参会 (${recommendedCards.length})`} collapsible defaultOpen>
|
||||
{recommendedCards.map((c) => (
|
||||
<AgentCard
|
||||
key={c.agent_id}
|
||||
@@ -490,6 +559,24 @@ export function RecommendAgentsDialog({
|
||||
}
|
||||
|
||||
function RationaleBlock({ state }: { state: InternalState }) {
|
||||
// 默认展开:推荐流程一开始就让用户能看到生成过程。
|
||||
// 生成完毕后(isStreaming 翻成 false)自动收起 —— 让 UI 聚焦在 picks 选择上。
|
||||
// userTouchedRef 标记用户是否手动点过按钮:点过就尊重用户偏好,不再自动收起。
|
||||
const [expanded, setExpanded] = useState(true);
|
||||
const userTouchedRef = useRef(false);
|
||||
// 步骤折叠卡:**始终默认收起**,header 上显示最新一步作为预览。
|
||||
const [stepsOpen, setStepsOpen] = useState(false);
|
||||
const isStreaming = state.phase === "recommending";
|
||||
// 流式期间禁用逐字动画——避免不断 re-tokenize 拖慢渲染;完成后回到普通 markdown。
|
||||
const rehypePlugins = useRehypeSplitWordsIntoSpans(isStreaming);
|
||||
|
||||
// 流式状态切换时,如果用户没有手动调过展开/收起,执行默认行为:
|
||||
// 流式中 → expanded=true,完成后 → expanded=false。
|
||||
// 步骤折叠卡不自动跟随 —— 始终默认收起。
|
||||
useEffect(() => {
|
||||
if (!userTouchedRef.current) setExpanded(isStreaming);
|
||||
}, [isStreaming]);
|
||||
|
||||
if (state.phase === "loading_agents") {
|
||||
return (
|
||||
<div className="flex items-center gap-2 text-muted-foreground">
|
||||
@@ -498,52 +585,178 @@ function RationaleBlock({ state }: { state: InternalState }) {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
if (state.phase === "recommending") {
|
||||
const label =
|
||||
state.streamPhase === "connecting"
|
||||
|
||||
if (state.phase === "error") {
|
||||
return (
|
||||
<div className="text-xs text-destructive">
|
||||
推荐失败,请使用下方的"重新推荐"重试,或取消后再次进入。
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const showRationaleStage =
|
||||
state.phase === "recommending" ||
|
||||
state.phase === "ready" ||
|
||||
state.phase === "fallback";
|
||||
if (!showRationaleStage) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const streamingLabel =
|
||||
state.phase === "recommending"
|
||||
? state.streamPhase === "connecting"
|
||||
? "调度推荐智能体..."
|
||||
: state.streamPhase === "connected"
|
||||
? "已连接,等待响应..."
|
||||
: "正在生成推荐...";
|
||||
return (
|
||||
<div className="space-y-1">
|
||||
: "正在生成推荐..."
|
||||
: null;
|
||||
|
||||
const hasRationale = !!state.rationale;
|
||||
|
||||
return (
|
||||
<div className="space-y-1.5">
|
||||
{streamingLabel && (
|
||||
<div className="flex items-center gap-2 text-muted-foreground text-xs">
|
||||
<Loader2Icon className="size-3 animate-spin" />
|
||||
{label}
|
||||
{streamingLabel}
|
||||
</div>
|
||||
{state.rationale ? (
|
||||
<p className="whitespace-pre-wrap break-words text-xs text-foreground leading-relaxed">
|
||||
{state.rationale}
|
||||
</p>
|
||||
) : (
|
||||
<p className="text-xs text-muted-foreground">正在分析任务与候选智能体...</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
if (state.phase === "ready" || state.phase === "fallback") {
|
||||
if (!state.rationale) {
|
||||
return <div className="text-xs text-muted-foreground">推荐完成。</div>;
|
||||
}
|
||||
return (
|
||||
<p className="whitespace-pre-wrap break-words text-xs text-foreground leading-relaxed max-h-32 overflow-y-auto">
|
||||
{state.rationale}
|
||||
</p>
|
||||
);
|
||||
}
|
||||
if (state.phase === "error") {
|
||||
return <div className="text-xs text-destructive">推荐失败,请使用下方的"重新推荐"重试,或取消后再次进入。</div>;
|
||||
}
|
||||
return null;
|
||||
)}
|
||||
|
||||
{/* 推荐流程阶段时间线(视觉与 chats/new 消息列表的 ChainOfThought 对齐):
|
||||
**始终默认收起**,收起 header 上显示最新一步作为预览。
|
||||
streaming 用 LightbulbIcon,其它阶段用 DotIcon —— 推荐流程没有 tool_calls,
|
||||
所以不需要 chats 那套完整的工具图标分支。 */}
|
||||
{state.steps.length > 0 && (() => {
|
||||
const lastStep = state.steps[state.steps.length - 1];
|
||||
const LastIcon = lastStep?.key === "streaming" ? LightbulbIcon : DotIcon;
|
||||
return (
|
||||
<ChainOfThought
|
||||
className="rounded-md border border-border/50 bg-muted/30 px-3 py-2"
|
||||
open={stepsOpen}
|
||||
onOpenChange={setStepsOpen}
|
||||
>
|
||||
<ChainOfThoughtHeader
|
||||
className="text-[11px]"
|
||||
icon={
|
||||
// 仅「生成中 + 收起」时用最新一步的图标作进度预览;完成后用默认脑子图标。
|
||||
!stepsOpen && isStreaming ? (
|
||||
<LastIcon className="size-3.5 text-muted-foreground" />
|
||||
) : undefined
|
||||
}
|
||||
>
|
||||
{stepsOpen
|
||||
? "步骤"
|
||||
: isStreaming
|
||||
? (lastStep?.label ?? `查看 ${state.steps.length} 个步骤`)
|
||||
: "步骤列表"}
|
||||
</ChainOfThoughtHeader>
|
||||
<ChainOfThoughtContent>
|
||||
{state.steps.map((s, i) => (
|
||||
<ChainOfThoughtStep
|
||||
key={s.id}
|
||||
icon={s.key === "streaming" ? LightbulbIcon : DotIcon}
|
||||
label={
|
||||
<span className="text-[12px] text-muted-foreground">
|
||||
<span className="font-medium text-foreground">{s.label}</span>
|
||||
<span className="ml-2 text-[10px] font-mono opacity-60">{s.ts}</span>
|
||||
</span>
|
||||
}
|
||||
status={
|
||||
isStreaming && i === state.steps.length - 1 ? "active" : "complete"
|
||||
}
|
||||
/>
|
||||
))}
|
||||
</ChainOfThoughtContent>
|
||||
</ChainOfThought>
|
||||
);
|
||||
})()}
|
||||
|
||||
{!hasRationale && isStreaming && (
|
||||
<p className="text-xs text-muted-foreground">正在分析任务与候选智能体...</p>
|
||||
)}
|
||||
|
||||
{!hasRationale && !isStreaming && (
|
||||
<div className="text-xs text-muted-foreground">推荐完成。</div>
|
||||
)}
|
||||
|
||||
{hasRationale && (
|
||||
<>
|
||||
{/* 按钮在上、内容在下 —— 展开/收起按钮始终留在原位,避免点开时
|
||||
内容把按钮挤到屏幕外,用户需要滚回去才能再点收起。
|
||||
收起时完全不渲染内容区,只保留按钮(图标 + "思考过程")。
|
||||
streamingLabel 已经在上方独立显示阶段进度,这里不重复 spinner。 */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
userTouchedRef.current = true;
|
||||
setExpanded((v) => !v);
|
||||
}}
|
||||
className="inline-flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground transition-colors cursor-pointer"
|
||||
>
|
||||
{expanded ? (
|
||||
<ChevronUpIcon className="size-3" />
|
||||
) : (
|
||||
<ChevronDownIcon className="size-3" />
|
||||
)}
|
||||
思考过程
|
||||
</button>
|
||||
{expanded && (
|
||||
<div className="max-h-96 overflow-y-auto rounded-md border border-border/50 bg-muted/30 px-3 py-2 text-xs text-foreground leading-relaxed">
|
||||
<MarkdownContent
|
||||
content={state.rationale}
|
||||
isLoading={isStreaming}
|
||||
rehypePlugins={rehypePlugins}
|
||||
className="text-xs"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function Section({ title, children }: { title: string; children: React.ReactNode }) {
|
||||
function Section({
|
||||
title,
|
||||
children,
|
||||
collapsible = false,
|
||||
defaultOpen = true,
|
||||
}: {
|
||||
title: string;
|
||||
children: React.ReactNode;
|
||||
/** True 时 header 可点击折叠/展开;false 保持原行为(始终展开,不可点击)。 */
|
||||
collapsible?: boolean;
|
||||
/** 仅当 collapsible=true 时生效。 */
|
||||
defaultOpen?: boolean;
|
||||
}) {
|
||||
const [open, setOpen] = useState(defaultOpen);
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<div className="text-xs font-medium text-muted-foreground uppercase tracking-wide">
|
||||
{title}
|
||||
</div>
|
||||
<div className="grid gap-2">{children}</div>
|
||||
{collapsible ? (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpen((v) => !v)}
|
||||
className="flex w-full items-center gap-1 text-xs font-medium text-muted-foreground uppercase tracking-wide hover:text-foreground transition-colors cursor-pointer"
|
||||
>
|
||||
{open ? (
|
||||
<ChevronUpIcon className="size-3" />
|
||||
) : (
|
||||
<ChevronDownIcon className="size-3" />
|
||||
)}
|
||||
<span>{title}</span>
|
||||
</button>
|
||||
) : (
|
||||
<div className="text-xs font-medium text-muted-foreground uppercase tracking-wide">
|
||||
{title}
|
||||
</div>
|
||||
)}
|
||||
{/* flex flex-col + w-full + items-stretch:替代原 `grid gap-2`,
|
||||
强制所有 AgentCard 满父宽。grid 在某些浏览器对 <button> 子项的
|
||||
justify-self: stretch 处理不一致,导致"推荐参会"与"其他候选"
|
||||
两个 Section 视觉上有几像素宽度差。 */}
|
||||
{(!collapsible || open) && (
|
||||
<div className="flex w-full flex-col gap-2">{children}</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -571,7 +784,9 @@ function AgentCard({
|
||||
type="button"
|
||||
onClick={onToggle}
|
||||
className={cn(
|
||||
"text-left rounded-md border p-3 transition-colors flex gap-3 items-start",
|
||||
// w-full:`<button>` 默认 inline-block,在 grid cell 里不一定拉满;
|
||||
// 显式 w-full 保证推荐区与其他候选区的卡片**宽度严格一致**。
|
||||
"w-full text-left rounded-md border p-3 transition-colors flex gap-3 items-start",
|
||||
checked
|
||||
? "border-primary bg-primary/5 hover:bg-primary/10"
|
||||
: "border-border hover:bg-muted/40",
|
||||
|
||||
Reference in new issue
Block a user