@clinebot/core 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +88 -0
- package/dist/account/cline-account-service.d.ts +34 -0
- package/dist/account/index.d.ts +3 -0
- package/dist/account/rpc.d.ts +38 -0
- package/dist/account/types.d.ts +74 -0
- package/dist/agents/agent-config-loader.d.ts +18 -0
- package/dist/agents/agent-config-parser.d.ts +25 -0
- package/dist/agents/hooks-config-loader.d.ts +23 -0
- package/dist/agents/index.d.ts +11 -0
- package/dist/agents/plugin-config-loader.d.ts +22 -0
- package/dist/agents/plugin-loader.d.ts +9 -0
- package/dist/agents/plugin-sandbox.d.ts +12 -0
- package/dist/agents/unified-config-file-watcher.d.ts +77 -0
- package/dist/agents/user-instruction-config-loader.d.ts +63 -0
- package/dist/auth/client.d.ts +11 -0
- package/dist/auth/cline.d.ts +41 -0
- package/dist/auth/codex.d.ts +39 -0
- package/dist/auth/oca.d.ts +22 -0
- package/dist/auth/server.d.ts +22 -0
- package/dist/auth/types.d.ts +72 -0
- package/dist/auth/utils.d.ts +32 -0
- package/dist/chat/chat-schema.d.ts +145 -0
- package/dist/default-tools/constants.d.ts +23 -0
- package/dist/default-tools/definitions.d.ts +96 -0
- package/dist/default-tools/executors/apply-patch-parser.d.ts +68 -0
- package/dist/default-tools/executors/apply-patch.d.ts +26 -0
- package/dist/default-tools/executors/bash.d.ts +49 -0
- package/dist/default-tools/executors/editor.d.ts +31 -0
- package/dist/default-tools/executors/file-read.d.ts +40 -0
- package/dist/default-tools/executors/index.d.ts +44 -0
- package/dist/default-tools/executors/search.d.ts +50 -0
- package/dist/default-tools/executors/web-fetch.d.ts +58 -0
- package/dist/default-tools/index.d.ts +57 -0
- package/dist/default-tools/presets.d.ts +124 -0
- package/dist/default-tools/schemas.d.ts +121 -0
- package/dist/default-tools/types.d.ts +237 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +220 -0
- package/dist/input/file-indexer.d.ts +5 -0
- package/dist/input/index.d.ts +4 -0
- package/dist/input/mention-enricher.d.ts +12 -0
- package/dist/mcp/config-loader.d.ts +15 -0
- package/dist/mcp/index.d.ts +4 -0
- package/dist/mcp/manager.d.ts +24 -0
- package/dist/mcp/types.d.ts +66 -0
- package/dist/runtime/hook-file-hooks.d.ts +18 -0
- package/dist/runtime/rules.d.ts +5 -0
- package/dist/runtime/runtime-builder.d.ts +5 -0
- package/dist/runtime/sandbox/subprocess-sandbox.d.ts +19 -0
- package/dist/runtime/session-runtime.d.ts +36 -0
- package/dist/runtime/tool-approval.d.ts +9 -0
- package/dist/runtime/workflows.d.ts +13 -0
- package/dist/server/index.d.ts +47 -0
- package/dist/server/index.js +641 -0
- package/dist/session/default-session-manager.d.ts +77 -0
- package/dist/session/rpc-session-service.d.ts +12 -0
- package/dist/session/runtime-oauth-token-manager.d.ts +28 -0
- package/dist/session/session-artifacts.d.ts +19 -0
- package/dist/session/session-graph.d.ts +15 -0
- package/dist/session/session-host.d.ts +21 -0
- package/dist/session/session-manager.d.ts +50 -0
- package/dist/session/session-manifest.d.ts +30 -0
- package/dist/session/session-service.d.ts +113 -0
- package/dist/session/sqlite-rpc-session-backend.d.ts +30 -0
- package/dist/session/unified-session-persistence-service.d.ts +93 -0
- package/dist/session/workspace-manager.d.ts +28 -0
- package/dist/session/workspace-manifest.d.ts +25 -0
- package/dist/storage/provider-settings-legacy-migration.d.ts +13 -0
- package/dist/storage/provider-settings-manager.d.ts +20 -0
- package/dist/storage/sqlite-session-store.d.ts +29 -0
- package/dist/storage/sqlite-team-store.d.ts +31 -0
- package/dist/storage/team-store.d.ts +2 -0
- package/dist/team/index.d.ts +1 -0
- package/dist/team/projections.d.ts +8 -0
- package/dist/types/common.d.ts +10 -0
- package/dist/types/config.d.ts +37 -0
- package/dist/types/events.d.ts +54 -0
- package/dist/types/provider-settings.d.ts +20 -0
- package/dist/types/sessions.d.ts +9 -0
- package/dist/types/storage.d.ts +37 -0
- package/dist/types/workspace.d.ts +7 -0
- package/dist/types.d.ts +26 -0
- package/package.json +63 -0
- package/src/account/cline-account-service.test.ts +101 -0
- package/src/account/cline-account-service.ts +267 -0
- package/src/account/index.ts +20 -0
- package/src/account/rpc.test.ts +62 -0
- package/src/account/rpc.ts +172 -0
- package/src/account/types.ts +80 -0
- package/src/agents/agent-config-loader.test.ts +234 -0
- package/src/agents/agent-config-loader.ts +107 -0
- package/src/agents/agent-config-parser.ts +191 -0
- package/src/agents/hooks-config-loader.ts +97 -0
- package/src/agents/index.ts +84 -0
- package/src/agents/plugin-config-loader.test.ts +91 -0
- package/src/agents/plugin-config-loader.ts +160 -0
- package/src/agents/plugin-loader.test.ts +102 -0
- package/src/agents/plugin-loader.ts +105 -0
- package/src/agents/plugin-sandbox.test.ts +120 -0
- package/src/agents/plugin-sandbox.ts +471 -0
- package/src/agents/unified-config-file-watcher.test.ts +196 -0
- package/src/agents/unified-config-file-watcher.ts +483 -0
- package/src/agents/user-instruction-config-loader.test.ts +158 -0
- package/src/agents/user-instruction-config-loader.ts +438 -0
- package/src/auth/client.test.ts +40 -0
- package/src/auth/client.ts +25 -0
- package/src/auth/cline.test.ts +130 -0
- package/src/auth/cline.ts +414 -0
- package/src/auth/codex.test.ts +170 -0
- package/src/auth/codex.ts +466 -0
- package/src/auth/oca.test.ts +215 -0
- package/src/auth/oca.ts +546 -0
- package/src/auth/server.ts +216 -0
- package/src/auth/types.ts +78 -0
- package/src/auth/utils.test.ts +128 -0
- package/src/auth/utils.ts +247 -0
- package/src/chat/chat-schema.ts +82 -0
- package/src/default-tools/constants.ts +35 -0
- package/src/default-tools/definitions.test.ts +233 -0
- package/src/default-tools/definitions.ts +632 -0
- package/src/default-tools/executors/apply-patch-parser.ts +520 -0
- package/src/default-tools/executors/apply-patch.ts +359 -0
- package/src/default-tools/executors/bash.ts +205 -0
- package/src/default-tools/executors/editor.ts +231 -0
- package/src/default-tools/executors/file-read.test.ts +25 -0
- package/src/default-tools/executors/file-read.ts +94 -0
- package/src/default-tools/executors/index.ts +75 -0
- package/src/default-tools/executors/search.ts +278 -0
- package/src/default-tools/executors/web-fetch.ts +259 -0
- package/src/default-tools/index.ts +161 -0
- package/src/default-tools/presets.test.ts +63 -0
- package/src/default-tools/presets.ts +168 -0
- package/src/default-tools/schemas.ts +228 -0
- package/src/default-tools/types.ts +324 -0
- package/src/index.ts +119 -0
- package/src/input/file-indexer.d.ts +11 -0
- package/src/input/file-indexer.test.ts +87 -0
- package/src/input/file-indexer.ts +280 -0
- package/src/input/index.ts +7 -0
- package/src/input/mention-enricher.test.ts +82 -0
- package/src/input/mention-enricher.ts +119 -0
- package/src/mcp/config-loader.test.ts +238 -0
- package/src/mcp/config-loader.ts +219 -0
- package/src/mcp/index.ts +26 -0
- package/src/mcp/manager.test.ts +106 -0
- package/src/mcp/manager.ts +262 -0
- package/src/mcp/types.ts +88 -0
- package/src/runtime/hook-file-hooks.test.ts +106 -0
- package/src/runtime/hook-file-hooks.ts +736 -0
- package/src/runtime/index.ts +27 -0
- package/src/runtime/rules.ts +34 -0
- package/src/runtime/runtime-builder.team-persistence.test.ts +203 -0
- package/src/runtime/runtime-builder.test.ts +215 -0
- package/src/runtime/runtime-builder.ts +515 -0
- package/src/runtime/runtime-parity.test.ts +132 -0
- package/src/runtime/sandbox/subprocess-sandbox.ts +207 -0
- package/src/runtime/session-runtime.ts +44 -0
- package/src/runtime/tool-approval.ts +104 -0
- package/src/runtime/workflows.test.ts +119 -0
- package/src/runtime/workflows.ts +54 -0
- package/src/server/index.ts +282 -0
- package/src/session/default-session-manager.e2e.test.ts +354 -0
- package/src/session/default-session-manager.test.ts +816 -0
- package/src/session/default-session-manager.ts +1286 -0
- package/src/session/index.ts +37 -0
- package/src/session/rpc-session-service.ts +189 -0
- package/src/session/runtime-oauth-token-manager.test.ts +137 -0
- package/src/session/runtime-oauth-token-manager.ts +265 -0
- package/src/session/session-artifacts.ts +106 -0
- package/src/session/session-graph.ts +90 -0
- package/src/session/session-host.ts +190 -0
- package/src/session/session-manager.ts +56 -0
- package/src/session/session-manifest.ts +29 -0
- package/src/session/session-service.team-persistence.test.ts +48 -0
- package/src/session/session-service.ts +610 -0
- package/src/session/sqlite-rpc-session-backend.ts +303 -0
- package/src/session/unified-session-persistence-service.ts +781 -0
- package/src/session/workspace-manager.ts +98 -0
- package/src/session/workspace-manifest.ts +100 -0
- package/src/storage/artifact-store.ts +1 -0
- package/src/storage/index.ts +11 -0
- package/src/storage/provider-settings-legacy-migration.test.ts +175 -0
- package/src/storage/provider-settings-legacy-migration.ts +637 -0
- package/src/storage/provider-settings-manager.test.ts +111 -0
- package/src/storage/provider-settings-manager.ts +129 -0
- package/src/storage/session-store.ts +1 -0
- package/src/storage/sqlite-session-store.ts +270 -0
- package/src/storage/sqlite-team-store.ts +443 -0
- package/src/storage/team-store.ts +5 -0
- package/src/team/index.ts +4 -0
- package/src/team/projections.ts +285 -0
- package/src/types/common.ts +14 -0
- package/src/types/config.ts +64 -0
- package/src/types/events.ts +46 -0
- package/src/types/index.ts +24 -0
- package/src/types/provider-settings.ts +43 -0
- package/src/types/sessions.ts +16 -0
- package/src/types/storage.ts +64 -0
- package/src/types/workspace.ts +7 -0
- package/src/types.ts +127 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { describe, expect, it, vi } from "vitest";
|
|
5
|
+
import { getFileIndex, prewarmFileIndex } from "./file-indexer";
|
|
6
|
+
|
|
7
|
+
vi.mock("node:worker_threads", async () => {
|
|
8
|
+
const actual = await vi.importActual<typeof import("node:worker_threads")>(
|
|
9
|
+
"node:worker_threads",
|
|
10
|
+
);
|
|
11
|
+
return {
|
|
12
|
+
...actual,
|
|
13
|
+
isMainThread: false,
|
|
14
|
+
parentPort: null,
|
|
15
|
+
};
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
async function createTempWorkspace(): Promise<string> {
|
|
19
|
+
return mkdtemp(path.join(os.tmpdir(), "core-file-index-"));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
describe("file indexer", () => {
|
|
23
|
+
it("indexes files by relative posix path", async () => {
|
|
24
|
+
const cwd = await createTempWorkspace();
|
|
25
|
+
try {
|
|
26
|
+
await mkdir(path.join(cwd, "src"), { recursive: true });
|
|
27
|
+
await writeFile(path.join(cwd, "src", "main.ts"), "export {}\n", "utf8");
|
|
28
|
+
await writeFile(path.join(cwd, "README.md"), "# Demo\n", "utf8");
|
|
29
|
+
|
|
30
|
+
const index = await getFileIndex(cwd, { ttlMs: 0 });
|
|
31
|
+
expect(index.has("src/main.ts")).toBe(true);
|
|
32
|
+
expect(index.has("README.md")).toBe(true);
|
|
33
|
+
} finally {
|
|
34
|
+
await rm(cwd, { recursive: true, force: true });
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("always excludes .git files from index", async () => {
|
|
39
|
+
const cwd = await createTempWorkspace();
|
|
40
|
+
try {
|
|
41
|
+
await mkdir(path.join(cwd, ".git"), { recursive: true });
|
|
42
|
+
await mkdir(path.join(cwd, "node_modules", "pkg"), { recursive: true });
|
|
43
|
+
await writeFile(path.join(cwd, ".git", "config"), "[core]\n", "utf8");
|
|
44
|
+
await writeFile(
|
|
45
|
+
path.join(cwd, "node_modules", "pkg", "index.js"),
|
|
46
|
+
"module.exports = {}\n",
|
|
47
|
+
"utf8",
|
|
48
|
+
);
|
|
49
|
+
await writeFile(
|
|
50
|
+
path.join(cwd, "app.ts"),
|
|
51
|
+
"export const app = 1\n",
|
|
52
|
+
"utf8",
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const index = await getFileIndex(cwd, { ttlMs: 0 });
|
|
56
|
+
expect(index.has("app.ts")).toBe(true);
|
|
57
|
+
expect(index.has(".git/config")).toBe(false);
|
|
58
|
+
} finally {
|
|
59
|
+
await rm(cwd, { recursive: true, force: true });
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("prewarm rebuilds index and includes new files", async () => {
|
|
64
|
+
const cwd = await createTempWorkspace();
|
|
65
|
+
try {
|
|
66
|
+
await writeFile(
|
|
67
|
+
path.join(cwd, "first.ts"),
|
|
68
|
+
"export const first = 1\n",
|
|
69
|
+
"utf8",
|
|
70
|
+
);
|
|
71
|
+
const firstIndex = await getFileIndex(cwd, { ttlMs: 60_000 });
|
|
72
|
+
expect(firstIndex.has("first.ts")).toBe(true);
|
|
73
|
+
|
|
74
|
+
await writeFile(
|
|
75
|
+
path.join(cwd, "second.ts"),
|
|
76
|
+
"export const second = 2\n",
|
|
77
|
+
"utf8",
|
|
78
|
+
);
|
|
79
|
+
await prewarmFileIndex(cwd, { ttlMs: 60_000 });
|
|
80
|
+
|
|
81
|
+
const rebuilt = await getFileIndex(cwd, { ttlMs: 60_000 });
|
|
82
|
+
expect(rebuilt.has("second.ts")).toBe(true);
|
|
83
|
+
} finally {
|
|
84
|
+
await rm(cwd, { recursive: true, force: true });
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { readdir } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { isMainThread, parentPort, Worker } from "node:worker_threads";
|
|
5
|
+
|
|
6
|
+
const DEFAULT_INDEX_TTL_MS = 15_000;
|
|
7
|
+
const DEFAULT_EXCLUDE_DIRS = new Set([
|
|
8
|
+
".git",
|
|
9
|
+
"node_modules",
|
|
10
|
+
"dist",
|
|
11
|
+
"build",
|
|
12
|
+
".next",
|
|
13
|
+
"coverage",
|
|
14
|
+
".turbo",
|
|
15
|
+
".cache",
|
|
16
|
+
"target",
|
|
17
|
+
"out",
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
interface CacheEntry {
|
|
21
|
+
files: Set<string>;
|
|
22
|
+
lastBuiltAt: number;
|
|
23
|
+
pending: Promise<Set<string>> | null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface FastFileIndexOptions {
|
|
27
|
+
ttlMs?: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface IndexRequestMessage {
|
|
31
|
+
type: "index";
|
|
32
|
+
requestId: number;
|
|
33
|
+
cwd: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface IndexResponseMessage {
|
|
37
|
+
type: "indexResult";
|
|
38
|
+
requestId: number;
|
|
39
|
+
files?: string[];
|
|
40
|
+
error?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const CACHE = new Map<string, CacheEntry>();
|
|
44
|
+
|
|
45
|
+
function toPosixRelative(cwd: string, absolutePath: string): string {
|
|
46
|
+
return path.relative(cwd, absolutePath).split(path.sep).join("/");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function listFilesWithRg(cwd: string): Promise<Set<string>> {
|
|
50
|
+
const output = await new Promise<string>((resolve, reject) => {
|
|
51
|
+
const child = spawn("rg", ["--files", "--hidden", "-g", "!.git"], {
|
|
52
|
+
cwd,
|
|
53
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
let stdout = "";
|
|
57
|
+
let stderr = "";
|
|
58
|
+
|
|
59
|
+
child.stdout.on("data", (chunk: Buffer | string) => {
|
|
60
|
+
stdout += chunk.toString();
|
|
61
|
+
});
|
|
62
|
+
child.stderr.on("data", (chunk: Buffer | string) => {
|
|
63
|
+
stderr += chunk.toString();
|
|
64
|
+
});
|
|
65
|
+
child.on("error", reject);
|
|
66
|
+
child.on("close", (code: number | null) => {
|
|
67
|
+
if (code === 0) {
|
|
68
|
+
resolve(stdout);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
reject(new Error(stderr || `rg exited with code ${code}`));
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const files = output
|
|
76
|
+
.split(/\r?\n/)
|
|
77
|
+
.map((line) => line.trim())
|
|
78
|
+
.filter((line) => line.length > 0)
|
|
79
|
+
.map((line) => line.replace(/\\/g, "/"));
|
|
80
|
+
|
|
81
|
+
return new Set(files);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function walkDir(
|
|
85
|
+
cwd: string,
|
|
86
|
+
dir: string,
|
|
87
|
+
files: Set<string>,
|
|
88
|
+
): Promise<void> {
|
|
89
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
90
|
+
for (const entry of entries) {
|
|
91
|
+
const absolutePath = path.join(dir, entry.name);
|
|
92
|
+
if (entry.isDirectory()) {
|
|
93
|
+
if (DEFAULT_EXCLUDE_DIRS.has(entry.name)) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
await walkDir(cwd, absolutePath, files);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (entry.isFile()) {
|
|
100
|
+
files.add(toPosixRelative(cwd, absolutePath));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function listFilesFallback(cwd: string): Promise<Set<string>> {
|
|
106
|
+
const files = new Set<string>();
|
|
107
|
+
await walkDir(cwd, cwd, files);
|
|
108
|
+
return files;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function buildIndex(cwd: string): Promise<Set<string>> {
|
|
112
|
+
try {
|
|
113
|
+
return await listFilesWithRg(cwd);
|
|
114
|
+
} catch {
|
|
115
|
+
return listFilesFallback(cwd);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function startWorkerServer(): void {
|
|
120
|
+
if (isMainThread || !parentPort) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const port = parentPort;
|
|
124
|
+
|
|
125
|
+
port.on("message", (message: IndexRequestMessage) => {
|
|
126
|
+
if (message.type !== "index") {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
void buildIndex(message.cwd)
|
|
131
|
+
.then((files) => {
|
|
132
|
+
const response: IndexResponseMessage = {
|
|
133
|
+
type: "indexResult",
|
|
134
|
+
requestId: message.requestId,
|
|
135
|
+
files: Array.from(files),
|
|
136
|
+
};
|
|
137
|
+
port.postMessage(response);
|
|
138
|
+
})
|
|
139
|
+
.catch((error: unknown) => {
|
|
140
|
+
const response: IndexResponseMessage = {
|
|
141
|
+
type: "indexResult",
|
|
142
|
+
requestId: message.requestId,
|
|
143
|
+
error:
|
|
144
|
+
error instanceof Error
|
|
145
|
+
? error.message
|
|
146
|
+
: "Failed to build file index",
|
|
147
|
+
};
|
|
148
|
+
port.postMessage(response);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
class FileIndexWorkerClient {
|
|
154
|
+
private readonly worker = new Worker(new URL(import.meta.url));
|
|
155
|
+
private nextRequestId = 0;
|
|
156
|
+
private pending = new Map<
|
|
157
|
+
number,
|
|
158
|
+
{
|
|
159
|
+
resolve: (files: string[]) => void;
|
|
160
|
+
reject: (reason: Error) => void;
|
|
161
|
+
}
|
|
162
|
+
>();
|
|
163
|
+
|
|
164
|
+
constructor() {
|
|
165
|
+
// Keep indexing opportunistic: this worker should never block process exit.
|
|
166
|
+
this.worker.unref();
|
|
167
|
+
this.worker.on("message", (message: IndexResponseMessage) => {
|
|
168
|
+
if (message.type !== "indexResult") {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const request = this.pending.get(message.requestId);
|
|
172
|
+
if (!request) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
this.pending.delete(message.requestId);
|
|
176
|
+
if (message.error) {
|
|
177
|
+
request.reject(new Error(message.error));
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
request.resolve(message.files ?? []);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
this.worker.on("error", (error: Error) => {
|
|
184
|
+
this.flushPending(error);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
this.worker.on("exit", (code) => {
|
|
188
|
+
if (code !== 0) {
|
|
189
|
+
this.flushPending(
|
|
190
|
+
new Error(`File index worker exited with code ${code}`),
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
requestIndex(cwd: string): Promise<string[]> {
|
|
197
|
+
const requestId = ++this.nextRequestId;
|
|
198
|
+
const result = new Promise<string[]>((resolve, reject) => {
|
|
199
|
+
this.pending.set(requestId, { resolve, reject });
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const message: IndexRequestMessage = {
|
|
203
|
+
type: "index",
|
|
204
|
+
requestId,
|
|
205
|
+
cwd,
|
|
206
|
+
};
|
|
207
|
+
this.worker.postMessage(message);
|
|
208
|
+
return result;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
private flushPending(error: Error): void {
|
|
212
|
+
for (const [requestId, request] of this.pending.entries()) {
|
|
213
|
+
request.reject(error);
|
|
214
|
+
this.pending.delete(requestId);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
startWorkerServer();
|
|
220
|
+
|
|
221
|
+
const workerClient = isMainThread ? new FileIndexWorkerClient() : null;
|
|
222
|
+
|
|
223
|
+
async function buildIndexInBackground(cwd: string): Promise<Set<string>> {
|
|
224
|
+
if (!workerClient) {
|
|
225
|
+
return buildIndex(cwd);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
try {
|
|
229
|
+
const files = await workerClient.requestIndex(cwd);
|
|
230
|
+
return new Set(files);
|
|
231
|
+
} catch {
|
|
232
|
+
return buildIndex(cwd);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export async function getFileIndex(
|
|
237
|
+
cwd: string,
|
|
238
|
+
options: FastFileIndexOptions = {},
|
|
239
|
+
): Promise<Set<string>> {
|
|
240
|
+
const ttlMs = options.ttlMs ?? DEFAULT_INDEX_TTL_MS;
|
|
241
|
+
const now = Date.now();
|
|
242
|
+
const existing = CACHE.get(cwd);
|
|
243
|
+
|
|
244
|
+
if (
|
|
245
|
+
existing &&
|
|
246
|
+
ttlMs > 0 &&
|
|
247
|
+
now - existing.lastBuiltAt <= ttlMs &&
|
|
248
|
+
existing.files.size > 0
|
|
249
|
+
) {
|
|
250
|
+
return existing.files;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (existing?.pending) {
|
|
254
|
+
return existing.pending;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const pending = buildIndexInBackground(cwd).then((files) => {
|
|
258
|
+
CACHE.set(cwd, {
|
|
259
|
+
files,
|
|
260
|
+
lastBuiltAt: Date.now(),
|
|
261
|
+
pending: null,
|
|
262
|
+
});
|
|
263
|
+
return files;
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
CACHE.set(cwd, {
|
|
267
|
+
files: existing?.files ?? new Set<string>(),
|
|
268
|
+
lastBuiltAt: existing?.lastBuiltAt ?? 0,
|
|
269
|
+
pending,
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
return pending;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export async function prewarmFileIndex(
|
|
276
|
+
cwd: string,
|
|
277
|
+
options: FastFileIndexOptions = {},
|
|
278
|
+
): Promise<void> {
|
|
279
|
+
await getFileIndex(cwd, { ...options, ttlMs: 0 });
|
|
280
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type { FastFileIndexOptions } from "./file-indexer";
|
|
2
|
+
export { getFileIndex, prewarmFileIndex } from "./file-indexer";
|
|
3
|
+
export type {
|
|
4
|
+
MentionEnricherOptions,
|
|
5
|
+
MentionEnrichmentResult,
|
|
6
|
+
} from "./mention-enricher";
|
|
7
|
+
export { enrichPromptWithMentions } from "./mention-enricher";
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { describe, expect, it, vi } from "vitest";
|
|
5
|
+
import { enrichPromptWithMentions } from "./mention-enricher";
|
|
6
|
+
|
|
7
|
+
vi.mock("node:worker_threads", async () => {
|
|
8
|
+
const actual = await vi.importActual<typeof import("node:worker_threads")>(
|
|
9
|
+
"node:worker_threads",
|
|
10
|
+
);
|
|
11
|
+
return {
|
|
12
|
+
...actual,
|
|
13
|
+
isMainThread: false,
|
|
14
|
+
parentPort: null,
|
|
15
|
+
};
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
async function createTempWorkspace(): Promise<string> {
|
|
19
|
+
return mkdtemp(path.join(os.tmpdir(), "core-mentions-"));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
describe("enrichPromptWithMentions", () => {
|
|
23
|
+
it("returns matched files for matching @path mentions", async () => {
|
|
24
|
+
const cwd = await createTempWorkspace();
|
|
25
|
+
try {
|
|
26
|
+
const sourcePath = path.join(cwd, "src", "index.ts");
|
|
27
|
+
await mkdir(path.dirname(sourcePath), { recursive: true });
|
|
28
|
+
await writeFile(sourcePath, "export const answer = 42\n", "utf8");
|
|
29
|
+
|
|
30
|
+
const result = await enrichPromptWithMentions(
|
|
31
|
+
"Review @src/index.ts",
|
|
32
|
+
cwd,
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
expect(result.matchedFiles).toEqual(["src/index.ts"]);
|
|
36
|
+
expect(result.ignoredMentions).toEqual([]);
|
|
37
|
+
expect(result.prompt).toBe("Review @src/index.ts");
|
|
38
|
+
} finally {
|
|
39
|
+
await rm(cwd, { recursive: true, force: true });
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("ignores emails and unmatched mentions", async () => {
|
|
44
|
+
const cwd = await createTempWorkspace();
|
|
45
|
+
try {
|
|
46
|
+
await writeFile(path.join(cwd, "README.md"), "# Demo\n", "utf8");
|
|
47
|
+
|
|
48
|
+
const result = await enrichPromptWithMentions(
|
|
49
|
+
"Ping me at test@example.com and check @missing/file.ts.",
|
|
50
|
+
cwd,
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
expect(result.matchedFiles).toEqual([]);
|
|
54
|
+
expect(result.ignoredMentions).toEqual(["missing/file.ts"]);
|
|
55
|
+
expect(result.prompt).toBe(
|
|
56
|
+
"Ping me at test@example.com and check @missing/file.ts.",
|
|
57
|
+
);
|
|
58
|
+
} finally {
|
|
59
|
+
await rm(cwd, { recursive: true, force: true });
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("respects maxTotalBytes while keeping prompt unchanged", async () => {
|
|
64
|
+
const cwd = await createTempWorkspace();
|
|
65
|
+
try {
|
|
66
|
+
await writeFile(path.join(cwd, "a.ts"), "123", "utf8");
|
|
67
|
+
await writeFile(path.join(cwd, "b.ts"), "const b = 2\n", "utf8");
|
|
68
|
+
|
|
69
|
+
const result = await enrichPromptWithMentions(
|
|
70
|
+
"Use @a.ts and @b.ts",
|
|
71
|
+
cwd,
|
|
72
|
+
{ maxTotalBytes: 5, maxFiles: 2, maxFileBytes: 5 },
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
expect(result.matchedFiles).toEqual(["a.ts"]);
|
|
76
|
+
expect(result.ignoredMentions).toEqual(["b.ts"]);
|
|
77
|
+
expect(result.prompt).toBe("Use @a.ts and @b.ts");
|
|
78
|
+
} finally {
|
|
79
|
+
await rm(cwd, { recursive: true, force: true });
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { stat } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { type FastFileIndexOptions, getFileIndex } from "./file-indexer";
|
|
4
|
+
|
|
5
|
+
const TRAILING_PUNCTUATION = /[),.:;!?`'"]+$/;
|
|
6
|
+
const LEADING_WRAPPERS = /^[(`'"]+/;
|
|
7
|
+
|
|
8
|
+
export interface MentionEnricherOptions extends FastFileIndexOptions {
|
|
9
|
+
maxFiles?: number;
|
|
10
|
+
maxFileBytes?: number;
|
|
11
|
+
maxTotalBytes?: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface MentionEnrichmentResult {
|
|
15
|
+
prompt: string;
|
|
16
|
+
matchedFiles: string[];
|
|
17
|
+
ignoredMentions: string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function extractMentionTokens(input: string): string[] {
|
|
21
|
+
const matches = input.matchAll(/(^|[\s])@([^\s]+)/g);
|
|
22
|
+
const mentions: string[] = [];
|
|
23
|
+
for (const match of matches) {
|
|
24
|
+
const token = (match[2] ?? "").trim();
|
|
25
|
+
if (token.length === 0) {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
const normalized = token
|
|
29
|
+
.replace(LEADING_WRAPPERS, "")
|
|
30
|
+
.replace(TRAILING_PUNCTUATION, "");
|
|
31
|
+
if (normalized.length === 0 || normalized.includes("@")) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
mentions.push(normalized);
|
|
35
|
+
}
|
|
36
|
+
return Array.from(new Set(mentions));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function normalizeMentionPath(
|
|
40
|
+
mention: string,
|
|
41
|
+
cwd: string,
|
|
42
|
+
): string | undefined {
|
|
43
|
+
const candidate = mention.replace(/\\/g, "/");
|
|
44
|
+
const maybeAbsolute = path.isAbsolute(candidate)
|
|
45
|
+
? path.resolve(candidate)
|
|
46
|
+
: path.resolve(cwd, candidate);
|
|
47
|
+
const relative = path.relative(cwd, maybeAbsolute);
|
|
48
|
+
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
return relative.split(path.sep).join("/");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function enrichPromptWithMentions(
|
|
55
|
+
input: string,
|
|
56
|
+
cwd: string,
|
|
57
|
+
options: MentionEnricherOptions = {},
|
|
58
|
+
): Promise<MentionEnrichmentResult> {
|
|
59
|
+
const mentions = extractMentionTokens(input);
|
|
60
|
+
if (mentions.length === 0) {
|
|
61
|
+
return {
|
|
62
|
+
prompt: input,
|
|
63
|
+
matchedFiles: [],
|
|
64
|
+
ignoredMentions: [],
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const maxFiles = options.maxFiles;
|
|
69
|
+
const maxFileBytes = options.maxFileBytes;
|
|
70
|
+
const maxTotalBytes = options.maxTotalBytes;
|
|
71
|
+
const fileList = await getFileIndex(cwd, { ttlMs: options.ttlMs });
|
|
72
|
+
const matched: string[] = [];
|
|
73
|
+
const ignored: string[] = [];
|
|
74
|
+
const attachments: Array<{ path: string; content: string }> = [];
|
|
75
|
+
let totalBytes = 0;
|
|
76
|
+
|
|
77
|
+
for (const mention of mentions) {
|
|
78
|
+
if (maxFiles && attachments.length >= maxFiles) {
|
|
79
|
+
ignored.push(mention);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const relativePath = normalizeMentionPath(mention, cwd);
|
|
84
|
+
if (!relativePath || !fileList.has(relativePath)) {
|
|
85
|
+
ignored.push(mention);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!maxFileBytes || !maxTotalBytes) {
|
|
90
|
+
matched.push(relativePath);
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const absolutePath = path.join(cwd, relativePath);
|
|
95
|
+
try {
|
|
96
|
+
const fileStat = await stat(absolutePath);
|
|
97
|
+
if (!fileStat.isFile()) {
|
|
98
|
+
ignored.push(mention);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const nextBytes = totalBytes + maxFileBytes;
|
|
102
|
+
if (nextBytes > maxTotalBytes) {
|
|
103
|
+
ignored.push(mention);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
totalBytes += nextBytes;
|
|
108
|
+
matched.push(relativePath);
|
|
109
|
+
} catch {
|
|
110
|
+
ignored.push(mention);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
prompt: input,
|
|
116
|
+
matchedFiles: matched,
|
|
117
|
+
ignoredMentions: ignored,
|
|
118
|
+
};
|
|
119
|
+
}
|