Files
ai-agent/web/lib/kefu-widget-config.ts
T
mlogclub 4ec529e860 feat: add user token secret management for channels
- Implemented user token secret generation and retrieval in channel service.
- Added ResetUserTokenSecret method to reset the user token secret for channels.
- Updated channel configuration parsing to include user token secret.
- Enhanced WebSocket service to utilize user token secret for external info retrieval.
- Modified dashboard channel edit component to support user token secret display and reset functionality.
- Introduced API endpoint for resetting user token secret.
- Updated IM and SDK configurations to include user token.
- Added tests for user token verification logic.
2026-04-28 10:15:15 +08:00

74 lines
2.2 KiB
TypeScript

export type KefuWidgetHostConfig = {
channelId: string
baseUrl: string
apiBaseUrl?: string
/** 与后端 enums.ExternalSource 一致,默认 web_chat */
externalSource?: string
/** 外部访客稳定标识;未传时使用浏览器本地访客 ID */
externalId?: string
/** 访客展示名,随请求以 X-External-Name / WS query externalName 传给后端 */
externalName?: string
/** 业务系统签发的前台用户 JWT */
userToken?: string
title?: string
subtitle?: string
position?: "left" | "right"
themeColor?: string
width?: string
}
declare global {
interface Window {
CSAgentConfig?: KefuWidgetHostConfig
__CS_AGENT_WIDGET_CONFIG__?: KefuWidgetHostConfig
__CS_AGENT_WIDGET_STATE__?: unknown
}
}
export function readKefuWidgetConfig(): KefuWidgetHostConfig {
if (typeof window === "undefined") {
return {
channelId: "",
baseUrl: "",
apiBaseUrl: "",
}
}
const query = new URLSearchParams(window.location.search)
const fallback: KefuWidgetHostConfig = {
channelId:
query.get("channelId") ??
process.env.NEXT_PUBLIC_OPEN_IM_CHANNEL_ID?.trim() ??
"",
baseUrl:
query.get("baseUrl") ??
process.env.NEXT_PUBLIC_API_BASE_URL?.trim() ??
window.location.origin,
apiBaseUrl:
query.get("apiBaseUrl") ??
process.env.NEXT_PUBLIC_API_BASE_URL?.trim() ??
undefined,
externalSource:
query.get("externalSource") ??
process.env.NEXT_PUBLIC_OPEN_IM_EXTERNAL_SOURCE?.trim() ??
undefined,
externalId: query.get("externalId") ?? undefined,
externalName: query.get("externalName") ?? undefined,
userToken: query.get("userToken") ?? undefined,
title: query.get("title") ?? undefined,
subtitle: query.get("subtitle") ?? undefined,
position: (query.get("position") as "left" | "right" | null) ?? undefined,
themeColor: query.get("themeColor") ?? undefined,
width: query.get("width") ?? undefined,
}
return window.__CS_AGENT_WIDGET_CONFIG__ ?? window.CSAgentConfig ?? fallback
}
export function setKefuWidgetConfig(config: KefuWidgetHostConfig) {
if (typeof window === "undefined") {
return
}
window.__CS_AGENT_WIDGET_CONFIG__ = config
}