File size: 6,386 Bytes
157b149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
WebSocket handler for real-time MindSphere Coach updates.
"""

from __future__ import annotations

import json
from typing import Any, Dict

from fastapi import WebSocket, WebSocketDisconnect

from .session import SessionManager


async def websocket_endpoint(
    websocket: WebSocket,
    session_id: str,
    session_manager: SessionManager,
):
    """
    WebSocket handler for a coaching session.

    Protocol:
        Client -> Server:
            {"type": "user_message", "content": "...", "message_type": "text|mc|choice"}
            {"type": "user_message", "content": "...", "answer_index": 0}
            {"type": "user_message", "content": "...", "choice": "accept"}
            {"type": "continue"}  (for phase transitions)
            {"type": "set_empathy", "value": 0.7}

        Server -> Client:
            {"type": "assistant_message", "content": "...", "phase": "...", "action": "..."}
            {"type": "question", "data": {...}}
            {"type": "sphere_update", "data": {...}}
            {"type": "counterfactual", "data": {...}}
            {"type": "intervention", "data": {...}}
            {"type": "phase_change", "phase": "..."}
            {"type": "belief_update", "data": {...}}
            {"type": "error", "message": "..."}
    """
    await websocket.accept()

    if not session_manager.session_exists(session_id):
        await websocket.send_json({"type": "error", "message": f"Session {session_id} not found"})
        await websocket.close()
        return

    agent = session_manager.get_agent(session_id)
    if agent is None:
        await websocket.send_json({"type": "error", "message": "Agent not found"})
        await websocket.close()
        return

    try:
        while True:
            data = await websocket.receive_json()
            msg_type = data.get("type", "")

            if msg_type == "user_message":
                content = data.get("content", "")
                message_type = data.get("message_type", "text")

                # Build user input
                user_input: Dict[str, Any] = {"answer": content}
                if "answer_index" in data:
                    user_input["answer_index"] = data["answer_index"]
                if "choice" in data:
                    user_input["choice"] = data["choice"]

                # Store in history
                session_manager.add_to_history(session_id, "user", content)

                # Process step
                result = agent.step(user_input)

                # Send assistant message
                if result.get("message"):
                    session_manager.add_to_history(session_id, "assistant", result["message"])
                    await websocket.send_json({
                        "type": "assistant_message",
                        "content": result["message"],
                        "phase": result.get("phase", ""),
                    })

                # Send question if present
                if result.get("question"):
                    await websocket.send_json({
                        "type": "question",
                        "data": result["question"],
                    })

                # Send sphere update if present
                if result.get("sphere_data"):
                    await websocket.send_json({
                        "type": "sphere_update",
                        "data": result["sphere_data"],
                    })

                # Send counterfactual if present
                if result.get("counterfactual"):
                    await websocket.send_json({
                        "type": "counterfactual",
                        "data": result["counterfactual"],
                    })

                # Send intervention if present
                if result.get("intervention"):
                    await websocket.send_json({
                        "type": "intervention",
                        "data": result["intervention"],
                    })

                # Send phase change
                await websocket.send_json({
                    "type": "phase_change",
                    "phase": result.get("phase", ""),
                    "progress": result.get("progress"),
                    "is_complete": result.get("is_complete", False),
                })

                # Send belief update
                if result.get("tom_stats") or result.get("user_type_summary"):
                    await websocket.send_json({
                        "type": "belief_update",
                        "data": {
                            "tom_stats": result.get("tom_stats"),
                            "user_type": result.get("user_type_summary"),
                            "beliefs": agent.get_belief_summary(),
                        },
                    })

            elif msg_type == "continue":
                # Trigger phase transition
                result = agent.step({})
                await websocket.send_json({
                    "type": "phase_change",
                    "phase": result.get("phase", ""),
                })
                if result.get("message"):
                    await websocket.send_json({
                        "type": "assistant_message",
                        "content": result["message"],
                        "phase": result.get("phase", ""),
                    })
                if result.get("sphere_data"):
                    await websocket.send_json({
                        "type": "sphere_update",
                        "data": result["sphere_data"],
                    })
                if result.get("intervention"):
                    await websocket.send_json({
                        "type": "intervention",
                        "data": result["intervention"],
                    })
                if result.get("counterfactual"):
                    await websocket.send_json({
                        "type": "counterfactual",
                        "data": result["counterfactual"],
                    })

            elif msg_type == "set_empathy":
                value = float(data.get("value", 0.5))
                agent.set_empathy_dial(value)
                await websocket.send_json({
                    "type": "empathy_updated",
                    "value": value,
                })

    except WebSocketDisconnect:
        pass