'use strict'; const assert = require('assert/strict'); const fs = require('fs'); const os = require('os'); const path = require('path'); const { AgentWorkbenchDb } = require('../mcp/src/core/agent-workbench-db'); const { __testing } = require('../mcp/src/dashboard/agent-service'); async function main() { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'qiwei-outbound-callback-')); const db = new AgentWorkbenchDb(path.join(dir, 'outbound.db'), { globalPaused: false, defaultMode: 'review', }); const emitted = []; const target = { config: { selfUserId: 'staff-1', allowedSenders: ['customer-1'] }, db, service: { emit: (...args) => emitted.push(args) }, qiwei: { async syncMessages(seq, limit) { assert.equal(seq, 19); assert.equal(limit, 10); return { syncMsgList: [{ seq: 21, msgType: 2, senderId: 'staff-1', receiverId: 'customer-1', msgData: { content: '手机端补采消息' }, msgUniqueIdentifier: 'outbound-synced-1', timestamp: 1_785_495_167, }], }; }, }, }; try { const direct = await __testing.ingestMessageForWorkbench(target, { seq: 10, msgType: 2, senderId: 'staff-1', receiverId: 'customer-1', msgData: { content: '电脑端直接消息' }, msgUniqueIdentifier: 'outbound-direct-1', timestamp: 1_785_495_100, }, 'relay_callback'); assert.equal(direct.status, 'outbound_recorded'); const synced = await __testing.ingestMessageForWorkbench(target, { seq: 20, msgType: 2001, senderId: 'staff-1', receiverId: 'customer-1', msgUniqueIdentifier: 'outbound-notification-1', timestamp: 1_785_495_162, msgData: {}, }, 'relay_callback'); assert.equal(synced.status, 'outbound_recorded'); const conversation = db.getConversationByContactId('customer-1'); const messages = db.listMessages(conversation.id, 20); assert.deepEqual(messages.map(item => [item.direction, item.sender_type, item.content]), [ ['outbound', 'human', '电脑端直接消息'], ['outbound', 'human', '手机端补采消息'], ]); assert.equal(emitted.length, 2); process.stdout.write('[ok] outbound callbacks are captured without triggering Agent\n'); } finally { db.close(); fs.rmSync(dir, { recursive: true, force: true }); } } main().catch(error => { console.error(error); process.exitCode = 1; });