mingdao-harness 0.6.30 → 0.6.32
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/docs/CONFIG.md +24 -0
- package/docs/DEVELOPER.md +91 -0
- package/package.json +3 -1
- package/src/agent.js +100 -25
- package/src/audit.js +60 -37
- package/src/cachestats.js +214 -125
- package/src/config.js +337 -18
- package/src/credentials.js +231 -18
- package/src/ledger.js +18 -5
- package/src/memory.js +3 -3
- package/src/model-discovery.js +43 -17
- package/src/net-guard.js +36 -16
- package/src/providers/index.js +71 -45
- package/src/routing.js +2 -2
- package/src/schedule.js +285 -84
- package/src/sync.js +35 -13
- package/src/task-state.js +7 -0
- package/src/tasks.js +137 -34
- package/src/titles.js +3 -3
- package/src/web/routes/domains/config.js +11 -11
- package/src/web/routes/domains/schedule.js +21 -17
- package/src/web/server.js +7 -5
package/src/cachestats.js
CHANGED
|
@@ -5,7 +5,7 @@ import fs from 'node:fs';
|
|
|
5
5
|
import path from 'node:path';
|
|
6
6
|
import { mingdaoHome, ensureHome } from './config.js';
|
|
7
7
|
import { estimateCost, cacheSplit, beijingDayStart, beijingParts } from './pricing.js';
|
|
8
|
-
import { withFileLockSync, atomicWriteFileSync, appendFilePrivateSync } from './atomic-write.js';
|
|
8
|
+
import { withFileLockSync, withFileLock, atomicWriteFileSync, appendFilePrivateSync } from './atomic-write.js';
|
|
9
9
|
|
|
10
10
|
export function cacheStatsFile() {
|
|
11
11
|
return path.join(mingdaoHome(), 'cache-stats.jsonl');
|
|
@@ -40,108 +40,155 @@ export function cacheStatsWriteError() {
|
|
|
40
40
|
* @param {any} entry
|
|
41
41
|
* @returns {{ok: boolean, error: string|null, phase: 'append'|'rotate'|null}}
|
|
42
42
|
*/
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const merged = [...todayCapped, ...tail.filter((l) => !todaySet.has(l))];
|
|
108
|
-
// 顺序按原文件恢复(避免打乱 byDay 折线的时间序)
|
|
109
|
-
const order = new Map(rows.map((l, k) => [l, k]));
|
|
110
|
-
merged.sort((a, b) => (order.get(a) ?? 0) - (order.get(b) ?? 0));
|
|
111
|
-
atomicWriteFileSync(cacheStatsFile(), merged.join('\n') + '\n');
|
|
112
|
-
}
|
|
43
|
+
/**
|
|
44
|
+
* 构造要落盘的那一行(含 ensureHome);同步/异步写入口共用,防止两份 JSON 字段漂移。
|
|
45
|
+
* @param {any} entry @returns {string}
|
|
46
|
+
*/
|
|
47
|
+
function buildCacheStatsLine(/** @type {any} */ entry) {
|
|
48
|
+
ensureHome(); // 审计 B10:与 audit/journal 一致,目录缺失不静默丢统计
|
|
49
|
+
return JSON.stringify({
|
|
50
|
+
at: Date.now(),
|
|
51
|
+
model: entry.model || '',
|
|
52
|
+
prompt: entry.prompt || 0,
|
|
53
|
+
completion: entry.completion || 0,
|
|
54
|
+
hit: entry.hit ?? null,
|
|
55
|
+
miss: entry.miss ?? null,
|
|
56
|
+
cost: entry.cost ?? null,
|
|
57
|
+
saved: entry.saved ?? null,
|
|
58
|
+
batch: entry.batch === true ? true : undefined, // Batch API 半价任务标记(/cost 分账展示)
|
|
59
|
+
steps: entry.steps ?? undefined, // 本回合步数(状态栏 步数统计)
|
|
60
|
+
llmMs: entry.llmMs ?? undefined, // 模型调用总耗时(状态栏 LLM 时长/tok-s)
|
|
61
|
+
toolMs: entry.toolMs ?? undefined, // 工具执行总耗时(状态栏 工具调用时长)
|
|
62
|
+
firstTokenMs: entry.firstTokenMs ?? undefined, // 首 token 延迟(状态栏平均首 token)
|
|
63
|
+
reasoning: entry.reasoning ?? undefined, // 省钱 B3:推理 token 估算(费用二级分账维度)
|
|
64
|
+
byTool: Array.isArray(entry.byTool) ? entry.byTool : undefined, // 省钱 B3:逐工具 {tool,calls,ms}
|
|
65
|
+
// v0.5.0 A4:垂域 Pack 归因(**标记记录**:cost 记 null 不计入 todayCost,
|
|
66
|
+
// 真实费用仍在回合级记录里 —— 两者相加不会重复计费;packCost 仅供 --by pack 展示)
|
|
67
|
+
pack: entry.pack ? String(entry.pack) : undefined,
|
|
68
|
+
purpose: entry.purpose ? String(entry.purpose) : undefined,
|
|
69
|
+
packCost: Number.isFinite(Number(entry.packCost)) ? Number(entry.packCost) : undefined,
|
|
70
|
+
// v0.4.7(T17):辅助模型调用(路由分类器 / 自动标题 / 记忆提炼)标记——
|
|
71
|
+
// 这些调用不在 agent 回合 usage 里,单独入账,不与回合级重复计费。
|
|
72
|
+
aux: entry.aux === true ? true : undefined,
|
|
73
|
+
auxReason: entry.auxReason ? String(entry.auxReason) : undefined,
|
|
74
|
+
// v0.6.7(报告一 H-4 / M-10):这笔消费**用量未知**(服务端没回 usage、或批次在拿不到
|
|
75
|
+
// token 数的情况下失败/被取消)。金额按 null 记,但 todayCost 会把条数汇总成可见信号,
|
|
76
|
+
// 免得"没记账"被读成"没花钱"。
|
|
77
|
+
usageUnknown: entry.usageUnknown === true ? true : undefined,
|
|
78
|
+
note: entry.note ? String(entry.note).slice(0, 200) : undefined,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 锁内临界区:追加 + 低频轮转。**追加与轮转必须同一把锁**(v0.6.3 BUG-009):
|
|
84
|
+
* 原实现「锁外追加 + 锁内轮转」时,B 的行落在 A 的读/写之间会被整文件替换覆盖;
|
|
85
|
+
* 丢的是费用明细,而它是日费用护栏的依据(少计 → 超支而不自知)。
|
|
86
|
+
* `markAppended` 在追加成功、**轮转之前**调用:轮转失败要如实报成 rotate,不能报成 append。
|
|
87
|
+
* @param {string} line @param {() => void} markAppended
|
|
88
|
+
*/
|
|
89
|
+
function appendCacheStatsUnderLock(/** @type {string} */ line, /** @type {() => void} */ markAppended) {
|
|
90
|
+
appendFilePrivateSync(cacheStatsFile(), line + '\n');
|
|
91
|
+
markAppended();
|
|
92
|
+
if (fs.statSync(cacheStatsFile()).size > MAX_BYTES) {
|
|
93
|
+
// 审计 P2-3(v0.4.2):轮转的 read-modify-write 同样有并发窗口,现在它与追加在同一锁内。
|
|
94
|
+
// v0.4.6 P2:轮转必须保留**当天全部**记录。此前只保留最后 KEEP_LINES 行——
|
|
95
|
+
// 单进程写满 2 万条后,当天早先的费用会被裁掉,todayCost() 随之变小,
|
|
96
|
+
// 日费用护栏被静默重置(用户可再次超支而不自知)。当天行(最多 MAX_LINES 条)
|
|
97
|
+
// 与最近 KEEP_LINES 行取并集,去重且保持原顺序。
|
|
98
|
+
const raw = fs.readFileSync(cacheStatsFile(), 'utf8');
|
|
99
|
+
const rows = raw.split('\n').filter(Boolean);
|
|
100
|
+
if (rows.length > MAX_LINES) {
|
|
101
|
+
const dayStart = beijingDayStart().getTime();
|
|
102
|
+
const todayLines = rows.filter((/** @type {string} */ l) => {
|
|
103
|
+
try {
|
|
104
|
+
return (JSON.parse(l).at || 0) >= dayStart;
|
|
105
|
+
} catch {
|
|
106
|
+
return false;
|
|
113
107
|
}
|
|
114
108
|
});
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
` 今日费用护栏会**少计**这部分消费(可能超支而不知);请检查 ${cacheStatsFile()} 的磁盘空间与权限。`
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
return { ok: false, error: msg, phase: 'append' };
|
|
128
|
-
}
|
|
129
|
-
// 追加已成功、只有轮转失败:丢的不是账,而是文件会持续增长
|
|
130
|
-
if (!rotateWarned) {
|
|
131
|
-
rotateWarned = true;
|
|
132
|
-
console.warn(`[MingDao] ⚠ 费用明细轮转失败:${msg}\n ${cacheStatsFile()} 会持续增长(不影响计费,但请检查磁盘空间/权限)。`);
|
|
133
|
-
}
|
|
134
|
-
return { ok: false, error: msg, phase: 'rotate' };
|
|
109
|
+
const tail = rows.slice(-KEEP_LINES);
|
|
110
|
+
const tailSet = new Set(tail);
|
|
111
|
+
const todayCapped = todayLines.length > MAX_LINES ? todayLines.slice(-MAX_LINES) : todayLines;
|
|
112
|
+
const todaySet = new Set(todayCapped);
|
|
113
|
+
const merged = [...todayCapped, ...tail.filter((l) => !todaySet.has(l))];
|
|
114
|
+
// 顺序按原文件恢复(避免打乱 byDay 折线的时间序)
|
|
115
|
+
const order = new Map(rows.map((l, k) => [l, k]));
|
|
116
|
+
merged.sort((a, b) => (order.get(a) ?? 0) - (order.get(b) ?? 0));
|
|
117
|
+
atomicWriteFileSync(cacheStatsFile(), merged.join('\n') + '\n');
|
|
135
118
|
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 写失败的统一收口:区分「追加失败(真的丢账)」与「轮转失败(文件会变大,不丢账)」。
|
|
123
|
+
* @param {any} err @param {boolean} appended
|
|
124
|
+
* @returns {{ok: false, error: string, phase: 'append'|'rotate'}} */
|
|
125
|
+
function cacheStatsFailure(/** @type {any} */ err, /** @type {boolean} */ appended) {
|
|
126
|
+
const msg = String(/** @type {any} */ (err)?.message ?? err);
|
|
127
|
+
lastWriteError = msg;
|
|
128
|
+
if (!appended) {
|
|
129
|
+
// 只提示一次:写不进去通常是持续性问题(磁盘满/权限),每回合刷屏反而让人忽略它
|
|
139
130
|
if (!writeWarned) {
|
|
140
131
|
writeWarned = true;
|
|
141
|
-
console.warn(
|
|
132
|
+
console.warn(
|
|
133
|
+
`[MingDao] ⚠ 费用明细写入失败:${msg}\n` +
|
|
134
|
+
` 今日费用护栏会**少计**这部分消费(可能超支而不知);请检查 ${cacheStatsFile()} 的磁盘空间与权限。`
|
|
135
|
+
);
|
|
142
136
|
}
|
|
143
137
|
return { ok: false, error: msg, phase: 'append' };
|
|
144
138
|
}
|
|
139
|
+
// 追加已成功、只有轮转失败:丢的不是账,而是文件会持续增长
|
|
140
|
+
if (!rotateWarned) {
|
|
141
|
+
rotateWarned = true;
|
|
142
|
+
console.warn(`[MingDao] ⚠ 费用明细轮转失败:${msg}\n ${cacheStatsFile()} 会持续增长(不影响计费,但请检查磁盘空间/权限)。`);
|
|
143
|
+
}
|
|
144
|
+
return { ok: false, error: msg, phase: 'rotate' };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 记录一条缓存/用量明细(**同步版**:CLI/REPL 与 tasks worker 进程用)。
|
|
149
|
+
* @param {any} entry
|
|
150
|
+
* @returns {{ok: boolean, error: string|null, phase: 'append'|'rotate'|null}}
|
|
151
|
+
*/
|
|
152
|
+
export function recordCacheStats(/** @type {any} */ entry) {
|
|
153
|
+
let line;
|
|
154
|
+
try {
|
|
155
|
+
line = buildCacheStatsLine(entry);
|
|
156
|
+
} catch (err) {
|
|
157
|
+
return cacheStatsFailure(err, false);
|
|
158
|
+
}
|
|
159
|
+
let appended = false;
|
|
160
|
+
try {
|
|
161
|
+
withFileLockSync(cacheStatsFile() + '.lock', () => appendCacheStatsUnderLock(line, () => {
|
|
162
|
+
appended = true;
|
|
163
|
+
}));
|
|
164
|
+
} catch (err) {
|
|
165
|
+
return cacheStatsFailure(err, appended);
|
|
166
|
+
}
|
|
167
|
+
return { ok: true, error: null, phase: null };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 记录一条缓存/用量明细 —— **WebUI 请求路径用它**(v0.7.0 §1.4)。
|
|
172
|
+
* 本函数在聊天收尾路径上、每回合至少一条;同步版在锁争用时用 `Atomics.wait` 睡眠,
|
|
173
|
+
* 会把整个 WebUI 事件循环(含其它会话的 SSE)冻住。这里 `await withFileLock` 让出事件循环。
|
|
174
|
+
* @param {any} entry
|
|
175
|
+
* @returns {Promise<{ok: boolean, error: string|null, phase: 'append'|'rotate'|null}>}
|
|
176
|
+
*/
|
|
177
|
+
export async function recordCacheStatsAsync(/** @type {any} */ entry) {
|
|
178
|
+
let line;
|
|
179
|
+
try {
|
|
180
|
+
line = buildCacheStatsLine(entry);
|
|
181
|
+
} catch (err) {
|
|
182
|
+
return cacheStatsFailure(err, false);
|
|
183
|
+
}
|
|
184
|
+
let appended = false;
|
|
185
|
+
try {
|
|
186
|
+
await withFileLock(cacheStatsFile() + '.lock', () => appendCacheStatsUnderLock(line, () => {
|
|
187
|
+
appended = true;
|
|
188
|
+
}));
|
|
189
|
+
} catch (err) {
|
|
190
|
+
return cacheStatsFailure(err, appended);
|
|
191
|
+
}
|
|
145
192
|
return { ok: true, error: null, phase: null };
|
|
146
193
|
}
|
|
147
194
|
|
|
@@ -211,9 +258,10 @@ export function summarizeCacheStats(/** @type {any} */ entries) {
|
|
|
211
258
|
return sum;
|
|
212
259
|
}
|
|
213
260
|
|
|
214
|
-
// 记录一次用量(agent 侧调用):自动计算命中拆分与节省额
|
|
215
261
|
// 记录一次用量(agent 侧调用):自动计算命中拆分与节省额;perf 为回合性能指标(状态栏)
|
|
216
|
-
|
|
262
|
+
/** 计算并组装一条用量记录(纯计算,不落盘);同步/异步入口共用,防口径漂移。
|
|
263
|
+
* @param {any} modelName @param {any} usage @param {any} perf */
|
|
264
|
+
function buildUsageEntry(/** @type {any} */ modelName, /** @type {any} */ usage, /** @type {any} */ perf) {
|
|
217
265
|
const split = cacheSplit(usage);
|
|
218
266
|
// v0.4.6:峰谷单价按「请求发起时刻」判定。响应落地与发起可能跨 12:00/18:00 边界,
|
|
219
267
|
// 用 new Date()(落账时刻)会把高峰调用记成闲时价(或少记一半),日累计与护栏随之偏移。
|
|
@@ -239,7 +287,7 @@ export function recordUsage(/** @type {any} */ modelName, /** @type {any} */ usa
|
|
|
239
287
|
// 覆盖内置定价 + 外部定价 + config.pricing.overrides 三条来源,不再依赖 modelPreset 单一判断。
|
|
240
288
|
cost = estimateCost(modelName, prompt, completion, null, priceAt);
|
|
241
289
|
}
|
|
242
|
-
return
|
|
290
|
+
return {
|
|
243
291
|
model: modelName,
|
|
244
292
|
prompt,
|
|
245
293
|
completion,
|
|
@@ -255,7 +303,22 @@ export function recordUsage(/** @type {any} */ modelName, /** @type {any} */ usa
|
|
|
255
303
|
firstTokenMs: perf?.firstTokenMs ?? undefined,
|
|
256
304
|
reasoning: perf?.reasoningTokens ?? undefined, // 省钱 B3
|
|
257
305
|
byTool: perf?.toolStats ?? undefined, // 省钱 B3
|
|
258
|
-
}
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** 同步记录一次用量(CLI/REPL/worker 进程)。
|
|
310
|
+
* @param {any} modelName @param {any} usage @param {any} perf */
|
|
311
|
+
export function recordUsage(/** @type {any} */ modelName, /** @type {any} */ usage, /** @type {any} */ perf = null) {
|
|
312
|
+
return recordCacheStats(buildUsageEntry(modelName, usage, perf));
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* 异步记录一次用量 —— **WebUI 聊天收尾路径用它**(每回合至少一条,v0.7.0 §1.4)。
|
|
317
|
+
* @param {any} modelName @param {any} usage @param {any} perf
|
|
318
|
+
* @returns {Promise<{ok: boolean, error: string|null, phase: 'append'|'rotate'|null}>}
|
|
319
|
+
*/
|
|
320
|
+
export async function recordUsageAsync(/** @type {any} */ modelName, /** @type {any} */ usage, /** @type {any} */ perf = null) {
|
|
321
|
+
return recordCacheStatsAsync(buildUsageEntry(modelName, usage, perf));
|
|
259
322
|
}
|
|
260
323
|
|
|
261
324
|
export function formatCacheSummary(/** @type {any} */ sum) {
|
|
@@ -276,33 +339,41 @@ export function formatCacheSummary(/** @type {any} */ sum) {
|
|
|
276
339
|
* @param {any} modelName @param {any} usage @param {string} [reason]
|
|
277
340
|
* @param {{requestStartAt?: number}|null} [perf] 峰谷价锚点(请求发起时刻);缺省时退回落账时刻
|
|
278
341
|
*/
|
|
279
|
-
|
|
342
|
+
/** 组装一条辅助调用记录;返回 null 表示"没有可记的 token"(不算错误)。
|
|
343
|
+
* @param {any} modelName @param {any} usage @param {string} reason @param {any} perf */
|
|
344
|
+
function buildAuxEntry(/** @type {any} */ modelName, /** @type {any} */ usage, /** @type {string} */ reason, /** @type {any} */ perf) {
|
|
345
|
+
const prompt = usage.prompt_tokens || 0;
|
|
346
|
+
const completion = usage.completion_tokens || 0;
|
|
347
|
+
if (!prompt && !completion) return null;
|
|
348
|
+
const split = cacheSplit(usage);
|
|
349
|
+
// 审计 BUG-048(实测复现):辅助调用(路由分类/标题/记忆抽取)此前**漏传 priceAt**,
|
|
350
|
+
// 于是 estimateCost 的第 5 参走默认 `new Date()` —— 按**落账时刻**选峰谷价,
|
|
351
|
+
// 与 recordUsage 的「按请求发起时刻」两套口径(实测同一笔调用 0.48 元 vs 0.24 元,
|
|
352
|
+
// 在 09:00/12:00/14:00/18:00 边界上正好差一倍)。这里与 recordUsage 对齐。
|
|
353
|
+
const startAt = Number(perf?.requestStartAt) || 0;
|
|
354
|
+
const priceAt = startAt > 0 ? new Date(startAt) : undefined;
|
|
355
|
+
const cost = split
|
|
356
|
+
? estimateCost(modelName, prompt, completion, split, priceAt)
|
|
357
|
+
: estimateCost(modelName, prompt, completion, null, priceAt);
|
|
358
|
+
return {
|
|
359
|
+
model: modelName,
|
|
360
|
+
prompt,
|
|
361
|
+
completion,
|
|
362
|
+
hit: split?.hit ?? null,
|
|
363
|
+
miss: split?.miss ?? null,
|
|
364
|
+
cost,
|
|
365
|
+
saved: null,
|
|
366
|
+
aux: true,
|
|
367
|
+
auxReason: reason,
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
export function recordAuxUsage(/** @type {any} */ modelName, /** @type {any} */ usage, /** @type {string} */ reason = 'aux', /** @type {{requestStartAt?: number}|null} */ perf = null) {
|
|
280
372
|
if (!usage) return { ok: true, error: null, phase: null };
|
|
281
373
|
try {
|
|
282
|
-
const
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
const split = cacheSplit(usage);
|
|
286
|
-
// 审计 BUG-048(实测复现):辅助调用(路由分类/标题/记忆抽取)此前**漏传 priceAt**,
|
|
287
|
-
// 于是 estimateCost 的第 5 参走默认 `new Date()` —— 按**落账时刻**选峰谷价,
|
|
288
|
-
// 与 recordUsage 的「按请求发起时刻」两套口径(实测同一笔调用 0.48 元 vs 0.24 元,
|
|
289
|
-
// 在 09:00/12:00/14:00/18:00 边界上正好差一倍)。这里与 recordUsage 对齐。
|
|
290
|
-
const startAt = Number(perf?.requestStartAt) || 0;
|
|
291
|
-
const priceAt = startAt > 0 ? new Date(startAt) : undefined;
|
|
292
|
-
const cost = split
|
|
293
|
-
? estimateCost(modelName, prompt, completion, split, priceAt)
|
|
294
|
-
: estimateCost(modelName, prompt, completion, null, priceAt);
|
|
295
|
-
return recordCacheStats({
|
|
296
|
-
model: modelName,
|
|
297
|
-
prompt,
|
|
298
|
-
completion,
|
|
299
|
-
hit: split?.hit ?? null,
|
|
300
|
-
miss: split?.miss ?? null,
|
|
301
|
-
cost,
|
|
302
|
-
saved: null,
|
|
303
|
-
aux: true,
|
|
304
|
-
auxReason: reason,
|
|
305
|
-
});
|
|
374
|
+
const entry = buildAuxEntry(modelName, usage, reason, perf);
|
|
375
|
+
if (!entry) return { ok: true, error: null, phase: null };
|
|
376
|
+
return recordCacheStats(entry);
|
|
306
377
|
} catch (err) {
|
|
307
378
|
// 辅助调用的计费同样不能静默丢(自动路由/标题/记忆提炼的费用也是钱)。
|
|
308
379
|
// 注意不在这里写 lastWriteError:本 try 也覆盖计价计算,真正的**写**失败已由
|
|
@@ -310,7 +381,25 @@ export function recordAuxUsage(modelName, usage, reason = 'aux', /** @type {{req
|
|
|
310
381
|
const msg = String(/** @type {any} */ (err)?.message ?? err);
|
|
311
382
|
return { ok: false, error: msg, phase: null };
|
|
312
383
|
}
|
|
313
|
-
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* 异步记录一次辅助模型调用(v0.7.0 §1.4)——**WebUI 的自动标题 / 记忆提炼 / 路由分类**用它。
|
|
388
|
+
* 这些调用都在请求路径上(常在回合收尾 await),同步锁争用会冻结其它会话。
|
|
389
|
+
* @param {any} modelName @param {any} usage @param {string} [reason]
|
|
390
|
+
* @param {{requestStartAt?: number}|null} [perf]
|
|
391
|
+
* @returns {Promise<{ok: boolean, error: string|null, phase: 'append'|'rotate'|null}>}
|
|
392
|
+
*/
|
|
393
|
+
export async function recordAuxUsageAsync(/** @type {any} */ modelName, /** @type {any} */ usage, /** @type {string} */ reason = 'aux', /** @type {{requestStartAt?: number}|null} */ perf = null) {
|
|
394
|
+
if (!usage) return { ok: true, error: null, phase: null };
|
|
395
|
+
try {
|
|
396
|
+
const entry = buildAuxEntry(modelName, usage, reason, perf);
|
|
397
|
+
if (!entry) return { ok: true, error: null, phase: null };
|
|
398
|
+
return await recordCacheStatsAsync(entry);
|
|
399
|
+
} catch (err) {
|
|
400
|
+
const msg = String(/** @type {any} */ (err)?.message ?? err);
|
|
401
|
+
return { ok: false, error: msg, phase: null };
|
|
402
|
+
}
|
|
314
403
|
}
|
|
315
404
|
|
|
316
405
|
/**
|