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 | 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 2x 2x 2x 4x 10x | import MainPage from "@/components/page/MainPage/MainPage";
import { SimulationScenario, SimulationStatus, SimulationWorld } from "@/objects/Simulation";
import { APP_ROUTES } from "@/routes/const";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { ChevronLeft, Dices, Loader2 } from "lucide-react";
import React, { useState } from "react";
import { Link, useNavigate } from "react-router-dom";
type Strategy = SimulationScenario["strategy"];
const STRATEGIES: Strategy[] = ["cooperative", "competitive", "mixed", "adversarial", "custom"];
const strategyDescriptions: Record<Strategy, string> = {
cooperative: "Agents work together toward a shared goal",
competitive: "Agents compete for resources or outcomes",
mixed: "Some agents cooperate, others compete",
adversarial: "Agents actively try to undermine each other",
custom: "Define your own interaction rules via world logic",
};
const STATUSES: SimulationStatus[] = ["draft", "active"];
const NewScenario: React.FC = () => {
const { getApiHandler } = useApiHandler();
const queryClient = useQueryClient();
const navigate = useNavigate();
const [name, setName] = useState("");
const [description, setDescription] = useState("");
const [strategy, setStrategy] = useState<Strategy>("custom");
const [status, setStatus] = useState<SimulationStatus>("draft");
const [worldId, setWorldId] = useState("");
const { data: worlds = [], isLoading: worldsLoading } = useQuery<SimulationWorld[]>({
queryKey: [QUERY_KEYS.GET.SIMULATION_WORLDS],
queryFn: () => getApiHandler().simulation_router.listWorlds(),
});
const createMutation = useMutation({
mutationFn: () =>
getApiHandler().simulation_router.createScenario(worldId, {
name: name.trim(),
description: description.trim() || undefined,
strategy,
status,
world_id: worldId,
}),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET.SIMULATION_SCENARIOS] });
navigate(APP_ROUTES.USER.INFERENCE.agents);
},
});
const canSubmit = name.trim() && worldId && !createMutation.isPending;
return (
<MainPage title="New Scenario" description="Define an evaluation scenario within a simulation world.">
<Link
to={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-6"
>
<ChevronLeft className="size-3" />
Scenarios
</Link>
<div className="max-w-2xl space-y-5">
{/* Core fields */}
<div className="rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 p-5 space-y-4">
<div className="flex items-center gap-2 pb-2 border-b border-gray-100 dark:border-gray-700">
<Dices className="size-4 text-gray-500 dark:text-gray-400" />
<h3 className="text-sm font-semibold text-gray-700 dark:text-gray-300">Scenario Details</h3>
</div>
<div>
<label className="block text-xs font-medium text-gray-600 dark:text-gray-400 mb-1">
World <span className="text-red-400">*</span>
</label>
<select
className="w-full rounded-lg border border-gray-200 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 px-3 py-2 text-sm text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-gray-400 dark:focus:ring-gray-500"
value={worldId}
onChange={(e) => setWorldId(e.target.value)}
disabled={worldsLoading}
>
<option value="">Select a simulation world…</option>
{worlds.map((w) => (
<option key={w.id} value={w.id}>{w.name}</option>
))}
</select>
{!worldsLoading && worlds.length === 0 && (
<p className="text-xs text-amber-500 mt-1">
No worlds found.{" "}
<Link to={APP_ROUTES.USER.INFERENCE.SIMULATIONS.WORLDS.new} className="underline">
Create a world first.
</Link>
</p>
)}
</div>
<div>
<label className="block text-xs font-medium text-gray-600 dark:text-gray-400 mb-1">
Name <span className="text-red-400">*</span>
</label>
<input
autoFocus
className="w-full rounded-lg border border-gray-200 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 px-3 py-2 text-sm text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-gray-400 dark:focus:ring-gray-500"
placeholder="e.g. Cold-start adversarial attack"
value={name}
onChange={(e) => setName(e.target.value)}
/>
</div>
<div>
<label className="block text-xs font-medium text-gray-600 dark:text-gray-400 mb-1">
Description <span className="text-gray-400">(optional)</span>
</label>
<textarea
rows={3}
className="w-full rounded-lg border border-gray-200 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 px-3 py-2 text-sm text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-gray-400 dark:focus:ring-gray-500 resize-none"
placeholder="What edge case, use case, or hypothesis does this scenario test?"
value={description}
onChange={(e) => setDescription(e.target.value)}
/>
</div>
<div>
<label className="block text-xs font-medium text-gray-600 dark:text-gray-400 mb-1">Status</label>
<div className="flex gap-2">
{STATUSES.map((s) => (
<button
key={s}
onClick={() => setStatus(s)}
className={`px-3 py-1.5 rounded-lg text-xs font-medium border capitalize transition-colors ${
status === s
? "bg-gray-900 dark:bg-white text-white dark:text-gray-900 border-gray-900 dark:border-white"
: "border-gray-200 dark:border-gray-600 text-gray-600 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700"
}`}
>
{s}
</button>
))}
</div>
</div>
</div>
{/* Strategy */}
<div className="rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 p-5">
<div className="flex items-center gap-2 pb-3 mb-4 border-b border-gray-100 dark:border-gray-700">
<h3 className="text-sm font-semibold text-gray-700 dark:text-gray-300">Strategy</h3>
<span className="text-xs text-gray-400 dark:text-gray-500">How agents interact in this scenario</span>
</div>
<div className="grid grid-cols-1 sm:grid-cols-2 gap-2">
{STRATEGIES.map((s) => (
<button
key={s}
onClick={() => setStrategy(s)}
className={`text-left px-4 py-3 rounded-xl border transition-all ${
strategy === s
? "border-gray-900 dark:border-gray-100 bg-gray-50 dark:bg-gray-700"
: "border-gray-200 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700/40"
}`}
>
<div className="text-xs font-semibold text-gray-800 dark:text-gray-200 capitalize mb-0.5">{s}</div>
<div className="text-[11px] text-gray-400 dark:text-gray-500 leading-snug">
{strategyDescriptions[s]}
</div>
</button>
))}
</div>
</div>
{/* Actions */}
<div className="flex items-center justify-end gap-3">
<Link
to={APP_ROUTES.USER.INFERENCE.agents}
className="px-4 py-2 text-sm text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 transition-colors"
>
Cancel
</Link>
<button
disabled={!canSubmit}
onClick={() => createMutation.mutate()}
className="flex items-center gap-1.5 px-5 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"
>
{createMutation.isPending && <Loader2 className="size-3.5 animate-spin" />}
{createMutation.isPending ? "Creating…" : "Create Scenario"}
</button>
</div>
</div>
</MainPage>
);
};
export default NewScenario;
|