tasks.js
1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const express = require('express');
const router = express.Router();
const { TaskService } = require('../services/task-service');
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,
conversationCount: t.conversations.length,
}));
_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;