fde-console 0.0.0-stage → 2.9.68
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +128 -2
- package/README.zh-CN.md +130 -0
- package/assets/icon.svg +9 -0
- package/cordis.patch.yml +7 -0
- package/lib/agentActivity.js +1180 -0
- package/lib/agentContext.js +146 -0
- package/lib/agentTranscript.js +1398 -0
- package/lib/analyze.js +625 -0
- package/lib/attribute.js +205 -0
- package/lib/backup.js +366 -0
- package/lib/desktopAgents.js +123 -0
- package/lib/diary.js +333 -0
- package/lib/dingtalk.js +412 -0
- package/lib/feishu.js +580 -0
- package/lib/giteaClient.js +810 -0
- package/lib/giteeClient.js +372 -0
- package/lib/githubClient.js +361 -0
- package/lib/heatmap.js +99 -0
- package/lib/imNotify.js +378 -0
- package/lib/inbox.js +438 -0
- package/lib/instanceLock.js +122 -0
- package/lib/jobs.js +56 -0
- package/lib/learn.js +1388 -0
- package/lib/learnImport.js +283 -0
- package/lib/learnSeeds.js +97 -0
- package/lib/learnSeedsAiEng.js +77 -0
- package/lib/mcpCore.js +135 -0
- package/lib/members.js +160 -0
- package/lib/migration.js +403 -0
- package/lib/overview.js +376 -0
- package/lib/postmortem.js +148 -0
- package/lib/radarIgnore.js +134 -0
- package/lib/redact.js +76 -0
- package/lib/router.js +246 -0
- package/lib/routes-gitea.js +262 -0
- package/lib/routes-setup.js +40 -0
- package/lib/scorecard.js +111 -0
- package/lib/sessionRef.js +227 -0
- package/lib/sessionSnapshot.js +67 -0
- package/lib/sessionSuggest.js +186 -0
- package/lib/settings.js +783 -0
- package/lib/setupCheck.js +330 -0
- package/lib/ssrfGuard.js +145 -0
- package/lib/store.js +564 -0
- package/lib/storeDb.js +418 -0
- package/lib/taskEvents.js +243 -0
- package/lib/webhookOut.js +125 -0
- package/lib/weekly.js +439 -0
- package/lib/workSearch.js +220 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +46 -4
- package/plugin/adapter.js +157 -0
- package/plugin/client.js +282 -0
- package/plugin/index.mjs +116 -0
- package/plugin/static.js +99 -0
- package/scripts/backup-data.js +41 -0
- package/scripts/mcp-server.mjs +68 -0
- package/scripts/migrate-data.js +301 -0
- package/scripts/migrate-storage.js +47 -0
- package/scripts/seed-demo.js +200 -0
- package/scripts/write-agent-context.js +43 -0
- package/test/helpers/appAssembler.js +1078 -0
- package/web/client/fde-client.js +292 -0
- package/web/css/app.css +1510 -0
- package/web/index.html +69 -0
- package/web/js/api.js +72 -0
- package/web/js/app.js +313 -0
- package/web/js/icons.js +48 -0
- package/web/js/mock.js +1150 -0
- package/web/js/state.js +7 -0
- package/web/js/ui.js +276 -0
- package/web/js/views/activity.js +410 -0
- package/web/js/views/agents.js +892 -0
- package/web/js/views/board.js +1656 -0
- package/web/js/views/learn.js +530 -0
- package/web/js/views/members.js +344 -0
- package/web/js/views/onboarding.js +103 -0
- package/web/js/views/overview.js +508 -0
- package/web/js/views/radar.js +1359 -0
- package/web/js/views/search.js +149 -0
- package/web/js/views/settings.js +1351 -0
- package/web/js/views/topology.js +471 -0
- package/web/mock.json +2363 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* desktopAgents.js — 桌面 agent 注册表 → 硅基成员派生(v2.6)
|
|
5
|
+
*
|
|
6
|
+
* 业主 2026-10-20:硅基成员从「单一 WorkBuddy」泛化为桌面 agent 注册表四源并列——
|
|
7
|
+
* workbuddy(~/.workbuddy/workbuddy.db)/ zcode(~/.zcode/cli)/ codex(~/.codex/sessions,
|
|
8
|
+
* ChatGPT 桌面)/ opencode(~/.local/share/opencode/opencode.db)。
|
|
9
|
+
* 四个适配器全部复用 lib/agentActivity.js 的只读实现(workbuddy/zcode 沿用 v2.2 活动面板
|
|
10
|
+
* 同款适配器——zcode 自 v2.9.14 改读 ~/.zcode/cli/db/db.sqlite session 表,客户端新版
|
|
11
|
+
* 会话落盘 session_<hex32> 家族不再走 sess_* 目录;codex/opencode 为 v2.6 桌面成员册专用适配器)。
|
|
12
|
+
* 开关与路径:config.agentActivity.sources 各源已有的 enabled/root/dbPath 直接生效(不新增配置节)。
|
|
13
|
+
* 纪律:每源独立 try/catch(失败 → 该源缺席,不拖垮其余源与人类名册);各源只读。
|
|
14
|
+
* hermes:~/.hermes 仅 skills/ 无会话库,本期不做(待考古)。
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const {
|
|
18
|
+
normalizeAgentActivity,
|
|
19
|
+
workbuddyAdapter,
|
|
20
|
+
zcodeAdapter,
|
|
21
|
+
codexAdapter,
|
|
22
|
+
opencodeAdapter,
|
|
23
|
+
hermesAdapter,
|
|
24
|
+
} = require('./agentActivity');
|
|
25
|
+
const { redact } = require('./redact');
|
|
26
|
+
|
|
27
|
+
/** 注册顺序即成员册输出顺序(workbuddy 保留 v2.5 既有路径与语义;hermes 2026-11-03 第七源入册) */
|
|
28
|
+
const DESKTOP_SOURCES = [
|
|
29
|
+
{ id: 'workbuddy', name: 'WorkBuddy', adapter: workbuddyAdapter },
|
|
30
|
+
{ id: 'zcode', name: 'ZCode', adapter: zcodeAdapter },
|
|
31
|
+
{ id: 'codex', name: 'Codex (ChatGPT 桌面)', adapter: codexAdapter },
|
|
32
|
+
{ id: 'opencode', name: 'OpenCode', adapter: opencodeAdapter },
|
|
33
|
+
{ id: 'hermes', name: 'Hermes', adapter: hermesAdapter },
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* collectDesktopMembers(agentActivityCfg, deps)
|
|
38
|
+
* agentActivityCfg:ctx.config.agentActivity(可为 null/缺节,内部回默认);
|
|
39
|
+
* deps:{ now, tasks, deriveTaskView, adapters }(deriveTaskView 由 overview.computeMembers 闭包注入,
|
|
40
|
+
* 桌面成员与人类成员共用同一套 当前任务/status/weekDone 推导;adapters 为源适配器覆盖表
|
|
41
|
+
* { id: fn },测试注入缝——AGENTS.md 坑 5 sqlite 夹具拦截的绕开路径,prod 恒缺省)。
|
|
42
|
+
* → 成员条目数组(字段形状与 v2.5 workbuddy 派生条目一致,另加 source 字段标注桌面来源)。
|
|
43
|
+
* 「当前任务/最近工作」归因:任务 agentId/memberId 精确等于源 id('zcode'/'codex' 等)即命中——
|
|
44
|
+
* 沿用 v2.5 workbuddy 的归因写法推广到四源。
|
|
45
|
+
*/
|
|
46
|
+
function collectDesktopMembers(agentActivityCfg, deps) {
|
|
47
|
+
const d = deps || {};
|
|
48
|
+
const now = d.now || Date.now();
|
|
49
|
+
const tasks = Array.isArray(d.tasks) ? d.tasks : [];
|
|
50
|
+
const deriveTaskView = typeof d.deriveTaskView === 'function' ? d.deriveTaskView : null;
|
|
51
|
+
const sec = normalizeAgentActivity(
|
|
52
|
+
agentActivityCfg !== null && typeof agentActivityCfg === 'object' ? agentActivityCfg : null
|
|
53
|
+
);
|
|
54
|
+
if (sec.enabled === false || deriveTaskView === null) return [];
|
|
55
|
+
|
|
56
|
+
const entries = [];
|
|
57
|
+
const overrides = d.adapters && typeof d.adapters === 'object' ? d.adapters : {};
|
|
58
|
+
for (const src of DESKTOP_SOURCES) {
|
|
59
|
+
const s = sec.sources[src.id] || {};
|
|
60
|
+
if (s.enabled === false) continue;
|
|
61
|
+
try {
|
|
62
|
+
const adapterFn = overrides[src.id] || src.adapter;
|
|
63
|
+
const r = adapterFn(s, { limit: sec.perSourceLimit, previewChars: sec.previewChars, now });
|
|
64
|
+
const latest = (Array.isArray(r.items) && r.items[0]) || null; // 各源取最近会话代表该 agent
|
|
65
|
+
if (!latest) continue;
|
|
66
|
+
const alive = latest.state === 'active';
|
|
67
|
+
const lastAct = Date.parse(latest.lastActiveAt) || null;
|
|
68
|
+
const myTasks = tasks.filter((t) => t.agentId === src.id || t.memberId === src.id);
|
|
69
|
+
const derived = deriveTaskView(myTasks, lastAct, { alive });
|
|
70
|
+
entries.push({
|
|
71
|
+
id: src.id,
|
|
72
|
+
name: src.name,
|
|
73
|
+
kind: 'agent',
|
|
74
|
+
manual: false,
|
|
75
|
+
role: '桌面 Agent',
|
|
76
|
+
status: derived.status,
|
|
77
|
+
currentTask: derived.currentTask,
|
|
78
|
+
lastActivityAt: derived.lastActivityAt,
|
|
79
|
+
todayCommits: 0, // 桌面 agent 无 Gitea 身份,提交/PR 统计恒 0(活动观测在「智能体动态」面板)
|
|
80
|
+
openPrs: 0,
|
|
81
|
+
weekDone: derived.weekDone,
|
|
82
|
+
session: { id: latest.id || src.id, alive },
|
|
83
|
+
source: src.id, // v2.6:标注桌面来源
|
|
84
|
+
});
|
|
85
|
+
} catch (e) {
|
|
86
|
+
// 单源故障隔离:该源跳过不报错,不影响其余源与人类名册;但必须留痕(红线 8,禁空 catch)
|
|
87
|
+
console.error(redact(`[desktopAgents] 源 ${src.id} 成员派生失败(该源成员缺席): ${(e && e.message) || e}`));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return entries;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* collectDesktopSourceHealth(agentActivityCfg, deps) — 桌面源健康探针(GET /api/members/source-health)。
|
|
95
|
+
* 逐源跑 adapter(limit:1 轻探,adapters 覆盖表同 collectDesktopMembers 注入缝),返回逐源
|
|
96
|
+
* {id,name,enabled,ok,detail,scannedAt}:关闭源 ok=null(不计入分母);故障源 ok:false 带 detail——
|
|
97
|
+
* 治「单源故障静默缺席无信号」(评审尾巴:红线 8 现状反例 desktopAgents 空 catch 的可见性配套)。
|
|
98
|
+
*/
|
|
99
|
+
function collectDesktopSourceHealth(agentActivityCfg, deps) {
|
|
100
|
+
const d = deps || {};
|
|
101
|
+
const now = d.now || Date.now();
|
|
102
|
+
const sec = normalizeAgentActivity(
|
|
103
|
+
agentActivityCfg !== null && typeof agentActivityCfg === 'object' ? agentActivityCfg : null
|
|
104
|
+
);
|
|
105
|
+
const overrides = d.adapters && typeof d.adapters === 'object' ? d.adapters : {};
|
|
106
|
+
const scannedAt = new Date(now).toISOString();
|
|
107
|
+
return DESKTOP_SOURCES.map((src) => {
|
|
108
|
+
const s = sec.sources[src.id] || {};
|
|
109
|
+
if (s.enabled === false || sec.enabled === false) {
|
|
110
|
+
return { id: src.id, name: src.name, enabled: false, ok: null, detail: '已关闭(设置页可开)', scannedAt };
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
const adapterFn = overrides[src.id] || src.adapter;
|
|
114
|
+
const r = adapterFn(s, { limit: 1, previewChars: 0, now });
|
|
115
|
+
const ok = !!(r && r.ok !== false);
|
|
116
|
+
return { id: src.id, name: src.name, enabled: true, ok, detail: (r && r.detail) || (ok ? '正常' : '失败'), scannedAt };
|
|
117
|
+
} catch (e) {
|
|
118
|
+
return { id: src.id, name: src.name, enabled: true, ok: false, detail: redact(String((e && e.message) || e)), scannedAt };
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
module.exports = { DESKTOP_SOURCES, collectDesktopMembers, collectDesktopSourceHealth };
|
package/lib/diary.js
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* diary.js — 任务日报(v2.7):多日任务的每日过程留痕
|
|
5
|
+
*
|
|
6
|
+
* - 存储 data/diary.json:[{taskId, date(YYYY-MM-DD), content(1..2000 字), updatedAt}],裸数组;
|
|
7
|
+
* 唯一键 taskId+date(同日再存 = 覆盖更新,updatedAt 刷新);原子写走 store.writeJSON
|
|
8
|
+
* - 端点(挂载于 appAssembler,任务路由之后):
|
|
9
|
+
* GET /api/tasks/:id/diary → 该任务全部日报(按日期倒序)
|
|
10
|
+
* PUT /api/tasks/:id/diary → upsert(同日覆盖)→ 201 新建 / 200 覆盖
|
|
11
|
+
* + 事件流 type:"diary"(detail=`日报 <date>`,仅首存发,重写不重复发)
|
|
12
|
+
* DELETE /api/tasks/:id/diary/:date → 204;该日期无日报 404 DIARY_NOT_FOUND
|
|
13
|
+
* GET /api/tasks/:id/diary/prefill → 预填 {date, giteaCommits, memoUpdates, draft}
|
|
14
|
+
* ⚠ 路由注册顺序:prefill(5 段字面量)必须先于 :date(5 段参数)注册——router 先命中先得,
|
|
15
|
+
* 反了会被 :date 吃掉返回 400。
|
|
16
|
+
* - prefill 两数据源(均只读):
|
|
17
|
+
* ① giteaCommits:data/activities.json 当天的 commit 类动态(feeds 词表 'commit';存量
|
|
18
|
+
* commit_pushed 容错),匹配 = lib/attribute.js 精确规则
|
|
19
|
+
* (branch 相等 / task/<id> 前缀 / 标题含 [#taskId])优先,任务 repo 相等兜底(存量动态
|
|
20
|
+
* branch 多为 null,只按 branch 永远命中不了);url 非 http(s) 剔除(防伪协议进前端)。
|
|
21
|
+
* ② memoUpdates:data/task_events.json 该任务当天 type:"memo" 事件,detail 形如
|
|
22
|
+
* `纪要《标题》:引用原句`(lib/inbox.js 写入),拆为 {note:标题, quote:原句};不合形态的
|
|
23
|
+
* 旧数据兜底 quote=detail、note=''。
|
|
24
|
+
* - 日期口径:预填 date 缺省 = 服务器本地今天;occurredAt/at(任意时区偏移的 ISO8601)换算到
|
|
25
|
+
* 服务器本地 YYYY-MM-DD 再与参数做字符串相等比较(与 overview 逾期口径同时区,一致可预期)。
|
|
26
|
+
* - 日期校验:正则 + 本地日历回读(与 dueDate 同款,拦 2026-02-30 / 2026-13-99),非法 400。
|
|
27
|
+
* - 删任务顺带清理其全部日报(removeTaskDiaries,与 taskEvents.removeTaskEvents 同纪律)。
|
|
28
|
+
* - 成员联动:/api/members 成员对象增 diaryToday(overview.js 调 taskIdsWithDiaryOn)。
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
const store = require('./store');
|
|
32
|
+
const storeDb = require('./storeDb');
|
|
33
|
+
const path = require('node:path');
|
|
34
|
+
const { HttpError, sendJSON, noContent } = require('./router');
|
|
35
|
+
const taskEvents = require('./taskEvents');
|
|
36
|
+
const attribute = require('./attribute');
|
|
37
|
+
|
|
38
|
+
const CONTENT_MAX = 2000;
|
|
39
|
+
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
|
|
40
|
+
const HTTP_URL_RE = /^https?:\/\/\S+$/i;
|
|
41
|
+
|
|
42
|
+
// ---------- 日期工具 ----------
|
|
43
|
+
|
|
44
|
+
function pad2(x) {
|
|
45
|
+
return String(x).padStart(2, '0');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 时间戳(缺省 now)→ 服务器本地时区 YYYY-MM-DD */
|
|
49
|
+
function localYmd(ts) {
|
|
50
|
+
const d = ts === undefined ? new Date() : new Date(ts);
|
|
51
|
+
return `${d.getFullYear()}-${pad2(d.getMonth() + 1)}-${pad2(d.getDate())}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** ISO8601(任意时区偏移)→ 服务器本地日期;解析失败 null */
|
|
55
|
+
function localDateOf(iso) {
|
|
56
|
+
const t = Date.parse(iso);
|
|
57
|
+
return Number.isFinite(t) ? localYmd(t) : null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** YYYY-MM-DD 校验:正则 + 本地日历回读(拦 2026-02-30 / 2026-13-99 这类正则通过但日历不存在的值) */
|
|
61
|
+
function validateYmd(v, label) {
|
|
62
|
+
if (typeof v !== 'string' || !DATE_RE.test(v)) {
|
|
63
|
+
throw new HttpError(400, 'FIELD_INVALID', `${label} 必须为 YYYY-MM-DD 格式字符串`);
|
|
64
|
+
}
|
|
65
|
+
const parts = v.split('-');
|
|
66
|
+
const y = Number(parts[0]);
|
|
67
|
+
const m = Number(parts[1]);
|
|
68
|
+
const d = Number(parts[2]);
|
|
69
|
+
const dt = new Date(y, m - 1, d);
|
|
70
|
+
if (dt.getFullYear() !== y || dt.getMonth() !== m - 1 || dt.getDate() !== d) {
|
|
71
|
+
throw new HttpError(400, 'FIELD_INVALID', `${label} 不是有效日期`);
|
|
72
|
+
}
|
|
73
|
+
return v;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ---------- 存储(原子写;唯一键 taskId+date) ----------
|
|
77
|
+
|
|
78
|
+
/** 容错读取:裸数组或 {diaries:[...]} 均容忍;缺失回退 [];剔除缺 taskId/date 的坏行。
|
|
79
|
+
* v2.9.33 批次 2:diary 迁移 → SQLite(storeDb 路由,返回形状不变) */
|
|
80
|
+
function loadDiaries(ctx) {
|
|
81
|
+
if (storeDb.unitBackend(path.dirname(ctx.diaryPath), 'diary').backend === 'sqlite') {
|
|
82
|
+
return storeDb.loadDiaries(storeDb.dbPathFor(path.dirname(ctx.diaryPath)));
|
|
83
|
+
}
|
|
84
|
+
const d = store.readJSON(ctx.diaryPath, null);
|
|
85
|
+
let list = [];
|
|
86
|
+
if (Array.isArray(d)) list = d;
|
|
87
|
+
else if (store.isPlainObject(d) && Array.isArray(d.diaries)) list = d.diaries;
|
|
88
|
+
return list.filter((x) => store.isPlainObject(x) && typeof x.taskId === 'string' && typeof x.date === 'string');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function saveDiaries(ctx, diaries) {
|
|
92
|
+
if (storeDb.unitBackend(path.dirname(ctx.diaryPath), 'diary').backend === 'sqlite') {
|
|
93
|
+
return storeDb.saveDiaries(storeDb.dbPathFor(path.dirname(ctx.diaryPath)), diaries);
|
|
94
|
+
}
|
|
95
|
+
store.writeJSON(ctx.diaryPath, diaries);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* upsert:同 taskId+date 覆盖(updatedAt 刷新),否则追加。
|
|
100
|
+
* @returns {{diary:{taskId,date,content,updatedAt}, created:boolean}}
|
|
101
|
+
*/
|
|
102
|
+
function upsertDiary(ctx, taskId, date, content) {
|
|
103
|
+
const diaries = loadDiaries(ctx);
|
|
104
|
+
const now = new Date().toISOString();
|
|
105
|
+
const found = diaries.find((x) => x.taskId === taskId && x.date === date);
|
|
106
|
+
let created = false;
|
|
107
|
+
if (found) {
|
|
108
|
+
found.content = content;
|
|
109
|
+
found.status = 'published';
|
|
110
|
+
found.updatedAt = now;
|
|
111
|
+
} else {
|
|
112
|
+
diaries.push({ taskId, date, content, status: 'published', updatedAt: now });
|
|
113
|
+
created = true;
|
|
114
|
+
}
|
|
115
|
+
saveDiaries(ctx, diaries);
|
|
116
|
+
return { diary: { taskId, date, content, status: 'published', updatedAt: now }, created };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** WAP 门禁:标记某条日报为 blocked(底层数据异常/质量不达标时人工调用) */
|
|
120
|
+
function blockDiary(ctx, taskId, date, reason) {
|
|
121
|
+
const diaries = loadDiaries(ctx);
|
|
122
|
+
const found = diaries.find((x) => x.taskId === taskId && x.date === date);
|
|
123
|
+
if (!found) return false;
|
|
124
|
+
found.status = 'blocked';
|
|
125
|
+
found.blockReason = typeof reason === 'string' ? reason.slice(0, 200) : '';
|
|
126
|
+
saveDiaries(ctx, diaries);
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** WAP 门禁:解除 blocked(人工确认数据修复后恢复为 published) */
|
|
131
|
+
function unblockDiary(ctx, taskId, date) {
|
|
132
|
+
const diaries = loadDiaries(ctx);
|
|
133
|
+
const found = diaries.find((x) => x.taskId === taskId && x.date === date);
|
|
134
|
+
if (!found) return false;
|
|
135
|
+
found.status = 'published';
|
|
136
|
+
delete found.blockReason;
|
|
137
|
+
saveDiaries(ctx, diaries);
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** 删除某任务某天日报;存在并删除 true,不存在 false(不写盘) */
|
|
142
|
+
function removeDiary(ctx, taskId, date) {
|
|
143
|
+
const diaries = loadDiaries(ctx);
|
|
144
|
+
const kept = diaries.filter((x) => !(x.taskId === taskId && x.date === date));
|
|
145
|
+
if (kept.length === diaries.length) return false;
|
|
146
|
+
saveDiaries(ctx, kept);
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** 删任务顺带清理其全部日报;返回清理条数(无则零写盘) */
|
|
151
|
+
function removeTaskDiaries(ctx, taskId) {
|
|
152
|
+
const diaries = loadDiaries(ctx);
|
|
153
|
+
const kept = diaries.filter((x) => x.taskId !== taskId);
|
|
154
|
+
if (kept.length === diaries.length) return 0;
|
|
155
|
+
saveDiaries(ctx, kept);
|
|
156
|
+
return diaries.length - kept.length;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** 某任务全部日报,按日期倒序(YYYY-MM-DD 字符串序 = 日历序) */
|
|
160
|
+
function diariesForTask(ctx, taskId) {
|
|
161
|
+
return loadDiaries(ctx)
|
|
162
|
+
.filter((x) => x.taskId === taskId)
|
|
163
|
+
.sort((a, b) => String(b.date).localeCompare(String(a.date)));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** 某天有日报的任务 id 集合(成员 diaryToday 联动用;纯读不落盘) */
|
|
167
|
+
function taskIdsWithDiaryOn(ctx, ymd) {
|
|
168
|
+
const out = new Set();
|
|
169
|
+
for (const d of loadDiaries(ctx)) {
|
|
170
|
+
if (d.date === ymd) out.add(d.taskId);
|
|
171
|
+
}
|
|
172
|
+
return out;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// ---------- 预填(两数据源,全部只读) ----------
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 当天命中的 commit 类活动:attribute 精确规则优先(branch 相等 / task/<id> 前缀 / [#taskId]),
|
|
179
|
+
* 任务 repo 相等兜底;url 非 http(s) 剔除。返回按 occurredAt 倒序(混合时区,按毫秒排)。
|
|
180
|
+
* 提交类型判定:feeds 词表 'commit'(giteaClient activities/feeds 数据源)∪ 存量 'commit_pushed' 容错。
|
|
181
|
+
*/
|
|
182
|
+
function giteaCommitsFor(ctx, task, date) {
|
|
183
|
+
const activities = store.loadActivities(ctx);
|
|
184
|
+
const out = [];
|
|
185
|
+
for (const act of activities) {
|
|
186
|
+
if (!act || (act.type !== 'commit' && act.type !== 'commit_pushed')) continue;
|
|
187
|
+
if (localDateOf(act.occurredAt) !== date) continue;
|
|
188
|
+
const url = typeof act.url === 'string' ? act.url : '';
|
|
189
|
+
if (!url || !HTTP_URL_RE.test(url)) continue; // 无 url / 伪协议剔除(与成果挂接同门槛)
|
|
190
|
+
const precise = attribute.matchReason(act, task);
|
|
191
|
+
const repoHit =
|
|
192
|
+
precise === null && typeof task.repo === 'string' && task.repo !== '' && act.repo === task.repo;
|
|
193
|
+
if (!precise && !repoHit) continue;
|
|
194
|
+
out.push({
|
|
195
|
+
sha: typeof act.sha === 'string' ? act.sha : null, // v2.0 前落盘的存量动态无 sha → null
|
|
196
|
+
title: typeof act.title === 'string' ? act.title : '',
|
|
197
|
+
url,
|
|
198
|
+
occurredAt: typeof act.occurredAt === 'string' ? act.occurredAt : null,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
out.sort((a, b) => (Date.parse(b.occurredAt) || 0) - (Date.parse(a.occurredAt) || 0));
|
|
202
|
+
return out;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* 当天该任务的 memo 事件 → [{quote, note}](at 倒序)。
|
|
207
|
+
* detail=`纪要《标题》:引用原句` 拆为 note=标题、quote=原句;不合形态兜底 quote=detail、note=''。
|
|
208
|
+
*/
|
|
209
|
+
function memoUpdatesFor(ctx, task, date) {
|
|
210
|
+
const events = taskEvents.loadEvents(ctx);
|
|
211
|
+
const out = [];
|
|
212
|
+
for (const e of events) {
|
|
213
|
+
if (!e || e.taskId !== task.id || e.type !== 'memo') continue;
|
|
214
|
+
if (localDateOf(e.at) !== date) continue;
|
|
215
|
+
const detail = typeof e.detail === 'string' ? e.detail : '';
|
|
216
|
+
const m = detail.match(/^纪要《([\s\S]*?)》:([\s\S]*)$/);
|
|
217
|
+
out.push({
|
|
218
|
+
at: typeof e.at === 'string' ? e.at : null,
|
|
219
|
+
quote: m ? m[2] : detail,
|
|
220
|
+
note: m ? m[1] : '',
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
out.sort((a, b) => (Date.parse(b.at) || 0) - (Date.parse(a.at) || 0));
|
|
224
|
+
return out.map((x) => ({ quote: x.quote, note: x.note }));
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** 草稿文本:两块拼装;两块皆空 → ''(前端显示「无预填内容」) */
|
|
228
|
+
function buildDraft(giteaCommits, memoUpdates) {
|
|
229
|
+
const lines = [];
|
|
230
|
+
if (giteaCommits.length > 0) {
|
|
231
|
+
lines.push(`■ Gitea 提交(${giteaCommits.length})`);
|
|
232
|
+
for (const c of giteaCommits) {
|
|
233
|
+
lines.push(`- ${c.title || '(无提交说明)'}`);
|
|
234
|
+
lines.push(` ${c.url}`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (memoUpdates.length > 0) {
|
|
238
|
+
lines.push(`■ 纪要摘录(${memoUpdates.length})`);
|
|
239
|
+
for (const m of memoUpdates) {
|
|
240
|
+
lines.push(`- 《${m.note}》:${m.quote}`);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return lines.join('\n');
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** 预填聚合(路由与测试共用) */
|
|
247
|
+
function collectPrefill(ctx, task, date) {
|
|
248
|
+
const giteaCommits = giteaCommitsFor(ctx, task, date);
|
|
249
|
+
const memoUpdates = memoUpdatesFor(ctx, task, date);
|
|
250
|
+
return { giteaCommits, memoUpdates, draft: buildDraft(giteaCommits, memoUpdates) };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// ---------- 路由 ----------
|
|
254
|
+
|
|
255
|
+
function validateContent(v) {
|
|
256
|
+
if (typeof v !== 'string') throw new HttpError(400, 'FIELD_INVALID', 'content 必填且为字符串');
|
|
257
|
+
if (v.length < 1 || v.length > CONTENT_MAX) {
|
|
258
|
+
throw new HttpError(400, 'FIELD_INVALID', `content 长度必须为 1..${CONTENT_MAX} 字符`);
|
|
259
|
+
}
|
|
260
|
+
if (v.trim().length < 1) throw new HttpError(400, 'FIELD_INVALID', 'content 不能为纯空白');
|
|
261
|
+
return v;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function registerDiaryRoutes(router, ctx) {
|
|
265
|
+
function requireTask(c) {
|
|
266
|
+
const { tasks } = store.loadTasks(ctx);
|
|
267
|
+
const task = tasks.find((t) => t.id === c.params.id);
|
|
268
|
+
if (!task) throw new HttpError(404, 'TASK_NOT_FOUND', '任务不存在');
|
|
269
|
+
return task;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// ⚠ 必须先于 DELETE/GET :date 同段数路由注册(见文件头「路由注册顺序」)
|
|
273
|
+
router.add('GET', '/api/tasks/:id/diary/prefill', (c) => {
|
|
274
|
+
const task = requireTask(c);
|
|
275
|
+
const q = c.query.get('date');
|
|
276
|
+
const date = q === null || q === '' ? localYmd() : validateYmd(q, 'date');
|
|
277
|
+
const { giteaCommits, memoUpdates, draft } = collectPrefill(ctx, task, date);
|
|
278
|
+
sendJSON(c.res, 200, { date, giteaCommits, memoUpdates, draft });
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
router.add('GET', '/api/tasks/:id/diary', (c) => {
|
|
282
|
+
requireTask(c);
|
|
283
|
+
sendJSON(c.res, 200, store.clone(diariesForTask(ctx, c.params.id)));
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
router.add('PUT', '/api/tasks/:id/diary', (c) => {
|
|
287
|
+
const task = requireTask(c);
|
|
288
|
+
const body = c.body || {};
|
|
289
|
+
if (body.date === undefined || body.date === null) {
|
|
290
|
+
throw new HttpError(400, 'FIELD_INVALID', 'date 必填(YYYY-MM-DD)');
|
|
291
|
+
}
|
|
292
|
+
const date = validateYmd(body.date, 'date');
|
|
293
|
+
const content = validateContent(body.content);
|
|
294
|
+
const { diary, created } = upsertDiary(ctx, task.id, date, content);
|
|
295
|
+
// 事件流仅首存发(同日重写不重复发,防噪)
|
|
296
|
+
if (created) {
|
|
297
|
+
taskEvents.appendEvents(ctx, [
|
|
298
|
+
taskEvents.makeEvent(task.id, 'diary', null, date, `日报 ${date}`, new Date().toISOString()),
|
|
299
|
+
]);
|
|
300
|
+
}
|
|
301
|
+
sendJSON(c.res, created ? 201 : 200, diary);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
router.add('DELETE', '/api/tasks/:id/diary/:date', (c) => {
|
|
305
|
+
requireTask(c);
|
|
306
|
+
const date = validateYmd(c.params.date, 'date');
|
|
307
|
+
if (!removeDiary(ctx, c.params.id, date)) {
|
|
308
|
+
throw new HttpError(404, 'DIARY_NOT_FOUND', '该日期没有日报');
|
|
309
|
+
}
|
|
310
|
+
noContent(c.res);
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
module.exports = {
|
|
315
|
+
CONTENT_MAX,
|
|
316
|
+
localYmd,
|
|
317
|
+
localDateOf,
|
|
318
|
+
validateYmd,
|
|
319
|
+
loadDiaries,
|
|
320
|
+
saveDiaries,
|
|
321
|
+
upsertDiary,
|
|
322
|
+
removeDiary,
|
|
323
|
+
removeTaskDiaries,
|
|
324
|
+
diariesForTask,
|
|
325
|
+
taskIdsWithDiaryOn,
|
|
326
|
+
giteaCommitsFor,
|
|
327
|
+
memoUpdatesFor,
|
|
328
|
+
buildDraft,
|
|
329
|
+
collectPrefill,
|
|
330
|
+
registerDiaryRoutes,
|
|
331
|
+
blockDiary,
|
|
332
|
+
unblockDiary,
|
|
333
|
+
};
|