fix: harden identity, live access, drafts and setup (review only)
Source-reviewed hardening changes and regression tests. UNBUILT / UNTESTED: not a deployable release. Go formatting, compilation, automated regression tests, full frontend validation and staging acceptance remain required. See docs/hardening/VALIDATION.md. Main remains the original clean snapshot; no upstream Git history imported.
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
// Modified in the independent hardening fork; see docs/hardening/README.md.
|
||||
// Modified for AgentDeskFixes: bind realtime work to the current session and sanitize diagnostics.
|
||||
"use client"
|
||||
|
||||
import { useEffect, useRef } from "react"
|
||||
import { toast } from "sonner"
|
||||
|
||||
import { useSession } from "@/components/session-provider"
|
||||
import { realtimeLogContext } from "@/lib/realtime-log-context"
|
||||
import { createAdminWebSocketUrl } from "@/lib/api/admin"
|
||||
import { type AgentMessage } from "@/lib/api/agent"
|
||||
import { shouldReloadConversationListForRealtimePatch } from "@/lib/agent-conversation-realtime"
|
||||
@@ -50,6 +54,8 @@ export function sendOperatorPresence(
|
||||
|
||||
export function useAgentConversationRealtime() {
|
||||
const t = useI18n()
|
||||
const { session } = useSession()
|
||||
const accessToken = session?.accessToken
|
||||
const selectedConversationId = useAgentConversationsStore(
|
||||
(state) => state.selectedConversationId
|
||||
)
|
||||
@@ -59,19 +65,23 @@ export function useAgentConversationRealtime() {
|
||||
const realtimeRef = useRef<AgentRealtimeConnection | null>(null)
|
||||
const subscribedConversationIdRef = useRef<number | null>(null)
|
||||
const selectedConversationIdRef = useRef<number | null>(selectedConversationId)
|
||||
const currentUserIdRef = useRef<number>(readSession()?.user.id ?? 0)
|
||||
const currentUserIdRef = useRef<number>(session?.user.id ?? 0)
|
||||
currentUserIdRef.current = session?.user.id ?? 0
|
||||
|
||||
useEffect(() => {
|
||||
selectedConversationIdRef.current = selectedConversationId
|
||||
}, [selectedConversationId])
|
||||
|
||||
useEffect(() => {
|
||||
if (!accessToken) return
|
||||
const isCurrentSession = () => readSession()?.accessToken === accessToken
|
||||
const realtime = createRealtimeConnectionManager({
|
||||
createSocket: () => new WebSocket(createAdminWebSocketUrl()),
|
||||
onStatusChange: setRealtimeStatus,
|
||||
onStatusChange: (status) => { if (isCurrentSession()) setRealtimeStatus(status) },
|
||||
onOpen: (socket) => {
|
||||
if (!isCurrentSession()) return
|
||||
console.info("[agent-realtime] websocket connected", {
|
||||
url: socket.url,
|
||||
...realtimeLogContext(socket.url),
|
||||
})
|
||||
|
||||
const conversationId = selectedConversationIdRef.current
|
||||
@@ -95,6 +105,7 @@ export function useAgentConversationRealtime() {
|
||||
}
|
||||
},
|
||||
onMessage: (event, socket) => {
|
||||
if (!isCurrentSession()) return
|
||||
try {
|
||||
const envelope = JSON.parse(event.data) as AgentRealtimeEnvelope
|
||||
const eventType = envelope.type ?? ""
|
||||
@@ -182,17 +193,16 @@ export function useAgentConversationRealtime() {
|
||||
},
|
||||
onClose: (event, socket) => {
|
||||
console.log("[agent-realtime] websocket closed", {
|
||||
url: socket.url,
|
||||
...realtimeLogContext(socket.url),
|
||||
readyState: socket.readyState,
|
||||
code: event.code,
|
||||
reason: event.reason,
|
||||
wasClean: event.wasClean,
|
||||
})
|
||||
subscribedConversationIdRef.current = null
|
||||
},
|
||||
onError: (_event, socket) => {
|
||||
console.log("[agent-realtime] websocket error", {
|
||||
url: socket.url,
|
||||
...realtimeLogContext(socket.url),
|
||||
readyState: socket.readyState,
|
||||
})
|
||||
},
|
||||
@@ -225,7 +235,7 @@ export function useAgentConversationRealtime() {
|
||||
realtime.disconnect()
|
||||
subscribedConversationIdRef.current = null
|
||||
}
|
||||
}, [setRealtimeStatus, t])
|
||||
}, [accessToken, setRealtimeStatus, t])
|
||||
|
||||
useEffect(() => {
|
||||
const socket = realtimeRef.current?.getSocket()
|
||||
|
||||
Reference in New Issue
Block a user