fix(web): refactor pico chat flow and fix proxied websocket URLs (#1639)
- move chat controller, state, protocol, history, and websocket logic into a dedicated chat feature module - improve chat reconnection, session hydration, and send gating based on actual websocket state - preserve gateway status during transient SSE disconnects and update stop state immediately - generate wss websocket URLs behind HTTPS proxies and add backend tests for forwarded proto handling
This commit is contained in:
1 parent
0c94e6f7b3
commit
c513ad22d7
16 files changed
+509
-215
No files matched your search
@@ -0,0 +1,57 @@
|
||||
export function normalizeWsUrlForBrowser(wsUrl: string): string {
|
||||
let finalWsUrl = wsUrl
|
||||
|
||||
try {
|
||||
const parsedUrl = new URL(wsUrl)
|
||||
const isLocalHost =
|
||||
parsedUrl.hostname === "localhost" ||
|
||||
parsedUrl.hostname === "127.0.0.1" ||
|
||||
parsedUrl.hostname === "0.0.0.0"
|
||||
const isBrowserLocal =
|
||||
window.location.hostname === "localhost" ||
|
||||
window.location.hostname === "127.0.0.1"
|
||||
|
||||
if (isLocalHost && !isBrowserLocal) {
|
||||
parsedUrl.hostname = window.location.hostname
|
||||
finalWsUrl = parsedUrl.toString()
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("Could not parse ws_url:", error)
|
||||
}
|
||||
|
||||
return finalWsUrl
|
||||
}
|
||||
|
||||
export function invalidateSocket(socket: WebSocket | null) {
|
||||
if (!socket) {
|
||||
return
|
||||
}
|
||||
|
||||
socket.onopen = null
|
||||
socket.onmessage = null
|
||||
socket.onclose = null
|
||||
socket.onerror = null
|
||||
socket.close()
|
||||
}
|
||||
|
||||
export function isCurrentSocket({
|
||||
socket,
|
||||
currentSocket,
|
||||
generation,
|
||||
currentGeneration,
|
||||
sessionId,
|
||||
currentSessionId,
|
||||
}: {
|
||||
socket: WebSocket
|
||||
currentSocket: WebSocket | null
|
||||
generation: number
|
||||
currentGeneration: number
|
||||
sessionId: string
|
||||
currentSessionId: string
|
||||
}): boolean {
|
||||
return (
|
||||
currentSocket === socket &&
|
||||
generation === currentGeneration &&
|
||||
sessionId === currentSessionId
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user