zcode-pro 1.10.0 → 1.12.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 +12 -5
- package/README.md +12 -5
- package/dist/inject.js +4515 -415
- package/package.json +3 -3
- package/src/host/helper.mjs +3099 -48
- package/src/host/hot-swap.mjs +76 -0
- package/src/host/main.mjs +36 -14
- package/src/host/prompt-inject.mjs +174 -0
- package/src/host/provider-proxy.mjs +562 -0
- package/src/host/proxy-chain.mjs +137 -0
- package/src/host/setup.mjs +42 -17
- package/src/host/taskIndex.mjs +38 -13
- package/src/host/tool-name-check.mjs +164 -0
- package/src/host/translate-lib.mjs +781 -0
- package/src/inject/core.js +453 -17
- package/src/inject/features/auto-continue.js +46 -15
- package/src/inject/features/hr-notice.js +138 -0
- package/src/inject/features/newtab-panels.js +60 -20
- package/src/inject/features/proxy-chain-notice.js +215 -0
- package/src/inject/features/selection-toolbar-translate.js +174 -0
- package/src/inject/features/settings-dialog.js +1072 -86
- package/src/inject/features/status-bar.js +41 -10
- package/src/inject/features/translate-panel.js +883 -0
- package/src/inject/features/turn-usage.js +22 -3
- package/src/inject/index.js +10 -0
- package/src/inject/locales/ja.js +235 -7
- package/src/inject/locales/ko.js +235 -7
- package/src/inject/locales/ru.js +235 -7
- package/src/inject/provider-model-thinking.js +142 -0
- package/src/inject/thinking-levels.js +117 -0
- package/src/inject/ui.js +472 -2
|
@@ -50,10 +50,11 @@ const continueTextFor = (value) => {
|
|
|
50
50
|
const DEFAULTS = {
|
|
51
51
|
enabled: true,
|
|
52
52
|
continueText: '',
|
|
53
|
-
maxRetries:
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
maxRetries: 10,
|
|
54
|
+
continueDelaySec: 120,
|
|
55
|
+
// 连续失败时继续延迟每次乘以这个倍数(1.3 → 2 分钟、2.6 分钟、3.4 分钟…);
|
|
56
|
+
// 1 = 不递增。连续次数按 30 分钟窗口统计,隔久了重新从基础延迟算
|
|
57
|
+
continueDelayFactor: 1.3,
|
|
57
58
|
stuckAfterMin: 30,
|
|
58
59
|
failedAfterMin: 3,
|
|
59
60
|
truncationContinueText: '',
|
|
@@ -70,6 +71,9 @@ const DEFAULTS = {
|
|
|
70
71
|
'access denied', 'permission denied',
|
|
71
72
|
'context[_ -]length', 'prompt (is )?too long', '上下文(超|过|长度)', '输入过长', 'token limit',
|
|
72
73
|
'exceeded the provider context window',
|
|
74
|
+
// 免费层级停用(如 Ling 3.1 Flash tier has been retired):403 但正文只有这句
|
|
75
|
+
// 英文提示,不含状态码,重试必然再失败
|
|
76
|
+
'tier has been retired', 'now a paid model',
|
|
73
77
|
'param incorrect', 'invalid[_ -]?request', 'invalid[_ -]?parameter', '参数(错误|不正确|无效)',
|
|
74
78
|
// 官方会把已知错误本地化后再显示;补上日/韩/俄的同类措辞,避免一直续接永久失败
|
|
75
79
|
'認証(に失敗|エラー|が必要)', 'ログイン.*(期限|失効|必要)', 'サインイン.*(期限|失効|必要)',
|
|
@@ -139,16 +143,12 @@ function trailingBanner() {
|
|
|
139
143
|
}
|
|
140
144
|
|
|
141
145
|
// 自动继续时间戳(页面存活期内):30 分钟窗口计总数(防故障反复时无限继续),
|
|
142
|
-
//
|
|
146
|
+
// 也是继续延迟递增的计数依据
|
|
143
147
|
let continueTimes = [];
|
|
144
148
|
function pruneTimes() {
|
|
145
149
|
const cutoff = Date.now() - 30 * 60 * 1000;
|
|
146
150
|
continueTimes = continueTimes.filter((t) => t >= cutoff);
|
|
147
151
|
}
|
|
148
|
-
function recentWithin(ms) {
|
|
149
|
-
const cutoff = Date.now() - ms;
|
|
150
|
-
return continueTimes.filter((t) => t >= cutoff).length;
|
|
151
|
-
}
|
|
152
152
|
|
|
153
153
|
function sleep(ms) {
|
|
154
154
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
@@ -202,12 +202,13 @@ async function clickSend() {
|
|
|
202
202
|
return false;
|
|
203
203
|
}
|
|
204
204
|
|
|
205
|
-
//
|
|
206
|
-
//
|
|
205
|
+
// 继续前先等 continueDelaySec(默认 2 分钟,让临时故障自己过去);30 分钟窗口内
|
|
206
|
+
// 第 n 次连续失败等待 基础延迟 × 倍数^(n-1)(倍数 1 = 不递增)
|
|
207
207
|
function delayFor(dense, cfg) {
|
|
208
|
-
|
|
209
|
-
const
|
|
210
|
-
|
|
208
|
+
const base = Math.max(0, cfg.continueDelaySec ?? DEFAULTS.continueDelaySec);
|
|
209
|
+
const factor = Math.max(1, cfg.continueDelayFactor ?? DEFAULTS.continueDelayFactor);
|
|
210
|
+
const times = Math.max(1, dense);
|
|
211
|
+
return Math.round(base * Math.pow(factor, times - 1) * 1000);
|
|
211
212
|
}
|
|
212
213
|
|
|
213
214
|
// 回合正常结束但收尾内容坏了(原插件 Stop 钩子角色的 DOM 版):最后一行是长回复且
|
|
@@ -246,6 +247,25 @@ async function tick() {
|
|
|
246
247
|
el.dataset.zcodeproAc = 'skip';
|
|
247
248
|
return;
|
|
248
249
|
}
|
|
250
|
+
// 横幅正文常是通用文案,供应商的真实报错(计费、鉴权)收在“展开详情”
|
|
251
|
+
// 弹窗里,正文里搜不到关键词:拿该会话在日志里记录的失败原因再判一次,
|
|
252
|
+
// 命中永久性错误就不继续(实测:Meta 供应商 Billing verification failed
|
|
253
|
+
// 的横幅只显示通用错误,不补这一道会再撞一次同样的墙)。失败日志先于
|
|
254
|
+
// 横幅落盘,一张横幅查一次就够——巡检 2 秒一轮,不设防会反复整扫日志;
|
|
255
|
+
// 查询失败不置标记,下轮重试
|
|
256
|
+
const sid = activeSessionId();
|
|
257
|
+
if (sid && el.dataset.zcodeproAcLog !== '1') {
|
|
258
|
+
const res = await rpc('/ac-stuck').catch(() => null);
|
|
259
|
+
if (res && res.ok) el.dataset.zcodeproAcLog = '1';
|
|
260
|
+
const f = res && res.ok && Array.isArray(res.failed) ? res.failed.find((x) => x.sessionId === sid) : null;
|
|
261
|
+
if (f && (isPermanent(f.message || '', cfg) || isCancelled(f.message || ''))) {
|
|
262
|
+
const dbgLog = window.__acDebug ||= [];
|
|
263
|
+
dbgLog.push(`${new Date().toISOString().slice(11, 19)} banner skip by log: ${String(f.message).slice(0, 60)}`);
|
|
264
|
+
if (dbgLog.length > 50) dbgLog.splice(0, dbgLog.length - 50);
|
|
265
|
+
el.dataset.zcodeproAc = 'skip';
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
249
269
|
}
|
|
250
270
|
// 有人重新发起回合、或用户已在输入框起草:不打扰
|
|
251
271
|
if (document.querySelector(STOP_SEL)) return;
|
|
@@ -258,7 +278,7 @@ async function tick() {
|
|
|
258
278
|
return;
|
|
259
279
|
}
|
|
260
280
|
el.dataset.zcodeproAc = 'pending';
|
|
261
|
-
await sleep(delayFor(
|
|
281
|
+
await sleep(delayFor(continueTimes.length + 1, cfg));
|
|
262
282
|
// 等待期间状态可能变化(用户手动继续、切走会话、目标行消失),重新校验后再发
|
|
263
283
|
if (!el.isConnected || document.querySelector(STOP_SEL)) {
|
|
264
284
|
el.dataset.zcodeproAc = 'skip';
|
|
@@ -324,6 +344,13 @@ async function rescueStuck(task) {
|
|
|
324
344
|
return r.ok;
|
|
325
345
|
}
|
|
326
346
|
|
|
347
|
+
// 当前打开会话的 id:侧栏选中行的 data-task-item-key 形如
|
|
348
|
+
// “/path/to/project/:session_xxx”,最后一个冒号后面就是会话号
|
|
349
|
+
function activeSessionId() {
|
|
350
|
+
const key = document.querySelector('[data-task-item-key].bg-selected')?.getAttribute('data-task-item-key') || '';
|
|
351
|
+
return key.includes(':') ? key.slice(key.lastIndexOf(':') + 1) : key;
|
|
352
|
+
}
|
|
353
|
+
|
|
327
354
|
// 切到任务所在的会话:侧栏行直接点;虚拟列表没渲染出该行时返回 false(留在当前视图)
|
|
328
355
|
function switchToTask(sessionId) {
|
|
329
356
|
const row = document.querySelector(`[data-testid="task-item-${sessionId}"]`);
|
|
@@ -432,6 +459,10 @@ function showNextStuckCard() {
|
|
|
432
459
|
const more = stuckQueue.length > 1;
|
|
433
460
|
const card = document.createElement('div');
|
|
434
461
|
card.style.cssText = 'position:fixed;right:16px;bottom:16px;z-index:2147483000;max-width:360px;padding:12px;border-radius:12px;border:1px solid var(--color-border, #666);background:var(--color-surface, #222);color:var(--color-foreground, #eee);font-size:13px;line-height:1.5;box-shadow:0 4px 24px rgba(0,0,0,.4)';
|
|
462
|
+
// 与 showCornerCard 共用同一组标记:其他功能(headroom 提示等)据此判断右下角
|
|
463
|
+
// 是否已被占用,避免两张卡片叠在同一角落互相覆盖
|
|
464
|
+
card.className = 'zcodepro-corner-card';
|
|
465
|
+
card.setAttribute('data-zcodepro-corner', 'br');
|
|
435
466
|
const title = document.createElement('div');
|
|
436
467
|
title.style.cssText = 'font-weight:600;margin-bottom:4px';
|
|
437
468
|
title.textContent = (task.failedMsg
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// headroom 供应商登记提示:headroom 插件在会话启动时对账「ZCode 供应商配置 ↔ 本地
|
|
2
|
+
// 压缩路由登记表」,把结果写进数据根的 hr-reconcile-notice.json。这里轮询 helper 拿
|
|
3
|
+
// 最新一条,弹右下角卡片。
|
|
4
|
+
//
|
|
5
|
+
// 为什么轮询而不是直接收推送:helper 的鉴权 token 每次启动随机生成、不落盘、不进
|
|
6
|
+
// 环境变量,外部进程(headroom 钩子)拿不到,没法直接调 helper 接口;写文件 + 页面
|
|
7
|
+
// 轮询是仓库里既有的做法(auto-continue 同款)。
|
|
8
|
+
//
|
|
9
|
+
// 频率约定:一次应用启动最多弹一次。启动时刻记在 localStorage,8 小时内算同一次
|
|
10
|
+
// 启动(页面刷新、注入重载都不重弹),超过就当作新的一次。
|
|
11
|
+
// 对账文件由 headroom 钩子在“会话第一轮”写入,可能晚于页面注入,所以仍需轮询等着;
|
|
12
|
+
// 但弹出后不再重复,避免每开一个新会话就被同一条待办打扰。
|
|
13
|
+
//
|
|
14
|
+
// 开关:features.hrNotice(默认开)。
|
|
15
|
+
import { getConfig, rpc } from '../core.js';
|
|
16
|
+
import { closeCornerCard, showCornerCard } from '../ui.js';
|
|
17
|
+
|
|
18
|
+
const POLL_MS = 15_000;
|
|
19
|
+
// 本次启动是否已经弹过。用 localStorage 存时间戳而不是 sessionStorage 布尔值:
|
|
20
|
+
// 应用可能长期不关,只靠“页面会话”会把同一天里的多次启动合并成一次;
|
|
21
|
+
// 反过来页面刷新(注入重载)不该重弹,所以按时间窗判定,8 小时内算同一次启动
|
|
22
|
+
const LAUNCH_KEY = 'zcodeproHrNoticeShownAt';
|
|
23
|
+
const LAUNCH_WINDOW_MS = 8 * 60 * 60 * 1000;
|
|
24
|
+
// 同一份对账结果只弹一次(跨页面刷新),防止注入重载后重复
|
|
25
|
+
const SEEN_KEY = 'zcodeproHrNoticeSeen';
|
|
26
|
+
// 过期结果不再弹:
|
|
27
|
+
// - 带「已自动修复」的结果是刚发生的事件,10 分钟后就不该再报(可能是几天前的)
|
|
28
|
+
// - 只有「需要处理」的结果代表仍未解决的问题,放宽到 24 小时:
|
|
29
|
+
// 启动时若没有会话恢复(对账文件没被重写)也不至于把待办漏掉
|
|
30
|
+
const MAX_AGE_FIXED_MS = 10 * 60 * 1000;
|
|
31
|
+
const MAX_AGE_PENDING_MS = 24 * 60 * 60 * 1000;
|
|
32
|
+
|
|
33
|
+
function readStore(kind, key) {
|
|
34
|
+
try {
|
|
35
|
+
return String((kind === 'local' ? localStorage : sessionStorage).getItem(key) || '');
|
|
36
|
+
} catch {
|
|
37
|
+
return '';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function writeStore(kind, key, value) {
|
|
42
|
+
try {
|
|
43
|
+
(kind === 'local' ? localStorage : sessionStorage).setItem(key, String(value ?? ''));
|
|
44
|
+
} catch { /* 隐私模式等写不进去:本轮可能重复弹,可接受 */ }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 8 小时内已经弹过就算本次启动已处理
|
|
48
|
+
function alreadyShownThisLaunch() {
|
|
49
|
+
const raw = readStore('local', LAUNCH_KEY);
|
|
50
|
+
if (!raw) return false;
|
|
51
|
+
const at = Number(raw);
|
|
52
|
+
if (!Number.isFinite(at) || at <= 0) return false;
|
|
53
|
+
const age = Date.now() - at;
|
|
54
|
+
return age >= 0 && age < LAUNCH_WINDOW_MS;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// 把对账结果拼成卡片文案;没有问题返回 null
|
|
58
|
+
function cardOf(notice) {
|
|
59
|
+
const fixed = Array.isArray(notice?.fixed) ? notice.fixed : [];
|
|
60
|
+
const pending = Array.isArray(notice?.pending) ? notice.pending : [];
|
|
61
|
+
if (!fixed.length && !pending.length) return null;
|
|
62
|
+
|
|
63
|
+
const parts = [];
|
|
64
|
+
if (fixed.length) {
|
|
65
|
+
// action 是插件对账给出的动作:key = 更新密钥,rename = 登记里的名称跟着 ZCode 里的
|
|
66
|
+
// 改名更新,removed = 供应商在 ZCode 里被删、登记跟着删。以前除了 key 以外都写成
|
|
67
|
+
// “补登记”,删除登记、同步名称也显示成补登记
|
|
68
|
+
const ACTION_LABEL = { key: '更新密钥', rename: '已同步名称', removed: '已删除登记' };
|
|
69
|
+
const items = fixed.map((x) => `${x.name}:${ACTION_LABEL[x.action] || '补登记'}`);
|
|
70
|
+
parts.push(`已自动同步供应商登记:${items.join(';')}`);
|
|
71
|
+
}
|
|
72
|
+
if (pending.length) {
|
|
73
|
+
parts.push(`${pending.map((x) => x.name).join('、')} 指向本地压缩路由但未登记,`
|
|
74
|
+
+ '且无法推断原上游地址。请运行 /hr-join 接入,否则这些供应商的请求会被拒绝。');
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
kind: pending.length ? 'warning' : 'success',
|
|
78
|
+
title: pending.length ? 'headroom 供应商登记需要处理' : 'headroom 供应商登记已同步',
|
|
79
|
+
desc: parts.join('\n\n'),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let shownAt = null;
|
|
84
|
+
let shownThisLaunch = false;
|
|
85
|
+
|
|
86
|
+
// 右下角是否已经有卡片(自动继续、代理链迁移等)。showCornerCard 会先移除同位置的
|
|
87
|
+
// 旧卡片,两个提示叠着会互相覆盖,所以有别的卡片时就跳过本轮,等它关掉后再弹
|
|
88
|
+
function brOccupied() {
|
|
89
|
+
return !!document.querySelector('.zcodepro-corner-card[data-zcodepro-corner="br"]');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function tick() {
|
|
93
|
+
if (shownThisLaunch || alreadyShownThisLaunch()) return;
|
|
94
|
+
const cfg = await getConfig();
|
|
95
|
+
if ((cfg.features || {}).hrNotice === false) return;
|
|
96
|
+
const res = await rpc('/headroom/reconcile-notice');
|
|
97
|
+
if (!res || res.ok === false) return;
|
|
98
|
+
const notice = res.notice;
|
|
99
|
+
if (!notice || !notice.at) return;
|
|
100
|
+
const at = Date.parse(notice.at);
|
|
101
|
+
if (!Number.isFinite(at)) return;
|
|
102
|
+
// 全部都是“已自动修复”才按短窗口丢弃;只要还有待办就按长窗口
|
|
103
|
+
const pendingList = Array.isArray(notice.pending) ? notice.pending : [];
|
|
104
|
+
const age = Date.now() - at;
|
|
105
|
+
if (age > (pendingList.length ? MAX_AGE_PENDING_MS : MAX_AGE_FIXED_MS)) return;
|
|
106
|
+
// 同一份结果只处理一次:本进程内 + 跨刷新
|
|
107
|
+
if (notice.at === shownAt || notice.at === readStore(SEEN_KEY)) return;
|
|
108
|
+
const card = cardOf(notice);
|
|
109
|
+
if (!card) return;
|
|
110
|
+
if (brOccupied()) return; // 让用户先处理已在的卡片,本卡留到下轮
|
|
111
|
+
shownAt = notice.at;
|
|
112
|
+
shownThisLaunch = true;
|
|
113
|
+
writeStore('local', LAUNCH_KEY, String(Date.now()));
|
|
114
|
+
writeStore('session', SEEN_KEY, notice.at);
|
|
115
|
+
showCornerCard({
|
|
116
|
+
position: 'br',
|
|
117
|
+
...card,
|
|
118
|
+
actions: [{ label: '知道了', primary: true, onClick: () => closeCornerCard('br') }],
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function startHrNotice() {
|
|
123
|
+
// 本次启动已经弹过就不再轮询:省掉每 15 秒一次的无用请求
|
|
124
|
+
if (shownThisLaunch || alreadyShownThisLaunch()) return;
|
|
125
|
+
const run = () => {
|
|
126
|
+
// 首轮立即跑,之后按周期:对账文件可能比页面注入晚写出来
|
|
127
|
+
tick().catch(() => { /* 单次失败不影响后续 */ });
|
|
128
|
+
};
|
|
129
|
+
run();
|
|
130
|
+
setInterval(() => {
|
|
131
|
+
if (shownThisLaunch) return; // 已弹过,停掉后续轮询
|
|
132
|
+
if (document.hidden) return; // 后台标签页不轮询,回到前台由 visibilitychange 补一轮
|
|
133
|
+
run();
|
|
134
|
+
}, POLL_MS);
|
|
135
|
+
document.addEventListener('visibilitychange', () => {
|
|
136
|
+
if (!document.hidden && !shownThisLaunch) run();
|
|
137
|
+
});
|
|
138
|
+
}
|
|
@@ -134,27 +134,36 @@ function surfaceTitle(s, locale) {
|
|
|
134
134
|
const PANEL_ICON_PATHS = ['<rect width="7" height="9" x="3" y="3" rx="1"/><rect width="7" height="5" x="14" y="3" rx="1"/><rect width="7" height="9" x="14" y="12" rx="1"/><rect width="7" height="5" x="3" y="16" rx="1"/>'];
|
|
135
135
|
|
|
136
136
|
// 找“新标签页”的模块网格:官方卡片按钮带 data-side-pane-open-tab-item
|
|
137
|
-
// (3.16.1
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const
|
|
144
|
-
|
|
137
|
+
// (3.16.1 实测标识),其父容器即网格;文本特征兜底校验(终端模块描述)。
|
|
138
|
+
// 返回全部网格,不只看第一个:侧边面板每开一个“新标签页”标签就多一份网格,
|
|
139
|
+
// 未激活的标签只是 display:none,DOM 里仍然在,且通常排在活动标签前面。
|
|
140
|
+
// 只取第一个会落在隐藏网格上(offsetParent 为 null)而整轮放弃注入。
|
|
141
|
+
function findNewTabGrids() {
|
|
142
|
+
const grids = new Set();
|
|
143
|
+
for (const item of document.querySelectorAll('[data-side-pane-open-tab-item]')) {
|
|
144
|
+
const grid = item.parentElement;
|
|
145
|
+
if (!grid || grid === document.body) continue;
|
|
146
|
+
const text = grid.textContent || '';
|
|
147
|
+
if (GRID_HINTS.some((h) => text.includes(h))) grids.add(grid);
|
|
148
|
+
}
|
|
149
|
+
return [...grids];
|
|
145
150
|
}
|
|
146
151
|
|
|
147
|
-
|
|
152
|
+
// 正在注入的网格:await listSurfaces 期间挡住第二次进入。去重标记要等列表
|
|
153
|
+
// 回来才写,MutationObserver(防抖 300ms)与 5 秒定时器都能在这个空档里再进
|
|
154
|
+
// 一次,各追加一份卡片,界面上就成对重复了
|
|
155
|
+
const inflight = new WeakSet();
|
|
156
|
+
|
|
157
|
+
// 把已取回的面板清单画进一个网格。清单在轮次内共用:多标签页时有几份网格就
|
|
158
|
+
// 有几个网格要注入,逐网格各查一次 listSurfaces 没必要
|
|
159
|
+
function injectCards(grid, surfaces) {
|
|
148
160
|
if (grid.dataset.zcodeproPanels) return;
|
|
149
161
|
const template = grid.querySelector('[data-side-pane-open-tab-item]');
|
|
150
162
|
if (!template) return;
|
|
151
|
-
const scope = sessionScope();
|
|
152
|
-
if (!scope) return; // 没有任何会话时暂不注入,下次 DOM 变动再试
|
|
153
|
-
const bridge = findBridge();
|
|
154
|
-
if (!bridge) return;
|
|
155
|
-
const surfaces = await thirdPartySurfaces(bridge, scope.workspacePath);
|
|
156
163
|
if (!surfaces.length) { grid.dataset.zcodeproPanels = '1'; return; }
|
|
157
164
|
grid.dataset.zcodeproPanels = '1';
|
|
165
|
+
// 兜底:残留的旧卡片先清掉,保证每次注入只留一份
|
|
166
|
+
for (const old of grid.querySelectorAll('[data-zcodepro-panel]')) old.remove();
|
|
158
167
|
const L = t();
|
|
159
168
|
const locale = uiLocale();
|
|
160
169
|
for (const s of surfaces) {
|
|
@@ -212,15 +221,46 @@ async function injectPanels(grid) {
|
|
|
212
221
|
}
|
|
213
222
|
}
|
|
214
223
|
|
|
224
|
+
// 一轮注入:先按网格过滤出真正要注入的,再只查一次面板清单,最后逐个网格画。
|
|
225
|
+
// 正在注入的网格记进 inflight,避免 await 期间 MutationObserver 与 5 秒定时器
|
|
226
|
+
// 再进一轮、各追加一份卡片
|
|
227
|
+
let roundInflight = false;
|
|
228
|
+
|
|
229
|
+
async function injectAll(grids) {
|
|
230
|
+
const todo = grids.filter((g) => !g.dataset.zcodeproPanels && !inflight.has(g));
|
|
231
|
+
if (!todo.length || roundInflight) return;
|
|
232
|
+
if (!todo.some((g) => g.querySelector('[data-side-pane-open-tab-item]'))) return;
|
|
233
|
+
const scope = sessionScope();
|
|
234
|
+
if (!scope) return; // 没有任何会话时暂不注入,下次 DOM 变动再试
|
|
235
|
+
const bridge = findBridge();
|
|
236
|
+
if (!bridge) return;
|
|
237
|
+
roundInflight = true;
|
|
238
|
+
for (const g of todo) inflight.add(g);
|
|
239
|
+
let surfaces;
|
|
240
|
+
try {
|
|
241
|
+
surfaces = await thirdPartySurfaces(bridge, scope.workspacePath);
|
|
242
|
+
} finally {
|
|
243
|
+
roundInflight = false;
|
|
244
|
+
for (const g of todo) inflight.delete(g);
|
|
245
|
+
}
|
|
246
|
+
for (const g of todo) {
|
|
247
|
+
// 等待期间网格可能已被 React 换掉(脱离文档),或本轮已画过
|
|
248
|
+
if (!g.isConnected) continue;
|
|
249
|
+
injectCards(g, surfaces);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
215
253
|
function tryInject() {
|
|
216
254
|
if (!enabled || document.hidden) return;
|
|
217
|
-
const
|
|
218
|
-
if (!
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
255
|
+
const grids = findNewTabGrids();
|
|
256
|
+
if (!grids.length) return;
|
|
257
|
+
for (const grid of grids) {
|
|
258
|
+
// React 重渲染可能冲掉注入的卡片:标记还在但卡片没了时清标记重注
|
|
259
|
+
if (grid.dataset.zcodeproPanels && !grid.querySelector('[data-zcodepro-panel]')) {
|
|
260
|
+
delete grid.dataset.zcodeproPanels;
|
|
261
|
+
}
|
|
222
262
|
}
|
|
223
|
-
void
|
|
263
|
+
void injectAll(grids);
|
|
224
264
|
}
|
|
225
265
|
|
|
226
266
|
export function startNewtabPanels() {
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// 右下角提示卡片(helper 那边发生的事)。helper 写提示文件,页面轮询 helper 读取、
|
|
2
|
+
// 按写入时间去重、只在首次见到时弹卡片。做法与 hr-notice 一致:helper 的 token
|
|
3
|
+
// 不落盘,外部进程调不了接口,写文件 + 轮询是仓库里既有的做法。
|
|
4
|
+
// 四种提示,由同一个接口一起返回,页面不用为每种各轮询一次:
|
|
5
|
+
// proxy-chain-notice.json
|
|
6
|
+
// auto-unchecked:有供应商因为地址变了(在 ZCode 里手动改的,或从别的设备同步来的)
|
|
7
|
+
// 自动退出了代理链。卡片上可以直接跳到代理链页
|
|
8
|
+
// (没有 kind): 旧的一次性迁移说明(供应商代理改成按代理链展示)
|
|
9
|
+
// proxy-health-notice.json
|
|
10
|
+
// health: 每 30 秒的巡检发现问题(转发器不在监听、走链的 headroom 实例不在
|
|
11
|
+
// 监听)。helper 发现时已经自动处理,卡片说明发现了什么、做了什么
|
|
12
|
+
// title-gen-notice.json
|
|
13
|
+
// title-gen: 新会话自动起名失败(供应商或模型不支持),会话名还是 ZCode 默认的
|
|
14
|
+
// tool-name-notice.json
|
|
15
|
+
// tool-name: 工具名合规检查有文件写不了(ZCode 自带插件在 root 目录下)
|
|
16
|
+
// 右下角同一时间只放一张卡片:按上面的顺序弹第一个没弹过的,其余等下一轮、右下角
|
|
17
|
+
// 空出来再弹。开关:features.proxyChainNotice(默认开)。
|
|
18
|
+
import { getConfig, isCjkLocale, rpc, t } from '../core.js';
|
|
19
|
+
import { openSettingsDialog } from './settings-dialog.js';
|
|
20
|
+
import { closeCornerCard, showCornerCard } from '../ui.js';
|
|
21
|
+
|
|
22
|
+
const POLL_MS = 10_000;
|
|
23
|
+
const MAX_AGE_MS = 10 * 60 * 1000;
|
|
24
|
+
|
|
25
|
+
function storeGet(key) {
|
|
26
|
+
try { return String(sessionStorage.getItem(key) || ''); } catch { return ''; }
|
|
27
|
+
}
|
|
28
|
+
function storeSet(key, at) {
|
|
29
|
+
try { sessionStorage.setItem(key, String(at || '')); } catch { /* 隐私模式写不进去:本轮会重复弹,可接受 */ }
|
|
30
|
+
}
|
|
31
|
+
// 写入时间在 10 分钟内才弹:页面刚打开时不补弹旧提示
|
|
32
|
+
function fresh(atStr) {
|
|
33
|
+
const at = Date.parse(atStr);
|
|
34
|
+
return Number.isFinite(at) && Date.now() - at <= MAX_AGE_MS;
|
|
35
|
+
}
|
|
36
|
+
function cornerBusy() {
|
|
37
|
+
return !!document.querySelector('.zcodepro-corner-card[data-zcodepro-corner="br"]');
|
|
38
|
+
}
|
|
39
|
+
// 端口列表的分隔符:中文用顿号,其他语言用逗号
|
|
40
|
+
const listSep = () => (isCjkLocale() ? '、' : ', ');
|
|
41
|
+
|
|
42
|
+
function showAutoUnchecked(notice, L) {
|
|
43
|
+
const items = (Array.isArray(notice.items) ? notice.items : [])
|
|
44
|
+
.filter((it) => it && typeof it === 'object');
|
|
45
|
+
const lines = items.map((it) => L.noticeAutoUncheckedItem
|
|
46
|
+
.replace('{name}', String(it.name || it.id || ''))
|
|
47
|
+
.replace('{url}', String(it.url || ''))
|
|
48
|
+
.replace('{chain}', String(it.chain || '')));
|
|
49
|
+
showCornerCard({
|
|
50
|
+
position: 'br',
|
|
51
|
+
kind: 'info',
|
|
52
|
+
title: L.noticeAutoUncheckedTitle,
|
|
53
|
+
desc: [L.noticeAutoUncheckedDesc, ...lines, L.noticeAutoUncheckedHow].join('\n\n'),
|
|
54
|
+
actions: [{
|
|
55
|
+
label: L.noticeGoChain,
|
|
56
|
+
primary: true,
|
|
57
|
+
onClick: () => { closeCornerCard('br'); openSettingsDialog('proxy', 'vendor'); },
|
|
58
|
+
}],
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function showMigration(notice) {
|
|
63
|
+
const cleared = Array.isArray(notice.cleared) ? notice.cleared : [];
|
|
64
|
+
const rebuilt = Array.isArray(notice.rebuilt) ? notice.rebuilt : [];
|
|
65
|
+
const parts = ['供应商代理已改为按代理链展示,原有配置已重置。'];
|
|
66
|
+
if (rebuilt.length) parts.push(`已重建:${rebuilt.join('、')}`);
|
|
67
|
+
if (cleared.length) parts.push(`已还原为直连或 headroom:${cleared.join('、')}`);
|
|
68
|
+
parts.push('请到 设置 → 代理 → 供应商代理 重新配置。');
|
|
69
|
+
showCornerCard({
|
|
70
|
+
position: 'br',
|
|
71
|
+
kind: 'warning',
|
|
72
|
+
title: '供应商代理配置已重置',
|
|
73
|
+
desc: parts.join('\n\n'),
|
|
74
|
+
actions: [{ label: '知道了', primary: true, onClick: () => closeCornerCard('br') }],
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function showChainNotice(notice, L) {
|
|
79
|
+
if (notice.kind === 'auto-unchecked') showAutoUnchecked(notice, L);
|
|
80
|
+
else showMigration(notice);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 巡检提示:每条说明一个发现(端口、做了什么、有没有处理成功)。
|
|
84
|
+
// 全是 headroom 实例的问题,按钮就去 Headroom 页;有转发器的问题就去代理链页
|
|
85
|
+
function showHealth(health, L) {
|
|
86
|
+
const items = (Array.isArray(health.items) ? health.items : [])
|
|
87
|
+
.filter((it) => it && typeof it === 'object');
|
|
88
|
+
if (!items.length) return;
|
|
89
|
+
const ports = (list) => (Array.isArray(list) ? list : []).map(String).join(listSep());
|
|
90
|
+
const lines = items.map((it) => {
|
|
91
|
+
if (it.type === 'router' || it.type === 'headroom') {
|
|
92
|
+
const tpl = it.type === 'router' ? L.noticeHealthRouter : L.noticeHealthInstance;
|
|
93
|
+
return tpl.replace('{ports}', ports(it.ports))
|
|
94
|
+
+ (it.failed ? L.noticeHealthInstanceFailed.replace('{error}', String(it.error || '')) : '');
|
|
95
|
+
}
|
|
96
|
+
return L.noticeHealthForwarder.replace('{ports}', ports(it.ports))
|
|
97
|
+
+ (it.stillDown ? L.noticeHealthForwarderDown : '');
|
|
98
|
+
});
|
|
99
|
+
const hasHeadroom = items.some((it) => it.type === 'headroom' || it.type === 'router');
|
|
100
|
+
const parts = [L.noticeHealthDesc, ...lines];
|
|
101
|
+
if (hasHeadroom) parts.push(L.noticeHealthHint);
|
|
102
|
+
const toHeadroom = hasHeadroom && items.every((it) => it.type === 'headroom' || it.type === 'router');
|
|
103
|
+
showCornerCard({
|
|
104
|
+
position: 'br',
|
|
105
|
+
kind: 'warning',
|
|
106
|
+
title: L.noticeHealthTitle,
|
|
107
|
+
desc: parts.join('\n\n'),
|
|
108
|
+
actions: [{
|
|
109
|
+
label: toHeadroom ? L.noticeHealthGoHeadroom : L.noticeGoChain,
|
|
110
|
+
primary: true,
|
|
111
|
+
onClick: () => {
|
|
112
|
+
closeCornerCard('br');
|
|
113
|
+
if (toHeadroom) openSettingsDialog('headroom');
|
|
114
|
+
else openSettingsDialog('proxy', 'vendor');
|
|
115
|
+
},
|
|
116
|
+
}],
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// 任务名生成失败:哪家供应商、哪个模型、服务端返回的原因。跟随会话起名时供应商一栏
|
|
121
|
+
// 写成“会话用的供应商”
|
|
122
|
+
function showTitleGen(notice, L) {
|
|
123
|
+
const provider = String(notice.providerName || '') || L.noticeTitleGenSession;
|
|
124
|
+
const item = L.noticeTitleGenItem
|
|
125
|
+
.replace('{provider}', provider)
|
|
126
|
+
.replace('{model}', String(notice.model || '—'))
|
|
127
|
+
.replace('{error}', String(notice.error || ''));
|
|
128
|
+
showCornerCard({
|
|
129
|
+
position: 'br',
|
|
130
|
+
kind: 'error',
|
|
131
|
+
title: L.noticeTitleGenTitle,
|
|
132
|
+
desc: [L.noticeTitleGenDesc, item, L.noticeTitleGenHow].join('\n\n'),
|
|
133
|
+
actions: [{
|
|
134
|
+
label: L.noticeTitleGenGo,
|
|
135
|
+
primary: true,
|
|
136
|
+
onClick: () => { closeCornerCard('br'); openSettingsDialog('agents', 'titleGen'); },
|
|
137
|
+
}],
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 工具名检查有文件改不了:列出文件、想改成什么、为什么写不进去
|
|
142
|
+
function showToolName(notice, L) {
|
|
143
|
+
const items = (Array.isArray(notice.items) ? notice.items : [])
|
|
144
|
+
.filter((it) => it && typeof it === 'object');
|
|
145
|
+
if (!items.length) return;
|
|
146
|
+
// 报错原文里混着临时文件名(.mcp.json.502418.…tmp),界面上压缩成短语;
|
|
147
|
+
// 认不出的错保留原文、截到 120 字符。原始报错在提示文件里留着
|
|
148
|
+
const errText = (raw) => {
|
|
149
|
+
const s = String(raw || '');
|
|
150
|
+
if (/EACCES|EPERM/i.test(s)) return L.noticeToolNameNoPerm;
|
|
151
|
+
return s.length > 120 ? s.slice(0, 120) + '…' : s;
|
|
152
|
+
};
|
|
153
|
+
const lines = items.map((it) => {
|
|
154
|
+
const renames = (Array.isArray(it.renames) ? it.renames : [])
|
|
155
|
+
.map((x) => `${String((x && x.from) || '')} → ${String((x && x.to) || '')}`).join(listSep());
|
|
156
|
+
return L.noticeToolNameItem
|
|
157
|
+
.replace('{file}', String(it.file || ''))
|
|
158
|
+
.replace('{changes}', renames)
|
|
159
|
+
.replace('{error}', errText(it.error));
|
|
160
|
+
});
|
|
161
|
+
// 说明 + 可复制的命令 + 后续行为。条目之间不空行(路径长,空行会把卡片撑很高)
|
|
162
|
+
const cmd = String(notice.command || '').trim();
|
|
163
|
+
const parts = [L.noticeToolNameDesc, lines.join('\n')];
|
|
164
|
+
if (cmd) parts.push(L.noticeToolNameFix, cmd, L.noticeToolNameAfter);
|
|
165
|
+
else parts.push(L.noticeToolNameFixNoCmd);
|
|
166
|
+
showCornerCard({
|
|
167
|
+
position: 'br',
|
|
168
|
+
kind: 'warning',
|
|
169
|
+
title: L.noticeToolNameTitle,
|
|
170
|
+
desc: parts.join('\n\n'),
|
|
171
|
+
actions: [{
|
|
172
|
+
label: L.noticeToolNameGo,
|
|
173
|
+
primary: true,
|
|
174
|
+
onClick: () => { closeCornerCard('br'); openSettingsDialog('agents', 'zcode'); },
|
|
175
|
+
}],
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// 顺序即优先级:链的提示最先,然后是巡检、任务名、工具名
|
|
180
|
+
const KINDS = [
|
|
181
|
+
{ field: 'notice', seenKey: 'zcodeproProxyChainNoticeSeen', show: showChainNotice },
|
|
182
|
+
{ field: 'health', seenKey: 'zcodeproProxyHealthNoticeSeen', show: showHealth },
|
|
183
|
+
{ field: 'titleGen', seenKey: 'zcodeproTitleGenNoticeSeen', show: showTitleGen },
|
|
184
|
+
{ field: 'toolName', seenKey: 'zcodeproToolNameNoticeSeen', show: showToolName },
|
|
185
|
+
];
|
|
186
|
+
const shownBy = {};
|
|
187
|
+
|
|
188
|
+
async function tick() {
|
|
189
|
+
const cfg = await getConfig();
|
|
190
|
+
if ((cfg.features || {}).proxyChainNotice === false) return;
|
|
191
|
+
const res = await rpc('/provider-proxies/migration-notice');
|
|
192
|
+
if (!res || res.ok === false) return;
|
|
193
|
+
for (const kind of KINDS) {
|
|
194
|
+
const n = res[kind.field];
|
|
195
|
+
if (!n || !n.at || !fresh(n.at)) continue;
|
|
196
|
+
if (n.at === shownBy[kind.field] || n.at === storeGet(kind.seenKey)) continue;
|
|
197
|
+
// 右下角已被别的卡片占着就先不弹:showCornerCard 会替换同位置的旧卡片,
|
|
198
|
+
// 叠上去等于把用户正在看的提示顶掉。不算“已弹过”,等对方关掉后下轮再来
|
|
199
|
+
if (cornerBusy()) return;
|
|
200
|
+
shownBy[kind.field] = n.at;
|
|
201
|
+
storeSet(kind.seenKey, n.at);
|
|
202
|
+
kind.show(n, t());
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export function startProxyChainNotice() {
|
|
208
|
+
const run = () => { tick().catch(() => { /* 单次失败不影响后续 */ }); };
|
|
209
|
+
run();
|
|
210
|
+
setInterval(() => {
|
|
211
|
+
if (document.hidden) return;
|
|
212
|
+
run();
|
|
213
|
+
}, POLL_MS);
|
|
214
|
+
document.addEventListener('visibilitychange', () => { if (!document.hidden) run(); });
|
|
215
|
+
}
|