opencode-codebase-index 0.7.0 → 0.8.1
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 +39 -7
- package/commands/index.md +4 -0
- package/commands/peek.md +24 -0
- package/commands/reindex.md +25 -0
- package/commands/status.md +5 -1
- package/dist/cli.cjs +2423 -730
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +2416 -723
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +2352 -711
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +2341 -700
- package/dist/index.js.map +1 -1
- package/native/codebase-index-native.darwin-arm64.node +0 -0
- package/native/codebase-index-native.darwin-x64.node +0 -0
- package/native/codebase-index-native.linux-arm64-gnu.node +0 -0
- package/native/codebase-index-native.linux-x64-gnu.node +0 -0
- package/native/codebase-index-native.win32-x64-msvc.node +0 -0
- package/package.json +3 -3
package/dist/cli.js
CHANGED
|
@@ -487,7 +487,7 @@ var require_ignore = __commonJS({
|
|
|
487
487
|
// path matching.
|
|
488
488
|
// - check `string` either `MODE_IGNORE` or `MODE_CHECK_IGNORE`
|
|
489
489
|
// @returns {TestResult} true if a file is ignored
|
|
490
|
-
test(
|
|
490
|
+
test(path13, checkUnignored, mode) {
|
|
491
491
|
let ignored = false;
|
|
492
492
|
let unignored = false;
|
|
493
493
|
let matchedRule;
|
|
@@ -496,7 +496,7 @@ var require_ignore = __commonJS({
|
|
|
496
496
|
if (unignored === negative && ignored !== unignored || negative && !ignored && !unignored && !checkUnignored) {
|
|
497
497
|
return;
|
|
498
498
|
}
|
|
499
|
-
const matched = rule[mode].test(
|
|
499
|
+
const matched = rule[mode].test(path13);
|
|
500
500
|
if (!matched) {
|
|
501
501
|
return;
|
|
502
502
|
}
|
|
@@ -517,17 +517,17 @@ var require_ignore = __commonJS({
|
|
|
517
517
|
var throwError = (message, Ctor) => {
|
|
518
518
|
throw new Ctor(message);
|
|
519
519
|
};
|
|
520
|
-
var checkPath = (
|
|
521
|
-
if (!isString(
|
|
520
|
+
var checkPath = (path13, originalPath, doThrow) => {
|
|
521
|
+
if (!isString(path13)) {
|
|
522
522
|
return doThrow(
|
|
523
523
|
`path must be a string, but got \`${originalPath}\``,
|
|
524
524
|
TypeError
|
|
525
525
|
);
|
|
526
526
|
}
|
|
527
|
-
if (!
|
|
527
|
+
if (!path13) {
|
|
528
528
|
return doThrow(`path must not be empty`, TypeError);
|
|
529
529
|
}
|
|
530
|
-
if (checkPath.isNotRelative(
|
|
530
|
+
if (checkPath.isNotRelative(path13)) {
|
|
531
531
|
const r = "`path.relative()`d";
|
|
532
532
|
return doThrow(
|
|
533
533
|
`path should be a ${r} string, but got "${originalPath}"`,
|
|
@@ -536,7 +536,7 @@ var require_ignore = __commonJS({
|
|
|
536
536
|
}
|
|
537
537
|
return true;
|
|
538
538
|
};
|
|
539
|
-
var isNotRelative = (
|
|
539
|
+
var isNotRelative = (path13) => REGEX_TEST_INVALID_PATH.test(path13);
|
|
540
540
|
checkPath.isNotRelative = isNotRelative;
|
|
541
541
|
checkPath.convert = (p) => p;
|
|
542
542
|
var Ignore2 = class {
|
|
@@ -566,19 +566,19 @@ var require_ignore = __commonJS({
|
|
|
566
566
|
}
|
|
567
567
|
// @returns {TestResult}
|
|
568
568
|
_test(originalPath, cache, checkUnignored, slices) {
|
|
569
|
-
const
|
|
569
|
+
const path13 = originalPath && checkPath.convert(originalPath);
|
|
570
570
|
checkPath(
|
|
571
|
-
|
|
571
|
+
path13,
|
|
572
572
|
originalPath,
|
|
573
573
|
this._strictPathCheck ? throwError : RETURN_FALSE
|
|
574
574
|
);
|
|
575
|
-
return this._t(
|
|
575
|
+
return this._t(path13, cache, checkUnignored, slices);
|
|
576
576
|
}
|
|
577
|
-
checkIgnore(
|
|
578
|
-
if (!REGEX_TEST_TRAILING_SLASH.test(
|
|
579
|
-
return this.test(
|
|
577
|
+
checkIgnore(path13) {
|
|
578
|
+
if (!REGEX_TEST_TRAILING_SLASH.test(path13)) {
|
|
579
|
+
return this.test(path13);
|
|
580
580
|
}
|
|
581
|
-
const slices =
|
|
581
|
+
const slices = path13.split(SLASH).filter(Boolean);
|
|
582
582
|
slices.pop();
|
|
583
583
|
if (slices.length) {
|
|
584
584
|
const parent = this._t(
|
|
@@ -591,18 +591,18 @@ var require_ignore = __commonJS({
|
|
|
591
591
|
return parent;
|
|
592
592
|
}
|
|
593
593
|
}
|
|
594
|
-
return this._rules.test(
|
|
594
|
+
return this._rules.test(path13, false, MODE_CHECK_IGNORE);
|
|
595
595
|
}
|
|
596
|
-
_t(
|
|
597
|
-
if (
|
|
598
|
-
return cache[
|
|
596
|
+
_t(path13, cache, checkUnignored, slices) {
|
|
597
|
+
if (path13 in cache) {
|
|
598
|
+
return cache[path13];
|
|
599
599
|
}
|
|
600
600
|
if (!slices) {
|
|
601
|
-
slices =
|
|
601
|
+
slices = path13.split(SLASH).filter(Boolean);
|
|
602
602
|
}
|
|
603
603
|
slices.pop();
|
|
604
604
|
if (!slices.length) {
|
|
605
|
-
return cache[
|
|
605
|
+
return cache[path13] = this._rules.test(path13, checkUnignored, MODE_IGNORE);
|
|
606
606
|
}
|
|
607
607
|
const parent = this._t(
|
|
608
608
|
slices.join(SLASH) + SLASH,
|
|
@@ -610,29 +610,29 @@ var require_ignore = __commonJS({
|
|
|
610
610
|
checkUnignored,
|
|
611
611
|
slices
|
|
612
612
|
);
|
|
613
|
-
return cache[
|
|
613
|
+
return cache[path13] = parent.ignored ? parent : this._rules.test(path13, checkUnignored, MODE_IGNORE);
|
|
614
614
|
}
|
|
615
|
-
ignores(
|
|
616
|
-
return this._test(
|
|
615
|
+
ignores(path13) {
|
|
616
|
+
return this._test(path13, this._ignoreCache, false).ignored;
|
|
617
617
|
}
|
|
618
618
|
createFilter() {
|
|
619
|
-
return (
|
|
619
|
+
return (path13) => !this.ignores(path13);
|
|
620
620
|
}
|
|
621
621
|
filter(paths) {
|
|
622
622
|
return makeArray(paths).filter(this.createFilter());
|
|
623
623
|
}
|
|
624
624
|
// @returns {TestResult}
|
|
625
|
-
test(
|
|
626
|
-
return this._test(
|
|
625
|
+
test(path13) {
|
|
626
|
+
return this._test(path13, this._testCache, true);
|
|
627
627
|
}
|
|
628
628
|
};
|
|
629
629
|
var factory = (options) => new Ignore2(options);
|
|
630
|
-
var isPathValid = (
|
|
630
|
+
var isPathValid = (path13) => checkPath(path13 && checkPath.convert(path13), path13, RETURN_FALSE);
|
|
631
631
|
var setupWindows = () => {
|
|
632
632
|
const makePosix = (str) => /^\\\\\?\\/.test(str) || /["<>|\u0000-\u001F]+/u.test(str) ? str : str.replace(/\\/g, "/");
|
|
633
633
|
checkPath.convert = makePosix;
|
|
634
634
|
const REGEX_TEST_WINDOWS_PATH_ABSOLUTE = /^[a-z]:\//i;
|
|
635
|
-
checkPath.isNotRelative = (
|
|
635
|
+
checkPath.isNotRelative = (path13) => REGEX_TEST_WINDOWS_PATH_ABSOLUTE.test(path13) || isNotRelative(path13);
|
|
636
636
|
};
|
|
637
637
|
if (
|
|
638
638
|
// Detect `process` so that it can run in browsers.
|
|
@@ -649,7 +649,7 @@ var require_ignore = __commonJS({
|
|
|
649
649
|
|
|
650
650
|
// src/cli.ts
|
|
651
651
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
652
|
-
import * as
|
|
652
|
+
import * as path12 from "path";
|
|
653
653
|
|
|
654
654
|
// src/config/constants.ts
|
|
655
655
|
var DEFAULT_INCLUDE = [
|
|
@@ -658,12 +658,14 @@ var DEFAULT_INCLUDE = [
|
|
|
658
658
|
"**/*.{go,rs,java,kt,scala}",
|
|
659
659
|
"**/*.{c,cpp,cc,h,hpp}",
|
|
660
660
|
"**/*.{rb,php,inc,swift}",
|
|
661
|
+
"**/*.{cls,trigger}",
|
|
661
662
|
"**/*.{vue,svelte,astro}",
|
|
662
663
|
"**/*.{sql,graphql,proto}",
|
|
663
664
|
"**/*.{yaml,yml,toml}",
|
|
664
665
|
"**/*.{md,mdx}",
|
|
665
666
|
"**/*.{sh,bash,zsh}",
|
|
666
|
-
"**/*.{txt,html,htm}"
|
|
667
|
+
"**/*.{txt,html,htm}",
|
|
668
|
+
"**/*.zig"
|
|
667
669
|
];
|
|
668
670
|
var DEFAULT_EXCLUDE = [
|
|
669
671
|
"**/node_modules/**",
|
|
@@ -728,7 +730,7 @@ var EMBEDDING_MODELS = {
|
|
|
728
730
|
provider: "ollama",
|
|
729
731
|
model: "nomic-embed-text",
|
|
730
732
|
dimensions: 768,
|
|
731
|
-
maxTokens:
|
|
733
|
+
maxTokens: 2048,
|
|
732
734
|
costPer1MTokens: 0
|
|
733
735
|
},
|
|
734
736
|
"mxbai-embed-large": {
|
|
@@ -752,9 +754,15 @@ var EMBEDDING_MODELS = {
|
|
|
752
754
|
var DEFAULT_PROVIDER_MODELS = {
|
|
753
755
|
"github-copilot": "text-embedding-3-small",
|
|
754
756
|
"openai": "text-embedding-3-small",
|
|
755
|
-
"google": "
|
|
757
|
+
"google": "gemini-embedding-001",
|
|
756
758
|
"ollama": "nomic-embed-text"
|
|
757
759
|
};
|
|
760
|
+
var AUTO_DETECT_PROVIDER_ORDER = [
|
|
761
|
+
"ollama",
|
|
762
|
+
"github-copilot",
|
|
763
|
+
"openai",
|
|
764
|
+
"google"
|
|
765
|
+
];
|
|
758
766
|
|
|
759
767
|
// src/config/env-substitution.ts
|
|
760
768
|
var ENV_REFERENCE_PATTERN = /^\{env:([A-Z_][A-Z0-9_]*)\}$/;
|
|
@@ -1013,9 +1021,12 @@ function getDefaultModelForProvider(provider) {
|
|
|
1013
1021
|
return models[providerDefault];
|
|
1014
1022
|
}
|
|
1015
1023
|
var availableProviders = Object.keys(EMBEDDING_MODELS);
|
|
1024
|
+
var autoDetectProviders = AUTO_DETECT_PROVIDER_ORDER.filter(
|
|
1025
|
+
(provider) => provider in EMBEDDING_MODELS
|
|
1026
|
+
);
|
|
1016
1027
|
|
|
1017
1028
|
// src/eval/cli.ts
|
|
1018
|
-
import * as
|
|
1029
|
+
import * as path10 from "path";
|
|
1019
1030
|
|
|
1020
1031
|
// src/eval/compare.ts
|
|
1021
1032
|
function metricDelta(current, baseline) {
|
|
@@ -1058,9 +1069,9 @@ function compareSummaries(current, baseline, againstPath) {
|
|
|
1058
1069
|
// src/eval/reports.ts
|
|
1059
1070
|
import { mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
1060
1071
|
import * as path from "path";
|
|
1061
|
-
function assertFiniteNumber(value,
|
|
1072
|
+
function assertFiniteNumber(value, path13) {
|
|
1062
1073
|
if (typeof value !== "number" || Number.isNaN(value) || !Number.isFinite(value)) {
|
|
1063
|
-
throw new Error(`${
|
|
1074
|
+
throw new Error(`${path13} must be a finite number`);
|
|
1064
1075
|
}
|
|
1065
1076
|
return value;
|
|
1066
1077
|
}
|
|
@@ -1234,16 +1245,352 @@ function buildPerQueryArtifact(perQuery) {
|
|
|
1234
1245
|
}
|
|
1235
1246
|
|
|
1236
1247
|
// src/eval/runner.ts
|
|
1237
|
-
import { existsSync as
|
|
1238
|
-
import {
|
|
1248
|
+
import { existsSync as existsSync7 } from "fs";
|
|
1249
|
+
import { mkdirSync as mkdirSync4 } from "fs";
|
|
1250
|
+
import { readFileSync as readFileSync8 } from "fs";
|
|
1239
1251
|
import { rmSync } from "fs";
|
|
1240
|
-
import
|
|
1241
|
-
import * as
|
|
1252
|
+
import { writeFileSync as writeFileSync4 } from "fs";
|
|
1253
|
+
import * as os5 from "os";
|
|
1254
|
+
import * as path9 from "path";
|
|
1242
1255
|
import { performance as performance3 } from "perf_hooks";
|
|
1243
1256
|
|
|
1257
|
+
// src/config/merger.ts
|
|
1258
|
+
import { existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
|
|
1259
|
+
import * as os2 from "os";
|
|
1260
|
+
import * as path4 from "path";
|
|
1261
|
+
|
|
1262
|
+
// src/config/paths.ts
|
|
1263
|
+
import { existsSync as existsSync2 } from "fs";
|
|
1264
|
+
import * as os from "os";
|
|
1265
|
+
import * as path3 from "path";
|
|
1266
|
+
|
|
1267
|
+
// src/git/index.ts
|
|
1268
|
+
import { existsSync, readFileSync as readFileSync2, readdirSync, statSync } from "fs";
|
|
1269
|
+
import * as path2 from "path";
|
|
1270
|
+
function readPackedRefs(gitDir) {
|
|
1271
|
+
const packedRefsPath = path2.join(gitDir, "packed-refs");
|
|
1272
|
+
if (!existsSync(packedRefsPath)) {
|
|
1273
|
+
return [];
|
|
1274
|
+
}
|
|
1275
|
+
try {
|
|
1276
|
+
return readFileSync2(packedRefsPath, "utf-8").split("\n").map((line) => line.trim()).filter((line) => line.length > 0 && !line.startsWith("#") && !line.startsWith("^"));
|
|
1277
|
+
} catch {
|
|
1278
|
+
return [];
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
function resolveCommonGitDir(gitDir) {
|
|
1282
|
+
const commonDirPath = path2.join(gitDir, "commondir");
|
|
1283
|
+
if (!existsSync(commonDirPath)) {
|
|
1284
|
+
return gitDir;
|
|
1285
|
+
}
|
|
1286
|
+
try {
|
|
1287
|
+
const raw = readFileSync2(commonDirPath, "utf-8").trim();
|
|
1288
|
+
if (!raw) {
|
|
1289
|
+
return gitDir;
|
|
1290
|
+
}
|
|
1291
|
+
const resolved = path2.isAbsolute(raw) ? raw : path2.resolve(gitDir, raw);
|
|
1292
|
+
if (existsSync(resolved)) {
|
|
1293
|
+
return resolved;
|
|
1294
|
+
}
|
|
1295
|
+
} catch {
|
|
1296
|
+
return gitDir;
|
|
1297
|
+
}
|
|
1298
|
+
return gitDir;
|
|
1299
|
+
}
|
|
1300
|
+
function resolveWorktreeMainRepoRoot(repoRoot) {
|
|
1301
|
+
const gitDir = resolveGitDir(repoRoot);
|
|
1302
|
+
if (!gitDir) {
|
|
1303
|
+
return null;
|
|
1304
|
+
}
|
|
1305
|
+
const commonGitDir = resolveCommonGitDir(gitDir);
|
|
1306
|
+
if (commonGitDir === gitDir || path2.basename(commonGitDir) !== ".git") {
|
|
1307
|
+
return null;
|
|
1308
|
+
}
|
|
1309
|
+
const mainRepoRoot = path2.dirname(commonGitDir);
|
|
1310
|
+
if (!existsSync(mainRepoRoot)) {
|
|
1311
|
+
return null;
|
|
1312
|
+
}
|
|
1313
|
+
return path2.resolve(mainRepoRoot) === path2.resolve(repoRoot) ? null : mainRepoRoot;
|
|
1314
|
+
}
|
|
1315
|
+
function resolveGitDir(repoRoot) {
|
|
1316
|
+
const gitPath = path2.join(repoRoot, ".git");
|
|
1317
|
+
if (!existsSync(gitPath)) {
|
|
1318
|
+
return null;
|
|
1319
|
+
}
|
|
1320
|
+
try {
|
|
1321
|
+
const stat = statSync(gitPath);
|
|
1322
|
+
if (stat.isDirectory()) {
|
|
1323
|
+
return gitPath;
|
|
1324
|
+
}
|
|
1325
|
+
if (stat.isFile()) {
|
|
1326
|
+
const content = readFileSync2(gitPath, "utf-8").trim();
|
|
1327
|
+
const match = content.match(/^gitdir:\s*(.+)$/);
|
|
1328
|
+
if (match) {
|
|
1329
|
+
const gitdir = match[1];
|
|
1330
|
+
const resolvedPath = path2.isAbsolute(gitdir) ? gitdir : path2.resolve(repoRoot, gitdir);
|
|
1331
|
+
if (existsSync(resolvedPath)) {
|
|
1332
|
+
return resolvedPath;
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
} catch {
|
|
1337
|
+
}
|
|
1338
|
+
return null;
|
|
1339
|
+
}
|
|
1340
|
+
function isGitRepo(dir) {
|
|
1341
|
+
return resolveGitDir(dir) !== null;
|
|
1342
|
+
}
|
|
1343
|
+
function getCurrentBranch(repoRoot) {
|
|
1344
|
+
const gitDir = resolveGitDir(repoRoot);
|
|
1345
|
+
if (!gitDir) {
|
|
1346
|
+
return null;
|
|
1347
|
+
}
|
|
1348
|
+
const headPath = path2.join(gitDir, "HEAD");
|
|
1349
|
+
if (!existsSync(headPath)) {
|
|
1350
|
+
return null;
|
|
1351
|
+
}
|
|
1352
|
+
try {
|
|
1353
|
+
const headContent = readFileSync2(headPath, "utf-8").trim();
|
|
1354
|
+
const match = headContent.match(/^ref: refs\/heads\/(.+)$/);
|
|
1355
|
+
if (match) {
|
|
1356
|
+
return match[1];
|
|
1357
|
+
}
|
|
1358
|
+
if (/^[0-9a-f]{40}$/i.test(headContent)) {
|
|
1359
|
+
return headContent.slice(0, 7);
|
|
1360
|
+
}
|
|
1361
|
+
return null;
|
|
1362
|
+
} catch {
|
|
1363
|
+
return null;
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
function getBaseBranch(repoRoot) {
|
|
1367
|
+
const gitDir = resolveGitDir(repoRoot);
|
|
1368
|
+
const refStoreDir = gitDir ? resolveCommonGitDir(gitDir) : null;
|
|
1369
|
+
const candidates = ["main", "master", "develop", "trunk"];
|
|
1370
|
+
if (refStoreDir) {
|
|
1371
|
+
for (const candidate of candidates) {
|
|
1372
|
+
const refPath = path2.join(refStoreDir, "refs", "heads", candidate);
|
|
1373
|
+
if (existsSync(refPath)) {
|
|
1374
|
+
return candidate;
|
|
1375
|
+
}
|
|
1376
|
+
const packedRefs = readPackedRefs(refStoreDir);
|
|
1377
|
+
if (packedRefs.some((line) => line.endsWith(` refs/heads/${candidate}`))) {
|
|
1378
|
+
return candidate;
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
return getCurrentBranch(repoRoot) ?? "main";
|
|
1383
|
+
}
|
|
1384
|
+
function getBranchOrDefault(repoRoot) {
|
|
1385
|
+
if (!isGitRepo(repoRoot)) {
|
|
1386
|
+
return "default";
|
|
1387
|
+
}
|
|
1388
|
+
return getCurrentBranch(repoRoot) ?? "default";
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
// src/config/paths.ts
|
|
1392
|
+
var PROJECT_CONFIG_RELATIVE_PATH = path3.join(".opencode", "codebase-index.json");
|
|
1393
|
+
var PROJECT_INDEX_RELATIVE_PATH = path3.join(".opencode", "index");
|
|
1394
|
+
function resolveWorktreeFallbackPath(projectRoot, relativePath) {
|
|
1395
|
+
const mainRepoRoot = resolveWorktreeMainRepoRoot(projectRoot);
|
|
1396
|
+
if (!mainRepoRoot) {
|
|
1397
|
+
return null;
|
|
1398
|
+
}
|
|
1399
|
+
const fallbackPath = path3.join(mainRepoRoot, relativePath);
|
|
1400
|
+
return existsSync2(fallbackPath) ? fallbackPath : null;
|
|
1401
|
+
}
|
|
1402
|
+
function hasProjectConfig(projectRoot) {
|
|
1403
|
+
return existsSync2(path3.join(projectRoot, PROJECT_CONFIG_RELATIVE_PATH));
|
|
1404
|
+
}
|
|
1405
|
+
function getGlobalIndexPath() {
|
|
1406
|
+
return path3.join(os.homedir(), ".opencode", "global-index");
|
|
1407
|
+
}
|
|
1408
|
+
function resolveProjectConfigPath(projectRoot) {
|
|
1409
|
+
const localConfigPath = path3.join(projectRoot, PROJECT_CONFIG_RELATIVE_PATH);
|
|
1410
|
+
if (existsSync2(localConfigPath)) {
|
|
1411
|
+
return localConfigPath;
|
|
1412
|
+
}
|
|
1413
|
+
return resolveWorktreeFallbackPath(projectRoot, PROJECT_CONFIG_RELATIVE_PATH) ?? localConfigPath;
|
|
1414
|
+
}
|
|
1415
|
+
function resolveProjectIndexPath(projectRoot, scope) {
|
|
1416
|
+
if (scope === "global") {
|
|
1417
|
+
return getGlobalIndexPath();
|
|
1418
|
+
}
|
|
1419
|
+
const localIndexPath = path3.join(projectRoot, PROJECT_INDEX_RELATIVE_PATH);
|
|
1420
|
+
if (existsSync2(localIndexPath)) {
|
|
1421
|
+
return localIndexPath;
|
|
1422
|
+
}
|
|
1423
|
+
if (hasProjectConfig(projectRoot)) {
|
|
1424
|
+
return localIndexPath;
|
|
1425
|
+
}
|
|
1426
|
+
return resolveWorktreeFallbackPath(projectRoot, PROJECT_INDEX_RELATIVE_PATH) ?? localIndexPath;
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
// src/config/merger.ts
|
|
1430
|
+
function loadJsonFile(filePath) {
|
|
1431
|
+
try {
|
|
1432
|
+
if (existsSync3(filePath)) {
|
|
1433
|
+
const content = readFileSync3(filePath, "utf-8");
|
|
1434
|
+
return JSON.parse(content);
|
|
1435
|
+
}
|
|
1436
|
+
} catch {
|
|
1437
|
+
}
|
|
1438
|
+
return null;
|
|
1439
|
+
}
|
|
1440
|
+
function normalizeRelativeConfigPath(candidate) {
|
|
1441
|
+
return candidate.replace(/\\/g, "/");
|
|
1442
|
+
}
|
|
1443
|
+
function rebasePathEntries(values, fromDir, toDir) {
|
|
1444
|
+
if (!Array.isArray(values)) {
|
|
1445
|
+
return [];
|
|
1446
|
+
}
|
|
1447
|
+
return values.filter((value) => typeof value === "string").map((value) => {
|
|
1448
|
+
const trimmed = value.trim();
|
|
1449
|
+
if (!trimmed || path4.isAbsolute(trimmed)) {
|
|
1450
|
+
return trimmed;
|
|
1451
|
+
}
|
|
1452
|
+
return normalizeRelativeConfigPath(path4.normalize(path4.relative(toDir, path4.resolve(fromDir, trimmed))));
|
|
1453
|
+
}).filter(Boolean);
|
|
1454
|
+
}
|
|
1455
|
+
function isWithinRoot(rootDir, targetPath) {
|
|
1456
|
+
const relativePath = path4.relative(rootDir, targetPath);
|
|
1457
|
+
return relativePath === "" || !relativePath.startsWith("..") && !path4.isAbsolute(relativePath);
|
|
1458
|
+
}
|
|
1459
|
+
function resolveInheritedKnowledgeBaseEntries(values, sourceRoot, targetRoot) {
|
|
1460
|
+
if (!Array.isArray(values)) {
|
|
1461
|
+
return [];
|
|
1462
|
+
}
|
|
1463
|
+
return values.filter((value) => typeof value === "string").map((value) => {
|
|
1464
|
+
const trimmed = value.trim();
|
|
1465
|
+
if (!trimmed) {
|
|
1466
|
+
return trimmed;
|
|
1467
|
+
}
|
|
1468
|
+
if (path4.isAbsolute(trimmed)) {
|
|
1469
|
+
if (isWithinRoot(sourceRoot, trimmed)) {
|
|
1470
|
+
return normalizeRelativeConfigPath(path4.normalize(path4.relative(sourceRoot, trimmed) || "."));
|
|
1471
|
+
}
|
|
1472
|
+
return path4.normalize(trimmed);
|
|
1473
|
+
}
|
|
1474
|
+
const resolvedFromSource = path4.resolve(sourceRoot, trimmed);
|
|
1475
|
+
if (isWithinRoot(sourceRoot, resolvedFromSource)) {
|
|
1476
|
+
return normalizeRelativeConfigPath(path4.normalize(trimmed));
|
|
1477
|
+
}
|
|
1478
|
+
return normalizeRelativeConfigPath(path4.normalize(path4.relative(targetRoot, resolvedFromSource)));
|
|
1479
|
+
}).filter(Boolean);
|
|
1480
|
+
}
|
|
1481
|
+
function materializeLocalProjectConfig(projectRoot, config) {
|
|
1482
|
+
const localConfigPath = path4.join(projectRoot, ".opencode", "codebase-index.json");
|
|
1483
|
+
mkdirSync2(path4.dirname(localConfigPath), { recursive: true });
|
|
1484
|
+
writeFileSync2(localConfigPath, JSON.stringify(config, null, 2), "utf-8");
|
|
1485
|
+
return localConfigPath;
|
|
1486
|
+
}
|
|
1487
|
+
function loadProjectConfigLayer(projectRoot) {
|
|
1488
|
+
const projectConfigPath = resolveProjectConfigPath(projectRoot);
|
|
1489
|
+
const projectConfig = loadJsonFile(projectConfigPath);
|
|
1490
|
+
if (!projectConfig) {
|
|
1491
|
+
return {};
|
|
1492
|
+
}
|
|
1493
|
+
const normalizedConfig = { ...projectConfig };
|
|
1494
|
+
const projectConfigBaseDir = path4.dirname(path4.dirname(projectConfigPath));
|
|
1495
|
+
if (Array.isArray(normalizedConfig.knowledgeBases)) {
|
|
1496
|
+
normalizedConfig.knowledgeBases = resolveInheritedKnowledgeBaseEntries(
|
|
1497
|
+
normalizedConfig.knowledgeBases,
|
|
1498
|
+
projectConfigBaseDir,
|
|
1499
|
+
projectRoot
|
|
1500
|
+
);
|
|
1501
|
+
}
|
|
1502
|
+
return normalizedConfig;
|
|
1503
|
+
}
|
|
1504
|
+
function loadMergedConfig(projectRoot) {
|
|
1505
|
+
const globalConfigPath = os2.homedir() + "/.config/opencode/codebase-index.json";
|
|
1506
|
+
const globalConfig = loadJsonFile(globalConfigPath);
|
|
1507
|
+
const projectConfigPath = resolveProjectConfigPath(projectRoot);
|
|
1508
|
+
const projectConfig = loadJsonFile(projectConfigPath);
|
|
1509
|
+
const normalizedProjectConfig = loadProjectConfigLayer(projectRoot);
|
|
1510
|
+
if (!globalConfig && !projectConfig) {
|
|
1511
|
+
return {};
|
|
1512
|
+
}
|
|
1513
|
+
if (!projectConfig && globalConfig) {
|
|
1514
|
+
return globalConfig;
|
|
1515
|
+
}
|
|
1516
|
+
if (!globalConfig && projectConfig) {
|
|
1517
|
+
return normalizedProjectConfig;
|
|
1518
|
+
}
|
|
1519
|
+
const merged = { ...globalConfig };
|
|
1520
|
+
if (projectConfig && "embeddingProvider" in normalizedProjectConfig) {
|
|
1521
|
+
merged.embeddingProvider = normalizedProjectConfig.embeddingProvider;
|
|
1522
|
+
} else if (globalConfig && globalConfig.embeddingProvider) {
|
|
1523
|
+
merged.embeddingProvider = globalConfig.embeddingProvider;
|
|
1524
|
+
}
|
|
1525
|
+
if (projectConfig && "customProvider" in normalizedProjectConfig) {
|
|
1526
|
+
merged.customProvider = normalizedProjectConfig.customProvider;
|
|
1527
|
+
} else if (globalConfig && globalConfig.customProvider) {
|
|
1528
|
+
merged.customProvider = globalConfig.customProvider;
|
|
1529
|
+
}
|
|
1530
|
+
if (projectConfig && "embeddingModel" in normalizedProjectConfig) {
|
|
1531
|
+
merged.embeddingModel = normalizedProjectConfig.embeddingModel;
|
|
1532
|
+
} else if (globalConfig && globalConfig.embeddingModel) {
|
|
1533
|
+
merged.embeddingModel = globalConfig.embeddingModel;
|
|
1534
|
+
}
|
|
1535
|
+
if (projectConfig && "reranker" in normalizedProjectConfig) {
|
|
1536
|
+
merged.reranker = normalizedProjectConfig.reranker;
|
|
1537
|
+
} else if (globalConfig && globalConfig.reranker) {
|
|
1538
|
+
merged.reranker = globalConfig.reranker;
|
|
1539
|
+
}
|
|
1540
|
+
if (projectConfig && "include" in normalizedProjectConfig) {
|
|
1541
|
+
merged.include = normalizedProjectConfig.include;
|
|
1542
|
+
} else if (globalConfig && globalConfig.include) {
|
|
1543
|
+
merged.include = globalConfig.include;
|
|
1544
|
+
}
|
|
1545
|
+
if (projectConfig && "exclude" in normalizedProjectConfig) {
|
|
1546
|
+
merged.exclude = normalizedProjectConfig.exclude;
|
|
1547
|
+
} else if (globalConfig && globalConfig.exclude) {
|
|
1548
|
+
merged.exclude = globalConfig.exclude;
|
|
1549
|
+
}
|
|
1550
|
+
if (projectConfig && "indexing" in normalizedProjectConfig) {
|
|
1551
|
+
merged.indexing = normalizedProjectConfig.indexing;
|
|
1552
|
+
} else if (globalConfig && globalConfig.indexing) {
|
|
1553
|
+
merged.indexing = globalConfig.indexing;
|
|
1554
|
+
}
|
|
1555
|
+
if (projectConfig && "search" in normalizedProjectConfig) {
|
|
1556
|
+
merged.search = normalizedProjectConfig.search;
|
|
1557
|
+
} else if (globalConfig && globalConfig.search) {
|
|
1558
|
+
merged.search = globalConfig.search;
|
|
1559
|
+
}
|
|
1560
|
+
if (projectConfig && "debug" in normalizedProjectConfig) {
|
|
1561
|
+
merged.debug = normalizedProjectConfig.debug;
|
|
1562
|
+
} else if (globalConfig && globalConfig.debug) {
|
|
1563
|
+
merged.debug = globalConfig.debug;
|
|
1564
|
+
}
|
|
1565
|
+
if (projectConfig && "scope" in normalizedProjectConfig) {
|
|
1566
|
+
merged.scope = normalizedProjectConfig.scope;
|
|
1567
|
+
} else if (globalConfig && "scope" in globalConfig) {
|
|
1568
|
+
merged.scope = globalConfig.scope;
|
|
1569
|
+
}
|
|
1570
|
+
if (projectConfig) {
|
|
1571
|
+
for (const key of Object.keys(projectConfig)) {
|
|
1572
|
+
if (key === "embeddingProvider" || key === "customProvider" || key === "embeddingModel" || key === "reranker" || key === "include" || key === "exclude" || key === "indexing" || key === "search" || key === "debug" || key === "scope" || key === "knowledgeBases" || key === "additionalInclude") {
|
|
1573
|
+
continue;
|
|
1574
|
+
}
|
|
1575
|
+
merged[key] = normalizedProjectConfig[key];
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
const globalKbs = globalConfig && Array.isArray(globalConfig.knowledgeBases) ? globalConfig.knowledgeBases : [];
|
|
1579
|
+
const projectKbs = projectConfig ? Array.isArray(normalizedProjectConfig.knowledgeBases) ? normalizedProjectConfig.knowledgeBases : [] : [];
|
|
1580
|
+
const allKbs = [...globalKbs, ...projectKbs];
|
|
1581
|
+
const uniqueKbs = [...new Set(allKbs.map((p) => String(p).trim()))];
|
|
1582
|
+
merged.knowledgeBases = uniqueKbs;
|
|
1583
|
+
const globalAdditional = globalConfig && Array.isArray(globalConfig.additionalInclude) ? globalConfig.additionalInclude : [];
|
|
1584
|
+
const projectAdditional = projectConfig && Array.isArray(projectConfig.additionalInclude) ? projectConfig.additionalInclude : [];
|
|
1585
|
+
const allAdditional = [...globalAdditional, ...projectAdditional];
|
|
1586
|
+
const uniqueAdditional = [...new Set(allAdditional.map((p) => String(p).trim()))];
|
|
1587
|
+
merged.additionalInclude = uniqueAdditional;
|
|
1588
|
+
return merged;
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1244
1591
|
// src/indexer/index.ts
|
|
1245
|
-
import { existsSync as
|
|
1246
|
-
import * as
|
|
1592
|
+
import { existsSync as existsSync6, readFileSync as readFileSync6, writeFileSync as writeFileSync3, renameSync, unlinkSync, mkdirSync as mkdirSync3, promises as fsPromises2 } from "fs";
|
|
1593
|
+
import * as path8 from "path";
|
|
1247
1594
|
import { performance as performance2 } from "perf_hooks";
|
|
1248
1595
|
|
|
1249
1596
|
// node_modules/eventemitter3/index.mjs
|
|
@@ -1268,7 +1615,7 @@ function pTimeout(promise, options) {
|
|
|
1268
1615
|
} = options;
|
|
1269
1616
|
let timer;
|
|
1270
1617
|
let abortHandler;
|
|
1271
|
-
const wrappedPromise = new Promise((
|
|
1618
|
+
const wrappedPromise = new Promise((resolve8, reject) => {
|
|
1272
1619
|
if (typeof milliseconds !== "number" || Math.sign(milliseconds) !== 1) {
|
|
1273
1620
|
throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\``);
|
|
1274
1621
|
}
|
|
@@ -1282,7 +1629,7 @@ function pTimeout(promise, options) {
|
|
|
1282
1629
|
};
|
|
1283
1630
|
signal.addEventListener("abort", abortHandler, { once: true });
|
|
1284
1631
|
}
|
|
1285
|
-
promise.then(
|
|
1632
|
+
promise.then(resolve8, reject);
|
|
1286
1633
|
if (milliseconds === Number.POSITIVE_INFINITY) {
|
|
1287
1634
|
return;
|
|
1288
1635
|
}
|
|
@@ -1290,7 +1637,7 @@ function pTimeout(promise, options) {
|
|
|
1290
1637
|
timer = customTimers.setTimeout.call(void 0, () => {
|
|
1291
1638
|
if (fallback) {
|
|
1292
1639
|
try {
|
|
1293
|
-
|
|
1640
|
+
resolve8(fallback());
|
|
1294
1641
|
} catch (error) {
|
|
1295
1642
|
reject(error);
|
|
1296
1643
|
}
|
|
@@ -1300,7 +1647,7 @@ function pTimeout(promise, options) {
|
|
|
1300
1647
|
promise.cancel();
|
|
1301
1648
|
}
|
|
1302
1649
|
if (message === false) {
|
|
1303
|
-
|
|
1650
|
+
resolve8();
|
|
1304
1651
|
} else if (message instanceof Error) {
|
|
1305
1652
|
reject(message);
|
|
1306
1653
|
} else {
|
|
@@ -1702,7 +2049,7 @@ var PQueue = class extends import_index.default {
|
|
|
1702
2049
|
// Assign unique ID if not provided
|
|
1703
2050
|
id: options.id ?? (this.#idAssigner++).toString()
|
|
1704
2051
|
};
|
|
1705
|
-
return new Promise((
|
|
2052
|
+
return new Promise((resolve8, reject) => {
|
|
1706
2053
|
const taskSymbol = /* @__PURE__ */ Symbol(`task-${options.id}`);
|
|
1707
2054
|
let cleanupQueueAbortHandler = () => void 0;
|
|
1708
2055
|
const run = async () => {
|
|
@@ -1742,7 +2089,7 @@ var PQueue = class extends import_index.default {
|
|
|
1742
2089
|
})]);
|
|
1743
2090
|
}
|
|
1744
2091
|
const result = await operation;
|
|
1745
|
-
|
|
2092
|
+
resolve8(result);
|
|
1746
2093
|
this.emit("completed", result);
|
|
1747
2094
|
} catch (error) {
|
|
1748
2095
|
reject(error);
|
|
@@ -1930,13 +2277,13 @@ var PQueue = class extends import_index.default {
|
|
|
1930
2277
|
});
|
|
1931
2278
|
}
|
|
1932
2279
|
async #onEvent(event, filter) {
|
|
1933
|
-
return new Promise((
|
|
2280
|
+
return new Promise((resolve8) => {
|
|
1934
2281
|
const listener = () => {
|
|
1935
2282
|
if (filter && !filter()) {
|
|
1936
2283
|
return;
|
|
1937
2284
|
}
|
|
1938
2285
|
this.off(event, listener);
|
|
1939
|
-
|
|
2286
|
+
resolve8();
|
|
1940
2287
|
};
|
|
1941
2288
|
this.on(event, listener);
|
|
1942
2289
|
});
|
|
@@ -2222,7 +2569,7 @@ async function onAttemptFailure({ error, attemptNumber, retriesConsumed, startTi
|
|
|
2222
2569
|
const finalDelay = Math.min(delayTime, remainingTime);
|
|
2223
2570
|
options.signal?.throwIfAborted();
|
|
2224
2571
|
if (finalDelay > 0) {
|
|
2225
|
-
await new Promise((
|
|
2572
|
+
await new Promise((resolve8, reject) => {
|
|
2226
2573
|
const onAbort = () => {
|
|
2227
2574
|
clearTimeout(timeoutToken);
|
|
2228
2575
|
options.signal?.removeEventListener("abort", onAbort);
|
|
@@ -2230,7 +2577,7 @@ async function onAttemptFailure({ error, attemptNumber, retriesConsumed, startTi
|
|
|
2230
2577
|
};
|
|
2231
2578
|
const timeoutToken = setTimeout(() => {
|
|
2232
2579
|
options.signal?.removeEventListener("abort", onAbort);
|
|
2233
|
-
|
|
2580
|
+
resolve8();
|
|
2234
2581
|
}, finalDelay);
|
|
2235
2582
|
if (options.unref) {
|
|
2236
2583
|
timeoutToken.unref?.();
|
|
@@ -2291,17 +2638,17 @@ async function pRetry(input, options = {}) {
|
|
|
2291
2638
|
}
|
|
2292
2639
|
|
|
2293
2640
|
// src/embeddings/detector.ts
|
|
2294
|
-
import { existsSync, readFileSync as
|
|
2295
|
-
import * as
|
|
2296
|
-
import * as
|
|
2641
|
+
import { existsSync as existsSync4, readFileSync as readFileSync4 } from "fs";
|
|
2642
|
+
import * as path5 from "path";
|
|
2643
|
+
import * as os3 from "os";
|
|
2297
2644
|
function getOpenCodeAuthPath() {
|
|
2298
|
-
return
|
|
2645
|
+
return path5.join(os3.homedir(), ".local", "share", "opencode", "auth.json");
|
|
2299
2646
|
}
|
|
2300
2647
|
function loadOpenCodeAuth() {
|
|
2301
2648
|
const authPath = getOpenCodeAuthPath();
|
|
2302
2649
|
try {
|
|
2303
|
-
if (
|
|
2304
|
-
return JSON.parse(
|
|
2650
|
+
if (existsSync4(authPath)) {
|
|
2651
|
+
return JSON.parse(readFileSync4(authPath, "utf-8"));
|
|
2305
2652
|
}
|
|
2306
2653
|
} catch {
|
|
2307
2654
|
}
|
|
@@ -2334,7 +2681,7 @@ async function detectEmbeddingProvider(preferredProvider, model) {
|
|
|
2334
2681
|
);
|
|
2335
2682
|
}
|
|
2336
2683
|
async function tryDetectProvider() {
|
|
2337
|
-
for (const provider of
|
|
2684
|
+
for (const provider of autoDetectProviders) {
|
|
2338
2685
|
const credentials = await getProviderCredentials(provider);
|
|
2339
2686
|
if (credentials) {
|
|
2340
2687
|
return {
|
|
@@ -2345,7 +2692,7 @@ async function tryDetectProvider() {
|
|
|
2345
2692
|
}
|
|
2346
2693
|
}
|
|
2347
2694
|
throw new Error(
|
|
2348
|
-
`No embedding-capable provider found. Please authenticate with OpenCode using one of: ${
|
|
2695
|
+
`No embedding-capable provider found. Please authenticate with OpenCode using one of: ${autoDetectProviders.join(", ")}.`
|
|
2349
2696
|
);
|
|
2350
2697
|
}
|
|
2351
2698
|
async function getProviderCredentials(provider) {
|
|
@@ -2665,11 +3012,12 @@ var GoogleEmbeddingProvider = class _GoogleEmbeddingProvider {
|
|
|
2665
3012
|
return this.modelInfo;
|
|
2666
3013
|
}
|
|
2667
3014
|
};
|
|
2668
|
-
var OllamaEmbeddingProvider = class {
|
|
3015
|
+
var OllamaEmbeddingProvider = class _OllamaEmbeddingProvider {
|
|
2669
3016
|
constructor(credentials, modelInfo) {
|
|
2670
3017
|
this.credentials = credentials;
|
|
2671
3018
|
this.modelInfo = modelInfo;
|
|
2672
3019
|
}
|
|
3020
|
+
static MIN_TRUNCATION_CHARS = 512;
|
|
2673
3021
|
async embedQuery(query) {
|
|
2674
3022
|
const result = await this.embedBatch([query]);
|
|
2675
3023
|
return {
|
|
@@ -2684,52 +3032,125 @@ var OllamaEmbeddingProvider = class {
|
|
|
2684
3032
|
tokensUsed: result.totalTokensUsed
|
|
2685
3033
|
};
|
|
2686
3034
|
}
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
texts.map(async (text) => {
|
|
2690
|
-
const response = await fetch(`${this.credentials.baseUrl}/api/embeddings`, {
|
|
2691
|
-
method: "POST",
|
|
2692
|
-
headers: {
|
|
2693
|
-
"Content-Type": "application/json"
|
|
2694
|
-
},
|
|
2695
|
-
body: JSON.stringify({
|
|
2696
|
-
model: this.modelInfo.model,
|
|
2697
|
-
prompt: text
|
|
2698
|
-
})
|
|
2699
|
-
});
|
|
2700
|
-
if (!response.ok) {
|
|
2701
|
-
const error = await response.text();
|
|
2702
|
-
throw new Error(`Ollama embedding API error: ${response.status} - ${error}`);
|
|
2703
|
-
}
|
|
2704
|
-
const data = await response.json();
|
|
2705
|
-
return {
|
|
2706
|
-
embedding: data.embedding,
|
|
2707
|
-
tokensUsed: Math.ceil(text.length / 4)
|
|
2708
|
-
};
|
|
2709
|
-
})
|
|
2710
|
-
);
|
|
2711
|
-
return {
|
|
2712
|
-
embeddings: results.map((r) => r.embedding),
|
|
2713
|
-
totalTokensUsed: results.reduce((sum, r) => sum + r.tokensUsed, 0)
|
|
2714
|
-
};
|
|
3035
|
+
estimateTokens(text) {
|
|
3036
|
+
return Math.ceil(text.length / 4);
|
|
2715
3037
|
}
|
|
2716
|
-
|
|
2717
|
-
|
|
3038
|
+
truncateToCharLimit(text, maxChars) {
|
|
3039
|
+
if (text.length <= maxChars) {
|
|
3040
|
+
return text;
|
|
3041
|
+
}
|
|
3042
|
+
return `${text.slice(0, Math.max(0, maxChars - 17))}
|
|
3043
|
+
... [truncated]`;
|
|
2718
3044
|
}
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
this.credentials = credentials;
|
|
2723
|
-
this.modelInfo = modelInfo;
|
|
3045
|
+
isContextLengthError(error) {
|
|
3046
|
+
const message = (error instanceof Error ? error.message : String(error)).toLowerCase();
|
|
3047
|
+
return message.includes("context length") && (message.includes("exceed") || message.includes("exceeded") || message.includes("too long")) || message.includes("input length exceeds the context length") || message.includes("context length exceeded");
|
|
2724
3048
|
}
|
|
2725
|
-
|
|
2726
|
-
const
|
|
2727
|
-
|
|
2728
|
-
|
|
3049
|
+
buildTruncationCandidates(text) {
|
|
3050
|
+
const baseMaxChars = Math.max(1, this.modelInfo.maxTokens * 4);
|
|
3051
|
+
const candidateLimits = /* @__PURE__ */ new Set();
|
|
3052
|
+
const baselineLimit = text.length > baseMaxChars ? baseMaxChars : Math.max(
|
|
3053
|
+
_OllamaEmbeddingProvider.MIN_TRUNCATION_CHARS,
|
|
3054
|
+
Math.floor(text.length * 0.9)
|
|
3055
|
+
);
|
|
3056
|
+
if (baselineLimit < text.length) {
|
|
3057
|
+
candidateLimits.add(baselineLimit);
|
|
2729
3058
|
}
|
|
2730
|
-
const
|
|
2731
|
-
|
|
2732
|
-
|
|
3059
|
+
for (const factor of [0.75, 0.6, 0.45, 0.35, 0.25]) {
|
|
3060
|
+
const scaledLimit = Math.max(
|
|
3061
|
+
_OllamaEmbeddingProvider.MIN_TRUNCATION_CHARS,
|
|
3062
|
+
Math.floor(baselineLimit * factor)
|
|
3063
|
+
);
|
|
3064
|
+
if (scaledLimit < text.length) {
|
|
3065
|
+
candidateLimits.add(scaledLimit);
|
|
3066
|
+
}
|
|
3067
|
+
}
|
|
3068
|
+
candidateLimits.add(Math.min(text.length - 1, _OllamaEmbeddingProvider.MIN_TRUNCATION_CHARS));
|
|
3069
|
+
const candidates = [];
|
|
3070
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3071
|
+
for (const limit of [...candidateLimits].sort((a, b) => b - a)) {
|
|
3072
|
+
if (limit <= 0 || limit >= text.length) {
|
|
3073
|
+
continue;
|
|
3074
|
+
}
|
|
3075
|
+
const truncated = this.truncateToCharLimit(text, limit);
|
|
3076
|
+
if (truncated === text || seen.has(truncated)) {
|
|
3077
|
+
continue;
|
|
3078
|
+
}
|
|
3079
|
+
seen.add(truncated);
|
|
3080
|
+
candidates.push(truncated);
|
|
3081
|
+
}
|
|
3082
|
+
return candidates;
|
|
3083
|
+
}
|
|
3084
|
+
async embedSingleWithFallback(text) {
|
|
3085
|
+
try {
|
|
3086
|
+
return await this.embedSingle(text);
|
|
3087
|
+
} catch (error) {
|
|
3088
|
+
if (!this.isContextLengthError(error)) {
|
|
3089
|
+
throw error;
|
|
3090
|
+
}
|
|
3091
|
+
let lastError = error;
|
|
3092
|
+
for (const truncated of this.buildTruncationCandidates(text)) {
|
|
3093
|
+
try {
|
|
3094
|
+
return await this.embedSingle(truncated);
|
|
3095
|
+
} catch (retryError) {
|
|
3096
|
+
if (!this.isContextLengthError(retryError)) {
|
|
3097
|
+
throw retryError;
|
|
3098
|
+
}
|
|
3099
|
+
lastError = retryError;
|
|
3100
|
+
}
|
|
3101
|
+
}
|
|
3102
|
+
throw lastError;
|
|
3103
|
+
}
|
|
3104
|
+
}
|
|
3105
|
+
async embedSingle(text) {
|
|
3106
|
+
const response = await fetch(`${this.credentials.baseUrl}/api/embeddings`, {
|
|
3107
|
+
method: "POST",
|
|
3108
|
+
headers: {
|
|
3109
|
+
"Content-Type": "application/json"
|
|
3110
|
+
},
|
|
3111
|
+
body: JSON.stringify({
|
|
3112
|
+
model: this.modelInfo.model,
|
|
3113
|
+
prompt: text,
|
|
3114
|
+
truncate: false
|
|
3115
|
+
})
|
|
3116
|
+
});
|
|
3117
|
+
if (!response.ok) {
|
|
3118
|
+
const error = await response.text();
|
|
3119
|
+
throw new Error(`Ollama embedding API error: ${response.status} - ${error}`);
|
|
3120
|
+
}
|
|
3121
|
+
const data = await response.json();
|
|
3122
|
+
return {
|
|
3123
|
+
embedding: data.embedding,
|
|
3124
|
+
tokensUsed: this.estimateTokens(text)
|
|
3125
|
+
};
|
|
3126
|
+
}
|
|
3127
|
+
async embedBatch(texts) {
|
|
3128
|
+
const results = [];
|
|
3129
|
+
for (const text of texts) {
|
|
3130
|
+
results.push(await this.embedSingleWithFallback(text));
|
|
3131
|
+
}
|
|
3132
|
+
return {
|
|
3133
|
+
embeddings: results.map((r) => r.embedding),
|
|
3134
|
+
totalTokensUsed: results.reduce((sum, r) => sum + r.tokensUsed, 0)
|
|
3135
|
+
};
|
|
3136
|
+
}
|
|
3137
|
+
getModelInfo() {
|
|
3138
|
+
return this.modelInfo;
|
|
3139
|
+
}
|
|
3140
|
+
};
|
|
3141
|
+
var CustomEmbeddingProvider = class {
|
|
3142
|
+
constructor(credentials, modelInfo) {
|
|
3143
|
+
this.credentials = credentials;
|
|
3144
|
+
this.modelInfo = modelInfo;
|
|
3145
|
+
}
|
|
3146
|
+
splitIntoRequestBatches(texts) {
|
|
3147
|
+
const maxBatchSize = this.modelInfo.maxBatchSize;
|
|
3148
|
+
if (!maxBatchSize || texts.length <= maxBatchSize) {
|
|
3149
|
+
return [texts];
|
|
3150
|
+
}
|
|
3151
|
+
const batches = [];
|
|
3152
|
+
for (let i = 0; i < texts.length; i += maxBatchSize) {
|
|
3153
|
+
batches.push(texts.slice(i, i + maxBatchSize));
|
|
2733
3154
|
}
|
|
2734
3155
|
return batches;
|
|
2735
3156
|
}
|
|
@@ -2911,8 +3332,8 @@ var SiliconFlowReranker = class {
|
|
|
2911
3332
|
|
|
2912
3333
|
// src/utils/files.ts
|
|
2913
3334
|
var import_ignore = __toESM(require_ignore(), 1);
|
|
2914
|
-
import { existsSync as
|
|
2915
|
-
import * as
|
|
3335
|
+
import { existsSync as existsSync5, readFileSync as readFileSync5, promises as fsPromises } from "fs";
|
|
3336
|
+
import * as path6 from "path";
|
|
2916
3337
|
function createIgnoreFilter(projectRoot) {
|
|
2917
3338
|
const ig = (0, import_ignore.default)();
|
|
2918
3339
|
const defaultIgnores = [
|
|
@@ -2933,9 +3354,9 @@ function createIgnoreFilter(projectRoot) {
|
|
|
2933
3354
|
"**/*build*/**"
|
|
2934
3355
|
];
|
|
2935
3356
|
ig.add(defaultIgnores);
|
|
2936
|
-
const gitignorePath =
|
|
2937
|
-
if (
|
|
2938
|
-
const gitignoreContent =
|
|
3357
|
+
const gitignorePath = path6.join(projectRoot, ".gitignore");
|
|
3358
|
+
if (existsSync5(gitignorePath)) {
|
|
3359
|
+
const gitignoreContent = readFileSync5(gitignorePath, "utf-8");
|
|
2939
3360
|
ig.add(gitignoreContent);
|
|
2940
3361
|
}
|
|
2941
3362
|
return ig;
|
|
@@ -2960,8 +3381,8 @@ async function* walkDirectory(dir, projectRoot, includePatterns, excludePatterns
|
|
|
2960
3381
|
const filesInDir = [];
|
|
2961
3382
|
const subdirs = [];
|
|
2962
3383
|
for (const entry of entries) {
|
|
2963
|
-
const fullPath =
|
|
2964
|
-
const relativePath =
|
|
3384
|
+
const fullPath = path6.join(dir, entry.name);
|
|
3385
|
+
const relativePath = path6.relative(projectRoot, fullPath);
|
|
2965
3386
|
if (entry.name.startsWith(".") && entry.name !== "." && entry.name !== "..") {
|
|
2966
3387
|
if (entry.isDirectory()) {
|
|
2967
3388
|
skipped.push({ path: relativePath, reason: "excluded" });
|
|
@@ -3010,7 +3431,7 @@ async function* walkDirectory(dir, projectRoot, includePatterns, excludePatterns
|
|
|
3010
3431
|
yield f;
|
|
3011
3432
|
}
|
|
3012
3433
|
for (let i = options.maxFilesPerDirectory; i < filesInDir.length; i++) {
|
|
3013
|
-
skipped.push({ path:
|
|
3434
|
+
skipped.push({ path: path6.relative(projectRoot, filesInDir[i].path), reason: "excluded" });
|
|
3014
3435
|
}
|
|
3015
3436
|
const canRecurse = options.maxDepth === -1 || currentDepth < options.maxDepth;
|
|
3016
3437
|
if (canRecurse) {
|
|
@@ -3050,8 +3471,8 @@ async function collectFiles(projectRoot, includePatterns, excludePatterns, maxFi
|
|
|
3050
3471
|
if (additionalRoots && additionalRoots.length > 0) {
|
|
3051
3472
|
const normalizedRoots = /* @__PURE__ */ new Set();
|
|
3052
3473
|
for (const kbRoot of additionalRoots) {
|
|
3053
|
-
const resolved =
|
|
3054
|
-
|
|
3474
|
+
const resolved = path6.normalize(
|
|
3475
|
+
path6.isAbsolute(kbRoot) ? kbRoot : path6.resolve(projectRoot, kbRoot)
|
|
3055
3476
|
);
|
|
3056
3477
|
normalizedRoots.add(resolved);
|
|
3057
3478
|
}
|
|
@@ -3441,13 +3862,13 @@ function initializeLogger(config) {
|
|
|
3441
3862
|
}
|
|
3442
3863
|
|
|
3443
3864
|
// src/native/index.ts
|
|
3444
|
-
import * as
|
|
3445
|
-
import * as
|
|
3865
|
+
import * as path7 from "path";
|
|
3866
|
+
import * as os4 from "os";
|
|
3446
3867
|
import * as module from "module";
|
|
3447
3868
|
import { fileURLToPath } from "url";
|
|
3448
3869
|
function getNativeBinding() {
|
|
3449
|
-
const platform2 =
|
|
3450
|
-
const arch2 =
|
|
3870
|
+
const platform2 = os4.platform();
|
|
3871
|
+
const arch2 = os4.arch();
|
|
3451
3872
|
let bindingName;
|
|
3452
3873
|
if (platform2 === "darwin" && arch2 === "arm64") {
|
|
3453
3874
|
bindingName = "codebase-index-native.darwin-arm64.node";
|
|
@@ -3465,18 +3886,19 @@ function getNativeBinding() {
|
|
|
3465
3886
|
let currentDir;
|
|
3466
3887
|
let requireTarget;
|
|
3467
3888
|
if (typeof import.meta !== "undefined" && import.meta.url) {
|
|
3468
|
-
currentDir =
|
|
3889
|
+
currentDir = path7.dirname(fileURLToPath(import.meta.url));
|
|
3469
3890
|
requireTarget = import.meta.url;
|
|
3470
3891
|
} else if (typeof __dirname !== "undefined") {
|
|
3471
3892
|
currentDir = __dirname;
|
|
3472
3893
|
requireTarget = __filename;
|
|
3473
3894
|
} else {
|
|
3474
3895
|
currentDir = process.cwd();
|
|
3475
|
-
requireTarget =
|
|
3896
|
+
requireTarget = path7.join(currentDir, "index.js");
|
|
3476
3897
|
}
|
|
3477
|
-
const
|
|
3478
|
-
const
|
|
3479
|
-
const
|
|
3898
|
+
const normalizedDir = currentDir.replace(/\\/g, "/");
|
|
3899
|
+
const isDevMode = normalizedDir.includes("/src/native") || currentDir.includes(path7.join("src", "native"));
|
|
3900
|
+
const packageRoot = isDevMode ? path7.resolve(currentDir, "../..") : path7.resolve(currentDir, "..");
|
|
3901
|
+
const nativePath = path7.join(packageRoot, "native", bindingName);
|
|
3480
3902
|
const require2 = module.createRequire(requireTarget);
|
|
3481
3903
|
return require2(nativePath);
|
|
3482
3904
|
}
|
|
@@ -3507,11 +3929,20 @@ function createMockNativeBinding() {
|
|
|
3507
3929
|
constructor() {
|
|
3508
3930
|
throw error;
|
|
3509
3931
|
}
|
|
3932
|
+
serialize() {
|
|
3933
|
+
throw error;
|
|
3934
|
+
}
|
|
3935
|
+
deserialize() {
|
|
3936
|
+
throw error;
|
|
3937
|
+
}
|
|
3510
3938
|
},
|
|
3511
3939
|
Database: class {
|
|
3512
3940
|
constructor() {
|
|
3513
3941
|
throw error;
|
|
3514
3942
|
}
|
|
3943
|
+
close() {
|
|
3944
|
+
throw error;
|
|
3945
|
+
}
|
|
3515
3946
|
}
|
|
3516
3947
|
};
|
|
3517
3948
|
}
|
|
@@ -3644,7 +4075,7 @@ var MAX_SINGLE_CHUNK_TOKENS = 2e3;
|
|
|
3644
4075
|
function estimateTokens2(text) {
|
|
3645
4076
|
return Math.ceil(text.length / CHARS_PER_TOKEN);
|
|
3646
4077
|
}
|
|
3647
|
-
function
|
|
4078
|
+
function getEmbeddingHeaderParts(chunk, filePath) {
|
|
3648
4079
|
const parts = [];
|
|
3649
4080
|
const fileName = filePath.split("/").pop() || filePath;
|
|
3650
4081
|
const dirPath = filePath.split("/").slice(-3, -1).join("/");
|
|
@@ -3691,23 +4122,52 @@ function createEmbeddingText(chunk, filePath) {
|
|
|
3691
4122
|
if (semanticHints.length > 0) {
|
|
3692
4123
|
parts.push(`Purpose: ${semanticHints.join(", ")}`);
|
|
3693
4124
|
}
|
|
3694
|
-
parts
|
|
3695
|
-
|
|
3696
|
-
|
|
3697
|
-
const
|
|
3698
|
-
if (
|
|
3699
|
-
|
|
4125
|
+
return parts;
|
|
4126
|
+
}
|
|
4127
|
+
function buildEmbeddingText(headerParts, content, partIndex, partCount) {
|
|
4128
|
+
const parts = [...headerParts];
|
|
4129
|
+
if (partCount && partCount > 1 && partIndex) {
|
|
4130
|
+
parts.push(`Part ${partIndex}/${partCount}`);
|
|
3700
4131
|
}
|
|
4132
|
+
parts.push("");
|
|
3701
4133
|
parts.push(content);
|
|
3702
4134
|
return parts.join("\n");
|
|
3703
4135
|
}
|
|
3704
|
-
function
|
|
4136
|
+
function splitOversizedContent(content, maxContentChars) {
|
|
4137
|
+
if (content.length <= maxContentChars) {
|
|
4138
|
+
return [content];
|
|
4139
|
+
}
|
|
4140
|
+
const overlapChars = Math.max(CHARS_PER_TOKEN * 32, Math.min(Math.floor(maxContentChars * 0.15), CHARS_PER_TOKEN * 128));
|
|
4141
|
+
const stepChars = Math.max(1, maxContentChars - overlapChars);
|
|
4142
|
+
const segments = [];
|
|
4143
|
+
for (let start = 0; start < content.length; start += stepChars) {
|
|
4144
|
+
const end = Math.min(content.length, start + maxContentChars);
|
|
4145
|
+
segments.push(content.slice(start, end));
|
|
4146
|
+
if (end >= content.length) {
|
|
4147
|
+
break;
|
|
4148
|
+
}
|
|
4149
|
+
}
|
|
4150
|
+
return segments;
|
|
4151
|
+
}
|
|
4152
|
+
function createEmbeddingTexts(chunk, filePath, maxChunkTokens = MAX_SINGLE_CHUNK_TOKENS) {
|
|
4153
|
+
const headerParts = getEmbeddingHeaderParts(chunk, filePath);
|
|
4154
|
+
const headerLength = buildEmbeddingText(headerParts, "", 1, 9).length;
|
|
4155
|
+
const maxContentChars = Math.max(1, maxChunkTokens * CHARS_PER_TOKEN - headerLength);
|
|
4156
|
+
const segments = splitOversizedContent(chunk.content, maxContentChars);
|
|
4157
|
+
if (segments.length === 1) {
|
|
4158
|
+
return [buildEmbeddingText(headerParts, segments[0])];
|
|
4159
|
+
}
|
|
4160
|
+
return segments.map((segment, index) => buildEmbeddingText(headerParts, segment, index + 1, segments.length));
|
|
4161
|
+
}
|
|
4162
|
+
function createDynamicBatches(chunks, options = {}) {
|
|
3705
4163
|
const batches = [];
|
|
3706
4164
|
let currentBatch = [];
|
|
3707
4165
|
let currentTokens = 0;
|
|
4166
|
+
const maxBatchTokens = Math.max(1, options.maxBatchTokens ?? MAX_BATCH_TOKENS);
|
|
4167
|
+
const maxBatchItems = Math.max(1, options.maxBatchItems ?? Number.MAX_SAFE_INTEGER);
|
|
3708
4168
|
for (const chunk of chunks) {
|
|
3709
|
-
const chunkTokens = estimateTokens2(chunk.text);
|
|
3710
|
-
if (currentBatch.length > 0 && currentTokens + chunkTokens >
|
|
4169
|
+
const chunkTokens = chunk.tokenCount ?? estimateTokens2(chunk.text);
|
|
4170
|
+
if (currentBatch.length > 0 && (currentTokens + chunkTokens > maxBatchTokens || currentBatch.length >= maxBatchItems)) {
|
|
3711
4171
|
batches.push(currentBatch);
|
|
3712
4172
|
currentBatch = [];
|
|
3713
4173
|
currentTokens = 0;
|
|
@@ -3826,6 +4286,12 @@ var InvertedIndex = class {
|
|
|
3826
4286
|
save() {
|
|
3827
4287
|
this.inner.save();
|
|
3828
4288
|
}
|
|
4289
|
+
serialize() {
|
|
4290
|
+
return this.inner.serialize();
|
|
4291
|
+
}
|
|
4292
|
+
deserialize(json) {
|
|
4293
|
+
this.inner.deserialize(json);
|
|
4294
|
+
}
|
|
3829
4295
|
addChunk(chunkId, content) {
|
|
3830
4296
|
this.inner.addChunk(chunkId, content);
|
|
3831
4297
|
}
|
|
@@ -3852,267 +4318,263 @@ var InvertedIndex = class {
|
|
|
3852
4318
|
};
|
|
3853
4319
|
var Database = class {
|
|
3854
4320
|
inner;
|
|
4321
|
+
closed = false;
|
|
3855
4322
|
constructor(dbPath) {
|
|
3856
4323
|
this.inner = new native.Database(dbPath);
|
|
3857
4324
|
}
|
|
4325
|
+
throwIfClosed() {
|
|
4326
|
+
if (this.closed) {
|
|
4327
|
+
throw new Error("Database is closed");
|
|
4328
|
+
}
|
|
4329
|
+
}
|
|
4330
|
+
close() {
|
|
4331
|
+
if (this.closed) {
|
|
4332
|
+
return;
|
|
4333
|
+
}
|
|
4334
|
+
if (typeof this.inner.close === "function") {
|
|
4335
|
+
this.inner.close();
|
|
4336
|
+
}
|
|
4337
|
+
this.closed = true;
|
|
4338
|
+
}
|
|
3858
4339
|
embeddingExists(contentHash) {
|
|
4340
|
+
this.throwIfClosed();
|
|
3859
4341
|
return this.inner.embeddingExists(contentHash);
|
|
3860
4342
|
}
|
|
3861
4343
|
getEmbedding(contentHash) {
|
|
4344
|
+
this.throwIfClosed();
|
|
3862
4345
|
return this.inner.getEmbedding(contentHash) ?? null;
|
|
3863
4346
|
}
|
|
3864
4347
|
upsertEmbedding(contentHash, embedding, chunkText, model) {
|
|
4348
|
+
this.throwIfClosed();
|
|
3865
4349
|
this.inner.upsertEmbedding(contentHash, embedding, chunkText, model);
|
|
3866
4350
|
}
|
|
3867
4351
|
upsertEmbeddingsBatch(items) {
|
|
4352
|
+
this.throwIfClosed();
|
|
3868
4353
|
if (items.length === 0) return;
|
|
3869
4354
|
this.inner.upsertEmbeddingsBatch(items);
|
|
3870
4355
|
}
|
|
3871
4356
|
getMissingEmbeddings(contentHashes) {
|
|
4357
|
+
this.throwIfClosed();
|
|
3872
4358
|
return this.inner.getMissingEmbeddings(contentHashes);
|
|
3873
4359
|
}
|
|
3874
4360
|
upsertChunk(chunk) {
|
|
4361
|
+
this.throwIfClosed();
|
|
3875
4362
|
this.inner.upsertChunk(chunk);
|
|
3876
4363
|
}
|
|
3877
4364
|
upsertChunksBatch(chunks) {
|
|
4365
|
+
this.throwIfClosed();
|
|
3878
4366
|
if (chunks.length === 0) return;
|
|
3879
4367
|
this.inner.upsertChunksBatch(chunks);
|
|
3880
4368
|
}
|
|
3881
4369
|
getChunk(chunkId) {
|
|
4370
|
+
this.throwIfClosed();
|
|
3882
4371
|
return this.inner.getChunk(chunkId) ?? null;
|
|
3883
4372
|
}
|
|
3884
4373
|
getChunksByFile(filePath) {
|
|
4374
|
+
this.throwIfClosed();
|
|
3885
4375
|
return this.inner.getChunksByFile(filePath);
|
|
3886
4376
|
}
|
|
3887
4377
|
getChunksByName(name) {
|
|
4378
|
+
this.throwIfClosed();
|
|
3888
4379
|
return this.inner.getChunksByName(name);
|
|
3889
4380
|
}
|
|
3890
4381
|
getChunksByNameCi(name) {
|
|
4382
|
+
this.throwIfClosed();
|
|
3891
4383
|
return this.inner.getChunksByNameCi(name);
|
|
3892
4384
|
}
|
|
3893
4385
|
deleteChunksByFile(filePath) {
|
|
4386
|
+
this.throwIfClosed();
|
|
3894
4387
|
return this.inner.deleteChunksByFile(filePath);
|
|
3895
4388
|
}
|
|
4389
|
+
deleteChunksByIds(chunkIds) {
|
|
4390
|
+
this.throwIfClosed();
|
|
4391
|
+
if (chunkIds.length === 0) return 0;
|
|
4392
|
+
return this.inner.deleteChunksByIds(chunkIds);
|
|
4393
|
+
}
|
|
3896
4394
|
addChunksToBranch(branch, chunkIds) {
|
|
4395
|
+
this.throwIfClosed();
|
|
3897
4396
|
this.inner.addChunksToBranch(branch, chunkIds);
|
|
3898
4397
|
}
|
|
3899
4398
|
addChunksToBranchBatch(branch, chunkIds) {
|
|
4399
|
+
this.throwIfClosed();
|
|
3900
4400
|
if (chunkIds.length === 0) return;
|
|
3901
4401
|
this.inner.addChunksToBranchBatch(branch, chunkIds);
|
|
3902
4402
|
}
|
|
3903
4403
|
clearBranch(branch) {
|
|
4404
|
+
this.throwIfClosed();
|
|
3904
4405
|
return this.inner.clearBranch(branch);
|
|
3905
4406
|
}
|
|
4407
|
+
deleteBranchChunksByChunkIds(chunkIds) {
|
|
4408
|
+
this.throwIfClosed();
|
|
4409
|
+
if (chunkIds.length === 0) return 0;
|
|
4410
|
+
return this.inner.deleteBranchChunksByChunkIds(chunkIds);
|
|
4411
|
+
}
|
|
4412
|
+
deleteBranchChunksForBranch(branch, chunkIds) {
|
|
4413
|
+
this.throwIfClosed();
|
|
4414
|
+
if (chunkIds.length === 0) return 0;
|
|
4415
|
+
return this.inner.deleteBranchChunksForBranch(branch, chunkIds);
|
|
4416
|
+
}
|
|
3906
4417
|
getBranchChunkIds(branch) {
|
|
4418
|
+
this.throwIfClosed();
|
|
3907
4419
|
return this.inner.getBranchChunkIds(branch);
|
|
3908
4420
|
}
|
|
3909
4421
|
getBranchDelta(branch, baseBranch) {
|
|
4422
|
+
this.throwIfClosed();
|
|
3910
4423
|
return this.inner.getBranchDelta(branch, baseBranch);
|
|
3911
4424
|
}
|
|
4425
|
+
getReferencedChunkIds(chunkIds) {
|
|
4426
|
+
this.throwIfClosed();
|
|
4427
|
+
if (chunkIds.length === 0) return [];
|
|
4428
|
+
return this.inner.getReferencedChunkIds(chunkIds);
|
|
4429
|
+
}
|
|
3912
4430
|
chunkExistsOnBranch(branch, chunkId) {
|
|
4431
|
+
this.throwIfClosed();
|
|
3913
4432
|
return this.inner.chunkExistsOnBranch(branch, chunkId);
|
|
3914
4433
|
}
|
|
3915
4434
|
getAllBranches() {
|
|
4435
|
+
this.throwIfClosed();
|
|
3916
4436
|
return this.inner.getAllBranches();
|
|
3917
4437
|
}
|
|
3918
4438
|
getMetadata(key) {
|
|
4439
|
+
this.throwIfClosed();
|
|
3919
4440
|
return this.inner.getMetadata(key) ?? null;
|
|
3920
4441
|
}
|
|
3921
4442
|
setMetadata(key, value) {
|
|
4443
|
+
this.throwIfClosed();
|
|
3922
4444
|
this.inner.setMetadata(key, value);
|
|
3923
4445
|
}
|
|
3924
4446
|
deleteMetadata(key) {
|
|
4447
|
+
this.throwIfClosed();
|
|
3925
4448
|
return this.inner.deleteMetadata(key);
|
|
3926
4449
|
}
|
|
4450
|
+
clearAllIndexedData() {
|
|
4451
|
+
this.throwIfClosed();
|
|
4452
|
+
this.inner.clearAllIndexedData();
|
|
4453
|
+
}
|
|
4454
|
+
clearCallEdgeTargetsForSymbols(symbolIds) {
|
|
4455
|
+
this.throwIfClosed();
|
|
4456
|
+
if (symbolIds.length === 0) return 0;
|
|
4457
|
+
return this.inner.clearCallEdgeTargetsForSymbols(symbolIds);
|
|
4458
|
+
}
|
|
3927
4459
|
gcOrphanEmbeddings() {
|
|
4460
|
+
this.throwIfClosed();
|
|
3928
4461
|
return this.inner.gcOrphanEmbeddings();
|
|
3929
4462
|
}
|
|
3930
4463
|
gcOrphanChunks() {
|
|
4464
|
+
this.throwIfClosed();
|
|
3931
4465
|
return this.inner.gcOrphanChunks();
|
|
3932
4466
|
}
|
|
3933
4467
|
getStats() {
|
|
4468
|
+
this.throwIfClosed();
|
|
3934
4469
|
return this.inner.getStats();
|
|
3935
4470
|
}
|
|
3936
4471
|
// ── Symbol methods ──────────────────────────────────────────────
|
|
3937
4472
|
upsertSymbol(symbol) {
|
|
4473
|
+
this.throwIfClosed();
|
|
3938
4474
|
this.inner.upsertSymbol(symbol);
|
|
3939
4475
|
}
|
|
3940
4476
|
upsertSymbolsBatch(symbols) {
|
|
4477
|
+
this.throwIfClosed();
|
|
3941
4478
|
if (symbols.length === 0) return;
|
|
3942
4479
|
this.inner.upsertSymbolsBatch(symbols);
|
|
3943
4480
|
}
|
|
3944
4481
|
getSymbolsByFile(filePath) {
|
|
4482
|
+
this.throwIfClosed();
|
|
3945
4483
|
return this.inner.getSymbolsByFile(filePath);
|
|
3946
4484
|
}
|
|
3947
4485
|
getSymbolByName(name, filePath) {
|
|
4486
|
+
this.throwIfClosed();
|
|
3948
4487
|
return this.inner.getSymbolByName(name, filePath) ?? null;
|
|
3949
4488
|
}
|
|
3950
4489
|
getSymbolsByName(name) {
|
|
4490
|
+
this.throwIfClosed();
|
|
3951
4491
|
return this.inner.getSymbolsByName(name);
|
|
3952
4492
|
}
|
|
3953
4493
|
getSymbolsByNameCi(name) {
|
|
4494
|
+
this.throwIfClosed();
|
|
3954
4495
|
return this.inner.getSymbolsByNameCi(name);
|
|
3955
4496
|
}
|
|
3956
4497
|
deleteSymbolsByFile(filePath) {
|
|
4498
|
+
this.throwIfClosed();
|
|
3957
4499
|
return this.inner.deleteSymbolsByFile(filePath);
|
|
3958
4500
|
}
|
|
3959
4501
|
// ── Call Edge methods ────────────────────────────────────────────
|
|
3960
4502
|
upsertCallEdge(edge) {
|
|
4503
|
+
this.throwIfClosed();
|
|
3961
4504
|
this.inner.upsertCallEdge(edge);
|
|
3962
4505
|
}
|
|
3963
4506
|
upsertCallEdgesBatch(edges) {
|
|
4507
|
+
this.throwIfClosed();
|
|
3964
4508
|
if (edges.length === 0) return;
|
|
3965
4509
|
this.inner.upsertCallEdgesBatch(edges);
|
|
3966
4510
|
}
|
|
3967
4511
|
getCallers(targetName, branch) {
|
|
4512
|
+
this.throwIfClosed();
|
|
3968
4513
|
return this.inner.getCallers(targetName, branch);
|
|
3969
4514
|
}
|
|
3970
4515
|
getCallersWithContext(targetName, branch) {
|
|
4516
|
+
this.throwIfClosed();
|
|
3971
4517
|
return this.inner.getCallersWithContext(targetName, branch);
|
|
3972
4518
|
}
|
|
3973
4519
|
getCallees(symbolId, branch) {
|
|
4520
|
+
this.throwIfClosed();
|
|
3974
4521
|
return this.inner.getCallees(symbolId, branch);
|
|
3975
4522
|
}
|
|
3976
4523
|
deleteCallEdgesByFile(filePath) {
|
|
4524
|
+
this.throwIfClosed();
|
|
3977
4525
|
return this.inner.deleteCallEdgesByFile(filePath);
|
|
3978
4526
|
}
|
|
3979
4527
|
resolveCallEdge(edgeId, toSymbolId) {
|
|
4528
|
+
this.throwIfClosed();
|
|
3980
4529
|
this.inner.resolveCallEdge(edgeId, toSymbolId);
|
|
3981
4530
|
}
|
|
3982
4531
|
// ── Branch Symbol methods ────────────────────────────────────────
|
|
3983
4532
|
addSymbolsToBranch(branch, symbolIds) {
|
|
4533
|
+
this.throwIfClosed();
|
|
3984
4534
|
this.inner.addSymbolsToBranch(branch, symbolIds);
|
|
3985
4535
|
}
|
|
3986
4536
|
addSymbolsToBranchBatch(branch, symbolIds) {
|
|
4537
|
+
this.throwIfClosed();
|
|
3987
4538
|
if (symbolIds.length === 0) return;
|
|
3988
4539
|
this.inner.addSymbolsToBranchBatch(branch, symbolIds);
|
|
3989
4540
|
}
|
|
3990
4541
|
getBranchSymbolIds(branch) {
|
|
4542
|
+
this.throwIfClosed();
|
|
3991
4543
|
return this.inner.getBranchSymbolIds(branch);
|
|
3992
4544
|
}
|
|
3993
4545
|
clearBranchSymbols(branch) {
|
|
4546
|
+
this.throwIfClosed();
|
|
3994
4547
|
return this.inner.clearBranchSymbols(branch);
|
|
3995
4548
|
}
|
|
4549
|
+
getReferencedSymbolIds(symbolIds) {
|
|
4550
|
+
this.throwIfClosed();
|
|
4551
|
+
if (symbolIds.length === 0) return [];
|
|
4552
|
+
return this.inner.getReferencedSymbolIds(symbolIds);
|
|
4553
|
+
}
|
|
4554
|
+
deleteBranchSymbolsBySymbolIds(symbolIds) {
|
|
4555
|
+
this.throwIfClosed();
|
|
4556
|
+
if (symbolIds.length === 0) return 0;
|
|
4557
|
+
return this.inner.deleteBranchSymbolsBySymbolIds(symbolIds);
|
|
4558
|
+
}
|
|
4559
|
+
deleteBranchSymbolsForBranch(branch, symbolIds) {
|
|
4560
|
+
this.throwIfClosed();
|
|
4561
|
+
if (symbolIds.length === 0) return 0;
|
|
4562
|
+
return this.inner.deleteBranchSymbolsForBranch(branch, symbolIds);
|
|
4563
|
+
}
|
|
3996
4564
|
// ── GC methods for symbols/edges ─────────────────────────────────
|
|
3997
4565
|
gcOrphanSymbols() {
|
|
4566
|
+
this.throwIfClosed();
|
|
3998
4567
|
return this.inner.gcOrphanSymbols();
|
|
3999
4568
|
}
|
|
4000
4569
|
gcOrphanCallEdges() {
|
|
4570
|
+
this.throwIfClosed();
|
|
4001
4571
|
return this.inner.gcOrphanCallEdges();
|
|
4002
4572
|
}
|
|
4003
4573
|
};
|
|
4004
4574
|
|
|
4005
|
-
// src/git/index.ts
|
|
4006
|
-
import { existsSync as existsSync3, readFileSync as readFileSync4, readdirSync, statSync } from "fs";
|
|
4007
|
-
import * as path5 from "path";
|
|
4008
|
-
function readPackedRefs(gitDir) {
|
|
4009
|
-
const packedRefsPath = path5.join(gitDir, "packed-refs");
|
|
4010
|
-
if (!existsSync3(packedRefsPath)) {
|
|
4011
|
-
return [];
|
|
4012
|
-
}
|
|
4013
|
-
try {
|
|
4014
|
-
return readFileSync4(packedRefsPath, "utf-8").split("\n").map((line) => line.trim()).filter((line) => line.length > 0 && !line.startsWith("#") && !line.startsWith("^"));
|
|
4015
|
-
} catch {
|
|
4016
|
-
return [];
|
|
4017
|
-
}
|
|
4018
|
-
}
|
|
4019
|
-
function resolveCommonGitDir(gitDir) {
|
|
4020
|
-
const commonDirPath = path5.join(gitDir, "commondir");
|
|
4021
|
-
if (!existsSync3(commonDirPath)) {
|
|
4022
|
-
return gitDir;
|
|
4023
|
-
}
|
|
4024
|
-
try {
|
|
4025
|
-
const raw = readFileSync4(commonDirPath, "utf-8").trim();
|
|
4026
|
-
if (!raw) {
|
|
4027
|
-
return gitDir;
|
|
4028
|
-
}
|
|
4029
|
-
const resolved = path5.isAbsolute(raw) ? raw : path5.resolve(gitDir, raw);
|
|
4030
|
-
if (existsSync3(resolved)) {
|
|
4031
|
-
return resolved;
|
|
4032
|
-
}
|
|
4033
|
-
} catch {
|
|
4034
|
-
return gitDir;
|
|
4035
|
-
}
|
|
4036
|
-
return gitDir;
|
|
4037
|
-
}
|
|
4038
|
-
function resolveGitDir(repoRoot) {
|
|
4039
|
-
const gitPath = path5.join(repoRoot, ".git");
|
|
4040
|
-
if (!existsSync3(gitPath)) {
|
|
4041
|
-
return null;
|
|
4042
|
-
}
|
|
4043
|
-
try {
|
|
4044
|
-
const stat = statSync(gitPath);
|
|
4045
|
-
if (stat.isDirectory()) {
|
|
4046
|
-
return gitPath;
|
|
4047
|
-
}
|
|
4048
|
-
if (stat.isFile()) {
|
|
4049
|
-
const content = readFileSync4(gitPath, "utf-8").trim();
|
|
4050
|
-
const match = content.match(/^gitdir:\s*(.+)$/);
|
|
4051
|
-
if (match) {
|
|
4052
|
-
const gitdir = match[1];
|
|
4053
|
-
const resolvedPath = path5.isAbsolute(gitdir) ? gitdir : path5.resolve(repoRoot, gitdir);
|
|
4054
|
-
if (existsSync3(resolvedPath)) {
|
|
4055
|
-
return resolvedPath;
|
|
4056
|
-
}
|
|
4057
|
-
}
|
|
4058
|
-
}
|
|
4059
|
-
} catch {
|
|
4060
|
-
}
|
|
4061
|
-
return null;
|
|
4062
|
-
}
|
|
4063
|
-
function isGitRepo(dir) {
|
|
4064
|
-
return resolveGitDir(dir) !== null;
|
|
4065
|
-
}
|
|
4066
|
-
function getCurrentBranch(repoRoot) {
|
|
4067
|
-
const gitDir = resolveGitDir(repoRoot);
|
|
4068
|
-
if (!gitDir) {
|
|
4069
|
-
return null;
|
|
4070
|
-
}
|
|
4071
|
-
const headPath = path5.join(gitDir, "HEAD");
|
|
4072
|
-
if (!existsSync3(headPath)) {
|
|
4073
|
-
return null;
|
|
4074
|
-
}
|
|
4075
|
-
try {
|
|
4076
|
-
const headContent = readFileSync4(headPath, "utf-8").trim();
|
|
4077
|
-
const match = headContent.match(/^ref: refs\/heads\/(.+)$/);
|
|
4078
|
-
if (match) {
|
|
4079
|
-
return match[1];
|
|
4080
|
-
}
|
|
4081
|
-
if (/^[0-9a-f]{40}$/i.test(headContent)) {
|
|
4082
|
-
return headContent.slice(0, 7);
|
|
4083
|
-
}
|
|
4084
|
-
return null;
|
|
4085
|
-
} catch {
|
|
4086
|
-
return null;
|
|
4087
|
-
}
|
|
4088
|
-
}
|
|
4089
|
-
function getBaseBranch(repoRoot) {
|
|
4090
|
-
const gitDir = resolveGitDir(repoRoot);
|
|
4091
|
-
const refStoreDir = gitDir ? resolveCommonGitDir(gitDir) : null;
|
|
4092
|
-
const candidates = ["main", "master", "develop", "trunk"];
|
|
4093
|
-
if (refStoreDir) {
|
|
4094
|
-
for (const candidate of candidates) {
|
|
4095
|
-
const refPath = path5.join(refStoreDir, "refs", "heads", candidate);
|
|
4096
|
-
if (existsSync3(refPath)) {
|
|
4097
|
-
return candidate;
|
|
4098
|
-
}
|
|
4099
|
-
const packedRefs = readPackedRefs(refStoreDir);
|
|
4100
|
-
if (packedRefs.some((line) => line.endsWith(` refs/heads/${candidate}`))) {
|
|
4101
|
-
return candidate;
|
|
4102
|
-
}
|
|
4103
|
-
}
|
|
4104
|
-
}
|
|
4105
|
-
return getCurrentBranch(repoRoot) ?? "main";
|
|
4106
|
-
}
|
|
4107
|
-
function getBranchOrDefault(repoRoot) {
|
|
4108
|
-
if (!isGitRepo(repoRoot)) {
|
|
4109
|
-
return "default";
|
|
4110
|
-
}
|
|
4111
|
-
return getCurrentBranch(repoRoot) ?? "default";
|
|
4112
|
-
}
|
|
4113
|
-
|
|
4114
4575
|
// src/indexer/index.ts
|
|
4115
|
-
var CALL_GRAPH_LANGUAGES = /* @__PURE__ */ new Set(["typescript", "tsx", "javascript", "jsx", "python", "go", "rust", "php"]);
|
|
4576
|
+
var CALL_GRAPH_LANGUAGES = /* @__PURE__ */ new Set(["typescript", "tsx", "javascript", "jsx", "python", "go", "rust", "php", "apex", "zig"]);
|
|
4577
|
+
var CASE_INSENSITIVE_LANGUAGES = /* @__PURE__ */ new Set(["apex"]);
|
|
4116
4578
|
var CALL_GRAPH_SYMBOL_CHUNK_TYPES = /* @__PURE__ */ new Set([
|
|
4117
4579
|
"function_declaration",
|
|
4118
4580
|
"function",
|
|
@@ -4134,7 +4596,11 @@ var CALL_GRAPH_SYMBOL_CHUNK_TYPES = /* @__PURE__ */ new Set([
|
|
|
4134
4596
|
"enum_item",
|
|
4135
4597
|
"trait_item",
|
|
4136
4598
|
"mod_item",
|
|
4137
|
-
"trait_declaration"
|
|
4599
|
+
"trait_declaration",
|
|
4600
|
+
"trigger_declaration",
|
|
4601
|
+
"test_declaration",
|
|
4602
|
+
"struct_declaration",
|
|
4603
|
+
"union_declaration"
|
|
4138
4604
|
]);
|
|
4139
4605
|
function float32ArrayToBuffer(arr) {
|
|
4140
4606
|
const float32 = new Float32Array(arr);
|
|
@@ -4159,12 +4625,192 @@ function isRateLimitError(error) {
|
|
|
4159
4625
|
const message = getErrorMessage(error);
|
|
4160
4626
|
return message.includes("429") || message.toLowerCase().includes("rate limit") || message.toLowerCase().includes("too many requests");
|
|
4161
4627
|
}
|
|
4628
|
+
function getSafeEmbeddingChunkTokenLimit(provider) {
|
|
4629
|
+
const providerMaxTokens = provider.modelInfo.maxTokens;
|
|
4630
|
+
const maxChunkTokens = Math.max(256, Math.floor(providerMaxTokens * 0.75));
|
|
4631
|
+
return Math.min(2e3, maxChunkTokens);
|
|
4632
|
+
}
|
|
4633
|
+
function getDynamicBatchOptions(provider) {
|
|
4634
|
+
if (provider.provider === "ollama") {
|
|
4635
|
+
return {
|
|
4636
|
+
maxBatchTokens: provider.modelInfo.maxTokens,
|
|
4637
|
+
maxBatchItems: 1
|
|
4638
|
+
};
|
|
4639
|
+
}
|
|
4640
|
+
return {};
|
|
4641
|
+
}
|
|
4642
|
+
function isSqliteCorruptionError(error) {
|
|
4643
|
+
const message = getErrorMessage(error).toLowerCase();
|
|
4644
|
+
return message.includes("database disk image is malformed") || message.includes("file is not a database") || message.includes("database schema is corrupt") || message.includes("sqlite_corrupt");
|
|
4645
|
+
}
|
|
4646
|
+
var STARTUP_WARNING_METADATA_KEY = "index.startupWarning";
|
|
4162
4647
|
var INDEX_METADATA_VERSION = "1";
|
|
4648
|
+
var EMBEDDING_STRATEGY_VERSION = "2";
|
|
4163
4649
|
var RANKING_TOKEN_CACHE_LIMIT = 4096;
|
|
4650
|
+
var RANK_HYBRID_CACHE_LIMIT = 256;
|
|
4651
|
+
function createPendingChunkStorageText(texts) {
|
|
4652
|
+
const primaryText = texts[0]?.text ?? "";
|
|
4653
|
+
if (texts.length <= 1) {
|
|
4654
|
+
return primaryText;
|
|
4655
|
+
}
|
|
4656
|
+
return `${primaryText}
|
|
4657
|
+
|
|
4658
|
+
... [split into ${texts.length} parts for embedding]`;
|
|
4659
|
+
}
|
|
4660
|
+
function normalizePendingChunk(rawChunk, maxChunkTokens) {
|
|
4661
|
+
if (!rawChunk || typeof rawChunk !== "object") {
|
|
4662
|
+
return null;
|
|
4663
|
+
}
|
|
4664
|
+
const chunk = rawChunk;
|
|
4665
|
+
if (typeof chunk.id !== "string" || typeof chunk.contentHash !== "string" || !chunk.metadata || typeof chunk.metadata !== "object") {
|
|
4666
|
+
return null;
|
|
4667
|
+
}
|
|
4668
|
+
const texts = Array.isArray(chunk.texts) ? chunk.texts.map((entry) => {
|
|
4669
|
+
if (!entry || typeof entry.text !== "string") {
|
|
4670
|
+
return null;
|
|
4671
|
+
}
|
|
4672
|
+
return {
|
|
4673
|
+
text: entry.text,
|
|
4674
|
+
tokenCount: typeof entry.tokenCount === "number" && Number.isFinite(entry.tokenCount) ? entry.tokenCount : estimateTokens2(entry.text)
|
|
4675
|
+
};
|
|
4676
|
+
}).filter((entry) => entry !== null) : [];
|
|
4677
|
+
if (texts.length === 0 && typeof chunk.text === "string") {
|
|
4678
|
+
if (typeof chunk.content === "string" && chunk.content.length > 0 && chunk.metadata && typeof chunk.metadata === "object") {
|
|
4679
|
+
const metadata = chunk.metadata;
|
|
4680
|
+
const rebuiltChunk = {
|
|
4681
|
+
content: chunk.content,
|
|
4682
|
+
startLine: typeof metadata.startLine === "number" ? metadata.startLine : 1,
|
|
4683
|
+
endLine: typeof metadata.endLine === "number" ? metadata.endLine : 1,
|
|
4684
|
+
chunkType: typeof metadata.chunkType === "string" ? metadata.chunkType : "other",
|
|
4685
|
+
name: typeof metadata.name === "string" ? metadata.name : void 0,
|
|
4686
|
+
language: typeof metadata.language === "string" ? metadata.language : "text"
|
|
4687
|
+
};
|
|
4688
|
+
const filePath = typeof metadata.filePath === "string" ? metadata.filePath : "unknown";
|
|
4689
|
+
texts.push(
|
|
4690
|
+
...createEmbeddingTexts(rebuiltChunk, filePath, maxChunkTokens).map((text) => ({
|
|
4691
|
+
text,
|
|
4692
|
+
tokenCount: estimateTokens2(text)
|
|
4693
|
+
}))
|
|
4694
|
+
);
|
|
4695
|
+
} else {
|
|
4696
|
+
texts.push({
|
|
4697
|
+
text: chunk.text,
|
|
4698
|
+
tokenCount: estimateTokens2(chunk.text)
|
|
4699
|
+
});
|
|
4700
|
+
}
|
|
4701
|
+
}
|
|
4702
|
+
if (texts.length === 0) {
|
|
4703
|
+
return null;
|
|
4704
|
+
}
|
|
4705
|
+
return {
|
|
4706
|
+
id: chunk.id,
|
|
4707
|
+
texts,
|
|
4708
|
+
storageText: typeof chunk.storageText === "string" ? chunk.storageText : createPendingChunkStorageText(texts),
|
|
4709
|
+
content: typeof chunk.content === "string" ? chunk.content : "",
|
|
4710
|
+
contentHash: chunk.contentHash,
|
|
4711
|
+
metadata: chunk.metadata
|
|
4712
|
+
};
|
|
4713
|
+
}
|
|
4714
|
+
function getPendingChunkFilePath(rawChunk) {
|
|
4715
|
+
if (!rawChunk || typeof rawChunk !== "object") {
|
|
4716
|
+
return null;
|
|
4717
|
+
}
|
|
4718
|
+
const chunk = rawChunk;
|
|
4719
|
+
if (!chunk.metadata || typeof chunk.metadata !== "object") {
|
|
4720
|
+
return null;
|
|
4721
|
+
}
|
|
4722
|
+
const metadata = chunk.metadata;
|
|
4723
|
+
return typeof metadata.filePath === "string" ? metadata.filePath : null;
|
|
4724
|
+
}
|
|
4725
|
+
function normalizeFailedBatch(batch, maxChunkTokens) {
|
|
4726
|
+
const chunks = batch.chunks.map((chunk) => normalizePendingChunk(chunk, maxChunkTokens)).filter((chunk) => chunk !== null);
|
|
4727
|
+
if (chunks.length === 0) {
|
|
4728
|
+
return null;
|
|
4729
|
+
}
|
|
4730
|
+
return {
|
|
4731
|
+
chunks,
|
|
4732
|
+
error: batch.error,
|
|
4733
|
+
attemptCount: batch.attemptCount,
|
|
4734
|
+
lastAttempt: batch.lastAttempt
|
|
4735
|
+
};
|
|
4736
|
+
}
|
|
4737
|
+
function createPendingEmbeddingRequests(chunks) {
|
|
4738
|
+
return chunks.flatMap(
|
|
4739
|
+
(chunk) => chunk.texts.map((textPart, partIndex) => ({
|
|
4740
|
+
chunk,
|
|
4741
|
+
partIndex,
|
|
4742
|
+
text: textPart.text,
|
|
4743
|
+
tokenCount: textPart.tokenCount
|
|
4744
|
+
}))
|
|
4745
|
+
);
|
|
4746
|
+
}
|
|
4747
|
+
function createPendingEmbeddingRequestBatches(chunks, options = {}) {
|
|
4748
|
+
return createDynamicBatches(createPendingEmbeddingRequests(chunks), options);
|
|
4749
|
+
}
|
|
4750
|
+
function getUniquePendingChunksFromRequests(requests) {
|
|
4751
|
+
const uniqueChunks = /* @__PURE__ */ new Map();
|
|
4752
|
+
for (const request of requests) {
|
|
4753
|
+
uniqueChunks.set(request.chunk.id, request.chunk);
|
|
4754
|
+
}
|
|
4755
|
+
return Array.from(uniqueChunks.values());
|
|
4756
|
+
}
|
|
4757
|
+
function coalesceFailedBatches(batches) {
|
|
4758
|
+
const grouped = /* @__PURE__ */ new Map();
|
|
4759
|
+
for (const batch of batches) {
|
|
4760
|
+
const key = `${batch.attemptCount}:${batch.lastAttempt}:${batch.error}`;
|
|
4761
|
+
const existing = grouped.get(key);
|
|
4762
|
+
if (!existing) {
|
|
4763
|
+
grouped.set(key, {
|
|
4764
|
+
...batch,
|
|
4765
|
+
chunks: [...batch.chunks]
|
|
4766
|
+
});
|
|
4767
|
+
continue;
|
|
4768
|
+
}
|
|
4769
|
+
existing.chunks.push(...batch.chunks);
|
|
4770
|
+
}
|
|
4771
|
+
return Array.from(grouped.values());
|
|
4772
|
+
}
|
|
4773
|
+
function poolEmbeddingVectors(vectors, weights) {
|
|
4774
|
+
const firstVector = vectors[0];
|
|
4775
|
+
if (!firstVector) {
|
|
4776
|
+
return [];
|
|
4777
|
+
}
|
|
4778
|
+
const pooled = new Array(firstVector.length).fill(0);
|
|
4779
|
+
let totalWeight = 0;
|
|
4780
|
+
for (let index = 0; index < vectors.length; index++) {
|
|
4781
|
+
const vector = vectors[index];
|
|
4782
|
+
const weight = Math.max(1, weights[index] ?? 1);
|
|
4783
|
+
totalWeight += weight;
|
|
4784
|
+
for (let dimension = 0; dimension < vector.length; dimension++) {
|
|
4785
|
+
pooled[dimension] += vector[dimension] * weight;
|
|
4786
|
+
}
|
|
4787
|
+
}
|
|
4788
|
+
if (totalWeight === 0) {
|
|
4789
|
+
return firstVector;
|
|
4790
|
+
}
|
|
4791
|
+
return pooled.map((value) => value / totalWeight);
|
|
4792
|
+
}
|
|
4793
|
+
function hasAllEmbeddingParts(parts, expectedPartCount) {
|
|
4794
|
+
if (parts.length !== expectedPartCount) {
|
|
4795
|
+
return false;
|
|
4796
|
+
}
|
|
4797
|
+
for (let index = 0; index < expectedPartCount; index++) {
|
|
4798
|
+
if (parts[index] === void 0) {
|
|
4799
|
+
return false;
|
|
4800
|
+
}
|
|
4801
|
+
}
|
|
4802
|
+
return true;
|
|
4803
|
+
}
|
|
4804
|
+
function isPathWithinRoot(filePath, rootPath) {
|
|
4805
|
+
const normalizedFilePath = path8.resolve(filePath);
|
|
4806
|
+
const normalizedRoot = path8.resolve(rootPath);
|
|
4807
|
+
return normalizedFilePath === normalizedRoot || normalizedFilePath.startsWith(`${normalizedRoot}${path8.sep}`);
|
|
4808
|
+
}
|
|
4164
4809
|
var rankingQueryTokenCache = /* @__PURE__ */ new Map();
|
|
4165
4810
|
var rankingNameTokenCache = /* @__PURE__ */ new Map();
|
|
4166
4811
|
var rankingPathTokenCache = /* @__PURE__ */ new Map();
|
|
4167
4812
|
var rankingTextTokenCache = /* @__PURE__ */ new Map();
|
|
4813
|
+
var rankHybridResultsCache = /* @__PURE__ */ new WeakMap();
|
|
4168
4814
|
var STOPWORDS = /* @__PURE__ */ new Set([
|
|
4169
4815
|
"the",
|
|
4170
4816
|
"and",
|
|
@@ -4380,7 +5026,16 @@ function classifyExternalRerankBand(candidate, preferSourcePaths, docIntent) {
|
|
|
4380
5026
|
function classifyQueryIntent(tokens) {
|
|
4381
5027
|
const sourceIntentHits = tokens.filter((t) => SOURCE_INTENT_HINTS.has(t)).length;
|
|
4382
5028
|
const docTestIntentHits = tokens.filter((t) => DOC_TEST_INTENT_HINTS.has(t)).length;
|
|
4383
|
-
|
|
5029
|
+
if (sourceIntentHits === 0 && docTestIntentHits === 0) {
|
|
5030
|
+
return "neutral";
|
|
5031
|
+
}
|
|
5032
|
+
if (sourceIntentHits > docTestIntentHits) {
|
|
5033
|
+
return "source";
|
|
5034
|
+
}
|
|
5035
|
+
if (docTestIntentHits > sourceIntentHits) {
|
|
5036
|
+
return "doc_test";
|
|
5037
|
+
}
|
|
5038
|
+
return "neutral";
|
|
4384
5039
|
}
|
|
4385
5040
|
function classifyQueryIntentRaw(query) {
|
|
4386
5041
|
const lowerQuery = query.toLowerCase();
|
|
@@ -4402,7 +5057,7 @@ function classifyQueryIntentRaw(query) {
|
|
|
4402
5057
|
if (docTestRawHits > sourceRawHits) {
|
|
4403
5058
|
return "doc_test";
|
|
4404
5059
|
}
|
|
4405
|
-
if (sourceRawHits > docTestRawHits) {
|
|
5060
|
+
if (sourceRawHits > docTestRawHits && sourceRawHits > 0) {
|
|
4406
5061
|
return "source";
|
|
4407
5062
|
}
|
|
4408
5063
|
const hasWhereIsPattern = /\bwhere\s+is\b/.test(lowerQuery);
|
|
@@ -4669,9 +5324,8 @@ function rerankResults(query, candidates, rerankTopN, options) {
|
|
|
4669
5324
|
return candidates;
|
|
4670
5325
|
}
|
|
4671
5326
|
const queryTokenList = Array.from(queryTokens);
|
|
4672
|
-
const intent = classifyQueryIntentRaw(query);
|
|
4673
5327
|
const docIntent = classifyDocIntent(queryTokenList);
|
|
4674
|
-
const preferSourcePaths = options?.prioritizeSourcePaths ??
|
|
5328
|
+
const preferSourcePaths = options?.prioritizeSourcePaths ?? classifyQueryIntentRaw(query) === "source";
|
|
4675
5329
|
const identifierHints = extractIdentifierHints(query);
|
|
4676
5330
|
const head = candidates.slice(0, topN).map((candidate, idx) => {
|
|
4677
5331
|
const pathTokens = splitPathTokens(candidate.metadata.filePath);
|
|
@@ -4821,13 +5475,38 @@ function buildDiversityKey(metadata) {
|
|
|
4821
5475
|
return normalizedPath;
|
|
4822
5476
|
}
|
|
4823
5477
|
function rankHybridResults(query, semanticResults, keywordResults, options) {
|
|
5478
|
+
const prioritizeSourcePaths = options.prioritizeSourcePaths ?? classifyQueryIntentRaw(query) === "source";
|
|
5479
|
+
const cacheKey = `${query}${options.fusionStrategy}|${options.rrfK}|${options.hybridWeight}|${options.rerankTopN}|${options.limit}|${prioritizeSourcePaths ? 1 : 0}`;
|
|
5480
|
+
let byKeyword = rankHybridResultsCache.get(semanticResults);
|
|
5481
|
+
if (!byKeyword) {
|
|
5482
|
+
byKeyword = /* @__PURE__ */ new WeakMap();
|
|
5483
|
+
rankHybridResultsCache.set(semanticResults, byKeyword);
|
|
5484
|
+
}
|
|
5485
|
+
let bucket = byKeyword.get(keywordResults);
|
|
5486
|
+
if (!bucket) {
|
|
5487
|
+
bucket = /* @__PURE__ */ new Map();
|
|
5488
|
+
byKeyword.set(keywordResults, bucket);
|
|
5489
|
+
} else {
|
|
5490
|
+
const cached = bucket.get(cacheKey);
|
|
5491
|
+
if (cached) {
|
|
5492
|
+
return cached;
|
|
5493
|
+
}
|
|
5494
|
+
}
|
|
4824
5495
|
const overfetchLimit = Math.max(options.limit * 4, options.limit);
|
|
4825
5496
|
const fused = options.fusionStrategy === "rrf" ? fuseResultsRrf(semanticResults, keywordResults, options.rrfK, overfetchLimit) : fuseResultsWeighted(semanticResults, keywordResults, options.hybridWeight, overfetchLimit);
|
|
4826
5497
|
const rerankPoolLimit = Math.max(overfetchLimit, options.rerankTopN * 3, options.limit * 6);
|
|
4827
5498
|
const rerankPool = fused.slice(0, rerankPoolLimit);
|
|
4828
|
-
|
|
4829
|
-
prioritizeSourcePaths
|
|
5499
|
+
const ranked = rerankResults(query, rerankPool, options.rerankTopN, {
|
|
5500
|
+
prioritizeSourcePaths
|
|
4830
5501
|
});
|
|
5502
|
+
if (bucket.size >= RANK_HYBRID_CACHE_LIMIT) {
|
|
5503
|
+
const oldest = bucket.keys().next().value;
|
|
5504
|
+
if (oldest !== void 0) {
|
|
5505
|
+
bucket.delete(oldest);
|
|
5506
|
+
}
|
|
5507
|
+
}
|
|
5508
|
+
bucket.set(cacheKey, ranked);
|
|
5509
|
+
return ranked;
|
|
4831
5510
|
}
|
|
4832
5511
|
function rankSemanticOnlyResults(query, semanticResults, options) {
|
|
4833
5512
|
const overfetchLimit = Math.max(options.limit * 4, options.limit);
|
|
@@ -5124,6 +5803,21 @@ function mergeTieredResults(symbolLane, hybridLane, limit) {
|
|
|
5124
5803
|
}
|
|
5125
5804
|
return out;
|
|
5126
5805
|
}
|
|
5806
|
+
function matchesSearchFilters(candidate, options, minScore) {
|
|
5807
|
+
if (candidate.score < minScore) return false;
|
|
5808
|
+
if (options?.fileType) {
|
|
5809
|
+
const ext = candidate.metadata.filePath.split(".").pop()?.toLowerCase();
|
|
5810
|
+
if (ext !== options.fileType.toLowerCase().replace(/^\./, "")) return false;
|
|
5811
|
+
}
|
|
5812
|
+
if (options?.directory) {
|
|
5813
|
+
const normalizedDir = options.directory.replace(/^\/|\/$/g, "");
|
|
5814
|
+
if (!candidate.metadata.filePath.includes(`/${normalizedDir}/`) && !candidate.metadata.filePath.includes(`${normalizedDir}/`)) return false;
|
|
5815
|
+
}
|
|
5816
|
+
if (options?.chunkType && candidate.metadata.chunkType !== options.chunkType) {
|
|
5817
|
+
return false;
|
|
5818
|
+
}
|
|
5819
|
+
return true;
|
|
5820
|
+
}
|
|
5127
5821
|
function unionCandidates(semanticCandidates, keywordCandidates) {
|
|
5128
5822
|
const byId = /* @__PURE__ */ new Map();
|
|
5129
5823
|
for (const candidate of semanticCandidates) {
|
|
@@ -5163,22 +5857,18 @@ var Indexer = class {
|
|
|
5163
5857
|
this.projectRoot = projectRoot;
|
|
5164
5858
|
this.config = config;
|
|
5165
5859
|
this.indexPath = this.getIndexPath();
|
|
5166
|
-
this.fileHashCachePath =
|
|
5167
|
-
this.failedBatchesPath =
|
|
5168
|
-
this.indexingLockPath =
|
|
5860
|
+
this.fileHashCachePath = path8.join(this.indexPath, "file-hashes.json");
|
|
5861
|
+
this.failedBatchesPath = path8.join(this.indexPath, "failed-batches.json");
|
|
5862
|
+
this.indexingLockPath = path8.join(this.indexPath, "indexing.lock");
|
|
5169
5863
|
this.logger = initializeLogger(config.debug);
|
|
5170
5864
|
}
|
|
5171
5865
|
getIndexPath() {
|
|
5172
|
-
|
|
5173
|
-
const homeDir = process.env.HOME || process.env.USERPROFILE || "";
|
|
5174
|
-
return path6.join(homeDir, ".opencode", "global-index");
|
|
5175
|
-
}
|
|
5176
|
-
return path6.join(this.projectRoot, ".opencode", "index");
|
|
5866
|
+
return resolveProjectIndexPath(this.projectRoot, this.config.scope);
|
|
5177
5867
|
}
|
|
5178
5868
|
loadFileHashCache() {
|
|
5179
5869
|
try {
|
|
5180
|
-
if (
|
|
5181
|
-
const data =
|
|
5870
|
+
if (existsSync6(this.fileHashCachePath)) {
|
|
5871
|
+
const data = readFileSync6(this.fileHashCachePath, "utf-8");
|
|
5182
5872
|
const parsed = JSON.parse(data);
|
|
5183
5873
|
this.fileHashCache = new Map(Object.entries(parsed));
|
|
5184
5874
|
}
|
|
@@ -5195,63 +5885,423 @@ var Indexer = class {
|
|
|
5195
5885
|
}
|
|
5196
5886
|
atomicWriteSync(targetPath, data) {
|
|
5197
5887
|
const tempPath = `${targetPath}.tmp`;
|
|
5198
|
-
|
|
5888
|
+
mkdirSync3(path8.dirname(targetPath), { recursive: true });
|
|
5889
|
+
writeFileSync3(tempPath, data);
|
|
5199
5890
|
renameSync(tempPath, targetPath);
|
|
5200
5891
|
}
|
|
5892
|
+
getScopedRoots() {
|
|
5893
|
+
const roots = /* @__PURE__ */ new Set([path8.resolve(this.projectRoot)]);
|
|
5894
|
+
for (const kbRoot of this.config.knowledgeBases) {
|
|
5895
|
+
roots.add(path8.resolve(this.projectRoot, kbRoot));
|
|
5896
|
+
}
|
|
5897
|
+
return Array.from(roots);
|
|
5898
|
+
}
|
|
5899
|
+
getBranchCatalogKey() {
|
|
5900
|
+
const branchName = this.currentBranch || "default";
|
|
5901
|
+
if (this.config.scope !== "global") {
|
|
5902
|
+
return branchName;
|
|
5903
|
+
}
|
|
5904
|
+
const projectHash = hashContent(path8.resolve(this.projectRoot)).slice(0, 16);
|
|
5905
|
+
return `${projectHash}:${branchName}`;
|
|
5906
|
+
}
|
|
5907
|
+
getLegacyBranchCatalogKey() {
|
|
5908
|
+
return this.currentBranch || "default";
|
|
5909
|
+
}
|
|
5910
|
+
getLegacyMigrationMetadataKey() {
|
|
5911
|
+
const projectHash = hashContent(path8.resolve(this.projectRoot)).slice(0, 16);
|
|
5912
|
+
return `index.globalBranchMigration.${projectHash}`;
|
|
5913
|
+
}
|
|
5914
|
+
getProjectEmbeddingStrategyMetadataKey() {
|
|
5915
|
+
const projectHash = hashContent(path8.resolve(this.projectRoot)).slice(0, 16);
|
|
5916
|
+
return `index.embeddingStrategyVersion.${projectHash}`;
|
|
5917
|
+
}
|
|
5918
|
+
getProjectForceReembedMetadataKey() {
|
|
5919
|
+
const projectHash = hashContent(path8.resolve(this.projectRoot)).slice(0, 16);
|
|
5920
|
+
return `index.forceReembed.${projectHash}`;
|
|
5921
|
+
}
|
|
5922
|
+
hasProjectForceReembedPending() {
|
|
5923
|
+
return this.config.scope === "global" && this.database?.getMetadata(this.getProjectForceReembedMetadataKey()) === "true";
|
|
5924
|
+
}
|
|
5925
|
+
hasScopedIndexedData() {
|
|
5926
|
+
if (!this.store || this.config.scope !== "global") {
|
|
5927
|
+
return false;
|
|
5928
|
+
}
|
|
5929
|
+
if (this.hasProjectForceReembedPending()) {
|
|
5930
|
+
return false;
|
|
5931
|
+
}
|
|
5932
|
+
const roots = this.getScopedRoots();
|
|
5933
|
+
if (Array.from(this.fileHashCache.keys()).some((filePath) => this.isFileInCurrentScope(filePath, roots))) {
|
|
5934
|
+
return true;
|
|
5935
|
+
}
|
|
5936
|
+
if (this.loadSerializedFailedBatches().some(
|
|
5937
|
+
(batch) => batch.chunks.some((chunk) => {
|
|
5938
|
+
const filePath = getPendingChunkFilePath(chunk);
|
|
5939
|
+
return filePath !== null && this.isFileInCurrentScope(filePath, roots);
|
|
5940
|
+
})
|
|
5941
|
+
)) {
|
|
5942
|
+
return true;
|
|
5943
|
+
}
|
|
5944
|
+
if (!this.database) {
|
|
5945
|
+
return false;
|
|
5946
|
+
}
|
|
5947
|
+
if (this.getBranchCatalogKeys().some((branchKey) => {
|
|
5948
|
+
const branchChunkIds = this.database.getBranchChunkIds(branchKey);
|
|
5949
|
+
if (branchChunkIds.length > 0) {
|
|
5950
|
+
return true;
|
|
5951
|
+
}
|
|
5952
|
+
return this.database.getBranchSymbolIds(branchKey).length > 0;
|
|
5953
|
+
})) {
|
|
5954
|
+
return true;
|
|
5955
|
+
}
|
|
5956
|
+
const hasAnyBranchRows = this.database.getAllBranches().some((branchKey) => {
|
|
5957
|
+
const branchChunkIds = this.database.getBranchChunkIds(branchKey);
|
|
5958
|
+
if (branchChunkIds.length > 0) {
|
|
5959
|
+
return true;
|
|
5960
|
+
}
|
|
5961
|
+
return this.database.getBranchSymbolIds(branchKey).length > 0;
|
|
5962
|
+
});
|
|
5963
|
+
if (hasAnyBranchRows) {
|
|
5964
|
+
return false;
|
|
5965
|
+
}
|
|
5966
|
+
return this.store.getAllMetadata().some(({ metadata }) => this.isFileInCurrentScope(metadata.filePath, roots));
|
|
5967
|
+
}
|
|
5968
|
+
loadStoredEmbeddingStrategyVersion() {
|
|
5969
|
+
if (!this.database) {
|
|
5970
|
+
return null;
|
|
5971
|
+
}
|
|
5972
|
+
if (this.hasProjectForceReembedPending()) {
|
|
5973
|
+
return null;
|
|
5974
|
+
}
|
|
5975
|
+
if (this.config.scope !== "global") {
|
|
5976
|
+
return this.database.getMetadata("index.embeddingStrategyVersion") ?? "1";
|
|
5977
|
+
}
|
|
5978
|
+
const projectVersion = this.database.getMetadata(this.getProjectEmbeddingStrategyMetadataKey());
|
|
5979
|
+
if (projectVersion) {
|
|
5980
|
+
return projectVersion;
|
|
5981
|
+
}
|
|
5982
|
+
const legacySharedVersion = this.database.getMetadata("index.embeddingStrategyVersion");
|
|
5983
|
+
if (legacySharedVersion && this.hasScopedIndexedData()) {
|
|
5984
|
+
return legacySharedVersion;
|
|
5985
|
+
}
|
|
5986
|
+
return null;
|
|
5987
|
+
}
|
|
5988
|
+
getBranchCatalogKeys() {
|
|
5989
|
+
const primary = this.getBranchCatalogKey();
|
|
5990
|
+
if (this.config.scope !== "global") {
|
|
5991
|
+
return [primary];
|
|
5992
|
+
}
|
|
5993
|
+
if (this.database?.getMetadata(this.getLegacyMigrationMetadataKey()) === "done") {
|
|
5994
|
+
return [primary];
|
|
5995
|
+
}
|
|
5996
|
+
const legacy = this.getLegacyBranchCatalogKey();
|
|
5997
|
+
return primary === legacy ? [primary] : [primary, legacy];
|
|
5998
|
+
}
|
|
5999
|
+
getBranchCatalogCleanupKeys() {
|
|
6000
|
+
const primary = this.getBranchCatalogKey();
|
|
6001
|
+
if (this.config.scope !== "global") {
|
|
6002
|
+
return [primary];
|
|
6003
|
+
}
|
|
6004
|
+
const legacy = this.getLegacyBranchCatalogKey();
|
|
6005
|
+
return primary === legacy ? [primary] : [primary, legacy];
|
|
6006
|
+
}
|
|
6007
|
+
getProjectLocalScopedOwnershipIds(roots) {
|
|
6008
|
+
const chunkIds = /* @__PURE__ */ new Set();
|
|
6009
|
+
const symbolIds = /* @__PURE__ */ new Set();
|
|
6010
|
+
if (!this.database) {
|
|
6011
|
+
return { chunkIds, symbolIds };
|
|
6012
|
+
}
|
|
6013
|
+
const projectRootPath = path8.resolve(this.projectRoot);
|
|
6014
|
+
const projectLocalFilePaths = /* @__PURE__ */ new Set([
|
|
6015
|
+
...Array.from(this.fileHashCache.keys()).filter(
|
|
6016
|
+
(filePath) => this.isFileInCurrentScope(filePath, roots) && isPathWithinRoot(filePath, projectRootPath)
|
|
6017
|
+
),
|
|
6018
|
+
...(this.store?.getAllMetadata() ?? []).map(({ metadata }) => metadata.filePath).filter(
|
|
6019
|
+
(filePath) => this.isFileInCurrentScope(filePath, roots) && isPathWithinRoot(filePath, projectRootPath)
|
|
6020
|
+
)
|
|
6021
|
+
]);
|
|
6022
|
+
for (const filePath of projectLocalFilePaths) {
|
|
6023
|
+
for (const chunk of this.database.getChunksByFile(filePath)) {
|
|
6024
|
+
chunkIds.add(chunk.chunkId);
|
|
6025
|
+
}
|
|
6026
|
+
for (const symbol of this.database.getSymbolsByFile(filePath)) {
|
|
6027
|
+
symbolIds.add(symbol.id);
|
|
6028
|
+
}
|
|
6029
|
+
}
|
|
6030
|
+
return { chunkIds, symbolIds };
|
|
6031
|
+
}
|
|
6032
|
+
getProjectScopedBranchCatalogCleanupKeys(projectChunkIds, projectSymbolIds) {
|
|
6033
|
+
if (this.config.scope !== "global") {
|
|
6034
|
+
return this.getBranchCatalogCleanupKeys();
|
|
6035
|
+
}
|
|
6036
|
+
const projectHash = hashContent(path8.resolve(this.projectRoot)).slice(0, 16);
|
|
6037
|
+
const keys = /* @__PURE__ */ new Set();
|
|
6038
|
+
const projectChunkIdSet = new Set(projectChunkIds);
|
|
6039
|
+
const projectSymbolIdSet = new Set(projectSymbolIds);
|
|
6040
|
+
for (const branchKey of this.database?.getAllBranches() ?? []) {
|
|
6041
|
+
if (branchKey.startsWith(`${projectHash}:`)) {
|
|
6042
|
+
keys.add(branchKey);
|
|
6043
|
+
continue;
|
|
6044
|
+
}
|
|
6045
|
+
if (branchKey.includes(":")) {
|
|
6046
|
+
continue;
|
|
6047
|
+
}
|
|
6048
|
+
const referencesProjectChunks = this.database?.getBranchChunkIds(branchKey).some((chunkId) => projectChunkIdSet.has(chunkId)) ?? false;
|
|
6049
|
+
const referencesProjectSymbols = this.database?.getBranchSymbolIds(branchKey).some((symbolId) => projectSymbolIdSet.has(symbolId)) ?? false;
|
|
6050
|
+
if (referencesProjectChunks || referencesProjectSymbols) {
|
|
6051
|
+
keys.add(branchKey);
|
|
6052
|
+
}
|
|
6053
|
+
}
|
|
6054
|
+
for (const branchKey of this.getBranchCatalogCleanupKeys()) {
|
|
6055
|
+
keys.add(branchKey);
|
|
6056
|
+
}
|
|
6057
|
+
return Array.from(keys);
|
|
6058
|
+
}
|
|
6059
|
+
isFileInCurrentScope(filePath, roots) {
|
|
6060
|
+
return roots.some((root) => isPathWithinRoot(filePath, root));
|
|
6061
|
+
}
|
|
6062
|
+
clearScopedFileHashCache(roots) {
|
|
6063
|
+
for (const filePath of Array.from(this.fileHashCache.keys())) {
|
|
6064
|
+
if (this.isFileInCurrentScope(filePath, roots)) {
|
|
6065
|
+
this.fileHashCache.delete(filePath);
|
|
6066
|
+
}
|
|
6067
|
+
}
|
|
6068
|
+
this.saveFileHashCache();
|
|
6069
|
+
}
|
|
6070
|
+
replaceScopedFileHashCache(currentFileHashes, roots) {
|
|
6071
|
+
for (const filePath of Array.from(this.fileHashCache.keys())) {
|
|
6072
|
+
if (this.isFileInCurrentScope(filePath, roots)) {
|
|
6073
|
+
this.fileHashCache.delete(filePath);
|
|
6074
|
+
}
|
|
6075
|
+
}
|
|
6076
|
+
for (const [filePath, hash] of currentFileHashes) {
|
|
6077
|
+
this.fileHashCache.set(filePath, hash);
|
|
6078
|
+
}
|
|
6079
|
+
this.saveFileHashCache();
|
|
6080
|
+
}
|
|
6081
|
+
partitionFailedBatches(roots, maxChunkTokens) {
|
|
6082
|
+
const scoped = [];
|
|
6083
|
+
const retained = [];
|
|
6084
|
+
for (const batch of this.loadSerializedFailedBatches()) {
|
|
6085
|
+
const scopedChunks = batch.chunks.filter((chunk) => {
|
|
6086
|
+
const filePath = getPendingChunkFilePath(chunk);
|
|
6087
|
+
return filePath !== null && this.isFileInCurrentScope(filePath, roots);
|
|
6088
|
+
});
|
|
6089
|
+
const retainedChunks = batch.chunks.filter((chunk) => {
|
|
6090
|
+
const filePath = getPendingChunkFilePath(chunk);
|
|
6091
|
+
return filePath === null || !this.isFileInCurrentScope(filePath, roots);
|
|
6092
|
+
});
|
|
6093
|
+
if (scopedChunks.length > 0) {
|
|
6094
|
+
const normalizedBatch = normalizeFailedBatch({ ...batch, chunks: scopedChunks }, maxChunkTokens);
|
|
6095
|
+
if (normalizedBatch) {
|
|
6096
|
+
scoped.push(normalizedBatch);
|
|
6097
|
+
}
|
|
6098
|
+
}
|
|
6099
|
+
if (retainedChunks.length > 0) {
|
|
6100
|
+
retained.push({ ...batch, chunks: retainedChunks });
|
|
6101
|
+
}
|
|
6102
|
+
}
|
|
6103
|
+
return { scoped, retained };
|
|
6104
|
+
}
|
|
6105
|
+
clearScopedFailedBatches(roots) {
|
|
6106
|
+
const { retained: retainedBatches } = this.partitionFailedBatches(roots);
|
|
6107
|
+
this.saveFailedBatches(retainedBatches);
|
|
6108
|
+
}
|
|
6109
|
+
hasForeignScopedFileHashData(roots) {
|
|
6110
|
+
return Array.from(this.fileHashCache.keys()).some((filePath) => !this.isFileInCurrentScope(filePath, roots));
|
|
6111
|
+
}
|
|
6112
|
+
hasForeignScopedFailedBatches(roots) {
|
|
6113
|
+
const { retained } = this.partitionFailedBatches(roots);
|
|
6114
|
+
return retained.length > 0;
|
|
6115
|
+
}
|
|
6116
|
+
hasForeignScopedBranchData() {
|
|
6117
|
+
if (!this.database || this.config.scope !== "global") {
|
|
6118
|
+
return false;
|
|
6119
|
+
}
|
|
6120
|
+
const projectHash = hashContent(path8.resolve(this.projectRoot)).slice(0, 16);
|
|
6121
|
+
const roots = this.getScopedRoots();
|
|
6122
|
+
const { chunkIds: projectLocalChunkIds, symbolIds: projectLocalSymbolIds } = this.getProjectLocalScopedOwnershipIds(roots);
|
|
6123
|
+
return this.database.getAllBranches().some(
|
|
6124
|
+
(branchKey) => {
|
|
6125
|
+
const branchChunkIds = this.database.getBranchChunkIds(branchKey);
|
|
6126
|
+
const branchSymbolIds = this.database.getBranchSymbolIds(branchKey);
|
|
6127
|
+
const hasBranchData = branchChunkIds.length > 0 || branchSymbolIds.length > 0;
|
|
6128
|
+
if (!hasBranchData) {
|
|
6129
|
+
return false;
|
|
6130
|
+
}
|
|
6131
|
+
if (branchKey.startsWith(`${projectHash}:`)) {
|
|
6132
|
+
return false;
|
|
6133
|
+
}
|
|
6134
|
+
if (!branchKey.includes(":")) {
|
|
6135
|
+
const referencesCurrentProjectChunks = branchChunkIds.some((chunkId) => projectLocalChunkIds.has(chunkId));
|
|
6136
|
+
const referencesCurrentProjectSymbols = branchSymbolIds.some((symbolId) => projectLocalSymbolIds.has(symbolId));
|
|
6137
|
+
return !(referencesCurrentProjectChunks || referencesCurrentProjectSymbols);
|
|
6138
|
+
}
|
|
6139
|
+
return true;
|
|
6140
|
+
}
|
|
6141
|
+
);
|
|
6142
|
+
}
|
|
6143
|
+
saveScopedFailedBatches(batches, roots) {
|
|
6144
|
+
const { retained } = this.partitionFailedBatches(roots);
|
|
6145
|
+
this.saveFailedBatches([...retained, ...batches]);
|
|
6146
|
+
}
|
|
6147
|
+
clearSharedIndexProjectData(store, invertedIndex, database, roots) {
|
|
6148
|
+
const allMetadata = store.getAllMetadata();
|
|
6149
|
+
const scopedEntries = allMetadata.filter(({ metadata }) => this.isFileInCurrentScope(metadata.filePath, roots));
|
|
6150
|
+
const filePaths = /* @__PURE__ */ new Set([
|
|
6151
|
+
...Array.from(this.fileHashCache.keys()).filter((filePath) => this.isFileInCurrentScope(filePath, roots)),
|
|
6152
|
+
...scopedEntries.map(({ metadata }) => metadata.filePath)
|
|
6153
|
+
]);
|
|
6154
|
+
const projectRootPath = path8.resolve(this.projectRoot);
|
|
6155
|
+
const projectLocalFilePaths = new Set(
|
|
6156
|
+
Array.from(filePaths).filter((filePath) => isPathWithinRoot(filePath, projectRootPath))
|
|
6157
|
+
);
|
|
6158
|
+
const removedChunkIds = new Set(scopedEntries.map(({ key }) => key));
|
|
6159
|
+
for (const filePath of filePaths) {
|
|
6160
|
+
for (const chunk of database.getChunksByFile(filePath)) {
|
|
6161
|
+
removedChunkIds.add(chunk.chunkId);
|
|
6162
|
+
}
|
|
6163
|
+
}
|
|
6164
|
+
const removedChunkIdList = Array.from(removedChunkIds);
|
|
6165
|
+
const projectLocalChunkIds = new Set(
|
|
6166
|
+
scopedEntries.filter(({ metadata }) => isPathWithinRoot(metadata.filePath, projectRootPath)).map(({ key }) => key)
|
|
6167
|
+
);
|
|
6168
|
+
for (const filePath of projectLocalFilePaths) {
|
|
6169
|
+
for (const chunk of database.getChunksByFile(filePath)) {
|
|
6170
|
+
projectLocalChunkIds.add(chunk.chunkId);
|
|
6171
|
+
}
|
|
6172
|
+
}
|
|
6173
|
+
const symbolIds = [];
|
|
6174
|
+
const projectLocalSymbolIds = /* @__PURE__ */ new Set();
|
|
6175
|
+
for (const filePath of filePaths) {
|
|
6176
|
+
for (const symbol of database.getSymbolsByFile(filePath)) {
|
|
6177
|
+
symbolIds.push(symbol.id);
|
|
6178
|
+
if (projectLocalFilePaths.has(filePath)) {
|
|
6179
|
+
projectLocalSymbolIds.add(symbol.id);
|
|
6180
|
+
}
|
|
6181
|
+
}
|
|
6182
|
+
}
|
|
6183
|
+
for (const branchKey of this.getProjectScopedBranchCatalogCleanupKeys(Array.from(projectLocalChunkIds), Array.from(projectLocalSymbolIds))) {
|
|
6184
|
+
database.deleteBranchChunksForBranch(branchKey, removedChunkIdList);
|
|
6185
|
+
}
|
|
6186
|
+
const sharedChunkIds = new Set(database.getReferencedChunkIds(removedChunkIdList));
|
|
6187
|
+
const removableChunkIds = removedChunkIdList.filter((chunkId) => !sharedChunkIds.has(chunkId));
|
|
6188
|
+
if (removableChunkIds.length > 0) {
|
|
6189
|
+
this.rebuildVectorStoreExcludingChunkIds(store, database, removableChunkIds);
|
|
6190
|
+
for (const chunkId of removableChunkIds) {
|
|
6191
|
+
invertedIndex.removeChunk(chunkId);
|
|
6192
|
+
}
|
|
6193
|
+
}
|
|
6194
|
+
for (const branchKey of this.getProjectScopedBranchCatalogCleanupKeys(Array.from(projectLocalChunkIds), Array.from(projectLocalSymbolIds))) {
|
|
6195
|
+
database.deleteBranchSymbolsForBranch(branchKey, symbolIds);
|
|
6196
|
+
}
|
|
6197
|
+
const sharedSymbolIds = new Set(database.getReferencedSymbolIds(symbolIds));
|
|
6198
|
+
const removableSymbolIds = symbolIds.filter((symbolId) => !sharedSymbolIds.has(symbolId));
|
|
6199
|
+
database.clearCallEdgeTargetsForSymbols(removableSymbolIds);
|
|
6200
|
+
for (const filePath of filePaths) {
|
|
6201
|
+
const fileChunkIds = database.getChunksByFile(filePath).map((chunk) => chunk.chunkId);
|
|
6202
|
+
const fileSymbols = database.getSymbolsByFile(filePath);
|
|
6203
|
+
if (fileChunkIds.every((chunkId) => !sharedChunkIds.has(chunkId))) {
|
|
6204
|
+
database.deleteChunksByFile(filePath);
|
|
6205
|
+
}
|
|
6206
|
+
if (fileSymbols.every((symbol) => !sharedSymbolIds.has(symbol.id))) {
|
|
6207
|
+
database.deleteCallEdgesByFile(filePath);
|
|
6208
|
+
database.deleteSymbolsByFile(filePath);
|
|
6209
|
+
}
|
|
6210
|
+
}
|
|
6211
|
+
database.gcOrphanCallEdges();
|
|
6212
|
+
database.gcOrphanSymbols();
|
|
6213
|
+
database.gcOrphanEmbeddings();
|
|
6214
|
+
database.gcOrphanChunks();
|
|
6215
|
+
store.save();
|
|
6216
|
+
invertedIndex.save();
|
|
6217
|
+
return {
|
|
6218
|
+
removedChunkIds: removedChunkIdList,
|
|
6219
|
+
hasForeignData: allMetadata.some(({ metadata }) => !this.isFileInCurrentScope(metadata.filePath, roots))
|
|
6220
|
+
};
|
|
6221
|
+
}
|
|
5201
6222
|
checkForInterruptedIndexing() {
|
|
5202
|
-
return
|
|
6223
|
+
return existsSync6(this.indexingLockPath);
|
|
5203
6224
|
}
|
|
5204
6225
|
acquireIndexingLock() {
|
|
5205
6226
|
const lockData = {
|
|
5206
6227
|
startedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
5207
6228
|
pid: process.pid
|
|
5208
6229
|
};
|
|
5209
|
-
|
|
6230
|
+
writeFileSync3(this.indexingLockPath, JSON.stringify(lockData));
|
|
5210
6231
|
}
|
|
5211
6232
|
releaseIndexingLock() {
|
|
5212
|
-
if (
|
|
6233
|
+
if (existsSync6(this.indexingLockPath)) {
|
|
5213
6234
|
unlinkSync(this.indexingLockPath);
|
|
5214
6235
|
}
|
|
5215
6236
|
}
|
|
5216
6237
|
async recoverFromInterruptedIndexing() {
|
|
5217
6238
|
this.logger.warn("Detected interrupted indexing session, recovering...");
|
|
5218
|
-
if (
|
|
6239
|
+
if (existsSync6(this.fileHashCachePath)) {
|
|
5219
6240
|
unlinkSync(this.fileHashCachePath);
|
|
5220
6241
|
}
|
|
5221
6242
|
await this.healthCheck();
|
|
5222
6243
|
this.releaseIndexingLock();
|
|
5223
6244
|
this.logger.info("Recovery complete, next index will re-process all files");
|
|
5224
6245
|
}
|
|
5225
|
-
loadFailedBatches() {
|
|
6246
|
+
loadFailedBatches(maxChunkTokens) {
|
|
5226
6247
|
try {
|
|
5227
|
-
|
|
5228
|
-
const data = readFileSync5(this.failedBatchesPath, "utf-8");
|
|
5229
|
-
return JSON.parse(data);
|
|
5230
|
-
}
|
|
6248
|
+
return this.loadSerializedFailedBatches().map((batch) => normalizeFailedBatch(batch, maxChunkTokens)).filter((batch) => batch !== null);
|
|
5231
6249
|
} catch {
|
|
5232
6250
|
return [];
|
|
5233
6251
|
}
|
|
5234
|
-
|
|
6252
|
+
}
|
|
6253
|
+
loadSerializedFailedBatches() {
|
|
6254
|
+
if (!existsSync6(this.failedBatchesPath)) {
|
|
6255
|
+
return [];
|
|
6256
|
+
}
|
|
6257
|
+
const data = readFileSync6(this.failedBatchesPath, "utf-8");
|
|
6258
|
+
const parsed = JSON.parse(data);
|
|
6259
|
+
return parsed.map((batch) => {
|
|
6260
|
+
const chunks = Array.isArray(batch.chunks) ? batch.chunks : [];
|
|
6261
|
+
if (chunks.length === 0) {
|
|
6262
|
+
return null;
|
|
6263
|
+
}
|
|
6264
|
+
return {
|
|
6265
|
+
chunks,
|
|
6266
|
+
error: typeof batch.error === "string" ? batch.error : "Unknown embedding error",
|
|
6267
|
+
attemptCount: typeof batch.attemptCount === "number" ? batch.attemptCount : 1,
|
|
6268
|
+
lastAttempt: typeof batch.lastAttempt === "string" ? batch.lastAttempt : (/* @__PURE__ */ new Date()).toISOString()
|
|
6269
|
+
};
|
|
6270
|
+
}).filter((batch) => batch !== null);
|
|
5235
6271
|
}
|
|
5236
6272
|
saveFailedBatches(batches) {
|
|
5237
6273
|
if (batches.length === 0) {
|
|
5238
|
-
if (
|
|
5239
|
-
|
|
5240
|
-
|
|
6274
|
+
if (existsSync6(this.failedBatchesPath)) {
|
|
6275
|
+
try {
|
|
6276
|
+
unlinkSync(this.failedBatchesPath);
|
|
6277
|
+
} catch {
|
|
6278
|
+
}
|
|
5241
6279
|
}
|
|
5242
6280
|
return;
|
|
5243
6281
|
}
|
|
5244
|
-
|
|
6282
|
+
writeFileSync3(this.failedBatchesPath, JSON.stringify(batches, null, 2));
|
|
5245
6283
|
}
|
|
5246
|
-
|
|
5247
|
-
const
|
|
5248
|
-
|
|
5249
|
-
chunks
|
|
5250
|
-
|
|
5251
|
-
|
|
5252
|
-
|
|
5253
|
-
|
|
5254
|
-
|
|
6284
|
+
collectRetryableFailedChunks(currentFileHashes, unchangedFilePaths, maxChunkTokens) {
|
|
6285
|
+
const retryableById = /* @__PURE__ */ new Map();
|
|
6286
|
+
for (const batch of this.loadFailedBatches(maxChunkTokens)) {
|
|
6287
|
+
for (const chunk of batch.chunks) {
|
|
6288
|
+
const filePath = chunk.metadata.filePath;
|
|
6289
|
+
if (!currentFileHashes.has(filePath)) {
|
|
6290
|
+
continue;
|
|
6291
|
+
}
|
|
6292
|
+
if (!unchangedFilePaths.has(filePath)) {
|
|
6293
|
+
continue;
|
|
6294
|
+
}
|
|
6295
|
+
const existing = retryableById.get(chunk.id);
|
|
6296
|
+
if (!existing || batch.attemptCount > existing.attemptCount) {
|
|
6297
|
+
retryableById.set(chunk.id, {
|
|
6298
|
+
chunk,
|
|
6299
|
+
attemptCount: batch.attemptCount
|
|
6300
|
+
});
|
|
6301
|
+
}
|
|
6302
|
+
}
|
|
6303
|
+
}
|
|
6304
|
+
return Array.from(retryableById.values());
|
|
5255
6305
|
}
|
|
5256
6306
|
getProviderRateLimits(provider) {
|
|
5257
6307
|
switch (provider) {
|
|
@@ -5456,31 +6506,54 @@ var Indexer = class {
|
|
|
5456
6506
|
}
|
|
5457
6507
|
await fsPromises2.mkdir(this.indexPath, { recursive: true });
|
|
5458
6508
|
const dimensions = this.configuredProviderInfo.modelInfo.dimensions;
|
|
5459
|
-
const storePath =
|
|
6509
|
+
const storePath = path8.join(this.indexPath, "vectors");
|
|
5460
6510
|
this.store = new VectorStore(storePath, dimensions);
|
|
5461
|
-
const indexFilePath =
|
|
5462
|
-
if (
|
|
6511
|
+
const indexFilePath = path8.join(this.indexPath, "vectors.usearch");
|
|
6512
|
+
if (existsSync6(indexFilePath)) {
|
|
5463
6513
|
this.store.load();
|
|
5464
6514
|
}
|
|
5465
|
-
const invertedIndexPath =
|
|
6515
|
+
const invertedIndexPath = path8.join(this.indexPath, "inverted-index.json");
|
|
5466
6516
|
this.invertedIndex = new InvertedIndex(invertedIndexPath);
|
|
5467
6517
|
try {
|
|
5468
6518
|
this.invertedIndex.load();
|
|
5469
6519
|
} catch {
|
|
5470
|
-
if (
|
|
6520
|
+
if (existsSync6(invertedIndexPath)) {
|
|
5471
6521
|
await fsPromises2.unlink(invertedIndexPath);
|
|
5472
6522
|
}
|
|
5473
6523
|
this.invertedIndex = new InvertedIndex(invertedIndexPath);
|
|
5474
6524
|
}
|
|
5475
|
-
const dbPath =
|
|
5476
|
-
|
|
5477
|
-
|
|
6525
|
+
const dbPath = path8.join(this.indexPath, "codebase.db");
|
|
6526
|
+
let dbIsNew = !existsSync6(dbPath);
|
|
6527
|
+
try {
|
|
6528
|
+
this.database = new Database(dbPath);
|
|
6529
|
+
} catch (error) {
|
|
6530
|
+
if (!await this.tryResetCorruptedIndex("initializing index database", error)) {
|
|
6531
|
+
throw error;
|
|
6532
|
+
}
|
|
6533
|
+
this.store = new VectorStore(storePath, dimensions);
|
|
6534
|
+
this.invertedIndex = new InvertedIndex(invertedIndexPath);
|
|
6535
|
+
this.database = new Database(dbPath);
|
|
6536
|
+
dbIsNew = true;
|
|
6537
|
+
}
|
|
6538
|
+
if (isGitRepo(this.projectRoot)) {
|
|
6539
|
+
this.currentBranch = getBranchOrDefault(this.projectRoot);
|
|
6540
|
+
this.baseBranch = getBaseBranch(this.projectRoot);
|
|
6541
|
+
this.logger.branch("info", "Detected git repository", {
|
|
6542
|
+
currentBranch: this.currentBranch,
|
|
6543
|
+
baseBranch: this.baseBranch
|
|
6544
|
+
});
|
|
6545
|
+
} else {
|
|
6546
|
+
this.currentBranch = "default";
|
|
6547
|
+
this.baseBranch = "default";
|
|
6548
|
+
this.logger.branch("debug", "Not a git repository, using default branch");
|
|
6549
|
+
}
|
|
5478
6550
|
if (this.checkForInterruptedIndexing()) {
|
|
5479
6551
|
await this.recoverFromInterruptedIndexing();
|
|
5480
6552
|
}
|
|
5481
6553
|
if (dbIsNew && this.store.count() > 0) {
|
|
5482
6554
|
this.migrateFromLegacyIndex();
|
|
5483
6555
|
}
|
|
6556
|
+
this.loadFileHashCache();
|
|
5484
6557
|
this.indexCompatibility = this.validateIndexCompatibility(this.configuredProviderInfo);
|
|
5485
6558
|
if (!this.indexCompatibility.compatible) {
|
|
5486
6559
|
this.logger.warn("Index compatibility issue detected", {
|
|
@@ -5489,18 +6562,6 @@ var Indexer = class {
|
|
|
5489
6562
|
configuredProviderInfo: this.configuredProviderInfo
|
|
5490
6563
|
});
|
|
5491
6564
|
}
|
|
5492
|
-
if (isGitRepo(this.projectRoot)) {
|
|
5493
|
-
this.currentBranch = getBranchOrDefault(this.projectRoot);
|
|
5494
|
-
this.baseBranch = getBaseBranch(this.projectRoot);
|
|
5495
|
-
this.logger.branch("info", "Detected git repository", {
|
|
5496
|
-
currentBranch: this.currentBranch,
|
|
5497
|
-
baseBranch: this.baseBranch
|
|
5498
|
-
});
|
|
5499
|
-
} else {
|
|
5500
|
-
this.currentBranch = "default";
|
|
5501
|
-
this.baseBranch = "default";
|
|
5502
|
-
this.logger.branch("debug", "Not a git repository, using default branch");
|
|
5503
|
-
}
|
|
5504
6565
|
if (this.config.indexing.autoGc) {
|
|
5505
6566
|
await this.maybeRunAutoGc();
|
|
5506
6567
|
}
|
|
@@ -5520,20 +6581,169 @@ var Indexer = class {
|
|
|
5520
6581
|
}
|
|
5521
6582
|
}
|
|
5522
6583
|
if (shouldRunGc) {
|
|
5523
|
-
await this.healthCheck();
|
|
6584
|
+
const result = await this.healthCheck();
|
|
6585
|
+
if (result.warning) {
|
|
6586
|
+
this.database.setMetadata(STARTUP_WARNING_METADATA_KEY, result.warning);
|
|
6587
|
+
} else {
|
|
6588
|
+
this.database.deleteMetadata(STARTUP_WARNING_METADATA_KEY);
|
|
6589
|
+
}
|
|
5524
6590
|
this.database.setMetadata("lastGcTimestamp", now.toString());
|
|
5525
6591
|
}
|
|
5526
6592
|
}
|
|
5527
6593
|
async maybeRunOrphanGc() {
|
|
5528
|
-
if (!this.database) return;
|
|
6594
|
+
if (!this.database) return null;
|
|
5529
6595
|
const stats = this.database.getStats();
|
|
5530
|
-
if (!stats) return;
|
|
6596
|
+
if (!stats) return null;
|
|
5531
6597
|
const orphanCount = stats.embeddingCount - stats.chunkCount;
|
|
5532
6598
|
if (orphanCount > this.config.indexing.gcOrphanThreshold) {
|
|
5533
|
-
|
|
5534
|
-
|
|
6599
|
+
try {
|
|
6600
|
+
this.database.gcOrphanEmbeddings();
|
|
6601
|
+
this.database.gcOrphanChunks();
|
|
6602
|
+
} catch (error) {
|
|
6603
|
+
if (await this.tryResetCorruptedIndex("running automatic orphan garbage collection", error)) {
|
|
6604
|
+
return {
|
|
6605
|
+
resetCorruptedIndex: true,
|
|
6606
|
+
warning: this.getCorruptedIndexWarning(path8.join(this.indexPath, "codebase.db"))
|
|
6607
|
+
};
|
|
6608
|
+
}
|
|
6609
|
+
throw error;
|
|
6610
|
+
}
|
|
5535
6611
|
this.database.setMetadata("lastGcTimestamp", Date.now().toString());
|
|
5536
6612
|
}
|
|
6613
|
+
return null;
|
|
6614
|
+
}
|
|
6615
|
+
rebuildVectorStoreExcludingChunkIds(store, database, excludedChunkIds) {
|
|
6616
|
+
const excludedSet = new Set(excludedChunkIds);
|
|
6617
|
+
if (excludedSet.size === 0) {
|
|
6618
|
+
return;
|
|
6619
|
+
}
|
|
6620
|
+
const retainedEntries = store.getAllMetadata().filter(({ key }) => !excludedSet.has(key));
|
|
6621
|
+
const storeBasePath = path8.join(this.indexPath, "vectors");
|
|
6622
|
+
const storeIndexPath = `${storeBasePath}.usearch`;
|
|
6623
|
+
const storeMetadataPath = `${storeBasePath}.meta.json`;
|
|
6624
|
+
const backupIndexPath = `${storeIndexPath}.bak`;
|
|
6625
|
+
const backupMetadataPath = `${storeMetadataPath}.bak`;
|
|
6626
|
+
let backedUpIndex = false;
|
|
6627
|
+
let backedUpMetadata = false;
|
|
6628
|
+
let rebuiltCount = 0;
|
|
6629
|
+
let skippedCount = 0;
|
|
6630
|
+
if (existsSync6(backupIndexPath)) {
|
|
6631
|
+
unlinkSync(backupIndexPath);
|
|
6632
|
+
}
|
|
6633
|
+
if (existsSync6(backupMetadataPath)) {
|
|
6634
|
+
unlinkSync(backupMetadataPath);
|
|
6635
|
+
}
|
|
6636
|
+
try {
|
|
6637
|
+
if (existsSync6(storeIndexPath)) {
|
|
6638
|
+
renameSync(storeIndexPath, backupIndexPath);
|
|
6639
|
+
backedUpIndex = true;
|
|
6640
|
+
}
|
|
6641
|
+
if (existsSync6(storeMetadataPath)) {
|
|
6642
|
+
renameSync(storeMetadataPath, backupMetadataPath);
|
|
6643
|
+
backedUpMetadata = true;
|
|
6644
|
+
}
|
|
6645
|
+
store.clear();
|
|
6646
|
+
for (const { key, metadata } of retainedEntries) {
|
|
6647
|
+
const chunk = database.getChunk(key);
|
|
6648
|
+
if (!chunk) {
|
|
6649
|
+
skippedCount += 1;
|
|
6650
|
+
continue;
|
|
6651
|
+
}
|
|
6652
|
+
const embeddingBuffer = database.getEmbedding(chunk.contentHash);
|
|
6653
|
+
if (!embeddingBuffer) {
|
|
6654
|
+
skippedCount += 1;
|
|
6655
|
+
continue;
|
|
6656
|
+
}
|
|
6657
|
+
const vector = bufferToFloat32Array(embeddingBuffer);
|
|
6658
|
+
store.add(key, Array.from(vector), metadata);
|
|
6659
|
+
rebuiltCount += 1;
|
|
6660
|
+
}
|
|
6661
|
+
store.save();
|
|
6662
|
+
if (backedUpIndex && existsSync6(backupIndexPath)) {
|
|
6663
|
+
unlinkSync(backupIndexPath);
|
|
6664
|
+
}
|
|
6665
|
+
if (backedUpMetadata && existsSync6(backupMetadataPath)) {
|
|
6666
|
+
unlinkSync(backupMetadataPath);
|
|
6667
|
+
}
|
|
6668
|
+
this.logger.gc("info", "Rebuilt vector store to avoid native remove", {
|
|
6669
|
+
excludedChunks: excludedSet.size,
|
|
6670
|
+
rebuiltChunks: rebuiltCount,
|
|
6671
|
+
skippedChunks: skippedCount
|
|
6672
|
+
});
|
|
6673
|
+
} catch (error) {
|
|
6674
|
+
try {
|
|
6675
|
+
store.clear();
|
|
6676
|
+
} catch {
|
|
6677
|
+
}
|
|
6678
|
+
if (existsSync6(storeIndexPath)) {
|
|
6679
|
+
unlinkSync(storeIndexPath);
|
|
6680
|
+
}
|
|
6681
|
+
if (existsSync6(storeMetadataPath)) {
|
|
6682
|
+
unlinkSync(storeMetadataPath);
|
|
6683
|
+
}
|
|
6684
|
+
if (backedUpIndex && existsSync6(backupIndexPath)) {
|
|
6685
|
+
renameSync(backupIndexPath, storeIndexPath);
|
|
6686
|
+
}
|
|
6687
|
+
if (backedUpMetadata && existsSync6(backupMetadataPath)) {
|
|
6688
|
+
renameSync(backupMetadataPath, storeMetadataPath);
|
|
6689
|
+
}
|
|
6690
|
+
if (backedUpIndex || backedUpMetadata) {
|
|
6691
|
+
store.load();
|
|
6692
|
+
}
|
|
6693
|
+
throw error;
|
|
6694
|
+
}
|
|
6695
|
+
}
|
|
6696
|
+
getCorruptedIndexWarning(dbPath) {
|
|
6697
|
+
if (this.config.scope === "global") {
|
|
6698
|
+
return `Detected a corrupted shared global SQLite index at ${dbPath}. Automatic repair is disabled for global scope because it may delete other projects' index data. Remove or repair the shared index manually, then rerun index_codebase with force=true.`;
|
|
6699
|
+
}
|
|
6700
|
+
return `Detected a corrupted local SQLite index at ${dbPath} and reset the local index. Run index_codebase to rebuild search data.`;
|
|
6701
|
+
}
|
|
6702
|
+
async tryResetCorruptedIndex(stage, error) {
|
|
6703
|
+
if (!isSqliteCorruptionError(error)) {
|
|
6704
|
+
return false;
|
|
6705
|
+
}
|
|
6706
|
+
const dbPath = path8.join(this.indexPath, "codebase.db");
|
|
6707
|
+
const warning = this.getCorruptedIndexWarning(dbPath);
|
|
6708
|
+
const errorMessage = getErrorMessage(error);
|
|
6709
|
+
if (this.config.scope === "global") {
|
|
6710
|
+
this.logger.error("Detected corrupted shared global index database", {
|
|
6711
|
+
stage,
|
|
6712
|
+
dbPath,
|
|
6713
|
+
error: errorMessage
|
|
6714
|
+
});
|
|
6715
|
+
throw new Error(`${warning} Original SQLite error: ${errorMessage}`);
|
|
6716
|
+
}
|
|
6717
|
+
this.logger.warn("Detected corrupted local index database, resetting local index", {
|
|
6718
|
+
stage,
|
|
6719
|
+
dbPath,
|
|
6720
|
+
error: errorMessage
|
|
6721
|
+
});
|
|
6722
|
+
this.store = null;
|
|
6723
|
+
this.invertedIndex = null;
|
|
6724
|
+
this.database?.close();
|
|
6725
|
+
this.database = null;
|
|
6726
|
+
this.indexCompatibility = null;
|
|
6727
|
+
this.fileHashCache.clear();
|
|
6728
|
+
const resetPaths = [
|
|
6729
|
+
path8.join(this.indexPath, "codebase.db"),
|
|
6730
|
+
path8.join(this.indexPath, "codebase.db-shm"),
|
|
6731
|
+
path8.join(this.indexPath, "codebase.db-wal"),
|
|
6732
|
+
path8.join(this.indexPath, "vectors.usearch"),
|
|
6733
|
+
path8.join(this.indexPath, "inverted-index.json"),
|
|
6734
|
+
path8.join(this.indexPath, "file-hashes.json"),
|
|
6735
|
+
path8.join(this.indexPath, "failed-batches.json"),
|
|
6736
|
+
path8.join(this.indexPath, "indexing.lock"),
|
|
6737
|
+
path8.join(this.indexPath, "vectors")
|
|
6738
|
+
];
|
|
6739
|
+
await Promise.all(resetPaths.map(async (targetPath) => {
|
|
6740
|
+
try {
|
|
6741
|
+
await fsPromises2.rm(targetPath, { recursive: true, force: true });
|
|
6742
|
+
} catch {
|
|
6743
|
+
}
|
|
6744
|
+
}));
|
|
6745
|
+
await fsPromises2.mkdir(this.indexPath, { recursive: true });
|
|
6746
|
+
return true;
|
|
5537
6747
|
}
|
|
5538
6748
|
migrateFromLegacyIndex() {
|
|
5539
6749
|
if (!this.store || !this.database) return;
|
|
@@ -5557,7 +6767,7 @@ var Indexer = class {
|
|
|
5557
6767
|
if (chunkDataBatch.length > 0) {
|
|
5558
6768
|
this.database.upsertChunksBatch(chunkDataBatch);
|
|
5559
6769
|
}
|
|
5560
|
-
this.database.addChunksToBranchBatch(this.
|
|
6770
|
+
this.database.addChunksToBranchBatch(this.getBranchCatalogKey(), chunkIds);
|
|
5561
6771
|
}
|
|
5562
6772
|
loadIndexMetadata() {
|
|
5563
6773
|
if (!this.database) return null;
|
|
@@ -5568,6 +6778,7 @@ var Indexer = class {
|
|
|
5568
6778
|
embeddingProvider: this.database.getMetadata("index.embeddingProvider") ?? "",
|
|
5569
6779
|
embeddingModel: this.database.getMetadata("index.embeddingModel") ?? "",
|
|
5570
6780
|
embeddingDimensions: parseInt(this.database.getMetadata("index.embeddingDimensions") ?? "0", 10),
|
|
6781
|
+
embeddingStrategyVersion: this.loadStoredEmbeddingStrategyVersion() ?? EMBEDDING_STRATEGY_VERSION,
|
|
5571
6782
|
createdAt: this.database.getMetadata("index.createdAt") ?? "",
|
|
5572
6783
|
updatedAt: this.database.getMetadata("index.updatedAt") ?? ""
|
|
5573
6784
|
};
|
|
@@ -5576,10 +6787,22 @@ var Indexer = class {
|
|
|
5576
6787
|
if (!this.database) return;
|
|
5577
6788
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5578
6789
|
const existingCreatedAt = this.database.getMetadata("index.createdAt");
|
|
6790
|
+
const completeProjectEmbeddingStrategyReset = !this.hasProjectForceReembedPending();
|
|
5579
6791
|
this.database.setMetadata("index.version", INDEX_METADATA_VERSION);
|
|
5580
6792
|
this.database.setMetadata("index.embeddingProvider", provider.provider);
|
|
5581
6793
|
this.database.setMetadata("index.embeddingModel", provider.modelInfo.model);
|
|
5582
6794
|
this.database.setMetadata("index.embeddingDimensions", provider.modelInfo.dimensions.toString());
|
|
6795
|
+
if (this.config.scope === "global") {
|
|
6796
|
+
if (completeProjectEmbeddingStrategyReset) {
|
|
6797
|
+
this.database.setMetadata(this.getProjectEmbeddingStrategyMetadataKey(), EMBEDDING_STRATEGY_VERSION);
|
|
6798
|
+
}
|
|
6799
|
+
this.database.setMetadata(this.getLegacyMigrationMetadataKey(), "done");
|
|
6800
|
+
if (completeProjectEmbeddingStrategyReset) {
|
|
6801
|
+
this.database.deleteMetadata(this.getProjectForceReembedMetadataKey());
|
|
6802
|
+
}
|
|
6803
|
+
} else {
|
|
6804
|
+
this.database.setMetadata("index.embeddingStrategyVersion", EMBEDDING_STRATEGY_VERSION);
|
|
6805
|
+
}
|
|
5583
6806
|
this.database.setMetadata("index.updatedAt", now);
|
|
5584
6807
|
if (!existingCreatedAt) {
|
|
5585
6808
|
this.database.setMetadata("index.createdAt", now);
|
|
@@ -5609,6 +6832,14 @@ var Indexer = class {
|
|
|
5609
6832
|
storedMetadata
|
|
5610
6833
|
};
|
|
5611
6834
|
}
|
|
6835
|
+
if (storedMetadata.embeddingStrategyVersion !== EMBEDDING_STRATEGY_VERSION) {
|
|
6836
|
+
return {
|
|
6837
|
+
compatible: false,
|
|
6838
|
+
code: "EMBEDDING_STRATEGY_MISMATCH" /* EMBEDDING_STRATEGY_MISMATCH */,
|
|
6839
|
+
reason: `Embedding strategy mismatch: index was built with embedding strategy v${storedMetadata.embeddingStrategyVersion}, but the current code requires v${EMBEDDING_STRATEGY_VERSION}. Run index_codebase with force=true to rebuild cached embeddings.`,
|
|
6840
|
+
storedMetadata
|
|
6841
|
+
};
|
|
6842
|
+
}
|
|
5612
6843
|
if (storedMetadata.embeddingProvider !== currentProvider) {
|
|
5613
6844
|
this.logger.warn("Provider changed", {
|
|
5614
6845
|
storedProvider: storedMetadata.embeddingProvider,
|
|
@@ -5656,6 +6887,10 @@ var Indexer = class {
|
|
|
5656
6887
|
}
|
|
5657
6888
|
async index(onProgress) {
|
|
5658
6889
|
const { store, provider, invertedIndex, database, configuredProviderInfo } = await this.ensureInitialized();
|
|
6890
|
+
const scopedRoots = this.config.scope === "global" ? this.getScopedRoots() : null;
|
|
6891
|
+
const branchCatalogKey = this.getBranchCatalogKey();
|
|
6892
|
+
const forceScopedReembed = scopedRoots !== null && database.getMetadata(this.getProjectForceReembedMetadataKey()) === "true";
|
|
6893
|
+
const failedForcedChunkIds = /* @__PURE__ */ new Set();
|
|
5659
6894
|
if (!this.indexCompatibility?.compatible) {
|
|
5660
6895
|
throw new Error(
|
|
5661
6896
|
`${this.indexCompatibility?.reason} Run index_codebase with force=true to rebuild the index.`
|
|
@@ -5677,6 +6912,7 @@ var Indexer = class {
|
|
|
5677
6912
|
skippedFiles: [],
|
|
5678
6913
|
parseFailures: []
|
|
5679
6914
|
};
|
|
6915
|
+
const failedBatchesForCurrentRun = [];
|
|
5680
6916
|
onProgress?.({
|
|
5681
6917
|
phase: "scanning",
|
|
5682
6918
|
filesProcessed: 0,
|
|
@@ -5736,6 +6972,12 @@ var Indexer = class {
|
|
|
5736
6972
|
const existingChunks = /* @__PURE__ */ new Map();
|
|
5737
6973
|
const existingChunksByFile = /* @__PURE__ */ new Map();
|
|
5738
6974
|
for (const { key, metadata } of store.getAllMetadata()) {
|
|
6975
|
+
if (scopedRoots && !this.isFileInCurrentScope(metadata.filePath, scopedRoots)) {
|
|
6976
|
+
continue;
|
|
6977
|
+
}
|
|
6978
|
+
if (forceScopedReembed && scopedRoots && this.isFileInCurrentScope(metadata.filePath, scopedRoots)) {
|
|
6979
|
+
continue;
|
|
6980
|
+
}
|
|
5739
6981
|
existingChunks.set(key, metadata.hash);
|
|
5740
6982
|
const fileChunks = existingChunksByFile.get(metadata.filePath) || /* @__PURE__ */ new Set();
|
|
5741
6983
|
fileChunks.add(key);
|
|
@@ -5757,7 +6999,7 @@ var Indexer = class {
|
|
|
5757
6999
|
for (const parsed of parsedFiles) {
|
|
5758
7000
|
currentFilePaths.add(parsed.path);
|
|
5759
7001
|
if (parsed.chunks.length === 0) {
|
|
5760
|
-
const relativePath =
|
|
7002
|
+
const relativePath = path8.relative(this.projectRoot, parsed.path);
|
|
5761
7003
|
stats.parseFailures.push(relativePath);
|
|
5762
7004
|
}
|
|
5763
7005
|
let fileChunkCount = 0;
|
|
@@ -5793,7 +7035,10 @@ var Indexer = class {
|
|
|
5793
7035
|
fileChunkCount++;
|
|
5794
7036
|
continue;
|
|
5795
7037
|
}
|
|
5796
|
-
const
|
|
7038
|
+
const texts = createEmbeddingTexts(chunk, parsed.path, getSafeEmbeddingChunkTokenLimit(configuredProviderInfo)).map((text) => ({
|
|
7039
|
+
text,
|
|
7040
|
+
tokenCount: estimateTokens2(text)
|
|
7041
|
+
}));
|
|
5797
7042
|
const metadata = {
|
|
5798
7043
|
filePath: parsed.path,
|
|
5799
7044
|
startLine: chunk.startLine,
|
|
@@ -5803,10 +7048,38 @@ var Indexer = class {
|
|
|
5803
7048
|
language: chunk.language,
|
|
5804
7049
|
hash: contentHash
|
|
5805
7050
|
};
|
|
5806
|
-
pendingChunks.push({
|
|
7051
|
+
pendingChunks.push({
|
|
7052
|
+
id,
|
|
7053
|
+
texts,
|
|
7054
|
+
storageText: createPendingChunkStorageText(texts),
|
|
7055
|
+
content: chunk.content,
|
|
7056
|
+
contentHash,
|
|
7057
|
+
metadata
|
|
7058
|
+
});
|
|
5807
7059
|
fileChunkCount++;
|
|
5808
7060
|
}
|
|
5809
7061
|
}
|
|
7062
|
+
const retryableFailedChunks = this.collectRetryableFailedChunks(
|
|
7063
|
+
currentFileHashes,
|
|
7064
|
+
unchangedFilePaths,
|
|
7065
|
+
getSafeEmbeddingChunkTokenLimit(configuredProviderInfo)
|
|
7066
|
+
);
|
|
7067
|
+
const retryableFailedAttemptCounts = /* @__PURE__ */ new Map();
|
|
7068
|
+
const retryableChunksWithExistingData = /* @__PURE__ */ new Set();
|
|
7069
|
+
if (retryableFailedChunks.length > 0) {
|
|
7070
|
+
const pendingChunkIds = new Set(pendingChunks.map((chunk) => chunk.id));
|
|
7071
|
+
for (const { chunk, attemptCount } of retryableFailedChunks) {
|
|
7072
|
+
retryableFailedAttemptCounts.set(chunk.id, attemptCount);
|
|
7073
|
+
if (existingChunks.has(chunk.id)) {
|
|
7074
|
+
retryableChunksWithExistingData.add(chunk.id);
|
|
7075
|
+
}
|
|
7076
|
+
if (!pendingChunkIds.has(chunk.id)) {
|
|
7077
|
+
pendingChunks.push(chunk);
|
|
7078
|
+
pendingChunkIds.add(chunk.id);
|
|
7079
|
+
currentChunkIds.add(chunk.id);
|
|
7080
|
+
}
|
|
7081
|
+
}
|
|
7082
|
+
}
|
|
5810
7083
|
if (chunkDataBatch.length > 0) {
|
|
5811
7084
|
database.upsertChunksBatch(chunkDataBatch);
|
|
5812
7085
|
}
|
|
@@ -5835,17 +7108,20 @@ var Indexer = class {
|
|
|
5835
7108
|
fileSymbols.push(symbol);
|
|
5836
7109
|
allSymbolIds.add(symbolId);
|
|
5837
7110
|
}
|
|
7111
|
+
const fileLanguage = parsed.chunks[0]?.language;
|
|
7112
|
+
const isCaseInsensitiveLanguage = !!fileLanguage && CASE_INSENSITIVE_LANGUAGES.has(fileLanguage);
|
|
7113
|
+
const normalizeSymbolKey = (name) => isCaseInsensitiveLanguage ? name.toLowerCase() : name;
|
|
5838
7114
|
const symbolsByName = /* @__PURE__ */ new Map();
|
|
5839
7115
|
for (const symbol of fileSymbols) {
|
|
5840
|
-
const
|
|
7116
|
+
const key = normalizeSymbolKey(symbol.name);
|
|
7117
|
+
const existing = symbolsByName.get(key) ?? [];
|
|
5841
7118
|
existing.push(symbol);
|
|
5842
|
-
symbolsByName.set(
|
|
7119
|
+
symbolsByName.set(key, existing);
|
|
5843
7120
|
}
|
|
5844
7121
|
if (fileSymbols.length > 0) {
|
|
5845
7122
|
database.upsertSymbolsBatch(fileSymbols);
|
|
5846
7123
|
symbolsByFile.set(parsed.path, fileSymbols);
|
|
5847
7124
|
}
|
|
5848
|
-
const fileLanguage = parsed.chunks[0]?.language;
|
|
5849
7125
|
if (!fileLanguage || !CALL_GRAPH_LANGUAGES.has(fileLanguage)) continue;
|
|
5850
7126
|
const callSites = extractCalls(changedFile.content, fileLanguage);
|
|
5851
7127
|
if (callSites.length === 0) continue;
|
|
@@ -5870,7 +7146,7 @@ var Indexer = class {
|
|
|
5870
7146
|
if (edges.length > 0) {
|
|
5871
7147
|
database.upsertCallEdgesBatch(edges);
|
|
5872
7148
|
for (const edge of edges) {
|
|
5873
|
-
const candidates = symbolsByName.get(edge.targetName);
|
|
7149
|
+
const candidates = symbolsByName.get(normalizeSymbolKey(edge.targetName));
|
|
5874
7150
|
if (candidates && candidates.length === 1) {
|
|
5875
7151
|
database.resolveCallEdge(edge.id, candidates[0].id);
|
|
5876
7152
|
}
|
|
@@ -5883,14 +7159,20 @@ var Indexer = class {
|
|
|
5883
7159
|
allSymbolIds.add(sym.id);
|
|
5884
7160
|
}
|
|
5885
7161
|
}
|
|
5886
|
-
|
|
7162
|
+
const removedChunkIds = [];
|
|
5887
7163
|
for (const [chunkId] of existingChunks) {
|
|
5888
7164
|
if (!currentChunkIds.has(chunkId)) {
|
|
5889
|
-
|
|
7165
|
+
removedChunkIds.push(chunkId);
|
|
7166
|
+
}
|
|
7167
|
+
}
|
|
7168
|
+
if (removedChunkIds.length > 0) {
|
|
7169
|
+
this.rebuildVectorStoreExcludingChunkIds(store, database, removedChunkIds);
|
|
7170
|
+
for (const chunkId of removedChunkIds) {
|
|
5890
7171
|
invertedIndex.removeChunk(chunkId);
|
|
5891
|
-
removedCount++;
|
|
5892
7172
|
}
|
|
7173
|
+
database.deleteChunksByIds(removedChunkIds);
|
|
5893
7174
|
}
|
|
7175
|
+
const removedCount = removedChunkIds.length;
|
|
5894
7176
|
stats.totalChunks = pendingChunks.length;
|
|
5895
7177
|
stats.existingChunks = currentChunkIds.size - pendingChunks.length;
|
|
5896
7178
|
stats.removedChunks = removedCount;
|
|
@@ -5902,12 +7184,20 @@ var Indexer = class {
|
|
|
5902
7184
|
removed: removedCount
|
|
5903
7185
|
});
|
|
5904
7186
|
if (pendingChunks.length === 0 && removedCount === 0) {
|
|
5905
|
-
database.clearBranch(
|
|
5906
|
-
database.addChunksToBranchBatch(
|
|
5907
|
-
database.clearBranchSymbols(
|
|
5908
|
-
database.addSymbolsToBranchBatch(
|
|
5909
|
-
|
|
5910
|
-
|
|
7187
|
+
database.clearBranch(branchCatalogKey);
|
|
7188
|
+
database.addChunksToBranchBatch(branchCatalogKey, Array.from(currentChunkIds));
|
|
7189
|
+
database.clearBranchSymbols(branchCatalogKey);
|
|
7190
|
+
database.addSymbolsToBranchBatch(branchCatalogKey, Array.from(allSymbolIds));
|
|
7191
|
+
if (scopedRoots) {
|
|
7192
|
+
this.replaceScopedFileHashCache(currentFileHashes, scopedRoots);
|
|
7193
|
+
this.clearScopedFailedBatches(scopedRoots);
|
|
7194
|
+
} else {
|
|
7195
|
+
this.fileHashCache = currentFileHashes;
|
|
7196
|
+
this.saveFileHashCache();
|
|
7197
|
+
this.saveFailedBatches([]);
|
|
7198
|
+
}
|
|
7199
|
+
this.saveIndexMetadata(configuredProviderInfo);
|
|
7200
|
+
this.indexCompatibility = { compatible: true };
|
|
5911
7201
|
stats.durationMs = Date.now() - startTime;
|
|
5912
7202
|
onProgress?.({
|
|
5913
7203
|
phase: "complete",
|
|
@@ -5920,14 +7210,22 @@ var Indexer = class {
|
|
|
5920
7210
|
return stats;
|
|
5921
7211
|
}
|
|
5922
7212
|
if (pendingChunks.length === 0) {
|
|
5923
|
-
database.clearBranch(
|
|
5924
|
-
database.addChunksToBranchBatch(
|
|
5925
|
-
database.clearBranchSymbols(
|
|
5926
|
-
database.addSymbolsToBranchBatch(
|
|
7213
|
+
database.clearBranch(branchCatalogKey);
|
|
7214
|
+
database.addChunksToBranchBatch(branchCatalogKey, Array.from(currentChunkIds));
|
|
7215
|
+
database.clearBranchSymbols(branchCatalogKey);
|
|
7216
|
+
database.addSymbolsToBranchBatch(branchCatalogKey, Array.from(allSymbolIds));
|
|
5927
7217
|
store.save();
|
|
5928
7218
|
invertedIndex.save();
|
|
5929
|
-
|
|
5930
|
-
|
|
7219
|
+
if (scopedRoots) {
|
|
7220
|
+
this.replaceScopedFileHashCache(currentFileHashes, scopedRoots);
|
|
7221
|
+
this.clearScopedFailedBatches(scopedRoots);
|
|
7222
|
+
} else {
|
|
7223
|
+
this.fileHashCache = currentFileHashes;
|
|
7224
|
+
this.saveFileHashCache();
|
|
7225
|
+
this.saveFailedBatches([]);
|
|
7226
|
+
}
|
|
7227
|
+
this.saveIndexMetadata(configuredProviderInfo);
|
|
7228
|
+
this.indexCompatibility = { compatible: true };
|
|
5931
7229
|
stats.durationMs = Date.now() - startTime;
|
|
5932
7230
|
onProgress?.({
|
|
5933
7231
|
phase: "complete",
|
|
@@ -5948,8 +7246,9 @@ var Indexer = class {
|
|
|
5948
7246
|
});
|
|
5949
7247
|
const allContentHashes = pendingChunks.map((c) => c.contentHash);
|
|
5950
7248
|
const missingHashes = new Set(database.getMissingEmbeddings(allContentHashes));
|
|
5951
|
-
const
|
|
5952
|
-
const
|
|
7249
|
+
const forcedReembedChunkIds = forceScopedReembed ? new Set(pendingChunks.map((chunk) => chunk.id)) : /* @__PURE__ */ new Set();
|
|
7250
|
+
const chunksNeedingEmbedding = pendingChunks.filter((c) => forcedReembedChunkIds.has(c.id) || missingHashes.has(c.contentHash));
|
|
7251
|
+
const chunksWithExistingEmbedding = pendingChunks.filter((c) => !forcedReembedChunkIds.has(c.id) && !missingHashes.has(c.contentHash));
|
|
5953
7252
|
this.logger.cache("info", "Embedding cache lookup", {
|
|
5954
7253
|
needsEmbedding: chunksNeedingEmbedding.length,
|
|
5955
7254
|
fromCache: chunksWithExistingEmbedding.length
|
|
@@ -5971,17 +7270,24 @@ var Indexer = class {
|
|
|
5971
7270
|
interval: providerRateLimits.intervalMs,
|
|
5972
7271
|
intervalCap: providerRateLimits.concurrency
|
|
5973
7272
|
});
|
|
5974
|
-
const
|
|
7273
|
+
const pendingChunksById = new Map(chunksNeedingEmbedding.map((chunk) => [chunk.id, chunk]));
|
|
7274
|
+
const embeddingPartsByChunk = /* @__PURE__ */ new Map();
|
|
7275
|
+
const completedChunkIds = /* @__PURE__ */ new Set();
|
|
7276
|
+
const failedChunkIds = /* @__PURE__ */ new Set();
|
|
7277
|
+
const requestBatches = createPendingEmbeddingRequestBatches(
|
|
7278
|
+
chunksNeedingEmbedding,
|
|
7279
|
+
getDynamicBatchOptions(configuredProviderInfo)
|
|
7280
|
+
);
|
|
5975
7281
|
let rateLimitBackoffMs = 0;
|
|
5976
|
-
for (const
|
|
7282
|
+
for (const requestBatch of requestBatches) {
|
|
5977
7283
|
queue.add(async () => {
|
|
5978
7284
|
if (rateLimitBackoffMs > 0) {
|
|
5979
|
-
await new Promise((
|
|
7285
|
+
await new Promise((resolve8) => setTimeout(resolve8, rateLimitBackoffMs));
|
|
5980
7286
|
}
|
|
5981
7287
|
try {
|
|
5982
7288
|
const result = await pRetry(
|
|
5983
7289
|
async () => {
|
|
5984
|
-
const texts =
|
|
7290
|
+
const texts = requestBatch.map((request) => request.text);
|
|
5985
7291
|
return provider.embedBatch(texts);
|
|
5986
7292
|
},
|
|
5987
7293
|
{
|
|
@@ -6011,29 +7317,82 @@ var Indexer = class {
|
|
|
6011
7317
|
if (rateLimitBackoffMs > 0) {
|
|
6012
7318
|
rateLimitBackoffMs = Math.max(0, rateLimitBackoffMs - 2e3);
|
|
6013
7319
|
}
|
|
6014
|
-
const
|
|
6015
|
-
|
|
6016
|
-
|
|
6017
|
-
|
|
6018
|
-
|
|
6019
|
-
|
|
6020
|
-
|
|
6021
|
-
|
|
6022
|
-
|
|
6023
|
-
|
|
6024
|
-
|
|
6025
|
-
|
|
6026
|
-
|
|
6027
|
-
|
|
6028
|
-
|
|
6029
|
-
|
|
7320
|
+
const touchedChunkIds = /* @__PURE__ */ new Set();
|
|
7321
|
+
requestBatch.forEach((request, idx) => {
|
|
7322
|
+
if (failedChunkIds.has(request.chunk.id) || completedChunkIds.has(request.chunk.id)) {
|
|
7323
|
+
return;
|
|
7324
|
+
}
|
|
7325
|
+
const vector = result.embeddings[idx];
|
|
7326
|
+
if (!vector) {
|
|
7327
|
+
throw new Error(`Embedding API returned too few vectors for chunk ${request.chunk.id}`);
|
|
7328
|
+
}
|
|
7329
|
+
const parts = embeddingPartsByChunk.get(request.chunk.id) ?? [];
|
|
7330
|
+
parts[request.partIndex] = {
|
|
7331
|
+
vector,
|
|
7332
|
+
tokenCount: request.tokenCount
|
|
7333
|
+
};
|
|
7334
|
+
embeddingPartsByChunk.set(request.chunk.id, parts);
|
|
7335
|
+
touchedChunkIds.add(request.chunk.id);
|
|
7336
|
+
});
|
|
7337
|
+
const pooledResults = [];
|
|
7338
|
+
for (const chunkId of touchedChunkIds) {
|
|
7339
|
+
if (failedChunkIds.has(chunkId) || completedChunkIds.has(chunkId)) {
|
|
7340
|
+
continue;
|
|
7341
|
+
}
|
|
7342
|
+
const chunk = pendingChunksById.get(chunkId);
|
|
7343
|
+
if (!chunk) {
|
|
7344
|
+
continue;
|
|
7345
|
+
}
|
|
7346
|
+
const parts = embeddingPartsByChunk.get(chunk.id) ?? [];
|
|
7347
|
+
if (!hasAllEmbeddingParts(parts, chunk.texts.length)) {
|
|
7348
|
+
continue;
|
|
7349
|
+
}
|
|
7350
|
+
const orderedParts = parts;
|
|
7351
|
+
pooledResults.push({
|
|
7352
|
+
chunk,
|
|
7353
|
+
vector: poolEmbeddingVectors(
|
|
7354
|
+
orderedParts.map((part) => part.vector),
|
|
7355
|
+
orderedParts.map((part) => part.tokenCount)
|
|
7356
|
+
)
|
|
7357
|
+
});
|
|
7358
|
+
}
|
|
7359
|
+
if (pooledResults.length > 0) {
|
|
7360
|
+
const items = pooledResults.map(({ chunk, vector }) => ({
|
|
7361
|
+
id: chunk.id,
|
|
7362
|
+
vector,
|
|
7363
|
+
metadata: chunk.metadata
|
|
7364
|
+
}));
|
|
7365
|
+
store.addBatch(items);
|
|
7366
|
+
const embeddingBatchItems = pooledResults.map(({ chunk, vector }) => ({
|
|
7367
|
+
contentHash: chunk.contentHash,
|
|
7368
|
+
embedding: float32ArrayToBuffer(vector),
|
|
7369
|
+
chunkText: chunk.storageText,
|
|
7370
|
+
model: configuredProviderInfo.modelInfo.model
|
|
7371
|
+
}));
|
|
7372
|
+
try {
|
|
7373
|
+
database.upsertEmbeddingsBatch(embeddingBatchItems);
|
|
7374
|
+
} catch (dbError) {
|
|
7375
|
+
this.rebuildVectorStoreExcludingChunkIds(
|
|
7376
|
+
store,
|
|
7377
|
+
database,
|
|
7378
|
+
pooledResults.map(({ chunk }) => chunk.id)
|
|
7379
|
+
);
|
|
7380
|
+
throw dbError;
|
|
7381
|
+
}
|
|
7382
|
+
for (const { chunk } of pooledResults) {
|
|
7383
|
+
invertedIndex.removeChunk(chunk.id);
|
|
7384
|
+
invertedIndex.addChunk(chunk.id, chunk.content);
|
|
7385
|
+
completedChunkIds.add(chunk.id);
|
|
7386
|
+
embeddingPartsByChunk.delete(chunk.id);
|
|
7387
|
+
}
|
|
7388
|
+
stats.indexedChunks += pooledResults.length;
|
|
7389
|
+
this.logger.recordChunksEmbedded(pooledResults.length);
|
|
6030
7390
|
}
|
|
6031
|
-
stats.indexedChunks += batch.length;
|
|
6032
7391
|
stats.tokensUsed += result.totalTokensUsed;
|
|
6033
|
-
this.logger.recordChunksEmbedded(batch.length);
|
|
6034
7392
|
this.logger.recordEmbeddingApiCall(result.totalTokensUsed);
|
|
6035
7393
|
this.logger.embedding("debug", `Embedded batch`, {
|
|
6036
|
-
batchSize:
|
|
7394
|
+
batchSize: pooledResults.length,
|
|
7395
|
+
requestCount: requestBatch.length,
|
|
6037
7396
|
tokens: result.totalTokensUsed
|
|
6038
7397
|
});
|
|
6039
7398
|
onProgress?.({
|
|
@@ -6044,17 +7403,49 @@ var Indexer = class {
|
|
|
6044
7403
|
totalChunks: pendingChunks.length
|
|
6045
7404
|
});
|
|
6046
7405
|
} catch (error) {
|
|
6047
|
-
|
|
6048
|
-
|
|
7406
|
+
const failedChunks = getUniquePendingChunksFromRequests(requestBatch).filter((chunk) => !completedChunkIds.has(chunk.id));
|
|
7407
|
+
const failureMessage = getErrorMessage(error);
|
|
7408
|
+
const failureTimestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
7409
|
+
for (const chunk of failedChunks) {
|
|
7410
|
+
if (!failedChunkIds.has(chunk.id)) {
|
|
7411
|
+
failedChunkIds.add(chunk.id);
|
|
7412
|
+
stats.failedChunks += 1;
|
|
7413
|
+
}
|
|
7414
|
+
if (forceScopedReembed) {
|
|
7415
|
+
failedForcedChunkIds.add(chunk.id);
|
|
7416
|
+
}
|
|
7417
|
+
embeddingPartsByChunk.delete(chunk.id);
|
|
7418
|
+
const existingFailedBatchIndex = failedBatchesForCurrentRun.findIndex(
|
|
7419
|
+
(failedBatch2) => failedBatch2.chunks[0]?.id === chunk.id
|
|
7420
|
+
);
|
|
7421
|
+
const existingFailedBatch = existingFailedBatchIndex === -1 ? void 0 : failedBatchesForCurrentRun[existingFailedBatchIndex];
|
|
7422
|
+
const failedBatch = {
|
|
7423
|
+
chunks: [chunk],
|
|
7424
|
+
error: failureMessage,
|
|
7425
|
+
attemptCount: (existingFailedBatch?.attemptCount ?? retryableFailedAttemptCounts.get(chunk.id) ?? 0) + 1,
|
|
7426
|
+
lastAttempt: failureTimestamp
|
|
7427
|
+
};
|
|
7428
|
+
if (existingFailedBatchIndex === -1) {
|
|
7429
|
+
failedBatchesForCurrentRun.push(failedBatch);
|
|
7430
|
+
} else {
|
|
7431
|
+
failedBatchesForCurrentRun[existingFailedBatchIndex] = failedBatch;
|
|
7432
|
+
}
|
|
7433
|
+
}
|
|
6049
7434
|
this.logger.recordEmbeddingError();
|
|
6050
7435
|
this.logger.embedding("error", `Failed to embed batch after retries`, {
|
|
6051
|
-
batchSize:
|
|
6052
|
-
|
|
7436
|
+
batchSize: failedChunks.length,
|
|
7437
|
+
requestCount: requestBatch.length,
|
|
7438
|
+
error: failureMessage
|
|
6053
7439
|
});
|
|
6054
7440
|
}
|
|
6055
7441
|
});
|
|
6056
7442
|
}
|
|
6057
7443
|
await queue.onIdle();
|
|
7444
|
+
if (scopedRoots) {
|
|
7445
|
+
this.saveScopedFailedBatches(coalesceFailedBatches(failedBatchesForCurrentRun), scopedRoots);
|
|
7446
|
+
} else {
|
|
7447
|
+
this.saveFailedBatches(coalesceFailedBatches(failedBatchesForCurrentRun));
|
|
7448
|
+
}
|
|
6058
7449
|
onProgress?.({
|
|
6059
7450
|
phase: "storing",
|
|
6060
7451
|
filesProcessed: files.length,
|
|
@@ -6062,18 +7453,48 @@ var Indexer = class {
|
|
|
6062
7453
|
chunksProcessed: stats.indexedChunks,
|
|
6063
7454
|
totalChunks: pendingChunks.length
|
|
6064
7455
|
});
|
|
6065
|
-
|
|
6066
|
-
|
|
6067
|
-
|
|
6068
|
-
|
|
7456
|
+
const branchChunkIds = Array.from(currentChunkIds).filter(
|
|
7457
|
+
(chunkId) => {
|
|
7458
|
+
const isNewlyFailed = failedChunkIds.has(chunkId) && !retryableChunksWithExistingData.has(chunkId);
|
|
7459
|
+
const isForcedFailed = forceScopedReembed && failedForcedChunkIds.has(chunkId);
|
|
7460
|
+
return !isNewlyFailed && !isForcedFailed;
|
|
7461
|
+
}
|
|
7462
|
+
);
|
|
7463
|
+
database.clearBranch(branchCatalogKey);
|
|
7464
|
+
database.addChunksToBranchBatch(branchCatalogKey, branchChunkIds);
|
|
7465
|
+
database.clearBranchSymbols(branchCatalogKey);
|
|
7466
|
+
database.addSymbolsToBranchBatch(branchCatalogKey, Array.from(allSymbolIds));
|
|
6069
7467
|
store.save();
|
|
6070
7468
|
invertedIndex.save();
|
|
6071
|
-
|
|
6072
|
-
|
|
7469
|
+
if (scopedRoots) {
|
|
7470
|
+
this.replaceScopedFileHashCache(currentFileHashes, scopedRoots);
|
|
7471
|
+
} else {
|
|
7472
|
+
this.fileHashCache = currentFileHashes;
|
|
7473
|
+
this.saveFileHashCache();
|
|
7474
|
+
}
|
|
6073
7475
|
if (this.config.indexing.autoGc && stats.removedChunks > 0) {
|
|
6074
|
-
await this.maybeRunOrphanGc();
|
|
7476
|
+
const gcReset = await this.maybeRunOrphanGc();
|
|
7477
|
+
if (gcReset) {
|
|
7478
|
+
stats.durationMs = Date.now() - startTime;
|
|
7479
|
+
stats.warning = gcReset.warning;
|
|
7480
|
+
stats.resetCorruptedIndex = true;
|
|
7481
|
+
this.logger.recordIndexingEnd();
|
|
7482
|
+
this.logger.warn("Indexing ended after resetting corrupted local index during automatic GC", {
|
|
7483
|
+
files: stats.totalFiles,
|
|
7484
|
+
indexed: stats.indexedChunks,
|
|
7485
|
+
existing: stats.existingChunks,
|
|
7486
|
+
removed: stats.removedChunks,
|
|
7487
|
+
failed: stats.failedChunks,
|
|
7488
|
+
tokens: stats.tokensUsed,
|
|
7489
|
+
durationMs: stats.durationMs
|
|
7490
|
+
});
|
|
7491
|
+
return stats;
|
|
7492
|
+
}
|
|
6075
7493
|
}
|
|
6076
7494
|
stats.durationMs = Date.now() - startTime;
|
|
7495
|
+
if (forceScopedReembed && failedForcedChunkIds.size === 0) {
|
|
7496
|
+
database.deleteMetadata(this.getProjectForceReembedMetadataKey());
|
|
7497
|
+
}
|
|
6077
7498
|
this.saveIndexMetadata(configuredProviderInfo);
|
|
6078
7499
|
this.indexCompatibility = { compatible: true };
|
|
6079
7500
|
this.logger.recordIndexingEnd();
|
|
@@ -6202,27 +7623,30 @@ var Indexer = class {
|
|
|
6202
7623
|
const keywordResults = await this.keywordSearch(query, maxResults * 4);
|
|
6203
7624
|
const keywordMs = performance2.now() - keywordStartTime;
|
|
6204
7625
|
let branchChunkIds = null;
|
|
6205
|
-
if (filterByBranch && this.currentBranch !== "default") {
|
|
6206
|
-
branchChunkIds = new Set(
|
|
7626
|
+
if (filterByBranch && (this.config.scope === "global" || this.currentBranch !== "default")) {
|
|
7627
|
+
branchChunkIds = new Set(
|
|
7628
|
+
this.getBranchCatalogKeys().flatMap((branchKey) => database.getBranchChunkIds(branchKey))
|
|
7629
|
+
);
|
|
6207
7630
|
}
|
|
6208
7631
|
const prefilterStartTime = performance2.now();
|
|
6209
|
-
const shouldPrefilterByBranch = branchChunkIds !== null && branchChunkIds.size > 0;
|
|
7632
|
+
const shouldPrefilterByBranch = branchChunkIds !== null && (this.config.scope === "global" || branchChunkIds.size > 0);
|
|
7633
|
+
const allowBranchPrefilterFallback = this.config.scope !== "global";
|
|
6210
7634
|
const prefilteredSemantic = shouldPrefilterByBranch && branchChunkIds ? semanticResults.filter((r) => branchChunkIds.has(r.id)) : semanticResults;
|
|
6211
7635
|
const prefilteredKeyword = shouldPrefilterByBranch && branchChunkIds ? keywordResults.filter((r) => branchChunkIds.has(r.id)) : keywordResults;
|
|
6212
|
-
const semanticCandidates = shouldPrefilterByBranch && semanticResults.length > 0 && prefilteredSemantic.length === 0 ? semanticResults : prefilteredSemantic;
|
|
6213
|
-
const keywordCandidates = shouldPrefilterByBranch && keywordResults.length > 0 && prefilteredKeyword.length === 0 ? keywordResults : prefilteredKeyword;
|
|
7636
|
+
const semanticCandidates = allowBranchPrefilterFallback && shouldPrefilterByBranch && semanticResults.length > 0 && prefilteredSemantic.length === 0 ? semanticResults : prefilteredSemantic;
|
|
7637
|
+
const keywordCandidates = allowBranchPrefilterFallback && shouldPrefilterByBranch && keywordResults.length > 0 && prefilteredKeyword.length === 0 ? keywordResults : prefilteredKeyword;
|
|
6214
7638
|
const prefilterMs = performance2.now() - prefilterStartTime;
|
|
6215
|
-
if (branchChunkIds && branchChunkIds.size === 0) {
|
|
7639
|
+
if (this.config.scope !== "global" && branchChunkIds && branchChunkIds.size === 0) {
|
|
6216
7640
|
this.logger.search("warn", "Branch prefilter skipped because branch catalog is empty", {
|
|
6217
7641
|
branch: this.currentBranch
|
|
6218
7642
|
});
|
|
6219
7643
|
}
|
|
6220
|
-
if (shouldPrefilterByBranch && semanticResults.length > 0 && prefilteredSemantic.length === 0) {
|
|
7644
|
+
if (allowBranchPrefilterFallback && shouldPrefilterByBranch && semanticResults.length > 0 && prefilteredSemantic.length === 0) {
|
|
6221
7645
|
this.logger.search("warn", "Branch prefilter produced no semantic overlap, using unfiltered semantic candidates", {
|
|
6222
7646
|
branch: this.currentBranch
|
|
6223
7647
|
});
|
|
6224
7648
|
}
|
|
6225
|
-
if (shouldPrefilterByBranch && keywordResults.length > 0 && prefilteredKeyword.length === 0) {
|
|
7649
|
+
if (allowBranchPrefilterFallback && shouldPrefilterByBranch && keywordResults.length > 0 && prefilteredKeyword.length === 0) {
|
|
6226
7650
|
this.logger.search("warn", "Branch prefilter produced no keyword overlap, using unfiltered keyword candidates", {
|
|
6227
7651
|
branch: this.currentBranch
|
|
6228
7652
|
});
|
|
@@ -6275,26 +7699,13 @@ var Indexer = class {
|
|
|
6275
7699
|
const primaryLane = mergeTieredResults(prePrimaryLane, symbolLane, maxResults * 4);
|
|
6276
7700
|
const tiered = mergeTieredResults(primaryLane, rescued, maxResults * 4);
|
|
6277
7701
|
const hasCodeHints = extractCodeTermHints(query).length > 0 || identifierHints.length > 0;
|
|
6278
|
-
const baseFiltered = tiered.filter((r) =>
|
|
6279
|
-
if (r.score < this.config.search.minScore) return false;
|
|
6280
|
-
if (options?.fileType) {
|
|
6281
|
-
const ext = r.metadata.filePath.split(".").pop()?.toLowerCase();
|
|
6282
|
-
if (ext !== options.fileType.toLowerCase().replace(/^\./, "")) return false;
|
|
6283
|
-
}
|
|
6284
|
-
if (options?.directory) {
|
|
6285
|
-
const normalizedDir = options.directory.replace(/^\/|\/$/g, "");
|
|
6286
|
-
if (!r.metadata.filePath.includes(`/${normalizedDir}/`) && !r.metadata.filePath.includes(`${normalizedDir}/`)) return false;
|
|
6287
|
-
}
|
|
6288
|
-
if (options?.chunkType) {
|
|
6289
|
-
if (r.metadata.chunkType !== options.chunkType) return false;
|
|
6290
|
-
}
|
|
6291
|
-
return true;
|
|
6292
|
-
});
|
|
7702
|
+
const baseFiltered = tiered.filter((r) => matchesSearchFilters(r, options, this.config.search.minScore));
|
|
6293
7703
|
const implementationOnly = baseFiltered.filter(
|
|
6294
7704
|
(r) => isLikelyImplementationPath(r.metadata.filePath) && isImplementationChunkType(r.metadata.chunkType)
|
|
6295
7705
|
);
|
|
6296
7706
|
const filtered = (sourceIntent && hasCodeHints && implementationOnly.length > 0 ? implementationOnly : baseFiltered).slice(0, maxResults);
|
|
6297
|
-
const
|
|
7707
|
+
const identifierFallback = !options?.definitionIntent && filtered.length === 0 && identifierHints.length > 0 ? buildSymbolDefinitionLane(query, database, branchChunkIds, maxResults, union, true).filter((r) => matchesSearchFilters(r, options, this.config.search.minScore)).slice(0, maxResults) : [];
|
|
7708
|
+
const finalResults = filtered.length > 0 ? filtered : identifierFallback;
|
|
6298
7709
|
const totalSearchMs = performance2.now() - searchStartTime;
|
|
6299
7710
|
this.logger.recordSearch(totalSearchMs, {
|
|
6300
7711
|
embeddingMs,
|
|
@@ -6364,7 +7775,8 @@ var Indexer = class {
|
|
|
6364
7775
|
return results.slice(0, limit);
|
|
6365
7776
|
}
|
|
6366
7777
|
async getStatus() {
|
|
6367
|
-
const { store, configuredProviderInfo } = await this.ensureInitialized();
|
|
7778
|
+
const { store, configuredProviderInfo, database } = await this.ensureInitialized();
|
|
7779
|
+
const failedBatchesCount = this.getFailedBatchesCount();
|
|
6368
7780
|
return {
|
|
6369
7781
|
indexed: store.count() > 0,
|
|
6370
7782
|
vectorCount: store.count(),
|
|
@@ -6373,23 +7785,86 @@ var Indexer = class {
|
|
|
6373
7785
|
indexPath: this.indexPath,
|
|
6374
7786
|
currentBranch: this.currentBranch,
|
|
6375
7787
|
baseBranch: this.baseBranch,
|
|
6376
|
-
compatibility: this.indexCompatibility
|
|
7788
|
+
compatibility: this.indexCompatibility,
|
|
7789
|
+
failedBatchesCount,
|
|
7790
|
+
failedBatchesPath: failedBatchesCount > 0 ? this.failedBatchesPath : void 0,
|
|
7791
|
+
warning: database.getMetadata(STARTUP_WARNING_METADATA_KEY) ?? void 0
|
|
6377
7792
|
};
|
|
6378
7793
|
}
|
|
6379
7794
|
async clearIndex() {
|
|
6380
7795
|
const { store, invertedIndex, database } = await this.ensureInitialized();
|
|
7796
|
+
if (this.config.scope === "global") {
|
|
7797
|
+
store.load();
|
|
7798
|
+
invertedIndex.load();
|
|
7799
|
+
this.loadFileHashCache();
|
|
7800
|
+
const roots = this.getScopedRoots();
|
|
7801
|
+
const compatibility = this.checkCompatibility();
|
|
7802
|
+
const allMetadata = store.getAllMetadata();
|
|
7803
|
+
const hasForeignData = allMetadata.some(({ metadata }) => !this.isFileInCurrentScope(metadata.filePath, roots)) || this.hasForeignScopedBranchData() || this.hasForeignScopedFileHashData(roots) || this.hasForeignScopedFailedBatches(roots);
|
|
7804
|
+
if (!compatibility.compatible && hasForeignData) {
|
|
7805
|
+
if (compatibility.code === "EMBEDDING_STRATEGY_MISMATCH" /* EMBEDDING_STRATEGY_MISMATCH */) {
|
|
7806
|
+
this.clearSharedIndexProjectData(store, invertedIndex, database, roots);
|
|
7807
|
+
this.clearScopedFileHashCache(roots);
|
|
7808
|
+
this.clearScopedFailedBatches(roots);
|
|
7809
|
+
database.setMetadata(this.getProjectForceReembedMetadataKey(), "true");
|
|
7810
|
+
database.deleteMetadata(this.getProjectEmbeddingStrategyMetadataKey());
|
|
7811
|
+
this.indexCompatibility = { compatible: true };
|
|
7812
|
+
return;
|
|
7813
|
+
}
|
|
7814
|
+
throw new Error(
|
|
7815
|
+
`Global index compatibility reset is unsafe because the shared index contains files from other projects. The current global index cannot be force-rebuilt for ${this.projectRoot} without deleting other repositories' indexed data. Use scope="project" for isolated rebuilds, or manually delete the shared global index if you intend to rebuild all projects.`
|
|
7816
|
+
);
|
|
7817
|
+
}
|
|
7818
|
+
if (!hasForeignData) {
|
|
7819
|
+
store.clear();
|
|
7820
|
+
store.save();
|
|
7821
|
+
invertedIndex.clear();
|
|
7822
|
+
invertedIndex.save();
|
|
7823
|
+
this.fileHashCache.clear();
|
|
7824
|
+
this.saveFileHashCache();
|
|
7825
|
+
database.clearAllIndexedData();
|
|
7826
|
+
this.saveFailedBatches([]);
|
|
7827
|
+
database.deleteMetadata("index.version");
|
|
7828
|
+
database.deleteMetadata("index.embeddingProvider");
|
|
7829
|
+
database.deleteMetadata("index.embeddingModel");
|
|
7830
|
+
database.deleteMetadata("index.embeddingDimensions");
|
|
7831
|
+
database.deleteMetadata("index.embeddingStrategyVersion");
|
|
7832
|
+
database.deleteMetadata(this.getProjectEmbeddingStrategyMetadataKey());
|
|
7833
|
+
database.deleteMetadata(this.getProjectForceReembedMetadataKey());
|
|
7834
|
+
database.deleteMetadata(this.getLegacyMigrationMetadataKey());
|
|
7835
|
+
database.deleteMetadata("index.createdAt");
|
|
7836
|
+
database.deleteMetadata("index.updatedAt");
|
|
7837
|
+
this.indexCompatibility = this.validateIndexCompatibility(this.configuredProviderInfo);
|
|
7838
|
+
return;
|
|
7839
|
+
}
|
|
7840
|
+
this.clearSharedIndexProjectData(store, invertedIndex, database, roots);
|
|
7841
|
+
this.clearScopedFileHashCache(roots);
|
|
7842
|
+
this.clearScopedFailedBatches(roots);
|
|
7843
|
+
this.indexCompatibility = compatibility;
|
|
7844
|
+
return;
|
|
7845
|
+
}
|
|
7846
|
+
const localProjectIndexPath = path8.join(this.projectRoot, ".opencode", "index");
|
|
7847
|
+
if (path8.resolve(this.indexPath) !== path8.resolve(localProjectIndexPath)) {
|
|
7848
|
+
throw new Error(
|
|
7849
|
+
"Project-scoped force rebuild is unsafe while using an inherited worktree index. Create a local project config boundary before clearing the index."
|
|
7850
|
+
);
|
|
7851
|
+
}
|
|
6381
7852
|
store.clear();
|
|
6382
7853
|
store.save();
|
|
6383
7854
|
invertedIndex.clear();
|
|
6384
7855
|
invertedIndex.save();
|
|
6385
7856
|
this.fileHashCache.clear();
|
|
6386
7857
|
this.saveFileHashCache();
|
|
6387
|
-
database.
|
|
6388
|
-
|
|
7858
|
+
database.clearAllIndexedData();
|
|
7859
|
+
this.saveFailedBatches([]);
|
|
6389
7860
|
database.deleteMetadata("index.version");
|
|
6390
7861
|
database.deleteMetadata("index.embeddingProvider");
|
|
6391
7862
|
database.deleteMetadata("index.embeddingModel");
|
|
6392
7863
|
database.deleteMetadata("index.embeddingDimensions");
|
|
7864
|
+
database.deleteMetadata("index.embeddingStrategyVersion");
|
|
7865
|
+
database.deleteMetadata(this.getProjectEmbeddingStrategyMetadataKey());
|
|
7866
|
+
database.deleteMetadata(this.getProjectForceReembedMetadataKey());
|
|
7867
|
+
database.deleteMetadata(this.getLegacyMigrationMetadataKey());
|
|
6393
7868
|
database.deleteMetadata("index.createdAt");
|
|
6394
7869
|
database.deleteMetadata("index.updatedAt");
|
|
6395
7870
|
this.indexCompatibility = this.validateIndexCompatibility(this.configuredProviderInfo);
|
|
@@ -6405,28 +7880,61 @@ var Indexer = class {
|
|
|
6405
7880
|
filePathsToChunkKeys.set(metadata.filePath, existing);
|
|
6406
7881
|
}
|
|
6407
7882
|
const removedFilePaths = [];
|
|
6408
|
-
|
|
7883
|
+
const removedChunkKeys = [];
|
|
7884
|
+
const chunkKeysByRemovedFile = /* @__PURE__ */ new Map();
|
|
6409
7885
|
for (const [filePath, chunkKeys] of filePathsToChunkKeys) {
|
|
6410
|
-
if (!
|
|
7886
|
+
if (!existsSync6(filePath)) {
|
|
7887
|
+
chunkKeysByRemovedFile.set(filePath, chunkKeys);
|
|
6411
7888
|
for (const key of chunkKeys) {
|
|
6412
|
-
|
|
6413
|
-
invertedIndex.removeChunk(key);
|
|
6414
|
-
removedCount++;
|
|
7889
|
+
removedChunkKeys.push(key);
|
|
6415
7890
|
}
|
|
6416
|
-
database.deleteChunksByFile(filePath);
|
|
6417
|
-
database.deleteCallEdgesByFile(filePath);
|
|
6418
|
-
database.deleteSymbolsByFile(filePath);
|
|
6419
7891
|
removedFilePaths.push(filePath);
|
|
6420
7892
|
}
|
|
6421
7893
|
}
|
|
7894
|
+
if (removedChunkKeys.length > 0) {
|
|
7895
|
+
this.rebuildVectorStoreExcludingChunkIds(store, database, removedChunkKeys);
|
|
7896
|
+
for (const key of removedChunkKeys) {
|
|
7897
|
+
invertedIndex.removeChunk(key);
|
|
7898
|
+
}
|
|
7899
|
+
}
|
|
7900
|
+
for (const filePath of removedFilePaths) {
|
|
7901
|
+
const fileChunkKeys = chunkKeysByRemovedFile.get(filePath) ?? [];
|
|
7902
|
+
if (fileChunkKeys.length > 0) {
|
|
7903
|
+
database.deleteChunksByIds(fileChunkKeys);
|
|
7904
|
+
}
|
|
7905
|
+
database.deleteCallEdgesByFile(filePath);
|
|
7906
|
+
database.deleteSymbolsByFile(filePath);
|
|
7907
|
+
}
|
|
7908
|
+
const removedCount = removedChunkKeys.length;
|
|
6422
7909
|
if (removedCount > 0) {
|
|
6423
7910
|
store.save();
|
|
6424
7911
|
invertedIndex.save();
|
|
6425
7912
|
}
|
|
6426
|
-
|
|
6427
|
-
|
|
6428
|
-
|
|
6429
|
-
|
|
7913
|
+
let gcOrphanEmbeddings;
|
|
7914
|
+
let gcOrphanChunks;
|
|
7915
|
+
let gcOrphanSymbols;
|
|
7916
|
+
let gcOrphanCallEdges;
|
|
7917
|
+
try {
|
|
7918
|
+
gcOrphanEmbeddings = database.gcOrphanEmbeddings();
|
|
7919
|
+
gcOrphanChunks = database.gcOrphanChunks();
|
|
7920
|
+
gcOrphanSymbols = database.gcOrphanSymbols();
|
|
7921
|
+
gcOrphanCallEdges = database.gcOrphanCallEdges();
|
|
7922
|
+
} catch (error) {
|
|
7923
|
+
if (!await this.tryResetCorruptedIndex("running index health check", error)) {
|
|
7924
|
+
throw error;
|
|
7925
|
+
}
|
|
7926
|
+
await this.ensureInitialized();
|
|
7927
|
+
return {
|
|
7928
|
+
removed: 0,
|
|
7929
|
+
filePaths: [],
|
|
7930
|
+
gcOrphanEmbeddings: 0,
|
|
7931
|
+
gcOrphanChunks: 0,
|
|
7932
|
+
gcOrphanSymbols: 0,
|
|
7933
|
+
gcOrphanCallEdges: 0,
|
|
7934
|
+
resetCorruptedIndex: true,
|
|
7935
|
+
warning: this.getCorruptedIndexWarning(path8.join(this.indexPath, "codebase.db"))
|
|
7936
|
+
};
|
|
7937
|
+
}
|
|
6430
7938
|
this.logger.recordGc(removedCount, gcOrphanChunks, gcOrphanEmbeddings);
|
|
6431
7939
|
this.logger.gc("info", "Health check complete", {
|
|
6432
7940
|
removedStale: removedCount,
|
|
@@ -6437,8 +7945,12 @@ var Indexer = class {
|
|
|
6437
7945
|
return { removed: removedCount, filePaths: removedFilePaths, gcOrphanEmbeddings, gcOrphanChunks, gcOrphanSymbols, gcOrphanCallEdges };
|
|
6438
7946
|
}
|
|
6439
7947
|
async retryFailedBatches() {
|
|
6440
|
-
const { store, provider, invertedIndex } = await this.ensureInitialized();
|
|
6441
|
-
const
|
|
7948
|
+
const { store, provider, invertedIndex, database, configuredProviderInfo } = await this.ensureInitialized();
|
|
7949
|
+
const maxChunkTokens = getSafeEmbeddingChunkTokenLimit(configuredProviderInfo);
|
|
7950
|
+
const providerRateLimits = this.getProviderRateLimits(configuredProviderInfo.provider);
|
|
7951
|
+
const roots = this.config.scope === "global" ? this.getScopedRoots() : null;
|
|
7952
|
+
const { scoped: scopedFailedBatches, retained: retainedFailedBatches } = roots ? this.partitionFailedBatches(roots, maxChunkTokens) : { scoped: this.loadFailedBatches(maxChunkTokens), retained: [] };
|
|
7953
|
+
const failedBatches = scopedFailedBatches;
|
|
6442
7954
|
if (failedBatches.length === 0) {
|
|
6443
7955
|
return { succeeded: 0, failed: 0, remaining: 0 };
|
|
6444
7956
|
}
|
|
@@ -6446,49 +7958,170 @@ var Indexer = class {
|
|
|
6446
7958
|
let failed = 0;
|
|
6447
7959
|
const stillFailing = [];
|
|
6448
7960
|
for (const batch of failedBatches) {
|
|
7961
|
+
const batchChunksById = new Map(batch.chunks.map((chunk) => [chunk.id, chunk]));
|
|
7962
|
+
const embeddingPartsByChunk = /* @__PURE__ */ new Map();
|
|
7963
|
+
const completedChunkIds = /* @__PURE__ */ new Set();
|
|
7964
|
+
const failedChunkIds = /* @__PURE__ */ new Set();
|
|
7965
|
+
const failedChunksForBatch = /* @__PURE__ */ new Map();
|
|
7966
|
+
const pooledResults = [];
|
|
6449
7967
|
try {
|
|
6450
|
-
const
|
|
6451
|
-
|
|
6452
|
-
|
|
6453
|
-
return provider.embedBatch(texts);
|
|
6454
|
-
},
|
|
6455
|
-
{
|
|
6456
|
-
retries: this.config.indexing.retries,
|
|
6457
|
-
minTimeout: this.config.indexing.retryDelayMs
|
|
6458
|
-
}
|
|
7968
|
+
const requestBatches = createPendingEmbeddingRequestBatches(
|
|
7969
|
+
batch.chunks,
|
|
7970
|
+
getDynamicBatchOptions(configuredProviderInfo)
|
|
6459
7971
|
);
|
|
6460
|
-
const
|
|
7972
|
+
for (const requestBatch of requestBatches) {
|
|
7973
|
+
try {
|
|
7974
|
+
const result = await pRetry(
|
|
7975
|
+
async () => {
|
|
7976
|
+
const texts = requestBatch.map((request) => request.text);
|
|
7977
|
+
return provider.embedBatch(texts);
|
|
7978
|
+
},
|
|
7979
|
+
{
|
|
7980
|
+
retries: this.config.indexing.retries,
|
|
7981
|
+
minTimeout: Math.max(this.config.indexing.retryDelayMs, providerRateLimits.minRetryMs),
|
|
7982
|
+
maxTimeout: providerRateLimits.maxRetryMs,
|
|
7983
|
+
factor: 2,
|
|
7984
|
+
shouldRetry: (error) => !(error.error instanceof CustomProviderNonRetryableError)
|
|
7985
|
+
}
|
|
7986
|
+
);
|
|
7987
|
+
const touchedChunkIds = /* @__PURE__ */ new Set();
|
|
7988
|
+
requestBatch.forEach((request, idx) => {
|
|
7989
|
+
if (failedChunkIds.has(request.chunk.id) || completedChunkIds.has(request.chunk.id)) {
|
|
7990
|
+
return;
|
|
7991
|
+
}
|
|
7992
|
+
const vector = result.embeddings[idx];
|
|
7993
|
+
if (!vector) {
|
|
7994
|
+
throw new Error(`Embedding API returned too few vectors for chunk ${request.chunk.id}`);
|
|
7995
|
+
}
|
|
7996
|
+
const parts = embeddingPartsByChunk.get(request.chunk.id) ?? [];
|
|
7997
|
+
parts[request.partIndex] = {
|
|
7998
|
+
vector,
|
|
7999
|
+
tokenCount: request.tokenCount
|
|
8000
|
+
};
|
|
8001
|
+
embeddingPartsByChunk.set(request.chunk.id, parts);
|
|
8002
|
+
touchedChunkIds.add(request.chunk.id);
|
|
8003
|
+
});
|
|
8004
|
+
for (const chunkId of touchedChunkIds) {
|
|
8005
|
+
if (failedChunkIds.has(chunkId) || completedChunkIds.has(chunkId)) {
|
|
8006
|
+
continue;
|
|
8007
|
+
}
|
|
8008
|
+
const chunk = batchChunksById.get(chunkId);
|
|
8009
|
+
if (!chunk) {
|
|
8010
|
+
continue;
|
|
8011
|
+
}
|
|
8012
|
+
const parts = embeddingPartsByChunk.get(chunk.id) ?? [];
|
|
8013
|
+
if (!hasAllEmbeddingParts(parts, chunk.texts.length)) {
|
|
8014
|
+
continue;
|
|
8015
|
+
}
|
|
8016
|
+
const orderedParts = parts;
|
|
8017
|
+
pooledResults.push({
|
|
8018
|
+
chunk,
|
|
8019
|
+
vector: poolEmbeddingVectors(
|
|
8020
|
+
orderedParts.map((part) => part.vector),
|
|
8021
|
+
orderedParts.map((part) => part.tokenCount)
|
|
8022
|
+
)
|
|
8023
|
+
});
|
|
8024
|
+
}
|
|
8025
|
+
this.logger.recordEmbeddingApiCall(result.totalTokensUsed);
|
|
8026
|
+
} catch (error) {
|
|
8027
|
+
const failureMessage = String(error);
|
|
8028
|
+
const failureTimestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
8029
|
+
const failedChunks = getUniquePendingChunksFromRequests(requestBatch).filter((chunk) => !completedChunkIds.has(chunk.id) && !failedChunkIds.has(chunk.id));
|
|
8030
|
+
for (const chunk of failedChunks) {
|
|
8031
|
+
failedChunkIds.add(chunk.id);
|
|
8032
|
+
embeddingPartsByChunk.delete(chunk.id);
|
|
8033
|
+
failedChunksForBatch.set(chunk.id, {
|
|
8034
|
+
chunks: [chunk],
|
|
8035
|
+
attemptCount: batch.attemptCount + 1,
|
|
8036
|
+
lastAttempt: failureTimestamp,
|
|
8037
|
+
error: failureMessage
|
|
8038
|
+
});
|
|
8039
|
+
}
|
|
8040
|
+
failed += failedChunks.length;
|
|
8041
|
+
this.logger.recordEmbeddingError();
|
|
8042
|
+
}
|
|
8043
|
+
}
|
|
8044
|
+
const successfulResults = pooledResults.filter(({ chunk }) => !failedChunkIds.has(chunk.id));
|
|
8045
|
+
const items = successfulResults.map(({ chunk, vector }) => ({
|
|
6461
8046
|
id: chunk.id,
|
|
6462
|
-
vector
|
|
8047
|
+
vector,
|
|
6463
8048
|
metadata: chunk.metadata
|
|
6464
8049
|
}));
|
|
6465
|
-
|
|
6466
|
-
|
|
8050
|
+
if (items.length > 0) {
|
|
8051
|
+
store.addBatch(items);
|
|
8052
|
+
}
|
|
8053
|
+
if (successfulResults.length > 0) {
|
|
8054
|
+
try {
|
|
8055
|
+
database.upsertEmbeddingsBatch(
|
|
8056
|
+
successfulResults.map(({ chunk, vector }) => ({
|
|
8057
|
+
contentHash: chunk.contentHash,
|
|
8058
|
+
embedding: float32ArrayToBuffer(vector),
|
|
8059
|
+
chunkText: chunk.storageText,
|
|
8060
|
+
model: configuredProviderInfo.modelInfo.model
|
|
8061
|
+
}))
|
|
8062
|
+
);
|
|
8063
|
+
} catch (dbError) {
|
|
8064
|
+
this.rebuildVectorStoreExcludingChunkIds(
|
|
8065
|
+
store,
|
|
8066
|
+
database,
|
|
8067
|
+
successfulResults.map(({ chunk }) => chunk.id)
|
|
8068
|
+
);
|
|
8069
|
+
throw dbError;
|
|
8070
|
+
}
|
|
8071
|
+
}
|
|
8072
|
+
for (const { chunk } of successfulResults) {
|
|
6467
8073
|
invertedIndex.removeChunk(chunk.id);
|
|
6468
8074
|
invertedIndex.addChunk(chunk.id, chunk.content);
|
|
8075
|
+
completedChunkIds.add(chunk.id);
|
|
8076
|
+
embeddingPartsByChunk.delete(chunk.id);
|
|
6469
8077
|
}
|
|
6470
|
-
|
|
6471
|
-
|
|
6472
|
-
|
|
8078
|
+
database.addChunksToBranchBatch(
|
|
8079
|
+
this.getBranchCatalogKey(),
|
|
8080
|
+
successfulResults.map(({ chunk }) => chunk.id)
|
|
8081
|
+
);
|
|
8082
|
+
this.logger.recordChunksEmbedded(successfulResults.length);
|
|
8083
|
+
succeeded += successfulResults.length;
|
|
8084
|
+
stillFailing.push(...failedChunksForBatch.values());
|
|
6473
8085
|
} catch (error) {
|
|
6474
|
-
|
|
8086
|
+
const failureMessage = getErrorMessage(error);
|
|
8087
|
+
const failureTimestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
8088
|
+
const unaccountedChunks = batch.chunks.filter(
|
|
8089
|
+
(chunk) => !failedChunksForBatch.has(chunk.id) && !completedChunkIds.has(chunk.id)
|
|
8090
|
+
);
|
|
8091
|
+
for (const chunk of unaccountedChunks) {
|
|
8092
|
+
failedChunksForBatch.set(chunk.id, {
|
|
8093
|
+
chunks: [chunk],
|
|
8094
|
+
attemptCount: batch.attemptCount + 1,
|
|
8095
|
+
lastAttempt: failureTimestamp,
|
|
8096
|
+
error: failureMessage
|
|
8097
|
+
});
|
|
8098
|
+
}
|
|
8099
|
+
failed += unaccountedChunks.length;
|
|
6475
8100
|
this.logger.recordEmbeddingError();
|
|
6476
|
-
stillFailing.push(
|
|
6477
|
-
...batch,
|
|
6478
|
-
attemptCount: batch.attemptCount + 1,
|
|
6479
|
-
lastAttempt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
6480
|
-
error: String(error)
|
|
6481
|
-
});
|
|
8101
|
+
stillFailing.push(...coalesceFailedBatches(Array.from(failedChunksForBatch.values())));
|
|
6482
8102
|
}
|
|
6483
8103
|
}
|
|
6484
|
-
|
|
8104
|
+
const persistedStillFailing = coalesceFailedBatches(stillFailing);
|
|
8105
|
+
if (roots) {
|
|
8106
|
+
this.saveFailedBatches([...retainedFailedBatches, ...persistedStillFailing]);
|
|
8107
|
+
} else {
|
|
8108
|
+
this.saveFailedBatches(persistedStillFailing);
|
|
8109
|
+
}
|
|
6485
8110
|
if (succeeded > 0) {
|
|
6486
8111
|
store.save();
|
|
6487
8112
|
invertedIndex.save();
|
|
6488
8113
|
}
|
|
6489
|
-
|
|
8114
|
+
if (roots && succeeded > 0 && persistedStillFailing.length === 0 && this.hasProjectForceReembedPending()) {
|
|
8115
|
+
database.deleteMetadata(this.getProjectForceReembedMetadataKey());
|
|
8116
|
+
this.saveIndexMetadata(configuredProviderInfo);
|
|
8117
|
+
this.indexCompatibility = { compatible: true };
|
|
8118
|
+
}
|
|
8119
|
+
return { succeeded, failed, remaining: persistedStillFailing.length };
|
|
6490
8120
|
}
|
|
6491
8121
|
getFailedBatchesCount() {
|
|
8122
|
+
if (this.config.scope === "global") {
|
|
8123
|
+
return this.partitionFailedBatches(this.getScopedRoots()).scoped.length;
|
|
8124
|
+
}
|
|
6492
8125
|
return this.loadFailedBatches().length;
|
|
6493
8126
|
}
|
|
6494
8127
|
getCurrentBranch() {
|
|
@@ -6537,20 +8170,23 @@ var Indexer = class {
|
|
|
6537
8170
|
const semanticResults = store.search(embedding, limit * 2);
|
|
6538
8171
|
const vectorMs = performance2.now() - vectorStartTime;
|
|
6539
8172
|
let branchChunkIds = null;
|
|
6540
|
-
if (filterByBranch && this.currentBranch !== "default") {
|
|
6541
|
-
branchChunkIds = new Set(
|
|
8173
|
+
if (filterByBranch && (this.config.scope === "global" || this.currentBranch !== "default")) {
|
|
8174
|
+
branchChunkIds = new Set(
|
|
8175
|
+
this.getBranchCatalogKeys().flatMap((branchKey) => database.getBranchChunkIds(branchKey))
|
|
8176
|
+
);
|
|
6542
8177
|
}
|
|
6543
8178
|
const prefilterStartTime = performance2.now();
|
|
6544
|
-
const shouldPrefilterByBranch = branchChunkIds !== null && branchChunkIds.size > 0;
|
|
8179
|
+
const shouldPrefilterByBranch = branchChunkIds !== null && (this.config.scope === "global" || branchChunkIds.size > 0);
|
|
8180
|
+
const allowBranchPrefilterFallback = this.config.scope !== "global";
|
|
6545
8181
|
const prefilteredSemantic = shouldPrefilterByBranch && branchChunkIds ? semanticResults.filter((r) => branchChunkIds.has(r.id)) : semanticResults;
|
|
6546
|
-
const semanticCandidates = shouldPrefilterByBranch && semanticResults.length > 0 && prefilteredSemantic.length === 0 ? semanticResults : prefilteredSemantic;
|
|
8182
|
+
const semanticCandidates = allowBranchPrefilterFallback && shouldPrefilterByBranch && semanticResults.length > 0 && prefilteredSemantic.length === 0 ? semanticResults : prefilteredSemantic;
|
|
6547
8183
|
const prefilterMs = performance2.now() - prefilterStartTime;
|
|
6548
|
-
if (branchChunkIds && branchChunkIds.size === 0) {
|
|
8184
|
+
if (this.config.scope !== "global" && branchChunkIds && branchChunkIds.size === 0) {
|
|
6549
8185
|
this.logger.search("warn", "Branch prefilter skipped because branch catalog is empty", {
|
|
6550
8186
|
branch: this.currentBranch
|
|
6551
8187
|
});
|
|
6552
8188
|
}
|
|
6553
|
-
if (shouldPrefilterByBranch && semanticResults.length > 0 && prefilteredSemantic.length === 0) {
|
|
8189
|
+
if (allowBranchPrefilterFallback && shouldPrefilterByBranch && semanticResults.length > 0 && prefilteredSemantic.length === 0) {
|
|
6554
8190
|
this.logger.search("warn", "Branch prefilter produced no semantic overlap, using unfiltered semantic candidates", {
|
|
6555
8191
|
branch: this.currentBranch
|
|
6556
8192
|
});
|
|
@@ -6623,11 +8259,39 @@ var Indexer = class {
|
|
|
6623
8259
|
}
|
|
6624
8260
|
async getCallers(targetName) {
|
|
6625
8261
|
const { database } = await this.ensureInitialized();
|
|
6626
|
-
|
|
8262
|
+
const seen = /* @__PURE__ */ new Set();
|
|
8263
|
+
const results = [];
|
|
8264
|
+
for (const branchKey of this.getBranchCatalogKeys()) {
|
|
8265
|
+
for (const edge of database.getCallersWithContext(targetName, branchKey)) {
|
|
8266
|
+
if (!seen.has(edge.id)) {
|
|
8267
|
+
seen.add(edge.id);
|
|
8268
|
+
results.push(edge);
|
|
8269
|
+
}
|
|
8270
|
+
}
|
|
8271
|
+
}
|
|
8272
|
+
return results;
|
|
6627
8273
|
}
|
|
6628
8274
|
async getCallees(symbolId) {
|
|
6629
8275
|
const { database } = await this.ensureInitialized();
|
|
6630
|
-
|
|
8276
|
+
const seen = /* @__PURE__ */ new Set();
|
|
8277
|
+
const results = [];
|
|
8278
|
+
for (const branchKey of this.getBranchCatalogKeys()) {
|
|
8279
|
+
for (const edge of database.getCallees(symbolId, branchKey)) {
|
|
8280
|
+
if (!seen.has(edge.id)) {
|
|
8281
|
+
seen.add(edge.id);
|
|
8282
|
+
results.push(edge);
|
|
8283
|
+
}
|
|
8284
|
+
}
|
|
8285
|
+
}
|
|
8286
|
+
return results;
|
|
8287
|
+
}
|
|
8288
|
+
async close() {
|
|
8289
|
+
await this.database?.close();
|
|
8290
|
+
this.database = null;
|
|
8291
|
+
this.store = null;
|
|
8292
|
+
this.invertedIndex = null;
|
|
8293
|
+
this.provider = null;
|
|
8294
|
+
this.reranker = null;
|
|
6631
8295
|
}
|
|
6632
8296
|
};
|
|
6633
8297
|
|
|
@@ -6878,9 +8542,9 @@ function computeEvalMetrics(queries, perQuery, embeddingCallCount, embeddingToke
|
|
|
6878
8542
|
}
|
|
6879
8543
|
|
|
6880
8544
|
// src/eval/schema.ts
|
|
6881
|
-
import { readFileSync as
|
|
8545
|
+
import { readFileSync as readFileSync7 } from "fs";
|
|
6882
8546
|
function parseJsonFile(filePath) {
|
|
6883
|
-
const content =
|
|
8547
|
+
const content = readFileSync7(filePath, "utf-8");
|
|
6884
8548
|
return JSON.parse(content);
|
|
6885
8549
|
}
|
|
6886
8550
|
function isRecord(value) {
|
|
@@ -6889,23 +8553,23 @@ function isRecord(value) {
|
|
|
6889
8553
|
function isStringArray2(value) {
|
|
6890
8554
|
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
6891
8555
|
}
|
|
6892
|
-
function asPositiveNumber(value,
|
|
8556
|
+
function asPositiveNumber(value, path13) {
|
|
6893
8557
|
if (typeof value !== "number" || Number.isNaN(value) || value < 0) {
|
|
6894
|
-
throw new Error(`${
|
|
8558
|
+
throw new Error(`${path13} must be a non-negative number`);
|
|
6895
8559
|
}
|
|
6896
8560
|
return value;
|
|
6897
8561
|
}
|
|
6898
|
-
function parseQueryType(value,
|
|
8562
|
+
function parseQueryType(value, path13) {
|
|
6899
8563
|
if (value === "definition" || value === "implementation-intent" || value === "similarity" || value === "keyword-heavy") {
|
|
6900
8564
|
return value;
|
|
6901
8565
|
}
|
|
6902
8566
|
throw new Error(
|
|
6903
|
-
`${
|
|
8567
|
+
`${path13} must be one of: definition, implementation-intent, similarity, keyword-heavy`
|
|
6904
8568
|
);
|
|
6905
8569
|
}
|
|
6906
|
-
function parseExpected(input,
|
|
8570
|
+
function parseExpected(input, path13) {
|
|
6907
8571
|
if (!isRecord(input)) {
|
|
6908
|
-
throw new Error(`${
|
|
8572
|
+
throw new Error(`${path13} must be an object`);
|
|
6909
8573
|
}
|
|
6910
8574
|
const filePathRaw = input.filePath;
|
|
6911
8575
|
const acceptableFilesRaw = input.acceptableFiles;
|
|
@@ -6914,16 +8578,16 @@ function parseExpected(input, path11) {
|
|
|
6914
8578
|
const filePath = typeof filePathRaw === "string" ? filePathRaw : void 0;
|
|
6915
8579
|
const acceptableFiles = isStringArray2(acceptableFilesRaw) ? acceptableFilesRaw : void 0;
|
|
6916
8580
|
if (!filePath && (!acceptableFiles || acceptableFiles.length === 0)) {
|
|
6917
|
-
throw new Error(`${
|
|
8581
|
+
throw new Error(`${path13} must include either expected.filePath or expected.acceptableFiles`);
|
|
6918
8582
|
}
|
|
6919
8583
|
if (acceptableFilesRaw !== void 0 && !isStringArray2(acceptableFilesRaw)) {
|
|
6920
|
-
throw new Error(`${
|
|
8584
|
+
throw new Error(`${path13}.acceptableFiles must be an array of strings`);
|
|
6921
8585
|
}
|
|
6922
8586
|
if (symbolRaw !== void 0 && typeof symbolRaw !== "string") {
|
|
6923
|
-
throw new Error(`${
|
|
8587
|
+
throw new Error(`${path13}.symbol must be a string when provided`);
|
|
6924
8588
|
}
|
|
6925
8589
|
if (branchRaw !== void 0 && typeof branchRaw !== "string") {
|
|
6926
|
-
throw new Error(`${
|
|
8590
|
+
throw new Error(`${path13}.branch must be a string when provided`);
|
|
6927
8591
|
}
|
|
6928
8592
|
return {
|
|
6929
8593
|
filePath,
|
|
@@ -6933,25 +8597,25 @@ function parseExpected(input, path11) {
|
|
|
6933
8597
|
};
|
|
6934
8598
|
}
|
|
6935
8599
|
function parseQuery(input, index) {
|
|
6936
|
-
const
|
|
8600
|
+
const path13 = `queries[${index}]`;
|
|
6937
8601
|
if (!isRecord(input)) {
|
|
6938
|
-
throw new Error(`${
|
|
8602
|
+
throw new Error(`${path13} must be an object`);
|
|
6939
8603
|
}
|
|
6940
8604
|
const id = input.id;
|
|
6941
8605
|
const query = input.query;
|
|
6942
8606
|
const queryType = input.queryType;
|
|
6943
8607
|
const expected = input.expected;
|
|
6944
8608
|
if (typeof id !== "string" || id.trim().length === 0) {
|
|
6945
|
-
throw new Error(`${
|
|
8609
|
+
throw new Error(`${path13}.id must be a non-empty string`);
|
|
6946
8610
|
}
|
|
6947
8611
|
if (typeof query !== "string" || query.trim().length === 0) {
|
|
6948
|
-
throw new Error(`${
|
|
8612
|
+
throw new Error(`${path13}.query must be a non-empty string`);
|
|
6949
8613
|
}
|
|
6950
8614
|
return {
|
|
6951
8615
|
id,
|
|
6952
8616
|
query,
|
|
6953
|
-
queryType: parseQueryType(queryType, `${
|
|
6954
|
-
expected: parseExpected(expected, `${
|
|
8617
|
+
queryType: parseQueryType(queryType, `${path13}.queryType`),
|
|
8618
|
+
expected: parseExpected(expected, `${path13}.expected`)
|
|
6955
8619
|
};
|
|
6956
8620
|
}
|
|
6957
8621
|
function parseGoldenDataset(raw, sourceLabel) {
|
|
@@ -7048,35 +8712,83 @@ function loadBudget(budgetPath) {
|
|
|
7048
8712
|
|
|
7049
8713
|
// src/eval/runner.ts
|
|
7050
8714
|
function toAbsolute(projectRoot, maybeRelative) {
|
|
7051
|
-
return
|
|
8715
|
+
return path9.isAbsolute(maybeRelative) ? maybeRelative : path9.join(projectRoot, maybeRelative);
|
|
8716
|
+
}
|
|
8717
|
+
function isProjectScopedConfigPath(configPath) {
|
|
8718
|
+
return path9.basename(configPath) === "codebase-index.json" && path9.basename(path9.dirname(configPath)) === ".opencode";
|
|
8719
|
+
}
|
|
8720
|
+
function normalizeEvalConfigKnowledgeBases(rawConfig, projectRoot, resolvedConfigPath) {
|
|
8721
|
+
const config = rawConfig && typeof rawConfig === "object" ? { ...rawConfig } : {};
|
|
8722
|
+
if (!Array.isArray(config.knowledgeBases)) {
|
|
8723
|
+
return config;
|
|
8724
|
+
}
|
|
8725
|
+
config.knowledgeBases = isProjectScopedConfigPath(resolvedConfigPath) ? resolveInheritedKnowledgeBaseEntries(
|
|
8726
|
+
config.knowledgeBases,
|
|
8727
|
+
path9.dirname(path9.dirname(resolvedConfigPath)),
|
|
8728
|
+
projectRoot
|
|
8729
|
+
) : rebasePathEntries(
|
|
8730
|
+
config.knowledgeBases,
|
|
8731
|
+
path9.dirname(resolvedConfigPath),
|
|
8732
|
+
projectRoot
|
|
8733
|
+
);
|
|
8734
|
+
return config;
|
|
7052
8735
|
}
|
|
7053
8736
|
function loadRawConfig(projectRoot, configPath) {
|
|
7054
8737
|
const fromPath = configPath ? toAbsolute(projectRoot, configPath) : null;
|
|
7055
|
-
if (fromPath &&
|
|
7056
|
-
return
|
|
8738
|
+
if (fromPath && existsSync7(fromPath)) {
|
|
8739
|
+
return normalizeEvalConfigKnowledgeBases(
|
|
8740
|
+
JSON.parse(readFileSync8(fromPath, "utf-8")),
|
|
8741
|
+
projectRoot,
|
|
8742
|
+
fromPath
|
|
8743
|
+
);
|
|
7057
8744
|
}
|
|
7058
|
-
const projectConfig =
|
|
7059
|
-
if (
|
|
7060
|
-
return
|
|
8745
|
+
const projectConfig = resolveProjectConfigPath(projectRoot);
|
|
8746
|
+
if (existsSync7(projectConfig)) {
|
|
8747
|
+
return normalizeEvalConfigKnowledgeBases(
|
|
8748
|
+
JSON.parse(readFileSync8(projectConfig, "utf-8")),
|
|
8749
|
+
projectRoot,
|
|
8750
|
+
projectConfig
|
|
8751
|
+
);
|
|
7061
8752
|
}
|
|
7062
|
-
const globalConfig =
|
|
7063
|
-
if (
|
|
7064
|
-
return JSON.parse(
|
|
8753
|
+
const globalConfig = path9.join(os5.homedir(), ".config", "opencode", "codebase-index.json");
|
|
8754
|
+
if (existsSync7(globalConfig)) {
|
|
8755
|
+
return JSON.parse(readFileSync8(globalConfig, "utf-8"));
|
|
7065
8756
|
}
|
|
7066
8757
|
return {};
|
|
7067
8758
|
}
|
|
7068
8759
|
function getIndexRootPath(projectRoot, scope) {
|
|
7069
|
-
|
|
7070
|
-
|
|
7071
|
-
|
|
7072
|
-
return
|
|
8760
|
+
return scope === "global" ? getGlobalIndexPath() : resolveProjectIndexPath(projectRoot, scope);
|
|
8761
|
+
}
|
|
8762
|
+
function getLocalProjectIndexRoot(projectRoot) {
|
|
8763
|
+
return path9.join(projectRoot, ".opencode", "index");
|
|
8764
|
+
}
|
|
8765
|
+
function getLocalProjectConfigPath(projectRoot) {
|
|
8766
|
+
return path9.join(projectRoot, ".opencode", "codebase-index.json");
|
|
7073
8767
|
}
|
|
7074
8768
|
function clearIndexRoot(projectRoot, scope) {
|
|
7075
|
-
const indexRoot = getIndexRootPath(projectRoot, scope);
|
|
7076
|
-
if (
|
|
8769
|
+
const indexRoot = scope === "global" ? getIndexRootPath(projectRoot, scope) : getLocalProjectIndexRoot(projectRoot);
|
|
8770
|
+
if (existsSync7(indexRoot)) {
|
|
7077
8771
|
rmSync(indexRoot, { recursive: true, force: true });
|
|
7078
8772
|
}
|
|
7079
8773
|
}
|
|
8774
|
+
function ensureLocalEvalProjectConfig(projectRoot, configPath) {
|
|
8775
|
+
const localConfigPath = getLocalProjectConfigPath(projectRoot);
|
|
8776
|
+
const resolvedConfigPath = configPath ? toAbsolute(projectRoot, configPath) : resolveProjectConfigPath(projectRoot);
|
|
8777
|
+
if (!configPath && existsSync7(localConfigPath)) {
|
|
8778
|
+
return localConfigPath;
|
|
8779
|
+
}
|
|
8780
|
+
if (!existsSync7(resolvedConfigPath) || resolvedConfigPath === localConfigPath) {
|
|
8781
|
+
return resolvedConfigPath;
|
|
8782
|
+
}
|
|
8783
|
+
const sourceConfig = normalizeEvalConfigKnowledgeBases(
|
|
8784
|
+
JSON.parse(readFileSync8(resolvedConfigPath, "utf-8")),
|
|
8785
|
+
projectRoot,
|
|
8786
|
+
resolvedConfigPath
|
|
8787
|
+
);
|
|
8788
|
+
mkdirSync4(path9.dirname(localConfigPath), { recursive: true });
|
|
8789
|
+
writeFileSync4(localConfigPath, JSON.stringify(sourceConfig, null, 2), "utf-8");
|
|
8790
|
+
return localConfigPath;
|
|
8791
|
+
}
|
|
7080
8792
|
function loadParsedConfig(projectRoot, configPath) {
|
|
7081
8793
|
const raw = loadRawConfig(projectRoot, configPath);
|
|
7082
8794
|
return parseConfig(raw);
|
|
@@ -7107,94 +8819,99 @@ async function runEvaluation(options) {
|
|
|
7107
8819
|
const againstPath = options.againstPath ? toAbsolute(options.projectRoot, options.againstPath) : void 0;
|
|
7108
8820
|
const budgetPath = options.budgetPath ? toAbsolute(options.projectRoot, options.budgetPath) : void 0;
|
|
7109
8821
|
const dataset = loadGoldenDataset(datasetPath);
|
|
7110
|
-
const
|
|
8822
|
+
const resolvedEvalConfigPath = options.reindex ? ensureLocalEvalProjectConfig(options.projectRoot, options.configPath) : options.configPath;
|
|
8823
|
+
const parsedConfig = loadParsedConfig(options.projectRoot, resolvedEvalConfigPath);
|
|
7111
8824
|
const effectiveConfig = resolveSearchConfig(parsedConfig, options.searchOverrides);
|
|
7112
8825
|
if (options.reindex) {
|
|
7113
8826
|
clearIndexRoot(options.projectRoot, effectiveConfig.scope);
|
|
7114
8827
|
}
|
|
7115
8828
|
const indexer = new Indexer(options.projectRoot, effectiveConfig);
|
|
7116
|
-
|
|
7117
|
-
|
|
7118
|
-
|
|
7119
|
-
|
|
7120
|
-
|
|
7121
|
-
`Query '${query.id}' expects branch '${query.expected.branch}', but current branch is '${indexer.getCurrentBranch()}'. Switch branch before running this dataset.`
|
|
7122
|
-
);
|
|
7123
|
-
}
|
|
7124
|
-
const start = performance3.now();
|
|
7125
|
-
const result = await indexer.search(query.query, 10, {
|
|
7126
|
-
metadataOnly: true,
|
|
7127
|
-
filterByBranch: query.expected.branch ? true : false
|
|
7128
|
-
});
|
|
7129
|
-
const elapsed = performance3.now() - start;
|
|
7130
|
-
const materialized = result.map((item) => ({
|
|
7131
|
-
filePath: item.filePath,
|
|
7132
|
-
startLine: item.startLine,
|
|
7133
|
-
endLine: item.endLine,
|
|
7134
|
-
score: item.score,
|
|
7135
|
-
chunkType: item.chunkType,
|
|
7136
|
-
name: item.name
|
|
7137
|
-
}));
|
|
7138
|
-
perQuery.push(buildPerQueryResult(query, materialized, elapsed, 10));
|
|
7139
|
-
}
|
|
7140
|
-
const logger = indexer.getLogger();
|
|
7141
|
-
const metricSnapshot = logger.getMetrics();
|
|
7142
|
-
const costPer1MTokensUsd = effectiveConfig.embeddingProvider === "custom" || effectiveConfig.embeddingProvider === "auto" ? 0 : getDefaultModelForProvider(effectiveConfig.embeddingProvider).costPer1MTokens;
|
|
7143
|
-
const summary = {
|
|
7144
|
-
generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
7145
|
-
projectRoot: options.projectRoot,
|
|
7146
|
-
datasetPath,
|
|
7147
|
-
datasetName: dataset.name,
|
|
7148
|
-
datasetVersion: dataset.version,
|
|
7149
|
-
queryCount: dataset.queries.length,
|
|
7150
|
-
topK: 10,
|
|
7151
|
-
searchConfig: {
|
|
7152
|
-
fusionStrategy: effectiveConfig.search.fusionStrategy,
|
|
7153
|
-
hybridWeight: effectiveConfig.search.hybridWeight,
|
|
7154
|
-
rrfK: effectiveConfig.search.rrfK,
|
|
7155
|
-
rerankTopN: effectiveConfig.search.rerankTopN
|
|
7156
|
-
},
|
|
7157
|
-
metrics: computeEvalMetrics(
|
|
7158
|
-
dataset.queries,
|
|
7159
|
-
perQuery,
|
|
7160
|
-
metricSnapshot.embeddingApiCalls,
|
|
7161
|
-
metricSnapshot.embeddingTokensUsed,
|
|
7162
|
-
costPer1MTokensUsd
|
|
7163
|
-
)
|
|
7164
|
-
};
|
|
7165
|
-
const outputDir = createRunDirectory(toAbsolute(options.projectRoot, options.outputRoot));
|
|
7166
|
-
const perQueryArtifact = buildPerQueryArtifact(perQuery);
|
|
7167
|
-
writeJson(path7.join(outputDir, "summary.json"), summary);
|
|
7168
|
-
writeJson(path7.join(outputDir, "per-query.json"), perQueryArtifact);
|
|
7169
|
-
let comparison;
|
|
7170
|
-
if (againstPath) {
|
|
7171
|
-
const baseline = loadSummary(againstPath);
|
|
7172
|
-
comparison = compareSummaries(summary, baseline, againstPath);
|
|
7173
|
-
writeJson(path7.join(outputDir, "compare.json"), comparison);
|
|
7174
|
-
}
|
|
7175
|
-
let gate;
|
|
7176
|
-
if (options.ciMode) {
|
|
7177
|
-
if (!budgetPath) {
|
|
7178
|
-
throw new Error("CI mode requires --budget path");
|
|
7179
|
-
}
|
|
7180
|
-
const budget = loadBudget(budgetPath);
|
|
7181
|
-
if (!comparison && budget.baselinePath) {
|
|
7182
|
-
const resolvedBaseline = toAbsolute(options.projectRoot, budget.baselinePath);
|
|
7183
|
-
if (existsSync5(resolvedBaseline)) {
|
|
7184
|
-
const baselineSummary = loadSummary(resolvedBaseline);
|
|
7185
|
-
comparison = compareSummaries(summary, baselineSummary, resolvedBaseline);
|
|
7186
|
-
writeJson(path7.join(outputDir, "compare.json"), comparison);
|
|
7187
|
-
} else if (budget.failOnMissingBaseline) {
|
|
8829
|
+
try {
|
|
8830
|
+
await indexer.index();
|
|
8831
|
+
const perQuery = [];
|
|
8832
|
+
for (const query of dataset.queries) {
|
|
8833
|
+
if (query.expected.branch && query.expected.branch !== indexer.getCurrentBranch()) {
|
|
7188
8834
|
throw new Error(
|
|
7189
|
-
`
|
|
8835
|
+
`Query '${query.id}' expects branch '${query.expected.branch}', but current branch is '${indexer.getCurrentBranch()}'. Switch branch before running this dataset.`
|
|
7190
8836
|
);
|
|
7191
8837
|
}
|
|
8838
|
+
const start = performance3.now();
|
|
8839
|
+
const result = await indexer.search(query.query, 10, {
|
|
8840
|
+
metadataOnly: true,
|
|
8841
|
+
filterByBranch: query.expected.branch ? true : false
|
|
8842
|
+
});
|
|
8843
|
+
const elapsed = performance3.now() - start;
|
|
8844
|
+
const materialized = result.map((item) => ({
|
|
8845
|
+
filePath: item.filePath,
|
|
8846
|
+
startLine: item.startLine,
|
|
8847
|
+
endLine: item.endLine,
|
|
8848
|
+
score: item.score,
|
|
8849
|
+
chunkType: item.chunkType,
|
|
8850
|
+
name: item.name
|
|
8851
|
+
}));
|
|
8852
|
+
perQuery.push(buildPerQueryResult(query, materialized, elapsed, 10));
|
|
8853
|
+
}
|
|
8854
|
+
const logger = indexer.getLogger();
|
|
8855
|
+
const metricSnapshot = logger.getMetrics();
|
|
8856
|
+
const costPer1MTokensUsd = effectiveConfig.embeddingProvider === "custom" || effectiveConfig.embeddingProvider === "auto" ? 0 : getDefaultModelForProvider(effectiveConfig.embeddingProvider).costPer1MTokens;
|
|
8857
|
+
const summary = {
|
|
8858
|
+
generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
8859
|
+
projectRoot: options.projectRoot,
|
|
8860
|
+
datasetPath,
|
|
8861
|
+
datasetName: dataset.name,
|
|
8862
|
+
datasetVersion: dataset.version,
|
|
8863
|
+
queryCount: dataset.queries.length,
|
|
8864
|
+
topK: 10,
|
|
8865
|
+
searchConfig: {
|
|
8866
|
+
fusionStrategy: effectiveConfig.search.fusionStrategy,
|
|
8867
|
+
hybridWeight: effectiveConfig.search.hybridWeight,
|
|
8868
|
+
rrfK: effectiveConfig.search.rrfK,
|
|
8869
|
+
rerankTopN: effectiveConfig.search.rerankTopN
|
|
8870
|
+
},
|
|
8871
|
+
metrics: computeEvalMetrics(
|
|
8872
|
+
dataset.queries,
|
|
8873
|
+
perQuery,
|
|
8874
|
+
metricSnapshot.embeddingApiCalls,
|
|
8875
|
+
metricSnapshot.embeddingTokensUsed,
|
|
8876
|
+
costPer1MTokensUsd
|
|
8877
|
+
)
|
|
8878
|
+
};
|
|
8879
|
+
const outputDir = createRunDirectory(toAbsolute(options.projectRoot, options.outputRoot));
|
|
8880
|
+
const perQueryArtifact = buildPerQueryArtifact(perQuery);
|
|
8881
|
+
writeJson(path9.join(outputDir, "summary.json"), summary);
|
|
8882
|
+
writeJson(path9.join(outputDir, "per-query.json"), perQueryArtifact);
|
|
8883
|
+
let comparison;
|
|
8884
|
+
if (againstPath) {
|
|
8885
|
+
const baseline = loadSummary(againstPath);
|
|
8886
|
+
comparison = compareSummaries(summary, baseline, againstPath);
|
|
8887
|
+
writeJson(path9.join(outputDir, "compare.json"), comparison);
|
|
8888
|
+
}
|
|
8889
|
+
let gate;
|
|
8890
|
+
if (options.ciMode) {
|
|
8891
|
+
if (!budgetPath) {
|
|
8892
|
+
throw new Error("CI mode requires --budget path");
|
|
8893
|
+
}
|
|
8894
|
+
const budget = loadBudget(budgetPath);
|
|
8895
|
+
if (!comparison && budget.baselinePath) {
|
|
8896
|
+
const resolvedBaseline = toAbsolute(options.projectRoot, budget.baselinePath);
|
|
8897
|
+
if (existsSync7(resolvedBaseline)) {
|
|
8898
|
+
const baselineSummary = loadSummary(resolvedBaseline);
|
|
8899
|
+
comparison = compareSummaries(summary, baselineSummary, resolvedBaseline);
|
|
8900
|
+
writeJson(path9.join(outputDir, "compare.json"), comparison);
|
|
8901
|
+
} else if (budget.failOnMissingBaseline) {
|
|
8902
|
+
throw new Error(
|
|
8903
|
+
`Budget baseline is missing: ${resolvedBaseline}. Set failOnMissingBaseline=false to allow CI run without baseline.`
|
|
8904
|
+
);
|
|
8905
|
+
}
|
|
8906
|
+
}
|
|
8907
|
+
gate = evaluateBudgetGate(budget, summary, comparison);
|
|
7192
8908
|
}
|
|
7193
|
-
|
|
8909
|
+
const markdown = createSummaryMarkdown(summary, comparison, gate);
|
|
8910
|
+
writeText(path9.join(outputDir, "summary.md"), markdown);
|
|
8911
|
+
return { outputDir, summary, perQuery, comparison, gate };
|
|
8912
|
+
} finally {
|
|
8913
|
+
await indexer.close();
|
|
7194
8914
|
}
|
|
7195
|
-
const markdown = createSummaryMarkdown(summary, comparison, gate);
|
|
7196
|
-
writeText(path7.join(outputDir, "summary.md"), markdown);
|
|
7197
|
-
return { outputDir, summary, perQuery, comparison, gate };
|
|
7198
8915
|
}
|
|
7199
8916
|
async function runSweep(options, sweep) {
|
|
7200
8917
|
const fusionValues = sweep.fusionStrategy && sweep.fusionStrategy.length > 0 ? [...sweep.fusionStrategy] : [void 0];
|
|
@@ -7248,15 +8965,15 @@ async function runSweep(options, sweep) {
|
|
|
7248
8965
|
bestByMrrAt10,
|
|
7249
8966
|
bestByP95Latency
|
|
7250
8967
|
};
|
|
7251
|
-
writeJson(
|
|
8968
|
+
writeJson(path9.join(outputDir, "compare.json"), aggregate);
|
|
7252
8969
|
const md = createSummaryMarkdown(
|
|
7253
8970
|
bestByHitAt5?.summary ?? runs[0].summary,
|
|
7254
8971
|
bestByHitAt5?.comparison,
|
|
7255
8972
|
void 0,
|
|
7256
8973
|
aggregate
|
|
7257
8974
|
);
|
|
7258
|
-
writeText(
|
|
7259
|
-
writeJson(
|
|
8975
|
+
writeText(path9.join(outputDir, "summary.md"), md);
|
|
8976
|
+
writeJson(path9.join(outputDir, "summary.json"), bestByHitAt5?.summary ?? runs[0].summary);
|
|
7260
8977
|
return { outputDir, aggregate };
|
|
7261
8978
|
}
|
|
7262
8979
|
|
|
@@ -7332,12 +9049,12 @@ function parseEvalArgs(argv, cwd) {
|
|
|
7332
9049
|
const arg = argv[i];
|
|
7333
9050
|
const next = argv[i + 1];
|
|
7334
9051
|
if (arg === "--project" && next) {
|
|
7335
|
-
parsed.projectRoot =
|
|
9052
|
+
parsed.projectRoot = path10.resolve(cwd, next);
|
|
7336
9053
|
i += 1;
|
|
7337
9054
|
continue;
|
|
7338
9055
|
}
|
|
7339
9056
|
if (arg === "--config" && next) {
|
|
7340
|
-
parsed.configPath =
|
|
9057
|
+
parsed.configPath = path10.resolve(cwd, next);
|
|
7341
9058
|
i += 1;
|
|
7342
9059
|
continue;
|
|
7343
9060
|
}
|
|
@@ -7531,22 +9248,22 @@ async function handleEvalCommand(args, cwd) {
|
|
|
7531
9248
|
if (!parsed.againstPath.endsWith(".json")) {
|
|
7532
9249
|
throw new Error("eval diff --against must point to a summary JSON file");
|
|
7533
9250
|
}
|
|
7534
|
-
const currentSummary = loadSummary(
|
|
9251
|
+
const currentSummary = loadSummary(path10.resolve(parsed.projectRoot, currentPath), {
|
|
7535
9252
|
allowLegacyDiversityMetrics: true
|
|
7536
9253
|
});
|
|
7537
|
-
const baselineSummary = loadSummary(
|
|
9254
|
+
const baselineSummary = loadSummary(path10.resolve(parsed.projectRoot, parsed.againstPath), {
|
|
7538
9255
|
allowLegacyDiversityMetrics: true
|
|
7539
9256
|
});
|
|
7540
9257
|
const comparison = compareSummaries(
|
|
7541
9258
|
currentSummary,
|
|
7542
9259
|
baselineSummary,
|
|
7543
|
-
|
|
9260
|
+
path10.resolve(parsed.projectRoot, parsed.againstPath)
|
|
7544
9261
|
);
|
|
7545
|
-
const outputDir = createRunDirectory(
|
|
9262
|
+
const outputDir = createRunDirectory(path10.resolve(parsed.projectRoot, parsed.outputRoot));
|
|
7546
9263
|
const summaryMd = createSummaryMarkdown(currentSummary, comparison);
|
|
7547
|
-
writeJson(
|
|
7548
|
-
writeText(
|
|
7549
|
-
writeJson(
|
|
9264
|
+
writeJson(path10.join(outputDir, "compare.json"), comparison);
|
|
9265
|
+
writeText(path10.join(outputDir, "summary.md"), summaryMd);
|
|
9266
|
+
writeJson(path10.join(outputDir, "summary.json"), currentSummary);
|
|
7550
9267
|
console.log(`Eval diff complete. Artifacts: ${outputDir}`);
|
|
7551
9268
|
return 0;
|
|
7552
9269
|
}
|
|
@@ -7556,6 +9273,8 @@ async function handleEvalCommand(args, cwd) {
|
|
|
7556
9273
|
// src/mcp-server.ts
|
|
7557
9274
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
7558
9275
|
import { z } from "zod";
|
|
9276
|
+
import * as path11 from "path";
|
|
9277
|
+
import { existsSync as existsSync8 } from "fs";
|
|
7559
9278
|
|
|
7560
9279
|
// src/tools/utils.ts
|
|
7561
9280
|
var MAX_CONTENT_LINES = 30;
|
|
@@ -7565,36 +9284,24 @@ function truncateContent(content) {
|
|
|
7565
9284
|
return lines.slice(0, MAX_CONTENT_LINES).join("\n") + `
|
|
7566
9285
|
// ... (${lines.length - MAX_CONTENT_LINES} more lines)`;
|
|
7567
9286
|
}
|
|
7568
|
-
function formatDefinitionLookup(results, query) {
|
|
7569
|
-
if (results.length === 0) {
|
|
7570
|
-
return `No definition found for "${query}". Try codebase_search for broader discovery, or verify the symbol name.`;
|
|
7571
|
-
}
|
|
7572
|
-
const formatted = results.map((r, idx) => {
|
|
7573
|
-
const header = r.name ? `[${idx + 1}] ${r.chunkType} "${r.name}" in ${r.filePath}:${r.startLine}-${r.endLine}` : `[${idx + 1}] ${r.chunkType} in ${r.filePath}:${r.startLine}-${r.endLine}`;
|
|
7574
|
-
return `${header} (score: ${r.score.toFixed(2)})
|
|
7575
|
-
\`\`\`
|
|
7576
|
-
${truncateContent(r.content)}
|
|
7577
|
-
\`\`\``;
|
|
7578
|
-
});
|
|
7579
|
-
return formatted.join("\n\n");
|
|
7580
|
-
}
|
|
7581
|
-
|
|
7582
|
-
// src/mcp-server.ts
|
|
7583
|
-
var MAX_CONTENT_LINES2 = 30;
|
|
7584
|
-
function truncateContent2(content) {
|
|
7585
|
-
const lines = content.split("\n");
|
|
7586
|
-
if (lines.length <= MAX_CONTENT_LINES2) return content;
|
|
7587
|
-
return lines.slice(0, MAX_CONTENT_LINES2).join("\n") + `
|
|
7588
|
-
// ... (${lines.length - MAX_CONTENT_LINES2} more lines)`;
|
|
7589
|
-
}
|
|
7590
9287
|
function formatIndexStats(stats, verbose = false) {
|
|
9288
|
+
if (stats.resetCorruptedIndex) {
|
|
9289
|
+
return stats.warning ?? "Detected a corrupted local index and reset it during indexing. Run index_codebase again to rebuild search data.";
|
|
9290
|
+
}
|
|
7591
9291
|
const lines = [];
|
|
9292
|
+
if (stats.failedChunks > 0) {
|
|
9293
|
+
lines.push(`INDEXING WARNING: ${stats.failedChunks} chunks failed to embed.`);
|
|
9294
|
+
if (stats.failedBatchesPath) {
|
|
9295
|
+
lines.push(`Inspect failed batches at: ${stats.failedBatchesPath}`);
|
|
9296
|
+
}
|
|
9297
|
+
lines.push("");
|
|
9298
|
+
}
|
|
7592
9299
|
if (stats.indexedChunks === 0 && stats.removedChunks === 0) {
|
|
7593
|
-
lines.push(
|
|
9300
|
+
lines.push(`${stats.totalFiles} files processed, ${stats.existingChunks} code chunks already up to date.`);
|
|
7594
9301
|
} else if (stats.indexedChunks === 0) {
|
|
7595
|
-
lines.push(
|
|
9302
|
+
lines.push(`${stats.totalFiles} files, removed ${stats.removedChunks} stale chunks, ${stats.existingChunks} chunks remain.`);
|
|
7596
9303
|
} else {
|
|
7597
|
-
let main2 =
|
|
9304
|
+
let main2 = `${stats.totalFiles} files processed, ${stats.indexedChunks} new chunks embedded.`;
|
|
7598
9305
|
if (stats.existingChunks > 0) {
|
|
7599
9306
|
main2 += ` ${stats.existingChunks} unchanged chunks skipped.`;
|
|
7600
9307
|
}
|
|
@@ -7633,21 +9340,103 @@ function formatIndexStats(stats, verbose = false) {
|
|
|
7633
9340
|
}
|
|
7634
9341
|
function formatStatus(status) {
|
|
7635
9342
|
if (!status.indexed) {
|
|
9343
|
+
if (status.warning) {
|
|
9344
|
+
return status.warning;
|
|
9345
|
+
}
|
|
9346
|
+
if (status.failedBatchesCount > 0) {
|
|
9347
|
+
const lines2 = [
|
|
9348
|
+
"Codebase is not indexed. The last indexing run left failed embedding batches.",
|
|
9349
|
+
"Fix the provider/model configuration, then rerun index_codebase normally to retry the saved failed batches. Use force=true only for a full rebuild or compatibility reset."
|
|
9350
|
+
];
|
|
9351
|
+
if (status.failedBatchesPath) {
|
|
9352
|
+
lines2.push(`Failed batches: ${status.failedBatchesPath}`);
|
|
9353
|
+
}
|
|
9354
|
+
return lines2.join("\n");
|
|
9355
|
+
}
|
|
7636
9356
|
return "Codebase is not indexed. Run index_codebase to create an index.";
|
|
7637
9357
|
}
|
|
7638
9358
|
const lines = [
|
|
7639
|
-
`
|
|
7640
|
-
`
|
|
7641
|
-
`
|
|
7642
|
-
`
|
|
7643
|
-
` Location: ${status.indexPath}`
|
|
9359
|
+
`Indexed chunks: ${status.vectorCount.toLocaleString()}`,
|
|
9360
|
+
`Provider: ${status.provider}`,
|
|
9361
|
+
`Model: ${status.model}`,
|
|
9362
|
+
`Location: ${status.indexPath}`
|
|
7644
9363
|
];
|
|
7645
9364
|
if (status.currentBranch !== "default") {
|
|
7646
|
-
lines.push(`
|
|
7647
|
-
lines.push(`
|
|
9365
|
+
lines.push(`Current branch: ${status.currentBranch}`);
|
|
9366
|
+
lines.push(`Base branch: ${status.baseBranch}`);
|
|
9367
|
+
}
|
|
9368
|
+
if (status.failedBatchesCount > 0) {
|
|
9369
|
+
lines.push("");
|
|
9370
|
+
lines.push(`INDEXING WARNING: ${status.failedBatchesCount} failed embedding batch${status.failedBatchesCount === 1 ? " remains" : "es remain"}.`);
|
|
9371
|
+
if (status.failedBatchesPath) {
|
|
9372
|
+
lines.push(`Failed batches: ${status.failedBatchesPath}`);
|
|
9373
|
+
}
|
|
9374
|
+
}
|
|
9375
|
+
if (status.compatibility && !status.compatibility.compatible) {
|
|
9376
|
+
lines.push("");
|
|
9377
|
+
lines.push(`COMPATIBILITY WARNING: ${status.compatibility.reason}`);
|
|
9378
|
+
if (status.compatibility.storedMetadata) {
|
|
9379
|
+
const stored = status.compatibility.storedMetadata;
|
|
9380
|
+
lines.push(`Index was built with: ${stored.embeddingProvider}/${stored.embeddingModel} (${stored.embeddingDimensions}D)`);
|
|
9381
|
+
lines.push(`Current config: ${status.provider}/${status.model}`);
|
|
9382
|
+
}
|
|
9383
|
+
} else if (!status.compatibility) {
|
|
9384
|
+
lines.push(`Compatibility: No compatibility information found. Maybe the index is not initialized yet, try running index_codebase.`);
|
|
9385
|
+
} else {
|
|
9386
|
+
lines.push(`Compatibility: Index is compatible with the current provider and model.`);
|
|
9387
|
+
}
|
|
9388
|
+
return lines.join("\n");
|
|
9389
|
+
}
|
|
9390
|
+
function formatHealthCheck(result) {
|
|
9391
|
+
if (result.resetCorruptedIndex) {
|
|
9392
|
+
return result.warning ?? "Detected a corrupted local index and reset it. Run index_codebase to rebuild search data.";
|
|
9393
|
+
}
|
|
9394
|
+
if (result.removed === 0 && result.gcOrphanEmbeddings === 0 && result.gcOrphanChunks === 0 && result.gcOrphanSymbols === 0 && result.gcOrphanCallEdges === 0) {
|
|
9395
|
+
return "Index is healthy. No stale entries found.";
|
|
9396
|
+
}
|
|
9397
|
+
const lines = [];
|
|
9398
|
+
if (result.removed > 0) {
|
|
9399
|
+
lines.push(`Removed stale entries: ${result.removed}`);
|
|
9400
|
+
}
|
|
9401
|
+
if (result.gcOrphanEmbeddings > 0) {
|
|
9402
|
+
lines.push(`Garbage collected orphan embeddings: ${result.gcOrphanEmbeddings}`);
|
|
9403
|
+
}
|
|
9404
|
+
if (result.gcOrphanChunks > 0) {
|
|
9405
|
+
lines.push(`Garbage collected orphan chunks: ${result.gcOrphanChunks}`);
|
|
9406
|
+
}
|
|
9407
|
+
if (result.gcOrphanSymbols > 0) {
|
|
9408
|
+
lines.push(`Garbage collected orphan symbols: ${result.gcOrphanSymbols}`);
|
|
9409
|
+
}
|
|
9410
|
+
if (result.gcOrphanCallEdges > 0) {
|
|
9411
|
+
lines.push(`Garbage collected orphan call edges: ${result.gcOrphanCallEdges}`);
|
|
9412
|
+
}
|
|
9413
|
+
if (result.filePaths.length > 0) {
|
|
9414
|
+
lines.push(`Cleaned paths: ${result.filePaths.join(", ")}`);
|
|
7648
9415
|
}
|
|
7649
9416
|
return lines.join("\n");
|
|
7650
9417
|
}
|
|
9418
|
+
function formatDefinitionLookup(results, query) {
|
|
9419
|
+
if (results.length === 0) {
|
|
9420
|
+
return `No definition found for "${query}". Try codebase_search for broader discovery, or verify the symbol name.`;
|
|
9421
|
+
}
|
|
9422
|
+
const formatted = results.map((r, idx) => {
|
|
9423
|
+
const header = r.name ? `[${idx + 1}] ${r.chunkType} "${r.name}" in ${r.filePath}:${r.startLine}-${r.endLine}` : `[${idx + 1}] ${r.chunkType} in ${r.filePath}:${r.startLine}-${r.endLine}`;
|
|
9424
|
+
return `${header} (score: ${r.score.toFixed(2)})
|
|
9425
|
+
\`\`\`
|
|
9426
|
+
${truncateContent(r.content)}
|
|
9427
|
+
\`\`\``;
|
|
9428
|
+
});
|
|
9429
|
+
return formatted.join("\n\n");
|
|
9430
|
+
}
|
|
9431
|
+
|
|
9432
|
+
// src/mcp-server.ts
|
|
9433
|
+
var MAX_CONTENT_LINES2 = 30;
|
|
9434
|
+
function truncateContent2(content) {
|
|
9435
|
+
const lines = content.split("\n");
|
|
9436
|
+
if (lines.length <= MAX_CONTENT_LINES2) return content;
|
|
9437
|
+
return lines.slice(0, MAX_CONTENT_LINES2).join("\n") + `
|
|
9438
|
+
// ... (${lines.length - MAX_CONTENT_LINES2} more lines)`;
|
|
9439
|
+
}
|
|
7651
9440
|
var CHUNK_TYPE_ENUM = [
|
|
7652
9441
|
"function",
|
|
7653
9442
|
"class",
|
|
@@ -7666,8 +9455,25 @@ function createMcpServer(projectRoot, config) {
|
|
|
7666
9455
|
name: "opencode-codebase-index",
|
|
7667
9456
|
version: "0.5.1"
|
|
7668
9457
|
});
|
|
7669
|
-
const
|
|
9458
|
+
const runtimeConfig = config;
|
|
9459
|
+
let indexer = new Indexer(projectRoot, runtimeConfig);
|
|
7670
9460
|
let initialized = false;
|
|
9461
|
+
function refreshIndexerFromConfig() {
|
|
9462
|
+
indexer = new Indexer(projectRoot, runtimeConfig);
|
|
9463
|
+
initialized = false;
|
|
9464
|
+
}
|
|
9465
|
+
function shouldForceLocalizeProjectIndex() {
|
|
9466
|
+
if (runtimeConfig.scope !== "project") {
|
|
9467
|
+
return false;
|
|
9468
|
+
}
|
|
9469
|
+
const localIndexPath = path11.join(projectRoot, ".opencode", "index");
|
|
9470
|
+
const mainRepoRoot = resolveWorktreeMainRepoRoot(projectRoot);
|
|
9471
|
+
if (!mainRepoRoot) {
|
|
9472
|
+
return false;
|
|
9473
|
+
}
|
|
9474
|
+
const inheritedIndexPath = path11.join(mainRepoRoot, ".opencode", "index");
|
|
9475
|
+
return !existsSync8(localIndexPath) && existsSync8(inheritedIndexPath);
|
|
9476
|
+
}
|
|
7671
9477
|
async function ensureInitialized() {
|
|
7672
9478
|
if (!initialized) {
|
|
7673
9479
|
await indexer.initialize();
|
|
@@ -7750,13 +9556,22 @@ Use Read tool to examine specific files.` }] };
|
|
|
7750
9556
|
verbose: z.boolean().optional().default(false).describe("Show detailed info about skipped files and parsing failures")
|
|
7751
9557
|
},
|
|
7752
9558
|
async (args) => {
|
|
7753
|
-
await ensureInitialized();
|
|
7754
9559
|
if (args.estimateOnly) {
|
|
9560
|
+
await ensureInitialized();
|
|
7755
9561
|
const estimate = await indexer.estimateCost();
|
|
7756
9562
|
return { content: [{ type: "text", text: formatCostEstimate(estimate) }] };
|
|
7757
9563
|
}
|
|
7758
9564
|
if (args.force) {
|
|
9565
|
+
if (shouldForceLocalizeProjectIndex()) {
|
|
9566
|
+
materializeLocalProjectConfig(projectRoot, loadProjectConfigLayer(projectRoot));
|
|
9567
|
+
refreshIndexerFromConfig();
|
|
9568
|
+
}
|
|
9569
|
+
await ensureInitialized();
|
|
7759
9570
|
await indexer.clearIndex();
|
|
9571
|
+
refreshIndexerFromConfig();
|
|
9572
|
+
await ensureInitialized();
|
|
9573
|
+
} else {
|
|
9574
|
+
await ensureInitialized();
|
|
7760
9575
|
}
|
|
7761
9576
|
const stats = await indexer.index();
|
|
7762
9577
|
return { content: [{ type: "text", text: formatIndexStats(stats, args.verbose ?? false) }] };
|
|
@@ -7779,29 +9594,7 @@ Use Read tool to examine specific files.` }] };
|
|
|
7779
9594
|
async () => {
|
|
7780
9595
|
await ensureInitialized();
|
|
7781
9596
|
const result = await indexer.healthCheck();
|
|
7782
|
-
|
|
7783
|
-
return { content: [{ type: "text", text: "Index is healthy. No stale entries found." }] };
|
|
7784
|
-
}
|
|
7785
|
-
const lines = [`Health check complete:`];
|
|
7786
|
-
if (result.removed > 0) {
|
|
7787
|
-
lines.push(` Removed stale entries: ${result.removed}`);
|
|
7788
|
-
}
|
|
7789
|
-
if (result.gcOrphanEmbeddings > 0) {
|
|
7790
|
-
lines.push(` Garbage collected orphan embeddings: ${result.gcOrphanEmbeddings}`);
|
|
7791
|
-
}
|
|
7792
|
-
if (result.gcOrphanChunks > 0) {
|
|
7793
|
-
lines.push(` Garbage collected orphan chunks: ${result.gcOrphanChunks}`);
|
|
7794
|
-
}
|
|
7795
|
-
if (result.gcOrphanSymbols > 0) {
|
|
7796
|
-
lines.push(` Garbage collected orphan symbols: ${result.gcOrphanSymbols}`);
|
|
7797
|
-
}
|
|
7798
|
-
if (result.gcOrphanCallEdges > 0) {
|
|
7799
|
-
lines.push(` Garbage collected orphan call edges: ${result.gcOrphanCallEdges}`);
|
|
7800
|
-
}
|
|
7801
|
-
if (result.filePaths.length > 0) {
|
|
7802
|
-
lines.push(` Cleaned paths: ${result.filePaths.join(", ")}`);
|
|
7803
|
-
}
|
|
7804
|
-
return { content: [{ type: "text", text: lines.join("\n") }] };
|
|
9597
|
+
return { content: [{ type: "text", text: formatHealthCheck(result) }] };
|
|
7805
9598
|
}
|
|
7806
9599
|
);
|
|
7807
9600
|
server.tool(
|
|
@@ -8030,115 +9823,15 @@ Use the implementation_lookup tool to find where this symbol is defined. This pr
|
|
|
8030
9823
|
return server;
|
|
8031
9824
|
}
|
|
8032
9825
|
|
|
8033
|
-
// src/config/merger.ts
|
|
8034
|
-
import { existsSync as existsSync6, readFileSync as readFileSync8 } from "fs";
|
|
8035
|
-
import * as path9 from "path";
|
|
8036
|
-
import * as os4 from "os";
|
|
8037
|
-
function loadJsonFile(filePath) {
|
|
8038
|
-
try {
|
|
8039
|
-
if (existsSync6(filePath)) {
|
|
8040
|
-
const content = readFileSync8(filePath, "utf-8");
|
|
8041
|
-
return JSON.parse(content);
|
|
8042
|
-
}
|
|
8043
|
-
} catch {
|
|
8044
|
-
}
|
|
8045
|
-
return null;
|
|
8046
|
-
}
|
|
8047
|
-
function loadMergedConfig(projectRoot) {
|
|
8048
|
-
const globalConfigPath = path9.join(os4.homedir(), ".config", "opencode", "codebase-index.json");
|
|
8049
|
-
const globalConfig = loadJsonFile(globalConfigPath);
|
|
8050
|
-
const projectConfigPath = path9.join(projectRoot, ".opencode", "codebase-index.json");
|
|
8051
|
-
const projectConfig = loadJsonFile(projectConfigPath);
|
|
8052
|
-
if (!globalConfig && !projectConfig) {
|
|
8053
|
-
return {};
|
|
8054
|
-
}
|
|
8055
|
-
if (!projectConfig && globalConfig) {
|
|
8056
|
-
return globalConfig;
|
|
8057
|
-
}
|
|
8058
|
-
if (!globalConfig && projectConfig) {
|
|
8059
|
-
return projectConfig;
|
|
8060
|
-
}
|
|
8061
|
-
const merged = { ...globalConfig };
|
|
8062
|
-
if (projectConfig && "embeddingProvider" in projectConfig) {
|
|
8063
|
-
merged.embeddingProvider = projectConfig.embeddingProvider;
|
|
8064
|
-
} else if (globalConfig && globalConfig.embeddingProvider) {
|
|
8065
|
-
merged.embeddingProvider = globalConfig.embeddingProvider;
|
|
8066
|
-
}
|
|
8067
|
-
if (projectConfig && "customProvider" in projectConfig) {
|
|
8068
|
-
merged.customProvider = projectConfig.customProvider;
|
|
8069
|
-
} else if (globalConfig && globalConfig.customProvider) {
|
|
8070
|
-
merged.customProvider = globalConfig.customProvider;
|
|
8071
|
-
}
|
|
8072
|
-
if (projectConfig && "embeddingModel" in projectConfig) {
|
|
8073
|
-
merged.embeddingModel = projectConfig.embeddingModel;
|
|
8074
|
-
} else if (globalConfig && globalConfig.embeddingModel) {
|
|
8075
|
-
merged.embeddingModel = globalConfig.embeddingModel;
|
|
8076
|
-
}
|
|
8077
|
-
if (projectConfig && "reranker" in projectConfig) {
|
|
8078
|
-
merged.reranker = projectConfig.reranker;
|
|
8079
|
-
} else if (globalConfig && globalConfig.reranker) {
|
|
8080
|
-
merged.reranker = globalConfig.reranker;
|
|
8081
|
-
}
|
|
8082
|
-
if (projectConfig && "include" in projectConfig) {
|
|
8083
|
-
merged.include = projectConfig.include;
|
|
8084
|
-
} else if (globalConfig && globalConfig.include) {
|
|
8085
|
-
merged.include = globalConfig.include;
|
|
8086
|
-
}
|
|
8087
|
-
if (projectConfig && "exclude" in projectConfig) {
|
|
8088
|
-
merged.exclude = projectConfig.exclude;
|
|
8089
|
-
} else if (globalConfig && globalConfig.exclude) {
|
|
8090
|
-
merged.exclude = globalConfig.exclude;
|
|
8091
|
-
}
|
|
8092
|
-
if (projectConfig && "indexing" in projectConfig) {
|
|
8093
|
-
merged.indexing = projectConfig.indexing;
|
|
8094
|
-
} else if (globalConfig && globalConfig.indexing) {
|
|
8095
|
-
merged.indexing = globalConfig.indexing;
|
|
8096
|
-
}
|
|
8097
|
-
if (projectConfig && "search" in projectConfig) {
|
|
8098
|
-
merged.search = projectConfig.search;
|
|
8099
|
-
} else if (globalConfig && globalConfig.search) {
|
|
8100
|
-
merged.search = globalConfig.search;
|
|
8101
|
-
}
|
|
8102
|
-
if (projectConfig && "debug" in projectConfig) {
|
|
8103
|
-
merged.debug = projectConfig.debug;
|
|
8104
|
-
} else if (globalConfig && globalConfig.debug) {
|
|
8105
|
-
merged.debug = globalConfig.debug;
|
|
8106
|
-
}
|
|
8107
|
-
if (projectConfig && "scope" in projectConfig) {
|
|
8108
|
-
merged.scope = projectConfig.scope;
|
|
8109
|
-
} else if (globalConfig && "scope" in globalConfig) {
|
|
8110
|
-
merged.scope = globalConfig.scope;
|
|
8111
|
-
}
|
|
8112
|
-
if (projectConfig) {
|
|
8113
|
-
for (const key of Object.keys(projectConfig)) {
|
|
8114
|
-
if (key === "embeddingProvider" || key === "customProvider" || key === "embeddingModel" || key === "reranker" || key === "include" || key === "exclude" || key === "indexing" || key === "search" || key === "debug" || key === "scope" || key === "knowledgeBases" || key === "additionalInclude") {
|
|
8115
|
-
continue;
|
|
8116
|
-
}
|
|
8117
|
-
merged[key] = projectConfig[key];
|
|
8118
|
-
}
|
|
8119
|
-
}
|
|
8120
|
-
const globalKbs = globalConfig && Array.isArray(globalConfig.knowledgeBases) ? globalConfig.knowledgeBases : [];
|
|
8121
|
-
const projectKbs = projectConfig && Array.isArray(projectConfig.knowledgeBases) ? projectConfig.knowledgeBases : [];
|
|
8122
|
-
const allKbs = [...globalKbs, ...projectKbs];
|
|
8123
|
-
const uniqueKbs = [...new Set(allKbs.map((p) => String(p).trim()))];
|
|
8124
|
-
merged.knowledgeBases = uniqueKbs;
|
|
8125
|
-
const globalAdditional = globalConfig && Array.isArray(globalConfig.additionalInclude) ? globalConfig.additionalInclude : [];
|
|
8126
|
-
const projectAdditional = projectConfig && Array.isArray(projectConfig.additionalInclude) ? projectConfig.additionalInclude : [];
|
|
8127
|
-
const allAdditional = [...globalAdditional, ...projectAdditional];
|
|
8128
|
-
const uniqueAdditional = [...new Set(allAdditional.map((p) => String(p).trim()))];
|
|
8129
|
-
merged.additionalInclude = uniqueAdditional;
|
|
8130
|
-
return merged;
|
|
8131
|
-
}
|
|
8132
|
-
|
|
8133
9826
|
// src/cli.ts
|
|
8134
9827
|
function parseArgs(argv) {
|
|
8135
9828
|
let project = process.cwd();
|
|
8136
9829
|
let config;
|
|
8137
9830
|
for (let i = 2; i < argv.length; i++) {
|
|
8138
9831
|
if (argv[i] === "--project" && argv[i + 1]) {
|
|
8139
|
-
project =
|
|
9832
|
+
project = path12.resolve(argv[++i]);
|
|
8140
9833
|
} else if (argv[i] === "--config" && argv[i + 1]) {
|
|
8141
|
-
config =
|
|
9834
|
+
config = path12.resolve(argv[++i]);
|
|
8142
9835
|
}
|
|
8143
9836
|
}
|
|
8144
9837
|
return { project, config };
|