dsh-team-bots 0.3.80 → 0.3.98
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/cordis.patch.yml +100 -0
- package/index.js +5 -2
- package/package.json +1 -1
- package/src/api.js +5 -1
- package/src/db.js +52 -0
- package/src/runtime.js +13 -1
- package/src/service.js +5 -0
- package/src/services/approvals.js +7 -2
- package/src/services/bots.js +8 -1
- package/src/services/chat-stores.js +564 -0
- package/src/services/chat.js +60 -28
- package/src/services/core.js +30 -10
- package/src/services/feishu.js +1 -1
- package/src/services/memory.js +14 -12
- package/src/services/model-fallback.js +3 -1
- package/src/services/notify.js +18 -8
- package/src/services/projects.js +25 -19
- package/src/services/questions.js +2 -1
- package/src/services/review-watchdog.js +31 -12
- package/src/services/run-finish.js +17 -13
- package/src/services/runs.js +29 -41
- package/src/services/runtime/context-window.js +6 -4
- package/src/services/runtime/lifecycle.js +1 -0
- package/src/services/runtime/prompt.js +21 -9
- package/src/services/runtime/sandbox-policy.js +1 -1
- package/src/services/runtime/session.js +22 -13
- package/src/services/runtime/trajectory.js +3 -1
- package/src/services/scheduler.js +48 -49
- package/src/services/system.js +9 -7
- package/src/services/task-review.js +27 -19
- package/src/services/task-runs.js +40 -21
- package/src/services/tasks.js +18 -10
- package/src/services/terminal.js +25 -14
- package/src/services/vision-proxy.js +9 -6
- package/src/services/wechat.js +1 -1
- package/src/subagent-ledger.js +23 -6
- package/src/usage-reconcile.js +93 -32
- package/src/usage.js +125 -50
- package/ui/modules/tasks.js +1 -1
- package/ui/styles/cards.css +13 -0
package/src/services/chat.js
CHANGED
|
@@ -18,6 +18,9 @@ export const commandRememberPattern = /^\/remember\s+([\s\S]+)$/u
|
|
|
18
18
|
|
|
19
19
|
export const commandClearPattern = /^\/clear$/u
|
|
20
20
|
|
|
21
|
+
// 交付摘要按行边界截断时,至少要留下预算的一半,否则只剩标题行等于没摘要。
|
|
22
|
+
const CHAT_SUMMARY_MIN_KEPT_RATIO = 0.5
|
|
23
|
+
|
|
21
24
|
export const chatMethods = {
|
|
22
25
|
insertMessage({ projectId, senderType, senderId, body, mentions = [], taskId = null, runId = null, replyToId = null, images = [], createdAt = nowIso() }) {
|
|
23
26
|
const group = this.groupOf(projectId)
|
|
@@ -29,14 +32,16 @@ export const chatMethods = {
|
|
|
29
32
|
if (senderType === 'bot' && !cleanedBody) return null
|
|
30
33
|
// 群消息持久化前过密钥掩码:密文值不进转录,模型下一轮只会看到 $<NAME> 标签(NAME 与一次性 env 文件里的变量名一致)。
|
|
31
34
|
const maskedBody = this.maskSecretValues(cleanedBody)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
+
// 聊天分库(chat-db-split Phase 1):群聊消息正文住该项目自己的聊天库。
|
|
36
|
+
const chatStore = this.groupChatStore(projectId)
|
|
37
|
+
chatStore.run(
|
|
38
|
+
`INSERT INTO messages(id, project_id, group_chat_id, sender_type, sender_id, body, mentions_json, task_id, run_id, reply_to_id, kind, question_id, approval_id, created_at)
|
|
39
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'text', NULL, NULL, ?)`,
|
|
35
40
|
messageId, projectId, group.id, senderType, senderId, maskedBody, JSON.stringify(mentions), taskId, runId, replyToId, createdAt,
|
|
36
41
|
)
|
|
37
42
|
// 图片另存 message_images:正文列保持纯文本,base64 不进 messages.body。
|
|
38
43
|
if (images.length > 0) {
|
|
39
|
-
insertMessageImages(
|
|
44
|
+
insertMessageImages(chatStore, { messageId, images, projectId })
|
|
40
45
|
}
|
|
41
46
|
return messageId
|
|
42
47
|
},
|
|
@@ -51,9 +56,11 @@ export const chatMethods = {
|
|
|
51
56
|
// 引用视图:excerpt 取原消息正文前 120 字。入库正文已过 maskSecretValues,这里不再掩码;
|
|
52
57
|
// bot 历史消息读取时再过一遍标记清洗(幂等),历史上已落库的残片不再显示成乱码。
|
|
53
58
|
// 目标行已被删(/clear 或级联)时 reply_to 置 null,引用悬空不炸视图。
|
|
54
|
-
|
|
59
|
+
// 被引消息与触发消息同项目(chat-db-split Phase 1:同库),projectId 由调用方随行带入。
|
|
60
|
+
replyView(replyToId, projectId = null) {
|
|
55
61
|
if (!replyToId) return null
|
|
56
|
-
const row = this.
|
|
62
|
+
const row = (projectId ? this.groupChatStore(projectId) : this.store)
|
|
63
|
+
.get('SELECT id, sender_type, sender_id, body FROM messages WHERE id = ?', replyToId)
|
|
57
64
|
if (!row) return null
|
|
58
65
|
const excerpt = row.sender_type === 'bot' ? stripLeakedToolMarkup(row.body) : row.body
|
|
59
66
|
return { id: row.id, sender_name: this.senderNameOf(row.sender_type, row.sender_id), excerpt: excerpt.slice(0, 120) }
|
|
@@ -66,23 +73,30 @@ export const chatMethods = {
|
|
|
66
73
|
const senderName = this.senderNameOf(row.sender_type, row.sender_id)
|
|
67
74
|
const task = row.task_id ? this.store.get('SELECT number, title, project_id FROM tasks WHERE id = ?', row.task_id) : undefined
|
|
68
75
|
const project = task ? this.store.get('SELECT key FROM projects WHERE id = ?', task.project_id) : undefined
|
|
76
|
+
// 消息正文与附图住聊天库(chat-db-split Phase 1);任务/项目仍是顶库控制面数据。
|
|
77
|
+
const chatStore = this.groupChatStore(row.project_id)
|
|
69
78
|
return {
|
|
70
79
|
...row,
|
|
71
80
|
body: this.chatBodyView(row),
|
|
72
81
|
mentions: jsonParse(row.mentions_json, []),
|
|
73
82
|
senderName,
|
|
74
|
-
reply_to: this.replyView(row.reply_to_id),
|
|
83
|
+
reply_to: this.replyView(row.reply_to_id, row.project_id),
|
|
75
84
|
taskIdentifier: task && project ? `${project.key}-${task.number}` : null,
|
|
76
85
|
taskTitle: task?.title ?? null,
|
|
77
86
|
// 附图元数据(不含 base64):前端拿 id 再按需取图。
|
|
78
|
-
images: messageImageViews(
|
|
87
|
+
images: messageImageViews(chatStore, row.id),
|
|
79
88
|
}
|
|
80
89
|
},
|
|
81
90
|
// 按 id 取聊天图片(含 base64):前端渲染 <img src="data:..."> 用。
|
|
82
91
|
// 鉴权:仅工作台本地用户可取;bot 一律拒绝(图片是用户上传内容,不是 bot 的可读范围)。
|
|
92
|
+
// 图片随消息分住各聊天库(chat-db-split Phase 1):按库扫一遍找行,库数是个位数、主键查。
|
|
83
93
|
messageImage(imageId, actor = this.actor()) {
|
|
84
94
|
if (actor?.type !== 'user') throw forbidden('仅工作台用户可读取聊天图片')
|
|
85
|
-
|
|
95
|
+
let row = null
|
|
96
|
+
for (const chatStore of this.allChatStores()) {
|
|
97
|
+
row = messageImageRow(chatStore, String(imageId ?? ''))
|
|
98
|
+
if (row) break
|
|
99
|
+
}
|
|
86
100
|
if (!row) throw notFound('图片不存在')
|
|
87
101
|
return {
|
|
88
102
|
id: row.id,
|
|
@@ -141,12 +155,12 @@ export const chatMethods = {
|
|
|
141
155
|
sourceMessageId: messageId,
|
|
142
156
|
postCard: true,
|
|
143
157
|
}, actor)
|
|
144
|
-
this.
|
|
158
|
+
this.groupChatStore(projectId).run('UPDATE messages SET task_id = ? WHERE id = ?', task.id, messageId)
|
|
145
159
|
return { messageId, taskId: task.id }
|
|
146
160
|
}
|
|
147
161
|
// 引用回复:目标消息必须存在于同项目,否则 400。命令路径(/clear、/remember、/task)不走到这里,按规格忽略引用。
|
|
148
162
|
const replyMessage = replyTo
|
|
149
|
-
? this.
|
|
163
|
+
? this.groupChatStore(projectId).get('SELECT id, sender_type, sender_id FROM messages WHERE id = ? AND project_id = ?', String(replyTo), projectId)
|
|
150
164
|
: null
|
|
151
165
|
if (replyTo && !replyMessage) throw badRequest('引用的消息不存在')
|
|
152
166
|
const mentions = parseMentions(body, members)
|
|
@@ -195,8 +209,8 @@ export const chatMethods = {
|
|
|
195
209
|
idempotencyKey: `chat:${messageId}:${bot.id}`,
|
|
196
210
|
})
|
|
197
211
|
// DSHBOTS-112 读图代理:带图消息投递后即评估是否派生转写运行(fire-and-forget,
|
|
198
|
-
// 不阻塞发送;判据与派生见 vision-proxy.js)。
|
|
199
|
-
void considerVisionProxy(this, { bot, message: { id: messageId, project_id: project.id } })
|
|
212
|
+
// 不阻塞发送;判据与派生见 vision-proxy.js)。附图住聊天库(chat-db-split Phase 1)。
|
|
213
|
+
void considerVisionProxy(this, { bot, message: { id: messageId, project_id: project.id }, messageStore: this.groupChatStore(project.id) })
|
|
200
214
|
}
|
|
201
215
|
void this.tick()
|
|
202
216
|
// 环境接单的回传:runBots 带总监供 UI 立思考气泡;真实运行由 1 秒后的接单入列。
|
|
@@ -214,7 +228,7 @@ export const chatMethods = {
|
|
|
214
228
|
try {
|
|
215
229
|
const project = this.store.get("SELECT id FROM projects WHERE id = ? AND status = 'active'", projectId)
|
|
216
230
|
const bot = this.store.get("SELECT id FROM bots WHERE id = ? AND status = 'active'", botId)
|
|
217
|
-
const message = this.
|
|
231
|
+
const message = this.groupChatStore(projectId).get("SELECT id FROM messages WHERE id = ? AND project_id = ?", messageId, projectId)
|
|
218
232
|
if (!project || !bot || !message) return false
|
|
219
233
|
this.enqueueOutbox({
|
|
220
234
|
kind: 'chat_run',
|
|
@@ -234,11 +248,13 @@ export const chatMethods = {
|
|
|
234
248
|
// /clear:清空本群全部聊天记录。任务、记忆等不受影响;操作进审计。
|
|
235
249
|
clearChatHistory(projectId, actor = this.actor()) {
|
|
236
250
|
const project = this.projectRow(projectId)
|
|
237
|
-
|
|
251
|
+
// 聊天正文住项目聊天库(chat-db-split Phase 1):清记录即清该库两张正文表。
|
|
252
|
+
const chatStore = this.groupChatStore(project.id)
|
|
253
|
+
const count = Number(chatStore.get('SELECT COUNT(*) AS count FROM messages WHERE project_id = ?', project.id)?.count ?? 0)
|
|
238
254
|
// 附图行随消息一起清:FK 级联已覆盖,这里再显式删一次,
|
|
239
255
|
// 避免外键开关被临时关掉时留下孤儿图片。
|
|
240
|
-
|
|
241
|
-
|
|
256
|
+
chatStore.run('DELETE FROM message_images WHERE project_id = ?', project.id)
|
|
257
|
+
chatStore.run('DELETE FROM messages WHERE project_id = ?', project.id)
|
|
242
258
|
this.audit(actor, 'chat.clear', 'project', project.id, { cleared: count })
|
|
243
259
|
return count
|
|
244
260
|
},
|
|
@@ -273,13 +289,15 @@ export const chatMethods = {
|
|
|
273
289
|
if (senderType === 'bot' && !cleanedBody) return null
|
|
274
290
|
// 单聊同样过密钥掩码:单聊正文会进模型上下文,明文密钥不该落库。
|
|
275
291
|
const maskedBody = this.maskSecretValues(cleanedBody)
|
|
276
|
-
|
|
292
|
+
// 聊天分库(chat-db-split Phase 1):单聊正文住该 bot 自己的聊天库。
|
|
293
|
+
const chatStore = this.soloChatStore(botId)
|
|
294
|
+
chatStore.run(
|
|
277
295
|
`INSERT INTO solo_messages(id, bot_id, sender_type, sender_id, body, run_id, kind, question_id, approval_id, created_at)
|
|
278
296
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
279
297
|
messageId, botId, senderType, senderId, maskedBody, runId, kind, questionId, approvalId, createdAt,
|
|
280
298
|
)
|
|
281
299
|
if (images.length > 0) {
|
|
282
|
-
insertMessageImages(
|
|
300
|
+
insertMessageImages(chatStore, { messageId, soloMessageId: messageId, images, botId })
|
|
283
301
|
}
|
|
284
302
|
return messageId
|
|
285
303
|
},
|
|
@@ -291,15 +309,17 @@ export const chatMethods = {
|
|
|
291
309
|
...row,
|
|
292
310
|
body: this.chatBodyView(row),
|
|
293
311
|
senderName,
|
|
294
|
-
// 附图元数据(不含 base64):前端拿 id
|
|
295
|
-
images: messageImageViews(this.
|
|
312
|
+
// 附图元数据(不含 base64):前端拿 id 再按需取图;随消息住聊天库。
|
|
313
|
+
images: messageImageViews(this.soloChatStore(row.bot_id), row.id),
|
|
296
314
|
}
|
|
297
315
|
},
|
|
298
316
|
clearSoloHistory(botId, actor = this.actor()) {
|
|
299
317
|
const bot = this.botRow(botId)
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
318
|
+
// 单聊正文住该 bot 聊天库(chat-db-split Phase 1)。
|
|
319
|
+
const chatStore = this.soloChatStore(bot.id)
|
|
320
|
+
const count = Number(chatStore.get('SELECT COUNT(*) AS count FROM solo_messages WHERE bot_id = ?', bot.id)?.count ?? 0)
|
|
321
|
+
chatStore.run('DELETE FROM message_images WHERE bot_id = ?', bot.id)
|
|
322
|
+
chatStore.run('DELETE FROM solo_messages WHERE bot_id = ?', bot.id)
|
|
303
323
|
this.audit(actor, 'chat.solo_clear', 'bot', bot.id, { cleared: count })
|
|
304
324
|
return count
|
|
305
325
|
},
|
|
@@ -359,17 +379,29 @@ export const chatMethods = {
|
|
|
359
379
|
idempotencyKey: `solo_chat:${messageId}`,
|
|
360
380
|
})
|
|
361
381
|
// DSHBOTS-112 读图代理:单聊带图消息同样评估派生(fire-and-forget,见 vision-proxy.js)。
|
|
362
|
-
void considerVisionProxy(this, { bot, message: { id: messageId } })
|
|
382
|
+
void considerVisionProxy(this, { bot, message: { id: messageId }, messageStore: this.soloChatStore(bot.id) })
|
|
363
383
|
void this.tick()
|
|
364
384
|
return { messageId, runBot: bot.id }
|
|
365
385
|
},
|
|
366
386
|
|
|
367
|
-
//
|
|
387
|
+
// 群聊只放结论:保留 markdown 块结构、按上限截断。
|
|
388
|
+
// 换行不能压成空格——标题与列表靠行首判定,整段压成一行后行首的 "#" 会变成整行的
|
|
389
|
+
// ATX 标题标记,群聊里整条报告渲染成一个 h1(一整块大号粗体,ZONGM-165 交付消息实测)。
|
|
368
390
|
chatSummary(output, maxChars = this.config.chatReplyMaxChars) {
|
|
369
|
-
const clean = String(output ?? '')
|
|
391
|
+
const clean = String(output ?? '')
|
|
392
|
+
.replace(/\r\n?/g, '\n')
|
|
393
|
+
.split('\n')
|
|
394
|
+
.map(line => line.replace(/[ \t]+$/, ''))
|
|
395
|
+
.join('\n')
|
|
396
|
+
.replace(/\n{3,}/g, '\n\n')
|
|
397
|
+
.trim()
|
|
370
398
|
if (!clean) return '模型未返回文本。'
|
|
371
399
|
if (clean.length <= maxChars) return clean
|
|
372
|
-
|
|
400
|
+
// 截断优先落在行边界;行边界太靠前(首行就超预算)时硬截,避免只留半句话。
|
|
401
|
+
const cut = clean.slice(0, maxChars)
|
|
402
|
+
const lastBreak = cut.lastIndexOf('\n')
|
|
403
|
+
const kept = lastBreak >= Math.floor(maxChars * CHAT_SUMMARY_MIN_KEPT_RATIO) ? cut.slice(0, lastBreak) : cut
|
|
404
|
+
return `${kept.trimEnd()}…`
|
|
373
405
|
},
|
|
374
406
|
|
|
375
407
|
// ===== Bot 间私信(team_message_bot)=====
|
package/src/services/core.js
CHANGED
|
@@ -39,6 +39,8 @@ export const coreMethods = {
|
|
|
39
39
|
async initialize() {
|
|
40
40
|
if (this.ready) return this.ready
|
|
41
41
|
this.ready = (async () => {
|
|
42
|
+
// 已停机的实例不再初始化:插件卸载会关闭底层库,初始化里的写库动作会撞上已关闭的库。
|
|
43
|
+
if (this.stopped) return
|
|
42
44
|
await ensureDirResilient(this.config.dataDir)
|
|
43
45
|
await ensureDirResilient(this.config.memoryDir)
|
|
44
46
|
await ensureDirResilient(this.config.messageDir)
|
|
@@ -109,10 +111,8 @@ export const coreMethods = {
|
|
|
109
111
|
WHERE status = 'running'`,
|
|
110
112
|
)
|
|
111
113
|
for (const run of interrupted) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
id('evt'), run.id, '宿主重启,运行重新排队', nowIso(),
|
|
115
|
-
)
|
|
114
|
+
// Phase 3:事件住运行归属聊天库(统一收口自动路由)。
|
|
115
|
+
this.insertRunEvent(run.id, 'recovered', '宿主重启,运行重新排队')
|
|
116
116
|
}
|
|
117
117
|
// 审批表与 messages 审批列的迁移由审批域自管(db.js 保持不超 800 行);
|
|
118
118
|
// 同一步顺手把残留 pending 置 timeout——宿主侧请求随进程消失,不可能再被应答。
|
|
@@ -132,11 +132,20 @@ export const coreMethods = {
|
|
|
132
132
|
ensureWechatTriggerSchema(this.store)
|
|
133
133
|
// 外部触发留痕表(B4-②/DSHBOTS-335):GitHub 源的 (source, external_id) 已见集合。
|
|
134
134
|
ensureExternalTriggerSchema(this.store)
|
|
135
|
+
// 聊天分库 Phase 1(docs/plans/chat-db-split.md):messages/solo_messages/
|
|
136
|
+
// message_images/run_trajectory 一次性迁入各聊天库;账本闸保证只跑一次,
|
|
137
|
+
// 崩溃重启续跑。必须在正文读写路径工作前完成。
|
|
138
|
+
this.migrateChatSplitP1()
|
|
139
|
+
this.migrateChatSplitP2()
|
|
140
|
+
this.migrateChatSplitP3()
|
|
135
141
|
// 终端命令随服务进程生死:重启后 running 行已无进程可停,统一置 interrupted,不留僵尸。
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
142
|
+
// Phase 3:terminal_commands 住各聊天库,重启置 interrupted 扇出执行。
|
|
143
|
+
for (const chat of this.allChatStores()) {
|
|
144
|
+
chat.run(
|
|
145
|
+
`UPDATE terminal_commands SET status = 'interrupted', error_message = '服务重启中断', ended_at = ? WHERE status = 'running'`,
|
|
146
|
+
nowIso(),
|
|
147
|
+
)
|
|
148
|
+
}
|
|
140
149
|
// 挂载决策按生效开关(DSHBOTS-334):通道页 settings 显式值 > patch.yml/env 默认。
|
|
141
150
|
if (this.wechatChannelEnabled()) {
|
|
142
151
|
// 微信通道是可选集成,其 vendor 依赖按需加载:缺失或损坏时只降级该通道,
|
|
@@ -410,18 +419,29 @@ export const coreMethods = {
|
|
|
410
419
|
},
|
|
411
420
|
|
|
412
421
|
start() {
|
|
413
|
-
|
|
422
|
+
// 停机不变量(DSHBOTS 热重载):插件卸载会关闭底层库,已停机的实例不得再起拍——
|
|
423
|
+
// 否则延迟首拍会打到已关闭的库,报 "database is not open"。
|
|
424
|
+
if (this.started || this.stopped) return
|
|
414
425
|
this.started = true
|
|
415
426
|
// 周期拍走空载门:空闲时降频为一次索引计数,有工作立即恢复全量拍;
|
|
416
427
|
// 事件唤醒(各入队路径的 void this.tick())不经此门。
|
|
417
428
|
this.timer = setInterval(() => void this.tickIdleGate(), this.config.schedulerIntervalMs)
|
|
418
429
|
this.timer.unref?.()
|
|
419
|
-
|
|
430
|
+
// 延迟首拍单独留句柄:停机时必须摘除,否则它会在库关闭后触发一轮。
|
|
431
|
+
this.bootTimer = setTimeout(() => void this.tick(), 300)
|
|
432
|
+
this.bootTimer.unref?.()
|
|
420
433
|
},
|
|
421
434
|
stop() {
|
|
435
|
+
if (this.stopped) return
|
|
436
|
+
this.stopped = true
|
|
422
437
|
if (this.timer) clearInterval(this.timer)
|
|
423
438
|
this.timer = null
|
|
439
|
+
// 延迟首拍的定时器也要摘除:停机后它再触发就会打到已关闭的库。
|
|
440
|
+
if (this.bootTimer) clearTimeout(this.bootTimer)
|
|
441
|
+
this.bootTimer = null
|
|
424
442
|
this.started = false
|
|
443
|
+
// 聊天库句柄一并关闭(LRU 常驻句柄不留到进程退出)。
|
|
444
|
+
this.closeChatStores?.()
|
|
425
445
|
// 在途审批定时器一并清空:abort 监听由宿主信号持有无法摘除(迟到结算已由
|
|
426
446
|
// settleApproval 容错),但停机后不得再由自身定时器主动触发写库。
|
|
427
447
|
for (const pending of this.approvalPending?.values() ?? []) clearTimeout(pending.timer)
|
package/src/services/feishu.js
CHANGED
|
@@ -136,7 +136,7 @@ export const feishuMethods = {
|
|
|
136
136
|
async feishuDirectorChat(director, body) {
|
|
137
137
|
const adminOuId = this.feishu?.status?.()?.adminOuId ?? null
|
|
138
138
|
const actor = { type: 'user', id: adminOuId ?? 'user:feishu' }
|
|
139
|
-
const firstContact = !this.
|
|
139
|
+
const firstContact = !this.soloChatStore(director.id).get(
|
|
140
140
|
"SELECT id FROM solo_messages WHERE bot_id = ? AND sender_type = 'user' AND sender_id = ? LIMIT 1",
|
|
141
141
|
director.id,
|
|
142
142
|
actor.id,
|
package/src/services/memory.js
CHANGED
|
@@ -55,9 +55,9 @@ export const memoryMethods = {
|
|
|
55
55
|
const raw = String(ref ?? '').trim()
|
|
56
56
|
if (!raw) throw badRequest('缺少要展开的引用 id')
|
|
57
57
|
if (!binding.project_id) {
|
|
58
|
-
//
|
|
58
|
+
// 单聊会话:没有项目上下文,只能展开本会话的消息引用(正文住该 bot 单聊库)。
|
|
59
59
|
if (!raw.startsWith('m_')) throw notFound('单聊会话中只支持展开 m_ 消息引用')
|
|
60
|
-
const message = this.
|
|
60
|
+
const message = this.soloChatStore(binding.bot_id).get(
|
|
61
61
|
'SELECT * FROM solo_messages WHERE bot_id = ? AND substr(id, -6) = ?',
|
|
62
62
|
binding.bot_id, raw.slice(2),
|
|
63
63
|
)
|
|
@@ -67,7 +67,7 @@ export const memoryMethods = {
|
|
|
67
67
|
const project = this.projectRow(binding.project_id)
|
|
68
68
|
|
|
69
69
|
if (raw.startsWith('m_')) {
|
|
70
|
-
const message = this.
|
|
70
|
+
const message = this.groupChatStore(project.id).get(
|
|
71
71
|
'SELECT * FROM messages WHERE project_id = ? AND substr(id, -6) = ?',
|
|
72
72
|
project.id, raw.slice(2),
|
|
73
73
|
)
|
|
@@ -75,12 +75,13 @@ export const memoryMethods = {
|
|
|
75
75
|
return `[${raw}] ${message.sender_type}:${message.sender_id} at ${message.created_at}\n\n${message.body}`
|
|
76
76
|
}
|
|
77
77
|
if (raw.startsWith('c_')) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
78
|
+
// Phase 3:task_comments 住项目聊天库;任务号回顶库 tasks 取。
|
|
79
|
+
const comment = this.groupChatStore(project.id).get(
|
|
80
|
+
'SELECT * FROM task_comments WHERE substr(id, -6) = ?',
|
|
81
|
+
raw.slice(2),
|
|
82
82
|
)
|
|
83
83
|
if (!comment) throw notFound('评论不存在')
|
|
84
|
+
comment.number = this.store.get('SELECT number FROM tasks WHERE id = ?', comment.task_id)?.number
|
|
84
85
|
return `[${raw}] ${project.key}-${comment.number} ${comment.author_type}:${comment.author_id} at ${comment.created_at}\n\n${comment.body}`
|
|
85
86
|
}
|
|
86
87
|
if (raw.startsWith('run_')) {
|
|
@@ -93,11 +94,11 @@ export const memoryMethods = {
|
|
|
93
94
|
`[${raw}] kind=${run.kind} status=${run.status} bot=${run.bot_id}`,
|
|
94
95
|
run.error_code ? `error=${run.error_code}: ${run.error_message}` : '',
|
|
95
96
|
'',
|
|
96
|
-
run
|
|
97
|
+
this.runOutputOf(run) || '(no output)',
|
|
97
98
|
].filter(Boolean).join('\n')
|
|
98
99
|
}
|
|
99
100
|
const task = this.resolveBotTask(project, raw)
|
|
100
|
-
const comments = this.
|
|
101
|
+
const comments = this.groupChatStore(task.project_id).all(
|
|
101
102
|
'SELECT author_type, author_id, body, created_at FROM task_comments WHERE task_id = ? ORDER BY created_at DESC LIMIT 10',
|
|
102
103
|
task.id,
|
|
103
104
|
).reverse()
|
|
@@ -201,7 +202,8 @@ export const memoryMethods = {
|
|
|
201
202
|
}
|
|
202
203
|
throw error
|
|
203
204
|
}
|
|
204
|
-
this.store.
|
|
205
|
+
const reviewRun = this.store.get('SELECT * FROM runs WHERE id = ?', reviewRunId)
|
|
206
|
+
this.runChatStore(reviewRun).run(
|
|
205
207
|
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
206
208
|
id('evt'), reviewRunId, 'queued', '任务交付完成,触发记忆审查', createdAt,
|
|
207
209
|
)
|
|
@@ -215,7 +217,7 @@ export const memoryMethods = {
|
|
|
215
217
|
if (!items.length) {
|
|
216
218
|
// 审查失败或模型没给出可用结论时,回退到规则沉淀,保证任务交付不会颗粒无收。
|
|
217
219
|
if (sourceRun?.task_id) {
|
|
218
|
-
this.
|
|
220
|
+
this.runChatStore(reviewRun).run(
|
|
219
221
|
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
220
222
|
id('evt'), reviewRun.id, 'review_fallback', `记忆审查未产出可用结论,回退规则沉淀:${error?.message ?? '无有效条目'}`, nowIso(),
|
|
221
223
|
)
|
|
@@ -286,7 +288,7 @@ export const memoryMethods = {
|
|
|
286
288
|
const rejectNote = rejected
|
|
287
289
|
? `,拒写 ${rejected} 条${capacityRejected ? `(其中 ${capacityRejected} 条因容量锁被拒)` : ''}:${rejectReasons.join(';')}`
|
|
288
290
|
: ''
|
|
289
|
-
this.
|
|
291
|
+
this.runChatStore(reviewRun).run(
|
|
290
292
|
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
291
293
|
id('evt'), reviewRun.id, 'reviewed', `记忆审查完成,写入 ${applied} 条${deduped ? `,去重 ${deduped} 条` : ''}${rejectNote}`, nowIso(),
|
|
292
294
|
)
|
|
@@ -143,7 +143,9 @@ export const modelFallbackMethods = {
|
|
|
143
143
|
run.trigger_type, run.trigger_ref, attempt, `model_fallback:${run.id}`,
|
|
144
144
|
fallback.provider, fallback.model, fallback.effort, createdAt,
|
|
145
145
|
)
|
|
146
|
-
|
|
146
|
+
// run_events 住运行归属的聊天库(chat-db-split Phase 3):事件挂在原运行 run 名下,
|
|
147
|
+
// 与 runs 插入同回调——UNIQUE 撞车时回调中止,事件不落,与原原子语义一致。
|
|
148
|
+
this.runChatStore(run).run(
|
|
147
149
|
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
148
150
|
id('evt'), run.id, 'model_fallback',
|
|
149
151
|
isQuotaExhausted(error)
|
package/src/services/notify.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// 走它,句柄不可达或目标无绑定会话时降级为群聊流(insertMessage + chat_run)。
|
|
8
8
|
|
|
9
9
|
import { badRequest, notFound } from '../errors.js'
|
|
10
|
-
import { text } from '../util.js'
|
|
10
|
+
import { id, nowIso, text } from '../util.js'
|
|
11
11
|
|
|
12
12
|
// ── 防循环三上限(DSHBOTS-267,配置默认值写死常量,KISS)──────────────────
|
|
13
13
|
// 真实事故背书:伙伴回执淹没主对话、委派续跑近百轮重发已推翻目标。
|
|
@@ -62,7 +62,9 @@ export const notifyMethods = {
|
|
|
62
62
|
// run 归属不到(发起方无活跃运行)时不设限——空闲/人工会话的零星通知无循环风险。
|
|
63
63
|
allowPartnerNotice(runId, targetId) {
|
|
64
64
|
if (!runId || !targetId) return true
|
|
65
|
-
|
|
65
|
+
// run_events 住运行归属的聊天库(chat-db-split Phase 3):按 run 行路由后计数。
|
|
66
|
+
const chatStore = this.runChatStore(this.store.get('SELECT * FROM runs WHERE id = ?', runId))
|
|
67
|
+
const distinctTargets = chatStore.get(
|
|
66
68
|
"SELECT COUNT(DISTINCT message) AS n FROM run_events WHERE run_id = ? AND type = 'partner_notice'",
|
|
67
69
|
runId,
|
|
68
70
|
).n
|
|
@@ -70,7 +72,7 @@ export const notifyMethods = {
|
|
|
70
72
|
this.logger.warn?.('dsh-team: 伙伴提醒超单 run 上限(%s 个目标),静默丢弃:run=%s target=%s', RUN_PARTNER_NOTICE_CAP, runId, targetId)
|
|
71
73
|
return false
|
|
72
74
|
}
|
|
73
|
-
const todayCount =
|
|
75
|
+
const todayCount = chatStore.get(
|
|
74
76
|
"SELECT COUNT(*) AS n FROM run_events WHERE run_id = ? AND type = 'partner_notice' AND message = ? AND date(created_at) = date('now')",
|
|
75
77
|
runId, `partner_notice:${targetId}`,
|
|
76
78
|
).n
|
|
@@ -82,9 +84,14 @@ export const notifyMethods = {
|
|
|
82
84
|
},
|
|
83
85
|
|
|
84
86
|
// 记一条放行的伙伴提醒事件(配额闸通过后调用;chat 唤醒路径共用同一事件流)。
|
|
87
|
+
// 运行行已不在时 runChatStore 回落顶库留痕,不丢审计线索(同 runs.ts 收口口径)。
|
|
85
88
|
recordPartnerNotice(runId, targetId) {
|
|
86
89
|
if (!runId || !targetId) return
|
|
87
|
-
this.
|
|
90
|
+
const run = this.store.get('SELECT * FROM runs WHERE id = ?', runId)
|
|
91
|
+
this.runChatStore(run).run(
|
|
92
|
+
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
93
|
+
id('evt'), runId, 'partner_notice', `partner_notice:${targetId}`, nowIso(),
|
|
94
|
+
)
|
|
88
95
|
},
|
|
89
96
|
|
|
90
97
|
// 滞留提醒投递(DSHBOTS-280):等价 owner 通知通道——与 D-274 死线升级同款
|
|
@@ -95,7 +102,7 @@ export const notifyMethods = {
|
|
|
95
102
|
// notifyMemberFromBot 路径照旧走同一配额流,本扫描路径的防重落点即消息留痕流。
|
|
96
103
|
notifyStaleCardOwner({ project, task, hit }) {
|
|
97
104
|
const identifier = `${project.key}-${task.number}`
|
|
98
|
-
const already = this.
|
|
105
|
+
const already = this.groupChatStore(project.id).get(
|
|
99
106
|
"SELECT id FROM messages WHERE task_id = ? AND sender_type = 'system' AND body LIKE ? AND date(created_at) = date('now') LIMIT 1",
|
|
100
107
|
task.id, `${STALE_CARD_NOTICE_PREFIX}%`,
|
|
101
108
|
)
|
|
@@ -201,7 +208,7 @@ export const notifyMethods = {
|
|
|
201
208
|
// DSHBOTS-267 单跟进者在途幂等:每张任务同时只允许一个跟进提醒在途。
|
|
202
209
|
// 在途判定=最近一条任务提醒落库后,该任务没有启动过新运行(提醒尚未被任何运行消费);
|
|
203
210
|
// 重复提醒静默吸收(不落新留痕、不注入、不计新),warn 留痕。
|
|
204
|
-
const lastNotice = this.
|
|
211
|
+
const lastNotice = this.groupChatStore(task.project_id).get(
|
|
205
212
|
"SELECT created_at FROM messages WHERE task_id = ? AND sender_type = 'bot' AND body LIKE '【任务提醒】%' ORDER BY created_at DESC, rowid DESC LIMIT 1",
|
|
206
213
|
task.id,
|
|
207
214
|
)
|
|
@@ -222,13 +229,16 @@ export const notifyMethods = {
|
|
|
222
229
|
mentions: task.assignee_bot_id ? [task.assignee_bot_id] : [],
|
|
223
230
|
})
|
|
224
231
|
const run = this.store.get(
|
|
225
|
-
"SELECT
|
|
232
|
+
"SELECT * FROM runs WHERE task_id = ? AND status = 'running' AND session_id IS NOT NULL ORDER BY started_at DESC LIMIT 1",
|
|
226
233
|
task.id,
|
|
227
234
|
)
|
|
228
235
|
if (run && typeof this.runtime?.steer === 'function') {
|
|
229
236
|
try {
|
|
230
237
|
await this.runtime.steer(run.session_id, `Task reminder from ${from.name}: ${body}`)
|
|
231
|
-
this.
|
|
238
|
+
this.runChatStore(run).run(
|
|
239
|
+
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
240
|
+
id('evt'), run.id, 'notified', `任务提醒已注入(${identifier})`, nowIso(),
|
|
241
|
+
)
|
|
232
242
|
return `已向任务 ${identifier} 的活跃运行注入提醒(留痕 ${messageId?.slice(-6) ?? '无'})。`
|
|
233
243
|
} catch (error ) {
|
|
234
244
|
this.logger.warn?.('dsh-team: 任务提醒注入失败 %s: %s', identifier, error?.message ?? error)
|
package/src/services/projects.js
CHANGED
|
@@ -29,17 +29,21 @@ export const projectsMethods = {
|
|
|
29
29
|
|
|
30
30
|
// 群聊未读计数(IM 式):以该读者对各项目的已读水位为界,统计其后到达的 bot 群聊消息。
|
|
31
31
|
// 没有水位的项目以项目创建时间为基线,历史消息不会一上来就全红。
|
|
32
|
+
// 聊天正文已分库(chat-db-split Phase 1):按项目聊天库逐库计数,库数是个位数。
|
|
32
33
|
unreadChatCounts() {
|
|
33
|
-
const rows =
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
34
|
+
const rows = []
|
|
35
|
+
for (const project of this.store.all('SELECT id, created_at FROM projects')) {
|
|
36
|
+
const readAt = this.store.get(
|
|
37
|
+
'SELECT read_at FROM chat_reads WHERE project_id = ? AND reader_id = ?',
|
|
38
|
+
project.id, this.localUserId,
|
|
39
|
+
)?.read_at
|
|
40
|
+
const watermark = readAt ?? project.created_at
|
|
41
|
+
const unread = Number(this.groupChatStore(project.id).get(
|
|
42
|
+
"SELECT COUNT(*) AS unread FROM messages WHERE project_id = ? AND sender_type = 'bot' AND created_at > ?",
|
|
43
|
+
project.id, watermark,
|
|
44
|
+
)?.unread ?? 0)
|
|
45
|
+
if (unread > 0) rows.push({ project_id: project.id, unread })
|
|
46
|
+
}
|
|
43
47
|
return new Map(rows.map(row => [row.project_id, Number(row.unread ?? 0)]))
|
|
44
48
|
},
|
|
45
49
|
|
|
@@ -47,7 +51,7 @@ export const projectsMethods = {
|
|
|
47
51
|
markChatRead(projectId, readAt) {
|
|
48
52
|
const stamp = String(readAt ?? '').trim()
|
|
49
53
|
if (!projectId || !stamp) return { ok: false, readAt: '' }
|
|
50
|
-
const latest = this.
|
|
54
|
+
const latest = this.groupChatStore(projectId).get(
|
|
51
55
|
"SELECT MAX(created_at) AS ts FROM messages WHERE project_id = ? AND sender_type = 'bot'",
|
|
52
56
|
projectId,
|
|
53
57
|
)
|
|
@@ -242,7 +246,7 @@ export const projectsMethods = {
|
|
|
242
246
|
"SELECT COUNT(*) AS total, SUM(CASE WHEN status = 'running' THEN 1 ELSE 0 END) AS running FROM runs WHERE project_id = ?",
|
|
243
247
|
project.id,
|
|
244
248
|
)
|
|
245
|
-
const messages = this.
|
|
249
|
+
const messages = this.groupChatStore(project.id).get('SELECT COUNT(*) AS count FROM messages WHERE project_id = ?', project.id)
|
|
246
250
|
return {
|
|
247
251
|
...project,
|
|
248
252
|
workspace_path: this.projectWorkDir(project),
|
|
@@ -509,24 +513,19 @@ export const projectsMethods = {
|
|
|
509
513
|
)
|
|
510
514
|
if (Number(active?.n) > 0) throw badRequest('项目仍有排队或运行中的 run,不能移除追踪')
|
|
511
515
|
const dir = this.projectWorkDir(project)
|
|
516
|
+
// 群聊正文(messages/message_images/run_trajectory,Phase 1)与任务评论/事件、终端命令
|
|
517
|
+
// (Phase 3)住项目聊天库:管理数据清完后整库文件删除即完成正文清理,不再逐表 DELETE。
|
|
512
518
|
this.store.transaction(() => {
|
|
513
519
|
const runIds = 'SELECT id FROM runs WHERE project_id = ?'
|
|
514
520
|
const taskIds = 'SELECT id FROM tasks WHERE project_id = ?'
|
|
515
|
-
// 聊天附图随消息一起清(FK 级联是主路径,这里再显式删一次防孤儿行)。
|
|
516
|
-
this.store.run('DELETE FROM message_images WHERE project_id = ?', projectId)
|
|
517
|
-
this.store.run('DELETE FROM messages WHERE project_id = ?', projectId)
|
|
518
521
|
this.store.run('DELETE FROM bot_messages WHERE project_id = ?', projectId)
|
|
519
|
-
this.store.run(`DELETE FROM run_trajectory WHERE run_id IN (${runIds})`, projectId)
|
|
520
522
|
this.store.run(`DELETE FROM run_events WHERE run_id IN (${runIds})`, projectId)
|
|
521
523
|
this.store.run(`DELETE FROM usage_reconciliation WHERE run_id IN (${runIds})`, projectId)
|
|
522
524
|
this.store.run('DELETE FROM runs WHERE project_id = ?', projectId)
|
|
523
525
|
this.store.run(`DELETE FROM task_labels WHERE task_id IN (${taskIds})`, projectId)
|
|
524
526
|
this.store.run(`DELETE FROM task_dependencies WHERE task_id IN (${taskIds}) OR depends_on_task_id IN (${taskIds})`, projectId, projectId)
|
|
525
|
-
this.store.run(`DELETE FROM task_comments WHERE task_id IN (${taskIds})`, projectId)
|
|
526
|
-
this.store.run(`DELETE FROM task_events WHERE task_id IN (${taskIds})`, projectId)
|
|
527
527
|
this.store.run('DELETE FROM tasks WHERE project_id = ?', projectId)
|
|
528
528
|
this.store.run('DELETE FROM session_bindings WHERE project_id = ?', projectId)
|
|
529
|
-
this.store.run('DELETE FROM terminal_commands WHERE project_id = ?', projectId)
|
|
530
529
|
this.store.run('DELETE FROM outbox WHERE project_id = ?', projectId)
|
|
531
530
|
this.store.run('DELETE FROM group_chats WHERE project_id = ?', projectId)
|
|
532
531
|
this.store.run('DELETE FROM secret_requests WHERE project_id = ?', projectId)
|
|
@@ -542,6 +541,13 @@ export const projectsMethods = {
|
|
|
542
541
|
deleteProductionDir: input.deleteProductionDir === true, dir,
|
|
543
542
|
})
|
|
544
543
|
})
|
|
544
|
+
// 事务提交后再删聊天库文件(含 -wal/-shm):删库不是数据库事务的一部分,
|
|
545
|
+
// 失败只 warn,管理数据已清完,残留文件可人工处理。
|
|
546
|
+
try {
|
|
547
|
+
this.dropGroupChatDb(projectId)
|
|
548
|
+
} catch (error ) {
|
|
549
|
+
this.logger.warn?.(`dsh-team: 移除项目 ${project.key} 聊天库文件失败:${error?.message ?? error}`)
|
|
550
|
+
}
|
|
545
551
|
// 生产目录默认保留;仅显式要求且确认名已匹配时,删解析出的这一个精确路径,不做任何路径推导。
|
|
546
552
|
// 失败只 warn 不回滚(管理数据已按口径清完),并补审计行如实记录。
|
|
547
553
|
if (input.deleteProductionDir === true) {
|
|
@@ -189,7 +189,8 @@ export const questionsMethods = {
|
|
|
189
189
|
})
|
|
190
190
|
} else {
|
|
191
191
|
const group = this.groupOf(project.id)
|
|
192
|
-
|
|
192
|
+
// 卡片锚点住项目聊天库(chat-db-split Phase 1)。
|
|
193
|
+
this.groupChatStore(project.id).run(
|
|
193
194
|
`INSERT INTO messages(id, project_id, group_chat_id, sender_type, sender_id, body, mentions_json, kind, question_id, created_at)
|
|
194
195
|
VALUES (?, ?, ?, 'system', ?, ?, '[]', 'question', ?, ?)`,
|
|
195
196
|
id('msg'), project.id, group?.id ?? null, bot.id, anchorBody, questionId, createdAt,
|