zcode-pro 1.6.2 → 1.8.0

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.
@@ -16,7 +16,8 @@ let tickSeq = 0; // 在途请求序号:过期响应丢弃,不覆盖新值
16
16
 
17
17
  // 流式输出速率估算:usage 缺失的模型(部分供应商不回传 token 数)和长调用
18
18
  // 生成期间没有真实数据,按最后一行回答的文本增长估算(约 2 字符/token 折算,
19
- // 标 ≈)。调用完成后有真实 usage 的会话切回真实值,没有的停在最后的估算值
19
+ // 标 ≈)。长调用生成中官方 60 秒窗口值要等调用结束才更新,会长时间不动;
20
+ // 文本在增长时优先用估算,流停止后切回官方真实值
20
21
  const EST_CHARS_PER_TOKEN = 2;
21
22
  let streamSample = null; // 上一次采样 { len, at }
22
23
  let streamEst = null; // 最近一次估算 { value, at }
@@ -46,9 +47,13 @@ let lastRenderedHtml = null; // renderBar 去重:内容没变不重写 DOM
46
47
 
47
48
  // —— 工具 ——
48
49
 
49
- // 选中会话的 id:只认 bg-selected 行(不走 turn-usage 的“侧栏最新一行”回退——
50
- // 那会让新建会话视图顶着别的会话数据);无选中返回空串
50
+ // 选中会话的 id:优先读会话面板自身的 data-session-id(官方属性,跟着对话视图
51
+ // 走——侧栏项目折叠后选中行会从 DOM 卸载,只认行会让整组会话项消失),
52
+ // 再回退 bg-selected 行;都没有返回空串
51
53
  function selectedSessionId() {
54
+ const pane = document.querySelector('[data-session-id]');
55
+ const pid = pane ? pane.getAttribute('data-session-id') : '';
56
+ if (pid && /^(?:sess|session)_/.test(pid)) return pid;
52
57
  // 交给选择器引擎找选中行(比全表扫 className 快,语义相同)
53
58
  const row = document.querySelector('[data-task-item-key].bg-selected');
54
59
  const key = row ? row.getAttribute('data-task-item-key') : '';
@@ -169,12 +174,14 @@ const GLOBAL_GROUP = new Set(['todayTokens', 'totalTokens', 'tasks']);
169
174
 
170
175
  // 各统计项的 lucide 图标(12px 内联,描边风格与官方一致)
171
176
  const SB_ICONS = {
172
- tokRate: '<path d="m12 14 4-4"/><path d="M3.34 19a10 10 0 1 1 17.32 0"/>',
177
+ tokRate: '<path d="m12 11 4-4"/><path d="M3.34 16a10 10 0 1 1 17.32 0"/>',
173
178
  ctx: '<path d="M21.21 15.89A10 10 0 1 1 8 2.83"/><path d="M22 12A10 10 0 0 0 12 2v10z"/>',
174
179
  asks: '<path d="M7.9 20A9 9 0 1 0 4 16.1L2 22Z"/>',
175
180
  today: '<circle cx="12" cy="12" r="4"/><path d="M12 2v2"/><path d="M12 20v2"/><path d="m4.93 4.93 1.41 1.41"/><path d="m17.66 17.66 1.41 1.41"/><path d="M2 12h2"/><path d="M20 12h2"/><path d="m6.34 17.66-1.41 1.41"/><path d="m19.07 4.93-1.41 1.41"/>',
176
181
  total: '<ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M3 5V19A9 3 0 0 0 21 19V5"/><path d="M3 12A9 3 0 0 0 21 12"/>',
177
182
  tasks: '<rect x="3" y="5" width="6" height="6" rx="1"/><path d="m3 17 2 2 4-4"/><path d="M13 6h8"/><path d="M13 12h8"/><path d="M13 18h8"/>',
183
+ inOut: '<path d="m21 16-4 4-4-4"/><path d="M17 20V4"/><path d="m3 8 4-4 4 4"/><path d="M7 4v16"/>',
184
+ hit: '<path d="m12.83 2.18a2 2 0 0 0-1.66 0L2.6 6.08a2 2 0 0 0 0 1.83l8.58 3.91a2 2 0 0 0 1.66 0l8.58-3.9a2 2 0 0 0 0-1.83Z"/><path d="m22 17.65-9.17 4.16a2 2 0 0 1-1.66 0L2 17.65"/><path d="m22 12.65-9.17 4.16a2 2 0 0 1-1.66 0L2 12.65"/>',
178
185
  coins: '<circle cx="8" cy="8" r="6"/><path d="M18.09 10.37A6 6 0 1 1 10.34 18"/><path d="M7 6h1v4"/><path d="m16.71 13.88.7.71-2.82 2.82"/>',
179
186
  };
180
187
  const sbIcon = (name) => `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="zcodepro-sb-icon">${SB_ICONS[name]}</svg>`;
@@ -211,19 +218,26 @@ function itemContent(id, L) {
211
218
  case 'tokRate': {
212
219
  const sid = selectedSessionId();
213
220
  if (!sid) return null; // 无选中会话不显示
214
- // 显示优先级:生成中的实时估算(usage 缺失/长调用期间唯一动得起来的信号)
215
- // > 真实锚定值(该会话数据就是取自它)> 最近一次估算(usage 缺失的会话答完后)
216
- const streaming = streamEst && Date.now() - streamEst.at < 6000;
217
- const r = (g && g.rate60 && statsSid === sid) ? g.rate60 : null;
221
+ // 官方净生成口径:本会话树(含子代理)最近 60 秒窗内主轮请求的
222
+ // Σ输出 ÷ Σ净生成秒数(与 Token 调试/每轮标注同款公式);usage 全缺
223
+ //(供应商不回传 token)才用文本增长估算兜底——估算按 2 字符/token 折算,
224
+ // 实测虚高 2-4 倍,有真实数据时绝不能让它抢显示。
225
+ // 例外:长调用正在生成时官方值要等调用结束才更新,文本还在增长的这
226
+ // 几秒内先用估算,流停止后自动切回官方值
227
+ const s = (lastStats && statsSid === sid) ? lastStats.sessionSummary : null;
228
+ const tps = s && s.tps != null ? s.tps : null;
218
229
  const est = streamEst ? streamEst.value : null;
219
- const useEst = !!streaming || !(r && r.out > 0);
220
- const shown = useEst ? est : r.out;
230
+ const streaming = !!(streamEst && Date.now() - streamEst.at < 5000);
231
+ const useEst = streaming && est != null;
232
+ const shown = useEst ? est : (tps != null ? tps : est);
221
233
  if (shown == null) return null;
222
234
  const zh = isZhLocale();
223
- const estMark = useEst && est != null ? '≈' : '';
224
- const detail = r
225
- ? `${L.sbLabelOut} ${r.out.toLocaleString()} tok/s · ${zh ? '总量' : 'total'} ${r.total.toLocaleString()} tok/s`
226
- : (zh ? '无 usage 数据,按文本增长估算' : 'no usage data, estimated from text growth');
235
+ const estMark = useEst || tps == null ? '≈' : '';
236
+ const detail = useEst
237
+ ? (zh ? '流式生成中,按文本增长估算(约 2 字符/token)' : 'streaming; estimated from text growth (~2 chars/token)')
238
+ : tps != null
239
+ ? `${L.sbLabelOut} ${tps.toLocaleString()} tok/s${zh ? '(官方净生成口径,本会话树)' : ' (net generation, session tree)'}`
240
+ : (zh ? '无 usage 数据,按文本增长估算' : 'no usage data, estimated from text growth');
227
241
  return {
228
242
  html: sbIcon('tokRate') + label(L.sbLabelOut) + `<b>${estMark}${fmtNum(Math.round(shown * 10) / 10)} tok/s</b>`,
229
243
  title: `${L.sbItemTokRate}${zh ? '(本会话):' : ' (this session): '}${detail}`,
@@ -244,10 +258,23 @@ function itemContent(id, L) {
244
258
  return null; // 官方窗口数据不在(视图切换间隙等)时该项隐藏
245
259
  }
246
260
  case 'questions': {
247
- // 官方“对话问题导航”的真实计数(当前会话的问题总数)
248
- const nav = document.querySelector('[data-testid="v4-turn-navigator"]');
249
- const n = nav ? Number(nav.getAttribute('data-item-count')) : 0;
250
- return n > 0 ? { html: sbIcon('asks') + label(L.sbLabelQuestions) + String(n), title: L.sbItemQuestions } : null;
261
+ // 主轮请求数:官方 model_usage 口径(与 Token 调试的“主轮请求”同源)
262
+ const s = (lastStats && statsSid === selectedSessionId()) ? lastStats.sessionSummary : null;
263
+ return s && s.requests > 0 ? { html: sbIcon('asks') + label(L.sbLabelQuestions) + String(s.requests), title: L.sbItemQuestions } : null;
264
+ }
265
+ case 'inOut': {
266
+ const s = (lastStats && statsSid === selectedSessionId()) ? lastStats.sessionSummary : null;
267
+ return s && s.totalIn + s.totalOut > 0 ? {
268
+ html: sbIcon('inOut') + label(L.sbLabelInOut) + `${fmtNum(s.totalIn)}/${fmtNum(s.totalOut)}`,
269
+ title: `${L.sbItemInOut}${isZhLocale() ? ':' : ': '}${s.totalIn.toLocaleString()} / ${s.totalOut.toLocaleString()}`,
270
+ } : null;
271
+ }
272
+ case 'avgHit': {
273
+ const s = (lastStats && statsSid === selectedSessionId()) ? lastStats.sessionSummary : null;
274
+ return s && s.hitRate != null ? {
275
+ html: sbIcon('hit') + label(L.sbLabelHit) + `${(s.hitRate * 100).toFixed(2)}%`,
276
+ title: `${L.sbItemHit}${isZhLocale() ? ':' : ': '}${(s.hitRate * 100).toFixed(2)}%`,
277
+ } : null;
251
278
  }
252
279
  case 'todayTokens': {
253
280
  const n = (official && official.todayTokens != null) ? official.todayTokens : (g ? g.todayTokens : 0);
@@ -287,38 +314,63 @@ function renderBar() {
287
314
  // 新建会话视图(无选中会话)下方是官方的功能按钮区,不能往下压:上下对称
288
315
  // 留 7px;普通会话视图下方是窗口底的官方留白,压进去贴底(同为约 7px)。
289
316
  // 按选中会话判定视图(按钮区与状态条不在同一父容器,兄弟判断不可用)
290
- const hasSession = !!document.querySelector('[data-task-item-key].bg-selected');
317
+ const hasSession = !!selectedSessionId();
291
318
  barEl.style.marginBottom = hasSession ? '-14px' : '0';
292
319
  barEl.style.paddingBottom = hasSession ? '0' : '7px';
293
320
  const L = t();
294
- const parts = [];
295
- let lastGroup = '';
321
+ const entries = [];
296
322
  for (const it of items) {
297
323
  if (!it.on) continue;
298
324
  const c = itemContent(it.id, L);
299
325
  if (!c) continue;
300
- const group = GLOBAL_GROUP.has(it.id) ? 'g' : 's';
301
- // 会话项与全局项之间用竖线分隔(比项间的 · 更强的分界)
302
- if (lastGroup && group !== lastGroup) parts.push('<span class="zcodepro-statusbar-groupsep"></span>');
303
- lastGroup = group;
304
- parts.push(`<span title="${c.title.replace(/"/g, '&quot;')}"${c.tone ? ` data-tone="${c.tone}"` : ''}>${c.html}</span>`);
326
+ entries.push({ id: it.id, group: GLOBAL_GROUP.has(it.id) ? 'g' : 's', ...c });
305
327
  }
306
- // 组内项之间用 ·,跨组竖线两侧不再叠加 ·
328
+ // 会话项与全局项之间用竖线分隔(比项间的 · 更强的分界);组内项之间用 ·,
329
+ // 跨组竖线两侧不再叠加 ·。隐藏的项在拼 html 之前就排除,圆点与竖线随
330
+ // 重建渲染,不会悬空在行首行尾
307
331
  const SEP = '<span class="zcodepro-statusbar-sep">·</span>';
308
332
  const GROUPSEP = '<span class="zcodepro-statusbar-groupsep"></span>';
333
+ const buildHtml = (hidden) => {
334
+ const parts = [];
335
+ let lastGroup = '';
336
+ for (const e of entries) {
337
+ if (hidden.has(e.id)) continue;
338
+ if (lastGroup && e.group !== lastGroup) parts.push(GROUPSEP);
339
+ lastGroup = e.group;
340
+ parts.push(`<span title="${e.title.replace(/"/g, '&quot;')}"${e.tone ? ` data-tone="${e.tone}"` : ''}>${e.html}</span>`);
341
+ }
342
+ return parts.reduce((acc, p, i) => {
343
+ if (i === 0) return p;
344
+ if (p === GROUPSEP || parts[i - 1] === GROUPSEP) return acc + p;
345
+ return acc + SEP + p;
346
+ }, '');
347
+ };
309
348
  // 数值没变就不重写 DOM:每 2 秒 tick 全量重建会在流式渲染期持续触发布局
310
349
  //(CPU 剖析中 renderBar 是我们唯一上榜的函数)
311
- const html = parts.reduce((acc, p, i) => {
312
- if (i === 0) return p;
313
- if (p === GROUPSEP || parts[i - 1] === GROUPSEP) return acc + p;
314
- return acc + SEP + p;
315
- }, '');
316
- barEl.style.display = parts.length ? '' : 'none';
317
- if (html === lastRenderedHtml) return;
318
- lastRenderedHtml = html;
319
- barEl.innerHTML = html;
350
+ // 超宽自适应:单行不折行,放不下就按 HIDE_ORDER 隐藏次要项直到放下;
351
+ // 输出/平均命中/上下文 三项不参与隐藏(必够放)
352
+ const hidden = new Set();
353
+ for (let guard = 0; guard <= HIDE_ORDER.length; guard++) {
354
+ const html = buildHtml(hidden);
355
+ barEl.style.display = html ? '' : 'none';
356
+ if (html !== lastRenderedHtml) {
357
+ lastRenderedHtml = html;
358
+ barEl.innerHTML = html;
359
+ }
360
+ // 子项 flex-shrink:0,子项宽度求和即真实内容宽(scrollWidth 量不出被压缩的)
361
+ const gap = 8;
362
+ let sum = 0;
363
+ for (const c of barEl.children) sum += c.getBoundingClientRect().width + gap;
364
+ if (sum - gap <= host.clientWidth + 2) break;
365
+ const next = HIDE_ORDER.find((id) => !hidden.has(id));
366
+ if (!next) break;
367
+ hidden.add(next);
368
+ }
320
369
  }
321
370
 
371
+ // 超宽时的隐藏顺序(先隐左边的):剩 输出/平均命中/上下文 三项必够放
372
+ const HIDE_ORDER = ['tasks', 'questions', 'totalTokens', 'todayTokens', 'inOut'];
373
+
322
374
  async function tick() {
323
375
  if (!enabled || document.hidden) return;
324
376
  refreshOfficial();