@chiway/contextweaver 1.1.0 → 1.5.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.
Files changed (37) hide show
  1. package/README.md +138 -28
  2. package/dist/{SearchService-MYPOCM3B.js → SearchService-WVD6THR3.js} +170 -82
  3. package/dist/chunk-3BNHQV5W.js +373 -0
  4. package/dist/chunk-BFCIZ52F.js +102 -0
  5. package/dist/{chunk-NQR4CGQ6.js → chunk-GDVB6PJ4.js} +58 -10
  6. package/dist/{lock-DVY3KJSK.js → chunk-HHYPQA3X.js} +2 -3
  7. package/dist/chunk-ISVCQFB4.js +223 -0
  8. package/dist/chunk-IZ6IUHNN.js +77 -0
  9. package/dist/{chunk-AMQQK4P7.js → chunk-JVKVSTQ3.js} +1 -2
  10. package/dist/chunk-LB42CZEB.js +18 -0
  11. package/dist/{chunk-6Z4JEEVJ.js → chunk-PPLFJGO3.js} +303 -58
  12. package/dist/chunk-R6CNZXZ7.js +143 -0
  13. package/dist/{chunk-RJURH22T.js → chunk-SKBAE26T.js} +0 -1
  14. package/dist/chunk-TPM6YP43.js +38 -0
  15. package/dist/{chunk-7G5V7YT5.js → chunk-V3K4YVAR.js} +12 -120
  16. package/dist/chunk-VWBKZ6QL.js +115 -0
  17. package/dist/chunk-XFIM2T6S.js +57 -0
  18. package/dist/{chunk-6QMYML5V.js → chunk-XMZZZKG7.js} +361 -295
  19. package/dist/chunk-XTWNT7KP.js +156 -0
  20. package/dist/chunk-Y6H7C3NA.js +85 -0
  21. package/dist/codebaseRetrieval-DIS5RH2C.js +14 -0
  22. package/dist/{config-BWZ6CU3W.js → config-LCOJHTCF.js} +1 -2
  23. package/dist/db-GBCLP4GG.js +68 -0
  24. package/dist/findReferences-N7ML7TUP.js +16 -0
  25. package/dist/getSymbolDefinition-6KMY4H33.js +17 -0
  26. package/dist/index.js +271 -40
  27. package/dist/listFiles-4VT2TPJD.js +14 -0
  28. package/dist/loadConfig-XTVT2OWW.js +9 -0
  29. package/dist/lock-HNKQ6X5B.js +8 -0
  30. package/dist/scanner-QDFZJLP7.js +13 -0
  31. package/dist/server-UAI3U7AB.js +347 -0
  32. package/dist/stats-AGKUCJQI.js +12 -0
  33. package/dist/vectorStore-4ODCERRO.js +12 -0
  34. package/package.json +9 -23
  35. package/dist/codebaseRetrieval-NLAMGOA2.js +0 -12
  36. package/dist/scanner-RFG4YWYI.js +0 -11
  37. package/dist/server-27HI7WZO.js +0 -147
@@ -1,14 +1,15 @@
1
+ import {
2
+ checkEnvOrRespond,
3
+ ensureIndexed
4
+ } from "./chunk-VWBKZ6QL.js";
1
5
  import {
2
6
  generateProjectId
3
- } from "./chunk-6Z4JEEVJ.js";
7
+ } from "./chunk-PPLFJGO3.js";
4
8
  import {
5
9
  logger
6
- } from "./chunk-AMQQK4P7.js";
10
+ } from "./chunk-JVKVSTQ3.js";
7
11
 
8
12
  // src/mcp/tools/codebaseRetrieval.ts
9
- import fs from "fs";
10
- import os from "os";
11
- import path from "path";
12
13
  import { z } from "zod";
