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.
@@ -87,7 +87,9 @@ function sessionScope() {
87
87
  const cur = rows.find((e) => (e.className + '').includes('bg-selected')) || rows[rows.length - 1];
88
88
  const key = cur ? cur.getAttribute('data-task-item-key') : '';
89
89
  if (!key) return null;
90
- const i = key.indexOf(':sess_');
90
+ // task_id 有 sess_ 与 session_ 两种前缀,按最后一个 ':' 切(Windows 盘符的
91
+ // ':' 在最前,id 段在最后且自身不含 ':')
92
+ const i = key.lastIndexOf(':');
91
93
  return i > 0 ? { workspacePath: key.slice(0, i), sessionId: key.slice(i + 1) } : null;
92
94
  }
93
95
 
@@ -169,7 +171,7 @@ async function injectPanels(grid) {
169
171
  const sc = sessionScope();
170
172
  const open = findOpenSidePane();
171
173
  if (!sc || !open) {
172
- showToast(L.ntpNeedSession, 'error');
174
+ showToast(L.ntpNeedSession, 'warning');
173
175
  return;
174
176
  }
175
177
  try {
@@ -187,7 +189,7 @@ async function injectPanels(grid) {
187
189
  });
188
190
  } catch (e) {
189
191
  console.warn('[zcodepro] 打开插件面板失败', e);
190
- showToast(L.ntpNeedSession, 'error');
192
+ showToast(L.ntpNeedSession, 'warning');
191
193
  }
192
194
  });
193
195
  grid.append(card);
