|
@@ -0,0 +1,166 @@
|
|
|
|
|
+const { restrictionMessageForUser } = require('./login-restrictions');
|
|
|
|
|
+
|
|
|
|
|
+const TABS = [
|
|
|
|
|
+ { key: 'cooperation', label: '机构合作', prefix: 'CO', initial: '待处理' },
|
|
|
|
|
+ { key: 'feedback', label: '问题反馈', prefix: 'FB', initial: '待处理' },
|
|
|
|
|
+ { key: 'ai_reply_report', label: 'AI 回复举报', prefix: 'AR', initial: '已受理' },
|
|
|
|
|
+];
|
|
|
|
|
+
|
|
|
|
|
+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 normalizeContactPhone(value) {
|
|
|
|
|
+ const phone = String(value || '').trim().replace(/[ ()-]/g, '');
|
|
|
|
|
+ return /^\+?[0-9]{7,15}$/.test(phone) ? phone : '';
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function referenceId(value, className) {
|
|
|
|
|
+ if (typeof value === 'string') return value.startsWith(className + '$') ? value.slice(className.length + 1) : value;
|
|
|
|
|
+ return String(value?.id || value?.objectId || '');
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function authContext(app) {
|
|
|
|
|
+ const parse = app.Parse;
|
|
|
|
|
+ const user = parse?.User?.current();
|
|
|
|
|
+ const sessionToken = user?.getSessionToken?.();
|
|
|
|
|
+ const companyId = referenceId(user?.get('company'), 'Company');
|
|
|
|
|
+ if (!sessionToken || !user?.id || !companyId || companyId !== app.globalData.company || restrictionMessageForUser(user)) return null;
|
|
|
|
|
+ return { parse, user, sessionToken, companyId };
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function pointer(parse, className, objectId) {
|
|
|
|
|
+ const obj = new parse.Object(className);
|
|
|
|
|
+ obj.id = objectId;
|
|
|
|
|
+ return obj;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function ownedQuery(scope, table, owner) {
|
|
|
|
|
+ const query = new scope.parse.Query(table);
|
|
|
|
|
+ query.equalTo('company', pointer(scope.parse, 'Company', scope.companyId));
|
|
|
|
|
+ query.equalTo(owner, pointer(scope.parse, '_User', scope.user.id));
|
|
|
|
|
+ query.notEqualTo('isDeleted', true);
|
|
|
|
|
+ return query;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function displayTime(value) {
|
|
|
|
|
+ const time = value instanceof Date ? value : new Date(value);
|
|
|
|
|
+ if (!Number.isFinite(time.getTime())) return '';
|
|
|
|
|
+ const two = n => String(n).padStart(2, '0');
|
|
|
|
|
+ return `${time.getFullYear()}-${two(time.getMonth() + 1)}-${two(time.getDate())} ${two(time.getHours())}:${two(time.getMinutes())}`;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function mapRecords(records, type) {
|
|
|
|
|
+ return records.filter(row => row.get('isDeleted') !== true).map(row => ({
|
|
|
|
|
+ id: row.id,
|
|
|
|
|
+ ticketNo: String(row.get('ticketNo') || ''),
|
|
|
|
|
+ status: String((type === 'ai_reply_report' && row.get('resolutionStatus')) || row.get('progress') || ''),
|
|
|
|
|
+ category: type === 'ai_reply_report' ? String(row.get('reportCategoryLabel') || '') : '',
|
|
|
|
|
+ snippet: String(row.get('content') || '').slice(0, 100),
|
|
|
|
|
+ result: String(row.get('resolutionSummary') || ''),
|
|
|
|
|
+ submittedAt: displayTime(row.createdAt),
|
|
|
|
|
+ }));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+async function listTickets(scope, type) {
|
|
|
|
|
+ const query = ownedQuery(scope, 'ServiceTicket', 'submitter');
|
|
|
|
|
+ query.equalTo('ticketType', type);
|
|
|
|
|
+ query.select('ticketNo', 'content', 'progress', 'resolutionStatus', 'resolutionSummary', 'reportCategoryLabel');
|
|
|
|
|
+ query.descending('createdAt');
|
|
|
|
|
+ query.limit(40);
|
|
|
|
|
+ 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) {
|
|
|
|
|
+ const tab = TABS.find(item => item.key === type);
|
|
|
|
|
+ if (!tab) throw Error('工单类型无效');
|
|
|
|
|
+ const isAI = type === 'ai_reply_report';
|
|
|
|
|
+ const name = String(form.name || '').trim();
|
|
|
|
|
+ const content = String(form.content || '').trim().slice(0, isAI ? 500 : 2000);
|
|
|
|
|
+ const phone = normalizeContactPhone(form.phone);
|
|
|
|
|
+ const category = REPORT_CATEGORIES.find(item => item.value === form.category);
|
|
|
|
|
+ if (isAI && (!category || !form.sessionId || !form.reply?.text)) throw Error('请选择 AI 回复和举报类型');
|
|
|
|
|
+ if (!isAI && !phone) throw Error('请填写有效联系电话');
|
|
|
|
|
+ if (type === 'cooperation' && !name) throw Error('请填写联系人姓名');
|
|
|
|
|
+ if (type === 'feedback' && !content) throw Error('请填写问题描述');
|
|
|
|
|
+
|
|
|
|
|
+ const ticketNo = tab.prefix + '-' + Date.now().toString(36).toUpperCase() + '-' + Math.random().toString(36).slice(2, 8).toUpperCase();
|
|
|
|
|
+ const ticket = new scope.parse.Object('ServiceTicket');
|
|
|
|
|
+ ticket.set({
|
|
|
|
|
+ company: pointer(scope.parse, 'Company', scope.companyId),
|
|
|
|
|
+ submitter: pointer(scope.parse, '_User', scope.user.id),
|
|
|
|
|
+ submitterName: (type === 'cooperation' ? name : (scope.user.get('nickname') || '微信用户')).slice(0, 80),
|
|
|
|
|
+ ticketNo, ticketType: type, source: tab.label, progress: tab.initial, isDeleted: false,
|
|
|
|
|
+ content: content || (isAI ? category.label : ''),
|
|
|
|
|
+ });
|
|
|
|
|
+ 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));
|
|
|
|
|
+ }
|
|
|
|
|
+ await ticket.save(null, { sessionToken: scope.sessionToken });
|
|
|
|
|
+ return ticketNo;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+module.exports = {
|
|
|
|
|
+ TABS, REPORT_CATEGORIES, authContext, listTickets, listConversations, listReplies, submitTicket,
|
|
|
|
|
+ mapRecords, replyChoices, normalizeContactPhone,
|
|
|
|
|
+};
|