Files
ai-agent/web/lib/im-realtime.ts
T
mlogclub 14e3df64f1 feat: add customer session refresh functionality and improve session management
- Introduced RealtimeCustomerSessionRefreshPayload and RealtimeCustomerSessionRefreshEvent types for handling session refresh events.
- Updated ws_service to verify customer session and handle session refresh notifications.
- Enhanced API client to manage customer session tokens and expiration.
- Implemented customer session validation and storage in session storage.
- Added functions to exchange and ensure customer sessions.
- Updated IM real-time connection to include customer session tokens in WebSocket requests.
- Modified SDK and widget configurations to support external IDs and user tokens.
2026-04-28 23:00:22 +08:00

36 lines
1.2 KiB
TypeScript

import { createWebSocketBaseUrl } from "@/lib/api/websocket"
import { getCustomerSessionToken, type ImMessage } from "@/lib/api/im"
import { readKefuWidgetConfig } from "@/lib/kefu-widget-config"
import type {
RealtimeConversationPatch,
RealtimeMessageCreatedPayload,
} from "@/lib/im-realtime-state"
export type ImRealtimeEnvelope = {
type: string
topic?: string
data?: RealtimeMessageCreatedPayload<ImMessage> &
RealtimeConversationPatch & {
customerSessionToken?: string
expiresAt?: string
}
payload?: RealtimeMessageCreatedPayload<ImMessage> &
RealtimeConversationPatch & {
customerSessionToken?: string
expiresAt?: string
}
}
export function createImRealtimeConnection() {
const config = readKefuWidgetConfig()
const apiBaseUrl = (config.apiBaseUrl || "").trim()
const baseUrl = apiBaseUrl
? apiBaseUrl.replace(/^http/, "ws").replace(/\/$/, "")
: createWebSocketBaseUrl()
const channelId = encodeURIComponent(config.channelId || "")
const customerSessionToken = getCustomerSessionToken()
return new WebSocket(
`${baseUrl}/api/ws/open?channelId=${channelId}&customerSessionToken=${encodeURIComponent(customerSessionToken)}`
)
}