File size: 19,259 Bytes
1b35d41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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<string | null>(null);\n  const [reward, setReward] = useState<number>(0);\n  const [ws, setWs] = useState<WebSocket | null>(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<typeof setInterval>;
    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<typeof setInterval>;
    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 = """            <button
              onClick={() => { setMode('eval'); resetEvalSelection(); resetInference(); }}
              className={`px-3 py-1 rounded cursor-pointer transition-colors ${mode === 'eval' ? 'bg-[#007fd4] text-white' : 'hover:bg-white/10'}`}
            >
              Evaluation Mode
            </button>
            <button
              onClick={() => { setMode('inference'); resetEvalSelection(); resetInference(); }}
              className={`px-3 py-1 rounded cursor-pointer transition-colors ${mode === 'inference' ? 'bg-[#007fd4] text-white' : 'hover:bg-white/10'}`}
            >
              Inference Mode
            </button>"""

nav_new = """            <button
              onClick={() => { setMode('demo'); resetEvalSelection(); resetInference(); }}
              className={`px-3 py-1 rounded cursor-pointer transition-colors ${mode === 'demo' ? 'bg-[#007fd4] text-white' : 'hover:bg-white/10'}`}
            >
              Demo Mode
            </button>
            <button
              onClick={() => { setMode('eval'); resetEvalSelection(); resetInference(); }}
              className={`px-3 py-1 rounded cursor-pointer transition-colors ${mode === 'eval' ? 'bg-[#007fd4] text-white' : 'hover:bg-white/10'}`}
            >
              Evaluation Mode
            </button>
            <button
              onClick={() => { setMode('inference'); resetEvalSelection(); resetInference(); }}
              className={`px-3 py-1 rounded cursor-pointer transition-colors ${mode === 'inference' ? 'bg-[#007fd4] text-white' : 'hover:bg-white/10'}`}
            >
              Inference Mode
            </button>"""
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' && (
                    <div className="text-[#d4d4d4]">
                      {mockGeneratedPatch.split('\\n').map((line, i) => {"""
patch_new = """                  {activeTab === 'patch' && (
                    <div className="text-[#d4d4d4]">
                      {(mode === 'demo' ? mockGeneratedPatch : (generatedPatch || '')).split('\\n').map((line, i) => {"""
content = content.replace(patch_old, patch_new)

# 8. Render Terminal
terminal_old = """                <>
                  <div><span className="text-[#23d18b]">$</span> Starting Migratron env for {repoName}...</div>
                  {simulationStep >= 1 && <div className="text-[#3b8eea]">[INFO] Target file selected: {selectedFile?.path}</div>}
                  {simulationStep >= 2 && <div className="text-[#3b8eea]">[INFO] Initializing agent context...</div>}
                  {simulationStep >= 3 && <div className="text-[#3b8eea]">[INFO] Executing agent reasoning loop...</div>}
                  {simulationStep >= 4 && <div>Agent generated patch for: <span className="text-[#f5f543]">{selectedFile?.path}</span></div>}
                  {simulationStep >= 5 && <div className="text-[#3b8eea]">[INFO] Applying patch and verifying syntax...</div>}
                  {simulationStep >= 6 && <div className="text-[#23d18b]">[SUCCESS] Task completed. Evaluating GRPO Reward...</div>}
                  <div className="animate-pulse mt-2">_</div>
                </>"""
terminal_new = """                <>
                  {mode === 'demo' ? (
                    <>
                      <div><span className="text-[#23d18b]">$</span> Starting Migratron env for {repoName}...</div>
                      {simulationStep >= 1 && <div className="text-[#3b8eea]">[INFO] Target file selected: {selectedFile?.path}</div>}
                      {simulationStep >= 2 && <div className="text-[#3b8eea]">[INFO] Initializing agent context...</div>}
                      {simulationStep >= 3 && <div className="text-[#3b8eea]">[INFO] Executing agent reasoning loop...</div>}
                      {simulationStep >= 4 && <div>Agent generated patch for: <span className="text-[#f5f543]">{selectedFile?.path}</span></div>}
                      {simulationStep >= 5 && <div className="text-[#3b8eea]">[INFO] Applying patch and verifying syntax...</div>}
                      {simulationStep >= 6 && <div className="text-[#23d18b]">[SUCCESS] Task completed. Evaluating GRPO Reward...</div>}
                    </>
                  ) : (
                    terminalLogs.map((log, idx) => (
                      <div key={idx} className={log.message?.includes('[ERROR]') ? 'text-[#f14c4c]' : (log.message?.includes('[SUCCESS]') ? 'text-[#23d18b]' : (log.message?.includes('[INFO]') ? 'text-[#3b8eea]' : ''))}>
                        {log.message || `Agent called ${log.tool_name}`}
                      </div>
                    ))
                  )}
                  {simulationState === 'running' && <div className="animate-pulse mt-2">_</div>}
                </>"""
content = content.replace(terminal_old, terminal_new)

# 9. Render Agent Reasoning
agent_old = """                  {simulationStep >= 2 && (
                    <div className="border border-[#3c3c3c] rounded p-3 bg-[#252526] animate-fade-in">
                      <div className="flex justify-between items-center mb-2">
                        <span className="text-xs font-sans text-[#cccccc] uppercase">Thought Process</span>
                      </div>
                      <p className="text-[13px] text-[#d4d4d4] italic">
                        "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."
                      </p>
                    </div>
                  )}

                  {simulationStep >= 3 && (
                    <div className="border border-[#3c3c3c] rounded p-3 bg-[#252526] border-l-2 border-l-[#007fd4] animate-fade-in">
                      <div className="flex justify-between items-center mb-2">
                        <span className="text-xs font-sans text-[#569cd6] uppercase flex items-center gap-1">
                          <Code size={12} /> Tool Execution
                        </span>
                      </div>
                      <div className="text-[12px] text-[#ce9178] bg-[#1e1e1e] p-2 rounded">
                        <div className="text-[#9cdcfe]">call:</div> <span className="text-[#dcdcaa]">view_file</span> {'{'} <br />
                        &nbsp;&nbsp;<span className="text-[#9cdcfe]">AbsolutePath</span>: <span className="text-[#ce9178]">"/work/{selectedFile.path}"</span><br />
                        {'}'}
                      </div>
                    </div>
                  )}

                  {simulationStep >= 4 && (
                    <div className="border border-[#3c3c3c] rounded p-3 bg-[#252526] border-l-2 border-l-[#23d18b] animate-fade-in">
                      <div className="flex justify-between items-center mb-2">
                        <span className="text-xs font-sans text-[#23d18b] uppercase flex items-center gap-1">
                          <CheckCircle2 size={12} /> Tool Response
                        </span>
                      </div>
                      <p className="text-[12px] text-[#d4d4d4]">
                        File contents read. Identifying migration targets and generating patch block...
                      </p>
                    </div>
                  )}"""

agent_new = """                  {mode === 'demo' ? (
                    <>
                      {simulationStep >= 2 && (
                        <div className="border border-[#3c3c3c] rounded p-3 bg-[#252526] animate-fade-in">
                          <div className="flex justify-between items-center mb-2">
                            <span className="text-xs font-sans text-[#cccccc] uppercase">Thought Process</span>
                          </div>
                          <p className="text-[13px] text-[#d4d4d4] italic">
                            "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."
                          </p>
                        </div>
                      )}

                      {simulationStep >= 3 && (
                        <div className="border border-[#3c3c3c] rounded p-3 bg-[#252526] border-l-2 border-l-[#007fd4] animate-fade-in">
                          <div className="flex justify-between items-center mb-2">
                            <span className="text-xs font-sans text-[#569cd6] uppercase flex items-center gap-1">
                              <Code size={12} /> Tool Execution
                            </span>
                          </div>
                          <div className="text-[12px] text-[#ce9178] bg-[#1e1e1e] p-2 rounded">
                            <div className="text-[#9cdcfe]">call:</div> <span className="text-[#dcdcaa]">view_file</span> {'{'} <br />
                            &nbsp;&nbsp;<span className="text-[#9cdcfe]">AbsolutePath</span>: <span className="text-[#ce9178]">"/work/{selectedFile.path}"</span><br />
                            {'}'}
                          </div>
                        </div>
                      )}

                      {simulationStep >= 4 && (
                        <div className="border border-[#3c3c3c] rounded p-3 bg-[#252526] border-l-2 border-l-[#23d18b] animate-fade-in">
                          <div className="flex justify-between items-center mb-2">
                            <span className="text-xs font-sans text-[#23d18b] uppercase flex items-center gap-1">
                              <CheckCircle2 size={12} /> Tool Response
                            </span>
                          </div>
                          <p className="text-[12px] text-[#d4d4d4]">
                            File contents read. Identifying migration targets and generating patch block...
                          </p>
                        </div>
                      )}
                    </>
                  ) : (
                    agentReasoning.map((r, idx) => (
                      <div key={idx} className="space-y-3 mb-3">
                        <div className="border border-[#3c3c3c] rounded p-3 bg-[#252526] border-l-2 border-l-[#007fd4] animate-fade-in">
                          <div className="flex justify-between items-center mb-2">
                            <span className="text-xs font-sans text-[#569cd6] uppercase flex items-center gap-1">
                              <Code size={12} /> Tool Execution (Step {r.step})
                            </span>
                          </div>
                          <div className="text-[12px] text-[#ce9178] bg-[#1e1e1e] p-2 rounded whitespace-pre-wrap">
                            <div className="text-[#9cdcfe]">call:</div> <span className="text-[#dcdcaa]">{r.tool_name}</span> {'{'}<br />
                            {Object.entries(r.tool_args || {}).map(([k, v]) => (
                                <React.Fragment key={k}>
                                  &nbsp;&nbsp;<span className="text-[#9cdcfe]">{k}</span>: <span className="text-[#ce9178]">{JSON.stringify(v)}</span><br />
                                </React.Fragment>
                            ))}
                            {'}'}
                          </div>
                        </div>
                        {r.result && (
                          <div className="border border-[#3c3c3c] rounded p-3 bg-[#252526] border-l-2 border-l-[#23d18b] animate-fade-in">
                            <div className="flex justify-between items-center mb-2">
                              <span className="text-xs font-sans text-[#23d18b] uppercase flex items-center gap-1">
                                <CheckCircle2 size={12} /> Tool Response
                              </span>
                            </div>
                            <div className="text-[12px] text-[#d4d4d4] max-h-32 overflow-y-auto whitespace-pre-wrap font-mono">
                              {r.result}
                            </div>
                          </div>
                        )}
                      </div>
                    ))
                  )}"""
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.")