feat: add web channel configuration support and update widget response structure

This commit is contained in:
mlogclub
2026-04-24 22:24:19 +08:00
parent a262a865a0
commit ea94837db2
9 changed files with 400 additions and 114 deletions
+6 -89
View File
@@ -3,20 +3,12 @@
import { useEffect, useMemo, useState } from "react"
import type { KefuWidgetHostConfig } from "@/lib/kefu-widget-config"
import { generateUUID } from "@/lib/utils"
const STORAGE_KEY = "cs-agent-web-widget-test-config"
const INITIAL_CONFIG: KefuWidgetHostConfig = {
channelId: "",
baseUrl: "",
apiBaseUrl: "",
externalSource: "web_chat",
title: "在线客服",
subtitle: "欢迎咨询",
position: "right",
themeColor: "#2563eb",
width: "680px",
subject: "",
}
declare global {
@@ -30,10 +22,6 @@ declare global {
}
}
function generateRandomSubject() {
return `访客-${generateUUID().replace(/-/g, "").slice(0, 8)}`
}
function getDefaultConfig(): KefuWidgetHostConfig {
if (typeof window === "undefined") {
return INITIAL_CONFIG
@@ -44,27 +32,11 @@ function getDefaultConfig(): KefuWidgetHostConfig {
? (JSON.parse(savedText) as Partial<KefuWidgetHostConfig>)
: {}
const query = new URLSearchParams(window.location.search)
const origin = window.location.origin
return {
channelId: query.get("channelId") ?? savedConfig.channelId ?? "",
baseUrl: query.get("baseUrl") ?? savedConfig.baseUrl ?? origin,
apiBaseUrl:
query.get("apiBaseUrl") ??
savedConfig.apiBaseUrl ??
savedConfig.baseUrl ??
origin,
externalSource:
query.get("externalSource") ?? savedConfig.externalSource ?? "web_chat",
title: query.get("title") ?? savedConfig.title ?? "在线客服",
subtitle: query.get("subtitle") ?? savedConfig.subtitle ?? "欢迎咨询",
position:
(query.get("position") as "left" | "right" | null) ??
savedConfig.position ??
"right",
themeColor: query.get("themeColor") ?? savedConfig.themeColor ?? "#2563eb",
width: query.get("width") ?? savedConfig.width ?? "680px",
subject: query.get("subject") ?? savedConfig.subject ?? generateRandomSubject(),
baseUrl: "",
apiBaseUrl: "",
}
}
@@ -119,25 +91,12 @@ export function KefuWidgetDemo() {
}, [])
const snippet = useMemo(() => {
const scriptSrc = config.baseUrl
? `${config.baseUrl.replace(/\/$/, "")}/sdk/cs-ai-agent-sdk.min.js`
: "/sdk/cs-ai-agent-sdk.min.js"
return `<script>
window.CSAgentConfig = {
channelId: "${config.channelId || ""}",
baseUrl: "${config.baseUrl || ""}",
apiBaseUrl: "${config.apiBaseUrl || config.baseUrl || ""}",
externalSource: "${config.externalSource || "web_chat"}",
title: "${config.title || "在线客服"}",
subtitle: "${config.subtitle || ""}",
position: "${config.position || "right"}",
themeColor: "${config.themeColor || "#2563eb"}",
width: "${config.width || "380px"}",
subject: "${config.subject || ""}",
channelId: "${config.channelId || ""}"
};
</script>
<script async src="${scriptSrc}"></script>`
<script async src="/sdk/cs-ai-agent-sdk.min.js"></script>`
}, [config])
function updateField<K extends keyof KefuWidgetHostConfig>(
@@ -151,15 +110,8 @@ export function KefuWidgetDemo() {
const nextConfig: KefuWidgetHostConfig = {
...config,
channelId: config.channelId.trim(),
baseUrl: config.baseUrl.trim() || window.location.origin,
apiBaseUrl:
config.apiBaseUrl?.trim() || config.baseUrl.trim() || window.location.origin,
externalSource: config.externalSource?.trim() || "web_chat",
title: config.title?.trim() || "在线客服",
subtitle: config.subtitle?.trim() || "",
themeColor: config.themeColor?.trim() || "#2563eb",
width: config.width?.trim() || "380px",
subject: config.subject?.trim() || generateRandomSubject(),
baseUrl: "",
apiBaseUrl: "",
}
setConfig(nextConfig)
@@ -188,41 +140,6 @@ export function KefuWidgetDemo() {
value={config.channelId}
onChange={(value) => updateField("channelId", value)}
/>
<TextField
label="baseUrl"
value={config.baseUrl}
onChange={(value) => updateField("baseUrl", value)}
/>
<TextField
label="apiBaseUrl"
value={config.apiBaseUrl || ""}
onChange={(value) => updateField("apiBaseUrl", value)}
/>
<TextField
label="title"
value={config.title || ""}
onChange={(value) => updateField("title", value)}
/>
<TextField
label="subtitle"
value={config.subtitle || ""}
onChange={(value) => updateField("subtitle", value)}
/>
<TextField
label="themeColor"
value={config.themeColor || ""}
onChange={(value) => updateField("themeColor", value)}
/>
<TextField
label="width"
value={config.width || ""}
onChange={(value) => updateField("width", value)}
/>
<TextField
label="subject"
value={config.subject || ""}
onChange={(value) => updateField("subject", value)}
/>
</div>
<div className="mt-5 flex gap-2">