13
14
  var codebaseRetrievalSchema = z.object({
14
15
  repo_path: z.string().describe(
@@ -21,80 +22,6 @@ var codebaseRetrievalSchema = z.object({
21
22
  "HARD FILTERS. Precise identifiers to narrow down results. Only use symbols KNOWN to exist to avoid false negatives."
22
23
  )
23
24
  });
24
- var BASE_DIR = path.join(os.homedir(), ".contextweaver");
25
- var INDEX_LOCK_TIMEOUT_MS = 10 * 60 * 1e3;
26
- async function ensureDefaultEnvFile() {
27
- const configDir = BASE_DIR;
28
- const envFile = path.join(configDir, ".env");
29
- if (fs.existsSync(envFile)) {
30
- return;
31
- }
32
- if (!fs.existsSync(configDir)) {
33
- fs.mkdirSync(configDir, { recursive: true });
34
- logger.info({ configDir }, "\u521B\u5EFA\u914D\u7F6E\u76EE\u5F55");
35
- }
36
- const defaultEnvContent = `# ContextWeaver \u793A\u4F8B\u73AF\u5883\u53D8\u91CF\u914D\u7F6E\u6587\u4EF6
37
-
38
- # Embedding API \u914D\u7F6E\uFF08\u5FC5\u9700\uFF09
39
- EMBEDDINGS_API_KEY=your-api-key-here
40
- EMBEDDINGS_BASE_URL=https://api.siliconflow.cn/v1/embeddings
41
- EMBEDDINGS_MODEL=BAAI/bge-m3
42
- EMBEDDINGS_MAX_CONCURRENCY=10
43
- EMBEDDINGS_DIMENSIONS=1024
44
-
45
- # Reranker \u914D\u7F6E\uFF08\u5FC5\u9700\uFF09
46
- RERANK_API_KEY=your-api-key-here
47
- RERANK_BASE_URL=https://api.siliconflow.cn/v1/rerank
48
- RERANK_MODEL=BAAI/bge-reranker-v2-m3
49
- RERANK_TOP_N=20
50
-
51
- # \u7D22\u5F15\u5FFD\u7565\u6A21\u5F0F\uFF08\u53EF\u9009\uFF0C\u9017\u53F7\u5206\u9694\uFF0C\u9ED8\u8BA4\u5DF2\u5305\u542B\u5E38\u89C1\u5FFD\u7565\u9879\uFF09
52
- # IGNORE_PATTERNS=.venv,node_modules
53
- `;
54
- fs.writeFileSync(envFile, defaultEnvContent);
55
- logger.info({ envFile }, "\u5DF2\u521B\u5EFA\u9ED8\u8BA4 .env \u914D\u7F6E\u6587\u4EF6");
56
- }
57
- function isProjectIndexed(projectId) {
58
- const dbPath = path.join(BASE_DIR, projectId, "index.db");
59
- return fs.existsSync(dbPath);
60
- }
61
- async function ensureIndexed(repoPath, projectId, onProgress) {
62
- const { withLock } = await import("./lock-DVY3KJSK.js");
63
- const { scan } = await import("./scanner-RFG4YWYI.js");
64
- await withLock(
65
- projectId,
66
- "index",
67
- async () => {
68
- const wasIndexed = isProjectIndexed(projectId);
69
- if (!wasIndexed) {
70
- logger.info(
71
- { repoPath, projectId: projectId.slice(0, 10) },
72
- "\u4EE3\u7801\u5E93\u672A\u521D\u59CB\u5316\uFF0C\u5F00\u59CB\u9996\u6B21\u7D22\u5F15..."
73
- );
74
- onProgress?.(0, 100, "\u4EE3\u7801\u5E93\u672A\u7D22\u5F15\uFF0C\u5F00\u59CB\u9996\u6B21\u7D22\u5F15...");
75
- } else {
76
- logger.debug({ projectId: projectId.slice(0, 10) }, "\u6267\u884C\u589E\u91CF\u7D22\u5F15...");
77
- }
78
- const startTime = Date.now();
79
- const stats = await scan(repoPath, { vectorIndex: true, onProgress });
80
- const elapsed = Date.now() - startTime;
81
- logger.info(
82
- {
83
- projectId: projectId.slice(0, 10),
84
- isFirstTime: !wasIndexed,
85
- totalFiles: stats.totalFiles,
86
- added: stats.added,
87
- modified: stats.modified,
88
- deleted: stats.deleted,
89
- vectorIndex: stats.vectorIndex,
90
- elapsedMs: elapsed
91
- },
92
- "\u7D22\u5F15\u5B8C\u6210"
93
- );
94
- },
95
- INDEX_LOCK_TIMEOUT_MS
96
- );
97
- }
98
25
  async function handleCodebaseRetrieval(args, onProgress) {
99
26
  const { repo_path, information_request, technical_terms } = args;
100
27
  logger.info(
@@ -105,17 +32,16 @@ async function handleCodebaseRetrieval(args, onProgress) {
105
32
  },
106
33
  "MCP codebase-retrieval \u8C03\u7528\u5F00\u59CB"
107
34
  );
108
- const { checkEmbeddingEnv, checkRerankerEnv } = await import("./config-BWZ6CU3W.js");
35
+ const { checkEmbeddingEnv, checkRerankerEnv } = await import("./config-LCOJHTCF.js");
109
36
  const embeddingCheck = checkEmbeddingEnv();
110
37
  const rerankerCheck = checkRerankerEnv();
111
38
  const allMissingVars = [...embeddingCheck.missingVars, ...rerankerCheck.missingVars];
112
39
  if (allMissingVars.length > 0) {
113
40
  logger.warn({ missingVars: allMissingVars }, "MCP \u73AF\u5883\u53D8\u91CF\u672A\u914D\u7F6E");
114
- await ensureDefaultEnvFile();
115
- return formatEnvMissingResponse(allMissingVars);
41
+ return await checkEnvOrRespond(allMissingVars);
116
42
  }
117
43
  const projectId = generateProjectId(repo_path);
118
- await ensureIndexed(repo_path, projectId, onProgress);
44
+ await ensureIndexed(repo_path, projectId, { onProgress });
119
45
  const query = [information_request, ...technical_terms || []].filter(Boolean).join(" ");
120
46
  logger.info(
121
47
  {
@@ -124,8 +50,9 @@ async function handleCodebaseRetrieval(args, onProgress) {
124
50
  },
125
51
  "MCP \u67E5\u8BE2\u6784\u5EFA"
126
52
  );
127
- const { SearchService } = await import("./SearchService-MYPOCM3B.js");
128
- const service = new SearchService(projectId, repo_path);
53
+ const { SearchService } = await import("./SearchService-WVD6THR3.js");
54
+ const { getSearchConfigOverrides } = await import("./loadConfig-XTVT2OWW.js");
55
+ const service = new SearchService(projectId, repo_path, getSearchConfigOverrides());
129
56
  await service.init();
130
57
  logger.debug("SearchService \u521D\u59CB\u5316\u5B8C\u6210");
131
58
  const contextPack = await service.buildContextPack(query);
@@ -244,43 +171,8 @@ function detectLanguage(filePath) {
244
171
  };
245
172
  return langMap[ext] || ext || "plaintext";
246
173
  }
247
- function formatEnvMissingResponse(missingVars) {
248
- const configPath = "~/.contextweaver/.env";
249
- const text = `## \u26A0\uFE0F \u914D\u7F6E\u7F3A\u5931
250
-
251
- ContextWeaver \u9700\u8981\u914D\u7F6E Embedding API \u624D\u80FD\u5DE5\u4F5C\u3002
252
-
253
- ### \u7F3A\u5931\u7684\u73AF\u5883\u53D8\u91CF
254
- ${missingVars.map((v) => `- \`${v}\``).join("\n")}
255
-
256
- ### \u914D\u7F6E\u6B65\u9AA4
257
-
258
- \u5DF2\u81EA\u52A8\u521B\u5EFA\u914D\u7F6E\u6587\u4EF6\uFF1A\`${configPath}\`
259
-
260
- \u8BF7\u7F16\u8F91\u8BE5\u6587\u4EF6\uFF0C\u586B\u5199\u4F60\u7684 API Key\uFF1A
261
-
262
- \`\`\`bash
263
- # Embedding API \u914D\u7F6E\uFF08\u5FC5\u9700\uFF09
264
- EMBEDDINGS_API_KEY=your-api-key-here # \u2190 \u66FF\u6362\u4E3A\u4F60\u7684 API Key
265
-
266
- # Reranker \u914D\u7F6E\uFF08\u5FC5\u9700\uFF09
267
- RERANK_API_KEY=your-api-key-here # \u2190 \u66FF\u6362\u4E3A\u4F60\u7684 API Key
268
- \`\`\`
269
-
270
- \u4FDD\u5B58\u6587\u4EF6\u540E\u91CD\u65B0\u8C03\u7528\u6B64\u5DE5\u5177\u5373\u53EF\u3002
271
- `;
272
- return {
273
- content: [
274
- {
275
- type: "text",
276
- text
277
- }
278
- ]
279
- };
280
- }
281
174
 
282
175
  export {
283
176
  codebaseRetrievalSchema,
284
177
  handleCodebaseRetrieval
285
178
  };
286
- //# sourceMappingURL=chunk-7G5V7YT5.js.map
@@ -0,0 +1,115 @@
1
+ import {
2
+ getDefaultEnvFileContent
3
+ } from "./chunk-TPM6YP43.js";
4
+ import {
5
+ logger
6
+ } from "./chunk-JVKVSTQ3.js";
7
+
8
+ // src/mcp/tools/shared.ts
9
+ import fs from "fs";
10
+ import os from "os";
11
+ import path from "path";
12
+ var BASE_DIR = path.join(os.homedir(), ".contextweaver");
13
+ var INDEX_LOCK_TIMEOUT_MS = 10 * 60 * 1e3;
14
+ function isProjectIndexed(projectId) {
15
+ const dbPath = path.join(BASE_DIR, projectId, "index.db");
16
+ return fs.existsSync(dbPath);
17
+ }
18
+ async function ensureDefaultEnvFile() {
19
+ const configDir = BASE_DIR;
20
+ const envFile = path.join(configDir, ".env");
21
+ if (fs.existsSync(envFile)) {
22
+ return;
23
+ }
24
+ if (!fs.existsSync(configDir)) {
25
+ fs.mkdirSync(configDir, { recursive: true });
26
+ logger.info({ configDir }, "\u521B\u5EFA\u914D\u7F6E\u76EE\u5F55");
27
+ }
28
+ fs.writeFileSync(envFile, getDefaultEnvFileContent());
29
+ logger.info({ envFile }, "\u5DF2\u521B\u5EFA\u9ED8\u8BA4 .env \u914D\u7F6E\u6587\u4EF6");
30
+ }
31
+ async function ensureIndexed(repoPath, projectId, options = {}) {
32
+ const { onProgress, vectorIndex = true } = options;
33
+ const { withLock } = await import("./lock-HNKQ6X5B.js");
34
+ const { scan } = await import("./scanner-QDFZJLP7.js");
35
+ await withLock(
36
+ projectId,
37
+ "index",
38
+ async () => {
39
+ const wasIndexed = isProjectIndexed(projectId);
40
+ if (!wasIndexed) {
41
+ logger.info(
42
+ { repoPath, projectId: projectId.slice(0, 10), vectorIndex },
43
+ "\u4EE3\u7801\u5E93\u672A\u521D\u59CB\u5316\uFF0C\u5F00\u59CB\u9996\u6B21\u7D22\u5F15..."
44
+ );
45
+ onProgress?.(0, 100, "\u4EE3\u7801\u5E93\u672A\u7D22\u5F15\uFF0C\u5F00\u59CB\u9996\u6B21\u7D22\u5F15...");
46
+ } else {
47
+ logger.debug({ projectId: projectId.slice(0, 10), vectorIndex }, "\u6267\u884C\u589E\u91CF\u7D22\u5F15...");
48
+ }
49
+ const startTime = Date.now();
50
+ const stats = await scan(repoPath, { vectorIndex, onProgress });
51
+ const elapsed = Date.now() - startTime;
52
+ logger.info(
53
+ {
54
+ projectId: projectId.slice(0, 10),
55
+ isFirstTime: !wasIndexed,
56
+ totalFiles: stats.totalFiles,
57
+ added: stats.added,
58
+ modified: stats.modified,
59
+ deleted: stats.deleted,
60
+ vectorIndex: stats.vectorIndex,
61
+ elapsedMs: elapsed
62
+ },
63
+ "\u7D22\u5F15\u5B8C\u6210"
64
+ );
65
+ },
66
+ INDEX_LOCK_TIMEOUT_MS
67
+ );
68
+ }
69
+ function formatTextResponse(text) {
70
+ return {
71
+ content: [
72
+ {
73
+ type: "text",
74
+ text
75
+ }
76
+ ]
77
+ };
78
+ }
79
+ function formatEnvMissingResponse(missingVars) {
80
+ const configPath = "~/.contextweaver/.env";
81
+ const text = `## \u26A0\uFE0F \u914D\u7F6E\u7F3A\u5931
82
+
83
+ ContextWeaver \u9700\u8981\u914D\u7F6E Embedding API \u624D\u80FD\u5DE5\u4F5C\u3002
84
+
85
+ ### \u7F3A\u5931\u7684\u73AF\u5883\u53D8\u91CF
86
+ ${missingVars.map((v) => `- \`${v}\``).join("\n")}
87
+
88
+ ### \u914D\u7F6E\u6B65\u9AA4
89
+
90
+ \u5DF2\u81EA\u52A8\u521B\u5EFA\u914D\u7F6E\u6587\u4EF6\uFF1A\`${configPath}\`
91
+
92
+ \u8BF7\u7F16\u8F91\u8BE5\u6587\u4EF6\uFF0C\u586B\u5199\u4F60\u7684 API Key\uFF1A
93
+
94
+ \`\`\`bash
95
+ # Embedding API \u914D\u7F6E\uFF08\u5FC5\u9700\uFF09
96
+ EMBEDDINGS_API_KEY=your-api-key-here # \u2190 \u66FF\u6362\u4E3A\u4F60\u7684 API Key
97
+
98
+ # Reranker \u914D\u7F6E\uFF08\u5FC5\u9700\uFF09
99
+ RERANK_API_KEY=your-api-key-here # \u2190 \u66FF\u6362\u4E3A\u4F60\u7684 API Key
100
+ \`\`\`
101
+
102
+ \u4FDD\u5B58\u6587\u4EF6\u540E\u91CD\u65B0\u8C03\u7528\u6B64\u5DE5\u5177\u5373\u53EF\u3002
103
+ `;
104
+ return formatTextResponse(text);
105
+ }
106
+ async function checkEnvOrRespond(missingVars) {
107
+ await ensureDefaultEnvFile();
108
+ return formatEnvMissingResponse(missingVars);
109
+ }
110
+
111
+ export {
112
+ ensureIndexed,
113
+ formatTextResponse,
114
+ checkEnvOrRespond
115
+ };
@@ -0,0 +1,57 @@
1
+ // src/search/ChunkContentLoader.ts
2
+ var ChunkContentLoader = class _ChunkContentLoader {
3
+ constructor(db) {
4
+ this.db = db;
5
+ }
6
+ /**
7
+ * 生成 cache key
8
+ */
9
+ static key(slice) {
10
+ return `${slice.filePath}#${slice.start_index}#${slice.end_index}`;
11
+ }
12
+ /**
13
+ * 批量加载 chunk 正文
14
+ *
15
+ * @returns Map<key, code>,key 由 ChunkContentLoader.key 生成
16
+ */
17
+ loadMany(slices) {
18
+ const result = /* @__PURE__ */ new Map();
19
+ if (slices.length === 0) return result;
20
+ const byPath = /* @__PURE__ */ new Map();
21
+ for (const s of slices) {
22
+ let arr = byPath.get(s.filePath);
23
+ if (!arr) {
24
+ arr = [];
25
+ byPath.set(s.filePath, arr);
26
+ }
27
+ arr.push(s);
28
+ }
29
+ const stmt = this.db.prepare("SELECT content FROM files WHERE path = ?");
30
+ for (const [path, spans] of byPath) {
31
+ const row = stmt.get(path);
32
+ const content = row?.content ?? null;
33
+ for (const s of spans) {
34
+ const k = _ChunkContentLoader.key(s);
35
+ if (content === null) {
36
+ result.set(k, "");
37
+ continue;
38
+ }
39
+ const safeStart = Math.max(0, Math.min(s.start_index, content.length));
40
+ const safeEnd = Math.max(safeStart, Math.min(s.end_index, content.length));
41
+ result.set(k, content.slice(safeStart, safeEnd));
42
+ }
43
+ }
44
+ return result;
45
+ }
46
+ /**
47
+ * 加载单个 chunk 正文(便捷方法,不推荐在批量场景使用)
48
+ */
49
+ loadOne(slice) {
50
+ const map = this.loadMany([slice]);
51
+ return map.get(_ChunkContentLoader.key(slice)) ?? "";
52
+ }
53
+ };
54
+
55
+ export {
56
+ ChunkContentLoader
57
+ };