import re with open('/Users/amritha/Desktop/work/intern/migratron-ui/src/components/Dashboard.tsx', 'r') as f: content = f.read() # 1. Update Mode content = content.replace( "const [mode, setMode] = useState<'eval' | 'inference'>('eval');", "const [mode, setMode] = useState<'demo' | 'eval' | 'inference'>('demo');\n const [terminalLogs, setTerminalLogs] = useState<{type: string, message?: string, step?: number, tool_name?: string, tool_args?: any}[]>([]);\n const [agentReasoning, setAgentReasoning] = useState<{step: number, tool_name: string, tool_args: any, result?: string}[]>([]);\n const [generatedPatch, setGeneratedPatch] = useState(null);\n const [reward, setReward] = useState(0);\n const [ws, setWs] = useState(null);" ) # 2. Update mode derived variables content = content.replace( "const repoName = mode === 'eval' ? selectedTask?.repo_name : (isInferenceLoaded ? inferenceRepoUrl.replace('https://github.com/', '').replace('http://github.com/', '') : null);", "const repoName = (mode === 'eval' || mode === 'demo') ? selectedTask?.repo_name : (isInferenceLoaded ? inferenceRepoUrl.replace('https://github.com/', '').replace('http://github.com/', '') : null);" ) content = content.replace( "const commitHash = mode === 'eval' ? selectedTask?.commit_hash : (isInferenceLoaded ? inferenceCommitHash : null);", "const commitHash = (mode === 'eval' || mode === 'demo') ? selectedTask?.commit_hash : (isInferenceLoaded ? inferenceCommitHash : null);" ) # 3. Update useEffect for timer timer_old = """ useEffect(() => { let timer: ReturnType; if (simulationState === 'running') { timer = setInterval(() => { setSimulationStep(prev => { if (prev >= 6) { setSimulationState('completed'); clearInterval(timer); return prev; } return prev + 1; }); }, 1500); } return () => clearInterval(timer); }, [simulationState]);""" timer_new = """ useEffect(() => { let timer: ReturnType; if (simulationState === 'running' && mode === 'demo') { timer = setInterval(() => { setSimulationStep(prev => { if (prev >= 6) { setSimulationState('completed'); clearInterval(timer); return prev; } return prev + 1; }); }, 1500); } return () => clearInterval(timer); }, [simulationState, mode]); // Cleanup WS on unmount useEffect(() => { return () => { if (ws) { ws.close(); } }; }, [ws]);""" content = content.replace(timer_old, timer_new) # 4. Update handleStartEpisode start_old = """ const handleStartEpisode = () => { if (selectedFile && repoName) { setSimulationState('running'); setSimulationStep(0); setActiveTab('code'); } };""" start_new = """ const handleStartEpisode = () => { if (selectedFile && repoName) { setSimulationState('running'); setSimulationStep(0); setActiveTab('code'); if (mode !== 'demo') { setTerminalLogs([]); setAgentReasoning([]); setGeneratedPatch(null); setReward(0); const socket = new WebSocket(`ws://${window.location.host}/ws/simulate`); setWs(socket); socket.onopen = () => { socket.send(JSON.stringify({ repo: repoName, commit: commitHash, mode: mode, task_index: selectedTaskIndex !== null ? selectedTaskIndex : 0 })); }; socket.onmessage = (event) => { const data = JSON.parse(event.data); if (data.type === 'info') { setTerminalLogs(prev => [...prev, data]); } else if (data.type === 'tool_call') { setTerminalLogs(prev => [...prev, data]); setSimulationStep(data.step); setAgentReasoning(prev => [...prev, { step: data.step, tool_name: data.tool_name, tool_args: data.tool_args }]); } else if (data.type === 'tool_result') { setTerminalLogs(prev => [...prev, data]); setAgentReasoning(prev => { const updated = [...prev]; const idx = updated.findIndex(r => r.step === data.step); if (idx >= 0) { updated[idx].result = data.result; } return updated; }); } else if (data.type === 'completed') { setSimulationState('completed'); setGeneratedPatch(data.patch); setReward(data.reward); setTerminalLogs(prev => [...prev, {type: 'info', message: data.success ? '[SUCCESS] Task completed successfully.' : '[FAILED] Task failed.'}]); socket.close(); } else if (data.type === 'error') { setTerminalLogs(prev => [...prev, {type: 'info', message: `[ERROR] ${data.message}`}]); setSimulationState('completed'); socket.close(); } }; socket.onerror = (error) => { console.error("WebSocket Error:", error); setTerminalLogs(prev => [...prev, {type: 'info', message: '[ERROR] WebSocket connection failed.'}]); setSimulationState('completed'); }; } } };""" content = content.replace(start_old, start_new) # 5. Nav Buttons nav_old = """ """ nav_new = """ """ content = content.replace(nav_old, nav_new) # 6. Sidebar Headers content = content.replace("repoTree ? 'Repository Files' : (mode === 'eval' ? 'Evaluation Tasks' : 'Inference Setup')", "repoTree ? 'Repository Files' : ((mode === 'eval' || mode === 'demo') ? 'Evaluation Tasks' : 'Inference Setup')") content = content.replace("!repoTree && mode === 'eval' && (", "!repoTree && (mode === 'eval' || mode === 'demo') && (") content = content.replace("mode === 'eval' ? resetEvalSelection : resetInference", "(mode === 'eval' || mode === 'demo') ? resetEvalSelection : resetInference") content = content.replace("activeTab === 'gold' && mode === 'eval'", "activeTab === 'gold' && (mode === 'eval' || mode === 'demo')") content = content.replace("mode === 'eval' && selectedTask?.gold_patch &&", "(mode === 'eval' || mode === 'demo') && selectedTask?.gold_patch &&") content = content.replace("mode === 'eval' && selectedTask && (", "(mode === 'eval' || mode === 'demo') && selectedTask && (") # 7. Render Patch patch_old = """ {activeTab === 'patch' && (
{mockGeneratedPatch.split('\\n').map((line, i) => {""" patch_new = """ {activeTab === 'patch' && (
{(mode === 'demo' ? mockGeneratedPatch : (generatedPatch || '')).split('\\n').map((line, i) => {""" content = content.replace(patch_old, patch_new) # 8. Render Terminal terminal_old = """ <>
$ Starting Migratron env for {repoName}...
{simulationStep >= 1 &&
[INFO] Target file selected: {selectedFile?.path}
} {simulationStep >= 2 &&
[INFO] Initializing agent context...
} {simulationStep >= 3 &&
[INFO] Executing agent reasoning loop...
} {simulationStep >= 4 &&
Agent generated patch for: {selectedFile?.path}
} {simulationStep >= 5 &&
[INFO] Applying patch and verifying syntax...
} {simulationStep >= 6 &&
[SUCCESS] Task completed. Evaluating GRPO Reward...
}
_
""" terminal_new = """ <> {mode === 'demo' ? ( <>
$ Starting Migratron env for {repoName}...
{simulationStep >= 1 &&
[INFO] Target file selected: {selectedFile?.path}
} {simulationStep >= 2 &&
[INFO] Initializing agent context...
} {simulationStep >= 3 &&
[INFO] Executing agent reasoning loop...
} {simulationStep >= 4 &&
Agent generated patch for: {selectedFile?.path}
} {simulationStep >= 5 &&
[INFO] Applying patch and verifying syntax...
} {simulationStep >= 6 &&
[SUCCESS] Task completed. Evaluating GRPO Reward...
} ) : ( terminalLogs.map((log, idx) => (
{log.message || `Agent called ${log.tool_name}`}
)) )} {simulationState === 'running' &&
_
} """ content = content.replace(terminal_old, terminal_new) # 9. Render Agent Reasoning agent_old = """ {simulationStep >= 2 && (
Thought Process

"The objective is to migrate {repoName}. I need to focus on {selectedFile.path}. I will first read the contents of this file to understand the current implementation."

)} {simulationStep >= 3 && (
Tool Execution
call:
view_file {'{'}
  AbsolutePath: "/work/{selectedFile.path}"
{'}'}
)} {simulationStep >= 4 && (
Tool Response

File contents read. Identifying migration targets and generating patch block...

)}""" agent_new = """ {mode === 'demo' ? ( <> {simulationStep >= 2 && (
Thought Process

"The objective is to migrate {repoName}. I need to focus on {selectedFile.path}. I will first read the contents of this file to understand the current implementation."

)} {simulationStep >= 3 && (
Tool Execution
call:
view_file {'{'}
  AbsolutePath: "/work/{selectedFile.path}"
{'}'}
)} {simulationStep >= 4 && (
Tool Response

File contents read. Identifying migration targets and generating patch block...

)} ) : ( agentReasoning.map((r, idx) => (
Tool Execution (Step {r.step})
call:
{r.tool_name} {'{'}
{Object.entries(r.tool_args || {}).map(([k, v]) => (   {k}: {JSON.stringify(v)}
))} {'}'}
{r.result && (
Tool Response
{r.result}
)}
)) )}""" content = content.replace(agent_old, agent_new) # 10. Reward reward_old = "{simulationState === 'completed' ? '+2.80' : '0.00'}" reward_new = "{mode === 'demo' ? (simulationState === 'completed' ? '+2.80' : '0.00') : (reward > 0 ? `+${reward.toFixed(2)}` : reward.toFixed(2))}" content = content.replace(reward_old, reward_new) with open('/Users/amritha/Desktop/work/intern/migratron-ui/src/components/Dashboard.tsx', 'w') as f: f.write(content) print("Patcher finished successfully.")