feat(phase4): implement session.title persistence on create and resume in stream for nabil
This commit is contained in:
+27
-15
@@ -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")
|
conv = save_conversation(universe_id, session_key, title=title or "Nouvelle conversation")
|
||||||
return conv
|
return conv
|
||||||
elif universe_id == "nabil":
|
elif universe_id == "nabil":
|
||||||
# Create session explicitly on hermes-nabil backend via session.create RPC
|
|
||||||
cookie = await get_nabil_session_cookie()
|
cookie = await get_nabil_session_cookie()
|
||||||
ticket = await get_nabil_ws_ticket()
|
ticket = await get_nabil_ws_ticket()
|
||||||
session_title = title or "Nouvelle conversation"
|
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_interval=20,
|
||||||
ping_timeout=20
|
ping_timeout=20
|
||||||
) as ws:
|
) as ws:
|
||||||
|
# 1. session.create
|
||||||
req_id = f"create-{uuid.uuid4().hex[:8]}"
|
req_id = f"create-{uuid.uuid4().hex[:8]}"
|
||||||
await ws.send(json.dumps({
|
await ws.send(json.dumps({
|
||||||
"jsonrpc": "2.0",
|
"jsonrpc": "2.0",
|
||||||
@@ -165,17 +165,32 @@ async def create_universe_conversation(universe_id: str, title: Optional[str] =
|
|||||||
"params": {"title": session_title}
|
"params": {"title": session_title}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
# Wait for session.create result frame
|
short_id = None
|
||||||
while True:
|
while True:
|
||||||
raw = await asyncio.wait_for(ws.recv(), timeout=6.0)
|
raw = await asyncio.wait_for(ws.recv(), timeout=6.0)
|
||||||
data = json.loads(raw)
|
data = json.loads(raw)
|
||||||
if data.get("id") == req_id and "result" in data:
|
if data.get("id") == req_id and "result" in data:
|
||||||
res = data["result"]
|
res = data["result"]
|
||||||
# Prioritize durable stored_session_id (format YYYYMMDD_HHMMSS_xxxxxx) over ephemeral handle
|
short_id = res.get("session_id")
|
||||||
session_key = res.get("stored_session_id") or res.get("session_id")
|
session_key = res.get("stored_session_id") or short_id
|
||||||
break
|
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:
|
except Exception:
|
||||||
# Fallback if WS fails
|
|
||||||
session_key = f"nabil_{uuid.uuid4().hex[:12]}"
|
session_key = f"nabil_{uuid.uuid4().hex[:12]}"
|
||||||
|
|
||||||
conv = save_conversation(universe_id, session_key, title=session_title)
|
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
|
active_sid = session_key
|
||||||
try:
|
while True:
|
||||||
while True:
|
raw_res = await asyncio.wait_for(ws.recv(), timeout=6.0)
|
||||||
raw_res = await asyncio.wait_for(ws.recv(), timeout=4.0)
|
data_res = json.loads(raw_res)
|
||||||
data_res = json.loads(raw_res)
|
if data_res.get("id") == resume_req_id:
|
||||||
if data_res.get("id") == resume_req_id:
|
if "result" in data_res:
|
||||||
if "result" in data_res:
|
active_sid = data_res["result"].get("session_id") or session_key
|
||||||
active_sid = data_res["result"].get("session_id") or session_key
|
break
|
||||||
break
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# 2. Submit prompt with active_sid
|
# 2. Submit prompt with active_sid
|
||||||
req_id = f"prompt-{uuid.uuid4().hex[:8]}"
|
req_id = f"prompt-{uuid.uuid4().hex[:8]}"
|
||||||
|
|||||||
Reference in New Issue
Block a user