@chiway/contextweaver 1.1.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,13 @@
1
1
  import {
2
+ ChunkContentLoader,
3
+ bootstrap,
2
4
  getGraphExpander,
3
5
  getIndexer,
4
- getVectorStore,
5
6
  scoreChunkTokenOverlap
6
- } from "./chunk-6QMYML5V.js";
7
+ } from "./chunk-AB24E3Z7.js";
8
+ import {
9
+ getVectorStore
10
+ } from "./chunk-ZOMGPIU6.js";
7
11
  import {
8
12
  initDb,
9
13
  isChunksFtsInitialized,
@@ -11,15 +15,15 @@ import {
11
15
  searchChunksFts,
12
16
  searchFilesFts,
13
17
  segmentQuery
14
- } from "./chunk-6Z4JEEVJ.js";
18
+ } from "./chunk-RGJSXUFS.js";
15
19
  import {
16
20
  isDebugEnabled,
17
21
  logger
18
- } from "./chunk-AMQQK4P7.js";
22
+ } from "./chunk-JVKVSTQ3.js";
19
23
  import {
20
24
  getEmbeddingConfig,
21
25
  getRerankerConfig
22
- } from "./chunk-RJURH22T.js";
26
+ } from "./chunk-SKBAE26T.js";
23
27
 
24
28
  // src/api/reranker.ts
25
29
  var RerankerClient = class {
@@ -350,6 +354,12 @@ var SearchService = class {
350
354
  this.indexer = await getIndexer(this.projectId, embeddingConfig.dimensions);
351
355
  this.vectorStore = await getVectorStore(this.projectId, embeddingConfig.dimensions);
352
356
  this.db = initDb(this.projectId);
357
+ try {
358
+ await bootstrap(this.db, this.vectorStore);
359
+ } catch (err) {
360
+ const error = err;
361
+ logger.warn({ error: error.message }, "bootstrap \u5931\u8D25\uFF0C\u7EE7\u7EED\u542F\u52A8");
362
+ }
353
363
  }
354
364
  // 公开接口
355
365
  /**
@@ -517,6 +527,20 @@ var SearchService = class {
517
527
  const allFilePaths = fileResults.map((r) => r.path);
518
528
  const chunksMap = await this.vectorStore?.getFilesChunks(allFilePaths);
519
529
  if (!chunksMap) return [];
530
+ const allChunkSlices = [];
531
+ for (const filePath of allFilePaths) {
532
+ const chunks = chunksMap.get(filePath);
533
+ if (!chunks) continue;
534
+ for (const c of chunks) {
535
+ allChunkSlices.push({
536
+ filePath: c.file_path,
537
+ start_index: c.start_index,
538
+ end_index: c.end_index
539
+ });
540
+ }
541
+ }
542
+ const contentLoader = new ChunkContentLoader(this.db);
543
+ const codeMap = contentLoader.loadMany(allChunkSlices);
520
544
  const allChunks = [];
521
545
  let totalChunks = 0;
522
546
  let skippedFiles = 0;
@@ -524,10 +548,19 @@ var SearchService = class {
524
548
  if (totalChunks >= this.config.lexTotalChunks) break;
525
549
  const chunks = chunksMap.get(filePath);
526
550
  if (!chunks || chunks.length === 0) continue;
527
- const scoredChunks = chunks.map((chunk) => ({
528
- chunk,
529
- overlapScore: scoreChunkTokenOverlap(chunk, queryTokens)
530
- }));
551
+ const scoredChunks = chunks.map((chunk) => {
552
+ const code = codeMap.get(
553
+ ChunkContentLoader.key({
554
+ filePath: chunk.file_path,
555
+ start_index: chunk.start_index,
556
+ end_index: chunk.end_index
557
+ })
558
+ ) ?? "";
559
+ return {
560
+ chunk,
561
+ overlapScore: scoreChunkTokenOverlap(chunk, code, queryTokens)
562
+ };
563
+ });
531
564
  const maxOverlap = Math.max(...scoredChunks.map((c) => c.overlapScore));
532
565
  if (maxOverlap === 0) {
533
566
  skippedFiles++;
@@ -647,12 +680,26 @@ var SearchService = class {
647
680
  return candidates;
648
681
  }
649
682
  const queryTokens = this.extractQueryTokens(query);
683
+ const loader = new ChunkContentLoader(this.db);
684
+ const codeMap = loader.loadMany(
685
+ candidates.map((c) => ({
686
+ filePath: c.filePath,
687
+ start_index: c.record.start_index,
688
+ end_index: c.record.end_index
689
+ }))
690
+ );
650
691
  const textExtractor = (chunk) => {
651
692
  const bc = this.truncateMiddle(chunk.record.breadcrumb, this.config.maxBreadcrumbChars);
693
+ const key = ChunkContentLoader.key({
694
+ filePath: chunk.filePath,
695
+ start_index: chunk.record.start_index,
696
+ end_index: chunk.record.end_index
697
+ });
698
+ const code = codeMap.get(key) ?? "";
652
699
  const budget = Math.max(0, this.config.maxRerankChars - bc.length - 1);
653
- const code = this.extractAroundHit(chunk.record.display_code, queryTokens, budget);
700
+ const trimmed = this.extractAroundHit(code, queryTokens, budget);
654
701
  return `${bc}
655
- ${code}`;
702
+ ${trimmed}`;
656
703
  };
657
704
  try {
658
705
  const reranked = await reranker.rerankWithData(query, candidates, textExtractor, {
@@ -883,4 +930,3 @@ ${code}`;
883
930
  export {
884
931
  SearchService
885
932
  };
886
- //# sourceMappingURL=SearchService-MYPOCM3B.js.map