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/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/ui/modules/tasks.js
CHANGED
|
@@ -384,7 +384,7 @@ function renderBots() {
|
|
|
384
384
|
<div class="bot-card-head">
|
|
385
385
|
${avatar(bot, 'lg')}
|
|
386
386
|
<div class="bot-card-title"><strong>${esc(bot.name)}</strong><span>${esc(bot.role || t("通用成员"))}</span></div>
|
|
387
|
-
<span class="bot-status ${attr(bot.status)}">${esc(BOT_STATUS[bot.status] ?? bot.status)}</span>
|
|
387
|
+
<span class="bot-status ${bot.status === 'active' ? (bot.busy ? 'busy' : 'idle') : attr(bot.status)}">${esc(bot.status === 'active' ? (bot.busy ? t("任务中") : t("空闲中")) : (BOT_STATUS[bot.status] ?? bot.status))}</span>
|
|
388
388
|
</div>
|
|
389
389
|
<div class="bot-model"><span>${esc(bot.modelRef ?? t("未配置模型"))}${bot.effort ? ` · ${esc(EFFORT[bot.effort] ?? bot.effort)}` : ''}</span>${bot.quota?.exhausted ? `<span class="quota-badge">${t("额度耗尽")}</span>` : ''}</div>
|
|
390
390
|
<div class="bot-metrics"><span><strong>${bot.openTasks}</strong> ${t("任务")}</span><span class="dot-sep"></span><span><strong>${bot.memoryCount}</strong> ${t("记忆")}</span><span class="dot-sep"></span><span><strong>${bot.stats.succeeded}</strong> ${t("次成功")}</span></div>
|
package/ui/styles/cards.css
CHANGED
|
@@ -108,6 +108,19 @@
|
|
|
108
108
|
.bot-status.active::before {
|
|
109
109
|
background: var(--green);
|
|
110
110
|
}
|
|
111
|
+
/* 工作负载态(总览 bots 卡片):任务中琥珀、空闲中绿——与项目成员卡的 pill 同色语义。 */
|
|
112
|
+
.bot-status.busy {
|
|
113
|
+
color: var(--ink-3);
|
|
114
|
+
}
|
|
115
|
+
.bot-status.busy::before {
|
|
116
|
+
background: var(--star);
|
|
117
|
+
}
|
|
118
|
+
.bot-status.idle {
|
|
119
|
+
color: var(--ink-3);
|
|
120
|
+
}
|
|
121
|
+
.bot-status.idle::before {
|
|
122
|
+
background: var(--green);
|
|
123
|
+
}
|
|
111
124
|
.bot-status.paused {
|
|
112
125
|
color: var(--ink-3);
|
|
113
126
|
}
|