ciyidogan commited on
Commit
09f225b
·
verified ·
1 Parent(s): 2f9c618

Update state_orchestrator.py

Browse files
Files changed (1) hide show
  1. state_orchestrator.py +38 -36
state_orchestrator.py CHANGED
@@ -375,46 +375,48 @@ class StateOrchestrator:
375
  data={"reason": "critical_error"}
376
  ))
377
 
378
- async def transition_to(self, session_id: str, new_state: ConversationState):
379
- """Transition to a new state"""
380
- current_state = self.get_state(session_id)
381
-
382
- if current_state is None:
383
- log_warning(f"⚠️ Session not found for transition", session_id=session_id)
384
- return
 
 
 
385
 
386
- # Check if transition is valid
387
- if new_state not in self.VALID_TRANSITIONS.get(current_state, set()):
388
- log_error(
389
- f"❌ Invalid state transition",
390
- session_id=session_id,
391
- from_state=current_state.value,
392
- to_state=new_state.value
393
- )
394
 
395
- await publish_error(
396
- session_id=session_id,
397
- error_type="invalid_transition",
398
- error_message=f"Cannot transition from {current_state.value} to {new_state.value}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
399
  )
400
- return
401
 
402
- # Update state
403
- self.sessions[session_id] = new_state
404
-
405
- log_info(
406
- f"🔄 State transition",
407
- session_id=session_id,
408
- from_state=current_state.value,
409
- to_state=new_state.value
410
- )
411
-
412
- # Publish state transition event
413
- await publish_state_transition(
414
- session_id=session_id,
415
- from_state=current_state.value,
416
- to_state=new_state.value
417
- )
418
 
419
  def get_state(self, session_id: str) -> Optional[ConversationState]:
420
  """Get current state for a session"""
 
375
  data={"reason": "critical_error"}
376
  ))
377
 
378
+ async def transition_to(self, session_id: str, new_state: ConversationState) -> bool:
379
+ """
380
+ Transition to a new state with validation
381
+ """
382
+ try:
383
+ # Get session context
384
+ context = self._sessions.get(session_id)
385
+ if not context:
386
+ log(f"❌ Session not found for state transition | session_id={session_id}")
387
+ return False
388
 
389
+ # Get current state from context (DÜZELTME BURADA)
390
+ current_state = context.state # context'i değil, context.state'i kullan
 
 
 
 
 
 
391
 
392
+ # Check if transition is valid
393
+ if new_state not in self.VALID_TRANSITIONS.get(current_state, set()):
394
+ log(f"❌ Invalid state transition | session_id={session_id}, current={current_state.value}, requested={new_state.value}")
395
+ return False
396
+
397
+ # Update state
398
+ old_state = current_state
399
+ context.state = new_state
400
+ context.last_activity = datetime.utcnow()
401
+
402
+ log(f"✅ State transition | session_id={session_id}, {old_state.value} → {new_state.value}")
403
+
404
+ # Emit state transition event
405
+ await self._event_bus.publish(
406
+ EventType.STATE_TRANSITION,
407
+ {
408
+ "session_id": session_id,
409
+ "old_state": old_state.value,
410
+ "new_state": new_state.value,
411
+ "timestamp": datetime.utcnow().isoformat()
412
+ }
413
  )
 
414
 
415
+ return True
416
+
417
+ except Exception as e:
418
+ log_error(f"❌ State transition error | session_id={session_id}", e)
419
+ return False
 
 
 
 
 
 
 
 
 
 
 
420
 
421
  def get_state(self, session_id: str) -> Optional[ConversationState]:
422
  """Get current state for a session"""