feat(ai-agent): 新增AI智能体通用支持框架
1. 新增多语言文案,适配AI智能体通用场景 2. 新增AI智能体页面路由常量与跳转逻辑 3. 改造AI聊天页面支持动态agent_id和页面标题 4. 升级AI服务层,支持多agent_id自动传递
This commit is contained in:
@@ -4,6 +4,13 @@ import store from '@/store/index.js'
|
||||
import lang from './lang.js'
|
||||
|
||||
let currentConversationId = null;
|
||||
|
||||
// ====== AI Agent Generalization (customer service is the first agent) ======
|
||||
// setAgentId updates currentAgentId; default is agent_aikefu.
|
||||
// Any sendMessage / sendStreamMessage / getServiceConfig call passes agent_id automatically.
|
||||
|
||||
export const DEFAULT_AGENT_ID = 'agent_aikefu';
|
||||
let currentAgentId = DEFAULT_AGENT_ID;
|
||||
export const CONVERSATION_KEY = 'ai_conversation_id';
|
||||
|
||||
/**
|
||||
@@ -50,6 +57,20 @@ function getUserId() {
|
||||
return guestId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前 AI Agent。
|
||||
*/
|
||||
function getAgentId() {
|
||||
return currentAgentId || DEFAULT_AGENT_ID;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置当前 AI Agent。
|
||||
*/
|
||||
function setAgentId(agentId) {
|
||||
if (agentId && typeof agentId === 'string') currentAgentId = agentId;
|
||||
}
|
||||
|
||||
// #ifdef MP
|
||||
// ================================
|
||||
// WebSocket 长连接管理(微信小程序)
|
||||
@@ -106,6 +127,7 @@ function ensureWebSocketConnected() {
|
||||
wsSocketTask.send({
|
||||
data: JSON.stringify({
|
||||
action: 'auth',
|
||||
agent_id: getAgentId(),
|
||||
uniacid: _uniacid,
|
||||
token: _token,
|
||||
user_id: _userId
|
||||
@@ -241,6 +263,7 @@ export default {
|
||||
const params = {
|
||||
url: '/api/kefu/chat',
|
||||
data: {
|
||||
agent_id: getAgentId(),
|
||||
user_id: getUserId(),
|
||||
stream: false,
|
||||
inputs: {},
|
||||
@@ -305,6 +328,7 @@ export default {
|
||||
wsSocketTask.send({
|
||||
data: JSON.stringify({
|
||||
action: 'chat',
|
||||
agent_id: getAgentId(),
|
||||
uniacid: this.getUniacid(),
|
||||
query: message,
|
||||
user_id: getUserId(),
|
||||
@@ -484,6 +508,7 @@ export default {
|
||||
const params = {
|
||||
url: '/api/kefu/createConversation',
|
||||
data: {
|
||||
agent_id: getAgentId(),
|
||||
uniacid: this.getUniacid(),
|
||||
token: store.state.token || 'test_token',
|
||||
user_id: getUserId(),
|
||||
@@ -533,6 +558,7 @@ export default {
|
||||
const params = {
|
||||
url: '/api/kefu/info',
|
||||
data: {
|
||||
agent_id: getAgentId(),
|
||||
uniacid: this.getUniacid(),
|
||||
token: store.state.token || 'test_token',
|
||||
user_id: getUserId(),
|
||||
@@ -563,6 +589,7 @@ export default {
|
||||
const response = await http.sendRequest({
|
||||
url: '/api/kefu/clear-conversation',
|
||||
data: {
|
||||
agent_id: getAgentId(),
|
||||
conversation_id: conversationId,
|
||||
...getDomainParams()
|
||||
},
|
||||
@@ -585,6 +612,7 @@ export default {
|
||||
}
|
||||
console.log('[AI History] 请求历史, conversation_id:', params.conversation_id, 'limit:', params.limit);
|
||||
const requestData = {
|
||||
agent_id: params.agent_id || getAgentId(),
|
||||
uniacid: params.uniacid || this.getUniacid(),
|
||||
conversation_id: params.conversation_id,
|
||||
user_id: params.user_id || getUserId(),
|
||||
@@ -632,6 +660,7 @@ export default {
|
||||
window.currentEventSource.close();
|
||||
}
|
||||
const paramsObj = {
|
||||
agent_id: getAgentId(),
|
||||
uniacid: this.getUniacid(),
|
||||
user_id: getUserId(),
|
||||
member_id: getUserId(),
|
||||
@@ -711,4 +740,20 @@ export default {
|
||||
// H5 无 WebSocket 长连接,无需操作
|
||||
},
|
||||
// #endif
|
||||
|
||||
/**
|
||||
* Set current AI Agent (default aikefu; link system passes agent_id).
|
||||
* @param {string} agentId
|
||||
*/
|
||||
setAgentId(agentId) {
|
||||
setAgentId(agentId);
|
||||
},
|
||||
|
||||
/**
|
||||
* Get current AI Agent.
|
||||
* @returns {string}
|
||||
*/
|
||||
getAgentId() {
|
||||
return getAgentId();
|
||||
},
|
||||
};
|
||||
|
||||
@@ -138,7 +138,8 @@ export const CATEGORY_PAGE_URL = '/pages_goods/category';
|
||||
export const CONTACT_PAGE_URL = '/pages_tool/contact/contact';
|
||||
export const MEMBER_PAGE_URL = '/pages_tool/member/index';
|
||||
export const LOGIN_PAGE_URL = '/pages_tool/login/login';
|
||||
export const AI_CHAT_PAGE_URL = '/pages_tool/ai-chat/index';
|
||||
export const AI_CHAT_PAGE_URL = '/pages_tool/ai-chat/index';
|
||||
export const AI_AGENT_PAGE_URL = '/pages_tool/ai-chat/index'; // AI 智能体入口页(智能客服是首个 agent)
|
||||
|
||||
// page_key 到实际路径的映射表(统一管理所有页面的语义常量 → 实际路径)
|
||||
// 前端组织页面(重命名路径、分包调整)时只需要修改此映射表
|
||||
@@ -1203,6 +1204,19 @@ export default {
|
||||
* @param {Object} link
|
||||
*/
|
||||
diyRedirectTo(link) {
|
||||
|
||||
// ---------- AI Agent 通用链接(智能客服是首个 agent;新增 AI 销售/HR 等不用改这段) ----------
|
||||
// 放在 stale-uniacid 与 resolveLinkUrl 之前:AI Agent 是纯内部路径,走后续 webview 分支会被错判。
|
||||
if (link && (link.name === 'AI_AGENT' || link.kind === 'AI_AGENT')) {
|
||||
const agentId = (link.page_params && link.page_params.agent_id) || 'agent_aikefu';
|
||||
const title = link.title || 'AI 智能体';
|
||||
return this.redirectTo(AI_AGENT_PAGE_URL, {
|
||||
agent_id: agentId,
|
||||
title: encodeURIComponent(title),
|
||||
enable_set_title: link.enable_set_title ? 1 : 0,
|
||||
});
|
||||
}
|
||||
|
||||
console.log(`-----> diyRedirectTo`, JSON.stringify(link));
|
||||
//if (link == null || Object.keys(link).length == 1) return;
|
||||
|
||||
|
||||
2090
common/js/util.js.bak
Normal file
2090
common/js/util.js.bak
Normal file
File diff suppressed because it is too large
Load Diff
@@ -136,4 +136,8 @@ export const lang = {
|
||||
logInitNavbarFailed: 'Failed to initialize navigation bar',
|
||||
logChatComponentNotInit: 'Chat component not initialized',
|
||||
logServicerConfigNotLoaded: '[AI Customer Service] servicerConfig not loaded, retrying after 200ms...'
|
||||
}
|
||||
|
||||
// ====== AI Agent general copy (customer service is the first agent) ======
|
||||
aiAgentDefaultTitle: 'AI Agent',
|
||||
aiAgentOpenFailed: 'Failed to open AI Agent, please try again later',
|
||||
aiAgentNotConfigured: 'This AI Agent is not enabled'}
|
||||
|
||||
@@ -136,4 +136,8 @@ export const lang = {
|
||||
logInitNavbarFailed: '初始化导航栏失败',
|
||||
logChatComponentNotInit: '聊天组件未初始化',
|
||||
logServicerConfigNotLoaded: '【AI客服】servicerConfig 未加载,200ms后重试...'
|
||||
}
|
||||
|
||||
// ====== AI Agent 通用文案(智能客服是首个 agent) ======
|
||||
aiAgentDefaultTitle: 'AI 智能体',
|
||||
aiAgentOpenFailed: '打开 AI 智能体失败,请稍后再试',
|
||||
aiAgentNotConfigured: '该 AI 智能体暂未启用'}
|
||||
|
||||
@@ -87,6 +87,9 @@ export default {
|
||||
return {
|
||||
initialMessages: [],
|
||||
fallbackWelcome: '',
|
||||
// AI Agent 通用入参(链接系统透传;老客服入口不传也能跑,默认 agent_aikefu)
|
||||
agentId: 'agent_aikefu',
|
||||
pageTitle: '',
|
||||
|
||||
// --- 系统导航栏/状态栏相关 ---
|
||||
navBarHeight: 44, // uni-page-head 系统导航栏,默认高度为44px
|
||||
@@ -127,6 +130,21 @@ export default {
|
||||
async onLoad(options) {
|
||||
this.fallbackWelcome = this.$lang('ai-chat.index.fallbackWelcome');
|
||||
|
||||
// ====== AI Agent 通用入参(链接系统 -> 通用 agent_id/title) ======
|
||||
// 老客服入口不传 options 也能跑(智能客服是首个 agent,默认 agent_aikefu)。
|
||||
// 新增 AI 销售/HR/...智能体只需在 link.page_params.agent_id 传不同值,
|
||||
// 当前 onShow/initChat 不区分 agent 行为,后续按业务扩展按 agentId 路由即可。
|
||||
try {
|
||||
this.agentId = (options && options.agent_id) || 'agent_aikefu';
|
||||
const rawTitle = (options && options.title) || this.$lang('ai-chat.index.title');
|
||||
this.pageTitle = decodeURIComponent(rawTitle);
|
||||
// 同步 agentId 给 ai-service,后续 sendMessage / WS auth 自动带新 agent_id
|
||||
try { aiService.setAgentId(this.agentId); } catch (e) { /* 兼容旧 ai-service 版本 */ }
|
||||
} catch (e) {
|
||||
this.agentId = 'agent_aikefu';
|
||||
this.pageTitle = this.$lang('ai-chat.index.title');
|
||||
}
|
||||
|
||||
// ✅ 修改:取消权限校验,允许所有客服类型访问此页面
|
||||
// const hasAccess = this.checkAccessPermission();
|
||||
// if (!hasAccess) {
|
||||
@@ -134,7 +152,7 @@ export default {
|
||||
// return;
|
||||
// }
|
||||
|
||||
this.$langConfig.title(this.$lang('ai-chat.index.title'));
|
||||
this.$langConfig.title(this.pageTitle || this.$lang('ai-chat.index.title'));
|
||||
this.initChat();
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user