dsh-team-bots 0.2.289 → 0.2.347
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/README.md +60 -122
- package/docs/README.zh.md +61 -123
- package/docs/assets/channel-redesign.png +0 -0
- package/docs/assets/global-config-dark.png +0 -0
- package/docs/assets/global-config-light.png +0 -0
- package/docs/assets/global-config-light.zh.png +0 -0
- package/docs/assets/global-config-redesign.png +0 -0
- package/docs/assets/secrets-redesign.png +0 -0
- package/docs/assets/tasks.png +0 -0
- package/docs/assets/tasks.zh.png +0 -0
- package/docs/assets/usage-redesign-global.png +0 -0
- package/docs/assets/usage.png +0 -0
- package/docs/assets/usage.zh.png +0 -0
- package/package.json +17 -5
- package/scripts/doctor.mjs +4 -4
- package/src/api.js +6 -3
- package/src/botmsg.js +1 -1
- package/src/config.js +18 -8
- package/src/db-migrations.js +76 -9
- package/src/db.js +28 -2
- package/src/http.js +22 -3
- package/src/paths.js +32 -1
- package/src/pricing.js +2 -2
- package/src/runtime.js +1 -1
- package/src/secrets.js +4 -4
- package/src/service.js +14 -5
- package/src/services/approvals.js +109 -24
- package/src/services/backoff.js +34 -0
- package/src/services/bot-quota.js +242 -0
- package/src/services/bots.js +35 -6
- package/src/services/chat.js +15 -9
- package/src/services/core.js +33 -5
- package/src/services/hygiene.js +8 -4
- package/src/services/locale.js +17 -0
- package/src/services/memory.js +112 -1
- package/src/services/message-images.js +3 -3
- package/src/services/notify.js +169 -0
- package/src/services/projects.js +119 -10
- package/src/services/proposals.js +2 -3
- package/src/services/questions.js +75 -32
- package/src/services/review-watchdog.js +261 -30
- package/src/services/run-finish.js +186 -17
- package/src/services/runs.js +42 -13
- package/src/services/runtime/lifecycle.js +3 -1
- package/src/services/runtime/model-restore.js +5 -5
- package/src/services/runtime/prompt.js +106 -45
- package/src/services/runtime/session.js +38 -14
- package/src/services/runtime/trajectory.js +1 -1
- package/src/services/scheduler.js +146 -19
- package/src/services/secrets.js +1 -1
- package/src/services/system.js +23 -6
- package/src/services/task-dispatch.js +315 -0
- package/src/services/task-notify.js +55 -11
- package/src/services/task-runs.js +179 -0
- package/src/services/task-stage.js +4 -0
- package/src/services/tasks.js +51 -399
- package/src/services/terminal.js +2 -2
- package/src/services/vision-proxy.js +3 -3
- package/src/services/wechat.js +228 -7
- package/src/services/workspace-groups.js +4 -3
- package/src/skill-parse.js +1 -1
- package/src/skills.js +1 -1
- package/src/tools.js +275 -6
- package/src/updater.js +52 -0
- package/src/usage-reconcile.js +1 -1
- package/src/usage.js +52 -11
- package/src/wechat/channel.js +14 -20
- package/src/wechat/commands.js +129 -81
- package/src/wechat/format.js +247 -0
- package/src/wechat/store.js +30 -2
- package/ui/app.js +57 -53
- package/ui/i18n/en/chat.js +140 -0
- package/ui/i18n/en/common.js +187 -0
- package/ui/i18n/en/nav.js +89 -0
- package/ui/i18n/en/settings.js +352 -0
- package/ui/i18n/en/tasks.js +229 -0
- package/ui/i18n/en/usage.js +141 -0
- package/ui/i18n/en/wechat.js +23 -0
- package/ui/i18n/en.js +17 -237
- package/ui/i18n/zh/chat.js +140 -0
- package/ui/i18n/zh/common.js +187 -0
- package/ui/i18n/zh/nav.js +89 -0
- package/ui/i18n/zh/settings.js +352 -0
- package/ui/i18n/zh/tasks.js +229 -0
- package/ui/i18n/zh/usage.js +141 -0
- package/ui/i18n/zh/wechat.js +23 -0
- package/ui/i18n/zh.js +17 -236
- package/ui/modules/api.js +4 -3
- package/ui/modules/bot-quota.js +27 -0
- package/ui/modules/cards.js +96 -58
- package/ui/modules/chat.js +28 -30
- package/ui/modules/composer.js +41 -37
- package/ui/modules/dom-morph.js +292 -0
- package/ui/modules/events-board.js +22 -22
- package/ui/modules/events-click.js +100 -68
- package/ui/modules/events-misc.js +34 -34
- package/ui/modules/events-submit.js +28 -28
- package/ui/modules/global-pages.js +125 -116
- package/ui/modules/i18n.js +0 -5
- package/ui/modules/icons.js +0 -1
- package/ui/modules/markdown.js +1 -1
- package/ui/modules/modal-forms.js +196 -203
- package/ui/modules/modal-layer.js +6 -9
- package/ui/modules/page-shell.js +62 -0
- package/ui/modules/settings.js +211 -198
- package/ui/modules/solo-archive.js +0 -4
- package/ui/modules/state.js +57 -50
- package/ui/modules/tasks.js +167 -171
- package/ui/modules/typing-linger.js +1 -4
- package/ui/modules/usage.js +65 -78
- package/ui/modules/util.js +92 -25
- package/ui/styles/avatars.css +14 -0
- package/ui/styles/cards.css +49 -0
- package/ui/styles/chat-cards.css +17 -21
- package/ui/styles/global-config.css +56 -2
- package/ui/styles/modal.css +13 -0
- package/ui/styles/usage.css +3 -61
- package/CONTRIBUTING.md +0 -20
- package/docs/assets/usage-dark.png +0 -0
|
@@ -1,21 +1,56 @@
|
|
|
1
|
-
// src/services/run-finish.
|
|
1
|
+
// src/services/run-finish.ts —— 运行收尾与交付派发域
|
|
2
2
|
// 运行结束后的用量记账、限流退避、attention 落库,以及按运行类别分发的后续动作
|
|
3
3
|
// (任务交付与退回重派、私信回信、群聊/单聊回复、模型备用切换、失败转总监处理)。
|
|
4
|
-
// 本文件只放方法体,方法挂到 TeamService 原型上(见 src/service.
|
|
4
|
+
// 本文件只放方法体,方法挂到 TeamService 原型上(见 src/service.ts 的 defineMethods 调用),
|
|
5
5
|
// 因此方法体内的 this 与拆分前完全一致。
|
|
6
6
|
|
|
7
7
|
import { runUsageTotals } from '../usage.js'
|
|
8
|
-
import { id, nowIso, text } from '../util.js'
|
|
8
|
+
import { id, nowIso, parseMentions, text } from '../util.js'
|
|
9
9
|
import { rolloverSummaryKey } from '../config.js'
|
|
10
10
|
import {
|
|
11
11
|
isQuotaExhausted,
|
|
12
12
|
isRateLimitError,
|
|
13
|
+
isRetryChainError,
|
|
14
|
+
isTransientNetworkError,
|
|
13
15
|
MAX_RATE_LIMIT_RETRIES,
|
|
14
16
|
needsSynthesizedBackoff,
|
|
17
|
+
needsTransientBackoff,
|
|
15
18
|
rateLimitBackoffMs,
|
|
19
|
+
transientBackoffMs,
|
|
16
20
|
} from './backoff.js'
|
|
17
21
|
import { ensureAttentionSchema, normalizeAttention } from './attention-schema.js'
|
|
18
22
|
import { completeVisionRun, maybeSpawnVisionAfterFailure } from './vision-proxy.js'
|
|
23
|
+
import { chatReply } from '../wechat/format.js'
|
|
24
|
+
|
|
25
|
+
// ── 失败终态白名单分类(DSHBOTS-267)──────────────────────────────────────
|
|
26
|
+
// run 失败终态进入群聊/微信回推前,先按结构化信号归入有限白名单类别,对外只透出
|
|
27
|
+
// 「类别+一句话摘要」;原始错误串、文件路径、请求正文不整段透传(完整信息仍落
|
|
28
|
+
// runs.error_message 与 run_events,供 owner 查询)。分类信号复用 D-266 的
|
|
29
|
+
// isTransientNetworkError(TRANSIENT_PATTERN)与既有限流/额度分类,不重复造。
|
|
30
|
+
// 优先级:配额 > 限流 > 沙箱拒绝 > 超时 > 网络 > 内部错误 > 未知。
|
|
31
|
+
const SANDBOX_DENIED_PATTERN = /(EPERM|EACCES|sandbox|file access denied|access denied)/i
|
|
32
|
+
const TIMEOUT_PATTERN = /timed[\s_-]?out|timeout|超时/i
|
|
33
|
+
const INTERNAL_PATTERN = /(internal error|EINTERNAL|INTERNAL_ERROR)/i
|
|
34
|
+
const FAILURE_CATEGORY_NOTICES = {
|
|
35
|
+
quota: { label: '配额', summary: '模型额度耗尽,已按额度策略切换备用或转受阻。' },
|
|
36
|
+
rate_limited: { label: '限流', summary: '上游限流,已按退避策略自动处理。' },
|
|
37
|
+
sandbox_denied: { label: '沙箱拒绝', summary: '文件或命令操作被沙箱策略拒绝。' },
|
|
38
|
+
timeout: { label: '超时', summary: '上游响应超时。' },
|
|
39
|
+
network: { label: '网络', summary: '网络瞬态失败,已按退避策略自动重试。' },
|
|
40
|
+
internal: { label: '内部错误', summary: '服务内部错误。' },
|
|
41
|
+
unknown: { label: '未知', summary: '运行失败,完整原因见运行日志。' },
|
|
42
|
+
}
|
|
43
|
+
export function classifyFailure(error) {
|
|
44
|
+
const raw = `${String(error?.code ?? '')} ${String(error?.message ?? '')}`
|
|
45
|
+
if (isQuotaExhausted(error)) return FAILURE_CATEGORY_NOTICES.quota
|
|
46
|
+
if (isRateLimitError(error)) return FAILURE_CATEGORY_NOTICES.rate_limited
|
|
47
|
+
if (SANDBOX_DENIED_PATTERN.test(raw)) return FAILURE_CATEGORY_NOTICES.sandbox_denied
|
|
48
|
+
if (TIMEOUT_PATTERN.test(raw)) return FAILURE_CATEGORY_NOTICES.timeout
|
|
49
|
+
if (isTransientNetworkError(error)) return FAILURE_CATEGORY_NOTICES.network
|
|
50
|
+
if (INTERNAL_PATTERN.test(raw)) return FAILURE_CATEGORY_NOTICES.internal
|
|
51
|
+
return FAILURE_CATEGORY_NOTICES.unknown
|
|
52
|
+
}
|
|
53
|
+
|
|
19
54
|
|
|
20
55
|
export const runFinishMethods = {
|
|
21
56
|
// 自动核验委派(DSHBOTS-113):交付迁移 in_review 的同一事务内调用。开关关闭或
|
|
@@ -49,7 +84,7 @@ export const runFinishMethods = {
|
|
|
49
84
|
})
|
|
50
85
|
return true
|
|
51
86
|
},
|
|
52
|
-
async finishRun({ runId, status, output, error, attention }) {
|
|
87
|
+
async finishRun({ runId, status, output, error, attention } ) {
|
|
53
88
|
const run = this.runRow(runId)
|
|
54
89
|
if (!['running', 'queued'].includes(run.status)) return
|
|
55
90
|
ensureAttentionSchema(this.store)
|
|
@@ -67,7 +102,14 @@ export const runFinishMethods = {
|
|
|
67
102
|
// DSHBOTS-99:额度耗尽(已达到限额)不合成指数退避——额度窗口用尽,退避重打撞同一堵墙。
|
|
68
103
|
const attemptNo = Math.max(1, Number(run.attempt) || 1)
|
|
69
104
|
const quotaExhausted = isQuotaExhausted(error)
|
|
70
|
-
|
|
105
|
+
// DSHBOTS-266:网络瞬态失败(ECONNRESET/5xx/408 等过性错误)同判合成退避,
|
|
106
|
+
// 与限流共用同一 retry_after_ms 落库与 plan_wait 派生链;真 Retry-After 仍优先。
|
|
107
|
+
const transientBackoff = needsTransientBackoff(error)
|
|
108
|
+
const synthesizedMs = needsSynthesizedBackoff(error)
|
|
109
|
+
? rateLimitBackoffMs(attemptNo)
|
|
110
|
+
: transientBackoff
|
|
111
|
+
? transientBackoffMs(attemptNo)
|
|
112
|
+
: null
|
|
71
113
|
// 额度耗尽时 provider 真给的重置时间照样保留(信息不丢,供 UI 显示额度恢复时间),
|
|
72
114
|
// 但它不再触发对主模型的自动重试(见下方失败分支与 scheduler.retryExpiredPlanWaits)。
|
|
73
115
|
const retryAfterMs = Number(error?.retryAfterMs) > 0 ? Number(error.retryAfterMs) : synthesizedMs
|
|
@@ -83,6 +125,30 @@ export const runFinishMethods = {
|
|
|
83
125
|
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
84
126
|
id('evt'), runId, status, error?.message ?? (status === 'succeeded' ? '完成' : ''), endedAt,
|
|
85
127
|
)
|
|
128
|
+
// DSHBOTS-241:撞 provider 额度墙时记录该 bot 的额度状态(含恢复时间),
|
|
129
|
+
// 供派发前路由(耗尽期走备用模型/排队)与 UI 手动解除;审计 bot.quota_exhausted。
|
|
130
|
+
// DSHBOTS-246:事件与状态记录同条件——只有真撞墙才写「额度耗尽」,成功运行或
|
|
131
|
+
// 非额度失败一律不记,否则每个 run 收尾都污染一条额度耗尽时间线。
|
|
132
|
+
if (quotaExhausted && run.bot_id && bot) {
|
|
133
|
+
const effectiveProvider = run.model_provider || bot.provider
|
|
134
|
+
if (effectiveProvider) {
|
|
135
|
+
const quotaRecorded = this.recordBotQuotaExhaustion({
|
|
136
|
+
botId: run.bot_id,
|
|
137
|
+
provider: effectiveProvider,
|
|
138
|
+
errorCode: error?.code ?? null,
|
|
139
|
+
errorMessage: error?.message ?? '',
|
|
140
|
+
retryAfterMs: Number(error?.retryAfterMs) > 0 ? Number(error.retryAfterMs) : null,
|
|
141
|
+
})
|
|
142
|
+
if (quotaRecorded) {
|
|
143
|
+
const resetNote = quotaRecorded.resetAt ? `,预计 ${quotaRecorded.resetAt} 恢复` : ''
|
|
144
|
+
this.insertRunEvent(
|
|
145
|
+
runId,
|
|
146
|
+
'quota_exhausted',
|
|
147
|
+
`额度耗尽(${quotaRecorded.provider})${resetNote},耗尽期内新任务改走备用模型或排队等待`,
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
86
152
|
// DSHBOTS-172 代码卫生:每轮运行结束扫一次项目工作区行数约定,超标即发系统指令告警
|
|
87
153
|
// (去重与阈值见 hygiene.js);扫描自身全量兜错,不阻塞收尾主流程。
|
|
88
154
|
this.runCodeHygieneScan(project, runId)
|
|
@@ -95,8 +161,10 @@ export const runFinishMethods = {
|
|
|
95
161
|
const suffix = attemptNo < MAX_RATE_LIMIT_RETRIES ? '后重试' : '后转总监处理'
|
|
96
162
|
this.insertRunEvent(
|
|
97
163
|
runId,
|
|
98
|
-
'rate_limit_backoff',
|
|
99
|
-
|
|
164
|
+
transientBackoff ? 'transient_backoff' : 'rate_limit_backoff',
|
|
165
|
+
transientBackoff
|
|
166
|
+
? `网络瞬态失败(${String(error?.code ?? error?.message ?? '未知错误').slice(0, 60)}),第 ${attemptNo} 次退避,约 ${Math.round(synthesizedMs / 1000)} 秒${suffix}`
|
|
167
|
+
: `上游限流(${error?.code})未返回 Retry-After,第 ${attemptNo} 次退避,约 ${Math.round(synthesizedMs / 1000)} 秒${suffix}`,
|
|
100
168
|
)
|
|
101
169
|
}
|
|
102
170
|
// 结构化提问(attention):只在 bot 显式给出时落库,规范超长/超量后写入;
|
|
@@ -188,7 +256,8 @@ export const runFinishMethods = {
|
|
|
188
256
|
senderId: bot.id,
|
|
189
257
|
body: status === 'succeeded'
|
|
190
258
|
? (cleanOutput || `总监已完成${label}。`)
|
|
191
|
-
|
|
259
|
+
// DSHBOTS-267:失败只透出白名单类别+摘要,不透传原始错误串。
|
|
260
|
+
: `总监${label}运行失败(${classifyFailure(error).label}):${classifyFailure(error).summary}`,
|
|
192
261
|
taskId: run.task_id,
|
|
193
262
|
runId,
|
|
194
263
|
})
|
|
@@ -225,7 +294,7 @@ export const runFinishMethods = {
|
|
|
225
294
|
projectId: project.id,
|
|
226
295
|
senderType: 'bot',
|
|
227
296
|
senderId: bot.id,
|
|
228
|
-
body: `总监${label}
|
|
297
|
+
body: `总监${label}运行失败(${classifyFailure(error).label}):${classifyFailure(error).summary}`,
|
|
229
298
|
runId,
|
|
230
299
|
})
|
|
231
300
|
}
|
|
@@ -296,25 +365,35 @@ export const runFinishMethods = {
|
|
|
296
365
|
}
|
|
297
366
|
}
|
|
298
367
|
} else if (run.kind === 'chat') {
|
|
299
|
-
this.insertMessage({
|
|
368
|
+
const replyMessageId = this.insertMessage({
|
|
300
369
|
projectId: project.id,
|
|
301
370
|
senderType: 'bot',
|
|
302
371
|
senderId: bot.id,
|
|
303
372
|
body: cleanOutput || '模型未返回文本。',
|
|
304
373
|
runId,
|
|
305
374
|
})
|
|
375
|
+
// DSHBOTS-252:chat 运行成功回复正文与用户消息同语义解析 @mention 并派发 chat_run
|
|
376
|
+
// 唤醒目标 bot。触发面仅此一处(验收/受阻摘要、失败提示、通知、任务卡、solo/dm
|
|
377
|
+
// 落库一律不触发);剥空不落库的回复(insertMessage 返回 null)同样不派发。
|
|
378
|
+
const wokenBotIds = this.dispatchBotMentionRuns(project, run, bot, replyMessageId)
|
|
379
|
+
if (wokenBotIds.length > 0) void this.tick()
|
|
380
|
+
this.notifyWechatChat(bot, run, cleanOutput || '模型未返回文本。')
|
|
306
381
|
// 群聊回复后的自动记忆审查(可在设置关闭;与任务交付审查同一队列同一幂等键规则)。
|
|
307
382
|
if (this.chatMemoryReviewEnabled()) {
|
|
308
383
|
this.enqueueMemoryReview(project, bot, null, runId)
|
|
309
384
|
}
|
|
310
385
|
} else if (run.kind === 'solo_chat') {
|
|
386
|
+
const reply = cleanOutput || '模型未返回文本。'
|
|
311
387
|
this.insertSoloMessage({
|
|
312
388
|
botId: bot.id,
|
|
313
389
|
senderType: 'bot',
|
|
314
390
|
senderId: bot.id,
|
|
315
|
-
body:
|
|
391
|
+
body: reply,
|
|
316
392
|
runId,
|
|
317
393
|
})
|
|
394
|
+
// DSHBOTS-258:微信通道投的项目外 director 单聊(trigger_type='wechat'),
|
|
395
|
+
// 回复同样推回微信管理员;普通 UI 单聊(mention)不推。
|
|
396
|
+
this.notifyWechatChat(bot, run, reply)
|
|
318
397
|
}
|
|
319
398
|
} else {
|
|
320
399
|
// DSHBOTS-112:无视觉模型收图失败的兜底信号——错误表明模型无视觉且消息带图时补派生
|
|
@@ -326,7 +405,9 @@ export const runFinishMethods = {
|
|
|
326
405
|
// 禁止立刻重打上游——不切备用模型(备用可能走同一网关),也不立刻排总监处理运行。
|
|
327
406
|
// DSHBOTS-99:额度耗尽(已达到限额)不走这条路径——不指数退避、无条件先试备用模型;
|
|
328
407
|
// 瞬时限流的等待语义原样保留。
|
|
329
|
-
|
|
408
|
+
// DSHBOTS-266:网络瞬态失败(ECONNRESET/5xx/408 等)进同一退避链——等待期内同样
|
|
409
|
+
// 不切备用不排 handle,plan_retry 到期派生接管重试;attempt≤3,第 4 次照常转 blocked。
|
|
410
|
+
const backoffWaitMs = isRetryChainError(error) && retryAfterMs > 0 ? retryAfterMs : 0
|
|
330
411
|
// 额度耗尽不做自动重试:重置时间只当信息留着,不据此重打主模型。
|
|
331
412
|
const willRetry = !quotaExhausted && backoffWaitMs > 0 && attemptNo < MAX_RATE_LIMIT_RETRIES
|
|
332
413
|
// 主 provider 不可用:临时切备用模型继续跑这条任务。派生成功后任务不置 blocked、
|
|
@@ -366,16 +447,22 @@ export const runFinishMethods = {
|
|
|
366
447
|
const quotaNote = quotaExhausted
|
|
367
448
|
? `\n已达限额(${error?.code ?? '未知错误'}),主备模型均不可用,任务转为受阻${quotaResetNote}。`
|
|
368
449
|
: ''
|
|
450
|
+
// 退避提示按失败类别措辞:瞬态抖动与限流都是「退避后自动重试」,不混用限流文案。
|
|
451
|
+
const backoffKind = isTransientNetworkError(error) ? '网络瞬态失败' : '上游限流'
|
|
452
|
+
const backoffVerb = isTransientNetworkError(error) ? '退避' : '指数退避'
|
|
369
453
|
const backoffNote = backoffWaitMs > 0
|
|
370
454
|
? (willRetry
|
|
371
|
-
? `\n
|
|
372
|
-
: `\n
|
|
455
|
+
? `\n${backoffKind},第 ${attemptNo} 次${backoffVerb},约 ${Math.round(backoffWaitMs / 1000)} 秒后自动重试。`
|
|
456
|
+
: `\n${backoffKind},已达自动重试上限,约 ${Math.round(backoffWaitMs / 1000)} 秒后转总监处理。`)
|
|
373
457
|
: ''
|
|
458
|
+
// DSHBOTS-267:失败只透出白名单类别+摘要;原始错误串不进群聊(完整信息已落
|
|
459
|
+
// runs.error_message 与 run_events)。quotaNote/backoffNote 仅含结构化错误码。
|
|
460
|
+
const failure = classifyFailure(error)
|
|
374
461
|
this.insertMessage({
|
|
375
462
|
projectId: project.id,
|
|
376
463
|
senderType: 'bot',
|
|
377
464
|
senderId: bot.id,
|
|
378
|
-
body:
|
|
465
|
+
body: `任务执行失败(${failure.label}):${failure.summary}\n运行 ID:${runId}${quotaNote}${backoffNote}`,
|
|
379
466
|
taskId: run.task_id,
|
|
380
467
|
runId,
|
|
381
468
|
})
|
|
@@ -393,23 +480,105 @@ export const runFinishMethods = {
|
|
|
393
480
|
})
|
|
394
481
|
}
|
|
395
482
|
} else if (run.kind === 'chat') {
|
|
483
|
+
// DSHBOTS-267:群聊与微信回推共用同一份分类文案,不含原始错误串。
|
|
484
|
+
const failureBody = `运行失败(${classifyFailure(error).label}):${classifyFailure(error).summary}`
|
|
396
485
|
this.insertMessage({
|
|
397
486
|
projectId: project.id,
|
|
398
487
|
senderType: 'bot',
|
|
399
488
|
senderId: bot.id,
|
|
400
|
-
body:
|
|
489
|
+
body: failureBody,
|
|
401
490
|
runId,
|
|
402
491
|
})
|
|
492
|
+
this.notifyWechatChat(bot, run, failureBody, true)
|
|
403
493
|
} else if (run.kind === 'solo_chat') {
|
|
494
|
+
const failureBody = `运行失败(${classifyFailure(error).label}):${classifyFailure(error).summary}`
|
|
404
495
|
this.insertSoloMessage({
|
|
405
496
|
botId: bot.id,
|
|
406
497
|
senderType: 'bot',
|
|
407
498
|
senderId: bot.id,
|
|
408
|
-
body:
|
|
499
|
+
body: failureBody,
|
|
409
500
|
runId,
|
|
410
501
|
})
|
|
502
|
+
// DSHBOTS-258:微信通道的 solo 直达失败提示同样推回(notifyWechatChat 内部按
|
|
503
|
+
// trigger_type='wechat' 门禁,普通 UI 单聊不推)。
|
|
504
|
+
this.notifyWechatChat(bot, run, failureBody, true)
|
|
411
505
|
}
|
|
412
506
|
}
|
|
413
507
|
if (run.project_id && this.rate[run.project_id]) delete this.rate[run.project_id]
|
|
414
508
|
},
|
|
509
|
+
|
|
510
|
+
// 微信直达对话回程(DSHBOTS-208/258):trigger_type='wechat' 的运行(项目群聊直达、
|
|
511
|
+
// 项目外 director 专属工作区 solo 直达)结束时,把 bot 回复(或失败提示)推回微信管理员,出站走通道既有合批/重试治理;
|
|
512
|
+
// 只推微信触发的运行,工作台群聊回复不受影响。推送失败不阻塞收尾主流程。
|
|
513
|
+
// 排版(头部标记/分隔线)集中在 wechat/format.js 的 chatReply(DSHBOTS-211)。
|
|
514
|
+
notifyWechatChat(bot, run, body, failed = false) {
|
|
515
|
+
if (run?.trigger_type !== 'wechat' || !this.wechat) return
|
|
516
|
+
try {
|
|
517
|
+
this.wechat.notify(chatReply(bot?.name ?? 'Bot', body, failed))
|
|
518
|
+
} catch (error ) {
|
|
519
|
+
this.logger.warn?.('dsh-team: 微信直达回复推送失败: %s', error?.message ?? error)
|
|
520
|
+
}
|
|
521
|
+
},
|
|
522
|
+
|
|
523
|
+
// ── Bot 群聊回复 mention 唤醒(DSHBOTS-252)────────────────────────────
|
|
524
|
+
// 与用户消息同语义(chat.ts sendMessage):@xxx 用 parseMentions 解析 active 成员、
|
|
525
|
+
// 显式 @ 优先(bot 路径无引用兜底道)、目标 slice(0,3);派发走既有 chat_run outbox →
|
|
526
|
+
// enqueueChatRun,串行车道沿用 D-219 的 project lane(sessionLaneKey 对 kind='chat'
|
|
527
|
+
// 恒为 project:<bot>:<project>),不新建串行道。防循环三护栏:
|
|
528
|
+
// ① 禁自唤醒——解析结果剔除回复 bot 自身;
|
|
529
|
+
// ② 链路深度 ≤2——chatMentionChainDepth 回溯来源链,本回复链深达 2 即不再派发;
|
|
530
|
+
// ③ 任务卡双保险——taskId 非空的消息不参与派发(群聊 messages 表无 kind 列,
|
|
531
|
+
// 「kind 恒为 text」由结构保证;带 kind 的 solo 落库点不触发本派发)。
|
|
532
|
+
chatMentionChainDepth(run) {
|
|
533
|
+
let depth = 0
|
|
534
|
+
let ref = run?.trigger_ref
|
|
535
|
+
const seen = new Set()
|
|
536
|
+
while (depth < 2 && ref && !seen.has(ref)) {
|
|
537
|
+
seen.add(ref)
|
|
538
|
+
const message = this.store.get('SELECT sender_type, run_id FROM messages WHERE id = ?', ref)
|
|
539
|
+
// 人发消息与结构化 bot 落库(notify 等无 run_id 的 bot 消息)都是链根,回溯到此为止。
|
|
540
|
+
if (!message || message.sender_type !== 'bot' || !message.run_id) break
|
|
541
|
+
const parent = this.store.get('SELECT kind, trigger_ref FROM runs WHERE id = ?', message.run_id)
|
|
542
|
+
if (!parent || parent.kind !== 'chat' || !parent.trigger_ref) break
|
|
543
|
+
depth += 1
|
|
544
|
+
ref = parent.trigger_ref
|
|
545
|
+
}
|
|
546
|
+
return depth
|
|
547
|
+
},
|
|
548
|
+
|
|
549
|
+
// 对 bot 回复正文解析 @mention 并派发 chat_run;返回被唤醒的 bot id 列表。
|
|
550
|
+
// 解析对象是落库行正文(已过剥残片+密钥掩码两道清洗),与所有成员读到的转录一致。
|
|
551
|
+
dispatchBotMentionRuns(project, run, bot, messageId) {
|
|
552
|
+
if (!messageId) return []
|
|
553
|
+
// 护栏三双保险:唯一调用点不带 taskId,这里按落库行复核,兜未来调用面扩张。
|
|
554
|
+
const message = this.store.get('SELECT id, task_id, body FROM messages WHERE id = ?', messageId)
|
|
555
|
+
if (!message || message.task_id) return []
|
|
556
|
+
const members = this.membersOf(project.id).filter(member => member.status === 'active')
|
|
557
|
+
// 护栏一:禁自唤醒——剔除回复 bot 自身。
|
|
558
|
+
const targets = parseMentions(message.body, members).filter(member => member.id !== bot.id)
|
|
559
|
+
if (targets.length === 0) return []
|
|
560
|
+
// mentions_json 记录过滤后的全部 @ 目标(对齐用户路径的 UI mention 渲染口径),
|
|
561
|
+
// 与实际派发数(slice(0,3))允许在 >3 目标时不同。
|
|
562
|
+
this.store.run(
|
|
563
|
+
'UPDATE messages SET mentions_json = ? WHERE id = ?',
|
|
564
|
+
JSON.stringify(targets.map(member => member.id)), messageId,
|
|
565
|
+
)
|
|
566
|
+
// 护栏二:链路深度 ≤2——深度达 2 的 bot 回复不再派发。
|
|
567
|
+
if (this.chatMentionChainDepth(run) >= 2) return []
|
|
568
|
+
// DSHBOTS-267 防循环上限(chat 路径):每次 @ 唤醒即一条伙伴提醒,与 notify 路径
|
|
569
|
+
// 共用同一 run_events 配额流;超限目标静默丢弃(allowPartnerNotice 内 warn 留痕)。
|
|
570
|
+
const dispatched = targets.slice(0, 3).filter(target => this.allowPartnerNotice(run.id, target.id))
|
|
571
|
+
for (const target of dispatched) {
|
|
572
|
+
this.recordPartnerNotice(run.id, target.id)
|
|
573
|
+
this.enqueueOutbox({
|
|
574
|
+
kind: 'chat_run',
|
|
575
|
+
payload: { projectId: project.id, botId: target.id, messageId, origin: null },
|
|
576
|
+
projectId: project.id,
|
|
577
|
+
sourceId: messageId,
|
|
578
|
+
// 幂等键与用户路径(chat:<id>:<bot>)前缀区分,outbox 一眼可辨唤醒来源。
|
|
579
|
+
idempotencyKey: `chat_bot:${messageId}:${target.id}`,
|
|
580
|
+
})
|
|
581
|
+
}
|
|
582
|
+
return dispatched.map(member => member.id)
|
|
583
|
+
},
|
|
415
584
|
}
|
package/src/services/runs.js
CHANGED
|
@@ -96,11 +96,11 @@ export const runsMethods = {
|
|
|
96
96
|
const kind = message.origin_kind === 'task' ? 'task' : message.origin_kind === 'dm' ? 'dm' : 'project'
|
|
97
97
|
const binding = message.origin_kind === 'task' && message.origin_task_id
|
|
98
98
|
? this.store.get(
|
|
99
|
-
"SELECT * FROM session_bindings WHERE bot_id = ? AND project_id = ? AND kind = 'task' AND task_id = ?",
|
|
99
|
+
"SELECT * FROM session_bindings WHERE bot_id = ? AND project_id = ? AND kind = 'task' AND task_id = ? AND alias = 0",
|
|
100
100
|
caller.id, message.project_id, message.origin_task_id,
|
|
101
101
|
)
|
|
102
102
|
: this.store.get(
|
|
103
|
-
'SELECT * FROM session_bindings WHERE bot_id = ? AND project_id = ? AND kind = ?',
|
|
103
|
+
'SELECT * FROM session_bindings WHERE bot_id = ? AND project_id = ? AND kind = ? AND alias = 0',
|
|
104
104
|
caller.id, message.project_id, kind,
|
|
105
105
|
)
|
|
106
106
|
// 无绑定(会话被轮换清理或首次私信):走 dm_reply 运行,由 startRun 重建会话。
|
|
@@ -110,7 +110,7 @@ export const runsMethods = {
|
|
|
110
110
|
} else {
|
|
111
111
|
try {
|
|
112
112
|
plan = deliveryPlan({ row: message, callerSessionBusy: this.runtime.sessionBusy(binding.session_id) })
|
|
113
|
-
} catch (error) {
|
|
113
|
+
} catch (error ) {
|
|
114
114
|
this.logger.warn?.('dsh-team: 私信 %s 投递计划失败:%s', message.id, error?.message ?? error)
|
|
115
115
|
return null
|
|
116
116
|
}
|
|
@@ -142,7 +142,9 @@ export const runsMethods = {
|
|
|
142
142
|
this.insertRunEvent(runId, 'queued', '私信回信排队投递给发送方会话')
|
|
143
143
|
return runId
|
|
144
144
|
},
|
|
145
|
-
|
|
145
|
+
// triggerType(DSHBOTS-258):'wechat' 表示消息来自微信通道(项目外 director 专属工作区),
|
|
146
|
+
// 与群聊直达(enqueueChatRun 的 origin 口径)同语义——运行收尾据此把 solo 回复推回微信管理员。
|
|
147
|
+
enqueueSoloChatRun(bot, messageId, { triggerType = 'mention' } = {}) {
|
|
146
148
|
const existing = this.store.get(
|
|
147
149
|
"SELECT id FROM runs WHERE trigger_ref = ? AND bot_id = ? AND kind = 'solo_chat'",
|
|
148
150
|
messageId,
|
|
@@ -154,16 +156,18 @@ export const runsMethods = {
|
|
|
154
156
|
this.store.run(
|
|
155
157
|
`INSERT INTO runs(id, project_id, bot_id, kind, mode, trigger_type, trigger_ref, status,
|
|
156
158
|
attempt, idempotency_key, queued_at)
|
|
157
|
-
VALUES (?, NULL, ?, 'solo_chat', 'write',
|
|
158
|
-
runId, bot.id, messageId, `solo_chat:${messageId}`, createdAt,
|
|
159
|
+
VALUES (?, NULL, ?, 'solo_chat', 'write', ?, ?, 'queued', 1, ?, ?)`,
|
|
160
|
+
runId, bot.id, triggerType, messageId, `solo_chat:${messageId}`, createdAt,
|
|
159
161
|
)
|
|
160
162
|
this.store.run(
|
|
161
163
|
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
162
|
-
id('evt'), runId, 'queued',
|
|
164
|
+
id('evt'), runId, 'queued',
|
|
165
|
+
triggerType === 'wechat' ? '微信消息触发(项目外单聊直达)' : '单聊消息触发',
|
|
166
|
+
createdAt,
|
|
163
167
|
)
|
|
164
168
|
return runId
|
|
165
169
|
},
|
|
166
|
-
enqueueChatRun(project, bot, messageId) {
|
|
170
|
+
enqueueChatRun(project, bot, messageId, origin = null) {
|
|
167
171
|
if (bot.status !== 'active') return null
|
|
168
172
|
const existing = this.store.get(
|
|
169
173
|
"SELECT id FROM runs WHERE trigger_ref = ? AND bot_id = ? AND kind = 'chat'",
|
|
@@ -173,15 +177,17 @@ export const runsMethods = {
|
|
|
173
177
|
if (existing) return existing.id
|
|
174
178
|
const runId = id('run')
|
|
175
179
|
const createdAt = nowIso()
|
|
180
|
+
// 微信直达(DSHBOTS-208):trigger_type 记 'wechat',运行收尾据此把 bot 回复推回微信管理员。
|
|
181
|
+
const triggerType = origin === 'wechat' ? 'wechat' : 'mention'
|
|
176
182
|
this.store.run(
|
|
177
183
|
`INSERT INTO runs(id, project_id, bot_id, kind, mode, trigger_type, trigger_ref, status,
|
|
178
184
|
attempt, idempotency_key, queued_at)
|
|
179
|
-
VALUES (?, ?, ?, 'chat', 'read',
|
|
180
|
-
runId, project.id, bot.id, messageId, `chat:${messageId}:${bot.id}`, createdAt,
|
|
185
|
+
VALUES (?, ?, ?, 'chat', 'read', ?, ?, 'queued', 1, ?, ?)`,
|
|
186
|
+
runId, project.id, bot.id, triggerType, messageId, `chat:${messageId}:${bot.id}`, createdAt,
|
|
181
187
|
)
|
|
182
188
|
this.store.run(
|
|
183
189
|
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
184
|
-
id('evt'), runId, 'queued', '群聊消息触发', createdAt,
|
|
190
|
+
id('evt'), runId, 'queued', triggerType === 'wechat' ? '微信群聊消息触发(直达)' : '群聊消息触发', createdAt,
|
|
185
191
|
)
|
|
186
192
|
return runId
|
|
187
193
|
},
|
|
@@ -297,6 +303,8 @@ export const runsMethods = {
|
|
|
297
303
|
: (run.kind === 'solo_chat' || (run.kind === 'vision' && !run.project_id)) && run.trigger_ref
|
|
298
304
|
? this.store.get('SELECT * FROM solo_messages WHERE id = ?', run.trigger_ref)
|
|
299
305
|
: null
|
|
306
|
+
// 合批消息(DSHBOTS-231):领取时合并进来的同通道排队消息,与触发消息同一轮注入。
|
|
307
|
+
const extraMessages = this.mergedMessagesFor(run)
|
|
300
308
|
if (run.mode === 'read') this.fingerprints.set(runId, this.workspaceFingerprint(project))
|
|
301
309
|
const sessionKind = this.sessionKindFor(run)
|
|
302
310
|
await this.runtime.startRun({
|
|
@@ -305,9 +313,10 @@ export const runsMethods = {
|
|
|
305
313
|
project,
|
|
306
314
|
task: task ?? this.sessionTaskFor(run, sessionKind),
|
|
307
315
|
chatMessage,
|
|
316
|
+
extraMessages,
|
|
308
317
|
sessionKind,
|
|
309
318
|
})
|
|
310
|
-
} catch (error) {
|
|
319
|
+
} catch (error ) {
|
|
311
320
|
this.logger.warn?.('dsh-team: run %s failed to start: %s', runId, error)
|
|
312
321
|
this.fingerprints.delete(runId)
|
|
313
322
|
await this.finishRun({
|
|
@@ -350,6 +359,24 @@ if (run.kind === 'dm_reply') {
|
|
|
350
359
|
if (!taskId) return null
|
|
351
360
|
return this.store.get('SELECT * FROM tasks WHERE id = ?', taskId) ?? null
|
|
352
361
|
},
|
|
362
|
+
|
|
363
|
+
// 合批消息(DSHBOTS-231):本运行领取时合并进来的同通道排队消息。
|
|
364
|
+
// 按 merged_messages 记录的消息 id 顺序取回;消息已被删除的条目跳过,不阻塞本次运行。
|
|
365
|
+
// 两张消息表的列不完全一致(solo_messages 没有 reply_to_id),按运行类别选列。
|
|
366
|
+
mergedMessagesFor(run) {
|
|
367
|
+
const ids = jsonParse(run.merged_messages, [])
|
|
368
|
+
if (!Array.isArray(ids) || !ids.length) return []
|
|
369
|
+
const columns = run.kind === 'solo_chat'
|
|
370
|
+
? 'id, sender_type, sender_id, body'
|
|
371
|
+
: 'id, sender_type, sender_id, body, reply_to_id'
|
|
372
|
+
const table = run.kind === 'solo_chat' ? 'solo_messages' : 'messages'
|
|
373
|
+
const rows = []
|
|
374
|
+
for (const messageId of ids) {
|
|
375
|
+
const row = this.store.get(`SELECT ${columns} FROM ${table} WHERE id = ?`, messageId)
|
|
376
|
+
if (row) rows.push(row)
|
|
377
|
+
}
|
|
378
|
+
return rows
|
|
379
|
+
},
|
|
353
380
|
insertRunEvent(runId, type, message) {
|
|
354
381
|
if (!runId) return
|
|
355
382
|
this.store.run(
|
|
@@ -516,13 +543,15 @@ if (run.kind === 'dm_reply') {
|
|
|
516
543
|
const task = run.task_id ? this.store.get('SELECT number, title, sandbox_override FROM tasks WHERE id = ?', run.task_id) : undefined
|
|
517
544
|
const project = run.project_id ? this.store.get('SELECT key FROM projects WHERE id = ?', run.project_id) : undefined
|
|
518
545
|
// recovery 是重启恢复的内部瞬态标记(置位→注入→清零),不属于 API 视图,随 attention_json 一并剥出。
|
|
519
|
-
|
|
546
|
+
// merged_messages 是合批消息 id 列表(DSHBOTS-231),同样剥出原始 JSON,只给解析后的 id。
|
|
547
|
+
const { write_scopes: rawScopes, attention_json: attentionJson, merged_messages: mergedRaw, recovery: _recovery, ...rest } = run
|
|
520
548
|
const fallbackRef = bot?.fallback_provider && bot?.fallback_model ? `${bot.fallback_provider}/${bot.fallback_model}` : null
|
|
521
549
|
const usedRef = run.model_provider && run.model_model ? `${run.model_provider}/${run.model_model}` : null
|
|
522
550
|
return {
|
|
523
551
|
...rest,
|
|
524
552
|
artifacts: jsonParse(run.artifacts, []),
|
|
525
553
|
attention: parseAttention(attentionJson),
|
|
554
|
+
mergedMessageIds: Array.isArray(jsonParse(mergedRaw, [])) ? jsonParse(mergedRaw, []) : [],
|
|
526
555
|
botName: bot?.name ?? 'Bot',
|
|
527
556
|
taskIdentifier: task ? `${project?.key ?? 'T'}-${task.number}` : null,
|
|
528
557
|
taskTitle: task?.title ?? null,
|
|
@@ -181,7 +181,9 @@ export const lifecycleMethods = {
|
|
|
181
181
|
const row = recordUsage(this.store, {
|
|
182
182
|
sourceKey,
|
|
183
183
|
sourceEventId: messageId || null,
|
|
184
|
-
|
|
184
|
+
// task_id(DSHBOTS-212):任务运行的用量归因到任务,支撑任务级端到端成本聚合
|
|
185
|
+
// (taskUsageTotals 经 COALESCE(u.task_id, r.task_id) 同时追认历史行)。
|
|
186
|
+
run: { id: pending.runId, project_id: pending.projectId, bot_id: pending.bot.id, task_id: pending.taskId ?? null },
|
|
185
187
|
provider: pending.bot.provider ?? '',
|
|
186
188
|
model: pending.bot.model ?? '',
|
|
187
189
|
usage,
|
|
@@ -76,8 +76,8 @@ const HOST_NESTED_REJECTION = /^HMR transactions cannot be nested$/
|
|
|
76
76
|
// 超时错误单独标记,便于与业务失败区分(超时不再重试:重试只会再排一次队)。
|
|
77
77
|
// cause 保留最后一次真实失败原因:超时/预算耗尽只是「没等到」,不是失败本身,
|
|
78
78
|
// 审计与 warn 必须同时看得见真正的业务错误。
|
|
79
|
-
class RestoreTimeoutError extends Error {
|
|
80
|
-
constructor(ms, cause) {
|
|
79
|
+
class RestoreTimeoutError extends Error {
|
|
80
|
+
constructor(ms, cause ) {
|
|
81
81
|
const detail = describeCause(cause)
|
|
82
82
|
super(`restore default model timed out after ${ms}ms${detail ? ` (last error: ${detail})` : ''}`)
|
|
83
83
|
this.name = 'RestoreTimeoutError'
|
|
@@ -130,7 +130,7 @@ const withTimeout = async (fn, waitMs, onLate) => {
|
|
|
130
130
|
// 返回是否恢复成功。失败必须可见:重试穷尽后 warn 一次并写审计 model_restore_failed,
|
|
131
131
|
// 审计自身异常只 warn,不把失败抛回 selectModel 的串行链。
|
|
132
132
|
// hooks.hmr:宿主 HMR 服务实例(ctx.get('hmr')),用于拿到 executing 做事务逃逸。
|
|
133
|
-
export async function restoreDefaultModel(defaults, before, hooks = {}) {
|
|
133
|
+
export async function restoreDefaultModel(defaults, before, hooks = {} ) {
|
|
134
134
|
const {
|
|
135
135
|
logger,
|
|
136
136
|
audit,
|
|
@@ -216,7 +216,7 @@ export async function restoreDefaultModel(defaults, before, hooks = {}) {
|
|
|
216
216
|
logger?.debug?.('dsh-team: restore default model ok via %s', escape)
|
|
217
217
|
} catch {}
|
|
218
218
|
return true
|
|
219
|
-
} catch (error) {
|
|
219
|
+
} catch (error ) {
|
|
220
220
|
lastError = error
|
|
221
221
|
if (error instanceof RestoreTimeoutError) {
|
|
222
222
|
// 调用排在宿主事务队列尾且迟迟不落地:重试只会再排一次队,直接收口。
|
|
@@ -266,7 +266,7 @@ export async function restoreDefaultModel(defaults, before, hooks = {}) {
|
|
|
266
266
|
...(budgetExhausted ? { budgetExhausted: true } : {}),
|
|
267
267
|
...(cause ? { cause } : {}),
|
|
268
268
|
})
|
|
269
|
-
} catch (error) {
|
|
269
|
+
} catch (error ) {
|
|
270
270
|
try {
|
|
271
271
|
logger?.warn?.('dsh-team: model restore audit failed: %s', error)
|
|
272
272
|
} catch {}
|