dsh-team-bots 0.3.80 → 0.3.98
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cordis.patch.yml +100 -0
- package/index.js +5 -2
- package/package.json +1 -1
- package/src/api.js +5 -1
- package/src/db.js +52 -0
- package/src/runtime.js +13 -1
- package/src/service.js +5 -0
- package/src/services/approvals.js +7 -2
- package/src/services/bots.js +8 -1
- package/src/services/chat-stores.js +564 -0
- package/src/services/chat.js +60 -28
- package/src/services/core.js +30 -10
- package/src/services/feishu.js +1 -1
- package/src/services/memory.js +14 -12
- package/src/services/model-fallback.js +3 -1
- package/src/services/notify.js +18 -8
- package/src/services/projects.js +25 -19
- package/src/services/questions.js +2 -1
- package/src/services/review-watchdog.js +31 -12
- package/src/services/run-finish.js +17 -13
- package/src/services/runs.js +29 -41
- package/src/services/runtime/context-window.js +6 -4
- package/src/services/runtime/lifecycle.js +1 -0
- package/src/services/runtime/prompt.js +21 -9
- package/src/services/runtime/sandbox-policy.js +1 -1
- package/src/services/runtime/session.js +22 -13
- package/src/services/runtime/trajectory.js +3 -1
- package/src/services/scheduler.js +48 -49
- package/src/services/system.js +9 -7
- package/src/services/task-review.js +27 -19
- package/src/services/task-runs.js +40 -21
- package/src/services/tasks.js +18 -10
- package/src/services/terminal.js +25 -14
- package/src/services/vision-proxy.js +9 -6
- package/src/services/wechat.js +1 -1
- package/src/subagent-ledger.js +23 -6
- package/src/usage-reconcile.js +93 -32
- package/src/usage.js +125 -50
- package/ui/modules/tasks.js +1 -1
- package/ui/styles/cards.css +13 -0
package/src/services/terminal.js
CHANGED
|
@@ -44,14 +44,15 @@ export const terminalMethods = {
|
|
|
44
44
|
if (!clean) throw badRequest('命令不能为空')
|
|
45
45
|
if (clean.length > TERMINAL_COMMAND_MAX) throw badRequest(`命令过长(最多 ${TERMINAL_COMMAND_MAX} 字符)`)
|
|
46
46
|
// 同项目同时只允许一条运行:检查与插入之间无 await,单线程内不会交错。
|
|
47
|
-
if (this.
|
|
47
|
+
if (this.groupChatStore(project.id).get("SELECT id FROM terminal_commands WHERE project_id = ? AND status = 'running'", project.id)) {
|
|
48
48
|
throw conflict('该项目已有终端命令在运行,请先停止或等待它结束')
|
|
49
49
|
}
|
|
50
50
|
const commandId = id('term')
|
|
51
51
|
const createdAt = nowIso()
|
|
52
52
|
// 命令与聊天消息同一姿势:可能拼进密钥值,入库(行、审计)前先过掩码,密钥原文不落库。
|
|
53
53
|
const storableCommand = this.maskSecretValues(clean)
|
|
54
|
-
|
|
54
|
+
// 终端命令住各项目聊天库(chat-db-split Phase 3):按 project_id 路由。
|
|
55
|
+
this.groupChatStore(project.id).run(
|
|
55
56
|
`INSERT INTO terminal_commands(id, project_id, command, status, created_at) VALUES (?, ?, ?, 'running', ?)`,
|
|
56
57
|
commandId, project.id, storableCommand, createdAt,
|
|
57
58
|
)
|
|
@@ -87,10 +88,18 @@ export const terminalMethods = {
|
|
|
87
88
|
this.finishTerminalCommand(commandId, 'failed', null, `命令执行失败:${error?.message ?? error}`)
|
|
88
89
|
})
|
|
89
90
|
this.audit(actor, 'terminal.run', 'project', project.id, { command: storableCommand })
|
|
90
|
-
return this.terminalCommandView(this.
|
|
91
|
+
return this.terminalCommandView(this.groupChatStore(project.id).get('SELECT * FROM terminal_commands WHERE id = ?', commandId))
|
|
92
|
+
},
|
|
93
|
+
// 命令行住各项目聊天库,commandId 不含项目信息:跨库扇出找行(库数是个位数、主键查)。
|
|
94
|
+
terminalCommandRow(commandId) {
|
|
95
|
+
for (const chatStore of this.allChatStores()) {
|
|
96
|
+
const row = chatStore.get('SELECT * FROM terminal_commands WHERE id = ?', commandId)
|
|
97
|
+
if (row) return row
|
|
98
|
+
}
|
|
99
|
+
return null
|
|
91
100
|
},
|
|
92
101
|
cancelTerminalCommand(commandId, actor = this.actor()) {
|
|
93
|
-
const row = this.
|
|
102
|
+
const row = this.terminalCommandRow(commandId)
|
|
94
103
|
if (!row) throw notFound('终端命令不存在')
|
|
95
104
|
if (row.status !== 'running') throw conflict('命令已结束,无需停止')
|
|
96
105
|
const entry = this.terminalActive.get(commandId)
|
|
@@ -102,16 +111,16 @@ export const terminalMethods = {
|
|
|
102
111
|
killTerminalGroup(entry.child)
|
|
103
112
|
}
|
|
104
113
|
this.audit(actor, 'terminal.cancel', 'terminal_command', commandId, {})
|
|
105
|
-
return this.terminalCommandView(this.
|
|
114
|
+
return this.terminalCommandView(this.groupChatStore(row.project_id).get('SELECT * FROM terminal_commands WHERE id = ?', commandId))
|
|
106
115
|
},
|
|
107
116
|
terminalState(projectId) {
|
|
108
117
|
// 顺手清理:每项目只保留最近 TERMINAL_HISTORY_LIMIT 条,不新增定时器。
|
|
109
|
-
this.
|
|
118
|
+
this.groupChatStore(projectId).run(
|
|
110
119
|
`DELETE FROM terminal_commands WHERE project_id = ? AND id NOT IN (
|
|
111
120
|
SELECT id FROM terminal_commands WHERE project_id = ? ORDER BY created_at DESC LIMIT ?)`,
|
|
112
121
|
projectId, projectId, TERMINAL_HISTORY_LIMIT,
|
|
113
122
|
)
|
|
114
|
-
const history = this.
|
|
123
|
+
const history = this.groupChatStore(projectId).all(
|
|
115
124
|
'SELECT * FROM terminal_commands WHERE project_id = ? ORDER BY created_at DESC LIMIT ?',
|
|
116
125
|
projectId, TERMINAL_HISTORY_LIMIT,
|
|
117
126
|
).map(row => this.terminalCommandView(row))
|
|
@@ -140,18 +149,20 @@ export const terminalMethods = {
|
|
|
140
149
|
const output = this.maskSecretValues(String(entry?.buffer ?? '').slice(-TERMINAL_OUTPUT_CAP))
|
|
141
150
|
// 服务停机竞态:子进程在 store 关闭后才退出时,收尾写库失败只告警,不再炸进程。
|
|
142
151
|
try {
|
|
143
|
-
const row = this.
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
152
|
+
const row = this.terminalCommandRow(commandId)
|
|
153
|
+
if (row) {
|
|
154
|
+
this.groupChatStore(row.project_id).run(
|
|
155
|
+
`UPDATE terminal_commands SET status = ?, exit_code = ?, output = ?, error_message = ?, ended_at = ? WHERE id = ?`,
|
|
156
|
+
status, exitCode, output, errorMessage, nowIso(), commandId,
|
|
157
|
+
)
|
|
158
|
+
this.trimTerminalHistory(row.project_id)
|
|
159
|
+
}
|
|
149
160
|
} catch (error ) {
|
|
150
161
|
this.logger.warn?.(`dsh-team: terminal command ${commandId} 收尾落库失败:${error?.message ?? error}`)
|
|
151
162
|
}
|
|
152
163
|
},
|
|
153
164
|
trimTerminalHistory(projectId) {
|
|
154
|
-
this.
|
|
165
|
+
this.groupChatStore(projectId).run(
|
|
155
166
|
`DELETE FROM terminal_commands WHERE project_id = ? AND id NOT IN (
|
|
156
167
|
SELECT id FROM terminal_commands WHERE project_id = ? ORDER BY created_at DESC LIMIT ?)`,
|
|
157
168
|
projectId, projectId, TERMINAL_HISTORY_LIMIT,
|
|
@@ -76,7 +76,9 @@ export function spawnVisionProxyRun(service, { bot, message, target }) {
|
|
|
76
76
|
runId, message.project_id ?? null, bot.id, message.id, idempotencyKey,
|
|
77
77
|
proxy.provider, proxy.model, createdAt,
|
|
78
78
|
)
|
|
79
|
-
|
|
79
|
+
// run_events 住运行归属的聊天库(chat-db-split Phase 3):路由行取自本回调刚
|
|
80
|
+
// 插入的 runs 参数(project_id/bot_id),与 runs 插入同回调保持撞车不落事件。
|
|
81
|
+
service.runChatStore({ id: runId, project_id: message.project_id ?? null, bot_id: bot.id }).run(
|
|
80
82
|
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
81
83
|
id('evt'), runId, 'queued',
|
|
82
84
|
`主模型无视觉,派生读图代理运行(${proxy.ref})转写消息 ${message.id} 的图片`,
|
|
@@ -97,11 +99,12 @@ export function spawnVisionProxyRun(service, { bot, message, target }) {
|
|
|
97
99
|
// 主动评估入口(sendMessage / sendSoloMessage 落库投递后 fire-and-forget 调用):
|
|
98
100
|
// 消息带图 && 主模型确定无视觉 → 已配置代理则派生转写运行;未配置则只记
|
|
99
101
|
// vision.fallback 审计(按现状报错,不派生不重试)。任何异常只告警,绝不阻塞发送。
|
|
100
|
-
|
|
102
|
+
// messageStore(chat-db-split Phase 1):附图所在聊天库,由调用方随消息上下文带入。
|
|
103
|
+
export async function considerVisionProxy(service, { bot, message, messageStore = null }) {
|
|
101
104
|
try {
|
|
102
105
|
if (!bot || !message?.id) return null
|
|
103
106
|
if (bot.status !== 'active') return null
|
|
104
|
-
if (!messageHasImages(service.store, message.id)) return null
|
|
107
|
+
if (!messageHasImages(messageStore ?? service.store, message.id)) return null
|
|
105
108
|
if (!await modelLacksImageSupport(service.runtime, bot)) return null
|
|
106
109
|
const target = visionProxyTarget(service.config)
|
|
107
110
|
if (!target) {
|
|
@@ -125,14 +128,14 @@ export function maybeSpawnVisionAfterFailure(service, { run, bot, error }) {
|
|
|
125
128
|
if (!isImageUnsupportedError(error)) return null
|
|
126
129
|
if (run?.kind !== 'chat' && run?.kind !== 'solo_chat') return null
|
|
127
130
|
const message = run.trigger_ref
|
|
128
|
-
? service.
|
|
131
|
+
? service.runChatStore(run).get(
|
|
129
132
|
run.kind === 'chat'
|
|
130
133
|
? 'SELECT * FROM messages WHERE id = ?'
|
|
131
134
|
: 'SELECT * FROM solo_messages WHERE id = ?',
|
|
132
135
|
run.trigger_ref,
|
|
133
136
|
)
|
|
134
137
|
: null
|
|
135
|
-
if (!message || !messageHasImages(service.
|
|
138
|
+
if (!message || !messageHasImages(service.runChatStore(run), message.id)) return null
|
|
136
139
|
const target = visionProxyTarget(service.config)
|
|
137
140
|
if (!target) {
|
|
138
141
|
service.audit({ type: 'bot', id: bot.id }, 'vision.fallback', 'bot', bot.id, {
|
|
@@ -154,7 +157,7 @@ export function maybeSpawnVisionAfterFailure(service, { run, bot, error }) {
|
|
|
154
157
|
export async function completeVisionRun(service, { run, bot, status, output, error }) {
|
|
155
158
|
const actor = { type: 'bot', id: bot?.id }
|
|
156
159
|
const message = run.trigger_ref
|
|
157
|
-
? service.
|
|
160
|
+
? service.runChatStore(run).get(
|
|
158
161
|
run.project_id
|
|
159
162
|
? 'SELECT * FROM messages WHERE id = ?'
|
|
160
163
|
: 'SELECT * FROM solo_messages WHERE id = ?',
|
package/src/services/wechat.js
CHANGED
|
@@ -160,7 +160,7 @@ export const wechatMethods = {
|
|
|
160
160
|
async wechatDirectorChat(director, body) {
|
|
161
161
|
const adminPeerId = this.wechat?.status?.()?.adminPeerId ?? null
|
|
162
162
|
const actor = { type: 'user', id: adminPeerId ?? 'user:wechat' }
|
|
163
|
-
const firstContact = !this.
|
|
163
|
+
const firstContact = !this.soloChatStore(director.id).get(
|
|
164
164
|
"SELECT id FROM solo_messages WHERE bot_id = ? AND sender_type = 'user' AND sender_id = ? LIMIT 1",
|
|
165
165
|
director.id,
|
|
166
166
|
actor.id,
|
package/src/subagent-ledger.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
// DSHBOTS-294 子代理账本(ADR §4.2.1):宿主事件缝 → 所属 task 运行的 run_events
|
|
1
|
+
// DSHBOTS-294 子代理账本(ADR §4.2.1):宿主事件缝 → 所属 task 运行的 run_events
|
|
2
|
+
// (chat-db-split Phase 3 起事件住运行归属聊天库,路由器 { forRun } 由调用方注入,
|
|
3
|
+
// 未注入时回落顶库,行为与分库前一致)。
|
|
2
4
|
// 双缝互补:tools 瀑布带 caller agent(父归属权威源,任务号标签在参数里);
|
|
3
5
|
// subagent/start|end 带 stopReason+lastAssistantMessage(终态摘要源,载荷无父身份,
|
|
4
6
|
// 靠 tools/result 配对或唯一 running task 运行兜底归属)。子代理不占 runs 行(§4.2)。
|
|
@@ -8,6 +10,13 @@ const SUBAGENT_TOOLS = new Set(['subagent', 'subagent_fork'])
|
|
|
8
10
|
const SUMMARY_LIMIT = 400
|
|
9
11
|
const STASH_LIMIT = 64
|
|
10
12
|
|
|
13
|
+
// bot → 在跑子代理计数(内存态,仅本轮进程内准确):总览 bots 页的「任务中」判定用。
|
|
14
|
+
const runningByBot = new Map()
|
|
15
|
+
|
|
16
|
+
export function runningSubagentCount(botId) {
|
|
17
|
+
return runningByBot.get(botId) ?? 0
|
|
18
|
+
}
|
|
19
|
+
|
|
11
20
|
// 任务号标签(DSHBOTS-292 约定标签=任务号,如 DSHBOTS-294):优先 description,其次 prompt。
|
|
12
21
|
export function taskLabelOf(args) {
|
|
13
22
|
const source = typeof args?.description === 'string' ? args.description
|
|
@@ -24,7 +33,7 @@ const truncate = (text) => {
|
|
|
24
33
|
}
|
|
25
34
|
|
|
26
35
|
// 在插件根 ctx 注册事件监听(根级注册面实测见 tests/subagent-ledger.test.ts)。
|
|
27
|
-
export function installSubagentLedger(ctx, store, logger) {
|
|
36
|
+
export function installSubagentLedger(ctx, store, logger, chatStores = null) {
|
|
28
37
|
// callId → 派发时快照(tools/result 按 callId 配对父归属)
|
|
29
38
|
const byCall = new Map()
|
|
30
39
|
// botId → 未消费的标签栈(subagent/end 兜底归属时取最近一次派发的标签)
|
|
@@ -51,10 +60,16 @@ export function installSubagentLedger(ctx, store, logger) {
|
|
|
51
60
|
return run ? { botId: binding.bot_id, runId: run.id } : null
|
|
52
61
|
}
|
|
53
62
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
63
|
+
// 事件住运行归属聊天库(Phase 3):只有 runId,先取路由列再走注入的路由器;
|
|
64
|
+
// 运行行已不在(删除竞态)或未注入路由器时回落顶库留痕,不丢审计线索。
|
|
65
|
+
const record = (runId, type, message) => {
|
|
66
|
+
const run = store.get('SELECT id, project_id, bot_id FROM runs WHERE id = ?', runId)
|
|
67
|
+
const target = run && chatStores?.forRun ? chatStores.forRun(run) : store
|
|
68
|
+
target.run(
|
|
69
|
+
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
70
|
+
id('rev'), runId, type, message, nowIso(),
|
|
71
|
+
)
|
|
72
|
+
}
|
|
58
73
|
|
|
59
74
|
const stashLabel = (botId, label) => {
|
|
60
75
|
const list = labelsByBot.get(botId) ?? []
|
|
@@ -108,6 +123,7 @@ export function installSubagentLedger(ctx, store, logger) {
|
|
|
108
123
|
record(owner.runId, 'subagent_spawned', `${exec.name} label=${label ?? '-'} bot=${owner.botId}`)
|
|
109
124
|
byCall.set(exec.callId, { ...owner, label })
|
|
110
125
|
stashLabel(owner.botId, label)
|
|
126
|
+
runningByBot.set(owner.botId, (runningByBot.get(owner.botId) ?? 0) + 1)
|
|
111
127
|
}
|
|
112
128
|
}
|
|
113
129
|
} catch (error) {
|
|
@@ -144,6 +160,7 @@ export function installSubagentLedger(ctx, store, logger) {
|
|
|
144
160
|
const entry = byCall.get(exec?.callId)
|
|
145
161
|
if (!entry) return
|
|
146
162
|
byCall.delete(exec?.callId)
|
|
163
|
+
runningByBot.set(entry.botId, Math.max(0, (runningByBot.get(entry.botId) ?? 1) - 1))
|
|
147
164
|
// 配对暂存的 subagent/end:child id 出现在结果 JSON 里即认领其终态摘要
|
|
148
165
|
let matched = null
|
|
149
166
|
const haystack = JSON.stringify(result ?? {})
|
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(',')]
|