const express = require('express'); const router = express.Router(); const { requireAuth } = require('../middleware/jwt-auth'); const { TaskService } = require('../services/task-service'); // 38 号文档 Phase 1:全站要求登录,公司归属过滤留到 Phase 2。 router.use(requireAuth); const taskService = new TaskService(); // ── Task List 缓存 (10秒 TTL) ── let _taskListCache = null; let _taskListCacheTime = 0; const TASK_LIST_CACHE_TTL = 10000; // GET /api/tasks — 扫描获取系统中聚合的 Task 列表(含父子关系) router.get('/', (req, res) => { try { if (_taskListCache && Date.now() - _taskListCacheTime < TASK_LIST_CACHE_TTL) { return res.json(_taskListCache); } const tasks = taskService.getAggregatedTasks(); const taskList = tasks.map(t => ({ taskId: t.taskId, title: t.title, createdAt: t.createdAt, updatedAt: t.updatedAt, status: t.status, from: t.from, to: t.to, parentTaskId: t.parentTaskId, children: t.children, isRoot: t.isRoot, agents: t.agents, // 批 5:会话数由 TaskService 直接给(原来这里自己取 t.conversations.length, // 而 conversations 是会话反推那套的产物,已随之删除) conversationCount: t.conversationCount, })); _taskListCache = taskList; _taskListCacheTime = Date.now(); res.json(taskList); } catch (err) { res.status(500).json({ error: err.message }); } }); // GET /api/tasks/:taskId/timeline — 获取跨 Agent 的所有 Timeline Log(含子任务) router.get('/:taskId/timeline', (req, res) => { try { const includeChildren = req.query.children !== 'false'; const timeline = taskService.getTaskTimeline(req.params.taskId, includeChildren); if (!timeline || timeline.length === 0) { return res.status(404).json({ error: '找不到该任务相关的聊天记录。' }); } res.json(timeline); } catch (err) { res.status(500).json({ error: err.message }); } }); module.exports = router;