dsh-team-bots 0.3.80 → 0.3.105
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-migrations.js +5 -1
- package/src/db.js +52 -0
- package/src/feishu/channel.js +15 -11
- package/src/feishu/format.js +37 -1
- 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 +33 -12
- 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 +37 -12
- package/src/services/review-watchdog.js +31 -12
- package/src/services/run-finish.js +21 -14
- 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 +27 -15
- 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 +67 -9
- package/src/services/wechat.js +24 -8
- package/src/subagent-ledger.js +23 -6
- package/src/usage-reconcile.js +93 -32
- package/src/usage.js +125 -50
- package/src/util.js +17 -0
- package/src/wechat/channel.js +3 -1
- package/src/wechat/commands.js +13 -1
- package/ui/modules/events-click.js +14 -1
- package/ui/modules/file-preview.js +22 -10
- package/ui/modules/markdown.js +10 -8
- package/ui/modules/tasks.js +1 -1
- package/ui/styles/cards.css +13 -0
- package/ui/styles/modal.css +4 -0
package/src/usage-reconcile.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// 覆盖对账摘要、价目设置、月度用量、运行明细对账、保留期清理与 CSV 导出。
|
|
3
3
|
// 用量明细记录、聚合与窗口解析见 ./usage.js。
|
|
4
4
|
|
|
5
|
-
import { BUDGET_CURRENCY,
|
|
5
|
+
import { BUDGET_CURRENCY, clearUsageSnapshotCache, costForTokens, filterClause, resolveUsageWindow, rollupTotals } from './usage.js'
|
|
6
6
|
import { currentPriceMap, lookupPrice } from './pricing.js'
|
|
7
7
|
import { id, nowIso } from './util.js'
|
|
8
8
|
|
|
@@ -53,24 +53,29 @@ export function setPrice(store, input) {
|
|
|
53
53
|
return priceId
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
// 月度预算口径(chat-db-split Phase 2 起读顶库 usage_rollup):
|
|
57
|
+
// 小时桶的 UTC 月份与原明细 occurred_at 的 UTC 月份一致,口径不变。
|
|
56
58
|
export function monthUsageForProject(store, projectId, budgetCurrency = BUDGET_CURRENCY) {
|
|
57
59
|
const month = new Date().toISOString().slice(0, 7)
|
|
58
|
-
return
|
|
60
|
+
return rollupTotals(store, ['u.project_id = ?', 'substr(u.hour_utc, 1, 7) = ?'], [projectId, month], budgetCurrency)
|
|
59
61
|
}
|
|
60
62
|
|
|
61
63
|
export function monthUsageGlobal(store, budgetCurrency = BUDGET_CURRENCY) {
|
|
62
64
|
const month = new Date().toISOString().slice(0, 7)
|
|
63
|
-
return
|
|
65
|
+
return rollupTotals(store, ['substr(u.hour_utc, 1, 7) = ?'], [month], budgetCurrency)
|
|
64
66
|
}
|
|
65
67
|
|
|
66
68
|
// 对账:运行表记录的用量与 usage_events 明细比对,修复「事件晚到」造成的缺口。
|
|
67
69
|
// expiredBefore:用量明细的保留期清理边界(ISO 字符串或毫秒时间戳)。
|
|
68
70
|
// 早于该边界、且明细已被清理的 run 判为 'expired'(不是丢数据,不计入不一致)。
|
|
69
|
-
export function reconcileUsage(store, { days = 30, expiredBefore = null } = {}) {
|
|
71
|
+
export function reconcileUsage(store, { days = 30, expiredBefore = null, chatStoreFor = null } = {}) {
|
|
72
|
+
// chatStoreFor(run)(chat-db-split Phase 2):明细与对账行住运行归属的聊天库;
|
|
73
|
+
// 未注入路由时回落顶库(测试/独立构造形态,明细仍在顶库)。
|
|
74
|
+
const detailStoreFor = run => (chatStoreFor ? chatStoreFor(run) : store)
|
|
70
75
|
const since = new Date(Date.now() - days * 86_400_000).toISOString()
|
|
71
76
|
const expiredAt = toMillis(expiredBefore)
|
|
72
77
|
const runs = store.all(
|
|
73
|
-
`SELECT id, ended_at, usage_input, usage_cache_read, usage_cache_write, usage_output
|
|
78
|
+
`SELECT id, project_id, bot_id, ended_at, usage_input, usage_cache_read, usage_cache_write, usage_output
|
|
74
79
|
FROM runs WHERE ended_at IS NOT NULL AND ended_at >= ?`,
|
|
75
80
|
since,
|
|
76
81
|
)
|
|
@@ -84,7 +89,8 @@ export function reconcileUsage(store, { days = 30, expiredBefore = null } = {})
|
|
|
84
89
|
repaired: 0,
|
|
85
90
|
}
|
|
86
91
|
for (const run of runs) {
|
|
87
|
-
const
|
|
92
|
+
const detailStore = detailStoreFor(run)
|
|
93
|
+
const events = detailStore.get(
|
|
88
94
|
`SELECT COUNT(*) AS calls,
|
|
89
95
|
COALESCE(SUM(input_tokens), 0) AS input,
|
|
90
96
|
COALESCE(SUM(cache_read_tokens), 0) AS cacheRead,
|
|
@@ -131,7 +137,7 @@ export function reconcileUsage(store, { days = 30, expiredBefore = null } = {})
|
|
|
131
137
|
summary.mismatched += 1
|
|
132
138
|
}
|
|
133
139
|
}
|
|
134
|
-
|
|
140
|
+
detailStore.run(
|
|
135
141
|
`INSERT INTO usage_reconciliation(id, run_id, event_calls, event_tokens, run_tokens, delta_tokens, status, note, checked_at)
|
|
136
142
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
137
143
|
ON CONFLICT(run_id) DO UPDATE SET event_calls = excluded.event_calls, event_tokens = excluded.event_tokens,
|
|
@@ -148,6 +154,20 @@ export function reconcileUsage(store, { days = 30, expiredBefore = null } = {})
|
|
|
148
154
|
summary.checkedAt,
|
|
149
155
|
)
|
|
150
156
|
}
|
|
157
|
+
// 顶库权威计数覆写(usageSnapshot 的摘要与缓存版本读这里)。
|
|
158
|
+
store.run(
|
|
159
|
+
`INSERT INTO usage_recon_state(id, runs_checked, ok_runs, missing_usage, expired_usage, repaired, checked_at)
|
|
160
|
+
VALUES (1, ?, ?, ?, ?, ?, ?)
|
|
161
|
+
ON CONFLICT(id) DO UPDATE SET runs_checked = excluded.runs_checked, ok_runs = excluded.ok_runs,
|
|
162
|
+
missing_usage = excluded.missing_usage, expired_usage = excluded.expired_usage,
|
|
163
|
+
repaired = excluded.repaired, checked_at = excluded.checked_at`,
|
|
164
|
+
summary.runsChecked,
|
|
165
|
+
summary.runsWithEvents - summary.repaired,
|
|
166
|
+
summary.missingUsage,
|
|
167
|
+
summary.expiredUsage,
|
|
168
|
+
summary.repaired,
|
|
169
|
+
summary.checkedAt,
|
|
170
|
+
)
|
|
151
171
|
// 对账写回后立即失效快照缓存:否则页面会拿旧对账摘要显示 30s,用户会以为对账没生效。
|
|
152
172
|
clearUsageSnapshotCache()
|
|
153
173
|
return summary
|
|
@@ -156,12 +176,14 @@ export function reconcileUsage(store, { days = 30, expiredBefore = null } = {})
|
|
|
156
176
|
// 数据保留:按天清理明细与事件,审计与聚合结果保留。
|
|
157
177
|
// 返回值里带上每类清理的时间边界(before = 被删掉内容的时间上界,ISO 字符串),
|
|
158
178
|
// 调用方可以把 usage.before 传给 reconcileUsage,让「明细被清理」的 run 判为 expired。
|
|
179
|
+
// chatStores(chat-db-split Phase 1):正文类表(run_trajectory/messages/solo_messages)
|
|
180
|
+
// 已分住各聊天库,清理逐库执行;缺省空数组时跳过正文清理(调用方未接入分库的旧形态)。
|
|
159
181
|
export function applyRetention(store, {
|
|
160
182
|
usageDays = 0,
|
|
161
183
|
runEventDays = 0,
|
|
162
184
|
messageDays = 0,
|
|
163
185
|
reconciliationDays = 30,
|
|
164
|
-
} = {}) {
|
|
186
|
+
} = {}, chatStores = []) {
|
|
165
187
|
const removed = {
|
|
166
188
|
usageEvents: 0,
|
|
167
189
|
runEvents: 0,
|
|
@@ -175,21 +197,46 @@ export function applyRetention(store, {
|
|
|
175
197
|
const cutoff = days => new Date(Date.now() - days * 86_400_000).toISOString()
|
|
176
198
|
if (usageDays > 0) {
|
|
177
199
|
const before = cutoff(usageDays)
|
|
178
|
-
|
|
200
|
+
// 明细住各聊天库(Phase 2);顶库兜底清无主遗留行,两边合计。
|
|
201
|
+
removed.usageEvents = Number(store.run('DELETE FROM usage_events WHERE occurred_at < ?', before).changes ?? 0)
|
|
202
|
+
for (const chatStore of chatStores) {
|
|
203
|
+
if (chatStore === store) continue
|
|
204
|
+
removed.usageEvents += Number(chatStore.run('DELETE FROM usage_events WHERE occurred_at < ?', before).changes ?? 0)
|
|
205
|
+
}
|
|
179
206
|
removed.usage = { removed: removed.usageEvents, before }
|
|
180
207
|
}
|
|
181
208
|
if (runEventDays > 0) {
|
|
182
|
-
|
|
183
|
-
removed.
|
|
209
|
+
// run_events 住各聊天库(Phase 3):逐库清理并合计,顶库只兜孤儿行。
|
|
210
|
+
removed.runEvents = Number(store.run('DELETE FROM run_events WHERE created_at < ?', cutoff(runEventDays)).changes ?? 0)
|
|
211
|
+
for (const chatStore of chatStores) {
|
|
212
|
+
if (chatStore === store) continue
|
|
213
|
+
removed.runEvents += Number(chatStore.run('DELETE FROM run_events WHERE created_at < ?', cutoff(runEventDays)).changes ?? 0)
|
|
214
|
+
}
|
|
215
|
+
// 顶库轨迹兜底清理:独立构造的 runtime(未注入聊天库路由)仍把轨迹落顶库;
|
|
216
|
+
// 分库后的常规数据在各聊天库,逐库清理。计数两边合计。
|
|
217
|
+
removed.runTrajectory = Number(store.run('DELETE FROM run_trajectory WHERE created_at < ?', cutoff(runEventDays)).changes ?? 0)
|
|
218
|
+
for (const chatStore of chatStores) {
|
|
219
|
+
if (chatStore === store) continue
|
|
220
|
+
removed.runTrajectory += Number(chatStore.run('DELETE FROM run_trajectory WHERE created_at < ?', cutoff(runEventDays)).changes ?? 0)
|
|
221
|
+
}
|
|
184
222
|
}
|
|
185
223
|
if (messageDays > 0) {
|
|
186
|
-
removed.messages = store.run('DELETE FROM messages WHERE created_at < ?', cutoff(messageDays)).changes
|
|
187
|
-
removed.soloMessages = store.run('DELETE FROM solo_messages WHERE created_at < ?', cutoff(messageDays)).changes
|
|
224
|
+
removed.messages = Number(store.run('DELETE FROM messages WHERE created_at < ?', cutoff(messageDays)).changes ?? 0)
|
|
225
|
+
removed.soloMessages = Number(store.run('DELETE FROM solo_messages WHERE created_at < ?', cutoff(messageDays)).changes ?? 0)
|
|
226
|
+
for (const chatStore of chatStores) {
|
|
227
|
+
if (chatStore === store) continue
|
|
228
|
+
removed.messages += Number(chatStore.run('DELETE FROM messages WHERE created_at < ?', cutoff(messageDays)).changes ?? 0)
|
|
229
|
+
removed.soloMessages += Number(chatStore.run('DELETE FROM solo_messages WHERE created_at < ?', cutoff(messageDays)).changes ?? 0)
|
|
230
|
+
}
|
|
188
231
|
}
|
|
189
232
|
if (reconciliationDays > 0) {
|
|
190
|
-
// 对账记录同样按保留期清理,避免 usage_reconciliation
|
|
233
|
+
// 对账记录同样按保留期清理,避免 usage_reconciliation 无限增长;明细在各聊天库。
|
|
191
234
|
const before = cutoff(reconciliationDays)
|
|
192
|
-
removed.reconciliationRecords = store.run('DELETE FROM usage_reconciliation WHERE checked_at < ?', before).changes
|
|
235
|
+
removed.reconciliationRecords = Number(store.run('DELETE FROM usage_reconciliation WHERE checked_at < ?', before).changes ?? 0)
|
|
236
|
+
for (const chatStore of chatStores) {
|
|
237
|
+
if (chatStore === store) continue
|
|
238
|
+
removed.reconciliationRecords += Number(chatStore.run('DELETE FROM usage_reconciliation WHERE checked_at < ?', before).changes ?? 0)
|
|
239
|
+
}
|
|
193
240
|
removed.reconciliation = { removed: removed.reconciliationRecords, before }
|
|
194
241
|
}
|
|
195
242
|
// 保留期清理会删明细与对账记录,清缓存避免页面继续显示已删数据。
|
|
@@ -210,8 +257,11 @@ function csvCell(value) {
|
|
|
210
257
|
return /[",\n]/.test(text) ? `"${text.replaceAll('"', '""')}"` : text
|
|
211
258
|
}
|
|
212
259
|
|
|
213
|
-
export function exportUsageCsv(store, { projectId = null, soloBotId = null, days = 30, window = null } = {}) {
|
|
214
|
-
//
|
|
260
|
+
export function exportUsageCsv(store, { projectId = null, soloBotId = null, days = 30, window = null } = {}, detailStores = null) {
|
|
261
|
+
// 明细住各聊天库(chat-db-split Phase 2):detailStores 为本口径涉及的明细库列表
|
|
262
|
+
// (全局=allChatStores、项目/单聊=对应单库);缺省回落顶库(测试/独立构造形态)。
|
|
263
|
+
// projects/bots 的富化列在顶库 JS 侧补——聊天库里没有这两张表可 JOIN。
|
|
264
|
+
const stores = detailStores && detailStores.length ? detailStores : [store]
|
|
215
265
|
const resolved = window ? resolveUsageWindow(window) : null
|
|
216
266
|
const since = resolved ? resolved.since : new Date(Date.now() - days * 86_400_000).toISOString()
|
|
217
267
|
const filters = []
|
|
@@ -229,21 +279,32 @@ export function exportUsageCsv(store, { projectId = null, soloBotId = null, days
|
|
|
229
279
|
filters.push('u.bot_id = ?', 'u.project_id IS NULL')
|
|
230
280
|
params.push(soloBotId)
|
|
231
281
|
}
|
|
232
|
-
const rows
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
282
|
+
const rows = []
|
|
283
|
+
for (const detailStore of stores) {
|
|
284
|
+
rows.push(...detailStore.all(
|
|
285
|
+
`SELECT u.occurred_at AS occurred_at, u.run_id AS run_id, u.task_id AS task_id,
|
|
286
|
+
u.project_id AS project_id, u.bot_id AS bot_id,
|
|
287
|
+
u.provider AS provider, u.model AS model, u.purpose AS purpose, u.currency AS currency,
|
|
288
|
+
u.input_tokens AS input_tokens, u.cache_read_tokens AS cache_read_tokens,
|
|
289
|
+
u.cache_write_tokens AS cache_write_tokens, u.output_tokens AS output_tokens,
|
|
290
|
+
u.reasoning_tokens AS reasoning_tokens, u.input_cost AS input_cost,
|
|
291
|
+
u.output_cost AS output_cost, u.total_cost AS total_cost
|
|
292
|
+
FROM usage_events u
|
|
293
|
+
${filterClause(filters)}`,
|
|
294
|
+
...params,
|
|
295
|
+
))
|
|
296
|
+
}
|
|
297
|
+
rows.sort((a, b) => String(a.occurred_at).localeCompare(String(b.occurred_at)))
|
|
298
|
+
const projectMap = new Map(store.all('SELECT id, key, name FROM projects')
|
|
299
|
+
.map((row ) => [row.id, row]))
|
|
300
|
+
const botMap = new Map(store.all('SELECT id, name FROM bots')
|
|
301
|
+
.map((row ) => [row.id, row]))
|
|
302
|
+
for (const row of rows) {
|
|
303
|
+
const project = row.project_id ? projectMap.get(row.project_id) : null
|
|
304
|
+
row.project_key = project?.key ?? ''
|
|
305
|
+
row.project_name = project?.name ?? ''
|
|
306
|
+
row.bot_name = row.bot_id ? botMap.get(row.bot_id)?.name ?? '' : ''
|
|
307
|
+
}
|
|
247
308
|
// 与用量页同口径:入库时未计价的明细按当前价目追认后再导出,避免页面与导出不一致。
|
|
248
309
|
const prices = currentPriceMap(store)
|
|
249
310
|
const lines = [CSV_COLUMNS.join(',')]
|
package/src/usage.js
CHANGED
|
@@ -33,6 +33,7 @@ export function recordUsage(store, {
|
|
|
33
33
|
usage,
|
|
34
34
|
occurredAt = nowIso(),
|
|
35
35
|
purpose = 'run',
|
|
36
|
+
chatStore = null,
|
|
36
37
|
}) {
|
|
37
38
|
if (!usage || typeof usage !== 'object') return null
|
|
38
39
|
const input = count(usage.inputTokens ?? usage.input)
|
|
@@ -41,7 +42,9 @@ export function recordUsage(store, {
|
|
|
41
42
|
const output = count(usage.outputTokens ?? usage.output)
|
|
42
43
|
const reasoning = count(usage.reasoningTokens ?? usage.reasoning)
|
|
43
44
|
if (input + cacheRead + cacheWrite + output + reasoning === 0) return null
|
|
44
|
-
|
|
45
|
+
// 明细住运行归属的聊天库(chat-db-split Phase 2);未注入路由(测试/独立构造)回落顶库。
|
|
46
|
+
const detailStore = chatStore ?? store
|
|
47
|
+
const existing = detailStore.get('SELECT * FROM usage_events WHERE source_key = ?', sourceKey)
|
|
45
48
|
if (existing) return existing
|
|
46
49
|
const price = currentPrice(store, provider, model)
|
|
47
50
|
let inputCost = null
|
|
@@ -55,7 +58,7 @@ export function recordUsage(store, {
|
|
|
55
58
|
}
|
|
56
59
|
const currency = String(price?.currency ?? BUDGET_CURRENCY).toUpperCase() || BUDGET_CURRENCY
|
|
57
60
|
const eventId = id('use')
|
|
58
|
-
|
|
61
|
+
detailStore.run(
|
|
59
62
|
`INSERT INTO usage_events(id, source_key, source_event_id, run_id, project_id, bot_id, task_id,
|
|
60
63
|
provider, model, purpose, currency,
|
|
61
64
|
input_tokens, cache_read_tokens, cache_write_tokens, output_tokens, reasoning_tokens,
|
|
@@ -83,9 +86,50 @@ export function recordUsage(store, {
|
|
|
83
86
|
occurredAt,
|
|
84
87
|
nowIso(),
|
|
85
88
|
)
|
|
86
|
-
|
|
89
|
+
// 顶库小时汇总同步累加(视图/预算只读这里);project_key 冗余存档,项目移除后不丢账。
|
|
90
|
+
const projectId = run?.project_id ?? null
|
|
91
|
+
const projectKey = projectId
|
|
92
|
+
? String(store.get('SELECT key FROM projects WHERE id = ?', projectId)?.key ?? '')
|
|
93
|
+
: ''
|
|
94
|
+
store.run(
|
|
95
|
+
`INSERT INTO usage_rollup(hour_utc, project_id, project_key, bot_id, provider, model, currency, priced,
|
|
96
|
+
calls, input_tokens, cache_read_tokens, cache_write_tokens, output_tokens, reasoning_tokens, stored_cost)
|
|
97
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?, ?, ?, ?, ?)
|
|
98
|
+
ON CONFLICT(hour_utc, project_id, bot_id, provider, model, currency, priced) DO UPDATE SET
|
|
99
|
+
calls = calls + 1,
|
|
100
|
+
input_tokens = input_tokens + excluded.input_tokens,
|
|
101
|
+
cache_read_tokens = cache_read_tokens + excluded.cache_read_tokens,
|
|
102
|
+
cache_write_tokens = cache_write_tokens + excluded.cache_write_tokens,
|
|
103
|
+
output_tokens = output_tokens + excluded.output_tokens,
|
|
104
|
+
reasoning_tokens = reasoning_tokens + excluded.reasoning_tokens,
|
|
105
|
+
stored_cost = stored_cost + excluded.stored_cost`,
|
|
106
|
+
String(occurredAt).slice(0, 13),
|
|
107
|
+
projectId ?? '',
|
|
108
|
+
projectKey,
|
|
109
|
+
run?.bot_id ?? '',
|
|
110
|
+
provider ?? '',
|
|
111
|
+
model ?? '',
|
|
112
|
+
currency,
|
|
113
|
+
totalCost === null ? 0 : 1,
|
|
114
|
+
input, cacheRead, cacheWrite, output, reasoning,
|
|
115
|
+
totalCost ?? 0,
|
|
116
|
+
)
|
|
117
|
+
return detailStore.get('SELECT * FROM usage_events WHERE id = ?', eventId)
|
|
87
118
|
}
|
|
88
119
|
|
|
120
|
+
// —— 汇总读层(chat-db-split Phase 2)——
|
|
121
|
+
// 视图/预算全部走顶库 usage_rollup(小时粒度):滚动窗口最多带进边界处的
|
|
122
|
+
// 不足一小时用量;日历日/月度分组按小时桶换算,与明细口径一致。
|
|
123
|
+
// project_id/bot_id 空串哨兵对应「无归属」。
|
|
124
|
+
|
|
125
|
+
const ROLLUP_TOKENS = `
|
|
126
|
+
COALESCE(SUM(input_tokens), 0) AS input_tokens,
|
|
127
|
+
COALESCE(SUM(cache_read_tokens), 0) AS cache_read_tokens,
|
|
128
|
+
COALESCE(SUM(cache_write_tokens), 0) AS cache_write_tokens,
|
|
129
|
+
COALESCE(SUM(output_tokens), 0) AS output_tokens,
|
|
130
|
+
COALESCE(SUM(reasoning_tokens), 0) AS reasoning_tokens,
|
|
131
|
+
COALESCE(SUM(calls), 0) AS calls`
|
|
132
|
+
|
|
89
133
|
// 明细级聚合:token 与调用次数。计价状态(priced/unpriced)由 costBreakdown 统一给出,
|
|
90
134
|
// 口径是「入库已算死 + 现价可追认」,避免同一语义在两处各统计一遍。
|
|
91
135
|
const AGGREGATES = `
|
|
@@ -238,23 +282,22 @@ export function aggregateUsage(store, filters = [], params = [], budgetCurrency
|
|
|
238
282
|
return mapTotals(row, breakdown)
|
|
239
283
|
}
|
|
240
284
|
|
|
241
|
-
|
|
242
|
-
|
|
285
|
+
// priceStore:价目表所在库(顶库)。明细聚合跑在聊天库上,model_prices 不在聊天库里。
|
|
286
|
+
export function runUsageTotals(store, runId, budgetCurrency = BUDGET_CURRENCY, priceStore = null) {
|
|
287
|
+
const prices = currentPriceMap(priceStore ?? store)
|
|
288
|
+
return aggregateUsage(store, ['u.run_id = ?'], [runId], budgetCurrency, prices)
|
|
243
289
|
}
|
|
244
290
|
|
|
245
291
|
// —— 任务级 usage 聚合(DSHBOTS-212)——
|
|
246
|
-
// 口径:一个任务端到端花多少 token
|
|
247
|
-
//
|
|
248
|
-
//
|
|
249
|
-
// (runs.task_id 自始有值)。COALESCE 两条路径同权,不做一次性回填迁移。
|
|
292
|
+
// 口径:一个任务端到端花多少 token。明细按 task_id 归因(lifecycle 记账随 run 透传;
|
|
293
|
+
// chat-db-split Phase 2 迁移把历史 NULL 行经顶库 runs 一次性回填),调用方传任务
|
|
294
|
+
// 所属项目的聊天库(明细住那里),不再 JOIN runs——聊天库里没有 runs 表。
|
|
250
295
|
// 覆盖该任务全部运行:成功交付、失败重试(attempt 递增仍同 run 行)、核验/受阻处理
|
|
251
296
|
// 派生运行(task_id 相同)——「端到端成本」即该任务名下发生过的一切模型消费。
|
|
252
297
|
// 缓存 token 分列(cache_read/cache_write),计价沿用 costForTokens 既有口径,不另算。
|
|
253
|
-
|
|
254
|
-
const
|
|
255
|
-
|
|
256
|
-
export function taskUsageTotals(store, taskId, budgetCurrency = BUDGET_CURRENCY) {
|
|
257
|
-
return aggregateUsage(store, [`${EFFECTIVE_TASK_ID} = ?`], [taskId], budgetCurrency, null, RUN_TASK_JOIN)
|
|
298
|
+
export function taskUsageTotals(store, taskId, budgetCurrency = BUDGET_CURRENCY, priceStore = null) {
|
|
299
|
+
const prices = currentPriceMap(priceStore ?? store)
|
|
300
|
+
return aggregateUsage(store, ['u.task_id = ?'], [taskId], budgetCurrency, prices)
|
|
258
301
|
}
|
|
259
302
|
|
|
260
303
|
// 任务子树 id 集(含自身):沿 tasks.parent_task_id 下钻全部后代。
|
|
@@ -275,11 +318,12 @@ export function taskSubtreeIds(store, taskId) {
|
|
|
275
318
|
// 拆分划算判定(C_removed > C_introduced)用它与「同类整卡任务」的 taskUsageTotals
|
|
276
319
|
// 对比:拆分后总成本 = 主项 usageTotal(含子卡),拆掉的是串行等待,引入的是每卡的
|
|
277
320
|
// 会话预热/交接重复上下文——两个口径都含缓存 token 分列,可直接看输入侧增量。
|
|
278
|
-
export function taskTreeUsageTotals(store, taskId, budgetCurrency = BUDGET_CURRENCY) {
|
|
279
|
-
const ids = taskSubtreeIds(store, taskId)
|
|
321
|
+
export function taskTreeUsageTotals(store, taskId, budgetCurrency = BUDGET_CURRENCY, subtreeIds = null, priceStore = null) {
|
|
322
|
+
const ids = subtreeIds ?? taskSubtreeIds(store, taskId)
|
|
280
323
|
if (!ids.length) return { ...ZERO_TOTALS }
|
|
281
324
|
const marks = `(${ids.map(() => '?').join(', ')})`
|
|
282
|
-
|
|
325
|
+
const prices = currentPriceMap(priceStore ?? store)
|
|
326
|
+
return aggregateUsage(store, [`u.task_id IN ${marks}`], ids, budgetCurrency, prices)
|
|
283
327
|
}
|
|
284
328
|
|
|
285
329
|
// 用量时间窗(DSHBOTS-121):今日 / 7 日 / 30 日 / 至今 四档。
|
|
@@ -367,6 +411,14 @@ export function resolveUsageWindow(key, options = {} ) {
|
|
|
367
411
|
}
|
|
368
412
|
}
|
|
369
413
|
|
|
414
|
+
// 小时桶列('YYYY-MM-DDTHH')的日历日表达式:与 dayExpression 同口径,
|
|
415
|
+
// 但要补 ':00:00' 才是 SQLite datetime() 认的完整时刻。
|
|
416
|
+
function hourDayExpression(column, offsetMinutes) {
|
|
417
|
+
if (offsetMinutes === null || offsetMinutes === undefined) return `substr(${column}, 1, 10)`
|
|
418
|
+
const sign = offsetMinutes < 0 ? '-' : '+'
|
|
419
|
+
return `substr(datetime(substr(replace(${column}, 'T', ' '), 1, 13) || ':00:00', '${sign}${Math.abs(offsetMinutes)} minutes'), 1, 10)`
|
|
420
|
+
}
|
|
421
|
+
|
|
370
422
|
// 日历日表达式:offsetMinutes 为空时沿用 UTC 日期(旧口径),否则按固定偏移换算到产品时区。
|
|
371
423
|
function dayExpression(column, offsetMinutes) {
|
|
372
424
|
if (offsetMinutes === null || offsetMinutes === undefined) return `substr(${column}, 1, 10)`
|
|
@@ -380,8 +432,8 @@ function scopeFilters(scope) {
|
|
|
380
432
|
const filters = []
|
|
381
433
|
const params = []
|
|
382
434
|
if (scope.since) {
|
|
383
|
-
filters.push('u.
|
|
384
|
-
params.push(scope.since)
|
|
435
|
+
filters.push('u.hour_utc >= ?')
|
|
436
|
+
params.push(String(scope.since).slice(0, 13))
|
|
385
437
|
}
|
|
386
438
|
if (scope.projectId) {
|
|
387
439
|
filters.push('u.project_id = ?')
|
|
@@ -389,7 +441,7 @@ function scopeFilters(scope) {
|
|
|
389
441
|
}
|
|
390
442
|
// 单聊口径(DSHBOTS-9):该 Bot 的项目外用量,与 view.solo.runs 同口径。
|
|
391
443
|
if (scope.soloBotId) {
|
|
392
|
-
filters.push('u.bot_id = ?',
|
|
444
|
+
filters.push('u.bot_id = ?', "u.project_id = ''")
|
|
393
445
|
params.push(scope.soloBotId)
|
|
394
446
|
}
|
|
395
447
|
// 至今窗口没有时间下界时可能一条条件都不剩,补一条恒真条件,保证 where 子句非空。
|
|
@@ -447,7 +499,7 @@ function zeroUsageBotRows(store, scope, budgetCurrency) {
|
|
|
447
499
|
${runFilters.length ? `AND ${runFilters.join(' AND ')}` : ''} AND ${runWindow.clause}
|
|
448
500
|
)
|
|
449
501
|
AND NOT EXISTS (
|
|
450
|
-
SELECT 1 FROM
|
|
502
|
+
SELECT 1 FROM usage_rollup u
|
|
451
503
|
WHERE u.bot_id = b.id${eventFilters.length ? ` AND ${eventFilters.join(' AND ')}` : ''}
|
|
452
504
|
)
|
|
453
505
|
ORDER BY b.name`,
|
|
@@ -472,7 +524,7 @@ function zeroUsageProjectRows(store, scope, budgetCurrency) {
|
|
|
472
524
|
JOIN runs r ON r.project_id = p.id
|
|
473
525
|
WHERE ${runFilters.length ? `${runFilters.join(' AND ')} AND` : ''} ${runWindow.clause}
|
|
474
526
|
AND NOT EXISTS (
|
|
475
|
-
SELECT 1 FROM
|
|
527
|
+
SELECT 1 FROM usage_rollup u WHERE u.project_id = p.id${eventFilters.length ? ` AND ${eventFilters.join(' AND ')}` : ''}
|
|
476
528
|
)
|
|
477
529
|
GROUP BY p.id, p.name, p.key
|
|
478
530
|
ORDER BY p.name`,
|
|
@@ -492,7 +544,7 @@ function zeroUsageDayRows(store, scope, budgetCurrency, daySince) {
|
|
|
492
544
|
const { filters: eventFilters, params: eventParams } = scopeFilters(scope)
|
|
493
545
|
const runWindow = runWindowFilter(scope)
|
|
494
546
|
const runDay = dayExpression('COALESCE(r.ended_at, r.started_at, r.queued_at)', scope.offsetMinutes)
|
|
495
|
-
const eventDay =
|
|
547
|
+
const eventDay = hourDayExpression('u.hour_utc', scope.offsetMinutes)
|
|
496
548
|
const rows = store.all(
|
|
497
549
|
`SELECT d.day AS day FROM (
|
|
498
550
|
SELECT DISTINCT ${runDay} AS day
|
|
@@ -500,7 +552,7 @@ function zeroUsageDayRows(store, scope, budgetCurrency, daySince) {
|
|
|
500
552
|
WHERE ${runFilters.length ? `${runFilters.join(' AND ')} AND` : ''} ${runWindow.clause}
|
|
501
553
|
) d
|
|
502
554
|
WHERE ${daySince ? 'd.day >= ? AND' : ''} NOT EXISTS (
|
|
503
|
-
SELECT 1 FROM
|
|
555
|
+
SELECT 1 FROM usage_rollup u
|
|
504
556
|
WHERE ${eventDay} = d.day${eventFilters.length ? ` AND ${eventFilters.join(' AND ')}` : ''}
|
|
505
557
|
)
|
|
506
558
|
ORDER BY d.day`,
|
|
@@ -515,8 +567,8 @@ function byBotRows(store, scope, budgetCurrency, prices = null) {
|
|
|
515
567
|
const { filters, params } = scopeFilters(scope)
|
|
516
568
|
const where = filterClause(filters)
|
|
517
569
|
const rows = store.all(
|
|
518
|
-
`SELECT u.bot_id AS bot_id, b.name AS name, ${
|
|
519
|
-
FROM
|
|
570
|
+
`SELECT u.bot_id AS bot_id, b.name AS name, ${ROLLUP_TOKENS}
|
|
571
|
+
FROM usage_rollup u LEFT JOIN bots b ON b.id = u.bot_id ${where}
|
|
520
572
|
GROUP BY u.bot_id, b.name
|
|
521
573
|
ORDER BY input_tokens + output_tokens DESC`,
|
|
522
574
|
...params,
|
|
@@ -533,8 +585,8 @@ function byProjectRows(store, scope, budgetCurrency, prices = null) {
|
|
|
533
585
|
const { filters, params } = scopeFilters(scope)
|
|
534
586
|
const where = filterClause(filters)
|
|
535
587
|
const rows = store.all(
|
|
536
|
-
`SELECT u.project_id AS project_id, p.name AS name, p.key AS key, ${
|
|
537
|
-
FROM
|
|
588
|
+
`SELECT u.project_id AS project_id, p.name AS name, COALESCE(p.key, u.project_key) AS key, ${ROLLUP_TOKENS}
|
|
589
|
+
FROM usage_rollup u LEFT JOIN projects p ON p.id = u.project_id ${where}
|
|
538
590
|
GROUP BY u.project_id, p.name, p.key
|
|
539
591
|
ORDER BY input_tokens + output_tokens DESC`,
|
|
540
592
|
...params,
|
|
@@ -552,8 +604,8 @@ function byModelRows(store, scope, budgetCurrency, prices = null) {
|
|
|
552
604
|
const { filters, params } = scopeFilters(scope)
|
|
553
605
|
const where = filterClause(filters)
|
|
554
606
|
const rows = store.all(
|
|
555
|
-
`SELECT u.provider AS provider, u.model AS model, ${
|
|
556
|
-
FROM
|
|
607
|
+
`SELECT u.provider AS provider, u.model AS model, ${ROLLUP_TOKENS}
|
|
608
|
+
FROM usage_rollup u ${where}
|
|
557
609
|
GROUP BY u.provider, u.model
|
|
558
610
|
ORDER BY input_tokens + output_tokens DESC`,
|
|
559
611
|
...params,
|
|
@@ -567,13 +619,13 @@ function byModelRows(store, scope, budgetCurrency, prices = null) {
|
|
|
567
619
|
|
|
568
620
|
function byDayRows(store, scope, budgetCurrency, daySince = null, prices = null) {
|
|
569
621
|
const { filters, params } = scopeFilters(scope)
|
|
570
|
-
const dayExpr =
|
|
622
|
+
const dayExpr = hourDayExpression('u.hour_utc', scope.offsetMinutes)
|
|
571
623
|
const allFilters = daySince ? [...filters, `${dayExpr} >= ?`] : filters
|
|
572
624
|
const allParams = daySince ? [...params, daySince] : params
|
|
573
625
|
const where = filterClause(allFilters)
|
|
574
626
|
const rows = store.all(
|
|
575
|
-
`SELECT ${dayExpr} AS day, ${
|
|
576
|
-
FROM
|
|
627
|
+
`SELECT ${dayExpr} AS day, ${ROLLUP_TOKENS}
|
|
628
|
+
FROM usage_rollup u ${where}
|
|
577
629
|
GROUP BY day ORDER BY day`,
|
|
578
630
|
...allParams,
|
|
579
631
|
)
|
|
@@ -592,8 +644,15 @@ function breakdownByDimension(store, scope, dimensionExpr, budgetCurrency, price
|
|
|
592
644
|
const { filters, params } = scopeFilters(scope)
|
|
593
645
|
const rows = store.all(
|
|
594
646
|
`SELECT ${dimensionExpr} AS dim, u.currency AS currency, u.provider AS provider, u.model AS model,
|
|
595
|
-
|
|
596
|
-
|
|
647
|
+
SUM(CASE WHEN u.priced = 1 THEN u.calls ELSE 0 END) AS priced_calls,
|
|
648
|
+
SUM(CASE WHEN u.priced = 0 THEN u.calls ELSE 0 END) AS null_calls,
|
|
649
|
+
COALESCE(SUM(u.stored_cost), 0) AS stored_cost,
|
|
650
|
+
COALESCE(SUM(CASE WHEN u.priced = 0 THEN u.input_tokens ELSE 0 END), 0) AS null_input_tokens,
|
|
651
|
+
COALESCE(SUM(CASE WHEN u.priced = 0 THEN u.cache_read_tokens ELSE 0 END), 0) AS null_cache_read_tokens,
|
|
652
|
+
COALESCE(SUM(CASE WHEN u.priced = 0 THEN u.cache_write_tokens ELSE 0 END), 0) AS null_cache_write_tokens,
|
|
653
|
+
COALESCE(SUM(CASE WHEN u.priced = 0 THEN u.output_tokens ELSE 0 END), 0) AS null_output_tokens,
|
|
654
|
+
COALESCE(SUM(CASE WHEN u.priced = 0 THEN u.reasoning_tokens ELSE 0 END), 0) AS null_reasoning_tokens
|
|
655
|
+
FROM usage_rollup u ${filterClause(filters)}
|
|
597
656
|
GROUP BY ${dimensionExpr}, u.currency, u.provider, u.model`,
|
|
598
657
|
...params,
|
|
599
658
|
)
|
|
@@ -619,17 +678,39 @@ function tierSet(store, scope, budgetCurrency, daySince = null, prices = null) {
|
|
|
619
678
|
}
|
|
620
679
|
}
|
|
621
680
|
|
|
681
|
+
// 汇总层通用聚合(scope 口径与月度预算共用):token/调用数直读 usage_rollup,
|
|
682
|
+
// 成本含按当前价目表追认未计价部分,口径与明细版 mergeBreakdownRows 一致。
|
|
683
|
+
export function rollupTotals(store, filters = [], params = [], budgetCurrency = BUDGET_CURRENCY, prices = null) {
|
|
684
|
+
const where = filterClause(filters)
|
|
685
|
+
const row = store.get(`SELECT ${ROLLUP_TOKENS} FROM usage_rollup u ${where}`, ...params)
|
|
686
|
+
const breakdownRows = store.all(
|
|
687
|
+
`SELECT u.currency AS currency, u.provider AS provider, u.model AS model,
|
|
688
|
+
SUM(CASE WHEN u.priced = 1 THEN u.calls ELSE 0 END) AS priced_calls,
|
|
689
|
+
SUM(CASE WHEN u.priced = 0 THEN u.calls ELSE 0 END) AS null_calls,
|
|
690
|
+
COALESCE(SUM(u.stored_cost), 0) AS stored_cost,
|
|
691
|
+
COALESCE(SUM(CASE WHEN u.priced = 0 THEN u.input_tokens ELSE 0 END), 0) AS null_input_tokens,
|
|
692
|
+
COALESCE(SUM(CASE WHEN u.priced = 0 THEN u.cache_read_tokens ELSE 0 END), 0) AS null_cache_read_tokens,
|
|
693
|
+
COALESCE(SUM(CASE WHEN u.priced = 0 THEN u.cache_write_tokens ELSE 0 END), 0) AS null_cache_write_tokens,
|
|
694
|
+
COALESCE(SUM(CASE WHEN u.priced = 0 THEN u.output_tokens ELSE 0 END), 0) as null_output_tokens,
|
|
695
|
+
COALESCE(SUM(CASE WHEN u.priced = 0 THEN u.reasoning_tokens ELSE 0 END), 0) AS null_reasoning_tokens
|
|
696
|
+
FROM usage_rollup u ${where}
|
|
697
|
+
GROUP BY u.currency, u.provider, u.model`,
|
|
698
|
+
...params,
|
|
699
|
+
)
|
|
700
|
+
return mapTotals(row, mergeBreakdownRows(breakdownRows, prices ?? currentPriceMap(store), budgetCurrency))
|
|
701
|
+
}
|
|
702
|
+
|
|
622
703
|
function scopeUsage(store, scope, budgetCurrency, prices = null) {
|
|
623
704
|
const { filters, params } = scopeFilters(scope)
|
|
624
|
-
return
|
|
705
|
+
return rollupTotals(store, filters, params, budgetCurrency, prices)
|
|
625
706
|
}
|
|
626
707
|
|
|
627
708
|
// 日均折算用的实际天数:滚动窗口用窗口天数;至今用最早明细到今天跨过的天数。
|
|
628
709
|
function windowSpanDays(store, window) {
|
|
629
710
|
if (Number.isFinite(window.days) && window.days > 0) return window.days
|
|
630
|
-
const earliest = store.get('SELECT MIN(
|
|
711
|
+
const earliest = store.get('SELECT MIN(hour_utc) AS earliest FROM usage_rollup')?.earliest ?? null
|
|
631
712
|
if (!earliest) return 1
|
|
632
|
-
const from = Date.parse(String(earliest))
|
|
713
|
+
const from = Date.parse(`${String(earliest).slice(0, 13)}:00:00.000Z`)
|
|
633
714
|
if (!Number.isFinite(from)) return 1
|
|
634
715
|
return Math.max(1, Math.ceil((Date.now() - from) / DAY_MS))
|
|
635
716
|
}
|
|
@@ -675,10 +756,10 @@ function snapshotCacheKey(projectId, days, budgetCurrency, soloBotId, options) {
|
|
|
675
756
|
}
|
|
676
757
|
|
|
677
758
|
function snapshotDataVersion(store) {
|
|
678
|
-
const events = store.get('SELECT COUNT(*) AS n, MAX(
|
|
679
|
-
const recon = store.get('SELECT
|
|
759
|
+
const events = store.get('SELECT COUNT(*) AS n, MAX(hour_utc) AS latest FROM usage_rollup') ?? {}
|
|
760
|
+
const recon = store.get('SELECT runs_checked AS n, checked_at AS checkedAt FROM usage_recon_state WHERE id = 1') ?? {}
|
|
680
761
|
const prices = store.get('SELECT COUNT(*) AS n, MAX(effective_at) AS effectiveAt FROM model_prices') ?? {}
|
|
681
|
-
return [events.n, events.
|
|
762
|
+
return [events.n, events.latest, recon.n, recon.checkedAt, prices.n, prices.effectiveAt].join(':')
|
|
682
763
|
}
|
|
683
764
|
|
|
684
765
|
// 写回方在改完明细 / 对账 / 价目后调用;测试也用它隔离用例。
|
|
@@ -752,15 +833,9 @@ function computeUsageSnapshot(store, projectId, days = 30, options = {} )
|
|
|
752
833
|
|
|
753
834
|
// 对账结果摘要(只读,不触发对账)。
|
|
754
835
|
export function reconciliationSummary(store) {
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
SUM(CASE WHEN status = 'missing' THEN 1 ELSE 0 END) AS missing_usage,
|
|
759
|
-
SUM(CASE WHEN status = 'expired' THEN 1 ELSE 0 END) AS expired_usage,
|
|
760
|
-
SUM(CASE WHEN status = 'repaired' THEN 1 ELSE 0 END) AS repaired,
|
|
761
|
-
MAX(checked_at) AS checked_at
|
|
762
|
-
FROM usage_reconciliation`,
|
|
763
|
-
)
|
|
836
|
+
// 对账明细住各聊天库(chat-db-split Phase 2);顶库 usage_recon_state 存每轮对账的
|
|
837
|
+
// 权威计数,由 reconcileUsage 每轮整体覆写——摘要读它,不再扫明细表。
|
|
838
|
+
const row = store.get('SELECT * FROM usage_recon_state WHERE id = 1')
|
|
764
839
|
return {
|
|
765
840
|
checkedAt: row?.checked_at ?? null,
|
|
766
841
|
runsChecked: Number(row?.runs_checked ?? 0),
|
package/src/util.js
CHANGED
|
@@ -24,6 +24,23 @@ export const projectKey = (value) => String(value ?? '')
|
|
|
24
24
|
|
|
25
25
|
export const text = (value, max = 20_000) => String(value ?? '').trim().slice(0, max)
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* 去 CR:CRLF 归一为 LF,其余孤立 CR 直接丢弃。
|
|
29
|
+
* 模型工具入参偶发把空格写成 CR(U+000D)——渲染器按行尾处理,一句话会被劈成多段
|
|
30
|
+
* (飞书卡片 markdown、聊天气泡都把 CR 当换行)。
|
|
31
|
+
*/
|
|
32
|
+
export const stripCarriageReturns = (value) => String(value ?? '')
|
|
33
|
+
.replace(/\r\n/g, '\n')
|
|
34
|
+
.replace(/\r/g, '')
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 单行化:去 CR 后把其余空白(空格/Tab/换行)折叠成单个半角空格并去首尾空白。
|
|
38
|
+
* 提问、选项这类单行字段统一过这一道,避免残留空白把版面撑散。
|
|
39
|
+
*/
|
|
40
|
+
export const flattenWhitespace = (value) => stripCarriageReturns(value)
|
|
41
|
+
.replace(/\s+/g, ' ')
|
|
42
|
+
.trim()
|
|
43
|
+
|
|
27
44
|
export const jsonParse = (value, fallback) => {
|
|
28
45
|
if (value === null || value === undefined || value === '') return fallback
|
|
29
46
|
try { return JSON.parse(value) } catch { return fallback }
|
package/src/wechat/channel.js
CHANGED
|
@@ -5,6 +5,7 @@ import { TencentIlinkTransport } from '../../vendor/wechat-ilink/index.js'
|
|
|
5
5
|
import { handleCommand, isRetryable, outboundChunks, WechatBusinessError, WechatIlinkError } from './commands.js'
|
|
6
6
|
import { batchDigest, failureText, welcomeAdmin } from './format.js'
|
|
7
7
|
import { AppError } from '../errors.js'
|
|
8
|
+
import { stripCarriageReturns } from '../util.js'
|
|
8
9
|
|
|
9
10
|
const { version: PLUGIN_VERSION } = createRequire(import.meta.url)('../../package.json')
|
|
10
11
|
const RECONNECT_DELAYS_MS = [1_000, 2_000, 5_000, 10_000, 30_000]
|
|
@@ -455,7 +456,8 @@ export class WechatChannel {
|
|
|
455
456
|
const peer = this.peers.get(peerId)
|
|
456
457
|
// 尚无会话凭证属于可恢复状态(等对方发来消息即可),重试有意义
|
|
457
458
|
if (!peer?.contextToken) throw new Error('尚无该联系人的会话凭证,等待对方发来消息')
|
|
458
|
-
|
|
459
|
+
// 出站边界去 CR:模型文本偶发带 CR,按行尾渲染会把一句话劈成多段。
|
|
460
|
+
const body = stripCarriageReturns(text).trim()
|
|
459
461
|
if (!body) return
|
|
460
462
|
// 拒发观测上下文:桥接 fetch 记录 ret!=0,vendor 抛错后由 attachRejectDetail 拼进文案
|
|
461
463
|
await ilinkRejectSlot.run({}, async () => {
|
package/src/wechat/commands.js
CHANGED
|
@@ -63,7 +63,8 @@ export async function handleCommand(service, rawText, ctx = null) {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
// DSHBOTS-249 固定指令:/ 前缀一律不进对话链路,未知 /xxx 回简短帮助提示。
|
|
66
|
-
|
|
66
|
+
const command = normalizeCommandPrefix(body)
|
|
67
|
+
if (command.startsWith('/')) return handleFixedCommand(service, command, ctx)
|
|
67
68
|
|
|
68
69
|
// DSHBOTS-215 问答回路:微信答复/审批按 token 路由回对应条目,绝不进普通对话。
|
|
69
70
|
// 仅在 service 实现了对应方法时拦截(测试桩未实现时按普通文本处理)。
|
|
@@ -103,6 +104,17 @@ export async function handleCommand(service, rawText, ctx = null) {
|
|
|
103
104
|
return ''
|
|
104
105
|
}
|
|
105
106
|
|
|
107
|
+
/**
|
|
108
|
+
* 固定指令前缀归一化:中文输入法常把指令里的 "/" 与 "?" 打成全角(/ 与 ?),
|
|
109
|
+
* 不归一化时 "/?" 匹配不上固定指令集,会被当成普通文本发进对话链路,用户看到的
|
|
110
|
+
* 却是「没有这个指令」。只改指令符号本身,正文原样保留。
|
|
111
|
+
*/
|
|
112
|
+
function normalizeCommandPrefix(body) {
|
|
113
|
+
return String(body ?? '')
|
|
114
|
+
.replace(/^\uFF0F/, '/')
|
|
115
|
+
.replace(/^\/([a-z??]+)/i, (matched, verb) => `/${verb.replace(/?/g, '?')}`)
|
|
116
|
+
}
|
|
117
|
+
|
|
106
118
|
/**
|
|
107
119
|
* / 前缀固定指令集(DSHBOTS-249)。只读指令对群成员开放;写操作(ADMIN_VERBS)
|
|
108
120
|
* 走 admin 门(与 答/批 同口径,分档依据见 handleCommand 注释)。
|