|
|
@ -258,17 +258,10 @@ var ChatbotSDK = (function () { |
|
|
onError: typeof raw.onError === 'function' ? raw.onError : undefined, |
|
|
onError: typeof raw.onError === 'function' ? raw.onError : undefined, |
|
|
onReady: typeof raw.onReady === 'function' ? raw.onReady : undefined, |
|
|
onReady: typeof raw.onReady === 'function' ? raw.onReady : undefined, |
|
|
onMessage: typeof raw.onMessage === 'function' ? raw.onMessage : undefined, |
|
|
onMessage: typeof raw.onMessage === 'function' ? raw.onMessage : undefined, |
|
|
// SDK 鉴权:token 和 roles(可选,用于新版鉴权模式)
|
|
|
|
|
|
token: typeof raw.token === 'string' && raw.token.trim() ? raw.token.trim() : undefined, |
|
|
|
|
|
roles: Array.isArray(raw.roles) && raw.roles.length > 0 ? raw.roles : undefined, |
|
|
|
|
|
|
|
|
token: raw.token, |
|
|
|
|
|
roles: raw.roles, |
|
|
chatId: '', // 初始为空,由 chatId 初始化流程填充
|
|
|
chatId: '', // 初始为空,由 chatId 初始化流程填充
|
|
|
}; |
|
|
}; |
|
|
// 鉴权模式日志
|
|
|
|
|
|
if (config.token) { |
|
|
|
|
|
logger.info(`鉴权模式: SDK Token 已配置, roles=${config.roles ? config.roles.length + '个' : '未配置'}`); |
|
|
|
|
|
} else { |
|
|
|
|
|
logger.info('兼容模式: 未配置 token,使用 integrateId 作为 roleId'); |
|
|
|
|
|
} |
|
|
|
|
|
logger.info(`配置解析完成 integrateId(=roleId)=${config.integrateId} userId(=accountId)=${config.userId || '(未设置)'} requestDomain=${config.requestDomain}`); |
|
|
logger.info(`配置解析完成 integrateId(=roleId)=${config.integrateId} userId(=accountId)=${config.userId || '(未设置)'} requestDomain=${config.requestDomain}`); |
|
|
return config; |
|
|
return config; |
|
|
} |
|
|
} |
|
|
@ -452,6 +445,10 @@ var ChatbotSDK = (function () { |
|
|
function setApiConfig(config) { |
|
|
function setApiConfig(config) { |
|
|
currentConfig = config; |
|
|
currentConfig = config; |
|
|
} |
|
|
} |
|
|
|
|
|
/** 清空当前配置(销毁 SDK 时调用) */ |
|
|
|
|
|
function clearApiConfig() { |
|
|
|
|
|
currentConfig = null; |
|
|
|
|
|
} |
|
|
/** 更新当前 chatId(对话 ID) */ |
|
|
/** 更新当前 chatId(对话 ID) */ |
|
|
function updateChatId(chatId) { |
|
|
function updateChatId(chatId) { |
|
|
if (currentConfig) { |
|
|
if (currentConfig) { |
|
|
@ -492,9 +489,8 @@ var ChatbotSDK = (function () { |
|
|
const params = new URLSearchParams(); |
|
|
const params = new URLSearchParams(); |
|
|
params.set('message', message); |
|
|
params.set('message', message); |
|
|
params.set('chatId', currentConfig.chatId); |
|
|
params.set('chatId', currentConfig.chatId); |
|
|
// 鉴权模式使用 selectedRoleId,兼容模式使用 integrateId
|
|
|
|
|
|
const roleId = currentConfig.token ? selectedRoleId : currentConfig.integrateId; |
|
|
|
|
|
setIfPresent(params, 'roleId', roleId); |
|
|
|
|
|
|
|
|
// integrateId 映射为 roleId
|
|
|
|
|
|
setIfPresent(params, 'roleId', currentConfig.integrateId); |
|
|
// userId 映射为 accountId
|
|
|
// userId 映射为 accountId
|
|
|
setIfPresent(params, 'accountId', currentConfig.userId); |
|
|
setIfPresent(params, 'accountId', currentConfig.userId); |
|
|
return buildUrl(`/ai/assistant_app/chat/sync?${params.toString()}`); |
|
|
return buildUrl(`/ai/assistant_app/chat/sync?${params.toString()}`); |
|
|
@ -506,9 +502,7 @@ var ChatbotSDK = (function () { |
|
|
const params = new URLSearchParams(); |
|
|
const params = new URLSearchParams(); |
|
|
params.set('message', message); |
|
|
params.set('message', message); |
|
|
params.set('chatId', currentConfig.chatId); |
|
|
params.set('chatId', currentConfig.chatId); |
|
|
// 鉴权模式使用 selectedRoleId,兼容模式使用 integrateId
|
|
|
|
|
|
const roleId = currentConfig.token ? selectedRoleId : currentConfig.integrateId; |
|
|
|
|
|
setIfPresent(params, 'roleId', roleId); |
|
|
|
|
|
|
|
|
setIfPresent(params, 'roleId', currentConfig.integrateId); |
|
|
setIfPresent(params, 'accountId', currentConfig.userId); |
|
|
setIfPresent(params, 'accountId', currentConfig.userId); |
|
|
setIfPresent(params, 'categoryId', categoryId !== null && categoryId !== void 0 ? categoryId : currentConfig.categoryId); |
|
|
setIfPresent(params, 'categoryId', categoryId !== null && categoryId !== void 0 ? categoryId : currentConfig.categoryId); |
|
|
return buildUrl(`/ai/assistant_app/chat/sse?${params.toString()}`); |
|
|
return buildUrl(`/ai/assistant_app/chat/sse?${params.toString()}`); |
|
|
@ -521,9 +515,7 @@ var ChatbotSDK = (function () { |
|
|
params.set('message', message); |
|
|
params.set('message', message); |
|
|
params.set('chatId', currentConfig.chatId); |
|
|
params.set('chatId', currentConfig.chatId); |
|
|
params.set('rewriteStrategy', currentConfig.rewriteStrategy || 'REWRITE'); |
|
|
params.set('rewriteStrategy', currentConfig.rewriteStrategy || 'REWRITE'); |
|
|
// 鉴权模式使用 selectedRoleId,兼容模式使用 integrateId
|
|
|
|
|
|
const roleId = currentConfig.token ? selectedRoleId : currentConfig.integrateId; |
|
|
|
|
|
setIfPresent(params, 'roleId', roleId); |
|
|
|
|
|
|
|
|
setIfPresent(params, 'roleId', currentConfig.integrateId); |
|
|
setIfPresent(params, 'accountId', currentConfig.userId); |
|
|
setIfPresent(params, 'accountId', currentConfig.userId); |
|
|
setIfPresent(params, 'categoryId', categoryId !== null && categoryId !== void 0 ? categoryId : currentConfig.categoryId); |
|
|
setIfPresent(params, 'categoryId', categoryId !== null && categoryId !== void 0 ? categoryId : currentConfig.categoryId); |
|
|
return buildUrl(`/ai/assistant_app/chat/rag/sse?${params.toString()}`); |
|
|
return buildUrl(`/ai/assistant_app/chat/rag/sse?${params.toString()}`); |
|
|
@ -536,9 +528,7 @@ var ChatbotSDK = (function () { |
|
|
params.set('message', message); |
|
|
params.set('message', message); |
|
|
params.set('chatId', currentConfig.chatId); |
|
|
params.set('chatId', currentConfig.chatId); |
|
|
params.set('rewriteStrategy', currentConfig.rewriteStrategy || 'REWRITE'); |
|
|
params.set('rewriteStrategy', currentConfig.rewriteStrategy || 'REWRITE'); |
|
|
// 鉴权模式使用 selectedRoleId,兼容模式使用 integrateId
|
|
|
|
|
|
const roleId = currentConfig.token ? selectedRoleId : currentConfig.integrateId; |
|
|
|
|
|
setIfPresent(params, 'roleId', roleId); |
|
|
|
|
|
|
|
|
setIfPresent(params, 'roleId', currentConfig.integrateId); |
|
|
setIfPresent(params, 'accountId', currentConfig.userId); |
|
|
setIfPresent(params, 'accountId', currentConfig.userId); |
|
|
setIfPresent(params, 'categoryId', categoryId !== null && categoryId !== void 0 ? categoryId : currentConfig.categoryId); |
|
|
setIfPresent(params, 'categoryId', categoryId !== null && categoryId !== void 0 ? categoryId : currentConfig.categoryId); |
|
|
return buildUrl(`/ai/assistant_app/rag/sources?${params.toString()}`); |
|
|
return buildUrl(`/ai/assistant_app/rag/sources?${params.toString()}`); |
|
|
@ -557,13 +547,27 @@ var ChatbotSDK = (function () { |
|
|
externalSignal.addEventListener('abort', () => controller.abort(), { once: true }); |
|
|
externalSignal.addEventListener('abort', () => controller.abort(), { once: true }); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
// 注入 SDK Token 认证头(仅对 /ai/ 路径注入,避免将 SDK Token 发送到管理接口)
|
|
|
|
|
|
const headers = Object.assign({}, options.headers || {}); |
|
|
|
|
|
if (currentConfig && currentConfig.token && url.includes('/ai/')) { |
|
|
|
|
|
headers['Authorization'] = `Bearer ${currentConfig.token}`; |
|
|
|
|
|
} |
|
|
|
|
|
try { |
|
|
try { |
|
|
|
|
|
// 注入 SDK Token 认证头(仅对 /ai/ 路径注入,避免将 SDK Token 发送到管理接口)
|
|
|
|
|
|
const headers = {}; |
|
|
|
|
|
if (options.headers) { |
|
|
|
|
|
if (options.headers instanceof Headers) { |
|
|
|
|
|
options.headers.forEach((value, key) => { headers[key] = value; }); |
|
|
|
|
|
} |
|
|
|
|
|
else if (Array.isArray(options.headers)) { |
|
|
|
|
|
options.headers.forEach(([key, value]) => { headers[key] = value; }); |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
Object.assign(headers, options.headers); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if ((currentConfig === null || currentConfig === void 0 ? void 0 : currentConfig.token) && url.includes('/ai/')) { |
|
|
|
|
|
headers['Authorization'] = `Bearer ${currentConfig.token}`; |
|
|
|
|
|
} |
|
|
const response = await fetch(url, Object.assign(Object.assign({}, options), { headers, signal: controller.signal, mode: 'cors', credentials: 'include' })); |
|
|
const response = await fetch(url, Object.assign(Object.assign({}, options), { headers, signal: controller.signal, mode: 'cors', credentials: 'include' })); |
|
|
|
|
|
if (response.status === 401 && url.includes('/ai/')) { |
|
|
|
|
|
logger.error('SDK 认证失败:Token 缺失或已过期,请重新调用 /open-api/auth/token 换取 Token'); |
|
|
|
|
|
} |
|
|
return response; |
|
|
return response; |
|
|
} |
|
|
} |
|
|
catch (err) { |
|
|
catch (err) { |
|
|
@ -845,9 +849,8 @@ var ChatbotSDK = (function () { |
|
|
const params = new URLSearchParams(); |
|
|
const params = new URLSearchParams(); |
|
|
if (currentConfig === null || currentConfig === void 0 ? void 0 : currentConfig.userId) |
|
|
if (currentConfig === null || currentConfig === void 0 ? void 0 : currentConfig.userId) |
|
|
params.set('accountId', currentConfig.userId); |
|
|
params.set('accountId', currentConfig.userId); |
|
|
const roleId = currentConfig.token ? selectedRoleId : currentConfig.integrateId; |
|
|
|
|
|
if (roleId) |
|
|
|
|
|
params.set('roleId', roleId); |
|
|
|
|
|
|
|
|
if (currentConfig === null || currentConfig === void 0 ? void 0 : currentConfig.integrateId) |
|
|
|
|
|
params.set('roleId', currentConfig.integrateId); |
|
|
const url = buildUrl(`/ai/sdk/conversation/${conversationId}/messages?${params.toString()}`); |
|
|
const url = buildUrl(`/ai/sdk/conversation/${conversationId}/messages?${params.toString()}`); |
|
|
try { |
|
|
try { |
|
|
const response = await safeFetch(url); |
|
|
const response = await safeFetch(url); |
|
|
@ -872,9 +875,8 @@ var ChatbotSDK = (function () { |
|
|
const params = new URLSearchParams(); |
|
|
const params = new URLSearchParams(); |
|
|
if (currentConfig === null || currentConfig === void 0 ? void 0 : currentConfig.userId) |
|
|
if (currentConfig === null || currentConfig === void 0 ? void 0 : currentConfig.userId) |
|
|
params.set('accountId', currentConfig.userId); |
|
|
params.set('accountId', currentConfig.userId); |
|
|
const roleId = currentConfig.token ? selectedRoleId : currentConfig.integrateId; |
|
|
|
|
|
if (roleId) |
|
|
|
|
|
params.set('roleId', roleId); |
|
|
|
|
|
|
|
|
if (currentConfig === null || currentConfig === void 0 ? void 0 : currentConfig.integrateId) |
|
|
|
|
|
params.set('roleId', currentConfig.integrateId); |
|
|
const url = buildUrl(`/ai/sdk/conversation/${conversationId}?${params.toString()}`); |
|
|
const url = buildUrl(`/ai/sdk/conversation/${conversationId}?${params.toString()}`); |
|
|
try { |
|
|
try { |
|
|
const response = await safeFetch(url, { method: 'DELETE' }); |
|
|
const response = await safeFetch(url, { method: 'DELETE' }); |
|
|
@ -897,9 +899,8 @@ var ChatbotSDK = (function () { |
|
|
const params = new URLSearchParams(); |
|
|
const params = new URLSearchParams(); |
|
|
if (currentConfig === null || currentConfig === void 0 ? void 0 : currentConfig.userId) |
|
|
if (currentConfig === null || currentConfig === void 0 ? void 0 : currentConfig.userId) |
|
|
params.set('accountId', currentConfig.userId); |
|
|
params.set('accountId', currentConfig.userId); |
|
|
const roleId = currentConfig.token ? selectedRoleId : currentConfig.integrateId; |
|
|
|
|
|
if (roleId) |
|
|
|
|
|
params.set('roleId', roleId); |
|
|
|
|
|
|
|
|
if (currentConfig === null || currentConfig === void 0 ? void 0 : currentConfig.integrateId) |
|
|
|
|
|
params.set('roleId', currentConfig.integrateId); |
|
|
return buildUrl(`/ai/sdk/conversation/${conversationId}/export?${params.toString()}`); |
|
|
return buildUrl(`/ai/sdk/conversation/${conversationId}/export?${params.toString()}`); |
|
|
} |
|
|
} |
|
|
// ==================== chatId 自动初始化 ====================
|
|
|
// ==================== chatId 自动初始化 ====================
|
|
|
@ -924,8 +925,7 @@ var ChatbotSDK = (function () { |
|
|
} |
|
|
} |
|
|
// 2. 查询后端会话列表
|
|
|
// 2. 查询后端会话列表
|
|
|
try { |
|
|
try { |
|
|
const roleId = currentConfig.token ? (selectedRoleId || currentConfig.integrateId) : currentConfig.integrateId; |
|
|
|
|
|
const result = await fetchConversationList(1, 5, currentConfig.userId, roleId); |
|
|
|
|
|
|
|
|
const result = await fetchConversationList(1, 5, currentConfig.userId, currentConfig.integrateId); |
|
|
if (result.list.length > 0) { |
|
|
if (result.list.length > 0) { |
|
|
// 使用最新会话的 conversationId 作为 chatId
|
|
|
// 使用最新会话的 conversationId 作为 chatId
|
|
|
const latestConv = result.list[0]; |
|
|
const latestConv = result.list[0]; |
|
|
@ -1355,26 +1355,6 @@ var ChatbotSDK = (function () { |
|
|
70% { box-shadow: 0 0 0 6px rgba(110, 231, 183, 0); } |
|
|
70% { box-shadow: 0 0 0 6px rgba(110, 231, 183, 0); } |
|
|
100% { box-shadow: 0 0 0 0 rgba(110, 231, 183, 0); } |
|
|
100% { box-shadow: 0 0 0 0 rgba(110, 231, 183, 0); } |
|
|
} |
|
|
} |
|
|
.csk-role-selector { |
|
|
|
|
|
margin-left: 8px; |
|
|
|
|
|
padding: 2px 6px; |
|
|
|
|
|
font-size: 11px; |
|
|
|
|
|
border: 1px solid rgba(255,255,255,0.3); |
|
|
|
|
|
border-radius: 6px; |
|
|
|
|
|
background: rgba(255,255,255,0.15); |
|
|
|
|
|
color: #fff; |
|
|
|
|
|
cursor: pointer; |
|
|
|
|
|
outline: none; |
|
|
|
|
|
max-width: 120px; |
|
|
|
|
|
appearance: auto; |
|
|
|
|
|
} |
|
|
|
|
|
.csk-role-selector:focus { |
|
|
|
|
|
border-color: rgba(255,255,255,0.6); |
|
|
|
|
|
} |
|
|
|
|
|
.csk-role-selector option { |
|
|
|
|
|
color: #333; |
|
|
|
|
|
background: #fff; |
|
|
|
|
|
} |
|
|
|
|
|
.csk-header__actions { |
|
|
.csk-header__actions { |
|
|
display: flex; |
|
|
display: flex; |
|
|
align-items: center; |
|
|
align-items: center; |
|
|
@ -2562,8 +2542,6 @@ var ChatbotSDK = (function () { |
|
|
actions.appendChild(closeBtn); |
|
|
actions.appendChild(closeBtn); |
|
|
header.appendChild(headerLeft); |
|
|
header.appendChild(headerLeft); |
|
|
header.appendChild(actions); |
|
|
header.appendChild(actions); |
|
|
// 渲染角色选择器(鉴权模式下,≥2 角色时显示下拉框)
|
|
|
|
|
|
renderRoleSelector(header); |
|
|
|
|
|
// === 消息区 ===
|
|
|
// === 消息区 ===
|
|
|
const messagesContainer = document.createElement('div'); |
|
|
const messagesContainer = document.createElement('div'); |
|
|
messagesContainer.id = 'csk-messages'; |
|
|
messagesContainer.id = 'csk-messages'; |
|
|
@ -4312,8 +4290,7 @@ var ChatbotSDK = (function () { |
|
|
return; |
|
|
return; |
|
|
listEl.innerHTML = `<div class="csk-history-panel__loading">加载中...</div>`; |
|
|
listEl.innerHTML = `<div class="csk-history-panel__loading">加载中...</div>`; |
|
|
try { |
|
|
try { |
|
|
const roleId = config$1.token ? (selectedRoleId || config$1.integrateId) : config$1.integrateId; |
|
|
|
|
|
const result = await fetchConversationList(1, 50, config$1.userId, roleId); |
|
|
|
|
|
|
|
|
const result = await fetchConversationList(1, 50, config$1.userId, config$1.integrateId); |
|
|
historyItems = result.list.map(c => ({ |
|
|
historyItems = result.list.map(c => ({ |
|
|
id: c.conversationId || '', |
|
|
id: c.conversationId || '', |
|
|
chatId: c.conversationId || '', |
|
|
chatId: c.conversationId || '', |
|
|
@ -4415,62 +4392,12 @@ var ChatbotSDK = (function () { |
|
|
let launcherPos = null; |
|
|
let launcherPos = null; |
|
|
/** 提示气泡元素 */ |
|
|
/** 提示气泡元素 */ |
|
|
let teaserEl = null; |
|
|
let teaserEl = null; |
|
|
/** SDK 鉴权:当前选中的角色 ID(从 roles 列表中选择) */ |
|
|
|
|
|
let selectedRoleId = null; |
|
|
|
|
|
/** 提示气泡定时器 */ |
|
|
/** 提示气泡定时器 */ |
|
|
let teaserTimer = null; |
|
|
let teaserTimer = null; |
|
|
/** 未读徽章元素(挂在 launcher 内部) */ |
|
|
/** 未读徽章元素(挂在 launcher 内部) */ |
|
|
let badgeEl = null; |
|
|
let badgeEl = null; |
|
|
/** 音频上下文(提示音用) */ |
|
|
/** 音频上下文(提示音用) */ |
|
|
let audioCtx = null; |
|
|
let audioCtx = null; |
|
|
|
|
|
|
|
|
// ==================== 角色选择器 ====================
|
|
|
|
|
|
/** |
|
|
|
|
|
* 渲染角色选择器下拉框(挂在 header 区域) |
|
|
|
|
|
* - 只有 1 个角色时:自动选定,不渲染下拉框 |
|
|
|
|
|
* - ≥2 个角色时:渲染下拉框,支持切换 |
|
|
|
|
|
* - 从 localStorage 恢复上次选择 |
|
|
|
|
|
*/ |
|
|
|
|
|
function renderRoleSelector(headerEl) { |
|
|
|
|
|
if (!currentConfig || !currentConfig.token || !currentConfig.roles) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
const roles = currentConfig.roles; |
|
|
|
|
|
if (roles.length === 0) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
if (roles.length === 1) { |
|
|
|
|
|
// 只有一个角色:自动选定,不渲染下拉框
|
|
|
|
|
|
selectedRoleId = String(roles[0].id); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
// ≥2 个角色:渲染下拉框
|
|
|
|
|
|
const select = document.createElement('select'); |
|
|
|
|
|
select.className = 'csk-role-selector'; |
|
|
|
|
|
roles.forEach(role => { |
|
|
|
|
|
const option = document.createElement('option'); |
|
|
|
|
|
option.value = String(role.id); |
|
|
|
|
|
option.textContent = role.name; |
|
|
|
|
|
select.appendChild(option); |
|
|
|
|
|
}); |
|
|
|
|
|
// 从 localStorage 恢复上次选择
|
|
|
|
|
|
const storageKey = `csk_role_${currentConfig.integrateId}_${currentConfig.userId || 'default'}`; |
|
|
|
|
|
const savedRoleId = localStorage.getItem(storageKey); |
|
|
|
|
|
if (savedRoleId && roles.some(r => String(r.id) === savedRoleId)) { |
|
|
|
|
|
select.value = savedRoleId; |
|
|
|
|
|
selectedRoleId = savedRoleId; |
|
|
|
|
|
} else { |
|
|
|
|
|
selectedRoleId = String(roles[0].id); |
|
|
|
|
|
} |
|
|
|
|
|
select.addEventListener('change', () => { |
|
|
|
|
|
selectedRoleId = select.value; |
|
|
|
|
|
localStorage.setItem(storageKey, select.value); |
|
|
|
|
|
logger.info(`切换角色 roleId=${selectedRoleId}`); |
|
|
|
|
|
}); |
|
|
|
|
|
// 挂到 header 的左侧区域后面
|
|
|
|
|
|
headerEl.appendChild(select); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ==================== 公开 API ====================
|
|
|
// ==================== 公开 API ====================
|
|
|
/** 初始化 SDK */ |
|
|
/** 初始化 SDK */ |
|
|
function init(rawConfig) { |
|
|
function init(rawConfig) { |
|
|
@ -4760,7 +4687,6 @@ var ChatbotSDK = (function () { |
|
|
launcherDragCleanup = null; |
|
|
launcherDragCleanup = null; |
|
|
} |
|
|
} |
|
|
launcherPos = null; |
|
|
launcherPos = null; |
|
|
selectedRoleId = null; |
|
|
|
|
|
removeStyles(); |
|
|
removeStyles(); |
|
|
document.removeEventListener('keydown', onKeyDown); |
|
|
document.removeEventListener('keydown', onKeyDown); |
|
|
window.removeEventListener('resize', onResize); |
|
|
window.removeEventListener('resize', onResize); |
|
|
@ -4778,6 +4704,7 @@ var ChatbotSDK = (function () { |
|
|
hideLoadingFn = null; |
|
|
hideLoadingFn = null; |
|
|
badgeEl = null; |
|
|
badgeEl = null; |
|
|
audioCtx = null; |
|
|
audioCtx = null; |
|
|
|
|
|
clearApiConfig(); |
|
|
logger.lifecycleDestroy(oldIntegrateId || ''); |
|
|
logger.lifecycleDestroy(oldIntegrateId || ''); |
|
|
} |
|
|
} |
|
|
function open() { |
|
|
function open() { |
|
|
|