feat: add strategy selection workbench
This commit is contained in:
1 parent
60e2c72514
commit
5fd2e8302c
34 files changed
+3188
-9
No files matched your search
@@ -0,0 +1,218 @@
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { Bot, Check, LoaderCircle, Save, Sparkles } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { getAgent, updateAgent } from "@/core/agents/api";
|
||||
import type { Agent } from "@/core/agents/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
import { STRATEGY_AGENT_ROLES } from "./constants";
|
||||
|
||||
interface AgentConfigDialogProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
}
|
||||
|
||||
function parseSkills(value: string): string[] {
|
||||
return [...new Set(value.split(/[,,\n]/).map((item) => item.trim()).filter(Boolean))];
|
||||
}
|
||||
|
||||
export function AgentConfigDialog({ open, onOpenChange }: AgentConfigDialogProps) {
|
||||
const [selectedId, setSelectedId] = useState<string>(STRATEGY_AGENT_ROLES[0].id);
|
||||
const [agent, setAgent] = useState<Agent | null>(null);
|
||||
const [soul, setSoul] = useState("");
|
||||
const [skillsText, setSkillsText] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
const selectedRole = useMemo(
|
||||
() => STRATEGY_AGENT_ROLES.find((role) => role.id === selectedId)!,
|
||||
[selectedId],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open || !selectedId) return;
|
||||
let active = true;
|
||||
setLoading(true);
|
||||
setError("");
|
||||
setAgent(null);
|
||||
void getAgent(selectedId)
|
||||
.then((data) => {
|
||||
if (!active) return;
|
||||
setAgent(data);
|
||||
setSoul(data.soul ?? "");
|
||||
setSkillsText((data.skills ?? []).join("\n"));
|
||||
})
|
||||
.catch((reason: unknown) => {
|
||||
if (!active) return;
|
||||
setError(
|
||||
reason instanceof Error
|
||||
? reason.message
|
||||
: "智能体配置加载失败,请确认后端已完成默认配置初始化。",
|
||||
);
|
||||
})
|
||||
.finally(() => {
|
||||
if (active) setLoading(false);
|
||||
});
|
||||
return () => {
|
||||
active = false;
|
||||
};
|
||||
}, [open, selectedId]);
|
||||
|
||||
const save = async () => {
|
||||
if (!agent) return;
|
||||
setSaving(true);
|
||||
try {
|
||||
const updated = await updateAgent(agent.id, {
|
||||
soul: soul.trim(),
|
||||
skills: parseSkills(skillsText),
|
||||
});
|
||||
setAgent(updated);
|
||||
setSoul(updated.soul ?? "");
|
||||
setSkillsText((updated.skills ?? []).join("\n"));
|
||||
toast.success(`${updated.name} 配置已保存`);
|
||||
} catch (reason) {
|
||||
toast.error(reason instanceof Error ? reason.message : "保存失败");
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="h-[min(780px,calc(100vh-48px))] max-w-[1080px] grid-rows-[auto_minmax(0,1fr)_auto] gap-0 overflow-hidden p-0 sm:max-w-[1080px]">
|
||||
<DialogHeader className="border-b px-6 py-5 dark:border-[#173d68]">
|
||||
<DialogTitle className="flex items-center gap-2">
|
||||
<Bot className="size-5 text-primary" />
|
||||
策略智能体配置
|
||||
</DialogTitle>
|
||||
<DialogDescription>
|
||||
每个流程角色对应一个默认智能体。管理员可修改 SOUL 和技能;保存后下一次运行即生效。
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="grid min-h-0 grid-cols-[290px_minmax(0,1fr)]">
|
||||
<div className="min-h-0 overflow-y-auto border-r bg-muted/20 p-3 dark:border-[#173d68] dark:bg-[#031b38]">
|
||||
<div className="mb-2 px-2 text-xs font-semibold tracking-wide text-muted-foreground">
|
||||
流程角色 · {STRATEGY_AGENT_ROLES.length}
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
{STRATEGY_AGENT_ROLES.map((role) => {
|
||||
const selected = role.id === selectedId;
|
||||
return (
|
||||
<button
|
||||
key={role.id}
|
||||
type="button"
|
||||
disabled={saving}
|
||||
onClick={() => setSelectedId(role.id)}
|
||||
className={cn(
|
||||
"w-full rounded-lg border px-3 py-3 text-left transition-colors disabled:cursor-not-allowed disabled:opacity-60",
|
||||
selected
|
||||
? "border-primary/45 bg-primary/10 shadow-sm"
|
||||
: "border-transparent hover:border-border hover:bg-background dark:hover:border-[#28507c] dark:hover:bg-[#08274c]",
|
||||
)}
|
||||
>
|
||||
<span className="flex items-center justify-between gap-2">
|
||||
<span className="text-sm font-medium">{role.name}</span>
|
||||
{selected && <Check className="size-4 text-primary" />}
|
||||
</span>
|
||||
<span className="mt-1 block text-xs leading-5 text-muted-foreground">
|
||||
{role.role}
|
||||
</span>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="min-h-0 overflow-y-auto px-6 py-5">
|
||||
<div className="mb-5 flex items-start justify-between gap-3">
|
||||
<div>
|
||||
<h3 className="font-semibold">{selectedRole.name}</h3>
|
||||
<p className="mt-1 text-sm text-muted-foreground">{selectedRole.role}</p>
|
||||
</div>
|
||||
<Badge variant="outline" className="font-mono text-[11px]">
|
||||
{selectedRole.id}
|
||||
</Badge>
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<div className="flex h-56 items-center justify-center gap-2 text-sm text-muted-foreground">
|
||||
<LoaderCircle className="size-4 animate-spin" />
|
||||
正在读取智能体配置…
|
||||
</div>
|
||||
) : error ? (
|
||||
<div className="rounded-lg border border-destructive/30 bg-destructive/5 p-4 text-sm text-destructive">
|
||||
<p className="font-medium">暂时无法加载该智能体</p>
|
||||
<p className="mt-1 leading-6 opacity-85">{error}</p>
|
||||
</div>
|
||||
) : agent ? (
|
||||
<div className="space-y-5">
|
||||
<div className="grid grid-cols-2 gap-3 rounded-lg border bg-muted/20 p-3 text-sm dark:border-[#173d68] dark:bg-[#031b38]">
|
||||
<div>
|
||||
<span className="text-xs text-muted-foreground">当前名称</span>
|
||||
<div className="mt-1 font-medium">{agent.name}</div>
|
||||
</div>
|
||||
<div>
|
||||
<span className="text-xs text-muted-foreground">运行模型</span>
|
||||
<div className="mt-1 font-medium">{agent.model || "使用系统默认模型"}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<label className="block">
|
||||
<span className="mb-2 flex items-center gap-2 text-sm font-medium">
|
||||
<Sparkles className="size-4 text-primary" /> SOUL
|
||||
</span>
|
||||
<Textarea
|
||||
value={soul}
|
||||
disabled={saving}
|
||||
onChange={(event) => setSoul(event.target.value)}
|
||||
className="min-h-[300px] resize-y font-mono text-xs leading-6"
|
||||
placeholder="定义智能体的角色、分析方法、边界和输出要求…"
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label className="block">
|
||||
<span className="mb-2 block text-sm font-medium">技能</span>
|
||||
<Textarea
|
||||
value={skillsText}
|
||||
disabled={saving}
|
||||
onChange={(event) => setSkillsText(event.target.value)}
|
||||
placeholder="多个技能可用逗号分隔"
|
||||
rows={3}
|
||||
className="resize-y text-sm"
|
||||
/>
|
||||
<p className="mt-1.5 text-xs text-muted-foreground">
|
||||
可用逗号、中文逗号或换行分隔;留空表示不绑定专用技能。
|
||||
</p>
|
||||
</label>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<DialogFooter className="border-t px-6 py-4 dark:border-[#173d68]">
|
||||
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
||||
关闭
|
||||
</Button>
|
||||
<Button disabled={!agent || loading || saving} onClick={() => void save()}>
|
||||
{saving ? <LoaderCircle className="animate-spin" /> : <Save />}
|
||||
保存当前智能体
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user