adityaverma977 commited on
Commit
ac45880
·
1 Parent(s): 98e2ee8

Fix report build using tickHistory updater to avoid stale closure; compute final report from authoritative history

Browse files
Files changed (1) hide show
  1. frontend/app/page.tsx +47 -36
frontend/app/page.tsx CHANGED
@@ -154,43 +154,54 @@ export default function Page() {
154
  simState.simulation_id,
155
  (msg) => {
156
  // store the full tick message (state + events) for richer analysis
157
- setTickHistory(prev => [...prev, msg])
158
- if (msg.type === "finished") {
159
- if (msg.state) {
160
- setSimState(msg.state)
161
- setWinnerLabel(msg.state.winner_model || null)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  }
163
- setAppState("gameover")
164
- return
165
- }
166
- if (msg.state) setSimState(msg.state)
167
- if (msg.events) {
168
- const newMsgs = msg.events.map((e: any) => {
169
- if (e.type === 'message') return { agent_id: e.model, text: e.content, type: 'message' }
170
- if (e.type === 'death') return { agent_id: e.model, text: '', type: 'death' }
171
- if (e.type === 'alliance_proposal') return { agent_id: e.from_model, text: '', type: 'alliance_proposal', to_model: e.to_model }
172
- if (e.type === 'alliance_accept') return { agent_id: e.model_a, text: '', type: 'alliance_accept', to_model: e.model_b }
173
- if (e.type === 'alliance_reject') return { agent_id: e.from_model, text: '', type: 'alliance_reject', to_model: e.to_model }
174
- return null
175
- }).filter(Boolean)
176
- setChatMessages(prev => [...prev, ...newMsgs])
177
- }
178
- if (msg.chat) {
179
- const chatMsgs = msg.chat.map((entry: any) => ({
180
- agent_id: entry.agent_id,
181
- text: entry.message,
182
- type: 'message',
183
- }))
184
- setChatMessages(prev => [...prev, ...chatMsgs])
185
- }
186
- if (msg.state?.status === "finished") {
187
- setWinnerLabel(msg.state.winner_model || null)
188
- setAppState("gameover")
189
- // prepare report
190
- const report = buildReport([...tickHistory, msg])
191
- setReportData(report)
192
- }
193
- },
194
  () => setAppState("gameover")
195
  )
196
  wsRef.current = ws
 
154
  simState.simulation_id,
155
  (msg) => {
156
  // store the full tick message (state + events) for richer analysis
157
+ setTickHistory(prev => {
158
+ const next = [...prev, msg]
159
+
160
+ // update sim state from the incoming message
161
+ if (msg.state) setSimState(msg.state)
162
+
163
+ // convert events into chat messages for UI
164
+ if (msg.events) {
165
+ const newMsgs = msg.events.map((e: any) => {
166
+ if (e.type === 'message') return { agent_id: e.model, text: e.content, type: 'message' }
167
+ if (e.type === 'death') return { agent_id: e.model, text: '', type: 'death' }
168
+ if (e.type === 'alliance_proposal') return { agent_id: e.from_model, text: '', type: 'alliance_proposal', to_model: e.to_model }
169
+ if (e.type === 'alliance_accept') return { agent_id: e.model_a, text: '', type: 'alliance_accept', to_model: e.model_b }
170
+ if (e.type === 'alliance_reject') return { agent_id: e.from_model, text: '', type: 'alliance_reject', to_model: e.to_model }
171
+ return null
172
+ }).filter(Boolean)
173
+ setChatMessages(prevMsgs => [...prevMsgs, ...newMsgs])
174
+ }
175
+
176
+ if (msg.chat) {
177
+ const chatMsgs = msg.chat.map((entry: any) => ({
178
+ agent_id: entry.agent_id,
179
+ text: entry.message,
180
+ type: 'message',
181
+ }))
182
+ setChatMessages(prevMsgs => [...prevMsgs, ...chatMsgs])
183
+ }
184
+
185
+ // If simulation finished, mark gameover and compute final report using full tick history
186
+ if (msg.type === 'finished' || msg.state?.status === 'finished') {
187
+ const finalState = msg.state || (msg.type === 'finished' ? msg.state : null)
188
+ if (finalState) {
189
+ setWinnerLabel(finalState.winner_model || null)
190
+ }
191
+ setAppState('gameover')
192
+ const report = buildReport(next)
193
+ setReportData(report)
194
+ }
195
+
196
+ return next
197
+ })
198
+
199
+ // if the message was an immediate finished payload without state (rare), still handle
200
+ if (msg.type === "finished" && !msg.state) {
201
+ setAppState("gameover")
202
+ return
203
  }
204
+ },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
  () => setAppState("gameover")
206
  )
207
  wsRef.current = ws