refactor: update references from CSAgent to AgentDesk across the codebase

This commit is contained in:
mlogclub
2026-05-31 18:48:49 +08:00
parent eb04c344a5
commit c1e972001d
7 changed files with 58 additions and 58 deletions
@@ -740,7 +740,7 @@ function WebAccessGuide({ channelId }: { channelId: string }) {
return ""
}
return `<script>
window.CSAgentConfig = {
window.AgentDeskConfig = {
channelId: "${channelId}"
};
</script>
+16 -16
View File
@@ -4,12 +4,12 @@ import { SignJWT } from "jose"
import { CheckIcon, CopyIcon } from "lucide-react"
import { useEffect, useMemo, useState } from "react"
import type { CSAgentConfig } from "@/lib/sdk/config-types"
import type { AgentDeskConfig } from "@/lib/sdk/config-types"
import { useI18n } from "@/i18n/provider"
const STORAGE_KEY = "agent-desk-web-widget-test-config"
const DEFAULT_JWT_TTL_MINUTES = "30"
const INITIAL_CONFIG: CSAgentConfig = {
const INITIAL_CONFIG: AgentDeskConfig = {
channelId: "",
baseUrl: "",
apiBaseUrl: "",
@@ -17,7 +17,7 @@ const INITIAL_CONFIG: CSAgentConfig = {
type AuthMode = "guest" | "jwt"
type WidgetDemoConfig = CSAgentConfig & {
type WidgetDemoConfig = AgentDeskConfig & {
authMode?: AuthMode
jwtSecret?: string
jwtUserId?: string
@@ -53,32 +53,32 @@ function removeMountedWidget() {
return
}
window.CSAgentWidget?.destroy()
window.AgentDeskWidget?.destroy()
document
.querySelectorAll(
'[data-agent-desk-widget="launcher"], [data-agent-desk-widget="frame"], [data-agent-desk-widget="script"]'
)
.forEach((node) => node.remove())
delete window.CSAgentConfig
delete window.AgentDeskConfig
delete window.__CS_AI_AGENT_WIDGET_CONFIG__
delete window.__CS_AI_AGENT_WIDGET_STATE__
delete window.CSAgentWidget
delete window.AgentDeskWidget
}
function injectWidget(config: CSAgentConfig) {
function injectWidget(config: AgentDeskConfig) {
removeMountedWidget()
window.CSAgentConfig = config
window.AgentDeskConfig = config
const script = document.createElement("script")
script.async = true
script.src = `${window.location.origin}/sdk/agent-desk-sdk.min.js`
script.dataset.csAgentWidget = "script"
script.dataset.agentDeskWidget = "script"
document.body.appendChild(script)
}
function buildWidgetConfig(config: WidgetDemoConfig): CSAgentConfig {
const nextConfig: CSAgentConfig = {
function buildWidgetConfig(config: WidgetDemoConfig): AgentDeskConfig {
const nextConfig: AgentDeskConfig = {
channelId: config.channelId.trim(),
baseUrl: "",
apiBaseUrl: "",
@@ -200,7 +200,7 @@ export function SupportWidgetDemo() {
}
return `<script>
window.CSAgentConfig = {
window.AgentDeskConfig = {
${configLines.join(",\n")}
};
</script>
@@ -225,11 +225,11 @@ ${configLines.join(",\n")}
}
async function handleCopyDirectUrl() {
if (!window.CSAgentWidget || typeof navigator === "undefined") {
if (!window.AgentDeskWidget || typeof navigator === "undefined") {
return
}
try {
const url = await window.CSAgentWidget.getChatUrl()
const url = await window.AgentDeskWidget.getChatUrl()
setLatestDirectChatUrl(url)
if (config.authMode === "jwt") {
setGeneratedToken(new URL(url).searchParams.get("userToken") || "")
@@ -243,11 +243,11 @@ ${configLines.join(",\n")}
}
async function handleOpenDirectChat() {
if (!window.CSAgentWidget) {
if (!window.AgentDeskWidget) {
return
}
try {
const url = await window.CSAgentWidget.getChatUrl()
const url = await window.AgentDeskWidget.getChatUrl()
setLatestDirectChatUrl(url)
if (config.authMode === "jwt") {
setGeneratedToken(new URL(url).searchParams.get("userToken") || "")
+8 -8
View File
@@ -68,7 +68,7 @@ async function loadSdk(config) {
createElementNS: (_namespace, tagName) => createElement(tagName),
},
window: {
CSAgentConfig: config,
AgentDeskConfig: config,
location: {
origin: "https://host.example",
},
@@ -109,10 +109,10 @@ test("getChatUrl resolves a fresh userToken for each call", async () => {
getUserToken: async () => `token_${++calls}`,
})
assert.equal(typeof sandbox.window.CSAgentWidget.getChatUrl, "function")
assert.equal(typeof sandbox.window.AgentDeskWidget.getChatUrl, "function")
const first = await sandbox.window.CSAgentWidget.getChatUrl()
const second = await sandbox.window.CSAgentWidget.getChatUrl()
const first = await sandbox.window.AgentDeskWidget.getChatUrl()
const second = await sandbox.window.AgentDeskWidget.getChatUrl()
assert.equal(new URL(first).pathname, "/support/chat/")
assert.equal(new URL(second).pathname, "/support/chat/")
@@ -129,7 +129,7 @@ test("launcher click creates chat iframe with a freshly resolved userToken", asy
})
await flushPromises()
const launcher = sandbox.document.body.children.find(
(child) => child.dataset.csAgentWidget === "launcher"
(child) => child.dataset.agentDeskWidget === "launcher"
)
assert.ok(launcher)
@@ -138,7 +138,7 @@ test("launcher click creates chat iframe with a freshly resolved userToken", asy
await flushPromises()
const frame = sandbox.document.body.children.find(
(child) => child.dataset.csAgentWidget === "frame"
(child) => child.dataset.agentDeskWidget === "frame"
)
assert.ok(frame)
@@ -153,14 +153,14 @@ test("chat iframe layout uses dynamic viewport height for iOS browser chrome", a
})
await flushPromises()
const launcher = sandbox.document.body.children.find(
(child) => child.dataset.csAgentWidget === "launcher"
(child) => child.dataset.agentDeskWidget === "launcher"
)
launcher.click()
await flushPromises()
const frame = sandbox.document.body.children.find(
(child) => child.dataset.csAgentWidget === "frame"
(child) => child.dataset.agentDeskWidget === "frame"
)
assert.ok(frame)
+23 -23
View File
@@ -1,10 +1,10 @@
import type {
CSAgentConfig,
CSAgentWidget,
AgentDeskConfig,
AgentDeskWidget,
SupportChatRuntimeConfig,
} from "./config-types"
type NormalizedCSAgentConfig = CSAgentConfig & {
type NormalizedAgentDeskConfig = AgentDeskConfig & {
baseUrl: string
channelId: string
position: "left" | "right"
@@ -23,7 +23,7 @@ type WidgetState = {
configLoading: boolean
frameHideTimer: number | null
frameDestroyTimer: number | null
config: NormalizedCSAgentConfig | null
config: NormalizedAgentDeskConfig | null
frameConfig: SupportChatRuntimeConfig | null
frameUrl: URL | null
animationDuration: number
@@ -33,7 +33,7 @@ type WidgetState = {
type WidgetConfigResponse = {
success?: boolean
data?: Partial<Pick<
CSAgentConfig,
AgentDeskConfig,
"title" | "subtitle" | "themeColor" | "position" | "width"
>>
}
@@ -64,7 +64,7 @@ type FrameMessage =
(function () {
const DEFAULT_CONFIG: Pick<
NormalizedCSAgentConfig,
NormalizedAgentDeskConfig,
"position" | "themeColor" | "width"
> = {
position: "right",
@@ -94,7 +94,7 @@ type FrameMessage =
window.__CS_AI_AGENT_WIDGET_STATE__ = state
}
function normalizeConfig(config?: CSAgentConfig): NormalizedCSAgentConfig {
function normalizeConfig(config?: AgentDeskConfig): NormalizedAgentDeskConfig {
const merged: Record<string, unknown> = { ...DEFAULT_CONFIG, ...(config || {}) }
merged.baseUrl = String(merged.baseUrl || window.location.origin).replace(/\/$/, "")
if (merged.apiBaseUrl) {
@@ -109,10 +109,10 @@ type FrameMessage =
if (typeof merged.getUserToken !== "function") {
delete merged.getUserToken
}
return merged as NormalizedCSAgentConfig
return merged as NormalizedAgentDeskConfig
}
function resolveWidgetBaseUrl(config: NormalizedCSAgentConfig) {
function resolveWidgetBaseUrl(config: NormalizedAgentDeskConfig) {
const currentScript = document.currentScript as HTMLScriptElement | null
if (currentScript?.src) {
return currentScript.src.replace(/\/sdk\/agent-desk-sdk\.min\.js(?:\?.*)?$/, "")
@@ -120,7 +120,7 @@ type FrameMessage =
return String(config.widgetBaseUrl || config.baseUrl || window.location.origin).replace(/\/$/, "")
}
function createFrameUrl(config: NormalizedCSAgentConfig, userToken: string) {
function createFrameUrl(config: NormalizedAgentDeskConfig, userToken: string) {
const widgetBaseUrl = resolveWidgetBaseUrl(config)
const frameUrl = new URL(`${widgetBaseUrl}/support/chat/`)
frameUrl.searchParams.set("channelId", config.channelId)
@@ -133,7 +133,7 @@ type FrameMessage =
}
function createFrameConfig(
config: NormalizedCSAgentConfig,
config: NormalizedAgentDeskConfig,
userToken: string
): SupportChatRuntimeConfig {
const { getUserToken: _getUserToken, ...payload } = config
@@ -169,13 +169,13 @@ type FrameMessage =
}
function mergeWidgetConfig(
config: NormalizedCSAgentConfig,
config: NormalizedAgentDeskConfig,
remoteConfig?: WidgetConfigResponse["data"]
) {
if (!remoteConfig) {
return config
}
const merged: NormalizedCSAgentConfig = { ...config }
const merged: NormalizedAgentDeskConfig = { ...config }
const remoteKeys = ["title", "subtitle", "themeColor", "position", "width"] as const
remoteKeys.forEach((key) => {
const value = remoteConfig[key]
@@ -186,7 +186,7 @@ type FrameMessage =
return merged
}
function fetchWidgetConfig(config: NormalizedCSAgentConfig) {
function fetchWidgetConfig(config: NormalizedAgentDeskConfig) {
const baseUrl = String(config.apiBaseUrl || config.baseUrl || "").replace(/\/$/, "")
if (!baseUrl || !config.channelId || typeof fetch !== "function") {
return Promise.resolve(config)
@@ -368,7 +368,7 @@ type FrameMessage =
}
state.frame = document.createElement("iframe")
state.frame.dataset.csAgentWidget = "frame"
state.frame.dataset.agentDeskWidget = "frame"
state.frame.title = state.config.title || getDefaultWidgetTitle()
state.frame.src = state.frameUrl.toString()
applyFrameLayout()
@@ -434,7 +434,7 @@ type FrameMessage =
]
const text = document.createElement("span")
button.type = "button"
button.dataset.csAgentWidget = "launcher"
button.dataset.agentDeskWidget = "launcher"
button.setAttribute("aria-label", config.title || getDefaultWidgetTitle())
icon.setAttribute("viewBox", "0 0 24 24")
icon.setAttribute("fill", "none")
@@ -491,8 +491,8 @@ type FrameMessage =
return button
}
function mount(config?: CSAgentConfig) {
const rawConfig = config || window.CSAgentConfig || { channelId: "" }
function mount(config?: AgentDeskConfig) {
const rawConfig = config || window.AgentDeskConfig || { channelId: "" }
state.config = normalizeConfig(rawConfig)
const widgetBaseUrl = resolveWidgetBaseUrl(state.config)
if (!rawConfig.baseUrl) {
@@ -552,7 +552,7 @@ type FrameMessage =
})
}
window.CSAgentWidget = {
window.AgentDeskWidget = {
mount,
destroy,
open: () => openWidget(),
@@ -562,21 +562,21 @@ type FrameMessage =
},
getChatUrl: () => {
if (!state.config) {
mount(window.CSAgentConfig || { channelId: "" })
mount(window.AgentDeskConfig || { channelId: "" })
}
if (!state.config?.channelId) {
return Promise.reject(new Error("channelId is required"))
}
return prepareFrameUrl().then((frameUrl) => frameUrl.toString())
},
} satisfies CSAgentWidget
} satisfies AgentDeskWidget
if (!state.listenerBound) {
window.addEventListener("message", handleWindowMessage)
state.listenerBound = true
}
if (window.CSAgentConfig) {
mount(window.CSAgentConfig)
if (window.AgentDeskConfig) {
mount(window.AgentDeskConfig)
}
})()
+7 -7
View File
@@ -1,4 +1,4 @@
export type CSAgentConfig = {
export type AgentDeskConfig = {
channelId: string
baseUrl?: string
apiBaseUrl?: string
@@ -16,13 +16,13 @@ export type CSAgentConfig = {
width?: string
}
export type SupportChatRuntimeConfig = Omit<CSAgentConfig, "getUserToken"> & {
/** Used only by /support/chat to exchange for a chat token; not part of CSAgentConfig. */
export type SupportChatRuntimeConfig = Omit<AgentDeskConfig, "getUserToken"> & {
/** Used only by /support/chat to exchange for a chat token; not part of AgentDeskConfig. */
userToken?: string
}
export type CSAgentWidget = {
mount: (config?: CSAgentConfig) => void
export type AgentDeskWidget = {
mount: (config?: AgentDeskConfig) => void
destroy: () => void
open: () => Promise<void>
close: () => void
@@ -31,8 +31,8 @@ export type CSAgentWidget = {
declare global {
interface Window {
CSAgentConfig?: CSAgentConfig
CSAgentWidget?: CSAgentWidget
AgentDeskConfig?: AgentDeskConfig
AgentDeskWidget?: AgentDeskWidget
__CS_AI_AGENT_WIDGET_CONFIG__?: SupportChatRuntimeConfig
__CS_AI_AGENT_WIDGET_STATE__?: unknown
}
+2 -2
View File
@@ -36,8 +36,8 @@ export function readSupportChatRuntimeConfig(): SupportChatRuntimeConfig {
if (window.__CS_AI_AGENT_WIDGET_CONFIG__) {
return window.__CS_AI_AGENT_WIDGET_CONFIG__
}
if (window.CSAgentConfig) {
const { getUserToken: _getUserToken, ...hostConfig } = window.CSAgentConfig
if (window.AgentDeskConfig) {
const { getUserToken: _getUserToken, ...hostConfig } = window.AgentDeskConfig
return {
...fallback,
...hostConfig,
File diff suppressed because one or more lines are too long