diff --git a/app/chat_service.py b/app/chat_service.py index 48fecfb..de738a0 100644 --- a/app/chat_service.py +++ b/app/chat_service.py @@ -143,7 +143,6 @@ async def create_universe_conversation(universe_id: str, title: Optional[str] = conv = save_conversation(universe_id, session_key, title=title or "Nouvelle conversation") return conv elif universe_id == "nabil": - # Create session explicitly on hermes-nabil backend via session.create RPC cookie = await get_nabil_session_cookie() ticket = await get_nabil_ws_ticket() session_title = title or "Nouvelle conversation" @@ -157,6 +156,7 @@ async def create_universe_conversation(universe_id: str, title: Optional[str] = ping_interval=20, ping_timeout=20 ) as ws: + # 1. session.create req_id = f"create-{uuid.uuid4().hex[:8]}" await ws.send(json.dumps({ "jsonrpc": "2.0", @@ -165,17 +165,32 @@ async def create_universe_conversation(universe_id: str, title: Optional[str] = "params": {"title": session_title} })) - # Wait for session.create result frame + short_id = None while True: raw = await asyncio.wait_for(ws.recv(), timeout=6.0) data = json.loads(raw) if data.get("id") == req_id and "result" in data: res = data["result"] - # Prioritize durable stored_session_id (format YYYYMMDD_HHMMSS_xxxxxx) over ephemeral handle - session_key = res.get("stored_session_id") or res.get("session_id") + short_id = res.get("session_id") + session_key = res.get("stored_session_id") or short_id break + + # 2. session.title (persists the row immediately to state.db under stored_session_id) + if short_id: + req_t_id = f"title-{uuid.uuid4().hex[:8]}" + await ws.send(json.dumps({ + "jsonrpc": "2.0", + "id": req_t_id, + "method": "session.title", + "params": {"session_id": short_id, "title": session_title} + })) + while True: + raw_t = await asyncio.wait_for(ws.recv(), timeout=6.0) + data_t = json.loads(raw_t) + if data_t.get("id") == req_t_id: + break + except Exception: - # Fallback if WS fails session_key = f"nabil_{uuid.uuid4().hex[:12]}" conv = save_conversation(universe_id, session_key, title=session_title) @@ -282,16 +297,13 @@ async def stream_chat_messages(universe_id: str, session_key: str, message: str) })) active_sid = session_key - try: - while True: - raw_res = await asyncio.wait_for(ws.recv(), timeout=4.0) - data_res = json.loads(raw_res) - if data_res.get("id") == resume_req_id: - if "result" in data_res: - active_sid = data_res["result"].get("session_id") or session_key - break - except Exception: - pass + while True: + raw_res = await asyncio.wait_for(ws.recv(), timeout=6.0) + data_res = json.loads(raw_res) + if data_res.get("id") == resume_req_id: + if "result" in data_res: + active_sid = data_res["result"].get("session_id") or session_key + break # 2. Submit prompt with active_sid req_id = f"prompt-{uuid.uuid4().hex[:8]}"