balance-dsh 2.2.3 → 2.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +95 -0
- package/lib/client.js +636 -37
- package/lib/index.js +303 -9
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -1514,6 +1514,15 @@ window.__ModuleLoader__.load({
|
|
|
1514
1514
|
.balance-settings__update-inline::before{content:"·";margin:0 2px 0 0;color:var(--dsw-alias-label-tertiary)}
|
|
1515
1515
|
.balance-settings__update-note{margin:7px 0 0;font-size:11.5px;line-height:17px;color:var(--dsw-alias-label-tertiary)}
|
|
1516
1516
|
.balance-settings__actions{display:flex;align-items:center;gap:8px;margin:14px 0 0;padding:12px 0 0;border-top:.5px solid var(--dsw-alias-border-l2,rgba(127,127,127,.22))}
|
|
1517
|
+
/* 数据诊断:按钮组 + 报告块。
|
|
1518
|
+
报告是等宽文本、要能整段选中复制,所以 pre + user-select:text;
|
|
1519
|
+
放不下时自己滚(max-height 限制高度,别把设置页撑到几屏长)。 */
|
|
1520
|
+
.balance-settings__diag-btns{display:flex;align-items:center;gap:6px;flex:none}
|
|
1521
|
+
.balance-settings__diag{box-sizing:border-box;margin:8px 0 0;padding:10px 12px;max-height:320px;overflow:auto;border:.5px solid var(--dsw-alias-border-l2,rgba(127,127,127,.22));border-radius:var(--dsw-radius-md,8px);background:var(--dsw-alias-bg-base,rgba(0,0,0,.18));color:var(--dsw-alias-label-secondary);font-family:ui-monospace,SFMono-Regular,Consolas,monospace;font-size:11.5px;line-height:1.65;white-space:pre;tab-size:2;user-select:text;-webkit-user-select:text}
|
|
1522
|
+
.balance-settings__diag--error{color:var(--dsw-alias-state-error-primary,#e5484d)}
|
|
1523
|
+
.balance-settings__diag-fix{margin:8px 0 0;font-size:12px;line-height:17px}
|
|
1524
|
+
.balance-settings__diag-fix--ok{color:var(--dsw-alias-state-success-primary,#3fb950)}
|
|
1525
|
+
.balance-settings__diag-fix--warn{color:var(--dsw-alias-state-warn-primary,#d97706)}
|
|
1517
1526
|
.balance-settings__status{flex:1;min-width:0;font-size:12px;line-height:17px;color:var(--dsw-alias-label-tertiary)}
|
|
1518
1527
|
.balance-settings__status--error{color:var(--dsw-alias-state-error-primary,#e5484d)}
|
|
1519
1528
|
.balance-settings__status--ok{color:var(--dsw-alias-state-success-primary,#22c55e)}
|
|
@@ -1861,6 +1870,10 @@ window.__ModuleLoader__.load({
|
|
|
1861
1870
|
* `/balance-api?geom=1`,然后 `GET /balance-api?diag=1` 的 `floatGeom` 就能看到。
|
|
1862
1871
|
* 节流是必须的:measure 每 400ms 跑一次,无脑发会把还要算余额/今日的接口刷满。 */
|
|
1863
1872
|
let floatGeomSentAt = 0;
|
|
1873
|
+
// 「让浮窗立刻重新量一次」的钩子(2026-11-09,配诊断里的「修复」按钮)。
|
|
1874
|
+
// measure 定义在浮窗组件的 effect 里,设置卡片根本够不着,只能这样接一根线。
|
|
1875
|
+
// 没挂上时(浮窗没渲染 / 不在对话页)调它就是空操作,不报错。
|
|
1876
|
+
let floatRemeasureHook = null;
|
|
1864
1877
|
let floatGeomKey = "";
|
|
1865
1878
|
function reportFloatGeom(payload) {
|
|
1866
1879
|
try {
|
|
@@ -2610,6 +2623,7 @@ const CN_CALENDAR_SOURCES = [
|
|
|
2610
2623
|
["usageLoadingStyle", "2.0.9"], // 消耗行加载动效:样式1(光团开窗)/ 样式2(计算中),含行内实时预览
|
|
2611
2624
|
["todaySessionLimit", "2.1.0"], // 今日消耗扫描上限:多开对话 / 多跑子代理时把今日汇总算全
|
|
2612
2625
|
["showUsagePill", "2.1.6"], // 2.1.6:消耗行搬成对话界面右下角小浮窗(两行:本会话估算 / 金额)
|
|
2626
|
+
["diag", "2.2.5"], // 2.2.5:设置页「数据诊断」—— 一键出结论 + 复制 + 查出问题给「修复」
|
|
2613
2627
|
]);
|
|
2614
2628
|
|
|
2615
2629
|
/** NEW_TAGS 的 key → 设置项中文名。横幅要写「本版新增:消耗行加载动效」,
|
|
@@ -2621,6 +2635,7 @@ const CN_CALENDAR_SOURCES = [
|
|
|
2621
2635
|
usageLoadingStyle: "消耗行加载动效",
|
|
2622
2636
|
todaySessionLimit: "今日消耗扫描上限",
|
|
2623
2637
|
showUsagePill: "对话右下角消耗浮窗",
|
|
2638
|
+
diag: "数据诊断",
|
|
2624
2639
|
};
|
|
2625
2640
|
|
|
2626
2641
|
/** 模板串里取一张表的三个 NEW 相关字段,缺省一律安全兜底。 */
|
|
@@ -3134,7 +3149,45 @@ const CN_CALENDAR_SOURCES = [
|
|
|
3134
3149
|
// 等待期间先摆一个不可见占位,让 meta 行高度从一开始就是最终高度 ——
|
|
3135
3150
|
// 否则「今日 ¥x」这个 flex-basis:100% 的元素一插入,整行就从 1 行变 2 行,
|
|
3136
3151
|
// 侧栏底部整体被顶下 14px,冷启动繁忙时看着就是「今日余额弹出来那下卡了」。
|
|
3137
|
-
const todayCache = { value: null, inFlight: null, at: 0, tried: false, ttl: 55000, retries: 0, settled: false };
|
|
3152
|
+
const todayCache = { value: null, inFlight: null, at: 0, tried: false, ttl: 55000, retries: 0, settled: false, suspect: false };
|
|
3153
|
+
// ─── 可疑的 0 不许就地定死(2026-11-09 用户报告:「今日一直显示 ¥0.00,刷新页面才正常」)───
|
|
3154
|
+
// 冷启动最初几秒 DSH 的会话索引还没就绪,`?today=1` 算出来的是「没法看」而不是「没花钱」。
|
|
3155
|
+
// 原来只按 3.2 秒补拉 3 次(约 10 秒)就放弃并 settled,而**此后没有任何时机再问一次**:
|
|
3156
|
+
// · 余额轮询不走 today;浮窗走的是 `?lite=1&session=`,服务端在「没有快照基线」时
|
|
3157
|
+
// 增量直接 null、且 lite 明确不回退全量 → 响应里没有 today → 客户端不触发刷新;
|
|
3158
|
+
// · 当天快照又因为可疑不落盘 → 增量永远没有基线 → 这个 0 能挂一整天。
|
|
3159
|
+
// 现在退避重试:15s → 30s → 60s → 120s → 300s → 封顶 900s,直到拿到可信值(或跨天后自然拿到)。
|
|
3160
|
+
// ⚠️ 封顶不能太小:今天**真的**没花钱时 suspiciousEmpty 也为 true(零点后必然如此),
|
|
3161
|
+
// 而可疑值服务端不落盘 → 每次重试都是一次全量扫描。15 分钟一次是「能及时纠正」与
|
|
3162
|
+
// 「别把主进程一直按在解压上」的折中(一次约 400ms)。
|
|
3163
|
+
const TODAY_SUSPECT_RETRY_MS = [15000, 30000, 60000, 120000, 300000, 900000];
|
|
3164
|
+
let todaySuspectTimer = null;
|
|
3165
|
+
let todaySuspectTries = 0;
|
|
3166
|
+
function clearTodaySuspectRetry() {
|
|
3167
|
+
if (todaySuspectTimer !== null) {
|
|
3168
|
+
clearTimeout(todaySuspectTimer);
|
|
3169
|
+
todaySuspectTimer = null;
|
|
3170
|
+
}
|
|
3171
|
+
todaySuspectTries = 0;
|
|
3172
|
+
}
|
|
3173
|
+
function scheduleTodaySuspectRetry() {
|
|
3174
|
+
if (todaySuspectTimer !== null) return; // 已经排着就等它,别叠
|
|
3175
|
+
const delay = TODAY_SUSPECT_RETRY_MS[Math.min(todaySuspectTries, TODAY_SUSPECT_RETRY_MS.length - 1)];
|
|
3176
|
+
todaySuspectTries += 1;
|
|
3177
|
+
todaySuspectTimer = setTimeout(() => {
|
|
3178
|
+
todaySuspectTimer = null;
|
|
3179
|
+
// force=true:可疑值本来就不该被 TTL 压住(服务端那边也没缓存它)
|
|
3180
|
+
void fetchTodayOnce(true);
|
|
3181
|
+
}, delay);
|
|
3182
|
+
}
|
|
3183
|
+
/** 这一拍的 0 是不是「不可信」(列不出会话 / 筛不出会话 / 有会话被让路跳过)。 */
|
|
3184
|
+
function todayZeroIsSuspect(value) {
|
|
3185
|
+
if (value == null || typeof value !== "object") return false;
|
|
3186
|
+
if (Number(value.cost ?? 0) !== 0) return false;
|
|
3187
|
+
return value.suspiciousEmpty === true
|
|
3188
|
+
|| value.emptyListing === true
|
|
3189
|
+
|| Number(value.skipped ?? 0) > 0;
|
|
3190
|
+
}
|
|
3138
3191
|
const todayListeners = new Set();
|
|
3139
3192
|
function notifyToday() {
|
|
3140
3193
|
// ⚠️ 逐个订阅者隔离(2026-10-31 全量审查)。任何一个订阅者抛错都不能
|
|
@@ -3173,6 +3226,11 @@ const CN_CALENDAR_SOURCES = [
|
|
|
3173
3226
|
? { ...prevToday, provisional: true }
|
|
3174
3227
|
: data.today;
|
|
3175
3228
|
todayCache.at = Date.now();
|
|
3229
|
+
// 可疑的 0:照常显示(2.2.2 的定案 —— 0 也不许让「今日」整行消失),
|
|
3230
|
+
// 但**不许就此定格**,排一条低频退避重试把它纠正回来(见 TODAY_SUSPECT_RETRY_MS)。
|
|
3231
|
+
todayCache.suspect = todayZeroIsSuspect(todayCache.value);
|
|
3232
|
+
if (todayCache.suspect) scheduleTodaySuspectRetry();
|
|
3233
|
+
else clearTodaySuspectRetry();
|
|
3176
3234
|
const partial = data.todayPartial === true && todayCache.retries < 3;
|
|
3177
3235
|
todayCache.ttl = partial ? 3000 : 20000;
|
|
3178
3236
|
// 「首屏数据是否已经定型」:partial 是**同一次计算的分阶段**(先给快照、
|
|
@@ -3188,6 +3246,9 @@ const CN_CALENDAR_SOURCES = [
|
|
|
3188
3246
|
setTimeout(() => { void fetchTodayOnce(true); }, 3200);
|
|
3189
3247
|
} else {
|
|
3190
3248
|
todayCache.retries = 0;
|
|
3249
|
+
// 补拉用完时还是可疑 0 → 立刻换成「短间隔」那档接着追(15 秒),
|
|
3250
|
+
// 别让用户对着一个 0 干等到下一档 30/60 秒。定时器已排着就不重复。
|
|
3251
|
+
if (todayCache.suspect) scheduleTodaySuspectRetry();
|
|
3191
3252
|
}
|
|
3192
3253
|
}
|
|
3193
3254
|
} catch {
|
|
@@ -5931,7 +5992,45 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
5931
5992
|
// 老版本 DSH 没有 data-composer-card:退回 dock 行顶边,同样留空隙
|
|
5932
5993
|
if (anchorBottom === null) anchorBottom = dockRect.top;
|
|
5933
5994
|
const bottom = Math.max(8, Math.round(vh - anchorBottom + FLOAT_EDGE_GAP));
|
|
5995
|
+
// ── 位置变化用不用滑动过渡(2026-11-03 用户:「当收起侧边栏时 会话消耗的浮窗会更新位置
|
|
5996
|
+
// 但是是闪过去的 能不能添加滑动动画」)────────────────────────────────
|
|
5997
|
+
// 判据只有一条:**这一步到底是「视口宽没变、位置变了」还是「视口宽也变了」**。
|
|
5998
|
+
// · 视口宽没变 → 是 DSH 自己的布局在动(收起/展开侧边栏 → 输入卡片变宽),
|
|
5999
|
+
// 给 150ms 的 `left/bottom` 过渡,卡片滑过去而不是闪过去;
|
|
6000
|
+
// · 视口宽变了 → 用户在拖窗口边缘,这次必须**不挂过渡**,否则卡片滞后于窗口,
|
|
6001
|
+
// 拖动时像被橡皮筋拽着(4 拍补拍都还在,位置本身不会错)。
|
|
6002
|
+
// 位移 ≤ FLOAT_JITTER_PX 的那种抖动早已被下面 setFloatBox 吞掉,走不到这里。
|
|
6003
|
+
// 写进 style 的是**补偿过包含块偏移**的值(见 cbOffsetX 的注释):
|
|
6004
|
+
// 视口目标 (left, bottom) − 包含块偏移 = fixed 实际需要的 left/bottom。
|
|
6005
|
+
const styleLeft = Math.round(left - cbOffsetX);
|
|
6006
|
+
const styleBottom = Math.round(bottom - cbOffsetBottom);
|
|
6007
|
+
// ⚠️ 包含块自己正在动(展开/收起侧边栏:容器一边变宽一边平移)时**不许挂 left 过渡**。
|
|
6008
|
+
// 2026-11-06 用户:「展开侧边栏的时候 小浮窗会往左边跑一下」。两个动画源打架:
|
|
6009
|
+
// 容器原点每帧在移动(fixed 后代跟着它走),同时 `left` 又在 150ms 里追一个移动中的目标 ——
|
|
6010
|
+
// 屏幕上就是先偏出去再被拉回来。包含块一动就瞬时对齐(容器本身是平滑的,看着照样连续)。
|
|
6011
|
+
const cbMoved = floatCbOffset === null
|
|
6012
|
+
|| floatCbOffset.x !== cbOffsetX
|
|
6013
|
+
|| floatCbOffset.bottom !== cbOffsetBottom;
|
|
6014
|
+
floatCbOffset = { x: cbOffsetX, bottom: cbOffsetBottom };
|
|
6015
|
+
// ⚠️ 位置**直接写 DOM**,不经过 React(2026-11-06:「有点掉帧的感觉」)。
|
|
6016
|
+
// 侧栏展开/收起是 width 过渡,这个值每帧都在变;走 state 就等于每帧重渲染整个浮窗子树
|
|
6017
|
+
// (标签 + 团队圆片 + 迷雾 + 滚格子),而位置只是两个长度,直接写 style 就够了。
|
|
6018
|
+
// React 那边因此**不再管 left/bottom**(见渲染处),也就不会在下次渲染时把新值覆盖回去;
|
|
6019
|
+
// 元素首次挂载由 pre-paint 的这一次 measure 写入,不会露 CSS 兜底。
|
|
6020
|
+
if (selfElRef !== null && selfElRef.style !== null && typeof selfElRef.style === "object") {
|
|
6021
|
+
try {
|
|
6022
|
+
selfElRef.style.left = styleLeft + "px";
|
|
6023
|
+
selfElRef.style.bottom = styleBottom + "px";
|
|
6024
|
+
selfElRef.style.right = "auto";
|
|
6025
|
+
} catch {
|
|
6026
|
+
/* 写不进去就靠下面的 setFloatBox 兜底 */
|
|
6027
|
+
}
|
|
6028
|
+
}
|
|
5934
6029
|
// 排障采样:这一拍到底量到了什么(见 reportFloatGeom 的注释)。
|
|
6030
|
+
// ⚠️ **必须放在写 style 之后**(2026-11-09):self 这一项读的是「卡片现在到底在哪」,
|
|
6031
|
+
// 放在写之前读到的是**上一拍**渲染的位置 —— 位置刚变过就会差好几像素,
|
|
6032
|
+
// 诊断里白白报一个「✗ 偏移」(实测差 6px)。写完之后读,getBoundingClientRect
|
|
6033
|
+
// 会强制刷新布局,拿到的才是这次写进去的落点。
|
|
5935
6034
|
// 代价只是几次 getBoundingClientRect,POST 有节流,出问题时可从 ?diag=1 直接读。
|
|
5936
6035
|
try {
|
|
5937
6036
|
// ⚠️ 采样本身要花钱(两次 querySelectorAll + 最多八个 getBoundingClientRect)。
|
|
@@ -5975,40 +6074,6 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
5975
6074
|
} catch {
|
|
5976
6075
|
/* ignore */
|
|
5977
6076
|
}
|
|
5978
|
-
// ── 位置变化用不用滑动过渡(2026-11-03 用户:「当收起侧边栏时 会话消耗的浮窗会更新位置
|
|
5979
|
-
// 但是是闪过去的 能不能添加滑动动画」)────────────────────────────────
|
|
5980
|
-
// 判据只有一条:**这一步到底是「视口宽没变、位置变了」还是「视口宽也变了」**。
|
|
5981
|
-
// · 视口宽没变 → 是 DSH 自己的布局在动(收起/展开侧边栏 → 输入卡片变宽),
|
|
5982
|
-
// 给 150ms 的 `left/bottom` 过渡,卡片滑过去而不是闪过去;
|
|
5983
|
-
// · 视口宽变了 → 用户在拖窗口边缘,这次必须**不挂过渡**,否则卡片滞后于窗口,
|
|
5984
|
-
// 拖动时像被橡皮筋拽着(4 拍补拍都还在,位置本身不会错)。
|
|
5985
|
-
// 位移 ≤ FLOAT_JITTER_PX 的那种抖动早已被下面 setFloatBox 吞掉,走不到这里。
|
|
5986
|
-
// 写进 style 的是**补偿过包含块偏移**的值(见 cbOffsetX 的注释):
|
|
5987
|
-
// 视口目标 (left, bottom) − 包含块偏移 = fixed 实际需要的 left/bottom。
|
|
5988
|
-
const styleLeft = Math.round(left - cbOffsetX);
|
|
5989
|
-
const styleBottom = Math.round(bottom - cbOffsetBottom);
|
|
5990
|
-
// ⚠️ 包含块自己正在动(展开/收起侧边栏:容器一边变宽一边平移)时**不许挂 left 过渡**。
|
|
5991
|
-
// 2026-11-06 用户:「展开侧边栏的时候 小浮窗会往左边跑一下」。两个动画源打架:
|
|
5992
|
-
// 容器原点每帧在移动(fixed 后代跟着它走),同时 `left` 又在 150ms 里追一个移动中的目标 ——
|
|
5993
|
-
// 屏幕上就是先偏出去再被拉回来。包含块一动就瞬时对齐(容器本身是平滑的,看着照样连续)。
|
|
5994
|
-
const cbMoved = floatCbOffset === null
|
|
5995
|
-
|| floatCbOffset.x !== cbOffsetX
|
|
5996
|
-
|| floatCbOffset.bottom !== cbOffsetBottom;
|
|
5997
|
-
floatCbOffset = { x: cbOffsetX, bottom: cbOffsetBottom };
|
|
5998
|
-
// ⚠️ 位置**直接写 DOM**,不经过 React(2026-11-06:「有点掉帧的感觉」)。
|
|
5999
|
-
// 侧栏展开/收起是 width 过渡,这个值每帧都在变;走 state 就等于每帧重渲染整个浮窗子树
|
|
6000
|
-
// (标签 + 团队圆片 + 迷雾 + 滚格子),而位置只是两个长度,直接写 style 就够了。
|
|
6001
|
-
// React 那边因此**不再管 left/bottom**(见渲染处),也就不会在下次渲染时把新值覆盖回去;
|
|
6002
|
-
// 元素首次挂载由 pre-paint 的这一次 measure 写入,不会露 CSS 兜底。
|
|
6003
|
-
if (selfElRef !== null && selfElRef.style !== null && typeof selfElRef.style === "object") {
|
|
6004
|
-
try {
|
|
6005
|
-
selfElRef.style.left = styleLeft + "px";
|
|
6006
|
-
selfElRef.style.bottom = styleBottom + "px";
|
|
6007
|
-
selfElRef.style.right = "auto";
|
|
6008
|
-
} catch {
|
|
6009
|
-
/* 写不进去就靠下面的 setFloatBox 兜底 */
|
|
6010
|
-
}
|
|
6011
|
-
}
|
|
6012
6077
|
const vwNow = Math.round(vw);
|
|
6013
6078
|
const vwChanged = floatMeasureVw !== null && vwNow !== floatMeasureVw;
|
|
6014
6079
|
floatMeasureVw = vwNow;
|
|
@@ -6123,6 +6188,8 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
6123
6188
|
};
|
|
6124
6189
|
syncObservers();
|
|
6125
6190
|
measureCtl.sync = syncObservers;
|
|
6191
|
+
// 把这次的 measure 挂出去:诊断里的「修复」按钮靠它把位置重算一遍。
|
|
6192
|
+
floatRemeasureHook = measure;
|
|
6126
6193
|
const onResize = () => {
|
|
6127
6194
|
measure();
|
|
6128
6195
|
scheduleSettle();
|
|
@@ -6138,6 +6205,8 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
6138
6205
|
return () => {
|
|
6139
6206
|
disposed = true;
|
|
6140
6207
|
clearInterval(timer);
|
|
6208
|
+
// 只拆自己挂的那个,别把后来者的钩子清掉
|
|
6209
|
+
if (floatRemeasureHook === measure) floatRemeasureHook = null;
|
|
6141
6210
|
clearSettle();
|
|
6142
6211
|
if (slideHold !== null) {
|
|
6143
6212
|
clearTimeout(slideHold);
|
|
@@ -6434,8 +6503,14 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
6434
6503
|
// 2026-11-02 补充:现在**先直接采纳**它(`raiseTodayFromSession`,只抬不降),
|
|
6435
6504
|
// 因为磁盘口径看不见当前会话还没落盘的那一截 —— 只当"补拉开关"用的时候,
|
|
6436
6505
|
// 冷启动会一直停在 ¥0.0000,最长要等一个 TTL(20s) 才动。
|
|
6437
|
-
|
|
6438
|
-
if (
|
|
6506
|
+
const sessionToday = u && u.today && typeof u.today.cost === "number" ? u.today : null;
|
|
6507
|
+
if (sessionToday !== null) raiseTodayFromSession(sessionToday);
|
|
6508
|
+
// ⚠️ 拿不到增量时**也要允许触发一次全量**(2026-11-09 那个「今日一整天 0」的第二个缺口):
|
|
6509
|
+
// 服务端在「当天快照不存在」时 `updateTodayCostWithSession` 直接返回 null,
|
|
6510
|
+
// 而 lite 路径明确不回退全量(为了流式时 600ms 一次不打爆磁盘)——
|
|
6511
|
+
// 于是响应里根本没有 today,这里原来两个 if 全不成立,一次都不会去问 `?today=1`。
|
|
6512
|
+
// 现在只要今日还停在**可疑的 0**上就照问,TTL(20 秒)负责压节奏。
|
|
6513
|
+
if (!disposed && todayCache.settled && (sessionToday !== null || todayCache.suspect === true)) {
|
|
6439
6514
|
void fetchTodayOnce(false);
|
|
6440
6515
|
}
|
|
6441
6516
|
// ⚠️ **空结果要重试**(2026-11-02 第三轮,「从大对话切到小对话还是加载很久」的真因):
|
|
@@ -7583,6 +7658,345 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
7583
7658
|
// 刻意**不用** ownerProps.form:那要接入 DSH 的 settings schema(内部 API,
|
|
7584
7659
|
// 无兼容承诺),配置值改走插件自己的 /balance-api?config=1(见 lib/index.js),
|
|
7585
7660
|
// 所以这里忽略 form、自己读写。缺失 view / props 时按 page 渲染,不会崩。
|
|
7661
|
+
// ─── 数据诊断(2026-11-09:「能不能在插件设置页里添加数据诊断按钮」)─────────
|
|
7662
|
+
// 起因:用户报「今日一直 ¥0.00」时,光看界面分不清是数据算错了、还是界面没刷新,
|
|
7663
|
+
// 得靠外部 PowerShell 打接口才看得出来。这里把那套自查搬进设置页,点一下就把
|
|
7664
|
+
// `?diag=1` 的关键字段摆成一张能选中复制、能直接贴给别人的纯文本表。
|
|
7665
|
+
// 刻意只读、不发任何写请求,也不改任何设置。
|
|
7666
|
+
function formatDiagMoney(value) {
|
|
7667
|
+
const n = Number(value);
|
|
7668
|
+
return Number.isFinite(n) && value !== null && value !== undefined ? "¥" + n.toFixed(4) : "¥—";
|
|
7669
|
+
}
|
|
7670
|
+
function formatDiagTime(ms) {
|
|
7671
|
+
const n = Number(ms);
|
|
7672
|
+
if (!Number.isFinite(n) || n <= 0) return "—";
|
|
7673
|
+
try {
|
|
7674
|
+
return new Date(n).toLocaleString("zh-CN", { hour12: false });
|
|
7675
|
+
} catch {
|
|
7676
|
+
return "—";
|
|
7677
|
+
}
|
|
7678
|
+
}
|
|
7679
|
+
function formatDiagRect(rect) {
|
|
7680
|
+
if (rect == null || typeof rect !== "object") return "—";
|
|
7681
|
+
const x = Number(rect.x ?? 0);
|
|
7682
|
+
const y = Number(rect.y ?? 0);
|
|
7683
|
+
const right = Number(rect.right ?? x);
|
|
7684
|
+
const bottom = Number(rect.bottom ?? y);
|
|
7685
|
+
return `${x}..${right} × ${y}..${bottom}`;
|
|
7686
|
+
}
|
|
7687
|
+
/** usage 的 token 分项:只印非零的那几项,再给个总数。 */
|
|
7688
|
+
function formatDiagTokens(usage) {
|
|
7689
|
+
const u = usage != null && typeof usage === "object" ? usage : {};
|
|
7690
|
+
const parts = [];
|
|
7691
|
+
let sum = 0;
|
|
7692
|
+
const pick = [
|
|
7693
|
+
["输入", u.inputTokens],
|
|
7694
|
+
["输出", u.outputTokens],
|
|
7695
|
+
["缓存写", u.cacheCreationTokens],
|
|
7696
|
+
["缓存读", u.cacheReadTokens],
|
|
7697
|
+
];
|
|
7698
|
+
for (const [name, raw] of pick) {
|
|
7699
|
+
const n = Number(raw ?? 0);
|
|
7700
|
+
if (!Number.isFinite(n) || n === 0) continue;
|
|
7701
|
+
sum += n;
|
|
7702
|
+
parts.push(name + " " + n);
|
|
7703
|
+
}
|
|
7704
|
+
return { sum, text: parts.length ? parts.join(" ") : "全 0" };
|
|
7705
|
+
}
|
|
7706
|
+
/** 点「数据诊断」时**直接告诉用户有没有问题**(2026-11-09:「如果数据异常会提醒用户吗」)。
|
|
7707
|
+
* 只列字段等于让用户自己判,多数人看不懂 `suspiciousEmpty=false` 是什么意思。
|
|
7708
|
+
* 所以先跑一遍能查的检查,把结论摆在报告最上面:
|
|
7709
|
+
* error = 这个数现在就是错的(假 0、取不到、定位偏掉)
|
|
7710
|
+
* warn = 数值可能不准,但插件已经在自动纠正,或者纯粹是「正在算」
|
|
7711
|
+
* 没抓到任何一条 = 一切正常。 */
|
|
7712
|
+
function collectDiagIssues(data, settings) {
|
|
7713
|
+
const d = data != null && typeof data === "object" ? data : {};
|
|
7714
|
+
const s = settings != null && typeof settings === "object" ? settings : {};
|
|
7715
|
+
const issues = [];
|
|
7716
|
+
|
|
7717
|
+
// 余额
|
|
7718
|
+
if (d.balance == null) {
|
|
7719
|
+
issues.push({
|
|
7720
|
+
level: "error",
|
|
7721
|
+
text: d.balanceError ? `余额取不到:${d.balanceError}` : "余额取不到(未登录或网络失败)",
|
|
7722
|
+
});
|
|
7723
|
+
} else if (d.balanceError) {
|
|
7724
|
+
issues.push({ level: "error", text: `余额读取报错:${d.balanceError}` });
|
|
7725
|
+
}
|
|
7726
|
+
|
|
7727
|
+
// 今日
|
|
7728
|
+
const today = d.today;
|
|
7729
|
+
const td = d.todayDiag != null && typeof d.todayDiag === "object" ? d.todayDiag : null;
|
|
7730
|
+
const snap = td != null && td.snapshot != null && typeof td.snapshot === "object" ? td.snapshot : null;
|
|
7731
|
+
if (today == null || typeof today !== "object") {
|
|
7732
|
+
issues.push({
|
|
7733
|
+
level: "warn",
|
|
7734
|
+
text: `今日服务端暂未给出(todayPartial=${d.todayPartial === true}),界面会走等待态并自动补拉`,
|
|
7735
|
+
});
|
|
7736
|
+
} else {
|
|
7737
|
+
const cost = Number(today.cost ?? 0);
|
|
7738
|
+
const why = [];
|
|
7739
|
+
const seen = new Set();
|
|
7740
|
+
const add = (flag, reason) => {
|
|
7741
|
+
if (flag !== true || seen.has(reason)) return;
|
|
7742
|
+
seen.add(reason);
|
|
7743
|
+
why.push(reason);
|
|
7744
|
+
};
|
|
7745
|
+
for (const src of [today, snap]) {
|
|
7746
|
+
if (src == null || typeof src !== "object") continue;
|
|
7747
|
+
add(src.suspiciousEmpty === true, "列得出会话但今天一个都没筛中");
|
|
7748
|
+
add(src.emptyListing === true, "连会话列表都没列出来(DSH 索引还没就绪)");
|
|
7749
|
+
const skipped = Number(src.skipped ?? 0);
|
|
7750
|
+
if (skipped > 0) add(true, `${skipped} 个会话被让路跳过`);
|
|
7751
|
+
}
|
|
7752
|
+
const pending = td != null && (td.cacheProvisional === true || td.recomputing === true);
|
|
7753
|
+
if (cost === 0 && why.length !== 0) {
|
|
7754
|
+
issues.push({
|
|
7755
|
+
level: "error",
|
|
7756
|
+
text: `今日 ¥0.0000 不可信:${why.join(";")}。插件正在自动退避重试,拿到真值会自己更新,不用手动刷新`,
|
|
7757
|
+
});
|
|
7758
|
+
} else if (cost === 0 && pending) {
|
|
7759
|
+
issues.push({ level: "warn", text: "今日的 0 还在算(缓存 provisional 或正在重算),这一拍不是最终值" });
|
|
7760
|
+
}
|
|
7761
|
+
if (d.todayPartial === true) {
|
|
7762
|
+
issues.push({ level: "warn", text: "今日是部分结果(刚冷启动或刚跨天),客户端会自动补拉" });
|
|
7763
|
+
}
|
|
7764
|
+
}
|
|
7765
|
+
|
|
7766
|
+
// 本会话 / 会话服务
|
|
7767
|
+
if (d.diag != null && typeof d.diag === "object") {
|
|
7768
|
+
if (d.diag.sessionsUsable === false) {
|
|
7769
|
+
issues.push({ level: "warn", text: "DSH 会话服务不可用(sessionsUsable=false),本会话消耗只能退回读磁盘,会慢" });
|
|
7770
|
+
}
|
|
7771
|
+
}
|
|
7772
|
+
if (d.usage == null && d.loading !== true) {
|
|
7773
|
+
issues.push({ level: "warn", text: "本会话 usage 读不到(会话没进内存或事件被切空),本会话金额会显示 ≈ ¥—" });
|
|
7774
|
+
}
|
|
7775
|
+
if (d.loading === true) {
|
|
7776
|
+
issues.push({ level: "warn", text: "DSH 正在装载本会话,插件主动让路 —— 这不是故障,几秒后自己会好" });
|
|
7777
|
+
}
|
|
7778
|
+
|
|
7779
|
+
// 浮窗定位
|
|
7780
|
+
const g = d.floatGeom;
|
|
7781
|
+
if (g != null && typeof g === "object") {
|
|
7782
|
+
const selfX = g.self != null && Number.isFinite(Number(g.self.x)) ? Number(g.self.x) : null;
|
|
7783
|
+
const left = Number.isFinite(Number(g.left)) ? Number(g.left) : null;
|
|
7784
|
+
if (selfX !== null && left !== null && Math.abs(selfX - left) > 2) {
|
|
7785
|
+
issues.push({
|
|
7786
|
+
level: "error",
|
|
7787
|
+
text: `浮窗定位偏移 ${Math.abs(selfX - left)}px(卡片在 x=${selfX},应该在 x=${left})`,
|
|
7788
|
+
});
|
|
7789
|
+
}
|
|
7790
|
+
}
|
|
7791
|
+
|
|
7792
|
+
// 设置自检:这几项填成 0 会让数字永远不对,但界面上完全看不出来
|
|
7793
|
+
if (Number(s.todaySessionLimit ?? 1) <= 0) {
|
|
7794
|
+
issues.push({ level: "error", text: "设置里的「今日扫描上限」是 0,今日金额会永远是 0" });
|
|
7795
|
+
}
|
|
7796
|
+
if (Number(s.statsSessionLimit ?? 1) <= 0) {
|
|
7797
|
+
issues.push({ level: "error", text: "设置里的「统计扫描上限」是 0,统计面板会永远是 0" });
|
|
7798
|
+
}
|
|
7799
|
+
if (Number(s.peakMultiplier ?? 1) <= 0) {
|
|
7800
|
+
issues.push({ level: "error", text: "设置里的「高峰倍率」是 0,高峰时段金额会被算成 0" });
|
|
7801
|
+
}
|
|
7802
|
+
return issues;
|
|
7803
|
+
}
|
|
7804
|
+
|
|
7805
|
+
/** 结论段:报告最上面那几行。 */
|
|
7806
|
+
function formatDiagVerdict(issues) {
|
|
7807
|
+
const bad = issues.filter((x) => x.level === "error");
|
|
7808
|
+
const warn = issues.filter((x) => x.level === "warn");
|
|
7809
|
+
const out = [];
|
|
7810
|
+
if (bad.length === 0 && warn.length === 0) {
|
|
7811
|
+
out.push("诊断结论 ✓ 一切正常(余额、今日、本会话、浮窗、设置都没查出问题)");
|
|
7812
|
+
return out;
|
|
7813
|
+
}
|
|
7814
|
+
const head = [];
|
|
7815
|
+
if (bad.length !== 0) head.push(`✗ ${bad.length} 处异常`);
|
|
7816
|
+
if (warn.length !== 0) head.push(`! ${warn.length} 处提醒`);
|
|
7817
|
+
out.push(`诊断结论 ${head.join("、")}`);
|
|
7818
|
+
for (const item of bad) out.push(` ✗ ${item.text}`);
|
|
7819
|
+
for (const item of warn) out.push(` ! ${item.text}`);
|
|
7820
|
+
return out;
|
|
7821
|
+
}
|
|
7822
|
+
|
|
7823
|
+
/** 一份「出问题时能把整条链路对出来」的诊断文本。
|
|
7824
|
+
* ⚠️ 2026-11-09 第二轮(用户:「诊断出来的信息是不是太少了 如果用户遇到别的数据异常
|
|
7825
|
+
* 这东西复制过来好像也不知道哪里有问题」):所以正文按环节分段把**能拿到的都列出来**,
|
|
7826
|
+
* 末尾再附一段**原始响应 JSON**(截断)—— 任何没被提到的情况都能从原始数据里查,
|
|
7827
|
+
* 不用再让用户去开控制台。 */
|
|
7828
|
+
function formatDiagnostics(data, version, settings) {
|
|
7829
|
+
const d = data != null && typeof data === "object" ? data : {};
|
|
7830
|
+
const s = settings != null && typeof settings === "object" ? settings : {};
|
|
7831
|
+
const lines = [];
|
|
7832
|
+
const where = typeof location === "object" && location !== null
|
|
7833
|
+
? `${location.hostname || "127.0.0.1"}:${location.port || "?"}`
|
|
7834
|
+
: "?";
|
|
7835
|
+
lines.push(`balance-dsh ${version || "—"} · ${where} · ${formatDiagTime(Date.now())}`);
|
|
7836
|
+
const issues = collectDiagIssues(d, s);
|
|
7837
|
+
lines.push("");
|
|
7838
|
+
for (const line of formatDiagVerdict(issues)) lines.push(line);
|
|
7839
|
+
|
|
7840
|
+
// ── 余额 ──
|
|
7841
|
+
const bal = d.balance;
|
|
7842
|
+
const balObj = bal != null && typeof bal === "object" ? bal : null;
|
|
7843
|
+
const balTotal = balObj === null ? null : balObj.total_balance ?? balObj.totalBalance ?? null;
|
|
7844
|
+
const granted = balObj === null ? null : balObj.granted_balance ?? balObj.grantedBalance ?? null;
|
|
7845
|
+
lines.push("");
|
|
7846
|
+
lines.push("── 余额 ──");
|
|
7847
|
+
lines.push(
|
|
7848
|
+
`总额 ${balTotal == null ? "¥—" : formatDiagMoney(balTotal)}` +
|
|
7849
|
+
(granted == null ? "" : ` 赠送 ${formatDiagMoney(granted)}`) +
|
|
7850
|
+
(balObj === null ? " (未登录或取不到)" : ` 可用=${balObj.is_available !== false}`),
|
|
7851
|
+
);
|
|
7852
|
+
if (d.balanceError) lines.push(`balanceError ${d.balanceError}`);
|
|
7853
|
+
|
|
7854
|
+
// ── 今日 ──
|
|
7855
|
+
// 响应里的 `today` 经常是**增量/合并**出来的值(带 `provisional: true`),
|
|
7856
|
+
// 那种对象不带 suspiciousEmpty / emptyListing / skipped —— 只看它会得出「一切正常」。
|
|
7857
|
+
// 所以判定要把 `todayDiag.snapshot` 里那份落盘快照的标记也一起算进来。
|
|
7858
|
+
const today = d.today;
|
|
7859
|
+
const td = d.todayDiag != null && typeof d.todayDiag === "object" ? d.todayDiag : null;
|
|
7860
|
+
const snap = td != null && td.snapshot != null && typeof td.snapshot === "object" ? td.snapshot : null;
|
|
7861
|
+
const anySuspect =
|
|
7862
|
+
today?.suspiciousEmpty === true ||
|
|
7863
|
+
today?.emptyListing === true ||
|
|
7864
|
+
Number(today?.skipped ?? 0) > 0 ||
|
|
7865
|
+
snap?.suspiciousEmpty === true ||
|
|
7866
|
+
snap?.emptyListing === true ||
|
|
7867
|
+
Number(snap?.skipped ?? 0) > 0;
|
|
7868
|
+
lines.push("");
|
|
7869
|
+
lines.push("── 今日 ──");
|
|
7870
|
+
if (today == null || typeof today !== "object") {
|
|
7871
|
+
lines.push(`服务端暂未给出(todayPartial=${d.todayPartial === true})`);
|
|
7872
|
+
} else {
|
|
7873
|
+
lines.push(
|
|
7874
|
+
`金额 ${formatDiagMoney(today.cost)} date=${today.date ?? "—"} ` +
|
|
7875
|
+
`sessions=${today.sessions ?? 0} tokens=${today.totalTokens ?? 0}`,
|
|
7876
|
+
);
|
|
7877
|
+
// 三态:光看 today 是分不出「真 0」和「还在算」的
|
|
7878
|
+
const zeroCost = Number(today.cost ?? 0) === 0;
|
|
7879
|
+
const pending = td != null && (td.cacheProvisional === true || td.recomputing === true);
|
|
7880
|
+
const verdict = !zeroCost
|
|
7881
|
+
? "可信"
|
|
7882
|
+
: anySuspect
|
|
7883
|
+
? "不可信(假 0,会自动退避重试)"
|
|
7884
|
+
: pending
|
|
7885
|
+
? "待定(服务端还在算,这一拍不是最终值)"
|
|
7886
|
+
: "可信";
|
|
7887
|
+
lines.push(
|
|
7888
|
+
`可信 ${verdict} suspiciousEmpty=${today.suspiciousEmpty === true} ` +
|
|
7889
|
+
`emptyListing=${today.emptyListing === true} skipped=${today.skipped ?? 0} ` +
|
|
7890
|
+
`partial=${d.todayPartial === true}`,
|
|
7891
|
+
);
|
|
7892
|
+
}
|
|
7893
|
+
if (td != null) {
|
|
7894
|
+
lines.push(
|
|
7895
|
+
`缓存 cost=${td.cacheCost == null ? "—" : Number(td.cacheCost).toFixed(4)} ` +
|
|
7896
|
+
`${Math.round(Number(td.cacheExpiresInMs ?? 0) / 1000)}s 后过期 ` +
|
|
7897
|
+
`provisional=${td.cacheProvisional === true} 重算中=${td.recomputing === true}`,
|
|
7898
|
+
);
|
|
7899
|
+
if (snap == null) {
|
|
7900
|
+
lines.push("快照 —(文件不存在:可疑结果按设计不落盘,也可能今天还没算过)");
|
|
7901
|
+
} else {
|
|
7902
|
+
lines.push(
|
|
7903
|
+
`快照 ${formatDiagMoney(snap.cost)} date=${snap.date ?? "—"} v=${snap.v ?? "—"} ` +
|
|
7904
|
+
`sessions=${snap.sessions ?? 0} at=${formatDiagTime(snap.at)}`,
|
|
7905
|
+
);
|
|
7906
|
+
lines.push(
|
|
7907
|
+
`标记 suspiciousEmpty=${snap.suspiciousEmpty === true} ` +
|
|
7908
|
+
`emptyListing=${snap.emptyListing === true} skipped=${snap.skipped ?? 0}`,
|
|
7909
|
+
);
|
|
7910
|
+
}
|
|
7911
|
+
}
|
|
7912
|
+
|
|
7913
|
+
// ── 本会话 ──
|
|
7914
|
+
lines.push("");
|
|
7915
|
+
lines.push("── 本会话 ──");
|
|
7916
|
+
const usage = d.usage;
|
|
7917
|
+
if (usage != null && typeof usage === "object") {
|
|
7918
|
+
const tk = formatDiagTokens(usage);
|
|
7919
|
+
lines.push(`金额 ${formatDiagMoney(usage.cost)} model=${d.model ?? "—"} tokens ${tk.sum}`);
|
|
7920
|
+
if (tk.text !== "全 0") lines.push(`分项 ${tk.text}`);
|
|
7921
|
+
const extra = [];
|
|
7922
|
+
if (usage.peakCost != null) extra.push(`高峰 ${formatDiagMoney(usage.peakCost)}`);
|
|
7923
|
+
if (usage.offpeakCost != null) extra.push(`空闲 ${formatDiagMoney(usage.offpeakCost)}`);
|
|
7924
|
+
if (usage.webToolCost != null) extra.push(`网页工具 ${formatDiagMoney(usage.webToolCost)}`);
|
|
7925
|
+
if (extra.length) lines.push(`拆分 ${extra.join(" ")}`);
|
|
7926
|
+
} else {
|
|
7927
|
+
lines.push("—(诊断请求没带会话参数,这一项不在本次统计内;要看本会话明细请用 /balance-dsh-diag)");
|
|
7928
|
+
}
|
|
7929
|
+
const team = d.team;
|
|
7930
|
+
if (team != null && typeof team === "object" && Number(team.count ?? 0) > 0) {
|
|
7931
|
+
lines.push(`团队 ${team.count} 人 ${formatDiagMoney(team.cost)} tokens ${team.tokens ?? 0}`);
|
|
7932
|
+
}
|
|
7933
|
+
if (d.subagentUsage != null && typeof d.subagentUsage === "object") {
|
|
7934
|
+
lines.push(`子代理 ${formatDiagMoney(d.subagentUsage.cost)} sessions=${d.subagentUsage.sessions ?? 0}`);
|
|
7935
|
+
}
|
|
7936
|
+
|
|
7937
|
+
// ── 设置(口径类,出问题时先确认这些没被改过) ──
|
|
7938
|
+
lines.push("");
|
|
7939
|
+
lines.push("── 设置 ──");
|
|
7940
|
+
lines.push(
|
|
7941
|
+
`轮询 ${s.refreshIntervalSec ?? "—"}s · 今日扫描上限 ${s.todaySessionLimit ?? "—"} · ` +
|
|
7942
|
+
`统计上限 ${s.statsSessionLimit ?? "—"} · 统计缓存 ${s.statsCacheSec ?? "—"}s · ` +
|
|
7943
|
+
`低余额阈值 ${s.lowBalanceThreshold ?? "—"}`,
|
|
7944
|
+
);
|
|
7945
|
+
lines.push(
|
|
7946
|
+
`高峰 ${s.peakWindowsText ?? "—"} × ${s.peakMultiplier ?? "—"} · ` +
|
|
7947
|
+
`浮窗 ${s.showUsagePill === false ? "关" : "开"} · 余额行 ${s.showBalanceRow === false ? "关" : "开"} · ` +
|
|
7948
|
+
`侧栏入口 ${s.showSidebarEntry === false ? "关" : "开"} · 圆钮 ${s.showFab === false ? "关" : "开"}`,
|
|
7949
|
+
);
|
|
7950
|
+
|
|
7951
|
+
// ── 浮窗定位 ──
|
|
7952
|
+
lines.push("");
|
|
7953
|
+
lines.push("── 浮窗 ──");
|
|
7954
|
+
const g = d.floatGeom;
|
|
7955
|
+
if (g == null || typeof g !== "object") {
|
|
7956
|
+
lines.push("—(还没有测量数据:浮窗没渲染,或页面刚起来)");
|
|
7957
|
+
} else {
|
|
7958
|
+
const selfX = g.self != null && Number.isFinite(Number(g.self.x)) ? Number(g.self.x) : null;
|
|
7959
|
+
const left = Number.isFinite(Number(g.left)) ? Number(g.left) : null;
|
|
7960
|
+
const aligned = selfX !== null && left !== null && Math.abs(selfX - left) <= 2;
|
|
7961
|
+
lines.push(
|
|
7962
|
+
`${aligned ? "✓ 对齐" : "✗ 偏移"} left=${left ?? "—"} self.x=${selfX ?? "—"} ` +
|
|
7963
|
+
`styleLeft=${g.styleLeft ?? "—"} 视口=${g.vw ?? "?"}×${g.vh ?? "?"}`,
|
|
7964
|
+
);
|
|
7965
|
+
lines.push(`card ${formatDiagRect(g.card)} dock ${formatDiagRect(g.dock)}`);
|
|
7966
|
+
lines.push(
|
|
7967
|
+
`cbOffsetX=${g.cbOffsetX ?? "—"} cbOffsetBottom=${g.cbOffsetBottom ?? "—"} ` +
|
|
7968
|
+
`测量于 ${formatDiagTime(g.at)}`,
|
|
7969
|
+
);
|
|
7970
|
+
}
|
|
7971
|
+
|
|
7972
|
+
// ── 会话读取(服务端 diag) ──
|
|
7973
|
+
const diag = d.diag;
|
|
7974
|
+
lines.push("");
|
|
7975
|
+
lines.push("── 会话读取 ──");
|
|
7976
|
+
if (diag != null && typeof diag === "object") {
|
|
7977
|
+
lines.push(
|
|
7978
|
+
`sessionsUsable=${diag.sessionsUsable === true} sessionLoading=${diag.sessionLoading === true} ` +
|
|
7979
|
+
`sessionUndefined=${diag.sessionUndefined === true} hasGet=${diag.hasGet === true}`,
|
|
7980
|
+
);
|
|
7981
|
+
if (diag.persistedError) lines.push(`persistedError=${diag.persistedError}`);
|
|
7982
|
+
if (d.loading === true) lines.push("loading=true(DSH 正在装载本会话,插件主动让路)");
|
|
7983
|
+
} else {
|
|
7984
|
+
lines.push("—(这次请求没带会话参数,服务端没算这一段)");
|
|
7985
|
+
}
|
|
7986
|
+
|
|
7987
|
+
// ── 原始响应:没被上面提到的东西全在这里,出问题时能对到具体字段 ──
|
|
7988
|
+
lines.push("");
|
|
7989
|
+
lines.push("── 原始响应(最多 6000 字)──");
|
|
7990
|
+
let raw = "";
|
|
7991
|
+
try {
|
|
7992
|
+
raw = JSON.stringify(d);
|
|
7993
|
+
} catch {
|
|
7994
|
+
raw = "[序列化失败]";
|
|
7995
|
+
}
|
|
7996
|
+
lines.push(raw.length > 6000 ? raw.slice(0, 6000) + " …(已截断,共 " + raw.length + " 字)" : raw);
|
|
7997
|
+
return lines.join("\n");
|
|
7998
|
+
}
|
|
7999
|
+
|
|
7586
8000
|
function BalanceSettingsCard(props) {
|
|
7587
8001
|
const view = props && props.view;
|
|
7588
8002
|
const settings = useSettings();
|
|
@@ -7597,6 +8011,13 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
7597
8011
|
// installed — 覆盖完成,等重启
|
|
7598
8012
|
// error — 查询或更新失败
|
|
7599
8013
|
const [updateCheck, setUpdateCheck] = react.useState({ kind: "idle" });
|
|
8014
|
+
// 数据诊断(设置页 → 「数据诊断」→ 点一下出表):
|
|
8015
|
+
// idle/loading/ok/error 四态 + 一份已经拼好的纯文本。只读,不写任何设置。
|
|
8016
|
+
const [diagReport, setDiagReport] = react.useState({ kind: "idle", text: "" });
|
|
8017
|
+
const [diagCopied, setDiagCopied] = react.useState(false);
|
|
8018
|
+
// 「修复」按钮(2026-11-09 用户:「如果异常能不能添加一个修复按钮」)。
|
|
8019
|
+
// kind: idle / running / done(全好了)/ partial(修完还剩问题)/ failed
|
|
8020
|
+
const [diagFix, setDiagFix] = react.useState({ kind: "idle", note: "" });
|
|
7600
8021
|
|
|
7601
8022
|
// 拿到设置后灌一次草稿;draft 已有内容就不再覆盖,避免打断正在输入的用户。
|
|
7602
8023
|
// ⚠️ 必须等 `loaded === true` 再灌(2026-10-31 审查):
|
|
@@ -7755,6 +8176,118 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
7755
8176
|
});
|
|
7756
8177
|
};
|
|
7757
8178
|
|
|
8179
|
+
// 数据诊断:打一次 `?diag=1`,把关键字段拼成纯文本摆出来。
|
|
8180
|
+
// 刻意每次都重新请求(不缓存)—— 用户点它的时机就是「刚刚好像不对」,
|
|
8181
|
+
// 拿缓存等于拿现场之前的数据,反而误导。
|
|
8182
|
+
const onRunDiag = () => {
|
|
8183
|
+
if (diagReport.kind === "loading") return;
|
|
8184
|
+
setDiagCopied(false);
|
|
8185
|
+
setDiagReport({ kind: "loading", text: "" });
|
|
8186
|
+
void (async () => {
|
|
8187
|
+
try {
|
|
8188
|
+
// 必须带 today=1:不然响应里 today 恒为 null(服务端只在要今日或带会话时才去算),
|
|
8189
|
+
// 诊断报告就成了「什么都没查到」。带 today 会触发一次今日计算,点一下才跑一次,可接受。
|
|
8190
|
+
const response = await fetch("/balance-api?diag=1&today=1", { headers: { accept: "application/json" } });
|
|
8191
|
+
if (!response.ok) throw new Error("HTTP " + response.status);
|
|
8192
|
+
const data = await response.json();
|
|
8193
|
+
// 查出 error 级问题就把整块换成警告色 —— 用户点这个按钮就是想确认「是不是出事了」,
|
|
8194
|
+
// 一份平淡的等宽文本会让人误以为一切正常。
|
|
8195
|
+
const issues = collectDiagIssues(data, settingsCache.value);
|
|
8196
|
+
const bad = issues.some((x) => x.level === "error");
|
|
8197
|
+
setDiagReport({
|
|
8198
|
+
kind: bad ? "warn" : "ok",
|
|
8199
|
+
text: formatDiagnostics(data, settingsCache.version, settingsCache.value),
|
|
8200
|
+
});
|
|
8201
|
+
} catch (error) {
|
|
8202
|
+
setDiagReport({
|
|
8203
|
+
kind: "error",
|
|
8204
|
+
text: "诊断请求失败:" + (error instanceof Error ? error.message : String(error)),
|
|
8205
|
+
});
|
|
8206
|
+
}
|
|
8207
|
+
})();
|
|
8208
|
+
};
|
|
8209
|
+
/** 「修复」:把能自己恢复的那几样清掉再重算一遍。
|
|
8210
|
+
* ⚠️ 刻意**不碰用户的设置**(那是有意配的值,不是故障),也不删落盘快照
|
|
8211
|
+
* (可疑的 0 本来就落不了盘,「只增不减」保护着可信那份,删了只会让下次冷启动更慢)。
|
|
8212
|
+
* 修完立刻重新诊断一次,剩多少问题如实显示 —— 不假装修好了。 */
|
|
8213
|
+
const onFixDiag = () => {
|
|
8214
|
+
if (diagFix.kind === "running") return;
|
|
8215
|
+
setDiagCopied(false);
|
|
8216
|
+
setDiagFix({ kind: "running", note: "" });
|
|
8217
|
+
const notes = [];
|
|
8218
|
+
void (async () => {
|
|
8219
|
+
// ① 今日:清内存缓存与重试状态。可疑的 0 是被「已 settled」和缓存 TTL 钉住的,
|
|
8220
|
+
// 不清这两个,重新请求也只会拿到同一个值。
|
|
8221
|
+
todayCache.at = 0;
|
|
8222
|
+
todayCache.tried = false;
|
|
8223
|
+
todayCache.settled = false;
|
|
8224
|
+
todayCache.suspect = false;
|
|
8225
|
+
todayCache.retries = 0;
|
|
8226
|
+
clearTodaySuspectRetry();
|
|
8227
|
+
notes.push("今日缓存已清");
|
|
8228
|
+
// ② 余额:本来就是实时请求,这里只是强制立刻重拉一次
|
|
8229
|
+
void fetchBalanceOnce(true);
|
|
8230
|
+
notes.push("余额重拉");
|
|
8231
|
+
// ③ 浮窗:让浮窗自己重新量一次(拿不到钩子就跳过后面的提示)
|
|
8232
|
+
if (typeof floatRemeasureHook === "function") {
|
|
8233
|
+
try {
|
|
8234
|
+
floatRemeasureHook();
|
|
8235
|
+
notes.push("浮窗已重新定位");
|
|
8236
|
+
} catch {
|
|
8237
|
+
/* 量不到就当没有这一步 */
|
|
8238
|
+
}
|
|
8239
|
+
}
|
|
8240
|
+
// ④ 让服务端也清一次今日缓存(进行中的重算会继续喂旧值),再拿最新数据重出报告
|
|
8241
|
+
try {
|
|
8242
|
+
const response = await fetch("/balance-api?diag=1&today=1&fix=1", {
|
|
8243
|
+
headers: { accept: "application/json" },
|
|
8244
|
+
});
|
|
8245
|
+
if (!response.ok) throw new Error("HTTP " + response.status);
|
|
8246
|
+
const data = await response.json();
|
|
8247
|
+
if (data.fix != null && typeof data.fix === "object") {
|
|
8248
|
+
if (data.fix.clearedTodayCache === true) notes.push("服务端缓存已清");
|
|
8249
|
+
if (data.fix.cancelledRecompute === true) notes.push("已中止进行中的重算");
|
|
8250
|
+
}
|
|
8251
|
+
const issues = collectDiagIssues(data, settingsCache.value);
|
|
8252
|
+
const bad = issues.some((x) => x.level === "error");
|
|
8253
|
+
setDiagReport({
|
|
8254
|
+
kind: bad ? "warn" : "ok",
|
|
8255
|
+
text: formatDiagnostics(data, settingsCache.version, settingsCache.value),
|
|
8256
|
+
});
|
|
8257
|
+
setDiagFix({
|
|
8258
|
+
kind: bad ? "partial" : "done",
|
|
8259
|
+
note: notes.join(";") + (bad ? ";还剩问题,见下方结论" : ";已恢复"),
|
|
8260
|
+
});
|
|
8261
|
+
// 服务端算完之后,客户端再跟一次,让界面数字也换成新的
|
|
8262
|
+
void fetchTodayOnce(true);
|
|
8263
|
+
} catch (error) {
|
|
8264
|
+
setDiagFix({
|
|
8265
|
+
kind: "failed",
|
|
8266
|
+
note: "修复请求失败:" + (error instanceof Error ? error.message : String(error)),
|
|
8267
|
+
});
|
|
8268
|
+
}
|
|
8269
|
+
})();
|
|
8270
|
+
};
|
|
8271
|
+
|
|
8272
|
+
const onCopyDiag = () => {
|
|
8273
|
+
const text = diagReport.text;
|
|
8274
|
+
if (!text) return;
|
|
8275
|
+
const done = () => {
|
|
8276
|
+
setDiagCopied(true);
|
|
8277
|
+
// 提示两秒后自己收回,不占地方
|
|
8278
|
+
setTimeout(() => setDiagCopied(false), 2000);
|
|
8279
|
+
};
|
|
8280
|
+
// navigator.clipboard 在非 secure context 下可能不存在;失败就让用户手动选中。
|
|
8281
|
+
try {
|
|
8282
|
+
if (navigator?.clipboard?.writeText) {
|
|
8283
|
+
void navigator.clipboard.writeText(text).then(done).catch(() => {});
|
|
8284
|
+
return;
|
|
8285
|
+
}
|
|
8286
|
+
} catch {
|
|
8287
|
+
// 落到下面的手动提示
|
|
8288
|
+
}
|
|
8289
|
+
};
|
|
8290
|
+
|
|
7758
8291
|
// 更新入口(用户要求:「检查到新版本时不要直接下载,要提醒用户是否下载」
|
|
7759
8292
|
// + 「不是弹窗,是在检查更新那个位置变成『检测到新版本,是否更新』(更新)」)。
|
|
7760
8293
|
// 所以只有一层:查到新版后同一块区域直接问「是否更新?」,旁边的按钮就叫「更新」,
|
|
@@ -8397,6 +8930,72 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
8397
8930
|
numberRow("peakMultiplier", "高峰时段倍率", "官方为 2(高峰价 = 空闲价 × 倍率)。", 1, 10, "倍"),
|
|
8398
8931
|
]),
|
|
8399
8932
|
|
|
8933
|
+
// 数据诊断排在最后:它是排障入口,不是日常设置,
|
|
8934
|
+
// 出问题时往下翻一屏就能找到,平时不占视线。只读,不写任何设置。
|
|
8935
|
+
group("diag", "数据诊断", [
|
|
8936
|
+
row(
|
|
8937
|
+
"diag",
|
|
8938
|
+
"生成诊断报告",
|
|
8939
|
+
// 2026-11-09 用户:「把这段指令放在诊断按钮的说明里面」—— 命令和按钮同源,
|
|
8940
|
+
// 说明里直接把指令写出来,用户看到这一行就知道会话里也能调。
|
|
8941
|
+
"把余额、今日消耗的可信度、会话读取状态、浮窗定位原样列成一张表;数字不对劲时点一下,复制出来就能直接发给别人看。只读取,不改任何设置。会话里也可以直接发 /balance-dsh-diag,那条会带上本会话明细。",
|
|
8942
|
+
jsx.jsxs("div", {
|
|
8943
|
+
className: "balance-settings__diag-btns",
|
|
8944
|
+
children: [
|
|
8945
|
+
jsx.jsx("button", {
|
|
8946
|
+
type: "button",
|
|
8947
|
+
className: "balance-settings__btn",
|
|
8948
|
+
onClick: onRunDiag,
|
|
8949
|
+
disabled: diagReport.kind === "loading",
|
|
8950
|
+
children:
|
|
8951
|
+
diagReport.kind === "loading"
|
|
8952
|
+
? "读取中…"
|
|
8953
|
+
: diagReport.kind === "idle"
|
|
8954
|
+
? "数据诊断"
|
|
8955
|
+
: "重新诊断",
|
|
8956
|
+
}),
|
|
8957
|
+
// 只有查出问题时才出现「修复」—— 一切正常时多一个按钮只会让人以为还有事没做
|
|
8958
|
+
diagReport.kind === "warn"
|
|
8959
|
+
? jsx.jsx("button", {
|
|
8960
|
+
type: "button",
|
|
8961
|
+
className: "balance-settings__btn",
|
|
8962
|
+
onClick: onFixDiag,
|
|
8963
|
+
disabled: diagFix.kind === "running",
|
|
8964
|
+
children: diagFix.kind === "running" ? "修复中…" : "修复",
|
|
8965
|
+
})
|
|
8966
|
+
: null,
|
|
8967
|
+
diagReport.text
|
|
8968
|
+
? jsx.jsx("button", {
|
|
8969
|
+
type: "button",
|
|
8970
|
+
className: "balance-settings__btn",
|
|
8971
|
+
onClick: onCopyDiag,
|
|
8972
|
+
children: diagCopied ? "已复制" : "复制",
|
|
8973
|
+
})
|
|
8974
|
+
: null,
|
|
8975
|
+
],
|
|
8976
|
+
}),
|
|
8977
|
+
),
|
|
8978
|
+
// 修完说清楚做了什么、还剩什么 —— 只报「成功」而不提剩余问题等于骗人
|
|
8979
|
+
diagFix.note
|
|
8980
|
+
? jsx.jsx("div", {
|
|
8981
|
+
className:
|
|
8982
|
+
"balance-settings__diag-fix" +
|
|
8983
|
+
(diagFix.kind === "failed" || diagFix.kind === "partial"
|
|
8984
|
+
? " balance-settings__diag-fix--warn"
|
|
8985
|
+
: " balance-settings__diag-fix--ok"),
|
|
8986
|
+
children: diagFix.note,
|
|
8987
|
+
})
|
|
8988
|
+
: null,
|
|
8989
|
+
diagReport.text
|
|
8990
|
+
? jsx.jsx("pre", {
|
|
8991
|
+
className:
|
|
8992
|
+
"balance-settings__diag" +
|
|
8993
|
+
(diagReport.kind === "error" || diagReport.kind === "warn" ? " balance-settings__diag--error" : ""),
|
|
8994
|
+
children: diagReport.text,
|
|
8995
|
+
})
|
|
8996
|
+
: null,
|
|
8997
|
+
]),
|
|
8998
|
+
|
|
8400
8999
|
jsx.jsxs("div", {
|
|
8401
9000
|
className: "balance-settings__actions",
|
|
8402
9001
|
children: [
|