zcode-pro 1.6.1 → 1.7.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.
- package/README.en.md +31 -21
- package/README.md +20 -12
- package/dist/inject.js +10431 -8836
- package/package.json +2 -1
- package/src/host/cdp.mjs +13 -3
- package/src/host/helper.mjs +1287 -488
- package/src/host/main.mjs +368 -30
- package/src/host/paths.mjs +17 -9
- package/src/host/pickFolder.mjs +11 -4
- package/src/host/settings.mjs +15 -2
- package/src/host/setup.mjs +54 -15
- package/src/host/skill-stats.mjs +6 -1
- package/src/host/taskIndex.mjs +464 -66
- package/src/host/taskOrder.mjs +128 -57
- package/src/host/version.mjs +15 -7
- package/src/host/wallpaper.mjs +76 -59
- package/src/host/webdav.mjs +66 -10
- package/src/inject/core.js +228 -103
- package/src/inject/features/alias-migration.js +5 -1
- package/src/inject/features/auto-rename.js +166 -0
- package/src/inject/features/file-menu.js +6 -2
- package/src/inject/features/idle-reclaim.js +31 -8
- package/src/inject/features/newtab-panels.js +11 -1
- package/src/inject/features/pinned-collapse.js +11 -3
- package/src/inject/features/plugin-card-retry.js +18 -8
- package/src/inject/features/plugin-updates.js +58 -0
- package/src/inject/features/sandbox-reclaim.js +17 -1
- package/src/inject/features/session-link.js +202 -0
- package/src/inject/features/session-switch.js +67 -30
- package/src/inject/features/settings-dialog.js +323 -124
- package/src/inject/features/status-bar.js +150 -28
- package/src/inject/features/subagent-icon.js +473 -0
- package/src/inject/features/table-copy.js +18 -2
- package/src/inject/features/task-order.js +47 -13
- package/src/inject/features/theme.js +88 -31
- package/src/inject/features/title-gen.js +5 -4
- package/src/inject/features/title-lock.js +13 -8
- package/src/inject/features/turn-usage.js +99 -47
- package/src/inject/features/ws-running.js +28 -13
- package/src/inject/index.js +7 -0
- package/src/inject/provider-fields.js +2 -2
- package/src/inject/ui.js +14 -0
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
// 子代理运行提示:任务行图标位空着(没有错误/加载图标)时挂机器人图标,
|
|
2
|
+
// 折叠的项目行把 folder 换成同款机器人——表示该会话/项目里有子代理在跑。侧栏
|
|
3
|
+
// 只显示会话级的运行状态,后台子代理跑着的时候会话行一片安静,看不出还在执行。
|
|
4
|
+
// 后台跑的是终端命令(不是子代理)时挂终端图标;两者同时在跑优先机器人。
|
|
5
|
+
// 未读圆点不跟"还在跑"抢位:圆点让位到行尾常显(归档/自动重命名等悬浮操作区
|
|
6
|
+
// 之前),行首留给机器人(回合空闲但子代理在跑)或转圈(回合运行中)。应用自身
|
|
7
|
+
// 的图标优先级是 错误 > 未读 > 加载(fat()),未读会把运行中的转圈盖成圆点——
|
|
8
|
+
// 这里在"未读盖住运行中"时补回转圈(克隆官方 loader),保证运行中恒为转圈。
|
|
9
|
+
// 后台任务清单来自 helper 的 /background-bash(进程握着的任务日志文件反查会话,
|
|
10
|
+
// 纯文件系统扫描)。不能用会话快照(getTaskSnapshot):出快照会把会话 resume
|
|
11
|
+
// 起来,逐会话轮询快照曾造成整夜 6600 次/小时 resume、会话内存暴涨。
|
|
12
|
+
// 信号:子代理子会话写入 CLI 会话库的活动时间(/subagent-children,含嵌套子代理,
|
|
13
|
+
// 由 host 查库)。不用应用 zcodeAgentService 的运行列表判活——它两头都不可靠:
|
|
14
|
+
// 长跑子代理跑着跑着会被误记为失败/结束(实测 48 分钟、60 分钟的都中过招),
|
|
15
|
+
// 崩溃/中断残留的又永远挂"运行中"(实测残留数月)。子代理运行时消息不断写入
|
|
16
|
+
// 会话库,超过 60 分钟无动静视为僵死不亮(单个长工具调用可静默 20 分钟上下,
|
|
17
|
+
// 取 60 分钟留足余量);应用的"结束"记录不早于最后活动时可信,用来让刚结束的
|
|
18
|
+
// 图标立刻熄灭,被误记结束但还在写的照常亮。
|
|
19
|
+
// 折叠项目的会话清单从任务索引反查(/workspace-sessions,最近更新的 30 个、
|
|
20
|
+
// 7 天内有动静);任务行在场时直接用行键里的会话 id。项目行的根路径优先按
|
|
21
|
+
// data-project-id 从 /projects-v2 反查(多目录项目全部 root 都算;页面重载后
|
|
22
|
+
// 行键缓存为空也能拿到),拿不到再退回行键里记住的那条路径。
|
|
23
|
+
// 查询卡死防护:单个工作区的进程被回收后重启时,服务调用可能挂住很久——
|
|
24
|
+
// 每个调用都加超时,tick 整体另有兜底解锁,一个卡住的查询不再堵死后续刷新
|
|
25
|
+
//(曾致图标整段不亮)。
|
|
26
|
+
// ws-running 的旋转 loader 优先:项目行在转就是会话级的运行,机器人不抢位。
|
|
27
|
+
// 开关:helper subagentIcon(默认 true)。
|
|
28
|
+
import { rpc, getConfig, t } from '../core.js';
|
|
29
|
+
import { findServices, iconHolderOf, wsHead } from './ws-running.js';
|
|
30
|
+
import { projectRows, rowPath } from './ws-rows.js';
|
|
31
|
+
|
|
32
|
+
let installed = false;
|
|
33
|
+
let enabled = true;
|
|
34
|
+
|
|
35
|
+
const POLL_MS = 20_000; // 全量刷新周期
|
|
36
|
+
const ACTIVITY_FRESH_MS = 60 * 60_000; // 子会话无活动超过此时长视为僵死
|
|
37
|
+
const WS_CANDIDATES_TTL_MS = 5 * 60_000; // 折叠项目会话清单 / 项目根路径缓存
|
|
38
|
+
const APPLY_DEBOUNCE_MS = 300; // DOM 变动后按已知状态补上图标的去抖
|
|
39
|
+
const CALL_TIMEOUT_MS = 6_000; // 单个服务/rpc 调用超时
|
|
40
|
+
const TICK_GUARD_MS = 30_000; // tick 兜底解锁:超过此时长强制放行下一轮
|
|
41
|
+
|
|
42
|
+
const TASK_SEL = '[data-task-item-key]';
|
|
43
|
+
const ROBOT_CLASS = 'zcodepro-subagent-icon';
|
|
44
|
+
const TERMINAL_CLASS = 'zcodepro-terminal-icon';
|
|
45
|
+
const ROBOT_ON_CLASS = 'zcodepro-subagent-on';
|
|
46
|
+
const UNREAD_MOVED_CLASS = 'zcodepro-subagent-unread-moved';
|
|
47
|
+
const UNREAD_TAIL_CLASS = 'zcodepro-subagent-unread-tail';
|
|
48
|
+
const UNREAD_IGNORE_CLASS = 'zcodepro-unread-error-only';
|
|
49
|
+
const BUSY_LOADER_CLASS = 'zcodepro-busy-loader';
|
|
50
|
+
|
|
51
|
+
// —— 图标 ——
|
|
52
|
+
|
|
53
|
+
// lucide bot(取自应用自带图标集);优先克隆页面上官方 bot 图标(形状/粗细
|
|
54
|
+
// 自动跟随应用版本),找不到再用内置图形
|
|
55
|
+
const BOT_FALLBACK = [
|
|
56
|
+
['path', { d: 'M12 8V4H8' }],
|
|
57
|
+
['rect', { width: '16', height: '12', x: '4', y: '8', rx: '2' }],
|
|
58
|
+
['path', { d: 'M2 14h2' }],
|
|
59
|
+
['path', { d: 'M20 14h2' }],
|
|
60
|
+
['path', { d: 'M15 13v2' }],
|
|
61
|
+
['path', { d: 'M9 13v2' }],
|
|
62
|
+
];
|
|
63
|
+
function makeBotSvg(baseClass) {
|
|
64
|
+
const ns = 'http://www.w3.org/2000/svg';
|
|
65
|
+
const proto = document.querySelector(`svg.lucide-bot:not(.${ROBOT_CLASS})`);
|
|
66
|
+
if (proto) {
|
|
67
|
+
const clone = proto.cloneNode(true);
|
|
68
|
+
const orig = proto.getAttribute('class') || '';
|
|
69
|
+
// 保留尺寸/颜色类,其余(动画等)不带
|
|
70
|
+
const keep = orig.split(/\s+/).filter((c) => /^(size-|text-)/.test(c)).join(' ');
|
|
71
|
+
clone.setAttribute('class', ((keep || baseClass || 'size-4') + ' ' + ROBOT_CLASS).trim());
|
|
72
|
+
clone.setAttribute('title', t().subagentRobotTip);
|
|
73
|
+
return clone;
|
|
74
|
+
}
|
|
75
|
+
const svg = document.createElementNS(ns, 'svg');
|
|
76
|
+
svg.setAttribute('viewBox', '0 0 24 24');
|
|
77
|
+
svg.setAttribute('fill', 'none');
|
|
78
|
+
svg.setAttribute('stroke', 'currentColor');
|
|
79
|
+
svg.setAttribute('stroke-width', '2');
|
|
80
|
+
svg.setAttribute('stroke-linecap', 'round');
|
|
81
|
+
svg.setAttribute('stroke-linejoin', 'round');
|
|
82
|
+
svg.setAttribute('class', ((baseClass || 'size-4') + ' ' + ROBOT_CLASS).trim());
|
|
83
|
+
svg.setAttribute('title', t().subagentRobotTip);
|
|
84
|
+
for (const [tag, attrs] of BOT_FALLBACK) {
|
|
85
|
+
const el = document.createElementNS(ns, tag);
|
|
86
|
+
for (const [k, v] of Object.entries(attrs)) el.setAttribute(k, v);
|
|
87
|
+
svg.append(el);
|
|
88
|
+
}
|
|
89
|
+
return svg;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// —— 应用到行 ——
|
|
93
|
+
|
|
94
|
+
// 任务行图标位(li 首个 div 里的 span[aria-hidden],与 ws-running 的 isRunningRow
|
|
95
|
+
// 同一假设):错误/未读/加载图标是应用渲染的,机器人只补空位
|
|
96
|
+
function rowIconSlot(row) {
|
|
97
|
+
return row.firstElementChild && row.firstElementChild.querySelector(':scope > span[aria-hidden]');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// 补挂的转圈(未读把官方转圈盖住时用):克隆页面上官方 loader,找不到用内置
|
|
101
|
+
// lucide loader 多段短辐条图形;样式同官方 size-4,自带 animate-spin
|
|
102
|
+
const LOADER_FALLBACK_PATHS = [
|
|
103
|
+
'M12 2v4', 'm16.2 7.8 2.9-2.9', 'M18 12h4', 'm19.8 16.2-2.9 2.9',
|
|
104
|
+
'M12 18v4', 'm7.8 19.8-2.9-2.9', 'M6 12H2', 'm4.2 7.8 2.9-2.9',
|
|
105
|
+
];
|
|
106
|
+
// 终端图标(lucide terminal):优先克隆页面上官方图标,找不到用内置图形
|
|
107
|
+
const TERMINAL_FALLBACK = [
|
|
108
|
+
['path', { d: 'm4 17 6-6-6-6' }],
|
|
109
|
+
['path', { d: 'M12 19h8' }],
|
|
110
|
+
];
|
|
111
|
+
function makeTerminalIcon() {
|
|
112
|
+
const ns = 'http://www.w3.org/2000/svg';
|
|
113
|
+
const proto = document.querySelector(`svg.lucide-terminal:not(.${TERMINAL_CLASS}), svg.lucide-square-terminal:not(.${TERMINAL_CLASS})`);
|
|
114
|
+
if (proto) {
|
|
115
|
+
const clone = proto.cloneNode(true);
|
|
116
|
+
const orig = proto.getAttribute('class') || '';
|
|
117
|
+
const keep = orig.split(/\s+/).filter((c) => /^(size-|text-)/.test(c)).join(' ');
|
|
118
|
+
clone.setAttribute('class', ((keep || 'size-4 text-foreground-subtle') + ' ' + TERMINAL_CLASS).trim());
|
|
119
|
+
clone.setAttribute('title', t().subagentTermTip);
|
|
120
|
+
return clone;
|
|
121
|
+
}
|
|
122
|
+
const svg = document.createElementNS(ns, 'svg');
|
|
123
|
+
svg.setAttribute('viewBox', '0 0 24 24');
|
|
124
|
+
svg.setAttribute('fill', 'none');
|
|
125
|
+
svg.setAttribute('stroke', 'currentColor');
|
|
126
|
+
svg.setAttribute('stroke-width', '2');
|
|
127
|
+
svg.setAttribute('stroke-linecap', 'round');
|
|
128
|
+
svg.setAttribute('stroke-linejoin', 'round');
|
|
129
|
+
svg.setAttribute('class', `size-4 text-foreground-subtle ${TERMINAL_CLASS}`);
|
|
130
|
+
svg.setAttribute('title', t().subagentTermTip);
|
|
131
|
+
for (const [tag, attrs] of TERMINAL_FALLBACK) {
|
|
132
|
+
const el = document.createElementNS(ns, tag);
|
|
133
|
+
for (const [k, v] of Object.entries(attrs)) el.setAttribute(k, v);
|
|
134
|
+
svg.append(el);
|
|
135
|
+
}
|
|
136
|
+
return svg;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function makeBusyLoader() {
|
|
140
|
+
const ns = 'http://www.w3.org/2000/svg';
|
|
141
|
+
const proto = document.querySelector(`svg.lucide-loader:not(.${BUSY_LOADER_CLASS})`);
|
|
142
|
+
if (proto) {
|
|
143
|
+
const clone = proto.cloneNode(true);
|
|
144
|
+
const orig = proto.getAttribute('class') || '';
|
|
145
|
+
const keep = orig.split(/\s+/).filter((c) => /^(size-|text-)/.test(c)).join(' ');
|
|
146
|
+
clone.setAttribute('class', ((keep || 'size-4 text-foreground-subtle') + ` animate-spin ${BUSY_LOADER_CLASS}`).trim());
|
|
147
|
+
return clone;
|
|
148
|
+
}
|
|
149
|
+
const svg = document.createElementNS(ns, 'svg');
|
|
150
|
+
svg.setAttribute('viewBox', '0 0 24 24');
|
|
151
|
+
svg.setAttribute('fill', 'none');
|
|
152
|
+
svg.setAttribute('stroke', 'currentColor');
|
|
153
|
+
svg.setAttribute('stroke-width', '2');
|
|
154
|
+
svg.setAttribute('stroke-linecap', 'round');
|
|
155
|
+
svg.setAttribute('stroke-linejoin', 'round');
|
|
156
|
+
svg.setAttribute('class', `size-4 text-foreground-subtle animate-spin ${BUSY_LOADER_CLASS}`);
|
|
157
|
+
for (const d of LOADER_FALLBACK_PATHS) {
|
|
158
|
+
const path = document.createElementNS(ns, 'path');
|
|
159
|
+
path.setAttribute('d', d);
|
|
160
|
+
svg.append(path);
|
|
161
|
+
}
|
|
162
|
+
return svg;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function updateRow(row, sub, busy, term) {
|
|
166
|
+
const slot = rowIconSlot(row);
|
|
167
|
+
if (!slot) return;
|
|
168
|
+
const unread = !!slot.querySelector('[data-unread-indicator]');
|
|
169
|
+
// 槽位里除未读圆点外的官方图标(错误点/官方转圈)在位时不干预
|
|
170
|
+
const hasBlocking = [...slot.children].some(
|
|
171
|
+
(c) => !c.classList.contains(ROBOT_CLASS) && !c.classList.contains(BUSY_LOADER_CLASS)
|
|
172
|
+
&& !c.classList.contains(TERMINAL_CLASS) && !c.hasAttribute('data-unread-indicator'));
|
|
173
|
+
// 官方把槽位藏起来的场景(置顶行 hover 显示图钉、移动中高亮)不挂
|
|
174
|
+
const hidden = slot.classList.contains('hidden') || slot.classList.contains('invisible');
|
|
175
|
+
const ok = !hasBlocking && !hidden;
|
|
176
|
+
// 图标优先级:运行中恒转圈 > 子代理机器人 > 后台终端
|
|
177
|
+
const wantLoader = ok && busy && unread;
|
|
178
|
+
const wantRobot = ok && sub && !busy;
|
|
179
|
+
const wantTerm = ok && term && !busy && !wantRobot;
|
|
180
|
+
const curRobot = slot.querySelector(`:scope > svg.${ROBOT_CLASS}`);
|
|
181
|
+
if (wantRobot && !curRobot) slot.append(makeBotSvg('size-4 text-foreground-subtle'));
|
|
182
|
+
else if (!wantRobot && curRobot) curRobot.remove();
|
|
183
|
+
const curTerm = slot.querySelector(`:scope > svg.${TERMINAL_CLASS}`);
|
|
184
|
+
if (wantTerm && !curTerm) slot.append(makeTerminalIcon());
|
|
185
|
+
else if (!wantTerm && curTerm) curTerm.remove();
|
|
186
|
+
const curLoader = slot.querySelector(`:scope > svg.${BUSY_LOADER_CLASS}`);
|
|
187
|
+
if (wantLoader && !curLoader) slot.append(makeBusyLoader());
|
|
188
|
+
else if (!wantLoader && curLoader) curLoader.remove();
|
|
189
|
+
// 蓝点搬家:机器人/终端/补挂转圈在场时原点隐藏(CSS),行尾挂常显复制品
|
|
190
|
+
const moveUnread = (wantRobot || wantLoader || wantTerm) && unread;
|
|
191
|
+
row.classList.toggle(UNREAD_MOVED_CLASS, moveUnread);
|
|
192
|
+
let tail = row.querySelector(`:scope > .${UNREAD_TAIL_CLASS}`);
|
|
193
|
+
if (moveUnread) {
|
|
194
|
+
if (!tail) {
|
|
195
|
+
tail = document.createElement('span');
|
|
196
|
+
tail.className = UNREAD_TAIL_CLASS;
|
|
197
|
+
tail.setAttribute('aria-hidden', 'true');
|
|
198
|
+
const dot = document.createElement('span');
|
|
199
|
+
dot.className = 'size-[7px] rounded-full bg-icon-blue';
|
|
200
|
+
tail.append(dot);
|
|
201
|
+
}
|
|
202
|
+
// 悬浮操作区(归档/自动重命名)是悬停才渲染的,插在它前面(没有就挂行尾),
|
|
203
|
+
// 悬停后操作区追加在点后面,顺序不变
|
|
204
|
+
row.insertBefore(tail, row.querySelector(':scope > [data-task-row-actions]') || null);
|
|
205
|
+
} else if (tail) {
|
|
206
|
+
tail.remove();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// 折叠项目行:folder 换机器人/终端(folder 的隐藏由 CSS 完成,React 重建 folder
|
|
211
|
+
// 也被规则罩住)。ws-running 的 loader 在转时不挂,两个图标不挤一个位置;机器人优先
|
|
212
|
+
function setProjectIcon(row, robotOn, termOn) {
|
|
213
|
+
const icon = iconHolderOf(row);
|
|
214
|
+
if (!icon) return;
|
|
215
|
+
const loaderSpins = icon.classList.contains('zcodepro-ws-running')
|
|
216
|
+
|| !!icon.querySelector(':scope > svg.zcodepro-ws-loader');
|
|
217
|
+
const wantRobot = robotOn && !loaderSpins;
|
|
218
|
+
const wantTerm = termOn && !loaderSpins && !wantRobot;
|
|
219
|
+
icon.classList.toggle(ROBOT_ON_CLASS, wantRobot || wantTerm);
|
|
220
|
+
const curRobot = icon.querySelector(`:scope > svg.${ROBOT_CLASS}`);
|
|
221
|
+
if (wantRobot && !curRobot) icon.append(makeBotSvg());
|
|
222
|
+
else if (!wantRobot && curRobot) curRobot.remove();
|
|
223
|
+
const curTerm = icon.querySelector(`:scope > svg.${TERMINAL_CLASS}`);
|
|
224
|
+
if (wantTerm && !curTerm) icon.append(makeTerminalIcon());
|
|
225
|
+
else if (!wantTerm && curTerm) curTerm.remove();
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// —— 状态维护 ——
|
|
229
|
+
|
|
230
|
+
let ticking = false;
|
|
231
|
+
let liveSessions = new Set(); // 有子代理在跑的会话 id
|
|
232
|
+
let liveByWs = new Set(); // 同上会话所在工作区(归一后),项目行聚合用
|
|
233
|
+
let busySessions = new Set(); // 回合运行中(应用活跃列表)的会话 id
|
|
234
|
+
let liveTermSessions = new Set(); // 有后台终端命令在跑的会话 id
|
|
235
|
+
let liveTermByWs = new Set(); // 同上会话所在工作区(归一后),项目行聚合用
|
|
236
|
+
let projectTrueUnread = new Map(); // 项目 id → 真未读任务数(未读但排除错误态)
|
|
237
|
+
const wsCandidatesCache = new Map(); // 工作区路径 → { at, ids }
|
|
238
|
+
let projRootsCache = { at: 0, map: null }; // 项目 id → [根路径]
|
|
239
|
+
|
|
240
|
+
function normWs(v) {
|
|
241
|
+
return String(v || '').replace(/[\\/]+$/, '');
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// 超时包装:慢调用按 fallback 放行,迟到的错误不冒泡
|
|
245
|
+
function withTimeout(p, ms, fallback) {
|
|
246
|
+
const guarded = Promise.resolve(p).catch(() => fallback);
|
|
247
|
+
return Promise.race([guarded, new Promise((resolve) => setTimeout(() => resolve(fallback), ms))]);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// 项目 id → 全部根路径(/projects-v2):折叠项目行拿不到行键路径时按 id 反查,
|
|
251
|
+
// 多目录项目每个 root 都查
|
|
252
|
+
async function projRoots() {
|
|
253
|
+
if (projRootsCache.map && Date.now() - projRootsCache.at < WS_CANDIDATES_TTL_MS) return projRootsCache.map;
|
|
254
|
+
const res = await withTimeout(rpc('/projects-v2'), CALL_TIMEOUT_MS, null);
|
|
255
|
+
const map = new Map();
|
|
256
|
+
if (res && res.ok && res.available !== false && Array.isArray(res.projects)) {
|
|
257
|
+
for (const proj of res.projects) {
|
|
258
|
+
const id = String(proj.id || '');
|
|
259
|
+
const paths = (proj.roots || []).map((r) => String(r.path || '')).filter(Boolean);
|
|
260
|
+
if (id && paths.length) map.set(id, paths);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
projRootsCache = { at: Date.now(), map };
|
|
264
|
+
return map;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function projectPathsOf(row, roots) {
|
|
268
|
+
const id = row.getAttribute && row.getAttribute('data-project-id');
|
|
269
|
+
const byId = id && roots && roots.get(id);
|
|
270
|
+
if (byId && byId.length) return byId;
|
|
271
|
+
const p = rowPath(row);
|
|
272
|
+
return p ? [p] : [];
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// 折叠项目的会话清单:行折叠后会话 id 不在 DOM 里,从任务索引反查
|
|
276
|
+
async function wsCandidates(ws) {
|
|
277
|
+
const c = wsCandidatesCache.get(ws);
|
|
278
|
+
if (c && Date.now() - c.at < WS_CANDIDATES_TTL_MS) return c.ids;
|
|
279
|
+
const res = await withTimeout(rpc('/workspace-sessions?path=' + encodeURIComponent(ws)), CALL_TIMEOUT_MS, null);
|
|
280
|
+
const ids = res && res.ok && Array.isArray(res.ids) ? res.ids : [];
|
|
281
|
+
wsCandidatesCache.set(ws, { at: Date.now(), ids });
|
|
282
|
+
return ids;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async function tick() {
|
|
286
|
+
if (!enabled || document.hidden || ticking) return;
|
|
287
|
+
ticking = true;
|
|
288
|
+
// 兜底解锁:卡死的查询不让后续刷新永远排队
|
|
289
|
+
const guard = setTimeout(() => { ticking = false; }, TICK_GUARD_MS);
|
|
290
|
+
try {
|
|
291
|
+
const services = findServices();
|
|
292
|
+
const ag = services && services.zcodeAgentService;
|
|
293
|
+
if (!ag) return;
|
|
294
|
+
// 候选会话:在场任务行(行键 <工作区路径>:<会话 id>)+ 折叠项目的会话清单
|
|
295
|
+
const candidates = new Map(); // 会话 id → 工作区路径
|
|
296
|
+
for (const el of document.querySelectorAll(TASK_SEL)) {
|
|
297
|
+
const key = el.getAttribute('data-task-item-key') || '';
|
|
298
|
+
const cut = key.lastIndexOf(':');
|
|
299
|
+
if (cut > 1) candidates.set(key.slice(cut + 1), key.slice(0, cut));
|
|
300
|
+
}
|
|
301
|
+
const roots = await projRoots();
|
|
302
|
+
const collapsedRows = projectRows().filter((row) => {
|
|
303
|
+
const head = wsHead(row);
|
|
304
|
+
return !!head && head.getAttribute('aria-expanded') === 'false';
|
|
305
|
+
});
|
|
306
|
+
const wsPaths = collapsedRows.flatMap((row) => projectPathsOf(row, roots));
|
|
307
|
+
const wsLists = await Promise.all(wsPaths.map((p) => wsCandidates(p)));
|
|
308
|
+
wsLists.forEach((ids, i) => {
|
|
309
|
+
for (const id of ids) {
|
|
310
|
+
if (!candidates.has(id)) candidates.set(id, wsPaths[i]);
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
// 子代理清单(含嵌套,根归属原会话)与会话库里的最近活动时间:判活的主信号。
|
|
314
|
+
// 应用自己的运行记录两头都不可靠(长跑子代理被误记为结束、崩溃残留的挂
|
|
315
|
+
// "运行中"),只借它的"结束"记录尽快收掉刚结束的图标——结束记录不早于
|
|
316
|
+
// 最后活动才采信,被误记结束但还在写的子代理照常亮(晚亮不早灭)
|
|
317
|
+
const childRes = await withTimeout(
|
|
318
|
+
rpc('/subagent-children?ids=' + encodeURIComponent([...candidates.keys()].join(','))),
|
|
319
|
+
CALL_TIMEOUT_MS, null);
|
|
320
|
+
// 结束记录只对"名下真有子代理"的会话有意义(收刚结束的图标),没子代理的
|
|
321
|
+
// 会话不必逐个查——这是每轮 × 全部会话的主要调用量
|
|
322
|
+
const rootsWithKids = new Set(((childRes && childRes.children) || []).map((c) => c.root));
|
|
323
|
+
const endedLists = await Promise.all([...candidates].filter(([sid]) => rootsWithKids.has(sid)).map(async ([sid, ws]) => {
|
|
324
|
+
const r = await withTimeout(
|
|
325
|
+
ag.listSessionSubagents({ workspacePath: ws, sessionId: sid, endedLimit: 20 }),
|
|
326
|
+
CALL_TIMEOUT_MS, null);
|
|
327
|
+
return (r && r.ended && r.ended.items) || [];
|
|
328
|
+
}));
|
|
329
|
+
const endedAt = new Map(); // 子会话 id → 最新结束时间
|
|
330
|
+
for (const items of endedLists) {
|
|
331
|
+
for (const e of items) {
|
|
332
|
+
const id = e && e.childSessionId;
|
|
333
|
+
if (id) endedAt.set(id, Math.max(endedAt.get(id) || 0, Number(e.endedAt) || 0));
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
const now = Date.now();
|
|
337
|
+
const live = new Set();
|
|
338
|
+
const liveWs = new Set();
|
|
339
|
+
for (const c of (childRes && childRes.children) || []) {
|
|
340
|
+
const at = Number(c.at) || 0;
|
|
341
|
+
if (!(at > 0 && now - at <= ACTIVITY_FRESH_MS)) continue;
|
|
342
|
+
const end = endedAt.get(c.id) || 0;
|
|
343
|
+
if (end && end >= at) continue; // 结束记录不早于最后活动 = 真结束了
|
|
344
|
+
live.add(c.root);
|
|
345
|
+
const ws = candidates.get(c.root);
|
|
346
|
+
if (ws) liveWs.add(normWs(ws));
|
|
347
|
+
}
|
|
348
|
+
liveSessions = live;
|
|
349
|
+
liveByWs = liveWs;
|
|
350
|
+
// 运行中判定(运行中恒转圈用):只查"有子代理或未读圆点"的行所在工作区,
|
|
351
|
+
// 会话在应用的活跃列表里(阶段非终态)就算运行中;两种尾分隔符都查
|
|
352
|
+
const ts = services && services.zcodeTaskService;
|
|
353
|
+
const busy = new Set();
|
|
354
|
+
if (ts && typeof ts.listActiveTaskIds === 'function') {
|
|
355
|
+
const needBusyWs = new Set();
|
|
356
|
+
for (const el of document.querySelectorAll(TASK_SEL)) {
|
|
357
|
+
const key = el.getAttribute('data-task-item-key') || '';
|
|
358
|
+
const cut = key.lastIndexOf(':');
|
|
359
|
+
const sid = cut > 1 ? key.slice(cut + 1) : '';
|
|
360
|
+
const slot = rowIconSlot(el);
|
|
361
|
+
const unread = slot && slot.querySelector('[data-unread-indicator]');
|
|
362
|
+
if (live.has(sid) || unread) needBusyWs.add(normWs(key.slice(0, cut)));
|
|
363
|
+
}
|
|
364
|
+
await Promise.all([...needBusyWs].flatMap((ws) => [ws, ws + '/'].map(async (w) => {
|
|
365
|
+
const r = await withTimeout(ts.listActiveTaskIds({ workspacePath: w }), CALL_TIMEOUT_MS, null);
|
|
366
|
+
if (Array.isArray(r)) for (const id of r) busy.add(String(id));
|
|
367
|
+
})));
|
|
368
|
+
}
|
|
369
|
+
busySessions = busy;
|
|
370
|
+
// 后台任务判定(终端图标):helper 一次文件系统扫描(/background-bash)给出
|
|
371
|
+
// 在跑的后台 bash 任务及其会话,bash 任务在跑才算终端(agent 任务归机器人管)。
|
|
372
|
+
// 这里绝不能用会话快照——出快照会把会话 resume 起来(内存暴涨的元凶)
|
|
373
|
+
const term = new Set();
|
|
374
|
+
const termWs = new Set();
|
|
375
|
+
const bbRes = await withTimeout(rpc('/background-bash'), CALL_TIMEOUT_MS, null);
|
|
376
|
+
for (const j of (bbRes && bbRes.jobs) || []) {
|
|
377
|
+
const sid = j && j.sessionId;
|
|
378
|
+
if (!sid || !candidates.has(sid)) continue;
|
|
379
|
+
term.add(sid);
|
|
380
|
+
const ws = candidates.get(sid);
|
|
381
|
+
if (ws) termWs.add(normWs(ws));
|
|
382
|
+
}
|
|
383
|
+
liveTermSessions = term;
|
|
384
|
+
liveTermByWs = termWs;
|
|
385
|
+
// 项目"真未读"(未读但排除错误态):错误行显示红色错误点,它的未读不该再
|
|
386
|
+
// 把项目行的蓝色未读徽标点亮(要忽略红色圆点);查不到(ok=false)不干预
|
|
387
|
+
const projIds = [...new Set(projectRows()
|
|
388
|
+
.map((r) => r.getAttribute('data-project-id')).filter(Boolean))];
|
|
389
|
+
if (projIds.length) {
|
|
390
|
+
const pu = await withTimeout(
|
|
391
|
+
rpc('/project-unread?ids=' + encodeURIComponent(projIds.join(','))),
|
|
392
|
+
CALL_TIMEOUT_MS, null);
|
|
393
|
+
if (pu && pu.ok && pu.unread) projectTrueUnread = new Map(Object.entries(pu.unread));
|
|
394
|
+
}
|
|
395
|
+
apply();
|
|
396
|
+
} finally {
|
|
397
|
+
clearTimeout(guard);
|
|
398
|
+
ticking = false;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function apply() {
|
|
403
|
+
for (const el of document.querySelectorAll(TASK_SEL)) {
|
|
404
|
+
const key = el.getAttribute('data-task-item-key') || '';
|
|
405
|
+
const cut = key.lastIndexOf(':');
|
|
406
|
+
const sid = cut > 1 ? key.slice(cut + 1) : '';
|
|
407
|
+
updateRow(el, enabled && liveSessions.has(sid), busySessions.has(sid), enabled && liveTermSessions.has(sid));
|
|
408
|
+
}
|
|
409
|
+
const roots = projRootsCache.map;
|
|
410
|
+
for (const row of projectRows()) {
|
|
411
|
+
const head = wsHead(row);
|
|
412
|
+
const collapsed = !!head && head.getAttribute('aria-expanded') === 'false';
|
|
413
|
+
const paths = projectPathsOf(row, roots);
|
|
414
|
+
setProjectIcon(row,
|
|
415
|
+
enabled && collapsed && paths.some((p) => liveByWs.has(normWs(p))),
|
|
416
|
+
enabled && collapsed && paths.some((p) => liveTermByWs.has(normWs(p))));
|
|
417
|
+
// 项目蓝色未读徽标忽略错误行:未读全是错误态(真未读=0)时藏掉徽标
|
|
418
|
+
const pid = row.getAttribute('data-project-id');
|
|
419
|
+
row.classList.toggle(UNREAD_IGNORE_CLASS, enabled && pid !== null && projectTrueUnread.get(pid) === 0);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// —— 配置与入口 ——
|
|
424
|
+
|
|
425
|
+
async function refreshConfig() {
|
|
426
|
+
try {
|
|
427
|
+
const cfg = await getConfig();
|
|
428
|
+
const next = !!(cfg && cfg.features && cfg.features.subagentIcon !== false);
|
|
429
|
+
if (next !== enabled) {
|
|
430
|
+
enabled = next;
|
|
431
|
+
apply(); // 关闭时把已挂的机器人清掉
|
|
432
|
+
}
|
|
433
|
+
} catch { /* helper 未就绪时保持现状 */ }
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
let applyDebounce = 0;
|
|
437
|
+
let tickThrottle = 0;
|
|
438
|
+
|
|
439
|
+
export function startSubagentIcon() {
|
|
440
|
+
if (installed || typeof document === 'undefined') return;
|
|
441
|
+
installed = true;
|
|
442
|
+
void refreshConfig();
|
|
443
|
+
void tick();
|
|
444
|
+
setInterval(() => { void refreshConfig(); void tick(); }, POLL_MS);
|
|
445
|
+
document.addEventListener('visibilitychange', () => { if (!document.hidden) { void refreshConfig(); void tick(); } });
|
|
446
|
+
// 行挂载/React 重建后按已知状态补上图标(不重查,重查留给周期 tick);
|
|
447
|
+
// 行内图标变动(未读圆点出现/消失、转圈切换)时 5 秒节流补一次重查,
|
|
448
|
+
// 把状态切换的滞后从一个刷新周期压到几秒
|
|
449
|
+
const observer = new MutationObserver((muts) => {
|
|
450
|
+
let rowHit = false;
|
|
451
|
+
let rowAdded = false;
|
|
452
|
+
for (const m of muts) {
|
|
453
|
+
if (m.type !== 'childList') continue;
|
|
454
|
+
const t = m.target instanceof Element ? m.target : null;
|
|
455
|
+
const added = [...m.addedNodes].filter((n) => n instanceof Element);
|
|
456
|
+
if (added.some((n) => n.matches(TASK_SEL) || n.querySelector(TASK_SEL))) rowAdded = true;
|
|
457
|
+
const touched = (t && t.closest(TASK_SEL))
|
|
458
|
+
|| added.some((n) => n.matches(TASK_SEL) || n.querySelector(TASK_SEL))
|
|
459
|
+
|| [...m.removedNodes].some((n) => n instanceof Element
|
|
460
|
+
&& (n.matches(TASK_SEL) || n.querySelector(TASK_SEL)));
|
|
461
|
+
if (touched) rowHit = true;
|
|
462
|
+
}
|
|
463
|
+
// 行被 React 重建(点击换行序、重排等)时当场同帧补挂——回调先于绘制,
|
|
464
|
+
// 图标不闪;否则要等去抖窗口,重建行会先露出应用自己的未读圆点
|
|
465
|
+
if (rowAdded) apply();
|
|
466
|
+
if (rowHit && !tickThrottle) {
|
|
467
|
+
tickThrottle = setTimeout(() => { tickThrottle = 0; void tick(); }, 5_000);
|
|
468
|
+
}
|
|
469
|
+
if (applyDebounce) return;
|
|
470
|
+
applyDebounce = setTimeout(() => { applyDebounce = 0; apply(); }, APPLY_DEBOUNCE_MS);
|
|
471
|
+
});
|
|
472
|
+
observer.observe(document.body, { childList: true, subtree: true });
|
|
473
|
+
}
|
|
@@ -138,11 +138,27 @@ export function startTableCopy() {
|
|
|
138
138
|
for (const bar of document.querySelectorAll('[data-markdown-table-toolbar]')) enhance(bar);
|
|
139
139
|
};
|
|
140
140
|
scan();
|
|
141
|
-
// 会话滚动/新回答渲染都会新增表格:body
|
|
141
|
+
// 会话滚动/新回答渲染都会新增表格:body 子树观察器兜底(现有各功能同款模式)。
|
|
142
|
+
// 回调先粗判新增节点里有没有表格工具条,命中才去抖扫描——每个变动批次都
|
|
143
|
+
// 全档 querySelectorAll 在流式渲染期是纯开销(自家插按钮的变动也会带进来)
|
|
142
144
|
void getConfig().then((cfg) => { enabled = cfg.features ? cfg.features.tableCopy !== false : true; }).catch(() => {});
|
|
143
145
|
window.addEventListener('zcodepro:config-changed', () => {
|
|
144
146
|
void getConfig(true).then((cfg) => { enabled = cfg.features ? cfg.features.tableCopy !== false : true; }).catch(() => {});
|
|
145
147
|
});
|
|
146
|
-
|
|
148
|
+
let scanTimer = 0;
|
|
149
|
+
const scheduleScan = () => {
|
|
150
|
+
if (scanTimer) return;
|
|
151
|
+
scanTimer = setTimeout(() => { scanTimer = 0; if (enabled) scan(); }, 300);
|
|
152
|
+
};
|
|
153
|
+
const maybeTable = (muts) => {
|
|
154
|
+
for (const m of muts) {
|
|
155
|
+
for (const n of m.addedNodes) {
|
|
156
|
+
if (!(n instanceof Element)) continue;
|
|
157
|
+
if (n.matches('[data-markdown-table-toolbar]') || n.querySelector('[data-markdown-table-toolbar]')) return true;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return false;
|
|
161
|
+
};
|
|
162
|
+
const observer = new MutationObserver((muts) => { if (enabled && maybeTable(muts)) scheduleScan(); });
|
|
147
163
|
observer.observe(document.body, { childList: true, subtree: true });
|
|
148
164
|
}
|
|
@@ -9,6 +9,29 @@ import { errText } from '../core.js';
|
|
|
9
9
|
|
|
10
10
|
let installed = false;
|
|
11
11
|
let dragKey = null; // 拖动源会话键(dragstart 记录,dragend 清除)
|
|
12
|
+
let lastOverRow = null; // 上一次预览的目标行与方向:都没变就不动 DOM
|
|
13
|
+
let lastOverBefore = false;
|
|
14
|
+
let moveRaf = 0; // 预览移动合并进 rAF:每帧最多动一次 DOM
|
|
15
|
+
let pendingMove = null; // 待执行的预览移动 { row, before }
|
|
16
|
+
|
|
17
|
+
// 落实一次预览移动(查源行 + insertBefore 都放在这里,dragover 只登记不写 DOM)
|
|
18
|
+
function applyMove() {
|
|
19
|
+
const mv = pendingMove;
|
|
20
|
+
pendingMove = null;
|
|
21
|
+
if (!mv || !dragKey) return;
|
|
22
|
+
const list = mv.row.parentElement;
|
|
23
|
+
if (!list) return;
|
|
24
|
+
const src = [...list.querySelectorAll('[data-task-item-key]')]
|
|
25
|
+
.find((el) => el.getAttribute('data-task-item-key') === dragKey);
|
|
26
|
+
if (src && src !== mv.row) {
|
|
27
|
+
mv.row.parentNode.insertBefore(src, mv.before ? mv.row : mv.row.nextSibling);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function scheduleMove(row, before) {
|
|
31
|
+
pendingMove = { row, before };
|
|
32
|
+
if (moveRaf) return;
|
|
33
|
+
moveRaf = requestAnimationFrame(() => { moveRaf = 0; applyMove(); });
|
|
34
|
+
}
|
|
12
35
|
|
|
13
36
|
export function startTaskOrderWatcher() {
|
|
14
37
|
if (installed || typeof document === 'undefined') return;
|
|
@@ -20,7 +43,12 @@ export function startTaskOrderWatcher() {
|
|
|
20
43
|
dragKey = row ? row.getAttribute('data-task-item-key') : null;
|
|
21
44
|
}, true);
|
|
22
45
|
|
|
23
|
-
document.addEventListener('dragend', () => {
|
|
46
|
+
document.addEventListener('dragend', () => {
|
|
47
|
+
dragKey = null;
|
|
48
|
+
lastOverRow = null;
|
|
49
|
+
pendingMove = null;
|
|
50
|
+
if (moveRaf) { cancelAnimationFrame(moveRaf); moveRaf = 0; }
|
|
51
|
+
}, true);
|
|
24
52
|
|
|
25
53
|
// 拖拽经过会话行时允许落下(应用自身未必 preventDefault),并做实时预览
|
|
26
54
|
document.addEventListener('dragover', (e) => {
|
|
@@ -29,18 +57,21 @@ export function startTaskOrderWatcher() {
|
|
|
29
57
|
if (!row || row.getAttribute('data-task-item-key') === dragKey) return;
|
|
30
58
|
e.preventDefault();
|
|
31
59
|
if (e.dataTransfer) e.dataTransfer.dropEffect = 'move';
|
|
32
|
-
// 实时预览:把拖动行移动到目标行前/后(拖动期间 React
|
|
60
|
+
// 实时预览:把拖动行移动到目标行前/后(拖动期间 React 不重渲染;写入数据库后刷新重建)。
|
|
61
|
+
// 方向每次按行的当前几何算(自己挪过 DOM 后行位置会变,缓存会算错),
|
|
62
|
+
// 但目标行与方向都没变就不登记移动——dragover 每秒几十次,逐次查源行并
|
|
63
|
+
// insertBefore 会读写交错抖动;真正移动合并进 rAF 每帧最多一次
|
|
33
64
|
const rect = row.getBoundingClientRect();
|
|
34
65
|
const before = e.clientY < rect.top + rect.height / 2;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
66
|
+
if (row === lastOverRow && before === lastOverBefore) return;
|
|
67
|
+
lastOverRow = row;
|
|
68
|
+
lastOverBefore = before;
|
|
69
|
+
scheduleMove(row, before);
|
|
40
70
|
}, true);
|
|
41
71
|
|
|
42
72
|
document.addEventListener('drop', (e) => {
|
|
43
73
|
if (!dragKey) return;
|
|
74
|
+
applyMove(); // 还没落地的预览移动先做掉,下面读到的顺序才是用户看到的顺序
|
|
44
75
|
const row = e.target.closest && e.target.closest('[data-task-item-key]');
|
|
45
76
|
const list = row ? row.parentElement : null;
|
|
46
77
|
if (!list) return;
|
|
@@ -49,12 +80,18 @@ export function startTaskOrderWatcher() {
|
|
|
49
80
|
e.preventDefault();
|
|
50
81
|
e.stopPropagation();
|
|
51
82
|
dragKey = null;
|
|
52
|
-
|
|
83
|
+
// 列表种类取自落点行的容器:项目分区(section[data-project-id])内是工作区
|
|
84
|
+
// 列表;带其他 section 的是分组/任务分区容器;没有 section 的是"已置顶"列表
|
|
85
|
+
//(可跨工作区)。不要按"键里工作区数量"猜——分组可能全在同一工作区,置顶
|
|
86
|
+
// 也可能跨工作区,两个方向都会猜错
|
|
87
|
+
const hint = row.closest('section[data-project-id]') ? 'project'
|
|
88
|
+
: row.closest('section') ? 'section' : 'pinned';
|
|
89
|
+
void persistOrder(rows.map((el) => el.getAttribute('data-task-item-key')), hint);
|
|
53
90
|
}, true);
|
|
54
91
|
}
|
|
55
92
|
|
|
56
93
|
let persisting = false;
|
|
57
|
-
async function persistOrder(ordered) {
|
|
94
|
+
async function persistOrder(ordered, hint) {
|
|
58
95
|
if (persisting) return;
|
|
59
96
|
persisting = true;
|
|
60
97
|
try {
|
|
@@ -62,10 +99,7 @@ async function persistOrder(ordered) {
|
|
|
62
99
|
if (config.features && config.features.taskOrder === false) return;
|
|
63
100
|
ensureStyle();
|
|
64
101
|
const L = t();
|
|
65
|
-
|
|
66
|
-
const workspaces = new Set(ordered.map((k) => k.slice(0, k.lastIndexOf(':'))));
|
|
67
|
-
const scope = workspaces.size === 1 ? 'workspace' : 'group-members';
|
|
68
|
-
const res = await rpc('/task-order', { method: 'POST', body: { scope, ordered } });
|
|
102
|
+
const res = await rpc('/task-order', { method: 'POST', body: { hint, ordered } });
|
|
69
103
|
if (res && res.ok) {
|
|
70
104
|
showToast(L.taskOrderSaved, 'success');
|
|
71
105
|
setTimeout(() => location.reload(), 600);
|