const http = require('http'); const fs = require('fs'); const path = require('path'); const { URL } = require('url'); const { outputsRoot } = require('../core/output-paths'); const { qiweiSyncExternalGroups, qiweiListExternalGroups, qiweiAnalyzeGroupMembers, qiweiConfirmExternalGroup, qiweiAddExternalGroup, qiweiConfigureGroupKeywords, qiweiRejectExternalGroup, qiweiSyncGroupMessages } = require('../tools/qiwei-group-management-run'); const { qiweiLoginStatus, qiweiLoginStart, qiweiLoginCheck, qiweiLoginVerify } = require('../tools/qiwei-login-run'); const { qiweiSubscriptionStatus } = require('../tools/qiwei-subscription-run'); const { createAccountConnectionMonitor } = require('../core/account-connection-monitor'); const { qiweiBatchAddFriends, qiweiCheckFriendStatus, qiweiGetCustomerProfile, qiweiAutoCreateGroup, qiweiUpdateCustomerInfo } = require('../tools/qiwei-customer-ops-run'); const { qiweiUpdateCustomerPortrait, qiweiSaveCustomerPortrait, qiweiBatchUpdateCustomerPortrait, qiweiExportCustomerPortraits, qiweiListCustomerTags, qiweiListAllTags, qiweiAddCustomerTags, qiweiRemoveCustomerTags, qiweiSyncPersonalLabels, qiweiCreatePersonalLabel, qiweiUpdatePersonalLabel, qiweiDeletePersonalLabel, qiweiApplyPersonalLabels } = require('../tools/qiwei-portrait-tags-run'); const { qiweiPreviewTransferPackage, qiweiExecuteTransfer } = require('../tools/qiwei-customer-transfer-run'); const { getStateSection, rebuildStateFromOutputs } = require('../core/dashboard-state'); const { switchActiveAccount, getAgentStatus, getConversations, syncConversations, changeGlobalMode, changeConversationMode, approveReply, approveDraft, rejectDraft, regenerateDraft, generateLatestDraft, manualSend, updateCustomerTask, syncCustomerTaskToOfficialTodo, updateCustomerAlert, getAudit, startListener, stopListener } = require('./agent-service'); const { listKnowledgeTree, readKnowledgeFile, listProperties, getProperty, listSkillRegistry, getSkillDetail } = require('./workspace-library-service'); const { getMeetingKnowledgeHub, syncMeetingKnowledge, analyzeMeetingKnowledge, getMeetingKnowledge } = require('./meeting-knowledge-service'); const { getDocumentKnowledgeHub, importDocumentKnowledge, createDocumentKnowledge, analyzeDocumentKnowledge, refreshDocumentKnowledge, getDocumentKnowledge, getTodoKnowledgeHub, searchTodoUsers, syncTodoKnowledge, refreshTodoKnowledgeDetails, createTodoKnowledge, completeTodoKnowledge } = require('./official-office-knowledge-service'); const { getUnifiedTaskHub, createUnifiedLocalTask, updateUnifiedTask } = require('./unified-task-service'); const { getCustomerMasterHub, updateCustomerMaster, updateCustomerRecommendationFeedback } = require('./customer-master-service'); const DASHBOARD_PORT = process.env.QIWEI_DASHBOARD_PORT || 4320; const STATIC_DIR = path.join(__dirname); const TMP_DIR = path.join(outputsRoot(), 'tmp'); const jobs = new Map(); const accountConnectionMonitor = createAccountConnectionMonitor({ checkStatus: () => qiweiLoginStatus({}), recoverLogin: () => qiweiLoginCheck({ manual: true, persistConfig: false }) }); accountConnectionMonitor.start(); function ensureTmpDir() { fs.mkdirSync(TMP_DIR, { recursive: true }); } function readBody(req) { return new Promise((resolve, reject) => { const chunks = []; req.on('data', chunk => chunks.push(chunk)); req.on('end', () => { try { resolve(JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}')); } catch { resolve({}); } }); req.on('error', reject); }); } function json(res, status, body) { res.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8' }); res.end(JSON.stringify(body)); } function createJob(runFn) { const id = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`; const job = { id, status: 'running', progress: 0, result: null, error: null, startedAt: Date.now() }; jobs.set(id, job); runFn() .then(r => { job.status = 'done'; job.progress = 100; job.result = r; }) .catch(e => { job.status = 'error'; job.error = e.message || String(e); }); return id; } function getJob(id) { const job = jobs.get(id); if (!job) return null; if (job.status === 'running') { const elapsed = Date.now() - job.startedAt; job.progress = Math.min(95, Math.floor((elapsed / 30000) * 100)); } return job; } function serveStatic(req, res, filePath, contentType) { fs.readFile(filePath, (err, data) => { if (err) { res.writeHead(404); res.end('not found'); return; } res.writeHead(200, { 'Content-Type': contentType }); res.end(data); }); } async function handleUpload(req) { const body = await readBody(req); const base64 = String(body.data || ''); const name = String(body.name || `upload-${Date.now()}`).replace(/[\\/]/g, '_'); if (!base64) throw new Error('缺少文件数据'); ensureTmpDir(); const buffer = Buffer.from(base64, 'base64'); const filePath = path.join(TMP_DIR, name); fs.writeFileSync(filePath, buffer); return { path: filePath }; } function handleOutputsDownload(req, res, query) { const requested = String(query.path || ''); if (!requested) { json(res, 400, { status: 'error', message: '缺少 path 参数' }); return; } const resolved = path.resolve(requested); const root = path.resolve(outputsRoot()); if (!resolved.startsWith(root)) { json(res, 403, { status: 'error', message: '禁止访问 outputs 目录之外的文件' }); return; } if (!fs.existsSync(resolved)) { json(res, 404, { status: 'error', message: '文件不存在' }); return; } const data = fs.readFileSync(resolved); const ext = path.extname(resolved).toLowerCase(); const contentType = { '.json': 'application/json', '.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', '.xls': 'application/vnd.ms-excel', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.mp3': 'audio/mpeg', '.mp4': 'video/mp4' }[ext] || 'application/octet-stream'; res.writeHead(200, { 'Content-Type': contentType, 'Content-Disposition': `attachment; filename="${path.basename(resolved)}"` }); res.end(data); } function getContentType(filePath) { const ext = path.extname(filePath).toLowerCase(); const map = { '.html': 'text/html; charset=utf-8', '.css': 'text/css; charset=utf-8', '.js': 'application/javascript; charset=utf-8', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.svg': 'image/svg+xml' }; return map[ext] || 'application/octet-stream'; } async function combinedStatus() { const [login, subscription] = await Promise.all([ accountConnectionMonitor.getStatus(), qiweiSubscriptionStatus({}) ]); return { status: 'ok', summary: { authConfigured: login.status !== 'needs_auth', online: !!login.summary?.online, subscribed: !!subscription.summary?.subscribed }, data: { login, subscription } }; } async function handleRequest(req, res) { const url = new URL(req.url, 'http://localhost'); const pathname = url.pathname; try { if (pathname === '/' || pathname === '/index.html') { serveStatic(req, res, path.join(STATIC_DIR, 'index.html'), 'text/html; charset=utf-8'); return; } if (pathname.startsWith('/dashboard/')) { const fileName = pathname.slice('/dashboard/'.length).replace(/\.{2,}/g, ''); const filePath = path.join(STATIC_DIR, fileName); if (!filePath.startsWith(STATIC_DIR)) { json(res, 403, { status: 'error', message: '禁止访问' }); return; } serveStatic(req, res, filePath, getContentType(filePath)); return; } if (pathname === '/api/health') { json(res, 200, { status: 'ok' }); return; } if (pathname === '/api/status' && req.method === 'GET') { json(res, 200, await combinedStatus()); return; } if (pathname === '/api/skills' && req.method === 'GET') { json(res, 200, listSkillRegistry()); return; } if (pathname === '/api/knowledge/tree' && req.method === 'GET') { json(res, 200, listKnowledgeTree()); return; } if (pathname === '/api/knowledge/file' && req.method === 'GET') { json(res, 200, readKnowledgeFile(url.searchParams.get('id'))); return; } if (pathname === '/api/knowledge/meetings' && req.method === 'GET') { json(res, 200, await getMeetingKnowledgeHub()); return; } if (pathname === '/api/knowledge/meetings/sync' && req.method === 'POST') { json(res, 200, await syncMeetingKnowledge(await readBody(req))); return; } const meetingAnalyzeRoute = pathname.match(/^\/api\/knowledge\/meetings\/([^/]+)\/analyze$/); if (meetingAnalyzeRoute && req.method === 'POST') { json(res, 200, await analyzeMeetingKnowledge(decodeURIComponent(meetingAnalyzeRoute[1]))); return; } const meetingDetailRoute = pathname.match(/^\/api\/knowledge\/meetings\/([^/]+)$/); if (meetingDetailRoute && req.method === 'GET') { json(res, 200, getMeetingKnowledge(decodeURIComponent(meetingDetailRoute[1]))); return; } if (pathname === '/api/knowledge/docs' && req.method === 'GET') { json(res, 200, await getDocumentKnowledgeHub()); return; } if (pathname === '/api/knowledge/docs/import' && req.method === 'POST') { json(res, 200, await importDocumentKnowledge(await readBody(req))); return; } if (pathname === '/api/knowledge/docs/create' && req.method === 'POST') { json(res, 200, await createDocumentKnowledge(await readBody(req))); return; } const documentAnalyzeRoute = pathname.match(/^\/api\/knowledge\/docs\/([^/]+)\/analyze$/); if (documentAnalyzeRoute && req.method === 'POST') { json(res, 200, await analyzeDocumentKnowledge(decodeURIComponent(documentAnalyzeRoute[1]))); return; } const documentRefreshRoute = pathname.match(/^\/api\/knowledge\/docs\/([^/]+)\/refresh$/); if (documentRefreshRoute && req.method === 'POST') { json(res, 200, await refreshDocumentKnowledge(decodeURIComponent(documentRefreshRoute[1]))); return; } const documentDetailRoute = pathname.match(/^\/api\/knowledge\/docs\/([^/]+)$/); if (documentDetailRoute && req.method === 'GET') { json(res, 200, getDocumentKnowledge(decodeURIComponent(documentDetailRoute[1]))); return; } if (pathname === '/api/knowledge/todos' && req.method === 'GET') { json(res, 200, await getTodoKnowledgeHub()); return; } if (pathname === '/api/knowledge/tasks' && req.method === 'GET') { json(res, 200, getUnifiedTaskHub()); return; } if (pathname === '/api/knowledge/tasks/create' && req.method === 'POST') { json(res, 200, createUnifiedLocalTask(await readBody(req))); return; } if (pathname === '/api/knowledge/tasks/update' && req.method === 'POST') { json(res, 200, await updateUnifiedTask(await readBody(req))); return; } if (pathname === '/api/knowledge/todos/search-users' && req.method === 'POST') { json(res, 200, await searchTodoUsers(await readBody(req))); return; } if (pathname === '/api/knowledge/todos/sync' && req.method === 'POST') { json(res, 200, await syncTodoKnowledge(await readBody(req))); return; } if (pathname === '/api/knowledge/todos/details' && req.method === 'POST') { json(res, 200, await refreshTodoKnowledgeDetails(await readBody(req))); return; } if (pathname === '/api/knowledge/todos/create' && req.method === 'POST') { json(res, 200, await createTodoKnowledge(await readBody(req))); return; } const todoCompleteRoute = pathname.match(/^\/api\/knowledge\/todos\/([^/]+)\/complete$/); if (todoCompleteRoute && req.method === 'POST') { json(res, 200, await completeTodoKnowledge(decodeURIComponent(todoCompleteRoute[1]))); return; } if (pathname === '/api/knowledge/properties' && req.method === 'GET') { json(res, 200, listProperties(Object.fromEntries(url.searchParams.entries()))); return; } const propertyRoute = pathname.match(/^\/api\/knowledge\/properties\/([^/]+)$/); if (propertyRoute && req.method === 'GET') { json(res, 200, getProperty(decodeURIComponent(propertyRoute[1]))); return; } const skillRoute = pathname.match(/^\/api\/skills\/(.+)$/); if (skillRoute && req.method === 'GET') { json(res, 200, getSkillDetail(decodeURIComponent(skillRoute[1]))); return; } if (pathname === '/api/agent/status' && req.method === 'GET') { json(res, 200, await getAgentStatus()); return; } if (pathname === '/api/accounts/switch' && req.method === 'POST') { json(res, 200, await switchActiveAccount(await readBody(req))); return; } if (pathname === '/api/agent/conversations' && req.method === 'GET') { json(res, 200, getConversations()); return; } if (pathname === '/api/agent/conversations/sync' && req.method === 'POST') { json(res, 200, await syncConversations()); return; } if (pathname === '/api/agent/audit' && req.method === 'GET') { json(res, 200, getAudit(url.searchParams.get('limit'))); return; } if (pathname === '/api/agent/mode' && req.method === 'POST') { const body = await readBody(req); json(res, 200, changeGlobalMode(body.mode)); return; } if (pathname === '/api/agent/listener/start' && req.method === 'POST') { json(res, 200, await startListener()); return; } if (pathname === '/api/agent/listener/stop' && req.method === 'POST') { json(res, 200, stopListener()); return; } const customerTaskSyncRoute = pathname.match(/^\/api\/agent\/tasks\/([^/]+)\/sync-official$/); if (customerTaskSyncRoute && req.method === 'POST') { const body = await readBody(req); json(res, 200, await syncCustomerTaskToOfficialTodo(customerTaskSyncRoute[1], body)); return; } const customerTaskRoute = pathname.match(/^\/api\/agent\/tasks\/([^/]+)$/); if (customerTaskRoute && req.method === 'POST') { const body = await readBody(req); json(res, 200, await updateCustomerTask(customerTaskRoute[1], body)); return; } const customerAlertRoute = pathname.match(/^\/api\/agent\/alerts\/([^/]+)$/); if (customerAlertRoute && req.method === 'POST') { const body = await readBody(req); json(res, 200, updateCustomerAlert(customerAlertRoute[1], body)); return; } const agentConversationRoute = pathname.match(/^\/api\/agent\/conversations\/([^/]+)\/(takeover|resume|pause|auto|approve-reply|generate|manual-send)$/); if (agentConversationRoute && req.method === 'POST') { const [, conversationId, action] = agentConversationRoute; const body = await readBody(req); if (action === 'takeover') json(res, 200, changeConversationMode(conversationId, 'human')); else if (action === 'resume') json(res, 200, changeConversationMode(conversationId, 'review')); else if (action === 'pause') json(res, 200, changeConversationMode(conversationId, 'paused')); else if (action === 'auto') json(res, 200, changeConversationMode(conversationId, 'auto')); else if (action === 'generate') json(res, 200, await generateLatestDraft(conversationId)); else if (action === 'manual-send') json(res, 200, await manualSend(conversationId, body.content)); else json(res, 200, await approveReply(conversationId, body.content)); return; } const agentDraftRoute = pathname.match(/^\/api\/agent\/drafts\/([^/]+)\/(approve|reject|regenerate)$/); if (agentDraftRoute && req.method === 'POST') { const [, draftId, action] = agentDraftRoute; const body = await readBody(req); if (action === 'approve') json(res, 200, await approveDraft(draftId, body.content)); else if (action === 'reject') json(res, 200, rejectDraft(draftId, body.reason)); else json(res, 200, await regenerateDraft(draftId)); return; } if (pathname === '/api/dashboard/summary' && req.method === 'GET') { json(res, 200, { status: 'ok', data: getStateSection('summary') }); return; } if (pathname === '/api/dashboard/customers' && req.method === 'GET') { const filter = { keyword: url.searchParams.get('keyword') || '', hasPortrait: url.searchParams.has('hasPortrait') ? url.searchParams.get('hasPortrait') === 'true' : undefined, friendRequestStatus: url.searchParams.get('friendRequestStatus') || '', tag: url.searchParams.get('tag') || '' }; json(res, 200, { status: 'ok', data: getStateSection('customers', { filter }) }); return; } if (pathname === '/api/dashboard/portraits' && req.method === 'GET') { json(res, 200, { status: 'ok', data: getStateSection('portraits') }); return; } if (pathname === '/api/dashboard/tags' && req.method === 'GET') { json(res, 200, { status: 'ok', data: getStateSection('tags') }); return; } if (pathname === '/api/dashboard/transfers' && req.method === 'GET') { json(res, 200, { status: 'ok', data: getStateSection('transfers', { limit: url.searchParams.get('limit') }) }); return; } if (pathname === '/api/dashboard/groups' && req.method === 'GET') { json(res, 200, { status: 'ok', data: getStateSection('groups') }); return; } if (pathname === '/api/dashboard/operations' && req.method === 'GET') { json(res, 200, { status: 'ok', data: getStateSection('operations', { limit: url.searchParams.get('limit') }) }); return; } if (pathname === '/api/dashboard/rebuild' && req.method === 'POST') { const state = rebuildStateFromOutputs(); json(res, 200, { status: 'ok', data: { message: '索引已重建', summary: getStateSection('summary'), updatedAt: state.updatedAt } }); return; } if (pathname === '/api/login/start' && req.method === 'POST') { const body = await readBody(req); const result = await qiweiLoginStart({ flowUi: false, openBrowser: false, ...body }); json(res, 200, result); return; } if (pathname === '/api/login/check' && req.method === 'POST') { const body = await readBody(req); const result = await qiweiLoginCheck(body); json(res, 200, result); return; } if (pathname === '/api/login/verify' && req.method === 'POST') { const body = await readBody(req); const result = await qiweiLoginVerify(body); json(res, 200, result); return; } if (pathname === '/api/groups/sync' && req.method === 'POST') { const body = await readBody(req); const id = createJob(() => qiweiSyncExternalGroups(body)); json(res, 200, { status: 'ok', data: { jobId: id } }); return; } if (pathname === '/api/groups/list' && req.method === 'GET') { const params = {}; if (url.searchParams.has('keyword')) params.keyword = url.searchParams.get('keyword'); if (url.searchParams.has('status')) params.status = url.searchParams.get('status'); if (url.searchParams.has('source')) params.source = url.searchParams.get('source'); if (url.searchParams.has('includeRejected')) params.includeRejected = url.searchParams.get('includeRejected') === 'true'; json(res, 200, await qiweiListExternalGroups(params)); return; } if (pathname === '/api/groups/analyze' && req.method === 'POST') { const body = await readBody(req); const id = createJob(() => qiweiAnalyzeGroupMembers(body)); json(res, 200, { status: 'ok', data: { jobId: id } }); return; } if (pathname === '/api/groups/confirm' && req.method === 'POST') { const body = await readBody(req); json(res, 200, await qiweiConfirmExternalGroup(body)); return; } if (pathname === '/api/groups/add' && req.method === 'POST') { const body = await readBody(req); json(res, 200, await qiweiAddExternalGroup(body)); return; } if (pathname === '/api/groups/reject' && req.method === 'POST') { const body = await readBody(req); json(res, 200, await qiweiRejectExternalGroup(body)); return; } if (pathname === '/api/groups/keywords' && req.method === 'POST') { const body = await readBody(req); json(res, 200, await qiweiConfigureGroupKeywords(body)); return; } if (pathname === '/api/groups/messages/sync' && req.method === 'POST') { const body = await readBody(req); const id = createJob(() => qiweiSyncGroupMessages(body)); json(res, 200, { status: 'ok', data: { jobId: id } }); return; } // Customer Operations if (pathname === '/api/customers' && req.method === 'GET') { json(res, 200, getCustomerMasterHub()); return; } const customerMasterRoute = pathname.match(/^\/api\/customers\/([^/]+)\/profile$/); if (customerMasterRoute && req.method === 'POST') { json(res, 200, updateCustomerMaster(decodeURIComponent(customerMasterRoute[1]), await readBody(req))); return; } const customerRecommendationRoute = pathname.match(/^\/api\/customers\/([^/]+)\/recommendations\/([^/]+)$/); if (customerRecommendationRoute && req.method === 'POST') { json(res, 200, updateCustomerRecommendationFeedback(decodeURIComponent(customerRecommendationRoute[1]), decodeURIComponent(customerRecommendationRoute[2]), await readBody(req))); return; } if (pathname === '/api/customer-ops/batch-add-friends' && req.method === 'POST') { const body = await readBody(req); const id = createJob(() => qiweiBatchAddFriends(body)); json(res, 200, { status: 'ok', data: { jobId: id } }); return; } if (pathname === '/api/customer-ops/check-friend-status' && req.method === 'POST') { const body = await readBody(req); const id = createJob(() => qiweiCheckFriendStatus(body)); json(res, 200, { status: 'ok', data: { jobId: id } }); return; } if (pathname === '/api/customer-ops/customer-profile' && req.method === 'POST') { const body = await readBody(req); json(res, 200, await qiweiGetCustomerProfile(body)); return; } if (pathname === '/api/customer-ops/update' && req.method === 'POST') { json(res, 200, await qiweiUpdateCustomerInfo(await readBody(req))); return; } if (pathname === '/api/customer-ops/auto-create-group' && req.method === 'POST') { const body = await readBody(req); const id = createJob(() => qiweiAutoCreateGroup(body)); json(res, 200, { status: 'ok', data: { jobId: id } }); return; } // Portraits & Tags if (pathname === '/api/portraits/update' && req.method === 'POST') { const body = await readBody(req); const id = createJob(() => qiweiUpdateCustomerPortrait(body)); json(res, 200, { status: 'ok', data: { jobId: id } }); return; } if (pathname === '/api/portraits/save' && req.method === 'POST') { const body = await readBody(req); json(res, 200, await qiweiSaveCustomerPortrait(body)); return; } if (pathname === '/api/portraits/batch' && req.method === 'POST') { const body = await readBody(req); const id = createJob(() => qiweiBatchUpdateCustomerPortrait(body)); json(res, 200, { status: 'ok', data: { jobId: id } }); return; } if (pathname === '/api/portraits/export' && req.method === 'POST') { const body = await readBody(req); const id = createJob(() => qiweiExportCustomerPortraits(body)); json(res, 200, { status: 'ok', data: { jobId: id } }); return; } if (pathname === '/api/tags/list' && req.method === 'POST') { const body = await readBody(req); json(res, 200, await qiweiListCustomerTags(body)); return; } if (pathname === '/api/tags/all' && req.method === 'POST') { const body = await readBody(req); json(res, 200, await qiweiListAllTags(body)); return; } if (pathname === '/api/tags/add' && req.method === 'POST') { const body = await readBody(req); json(res, 200, await qiweiAddCustomerTags(body)); return; } if (pathname === '/api/tags/remove' && req.method === 'POST') { const body = await readBody(req); json(res, 200, await qiweiRemoveCustomerTags(body)); return; } if (pathname === '/api/personal-labels/sync' && req.method === 'POST') { const body = await readBody(req); json(res, 200, await qiweiSyncPersonalLabels(body)); return; } if (pathname === '/api/personal-labels/create' && req.method === 'POST') { const body = await readBody(req); json(res, 200, await qiweiCreatePersonalLabel(body)); return; } if (pathname === '/api/personal-labels/update' && req.method === 'POST') { const body = await readBody(req); json(res, 200, await qiweiUpdatePersonalLabel(body)); return; } if (pathname === '/api/personal-labels/delete' && req.method === 'POST') { const body = await readBody(req); json(res, 200, await qiweiDeletePersonalLabel(body)); return; } if (pathname === '/api/personal-labels/apply' && req.method === 'POST') { const body = await readBody(req); json(res, 200, await qiweiApplyPersonalLabels(body)); return; } // Customer Transfers if (pathname === '/api/transfers/preview' && req.method === 'POST') { const body = await readBody(req); json(res, 200, await qiweiPreviewTransferPackage(body)); return; } if (pathname === '/api/transfers/execute' && req.method === 'POST') { const body = await readBody(req); const id = createJob(() => qiweiExecuteTransfer(body)); json(res, 200, { status: 'ok', data: { jobId: id } }); return; } if (pathname === '/api/upload' && req.method === 'POST') { const result = await handleUpload(req); json(res, 200, { status: 'ok', data: result }); return; } if (pathname === '/api/outputs' && req.method === 'GET') { handleOutputsDownload(req, res, url.searchParams); return; } if (pathname.startsWith('/api/jobs/') && req.method === 'GET') { const id = pathname.slice('/api/jobs/'.length); const job = getJob(id); if (!job) { json(res, 404, { status: 'error', message: '任务不存在' }); return; } json(res, 200, { status: 'ok', data: job }); return; } json(res, 404, { status: 'error', message: 'not found' }); } catch (error) { json(res, 500, { status: 'error', message: error.message || String(error) }); } } function startServer(port = DASHBOARD_PORT) { const server = http.createServer(handleRequest); return new Promise((resolve, reject) => { server.once('error', reject); server.listen(port, '127.0.0.1', () => { console.log(`Qiwei Dashboard 已启动:http://127.0.0.1:${port}/`); resolve({ server, url: `http://127.0.0.1:${port}/`, port }); }); }); } if (require.main === module) { startServer().catch(error => { console.error('Dashboard 启动失败:', error); process.exit(1); }); } module.exports = { startServer };