161 lines
6.0 KiB
TypeScript
161 lines
6.0 KiB
TypeScript
"use client"
|
|
|
|
import { useEffect, useRef, useState } from "react"
|
|
|
|
/**
|
|
* MessageFeed — Kernfeature 2: rollierendes Spool-Journal.
|
|
* Zeigt from/to/priority/status je Eintrag; escalations rot hervorgehoben;
|
|
* Filter nach type und agent. Poll 5 s mit Epoch-Cursor (nur neue Eintraege).
|
|
*/
|
|
|
|
type JournalLine = {
|
|
event?: string
|
|
task_id?: string
|
|
from?: string
|
|
to?: string
|
|
type?: string
|
|
priority?: string
|
|
status?: string
|
|
file?: string
|
|
time_epoch?: number
|
|
errors?: string
|
|
raw?: string
|
|
[k: string]: unknown
|
|
}
|
|
|
|
const EVENT_COLOR: Record<string, string> = {
|
|
accepted: "var(--status-running)",
|
|
rejected: "var(--status-stopped)",
|
|
run_finished: "var(--nvidia-green)",
|
|
escalation: "#e5534b",
|
|
}
|
|
|
|
export default function MessageFeed() {
|
|
const [lines, setLines] = useState<JournalLine[]>([])
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [typeFilter, setTypeFilter] = useState<string>("all")
|
|
const [agentFilter, setAgentFilter] = useState<string>("all")
|
|
const cursorRef = useRef(0)
|
|
|
|
useEffect(() => {
|
|
let cancelled = false
|
|
const load = () => {
|
|
const url = `/api/fleet/messages?limit=200${cursorRef.current > 0 ? `&cursor=${cursorRef.current}` : ""}`
|
|
fetch(url, { cache: "no-store" })
|
|
.then(async (res) => {
|
|
if (!res.ok) throw new Error((await res.json()).error || `HTTP ${res.status}`)
|
|
return res.json()
|
|
})
|
|
.then((d: { messages: JournalLine[] }) => {
|
|
if (cancelled) return
|
|
if (d.messages.length > 0) {
|
|
const maxEpoch = Math.max(...d.messages.map((m) => Number(m.time_epoch || 0)))
|
|
if (maxEpoch > cursorRef.current) cursorRef.current = maxEpoch
|
|
setLines((prev) => {
|
|
const seen = new Set(prev.map((l) => `${l.task_id}:${l.event}:${l.time_epoch}`))
|
|
const fresh = d.messages.filter((l) => !seen.has(`${l.task_id}:${l.event}:${l.time_epoch}`))
|
|
return [...prev, ...fresh].slice(-500)
|
|
})
|
|
}
|
|
setError(null)
|
|
})
|
|
.catch((e: Error) => {
|
|
if (!cancelled) setError(e.message)
|
|
})
|
|
}
|
|
load()
|
|
const timer = setInterval(load, 5000)
|
|
return () => {
|
|
cancelled = true
|
|
clearInterval(timer)
|
|
}
|
|
}, [])
|
|
|
|
const visible = lines.filter((l) => {
|
|
if (typeFilter !== "all" && l.type !== typeFilter && l.event !== typeFilter) return false
|
|
if (agentFilter !== "all" && l.from !== agentFilter && l.to !== agentFilter) return false
|
|
return true
|
|
})
|
|
|
|
if (error) {
|
|
return (
|
|
<div className="panel p-8 text-center" data-testid="fleet-messages-error">
|
|
<h3 className="text-sm font-semibold text-[var(--status-stopped)] uppercase tracking-wider">
|
|
Spool-Journal nicht erreichbar
|
|
</h3>
|
|
<p className="text-xs text-[var(--foreground-dim)] mt-2 font-mono break-all">{error}</p>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<div className="panel p-4 flex flex-wrap gap-3 items-center">
|
|
<h1 className="text-lg font-semibold text-[var(--nvidia-green)] uppercase tracking-wider mr-auto">
|
|
MESSAGES
|
|
</h1>
|
|
<select
|
|
value={typeFilter}
|
|
onChange={(e) => setTypeFilter(e.target.value)}
|
|
className="bg-[var(--background-tertiary)] text-xs font-mono px-2 py-1 rounded-sm"
|
|
data-testid="fleet-msg-type-filter"
|
|
>
|
|
{["all", "escalation", "chat", "build", "review", "vote", "fyi", "accepted", "rejected", "run_finished"].map(
|
|
(t) => (
|
|
<option key={t} value={t}>
|
|
{t}
|
|
</option>
|
|
)
|
|
)}
|
|
</select>
|
|
<select
|
|
value={agentFilter}
|
|
onChange={(e) => setAgentFilter(e.target.value)}
|
|
className="bg-[var(--background-tertiary)] text-xs font-mono px-2 py-1 rounded-sm"
|
|
data-testid="fleet-msg-agent-filter"
|
|
>
|
|
{["all", "ceo", "cto", "ciso", "chro", "coo", "cmo", "agile-coach", "research", "devops"].map((a) => (
|
|
<option key={a} value={a}>
|
|
{a}
|
|
</option>
|
|
))}
|
|
</select>
|
|
<span className="text-[10px] text-[var(--foreground-dim)] font-mono">{visible.length} Eintraege</span>
|
|
</div>
|
|
<div className="panel p-4 max-h-[70vh] overflow-y-auto" data-testid="fleet-message-feed">
|
|
{visible.length === 0 && (
|
|
<p className="text-xs text-[var(--foreground-dim)] font-mono py-8 text-center">
|
|
Keine Eintraege (Filter oder wartet auf ersten Task)
|
|
</p>
|
|
)}
|
|
{visible.map((l, i) => {
|
|
const key = `${l.task_id}-${l.event}-${l.time_epoch}-${i}`
|
|
const isEsc = l.type === "escalation" || l.event === "rejected"
|
|
return (
|
|
<div
|
|
key={key}
|
|
className="border-b border-[var(--border)] py-2 font-mono text-xs flex gap-3 items-baseline"
|
|
data-fleet-message={l.event}
|
|
style={isEsc ? { borderLeft: "3px solid #e5534b", paddingLeft: "8px" } : undefined}
|
|
>
|
|
<span className="text-[var(--foreground-dim)] w-20 shrink-0">
|
|
{l.time_epoch ? new Date(l.time_epoch * 1000).toLocaleTimeString("de-DE") : "--:--"}
|
|
</span>
|
|
<span style={{ color: EVENT_COLOR[l.event || ""] || "var(--foreground)" }} className="w-24 shrink-0">
|
|
{l.event || "?"}
|
|
</span>
|
|
<span className="w-16 shrink-0">{l.from || "system"}</span>
|
|
<span className="text-[var(--foreground-dim)] w-4 shrink-0">→</span>
|
|
<span className="w-16 shrink-0">{l.to || "-"}</span>
|
|
<span className="w-10 shrink-0 text-[var(--foreground-dim)]">{l.priority || ""}</span>
|
|
<span className="w-16 shrink-0 text-[var(--foreground-dim)]">{l.type || ""}</span>
|
|
<span className="w-20 shrink-0 text-[var(--foreground-dim)]">{l.status || ""}</span>
|
|
<span className="truncate text-[var(--foreground-dim)]">{l.task_id?.slice(0, 8) || l.file || l.raw || ""}</span>
|
|
</div>
|
|
)
|
|
})}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|