/** * 配置解析模块 - 参数校验 + 默认值填充 * * 参数映射: * integrateId → 后端 roleId(客服角色 ID) * userId → 后端 accountId(客户账号 ID) * chatId → 自动管理(不在用户配置中暴露) */ import { SDKConfig, ResolvedConfig } from './types'; import { logger } from './logger'; import launcherLogo from '../assets/launcher-logo.png'; /** 默认悬浮按钮图标 — 使用 assets/launcher-logo.png 的内联 base64 */ const DEFAULT_LAUNCHER_ICON = ``; /** * 解析并校验用户传入的配置,填充默认值 */ export function parseConfig(raw: SDKConfig): ResolvedConfig | null { // 校验必传参数:integrateId(对应后端 roleId) if (!raw.integrateId || (typeof raw.integrateId !== 'string' && typeof raw.integrateId !== 'number') || (typeof raw.integrateId === 'string' && raw.integrateId.trim() === '')) { logger.error('integrateId 是必传参数(对应后端 roleId 客服角色 ID),请检查 init() 调用。示例:ChatbotSDK.init({ integrateId: 1, requestDomain: "https://api.example.com" })'); return null; } // 校验必传参数:requestDomain if (!raw.requestDomain || typeof raw.requestDomain !== 'string' || raw.requestDomain.trim() === '') { logger.error('requestDomain 是必传参数,请检查 init() 调用。示例:ChatbotSDK.init({ integrateId: 1, requestDomain: "https://api.example.com" })'); return null; } // 校验 requestDomain 是否为合法 URL 格式 try { new URL(raw.requestDomain); } catch { logger.error(`requestDomain 不是合法的 URL 格式:${raw.requestDomain}。请提供完整的域名,如 https://api.example.com`); return null; } // integrateId 统一转为字符串(后端 roleId 为 Long,但 query param 传字符串也可接收) const integrateIdStr = String(raw.integrateId).trim(); // 解析图标:用户传入优先,否则使用默认极光粒子图标 const resolvedLauncherIcon = raw.launcherIcon || DEFAULT_LAUNCHER_ICON; const resolvedPrimaryColor = raw.primaryColor || '#4F46E5'; // 填充默认值 const config: ResolvedConfig = { integrateId: integrateIdStr, requestDomain: raw.requestDomain.replace(/\/+$/, ''), // 去掉末尾斜杠 userId: raw.userId, categoryId: raw.categoryId, showCategorySwitch: raw.showCategorySwitch ?? false, title: raw.title || 'AI 智能助手', width: raw.width ?? 380, height: Math.max(300, raw.height ?? 520), position: raw.position === 'left-bottom' ? 'left-bottom' : 'right-bottom', primaryColor: resolvedPrimaryColor, launcherIcon: resolvedLauncherIcon, showClear: raw.showClear ?? true, showAdminPanel: raw.showAdminPanel ?? false, quickReplies: Array.isArray(raw.quickReplies) ? raw.quickReplies.map(s => String(s).trim()).filter(Boolean) : [], theme: raw.theme === 'dark' ? 'dark' : 'light', showTeaser: raw.showTeaser ?? true, teaserText: (typeof raw.teaserText === 'string' && raw.teaserText.trim()) || '', resizable: raw.resizable ?? true, streaming: raw.streaming ?? true, enableRag: raw.enableRag ?? true, rewriteStrategy: raw.rewriteStrategy || 'REWRITE', locale: raw.locale || 'zh-CN', debug: raw.debug ?? true, sound: raw.sound ?? false, notification: raw.notification ?? false, onError: typeof raw.onError === 'function' ? raw.onError : undefined, onReady: typeof raw.onReady === 'function' ? raw.onReady : undefined, onMessage: typeof raw.onMessage === 'function' ? raw.onMessage : undefined, token: raw.token, roles: raw.roles, chatId: '', // 初始为空,由 chatId 初始化流程填充 }; logger.info(`配置解析完成 integrateId(=roleId)=${config.integrateId} userId(=accountId)=${config.userId || '(未设置)'} requestDomain=${config.requestDomain}`); return config; }