balance-dsh 2.2.2 → 2.2.4
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 +73 -0
- package/lib/client.js +667 -37
- package/lib/index.js +256 -8
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -1514,6 +1514,12 @@ 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)}
|
|
1517
1523
|
.balance-settings__status{flex:1;min-width:0;font-size:12px;line-height:17px;color:var(--dsw-alias-label-tertiary)}
|
|
1518
1524
|
.balance-settings__status--error{color:var(--dsw-alias-state-error-primary,#e5484d)}
|
|
1519
1525
|
.balance-settings__status--ok{color:var(--dsw-alias-state-success-primary,#22c55e)}
|
|
@@ -1724,29 +1730,106 @@ window.__ModuleLoader__.load({
|
|
|
1724
1730
|
* @param wrap 本插件自己的浮窗包裹层(宽度 0 的那个)
|
|
1725
1731
|
* @returns dock 行的元素;实在找不到返回 null(调用方退回 CSS 兜底)
|
|
1726
1732
|
*/
|
|
1733
|
+
/** 元素有真实盒子(display:contents 的出口容器量出来是 0×0)。 */
|
|
1734
|
+
function hasRealBox(el) {
|
|
1735
|
+
if (el === null || el === undefined || typeof el.getBoundingClientRect !== "function") return false;
|
|
1736
|
+
const rect = el.getBoundingClientRect();
|
|
1737
|
+
return rect.width > 0 && rect.height > 0;
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
/** dock 行是否在自己的祖先链上(多 composer 实例时用来锁定"自己那一个")。 */
|
|
1741
|
+
function ancestorHasAttr(node, attr, maxDepth) {
|
|
1742
|
+
let cur = node;
|
|
1743
|
+
for (let i = 0; cur !== null && cur !== undefined && i < maxDepth; i += 1) {
|
|
1744
|
+
try {
|
|
1745
|
+
if (typeof cur.hasAttribute === "function" && cur.hasAttribute(attr)) return true;
|
|
1746
|
+
} catch { /* 跨文档 / 代理对象,当作没有 */ }
|
|
1747
|
+
cur = cur.parentElement;
|
|
1748
|
+
}
|
|
1749
|
+
return false;
|
|
1750
|
+
}
|
|
1751
|
+
|
|
1727
1752
|
function resolveComposerDockRow(wrap) {
|
|
1728
1753
|
if (typeof document === "undefined") return null;
|
|
1754
|
+
// ⚠️ 2026-11-06(DSH 0.2.2 桌面版:「会话消耗浮窗位置异常」):
|
|
1755
|
+
// 原来是 `document.querySelector("[data-composer-dock]")` 取**第一个**。新版 DSH 会同时存在
|
|
1756
|
+
// 多个 composer 实例(hero / 对话态),先命中的那个不一定是自己所在的,量出来的矩形就错。
|
|
1757
|
+
// 现在先在**自己的祖先链**上找带 data-composer-dock 的那一层,找不到才退全局扫描。
|
|
1758
|
+
let node = wrap !== null && wrap !== undefined ? wrap : null;
|
|
1759
|
+
for (let i = 0; node !== null && node !== undefined && i < 8; i += 1) {
|
|
1760
|
+
let own = false;
|
|
1761
|
+
try {
|
|
1762
|
+
own = typeof node.hasAttribute === "function" && node.hasAttribute("data-composer-dock");
|
|
1763
|
+
} catch { own = false; }
|
|
1764
|
+
if (own && hasRealBox(node)) return node;
|
|
1765
|
+
node = node.parentElement;
|
|
1766
|
+
}
|
|
1729
1767
|
try {
|
|
1730
|
-
const
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
if (rect.width > 0 && rect.height > 0) return row;
|
|
1768
|
+
const rows = document.querySelectorAll("[data-composer-dock]");
|
|
1769
|
+
for (let i = 0; i < rows.length; i += 1) {
|
|
1770
|
+
if (hasRealBox(rows[i])) return rows[i];
|
|
1734
1771
|
}
|
|
1735
1772
|
} catch {
|
|
1736
1773
|
/* 结构变了就往下退 */
|
|
1737
1774
|
}
|
|
1738
1775
|
// 退路:从包裹层往上爬,跳过所有不生成盒子的层(display:contents 的出口容器)
|
|
1739
|
-
let
|
|
1740
|
-
for (let i = 0;
|
|
1741
|
-
if (
|
|
1742
|
-
|
|
1743
|
-
if (rect.width > 0 && rect.height > 0) return node;
|
|
1744
|
-
}
|
|
1745
|
-
node = node.parentElement;
|
|
1776
|
+
let up = wrap !== null && wrap !== undefined ? wrap.parentElement : null;
|
|
1777
|
+
for (let i = 0; up !== null && i < 4; i += 1) {
|
|
1778
|
+
if (hasRealBox(up) && up.ownerDocument === document) return up;
|
|
1779
|
+
up = up.parentElement;
|
|
1746
1780
|
}
|
|
1747
1781
|
return null;
|
|
1748
1782
|
}
|
|
1749
1783
|
|
|
1784
|
+
/** 找**当前 composer 自己**那张输入卡片(`[data-composer-card]`)。
|
|
1785
|
+
*
|
|
1786
|
+
* ⚠️ 2026-11-06 同样的问题:新版 DSH 里 `document.querySelector("[data-composer-card]")`
|
|
1787
|
+
* 可能命中另一个实例(隐藏的 hero composer,rect 全 0),下一句守卫 `rect.width > 0` 直接把它
|
|
1788
|
+
* 判掉 → 调用方拿到 null → 横向退回「聊天列右缘 − 4 − 浮窗宽」的老公式,浮窗被甩到窗口右边
|
|
1789
|
+
* (用户截图:浮窗左缘 x1167,输入卡片右缘才 x1010)。
|
|
1790
|
+
* 现在从 dock 往上爬,优先在自己那棵子树里找**可见**的卡片;全都不行才退全局扫描。
|
|
1791
|
+
*
|
|
1792
|
+
* @param dock resolveComposerDockRow 的结果(可以是 null)
|
|
1793
|
+
* @param vw 视口宽度(<=0 表示不校验右缘)
|
|
1794
|
+
* @returns 卡片元素;找不到返回 null(调用方退回老公式 / CSS 兜底)
|
|
1795
|
+
*/
|
|
1796
|
+
function resolveComposerCardEl(dock, vw) {
|
|
1797
|
+
if (typeof document === "undefined") return null;
|
|
1798
|
+
const pick = (root) => {
|
|
1799
|
+
if (root === null || root === undefined || typeof root.querySelectorAll !== "function") return null;
|
|
1800
|
+
let list;
|
|
1801
|
+
try {
|
|
1802
|
+
list = root.querySelectorAll("[data-composer-card]");
|
|
1803
|
+
} catch {
|
|
1804
|
+
return null;
|
|
1805
|
+
}
|
|
1806
|
+
for (let i = 0; i < list.length; i += 1) {
|
|
1807
|
+
const el = list[i];
|
|
1808
|
+
if (el === null || el === undefined || typeof el.getBoundingClientRect !== "function") continue;
|
|
1809
|
+
const rect = el.getBoundingClientRect();
|
|
1810
|
+
if (rect.width > 0 && rect.height > 0 && rect.right > 8 && (vw <= 0 || rect.right <= vw)) return el;
|
|
1811
|
+
}
|
|
1812
|
+
return null;
|
|
1813
|
+
};
|
|
1814
|
+
// ① 自己的 composer 根:dock 是卡片的兄弟,往上爬到 root 就能同时看到两者
|
|
1815
|
+
let scope = dock;
|
|
1816
|
+
for (let i = 0; scope !== null && scope !== undefined && i < 6; i += 1) {
|
|
1817
|
+
const found = pick(scope);
|
|
1818
|
+
if (found !== null) return found;
|
|
1819
|
+
scope = scope.parentElement;
|
|
1820
|
+
}
|
|
1821
|
+
// ② 全局兜底(老版本只有一个实例,行为与原来一致)
|
|
1822
|
+
return pick(document);
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1825
|
+
/** 同上,直接给矩形(拿不到就是 null)。 */
|
|
1826
|
+
function resolveComposerCardRect(dock, vw) {
|
|
1827
|
+
const el = resolveComposerCardEl(dock, vw);
|
|
1828
|
+
if (el === null || typeof el.getBoundingClientRect !== "function") return null;
|
|
1829
|
+
const rect = el.getBoundingClientRect();
|
|
1830
|
+
return rect && rect.width > 0 && rect.height > 0 ? rect : null;
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1750
1833
|
/** 浮窗「上一次量对的位置」,**模块级**(跨组件挂载存活)。
|
|
1751
1834
|
*
|
|
1752
1835
|
* 2026-11-02 第十四轮:用户「切换对话的时候 小浮窗会闪很短暂的一帧在之前的旧位置
|
|
@@ -1763,6 +1846,46 @@ window.__ModuleLoader__.load({
|
|
|
1763
1846
|
* 比如收起/展开侧边栏)」。前者绝对不能挂位置过渡,否则卡片会被窗口边缘拖着橡皮筋走;
|
|
1764
1847
|
* 后者要挂过渡,否则就是从旧位「闪」到新位(2026-11-03 用户:「能不能添加滑动动画」)。*/
|
|
1765
1848
|
let floatMeasureVw = null;
|
|
1849
|
+
/** 上一拍的「fixed 包含块偏移」。用来判断包含块自己有没有在动 ——
|
|
1850
|
+
* 在动就不能挂 left 过渡,否则容器运动 + 过渡追目标会叠成「先跑偏再回来」
|
|
1851
|
+
* (2026-11-06 用户:「展开侧边栏的时候 小浮窗会往左边跑一下」)。*/
|
|
1852
|
+
let floatCbOffset = null;
|
|
1853
|
+
|
|
1854
|
+
/** 矩形的可读快照(排障上报用)。 */
|
|
1855
|
+
function rectBrief(rect) {
|
|
1856
|
+
if (rect === null || rect === undefined) return null;
|
|
1857
|
+
return {
|
|
1858
|
+
x: Math.round(rect.left), y: Math.round(rect.top),
|
|
1859
|
+
w: Math.round(rect.width), h: Math.round(rect.height),
|
|
1860
|
+
right: Math.round(rect.right), bottom: Math.round(rect.bottom),
|
|
1861
|
+
};
|
|
1862
|
+
}
|
|
1863
|
+
|
|
1864
|
+
/** 浮窗几何上报(2026-11-06,排障通道):内容变了才发,且最多 3 秒一次。
|
|
1865
|
+
* DSH 升级改掉 composer 的 DOM 时,界面只表现为「浮窗位置不对」,
|
|
1866
|
+
* 完全看不出是锚点没查到、量到 0×0、还是公式走岔。把这一拍的原始量发给
|
|
1867
|
+
* `/balance-api?geom=1`,然后 `GET /balance-api?diag=1` 的 `floatGeom` 就能看到。
|
|
1868
|
+
* 节流是必须的:measure 每 400ms 跑一次,无脑发会把还要算余额/今日的接口刷满。 */
|
|
1869
|
+
let floatGeomSentAt = 0;
|
|
1870
|
+
let floatGeomKey = "";
|
|
1871
|
+
function reportFloatGeom(payload) {
|
|
1872
|
+
try {
|
|
1873
|
+
if (typeof fetch !== "function") return;
|
|
1874
|
+
const key = JSON.stringify(payload);
|
|
1875
|
+
if (key === floatGeomKey) return;
|
|
1876
|
+
const now = Date.now();
|
|
1877
|
+
if (now - floatGeomSentAt < 3000) return;
|
|
1878
|
+
floatGeomKey = key;
|
|
1879
|
+
floatGeomSentAt = now;
|
|
1880
|
+
void fetch("/balance-api?geom=1", {
|
|
1881
|
+
method: "POST",
|
|
1882
|
+
headers: { "Content-Type": "application/json" },
|
|
1883
|
+
body: key,
|
|
1884
|
+
}).catch(() => {});
|
|
1885
|
+
} catch {
|
|
1886
|
+
/* 排障通道绝不许影响界面 */
|
|
1887
|
+
}
|
|
1888
|
+
}
|
|
1766
1889
|
|
|
1767
1890
|
/** 打开外部网页:交给系统默认浏览器。DSH 的 Electron 主进程拦截 window.open
|
|
1768
1891
|
* 并转给 shell.openExternal,所以这里不会弹出应用内窗口。 */
|
|
@@ -3017,7 +3140,45 @@ const CN_CALENDAR_SOURCES = [
|
|
|
3017
3140
|
// 等待期间先摆一个不可见占位,让 meta 行高度从一开始就是最终高度 ——
|
|
3018
3141
|
// 否则「今日 ¥x」这个 flex-basis:100% 的元素一插入,整行就从 1 行变 2 行,
|
|
3019
3142
|
// 侧栏底部整体被顶下 14px,冷启动繁忙时看着就是「今日余额弹出来那下卡了」。
|
|
3020
|
-
const todayCache = { value: null, inFlight: null, at: 0, tried: false, ttl: 55000, retries: 0, settled: false };
|
|
3143
|
+
const todayCache = { value: null, inFlight: null, at: 0, tried: false, ttl: 55000, retries: 0, settled: false, suspect: false };
|
|
3144
|
+
// ─── 可疑的 0 不许就地定死(2026-11-09 用户报告:「今日一直显示 ¥0.00,刷新页面才正常」)───
|
|
3145
|
+
// 冷启动最初几秒 DSH 的会话索引还没就绪,`?today=1` 算出来的是「没法看」而不是「没花钱」。
|
|
3146
|
+
// 原来只按 3.2 秒补拉 3 次(约 10 秒)就放弃并 settled,而**此后没有任何时机再问一次**:
|
|
3147
|
+
// · 余额轮询不走 today;浮窗走的是 `?lite=1&session=`,服务端在「没有快照基线」时
|
|
3148
|
+
// 增量直接 null、且 lite 明确不回退全量 → 响应里没有 today → 客户端不触发刷新;
|
|
3149
|
+
// · 当天快照又因为可疑不落盘 → 增量永远没有基线 → 这个 0 能挂一整天。
|
|
3150
|
+
// 现在退避重试:15s → 30s → 60s → 120s → 300s → 封顶 900s,直到拿到可信值(或跨天后自然拿到)。
|
|
3151
|
+
// ⚠️ 封顶不能太小:今天**真的**没花钱时 suspiciousEmpty 也为 true(零点后必然如此),
|
|
3152
|
+
// 而可疑值服务端不落盘 → 每次重试都是一次全量扫描。15 分钟一次是「能及时纠正」与
|
|
3153
|
+
// 「别把主进程一直按在解压上」的折中(一次约 400ms)。
|
|
3154
|
+
const TODAY_SUSPECT_RETRY_MS = [15000, 30000, 60000, 120000, 300000, 900000];
|
|
3155
|
+
let todaySuspectTimer = null;
|
|
3156
|
+
let todaySuspectTries = 0;
|
|
3157
|
+
function clearTodaySuspectRetry() {
|
|
3158
|
+
if (todaySuspectTimer !== null) {
|
|
3159
|
+
clearTimeout(todaySuspectTimer);
|
|
3160
|
+
todaySuspectTimer = null;
|
|
3161
|
+
}
|
|
3162
|
+
todaySuspectTries = 0;
|
|
3163
|
+
}
|
|
3164
|
+
function scheduleTodaySuspectRetry() {
|
|
3165
|
+
if (todaySuspectTimer !== null) return; // 已经排着就等它,别叠
|
|
3166
|
+
const delay = TODAY_SUSPECT_RETRY_MS[Math.min(todaySuspectTries, TODAY_SUSPECT_RETRY_MS.length - 1)];
|
|
3167
|
+
todaySuspectTries += 1;
|
|
3168
|
+
todaySuspectTimer = setTimeout(() => {
|
|
3169
|
+
todaySuspectTimer = null;
|
|
3170
|
+
// force=true:可疑值本来就不该被 TTL 压住(服务端那边也没缓存它)
|
|
3171
|
+
void fetchTodayOnce(true);
|
|
3172
|
+
}, delay);
|
|
3173
|
+
}
|
|
3174
|
+
/** 这一拍的 0 是不是「不可信」(列不出会话 / 筛不出会话 / 有会话被让路跳过)。 */
|
|
3175
|
+
function todayZeroIsSuspect(value) {
|
|
3176
|
+
if (value == null || typeof value !== "object") return false;
|
|
3177
|
+
if (Number(value.cost ?? 0) !== 0) return false;
|
|
3178
|
+
return value.suspiciousEmpty === true
|
|
3179
|
+
|| value.emptyListing === true
|
|
3180
|
+
|| Number(value.skipped ?? 0) > 0;
|
|
3181
|
+
}
|
|
3021
3182
|
const todayListeners = new Set();
|
|
3022
3183
|
function notifyToday() {
|
|
3023
3184
|
// ⚠️ 逐个订阅者隔离(2026-10-31 全量审查)。任何一个订阅者抛错都不能
|
|
@@ -3056,6 +3217,11 @@ const CN_CALENDAR_SOURCES = [
|
|
|
3056
3217
|
? { ...prevToday, provisional: true }
|
|
3057
3218
|
: data.today;
|
|
3058
3219
|
todayCache.at = Date.now();
|
|
3220
|
+
// 可疑的 0:照常显示(2.2.2 的定案 —— 0 也不许让「今日」整行消失),
|
|
3221
|
+
// 但**不许就此定格**,排一条低频退避重试把它纠正回来(见 TODAY_SUSPECT_RETRY_MS)。
|
|
3222
|
+
todayCache.suspect = todayZeroIsSuspect(todayCache.value);
|
|
3223
|
+
if (todayCache.suspect) scheduleTodaySuspectRetry();
|
|
3224
|
+
else clearTodaySuspectRetry();
|
|
3059
3225
|
const partial = data.todayPartial === true && todayCache.retries < 3;
|
|
3060
3226
|
todayCache.ttl = partial ? 3000 : 20000;
|
|
3061
3227
|
// 「首屏数据是否已经定型」:partial 是**同一次计算的分阶段**(先给快照、
|
|
@@ -3071,6 +3237,9 @@ const CN_CALENDAR_SOURCES = [
|
|
|
3071
3237
|
setTimeout(() => { void fetchTodayOnce(true); }, 3200);
|
|
3072
3238
|
} else {
|
|
3073
3239
|
todayCache.retries = 0;
|
|
3240
|
+
// 补拉用完时还是可疑 0 → 立刻换成「短间隔」那档接着追(15 秒),
|
|
3241
|
+
// 别让用户对着一个 0 干等到下一档 30/60 秒。定时器已排着就不重复。
|
|
3242
|
+
if (todayCache.suspect) scheduleTodaySuspectRetry();
|
|
3074
3243
|
}
|
|
3075
3244
|
}
|
|
3076
3245
|
} catch {
|
|
@@ -5671,16 +5840,32 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
5671
5840
|
setFloatSlide((prev) => (prev === false ? prev : false));
|
|
5672
5841
|
},
|
|
5673
5842
|
};
|
|
5843
|
+
// ⚠️ 元素缓存(2026-11-06:「收起和展开时的小浮窗滑动 有点掉帧的感觉」)。
|
|
5844
|
+
// 侧栏的展开/收起是 CSS width 过渡,每帧都会 layout → ResizeObserver 每帧回调 →
|
|
5845
|
+
// measure 每帧跑。原来每拍都要 resolveComposerDockRow + resolveComposerCardEl 里
|
|
5846
|
+
// 若干次 querySelectorAll/getBoundingClientRect,光找锚点就把一帧的预算吃掉一半。
|
|
5847
|
+
// 锚点元素在两次侧栏动画之间是**稳定的**(只有它们的 rect 在动),所以缓存引用、
|
|
5848
|
+
// 每拍只重取 rect;一旦元素被重建(isConnected 或尺寸变化)才重新解析。
|
|
5849
|
+
let dockCache = null;
|
|
5850
|
+
let cardCache = null;
|
|
5674
5851
|
const measure = () => {
|
|
5675
5852
|
if (disposed) return;
|
|
5676
5853
|
const wrap = document.querySelector(".balance-usage-wrap--float");
|
|
5677
5854
|
// ⚠️ 必须拿**真元素**:前面几版查的是 [data-slot="conversation.composer.dock"],
|
|
5678
5855
|
// 那是 display:contents 的出口容器 → rect 全 0 → 被下面的守卫直接 return 掉,
|
|
5679
5856
|
// 于是这套定位从来没生效过,卡片一直停在 CSS 兜底的 16/16(见 resolveComposerDockRow 的注释)。
|
|
5680
|
-
|
|
5681
|
-
|
|
5682
|
-
|
|
5683
|
-
|
|
5857
|
+
let dock = dockCache;
|
|
5858
|
+
let dockRect = dock !== null && dock.isConnected !== false && typeof dock.getBoundingClientRect === "function"
|
|
5859
|
+
? dock.getBoundingClientRect()
|
|
5860
|
+
: null;
|
|
5861
|
+
if (dockRect === null || dockRect.width === 0 || dockRect.height === 0) {
|
|
5862
|
+
dock = resolveComposerDockRow(wrap);
|
|
5863
|
+
dockCache = dock;
|
|
5864
|
+
dockRect = dock !== null && typeof dock.getBoundingClientRect === "function"
|
|
5865
|
+
? dock.getBoundingClientRect()
|
|
5866
|
+
: null;
|
|
5867
|
+
}
|
|
5868
|
+
if (dock === null || dockRect === null || dockRect.width === 0 || dockRect.height === 0) return;
|
|
5684
5869
|
// 往上取一层(卡片那一列),只在高度合理且确实在 dock 上方时才用
|
|
5685
5870
|
let box = dockRect;
|
|
5686
5871
|
const outer = dock.parentElement;
|
|
@@ -5709,23 +5894,73 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
5709
5894
|
// 几百像素的屏幕最右边(用户截图:1920 宽下卡片在 x1808,输入框右缘才 1573)。
|
|
5710
5895
|
// 现在锚**输入卡片自己的右缘 + FLOAT_SIDE_GAP**,窗口再宽也跟输入框走。
|
|
5711
5896
|
let cardRect = null;
|
|
5712
|
-
|
|
5713
|
-
|
|
5714
|
-
|
|
5715
|
-
|
|
5716
|
-
|
|
5897
|
+
{
|
|
5898
|
+
// 2026-11-06(DSH 0.2.2):改走 resolveComposerCardRect —— 它会在**自己那棵 composer
|
|
5899
|
+
// 子树**里挑可见的卡片,不再被另一个实例(隐藏的 hero composer,rect 全 0)抢走,
|
|
5900
|
+
// 也就不会掉进「聊天列右缘 − 4」那条老公式、把浮窗甩到窗口最右边。
|
|
5901
|
+
// 同样走缓存:稳定时每拍只读一次 rect,省掉 querySelectorAll + 遍历(见 dockCache 的注释)。
|
|
5902
|
+
let card = cardCache;
|
|
5903
|
+
let cachedRect = card !== null && card.isConnected !== false && typeof card.getBoundingClientRect === "function"
|
|
5904
|
+
? card.getBoundingClientRect()
|
|
5905
|
+
: null;
|
|
5906
|
+
if (cachedRect === null || cachedRect.width === 0 || cachedRect.height === 0
|
|
5907
|
+
|| cachedRect.right <= 8 || cachedRect.right > vw) {
|
|
5908
|
+
card = resolveComposerCardEl(dock, vw);
|
|
5909
|
+
cardCache = card;
|
|
5910
|
+
cachedRect = card !== null && typeof card.getBoundingClientRect === "function"
|
|
5911
|
+
? card.getBoundingClientRect()
|
|
5912
|
+
: null;
|
|
5717
5913
|
}
|
|
5914
|
+
if (cachedRect !== null && cachedRect.width > 0 && cachedRect.height > 0) cardRect = cachedRect;
|
|
5718
5915
|
}
|
|
5719
5916
|
// 浮窗自己的宽度(fixed 定位,包裹层被压成 0×0 不影响卡片自己的矩形)
|
|
5720
5917
|
let floatW = 0;
|
|
5918
|
+
let selfElRef = null;
|
|
5919
|
+
let selfRectRef = null;
|
|
5721
5920
|
if (wrap !== null && typeof wrap.querySelector === "function") {
|
|
5722
5921
|
const selfEl = wrap.querySelector(".balance-float");
|
|
5723
5922
|
if (selfEl !== null && typeof selfEl.getBoundingClientRect === "function") {
|
|
5724
5923
|
const selfRect = selfEl.getBoundingClientRect();
|
|
5924
|
+
selfElRef = selfEl;
|
|
5925
|
+
selfRectRef = selfRect;
|
|
5725
5926
|
if (selfRect.width > 0) floatW = selfRect.width;
|
|
5726
5927
|
}
|
|
5727
5928
|
}
|
|
5728
5929
|
if (floatW <= 0) floatW = 101; // 兜底:标签 77 + 左右 padding 24
|
|
5930
|
+
// ── fixed 的包含块偏移(2026-11-06 DSH 0.2.2「会话消耗浮窗位置异常」的真凶)──────
|
|
5931
|
+
// 实测数据:算出来 left=1021 / bottom=41,屏幕上卡片却在 x1174 / 底边距 46;
|
|
5932
|
+
// 1174 − 1021 = 153 = composer 容器(dock 的父元素)的左缘,46 − 41 = 5 = 它到底边的距离。
|
|
5933
|
+
// 也就是说 `position:fixed` **不再相对视口**,而是相对那个容器 —— 新版给 composer 的
|
|
5934
|
+
// 祖先加了 `transform` / `filter` 这类会生成包含块的属性,fixed 后代就会被它「捕获」。
|
|
5935
|
+
// 解法不是 portal(切视图时祖先 display:none,portal 出去会浮在设置页上),
|
|
5936
|
+
// 而是**量出包含块偏移再补偿**:`rect.left − computed.left` 恒等于包含块原点,
|
|
5937
|
+
// 与当前写的是 left 还是 right、有没有过渡都无关,所以每次测量都能自校准。
|
|
5938
|
+
let cbOffsetX = 0;
|
|
5939
|
+
let cbOffsetBottom = 0;
|
|
5940
|
+
if (selfElRef !== null && selfRectRef !== null
|
|
5941
|
+
&& selfElRef.isConnected !== false
|
|
5942
|
+
&& typeof window.getComputedStyle === "function") {
|
|
5943
|
+
try {
|
|
5944
|
+
const cs = window.getComputedStyle(selfElRef);
|
|
5945
|
+
const csLeft = parseFloat(cs.left);
|
|
5946
|
+
const csBottom = parseFloat(cs.bottom);
|
|
5947
|
+
if (Number.isFinite(csLeft)) cbOffsetX = Math.round(selfRectRef.left - csLeft);
|
|
5948
|
+
if (Number.isFinite(csBottom)) cbOffsetBottom = Math.round((vh - selfRectRef.bottom) - csBottom);
|
|
5949
|
+
} catch {
|
|
5950
|
+
cbOffsetX = 0;
|
|
5951
|
+
cbOffsetBottom = 0;
|
|
5952
|
+
}
|
|
5953
|
+
}
|
|
5954
|
+
// ⚠️ 包含块原点是「视口内的一点」,必须落在 [0, 视口尺寸] 里。
|
|
5955
|
+
// 跑出这个范围只能是这一拍量到了不可靠的东西:侧栏展开/收起时 composer 子树会被整块重建,
|
|
5956
|
+
// 刚挂上的那个元素还没布局 → rect 全 0 → `rect.left − computed.left` 变成负的一大截(−870 这种)。
|
|
5957
|
+
// ⚠️ 而且这一拍**不能只回退偏移、照旧写位置**:屏幕落点 = left − (本拍偏移 − 真偏移),
|
|
5958
|
+
// 偏移是旧值、left(卡片右缘)已经是新布局时,两者不同步,落点就凭空平移了那 66px ——
|
|
5959
|
+
// 正是 2026-11-06 用户说的「过冲 收起和展开都是」(截图:浮窗压在输入框右下角)。
|
|
5960
|
+
// 所以整拍丢弃,等下一拍(补拍 16ms 内一定还有一拍)重算。
|
|
5961
|
+
if (!(cbOffsetX >= 0 && cbOffsetX <= vw) || !(cbOffsetBottom >= 0 && cbOffsetBottom <= vh)) {
|
|
5962
|
+
return;
|
|
5963
|
+
}
|
|
5729
5964
|
// 横向:卡片**左缘**落在「输入卡片右缘 + 16px」。
|
|
5730
5965
|
// ⚠️ 2026-11-02 第十六轮:「切换对话之后有非常短距离的位移」—— 上一版锚的是 right
|
|
5731
5966
|
// (`vw - (卡片右缘 + 16 + floatW)`),公式里含**浮窗自己的宽度**:切会话时卡片内容
|
|
@@ -5756,20 +5991,97 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
5756
5991
|
// · 视口宽变了 → 用户在拖窗口边缘,这次必须**不挂过渡**,否则卡片滞后于窗口,
|
|
5757
5992
|
// 拖动时像被橡皮筋拽着(4 拍补拍都还在,位置本身不会错)。
|
|
5758
5993
|
// 位移 ≤ FLOAT_JITTER_PX 的那种抖动早已被下面 setFloatBox 吞掉,走不到这里。
|
|
5994
|
+
// 写进 style 的是**补偿过包含块偏移**的值(见 cbOffsetX 的注释):
|
|
5995
|
+
// 视口目标 (left, bottom) − 包含块偏移 = fixed 实际需要的 left/bottom。
|
|
5996
|
+
const styleLeft = Math.round(left - cbOffsetX);
|
|
5997
|
+
const styleBottom = Math.round(bottom - cbOffsetBottom);
|
|
5998
|
+
// ⚠️ 包含块自己正在动(展开/收起侧边栏:容器一边变宽一边平移)时**不许挂 left 过渡**。
|
|
5999
|
+
// 2026-11-06 用户:「展开侧边栏的时候 小浮窗会往左边跑一下」。两个动画源打架:
|
|
6000
|
+
// 容器原点每帧在移动(fixed 后代跟着它走),同时 `left` 又在 150ms 里追一个移动中的目标 ——
|
|
6001
|
+
// 屏幕上就是先偏出去再被拉回来。包含块一动就瞬时对齐(容器本身是平滑的,看着照样连续)。
|
|
6002
|
+
const cbMoved = floatCbOffset === null
|
|
6003
|
+
|| floatCbOffset.x !== cbOffsetX
|
|
6004
|
+
|| floatCbOffset.bottom !== cbOffsetBottom;
|
|
6005
|
+
floatCbOffset = { x: cbOffsetX, bottom: cbOffsetBottom };
|
|
6006
|
+
// ⚠️ 位置**直接写 DOM**,不经过 React(2026-11-06:「有点掉帧的感觉」)。
|
|
6007
|
+
// 侧栏展开/收起是 width 过渡,这个值每帧都在变;走 state 就等于每帧重渲染整个浮窗子树
|
|
6008
|
+
// (标签 + 团队圆片 + 迷雾 + 滚格子),而位置只是两个长度,直接写 style 就够了。
|
|
6009
|
+
// React 那边因此**不再管 left/bottom**(见渲染处),也就不会在下次渲染时把新值覆盖回去;
|
|
6010
|
+
// 元素首次挂载由 pre-paint 的这一次 measure 写入,不会露 CSS 兜底。
|
|
6011
|
+
if (selfElRef !== null && selfElRef.style !== null && typeof selfElRef.style === "object") {
|
|
6012
|
+
try {
|
|
6013
|
+
selfElRef.style.left = styleLeft + "px";
|
|
6014
|
+
selfElRef.style.bottom = styleBottom + "px";
|
|
6015
|
+
selfElRef.style.right = "auto";
|
|
6016
|
+
} catch {
|
|
6017
|
+
/* 写不进去就靠下面的 setFloatBox 兜底 */
|
|
6018
|
+
}
|
|
6019
|
+
}
|
|
6020
|
+
// 排障采样:这一拍到底量到了什么(见 reportFloatGeom 的注释)。
|
|
6021
|
+
// ⚠️ **必须放在写 style 之后**(2026-11-09):self 这一项读的是「卡片现在到底在哪」,
|
|
6022
|
+
// 放在写之前读到的是**上一拍**渲染的位置 —— 位置刚变过就会差好几像素,
|
|
6023
|
+
// 诊断里白白报一个「✗ 偏移」(实测差 6px)。写完之后读,getBoundingClientRect
|
|
6024
|
+
// 会强制刷新布局,拿到的才是这次写进去的落点。
|
|
6025
|
+
// 代价只是几次 getBoundingClientRect,POST 有节流,出问题时可从 ?diag=1 直接读。
|
|
6026
|
+
try {
|
|
6027
|
+
// ⚠️ 采样本身要花钱(两次 querySelectorAll + 最多八个 getBoundingClientRect)。
|
|
6028
|
+
// 侧栏动画期间 measure 每帧跑,必须**先判节流再采样**,否则排障通道自己就是掉帧源。
|
|
6029
|
+
if (Date.now() - floatGeomSentAt < 3000) throw 0;
|
|
6030
|
+
const collect = (sel) => {
|
|
6031
|
+
if (typeof document.querySelectorAll !== "function") return null;
|
|
6032
|
+
const list = document.querySelectorAll(sel);
|
|
6033
|
+
const rects = [];
|
|
6034
|
+
for (let i = 0; i < list.length && i < 4; i += 1) {
|
|
6035
|
+
const el = list[i];
|
|
6036
|
+
if (el !== null && el !== undefined && typeof el.getBoundingClientRect === "function") {
|
|
6037
|
+
rects.push(rectBrief(el.getBoundingClientRect()));
|
|
6038
|
+
}
|
|
6039
|
+
}
|
|
6040
|
+
return { count: list.length, rects };
|
|
6041
|
+
};
|
|
6042
|
+
let selfRect = null;
|
|
6043
|
+
if (wrap !== null && typeof wrap.querySelector === "function") {
|
|
6044
|
+
const selfEl = wrap.querySelector(".balance-float");
|
|
6045
|
+
if (selfEl !== null && typeof selfEl.getBoundingClientRect === "function") {
|
|
6046
|
+
selfRect = rectBrief(selfEl.getBoundingClientRect());
|
|
6047
|
+
}
|
|
6048
|
+
}
|
|
6049
|
+
reportFloatGeom({
|
|
6050
|
+
vw: Math.round(vw),
|
|
6051
|
+
vh: Math.round(vh),
|
|
6052
|
+
dock: rectBrief(dockRect),
|
|
6053
|
+
box: rectBrief(box),
|
|
6054
|
+
card: rectBrief(cardRect),
|
|
6055
|
+
composerCards: collect("[data-composer-card]"),
|
|
6056
|
+
composerDocks: collect("[data-composer-dock]"),
|
|
6057
|
+
self: selfRect,
|
|
6058
|
+
cbOffsetX,
|
|
6059
|
+
cbOffsetBottom,
|
|
6060
|
+
left,
|
|
6061
|
+
bottom,
|
|
6062
|
+
styleLeft: Math.round(left - cbOffsetX),
|
|
6063
|
+
styleBottom: Math.round(bottom - cbOffsetBottom),
|
|
6064
|
+
});
|
|
6065
|
+
} catch {
|
|
6066
|
+
/* ignore */
|
|
6067
|
+
}
|
|
5759
6068
|
const vwNow = Math.round(vw);
|
|
5760
6069
|
const vwChanged = floatMeasureVw !== null && vwNow !== floatMeasureVw;
|
|
5761
6070
|
floatMeasureVw = vwNow;
|
|
5762
6071
|
const movedPx = floatBoxMemo === null
|
|
5763
6072
|
? 0
|
|
5764
|
-
: Math.max(Math.abs(floatBoxMemo.left -
|
|
6073
|
+
: Math.max(Math.abs(floatBoxMemo.left - styleLeft), Math.abs(floatBoxMemo.bottom - styleBottom));
|
|
5765
6074
|
// 大跳变 + 视口没变 → 内部布局在动(收起/展开侧边栏),挂滑动过渡并保持一段;
|
|
5766
6075
|
// 视口变了 → 用户在拖窗口边缘,立刻摘掉过渡,卡片必须紧跟窗口(否则像被橡皮筋拽着)。
|
|
5767
|
-
if (vwChanged) measureCtl.slideOff();
|
|
6076
|
+
if (vwChanged || cbMoved) measureCtl.slideOff();
|
|
5768
6077
|
else if (movedPx >= FLOAT_SLIDE_MIN_PX) measureCtl.slideOn();
|
|
5769
6078
|
// 布局还在动(侧边栏展开/收起带动输入卡片变宽)→ 顺手把观察者补挂到当前 DOM 上,
|
|
5770
6079
|
// 并再跟几拍。少了这两句,元素一被重建就退回「等兜底轮询」的老路 = 位置刷新慢半拍。
|
|
5771
6080
|
measureCtl.sync();
|
|
5772
|
-
|
|
6081
|
+
// ⚠️ 包含块自己在动时(侧栏展开/收起的 width 过渡)**不排补拍**:
|
|
6082
|
+
// 那段里 ResizeObserver 每帧都会回调 measure,本来就不缺触发源;
|
|
6083
|
+
// 每帧再排 8 个定时器(0/16/40/…/400ms)纯属自己给自己加戏,是掉帧的一半来源。
|
|
6084
|
+
if (movedPx > FLOAT_JITTER_PX && !cbMoved) measureCtl.settle();
|
|
5773
6085
|
// 写 state 的同时记进模块级 memo:下一帧/下一次重挂载的首帧都用这个值,
|
|
5774
6086
|
// 不再等测量(否则切会话会闪一帧 CSS 兜底的 16/16)。
|
|
5775
6087
|
setFloatBox((prev) => {
|
|
@@ -5782,17 +6094,17 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
5782
6094
|
// 不会被它挡住;切会话时 memo 里那份也照样生效)。
|
|
5783
6095
|
if (
|
|
5784
6096
|
prev !== null &&
|
|
5785
|
-
Math.abs(prev.left -
|
|
5786
|
-
Math.abs(prev.bottom -
|
|
6097
|
+
Math.abs(prev.left - styleLeft) <= FLOAT_JITTER_PX &&
|
|
6098
|
+
Math.abs(prev.bottom - styleBottom) <= FLOAT_JITTER_PX
|
|
5787
6099
|
) {
|
|
5788
6100
|
floatBoxMemo = prev;
|
|
5789
6101
|
return prev;
|
|
5790
6102
|
}
|
|
5791
|
-
if (prev !== null && prev.left ===
|
|
6103
|
+
if (prev !== null && prev.left === styleLeft && prev.bottom === styleBottom) {
|
|
5792
6104
|
floatBoxMemo = prev;
|
|
5793
6105
|
return prev;
|
|
5794
6106
|
}
|
|
5795
|
-
floatBoxMemo = { left, bottom };
|
|
6107
|
+
floatBoxMemo = { left: styleLeft, bottom: styleBottom };
|
|
5796
6108
|
return floatBoxMemo;
|
|
5797
6109
|
});
|
|
5798
6110
|
};
|
|
@@ -5836,7 +6148,10 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
5836
6148
|
}
|
|
5837
6149
|
const targets = [];
|
|
5838
6150
|
const floatWrap = document.querySelector(".balance-usage-wrap--float");
|
|
5839
|
-
const
|
|
6151
|
+
const dockRow = resolveComposerDockRow(floatWrap);
|
|
6152
|
+
// 2026-11-06:观察目标同样要挑「自己这张卡」。原来 querySelector 取第一个,
|
|
6153
|
+
// 命中隐藏的 hero composer 就等于把 RO 挂在一个永不变化的零尺寸元素上(位置慢半拍)。
|
|
6154
|
+
const composerCard = resolveComposerCardEl(dockRow, window.innerWidth || 0);
|
|
5840
6155
|
if (composerCard !== null) targets.push(composerCard);
|
|
5841
6156
|
// ⚠️ 光盯 composer 卡片不够:收起/展开侧边栏时 DSH 会把 composer 子树整块重建,
|
|
5842
6157
|
// 旧元素上的观察随元素一起失效 —— 那一刻没有任何东西通知我们「布局变了」,
|
|
@@ -5849,7 +6164,6 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
5849
6164
|
if (ancestor === null || ancestor === document.body || ancestor === document.documentElement) break;
|
|
5850
6165
|
targets.push(ancestor);
|
|
5851
6166
|
}
|
|
5852
|
-
const dockRow = resolveComposerDockRow(floatWrap);
|
|
5853
6167
|
if (dockRow !== null) targets.push(dockRow);
|
|
5854
6168
|
if (floatWrap !== null && typeof floatWrap.querySelector === "function") {
|
|
5855
6169
|
const selfEl = floatWrap.querySelector(".balance-float");
|
|
@@ -6176,8 +6490,14 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
6176
6490
|
// 2026-11-02 补充:现在**先直接采纳**它(`raiseTodayFromSession`,只抬不降),
|
|
6177
6491
|
// 因为磁盘口径看不见当前会话还没落盘的那一截 —— 只当"补拉开关"用的时候,
|
|
6178
6492
|
// 冷启动会一直停在 ¥0.0000,最长要等一个 TTL(20s) 才动。
|
|
6179
|
-
|
|
6180
|
-
if (
|
|
6493
|
+
const sessionToday = u && u.today && typeof u.today.cost === "number" ? u.today : null;
|
|
6494
|
+
if (sessionToday !== null) raiseTodayFromSession(sessionToday);
|
|
6495
|
+
// ⚠️ 拿不到增量时**也要允许触发一次全量**(2026-11-09 那个「今日一整天 0」的第二个缺口):
|
|
6496
|
+
// 服务端在「当天快照不存在」时 `updateTodayCostWithSession` 直接返回 null,
|
|
6497
|
+
// 而 lite 路径明确不回退全量(为了流式时 600ms 一次不打爆磁盘)——
|
|
6498
|
+
// 于是响应里根本没有 today,这里原来两个 if 全不成立,一次都不会去问 `?today=1`。
|
|
6499
|
+
// 现在只要今日还停在**可疑的 0**上就照问,TTL(20 秒)负责压节奏。
|
|
6500
|
+
if (!disposed && todayCache.settled && (sessionToday !== null || todayCache.suspect === true)) {
|
|
6181
6501
|
void fetchTodayOnce(false);
|
|
6182
6502
|
}
|
|
6183
6503
|
// ⚠️ **空结果要重试**(2026-11-02 第三轮,「从大对话切到小对话还是加载很久」的真因):
|
|
@@ -7140,11 +7460,11 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
7140
7460
|
"aria-label": "本会话消耗明细",
|
|
7141
7461
|
onClick: () => setDetailsOpen((value) => !value),
|
|
7142
7462
|
style: {
|
|
7143
|
-
|
|
7144
|
-
|
|
7145
|
-
|
|
7146
|
-
|
|
7147
|
-
|
|
7463
|
+
// ⚠️ 位置不在这里写(2026-11-06:「有点掉帧的感觉」)。
|
|
7464
|
+
// left/bottom 由定位 effect 直接写到 DOM 上(它每帧都在变,走 state 会每帧重渲染整棵子树);
|
|
7465
|
+
// 这里只负责把 CSS 兜底那条 `right:16px` 关掉,否则 left/right 同时生效会被拉伸。
|
|
7466
|
+
// 首帧不会露兜底:那个 effect 是 pre-paint 的,量完立刻写。
|
|
7467
|
+
...(floatBox === null ? null : { right: "auto" }),
|
|
7148
7468
|
// 插件加载动画走完之前不显形(用户:「插件加载之前 会话消耗小窗不要加载」)。
|
|
7149
7469
|
// 用 visibility 而不是不渲染:`.balance-usage-line` 必须留在 DOM 里(离场探测/轮询靠它)。
|
|
7150
7470
|
// 窗口缩到很窄时整块藏起来(2026-11-03:「这俩悬浮窗直接隐藏」+「隐藏的时候淡出」):
|
|
@@ -7325,6 +7645,225 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
7325
7645
|
// 刻意**不用** ownerProps.form:那要接入 DSH 的 settings schema(内部 API,
|
|
7326
7646
|
// 无兼容承诺),配置值改走插件自己的 /balance-api?config=1(见 lib/index.js),
|
|
7327
7647
|
// 所以这里忽略 form、自己读写。缺失 view / props 时按 page 渲染,不会崩。
|
|
7648
|
+
// ─── 数据诊断(2026-11-09:「能不能在插件设置页里添加数据诊断按钮」)─────────
|
|
7649
|
+
// 起因:用户报「今日一直 ¥0.00」时,光看界面分不清是数据算错了、还是界面没刷新,
|
|
7650
|
+
// 得靠外部 PowerShell 打接口才看得出来。这里把那套自查搬进设置页,点一下就把
|
|
7651
|
+
// `?diag=1` 的关键字段摆成一张能选中复制、能直接贴给别人的纯文本表。
|
|
7652
|
+
// 刻意只读、不发任何写请求,也不改任何设置。
|
|
7653
|
+
function formatDiagMoney(value) {
|
|
7654
|
+
const n = Number(value);
|
|
7655
|
+
return Number.isFinite(n) && value !== null && value !== undefined ? "¥" + n.toFixed(4) : "¥—";
|
|
7656
|
+
}
|
|
7657
|
+
function formatDiagTime(ms) {
|
|
7658
|
+
const n = Number(ms);
|
|
7659
|
+
if (!Number.isFinite(n) || n <= 0) return "—";
|
|
7660
|
+
try {
|
|
7661
|
+
return new Date(n).toLocaleString("zh-CN", { hour12: false });
|
|
7662
|
+
} catch {
|
|
7663
|
+
return "—";
|
|
7664
|
+
}
|
|
7665
|
+
}
|
|
7666
|
+
function formatDiagRect(rect) {
|
|
7667
|
+
if (rect == null || typeof rect !== "object") return "—";
|
|
7668
|
+
const x = Number(rect.x ?? 0);
|
|
7669
|
+
const y = Number(rect.y ?? 0);
|
|
7670
|
+
const right = Number(rect.right ?? x);
|
|
7671
|
+
const bottom = Number(rect.bottom ?? y);
|
|
7672
|
+
return `${x}..${right} × ${y}..${bottom}`;
|
|
7673
|
+
}
|
|
7674
|
+
/** usage 的 token 分项:只印非零的那几项,再给个总数。 */
|
|
7675
|
+
function formatDiagTokens(usage) {
|
|
7676
|
+
const u = usage != null && typeof usage === "object" ? usage : {};
|
|
7677
|
+
const parts = [];
|
|
7678
|
+
let sum = 0;
|
|
7679
|
+
const pick = [
|
|
7680
|
+
["输入", u.inputTokens],
|
|
7681
|
+
["输出", u.outputTokens],
|
|
7682
|
+
["缓存写", u.cacheCreationTokens],
|
|
7683
|
+
["缓存读", u.cacheReadTokens],
|
|
7684
|
+
];
|
|
7685
|
+
for (const [name, raw] of pick) {
|
|
7686
|
+
const n = Number(raw ?? 0);
|
|
7687
|
+
if (!Number.isFinite(n) || n === 0) continue;
|
|
7688
|
+
sum += n;
|
|
7689
|
+
parts.push(name + " " + n);
|
|
7690
|
+
}
|
|
7691
|
+
return { sum, text: parts.length ? parts.join(" ") : "全 0" };
|
|
7692
|
+
}
|
|
7693
|
+
/** 一份「出问题时能把整条链路对出来」的诊断文本。
|
|
7694
|
+
* ⚠️ 2026-11-09 第二轮(用户:「诊断出来的信息是不是太少了 如果用户遇到别的数据异常
|
|
7695
|
+
* 这东西复制过来好像也不知道哪里有问题」):所以正文按环节分段把**能拿到的都列出来**,
|
|
7696
|
+
* 末尾再附一段**原始响应 JSON**(截断)—— 任何没被提到的情况都能从原始数据里查,
|
|
7697
|
+
* 不用再让用户去开控制台。 */
|
|
7698
|
+
function formatDiagnostics(data, version, settings) {
|
|
7699
|
+
const d = data != null && typeof data === "object" ? data : {};
|
|
7700
|
+
const s = settings != null && typeof settings === "object" ? settings : {};
|
|
7701
|
+
const lines = [];
|
|
7702
|
+
const where = typeof location === "object" && location !== null
|
|
7703
|
+
? `${location.hostname || "127.0.0.1"}:${location.port || "?"}`
|
|
7704
|
+
: "?";
|
|
7705
|
+
lines.push(`balance-dsh ${version || "—"} · ${where} · ${formatDiagTime(Date.now())}`);
|
|
7706
|
+
|
|
7707
|
+
// ── 余额 ──
|
|
7708
|
+
const bal = d.balance;
|
|
7709
|
+
const balObj = bal != null && typeof bal === "object" ? bal : null;
|
|
7710
|
+
const balTotal = balObj === null ? null : balObj.total_balance ?? balObj.totalBalance ?? null;
|
|
7711
|
+
const granted = balObj === null ? null : balObj.granted_balance ?? balObj.grantedBalance ?? null;
|
|
7712
|
+
lines.push("");
|
|
7713
|
+
lines.push("── 余额 ──");
|
|
7714
|
+
lines.push(
|
|
7715
|
+
`总额 ${balTotal == null ? "¥—" : formatDiagMoney(balTotal)}` +
|
|
7716
|
+
(granted == null ? "" : ` 赠送 ${formatDiagMoney(granted)}`) +
|
|
7717
|
+
(balObj === null ? " (未登录或取不到)" : ` 可用=${balObj.is_available !== false}`),
|
|
7718
|
+
);
|
|
7719
|
+
if (d.balanceError) lines.push(`balanceError ${d.balanceError}`);
|
|
7720
|
+
|
|
7721
|
+
// ── 今日 ──
|
|
7722
|
+
// 响应里的 `today` 经常是**增量/合并**出来的值(带 `provisional: true`),
|
|
7723
|
+
// 那种对象不带 suspiciousEmpty / emptyListing / skipped —— 只看它会得出「一切正常」。
|
|
7724
|
+
// 所以判定要把 `todayDiag.snapshot` 里那份落盘快照的标记也一起算进来。
|
|
7725
|
+
const today = d.today;
|
|
7726
|
+
const td = d.todayDiag != null && typeof d.todayDiag === "object" ? d.todayDiag : null;
|
|
7727
|
+
const snap = td != null && td.snapshot != null && typeof td.snapshot === "object" ? td.snapshot : null;
|
|
7728
|
+
const anySuspect =
|
|
7729
|
+
today?.suspiciousEmpty === true ||
|
|
7730
|
+
today?.emptyListing === true ||
|
|
7731
|
+
Number(today?.skipped ?? 0) > 0 ||
|
|
7732
|
+
snap?.suspiciousEmpty === true ||
|
|
7733
|
+
snap?.emptyListing === true ||
|
|
7734
|
+
Number(snap?.skipped ?? 0) > 0;
|
|
7735
|
+
lines.push("");
|
|
7736
|
+
lines.push("── 今日 ──");
|
|
7737
|
+
if (today == null || typeof today !== "object") {
|
|
7738
|
+
lines.push(`服务端暂未给出(todayPartial=${d.todayPartial === true})`);
|
|
7739
|
+
} else {
|
|
7740
|
+
lines.push(
|
|
7741
|
+
`金额 ${formatDiagMoney(today.cost)} date=${today.date ?? "—"} ` +
|
|
7742
|
+
`sessions=${today.sessions ?? 0} tokens=${today.totalTokens ?? 0}`,
|
|
7743
|
+
);
|
|
7744
|
+
// 三态:光看 today 是分不出「真 0」和「还在算」的
|
|
7745
|
+
const zeroCost = Number(today.cost ?? 0) === 0;
|
|
7746
|
+
const pending = td != null && (td.cacheProvisional === true || td.recomputing === true);
|
|
7747
|
+
const verdict = !zeroCost
|
|
7748
|
+
? "可信"
|
|
7749
|
+
: anySuspect
|
|
7750
|
+
? "不可信(假 0,会自动退避重试)"
|
|
7751
|
+
: pending
|
|
7752
|
+
? "待定(服务端还在算,这一拍不是最终值)"
|
|
7753
|
+
: "可信";
|
|
7754
|
+
lines.push(
|
|
7755
|
+
`可信 ${verdict} suspiciousEmpty=${today.suspiciousEmpty === true} ` +
|
|
7756
|
+
`emptyListing=${today.emptyListing === true} skipped=${today.skipped ?? 0} ` +
|
|
7757
|
+
`partial=${d.todayPartial === true}`,
|
|
7758
|
+
);
|
|
7759
|
+
}
|
|
7760
|
+
if (td != null) {
|
|
7761
|
+
lines.push(
|
|
7762
|
+
`缓存 cost=${td.cacheCost == null ? "—" : Number(td.cacheCost).toFixed(4)} ` +
|
|
7763
|
+
`${Math.round(Number(td.cacheExpiresInMs ?? 0) / 1000)}s 后过期 ` +
|
|
7764
|
+
`provisional=${td.cacheProvisional === true} 重算中=${td.recomputing === true}`,
|
|
7765
|
+
);
|
|
7766
|
+
if (snap == null) {
|
|
7767
|
+
lines.push("快照 —(文件不存在:可疑结果按设计不落盘,也可能今天还没算过)");
|
|
7768
|
+
} else {
|
|
7769
|
+
lines.push(
|
|
7770
|
+
`快照 ${formatDiagMoney(snap.cost)} date=${snap.date ?? "—"} v=${snap.v ?? "—"} ` +
|
|
7771
|
+
`sessions=${snap.sessions ?? 0} at=${formatDiagTime(snap.at)}`,
|
|
7772
|
+
);
|
|
7773
|
+
lines.push(
|
|
7774
|
+
`标记 suspiciousEmpty=${snap.suspiciousEmpty === true} ` +
|
|
7775
|
+
`emptyListing=${snap.emptyListing === true} skipped=${snap.skipped ?? 0}`,
|
|
7776
|
+
);
|
|
7777
|
+
}
|
|
7778
|
+
}
|
|
7779
|
+
|
|
7780
|
+
// ── 本会话 ──
|
|
7781
|
+
lines.push("");
|
|
7782
|
+
lines.push("── 本会话 ──");
|
|
7783
|
+
const usage = d.usage;
|
|
7784
|
+
if (usage != null && typeof usage === "object") {
|
|
7785
|
+
const tk = formatDiagTokens(usage);
|
|
7786
|
+
lines.push(`金额 ${formatDiagMoney(usage.cost)} model=${d.model ?? "—"} tokens ${tk.sum}`);
|
|
7787
|
+
if (tk.text !== "全 0") lines.push(`分项 ${tk.text}`);
|
|
7788
|
+
const extra = [];
|
|
7789
|
+
if (usage.peakCost != null) extra.push(`高峰 ${formatDiagMoney(usage.peakCost)}`);
|
|
7790
|
+
if (usage.offpeakCost != null) extra.push(`空闲 ${formatDiagMoney(usage.offpeakCost)}`);
|
|
7791
|
+
if (usage.webToolCost != null) extra.push(`网页工具 ${formatDiagMoney(usage.webToolCost)}`);
|
|
7792
|
+
if (extra.length) lines.push(`拆分 ${extra.join(" ")}`);
|
|
7793
|
+
} else {
|
|
7794
|
+
lines.push("—(诊断请求没带会话参数,这一项不在本次统计内;要看本会话明细请用 /balance-dsh-diag)");
|
|
7795
|
+
}
|
|
7796
|
+
const team = d.team;
|
|
7797
|
+
if (team != null && typeof team === "object" && Number(team.count ?? 0) > 0) {
|
|
7798
|
+
lines.push(`团队 ${team.count} 人 ${formatDiagMoney(team.cost)} tokens ${team.tokens ?? 0}`);
|
|
7799
|
+
}
|
|
7800
|
+
if (d.subagentUsage != null && typeof d.subagentUsage === "object") {
|
|
7801
|
+
lines.push(`子代理 ${formatDiagMoney(d.subagentUsage.cost)} sessions=${d.subagentUsage.sessions ?? 0}`);
|
|
7802
|
+
}
|
|
7803
|
+
|
|
7804
|
+
// ── 设置(口径类,出问题时先确认这些没被改过) ──
|
|
7805
|
+
lines.push("");
|
|
7806
|
+
lines.push("── 设置 ──");
|
|
7807
|
+
lines.push(
|
|
7808
|
+
`轮询 ${s.refreshIntervalSec ?? "—"}s · 今日扫描上限 ${s.todaySessionLimit ?? "—"} · ` +
|
|
7809
|
+
`统计上限 ${s.statsSessionLimit ?? "—"} · 统计缓存 ${s.statsCacheSec ?? "—"}s · ` +
|
|
7810
|
+
`低余额阈值 ${s.lowBalanceThreshold ?? "—"}`,
|
|
7811
|
+
);
|
|
7812
|
+
lines.push(
|
|
7813
|
+
`高峰 ${s.peakWindowsText ?? "—"} × ${s.peakMultiplier ?? "—"} · ` +
|
|
7814
|
+
`浮窗 ${s.showUsagePill === false ? "关" : "开"} · 余额行 ${s.showBalanceRow === false ? "关" : "开"} · ` +
|
|
7815
|
+
`侧栏入口 ${s.showSidebarEntry === false ? "关" : "开"} · 圆钮 ${s.showFab === false ? "关" : "开"}`,
|
|
7816
|
+
);
|
|
7817
|
+
|
|
7818
|
+
// ── 浮窗定位 ──
|
|
7819
|
+
lines.push("");
|
|
7820
|
+
lines.push("── 浮窗 ──");
|
|
7821
|
+
const g = d.floatGeom;
|
|
7822
|
+
if (g == null || typeof g !== "object") {
|
|
7823
|
+
lines.push("—(还没有测量数据:浮窗没渲染,或页面刚起来)");
|
|
7824
|
+
} else {
|
|
7825
|
+
const selfX = g.self != null && Number.isFinite(Number(g.self.x)) ? Number(g.self.x) : null;
|
|
7826
|
+
const left = Number.isFinite(Number(g.left)) ? Number(g.left) : null;
|
|
7827
|
+
const aligned = selfX !== null && left !== null && Math.abs(selfX - left) <= 2;
|
|
7828
|
+
lines.push(
|
|
7829
|
+
`${aligned ? "✓ 对齐" : "✗ 偏移"} left=${left ?? "—"} self.x=${selfX ?? "—"} ` +
|
|
7830
|
+
`styleLeft=${g.styleLeft ?? "—"} 视口=${g.vw ?? "?"}×${g.vh ?? "?"}`,
|
|
7831
|
+
);
|
|
7832
|
+
lines.push(`card ${formatDiagRect(g.card)} dock ${formatDiagRect(g.dock)}`);
|
|
7833
|
+
lines.push(
|
|
7834
|
+
`cbOffsetX=${g.cbOffsetX ?? "—"} cbOffsetBottom=${g.cbOffsetBottom ?? "—"} ` +
|
|
7835
|
+
`测量于 ${formatDiagTime(g.at)}`,
|
|
7836
|
+
);
|
|
7837
|
+
}
|
|
7838
|
+
|
|
7839
|
+
// ── 会话读取(服务端 diag) ──
|
|
7840
|
+
const diag = d.diag;
|
|
7841
|
+
lines.push("");
|
|
7842
|
+
lines.push("── 会话读取 ──");
|
|
7843
|
+
if (diag != null && typeof diag === "object") {
|
|
7844
|
+
lines.push(
|
|
7845
|
+
`sessionsUsable=${diag.sessionsUsable === true} sessionLoading=${diag.sessionLoading === true} ` +
|
|
7846
|
+
`sessionUndefined=${diag.sessionUndefined === true} hasGet=${diag.hasGet === true}`,
|
|
7847
|
+
);
|
|
7848
|
+
if (diag.persistedError) lines.push(`persistedError=${diag.persistedError}`);
|
|
7849
|
+
if (d.loading === true) lines.push("loading=true(DSH 正在装载本会话,插件主动让路)");
|
|
7850
|
+
} else {
|
|
7851
|
+
lines.push("—(这次请求没带会话参数,服务端没算这一段)");
|
|
7852
|
+
}
|
|
7853
|
+
|
|
7854
|
+
// ── 原始响应:没被上面提到的东西全在这里,出问题时能对到具体字段 ──
|
|
7855
|
+
lines.push("");
|
|
7856
|
+
lines.push("── 原始响应(最多 6000 字)──");
|
|
7857
|
+
let raw = "";
|
|
7858
|
+
try {
|
|
7859
|
+
raw = JSON.stringify(d);
|
|
7860
|
+
} catch {
|
|
7861
|
+
raw = "[序列化失败]";
|
|
7862
|
+
}
|
|
7863
|
+
lines.push(raw.length > 6000 ? raw.slice(0, 6000) + " …(已截断,共 " + raw.length + " 字)" : raw);
|
|
7864
|
+
return lines.join("\n");
|
|
7865
|
+
}
|
|
7866
|
+
|
|
7328
7867
|
function BalanceSettingsCard(props) {
|
|
7329
7868
|
const view = props && props.view;
|
|
7330
7869
|
const settings = useSettings();
|
|
@@ -7339,6 +7878,10 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
7339
7878
|
// installed — 覆盖完成,等重启
|
|
7340
7879
|
// error — 查询或更新失败
|
|
7341
7880
|
const [updateCheck, setUpdateCheck] = react.useState({ kind: "idle" });
|
|
7881
|
+
// 数据诊断(设置页 → 「数据诊断」→ 点一下出表):
|
|
7882
|
+
// idle/loading/ok/error 四态 + 一份已经拼好的纯文本。只读,不写任何设置。
|
|
7883
|
+
const [diagReport, setDiagReport] = react.useState({ kind: "idle", text: "" });
|
|
7884
|
+
const [diagCopied, setDiagCopied] = react.useState(false);
|
|
7342
7885
|
|
|
7343
7886
|
// 拿到设置后灌一次草稿;draft 已有内容就不再覆盖,避免打断正在输入的用户。
|
|
7344
7887
|
// ⚠️ 必须等 `loaded === true` 再灌(2026-10-31 审查):
|
|
@@ -7497,6 +8040,48 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
7497
8040
|
});
|
|
7498
8041
|
};
|
|
7499
8042
|
|
|
8043
|
+
// 数据诊断:打一次 `?diag=1`,把关键字段拼成纯文本摆出来。
|
|
8044
|
+
// 刻意每次都重新请求(不缓存)—— 用户点它的时机就是「刚刚好像不对」,
|
|
8045
|
+
// 拿缓存等于拿现场之前的数据,反而误导。
|
|
8046
|
+
const onRunDiag = () => {
|
|
8047
|
+
if (diagReport.kind === "loading") return;
|
|
8048
|
+
setDiagCopied(false);
|
|
8049
|
+
setDiagReport({ kind: "loading", text: "" });
|
|
8050
|
+
void (async () => {
|
|
8051
|
+
try {
|
|
8052
|
+
// 必须带 today=1:不然响应里 today 恒为 null(服务端只在要今日或带会话时才去算),
|
|
8053
|
+
// 诊断报告就成了「什么都没查到」。带 today 会触发一次今日计算,点一下才跑一次,可接受。
|
|
8054
|
+
const response = await fetch("/balance-api?diag=1&today=1", { headers: { accept: "application/json" } });
|
|
8055
|
+
if (!response.ok) throw new Error("HTTP " + response.status);
|
|
8056
|
+
const data = await response.json();
|
|
8057
|
+
setDiagReport({ kind: "ok", text: formatDiagnostics(data, settingsCache.version, settingsCache.value) });
|
|
8058
|
+
} catch (error) {
|
|
8059
|
+
setDiagReport({
|
|
8060
|
+
kind: "error",
|
|
8061
|
+
text: "诊断请求失败:" + (error instanceof Error ? error.message : String(error)),
|
|
8062
|
+
});
|
|
8063
|
+
}
|
|
8064
|
+
})();
|
|
8065
|
+
};
|
|
8066
|
+
const onCopyDiag = () => {
|
|
8067
|
+
const text = diagReport.text;
|
|
8068
|
+
if (!text) return;
|
|
8069
|
+
const done = () => {
|
|
8070
|
+
setDiagCopied(true);
|
|
8071
|
+
// 提示两秒后自己收回,不占地方
|
|
8072
|
+
setTimeout(() => setDiagCopied(false), 2000);
|
|
8073
|
+
};
|
|
8074
|
+
// navigator.clipboard 在非 secure context 下可能不存在;失败就让用户手动选中。
|
|
8075
|
+
try {
|
|
8076
|
+
if (navigator?.clipboard?.writeText) {
|
|
8077
|
+
void navigator.clipboard.writeText(text).then(done).catch(() => {});
|
|
8078
|
+
return;
|
|
8079
|
+
}
|
|
8080
|
+
} catch {
|
|
8081
|
+
// 落到下面的手动提示
|
|
8082
|
+
}
|
|
8083
|
+
};
|
|
8084
|
+
|
|
7500
8085
|
// 更新入口(用户要求:「检查到新版本时不要直接下载,要提醒用户是否下载」
|
|
7501
8086
|
// + 「不是弹窗,是在检查更新那个位置变成『检测到新版本,是否更新』(更新)」)。
|
|
7502
8087
|
// 所以只有一层:查到新版后同一块区域直接问「是否更新?」,旁边的按钮就叫「更新」,
|
|
@@ -8139,6 +8724,51 @@ function UsageCostLine({ useProjection, sessionId }) {
|
|
|
8139
8724
|
numberRow("peakMultiplier", "高峰时段倍率", "官方为 2(高峰价 = 空闲价 × 倍率)。", 1, 10, "倍"),
|
|
8140
8725
|
]),
|
|
8141
8726
|
|
|
8727
|
+
// 数据诊断排在最后:它是排障入口,不是日常设置,
|
|
8728
|
+
// 出问题时往下翻一屏就能找到,平时不占视线。只读,不写任何设置。
|
|
8729
|
+
group("diag", "数据诊断", [
|
|
8730
|
+
row(
|
|
8731
|
+
"diag",
|
|
8732
|
+
"生成诊断报告",
|
|
8733
|
+
// 2026-11-09 用户:「把这段指令放在诊断按钮的说明里面」—— 命令和按钮同源,
|
|
8734
|
+
// 说明里直接把指令写出来,用户看到这一行就知道会话里也能调。
|
|
8735
|
+
"把余额、今日消耗的可信度、会话读取状态、浮窗定位原样列成一张表;数字不对劲时点一下,复制出来就能直接发给别人看。只读取,不改任何设置。会话里也可以直接发 /balance-dsh-diag,那条会带上本会话明细。",
|
|
8736
|
+
jsx.jsxs("div", {
|
|
8737
|
+
className: "balance-settings__diag-btns",
|
|
8738
|
+
children: [
|
|
8739
|
+
jsx.jsx("button", {
|
|
8740
|
+
type: "button",
|
|
8741
|
+
className: "balance-settings__btn",
|
|
8742
|
+
onClick: onRunDiag,
|
|
8743
|
+
disabled: diagReport.kind === "loading",
|
|
8744
|
+
children:
|
|
8745
|
+
diagReport.kind === "loading"
|
|
8746
|
+
? "读取中…"
|
|
8747
|
+
: diagReport.kind === "idle"
|
|
8748
|
+
? "数据诊断"
|
|
8749
|
+
: "重新诊断",
|
|
8750
|
+
}),
|
|
8751
|
+
diagReport.text
|
|
8752
|
+
? jsx.jsx("button", {
|
|
8753
|
+
type: "button",
|
|
8754
|
+
className: "balance-settings__btn",
|
|
8755
|
+
onClick: onCopyDiag,
|
|
8756
|
+
children: diagCopied ? "已复制" : "复制",
|
|
8757
|
+
})
|
|
8758
|
+
: null,
|
|
8759
|
+
],
|
|
8760
|
+
}),
|
|
8761
|
+
),
|
|
8762
|
+
diagReport.text
|
|
8763
|
+
? jsx.jsx("pre", {
|
|
8764
|
+
className:
|
|
8765
|
+
"balance-settings__diag" +
|
|
8766
|
+
(diagReport.kind === "error" ? " balance-settings__diag--error" : ""),
|
|
8767
|
+
children: diagReport.text,
|
|
8768
|
+
})
|
|
8769
|
+
: null,
|
|
8770
|
+
]),
|
|
8771
|
+
|
|
8142
8772
|
jsx.jsxs("div", {
|
|
8143
8773
|
className: "balance-settings__actions",
|
|
8144
8774
|
children: [
|