@evandu/super-pi 0.1.237 → 0.1.239
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/dist/client/.super-pi-client-sources.json +9 -8
- package/dist/client/assets/{CodeMirrorEditor-C0egC0LO.js → CodeMirrorEditor-CtzPSOsS.js} +1 -1
- package/dist/client/assets/{GitDiffEditor-aldh2_D_.js → GitDiffEditor-iNVZVVMb.js} +1 -1
- package/dist/client/assets/index-C5kxyPTT.js +340 -0
- package/dist/client/assets/index-DN0oBG6l.css +1 -0
- package/dist/client/index.html +2 -2
- package/dist/server/file-name-index.d.ts +12 -7
- package/dist/server/file-name-index.js +94 -83
- package/dist/server/git.d.ts +2 -2
- package/dist/server/git.js +3 -1
- package/dist/server/http-server.js +22 -29
- package/dist/shared/file-mentions.d.ts +1 -0
- package/dist/shared/file-mentions.js +60 -24
- package/dist/shared/git-graph.d.ts +4 -0
- package/package.json +1 -1
- package/dist/client/assets/index-BWLRPUnY.css +0 -1
- package/dist/client/assets/index-Bolukoov.js +0 -340
package/dist/client/index.html
CHANGED
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
<link rel="alternate icon" href="/favicon.ico" />
|
|
9
9
|
<link rel="apple-touch-icon" href="/favicon.svg" />
|
|
10
10
|
<title>Super Pi</title>
|
|
11
|
-
<script type="module" crossorigin src="/assets/index-
|
|
11
|
+
<script type="module" crossorigin src="/assets/index-C5kxyPTT.js"></script>
|
|
12
12
|
<link rel="modulepreload" crossorigin href="/assets/rolldown-runtime-XKyaHtl9.js">
|
|
13
|
-
<link rel="stylesheet" crossorigin href="/assets/index-
|
|
13
|
+
<link rel="stylesheet" crossorigin href="/assets/index-DN0oBG6l.css">
|
|
14
14
|
</head>
|
|
15
15
|
<body>
|
|
16
16
|
<div id="root"></div>
|
|
@@ -28,7 +28,7 @@ export type IndexProgress = {
|
|
|
28
28
|
/** The answer to one question: which paths contain this needle. */
|
|
29
29
|
export type NameSearchResult = {
|
|
30
30
|
hits: NameEntry[];
|
|
31
|
-
/**
|
|
31
|
+
/** Total matches before the result page is applied. */
|
|
32
32
|
total: number;
|
|
33
33
|
/** Whether the index has finished its first pass. `false` means "more may still arrive". */
|
|
34
34
|
indexed: boolean;
|
|
@@ -36,6 +36,7 @@ export type NameSearchResult = {
|
|
|
36
36
|
/** Set when the scan hit a limit. Its presence is the reason the answer is not the whole tree. */
|
|
37
37
|
incomplete: boolean;
|
|
38
38
|
};
|
|
39
|
+
export declare const QUICK_OPEN_PAGE_SIZE = 60;
|
|
39
40
|
/**
|
|
40
41
|
* The result ordering, and why it is not the path dictionary order.
|
|
41
42
|
*
|
|
@@ -43,10 +44,10 @@ export type NameSearchResult = {
|
|
|
43
44
|
* a whole string. In a list of two hundred that is actively hostile: the file whose name STARTS
|
|
44
45
|
* with what you typed is the one you meant, and it is somewhere in the middle. So the order is:
|
|
45
46
|
*
|
|
46
|
-
* 1.
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
47
|
+
* 1. exact filenames;
|
|
48
|
+
* 2. the hit position inside the file name, so prefixes rank ahead of other substrings;
|
|
49
|
+
* 3. shallower directories first;
|
|
50
|
+
* 4. path order as the final tie-break.
|
|
50
51
|
*/
|
|
51
52
|
export declare function compareNameHits(a: NameEntry, b: NameEntry, needle: string): number;
|
|
52
53
|
/**
|
|
@@ -74,6 +75,8 @@ export declare class FileNameIndex {
|
|
|
74
75
|
private incomplete;
|
|
75
76
|
private needsScan;
|
|
76
77
|
private generation;
|
|
78
|
+
private unread;
|
|
79
|
+
private hasScanned;
|
|
77
80
|
private readonly removedDuringScan;
|
|
78
81
|
private readonly directoriesReadDuringScan;
|
|
79
82
|
constructor(workspaceId: string, cwd?: string);
|
|
@@ -88,9 +91,12 @@ export declare class FileNameIndex {
|
|
|
88
91
|
* A scan that FAILED has not finished either - it never will be, and `failure` says why.
|
|
89
92
|
*/
|
|
90
93
|
get indexed(): boolean;
|
|
94
|
+
get hasSnapshot(): boolean;
|
|
91
95
|
get progress(): IndexProgress;
|
|
92
96
|
/** Begin the background scan. Safe to call more than once; the second call is a no-op. */
|
|
93
97
|
start(root: string): void;
|
|
98
|
+
/** Keep the preceding results usable while checking files written outside Super Pi. */
|
|
99
|
+
revalidate(root: string): void;
|
|
94
100
|
markStale(): void;
|
|
95
101
|
/**
|
|
96
102
|
* WHY THE SCAN'S FAILURE IS RECORDED RATHER THAN THROWN.
|
|
@@ -109,8 +115,6 @@ export declare class FileNameIndex {
|
|
|
109
115
|
get broken(): unknown;
|
|
110
116
|
/** Rethrow the scan's failure, so the route can turn it into the status it deserves. */
|
|
111
117
|
throwIfBroken(): void;
|
|
112
|
-
/** Rebuild from scratch. The ↻ button. */
|
|
113
|
-
reset(root: string): void;
|
|
114
118
|
/** Record a path that appeared or changed. Used for agent writes and user saves. */
|
|
115
119
|
upsert(relativePath: string, kind?: "file" | "directory", openLink?: true): void;
|
|
116
120
|
/** Forget a path that is gone. */
|
|
@@ -125,6 +129,7 @@ export declare class FileNameIndex {
|
|
|
125
129
|
* `app` return every file in `application/` as well - a result set nobody can read.
|
|
126
130
|
*/
|
|
127
131
|
search(needle: string, linksOnly?: boolean): NameSearchResult;
|
|
132
|
+
searchQuickOpen(query: string, offset: number): NameSearchResult;
|
|
128
133
|
/**
|
|
129
134
|
* Wait until the first scan has finished, or until the deadline passes.
|
|
130
135
|
*
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
*/
|
|
34
34
|
import { readdir, stat } from "node:fs/promises";
|
|
35
35
|
import { resolve } from "node:path";
|
|
36
|
-
import
|
|
36
|
+
import Fuse from "fuse.js";
|
|
37
|
+
import { matchPathMentions, rankPathMatches } from "../shared/file-mentions.js";
|
|
37
38
|
import { createRootGuard, toRelative } from "./files.js";
|
|
38
39
|
import { belowPath, blockedBy, inScope, matchesDirectoryReads } from "./file-index.js";
|
|
39
40
|
import { explorerInternalPath } from "./workspace-file-operations.js";
|
|
@@ -67,8 +68,10 @@ export function recordWorkspacePath(indexes, workspaceId, workspaceRoot, path) {
|
|
|
67
68
|
index.upsert(scoped);
|
|
68
69
|
}
|
|
69
70
|
}
|
|
71
|
+
export const QUICK_OPEN_PAGE_SIZE = 60;
|
|
70
72
|
const MAX_DIRECTORIES = 20000;
|
|
71
|
-
const
|
|
73
|
+
const MAX_ENTRIES = 200000;
|
|
74
|
+
const DIRECTORY_BATCH = 12;
|
|
72
75
|
/**
|
|
73
76
|
* The result ordering, and why it is not the path dictionary order.
|
|
74
77
|
*
|
|
@@ -76,12 +79,15 @@ const MAX_FILES = 200000;
|
|
|
76
79
|
* a whole string. In a list of two hundred that is actively hostile: the file whose name STARTS
|
|
77
80
|
* with what you typed is the one you meant, and it is somewhere in the middle. So the order is:
|
|
78
81
|
*
|
|
79
|
-
* 1.
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
82
|
+
* 1. exact filenames;
|
|
83
|
+
* 2. the hit position inside the file name, so prefixes rank ahead of other substrings;
|
|
84
|
+
* 3. shallower directories first;
|
|
85
|
+
* 4. path order as the final tie-break.
|
|
83
86
|
*/
|
|
84
87
|
export function compareNameHits(a, b, needle) {
|
|
88
|
+
const exact = Number(b.name.toLowerCase() === needle) - Number(a.name.toLowerCase() === needle);
|
|
89
|
+
if (exact !== 0)
|
|
90
|
+
return exact;
|
|
85
91
|
const position = hitPosition(a, needle) - hitPosition(b, needle);
|
|
86
92
|
if (position !== 0)
|
|
87
93
|
return position;
|
|
@@ -120,6 +126,8 @@ export class FileNameIndex {
|
|
|
120
126
|
incomplete = false;
|
|
121
127
|
needsScan = false;
|
|
122
128
|
generation = 0;
|
|
129
|
+
unread = new Set();
|
|
130
|
+
hasScanned = false;
|
|
123
131
|
removedDuringScan = new Set();
|
|
124
132
|
directoriesReadDuringScan = new Map();
|
|
125
133
|
constructor(workspaceId, cwd = "") {
|
|
@@ -139,6 +147,7 @@ export class FileNameIndex {
|
|
|
139
147
|
get indexed() {
|
|
140
148
|
return !this.building && this.failure === undefined;
|
|
141
149
|
}
|
|
150
|
+
get hasSnapshot() { return this.hasScanned; }
|
|
142
151
|
get progress() {
|
|
143
152
|
// The percentage is deliberately not a promise about the whole tree: the total is unknown
|
|
144
153
|
// until the scan ends, so it is derived from the directory budget instead, and the UI shows
|
|
@@ -151,17 +160,28 @@ export class FileNameIndex {
|
|
|
151
160
|
// The guard is "has a scan been started", NOT "are there any entries". An EMPTY workspace
|
|
152
161
|
// finishes its scan with zero entries, and an emptiness test would restart the scan on every
|
|
153
162
|
// request - so the answer to every query would be "still building" forever.
|
|
163
|
+
if (!this.started || this.needsScan)
|
|
164
|
+
this.revalidate(root);
|
|
165
|
+
}
|
|
166
|
+
/** Keep the preceding results usable while checking files written outside Super Pi. */
|
|
167
|
+
revalidate(root) {
|
|
154
168
|
if (this.building)
|
|
155
169
|
return;
|
|
156
|
-
if (this.
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
return;
|
|
170
|
+
if (this.root && this.root !== root) {
|
|
171
|
+
this.entries.clear();
|
|
172
|
+
this.hasScanned = false;
|
|
160
173
|
}
|
|
174
|
+
this.unread = new Set(this.entries.keys());
|
|
175
|
+
this.removedDuringScan.clear();
|
|
176
|
+
this.directoriesReadDuringScan.clear();
|
|
177
|
+
this.root = root;
|
|
161
178
|
this.needsScan = false;
|
|
162
179
|
this.started = true;
|
|
163
|
-
this.root = root;
|
|
164
180
|
this.building = true;
|
|
181
|
+
this.failure = undefined;
|
|
182
|
+
this.incomplete = false;
|
|
183
|
+
this.stopped = false;
|
|
184
|
+
this.scanned = 0;
|
|
165
185
|
void this.scan(++this.generation);
|
|
166
186
|
}
|
|
167
187
|
markStale() { if (this.started)
|
|
@@ -189,19 +209,6 @@ export class FileNameIndex {
|
|
|
189
209
|
return;
|
|
190
210
|
throw this.failure;
|
|
191
211
|
}
|
|
192
|
-
/** Rebuild from scratch. The ↻ button. */
|
|
193
|
-
reset(root) {
|
|
194
|
-
this.entries.clear();
|
|
195
|
-
this.removedDuringScan.clear();
|
|
196
|
-
this.directoriesReadDuringScan.clear();
|
|
197
|
-
this.scanned = 0;
|
|
198
|
-
this.incomplete = false;
|
|
199
|
-
this.stopped = false;
|
|
200
|
-
this.building = false;
|
|
201
|
-
this.started = false;
|
|
202
|
-
this.failure = undefined;
|
|
203
|
-
this.start(root);
|
|
204
|
-
}
|
|
205
212
|
/** Record a path that appeared or changed. Used for agent writes and user saves. */
|
|
206
213
|
upsert(relativePath, kind = "file", openLink) {
|
|
207
214
|
const path = relativePath.replace(/\\/g, "/").replace(/^\/+/, "");
|
|
@@ -210,6 +217,7 @@ export class FileNameIndex {
|
|
|
210
217
|
const cut = path.lastIndexOf("/");
|
|
211
218
|
const name = cut < 0 ? path : path.slice(cut + 1);
|
|
212
219
|
const directory = cut < 0 ? "" : path.slice(0, cut);
|
|
220
|
+
this.unread.delete(path);
|
|
213
221
|
this.entries.set(path, { path, name, directory, kind, ...(openLink ? { openLink } : {}) });
|
|
214
222
|
}
|
|
215
223
|
/** Forget a path that is gone. */
|
|
@@ -276,6 +284,21 @@ export class FileNameIndex {
|
|
|
276
284
|
incomplete: this.incomplete,
|
|
277
285
|
};
|
|
278
286
|
}
|
|
287
|
+
searchQuickOpen(query, offset) {
|
|
288
|
+
const files = [...this.entries.values()].filter(entry => entry.kind === "file");
|
|
289
|
+
const ranked = query ? rankPathMatches(files, query) : files;
|
|
290
|
+
if (query && !ranked.length && this.indexed) {
|
|
291
|
+
const approximate = new Fuse(files, { keys: [{ name: "name", weight: 2 }, { name: "path", weight: 1 }], threshold: 0.38, ignoreLocation: true });
|
|
292
|
+
ranked.push(...approximate.search(query, { limit: QUICK_OPEN_PAGE_SIZE }).map(match => match.item));
|
|
293
|
+
}
|
|
294
|
+
return {
|
|
295
|
+
hits: ranked.slice(offset, offset + (query ? QUICK_OPEN_PAGE_SIZE : 20)),
|
|
296
|
+
total: ranked.length,
|
|
297
|
+
indexed: this.indexed,
|
|
298
|
+
progress: this.progress,
|
|
299
|
+
incomplete: this.incomplete,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
279
302
|
/**
|
|
280
303
|
* Wait until the first scan has finished, or until the deadline passes.
|
|
281
304
|
*
|
|
@@ -311,78 +334,66 @@ export class FileNameIndex {
|
|
|
311
334
|
* Link names can be found, and linked directories never enter this traversal's queue.
|
|
312
335
|
*/
|
|
313
336
|
async scan(generation) {
|
|
314
|
-
const started = Date.now();
|
|
315
337
|
const guard = createRootGuard(this.root);
|
|
316
338
|
const queue = [""];
|
|
317
|
-
|
|
339
|
+
let count = 0;
|
|
340
|
+
scan: for (let next = 0; next < queue.length;) {
|
|
318
341
|
if (this.stopped || generation !== this.generation)
|
|
319
342
|
return;
|
|
320
|
-
const
|
|
321
|
-
if (
|
|
343
|
+
const batch = queue.slice(next, Math.min(next + DIRECTORY_BATCH, MAX_DIRECTORIES));
|
|
344
|
+
if (!batch.length) {
|
|
322
345
|
this.incomplete = true;
|
|
323
346
|
break;
|
|
324
347
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
entries = await readdir(await guard(directory), { withFileTypes: true });
|
|
329
|
-
}
|
|
330
|
-
catch (error) {
|
|
331
|
-
if (generation !== this.generation || this.stopped)
|
|
332
|
-
return;
|
|
333
|
-
// THE ROOT IS NOT ONE OF THE SKIPPABLE DIRECTORIES.
|
|
334
|
-
//
|
|
335
|
-
// A workspace whose directory is gone must answer 404 `workspace_missing`, and the reason is
|
|
336
|
-
// the one this file's own `incomplete` flag cannot express: "I searched and found nothing"
|
|
337
|
-
// and "there was nothing to search" are DIFFERENT answers, and only one of them is true.
|
|
338
|
-
// The old walk rethrew the root's failure and the route turned it into a 404; swallowing it
|
|
339
|
-
// here would put that exact bug back - a 200 with an empty list, which is byte-for-byte the
|
|
340
|
-
// response for "your search matched nothing".
|
|
341
|
-
//
|
|
342
|
-
// The failure is RECORDED, not thrown: see `failure` above for why throwing it here would
|
|
343
|
-
// take the whole server down with it.
|
|
344
|
-
//
|
|
345
|
-
// A SUBDIRECTORY that cannot be read is skipped instead: one unreadable folder in a large
|
|
346
|
-
// tree must not cost the reader every other match, and the scan says so through
|
|
347
|
-
// `incomplete` rather than by pretending the folder was empty.
|
|
348
|
-
if (directory === "") {
|
|
349
|
-
this.failure = error;
|
|
350
|
-
this.building = false;
|
|
351
|
-
return;
|
|
352
|
-
}
|
|
353
|
-
this.incomplete = true;
|
|
354
|
-
continue;
|
|
355
|
-
}
|
|
356
|
-
if (generation !== this.generation || this.stopped)
|
|
348
|
+
next += batch.length;
|
|
349
|
+
const listings = await Promise.allSettled(batch.map(async (directory) => readdir(await guard(directory), { withFileTypes: true })));
|
|
350
|
+
if (this.stopped || generation !== this.generation)
|
|
357
351
|
return;
|
|
358
|
-
for (const
|
|
359
|
-
const
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
352
|
+
for (const [index, listing] of listings.entries()) {
|
|
353
|
+
const directory = batch[index];
|
|
354
|
+
this.scanned += 1;
|
|
355
|
+
if (listing.status === "rejected") {
|
|
356
|
+
if (!directory) {
|
|
357
|
+
this.failure = listing.reason;
|
|
358
|
+
this.building = false;
|
|
359
|
+
this.unread.clear();
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
this.incomplete = true;
|
|
366
363
|
continue;
|
|
367
364
|
}
|
|
368
|
-
const
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
365
|
+
for (const entry of listing.value) {
|
|
366
|
+
const relative = directory ? `${directory}/${entry.name}` : entry.name;
|
|
367
|
+
if (explorerInternalPath(this.cwd ? `${this.cwd}/${relative}` : relative) || blockedBy(relative, this.removedDuringScan)
|
|
368
|
+
|| !matchesDirectoryReads(relative, this.directoriesReadDuringScan))
|
|
369
|
+
continue;
|
|
370
|
+
const isDirectory = entry.isDirectory();
|
|
371
|
+
const openLink = entry.isSymbolicLink() && await stat(resolve(this.root, relative)).then(info => info.isFile(), () => false);
|
|
372
|
+
if (generation !== this.generation || this.stopped)
|
|
373
|
+
return;
|
|
374
|
+
if (openLink && (blockedBy(relative, this.removedDuringScan) || !matchesDirectoryReads(relative, this.directoriesReadDuringScan)))
|
|
375
|
+
continue;
|
|
376
|
+
if (!isDirectory && !entry.isFile() && !openLink)
|
|
377
|
+
continue;
|
|
378
|
+
if (count >= MAX_ENTRIES) {
|
|
379
|
+
this.incomplete = true;
|
|
380
|
+
break scan;
|
|
381
|
+
}
|
|
382
|
+
count++;
|
|
383
|
+
this.unread.delete(relative);
|
|
384
|
+
this.entries.set(relative, { path: relative, name: entry.name, directory, kind: isDirectory ? "directory" : "file",
|
|
385
|
+
...(openLink ? { openLink: true } : {}) });
|
|
386
|
+
if (isDirectory)
|
|
387
|
+
queue.push(relative);
|
|
378
388
|
}
|
|
379
|
-
this.entries.set(relative, { path: relative, name: entry.name, directory, kind: "file", ...(openLink ? { openLink: true } : {}) });
|
|
380
389
|
}
|
|
381
|
-
// Yield between directories so a large tree does not monopolise the event loop and starve
|
|
382
|
-
// the WebSocket heartbeat. `setTimeout(0)` is the cheapest yield that still lets timers run.
|
|
383
|
-
if (Date.now() - started > 0 && queue.length)
|
|
384
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
385
390
|
}
|
|
391
|
+
if (!this.incomplete) {
|
|
392
|
+
for (const path of this.unread)
|
|
393
|
+
this.entries.delete(path);
|
|
394
|
+
}
|
|
395
|
+
this.unread.clear();
|
|
396
|
+
this.hasScanned = true;
|
|
386
397
|
this.building = false;
|
|
387
398
|
this.removedDuringScan.clear();
|
|
388
399
|
this.directoriesReadDuringScan.clear();
|
package/dist/server/git.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { GitCommitDetail } from "../shared/git-commit.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { GitGraphResponse } from "../shared/git-graph.js";
|
|
3
3
|
export type GitWorkspace = {
|
|
4
4
|
branch: string | null;
|
|
5
5
|
detached: boolean;
|
|
@@ -26,7 +26,7 @@ export declare function gitLog(root: string, limit: number, cwd?: string, query?
|
|
|
26
26
|
}[];
|
|
27
27
|
truncated: boolean;
|
|
28
28
|
}>;
|
|
29
|
-
export declare function gitGraph(root: string, limit: number, offset: number, snapshot: string, after: string): Promise<
|
|
29
|
+
export declare function gitGraph(root: string, limit: number, offset: number, snapshot: string, after: string, knownSnapshot?: string): Promise<GitGraphResponse>;
|
|
30
30
|
export declare function gitCommit(root: string, cwd: string, hash: string): Promise<GitCommitDetail>;
|
|
31
31
|
export declare function gitCommitDiff(root: string, cwd: string, hash: string, path: string, fullContext?: boolean): Promise<{
|
|
32
32
|
path: string;
|
package/dist/server/git.js
CHANGED
|
@@ -83,7 +83,7 @@ function parseGitCommits(raw) {
|
|
|
83
83
|
return commits;
|
|
84
84
|
}
|
|
85
85
|
const COMMIT_HASH = /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/i;
|
|
86
|
-
export async function gitGraph(root, limit, offset, snapshot, after) {
|
|
86
|
+
export async function gitGraph(root, limit, offset, snapshot, after, knownSnapshot = "") {
|
|
87
87
|
const size = pageNumber(limit, 50, 1, 100);
|
|
88
88
|
const start = pageNumber(offset, 0, 0, 2_147_483_647);
|
|
89
89
|
const repo = await worktreeRoot(root);
|
|
@@ -97,6 +97,8 @@ export async function gitGraph(root, limit, offset, snapshot, after) {
|
|
|
97
97
|
const changed = () => gitFailure("提交图已变化,请刷新后继续查看。", "graph_changed");
|
|
98
98
|
if (start && snapshot !== version)
|
|
99
99
|
throw changed();
|
|
100
|
+
if (!start && knownSnapshot === version)
|
|
101
|
+
return { unchanged: true, snapshot: version };
|
|
100
102
|
const labels = new Map();
|
|
101
103
|
const mark = (hash, label) => {
|
|
102
104
|
const list = labels.get(hash) ?? [];
|
|
@@ -26,7 +26,7 @@ import { MAX_WEB_REQUEST_BYTES } from "./web.js";
|
|
|
26
26
|
import { MAX_TOOL_BODY_BYTES } from "./tool-registry.js";
|
|
27
27
|
import { claimUpdateSlot, getLastUpdateOutcome, isUpdateInFlight, packageUpdateArgs, recordUpdateOutcome, releaseUpdateSlot, shellArgs } from "./update.js";
|
|
28
28
|
import { FileIndex, refreshWorkspaceIndex } from "./file-index.js";
|
|
29
|
-
import { ensureNameIndex, recordWorkspacePath } from "./file-name-index.js";
|
|
29
|
+
import { ensureNameIndex, QUICK_OPEN_PAGE_SIZE, recordWorkspacePath } from "./file-name-index.js";
|
|
30
30
|
import { resolveFileCwd, sessionScopeKey, workspacePathFromSession } from "./session-cwd.js";
|
|
31
31
|
import { explorerInternalPath, explorerPath, inspectWorkspaceUpload, listWorkspaceTrash, MAX_WORKSPACE_UPLOAD_BYTES, mutateWorkspaceTree, serializeWorkspaceWrite, uploadWorkspaceFile } from "./workspace-file-operations.js";
|
|
32
32
|
import { readWorkspaceDocumentPreview, respondWithWorkspaceFile } from "./workspace-file-response.js";
|
|
@@ -1433,7 +1433,7 @@ export function createHttpServer(options) {
|
|
|
1433
1433
|
if (op === "status")
|
|
1434
1434
|
json(200, await gitStatus(workspace.path, cwd));
|
|
1435
1435
|
else if (op === "graph") {
|
|
1436
|
-
json(200, await gitGraph(workspace.path, Number(url.searchParams.get("limit") ?? 50), Number(url.searchParams.get("offset") ?? 0), url.searchParams.get("snapshot") ?? "", url.searchParams.get("after") ?? ""));
|
|
1436
|
+
json(200, await gitGraph(workspace.path, Number(url.searchParams.get("limit") ?? 50), Number(url.searchParams.get("offset") ?? 0), url.searchParams.get("snapshot") ?? "", url.searchParams.get("after") ?? "", url.searchParams.get("knownSnapshot") ?? ""));
|
|
1437
1437
|
}
|
|
1438
1438
|
else if (op === "log" || op === "branches" || op === "stash") {
|
|
1439
1439
|
const query = url.searchParams.get("q") ?? "";
|
|
@@ -2973,41 +2973,34 @@ export function createHttpServer(options) {
|
|
|
2973
2973
|
json(405, { error: "目录列表需要 GET 请求。", code: "method" });
|
|
2974
2974
|
return;
|
|
2975
2975
|
}
|
|
2976
|
-
const query = url.searchParams.get("q");
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
// shape does not change under the client, but they are now constants that say the
|
|
2985
|
-
// opposite of what they used to.
|
|
2976
|
+
const query = url.searchParams.get("q")?.trim();
|
|
2977
|
+
const quick = url.searchParams.get("quick") === "1";
|
|
2978
|
+
if (query !== undefined && (query || quick)) {
|
|
2979
|
+
const page = Number(url.searchParams.get("page") ?? 0);
|
|
2980
|
+
if (quick && (query.length > 8192 || !Number.isSafeInteger(page) || page < 0 || page > Math.floor(Number.MAX_SAFE_INTEGER / QUICK_OPEN_PAGE_SIZE))) {
|
|
2981
|
+
json(400, { error: "文件查找请求无效。", code: "bad_query" });
|
|
2982
|
+
return;
|
|
2983
|
+
}
|
|
2986
2984
|
const index = nameIndexFor(context, workspaceId, root, cwd);
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
//
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
await index.waitUntilIndexed(4000);
|
|
2985
|
+
const refresh = url.searchParams.get("refresh") === "1";
|
|
2986
|
+
if (refresh)
|
|
2987
|
+
index.revalidate(root);
|
|
2988
|
+
// A warm picker can use known results while refresh works through the remaining paths.
|
|
2989
|
+
if (!quick)
|
|
2990
|
+
await index.waitUntilIndexed(4000);
|
|
2991
|
+
else if (!refresh && !index.hasSnapshot)
|
|
2992
|
+
await index.waitUntilIndexed(800);
|
|
2996
2993
|
index.throwIfBroken();
|
|
2997
|
-
// WAIT FOR THE INDEX, briefly. A search answered from a half-built map would say "no
|
|
2998
|
-
// matches" for files that are on disk - which is precisely the "搜不出他想搜的东西"
|
|
2999
|
-
// failure this work exists to remove. Past the deadline the answer is still returned, with
|
|
3000
|
-
// `indexed: false` so the UI can say the index is still building rather than pretending
|
|
3001
|
-
// the tree is empty.
|
|
3002
2994
|
const linksOnly = url.searchParams.get("links") === "1";
|
|
3003
|
-
const
|
|
3004
|
-
const
|
|
2995
|
+
const offset = page * QUICK_OPEN_PAGE_SIZE;
|
|
2996
|
+
const result = quick ? index.searchQuickOpen(query, offset) : index.search(query, linksOnly);
|
|
2997
|
+
const hits = !quick && linksOnly ? result.hits.slice(0, 20) : result.hits;
|
|
3005
2998
|
json(200, {
|
|
3006
2999
|
mode: "search",
|
|
3007
3000
|
query: query.trim(),
|
|
3008
3001
|
hits: hits.map(({ path, name, directory, openLink }) => ({ path, name, directory, ...(openLink ? { openLink } : {}) })),
|
|
3009
3002
|
total: result.total,
|
|
3010
|
-
truncated: hits.length < result.total,
|
|
3003
|
+
truncated: (quick ? offset : 0) + hits.length < result.total,
|
|
3011
3004
|
skipped: [],
|
|
3012
3005
|
indexed: result.indexed,
|
|
3013
3006
|
progress: result.progress,
|
|
@@ -9,6 +9,7 @@ export interface MentionToken {
|
|
|
9
9
|
}
|
|
10
10
|
/** Only a standalone @ token opens completion; email addresses and escaped @ stay literal. */
|
|
11
11
|
export declare function findMentionToken(text: string, caret: number, selectionEnd?: number): MentionToken | undefined;
|
|
12
|
+
export declare function rankPathMatches<T extends PathMentionEntry>(entries: Iterable<T>, query: string): T[];
|
|
12
13
|
export declare function matchPathMentions<T extends PathMentionEntry>(entries: Iterable<T>, query: string, limit?: number): T[];
|
|
13
14
|
export declare function insertPathMention(text: string, token: MentionToken, entry: PathMentionEntry): {
|
|
14
15
|
text: string;
|
|
@@ -10,42 +10,78 @@ export function findMentionToken(text, caret, selectionEnd = caret) {
|
|
|
10
10
|
const remaining = match[1] !== undefined ? /^[^"\n]*(?:"|$)/u.exec(tail)?.[0] : /^[^\s@"\n]*/u.exec(tail)?.[0];
|
|
11
11
|
return { start, end: caret + (remaining?.length ?? 0), query: match[1] ?? match[2] ?? "" };
|
|
12
12
|
}
|
|
13
|
-
|
|
13
|
+
var MatchRank;
|
|
14
|
+
(function (MatchRank) {
|
|
15
|
+
MatchRank[MatchRank["ExactPath"] = 0] = "ExactPath";
|
|
16
|
+
MatchRank[MatchRank["FoldedPath"] = 1] = "FoldedPath";
|
|
17
|
+
MatchRank[MatchRank["ExactName"] = 2] = "ExactName";
|
|
18
|
+
MatchRank[MatchRank["NamePrefix"] = 3] = "NamePrefix";
|
|
19
|
+
MatchRank[MatchRank["NameSubstring"] = 4] = "NameSubstring";
|
|
20
|
+
MatchRank[MatchRank["NameSubsequence"] = 5] = "NameSubsequence";
|
|
21
|
+
MatchRank[MatchRank["PathSubstring"] = 6] = "PathSubstring";
|
|
22
|
+
MatchRank[MatchRank["PathSubsequence"] = 7] = "PathSubsequence";
|
|
23
|
+
})(MatchRank || (MatchRank = {}));
|
|
24
|
+
function subsequenceScore(text, query, rank) {
|
|
25
|
+
let start = -1;
|
|
26
|
+
let end = 0;
|
|
27
|
+
for (const character of query) {
|
|
28
|
+
const position = text.indexOf(character, end);
|
|
29
|
+
if (position < 0)
|
|
30
|
+
return;
|
|
31
|
+
if (start < 0)
|
|
32
|
+
start = position;
|
|
33
|
+
end = position + character.length;
|
|
34
|
+
}
|
|
35
|
+
// Count gaps within the match. Long directories should never improve a sparse match.
|
|
36
|
+
return [rank, end - start - query.length, start];
|
|
37
|
+
}
|
|
14
38
|
function fuzzyScore(path, query) {
|
|
15
|
-
const name = path.slice(path.lastIndexOf("/") + 1);
|
|
16
39
|
if (path === query)
|
|
17
|
-
return
|
|
40
|
+
return [MatchRank.FoldedPath];
|
|
41
|
+
const name = path.slice(path.lastIndexOf("/") + 1);
|
|
18
42
|
if (name === query)
|
|
19
|
-
return
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return undefined;
|
|
31
|
-
gaps += next - position - 1;
|
|
32
|
-
position = next;
|
|
33
|
-
}
|
|
34
|
-
return 50 + gaps / Math.max(1, path.length);
|
|
43
|
+
return [MatchRank.ExactName];
|
|
44
|
+
const nameStart = name.indexOf(query);
|
|
45
|
+
if (nameStart === 0)
|
|
46
|
+
return [MatchRank.NamePrefix, name.length];
|
|
47
|
+
if (nameStart > 0)
|
|
48
|
+
return [MatchRank.NameSubstring, nameStart, name.length];
|
|
49
|
+
const nameMatch = subsequenceScore(name, query, MatchRank.NameSubsequence);
|
|
50
|
+
if (nameMatch)
|
|
51
|
+
return nameMatch;
|
|
52
|
+
const pathStart = path.indexOf(query);
|
|
53
|
+
return pathStart >= 0 ? [MatchRank.PathSubstring, pathStart] : subsequenceScore(path, query, MatchRank.PathSubsequence);
|
|
35
54
|
}
|
|
36
|
-
|
|
37
|
-
|
|
55
|
+
function compareScores(a, b) {
|
|
56
|
+
return a[0] - b[0] || (a[1] ?? 0) - (b[1] ?? 0) || (a[2] ?? 0) - (b[2] ?? 0);
|
|
57
|
+
}
|
|
58
|
+
export function rankPathMatches(entries, query) {
|
|
59
|
+
const normalized = query.trim().replaceAll("\\", "/");
|
|
60
|
+
const needle = normalized.toLowerCase();
|
|
38
61
|
const matches = [];
|
|
39
62
|
for (const entry of entries) {
|
|
40
63
|
if (!entry.path || entry.path.startsWith("/") || /^[a-z]:/iu.test(entry.path) || entry.path.split("/").includes(".."))
|
|
41
64
|
continue;
|
|
42
65
|
const path = entry.path.toLowerCase();
|
|
43
|
-
const score = needle
|
|
66
|
+
const score = !needle || entry.path === normalized
|
|
67
|
+
? [MatchRank.ExactPath] : fuzzyScore(path, needle);
|
|
44
68
|
if (score !== undefined)
|
|
45
69
|
matches.push({ entry, score, depth: entry.path.split("/").length });
|
|
46
70
|
}
|
|
47
|
-
matches.sort((a, b) =>
|
|
48
|
-
|
|
71
|
+
matches.sort((a, b) => {
|
|
72
|
+
const order = compareScores(a.score, b.score);
|
|
73
|
+
if (order)
|
|
74
|
+
return order;
|
|
75
|
+
const directoriesFirst = Number(b.entry.kind === "directory") - Number(a.entry.kind === "directory");
|
|
76
|
+
// Keep files ahead of tied fuzzy directory suggestions in path mentions.
|
|
77
|
+
return (a.score[0] === MatchRank.PathSubsequence ? -directoriesFirst : 0)
|
|
78
|
+
|| a.depth - b.depth || directoriesFirst
|
|
79
|
+
|| (a.entry.path < b.entry.path ? -1 : a.entry.path > b.entry.path ? 1 : 0);
|
|
80
|
+
});
|
|
81
|
+
return matches.map(item => item.entry);
|
|
82
|
+
}
|
|
83
|
+
export function matchPathMentions(entries, query, limit = 30) {
|
|
84
|
+
return rankPathMatches(entries, query).slice(0, Math.max(0, Math.min(100, limit)));
|
|
49
85
|
}
|
|
50
86
|
export function insertPathMention(text, token, entry) {
|
|
51
87
|
const path = entry.path + (entry.kind === "directory" && !entry.path.endsWith("/") ? "/" : "");
|
|
@@ -12,6 +12,10 @@ export type GitGraphCommit = {
|
|
|
12
12
|
date: string;
|
|
13
13
|
refs: GitGraphRef[];
|
|
14
14
|
};
|
|
15
|
+
export type GitGraphResponse = GitGraphPage | {
|
|
16
|
+
unchanged: true;
|
|
17
|
+
snapshot: string;
|
|
18
|
+
};
|
|
15
19
|
export type GitGraphPage = {
|
|
16
20
|
commits: GitGraphCommit[];
|
|
17
21
|
snapshot: string;
|