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
package/lib/analyze.js
ADDED
|
@@ -0,0 +1,625 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* analyze.js — AI 拆解管线(PRD 任务雷达 §5)
|
|
5
|
+
*
|
|
6
|
+
* 引擎调用形态(2026-10-18 阶段 0 真机实测):
|
|
7
|
+
* node <dshBin> --profile headless <prompt> —— dsh 是 npm .cmd shim,
|
|
8
|
+
* 直 spawn node + bin.js 绕开 cmd.exe(零 shell、零注入面、绕开 8191 cmd 行长限制;
|
|
9
|
+
* Windows CreateProcess 上限 32767 UTF-16,实测 27K 中文 prompt 通过)。
|
|
10
|
+
* dsh headless 无 stdin/无 system 旗标 → 纯 JSON 靠 prompt 约束 + 后处理提取双保险。
|
|
11
|
+
* 实测耗时 12~21s/条(GLM glm-5.3-flash 路由)→ 批量走异步 job,立即 202 + 进度可查。
|
|
12
|
+
*
|
|
13
|
+
* fixture:DSH_FIXTURE 指向的文件内容(原样文本)替代引擎 stdout(测试全离线)。
|
|
14
|
+
*
|
|
15
|
+
* 输出契约:{tasks:[{title,desc,priority,suggestedMemberId,dueHint,confidence,quote,rationale}],
|
|
16
|
+
* taskUpdates:[{existingTaskId,quote,note,confidence}], noise:[{quote,reason}]}
|
|
17
|
+
* - v1.9 查重分诊:纪要内容对看板已有任务的讨论/进展/变更 → 产 taskUpdates(更新提案,
|
|
18
|
+
* 含原文引用),不再走「疑似重复降置信度」老路;全新事项照常产 tasks;
|
|
19
|
+
* tasks + taskUpdates 合计 ≤ maxTasksPerMessage,按置信度合并截断
|
|
20
|
+
* - 解析失败重试一次,再失败该纪要标记 ANALYZE_FAILED 可重试(PRD §5)
|
|
21
|
+
* - 引擎超时缺省 300s;对已失败纪要的重试自动升压 ×2(P1-B,job 记录 attempt/timeoutSec)
|
|
22
|
+
* - 说话人映射用聚合名册(store.aggregateRoster:手工 ∪ Gitea 缓存)四优先级匹配,
|
|
23
|
+
* 候选卡带 matchSource(alias/name/gitea)归人依据(P1-A)
|
|
24
|
+
* - confidence < 0.5 不产出候选 → noise(PRD §5 置信度规则)
|
|
25
|
+
* - 单纪要候选 ≤ maxTasksPerMessage(缺省 10),按置信度截断
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
const fs = require('node:fs');
|
|
29
|
+
const os = require('node:os');
|
|
30
|
+
const path = require('node:path');
|
|
31
|
+
const { execFile } = require('node:child_process');
|
|
32
|
+
const store = require('./store');
|
|
33
|
+
const { HttpError, sendJSON } = require('./router');
|
|
34
|
+
const { redact } = require('./redact');
|
|
35
|
+
const jobsMod = require('./jobs');
|
|
36
|
+
|
|
37
|
+
const PROJECT_ROOT = path.resolve(__dirname, '..');
|
|
38
|
+
|
|
39
|
+
const PRIORITIES = new Set(['high', 'medium', 'low', 'unset']);
|
|
40
|
+
|
|
41
|
+
// ------------------------------------------------------------------ dsh CLI
|
|
42
|
+
|
|
43
|
+
let dshBinCache = null;
|
|
44
|
+
|
|
45
|
+
/** 解析 dsh 的 node 入口 bin.js:config.analyze.dshBin → %APPDATA%\npm\...;全败 null */
|
|
46
|
+
function resolveDshBin(cfg) {
|
|
47
|
+
const configured = cfg && cfg.analyze && typeof cfg.analyze.dshBin === 'string' && cfg.analyze.dshBin.trim();
|
|
48
|
+
if (configured) {
|
|
49
|
+
const p = path.resolve(configured);
|
|
50
|
+
if (fs.existsSync(p)) return p;
|
|
51
|
+
return null; // 显式配置了但不存在 → 不再兜底(配置错误应显性暴露)
|
|
52
|
+
}
|
|
53
|
+
if (dshBinCache && fs.existsSync(dshBinCache)) return dshBinCache;
|
|
54
|
+
const appData = process.env.APPDATA || path.join(os.homedir(), 'AppData', 'Roaming');
|
|
55
|
+
const fallback = path.join(appData, 'npm', 'node_modules', '@deepseek-ai', 'dsh', 'lib', 'bin.js');
|
|
56
|
+
if (fs.existsSync(fallback)) {
|
|
57
|
+
dshBinCache = fallback;
|
|
58
|
+
return fallback;
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function fixtureFile() {
|
|
64
|
+
const p = process.env.DSH_FIXTURE;
|
|
65
|
+
if (!p) return null;
|
|
66
|
+
return path.isAbsolute(p) ? p : path.resolve(PROJECT_ROOT, p);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** DSH_FIXTURE 模式:文件原样文本即引擎 stdout */
|
|
70
|
+
function runHeadlessFixture() {
|
|
71
|
+
const file = fixtureFile();
|
|
72
|
+
if (!file) return null;
|
|
73
|
+
return fs.readFileSync(file, 'utf8');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 真实引擎调用:node <dshBin> --profile headless <prompt>,返回 stdout 文本。
|
|
78
|
+
* 独立导出供 /api/radar/engine/test 复用。opts.timeoutSec 可覆盖配置超时(P1-B 重试升压用)。
|
|
79
|
+
*/
|
|
80
|
+
function runHeadless(cfg, prompt, opts = {}) {
|
|
81
|
+
const dshBin = resolveDshBin(cfg);
|
|
82
|
+
if (!dshBin) {
|
|
83
|
+
throw new HttpError(502, 'DSH_NOT_FOUND', '未找到 dsh 引擎入口(config.analyze.dshBin 或 %APPDATA%\\npm\\node_modules\\@deepseek-ai\\dsh\\lib\\bin.js)');
|
|
84
|
+
}
|
|
85
|
+
const timeoutSec = Number(opts && opts.timeoutSec) || Number(cfg.analyze && cfg.analyze.timeoutSec) || 300;
|
|
86
|
+
return new Promise((resolve, reject) => {
|
|
87
|
+
execFile(
|
|
88
|
+
process.execPath,
|
|
89
|
+
[dshBin, '--profile', 'headless', prompt],
|
|
90
|
+
// 桌面宿主里 process.execPath 是 Electron 主程序,且 env 不带 ELECTRON_RUN_AS_NODE:
|
|
91
|
+
// 不强制这一位会把「拆解」变成「又开一个 DSH 窗口」而不是跑 headless。纯 node 下该变量被忽略。
|
|
92
|
+
{ timeout: timeoutSec * 1000, windowsHide: true, maxBuffer: 16 * 1024 * 1024, env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' } },
|
|
93
|
+
(err, stdout, stderr) => {
|
|
94
|
+
if (err) {
|
|
95
|
+
const reason = err.killed || err.signal === 'SIGTERM' ? `超时(>${timeoutSec}s)` : (err.message || String(err));
|
|
96
|
+
return reject(Object.assign(new Error(redact(`dsh 执行失败: ${reason} ${String(stderr || '').slice(0, 200)}`)), { code: 'DSH_ERROR' }));
|
|
97
|
+
}
|
|
98
|
+
resolve(String(stdout || ''));
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ------------------------------------------------------------- prompt 构造
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 说话人 raw → {id, source}(v1.5 goose 验收 P1-A:聚合名册四优先级匹配)。
|
|
108
|
+
* roster = store.aggregateRoster(ctx) 快照(无 source 字段的条目按 manual 处理,兼容旧夹具):
|
|
109
|
+
* ① 手工 alias 精确(source 'alias')
|
|
110
|
+
* ② 手工 name 精确('name')
|
|
111
|
+
* ③ gitea login(id)/name 精确('gitea')
|
|
112
|
+
* ④ 全部字段小写双向包含(raw 与字段均 ≥2 字,防单字误伤),仍按 ①②③ 来源优先
|
|
113
|
+
* 未命中返回 null。
|
|
114
|
+
*/
|
|
115
|
+
function resolveSpeaker(raw, roster) {
|
|
116
|
+
const key = String(raw || '').trim().toLowerCase();
|
|
117
|
+
if (!key) return null;
|
|
118
|
+
const norm = (v) => String(v || '').trim().toLowerCase();
|
|
119
|
+
const srcOf = (m) => (m && m.source === 'gitea' ? 'gitea' : 'manual');
|
|
120
|
+
const manual = roster.filter((m) => srcOf(m) === 'manual');
|
|
121
|
+
const gitea = roster.filter((m) => srcOf(m) === 'gitea');
|
|
122
|
+
// 双向包含且两边 ≥2 字(v1.5 P1-A:小写包含匹配,单字不上包含档防误伤)
|
|
123
|
+
const containsLoose = (field, k) => field.length >= 2 && (field.includes(k) || k.includes(field));
|
|
124
|
+
for (const m of manual) {
|
|
125
|
+
if ((m.aliases || []).some((a) => norm(a) === key)) return { id: m.id, source: 'alias' };
|
|
126
|
+
}
|
|
127
|
+
for (const m of manual) {
|
|
128
|
+
if (norm(m.name) === key) return { id: m.id, source: 'name' };
|
|
129
|
+
}
|
|
130
|
+
for (const m of gitea) {
|
|
131
|
+
if (norm(m.id) === key || norm(m.name) === key) return { id: m.id, source: 'gitea' };
|
|
132
|
+
}
|
|
133
|
+
if (key.length >= 2) {
|
|
134
|
+
for (const m of manual) {
|
|
135
|
+
if ((m.aliases || []).some((a) => containsLoose(norm(a), key))) return { id: m.id, source: 'alias' };
|
|
136
|
+
}
|
|
137
|
+
for (const m of manual) {
|
|
138
|
+
if (containsLoose(norm(m.name), key)) return { id: m.id, source: 'name' };
|
|
139
|
+
}
|
|
140
|
+
for (const m of gitea) {
|
|
141
|
+
if (containsLoose(norm(m.id), key) || containsLoose(norm(m.name), key)) return { id: m.id, source: 'gitea' };
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** 说话人 raw → 成员 id:聚合名册四优先级(见 resolveSpeaker);未命中 null(旧接口形状) */
|
|
148
|
+
function mapSpeaker(raw, roster) {
|
|
149
|
+
const hit = resolveSpeaker(raw, roster);
|
|
150
|
+
return hit ? hit.id : null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** 说话人分段文本行:【raw|memberId】text;memberId 已由别名预匹配解析(未识别 "-") */
|
|
154
|
+
function transcriptLines(message, roster) {
|
|
155
|
+
const lines = [];
|
|
156
|
+
for (const seg of message.speakerSegments || []) {
|
|
157
|
+
const mid = mapSpeaker(seg.raw, roster) || '-';
|
|
158
|
+
lines.push(`【${seg.raw}|${mid}】${seg.text}`);
|
|
159
|
+
}
|
|
160
|
+
return lines.join('\n');
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** 组 prompt(PRD §5:置信度规则 + few-shot + 防重清单 + 名册;只输出 JSON) */
|
|
164
|
+
function buildPrompt(message, roster, boardTasks, cfg) {
|
|
165
|
+
const maxTasks = Number(cfg.analyze && cfg.analyze.maxTasksPerMessage) || 10;
|
|
166
|
+
const maxChars = Number(cfg.analyze && cfg.analyze.maxPromptChars) || 24000;
|
|
167
|
+
|
|
168
|
+
let transcript = transcriptLines(message, roster);
|
|
169
|
+
let truncated = false;
|
|
170
|
+
if (transcript.length > maxChars) {
|
|
171
|
+
transcript = transcript.slice(0, maxChars) + '\n【已截断:超出长度上限】';
|
|
172
|
+
truncated = true;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const rosterLines = roster
|
|
176
|
+
.map((m) => {
|
|
177
|
+
const aliases = Array.isArray(m.aliases) && m.aliases.length ? `(别名: ${m.aliases.join('/')})` : '';
|
|
178
|
+
return `- ${m.id} | ${m.name}${aliases}`;
|
|
179
|
+
})
|
|
180
|
+
.join('\n') || '(名册为空)';
|
|
181
|
+
|
|
182
|
+
const boardLines = (boardTasks || [])
|
|
183
|
+
.map((t) => `- ${t.id} | [${t.status}] ${t.title}`)
|
|
184
|
+
.join('\n') || '(看板当前没有任务)';
|
|
185
|
+
|
|
186
|
+
return `你是任务拆解引擎。从会议逐字稿中识别"待办任务"与"对已有任务的更新",只输出一个 JSON 对象,禁止输出任何其他文字、解释或 markdown 代码块围栏。
|
|
187
|
+
|
|
188
|
+
# 输出 schema
|
|
189
|
+
{"tasks":[{"title":"","desc":"","priority":"high|medium|low|unset","suggestedMemberId":"","dueHint":"","confidence":0.0,"quote":"","rationale":""}],"taskUpdates":[{"existingTaskId":"","quote":"","note":"","confidence":0.0}],"noise":[{"quote":"","reason":""}]}
|
|
190
|
+
|
|
191
|
+
# 置信度规则(严格遵守;tasks 与 taskUpdates 适用同一规则)
|
|
192
|
+
- 0.9 以上:明确动作 + 明确负责人 + 明确时间
|
|
193
|
+
- 0.7 到 0.9:明确动作,负责人或时间模糊
|
|
194
|
+
- 0.5 到 0.7:推断性表述(如"这个后面得弄")
|
|
195
|
+
- 低于 0.5:tasks 与 taskUpdates 均不产出,归入 noise
|
|
196
|
+
|
|
197
|
+
# 判定正反例
|
|
198
|
+
- 寒暄、过程性发言 ✗
|
|
199
|
+
- "我看看" ✗(无明确动作与交付物)
|
|
200
|
+
- "周五前给我" ✓
|
|
201
|
+
- "@小王 把部署文档更新下" ✓
|
|
202
|
+
- 同一工程的连续子步骤(整理清单→写 SOP→建模板)✗ 逐条拆成多张(合成一张,desc 列步骤)
|
|
203
|
+
- 对已有任务的跟进讨论(如"SSO 联调遇阻,数据源还没通"且看板已有该任务)→ taskUpdate ✓
|
|
204
|
+
- 只是顺带提了一句旧任务名、没有实质进展或变更 → 不产 taskUpdate ✗
|
|
205
|
+
|
|
206
|
+
# 拆解规则
|
|
207
|
+
- tasks 与 taskUpdates 合计最多 ${maxTasks} 条,按置信度从高到低截断
|
|
208
|
+
- 粒度原则(优先级最高,覆盖一切拆分冲动):一张候选 = 一个「可独立指派、独立验收」的工作单元——同一目标、同一负责对象、同一交付物下的多个准备步骤/子动作,合成一张候选,把步骤写进 desc。例:一次需求调研里的"整理报表清单""写 SOP""建模板"是同一工程的三步,合一张候选,desc 列出步骤
|
|
209
|
+
- 判断标准:这些动作会不会由同一个人、围绕同一个交付物连续推进?会 → 一张;不同人各领一段、交付物截然不同、或需要独立跟进验收 → 才拆
|
|
210
|
+
- 宁大勿碎:产出 1 条步骤完整的候选,好过 3 条碎片候选(碎片让看板失焦、派工失真);大小拿不准时选更大的一方
|
|
211
|
+
- 互不相干的两件事(不同目标/不同负责人/不同交付物)才拆成两张;不建父子层级
|
|
212
|
+
- dueHint 只写原文中的日期词(如"周五""月底"),不要换算成具体日期
|
|
213
|
+
- suggestedMemberId 只能从名册 id 中选;依据是发言分段的说话人映射或上下文;无法判断时填空字符串
|
|
214
|
+
- quote 必须是逐字稿中的原句(可截取,不改写)
|
|
215
|
+
- 查重分诊:先对照"现有看板任务"判断逐字稿内容与其的关系——
|
|
216
|
+
- 全新事项(看板中没有对应任务)→ 产 tasks
|
|
217
|
+
- 对看板已有任务的讨论/进展/变更(跟进情况、阻塞、方案调整、完成汇报)→ 不产 tasks,改产 taskUpdates:
|
|
218
|
+
existingTaskId 填看板任务 id,quote 填本次讨论的原句,note 用一句话概括本次进展/变更
|
|
219
|
+
- 顺带提了一句旧任务名但无实质讨论或变更 → 既不产 tasks 也不产 taskUpdates
|
|
220
|
+
- ${truncated ? '注意:逐字稿被截断,只分析可见部分' : '逐字稿完整'}
|
|
221
|
+
|
|
222
|
+
# 成员名册(id | 姓名(别名))
|
|
223
|
+
${rosterLines}
|
|
224
|
+
|
|
225
|
+
# 现有看板任务(id | [状态] 标题;查重分诊与 taskUpdates.existingTaskId 参考)
|
|
226
|
+
${boardLines}
|
|
227
|
+
|
|
228
|
+
# 会议逐字稿(【说话人|成员id】分段;成员id 为 "-" 表示未识别)
|
|
229
|
+
${transcript}
|
|
230
|
+
|
|
231
|
+
只输出 JSON。`;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// ------------------------------------------------------------- 输出解析/整形
|
|
235
|
+
|
|
236
|
+
/** 从引擎输出提取 JSON:剥代码围栏 → 首个 { 到末个 } → JSON.parse */
|
|
237
|
+
function extractJSON(text) {
|
|
238
|
+
let t = String(text || '').trim();
|
|
239
|
+
const fence = t.match(/```(?:json)?\s*([\s\S]*?)```/i);
|
|
240
|
+
if (fence && fence[1].includes('{')) t = fence[1].trim();
|
|
241
|
+
const i = t.indexOf('{');
|
|
242
|
+
const j = t.lastIndexOf('}');
|
|
243
|
+
if (i < 0 || j <= i) throw new Error('输出中未找到 JSON 对象');
|
|
244
|
+
const parsed = JSON.parse(t.slice(i, j + 1));
|
|
245
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
246
|
+
throw new Error('输出不是 JSON 对象');
|
|
247
|
+
}
|
|
248
|
+
return parsed;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function clipStr(v, max) {
|
|
252
|
+
return typeof v === 'string' ? v.slice(0, max) : '';
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** 模型输出 → 严格候选结构(脏数据逐项防御,不因单条畸形丢弃整批) */
|
|
256
|
+
function coerceResult(parsed, roster, cfg) {
|
|
257
|
+
const maxTasks = Number(cfg.analyze && cfg.analyze.maxTasksPerMessage) || 10;
|
|
258
|
+
const rosterIds = new Set(roster.map((m) => m.id));
|
|
259
|
+
let tasks = [];
|
|
260
|
+
for (const t of Array.isArray(parsed.tasks) ? parsed.tasks : []) {
|
|
261
|
+
if (!t || typeof t !== 'object') continue;
|
|
262
|
+
const title = clipStr(t.title, 200).trim();
|
|
263
|
+
if (!title) continue; // 无标题的候选不可用,丢弃
|
|
264
|
+
let confidence = Number(t.confidence);
|
|
265
|
+
if (!Number.isFinite(confidence)) confidence = 0;
|
|
266
|
+
confidence = Math.min(1, Math.max(0, confidence));
|
|
267
|
+
const suggested = typeof t.suggestedMemberId === 'string' && rosterIds.has(t.suggestedMemberId) ? t.suggestedMemberId : '';
|
|
268
|
+
tasks.push({
|
|
269
|
+
title,
|
|
270
|
+
desc: clipStr(t.desc, 2000),
|
|
271
|
+
priority: PRIORITIES.has(t.priority) ? t.priority : 'unset',
|
|
272
|
+
suggestedMemberId: suggested,
|
|
273
|
+
dueHint: clipStr(t.dueHint, 60).trim(),
|
|
274
|
+
confidence: Math.round(confidence * 100) / 100,
|
|
275
|
+
claimLevel: confidence >= 0.9 ? 'fact' : confidence >= 0.7 ? 'observation' : confidence >= 0.5 ? 'hypothesis' : 'suggestion',
|
|
276
|
+
quote: clipStr(t.quote, 300),
|
|
277
|
+
rationale: clipStr(t.rationale, 500),
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
tasks.sort((a, b) => b.confidence - a.confidence);
|
|
281
|
+
|
|
282
|
+
// v1.9 任务更新提案:existingTaskId 必填(缺了提案无处可挂,丢弃),quote/note/clamp 同套路
|
|
283
|
+
let updates = [];
|
|
284
|
+
for (const u of Array.isArray(parsed.taskUpdates) ? parsed.taskUpdates : []) {
|
|
285
|
+
if (!u || typeof u !== 'object') continue;
|
|
286
|
+
const existingTaskId = clipStr(u.existingTaskId, 100).trim();
|
|
287
|
+
if (!existingTaskId) continue;
|
|
288
|
+
let confidence = Number(u.confidence);
|
|
289
|
+
if (!Number.isFinite(confidence)) confidence = 0;
|
|
290
|
+
confidence = Math.min(1, Math.max(0, confidence));
|
|
291
|
+
updates.push({
|
|
292
|
+
existingTaskId,
|
|
293
|
+
quote: clipStr(u.quote, 300),
|
|
294
|
+
note: clipStr(u.note, 500),
|
|
295
|
+
confidence: Math.round(confidence * 100) / 100,
|
|
296
|
+
claimLevel: confidence >= 0.9 ? 'fact' : confidence >= 0.7 ? 'observation' : confidence >= 0.5 ? 'hypothesis' : 'suggestion',
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
// PRD §5 上限扩到两类合计:合并按置信度截断后再拆回(各自仍保持降序)
|
|
300
|
+
if (tasks.length + updates.length > maxTasks) {
|
|
301
|
+
const merged = tasks
|
|
302
|
+
.map((t) => ({ isTask: true, item: t, confidence: t.confidence }))
|
|
303
|
+
.concat(updates.map((u) => ({ isTask: false, item: u, confidence: u.confidence })));
|
|
304
|
+
merged.sort((a, b) => b.confidence - a.confidence);
|
|
305
|
+
const kept = merged.slice(0, maxTasks);
|
|
306
|
+
tasks = kept.filter((x) => x.isTask).map((x) => x.item);
|
|
307
|
+
updates = kept.filter((x) => !x.isTask).map((x) => x.item);
|
|
308
|
+
}
|
|
309
|
+
const noiseRaw = Array.isArray(parsed.noise) ? parsed.noise : [];
|
|
310
|
+
const noise = noiseRaw
|
|
311
|
+
.filter((n) => n && typeof n === 'object')
|
|
312
|
+
.slice(0, 50)
|
|
313
|
+
.map((n) => ({ quote: clipStr(n.quote, 300), reason: clipStr(n.reason, 300) }));
|
|
314
|
+
return { tasks: tasks.slice(0, maxTasks), taskUpdates: updates, noise };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// ----------------------------------------------------------------- 管线主体
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* 分析一条纪要:组 prompt → 调引擎 → 解析(失败重试一次)→ coerce。
|
|
321
|
+
* opts.timeoutSec 覆盖本次引擎超时(P1-B 重试升压:路由层按 attempt 计算 ×2 后传入)。
|
|
322
|
+
* 名册 = store.aggregateRoster(ctx) 聚合快照(手工 ∪ Gitea 缓存,P1-A)。
|
|
323
|
+
* 成功返回 {tasks, noise, raw};两次解析失败抛错(调用方标记 ANALYZE_FAILED)。
|
|
324
|
+
*/
|
|
325
|
+
async function analyzeMessage(ctx, message, opts = {}) {
|
|
326
|
+
const cfg = ctx.config;
|
|
327
|
+
const roster = store.aggregateRoster(ctx);
|
|
328
|
+
const { tasks: boardTasks } = store.loadTasks(ctx);
|
|
329
|
+
const prompt = buildPrompt(message, roster, boardTasks, cfg);
|
|
330
|
+
|
|
331
|
+
let lastErr = null;
|
|
332
|
+
for (let attempt = 1; attempt <= 2; attempt += 1) {
|
|
333
|
+
let output = null;
|
|
334
|
+
const fx = runHeadlessFixture();
|
|
335
|
+
if (fx !== null) {
|
|
336
|
+
output = fx; // fixture 模式不 spawn
|
|
337
|
+
} else {
|
|
338
|
+
output = await runHeadless(cfg, prompt, { timeoutSec: opts.timeoutSec }); // 引擎级失败(超时/找不到 bin)直接抛,不重试
|
|
339
|
+
}
|
|
340
|
+
try {
|
|
341
|
+
const parsed = extractJSON(output);
|
|
342
|
+
const result = coerceResult(parsed, roster, cfg);
|
|
343
|
+
result.raw = output;
|
|
344
|
+
return result;
|
|
345
|
+
} catch (e) {
|
|
346
|
+
lastErr = e;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
throw Object.assign(new Error(`引擎输出两次解析失败: ${lastErr && lastErr.message}`), { code: 'ANALYZE_PARSE_FAILED' });
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** quote 反查说话人 raw:双向包含(模型引用可能是分段全文的子串,也可能跨段拼接) */
|
|
353
|
+
function speakerRawForQuote(message, quote) {
|
|
354
|
+
if (!quote) return '';
|
|
355
|
+
const seg = (message.speakerSegments || []).find(
|
|
356
|
+
(s) => s.text && (s.text.includes(quote) || quote.includes(s.text))
|
|
357
|
+
);
|
|
358
|
+
return seg ? seg.raw : '';
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* matchSource(v1.5 P1-A):候选卡上的归人依据。
|
|
363
|
+
* 优先取「该候选说话人原文预匹配命中同一成员」的档位(alias/name/gitea);
|
|
364
|
+
* 模型按上下文归人时回退为该成员在聚合名册中的来源档位(gitea/manual→name);
|
|
365
|
+
* 未归人恒 null。
|
|
366
|
+
*/
|
|
367
|
+
function matchSourceFor(t, speakerRaw, roster) {
|
|
368
|
+
if (!t.suggestedMemberId) return null;
|
|
369
|
+
const resolved = speakerRaw ? resolveSpeaker(speakerRaw, roster) : null;
|
|
370
|
+
if (resolved && resolved.id === t.suggestedMemberId) return resolved.source;
|
|
371
|
+
const entry = roster.find((r) => r.id === t.suggestedMemberId);
|
|
372
|
+
return entry ? (entry.source === 'gitea' ? 'gitea' : 'name') : null;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** 候选去重键:任务候选按 消息|引用|标题;更新提案按 消息|update|目标任务|引用 */
|
|
376
|
+
function candidateKey(c) {
|
|
377
|
+
return c.kind === 'update'
|
|
378
|
+
? `${c.messageId}|update|${c.existingTaskId}|${c.quote}`
|
|
379
|
+
: `${c.messageId}|${c.quote}|${c.title}`;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* 把分析结果落盘:候选入池(confidence ≥ 0.5,防重去抖)、noise 记录到消息、
|
|
384
|
+
* 水位 analyzedAt/analyzeState 更新。
|
|
385
|
+
* confidence < 0.5 的项不产出候选(PRD §5),落入消息 noise 留痕。
|
|
386
|
+
* v1.9:result.taskUpdates(更新提案)落为 kind:'update' 候选(带 existingTaskId/quote/note),
|
|
387
|
+
* 不入看板;任务候选显式标 kind:'task'(存量候选无 kind 字段,消费端按 task 容错)。
|
|
388
|
+
* @returns {number} 新增候选数
|
|
389
|
+
*/
|
|
390
|
+
function applyAnalysis(ctx, message, result) {
|
|
391
|
+
const inbox = store.loadInbox(ctx);
|
|
392
|
+
const roster = store.aggregateRoster(ctx); // matchSource 归因依据(P1-A)
|
|
393
|
+
// 重试场景:清掉该消息此前可能残留的 pending 候选(部分成功后被标 FAILED 的脏数据)
|
|
394
|
+
const kept = inbox.filter((c) => !(c.messageId === message.id && c.state === 'pending'));
|
|
395
|
+
const existed = new Set(kept.map(candidateKey));
|
|
396
|
+
const now = new Date().toISOString();
|
|
397
|
+
let added = 0;
|
|
398
|
+
const noise = result.noise.slice();
|
|
399
|
+
|
|
400
|
+
for (const t of result.tasks) {
|
|
401
|
+
if (t.confidence < 0.5) {
|
|
402
|
+
noise.push({ quote: t.quote, reason: t.rationale || `低置信度 ${t.confidence},不产出候选` });
|
|
403
|
+
continue; // PRD:低于 0.5 不产出候选
|
|
404
|
+
}
|
|
405
|
+
const key = `${message.id}|${t.quote}|${t.title}`;
|
|
406
|
+
if (existed.has(key)) continue;
|
|
407
|
+
existed.add(key);
|
|
408
|
+
const speakerRaw = speakerRawForQuote(message, t.quote);
|
|
409
|
+
kept.push({
|
|
410
|
+
id: `cand-${Date.now().toString(36)}-${store.randomToken(4)}-${added}`,
|
|
411
|
+
kind: 'task',
|
|
412
|
+
messageId: message.id,
|
|
413
|
+
messageTitle: message.title,
|
|
414
|
+
occurredAt: message.occurredAt,
|
|
415
|
+
title: t.title,
|
|
416
|
+
desc: t.desc,
|
|
417
|
+
priority: t.priority,
|
|
418
|
+
suggestedMemberId: t.suggestedMemberId,
|
|
419
|
+
matchSource: matchSourceFor(t, speakerRaw, roster), // v1.5 P1-A:归人依据落候选卡
|
|
420
|
+
dueHint: t.dueHint,
|
|
421
|
+
confidence: t.confidence,
|
|
422
|
+
quote: t.quote,
|
|
423
|
+
rationale: t.rationale,
|
|
424
|
+
speakerRaw,
|
|
425
|
+
state: 'pending',
|
|
426
|
+
decidedAt: null,
|
|
427
|
+
createdAt: now,
|
|
428
|
+
});
|
|
429
|
+
added += 1;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// v1.9:任务更新提案 → kind:'update' 候选(确认后写目标任务时间轴,不建新任务)
|
|
433
|
+
for (const u of Array.isArray(result.taskUpdates) ? result.taskUpdates : []) {
|
|
434
|
+
if (u.confidence < 0.5) {
|
|
435
|
+
noise.push({ quote: u.quote, reason: `低置信度 ${u.confidence},不产出更新提案` });
|
|
436
|
+
continue; // 与任务候选同一置信度闸门
|
|
437
|
+
}
|
|
438
|
+
const key = `${message.id}|update|${u.existingTaskId}|${u.quote}`;
|
|
439
|
+
if (existed.has(key)) continue;
|
|
440
|
+
existed.add(key);
|
|
441
|
+
kept.push({
|
|
442
|
+
id: `cand-${Date.now().toString(36)}-${store.randomToken(4)}-${added}`,
|
|
443
|
+
kind: 'update',
|
|
444
|
+
messageId: message.id,
|
|
445
|
+
messageTitle: message.title,
|
|
446
|
+
occurredAt: message.occurredAt,
|
|
447
|
+
existingTaskId: u.existingTaskId,
|
|
448
|
+
quote: u.quote,
|
|
449
|
+
note: u.note,
|
|
450
|
+
confidence: u.confidence,
|
|
451
|
+
speakerRaw: speakerRawForQuote(message, u.quote),
|
|
452
|
+
state: 'pending',
|
|
453
|
+
decidedAt: null,
|
|
454
|
+
createdAt: now,
|
|
455
|
+
});
|
|
456
|
+
added += 1;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
store.saveInbox(ctx, kept);
|
|
460
|
+
|
|
461
|
+
message.analyzedAt = now;
|
|
462
|
+
message.analyzeState = 'ok';
|
|
463
|
+
message.analyzeError = null;
|
|
464
|
+
message.noise = noise;
|
|
465
|
+
return added;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** 标记 ANALYZE_FAILED(可重试:analyzedAt 仍为 null,analyze 端点据此放行重跑) */
|
|
469
|
+
function markFailed(ctx, message, err) {
|
|
470
|
+
message.analyzeState = 'ANALYZE_FAILED';
|
|
471
|
+
message.analyzeError = redact((err && err.message) || String(err)).slice(0, 500);
|
|
472
|
+
message.analyzedAt = null;
|
|
473
|
+
const messages = store.loadMessages(ctx);
|
|
474
|
+
const idx = messages.findIndex((m) => m.id === message.id);
|
|
475
|
+
if (idx !== -1) {
|
|
476
|
+
messages[idx] = message;
|
|
477
|
+
store.saveMessages(ctx, messages);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function persistAnalyzedMessage(ctx, message) {
|
|
482
|
+
const messages = store.loadMessages(ctx);
|
|
483
|
+
const idx = messages.findIndex((m) => m.id === message.id);
|
|
484
|
+
if (idx !== -1) {
|
|
485
|
+
messages[idx] = message;
|
|
486
|
+
store.saveMessages(ctx, messages);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// ------------------------------------------------------------------- routes
|
|
491
|
+
|
|
492
|
+
/** 同一消息并发防重入(两个 job 撞车时后者跳过) */
|
|
493
|
+
const inFlight = new Set();
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* v1.5(goose 验收 P1-B):引擎超时按尝试次数升压——首次按配置值,重试(attempt≥2)
|
|
497
|
+
* 用配置 ×2(升压一次封顶,不指数放大)。job 记录里带 attempt/timeoutSec 可观测。
|
|
498
|
+
*/
|
|
499
|
+
function effectiveTimeoutSec(base, attempt) {
|
|
500
|
+
const b = Number(base) || 300;
|
|
501
|
+
return Number(attempt) > 1 ? b * 2 : b;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function registerAnalyzeRoutes(router, ctx) {
|
|
505
|
+
router.add('POST', '/api/radar/analyze', (c) => {
|
|
506
|
+
const body = c.body || {};
|
|
507
|
+
if (!Array.isArray(body.messageIds) || body.messageIds.length === 0 || body.messageIds.length > 50) {
|
|
508
|
+
throw new HttpError(400, 'FIELD_INVALID', 'messageIds 必填且为 1..50 个消息 id 的数组');
|
|
509
|
+
}
|
|
510
|
+
const messages = store.loadMessages(ctx);
|
|
511
|
+
const byId = new Map(messages.map((m) => [m.id, m]));
|
|
512
|
+
const eligible = [];
|
|
513
|
+
const skipped = [];
|
|
514
|
+
for (const id of body.messageIds) {
|
|
515
|
+
const m = byId.get(String(id));
|
|
516
|
+
if (!m) {
|
|
517
|
+
skipped.push({ messageId: id, reason: 'NOT_FOUND' });
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
if (m.analyzedAt && m.analyzeState !== 'ANALYZE_FAILED') {
|
|
521
|
+
skipped.push({ messageId: id, reason: 'ALREADY_ANALYZED' });
|
|
522
|
+
continue;
|
|
523
|
+
}
|
|
524
|
+
if (inFlight.has(String(id))) {
|
|
525
|
+
skipped.push({ messageId: id, reason: 'ALREADY_RUNNING' });
|
|
526
|
+
continue;
|
|
527
|
+
}
|
|
528
|
+
eligible.push(m);
|
|
529
|
+
}
|
|
530
|
+
const baseTimeout = Number(ctx.config.analyze && ctx.config.analyze.timeoutSec) || 300;
|
|
531
|
+
const job = jobsMod.createJob('analyze', eligible.length);
|
|
532
|
+
(async () => {
|
|
533
|
+
for (const message of eligible) {
|
|
534
|
+
const id = String(message.id);
|
|
535
|
+
inFlight.add(id);
|
|
536
|
+
job.current = message.title;
|
|
537
|
+
// P1-B:尝试次数落消息(成功/失败两条持久化路径都会写回),重试即升压
|
|
538
|
+
const attempt = (Number(message.analyzeAttempts) || 0) + 1;
|
|
539
|
+
message.analyzeAttempts = attempt;
|
|
540
|
+
const timeoutSec = effectiveTimeoutSec(baseTimeout, attempt);
|
|
541
|
+
try {
|
|
542
|
+
const result = await analyzeMessage(ctx, message, { timeoutSec });
|
|
543
|
+
const added = applyAnalysis(ctx, message, result);
|
|
544
|
+
persistAnalyzedMessage(ctx, message);
|
|
545
|
+
job.results.push({ item: id, ok: true, added, attempt, timeoutSec });
|
|
546
|
+
} catch (e) {
|
|
547
|
+
markFailed(ctx, message, e);
|
|
548
|
+
job.results.push({ item: id, ok: false, error: redact((e && e.message) || String(e)), attempt, timeoutSec });
|
|
549
|
+
} finally {
|
|
550
|
+
inFlight.delete(id);
|
|
551
|
+
job.done += 1;
|
|
552
|
+
job.current = null;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
jobsMod.finishJob(job);
|
|
556
|
+
})();
|
|
557
|
+
sendJSON(c.res, 202, {
|
|
558
|
+
jobId: job.id,
|
|
559
|
+
accepted: eligible.length,
|
|
560
|
+
skipped,
|
|
561
|
+
// v1.5 P1-B:带段数供前端预估提示(segmentCount>150 = 长会,可能 2-4 分钟)
|
|
562
|
+
items: eligible.map((m) => ({
|
|
563
|
+
id: String(m.id),
|
|
564
|
+
segmentCount: Array.isArray(m.speakerSegments) ? m.speakerSegments.length : 0,
|
|
565
|
+
})),
|
|
566
|
+
});
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
router.add('GET', '/api/radar/analyze/:id', (c) => {
|
|
570
|
+
const job = jobsMod.getJob(c.params.id);
|
|
571
|
+
if (!job) throw new HttpError(404, 'JOB_NOT_FOUND', '任务不存在或已被滚动清理');
|
|
572
|
+
sendJSON(c.res, 200, jobsMod.jobView(job));
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
// v2.9.28 转写入口泛化:POST /api/radar/text {title, text}——任意文本(非钉钉听记)入纪要池。
|
|
576
|
+
// 只持久化合成消息(source:'text',全文作单段 speakerSegment);分析复用既有 POST analyze
|
|
577
|
+
// {messageIds:[id]}——零管线改动。返回 messageId 后调用方自行触发 analyze(202 job 模式)。
|
|
578
|
+
router.add('POST', '/api/radar/text', (c) => {
|
|
579
|
+
const body = c.body || {};
|
|
580
|
+
const title = typeof body.title === 'string' ? body.title.trim() : '';
|
|
581
|
+
const text = typeof body.text === 'string' ? body.text.trim() : '';
|
|
582
|
+
if (title.length < 1 || title.length > 200) {
|
|
583
|
+
throw new HttpError(400, 'FIELD_INVALID', 'title 必填且为 1..200 字符');
|
|
584
|
+
}
|
|
585
|
+
if (text.length < 10) {
|
|
586
|
+
throw new HttpError(400, 'FIELD_INVALID', 'text 至少 10 字(转写正文)');
|
|
587
|
+
}
|
|
588
|
+
if (text.length > 200000) {
|
|
589
|
+
throw new HttpError(400, 'FIELD_INVALID', 'text 超长(上限 200000 字符)');
|
|
590
|
+
}
|
|
591
|
+
const messages = store.loadMessages(ctx);
|
|
592
|
+
const now = new Date().toISOString();
|
|
593
|
+
const message = {
|
|
594
|
+
id: `msg-text-${Date.now().toString(36)}-${store.randomToken(4)}`,
|
|
595
|
+
title,
|
|
596
|
+
source: 'text', // 纪要池源徽标区分(钉钉听记无此字段)
|
|
597
|
+
occurredAt: now,
|
|
598
|
+
createdAt: now,
|
|
599
|
+
speakerSegments: [{ raw: '文本投喂', text }], // 单段:整篇作无名说话人;transcriptLines 直接可用
|
|
600
|
+
analyzedAt: null,
|
|
601
|
+
analyzeState: null,
|
|
602
|
+
analyzeAttempts: 0,
|
|
603
|
+
noise: [],
|
|
604
|
+
};
|
|
605
|
+
messages.push(message);
|
|
606
|
+
store.saveMessages(ctx, messages);
|
|
607
|
+
sendJSON(c.res, 201, { messageId: message.id, title: message.title, next: `/api/radar/analyze` });
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
module.exports = {
|
|
612
|
+
registerAnalyzeRoutes,
|
|
613
|
+
analyzeMessage,
|
|
614
|
+
applyAnalysis,
|
|
615
|
+
markFailed,
|
|
616
|
+
buildPrompt,
|
|
617
|
+
extractJSON,
|
|
618
|
+
coerceResult,
|
|
619
|
+
mapSpeaker,
|
|
620
|
+
resolveSpeaker,
|
|
621
|
+
effectiveTimeoutSec,
|
|
622
|
+
runHeadless,
|
|
623
|
+
runHeadlessFixture,
|
|
624
|
+
resolveDshBin,
|
|
625
|
+
};
|