@hjd-hitsz/claudex-web 4.1.3 → 4.1.5
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/CHANGELOG.md +36 -0
- package/build-info.json +4 -4
- package/client/dist/app.js +85 -85
- package/client/dist/document-editor.js +4 -3
- package/client/dist/style.css +1 -1
- package/docs/architecture/backends.md +1 -1
- package/docs/architecture/behavior.md +24 -10
- package/docs/architecture.md +18 -6
- package/docs/deployment.md +5 -3
- package/docs/development.md +17 -5
- package/docs/releases/v4.1.4.md +7 -0
- package/docs/releases/v4.1.5.md +6 -0
- package/package.json +6 -4
- package/packages/translation-engine/package.json +1 -1
- package/server/src/backends/claude/adapter.mjs +2 -0
- package/server/src/backends/claude/execution-session.mjs +29 -1
- package/server/src/backends/claude/provider-environment.mjs +3 -0
- package/server/src/backends/claude/provider-transport.mjs +29 -13
- package/server/src/backends/claude/runtime.mjs +15 -8
- package/server/src/backends/claude/worker-client.mjs +12 -5
- package/server/src/backends/claude/worker-server.mjs +3 -1
- package/server/src/backends/codex/adapter.mjs +9 -6
- package/server/src/backends/codex/methods.mjs +0 -1
- package/server/src/backends/configuration.mjs +6 -0
- package/server/src/backends/manager.mjs +14 -14
- package/server/src/backends/subagent-settings.mjs +14 -0
- package/server/src/service-maintenance-process.mjs +69 -0
- package/server/src/service-maintenance-worker.mjs +60 -25
- package/server/src/service-maintenance.mjs +66 -27
- package/server/src/workspace/file-search.mjs +244 -0
- package/server/src/workspace/service.mjs +17 -3
- package/shared/backend-activity.mjs +2 -0
- package/shared/subagent-settings.mjs +18 -0
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { readFile, readdir, stat } from "node:fs/promises";
|
|
2
|
+
import {
|
|
3
|
+
basename,
|
|
4
|
+
dirname,
|
|
5
|
+
isAbsolute,
|
|
6
|
+
join,
|
|
7
|
+
relative,
|
|
8
|
+
resolve,
|
|
9
|
+
} from "node:path";
|
|
10
|
+
import { setImmediate } from "node:timers/promises";
|
|
11
|
+
import fuzzysort from "fuzzysort";
|
|
12
|
+
import ignore from "ignore";
|
|
13
|
+
|
|
14
|
+
const RESULT_LIMIT = 60;
|
|
15
|
+
const SCAN_BATCH_SIZE = 32;
|
|
16
|
+
const SEARCH_BATCH_SIZE = 8192;
|
|
17
|
+
const PREPARED_BATCH_LIMIT = 4;
|
|
18
|
+
const INDEX_LIFETIME_MS = 5000;
|
|
19
|
+
const MAX_CACHED_ROOTS = 3;
|
|
20
|
+
const normalize = (text) => text.normalize("NFKC").toLowerCase();
|
|
21
|
+
|
|
22
|
+
async function readDirectory(root, path) {
|
|
23
|
+
try {
|
|
24
|
+
return await readdir(join(root, path), { withFileTypes: true });
|
|
25
|
+
} catch (error) {
|
|
26
|
+
// A descendant can disappear while the workspace is being indexed.
|
|
27
|
+
if (!path || !["ENOENT", "ENOTDIR", "EACCES", "EPERM"].includes(error.code))
|
|
28
|
+
throw error;
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function ignored(path, layers) {
|
|
34
|
+
let excluded = false;
|
|
35
|
+
for (const { base, rules } of layers) {
|
|
36
|
+
const result = rules.test(path.slice(base.length));
|
|
37
|
+
if (result.ignored || result.unignored) excluded = result.ignored;
|
|
38
|
+
}
|
|
39
|
+
return excluded;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function directoryEntries(root, entry) {
|
|
43
|
+
const children = await readDirectory(root, entry.path);
|
|
44
|
+
const names = new Set(children.map(({ name }) => name));
|
|
45
|
+
const files = [".gitignore", ".ignore"].filter((name) => names.has(name));
|
|
46
|
+
const patterns = await Promise.all(
|
|
47
|
+
files.map((name) =>
|
|
48
|
+
readFile(join(root, entry.path, name), "utf8").catch((error) => {
|
|
49
|
+
if (error.code === "ENOENT") return "";
|
|
50
|
+
throw error;
|
|
51
|
+
}),
|
|
52
|
+
),
|
|
53
|
+
);
|
|
54
|
+
const layers = files.length
|
|
55
|
+
? [
|
|
56
|
+
...entry.layers,
|
|
57
|
+
{
|
|
58
|
+
base: entry.path ? entry.path + "/" : "",
|
|
59
|
+
rules: ignore().add(patterns.join("\n")),
|
|
60
|
+
},
|
|
61
|
+
]
|
|
62
|
+
: entry.layers;
|
|
63
|
+
return { path: entry.path, children, layers };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function buildIndex(root, signal) {
|
|
67
|
+
const pending = [{ path: "", layers: [] }];
|
|
68
|
+
const batches = [];
|
|
69
|
+
const append = (entries) =>
|
|
70
|
+
batches.push(
|
|
71
|
+
batches.length < PREPARED_BATCH_LIMIT
|
|
72
|
+
? fuzzysort.snapshot(entries, { key: "target" })
|
|
73
|
+
: entries,
|
|
74
|
+
);
|
|
75
|
+
let entries = [];
|
|
76
|
+
while (pending.length) {
|
|
77
|
+
signal.throwIfAborted();
|
|
78
|
+
const directories = pending.splice(-SCAN_BATCH_SIZE);
|
|
79
|
+
const contents = await Promise.all(
|
|
80
|
+
directories.map((entry) => directoryEntries(root, entry)),
|
|
81
|
+
);
|
|
82
|
+
for (const { path: parent, children, layers } of contents) {
|
|
83
|
+
for (const child of children) {
|
|
84
|
+
const path = parent ? parent + "/" + child.name : child.name;
|
|
85
|
+
const directory =
|
|
86
|
+
child.isDirectory() ||
|
|
87
|
+
(child.isSymbolicLink() &&
|
|
88
|
+
(await stat(join(root, path)).then(
|
|
89
|
+
(target) => target.isDirectory(),
|
|
90
|
+
() => false,
|
|
91
|
+
)));
|
|
92
|
+
if (ignored(directory ? path + "/" : path, layers)) continue;
|
|
93
|
+
if (child.isDirectory() && child.name !== ".git")
|
|
94
|
+
pending.push({ path, layers });
|
|
95
|
+
const name = normalize(child.name);
|
|
96
|
+
entries.push({
|
|
97
|
+
path,
|
|
98
|
+
file_name: child.name,
|
|
99
|
+
match_type: directory ? "directory" : "file",
|
|
100
|
+
name,
|
|
101
|
+
target: normalize(path),
|
|
102
|
+
depth: path.split("/").length,
|
|
103
|
+
hidden: path.startsWith(".") || path.includes("/."),
|
|
104
|
+
});
|
|
105
|
+
if (entries.length === SEARCH_BATCH_SIZE) {
|
|
106
|
+
append(entries);
|
|
107
|
+
entries = [];
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (entries.length) append(entries);
|
|
113
|
+
return batches;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function searchQuery(root, input) {
|
|
117
|
+
const text = input.replaceAll("\\", "/").trim();
|
|
118
|
+
const query = normalize(text);
|
|
119
|
+
if (!query) return null;
|
|
120
|
+
const anchored = /^(?:\/|\.\.?\/)/.test(query);
|
|
121
|
+
const pathQuery = query.includes("/");
|
|
122
|
+
const term = query.slice(query.lastIndexOf("/") + 1);
|
|
123
|
+
if (!term) return null;
|
|
124
|
+
const rooted = anchored ? relative(root, resolve(root, text)) : "";
|
|
125
|
+
if (anchored && (rooted === ".." || rooted.startsWith("../"))) return null;
|
|
126
|
+
return {
|
|
127
|
+
query: anchored ? normalize(rooted) : query,
|
|
128
|
+
term,
|
|
129
|
+
pathQuery,
|
|
130
|
+
parent: anchored ? dirname(normalize(rooted)) : null,
|
|
131
|
+
parentTerm: pathQuery ? basename(dirname(query)) : "",
|
|
132
|
+
explicitHidden: query.split("/").some((part) => /^\.[^.]/.test(part)),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function relevance(
|
|
137
|
+
match,
|
|
138
|
+
{ query, term, pathQuery, parent, parentTerm, explicitHidden },
|
|
139
|
+
) {
|
|
140
|
+
const { name, target, match_type, hidden, depth } = match.obj;
|
|
141
|
+
const directoryPath = pathQuery ? dirname(target) : "";
|
|
142
|
+
if (parent !== null && directoryPath !== parent) return 0;
|
|
143
|
+
const directory = match_type === "directory";
|
|
144
|
+
const nameMatch = fuzzysort.single(term, name);
|
|
145
|
+
if (!nameMatch && (pathQuery || directory)) return 0;
|
|
146
|
+
const stem = directory ? name : name.replace(/(?<=.)\.[^.]+$/, "");
|
|
147
|
+
const exactPath = target === query || target.endsWith("/" + query);
|
|
148
|
+
const prefixPath = ("/" + target).includes("/" + query);
|
|
149
|
+
const grade =
|
|
150
|
+
pathQuery && exactPath
|
|
151
|
+
? 6
|
|
152
|
+
: pathQuery && prefixPath
|
|
153
|
+
? 5
|
|
154
|
+
: name === term || stem === term
|
|
155
|
+
? 4
|
|
156
|
+
: name.startsWith(term)
|
|
157
|
+
? 3
|
|
158
|
+
: nameMatch
|
|
159
|
+
? 2
|
|
160
|
+
: 1;
|
|
161
|
+
const parentMatch =
|
|
162
|
+
parentTerm && fuzzysort.single(parentTerm, basename(directoryPath));
|
|
163
|
+
const quality = pathQuery
|
|
164
|
+
? 0.45 * (parentMatch?.score || 0) +
|
|
165
|
+
0.35 * nameMatch.score +
|
|
166
|
+
0.2 * match.score
|
|
167
|
+
: (nameMatch?.score ?? match.score);
|
|
168
|
+
return (
|
|
169
|
+
((directory ? 16 : 0) +
|
|
170
|
+
(explicitHidden || !hidden ? 8 : 0) +
|
|
171
|
+
grade +
|
|
172
|
+
quality * 0.8 +
|
|
173
|
+
0.15 / depth) /
|
|
174
|
+
32
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function createFileSearch() {
|
|
179
|
+
const controller = new AbortController();
|
|
180
|
+
const indexes = new Map();
|
|
181
|
+
|
|
182
|
+
function index(root) {
|
|
183
|
+
const now = Date.now();
|
|
184
|
+
let entry = indexes.get(root);
|
|
185
|
+
if (!entry || entry.expires <= now) {
|
|
186
|
+
entry = { expires: Infinity };
|
|
187
|
+
const current = entry;
|
|
188
|
+
entry.batches = buildIndex(root, controller.signal).then(
|
|
189
|
+
(batches) => {
|
|
190
|
+
current.expires = Date.now() + INDEX_LIFETIME_MS;
|
|
191
|
+
return batches;
|
|
192
|
+
},
|
|
193
|
+
(error) => {
|
|
194
|
+
if (indexes.get(root) === current) indexes.delete(root);
|
|
195
|
+
throw error;
|
|
196
|
+
},
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
indexes.delete(root);
|
|
200
|
+
indexes.set(root, entry);
|
|
201
|
+
if (indexes.size > MAX_CACHED_ROOTS)
|
|
202
|
+
indexes.delete(indexes.keys().next().value);
|
|
203
|
+
return entry.batches;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
async search({ roots, query }) {
|
|
208
|
+
if (
|
|
209
|
+
typeof query !== "string" ||
|
|
210
|
+
!Array.isArray(roots) ||
|
|
211
|
+
!roots.every((root) => typeof root === "string" && isAbsolute(root))
|
|
212
|
+
)
|
|
213
|
+
throw new Error("文件搜索需要关键词和绝对工作目录");
|
|
214
|
+
const matches = [];
|
|
215
|
+
for (const root of new Set(roots.map((path) => resolve(path)))) {
|
|
216
|
+
const request = searchQuery(root, query);
|
|
217
|
+
if (!request) continue;
|
|
218
|
+
for (const batch of await index(root)) {
|
|
219
|
+
await setImmediate(undefined, { signal: controller.signal });
|
|
220
|
+
const results = fuzzysort.go(request.query, batch, {
|
|
221
|
+
...(Array.isArray(batch) && { key: "target" }),
|
|
222
|
+
threshold: 0,
|
|
223
|
+
limit: RESULT_LIMIT,
|
|
224
|
+
scoreFn: (match) => relevance(match, request),
|
|
225
|
+
});
|
|
226
|
+
for (const {
|
|
227
|
+
obj: { path, file_name, match_type },
|
|
228
|
+
score,
|
|
229
|
+
} of results)
|
|
230
|
+
matches.push({ root, path, file_name, match_type, score });
|
|
231
|
+
if (Array.isArray(batch)) fuzzysort.cleanup();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
fuzzysort.cleanup();
|
|
235
|
+
matches.sort((a, b) => b.score - a.score || a.path.localeCompare(b.path));
|
|
236
|
+
return { files: matches.slice(0, RESULT_LIMIT), ranked: true };
|
|
237
|
+
},
|
|
238
|
+
close() {
|
|
239
|
+
controller.abort();
|
|
240
|
+
indexes.clear();
|
|
241
|
+
fuzzysort.cleanup();
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
}
|
|
@@ -2,14 +2,25 @@ import { workspaceMethods } from "../../../shared/product-contract.mjs";
|
|
|
2
2
|
import { fileRequest } from "./files.mjs";
|
|
3
3
|
import { executeCommand } from "./commands.mjs";
|
|
4
4
|
import { createTerminals } from "./terminals.mjs";
|
|
5
|
+
import { createFileSearch } from "./file-search.mjs";
|
|
5
6
|
|
|
6
|
-
// 网页交互终端和主机文件操作由服务持有,原生搜索由所属后端提供。
|
|
7
7
|
export function createWorkspace({ broadcast, configDir }) {
|
|
8
8
|
const terminals = createTerminals(broadcast);
|
|
9
|
+
const files = createFileSearch();
|
|
9
10
|
const methods = new Set(workspaceMethods);
|
|
10
11
|
return {
|
|
11
|
-
handles
|
|
12
|
+
handles(method, backend, workspaceOwner) {
|
|
13
|
+
if (backend && method === "fuzzyFileSearch")
|
|
14
|
+
return (
|
|
15
|
+
backend.type === "claude" ||
|
|
16
|
+
["127.0.0.1", "localhost", "[::1]"].includes(
|
|
17
|
+
new URL(backend.url).hostname,
|
|
18
|
+
)
|
|
19
|
+
);
|
|
20
|
+
return methods.has(method) && (!backend || workspaceOwner === "bridge");
|
|
21
|
+
},
|
|
12
22
|
request(method, params) {
|
|
23
|
+
if (method === "fuzzyFileSearch") return files.search(params);
|
|
13
24
|
if (method.startsWith("fs/"))
|
|
14
25
|
return fileRequest(method, params, configDir);
|
|
15
26
|
if (method === "command/exec" && !params.tty)
|
|
@@ -22,6 +33,9 @@ export function createWorkspace({ broadcast, configDir }) {
|
|
|
22
33
|
throw new Error(`未知工作区操作:${method}`);
|
|
23
34
|
},
|
|
24
35
|
snapshot: terminals.snapshot,
|
|
25
|
-
close
|
|
36
|
+
close() {
|
|
37
|
+
files.close();
|
|
38
|
+
terminals.close();
|
|
39
|
+
},
|
|
26
40
|
};
|
|
27
41
|
}
|
|
@@ -3,6 +3,8 @@ import { isCompacting } from "./compaction-state.mjs";
|
|
|
3
3
|
// 后端快照包含原生执行与网页占用;停止前仍须在执行方原子复核。
|
|
4
4
|
export function hasBackendWork(state = {}) {
|
|
5
5
|
return Boolean(
|
|
6
|
+
state.installingBackends?.length ||
|
|
7
|
+
state.backends?.some((backend) => backend.installation?.installing) ||
|
|
6
8
|
Object.keys(state.activeTurns || {}).length ||
|
|
7
9
|
Object.keys(state.backgroundTasks || {}).length ||
|
|
8
10
|
Object.keys(state.queuedInputs || {}).length ||
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const fields = ["subagentModel", "subagentProviderId", "subagentEffort"];
|
|
2
|
+
|
|
3
|
+
export function subagentSettings(backend) {
|
|
4
|
+
return Object.fromEntries(fields.map((key) => [key, backend[key] || ""]));
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function isSubagentSettings(input) {
|
|
8
|
+
return (
|
|
9
|
+
fields.some((key) => Object.hasOwn(input, key)) &&
|
|
10
|
+
Object.keys(input).every((key) => ["id", "type", ...fields].includes(key))
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function executionBackend(backend) {
|
|
15
|
+
return Object.fromEntries(
|
|
16
|
+
Object.entries(backend).filter(([key]) => !fields.includes(key)),
|
|
17
|
+
);
|
|
18
|
+
}
|