MCPcopy Create free account
hub / github.com/1jehuang/jcode / process_event

Function process_event

scripts/jcode_monitor.py:220–278  ·  view source on GitHub ↗

Process a single event and update state

(event: dict, state: MonitorState)

Source from the content-addressed store, hash-verified

218
219
220def process_event(event: dict, state: MonitorState):
221 """Process a single event and update state"""
222 state.events_received += 1
223 state.last_event_time = time.time()
224
225 event_type = event.get("type", "")
226
227 if event_type == "ack":
228 state.is_processing = True
229
230 elif event_type == "text_delta":
231 state.current_text += event.get("text", "")
232 # Keep last 2000 chars
233 if len(state.current_text) > 2000:
234 state.current_text = state.current_text[-2000:]
235
236 elif event_type == "tool_start":
237 tool_id = event.get("id", "")
238 tool_name = event.get("name", "unknown")
239 state.active_tools[tool_id] = tool_name
240
241 elif event_type == "tool_exec":
242 pass # Tool is executing, still active
243
244 elif event_type == "tool_done":
245 tool_id = event.get("id", "")
246 tool_name = event.get("name", "unknown")
247 output = event.get("output", "")
248 error = event.get("error")
249
250 # Remove from active
251 state.active_tools.pop(tool_id, None)
252
253 # Add to history
254 state.tool_history.append((tool_name, error is None, output[:100] if output else "(empty)"))
255 # Keep last 20
256 state.tool_history = state.tool_history[-20:]
257
258 elif event_type == "tokens":
259 state.input_tokens = event.get("input", 0)
260 state.output_tokens = event.get("output", 0)
261
262 elif event_type == "done":
263 state.is_processing = False
264 state.current_text = "" # Clear for next turn
265
266 elif event_type == "error":
267 state.errors.append(event.get("message", "unknown error"))
268 state.errors = state.errors[-10:]
269
270 elif event_type == "pong":
271 pass # Health check response
272
273 elif event_type == "state":
274 state.session_id = event.get("session_id", "")
275 state.is_processing = event.get("is_processing", False)
276
277 elif event_type == "session":

Callers 1

mainFunction · 0.85

Calls 1

getMethod · 0.45

Tested by

no test coverage detected