|
@@ -3,45 +3,9 @@ const { restrictionMessageForUser } = require('./login-restrictions');
|
|
|
const TABS = [
|
|
const TABS = [
|
|
|
{ key: 'cooperation', label: '机构合作', prefix: 'CO', initial: '待处理' },
|
|
{ key: 'cooperation', label: '机构合作', prefix: 'CO', initial: '待处理' },
|
|
|
{ key: 'feedback', label: '问题反馈', prefix: 'FB', initial: '待处理' },
|
|
{ key: 'feedback', label: '问题反馈', prefix: 'FB', initial: '待处理' },
|
|
|
- { key: 'ai_reply_report', label: 'AI 回复举报', prefix: 'AR', initial: '已受理' },
|
|
|
|
|
|
|
+ { key: 'ai_reply_report', label: 'AI 回复举报' },
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
-const REPORT_CATEGORIES = [
|
|
|
|
|
- { value: 'identity_misleading', label: 'AI 身份误导' },
|
|
|
|
|
- { value: 'life_health_harm', label: '自伤、自杀或暴力不当回复' },
|
|
|
|
|
- { value: 'emotional_manipulation', label: '情感操控或诱导沉迷' },
|
|
|
|
|
- { value: 'privacy_leak', label: '隐私泄露' },
|
|
|
|
|
- { value: 'minor_inappropriate', label: '未成年人不适宜' },
|
|
|
|
|
- { value: 'other_harmful', label: '虚假或其他有害内容' },
|
|
|
|
|
-];
|
|
|
|
|
-
|
|
|
|
|
-function pickerChoices(items, label) {
|
|
|
|
|
- return [{ label }, ...items.map(item => ({ label: item.label }))];
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function selectedPickerIndex(value, items) {
|
|
|
|
|
- if (typeof value !== 'number' && (typeof value !== 'string' || !/^\d+$/.test(value))) return -1;
|
|
|
|
|
- const index = Number(value) - 1;
|
|
|
|
|
- return Number.isInteger(index) && index >= 0 && index < items.length ? index : -1;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-const INPUT_MESSAGES = [
|
|
|
|
|
- '请填写有效联系电话', '请填写联系人姓名', '请填写问题描述',
|
|
|
|
|
- '请先选择自己的对话', '请先选择要举报的 AI 回复', '请选择举报类型',
|
|
|
|
|
-];
|
|
|
|
|
-
|
|
|
|
|
-function submitErrorMessage(error, type) {
|
|
|
|
|
- if (INPUT_MESSAGES.includes(error?.message)) return error.message;
|
|
|
|
|
- if (type === 'ai_reply_report') {
|
|
|
|
|
- const code = Number(error?.code || error?.statusCode);
|
|
|
|
|
- if (code === 209 || code === 401) return '登录已失效,请重新登录后提交举报。';
|
|
|
|
|
- if (code === 119 || code === 403) return '当前账号暂不能提交这段对话的举报,请重新登录并重新选择自己的对话。';
|
|
|
|
|
- if (code === 101 || code === 404) return '这段对话已不可用,请重新选择。';
|
|
|
|
|
- if (code === 111 || code === 142) return '举报资料未通过服务端校验,请重新选择对话和回复后提交。';
|
|
|
|
|
- }
|
|
|
|
|
- return '提交结果待核对,请先刷新处理记录。';
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
function normalizeContactPhone(value) {
|
|
function normalizeContactPhone(value) {
|
|
|
const phone = String(value || '').trim().replace(/[ ()-]/g, '');
|
|
const phone = String(value || '').trim().replace(/[ ()-]/g, '');
|
|
|
return /^\+?[0-9]{7,15}$/.test(phone) ? phone : '';
|
|
return /^\+?[0-9]{7,15}$/.test(phone) ? phone : '';
|
|
@@ -103,57 +67,14 @@ async function listTickets(scope, type) {
|
|
|
return mapRecords(await query.find({ sessionToken: scope.sessionToken }), type);
|
|
return mapRecords(await query.find({ sessionToken: scope.sessionToken }), type);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-async function listConversations(scope) {
|
|
|
|
|
- const query = ownedQuery(scope, 'ChatSession', 'user');
|
|
|
|
|
- query.select('title', 'sessionType');
|
|
|
|
|
- query.descending('updatedAt');
|
|
|
|
|
- query.limit(30);
|
|
|
|
|
- const rows = await query.find({ sessionToken: scope.sessionToken });
|
|
|
|
|
- return rows.filter(row => row.get('sessionType') !== 'recording').map(row => ({
|
|
|
|
|
- id: row.id,
|
|
|
|
|
- label: [row.get('title') || '对话', displayTime(row.updatedAt || row.createdAt)].filter(Boolean).join(' · '),
|
|
|
|
|
- }));
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function replyChoices(messages) {
|
|
|
|
|
- if (!Array.isArray(messages)) return [];
|
|
|
|
|
- let previous = null;
|
|
|
|
|
- const replies = [];
|
|
|
|
|
- messages.forEach((message, index) => {
|
|
|
|
|
- if (!message || message.hidden === true) return;
|
|
|
|
|
- const source = message.content ?? message.text;
|
|
|
|
|
- const text = typeof source === 'string' ? source.trim().slice(0, 4000) : '';
|
|
|
|
|
- if (!text) return;
|
|
|
|
|
- const id = String(message.id || message.clientMessageId || index);
|
|
|
|
|
- if (message.role === 'user') previous = { id, text };
|
|
|
|
|
- if (message.role === 'assistant' || message.role === 'ai') {
|
|
|
|
|
- replies.push({ id, text, previous, label: text.slice(0, 72) });
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- return replies.slice(-50).reverse();
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-async function listReplies(scope, sessionId) {
|
|
|
|
|
- const query = ownedQuery(scope, 'ChatSession', 'user');
|
|
|
|
|
- query.equalTo('objectId', sessionId);
|
|
|
|
|
- query.select('messageList', 'sessionType');
|
|
|
|
|
- const row = await query.first({ sessionToken: scope.sessionToken });
|
|
|
|
|
- if (!row || row.get('sessionType') === 'recording') return [];
|
|
|
|
|
- return replyChoices(row.get('messageList'));
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
async function submitTicket(scope, type, form) {
|
|
async function submitTicket(scope, type, form) {
|
|
|
|
|
+ if (type === 'ai_reply_report') throw Error('请在对话里举报此回复');
|
|
|
const tab = TABS.find(item => item.key === type);
|
|
const tab = TABS.find(item => item.key === type);
|
|
|
if (!tab) throw Error('工单类型无效');
|
|
if (!tab) throw Error('工单类型无效');
|
|
|
- const isAI = type === 'ai_reply_report';
|
|
|
|
|
const name = String(form.name || '').trim();
|
|
const name = String(form.name || '').trim();
|
|
|
- const content = String(form.content || '').trim().slice(0, isAI ? 500 : 2000);
|
|
|
|
|
|
|
+ const content = String(form.content || '').trim().slice(0, 2000);
|
|
|
const phone = normalizeContactPhone(form.phone);
|
|
const phone = normalizeContactPhone(form.phone);
|
|
|
- const category = REPORT_CATEGORIES.find(item => item.value === form.category);
|
|
|
|
|
- if (isAI && !form.sessionId) throw Error('请先选择自己的对话');
|
|
|
|
|
- if (isAI && !form.reply?.text) throw Error('请先选择要举报的 AI 回复');
|
|
|
|
|
- if (isAI && !category) throw Error('请选择举报类型');
|
|
|
|
|
- if (!isAI && !phone) throw Error('请填写有效联系电话');
|
|
|
|
|
|
|
+ if (!phone) throw Error('请填写有效联系电话');
|
|
|
if (type === 'cooperation' && !name) throw Error('请填写联系人姓名');
|
|
if (type === 'cooperation' && !name) throw Error('请填写联系人姓名');
|
|
|
if (type === 'feedback' && !content) throw Error('请填写问题描述');
|
|
if (type === 'feedback' && !content) throw Error('请填写问题描述');
|
|
|
|
|
|
|
@@ -164,32 +85,16 @@ async function submitTicket(scope, type, form) {
|
|
|
submitter: pointer(scope.parse, '_User', scope.user.id),
|
|
submitter: pointer(scope.parse, '_User', scope.user.id),
|
|
|
submitterName: (type === 'cooperation' ? name : (scope.user.get('nickname') || '微信用户')).slice(0, 80),
|
|
submitterName: (type === 'cooperation' ? name : (scope.user.get('nickname') || '微信用户')).slice(0, 80),
|
|
|
ticketNo, ticketType: type, source: tab.label, progress: tab.initial, isDeleted: false,
|
|
ticketNo, ticketType: type, source: tab.label, progress: tab.initial, isDeleted: false,
|
|
|
- content: content || (isAI ? category.label : ''),
|
|
|
|
|
|
|
+ contactPhone: phone, content,
|
|
|
});
|
|
});
|
|
|
- if (isAI) {
|
|
|
|
|
- const reply = form.reply;
|
|
|
|
|
- const consentedMessages = [];
|
|
|
|
|
- if (form.includePrevious && reply.previous) {
|
|
|
|
|
- consentedMessages.push({ role: 'user', messageId: reply.previous.id, content: reply.previous.text });
|
|
|
|
|
- }
|
|
|
|
|
- consentedMessages.push({ role: 'assistant', messageId: reply.id, content: reply.text });
|
|
|
|
|
- ticket.set({
|
|
|
|
|
- session: pointer(scope.parse, 'ChatSession', form.sessionId),
|
|
|
|
|
- sessionId: form.sessionId, assistantMessageId: reply.id,
|
|
|
|
|
- resolutionStatus: tab.initial, priority: category.value === 'life_health_harm' ? 'P0' : 'P1',
|
|
|
|
|
- reportCategory: category.value, reportCategoryLabel: category.label, consentedMessages,
|
|
|
|
|
- expectedHandledAt: new Date(Date.now() + 86400000),
|
|
|
|
|
- idempotencyKey: ['ai-reply-report-v1', scope.user.id, form.sessionId, reply.id, category.value].join(':'),
|
|
|
|
|
- });
|
|
|
|
|
- } else {
|
|
|
|
|
- ticket.set('contactPhone', phone);
|
|
|
|
|
- if (type === 'cooperation') ticket.set('companyName', String(form.organization || '').trim().slice(0, 120));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (type === 'cooperation') ticket.set('companyName', String(form.organization || '').trim().slice(0, 120));
|
|
|
await ticket.save(null, { sessionToken: scope.sessionToken });
|
|
await ticket.save(null, { sessionToken: scope.sessionToken });
|
|
|
return ticketNo;
|
|
return ticketNo;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-module.exports = {
|
|
|
|
|
- TABS, REPORT_CATEGORIES, authContext, listTickets, listConversations, listReplies, submitTicket,
|
|
|
|
|
- mapRecords, replyChoices, normalizeContactPhone, pickerChoices, selectedPickerIndex, submitErrorMessage,
|
|
|
|
|
-};
|
|
|
|
|
|
|
+function submitErrorMessage(error) {
|
|
|
|
|
+ const messages = ['请填写有效联系电话', '请填写联系人姓名', '请填写问题描述'];
|
|
|
|
|
+ return messages.includes(error?.message) ? error.message : '提交结果待核对,请先刷新处理记录。';
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+module.exports = { TABS, authContext, listTickets, submitTicket, mapRecords, normalizeContactPhone, submitErrorMessage };
|