zcode-pro 1.5.0 → 1.6.1

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.
@@ -32,8 +32,9 @@ const attempted = new Set(); // 已处理过(成功或失败)的会话 id,
32
32
  const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
33
33
 
34
34
  function sessIdOf(key) {
35
- // 会话键格式 <workspace_path>:<sess_id>;sess 段固定以 "sess_" 开头(见 session-switch.js)
36
- const i = key.indexOf(':sess_');
35
+ // 会话键格式 <workspace_path>:<task_id>;task_id 有 sess_ 与 session_ 两种
36
+ // 前缀,按最后一个 ':' 切(见 session-switch.js 的 workspaceOf)
37
+ const i = key.lastIndexOf(':');
37
38
  return i > 0 ? key.slice(i + 1) : '';
38
39
  }
39
40
 
@@ -36,8 +36,9 @@ let fetching = null; // 进行中的取表请求(去重并发:批量失
36
36
  const pendingKeys = new Set(); // 已排期复核的会话键
37
37
 
38
38
  function sessIdOf(key) {
39
- // 会话键格式 <workspace_path>:<sess_id>;sess 段固定以 "sess_" 开头(见 session-switch.js)
40
- const i = key.indexOf(':sess_');
39
+ // 会话键格式 <workspace_path>:<task_id>;task_id 有 sess_ 与 session_ 两种
40
+ // 前缀,按最后一个 ':' 切(见 session-switch.js 的 workspaceOf)
41
+ const i = key.lastIndexOf(':');
41
42
  return i > 0 ? key.slice(i + 1) : key;
42
43
  }
43
44
 
@@ -15,6 +15,9 @@
15
15
  // 探查调度:只在“捕获到运行中”后复核(20 秒首查、其后每 60 秒)。不做启动
16
16
  // 普查:应用重启后所有会话本就处于停止状态,无运行可发现;且启动期界面还在
17
17
  // 恢复,探查会打扰(实测首启会出现项目逐个展开又收起的干扰)。
18
+ // alt+r“运行中”列表另有一路权威来源:全局扫描(globalAnyActive)确认的活跃
19
+ // 会话直接并入运行键集合(wsRunningKeys)——折叠期间启动、刷新后仍折叠的
20
+ // 会话行从未在场,上面的 DOM 三层机制都看不到它们。
18
21
  // 开关:helper wsRunningSpin(默认 true)。
19
22
  import { rpc } from '../core.js';
20
23
  import { projectRows, findProjectRow, rowPath, projectRowSelector } from './ws-rows.js';
