You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
2.5 KiB
59 lines
2.5 KiB
/**
|
|
* 配置解析模块 - 参数校验 + 默认值填充
|
|
*/
|
|
import { SDKConfig, ResolvedConfig } from './types';
|
|
import { logger } from './logger';
|
|
|
|
/** 默认悬浮按钮 SVG 图标(客服对话气泡) */
|
|
const DEFAULT_LAUNCHER_ICON = `<svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>
|
|
<path d="M8 9h8"/><path d="M8 13h6"/>
|
|
</svg>`;
|
|
|
|
/**
|
|
* 解析并校验用户传入的配置,填充默认值
|
|
*/
|
|
export function parseConfig(raw: SDKConfig): ResolvedConfig | null {
|
|
// 校验必传参数:integrateId
|
|
if (!raw.integrateId || typeof raw.integrateId !== 'string' || raw.integrateId.trim() === '') {
|
|
logger.error('integrateId 是必传参数,请检查 init() 调用。示例:ChatbotSDK.init({ integrateId: "my-app", 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: "my-app", 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;
|
|
}
|
|
|
|
// 填充默认值
|
|
const config: ResolvedConfig = {
|
|
integrateId: raw.integrateId.trim(),
|
|
requestDomain: raw.requestDomain.replace(/\/+$/, ''), // 去掉末尾斜杠
|
|
userId: raw.userId,
|
|
roleId: raw.roleId,
|
|
categoryId: raw.categoryId,
|
|
showCategorySwitch: raw.showCategorySwitch ?? false,
|
|
title: raw.title || 'AI 智能助手',
|
|
width: raw.width ?? 380,
|
|
position: raw.position === 'left-bottom' ? 'left-bottom' : 'right-bottom',
|
|
primaryColor: raw.primaryColor || '#4F46E5',
|
|
launcherIcon: raw.launcherIcon || DEFAULT_LAUNCHER_ICON,
|
|
showClear: raw.showClear ?? true,
|
|
showAdminPanel: raw.showAdminPanel ?? false,
|
|
streaming: raw.streaming ?? true,
|
|
locale: raw.locale || 'zh-CN',
|
|
debug: raw.debug ?? true,
|
|
};
|
|
|
|
logger.info(`配置解析完成 integrateId=${config.integrateId} requestDomain=${config.requestDomain}`);
|
|
return config;
|
|
}
|