@@ -211,7 +211,7 @@ async function injectNewMenuItems(content, menuRow) {
211
211
  wireHighlight(content, copyItem);
212
212
  copyItem.addEventListener('click', () => {
213
213
  try { content.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', code: 'Escape', bubbles: true, cancelable: true })); } catch { /* ignore */ }
214
- void copyToClipboard(primary.path).then((ok) => showToast(ok ? L.pathCopied : L.pathCopyFailed, ok ? undefined : 'error'));
214
+ void copyToClipboard(primary.path).then((ok) => showToast(ok ? L.pathCopied : L.pathCopyFailed, ok ? 'success' : 'error'));
215
215
  });
216
216
  openItem.after(copyItem);
217
217
  }
@@ -123,7 +123,8 @@ export function openRelocateDialog(project) {
123
123
  submitBtn.removeAttribute('disabled');
124
124
  if (res.ok) {
125
125
  close();
126
- showToast(res.indexSynced === false ? L.relocateIndexSkipped : L.relocateSuccess);
126
+ showToast(res.indexSynced === false ? L.relocateIndexSkipped : L.relocateSuccess,
127
+ res.indexSynced === false ? 'warning' : 'success');
127
128
  setTimeout(() => location.reload(), 900);
128
129
  return;
129
130
  }
@@ -1,13 +1,13 @@
1
1
  // 会话快捷切换:alt+z 立即切到上次使用的会话(再按切回);按住 alt 按 x/c 打开
2
2
  // MRU 弹窗前后移动高亮(x 更早 / c 更新,c 到底且还有更多时加载 5 条并选中新
3
3
  // 加载的第一条),alt+e 直接打开同一弹窗(初始显示最近 10 条),alt+r 打开
4
- // “运行中 / 运行完未查看”任务弹窗,松开 alt 切换,Esc / 点弹窗外取消,点行即切。
4
+ // “运行中 / 运行完未查看”任务弹窗,Enter 或点击行切换,Esc / 点弹窗外取消。
5
5
  // MRU(最近使用序)从注入起记录(不回填历史库,上限 50 条),
6
6
  // 与侧栏展示顺序无关;当前会话靠侧栏条目的 bg-selected 类识别(实测标记),
7
7
  // 事件驱动扫描(会话行增删/选中标记移动才触发)+ 低频兜底轮询——
8
8
  // 点击、新建任务自动跳转等任何方式的切换都能捕捉,聊天区流式输出不产生扫描。
9
9
  // 目标条目在折叠项目下时先展开项目行再点击(展开先例见 pinned-expand.js)。
10
- import { h, t, getConfig, DEFAULT_SHORTCUTS } from '../core.js';
10
+ import { h, t, rpc, getConfig, DEFAULT_SHORTCUTS } from '../core.js';
11
11
  import { ensureStyle, showToast } from '../ui.js';
12
12
  import { findProjectRow } from './ws-rows.js';
13
13
  import { wsRunningKeys } from './ws-running.js';
@@ -30,7 +30,6 @@ let popupMode = 'mru'; // 'mru':最近使用(x/c/e);'attention':运
30
30
  let visibleCount = INITIAL_COUNT;
31
31
  let unseen = new Set(); // 运行结束以来未再切入查看的会话键
32
32
  let lastRunKeys = null; // 上次采样的运行键集合(用于识别“运行→结束”的转变)
33
- let movedSinceOpen = false; // 打开弹窗后是否移动过高亮(决定松开 alt 是提交还是保持)
34
33
  // 键位(alt+字母/数字):默认见 core.js,可在设置 → 快捷键修改;
35
34
  // 设置保存时会派发 zcodepro:config-changed,这里刷新缓存
36
35
  let hotkeys = { ...DEFAULT_SHORTCUTS };
@@ -52,9 +51,9 @@ function firstLineOf(item) {
52
51
  }
53
52
 
54
53
  function workspaceOf(key) {
55
- // 键格式 <workspace_path>:<sess_id>;sess 段固定以 "sess_" 开头,按其定位分隔符
56
- // (Windows 盘符路径里也有冒号,不能按第一个 ':' 切)
57
- const i = key.indexOf(':sess_');
54
+ // 键格式 <workspace_path>:<task_id>;task_id 有 sess_ 与 session_ 两种前缀
55
+ // (新版应用为 session_),按最后一个 ':' 切(ws-running.js 同款)
56
+ const i = key.lastIndexOf(':');
58
57
  return i > 0 ? key.slice(0, i) : key;
59
58
  }
60
59
 
@@ -115,7 +114,11 @@ function scheduleScan() {
115
114
 
116
115
  // 切到指定会话:条目不在 DOM(项目折叠)时先展开项目行,等条目出现再点击
117
116
  async function switchTo(key) {
118
- const find = () => document.querySelector(`[data-task-item-key="${CSS.escape(key)}"]`);
117
+ // 权威扫描拼出的键(工作区前缀无尾斜杠)可能与行键(可能带尾斜杠)不同形,
118
+ // 精确匹配落空时按会话 id 后缀兜底(会话 id 全局唯一,不会命中别的会话)
119
+ const id = key.slice(key.lastIndexOf(':') + 1);
120
+ const find = () => document.querySelector(`[data-task-item-key="${CSS.escape(key)}"]`)
121
+ || (id ? document.querySelector(`[data-task-item-key$=":${CSS.escape(id)}"]`) : null);
119
122
  let item = find();
120
123
  if (!item) {
121
124
  const ws = workspaceOf(key);
@@ -134,21 +137,30 @@ async function switchTo(key) {
134
137
  return true;
135
138
  }
136
139
 
137
- // —— 切换弹窗(类 alt+tab):居中列表卡,x/c 移动高亮,松开 alt / 点击提交 ——
140
+ // —— 切换弹窗(类 alt+tab):居中列表卡,x/c 移动高亮,Enter / 点击提交 ——
138
141
 
139
142
  // attention 弹窗的列表:正在运行或运行完未查看的任务。MRU 只记录切入过的
140
143
  // 会话,而“未查看”的任务可能从未切入,不在 MRU 里——这部分(先运行中、
141
- // 再未查看,各按发现序)追加在 MRU 命中项之后
144
+ // 再未查看,各按发现序)追加在 MRU 命中项之后。
145
+ // 同一会话有两种键形(行在场与否决定:DOM 行键可能带尾斜杠、权威拼的键
146
+ // 不带),按会话 id 去重——同一会话只出一行,运行中的键形优先(标签按它
147
+ // 判定才是“运行中”,点击也命中在场行)
142
148
  function attentionList() {
143
149
  const run = wsRunningKeys();
150
+ const idOf = (k) => k.slice(k.lastIndexOf(':') + 1);
151
+ const runKeyById = new Map([...run].map((k) => [idOf(k), k]));
144
152
  const listed = new Set();
145
153
  const head = [];
146
154
  for (const k of mru) {
147
- if (run.has(k) || unseen.has(k)) { head.push(k); listed.add(k); }
155
+ const id = idOf(k);
156
+ if (listed.has(id)) continue;
157
+ const runForm = runKeyById.get(id);
158
+ if (run.has(k) || unseen.has(k) || runForm) { head.push(runForm || k); listed.add(id); }
148
159
  }
149
160
  const tail = [];
150
- for (const k of run) if (!listed.has(k)) { tail.push(k); listed.add(k); }
151
- for (const k of unseen) if (!listed.has(k)) { tail.push(k); listed.add(k); }
161
+ const push = (k) => { const id = idOf(k); if (listed.has(id)) return; listed.add(id); tail.push(k); };
162
+ for (const k of run) push(k);
163
+ for (const k of unseen) push(k);
152
164
  return [...head, ...tail];
153
165
  }
154
166
 
@@ -171,10 +183,27 @@ function closePopup(commit) {
171
183
  function findTitle(key) {
172
184
  const item = document.querySelector(`[data-task-item-key="${CSS.escape(key)}"]`);
173
185
  if (item) titles.set(key, firstLineOf(item));
174
- const id = key.slice(key.indexOf(':sess_') + 1);
186
+ const id = key.slice(key.lastIndexOf(':') + 1);
175
187
  return titles.get(key) || `sess ${id.slice(0, 8)}`;
176
188
  }
177
189
 
190
+ // 折叠项目的会话行不在场,标题缓存里没有:打开 attention 弹窗时按会话 id 批量
191
+ // 查任务索引(helper /task-statuses 附带 title);查不到保持 "sess xxx" 兜底
192
+ async function fetchMissingTitles(keys) {
193
+ const sessIdOf = (k) => k.slice(k.lastIndexOf(':') + 1);
194
+ const missing = keys.filter((k) => {
195
+ const id = sessIdOf(k);
196
+ return id && !titles.get(k) && !document.querySelector(`[data-task-item-key$=":${CSS.escape(id)}"]`);
197
+ });
198
+ if (!missing.length) return;
199
+ const st = await rpc('/task-statuses?ids=' + encodeURIComponent(missing.map(sessIdOf).join(','))).catch(() => null);
200
+ if (!st || !st.ok || !st.available || !st.statuses) return;
201
+ for (const k of missing) {
202
+ const rec = st.statuses[sessIdOf(k)];
203
+ if (rec && rec.title) titles.set(k, String(rec.title));
204
+ }
205
+ }
206
+
178
207
  // 项目显示名:优先自定义别名(与侧栏一致),无别名退回目录名
179
208
  function projLabelOf(key, aliases) {
180
209
  const ws = workspaceOf(key);
@@ -212,14 +241,25 @@ function renderPopup() {
212
241
  }
213
242
  }
214
243
 
215
- async function openPopup(mode = 'mru') {
244
+ // 打开进行中的 Promise:连按(r 后紧接 x/c)等同一次打开,等弹窗真正挂载完
245
+ // 再移动——移动若落在打开流程的 await 窗口里,会被打开时的状态重置冲掉
246
+ let openPopupInflight = null;
247
+
248
+ function openPopup(mode = 'mru') {
216
249
  if (popupEl) return;
250
+ if (openPopupInflight) return openPopupInflight;
251
+ openPopupInflight = openPopupInner(mode).finally(() => { openPopupInflight = null; });
252
+ return openPopupInflight;
253
+ }
254
+
255
+ async function openPopupInner(mode) {
217
256
  popupMode = mode;
218
257
  visibleCount = INITIAL_COUNT;
219
258
  highlight = 0;
220
- movedSinceOpen = false;
221
259
  const cfg = await getConfig().catch(() => null);
222
260
  aliasesCache = (cfg && cfg.aliases && typeof cfg.aliases === 'object') ? cfg.aliases : {};
261
+ // attention 模式的行可能整段来自权威扫描(折叠项目),先补齐它们的标题再渲染
262
+ if (mode === 'attention') await fetchMissingTitles(attentionList()).catch(() => {});
223
263
  ensureStyle();
224
264
  const L = t();
225
265
  popupEl = h('div', {
@@ -247,7 +287,6 @@ function moveUp() {
247
287
  const len = Math.min(activeList().length, visibleCount);
248
288
  if (len < 2) return;
249
289
  highlight = highlight > 0 ? highlight - 1 : len - 1; // 到顶循环回已加载的末尾
250
- movedSinceOpen = true;
251
290
  renderPopup();
252
291
  scrollHighlight();
253
292
  }
@@ -265,7 +304,6 @@ function moveDown() {
265
304
  } else {
266
305
  highlight = 0; // 全部加载完后再到底:循环回顶部
267
306
  }
268
- movedSinceOpen = true;
269
307
  renderPopup();
270
308
  scrollHighlight();
271
309
  }
@@ -288,8 +326,13 @@ export function startSessionSwitch() {
288
326
  if (document.hidden) return;
289
327
  const now = wsRunningKeys();
290
328
  if (lastRunKeys) {
329
+ // 按会话 id 判“从运行集合消失”:同一会话的键形会随行挂载/卸载在
330
+ // “权威拼的键”与“DOM 行键”之间切换,按整键比较会把键形切换误判成
331
+ // 运行结束,生成一行点不动的“未查看”幽灵行
332
+ const idOf = (k) => k.slice(k.lastIndexOf(':') + 1);
333
+ const nowIds = new Set([...now].map(idOf));
291
334
  for (const k of lastRunKeys) {
292
- if (!now.has(k) && k !== current) unseen.add(k);
335
+ if (!nowIds.has(idOf(k)) && k !== current) unseen.add(k);
293
336
  }
294
337
  }
295
338
  lastRunKeys = now;
@@ -310,6 +353,16 @@ export function startSessionSwitch() {
310
353
  closePopup(false);
311
354
  return;
312
355
  }
356
+ // 弹窗开着时松开 alt 后裸按 x/c 也移动高亮:提示文案一直是
357
+ // “x / c 或 ↑ / ↓ 选择”,与方向键同规则(下到底加载 5 条)
358
+ const bareK = e.key.toLowerCase();
359
+ if (popupEl && !e.altKey && !e.ctrlKey && !e.metaKey && !e.isComposing
360
+ && (bareK === hotkeys.switchPrev || bareK === hotkeys.switchNext)) {
361
+ e.preventDefault();
362
+ e.stopImmediatePropagation();
363
+ if (bareK === hotkeys.switchPrev) moveUp(); else moveDown();
364
+ return;
365
+ }
313
366
  // 弹窗开着时方向键 / Enter 也可操作(此时用户往往已松开 alt):
314
367
  // ↑/↓ 与 x/c 同规则(下到底加载 5 条),Enter 提交高亮项
315
368
  if (popupEl && (e.key === 'ArrowUp' || e.key === 'ArrowDown' || e.key === 'Enter')) {
@@ -344,7 +397,7 @@ export function startSessionSwitch() {
344
397
  const target = live
345
398
  ? (mru[0] === current ? mru[1] : mru.find((mk) => mk !== current))
346
399
  : mru[0];
347
- if (!target) { showToast(t().switcherEmpty); return; }
400
+ if (!target) { showToast(t().switcherEmpty, 'warning'); return; }
348
401
  void switchTo(target).then((ok) => { if (!ok) showToast(t().switcherFailed, 'error'); });
349
402
  return;
350
403
  }
@@ -353,31 +406,40 @@ export function startSessionSwitch() {
353
406
  if (e.repeat) return;
354
407
  closePopup(false);
355
408
  if (!attentionList().length) {
356
- showToast(t().switcherAttentionEmpty);
409
+ showToast(t().switcherAttentionEmpty, 'warning');
357
410
  return;
358
411
  }
359
412
  await openPopup('attention');
360
413
  return;
361
414
  }
362
- // x/c/e:最近使用弹窗(无可导航会话时与 z 一致轻提示)
363
- if (mru.length < 2) { showToast(t().switcherEmpty); return; }
364
- // e 只负责打开(初始显示最近 10 条,c 可继续加载更早的)
415
+ // e 只负责打开(初始显示最近 10 条,c 可继续加载更早的);已开着或正在
416
+ // 打开时不动作。只在 mru 为空(连当前会话都没有,如停在新建任务页)才
417
+ // 提示——首次仅开着当前会话时照常打开,列表里就是它这一条
365
418
  if (k === hotkeys.switchRecent) {
366
- if (!e.repeat && !popupEl) await openPopup('mru');
419
+ if (!e.repeat && !popupEl && !openPopupInflight) {
420
+ if (!mru.length) { showToast(t().switcherEmpty, 'warning'); return; }
421
+ await openPopup('mru');
422
+ }
367
423
  return;
368
424
  }
369
- if (!popupEl) await openPopup('mru');
425
+ // x/c:弹窗已开着(含 attention 模式)直接在当前列表移动——attention 的
426
+ // 列表不是 mru,不能用 mru 的条数门槛拦截;正在打开(r 的异步流程没走完,
427
+ // popupEl 还没挂上)就等它开完再移动,否则要么被 mru 门槛拦成“空”,要么
428
+ // 移动被打开流程的状态重置冲掉;没开则以 mru 打开再移动(mru 为空才提示,
429
+ // 单条=当前会话时打开,移动自然无效)
430
+ if (openPopupInflight) {
431
+ await openPopupInflight;
432
+ } else if (!popupEl) {
433
+ if (!mru.length) { showToast(t().switcherEmpty, 'warning'); return; }
434
+ await openPopup('mru');
435
+ }
370
436
  // 键位对调:alt+x 上移一位(更靠上/更近的键管向上),alt+c 下移
371
437
  if (k === hotkeys.switchPrev) moveUp(); else moveDown();
372
438
  })();
373
439
  }, true);
374
440
 
375
- document.addEventListener('keyup', (e) => {
376
- // 松开 alt:移动过高亮则提交(alt+tab 流);从未移动则保持弹窗打开,
377
- // 让用户继续用方向键选择(alt+e / alt+r 打开后松手查看的场景)
378
- if (e.key === 'Alt' && popupEl && movedSinceOpen) closePopup(true);
379
- }, true);
380
-
381
- // 窗口失焦(alt+tab 到别的应用等)视同取消
441
+ // 松开 alt 不做任何事(曾按 alt+tab 语义在松开时提交,但 Electron 的应用
442
+ // 菜单会抢走按松 Alt 的焦点,keyup 时有时无,行为不可靠,已移除);切换只
443
+ // 认 Enter 与点击行。窗口失焦(alt+tab 到别的应用、系统抢焦点)视同取消
382
444
  window.addEventListener('blur', () => closePopup(false));
383
445
  }