@jayyuen66/dsh-zvec-grep 0.1.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/LICENSE +21 -0
- package/README.md +224 -0
- package/client.js +1016 -0
- package/cordis.patch.yml +15 -0
- package/host.js +2457 -0
- package/icon.svg +1 -0
- package/package.json +104 -0
package/client.js
ADDED
|
@@ -0,0 +1,1016 @@
|
|
|
1
|
+
// GENERATED by buildClient() in build-client.mjs — edit src/client-entry.ts, then run `node build-client.mjs`.
|
|
2
|
+
window.__ModuleLoader__.load({
|
|
3
|
+
id: '@jayyuen66/dsh-zvec-grep',
|
|
4
|
+
factory: (require) => {
|
|
5
|
+
var module = { exports: {} };
|
|
6
|
+
var exports = module.exports;
|
|
7
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
8
|
+
let react = require("react");
|
|
9
|
+
//#region lib/embedding-catalog.ts
|
|
10
|
+
/** 默认模型:本地最强(1024 维 / 8192 上下文 / 中英通用),约 640MB 一次性下载。 */
|
|
11
|
+
const DEFAULT_EMBEDDING = "local/qwen3-embedding-0.6b";
|
|
12
|
+
/**
|
|
13
|
+
* transformers.js 后端的 id。清单里 384/768 维的那批纯 JS ONNX 小模型都挂这一档,
|
|
14
|
+
* 故取值归一处;带 `LocalEmbeddingBackend` 标注,枚举成员改名时这里当场编译红,
|
|
15
|
+
* 而不是静默产出一个不在联合里的字符串。
|
|
16
|
+
*/
|
|
17
|
+
const TRANSFORMERS_JS_BACKEND = "transformers-js";
|
|
18
|
+
/**
|
|
19
|
+
* 本地候选清单,按后端分三档(体积/语言口径,实测 zg v0.2.1):
|
|
20
|
+
* - llama-cpp:qwen3-embedding-0.6b 约 640MB(本包默认)、
|
|
21
|
+
* embeddinggemma-300m 约 300MB(英文)——两档都要原生件,Metal 加速由 zg 侧 device=auto 试;
|
|
22
|
+
* - model2vec:potion-code-16m-v2 约 32MB(代码专用、最轻最快)、potion-multilingual-128m
|
|
23
|
+
* (多语言轻量)、potion-retrieval-32m(通用检索)——纯 JS worker,零原生依赖;
|
|
24
|
+
* - transformers-js:其余 ONNX 小模型,384 维那批是英文/多语言轻量档,768 维那批里
|
|
25
|
+
* jina 面向代码、gte/nomic 面向 8192 长文。
|
|
26
|
+
*/
|
|
27
|
+
const LOCAL_EMBEDDINGS = [
|
|
28
|
+
{
|
|
29
|
+
reference: "local/qwen3-embedding-0.6b",
|
|
30
|
+
backend: "llama-cpp",
|
|
31
|
+
dimension: 1024
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
reference: "local/embeddinggemma-300m",
|
|
35
|
+
backend: "llama-cpp",
|
|
36
|
+
dimension: 768
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
reference: "local/potion-code-16m-v2",
|
|
40
|
+
backend: "model2vec",
|
|
41
|
+
dimension: 256
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
reference: "local/potion-multilingual-128m",
|
|
45
|
+
backend: "model2vec",
|
|
46
|
+
dimension: 256
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
reference: "local/potion-retrieval-32m",
|
|
50
|
+
backend: "model2vec",
|
|
51
|
+
dimension: 512
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
reference: "local/bge-small-en-v1.5",
|
|
55
|
+
backend: TRANSFORMERS_JS_BACKEND,
|
|
56
|
+
dimension: 384
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
reference: "local/all-minilm-l6-v2",
|
|
60
|
+
backend: TRANSFORMERS_JS_BACKEND,
|
|
61
|
+
dimension: 384
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
reference: "local/multilingual-e5-small",
|
|
65
|
+
backend: TRANSFORMERS_JS_BACKEND,
|
|
66
|
+
dimension: 384
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
reference: "local/jina-embeddings-v2-base-code",
|
|
70
|
+
backend: TRANSFORMERS_JS_BACKEND,
|
|
71
|
+
dimension: 768
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
reference: "local/gte-modernbert-base",
|
|
75
|
+
backend: TRANSFORMERS_JS_BACKEND,
|
|
76
|
+
dimension: 768
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
reference: "local/nomic-embed-text-v1.5",
|
|
80
|
+
backend: TRANSFORMERS_JS_BACKEND,
|
|
81
|
+
dimension: 768
|
|
82
|
+
}
|
|
83
|
+
];
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region lib/card-draft.ts
|
|
86
|
+
/** 草稿优先取值:touched 有该键(哪怕值是 undefined=待清空)即用草稿。 */
|
|
87
|
+
function draftOf(touched, value, field) {
|
|
88
|
+
return field in touched ? touched[field] : value[field];
|
|
89
|
+
}
|
|
90
|
+
/** 数值型输入的显示文本:非有限数(含 undefined)回落到默认文案。 */
|
|
91
|
+
function numberText(raw, fallback) {
|
|
92
|
+
return typeof raw === "number" && Number.isFinite(raw) ? String(raw) : fallback;
|
|
93
|
+
}
|
|
94
|
+
//#endregion
|
|
95
|
+
//#region node_modules/.pnpm/@jayyuen66+dsh-plugin-shared@0.1.0_@deepseek-ai+cordis@4.0.4_eslint-plugin-sonarjs@4.2._a5b0027b68963132c312048d042510c0/node_modules/@jayyuen66/dsh-plugin-shared/dist/record.js
|
|
96
|
+
/** 对象类型守卫:非 `null` 的 `object` 且非数组(`Record` 索引签名投影的基础)。 */
|
|
97
|
+
function isRecord(value) {
|
|
98
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
99
|
+
}
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region lib/card-logic.ts
|
|
102
|
+
/**
|
|
103
|
+
* 把 settings scope 桥成 { getSnapshot, subscribe } store。
|
|
104
|
+
* 缓存必须 per-scope(闭包内):模块全局会在多 scope 交错 getSnapshot 时互相
|
|
105
|
+
* 冲 memo,导致 useSyncExternalStore 每次拿到新引用 → 无限重渲染。
|
|
106
|
+
*/
|
|
107
|
+
function cardStore(scope) {
|
|
108
|
+
const EMPTY_SNAPSHOT = {
|
|
109
|
+
status: "loading",
|
|
110
|
+
writable: false,
|
|
111
|
+
value: {}
|
|
112
|
+
};
|
|
113
|
+
let cachedSnap = null;
|
|
114
|
+
let cachedView = EMPTY_SNAPSHOT;
|
|
115
|
+
return {
|
|
116
|
+
getSnapshot() {
|
|
117
|
+
const snap = scope.getSnapshot();
|
|
118
|
+
if (snap !== cachedSnap) {
|
|
119
|
+
cachedSnap = snap;
|
|
120
|
+
cachedView = {
|
|
121
|
+
status: snap.status,
|
|
122
|
+
writable: snap.writable,
|
|
123
|
+
value: snap.value ?? {}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
return cachedView;
|
|
127
|
+
},
|
|
128
|
+
subscribe(listener) {
|
|
129
|
+
return scope.subscribe(listener);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
const DEFAULT_LIMIT_TEXT = "10";
|
|
134
|
+
const DEFAULT_BUDGET_TEXT = "3";
|
|
135
|
+
const DEFAULT_WINDOW_TEXT = "10";
|
|
136
|
+
/** 六个字段的草稿显示值(ZgCard 只渲染这里算出来的东西)。 */
|
|
137
|
+
function draftFields(touched, value) {
|
|
138
|
+
const embedding = draftOf(touched, value, "defaultEmbedding");
|
|
139
|
+
const endpoint = draftOf(touched, value, "hfEndpoint");
|
|
140
|
+
const enforce = draftOf(touched, value, "enforceSearchFirst");
|
|
141
|
+
const limitDraft = draftOf(touched, value, "defaultLimit");
|
|
142
|
+
let limitDisplay = numberText(limitDraft, DEFAULT_LIMIT_TEXT);
|
|
143
|
+
if (limitDraft === void 0) limitDisplay = "defaultLimit" in touched ? "" : DEFAULT_LIMIT_TEXT;
|
|
144
|
+
return {
|
|
145
|
+
embedding: typeof embedding === "string" && embedding.length > 0 ? embedding : DEFAULT_EMBEDDING,
|
|
146
|
+
limitDisplay,
|
|
147
|
+
hfEndpoint: typeof endpoint === "string" ? endpoint : "",
|
|
148
|
+
enforceSearchFirst: enforce !== false,
|
|
149
|
+
grepBudget: numberText(draftOf(touched, value, "grepBudgetPerSearch"), DEFAULT_BUDGET_TEXT),
|
|
150
|
+
unlockWindow: numberText(draftOf(touched, value, "unlockWindowMin"), DEFAULT_WINDOW_TEXT)
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
const LIMIT_MIN = 1;
|
|
154
|
+
const LIMIT_MAX = 50;
|
|
155
|
+
function resolveLimitInput(raw, t) {
|
|
156
|
+
const trimmed = raw.trim();
|
|
157
|
+
if (trimmed.length === 0) return { kind: "empty" };
|
|
158
|
+
const num = Number(trimmed);
|
|
159
|
+
if (!Number.isSafeInteger(num)) return {
|
|
160
|
+
kind: "invalid",
|
|
161
|
+
hint: t("limitInvalid", {
|
|
162
|
+
min: LIMIT_MIN,
|
|
163
|
+
max: LIMIT_MAX
|
|
164
|
+
})
|
|
165
|
+
};
|
|
166
|
+
if (num < LIMIT_MIN) return {
|
|
167
|
+
kind: "clamped",
|
|
168
|
+
value: LIMIT_MIN,
|
|
169
|
+
hint: t("limitClamped", {
|
|
170
|
+
min: LIMIT_MIN,
|
|
171
|
+
max: LIMIT_MAX,
|
|
172
|
+
value: LIMIT_MIN
|
|
173
|
+
})
|
|
174
|
+
};
|
|
175
|
+
if (num > LIMIT_MAX) return {
|
|
176
|
+
kind: "clamped",
|
|
177
|
+
value: LIMIT_MAX,
|
|
178
|
+
hint: t("limitClamped", {
|
|
179
|
+
min: LIMIT_MIN,
|
|
180
|
+
max: LIMIT_MAX,
|
|
181
|
+
value: LIMIT_MAX
|
|
182
|
+
})
|
|
183
|
+
};
|
|
184
|
+
return {
|
|
185
|
+
kind: "valid",
|
|
186
|
+
value: num
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
function limitDraftOf(raw, t) {
|
|
190
|
+
const resolution = resolveLimitInput(raw, t);
|
|
191
|
+
if (resolution.kind === "invalid") return {
|
|
192
|
+
draft: void 0,
|
|
193
|
+
error: resolution.hint
|
|
194
|
+
};
|
|
195
|
+
if (resolution.kind === "empty") return {
|
|
196
|
+
draft: void 0,
|
|
197
|
+
error: null
|
|
198
|
+
};
|
|
199
|
+
return {
|
|
200
|
+
draft: resolution.value,
|
|
201
|
+
error: null
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* 轮询应答 → 卡片状态。
|
|
206
|
+
* 关键:harness 的 bash-local 对**任何自然退出**都置 `status:'completed'`
|
|
207
|
+
* (packages/shell/bash-local/src/index.ts:308 只在信号终止/调用方 abort 时给
|
|
208
|
+
* 'killed'),exit 127「zg 没装」也是 completed。只看 status 会把失败显示成
|
|
209
|
+
* 「重建完成」,故成功判据 = completed 且 exit 0。
|
|
210
|
+
* 状态词现取自官方作业注册表(ctx.jobs),比原先的进程态多两枚:
|
|
211
|
+
* - `stopping`:取消已发出、进程还没收完 ⇒ 仍在跑,绝不能渲染成失败;
|
|
212
|
+
* - `failed`:注册表侧判定失败(生产者的 done 被拒、或收尾抛错),此时没有退出码可读。
|
|
213
|
+
*/
|
|
214
|
+
function parseRebuildPoll(body, t) {
|
|
215
|
+
const { status, exitCode, output } = body;
|
|
216
|
+
const outputText = typeof output === "string" ? output : "";
|
|
217
|
+
if (status === "running" || status === "stopping") return {
|
|
218
|
+
phase: "running",
|
|
219
|
+
output: outputText.length > 0 ? outputText : t("rebuildRunning")
|
|
220
|
+
};
|
|
221
|
+
if (status === "completed" && exitCode === 0) return {
|
|
222
|
+
phase: "done",
|
|
223
|
+
output: outputText
|
|
224
|
+
};
|
|
225
|
+
let reason;
|
|
226
|
+
if (status === "killed") reason = t("rebuildKilled");
|
|
227
|
+
else if (status === "failed") reason = t("rebuildFailed");
|
|
228
|
+
else reason = t("rebuildExitCode", { exitCode: String(exitCode) });
|
|
229
|
+
return {
|
|
230
|
+
phase: "error",
|
|
231
|
+
output: outputText.length > 0 ? `${reason}\n${outputText}` : reason
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* 响应不健康的可行动描述;健康返回 null。
|
|
236
|
+
* @param ok - res.ok
|
|
237
|
+
* @param status - res.status(403 单列:CSRF token 过期时卡片要「重取 token 重试」)
|
|
238
|
+
* @param contentType - res.headers.get('content-type') ?? ''
|
|
239
|
+
* @param t - 文案取值(官方 locale 的 translator)
|
|
240
|
+
*/
|
|
241
|
+
function httpProblem(ok, status, contentType, t) {
|
|
242
|
+
if (status === 403) return t("httpForbidden");
|
|
243
|
+
if (!ok) return t("httpFailed", { status });
|
|
244
|
+
if (!contentType.toLowerCase().includes("application/json")) return t("httpNotJson", { contentType: contentType.length > 0 ? contentType : t("contentTypeMissing") });
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
/** JSON 文本 → 对象;非对象/解析失败返回 null(调用方给可读错误)。 */
|
|
248
|
+
function parseJsonRecord(text) {
|
|
249
|
+
let parsed;
|
|
250
|
+
try {
|
|
251
|
+
parsed = JSON.parse(text);
|
|
252
|
+
} catch {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
return isRecord(parsed) ? parsed : null;
|
|
256
|
+
}
|
|
257
|
+
/** 端点统一应答形状:`{ ok: true, … }` 才算成功。 */
|
|
258
|
+
function isOkBody(body) {
|
|
259
|
+
return body["ok"] === true;
|
|
260
|
+
}
|
|
261
|
+
//#endregion
|
|
262
|
+
//#region src/ui-messages.ts
|
|
263
|
+
const UI_MESSAGES = {
|
|
264
|
+
zh: {
|
|
265
|
+
cardTitle: "zvec-grep(zg)",
|
|
266
|
+
cardDescription: "工作区语义检索:zg_search / zg_index / zg_status;本地 embedding + BM25 + 向量 RRF",
|
|
267
|
+
statusReadOnly: "当前作用域只读",
|
|
268
|
+
statusDirty: "有未保存的修改,点「保存」生效",
|
|
269
|
+
statusClean: "无未保存的修改",
|
|
270
|
+
save: "保存",
|
|
271
|
+
saving: "保存中…",
|
|
272
|
+
revert: "撤销",
|
|
273
|
+
saveFailed: "保存失败:{reason}",
|
|
274
|
+
saveRejected: "保存被宿主拒绝(配置校验未通过或写入被跳过),修改未生效",
|
|
275
|
+
embeddingLabel: "默认本地 embedding",
|
|
276
|
+
embeddingHint: "只影响「新建」索引;切换并保存后,已有索引需点下方「重建」重新 embedding",
|
|
277
|
+
limitLabel: "默认每组结果数(1-50)",
|
|
278
|
+
limitInvalid: "请输入 {min}-{max} 的整数",
|
|
279
|
+
limitClamped: "已钳到 {min}-{max} 范围:{value}",
|
|
280
|
+
endpointLabel: "HuggingFace 镜像地址(可选)",
|
|
281
|
+
endpointHint: "留空走 huggingface.co;国内网络可填 https://modelscope.cn/models 等兼容镜像(仅 llama-cpp 后端生效,如 qwen3)",
|
|
282
|
+
enforceLabel: "强制 search-first(先 zg_search 再 grep/rg)",
|
|
283
|
+
enforceHint: "开启后,已建索引工作区内的 grep/rg 受配额门禁约束:每次成功 zg_search 解锁下方次数的 grep/rg(时效见下),用尽或过期后再次拦截(提高检索覆盖与准确性)",
|
|
284
|
+
budgetLabel: "每次 zg_search 解锁的 grep/rg 次数(1-20)",
|
|
285
|
+
budgetHint: "配额越小语义检索越频繁、准确性约束越强;清空恢复默认 3",
|
|
286
|
+
budgetInvalid: "解锁次数必须是 {min}-{max} 的整数",
|
|
287
|
+
windowLabel: "解锁时效(分钟,1-240)",
|
|
288
|
+
windowHint: "超过该时长未再次 zg_search,配额整体过期;清空恢复默认 10",
|
|
289
|
+
windowInvalid: "解锁时效必须是 {min}-{max} 的整数",
|
|
290
|
+
rebuildLabel: "工作区重建(切换 embedding 并保存后执行)",
|
|
291
|
+
rebuildHint: "以当前默认模型重建:{embedding};等价于 zg index --rebuild。{roots}",
|
|
292
|
+
rootsNoteWith: "候选工作区来自本 daemon 已打开过的会话(服务端白名单)。",
|
|
293
|
+
rootsNoteEmpty: "尚无可选工作区:先在该工作区里开一个会话(任一次工具调用即登记)。",
|
|
294
|
+
rebuildButton: "重建索引(动作,点击即执行)",
|
|
295
|
+
rebuildRunning: "重建中…",
|
|
296
|
+
rebuildDone: "重建完成",
|
|
297
|
+
rebuildStarting: "正在后台重建索引…(首次会下载 embedding 模型,可能较慢)",
|
|
298
|
+
rebuildKilled: "重建进程被终止",
|
|
299
|
+
rebuildFailed: "重建作业被宿主注册表判定失败(没拿到进程退出码)",
|
|
300
|
+
rebuildExitCode: "重建进程退出码 {exitCode}(zg 未安装/权限不足/沙箱拒绝都会走到这里)",
|
|
301
|
+
rebuildMissingJobId: "启动重建失败:应答缺 jobId",
|
|
302
|
+
rebuildRootNotAbsolute: "root 必须是绝对路径(以 / 开头)",
|
|
303
|
+
modelOption: "{reference}({dimension}维)",
|
|
304
|
+
backendLlamaCpp: "llama-cpp(原生,需 node-llama-cpp)",
|
|
305
|
+
backendTransformersJs: "transformers-js(纯 JS ONNX)",
|
|
306
|
+
backendModel2Vec: "model2vec(纯 JS worker,最轻)",
|
|
307
|
+
httpForbidden: "403:请求被拒绝(同源校验或 CSRF token 失效)",
|
|
308
|
+
httpFailed: "请求失败(HTTP {status})",
|
|
309
|
+
httpNotJson: "响应不是 JSON(content-type={contentType}):重建端点未注册或路径被前端兜住了",
|
|
310
|
+
contentTypeMissing: "缺省",
|
|
311
|
+
notJsonBody: "响应不是 JSON 对象:{url}",
|
|
312
|
+
requestRejected: "请求未被接受"
|
|
313
|
+
},
|
|
314
|
+
en: {
|
|
315
|
+
cardTitle: "zvec-grep (zg)",
|
|
316
|
+
cardDescription: "Workspace semantic search: zg_search / zg_index / zg_status; local embedding + BM25 + vector RRF",
|
|
317
|
+
statusReadOnly: "This scope is read-only",
|
|
318
|
+
statusDirty: "Unsaved changes — press Save to apply",
|
|
319
|
+
statusClean: "No unsaved changes",
|
|
320
|
+
save: "Save",
|
|
321
|
+
saving: "Saving…",
|
|
322
|
+
revert: "Revert",
|
|
323
|
+
saveFailed: "save failed: {reason}",
|
|
324
|
+
saveRejected: "save rejected by host (validation failed or write skipped) — changes not applied",
|
|
325
|
+
embeddingLabel: "Default local embedding",
|
|
326
|
+
embeddingHint: "Affects newly built indexes only; after switching and saving, press Rebuild below to re-embed existing indexes",
|
|
327
|
+
limitLabel: "Default hits per group (1-50)",
|
|
328
|
+
limitInvalid: "enter an integer between {min} and {max}",
|
|
329
|
+
limitClamped: "clamped into the {min}-{max} range: {value}",
|
|
330
|
+
endpointLabel: "HuggingFace mirror URL (optional)",
|
|
331
|
+
endpointHint: "Leave empty for huggingface.co; on networks that cannot reach HF, fill a compatible mirror such as https://modelscope.cn/models (llama-cpp backends only, e.g. qwen3)",
|
|
332
|
+
enforceLabel: "Enforce search-first (zg_search before grep/rg)",
|
|
333
|
+
enforceHint: "When on, grep/rg inside indexed workspaces is quota-gated: each successful zg_search unlocks the number of grep/rg calls below (valid for the window below), and the gate blocks again once it is spent or expired (better coverage and relevance)",
|
|
334
|
+
budgetLabel: "grep/rg calls unlocked per zg_search (1-20)",
|
|
335
|
+
budgetHint: "A smaller quota means more semantic searches and a stronger relevance constraint; clear to restore the default 3",
|
|
336
|
+
budgetInvalid: "the unlock quota must be an integer between {min} and {max}",
|
|
337
|
+
windowLabel: "Unlock window (minutes, 1-240)",
|
|
338
|
+
windowHint: "Without another zg_search inside this window the whole quota expires; clear to restore the default 10",
|
|
339
|
+
windowInvalid: "the unlock window must be an integer between {min} and {max}",
|
|
340
|
+
rebuildLabel: "Workspace rebuild (run after switching embedding and saving)",
|
|
341
|
+
rebuildHint: "Rebuild with the current default model: {embedding}; same as zg index --rebuild. {roots}",
|
|
342
|
+
rootsNoteWith: "Candidates come from sessions this daemon already opened (server-side whitelist).",
|
|
343
|
+
rootsNoteEmpty: "No workspace to pick yet: open a session in that workspace first (any tool call registers it).",
|
|
344
|
+
rebuildButton: "Rebuild index (action, runs on click)",
|
|
345
|
+
rebuildRunning: "rebuilding…",
|
|
346
|
+
rebuildDone: "rebuild finished",
|
|
347
|
+
rebuildStarting: "Rebuilding in the background… (the first run downloads the embedding model and may be slow)",
|
|
348
|
+
rebuildKilled: "the rebuild process was terminated",
|
|
349
|
+
rebuildFailed: "the host job registry reported the rebuild as failed (no process exit code)",
|
|
350
|
+
rebuildExitCode: "the rebuild process exited with code {exitCode} (missing zg, insufficient permissions and sandbox denials all land here)",
|
|
351
|
+
rebuildMissingJobId: "failed to start the rebuild: the response has no jobId",
|
|
352
|
+
rebuildRootNotAbsolute: "root must be an absolute path (starting with /)",
|
|
353
|
+
modelOption: "{reference} ({dimension} dims)",
|
|
354
|
+
backendLlamaCpp: "llama-cpp (native, requires node-llama-cpp)",
|
|
355
|
+
backendTransformersJs: "transformers-js (pure JS ONNX)",
|
|
356
|
+
backendModel2Vec: "model2vec (pure JS worker, lightest)",
|
|
357
|
+
httpForbidden: "403: request rejected (same-origin check or expired CSRF token)",
|
|
358
|
+
httpFailed: "request failed (HTTP {status})",
|
|
359
|
+
httpNotJson: "response is not JSON (content-type={contentType}): the rebuild endpoint is not registered, or a front-end fallback caught the path",
|
|
360
|
+
contentTypeMissing: "unset",
|
|
361
|
+
notJsonBody: "response is not a JSON object: {url}",
|
|
362
|
+
requestRejected: "the request was not accepted"
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
//#endregion
|
|
366
|
+
//#region node_modules/.pnpm/@jayyuen66+dsh-plugin-shared@0.1.0_@deepseek-ai+cordis@4.0.4_eslint-plugin-sonarjs@4.2._a5b0027b68963132c312048d042510c0/node_modules/@jayyuen66/dsh-plugin-shared/dist/errors.js
|
|
367
|
+
/** `Error` 取 `message`,其余一律 `String(error)`(`null` → `"null"`,对象 → 其 `toString()`)。 */
|
|
368
|
+
function errorText(error) {
|
|
369
|
+
return error instanceof Error ? error.message : String(error);
|
|
370
|
+
}
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region src/client-entry.ts
|
|
373
|
+
const NS = "zvec-grep";
|
|
374
|
+
/**
|
|
375
|
+
* `plugins.bundle.config` 的 key = **bundle 的 npm 包名**(不是裸条目 id)。
|
|
376
|
+
* 派发证据(installed dsh 0.1.7):
|
|
377
|
+
* · dsh-client-ui-plugin-manager/lib/client.js:1821 `renderSlot("plugins.bundle.config",
|
|
378
|
+
* { view: "page" }, { entryKey: pkg.name })`——pkg.name 是 bundle 包名;
|
|
379
|
+
* · 同文件 :2698 `configured: ledger.bundles.has(openPkg.name)`,:306/:320 的
|
|
380
|
+
* `packageView()` 里 `name: bundle.name`(= bundle 包),所以没有第二条可匹配的 key;
|
|
381
|
+
* · 同包 lib/types/client/slot-contract.d.ts:96-100「A bundle's own configuration,
|
|
382
|
+
* **keyed by the bundle's package name**」,且 kind 为 `keyed` → `key` 必填;
|
|
383
|
+
* · dsh-client-ui-renderer/lib/client.js:1154 逐字相等匹配 `entry.options.key === entryKey`,
|
|
384
|
+
* 错一个字符就是「整张卡不渲染」(不是回落、不是报错);
|
|
385
|
+
* · 官方占位者 dsh-experimental-client-ui-voice-input/lib/client.js:5659-5661 同样把
|
|
386
|
+
* key 写成包名 `@deepseek-ai/dsh-experimental-voice-input-bundle`。
|
|
387
|
+
* 行槽(本包未用)则是 `<pkg>#<rowId>`(plugin-manager/lib/client.js:27-28 `rowConfigKey`)。
|
|
388
|
+
* 值 = 本包 package.json 的 name,且必须出现在 profile 的 `dsh.profile.bundles` 里;
|
|
389
|
+
* test/build-client.test.ts 从 `~/.dsh/profiles/web/package.json` 解析校验,不在此处硬抄。
|
|
390
|
+
* 设置命名空间**不跟着改**:`ctx.configForms.get(entryId)` 仍吃裸条目 id(见 apply)。
|
|
391
|
+
*/
|
|
392
|
+
const BUNDLE_PKG = "@jayyuen66/dsh-zvec-grep";
|
|
393
|
+
const REBUILD_ROOTS_URL = "/_dsh/zvec-grep/rebuild-roots";
|
|
394
|
+
const REBUILD_URL = "/_dsh/zvec-grep/rebuild";
|
|
395
|
+
const REBUILD_STATUS_URL = "/_dsh/zvec-grep/rebuild-status";
|
|
396
|
+
/** 与 host.ts REBUILD_CSRF_HEADER 一致:动作端点要求回填这个头。 */
|
|
397
|
+
const REBUILD_CSRF_HEADER = "x-zvec-grep-csrf";
|
|
398
|
+
/** 重建轮询间隔(宿主后台进程,2s 足够跟上 zg 的进度输出)。 */
|
|
399
|
+
const REBUILD_POLL_MS = 2e3;
|
|
400
|
+
/** 解锁配额/时效的合法上限(与 host Config 的 schema 同范围,输入框与提示共用)。 */
|
|
401
|
+
const BUDGET_MAX = 20;
|
|
402
|
+
const WINDOW_MAX = 240;
|
|
403
|
+
const CARD_CSS = [
|
|
404
|
+
".zgc-card{border:1px solid var(--dsw-alias-border-l2);background:var(--dsw-alias-bg-layer-3);border-radius:12px;list-style:none;transition:border-color .16s,background .16s}",
|
|
405
|
+
".zgc-card:hover{border-color:var(--dsw-alias-label-dimmed)}",
|
|
406
|
+
".zgc-card-open{background:var(--dsw-alias-bg-layer-2);border-color:var(--dsw-alias-label-dimmed)}",
|
|
407
|
+
".zgc-card-header{appearance:none;width:100%;font:inherit;color:inherit;text-align:left;cursor:pointer;background:transparent;border:0;border-radius:12px;align-items:center;gap:12px;padding:14px 16px;display:flex}",
|
|
408
|
+
".zgc-card-header:focus-visible{outline:2px solid var(--dsw-alias-brand-primary);outline-offset:-2px}",
|
|
409
|
+
".zgc-card-head{flex-direction:column;flex:1;gap:4px;min-width:0;display:flex}",
|
|
410
|
+
".zgc-card-name{color:var(--dsw-alias-label-primary);font-size:15px;font-weight:600;line-height:1.4}",
|
|
411
|
+
".zgc-card-desc{color:var(--dsw-alias-label-tertiary);font-size:13px;line-height:1.5}",
|
|
412
|
+
".zgc-card-chevron{color:var(--dsw-alias-label-tertiary);flex:none;transition:transform .16s}",
|
|
413
|
+
".zgc-card-chevron-open{transform:rotate(180deg)}",
|
|
414
|
+
".zgc-card-body{border-top:1px solid var(--dsw-alias-border-l2);margin:0 16px;padding:8px 0 12px}",
|
|
415
|
+
".zgc-row{display:flex;flex-direction:column;gap:6px;padding:9px 0}",
|
|
416
|
+
".zgc-label{font-size:13px;color:var(--dsw-alias-label-primary,inherit)}",
|
|
417
|
+
".zgc-hint{font-size:12px;color:var(--dsw-alias-label-tertiary,#8a8f99)}",
|
|
418
|
+
".zgc-select{width:100%;font:inherit;font-size:13px;color:var(--dsw-alias-label-primary,inherit);background:var(--dsw-alias-fill-primary,transparent);border:1px solid var(--dsw-alias-border-l3,#d8dbe2);border-radius:6px;padding:6px 8px;box-sizing:border-box}",
|
|
419
|
+
".zgc-input{width:100%;font:inherit;font-size:13px;color:var(--dsw-alias-label-primary,inherit);background:var(--dsw-alias-fill-primary,transparent);border:1px solid var(--dsw-alias-border-l3,#d8dbe2);border-radius:6px;padding:6px 8px;box-sizing:border-box}",
|
|
420
|
+
".zgc-button{appearance:none;border:1px solid var(--dsw-alias-brand-primary,#2f6fed);color:var(--dsw-alias-brand-primary,#2f6fed);background:transparent;border-radius:6px;padding:5px 12px;font:inherit;font-size:13px;cursor:pointer}",
|
|
421
|
+
".zgc-button:hover:not(:disabled){background:var(--dsw-alias-fill-secondary,rgba(0,0,0,.05))}",
|
|
422
|
+
".zgc-button:disabled{opacity:.5;cursor:progress}",
|
|
423
|
+
".zgc-output{font-size:12px;line-height:1.6;color:var(--dsw-alias-label-secondary,inherit);white-space:pre-wrap;word-break:break-all;max-height:180px;overflow:auto;background:var(--dsw-alias-fill-primary,transparent);border:1px solid var(--dsw-alias-border-l2);border-radius:6px;padding:8px}",
|
|
424
|
+
".zgc-error{font-size:12px;color:var(--dsw-alias-text-danger,#d33)}",
|
|
425
|
+
".zgc-savebar{display:flex;gap:8px;align-items:center;padding:10px 0 2px;border-top:1px dashed var(--dsw-alias-border-l2);margin-top:6px;flex-wrap:wrap}",
|
|
426
|
+
".zgc-btn-primary{appearance:none;border:1px solid var(--dsw-alias-brand-primary,#2f6fed);background:var(--dsw-alias-brand-primary,#2f6fed);color:var(--dsw-alias-label-primary-foreground,#fff);border-radius:6px;padding:5px 12px;font:inherit;font-size:13px;cursor:pointer}",
|
|
427
|
+
".zgc-btn-plain{appearance:none;border:1px solid var(--dsw-alias-border-l2,#d8dbe2);background:transparent;color:var(--dsw-alias-label-primary,inherit);border-radius:6px;padding:5px 12px;font:inherit;font-size:13px;cursor:pointer}",
|
|
428
|
+
".zgc-button:focus-visible,.zgc-btn-primary:focus-visible,.zgc-btn-plain:focus-visible{outline:2px solid var(--dsw-alias-brand-primary,#2f6fed);outline-offset:1px}",
|
|
429
|
+
".zgc-btn-primary:hover:not(:disabled){filter:brightness(.96)}",
|
|
430
|
+
".zgc-btn-plain:hover:not(:disabled){background:var(--dsw-alias-fill-secondary,rgba(0,0,0,.05))}",
|
|
431
|
+
".zgc-btn-primary:disabled,.zgc-btn-plain:disabled{opacity:.5;cursor:not-allowed}",
|
|
432
|
+
".zgc-dirty{font-size:12px;color:var(--dsw-alias-label-tertiary,#8a8f99)}",
|
|
433
|
+
".zgc-saveerr{font-size:12px;color:var(--dsw-alias-text-danger,#d33)}"
|
|
434
|
+
].join("\n");
|
|
435
|
+
const BACKENDS = [
|
|
436
|
+
"llama-cpp",
|
|
437
|
+
"transformers-js",
|
|
438
|
+
"model2vec"
|
|
439
|
+
];
|
|
440
|
+
/** SVG path 的 data 属性键(id-length 豁免需经变量计算键表达,oxfmt 不解引号)。 */
|
|
441
|
+
const PATH_DATA_KEY = "d";
|
|
442
|
+
/** 重建 root 输入框的候选列表 id(与 <datalist> 配对)。 */
|
|
443
|
+
const ROOT_OPTIONS_ID = "zgc-rebuild-root-options";
|
|
444
|
+
/** 候选按后端分组,供 <optgroup> 使用(文案取自 UI 字典,这里只留键)。 */
|
|
445
|
+
const BACKEND_LABEL_KEY = {
|
|
446
|
+
"llama-cpp": "backendLlamaCpp",
|
|
447
|
+
"transformers-js": "backendTransformersJs",
|
|
448
|
+
model2vec: "backendModel2Vec"
|
|
449
|
+
};
|
|
450
|
+
/** 取设置卡快照的配置值子对象(快照契约 {status, writable, value:{...}})。
|
|
451
|
+
* 独立函数保持字面量 `snap.value`(build-client.test 内容契约锁定该形态)。 */
|
|
452
|
+
function snapshotValue(snap) {
|
|
453
|
+
return snap.value;
|
|
454
|
+
}
|
|
455
|
+
function PluginCard(props) {
|
|
456
|
+
const { title, description, children } = props;
|
|
457
|
+
const [open, setOpen] = (0, react.useState)(false);
|
|
458
|
+
const headInner = (0, react.createElement)("span", { className: "zgc-card-head" }, (0, react.createElement)("span", { className: "zgc-card-name" }, title), (0, react.createElement)("span", { className: "zgc-card-desc" }, description));
|
|
459
|
+
const chevron = (0, react.createElement)("svg", {
|
|
460
|
+
width: 14,
|
|
461
|
+
height: 14,
|
|
462
|
+
viewBox: "0 0 14 14",
|
|
463
|
+
"aria-hidden": true,
|
|
464
|
+
className: open ? "zgc-card-chevron zgc-card-chevron-open" : "zgc-card-chevron"
|
|
465
|
+
}, (0, react.createElement)("path", {
|
|
466
|
+
[PATH_DATA_KEY]: "M3 5l4 4 4-4",
|
|
467
|
+
fill: "none",
|
|
468
|
+
stroke: "currentColor",
|
|
469
|
+
strokeWidth: 1.5,
|
|
470
|
+
strokeLinecap: "round",
|
|
471
|
+
strokeLinejoin: "round"
|
|
472
|
+
}));
|
|
473
|
+
const header = (0, react.createElement)("button", {
|
|
474
|
+
type: "button",
|
|
475
|
+
className: "zgc-card-header",
|
|
476
|
+
"aria-expanded": open,
|
|
477
|
+
onClick: () => {
|
|
478
|
+
setOpen(!open);
|
|
479
|
+
}
|
|
480
|
+
}, headInner, chevron);
|
|
481
|
+
const body = open ? (0, react.createElement)("div", { className: "zgc-card-body" }, children) : null;
|
|
482
|
+
return (0, react.createElement)("li", { className: open ? "zgc-card zgc-card-open" : "zgc-card" }, header, body);
|
|
483
|
+
}
|
|
484
|
+
/** touched 层与快照的差异字段(值语义比较;undefined 与缺失等价)。 */
|
|
485
|
+
function diffTouched(touched, value) {
|
|
486
|
+
const out = [];
|
|
487
|
+
for (const key of Object.keys(touched)) if (JSON.stringify(touched[key] ?? null) !== JSON.stringify(value[key] ?? null)) out.push(key);
|
|
488
|
+
return out;
|
|
489
|
+
}
|
|
490
|
+
/** 保存条(全自研插件统一)。 */
|
|
491
|
+
function SaveBar(props) {
|
|
492
|
+
const { t } = props;
|
|
493
|
+
const disabled = !props.writable || props.busy;
|
|
494
|
+
const saveBtn = (0, react.createElement)("button", {
|
|
495
|
+
type: "button",
|
|
496
|
+
className: "zgc-btn-primary",
|
|
497
|
+
"data-field": "save",
|
|
498
|
+
disabled: disabled || !props.dirty,
|
|
499
|
+
onClick: props.onSave
|
|
500
|
+
}, props.busy ? t("saving") : t("save"));
|
|
501
|
+
const discardBtn = (0, react.createElement)("button", {
|
|
502
|
+
type: "button",
|
|
503
|
+
className: "zgc-btn-plain",
|
|
504
|
+
"data-field": "discard",
|
|
505
|
+
disabled: disabled || !props.dirty,
|
|
506
|
+
onClick: props.onDiscard
|
|
507
|
+
}, t("revert"));
|
|
508
|
+
let statusNode;
|
|
509
|
+
if (props.error !== null) statusNode = (0, react.createElement)("span", { className: "zgc-saveerr" }, props.error);
|
|
510
|
+
else if (props.writable) statusNode = (0, react.createElement)("span", { className: "zgc-dirty" }, props.dirty ? t("statusDirty") : t("statusClean"));
|
|
511
|
+
else statusNode = (0, react.createElement)("span", { className: "zgc-dirty" }, t("statusReadOnly"));
|
|
512
|
+
return (0, react.createElement)("div", { className: "zgc-savebar" }, saveBtn, discardBtn, statusNode);
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* embedding 候选项文本(官方 locale 的 {name} 插值)。提成模块级函数是为了不把
|
|
516
|
+
* t() 嵌进 createElement(unicorn/max-nested-calls 上限 3)。
|
|
517
|
+
*/
|
|
518
|
+
function modelOptionText(t, reference, dimension) {
|
|
519
|
+
return t("modelOption", {
|
|
520
|
+
reference,
|
|
521
|
+
dimension
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
/** 重建行的整行提示(同上:把带插值的 t() 挪出 createElement)。 */
|
|
525
|
+
function rebuildHintText(t, embedding, hasRoots) {
|
|
526
|
+
return t("rebuildHint", {
|
|
527
|
+
embedding,
|
|
528
|
+
roots: t(hasRoots ? "rootsNoteWith" : "rootsNoteEmpty")
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
/** 数值输入的合法区间文案(同上:t() 不进 onChange 的三层嵌套)。 */
|
|
532
|
+
function rangeErrorText(t, key, max) {
|
|
533
|
+
return t(key, {
|
|
534
|
+
min: 1,
|
|
535
|
+
max
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
/** embedding <select> 的分组候选(逐层构造,避免 createElement 深层嵌套)。 */
|
|
539
|
+
function buildOptionGroups(t, embeddingValue, writable, setField) {
|
|
540
|
+
const groups = [];
|
|
541
|
+
for (const backend of BACKENDS) {
|
|
542
|
+
const options = [];
|
|
543
|
+
for (const entry of LOCAL_EMBEDDINGS) if (entry.backend === backend) {
|
|
544
|
+
const { reference, dimension } = entry;
|
|
545
|
+
options.push((0, react.createElement)("option", {
|
|
546
|
+
key: reference,
|
|
547
|
+
value: reference
|
|
548
|
+
}, modelOptionText(t, reference, dimension)));
|
|
549
|
+
}
|
|
550
|
+
groups.push((0, react.createElement)("optgroup", {
|
|
551
|
+
key: backend,
|
|
552
|
+
label: t(BACKEND_LABEL_KEY[backend])
|
|
553
|
+
}, options));
|
|
554
|
+
}
|
|
555
|
+
return [(0, react.createElement)("select", {
|
|
556
|
+
className: "zgc-select",
|
|
557
|
+
value: embeddingValue,
|
|
558
|
+
disabled: !writable,
|
|
559
|
+
onChange: (ev) => {
|
|
560
|
+
setField("defaultEmbedding", ev.target.value);
|
|
561
|
+
}
|
|
562
|
+
}, groups)];
|
|
563
|
+
}
|
|
564
|
+
/** embedding 行:本地后端分组的 <select>(候选文本带维度插值)。 */
|
|
565
|
+
function buildEmbeddingRow(deps) {
|
|
566
|
+
const { fields, setField, t, writable } = deps;
|
|
567
|
+
const embeddingLabel = (0, react.createElement)("div", { className: "zgc-label" }, t("embeddingLabel"));
|
|
568
|
+
const embeddingHint = (0, react.createElement)("div", { className: "zgc-hint" }, t("embeddingHint"));
|
|
569
|
+
const embeddingSelects = buildOptionGroups(t, fields.embedding, writable, setField);
|
|
570
|
+
return (0, react.createElement)("div", { className: "zgc-row" }, embeddingLabel, embeddingHint, embeddingSelects[0]);
|
|
571
|
+
}
|
|
572
|
+
/** defaultLimit 行:数字输入,区间判定在 lib/card-logic.ts 的 limitDraftOf。 */
|
|
573
|
+
function buildLimitRow(deps) {
|
|
574
|
+
const { fields, setField, setSaveError, t, writable } = deps;
|
|
575
|
+
const limitLabel = (0, react.createElement)("div", { className: "zgc-label" }, t("limitLabel"));
|
|
576
|
+
const limitInput = (0, react.createElement)("input", {
|
|
577
|
+
className: "zgc-input",
|
|
578
|
+
type: "number",
|
|
579
|
+
min: 1,
|
|
580
|
+
max: 50,
|
|
581
|
+
value: fields.limitDisplay,
|
|
582
|
+
disabled: !writable,
|
|
583
|
+
onChange: (ev) => {
|
|
584
|
+
const draft = limitDraftOf(ev.target.value, t);
|
|
585
|
+
setSaveError(draft.error);
|
|
586
|
+
if (draft.error === null) setField("defaultLimit", draft.draft);
|
|
587
|
+
}
|
|
588
|
+
});
|
|
589
|
+
return (0, react.createElement)("div", { className: "zgc-row" }, limitLabel, limitInput);
|
|
590
|
+
}
|
|
591
|
+
/** hfEndpoint 行:镜像基地址(占位符即 schema 默认的那枚镜像,留空走官方 HF)。 */
|
|
592
|
+
function buildEndpointRow(deps) {
|
|
593
|
+
const { fields, setField, t, writable } = deps;
|
|
594
|
+
const endpointLabel = (0, react.createElement)("div", { className: "zgc-label" }, t("endpointLabel"));
|
|
595
|
+
const endpointHint = (0, react.createElement)("div", { className: "zgc-hint" }, t("endpointHint"));
|
|
596
|
+
const endpointInput = (0, react.createElement)("input", {
|
|
597
|
+
className: "zgc-input",
|
|
598
|
+
value: fields.hfEndpoint,
|
|
599
|
+
placeholder: "https://modelscope.cn/models",
|
|
600
|
+
disabled: !writable,
|
|
601
|
+
onChange: (ev) => {
|
|
602
|
+
setField("hfEndpoint", ev.target.value);
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
return (0, react.createElement)("div", { className: "zgc-row" }, endpointLabel, endpointHint, endpointInput);
|
|
606
|
+
}
|
|
607
|
+
/** enforceSearchFirst 行:整行可点的勾选(只读时光标退回 default,与禁用态一致)。 */
|
|
608
|
+
function buildEnforceRow(deps) {
|
|
609
|
+
const { fields, setField, t, writable } = deps;
|
|
610
|
+
const enforceCheckbox = (0, react.createElement)("input", {
|
|
611
|
+
type: "checkbox",
|
|
612
|
+
checked: fields.enforceSearchFirst,
|
|
613
|
+
disabled: !writable,
|
|
614
|
+
onChange: (ev) => {
|
|
615
|
+
setField("enforceSearchFirst", ev.target.checked);
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
const enforceText = (0, react.createElement)("span", { className: "zgc-label" }, t("enforceLabel"));
|
|
619
|
+
const enforceInner = (0, react.createElement)("label", { style: {
|
|
620
|
+
display: "flex",
|
|
621
|
+
gap: "8px",
|
|
622
|
+
alignItems: "center",
|
|
623
|
+
cursor: writable ? "pointer" : "default"
|
|
624
|
+
} }, enforceCheckbox, enforceText);
|
|
625
|
+
const enforceHint = (0, react.createElement)("div", { className: "zgc-hint" }, t("enforceHint"));
|
|
626
|
+
return (0, react.createElement)("div", { className: "zgc-row" }, enforceInner, enforceHint);
|
|
627
|
+
}
|
|
628
|
+
/** 配额 / 时效行:区间内落草稿、清空即回「未填」、越界给区间文案并置保存条错误。 */
|
|
629
|
+
function buildRangeRow(deps, spec) {
|
|
630
|
+
const { setField, setSaveError, t, writable } = deps;
|
|
631
|
+
const label = (0, react.createElement)("div", { className: "zgc-label" }, t(spec.labelKey));
|
|
632
|
+
const hint = (0, react.createElement)("div", { className: "zgc-hint" }, t(spec.hintKey));
|
|
633
|
+
const input = (0, react.createElement)("input", {
|
|
634
|
+
className: "zgc-input",
|
|
635
|
+
type: "number",
|
|
636
|
+
min: 1,
|
|
637
|
+
max: spec.max,
|
|
638
|
+
value: spec.value,
|
|
639
|
+
disabled: !writable,
|
|
640
|
+
onChange: (ev) => {
|
|
641
|
+
const { value: raw } = ev.target;
|
|
642
|
+
if (raw === "") {
|
|
643
|
+
setSaveError(null);
|
|
644
|
+
setField(spec.field, void 0);
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
const num = Number(raw);
|
|
648
|
+
if (Number.isSafeInteger(num) && num >= 1 && num <= spec.max) {
|
|
649
|
+
setSaveError(null);
|
|
650
|
+
setField(spec.field, num);
|
|
651
|
+
} else setSaveError(rangeErrorText(t, spec.invalidKey, spec.max));
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
return (0, react.createElement)("div", { className: "zgc-row" }, label, hint, input);
|
|
655
|
+
}
|
|
656
|
+
/** 重建行:root 输入(白名单候选进 <datalist>)+ 触发按钮(在跑时禁用)。 */
|
|
657
|
+
function buildRebuildRow(deps) {
|
|
658
|
+
const { fields, rebuild, root, roots, setRoot, t } = deps;
|
|
659
|
+
const rebuildLabel = (0, react.createElement)("div", { className: "zgc-label" }, t("rebuildLabel"));
|
|
660
|
+
const rebuildHint = (0, react.createElement)("div", { className: "zgc-hint" }, rebuildHintText(t, fields.embedding, roots.length > 0));
|
|
661
|
+
const rootOptions = roots.map((entry) => (0, react.createElement)("option", {
|
|
662
|
+
key: entry,
|
|
663
|
+
value: entry
|
|
664
|
+
}));
|
|
665
|
+
const rootDatalist = (0, react.createElement)("datalist", { id: ROOT_OPTIONS_ID }, ...rootOptions);
|
|
666
|
+
const rebuildInput = (0, react.createElement)("input", {
|
|
667
|
+
className: "zgc-input",
|
|
668
|
+
value: root,
|
|
669
|
+
list: ROOT_OPTIONS_ID,
|
|
670
|
+
placeholder: "/absolute/path/to/workspace",
|
|
671
|
+
onChange: (ev) => {
|
|
672
|
+
setRoot(ev.target.value);
|
|
673
|
+
}
|
|
674
|
+
});
|
|
675
|
+
const rebuildButton = (0, react.createElement)("button", {
|
|
676
|
+
type: "button",
|
|
677
|
+
className: "zgc-button",
|
|
678
|
+
"data-field": "rebuild",
|
|
679
|
+
disabled: rebuild.phase === "running",
|
|
680
|
+
onClick: deps.onRebuild
|
|
681
|
+
}, rebuild.phase === "running" ? t("rebuildRunning") : t("rebuildButton"));
|
|
682
|
+
return (0, react.createElement)("div", { className: "zgc-row" }, rebuildLabel, rebuildHint, rootDatalist, rebuildInput, rebuildButton);
|
|
683
|
+
}
|
|
684
|
+
/** 重建状态节点:phase 非 idle 才出现;done 且无输出时给一句「已完成」。 */
|
|
685
|
+
function buildRebuildStatusRow(deps) {
|
|
686
|
+
const { rebuild, t } = deps;
|
|
687
|
+
if (rebuild.phase === "idle") return null;
|
|
688
|
+
const displayClass = rebuild.phase === "error" ? "zgc-error" : "zgc-output";
|
|
689
|
+
let displayText;
|
|
690
|
+
if (rebuild.output === "") displayText = rebuild.phase === "done" ? t("rebuildDone") : "";
|
|
691
|
+
else displayText = rebuild.output;
|
|
692
|
+
return (0, react.createElement)("div", { className: displayClass }, displayText);
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* 卡片主体所有行(含重建 status 节点),返回子元素数组。
|
|
696
|
+
* 每行自己的构造器在上方按职责拆好,这里只负责顺序与两行区间行的差异面。
|
|
697
|
+
*/
|
|
698
|
+
function buildCardBody(deps) {
|
|
699
|
+
const { fields, t } = deps;
|
|
700
|
+
const rows = [
|
|
701
|
+
buildEmbeddingRow(deps),
|
|
702
|
+
buildLimitRow(deps),
|
|
703
|
+
buildEndpointRow(deps),
|
|
704
|
+
buildEnforceRow(deps),
|
|
705
|
+
buildRangeRow(deps, {
|
|
706
|
+
labelKey: "budgetLabel",
|
|
707
|
+
hintKey: "budgetHint",
|
|
708
|
+
field: "grepBudgetPerSearch",
|
|
709
|
+
invalidKey: "budgetInvalid",
|
|
710
|
+
max: BUDGET_MAX,
|
|
711
|
+
value: fields.grepBudget
|
|
712
|
+
}),
|
|
713
|
+
buildRangeRow(deps, {
|
|
714
|
+
labelKey: "windowLabel",
|
|
715
|
+
hintKey: "windowHint",
|
|
716
|
+
field: "unlockWindowMin",
|
|
717
|
+
invalidKey: "windowInvalid",
|
|
718
|
+
max: WINDOW_MAX,
|
|
719
|
+
value: fields.unlockWindow
|
|
720
|
+
}),
|
|
721
|
+
(0, react.createElement)(SaveBar, {
|
|
722
|
+
t,
|
|
723
|
+
dirty: deps.dirty,
|
|
724
|
+
writable: deps.writable,
|
|
725
|
+
busy: deps.busy,
|
|
726
|
+
error: deps.saveError,
|
|
727
|
+
onSave: deps.onSave,
|
|
728
|
+
onDiscard: deps.onDiscard
|
|
729
|
+
}),
|
|
730
|
+
buildRebuildRow(deps)
|
|
731
|
+
];
|
|
732
|
+
const status = buildRebuildStatusRow(deps);
|
|
733
|
+
if (status !== null) rows.push(status);
|
|
734
|
+
return rows;
|
|
735
|
+
}
|
|
736
|
+
/** 显式字段写入(schema-coverage 门禁按 set('字段名') 字面量识别绑定)。 */
|
|
737
|
+
function pushFieldWrite(ops, key, draft, set) {
|
|
738
|
+
if (key === "defaultEmbedding") ops.push(set("defaultEmbedding", draft));
|
|
739
|
+
else if (key === "defaultLimit") ops.push(set("defaultLimit", draft));
|
|
740
|
+
else if (key === "hfEndpoint") ops.push(set("hfEndpoint", draft));
|
|
741
|
+
else if (key === "enforceSearchFirst") ops.push(set("enforceSearchFirst", draft));
|
|
742
|
+
else if (key === "grepBudgetPerSearch") ops.push(set("grepBudgetPerSearch", draft));
|
|
743
|
+
else if (key === "unlockWindowMin") ops.push(set("unlockWindowMin", draft));
|
|
744
|
+
}
|
|
745
|
+
/** 服务端 `{ok:false,error}` 里的可行动文案(无 error 字段给通用兜底)。 */
|
|
746
|
+
function serverErrorOf(body, t) {
|
|
747
|
+
if (body === null) return null;
|
|
748
|
+
const { error } = body;
|
|
749
|
+
return typeof error === "string" && error.length > 0 ? error : t("requestRejected");
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* 一次 JSON 往返:先按 content-type 决定能不能当 JSON 读(未注册的路径会被前端
|
|
753
|
+
* SPA fallback 兜成 200 + text/html,直接 JSON.parse 只能给出
|
|
754
|
+
* `SyntaxError: Unexpected token '<'`),再让服务端的 error 文案优先于 HTTP 层
|
|
755
|
+
* 的笼统描述。
|
|
756
|
+
*/
|
|
757
|
+
async function requestJson(url, t, init) {
|
|
758
|
+
const res = await fetch(url, init);
|
|
759
|
+
const contentType = res.headers.get("content-type") ?? "";
|
|
760
|
+
const record = contentType.toLowerCase().includes("json") ? parseJsonRecord(await res.text()) : null;
|
|
761
|
+
const problem = serverErrorOf(record, t) ?? httpProblem(res.ok, res.status, contentType, t);
|
|
762
|
+
if (problem !== null) throw new Error(problem);
|
|
763
|
+
if (record === null) throw new Error(t("notJsonBody", { url }));
|
|
764
|
+
return record;
|
|
765
|
+
}
|
|
766
|
+
const NO_ACCESS = {
|
|
767
|
+
csrf: "",
|
|
768
|
+
roots: []
|
|
769
|
+
};
|
|
770
|
+
/** 引导端点应答 → { csrf, roots }(字段缺失/类型不对一律当作没拿到)。 */
|
|
771
|
+
function readAccess(body) {
|
|
772
|
+
const { csrf, roots } = body;
|
|
773
|
+
return {
|
|
774
|
+
csrf: typeof csrf === "string" ? csrf : "",
|
|
775
|
+
roots: Array.isArray(roots) ? roots.filter((item) => typeof item === "string") : []
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* 「工作区重建」状态机:取 token/白名单 → POST 启动 → 2s 轮询到终态。
|
|
780
|
+
* 从 ZgCard 拆出,保持组件体在一屏内可读。
|
|
781
|
+
* @param t - 取文案(官方 locale 的 translator,随卡片一起下发)。
|
|
782
|
+
*/
|
|
783
|
+
function useRebuild(t) {
|
|
784
|
+
const [root, setRoot] = (0, react.useState)("");
|
|
785
|
+
const [access, setAccess] = (0, react.useState)(NO_ACCESS);
|
|
786
|
+
const [rebuild, setRebuild] = (0, react.useState)({
|
|
787
|
+
phase: "idle",
|
|
788
|
+
output: ""
|
|
789
|
+
});
|
|
790
|
+
const pollRef = (0, react.useRef)(null);
|
|
791
|
+
function stopPolling() {
|
|
792
|
+
if (pollRef.current !== null) {
|
|
793
|
+
clearInterval(pollRef.current);
|
|
794
|
+
pollRef.current = null;
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
(0, react.useEffect)(() => () => {
|
|
798
|
+
stopPolling();
|
|
799
|
+
}, []);
|
|
800
|
+
/** 拉 CSRF token + 白名单。失败**不**改重建面板:面板 idle 时冒一个红字只是噪音,
|
|
801
|
+
* 用户真点「重建」时 start() 会把同一错误显示出来。 */
|
|
802
|
+
const refreshAccess = (0, react.useCallback)(async () => {
|
|
803
|
+
const next = readAccess(await requestJson(REBUILD_ROOTS_URL, t));
|
|
804
|
+
setAccess(next);
|
|
805
|
+
return next;
|
|
806
|
+
}, [t]);
|
|
807
|
+
(0, react.useEffect)(() => {
|
|
808
|
+
(async () => {
|
|
809
|
+
try {
|
|
810
|
+
await refreshAccess();
|
|
811
|
+
} catch {
|
|
812
|
+
setAccess(NO_ACCESS);
|
|
813
|
+
}
|
|
814
|
+
})();
|
|
815
|
+
}, [refreshAccess]);
|
|
816
|
+
async function pollStatus(jobId) {
|
|
817
|
+
try {
|
|
818
|
+
const poll = parseRebuildPoll(await requestJson(`${REBUILD_STATUS_URL}?jobId=${encodeURIComponent(jobId)}`, t), t);
|
|
819
|
+
if (poll.phase !== "running") stopPolling();
|
|
820
|
+
setRebuild({
|
|
821
|
+
phase: poll.phase,
|
|
822
|
+
output: poll.output
|
|
823
|
+
});
|
|
824
|
+
} catch (error) {
|
|
825
|
+
stopPolling();
|
|
826
|
+
setRebuild({
|
|
827
|
+
phase: "error",
|
|
828
|
+
output: errorText(error)
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
/** 发起一次重建(带 token);成功后进入轮询。 */
|
|
833
|
+
async function startOnce(token) {
|
|
834
|
+
const body = await requestJson(`${REBUILD_URL}?root=${encodeURIComponent(root.trim())}`, t, {
|
|
835
|
+
method: "POST",
|
|
836
|
+
headers: { [REBUILD_CSRF_HEADER]: token }
|
|
837
|
+
});
|
|
838
|
+
const { jobId } = body;
|
|
839
|
+
if (!isOkBody(body) || typeof jobId !== "string") throw new Error(t("rebuildMissingJobId"));
|
|
840
|
+
pollRef.current = setInterval(() => {
|
|
841
|
+
pollStatus(jobId);
|
|
842
|
+
}, REBUILD_POLL_MS);
|
|
843
|
+
}
|
|
844
|
+
async function start() {
|
|
845
|
+
if (!root.trim().startsWith("/")) {
|
|
846
|
+
setRebuild({
|
|
847
|
+
phase: "error",
|
|
848
|
+
output: t("rebuildRootNotAbsolute")
|
|
849
|
+
});
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
stopPolling();
|
|
853
|
+
setRebuild({
|
|
854
|
+
phase: "running",
|
|
855
|
+
output: t("rebuildStarting")
|
|
856
|
+
});
|
|
857
|
+
try {
|
|
858
|
+
let token = access.csrf;
|
|
859
|
+
if (token === "") token = (await refreshAccess()).csrf;
|
|
860
|
+
try {
|
|
861
|
+
await startOnce(token);
|
|
862
|
+
} catch (error) {
|
|
863
|
+
if (access.csrf === "") throw error;
|
|
864
|
+
await startOnce((await refreshAccess()).csrf);
|
|
865
|
+
}
|
|
866
|
+
} catch (error) {
|
|
867
|
+
stopPolling();
|
|
868
|
+
setRebuild({
|
|
869
|
+
phase: "error",
|
|
870
|
+
output: errorText(error)
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
return {
|
|
875
|
+
root,
|
|
876
|
+
roots: access.roots,
|
|
877
|
+
state: rebuild,
|
|
878
|
+
setRoot,
|
|
879
|
+
start
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
function ZgCard(props) {
|
|
883
|
+
const { t } = props;
|
|
884
|
+
const snap = props.useCard((state) => state);
|
|
885
|
+
const { status, writable: isSnapWritable } = snap;
|
|
886
|
+
const writable = status === "ready" && isSnapWritable;
|
|
887
|
+
const value = snapshotValue(snap);
|
|
888
|
+
const [touched, setTouched] = (0, react.useState)({});
|
|
889
|
+
const [busy, setBusy] = (0, react.useState)(false);
|
|
890
|
+
const [saveError, setSaveError] = (0, react.useState)(null);
|
|
891
|
+
const rebuildView = useRebuild(t);
|
|
892
|
+
const fields = draftFields(touched, value);
|
|
893
|
+
const dirty = diffTouched(touched, value).length > 0;
|
|
894
|
+
const setField = (field, draft) => {
|
|
895
|
+
setTouched((prev) => ({
|
|
896
|
+
...prev,
|
|
897
|
+
[field]: draft
|
|
898
|
+
}));
|
|
899
|
+
};
|
|
900
|
+
const save = async () => {
|
|
901
|
+
const keys = diffTouched(touched, value);
|
|
902
|
+
if (keys.length === 0) return;
|
|
903
|
+
setBusy(true);
|
|
904
|
+
setSaveError(null);
|
|
905
|
+
const ops = [];
|
|
906
|
+
for (const key of keys) {
|
|
907
|
+
const draft = touched[key];
|
|
908
|
+
if (draft === void 0) ops.push(props.unset(key));
|
|
909
|
+
else pushFieldWrite(ops, key, draft, props.set);
|
|
910
|
+
}
|
|
911
|
+
try {
|
|
912
|
+
if ((await Promise.all(ops)).some((accepted) => !accepted)) {
|
|
913
|
+
setBusy(false);
|
|
914
|
+
setSaveError(t("saveRejected"));
|
|
915
|
+
return;
|
|
916
|
+
}
|
|
917
|
+
} catch (error) {
|
|
918
|
+
setBusy(false);
|
|
919
|
+
setSaveError(t("saveFailed", { reason: errorText(error) }));
|
|
920
|
+
return;
|
|
921
|
+
}
|
|
922
|
+
const savedKeys = new Set(keys);
|
|
923
|
+
const next = Object.fromEntries(Object.entries(touched).filter(([entryKey]) => !savedKeys.has(entryKey)));
|
|
924
|
+
setBusy(false);
|
|
925
|
+
setTouched(next);
|
|
926
|
+
};
|
|
927
|
+
const discard = () => {
|
|
928
|
+
setTouched({});
|
|
929
|
+
setSaveError(null);
|
|
930
|
+
};
|
|
931
|
+
const rows = buildCardBody({
|
|
932
|
+
t,
|
|
933
|
+
fields,
|
|
934
|
+
writable,
|
|
935
|
+
roots: rebuildView.roots,
|
|
936
|
+
root: rebuildView.root,
|
|
937
|
+
rebuild: rebuildView.state,
|
|
938
|
+
dirty,
|
|
939
|
+
busy,
|
|
940
|
+
saveError,
|
|
941
|
+
setField,
|
|
942
|
+
setRoot: rebuildView.setRoot,
|
|
943
|
+
setSaveError,
|
|
944
|
+
onSave: () => {
|
|
945
|
+
save();
|
|
946
|
+
},
|
|
947
|
+
onDiscard: discard,
|
|
948
|
+
onRebuild: () => {
|
|
949
|
+
rebuildView.start();
|
|
950
|
+
}
|
|
951
|
+
});
|
|
952
|
+
const body = (0, react.createElement)("div", { style: { padding: "2px 0" } }, rows);
|
|
953
|
+
return (0, react.createElement)(PluginCard, {
|
|
954
|
+
title: t("cardTitle"),
|
|
955
|
+
description: t("cardDescription")
|
|
956
|
+
}, body);
|
|
957
|
+
}
|
|
958
|
+
const inject = [
|
|
959
|
+
"slots",
|
|
960
|
+
"configForms",
|
|
961
|
+
"locale"
|
|
962
|
+
];
|
|
963
|
+
function apply(ctx) {
|
|
964
|
+
ctx.effect(() => {
|
|
965
|
+
const tag = document.createElement("style");
|
|
966
|
+
tag.id = "zvec-grep-card-css";
|
|
967
|
+
tag.textContent = CARD_CSS;
|
|
968
|
+
document.head.append(tag);
|
|
969
|
+
return () => {
|
|
970
|
+
tag.remove();
|
|
971
|
+
};
|
|
972
|
+
}, "zvec-grep-card: styles");
|
|
973
|
+
const scope = ctx.configForms.get(NS);
|
|
974
|
+
const store = cardStore(scope);
|
|
975
|
+
ctx.effect(() => ctx.locale.register(NS, UI_MESSAGES), "zvec-grep-card: locale dictionaries");
|
|
976
|
+
const t = ctx.locale.bind(NS);
|
|
977
|
+
const writeField = async (field, valueDraft) => {
|
|
978
|
+
try {
|
|
979
|
+
return await scope.set(field, valueDraft);
|
|
980
|
+
} catch (error) {
|
|
981
|
+
console.error(`[zvec-grep] set ${field} failed:`, error);
|
|
982
|
+
throw error;
|
|
983
|
+
}
|
|
984
|
+
};
|
|
985
|
+
const unsetField = async (field) => {
|
|
986
|
+
try {
|
|
987
|
+
return await scope.unset(field);
|
|
988
|
+
} catch (error) {
|
|
989
|
+
console.error(`[zvec-grep] unset ${field} failed:`, error);
|
|
990
|
+
throw error;
|
|
991
|
+
}
|
|
992
|
+
};
|
|
993
|
+
ctx.slots.inject("plugins.bundle.config", () => {
|
|
994
|
+
return ctx.slots.register({
|
|
995
|
+
name: "plugins.bundle.config",
|
|
996
|
+
key: BUNDLE_PKG,
|
|
997
|
+
inject: () => ({
|
|
998
|
+
t,
|
|
999
|
+
hooks: { card: store },
|
|
1000
|
+
set: writeField,
|
|
1001
|
+
unset: unsetField
|
|
1002
|
+
})
|
|
1003
|
+
}, ZgCard);
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
//#endregion
|
|
1007
|
+
exports.apply = apply;
|
|
1008
|
+
exports.cardStore = cardStore;
|
|
1009
|
+
exports.diffTouched = diffTouched;
|
|
1010
|
+
exports.draftFields = draftFields;
|
|
1011
|
+
exports.inject = inject;
|
|
1012
|
+
exports.parseRebuildPoll = parseRebuildPoll;
|
|
1013
|
+
exports.resolveLimitInput = resolveLimitInput;
|
|
1014
|
+
return module.exports;
|
|
1015
|
+
},
|
|
1016
|
+
});
|