|
|
@@ -4,8 +4,9 @@ import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
|
|
import {
|
|
|
ArrowLeft, Send, Square, ChevronDown, ChevronRight,
|
|
|
Bot, User, Clock, Brain, Folder, Download, Trash2, X,
|
|
|
+ Save, RotateCcw, History, Key,
|
|
|
} from 'lucide-react'
|
|
|
-import { agentsApi, type WorkspaceFile, type MemoryEntry } from '../api/agents'
|
|
|
+import { agentsApi, type WorkspaceFile, type MemoryEntry, type CoreMemory, type RecallEntry } from '../api/agents'
|
|
|
import { useAppStore } from '../store/app'
|
|
|
import { Spinner } from '../components/ui'
|
|
|
import { clsx } from '../lib/clsx'
|
|
|
@@ -174,31 +175,85 @@ function MessageBubble({ msg, agentId, apiKey }: { msg: Message; agentId: string
|
|
|
|
|
|
// ── Memory panel ──────────────────────────────────────────────────
|
|
|
|
|
|
-const TIER_STYLES: Record<string, string> = {
|
|
|
- semantic: 'bg-blue-100 text-blue-700',
|
|
|
- episodic: 'bg-yellow-100 text-yellow-700',
|
|
|
- working: 'bg-gray-100 text-gray-600',
|
|
|
-}
|
|
|
-
|
|
|
function MemoryPanel({ agentId, onClose }: { agentId: string; onClose: () => void }) {
|
|
|
const qc = useQueryClient()
|
|
|
- const { data, isLoading } = useQuery({
|
|
|
- queryKey: ['memory', agentId],
|
|
|
- queryFn: () => agentsApi.memory(agentId),
|
|
|
+ const [tab, setTab] = useState<'core' | 'recall'>('core')
|
|
|
+
|
|
|
+ // ── Core Memory (Layer 1) ──
|
|
|
+ const { data: coreData, isLoading: coreLoading } = useQuery({
|
|
|
+ queryKey: ['memory-core', agentId],
|
|
|
+ queryFn: () => agentsApi.coreMemory(agentId),
|
|
|
+ refetchInterval: tab === 'core' ? 5000 : false,
|
|
|
})
|
|
|
|
|
|
- const deleteMut = useMutation({
|
|
|
- mutationFn: (key: string) => agentsApi.deleteMemory(agentId, key),
|
|
|
- onSuccess: () => qc.invalidateQueries({ queryKey: ['memory', agentId] }),
|
|
|
+ // Local editing state — a copy of the core dict for editing
|
|
|
+ const [editing, setEditing] = useState<CoreMemory | null>(null)
|
|
|
+ const [newKey, setNewKey] = useState('')
|
|
|
+ const [newVal, setNewVal] = useState('')
|
|
|
+
|
|
|
+ // Sync editing state when remote data arrives (only if not actively editing)
|
|
|
+ const prevCoreRef = useRef<CoreMemory | null>(null)
|
|
|
+ useEffect(() => {
|
|
|
+ if (coreData && !editing) {
|
|
|
+ prevCoreRef.current = coreData.core
|
|
|
+ }
|
|
|
+ }, [coreData, editing])
|
|
|
+
|
|
|
+ const updateCoreMut = useMutation({
|
|
|
+ mutationFn: (core: CoreMemory) => agentsApi.updateCoreMemory(agentId, core),
|
|
|
+ onSuccess: () => {
|
|
|
+ setEditing(null)
|
|
|
+ qc.invalidateQueries({ queryKey: ['memory-core', agentId] })
|
|
|
+ },
|
|
|
})
|
|
|
|
|
|
- function fmt(ts: number) {
|
|
|
- return new Date(ts * 1000).toLocaleString('zh-CN', {
|
|
|
- month: 'numeric', day: 'numeric', hour: '2-digit', minute: '2-digit',
|
|
|
+ const clearCoreMut = useMutation({
|
|
|
+ mutationFn: () => agentsApi.clearCoreMemory(agentId),
|
|
|
+ onSuccess: () => {
|
|
|
+ setEditing(null)
|
|
|
+ qc.invalidateQueries({ queryKey: ['memory-core', agentId] })
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+ const liveCore: CoreMemory = editing ?? coreData?.core ?? {}
|
|
|
+ const coreEntries = Object.entries(liveCore)
|
|
|
+
|
|
|
+ function startEdit() { setEditing({ ...coreData?.core }) }
|
|
|
+ function cancelEdit() { setEditing(null); setNewKey(''); setNewVal('') }
|
|
|
+
|
|
|
+ function addPair() {
|
|
|
+ if (!newKey.trim()) return
|
|
|
+ setEditing(prev => ({ ...(prev ?? {}), [newKey.trim()]: newVal }))
|
|
|
+ setNewKey(''); setNewVal('')
|
|
|
+ }
|
|
|
+
|
|
|
+ function removePair(key: string) {
|
|
|
+ setEditing(prev => {
|
|
|
+ const next = { ...(prev ?? {}) }
|
|
|
+ delete next[key]
|
|
|
+ return next
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- const entries = data?.entries ?? []
|
|
|
+ function updateValue(key: string, val: string) {
|
|
|
+ setEditing(prev => ({ ...(prev ?? {}), [key]: val }))
|
|
|
+ }
|
|
|
+
|
|
|
+ // ── Recall Memory (Layer 2) ──
|
|
|
+ const { data: recallData, isLoading: recallLoading } = useQuery({
|
|
|
+ queryKey: ['memory-recall', agentId],
|
|
|
+ queryFn: () => agentsApi.recallMemory(agentId, 30),
|
|
|
+ enabled: tab === 'recall',
|
|
|
+ refetchInterval: tab === 'recall' ? 10000 : false,
|
|
|
+ })
|
|
|
+
|
|
|
+ const recallEntries = recallData?.entries ?? []
|
|
|
+
|
|
|
+ function fmtTs(ts: string) {
|
|
|
+ return new Date(ts).toLocaleString('zh-CN', {
|
|
|
+ month: 'numeric', day: 'numeric', hour: '2-digit', minute: '2-digit',
|
|
|
+ })
|
|
|
+ }
|
|
|
|
|
|
return (
|
|
|
<div className="absolute top-0 right-0 h-full w-80 bg-white border-l border-gray-200 flex flex-col z-10 shadow-xl">
|
|
|
@@ -207,55 +262,190 @@ function MemoryPanel({ agentId, onClose }: { agentId: string; onClose: () => voi
|
|
|
<div className="flex items-center gap-2">
|
|
|
<Brain size={15} className="text-indigo-500" />
|
|
|
<span className="text-sm font-semibold text-gray-900">智能体记忆</span>
|
|
|
- {data && (
|
|
|
- <span className="text-xs bg-gray-100 text-gray-500 px-1.5 py-0.5 rounded-full">
|
|
|
- {data.count}
|
|
|
- </span>
|
|
|
- )}
|
|
|
</div>
|
|
|
<button onClick={onClose} className="text-gray-400 hover:text-gray-700 transition-colors">
|
|
|
<X size={16} />
|
|
|
</button>
|
|
|
</div>
|
|
|
|
|
|
- {/* Body */}
|
|
|
- <div className="flex-1 overflow-y-auto p-3 space-y-2">
|
|
|
- {isLoading && (
|
|
|
- <div className="flex justify-center py-8"><Spinner size={24} /></div>
|
|
|
- )}
|
|
|
- {!isLoading && entries.length === 0 && (
|
|
|
- <div className="text-center py-10 text-sm text-gray-400">
|
|
|
- <Brain size={32} className="mx-auto mb-2 opacity-30" />
|
|
|
- 该智能体暂无记忆
|
|
|
+ {/* Tabs */}
|
|
|
+ <div className="flex border-b border-gray-200 shrink-0">
|
|
|
+ <button
|
|
|
+ onClick={() => { setTab('core'); cancelEdit() }}
|
|
|
+ className={clsx(
|
|
|
+ 'flex-1 flex items-center justify-center gap-1.5 py-2 text-xs font-medium transition-colors',
|
|
|
+ tab === 'core'
|
|
|
+ ? 'text-indigo-600 border-b-2 border-indigo-500'
|
|
|
+ : 'text-gray-500 hover:text-gray-700'
|
|
|
+ )}
|
|
|
+ >
|
|
|
+ <Key size={12} /> 核心记忆
|
|
|
+ </button>
|
|
|
+ <button
|
|
|
+ onClick={() => { setTab('recall'); cancelEdit() }}
|
|
|
+ className={clsx(
|
|
|
+ 'flex-1 flex items-center justify-center gap-1.5 py-2 text-xs font-medium transition-colors',
|
|
|
+ tab === 'recall'
|
|
|
+ ? 'text-indigo-600 border-b-2 border-indigo-500'
|
|
|
+ : 'text-gray-500 hover:text-gray-700'
|
|
|
+ )}
|
|
|
+ >
|
|
|
+ <History size={12} /> 近期对话
|
|
|
+ {recallData && recallData.total > 0 && (
|
|
|
+ <span className="bg-gray-100 text-gray-500 px-1.5 py-0.5 rounded-full text-xs">
|
|
|
+ {recallData.total}
|
|
|
+ </span>
|
|
|
+ )}
|
|
|
+ </button>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ {/* ── Core Memory Tab ── */}
|
|
|
+ {tab === 'core' && (
|
|
|
+ <div className="flex-1 flex flex-col min-h-0">
|
|
|
+ {/* Toolbar */}
|
|
|
+ <div className="flex items-center gap-2 px-3 py-2 border-b border-gray-100 shrink-0">
|
|
|
+ {!editing ? (
|
|
|
+ <>
|
|
|
+ <button
|
|
|
+ onClick={startEdit}
|
|
|
+ className="flex items-center gap-1 text-xs text-indigo-600 hover:text-indigo-800 font-medium"
|
|
|
+ >
|
|
|
+ <Key size={11} /> 编辑
|
|
|
+ </button>
|
|
|
+ <span className="text-gray-200">|</span>
|
|
|
+ <button
|
|
|
+ onClick={() => clearCoreMut.mutate()}
|
|
|
+ disabled={clearCoreMut.isPending || coreEntries.length === 0}
|
|
|
+ className="flex items-center gap-1 text-xs text-red-500 hover:text-red-700 disabled:opacity-30"
|
|
|
+ >
|
|
|
+ <Trash2 size={11} /> 清空
|
|
|
+ </button>
|
|
|
+ </>
|
|
|
+ ) : (
|
|
|
+ <>
|
|
|
+ <button
|
|
|
+ onClick={() => updateCoreMut.mutate(liveCore)}
|
|
|
+ disabled={updateCoreMut.isPending}
|
|
|
+ className="flex items-center gap-1 text-xs text-green-600 hover:text-green-800 font-medium"
|
|
|
+ >
|
|
|
+ <Save size={11} /> 保存
|
|
|
+ </button>
|
|
|
+ <span className="text-gray-200">|</span>
|
|
|
+ <button
|
|
|
+ onClick={cancelEdit}
|
|
|
+ className="flex items-center gap-1 text-xs text-gray-500 hover:text-gray-700"
|
|
|
+ >
|
|
|
+ <RotateCcw size={11} /> 取消
|
|
|
+ </button>
|
|
|
+ </>
|
|
|
+ )}
|
|
|
+ <span className="ml-auto text-xs text-gray-400">
|
|
|
+ {coreEntries.length} 个字段
|
|
|
+ </span>
|
|
|
</div>
|
|
|
- )}
|
|
|
- {entries.map(entry => (
|
|
|
- <div key={entry.key} className="border border-gray-100 rounded-lg p-3 space-y-2 group hover:border-gray-200 transition-colors">
|
|
|
- <div className="flex items-start justify-between gap-2">
|
|
|
- <div className="flex flex-wrap gap-1">
|
|
|
- <span className={clsx('text-xs px-1.5 py-0.5 rounded-full font-medium', TIER_STYLES[entry.tier] ?? TIER_STYLES.working)}>
|
|
|
- {entry.tier}
|
|
|
- </span>
|
|
|
- {entry.tags.map(t => (
|
|
|
- <span key={t} className="text-xs bg-gray-100 text-gray-500 px-1.5 py-0.5 rounded-full">{t}</span>
|
|
|
- ))}
|
|
|
+
|
|
|
+ {/* Key-value list */}
|
|
|
+ <div className="flex-1 overflow-y-auto p-3 space-y-2">
|
|
|
+ {coreLoading && (
|
|
|
+ <div className="flex justify-center py-8"><Spinner size={24} /></div>
|
|
|
+ )}
|
|
|
+ {!coreLoading && coreEntries.length === 0 && !editing && (
|
|
|
+ <div className="text-center py-10 text-sm text-gray-400">
|
|
|
+ <Brain size={32} className="mx-auto mb-2 opacity-30" />
|
|
|
+ <p>暂无核心记忆</p>
|
|
|
+ <p className="text-xs mt-1">Agent 运行后会自动填充</p>
|
|
|
</div>
|
|
|
- <button
|
|
|
- onClick={() => deleteMut.mutate(entry.key)}
|
|
|
- disabled={deleteMut.isPending}
|
|
|
- className="opacity-0 group-hover:opacity-100 text-gray-300 hover:text-red-500 transition-all shrink-0"
|
|
|
- title="删除"
|
|
|
- >
|
|
|
- <Trash2 size={13} />
|
|
|
- </button>
|
|
|
- </div>
|
|
|
- <p className="text-xs text-gray-700 line-clamp-4 leading-relaxed">
|
|
|
- {entry.content}
|
|
|
- </p>
|
|
|
- <p className="text-xs text-gray-400">{fmt(entry.created_at)}</p>
|
|
|
+ )}
|
|
|
+ {coreEntries.map(([key, val]) => (
|
|
|
+ <div key={key} className="group rounded-lg border border-gray-100 bg-gray-50 p-2.5">
|
|
|
+ <div className="flex items-center justify-between gap-1 mb-1">
|
|
|
+ <span className="text-xs font-mono font-semibold text-indigo-600 truncate">{key}</span>
|
|
|
+ {editing && (
|
|
|
+ <button
|
|
|
+ onClick={() => removePair(key)}
|
|
|
+ className="text-gray-300 hover:text-red-500 opacity-0 group-hover:opacity-100 transition-all shrink-0"
|
|
|
+ >
|
|
|
+ <Trash2 size={11} />
|
|
|
+ </button>
|
|
|
+ )}
|
|
|
+ </div>
|
|
|
+ {editing ? (
|
|
|
+ <textarea
|
|
|
+ value={typeof val === 'string' ? val : JSON.stringify(val, null, 2)}
|
|
|
+ onChange={e => updateValue(key, e.target.value)}
|
|
|
+ rows={2}
|
|
|
+ className="w-full text-xs text-gray-700 bg-white border border-gray-200 rounded p-1.5 resize-y font-mono focus:outline-none focus:border-indigo-300"
|
|
|
+ />
|
|
|
+ ) : (
|
|
|
+ <p className="text-xs text-gray-700 line-clamp-3 leading-relaxed">
|
|
|
+ {typeof val === 'string' ? val : JSON.stringify(val)}
|
|
|
+ </p>
|
|
|
+ )}
|
|
|
+ </div>
|
|
|
+ ))}
|
|
|
+
|
|
|
+ {/* Add new key-value pair (edit mode only) */}
|
|
|
+ {editing && (
|
|
|
+ <div className="rounded-lg border border-dashed border-indigo-200 bg-indigo-50 p-2.5 space-y-2">
|
|
|
+ <p className="text-xs font-medium text-indigo-600">添加字段</p>
|
|
|
+ <input
|
|
|
+ value={newKey}
|
|
|
+ onChange={e => setNewKey(e.target.value)}
|
|
|
+ placeholder="字段名"
|
|
|
+ className="w-full text-xs border border-gray-200 rounded px-2 py-1 font-mono focus:outline-none focus:border-indigo-300"
|
|
|
+ />
|
|
|
+ <textarea
|
|
|
+ value={newVal}
|
|
|
+ onChange={e => setNewVal(e.target.value)}
|
|
|
+ placeholder="字段值"
|
|
|
+ rows={2}
|
|
|
+ className="w-full text-xs border border-gray-200 rounded px-2 py-1 resize-y focus:outline-none focus:border-indigo-300"
|
|
|
+ />
|
|
|
+ <button
|
|
|
+ onClick={addPair}
|
|
|
+ disabled={!newKey.trim()}
|
|
|
+ className="text-xs text-indigo-600 font-medium hover:text-indigo-800 disabled:opacity-30"
|
|
|
+ >
|
|
|
+ + 添加
|
|
|
+ </button>
|
|
|
+ </div>
|
|
|
+ )}
|
|
|
</div>
|
|
|
- ))}
|
|
|
- </div>
|
|
|
+ </div>
|
|
|
+ )}
|
|
|
+
|
|
|
+ {/* ── Recall Memory Tab ── */}
|
|
|
+ {tab === 'recall' && (
|
|
|
+ <div className="flex-1 overflow-y-auto p-3 space-y-2">
|
|
|
+ {recallLoading && (
|
|
|
+ <div className="flex justify-center py-8"><Spinner size={24} /></div>
|
|
|
+ )}
|
|
|
+ {!recallLoading && recallEntries.length === 0 && (
|
|
|
+ <div className="text-center py-10 text-sm text-gray-400">
|
|
|
+ <History size={32} className="mx-auto mb-2 opacity-30" />
|
|
|
+ <p>暂无对话记录</p>
|
|
|
+ </div>
|
|
|
+ )}
|
|
|
+ {recallEntries.map((entry, i) => (
|
|
|
+ <div key={entry.run_id + i} className="border border-gray-100 rounded-lg p-2.5 space-y-1.5">
|
|
|
+ <div className="flex items-center justify-between gap-2">
|
|
|
+ <span className="text-xs font-mono text-gray-400 truncate">{entry.run_id}</span>
|
|
|
+ <span className="text-xs text-gray-400 shrink-0">{fmtTs(entry.ts)}</span>
|
|
|
+ </div>
|
|
|
+ <div className="space-y-1">
|
|
|
+ <div className="flex gap-1.5">
|
|
|
+ <span className="text-xs font-medium text-blue-600 shrink-0">Q:</span>
|
|
|
+ <p className="text-xs text-gray-600 line-clamp-2 leading-relaxed">{entry.input}</p>
|
|
|
+ </div>
|
|
|
+ <div className="flex gap-1.5">
|
|
|
+ <span className="text-xs font-medium text-green-600 shrink-0">A:</span>
|
|
|
+ <p className="text-xs text-gray-600 line-clamp-2 leading-relaxed">{entry.output}</p>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ ))}
|
|
|
+ </div>
|
|
|
+ )}
|
|
|
</div>
|
|
|
)
|
|
|
}
|