| 1 | /** Build the persisted WebUI session key (mirrors ``webui_session_key`` in Python). */ |
| 2 | export function webuiSessionKey( |
| 3 | userId: string | null | undefined, |
| 4 | chatId: string, |
| 5 | ): string { |
| 6 | const uid = userId?.trim(); |
| 7 | const cid = chatId.trim(); |
| 8 | if (!uid) { |
| 9 | throw new Error("webuiSessionKey requires userId"); |
| 10 | } |
| 11 | if (!cid) { |
| 12 | throw new Error("webuiSessionKey requires chatId"); |
| 13 | } |
| 14 | return `websocket:${uid}:${cid}`; |
| 15 | } |
| 16 | |
| 17 | /** Return true for deprecated ``websocket:<chatId>`` keys (no user segment). */ |
| 18 | export function isLegacyTwoPartSessionKey(key: string): boolean { |
| 19 | if (!key.startsWith("websocket:")) return false; |
| 20 | const rest = key.slice("websocket:".length); |
| 21 | return rest.length > 0 && !rest.includes(":"); |
| 22 | } |
| 23 | |
| 24 | /** Extract the wire ``chat_id`` from a persisted session key. */ |
| 25 | export function webuiChatIdFromKey(key: string): string { |
| 26 | return key.slice(key.lastIndexOf(":") + 1); |
| 27 | } |
| 28 |