Spaces:
Sleeping
Sleeping
File size: 8,972 Bytes
5fe93dd | 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 | import React, { useState, useEffect } from 'react';
import { useApp } from '../context/AppContext';
import { config } from '../config';
const DynamicScenarioInjector = ({ scenario }) => {
const { globalMaxSteps, setGlobalMaxSteps } = useApp();
const [mode, setMode] = useState('simple'); // 'simple' or 'structured'
// Auto-update injector fields when scenario prop changes
useEffect(() => {
if (scenario && scenario.id) {
setSimpleText(`name: ${scenario.id}\ncontext: ${scenario.context || ''}\nsymptoms: ${scenario.description || ''}\nobjective: Identify the root cause and propose a verified fix.`);
setJsonInput(JSON.stringify({
task: "custom-incident",
custom_scenario: scenario
}, null, 2));
}
}, [scenario]);
const [simpleText, setSimpleText] = useState(`name: Database Latency Issue
context: The API connects to a cloud database cluster.
symptoms: Users report 5s delays on GET /users.
objective: Identify if a missing index or connection pool exhaustion is the cause.`);
const [jsonInput, setJsonInput] = useState(`{
"task": "software-incident",
"custom_scenario": {
"id": "custom-scenario-1",
"description": "Slow API response times.",
"context": "Cloud DB cluster.",
"difficulty": "medium",
"clue_map": {
"check_service_status:database": "CPU is high."
}
}
}`);
const [submitting, setSubmitting] = useState(false);
const [feedback, setFeedback] = useState("");
const handleFileUpload = (e) => {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (e) => {
try {
const parsed = JSON.parse(e.target.result);
setMode('structured');
if (!parsed.custom_scenario) {
setJsonInput(JSON.stringify({ task: parsed.task || "software-incident", custom_scenario: parsed }, null, 2));
} else {
setJsonInput(JSON.stringify(parsed, null, 2));
}
} catch (err) {
setFeedback("Invalid JSON file uploaded.");
}
};
reader.readAsText(file);
};
const submitScenario = async () => {
setSubmitting(true);
setFeedback("");
try {
let payload;
if (mode === 'simple') {
payload = {
task: "custom-incident",
custom_scenario: {
id: `id_${Date.now()}`,
description: simpleText,
context: "Manually injected scenario description.",
difficulty: "medium",
clue_map: {}
},
max_steps: globalMaxSteps
};
} else {
payload = JSON.parse(jsonInput);
payload.max_steps = globalMaxSteps;
}
const res = await fetch(`${config.API_BASE}/reset`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload)
});
if (res.ok) {
setFeedback("Scenario injected successfully.");
} else {
setFeedback(`Error: ${res.statusText}`);
}
} catch (e) {
setFeedback(`Error: ${e.message}`);
}
setSubmitting(false);
};
return (
<section className="bg-surface-container-low/40 backdrop-blur-md rounded-lg p-5 border border-primary/20 refractive-edge">
<div className="flex justify-between items-center mb-4">
<div className="flex items-center gap-2">
<span className="material-symbols-outlined text-primary text-sm">data_object</span>
<h3 className="text-xs font-bold font-headline tracking-widest uppercase text-primary">Scenario Configurator</h3>
</div>
<div className="flex bg-surface-container-highest rounded p-1 border border-white/5">
<button
onClick={() => setMode('simple')}
className={`px-3 py-1 text-[9px] font-bold rounded transition-all ${mode === 'simple' ? 'bg-primary text-surface shadow-lg' : 'text-on-surface/60 hover:text-on-surface'}`}
>
SIMPLE (TEXT)
</button>
<button
onClick={() => setMode('structured')}
className={`px-3 py-1 text-[9px] font-bold rounded transition-all ${mode === 'structured' ? 'bg-primary text-surface shadow-lg' : 'text-on-surface/60 hover:text-on-surface'}`}
>
STRUCTURED (JSON)
</button>
</div>
</div>
{mode === 'simple' ? (
<textarea
className="w-full h-48 bg-surface-container-lowest text-on-surface font-mono text-[11px] p-3 rounded border border-white/5 focus:border-primary/50 focus:outline-none mb-4 leading-relaxed"
value={simpleText}
onChange={(e) => setSimpleText(e.target.value)}
placeholder="Type your scenario description here... (e.g. Symptoms, Objective, etc.)"
/>
) : (
<textarea
className="w-full h-48 bg-surface-container-lowest text-primary font-mono text-[10px] p-3 rounded border border-white/5 focus:border-primary/50 focus:outline-none mb-4"
value={jsonInput}
onChange={(e) => setJsonInput(e.target.value)}
spellCheck={false}
/>
)}
<div className="flex flex-wrap justify-between items-center gap-4">
<div className="flex items-center gap-4 flex-1 min-w-0">
<input
type="file"
accept=".json"
onChange={handleFileUpload}
className="text-[9px] w-full max-w-48 truncate file:mr-4 file:py-1 file:px-3 file:rounded file:border-0 file:text-[9px] file:font-semibold file:bg-surface-container-highest file:text-on-surface hover:file:bg-primary/20 cursor-pointer"
/>
<span className={`text-[10px] font-mono truncate ${feedback.includes('Error') ? 'text-error' : 'text-tertiary'}`}>
{feedback}
</span>
</div>
<div className="flex items-center gap-4 bg-surface-container-highest p-2 rounded border border-white/5 mx-auto lg:mx-0">
<div className="flex flex-col">
<span className="text-[9px] font-mono uppercase text-on-surface-variant flex justify-between">
<span>Max Steps</span>
<span className="text-primary font-bold">{globalMaxSteps}</span>
</span>
<input
type="range"
min="1"
max="100"
value={globalMaxSteps}
onChange={e => setGlobalMaxSteps(parseInt(e.target.value))}
className="w-32 accent-primary h-1 bg-surface-container-lowest rounded-lg appearance-none cursor-pointer mt-1"
/>
</div>
</div>
<div className="flex flex-wrap items-center gap-3 shrink-0">
<button
onClick={submitScenario}
disabled={submitting}
className="px-6 py-2 bg-primary/20 hover:bg-primary/30 text-primary border border-primary/50 rounded font-headline text-xs font-bold uppercase tracking-wider transition-colors disabled:opacity-50 shadow-[0_0_15px_rgba(0,212,255,0.1)] active:scale-95 whitespace-nowrap"
>
{submitting ? 'Injecting...' : 'Inject & Reset'}
</button>
<button
onClick={() => fetch(`${config.API_BASE}/start-simulation`, { method: "POST" })}
className="px-4 py-2 bg-secondary/10 hover:bg-secondary/20 text-secondary border border-secondary/30 rounded font-headline text-xs font-bold uppercase tracking-wider transition-colors shadow-[0_0_15px_rgba(221,183,255,0.05)] active:scale-95 whitespace-nowrap"
>
+ START FULL SIMULATION
</button>
</div>
</div>
</section>
);
};
export default DynamicScenarioInjector;
|