Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 2x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | import SubPage from "@/components/page/SubPage/SubPage";
import { Simulation, SimulationRoundResult } from "@/objects/Simulation";
import { APP_ROUTES, ROUTE_PARAMS } from "@/routes/const";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
AlertTriangle,
Bot,
Calendar,
CheckCircle2,
ChevronDown,
ChevronLeft,
ChevronRight,
Clock,
Dices,
Globe,
Loader2,
Play,
Trash2,
Waypoints,
XCircle,
} from "lucide-react";
import React, { useState } from "react";
import { Link, useLocation, useNavigate, useParams } from "react-router-dom";
import {
ConfirmDialog,
LIFECYCLE_STATUS,
Skeleton,
StatusBadge,
Strong,
encodeResourceId,
} from "@/components/detail";
import { parseServerDate } from "@/util/time";
// ─── Primitives ────────────────────────────────────────────────────────────────
// ─── Round card ───────────────────────────────────────────────────────────────
const RoundCard: React.FC<{ round: SimulationRoundResult; agentNames?: Record<string, string> }> = ({
round,
agentNames = {},
}) => {
const [open, setOpen] = useState(round.round_number === 1);
const totalArtifacts = Object.values(round.artifacts ?? {}).reduce((n, a) => n + a.length, 0);
return (
<div className="rounded-lg border border-gray-200 dark:border-gray-700 overflow-hidden">
<button
onClick={() => setOpen((v) => !v)}
className="w-full flex items-center justify-between px-4 py-2.5 bg-gray-50 dark:bg-gray-700/40 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors text-left"
>
<div className="flex items-center gap-2">
<span className="text-xs font-semibold text-gray-700 dark:text-gray-300">
Round {round.round_number}
</span>
{round.terminated && round.termination_reason && (
<span className="text-[10px] px-1.5 py-0.5 rounded bg-amber-100 dark:bg-amber-900/40 text-amber-700 dark:text-amber-300">
{round.termination_reason}
</span>
)}
<span className="text-xs text-gray-400 dark:text-gray-500">
{Object.keys(round.agent_outputs).length} agent(s)
</span>
{totalArtifacts > 0 && (
<span className="text-[10px] px-1.5 py-0.5 rounded bg-indigo-50 dark:bg-indigo-950/40 text-indigo-600 dark:text-indigo-300 border border-indigo-200 dark:border-indigo-800">
{totalArtifacts} artifact{totalArtifacts !== 1 ? "s" : ""}
</span>
)}
</div>
{open ? (
<ChevronDown className="size-3.5 text-gray-400 shrink-0" />
) : (
<ChevronRight className="size-3.5 text-gray-400 shrink-0" />
)}
</button>
{open && (
<div className="divide-y divide-gray-100 dark:divide-gray-700">
{Object.entries(round.agent_outputs).map(([agentId, output]) => {
const artifacts = round.artifacts?.[agentId] ?? [];
return (
<div key={agentId} className="px-4 py-3">
<div className="flex items-center gap-1.5 mb-1.5">
<Bot className="size-3 text-gray-400 shrink-0" />
<span className="text-[10px] font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wide">
{agentNames[agentId] ?? agentId.slice(0, 8) + "…"}
</span>
</div>
<p className="text-sm text-gray-700 dark:text-gray-300 leading-relaxed whitespace-pre-wrap">
{output}
</p>
{artifacts.length > 0 && (
<div className="mt-2.5 space-y-1.5">
<div className="text-[10px] font-medium text-indigo-500 dark:text-indigo-400 uppercase tracking-wide">
Tool artifacts ({artifacts.length})
</div>
{artifacts.map((artifact, i) => (
<pre
key={i}
className="text-xs text-gray-600 dark:text-gray-300 bg-indigo-50/60 dark:bg-indigo-950/20 border border-indigo-100 dark:border-indigo-900 rounded p-2 overflow-auto max-h-32 font-mono leading-relaxed whitespace-pre-wrap"
>
{artifact}
</pre>
))}
</div>
)}
</div>
);
})}
</div>
)}
</div>
);
};
// ─── Delete modal ─────────────────────────────────────────────────────────────
// ─── SimulationViewer ─────────────────────────────────────────────────────────
const SimulationViewer: React.FC = () => {
const { simulationId } = useParams();
const location = useLocation();
const { getApiHandler } = useApiHandler();
const queryClient = useQueryClient();
const navigate = useNavigate();
const [confirmDelete, setConfirmDelete] = useState(false);
const decodedId = simulationId ? atob(simulationId) : "";
// worldId may be passed via router state when navigating from WorldViewer
const hintWorldId: string | undefined = (location.state as Record<string, unknown> | null)?.worldId as string | undefined;
// Load worlds to resolve world context (and scenario/world names)
const { data: worlds = [] } = useQuery({
queryKey: [QUERY_KEYS.GET.SIMULATION_WORLDS],
queryFn: () => getApiHandler().simulation_router.listWorlds(),
});
const { data: simulation, isLoading, error } = useQuery<Simulation | null>({
queryKey: [QUERY_KEYS.GET.SIMULATION_BY_ID, decodedId],
queryFn: async () => {
const api = getApiHandler().simulation_router;
if (hintWorldId) {
const s = await api.getSimulation(hintWorldId, decodedId);
if (s) return s;
}
for (const world of worlds) {
const s = await api.getSimulation(world.id, decodedId);
if (s) return s;
}
return null;
},
enabled: !!decodedId && (!!hintWorldId || worlds.length > 0),
refetchInterval: (query) =>
query.state.data?.status === "running" ? 3000 : false,
});
const world = simulation ? worlds.find((w) => w.id === simulation.world_id) : undefined;
const worldHref = world
? APP_ROUTES.USER.INFERENCE.SIMULATIONS.WORLDS.id.replace(ROUTE_PARAMS.WORLD.id, encodeResourceId(world.id))
: undefined;
const runMutation = useMutation({
mutationFn: () =>
getApiHandler().simulation_router.runSimulation(simulation!.world_id, decodedId),
onSuccess: () =>
queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET.SIMULATION_BY_ID, decodedId] }),
});
const deleteMutation = useMutation({
mutationFn: () =>
getApiHandler().simulation_router.deleteSimulation(simulation!.world_id, decodedId),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET.SIMULATIONS] });
if (world) {
navigate(APP_ROUTES.USER.INFERENCE.SIMULATIONS.WORLDS.id.replace(ROUTE_PARAMS.WORLD.id, encodeResourceId(world.id)));
} else {
navigate(APP_ROUTES.USER.INFERENCE.agents);
}
},
});
Iif (!simulationId) return <div className="p-4 text-red-500">Invalid simulation ID.</div>;
Iif (error) return <div className="p-4 text-red-500">Error: {error.toString()}</div>;
const results = simulation?.results;
const isRunning = simulation?.status === "running" || runMutation.isPending;
const actionLinks = isLoading
? []
: [
<button
key="run"
onClick={() => runMutation.mutate()}
disabled={isRunning}
className="flex items-center gap-1.5 text-xs px-2.5 py-1.5 rounded-lg border border-gray-200 dark:border-gray-600 text-gray-600 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors disabled:opacity-50"
>
{isRunning ? <Loader2 className="size-3.5 animate-spin" /> : <Play className="size-3.5" />}
{isRunning ? "Running…" : "Run"}
</button>,
<button
key="delete"
onClick={() => setConfirmDelete(true)}
className="flex items-center gap-1.5 text-xs px-2.5 py-1.5 rounded-lg border border-red-200 dark:border-red-800 text-red-500 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-950/30 transition-colors"
>
<Trash2 className="size-3.5" />
Delete
</button>,
];
return (
<>
{confirmDelete && simulation && (
<ConfirmDialog
title="Delete Simulation"
icon={Trash2}
body={
<>
Delete <Strong>{simulation.name}</Strong>? This cannot be undone.
</>
}
loading={deleteMutation.isPending}
onConfirm={() => deleteMutation.mutate()}
onCancel={() => setConfirmDelete(false)}
/>
)}
<SubPage
title={isLoading ? <Skeleton className="h-7 w-48 inline-block" /> : simulation?.name ?? "Simulation"}
title_icon={<Waypoints className="size-10 text-indigo-400 dark:text-indigo-500" />}
links={actionLinks as React.ReactNode[]}
>
<Link
to={worldHref ?? APP_ROUTES.USER.INFERENCE.agents}
className="inline-flex items-center gap-1 text-xs text-gray-400 dark:text-gray-500 hover:text-gray-700 dark:hover:text-gray-300 transition-colors mb-4"
>
<ChevronLeft className="size-3" />
{world?.name ?? "Worlds"}
</Link>
{/* Run error banner */}
{runMutation.isError && (
<div className="mb-4 flex items-center gap-2 px-4 py-3 rounded-lg bg-red-50 dark:bg-red-950/30 border border-red-200 dark:border-red-800 text-red-700 dark:text-red-300 text-sm">
<AlertTriangle className="size-4 shrink-0" />
Run failed: {String(runMutation.error)}
</div>
)}
{/* Metadata strip */}
<div className="rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shadow-sm mb-4 p-5">
{isLoading ? (
<div className="grid grid-cols-2 sm:grid-cols-3 gap-5">
{Array.from({ length: 4 }).map((_, i) => (
<div key={i} className="space-y-1.5">
<Skeleton className="h-3 w-16" />
<Skeleton className="h-4 w-24" />
</div>
))}
</div>
) : (
<div className="grid grid-cols-2 sm:grid-cols-3 gap-5">
<div>
<div className="text-[10px] uppercase tracking-wide font-medium text-gray-400 dark:text-gray-500 mb-1">Status</div>
{simulation && <StatusBadge scale={LIFECYCLE_STATUS} status={simulation.status} />}
</div>
<div>
<div className="text-[10px] uppercase tracking-wide font-medium text-gray-400 dark:text-gray-500 mb-1 flex items-center gap-1">
<Calendar className="size-3" /> Created
</div>
<div className="text-sm text-gray-800 dark:text-gray-200">
{simulation ? parseServerDate(simulation.created_at).toLocaleDateString(undefined, { year: "numeric", month: "short", day: "numeric" }) : "—"}
</div>
</div>
{world && worldHref && (
<div>
<div className="text-[10px] uppercase tracking-wide font-medium text-gray-400 dark:text-gray-500 mb-1 flex items-center gap-1">
<Globe className="size-3" /> World
</div>
<Link to={worldHref} className="text-sm text-gray-800 dark:text-gray-200 hover:underline">
{world.name}
</Link>
</div>
)}
{simulation?.started_at && (
<div>
<div className="text-[10px] uppercase tracking-wide font-medium text-gray-400 dark:text-gray-500 mb-1 flex items-center gap-1">
<Clock className="size-3" /> Last Run
</div>
<div className="text-sm text-gray-800 dark:text-gray-200">
{parseServerDate(simulation.started_at).toLocaleString()}
</div>
</div>
)}
{simulation?.agent_ids && simulation.agent_ids.length > 0 && (
<div>
<div className="text-[10px] uppercase tracking-wide font-medium text-gray-400 dark:text-gray-500 mb-1 flex items-center gap-1">
<Bot className="size-3" /> Agents
</div>
<div className="text-sm text-gray-800 dark:text-gray-200">{simulation.agent_ids.length} agent(s)</div>
</div>
)}
{simulation?.description && (
<div className="col-span-2 sm:col-span-3">
<div className="text-[10px] uppercase tracking-wide font-medium text-gray-400 dark:text-gray-500 mb-1">Description</div>
<p className="text-sm text-gray-700 dark:text-gray-300">{simulation.description}</p>
</div>
)}
</div>
)}
</div>
{/* Results */}
{results && (
<div className="rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shadow-sm overflow-hidden">
<div className="flex items-center justify-between gap-2 px-5 py-3.5 border-b border-gray-100 dark:border-gray-700">
<div className="flex items-center gap-2">
<Waypoints className="size-4 text-gray-500 dark:text-gray-400" />
<h3 className="text-sm font-semibold text-gray-700 dark:text-gray-300">Run Results</h3>
{results.status === "completed" ? (
<CheckCircle2 className="size-3.5 text-green-500" />
) : results.status === "failed" ? (
<XCircle className="size-3.5 text-red-500" />
) : null}
</div>
{results.final_summary && (
<span className="text-xs text-gray-400 dark:text-gray-500">{results.final_summary}</span>
)}
</div>
<div className="px-5 py-4 space-y-3">
{results.error && (
<div className="flex items-center gap-2 px-3 py-2 rounded-lg bg-red-50 dark:bg-red-950/30 text-red-700 dark:text-red-300 text-sm">
<AlertTriangle className="size-3.5 shrink-0" />
{results.error}
</div>
)}
{results.rounds.length === 0 && !results.error && (
<p className="text-sm text-gray-400 dark:text-gray-500 text-center py-4">No rounds recorded.</p>
)}
{results.rounds.map((round) => (
<RoundCard key={round.round_number} round={round} />
))}
</div>
</div>
)}
{/* No results yet */}
{!results && !isLoading && simulation && (
<div className="rounded-xl border border-dashed border-gray-200 dark:border-gray-700 bg-gray-50/50 dark:bg-gray-800/30 p-8 text-center">
<Waypoints className="size-8 text-gray-200 dark:text-gray-600 mx-auto mb-3" />
<p className="text-sm text-gray-400 dark:text-gray-500 mb-3">
This simulation hasn't been run yet.
</p>
<button
onClick={() => runMutation.mutate()}
disabled={isRunning}
className="inline-flex items-center gap-1.5 px-4 py-2 rounded-lg bg-gray-900 dark:bg-white text-white dark:text-gray-900 text-sm font-medium hover:opacity-80 transition-opacity disabled:opacity-40"
>
{isRunning ? <Loader2 className="size-3.5 animate-spin" /> : <Play className="size-3.5" />}
{isRunning ? "Running…" : "Run Simulation"}
</button>
</div>
)}
</SubPage>
</>
);
};
export default SimulationViewer;
|