@@ -191,9 +194,10 @@ export async function authoritativeIdle(ws) {
191
194
 
192
195
 
193
196
  function workspaceOf(key) {
194
- // 会话键格式 <workspace_path>:<sess_id>;sess 段固定以 "sess_" 开头,
195
- // 按它定位分隔符(Windows 盘符路径里也有冒号,不能按第一个 ':' 切)
196
- const i = key.indexOf(':sess_');
197
+ // 会话键格式 <workspace_path>:<task_id>;task_id 有 sess_ 与 session_ 两种
198
+ // 前缀(新版应用为 session_),不能按 ':sess_' 定位分隔符——取最后一个 ':'
199
+ // (Windows 盘符路径的 ':' 在最前,id 段在最后且自身不含 ':')
200
+ const i = key.lastIndexOf(':');
197
201
  return i > 0 ? key.slice(0, i) : key;
198
202
  }
199
203
 
@@ -380,6 +384,10 @@ let anyActiveCache = { at: 0, value: null, busy: false };
380
384
  const sectionActive = { task: false, project: false, pinned: false };
381
385
  // 权威扫描发现的有活跃任务的项目路径集合(全部 root);项目行折叠态判定用
382
386
  let activeProjectPaths = new Set();
387
+ // 权威扫描确认的活跃会话(工作区路径 → 通过终态过滤的会话 id):alt+r 弹窗的
388
+ // 运行中列表以此补 DOM 之外的盲区——折叠期间启动、页面刷新后仍折叠的会话行
389
+ // 从未在场,runningByWs 里没有它们的键,三层 DOM 机制都看不到
390
+ let authActiveByWs = new Map();
383
391
  // 周期复查:缓存到期就无条件重扫(不看当前值)——只在 true 时扫会有两个缺口:
384
392
  // 空闲后新任务启动无人触发(折叠分区头不亮);任务结束但数据库完成状态尚未
385
393
  // 写入的过渡窗口里旧值一直被沿用。每 10 秒全量一次,扫描本身是内存级过滤
@@ -394,6 +402,7 @@ function globalAnyActive() {
394
402
  sectionActive.project = false;
395
403
  sectionActive.pinned = false;
396
404
  activeProjectPaths = new Set();
405
+ authActiveByWs = new Map();
397
406
  void (async () => {
398
407
  let hit = null;
399
408
  try {
@@ -452,12 +461,28 @@ function globalAnyActive() {
452
461
  // 不回滚(实测本会话 interrupted 后复跑)——终态但 updated_at 近 5 分钟内
453
462
  // 有动静的按真活跃;长期不动的才是 syncer 卡滞误报
454
463
  const TERMINAL = new Set(['completed', 'error', 'interrupted']);
464
+ const recOf = (id) => (st && st.ok && st.available ? st.statuses[id] : undefined);
455
465
  const live = (id) => {
456
- const rec = st && st.ok && st.available ? st.statuses[id] : undefined;
466
+ const rec = recOf(id);
457
467
  if (rec === undefined) return true;
458
468
  if (!TERMINAL.has(rec.status)) return true;
459
469
  return Date.now() - rec.updatedAt < 300_000;
460
470
  };
471
+ // 权威活跃会话(按工作区):alt+r 弹窗从这里补折叠项目的真实会话键。
472
+ // 只收“确认是会话”的 id——任务索引认识的、或侧栏行在场的;两者都不
473
+ // 满足的(listActiveTaskIds 混入的非会话标识、索引尚未记录的新会话)
474
+ // 不进弹窗,否则会出现点了切不过去的幽灵行
475
+ const liveSession = (id) => {
476
+ const rec = recOf(id);
477
+ if (rec === undefined) return !!domRowKeyOf(id);
478
+ if (!TERMINAL.has(rec.status)) return true;
479
+ return Date.now() - rec.updatedAt < 300_000;
480
+ };
481
+ authActiveByWs = new Map();
482
+ for (const [base, ids] of byKindByPath) {
483
+ const liveIds = ids.filter(liveSession);
484
+ if (liveIds.length) authActiveByWs.set(base, liveIds);
485
+ }
461
486
  // 活跃的项目路径集合(项目行折叠态判定用):按 root→项目聚合,
462
487
  // 该项目任一 root 上有活跃任务即记入
463
488
  activeProjectPaths = new Set();
@@ -494,7 +519,7 @@ function globalAnyActive() {
494
519
  for (const el of list.querySelectorAll('[data-task-item-key]')) {
495
520
  if (el.closest('section[data-project-id]')) continue;
496
521
  const k = el.getAttribute('data-task-item-key') || '';
497
- const id = k.slice(k.indexOf(':sess_') + 1);
522
+ const id = k.slice(k.lastIndexOf(':') + 1);
498
523
  const base = k.slice(0, k.lastIndexOf(':')).replace(/[\/]+$/, '');
499
524
  const ids = byKindByPath.get(base) || [];
500
525
  if (ids.map(String).includes(id) && ids.some(live)) {
@@ -595,7 +620,8 @@ async function peek(ws, opts) {
595
620
  } else {
596
621
  let set = runningByWs.get(ws);
597
622
  if (!set) { set = new Set(); runningByWs.set(ws, set); }
598
- set.add(ws + ':active'); // 占位键:apply 只看集合大小
623
+ set.add(ws + ':active'); // 占位键:apply 只看集合大小;真实会话键由
624
+ // wsRunningKeys 从权威扫描集合补,占位键不进弹窗
599
625
  schedulePeek(ws, PEEK_REPEAT_MS);
600
626
  }
601
627
  apply();
@@ -676,10 +702,31 @@ export function wsRunningSnapshot() {
676
702
  };
677
703
  }
678
704
 
679
- // 当前所有运行中会话键的集合(会话切换的 alt+r 以此筛“正在运行”的任务)
705
+ // 按会话 id 找在场任务行并返回它的行键:行键的工作区前缀可能带尾斜杠,权威
706
+ // 拼出的键与 DOM 实际键可能不同形——行在场时一律用 DOM 的真实键,弹窗里同
707
+ // 一会话不会出现两行,切换与读标题也按真实键命中(会话 id 全局唯一,后缀
708
+ // 匹配不会撞到别的会话)
709
+ function domRowKeyOf(id) {
710
+ if (!id) return '';
711
+ try {
712
+ const el = document.querySelector(`[data-task-item-key$=":${CSS.escape(id)}"]`);
713
+ return (el && el.getAttribute('data-task-item-key')) || '';
714
+ } catch { return ''; }
715
+ }
716
+
717
+ // 当前所有运行中会话键的集合(会话切换的 alt+r 以此筛“正在运行”的任务):
718
+ // DOM 记录(runningByWs)+ 权威扫描确认的活跃会话两者取并集——折叠项目里
719
+ // 启动、页面刷新后仍折叠的会话行从未在场,只有权威扫描知道它们。
720
+ // peek 的 ":active" 占位键不是真实会话,不进弹窗(图标的连续性由 apply 的
721
+ // 权威路径保证,占位键只维持 runningByWs 非空这一路回退)
680
722
  export function wsRunningKeys() {
681
723
  const keys = new Set();
682
- for (const set of runningByWs.values()) for (const k of set) keys.add(k);
724
+ for (const set of runningByWs.values()) {
725
+ for (const k of set) if (!k.endsWith(':active')) keys.add(k);
726
+ }
727
+ for (const [base, ids] of authActiveByWs) {
728
+ for (const id of ids) keys.add(domRowKeyOf(id) || `${base}:${id}`);
729
+ }
683
730
  return keys;
684
731
  }
685
732
 
@@ -846,6 +893,12 @@ let scanDebounce = 0;
846
893
  export function startWsRunningSpin() {
847
894
  if (installed || typeof document === 'undefined') return;
848
895
  installed = true;
896
+ // 加载中图标用默认灰压深一点点:既比默认灰显眼,又不像主题色那么跳
897
+ //(描边是 currentColor,改 color 即可)
898
+ const st = document.createElement('style');
899
+ st.id = '__zcodepro_ws_style__';
900
+ st.textContent = '.zcodepro-ws-loader{color:color-mix(in srgb, currentColor 85%, black) !important}';
901
+ document.head.append(st);
849
902
  void refreshConfig();
850
903
  pollConfig();
851
904
 
@@ -36,7 +36,19 @@ import { ensureStyle } from './ui.js';
36
36
  const boot = window.__ZCODEPRO__ || {};
37
37
  const sameGeneration = window.__zcodeproBootedToken && boot.token
38
38
  && window.__zcodeproBootedToken === boot.token;
39
- if (!sameGeneration) location.reload();
39
+ if (!sameGeneration) {
40
+ // 换代刷新限次(记在 sessionStorage,跨刷新存活):两个不同 token 的注入器
41
+ // 互相看对方不顺眼时,30 秒内最多刷 2 次,别进无限刷新
42
+ const key = 'zcodeproReloadLog';
43
+ let log = [];
44
+ try { log = JSON.parse(sessionStorage.getItem(key) || '[]'); } catch { /* ignore */ }
45
+ if (!Array.isArray(log)) log = [];
46
+ log = log.filter((ts) => Date.now() - ts < 30000);
47
+ if (log.length < 2) {
48
+ try { sessionStorage.setItem(key, JSON.stringify([...log, Date.now()])); } catch { /* ignore */ }
49
+ location.reload();
50
+ }
51
+ }
40
52
  return;
41
53
  }
42
54
  window.__zcodeproInjected = true;
package/src/inject/ui.js CHANGED
@@ -5,19 +5,40 @@ import { h, t } from './core.js';
5
5
 
6
6
  let toastTimer = null;
7
7
 
8
+ // 提示条:设置弹窗里触发的提示居中叠在弹窗上,其余居中显示在 ZCode 窗体中间。
9
+ // kind 区分成功 / 警告 / 失败 / 信息('ok' 是旧调用点的成功写法,按成功处理);
10
+ // 配色走 ensureStyle 的自有样式,不依赖宿主 Tailwind 生成配色工具类
11
+ const TOAST_KINDS = {
12
+ success: { name: 'success', glyph: '✓', ms: 2600 },
13
+ warning: { name: 'warning', glyph: '!', ms: 3600 },
14
+ error: { name: 'error', glyph: '✕', ms: 5000 },
15
+ info: { name: 'info', glyph: 'ⓘ', ms: 2600 },
16
+ };
17
+
8
18
  export function showToast(text, kind = 'info') {
9
19
  document.getElementById('__zcodepro_toast__')?.remove();
10
- const el = h('div', {
11
- id: '__zcodepro_toast__',
12
- class: 'fixed bottom-6 z-[100] flex max-w-md items-center gap-2 rounded-lg border px-4 py-3 text-ui-sm shadow-lg ' +
13
- (kind === 'error' ? 'border-destructive/40 bg-menu text-foreground' : 'border-border bg-menu text-foreground'),
14
- // left 用内联样式:ZCode 的 Tailwind 未编译 left-6/right-6 工具类(bottom-6 有)
15
- style: 'animation:zcodepro-fade-in .18s ease; left: 1.5rem;',
20
+ clearTimeout(toastTimer);
21
+ const v = TOAST_KINDS[kind === 'ok' ? 'success' : kind] || TOAST_KINDS.info;
22
+ const chip = h('div', {
23
+ class: `zcodepro-toast zcodepro-toast-${v.name}`,
24
+ style: 'animation:zcodepro-fade-in .18s ease;',
16
25
  },
26
+ h('span', { class: 'zcodepro-toast-glyph' }, v.glyph),
17
27
  h('span', { class: 'truncate' }, text));
18
- document.body.append(el);
19
- clearTimeout(toastTimer);
20
- toastTimer = setTimeout(() => el.remove(), kind === 'error' ? 5000 : 2600);
28
+ const wrap = h('div', { id: '__zcodepro_toast__', class: 'pointer-events-none flex items-center justify-center' });
29
+ // 设置弹窗可拖动、内部可滚动:按弹窗当前盒子定位(fixed),
30
+ // 提示始终落在可见弹窗的正中间;不属于弹窗的提示落到窗体正中间
31
+ const host = document.querySelector('[data-zcodepro-settings]');
32
+ if (host) {
33
+ const r = host.getBoundingClientRect();
34
+ wrap.style.cssText = `position:fixed; left:${Math.round(r.left)}px; top:${Math.round(r.top)}px;`
35
+ + ` width:${Math.round(r.width)}px; height:${Math.round(r.height)}px; z-index:100;`;
36
+ } else {
37
+ wrap.style.cssText = 'position:fixed; inset:0; z-index:100;';
38
+ }
39
+ wrap.append(chip);
40
+ document.body.append(wrap);
41
+ toastTimer = setTimeout(() => wrap.remove(), v.ms);
21
42
  }
22
43
 
23
44
  // 复用应用自身 AlertDialog 的真实类名组合(逆向自 app.asar 渲染层源码):
@@ -45,6 +66,10 @@ export function openDialog({ title, description, onMount, onClose, width = 'sm:m
45
66
  const prevActive = document.activeElement;
46
67
  let cleanupDrag = null;
47
68
  const close = () => {
69
+ // 先 blur 弹窗内焦点元素:输入框靠 change 提交,直接 remove 不触发 change,
70
+ // Esc 关弹窗会把没失焦的输入悄悄丢掉
71
+ const active = document.activeElement;
72
+ if (active && overlayEl.contains(active)) { try { active.blur(); } catch { /* ignore */ } }
48
73
  overlayEl.remove();
49
74
  dialogStack = dialogStack.filter((e) => e !== overlayEl);
50
75
  document.removeEventListener('keydown', onKey);
@@ -102,14 +127,40 @@ export function openDialog({ title, description, onMount, onClose, width = 'sm:m
102
127
  titleEl.style.cursor = 'move';
103
128
  titleEl.style.userSelect = 'none';
104
129
  let dragging = false, sx = 0, sy = 0, ox = 0, oy = 0;
105
- // 边界按遮罩实际范围算(Linux 下遮罩顶部让出自绘标题栏),不能按窗口算:
106
- // 否则卡片能被拖进标题栏下面,拖动把手和关闭按钮被标题栏盖住就都点不到了
130
+ // 应用自绘标题栏高度:标题栏带 -webkit-app-region:drag,是合成层区域、
131
+ // 在所有页面内容之上——卡片钻到它下面后拖动把手和关闭按钮就再也点不到。
132
+ // 实测 3.16.2 起宿主样式表不再编译 platform-linux-desktop:top-12,遮罩
133
+ // 让位旧机制失效,因此运行时实测标题栏(结果短缓存,视图切换后能跟上)
134
+ let tbCache = { at: 0, h: 0 };
135
+ const titleBarHeight = () => {
136
+ if (Date.now() - tbCache.at < 2000) return tbCache.h;
137
+ let h = 0;
138
+ try {
139
+ for (const el of document.querySelectorAll('[class*="app-region"], [style*="app-region"], header')) {
140
+ const st = getComputedStyle(el);
141
+ if ((st.webkitAppRegion || st.appRegion) !== 'drag') continue;
142
+ const r = el.getBoundingClientRect();
143
+ if (r.top <= 2 && r.height > 8 && r.height < 120) h = Math.max(h, r.bottom);
144
+ }
145
+ } catch { /* ignore */ }
146
+ tbCache = { at: Date.now(), h };
147
+ return h;
148
+ };
149
+ // 卡片未平移时的矩形(当前矩形扣除平移量)。平移边界按卡片实际边缘不出
150
+ // 窗体算(旧算法按遮罩内居中对称取值:顶部能钻进标题栏、底部也够不到边)
151
+ const baseRect = () => {
152
+ const r = content.getBoundingClientRect();
153
+ return { left: r.left - ox, top: r.top - oy, right: r.right - ox, bottom: r.bottom - oy };
154
+ };
155
+ // 单轴夹取:范围倒挂(卡片比可用空间还宽/高)时取中点,保证两边均可见
156
+ const clampAxis = (v, lo, hi) => (lo > hi ? (lo + hi) / 2 : Math.min(hi, Math.max(lo, v)));
107
157
  const clampPos = (x, y) => {
108
- const ob = overlayEl.getBoundingClientRect();
109
- // 卡片在遮罩内居中,平移量不能超过卡片边缘到遮罩边缘的距离(留 8px 余量)
110
- const mx = Math.max(0, (ob.width - content.offsetWidth) / 2 - 8);
111
- const my = Math.max(0, (ob.height - content.offsetHeight) / 2 - 8);
112
- return [Math.min(mx, Math.max(-mx, x)), Math.min(my, Math.max(-my, y))];
158
+ const M = 8; // 距窗体边缘的留白
159
+ const b = baseRect();
160
+ return [
161
+ clampAxis(x, M - b.left, (window.innerWidth - M) - b.right),
162
+ clampAxis(y, (titleBarHeight() + M) - b.top, (window.innerHeight - M) - b.bottom),
163
+ ];
113
164
  };
114
165
  const applyPos = () => {
115
166
  content.style.transform = (ox || oy) ? `translate(${ox}px, ${oy}px)` : '';
@@ -379,6 +430,23 @@ export function ensureStyle() {
379
430
  if (!root) return;
380
431
  root.append(h('style', { id: '__zcodepro_style__' }, `
381
432
  @keyframes zcodepro-fade-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
433
+ /* 提示条四态(成功/警告/失败/信息):配色用主题变量加字面量兜底,
434
+ 不依赖宿主 Tailwind 生成配色工具类 */
435
+ .zcodepro-toast {
436
+ display: flex; align-items: center; gap: .5rem;
437
+ max-width: 28rem; padding: .75rem 1rem;
438
+ border-radius: .5rem; border: 1px solid var(--color-border, #555);
439
+ background: var(--color-menu, #1f1f1f); color: var(--color-foreground, #eee);
440
+ font-size: .875rem; box-shadow: 0 10px 15px -3px rgb(0 0 0 / .3);
441
+ }
442
+ .zcodepro-toast .zcodepro-toast-glyph { font-weight: 600; }
443
+ .zcodepro-toast-success { border-color: #22c55e; }
444
+ .zcodepro-toast-success .zcodepro-toast-glyph { color: #22c55e; }
445
+ .zcodepro-toast-warning { border-color: #f59e0b; }
446
+ .zcodepro-toast-warning .zcodepro-toast-glyph { color: #f59e0b; }
447
+ .zcodepro-toast-error { border-color: var(--color-destructive, #ef4444); }
448
+ .zcodepro-toast-error .zcodepro-toast-glyph { color: var(--color-destructive, #ef4444); }
449
+ .zcodepro-toast-info .zcodepro-toast-glyph { color: var(--color-foreground-subtle, #999); }
382
450
  /* 弹窗遮罩/面板兜底:不依赖应用 Tailwind 类是否仍存在;主题色走 --color-* 变量,缺失时回退字面量 */
383
451
  .zcodepro-overlay { background-color: rgba(0, 0, 0, 0.6); }
384
452
  .zcodepro-overlay[data-overlay="none"] { background-color: transparent; }
@@ -482,7 +550,7 @@ export function ensureStyle() {
482
550
  }
483
551
  .zcodepro-bg-card:hover { filter: brightness(1.15); }
484
552
  .zcodepro-bg-card[data-state="active"] { border-color: var(--color-primary, #333); }
485
- .zcodepro-bg-card img { width: 100%; height: 100%; object-fit: cover; display: block; }
553
+ .zcodepro-bg-card img, .zcodepro-bg-card video { width: 100%; height: 100%; object-fit: cover; display: block; }
486
554
  .zcodepro-bg-del {
487
555
  position: absolute; top: 4px; right: 4px; width: 20px; height: 20px;
488
556
  border-radius: 6px; border: none; cursor: pointer; font-size: 11px; line-height: 1;