studio / src /components /panels /AgentsPanel.jsx
cheumk's picture
Upload 32 files
4e039c5 verified
Raw History Blame Contribute Delete
5.09 kB
import { useState } from 'react'
import { Plus, Trash2, Copy, Save, RotateCcw, ArrowLeft } from 'lucide-react'
import { useStore } from '../../store/useStore'
import { BUILTIN_TOOLS } from '../../lib/tools'
import { uid } from '../../lib/providers'
import { Btn, Field, Section, inputCls } from '../ui'
function AgentEditor({ agent, onBack }) {
const customTools = useStore((s) => s.customTools)
const { saveAgent, removeAgent } = useStore.getState()
const [d, setD] = useState({ ...agent, tools: [...(agent.tools || [])] })
const all = [...BUILTIN_TOOLS.map((t) => ({ name: t.name, label: t.label })), ...customTools.map((t) => ({ name: t.name, label: `${t.name}(自訂)` }))]
const toggle = (name) => setD({ ...d, tools: d.tools.includes(name) ? d.tools.filter((x) => x !== name) : [...d.tools, name] })
return (
<div className="p-3 space-y-3">
<button type="button" onClick={onBack} className="text-xs text-muted hover:text-ink inline-flex items-center gap-1"><ArrowLeft className="w-3.5 h-3.5" /> 返回清單</button>
<div className="flex gap-2">
<Field label="圖示"><input className={`${inputCls} w-16 text-center`} value={d.icon} onChange={(e) => setD({ ...d, icon: e.target.value })} /></Field>
<div className="flex-1"><Field label="名稱"><input className={inputCls} value={d.name} onChange={(e) => setD({ ...d, name: e.target.value })} /></Field></div>
</div>
<Field label="說明"><input className={inputCls} value={d.description} onChange={(e) => setD({ ...d, description: e.target.value })} /></Field>
<Field label="System prompt">
<textarea className={`${inputCls} text-xs leading-relaxed`} rows={8} value={d.systemPrompt} onChange={(e) => setD({ ...d, systemPrompt: e.target.value })} />
</Field>
<div>
<div className="text-xs text-muted mb-1 flex justify-between">
啟用工具({d.tools.length})
<span className="space-x-2">
<button type="button" className="hover:text-ink" onClick={() => setD({ ...d, tools: all.map((t) => t.name) })}>全選</button>
<button type="button" className="hover:text-ink" onClick={() => setD({ ...d, tools: [] })}>全不選</button>
</span>
</div>
<div className="grid grid-cols-2 gap-1">
{all.map((t) => (
<label key={t.name} className="flex items-center gap-1.5 text-xs bg-field rounded px-2 py-1.5 cursor-pointer">
<input type="checkbox" checked={d.tools.includes(t.name)} onChange={() => toggle(t.name)} />
<span className="truncate" title={t.name}>{t.label}</span>
</label>
))}
</div>
</div>
<div className="flex gap-2">
<Btn variant="primary" className="flex-1" onClick={() => { saveAgent(d); onBack() }}><Save className="w-4 h-4" /> 儲存</Btn>
<Btn title="複製成新角色" onClick={() => { saveAgent({ ...d, id: uid(), name: `${d.name}(副本)`, builtin: false }); onBack() }}><Copy className="w-4 h-4" /></Btn>
<Btn variant="danger" title="刪除" onClick={() => { if (confirm(`刪除 Agent「${d.name}」?`)) { removeAgent(d.id); onBack() } }}><Trash2 className="w-4 h-4" /></Btn>
</div>
</div>
)
}
export default function AgentsPanel() {
const agents = useStore((s) => s.agents)
const [editing, setEditing] = useState(null)
if (editing) {
const agent = agents.find((a) => a.id === editing) || { id: editing, name: '新角色', icon: '🤖', description: '', systemPrompt: '你是一個樂於助人的助手。', tools: ['web_search'] }
return <AgentEditor key={editing} agent={agent} onBack={() => setEditing(null)} />
}
return (
<div>
<Section title={`Agent 角色(${agents.length})`}>
<p className="text-[11px] text-muted">每個角色有自己的 system prompt 與工具組合。點一下編輯。</p>
<div className="space-y-1.5">
{agents.map((a) => (
<button key={a.id} type="button" onClick={() => setEditing(a.id)} className="w-full text-left px-3 py-2 rounded-lg bg-field hover:bg-edge flex items-center gap-3">
<span className="text-xl">{a.icon}</span>
<span className="min-w-0 flex-1">
<span className="block text-sm font-medium truncate">{a.name}{a.builtin ? '' : ' ✦'}</span>
<span className="block text-[11px] text-muted truncate">{a.description}</span>
</span>
<span className="text-[11px] text-muted">{a.tools?.length || 0}🔧</span>
</button>
))}
</div>
<div className="flex gap-2">
<Btn variant="primary" className="flex-1" onClick={() => setEditing(uid())}><Plus className="w-4 h-4" /> 新角色</Btn>
<Btn title="還原內建角色" onClick={() => confirm('把內建角色還原成預設?(自建角色不受影響)') && useStore.getState().resetBuiltinAgents()}>
<RotateCcw className="w-4 h-4" />
</Btn>
</div>
</Section>
</div>
)
}