diff --git a/src/timmy/workshop_state.py b/src/timmy/workshop_state.py index d68c100..76a8e65 100644 --- a/src/timmy/workshop_state.py +++ b/src/timmy/workshop_state.py @@ -86,6 +86,40 @@ def _pip_snapshot(mood: str, confidence: float) -> dict: return pip_familiar.snapshot().to_dict() +def _resolve_mood(state) -> str: + """Map cognitive mood/engagement to a presence mood string.""" + if state.engagement == "idle" and state.mood == "settled": + return "calm" + return _MOOD_MAP.get(state.mood, "calm") + + +def _resolve_confidence(state) -> float: + """Compute normalised confidence from cognitive tracker state.""" + if state._confidence_count > 0: + raw = state._confidence_sum / state._confidence_count + else: + raw = 0.7 + return round(max(0.0, min(1.0, raw)), 2) + + +def _build_active_threads(state) -> list[dict]: + """Convert active commitments into presence thread dicts.""" + return [ + {"type": "thinking", "ref": c[:80], "status": "active"} + for c in state.active_commitments[:10] + ] + + +def _build_environment() -> dict: + """Return the environment section using local wall-clock time.""" + local_now = datetime.now() + return { + "time_of_day": _time_of_day(local_now.hour), + "local_time": local_now.strftime("%-I:%M %p"), + "day_of_week": local_now.strftime("%A"), + } + + def get_state_dict() -> dict: """Build presence state dict from current cognitive state. @@ -98,37 +132,19 @@ def get_state_dict() -> dict: state = cognitive_tracker.get_state() now = datetime.now(UTC) - # Map cognitive mood to presence mood - mood = _MOOD_MAP.get(state.mood, "calm") - if state.engagement == "idle" and state.mood == "settled": - mood = "calm" - - # Confidence from cognitive tracker - if state._confidence_count > 0: - confidence = state._confidence_sum / state._confidence_count - else: - confidence = 0.7 - - # Build active threads from commitments - threads = [] - for commitment in state.active_commitments[:10]: - threads.append({"type": "thinking", "ref": commitment[:80], "status": "active"}) - - # Activity + mood = _resolve_mood(state) + confidence = _resolve_confidence(state) activity = _ACTIVITY_MAP.get(state.engagement, "idle") - # Environment - local_now = datetime.now() - return { "version": 1, "liveness": now.strftime("%Y-%m-%dT%H:%M:%SZ"), "current_focus": state.focus_topic or "", - "active_threads": threads, + "active_threads": _build_active_threads(state), "recent_events": [], "concerns": [], "mood": mood, - "confidence": round(max(0.0, min(1.0, confidence)), 2), + "confidence": confidence, "energy": round(_current_energy(), 2), "identity": { "name": "Timmy", @@ -143,11 +159,7 @@ def get_state_dict() -> dict: "visitor_present": False, "conversation_turns": state.conversation_depth, }, - "environment": { - "time_of_day": _time_of_day(local_now.hour), - "local_time": local_now.strftime("%-I:%M %p"), - "day_of_week": local_now.strftime("%A"), - }, + "environment": _build_environment(), "familiar": _pip_snapshot(mood, confidence), "meta": { "schema_version": 1,