mdcontext 0.1.0 → 0.2.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 (251) hide show
  1. package/.changeset/config.json +9 -9
  2. package/.claude/settings.local.json +25 -0
  3. package/.github/workflows/claude-code-review.yml +44 -0
  4. package/.github/workflows/claude.yml +85 -0
  5. package/CONTRIBUTING.md +186 -0
  6. package/NOTES/NOTES +44 -0
  7. package/README.md +206 -3
  8. package/biome.json +1 -1
  9. package/dist/chunk-23UPXDNL.js +3044 -0
  10. package/dist/chunk-2W7MO2DL.js +1366 -0
  11. package/dist/chunk-3NUAZGMA.js +1689 -0
  12. package/dist/chunk-7TOWB2XB.js +366 -0
  13. package/dist/chunk-7XOTOADQ.js +3065 -0
  14. package/dist/chunk-AH2PDM2K.js +3042 -0
  15. package/dist/chunk-BNXWSZ63.js +3742 -0
  16. package/dist/chunk-BTL5DJVU.js +3222 -0
  17. package/dist/chunk-HDHYG7E4.js +104 -0
  18. package/dist/chunk-HLR4KZBP.js +3234 -0
  19. package/dist/chunk-IP3FRFEB.js +1045 -0
  20. package/dist/chunk-KHU56VDO.js +3042 -0
  21. package/dist/chunk-KRYIFLQR.js +85 -89
  22. package/dist/chunk-LBSDNLEM.js +287 -0
  23. package/dist/chunk-MNTQ7HCP.js +2643 -0
  24. package/dist/chunk-MUJELQQ6.js +1387 -0
  25. package/dist/chunk-MXJGMSLV.js +2199 -0
  26. package/dist/chunk-N6QJGC3Z.js +2636 -0
  27. package/dist/chunk-OBELGBPM.js +1713 -0
  28. package/dist/chunk-OT7R5XTA.js +3192 -0
  29. package/dist/chunk-P7X4RA2T.js +106 -0
  30. package/dist/chunk-PIDUQNC2.js +3185 -0
  31. package/dist/chunk-POGCDIH4.js +3187 -0
  32. package/dist/chunk-PSIEOQGZ.js +3043 -0
  33. package/dist/chunk-PVRT3IHA.js +3238 -0
  34. package/dist/chunk-QNN4TT23.js +1430 -0
  35. package/dist/chunk-RE3R45RJ.js +3042 -0
  36. package/dist/chunk-S7E6TFX6.js +718 -657
  37. package/dist/chunk-SG6GLU4U.js +1378 -0
  38. package/dist/chunk-SJCDV2ST.js +274 -0
  39. package/dist/chunk-SYE5XLF3.js +104 -0
  40. package/dist/chunk-T5VLYBZD.js +103 -0
  41. package/dist/chunk-TOQB7VWU.js +3238 -0
  42. package/dist/chunk-VFNMZ4ZQ.js +3228 -0
  43. package/dist/chunk-VVTGZNBT.js +1533 -1423
  44. package/dist/chunk-W7Q4RFEV.js +104 -0
  45. package/dist/chunk-XTYYVRLO.js +3190 -0
  46. package/dist/chunk-Y6MDYVJD.js +3063 -0
  47. package/dist/cli/main.js +4072 -629
  48. package/dist/index.d.ts +420 -33
  49. package/dist/index.js +8 -15
  50. package/dist/mcp/server.js +103 -7
  51. package/dist/schema-BAWSG7KY.js +22 -0
  52. package/dist/schema-E3QUPL26.js +20 -0
  53. package/dist/schema-EHL7WUT6.js +20 -0
  54. package/docs/019-USAGE.md +44 -5
  55. package/docs/020-current-implementation.md +8 -8
  56. package/docs/021-DOGFOODING-FINDINGS.md +1 -1
  57. package/docs/CONFIG.md +1123 -0
  58. package/docs/ERRORS.md +383 -0
  59. package/docs/summarization.md +320 -0
  60. package/justfile +40 -0
  61. package/package.json +39 -33
  62. package/research/INDEX.md +315 -0
  63. package/research/code-review/README.md +90 -0
  64. package/research/code-review/cli-error-handling-review.md +979 -0
  65. package/research/code-review/code-review-validation-report.md +464 -0
  66. package/research/code-review/main-ts-review.md +1128 -0
  67. package/research/config-docs/SUMMARY.md +357 -0
  68. package/research/config-docs/TEST-RESULTS.md +776 -0
  69. package/research/config-docs/TODO.md +542 -0
  70. package/research/config-docs/analysis.md +744 -0
  71. package/research/config-docs/fix-validation.md +502 -0
  72. package/research/config-docs/help-audit.md +264 -0
  73. package/research/config-docs/help-system-analysis.md +890 -0
  74. package/research/frontmatter/COMMENTS-ARE-SKIPPED.md +149 -0
  75. package/research/frontmatter/LLM-CODE-NAVIGATION.md +276 -0
  76. package/research/issue-review.md +603 -0
  77. package/research/llm-summarization/agent-cli-tools-2026.md +1082 -0
  78. package/research/llm-summarization/alternative-providers-2026.md +1428 -0
  79. package/research/llm-summarization/anthropic-2026.md +367 -0
  80. package/research/llm-summarization/claude-cli-integration.md +1706 -0
  81. package/research/llm-summarization/cli-integration-patterns.md +3155 -0
  82. package/research/llm-summarization/openai-2026.md +473 -0
  83. package/research/llm-summarization/openai-compatible-providers-2026.md +1022 -0
  84. package/research/llm-summarization/opencode-cli-integration.md +1552 -0
  85. package/research/llm-summarization/prompt-engineering-2026.md +1426 -0
  86. package/research/llm-summarization/prototype-results.md +56 -0
  87. package/research/llm-summarization/provider-switching-patterns-2026.md +2153 -0
  88. package/research/llm-summarization/typescript-llm-libraries-2026.md +2436 -0
  89. package/research/mdcontext-pudding/00-EXECUTIVE-SUMMARY.md +282 -0
  90. package/research/mdcontext-pudding/01-index-embed.md +956 -0
  91. package/research/mdcontext-pudding/02-search-COMMANDS.md +142 -0
  92. package/research/mdcontext-pudding/02-search-SUMMARY.md +146 -0
  93. package/research/mdcontext-pudding/02-search.md +970 -0
  94. package/research/mdcontext-pudding/03-context.md +779 -0
  95. package/research/mdcontext-pudding/04-navigation-and-analytics.md +803 -0
  96. package/research/mdcontext-pudding/04-tree.md +704 -0
  97. package/research/mdcontext-pudding/05-config.md +1038 -0
  98. package/research/mdcontext-pudding/06-links-summary.txt +87 -0
  99. package/research/mdcontext-pudding/06-links.md +679 -0
  100. package/research/mdcontext-pudding/07-stats.md +693 -0
  101. package/research/mdcontext-pudding/BUG-FIX-PLAN.md +388 -0
  102. package/research/mdcontext-pudding/P0-BUG-VALIDATION.md +167 -0
  103. package/research/mdcontext-pudding/README.md +168 -0
  104. package/research/mdcontext-pudding/TESTING-SUMMARY.md +128 -0
  105. package/research/research-quality-review.md +834 -0
  106. package/research/semantic-search/embedding-text-analysis.md +156 -0
  107. package/research/semantic-search/multi-word-failure-reproduction.md +171 -0
  108. package/research/semantic-search/query-processing-analysis.md +207 -0
  109. package/research/semantic-search/root-cause-and-solution.md +114 -0
  110. package/research/semantic-search/threshold-validation-report.md +69 -0
  111. package/research/semantic-search/vector-search-analysis.md +63 -0
  112. package/research/test-path-issues.md +276 -0
  113. package/review/ALP-76/1-error-type-design.md +962 -0
  114. package/review/ALP-76/2-error-handling-patterns.md +906 -0
  115. package/review/ALP-76/3-error-presentation.md +624 -0
  116. package/review/ALP-76/4-test-coverage.md +625 -0
  117. package/review/ALP-76/5-migration-completeness.md +440 -0
  118. package/review/ALP-76/6-effect-best-practices.md +755 -0
  119. package/scripts/apply-branch-protection.sh +47 -0
  120. package/scripts/branch-protection-templates.json +79 -0
  121. package/scripts/prototype-summarization.ts +346 -0
  122. package/scripts/rebuild-hnswlib.js +32 -37
  123. package/scripts/setup-branch-protection.sh +64 -0
  124. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/active-provider.json +7 -0
  125. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/bm25.json +541 -0
  126. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/bm25.meta.json +5 -0
  127. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/config.json +8 -0
  128. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/embeddings/openai_text-embedding-3-small_512/vectors.bin +0 -0
  129. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/embeddings/openai_text-embedding-3-small_512/vectors.meta.bin +0 -0
  130. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/indexes/documents.json +60 -0
  131. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/indexes/links.json +13 -0
  132. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/indexes/sections.json +1197 -0
  133. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/configuration-management.md +99 -0
  134. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/distributed-systems.md +92 -0
  135. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/error-handling.md +78 -0
  136. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/failure-automation.md +55 -0
  137. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/job-context.md +69 -0
  138. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/process-orchestration.md +99 -0
  139. package/src/cli/argv-preprocessor.test.ts +2 -2
  140. package/src/cli/cli.test.ts +230 -33
  141. package/src/cli/commands/config-cmd.ts +642 -0
  142. package/src/cli/commands/context.ts +97 -9
  143. package/src/cli/commands/duplicates.ts +122 -0
  144. package/src/cli/commands/embeddings.ts +529 -0
  145. package/src/cli/commands/index-cmd.ts +210 -30
  146. package/src/cli/commands/index.ts +3 -0
  147. package/src/cli/commands/search.ts +894 -64
  148. package/src/cli/commands/stats.ts +3 -0
  149. package/src/cli/commands/tree.ts +26 -5
  150. package/src/cli/config-layer.ts +176 -0
  151. package/src/cli/error-handler.test.ts +235 -0
  152. package/src/cli/error-handler.ts +655 -0
  153. package/src/cli/flag-schemas.ts +66 -0
  154. package/src/cli/help.ts +209 -7
  155. package/src/cli/main.ts +348 -58
  156. package/src/cli/options.ts +10 -0
  157. package/src/cli/shared-error-handling.ts +199 -0
  158. package/src/cli/utils.ts +150 -17
  159. package/src/config/file-provider.test.ts +320 -0
  160. package/src/config/file-provider.ts +273 -0
  161. package/src/config/index.ts +72 -0
  162. package/src/config/integration.test.ts +667 -0
  163. package/src/config/precedence.test.ts +277 -0
  164. package/src/config/precedence.ts +451 -0
  165. package/src/config/schema.test.ts +414 -0
  166. package/src/config/schema.ts +603 -0
  167. package/src/config/service.test.ts +320 -0
  168. package/src/config/service.ts +243 -0
  169. package/src/config/testing.test.ts +264 -0
  170. package/src/config/testing.ts +110 -0
  171. package/src/core/types.ts +6 -33
  172. package/src/duplicates/detector.test.ts +183 -0
  173. package/src/duplicates/detector.ts +414 -0
  174. package/src/duplicates/index.ts +18 -0
  175. package/src/embeddings/embedding-namespace.test.ts +300 -0
  176. package/src/embeddings/embedding-namespace.ts +947 -0
  177. package/src/embeddings/heading-boost.test.ts +222 -0
  178. package/src/embeddings/hnsw-build-options.test.ts +198 -0
  179. package/src/embeddings/hyde.test.ts +272 -0
  180. package/src/embeddings/hyde.ts +264 -0
  181. package/src/embeddings/index.ts +2 -0
  182. package/src/embeddings/openai-provider.ts +332 -83
  183. package/src/embeddings/pricing.json +22 -0
  184. package/src/embeddings/provider-constants.ts +204 -0
  185. package/src/embeddings/provider-errors.test.ts +967 -0
  186. package/src/embeddings/provider-errors.ts +565 -0
  187. package/src/embeddings/provider-factory.test.ts +240 -0
  188. package/src/embeddings/provider-factory.ts +225 -0
  189. package/src/embeddings/provider-integration.test.ts +788 -0
  190. package/src/embeddings/query-preprocessing.test.ts +187 -0
  191. package/src/embeddings/semantic-search-threshold.test.ts +508 -0
  192. package/src/embeddings/semantic-search.ts +780 -93
  193. package/src/embeddings/types.ts +293 -16
  194. package/src/embeddings/vector-store.ts +486 -77
  195. package/src/embeddings/voyage-provider.ts +313 -0
  196. package/src/errors/errors.test.ts +845 -0
  197. package/src/errors/index.ts +533 -0
  198. package/src/index/ignore-patterns.test.ts +354 -0
  199. package/src/index/ignore-patterns.ts +305 -0
  200. package/src/index/indexer.ts +286 -48
  201. package/src/index/storage.ts +94 -30
  202. package/src/index/types.ts +40 -2
  203. package/src/index/watcher.ts +67 -9
  204. package/src/index.ts +22 -0
  205. package/src/integration/search-keyword.test.ts +678 -0
  206. package/src/mcp/server.ts +135 -6
  207. package/src/parser/parser.ts +18 -19
  208. package/src/parser/section-filter.test.ts +277 -0
  209. package/src/parser/section-filter.ts +125 -3
  210. package/src/search/__tests__/hybrid-search.test.ts +650 -0
  211. package/src/search/bm25-store.ts +366 -0
  212. package/src/search/cross-encoder.test.ts +253 -0
  213. package/src/search/cross-encoder.ts +406 -0
  214. package/src/search/fuzzy-search.test.ts +419 -0
  215. package/src/search/fuzzy-search.ts +273 -0
  216. package/src/search/hybrid-search.ts +448 -0
  217. package/src/search/path-matcher.test.ts +276 -0
  218. package/src/search/path-matcher.ts +33 -0
  219. package/src/search/searcher.test.ts +99 -1
  220. package/src/search/searcher.ts +189 -67
  221. package/src/search/wink-bm25.d.ts +30 -0
  222. package/src/summarization/cli-providers/claude.ts +202 -0
  223. package/src/summarization/cli-providers/detection.test.ts +273 -0
  224. package/src/summarization/cli-providers/detection.ts +118 -0
  225. package/src/summarization/cli-providers/index.ts +8 -0
  226. package/src/summarization/cost.test.ts +139 -0
  227. package/src/summarization/cost.ts +102 -0
  228. package/src/summarization/error-handler.test.ts +127 -0
  229. package/src/summarization/error-handler.ts +111 -0
  230. package/src/summarization/index.ts +102 -0
  231. package/src/summarization/pipeline.test.ts +498 -0
  232. package/src/summarization/pipeline.ts +231 -0
  233. package/src/summarization/prompts.test.ts +269 -0
  234. package/src/summarization/prompts.ts +133 -0
  235. package/src/summarization/provider-factory.test.ts +396 -0
  236. package/src/summarization/provider-factory.ts +178 -0
  237. package/src/summarization/types.ts +184 -0
  238. package/src/summarize/summarizer.ts +104 -35
  239. package/src/types/huggingface-transformers.d.ts +66 -0
  240. package/tests/fixtures/cli/.mdcontext/active-provider.json +7 -0
  241. package/tests/fixtures/cli/.mdcontext/embeddings/openai_text-embedding-3-small_512/vectors.bin +0 -0
  242. package/tests/fixtures/cli/.mdcontext/embeddings/openai_text-embedding-3-small_512/vectors.meta.bin +0 -0
  243. package/tests/fixtures/cli/.mdcontext/indexes/documents.json +4 -4
  244. package/tests/fixtures/cli/.mdcontext/indexes/sections.json +14 -0
  245. package/tests/integration/embed-index.test.ts +712 -0
  246. package/tests/integration/search-context.test.ts +469 -0
  247. package/tests/integration/search-semantic.test.ts +522 -0
  248. package/vitest.config.ts +1 -6
  249. package/AGENTS.md +0 -46
  250. package/tests/fixtures/cli/.mdcontext/vectors.bin +0 -0
  251. package/tests/fixtures/cli/.mdcontext/vectors.meta.json +0 -1264
@@ -0,0 +1,3742 @@
1
+ import {
2
+ ApiKeyInvalidError,
3
+ ApiKeyMissingError,
4
+ ConfigError,
5
+ DimensionMismatchError,
6
+ EmbeddingError,
7
+ EmbeddingsNotFoundError,
8
+ INDEX_DIR,
9
+ IndexNotFoundError,
10
+ VectorStoreError,
11
+ countTokensApprox,
12
+ createStorage,
13
+ loadDocumentIndex,
14
+ loadSectionIndex,
15
+ parseFile
16
+ } from "./chunk-MUJELQQ6.js";
17
+ import {
18
+ MdContextConfig,
19
+ defaultConfig
20
+ } from "./chunk-7TOWB2XB.js";
21
+
22
+ // src/config/precedence.ts
23
+ import { ConfigProvider as ConfigProvider2, Effect as Effect2 } from "effect";
24
+
25
+ // src/config/file-provider.ts
26
+ import * as fs from "fs";
27
+ import * as path from "path";
28
+ import { ConfigProvider, Effect } from "effect";
29
+ var CONFIG_FILE_NAMES = [
30
+ "mdcontext.config.ts",
31
+ "mdcontext.config.js",
32
+ "mdcontext.config.mjs",
33
+ "mdcontext.config.json",
34
+ ".mdcontextrc",
35
+ ".mdcontextrc.json"
36
+ ];
37
+ var findConfigFile = (startDir) => {
38
+ let currentDir = path.resolve(startDir);
39
+ const root = path.parse(currentDir).root;
40
+ while (currentDir !== root) {
41
+ for (const fileName of CONFIG_FILE_NAMES) {
42
+ const configPath = path.join(currentDir, fileName);
43
+ if (fs.existsSync(configPath)) {
44
+ const format = getConfigFormat(fileName);
45
+ return { path: configPath, format };
46
+ }
47
+ }
48
+ const parentDir = path.dirname(currentDir);
49
+ if (parentDir === currentDir) break;
50
+ currentDir = parentDir;
51
+ }
52
+ return null;
53
+ };
54
+ var getConfigFormat = (fileName) => {
55
+ if (fileName.endsWith(".ts")) return "ts";
56
+ if (fileName.endsWith(".js") || fileName.endsWith(".mjs")) return "js";
57
+ return "json";
58
+ };
59
+ var loadJsonConfig = (filePath) => Effect.try({
60
+ try: () => {
61
+ const content = fs.readFileSync(filePath, "utf-8");
62
+ return JSON.parse(content);
63
+ },
64
+ catch: (error) => new ConfigError({
65
+ field: "configFile",
66
+ message: `Failed to load config from ${filePath}: ${error instanceof Error ? error.message : String(error)}`,
67
+ cause: error
68
+ })
69
+ });
70
+ var loadJsConfig = (filePath) => Effect.tryPromise({
71
+ try: async () => {
72
+ const fileUrl = `file://${filePath}`;
73
+ const module = await import(fileUrl);
74
+ const config = module.default ?? module.config;
75
+ if (!config || typeof config !== "object") {
76
+ throw new Error(
77
+ 'Config file must export a default object or named "config" export'
78
+ );
79
+ }
80
+ return config;
81
+ },
82
+ catch: (error) => new ConfigError({
83
+ field: "configFile",
84
+ message: `Failed to load config from ${filePath}: ${error instanceof Error ? error.message : String(error)}`,
85
+ cause: error
86
+ })
87
+ });
88
+ var loadConfigFromFile = (filePath, format) => {
89
+ switch (format) {
90
+ case "json":
91
+ return loadJsonConfig(filePath);
92
+ case "ts":
93
+ case "js":
94
+ return loadJsConfig(filePath);
95
+ }
96
+ };
97
+ var loadConfigFile = (startDir) => Effect.gen(function* () {
98
+ const found = findConfigFile(startDir);
99
+ if (!found) {
100
+ return {
101
+ found: false,
102
+ searched: CONFIG_FILE_NAMES.map((name) => path.join(startDir, name))
103
+ };
104
+ }
105
+ const config = yield* loadConfigFromFile(found.path, found.format);
106
+ return {
107
+ found: true,
108
+ path: found.path,
109
+ config
110
+ };
111
+ });
112
+ var loadConfigFromPath = (configPath) => Effect.gen(function* () {
113
+ const resolvedPath = path.resolve(configPath);
114
+ if (!fs.existsSync(resolvedPath)) {
115
+ return yield* Effect.fail(
116
+ new ConfigError({
117
+ field: "configFile",
118
+ message: `Config file not found: ${resolvedPath}`
119
+ })
120
+ );
121
+ }
122
+ const format = getConfigFormat(path.basename(configPath));
123
+ return yield* loadConfigFromFile(resolvedPath, format);
124
+ });
125
+
126
+ // src/config/precedence.ts
127
+ var flattenConfig = (config, prefix = "") => {
128
+ const result = /* @__PURE__ */ new Map();
129
+ const flatten = (obj, currentPrefix) => {
130
+ if (obj === null || obj === void 0) {
131
+ return;
132
+ }
133
+ if (Array.isArray(obj)) {
134
+ result.set(currentPrefix, obj.join(","));
135
+ return;
136
+ }
137
+ if (typeof obj === "object") {
138
+ for (const [key, value] of Object.entries(obj)) {
139
+ const newKey = currentPrefix ? `${currentPrefix}.${key}` : key;
140
+ flatten(value, newKey);
141
+ }
142
+ return;
143
+ }
144
+ result.set(currentPrefix, String(obj));
145
+ };
146
+ flatten(config, prefix);
147
+ return result;
148
+ };
149
+ var ENV_SEQ_DELIM = ",";
150
+ var CONFIG_SCHEMA_KEYS = {
151
+ index: [
152
+ "maxDepth",
153
+ "excludePatterns",
154
+ "fileExtensions",
155
+ "followSymlinks",
156
+ "indexDir"
157
+ ],
158
+ search: [
159
+ "defaultLimit",
160
+ "maxLimit",
161
+ "minSimilarity",
162
+ "includeSnippets",
163
+ "snippetLength",
164
+ "autoIndexThreshold"
165
+ ],
166
+ embeddings: [
167
+ "provider",
168
+ "baseURL",
169
+ "model",
170
+ "dimensions",
171
+ "batchSize",
172
+ "maxRetries",
173
+ "retryDelayMs",
174
+ "timeoutMs",
175
+ "apiKey"
176
+ ],
177
+ summarization: [
178
+ "briefTokenBudget",
179
+ "summaryTokenBudget",
180
+ "compressionRatio",
181
+ "minSectionTokens",
182
+ "maxTopics",
183
+ "minPartialBudget"
184
+ ],
185
+ output: ["format", "color", "prettyJson", "verbose", "debug"],
186
+ paths: ["root", "configFile", "cacheDir"]
187
+ };
188
+ var generateEnvKeyMapping = () => {
189
+ const mapping = {};
190
+ for (const [section, keys] of Object.entries(CONFIG_SCHEMA_KEYS)) {
191
+ for (const key of keys) {
192
+ const envKey = `${section}_${key}`.toLowerCase();
193
+ const configKey = `${section}.${key}`;
194
+ mapping[envKey] = configKey;
195
+ }
196
+ }
197
+ return mapping;
198
+ };
199
+ var ENV_KEY_MAPPING = generateEnvKeyMapping();
200
+ var readEnvConfig = (prefix = "MDCONTEXT") => {
201
+ const result = /* @__PURE__ */ new Map();
202
+ const prefixWithUnderscore = `${prefix}_`;
203
+ for (const [key, value] of Object.entries(process.env)) {
204
+ if (key.startsWith(prefixWithUnderscore) && value !== void 0) {
205
+ const envKey = key.slice(prefixWithUnderscore.length).toLowerCase();
206
+ const configKey = ENV_KEY_MAPPING[envKey];
207
+ if (configKey) {
208
+ result.set(configKey, value);
209
+ }
210
+ }
211
+ }
212
+ return result;
213
+ };
214
+ var createConfigProvider = (options = {}) => Effect2.gen(function* () {
215
+ const {
216
+ cliOverrides,
217
+ configPath,
218
+ workingDir = process.cwd(),
219
+ envPrefix = "MDCONTEXT",
220
+ skipConfigFile = false,
221
+ skipEnv = false
222
+ } = options;
223
+ const mergedMap = /* @__PURE__ */ new Map();
224
+ if (!skipConfigFile) {
225
+ let fileConfig;
226
+ if (configPath) {
227
+ fileConfig = yield* loadConfigFromPath(configPath);
228
+ } else {
229
+ const result = yield* loadConfigFile(workingDir);
230
+ if (result.found) {
231
+ fileConfig = result.config;
232
+ }
233
+ }
234
+ if (fileConfig) {
235
+ const flattened = flattenConfig(fileConfig);
236
+ for (const [k, v] of flattened) {
237
+ mergedMap.set(k, v);
238
+ }
239
+ }
240
+ }
241
+ if (!skipEnv) {
242
+ const envConfig = readEnvConfig(envPrefix);
243
+ for (const [k, v] of envConfig) {
244
+ mergedMap.set(k, v);
245
+ }
246
+ }
247
+ if (cliOverrides && Object.keys(cliOverrides).length > 0) {
248
+ const flattened = flattenConfig(cliOverrides);
249
+ for (const [k, v] of flattened) {
250
+ mergedMap.set(k, v);
251
+ }
252
+ }
253
+ return ConfigProvider2.fromMap(mergedMap, {
254
+ pathDelim: ".",
255
+ seqDelim: ENV_SEQ_DELIM
256
+ });
257
+ });
258
+ var createConfigProviderSync = (options = {}) => {
259
+ const {
260
+ cliOverrides,
261
+ fileConfig,
262
+ envPrefix = "MDCONTEXT",
263
+ skipConfigFile = false,
264
+ skipEnv = false
265
+ } = options;
266
+ const mergedMap = /* @__PURE__ */ new Map();
267
+ if (!skipConfigFile && fileConfig) {
268
+ const flattened = flattenConfig(fileConfig);
269
+ for (const [k, v] of flattened) {
270
+ mergedMap.set(k, v);
271
+ }
272
+ }
273
+ if (!skipEnv) {
274
+ const envConfig = readEnvConfig(envPrefix);
275
+ for (const [k, v] of envConfig) {
276
+ mergedMap.set(k, v);
277
+ }
278
+ }
279
+ if (cliOverrides && Object.keys(cliOverrides).length > 0) {
280
+ const flattened = flattenConfig(cliOverrides);
281
+ for (const [k, v] of flattened) {
282
+ mergedMap.set(k, v);
283
+ }
284
+ }
285
+ return ConfigProvider2.fromMap(mergedMap, {
286
+ pathDelim: ".",
287
+ seqDelim: ENV_SEQ_DELIM
288
+ });
289
+ };
290
+
291
+ // src/config/service.ts
292
+ import { Context, Effect as Effect3, Layer } from "effect";
293
+ var ConfigService = class extends Context.Tag("ConfigService")() {
294
+ };
295
+ var ConfigServiceLive = Layer.effect(ConfigService, MdContextConfig);
296
+ var makeConfigLayer = (config) => Layer.succeed(ConfigService, config);
297
+ var ConfigServiceDefault = makeConfigLayer(defaultConfig);
298
+
299
+ // src/config/testing.ts
300
+ import { Effect as Effect4, Layer as Layer2 } from "effect";
301
+ var TestConfigLayer = Layer2.succeed(
302
+ ConfigService,
303
+ defaultConfig
304
+ );
305
+
306
+ // src/summarize/formatters.ts
307
+ var formatSummary = (summary, options = {}) => {
308
+ const maxTokens = options.maxTokens;
309
+ const flatSections = [];
310
+ const collectSections = (section, depth = 0, parentNumber = "", index = 0) => {
311
+ const number = parentNumber ? `${parentNumber}.${index + 1}` : `${index + 1}`;
312
+ flatSections.push({ section, depth, number });
313
+ section.children.forEach((child, i) => {
314
+ collectSections(child, depth + 1, number, i);
315
+ });
316
+ };
317
+ summary.sections.forEach((section, i) => {
318
+ collectSections(section, 0, "", i);
319
+ });
320
+ const buildOutput = (includedSectionIndices, truncationInfo, includeTopics2) => {
321
+ const lines = [];
322
+ if (truncationInfo.showWarning && truncationInfo.truncatedCount > 0 && truncationInfo.tokensTotal > 0) {
323
+ const pct = Math.round(
324
+ truncationInfo.tokensShown / truncationInfo.tokensTotal * 100
325
+ );
326
+ lines.push(
327
+ `\u26A0\uFE0F Truncated: Showing ~${truncationInfo.tokensShown}/${truncationInfo.tokensTotal} tokens (${pct}%)`
328
+ );
329
+ if (truncationInfo.includedNumbers.length > 0) {
330
+ const includedDisplay = truncationInfo.includedNumbers.length <= 6 ? truncationInfo.includedNumbers.join(", ") : truncationInfo.includedNumbers.slice(0, 5).join(", ") + `, ... (+${truncationInfo.includedNumbers.length - 5} more)`;
331
+ lines.push(`Sections included: ${includedDisplay}`);
332
+ }
333
+ if (truncationInfo.excludedNumbers.length > 0) {
334
+ const excludedDisplay = truncationInfo.excludedNumbers.length <= 6 ? truncationInfo.excludedNumbers.join(", ") : truncationInfo.excludedNumbers.slice(0, 5).join(", ") + `, ... (+${truncationInfo.excludedNumbers.length - 5} more)`;
335
+ lines.push(`Sections excluded: ${excludedDisplay}`);
336
+ }
337
+ lines.push(
338
+ "Use --full for complete content or --section to target specific sections."
339
+ );
340
+ lines.push("");
341
+ }
342
+ lines.push(`# ${summary.title}`);
343
+ lines.push(`Path: ${summary.path}`);
344
+ const tokenLineIndex = lines.length;
345
+ lines.push("PLACEHOLDER");
346
+ lines.push("");
347
+ const fullTopicsLine2 = summary.keyTopics.length > 0 ? `**Topics:** ${summary.keyTopics.join(", ")}` : "";
348
+ if (includeTopics2 && fullTopicsLine2) {
349
+ lines.push(fullTopicsLine2);
350
+ lines.push("");
351
+ }
352
+ const sectionLines = [];
353
+ for (let i = 0; i < flatSections.length; i++) {
354
+ if (!includedSectionIndices.has(i)) continue;
355
+ const { section, depth } = flatSections[i];
356
+ const indent = " ".repeat(depth);
357
+ const prefix = "#".repeat(section.level);
358
+ sectionLines.push(`${indent}${prefix} ${section.heading}`);
359
+ if (section.summary) {
360
+ sectionLines.push(`${indent}${section.summary}`);
361
+ }
362
+ }
363
+ lines.push(sectionLines.join("\n"));
364
+ const tempOutput = lines.join("\n");
365
+ const tokensWithoutLine = countTokensApprox(
366
+ tempOutput.replace("PLACEHOLDER", "")
367
+ );
368
+ let estimatedTotal = tokensWithoutLine + 8;
369
+ for (let iter = 0; iter < 3; iter++) {
370
+ const testTokenLine = `Tokens: ${estimatedTotal} (${(summary.compressionRatio * 100).toFixed(0)}% reduction from ${summary.originalTokens})`;
371
+ const testOutput = tempOutput.replace("PLACEHOLDER", testTokenLine);
372
+ const actualTotal = countTokensApprox(testOutput);
373
+ if (actualTotal === estimatedTotal) break;
374
+ estimatedTotal = actualTotal;
375
+ }
376
+ const finalTokenLine = `Tokens: ${estimatedTotal} (${(summary.compressionRatio * 100).toFixed(0)}% reduction from ${summary.originalTokens})`;
377
+ lines[tokenLineIndex] = finalTokenLine;
378
+ return lines.join("\n");
379
+ };
380
+ if (maxTokens === void 0) {
381
+ const allIndices = new Set(flatSections.map((_, i) => i));
382
+ const hasPriorTruncation = summary.truncated && summary.truncatedCount;
383
+ return buildOutput(
384
+ allIndices,
385
+ {
386
+ showWarning: !!hasPriorTruncation,
387
+ truncatedCount: summary.truncatedCount ?? 0,
388
+ includedNumbers: flatSections.map((s) => s.number),
389
+ excludedNumbers: [],
390
+ tokensShown: summary.summaryTokens,
391
+ tokensTotal: summary.originalTokens
392
+ },
393
+ true
394
+ );
395
+ }
396
+ const includedIndices = /* @__PURE__ */ new Set();
397
+ let truncatedCount = 0;
398
+ let includeTopics = true;
399
+ const SAFETY_MARGIN = 1.15;
400
+ const minHeaderTemplate = [
401
+ `# ${summary.title}`,
402
+ `Path: ${summary.path}`,
403
+ `Tokens: 9999 (${(summary.compressionRatio * 100).toFixed(0)}% reduction from ${summary.originalTokens})`,
404
+ "",
405
+ ""
406
+ ].join("\n");
407
+ const minHeaderTokens = Math.ceil(
408
+ countTokensApprox(minHeaderTemplate) * SAFETY_MARGIN
409
+ );
410
+ const fullTopicsLine = summary.keyTopics.length > 0 ? `**Topics:** ${summary.keyTopics.join(", ")}
411
+ ` : "";
412
+ const topicsTokens = fullTopicsLine ? Math.ceil(countTokensApprox(fullTopicsLine) * SAFETY_MARGIN) : 0;
413
+ const truncationWarningTokens = Math.ceil(
414
+ countTokensApprox(
415
+ `\u26A0\uFE0F Truncated: Showing ~9999/9999 tokens (99%)
416
+ Sections included: 1, 2, 3, 4, 5, ... (+99 more)
417
+ Sections excluded: 6, 7, 8, 9, 10, ... (+99 more)
418
+ Use --full for complete content or --section to target specific sections.
419
+ `
420
+ ) * SAFETY_MARGIN
421
+ );
422
+ let headerTokens = minHeaderTokens + topicsTokens;
423
+ if (headerTokens >= maxTokens) {
424
+ includeTopics = false;
425
+ headerTokens = minHeaderTokens;
426
+ }
427
+ let contentBudget = maxTokens - headerTokens - truncationWarningTokens;
428
+ let tokensUsed = 0;
429
+ for (let i = 0; i < flatSections.length; i++) {
430
+ const { section, depth } = flatSections[i];
431
+ const indent = " ".repeat(depth);
432
+ const prefix = "#".repeat(section.level);
433
+ const sectionContent = section.summary ? `${indent}${prefix} ${section.heading}
434
+ ${indent}${section.summary}` : `${indent}${prefix} ${section.heading}`;
435
+ const sectionTokens = Math.ceil(
436
+ countTokensApprox(sectionContent) * SAFETY_MARGIN
437
+ );
438
+ if (tokensUsed + sectionTokens <= contentBudget) {
439
+ includedIndices.add(i);
440
+ tokensUsed += sectionTokens;
441
+ } else {
442
+ truncatedCount++;
443
+ }
444
+ }
445
+ if (truncatedCount === 0) {
446
+ contentBudget += truncationWarningTokens;
447
+ }
448
+ const includedNumbers = [];
449
+ const excludedNumbers = [];
450
+ for (let i = 0; i < flatSections.length; i++) {
451
+ if (includedIndices.has(i)) {
452
+ includedNumbers.push(flatSections[i].number);
453
+ } else {
454
+ excludedNumbers.push(flatSections[i].number);
455
+ }
456
+ }
457
+ let tokensShown = 0;
458
+ for (const idx of includedIndices) {
459
+ tokensShown += flatSections[idx].section.summaryTokens;
460
+ }
461
+ let output = buildOutput(
462
+ includedIndices,
463
+ {
464
+ showWarning: truncatedCount > 0,
465
+ truncatedCount,
466
+ includedNumbers,
467
+ excludedNumbers,
468
+ tokensShown,
469
+ tokensTotal: summary.originalTokens
470
+ },
471
+ includeTopics
472
+ );
473
+ let actualTokens = countTokensApprox(output);
474
+ const sortedIndices = Array.from(includedIndices).sort((a, b) => b - a);
475
+ let removalIndex = 0;
476
+ while (actualTokens > maxTokens && removalIndex < sortedIndices.length) {
477
+ const indexToRemove = sortedIndices[removalIndex];
478
+ includedIndices.delete(indexToRemove);
479
+ truncatedCount++;
480
+ removalIndex++;
481
+ const removedNumber = flatSections[indexToRemove].number;
482
+ const includedIdx = includedNumbers.indexOf(removedNumber);
483
+ if (includedIdx !== -1) {
484
+ includedNumbers.splice(includedIdx, 1);
485
+ excludedNumbers.push(removedNumber);
486
+ }
487
+ tokensShown -= flatSections[indexToRemove].section.summaryTokens;
488
+ output = buildOutput(
489
+ includedIndices,
490
+ {
491
+ showWarning: true,
492
+ truncatedCount,
493
+ includedNumbers,
494
+ excludedNumbers,
495
+ tokensShown,
496
+ tokensTotal: summary.originalTokens
497
+ },
498
+ includeTopics
499
+ );
500
+ actualTokens = countTokensApprox(output);
501
+ }
502
+ if (actualTokens > maxTokens && includeTopics) {
503
+ includeTopics = false;
504
+ output = buildOutput(
505
+ includedIndices,
506
+ {
507
+ showWarning: truncatedCount > 0,
508
+ truncatedCount,
509
+ includedNumbers,
510
+ excludedNumbers,
511
+ tokensShown,
512
+ tokensTotal: summary.originalTokens
513
+ },
514
+ includeTopics
515
+ );
516
+ actualTokens = countTokensApprox(output);
517
+ }
518
+ if (actualTokens > maxTokens && truncatedCount > 0) {
519
+ output = buildOutput(
520
+ includedIndices,
521
+ {
522
+ showWarning: false,
523
+ truncatedCount,
524
+ includedNumbers,
525
+ excludedNumbers,
526
+ tokensShown,
527
+ tokensTotal: summary.originalTokens
528
+ },
529
+ includeTopics
530
+ );
531
+ actualTokens = countTokensApprox(output);
532
+ }
533
+ return output;
534
+ };
535
+ var formatAssembledContext = (context) => {
536
+ const lines = [];
537
+ lines.push("# Context Assembly");
538
+ lines.push(`Total tokens: ${context.totalTokens}/${context.budget}`);
539
+ lines.push(`Sources: ${context.sources.length}`);
540
+ lines.push("");
541
+ for (const source of context.sources) {
542
+ lines.push("---");
543
+ lines.push("");
544
+ lines.push(source.content);
545
+ }
546
+ if (context.overflow.length > 0) {
547
+ lines.push("---");
548
+ lines.push("");
549
+ lines.push("## Overflow (not included due to budget)");
550
+ for (const overflowPath of context.overflow) {
551
+ lines.push(`- ${overflowPath}`);
552
+ }
553
+ }
554
+ return lines.join("\n");
555
+ };
556
+
557
+ // src/summarize/summarizer.ts
558
+ import * as fs2 from "fs/promises";
559
+ import * as path2 from "path";
560
+ import { Effect as Effect5 } from "effect";
561
+
562
+ // src/parser/section-filter.ts
563
+ var globMatch = (text, pattern) => {
564
+ const regexPattern = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, ".");
565
+ const regex = new RegExp(`^${regexPattern}$`, "i");
566
+ return regex.test(text);
567
+ };
568
+ var buildSectionList = (document) => {
569
+ const result = [];
570
+ const processSection = (section, prefix, index) => {
571
+ const number = prefix ? `${prefix}.${index + 1}` : `${index + 1}`;
572
+ result.push({
573
+ number,
574
+ heading: section.heading,
575
+ level: section.level,
576
+ tokenCount: section.metadata.tokenCount
577
+ });
578
+ section.children.forEach((child, i) => {
579
+ processSection(child, number, i);
580
+ });
581
+ };
582
+ document.sections.forEach((section, i) => {
583
+ processSection(section, "", i);
584
+ });
585
+ return result;
586
+ };
587
+ var formatSectionList = (sections) => {
588
+ const lines = [];
589
+ for (const section of sections) {
590
+ const depth = (section.number.match(/\./g) || []).length;
591
+ const indent = " ".repeat(depth);
592
+ lines.push(
593
+ `${indent}${section.number}. ${section.heading} (${section.tokenCount} tokens)`
594
+ );
595
+ }
596
+ return lines.join("\n");
597
+ };
598
+ var matchesSelector = (section, selector) => {
599
+ if (/^[\d.]+$/.test(selector)) {
600
+ return section.number === selector;
601
+ }
602
+ if (section.heading.toLowerCase() === selector.toLowerCase()) {
603
+ return true;
604
+ }
605
+ if (selector.includes("*") || selector.includes("?")) {
606
+ return globMatch(section.heading, selector);
607
+ }
608
+ return section.heading.toLowerCase().includes(selector.toLowerCase());
609
+ };
610
+ var matchesExclusionPatterns = (section, excludePatterns) => {
611
+ return excludePatterns.some((pattern) => matchesSelector(section, pattern));
612
+ };
613
+ var findMatchingSections = (sectionList, selector) => {
614
+ return sectionList.filter((s) => matchesSelector(s, selector));
615
+ };
616
+ var filterExcludedSections = (sectionList, excludePatterns) => {
617
+ if (excludePatterns.length === 0) {
618
+ return sectionList;
619
+ }
620
+ return sectionList.filter(
621
+ (s) => !matchesExclusionPatterns(s, excludePatterns)
622
+ );
623
+ };
624
+ var getDescendantNumbers = (sectionList, parentNumber) => {
625
+ const result = /* @__PURE__ */ new Set();
626
+ const prefix = `${parentNumber}.`;
627
+ for (const section of sectionList) {
628
+ if (section.number.startsWith(prefix)) {
629
+ result.add(section.number);
630
+ }
631
+ }
632
+ return result;
633
+ };
634
+ var extractSectionContent = (document, selector, options = {}) => {
635
+ const sectionList = buildSectionList(document);
636
+ let matchedSections = findMatchingSections(sectionList, selector);
637
+ const excludedNumbers = [];
638
+ if (options.exclude && options.exclude.length > 0) {
639
+ const beforeFilter = matchedSections;
640
+ matchedSections = filterExcludedSections(matchedSections, options.exclude);
641
+ for (const section of beforeFilter) {
642
+ if (!matchedSections.includes(section)) {
643
+ excludedNumbers.push(section.number);
644
+ }
645
+ }
646
+ }
647
+ if (matchedSections.length === 0) {
648
+ return { sections: [], matchedNumbers: [], excludedNumbers };
649
+ }
650
+ const numbersToInclude = /* @__PURE__ */ new Set();
651
+ const matchedNumbers = [];
652
+ for (const matched of matchedSections) {
653
+ numbersToInclude.add(matched.number);
654
+ matchedNumbers.push(matched.number);
655
+ if (!options.shallow) {
656
+ const descendants = getDescendantNumbers(sectionList, matched.number);
657
+ for (const desc of descendants) {
658
+ numbersToInclude.add(desc);
659
+ }
660
+ }
661
+ }
662
+ const numberToSection = /* @__PURE__ */ new Map();
663
+ const mapSections = (sections, prefix) => {
664
+ sections.forEach((section, i) => {
665
+ const number = prefix ? `${prefix}.${i + 1}` : `${i + 1}`;
666
+ numberToSection.set(number, section);
667
+ mapSections(section.children, number);
668
+ });
669
+ };
670
+ mapSections(document.sections, "");
671
+ const extractedSections = [];
672
+ for (const number of matchedNumbers) {
673
+ const section = numberToSection.get(number);
674
+ if (section) {
675
+ if (options.shallow) {
676
+ extractedSections.push({
677
+ ...section,
678
+ children: []
679
+ });
680
+ } else {
681
+ extractedSections.push(section);
682
+ }
683
+ }
684
+ }
685
+ return { sections: extractedSections, matchedNumbers, excludedNumbers };
686
+ };
687
+ var formatExtractedSections = (sections) => {
688
+ const formatSection = (section, includeChildren) => {
689
+ const lines = [];
690
+ const headingPrefix = "#".repeat(section.level);
691
+ lines.push(`${headingPrefix} ${section.heading}`);
692
+ lines.push("");
693
+ const contentLines = section.content.split("\n");
694
+ const contentWithoutHeading = contentLines.filter((line, i) => i > 0 || !line.startsWith("#")).join("\n").trim();
695
+ if (contentWithoutHeading) {
696
+ lines.push(contentWithoutHeading);
697
+ }
698
+ if (includeChildren) {
699
+ for (const child of section.children) {
700
+ lines.push("");
701
+ lines.push(formatSection(child, true));
702
+ }
703
+ }
704
+ return lines.join("\n");
705
+ };
706
+ return sections.map((s) => formatSection(s, true)).join("\n\n");
707
+ };
708
+ var filterDocumentSections = (document, excludePatterns) => {
709
+ if (excludePatterns.length === 0) {
710
+ return { document, excludedCount: 0 };
711
+ }
712
+ const sectionList = buildSectionList(document);
713
+ let excludedCount = 0;
714
+ const numbersToExclude = /* @__PURE__ */ new Set();
715
+ for (const section of sectionList) {
716
+ if (matchesExclusionPatterns(section, excludePatterns)) {
717
+ const prefix = `${section.number}.`;
718
+ for (const candidate of sectionList) {
719
+ if (candidate.number === section.number || candidate.number.startsWith(prefix)) {
720
+ if (!numbersToExclude.has(candidate.number)) {
721
+ numbersToExclude.add(candidate.number);
722
+ excludedCount++;
723
+ }
724
+ }
725
+ }
726
+ }
727
+ }
728
+ if (numbersToExclude.size === 0) {
729
+ return { document, excludedCount: 0 };
730
+ }
731
+ const filterSections = (sections, prefix) => {
732
+ const result = [];
733
+ sections.forEach((section, i) => {
734
+ const number = prefix ? `${prefix}.${i + 1}` : `${i + 1}`;
735
+ if (!numbersToExclude.has(number)) {
736
+ result.push({
737
+ ...section,
738
+ children: filterSections(section.children, number)
739
+ });
740
+ }
741
+ });
742
+ return result;
743
+ };
744
+ const filteredSections = filterSections(document.sections, "");
745
+ return {
746
+ document: {
747
+ ...document,
748
+ sections: filteredSections
749
+ },
750
+ excludedCount
751
+ };
752
+ };
753
+
754
+ // src/summarize/summarizer.ts
755
+ var TOKEN_BUDGETS = {
756
+ brief: 100,
757
+ summary: 500,
758
+ full: Infinity
759
+ };
760
+ var MIN_SENTENCE_LENGTH = 10;
761
+ var SENTENCE_SCORE_DEFINITION = 2;
762
+ var SENTENCE_SCORE_PROPER_START = 1;
763
+ var SENTENCE_SCORE_MEDIUM_LENGTH = 1;
764
+ var SENTENCE_SCORE_EMPHASIS = 1;
765
+ var SENTENCE_LENGTH_MIN = 50;
766
+ var SENTENCE_LENGTH_MAX = 200;
767
+ var SUMMARY_COMPRESSION_RATIO = 0.3;
768
+ var MIN_SECTION_TOKENS = 20;
769
+ var MIN_SUMMARY_SENTENCES = 2;
770
+ var TOKENS_PER_SENTENCE_ESTIMATE = 30;
771
+ var MIN_TOPIC_LENGTH = 2;
772
+ var MAX_TOPIC_LENGTH = 50;
773
+ var MAX_TOPICS = 10;
774
+ var MIN_PARTIAL_BUDGET = 50;
775
+ var extractKeyPoints = (content, maxSentences) => {
776
+ const sentences = content.replace(/\n+/g, " ").split(/(?<=[.!?])\s+/).filter((s) => s.trim().length > MIN_SENTENCE_LENGTH);
777
+ if (sentences.length <= maxSentences) {
778
+ return sentences;
779
+ }
780
+ const scored = sentences.map((s) => {
781
+ let score = 0;
782
+ if (s.includes(":")) score += SENTENCE_SCORE_DEFINITION;
783
+ if (/^[A-Z]/.test(s)) score += SENTENCE_SCORE_PROPER_START;
784
+ if (s.length > SENTENCE_LENGTH_MIN && s.length < SENTENCE_LENGTH_MAX)
785
+ score += SENTENCE_SCORE_MEDIUM_LENGTH;
786
+ if (/\*\*|`/.test(s)) score += SENTENCE_SCORE_EMPHASIS;
787
+ return { sentence: s, score };
788
+ });
789
+ scored.sort((a, b) => b.score - a.score);
790
+ return scored.slice(0, maxSentences).map((s) => s.sentence);
791
+ };
792
+ var summarizeSection = (section, level) => {
793
+ const originalTokens = section.metadata.tokenCount;
794
+ const children = section.children.map(
795
+ (child) => summarizeSection(child, level)
796
+ );
797
+ const targetTokens = Math.min(
798
+ TOKEN_BUDGETS[level],
799
+ Math.max(originalTokens * SUMMARY_COMPRESSION_RATIO, MIN_SECTION_TOKENS)
800
+ );
801
+ let summary;
802
+ if (level === "full" || originalTokens <= targetTokens) {
803
+ summary = section.plainText;
804
+ } else if (level === "brief") {
805
+ const meta = [];
806
+ if (section.metadata.hasCode) meta.push("code");
807
+ if (section.metadata.hasList) meta.push("list");
808
+ if (section.metadata.hasTable) meta.push("table");
809
+ summary = meta.length > 0 ? `[${meta.join(", ")}]` : "";
810
+ } else {
811
+ const maxSentences = Math.max(
812
+ MIN_SUMMARY_SENTENCES,
813
+ Math.floor(targetTokens / TOKENS_PER_SENTENCE_ESTIMATE)
814
+ );
815
+ const keyPoints = extractKeyPoints(section.plainText, maxSentences);
816
+ if (keyPoints.length > 0) {
817
+ summary = keyPoints.join(" ");
818
+ } else {
819
+ const words = section.plainText.split(/\s+/).slice(0, targetTokens);
820
+ summary = words.join(" ") + (words.length < section.plainText.split(/\s+/).length ? "..." : "");
821
+ }
822
+ }
823
+ const summaryTokens = countTokensApprox(summary);
824
+ return {
825
+ heading: section.heading,
826
+ level: section.level,
827
+ originalTokens,
828
+ summaryTokens,
829
+ summary,
830
+ children,
831
+ hasCode: section.metadata.hasCode,
832
+ hasList: section.metadata.hasList,
833
+ hasTable: section.metadata.hasTable
834
+ };
835
+ };
836
+ var extractTopics = (document) => {
837
+ const topics = /* @__PURE__ */ new Set();
838
+ const processSection = (section) => {
839
+ const cleanHeading = section.heading.replace(/[:#\-_]/g, " ").trim().toLowerCase();
840
+ if (cleanHeading.length > MIN_TOPIC_LENGTH && cleanHeading.length < MAX_TOPIC_LENGTH) {
841
+ topics.add(cleanHeading);
842
+ }
843
+ for (const child of section.children) {
844
+ processSection(child);
845
+ }
846
+ };
847
+ for (const section of document.sections) {
848
+ processSection(section);
849
+ }
850
+ const frontmatter = document.frontmatter;
851
+ if (frontmatter.tags && Array.isArray(frontmatter.tags)) {
852
+ for (const tag of frontmatter.tags) {
853
+ if (typeof tag === "string") {
854
+ topics.add(tag.toLowerCase());
855
+ }
856
+ }
857
+ }
858
+ return Array.from(topics).slice(0, MAX_TOPICS);
859
+ };
860
+ var summarizeDocument = (document, options = {}) => {
861
+ const level = options.level ?? "summary";
862
+ const maxTokens = options.maxTokens ?? TOKEN_BUDGETS[level];
863
+ const allSections = document.sections.map((s) => summarizeSection(s, level));
864
+ const originalTokens = document.metadata.tokenCount;
865
+ let totalSummaryTokens = 0;
866
+ const flatSections = [];
867
+ const flattenWithTokens = (section) => {
868
+ flatSections.push(section);
869
+ totalSummaryTokens += section.summaryTokens;
870
+ for (const child of section.children) {
871
+ flattenWithTokens(child);
872
+ }
873
+ };
874
+ for (const section of allSections) {
875
+ flattenWithTokens(section);
876
+ }
877
+ const topics = extractTopics(document);
878
+ const headerTemplate = `# ${document.title}
879
+ Path: ${document.path}
880
+ Tokens: 9999 (99% reduction from ${document.metadata.tokenCount})
881
+ `;
882
+ const topicsLine = topics.length > 0 ? `
883
+ **Topics:** ${topics.join(", ")}
884
+ ` : "";
885
+ const truncationWarning = "\n\u26A0\uFE0F TRUNCATED: 999 sections omitted to fit token budget";
886
+ const baseOverhead = countTokensApprox(
887
+ headerTemplate + topicsLine + truncationWarning
888
+ );
889
+ const formattingOverhead = Math.ceil(baseOverhead * 1.2) + 20;
890
+ const contentBudget = maxTokens - formattingOverhead;
891
+ let truncated = false;
892
+ let truncatedCount = 0;
893
+ let sections;
894
+ let summaryTokens;
895
+ if (totalSummaryTokens > contentBudget && contentBudget > 0) {
896
+ let tokensUsed = 0;
897
+ const truncateSections = (sectionList) => {
898
+ const result2 = [];
899
+ for (const section of sectionList) {
900
+ const sectionOwnTokens = section.summaryTokens;
901
+ const fitsInBudget = tokensUsed + sectionOwnTokens <= contentBudget;
902
+ if (fitsInBudget) {
903
+ tokensUsed += sectionOwnTokens;
904
+ const truncatedChildren = truncateSections(section.children);
905
+ result2.push({
906
+ ...section,
907
+ children: truncatedChildren
908
+ });
909
+ } else {
910
+ truncatedCount++;
911
+ const rescuedChildren = truncateSections(section.children);
912
+ result2.push(...rescuedChildren);
913
+ }
914
+ }
915
+ return result2;
916
+ };
917
+ sections = truncateSections(allSections);
918
+ summaryTokens = tokensUsed;
919
+ truncated = truncatedCount > 0;
920
+ } else {
921
+ sections = allSections;
922
+ summaryTokens = totalSummaryTokens;
923
+ }
924
+ const compressionRatio = originalTokens > 0 ? 1 - summaryTokens / originalTokens : 0;
925
+ const result = {
926
+ path: document.path,
927
+ title: document.title,
928
+ originalTokens,
929
+ summaryTokens,
930
+ compressionRatio,
931
+ sections,
932
+ keyTopics: topics
933
+ };
934
+ if (truncated) {
935
+ return {
936
+ ...result,
937
+ truncated: true,
938
+ truncatedCount
939
+ };
940
+ }
941
+ return result;
942
+ };
943
+ var summarizeFile = (filePath, options = {}) => Effect5.gen(function* () {
944
+ let document = yield* parseFile(filePath);
945
+ if (options.exclude && options.exclude.length > 0) {
946
+ const { document: filteredDoc } = filterDocumentSections(
947
+ document,
948
+ options.exclude
949
+ );
950
+ document = filteredDoc;
951
+ }
952
+ return summarizeDocument(document, options);
953
+ });
954
+ var assembleContext = (rootPath, sourcePaths, options) => Effect5.gen(function* () {
955
+ const budget = options.budget;
956
+ const level = options.level ?? "summary";
957
+ const excludePatterns = options.exclude ?? [];
958
+ const sources = [];
959
+ const overflow = [];
960
+ let totalTokens = 0;
961
+ const perSourceBudget = Math.floor(budget / sourcePaths.length);
962
+ for (const sourcePath of sourcePaths) {
963
+ const resolvedPath = path2.isAbsolute(sourcePath) ? sourcePath : path2.join(rootPath, sourcePath);
964
+ const summaryResult = yield* summarizeFile(resolvedPath, {
965
+ level,
966
+ maxTokens: perSourceBudget,
967
+ exclude: excludePatterns
968
+ }).pipe(
969
+ Effect5.map((s) => s),
970
+ // Log error for observability before gracefully degrading
971
+ Effect5.tapError(
972
+ (error) => Effect5.logError(`Failed to summarize ${sourcePath}`, error)
973
+ ),
974
+ // Note: catchAll intentional for batch processing - individual file
975
+ // failures add to overflow instead of stopping assembly
976
+ Effect5.catchAll(() => Effect5.succeed(null))
977
+ );
978
+ if (!summaryResult) {
979
+ overflow.push(sourcePath);
980
+ continue;
981
+ }
982
+ const summary = summaryResult;
983
+ const content = formatSummary(summary);
984
+ const tokens = countTokensApprox(content);
985
+ if (totalTokens + tokens <= budget) {
986
+ sources.push({
987
+ path: path2.relative(rootPath, resolvedPath),
988
+ title: summary.title,
989
+ tokens,
990
+ content
991
+ });
992
+ totalTokens += tokens;
993
+ } else {
994
+ const remaining = budget - totalTokens;
995
+ if (remaining > MIN_PARTIAL_BUDGET) {
996
+ const briefSummary = yield* summarizeFile(resolvedPath, {
997
+ level: "brief",
998
+ maxTokens: remaining,
999
+ exclude: excludePatterns
1000
+ }).pipe(
1001
+ Effect5.map((s) => s),
1002
+ // Log error for observability before gracefully degrading
1003
+ Effect5.tapError(
1004
+ (error) => Effect5.logError(
1005
+ `Failed to create brief summary for ${sourcePath}`,
1006
+ error
1007
+ )
1008
+ ),
1009
+ Effect5.catchAll(
1010
+ () => Effect5.succeed(null)
1011
+ )
1012
+ );
1013
+ if (briefSummary) {
1014
+ const briefContent = formatSummary(briefSummary);
1015
+ const briefTokens = countTokensApprox(briefContent);
1016
+ sources.push({
1017
+ path: path2.relative(rootPath, resolvedPath),
1018
+ title: briefSummary.title,
1019
+ tokens: briefTokens,
1020
+ content: briefContent
1021
+ });
1022
+ totalTokens += briefTokens;
1023
+ } else {
1024
+ overflow.push(path2.relative(rootPath, resolvedPath));
1025
+ }
1026
+ } else {
1027
+ overflow.push(path2.relative(rootPath, resolvedPath));
1028
+ }
1029
+ }
1030
+ }
1031
+ return {
1032
+ sources,
1033
+ totalTokens,
1034
+ budget,
1035
+ overflow
1036
+ };
1037
+ });
1038
+
1039
+ // src/embeddings/semantic-search.ts
1040
+ import * as fs5 from "fs/promises";
1041
+ import * as path5 from "path";
1042
+ import { Effect as Effect12 } from "effect";
1043
+
1044
+ // src/embeddings/embedding-namespace.ts
1045
+ import * as fs3 from "fs/promises";
1046
+ import * as path3 from "path";
1047
+ import * as msgpack from "@msgpack/msgpack";
1048
+ import { Effect as Effect6 } from "effect";
1049
+ var EMBEDDINGS_DIR = "embeddings";
1050
+ var ACTIVE_PROVIDER_FILE = "active-provider.json";
1051
+ var VECTOR_INDEX_FILE = "vectors.bin";
1052
+ var VECTOR_META_FILE = "vectors.meta.bin";
1053
+ var EmbeddingNamespaceError = class extends Error {
1054
+ _tag = "EmbeddingNamespaceError";
1055
+ operation;
1056
+ cause;
1057
+ constructor(params) {
1058
+ super(params.message);
1059
+ this.name = "EmbeddingNamespaceError";
1060
+ this.operation = params.operation;
1061
+ this.cause = params.cause;
1062
+ }
1063
+ };
1064
+ var generateNamespace = (provider, model, dimensions) => {
1065
+ const sanitize = (s) => s.replace(/[^a-zA-Z0-9-]/g, "_").toLowerCase();
1066
+ const sanitizedProvider = sanitize(provider);
1067
+ const sanitizedModel = sanitize(model);
1068
+ if (!sanitizedProvider || sanitizedProvider.length === 0) {
1069
+ throw new Error("Provider name cannot be empty");
1070
+ }
1071
+ if (!sanitizedModel || sanitizedModel.length === 0) {
1072
+ throw new Error("Model name cannot be empty");
1073
+ }
1074
+ if (dimensions <= 0 || !Number.isFinite(dimensions)) {
1075
+ throw new Error("Dimensions must be a positive number");
1076
+ }
1077
+ return `${sanitizedProvider}_${sanitizedModel}_${dimensions}`;
1078
+ };
1079
+ var parseNamespace = (namespace) => {
1080
+ if (!namespace || namespace.length === 0) return null;
1081
+ const lastUnderscoreIdx = namespace.lastIndexOf("_");
1082
+ if (lastUnderscoreIdx === -1) return null;
1083
+ const dimensionsStr = namespace.slice(lastUnderscoreIdx + 1);
1084
+ if (!/^\d+$/.test(dimensionsStr)) return null;
1085
+ const dimensions = parseInt(dimensionsStr, 10);
1086
+ if (Number.isNaN(dimensions) || dimensions <= 0) return null;
1087
+ const providerModel = namespace.slice(0, lastUnderscoreIdx);
1088
+ const firstUnderscoreIdx = providerModel.indexOf("_");
1089
+ if (firstUnderscoreIdx === -1) return null;
1090
+ const provider = providerModel.slice(0, firstUnderscoreIdx);
1091
+ const model = providerModel.slice(firstUnderscoreIdx + 1);
1092
+ if (!provider || provider.length === 0) return null;
1093
+ if (!model || model.length === 0) return null;
1094
+ return { provider, model, dimensions };
1095
+ };
1096
+ var getEmbeddingsDir = (rootPath) => path3.join(rootPath, INDEX_DIR, EMBEDDINGS_DIR);
1097
+ var validateNamespace = (namespace) => {
1098
+ if (namespace.includes("/") || namespace.includes("\\") || namespace.includes("..") || namespace.includes("\0")) {
1099
+ throw new Error(
1100
+ `Invalid namespace: contains path separators or traversal sequences`
1101
+ );
1102
+ }
1103
+ };
1104
+ var getNamespaceDir = (rootPath, namespace) => {
1105
+ validateNamespace(namespace);
1106
+ const embeddingsDir = getEmbeddingsDir(rootPath);
1107
+ const resolved = path3.join(embeddingsDir, namespace);
1108
+ const normalizedEmbeddings = path3.resolve(embeddingsDir);
1109
+ const normalizedResolved = path3.resolve(resolved);
1110
+ if (!normalizedResolved.startsWith(normalizedEmbeddings + path3.sep)) {
1111
+ throw new Error(`Invalid namespace: resolves outside embeddings directory`);
1112
+ }
1113
+ return resolved;
1114
+ };
1115
+ var getVectorPath = (rootPath, namespace) => path3.join(getNamespaceDir(rootPath, namespace), VECTOR_INDEX_FILE);
1116
+ var getMetaPath = (rootPath, namespace) => path3.join(getNamespaceDir(rootPath, namespace), VECTOR_META_FILE);
1117
+ var getActiveProviderPath = (rootPath) => path3.join(rootPath, INDEX_DIR, ACTIVE_PROVIDER_FILE);
1118
+ var readActiveProvider = (rootPath) => Effect6.gen(function* () {
1119
+ const filePath = getActiveProviderPath(rootPath);
1120
+ const exists = yield* Effect6.tryPromise({
1121
+ try: async () => {
1122
+ await fs3.access(filePath);
1123
+ return true;
1124
+ },
1125
+ catch: () => new EmbeddingNamespaceError({
1126
+ operation: "readActiveProvider",
1127
+ message: "File not found"
1128
+ })
1129
+ }).pipe(Effect6.catchAll(() => Effect6.succeed(false)));
1130
+ if (!exists) {
1131
+ return null;
1132
+ }
1133
+ const content = yield* Effect6.tryPromise({
1134
+ try: () => fs3.readFile(filePath, "utf-8"),
1135
+ catch: (e) => new EmbeddingNamespaceError({
1136
+ operation: "readActiveProvider",
1137
+ message: `Failed to read active provider: ${e}`,
1138
+ cause: e
1139
+ })
1140
+ });
1141
+ return yield* Effect6.try({
1142
+ try: () => JSON.parse(content),
1143
+ catch: (e) => new EmbeddingNamespaceError({
1144
+ operation: "readActiveProvider",
1145
+ message: `Failed to parse active provider: ${e}`,
1146
+ cause: e
1147
+ })
1148
+ });
1149
+ });
1150
+ var writeActiveProvider = (rootPath, activeProvider) => Effect6.gen(function* () {
1151
+ const filePath = getActiveProviderPath(rootPath);
1152
+ const indexDir = path3.dirname(filePath);
1153
+ yield* Effect6.tryPromise({
1154
+ try: () => fs3.mkdir(indexDir, { recursive: true }),
1155
+ catch: (e) => new EmbeddingNamespaceError({
1156
+ operation: "writeActiveProvider",
1157
+ message: `Failed to create directory: ${e}`,
1158
+ cause: e
1159
+ })
1160
+ });
1161
+ yield* Effect6.tryPromise({
1162
+ try: () => fs3.writeFile(filePath, JSON.stringify(activeProvider, null, 2)),
1163
+ catch: (e) => new EmbeddingNamespaceError({
1164
+ operation: "writeActiveProvider",
1165
+ message: `Failed to write active provider: ${e}`,
1166
+ cause: e
1167
+ })
1168
+ });
1169
+ });
1170
+ var listNamespaces = (rootPath) => Effect6.gen(function* () {
1171
+ const embeddingsDir = getEmbeddingsDir(rootPath);
1172
+ const exists = yield* Effect6.tryPromise({
1173
+ try: async () => {
1174
+ await fs3.access(embeddingsDir);
1175
+ return true;
1176
+ },
1177
+ catch: () => new EmbeddingNamespaceError({
1178
+ operation: "listNamespaces",
1179
+ message: "Directory not found"
1180
+ })
1181
+ }).pipe(Effect6.catchAll(() => Effect6.succeed(false)));
1182
+ if (!exists) {
1183
+ return [];
1184
+ }
1185
+ const activeProvider = yield* readActiveProvider(rootPath).pipe(
1186
+ Effect6.catchAll(() => Effect6.succeed(null))
1187
+ );
1188
+ const entries = yield* Effect6.tryPromise({
1189
+ try: () => fs3.readdir(embeddingsDir, { withFileTypes: true }),
1190
+ catch: (e) => new EmbeddingNamespaceError({
1191
+ operation: "listNamespaces",
1192
+ message: `Failed to read embeddings directory: ${e}`,
1193
+ cause: e
1194
+ })
1195
+ });
1196
+ const namespaces = [];
1197
+ for (const entry of entries) {
1198
+ if (!entry.isDirectory()) continue;
1199
+ const namespace = entry.name;
1200
+ const parsed = parseNamespace(namespace);
1201
+ if (!parsed) continue;
1202
+ const metaPath = getMetaPath(rootPath, namespace);
1203
+ const vectorPath = getVectorPath(rootPath, namespace);
1204
+ const metaExists = yield* Effect6.tryPromise({
1205
+ try: async () => {
1206
+ await fs3.access(metaPath);
1207
+ return true;
1208
+ },
1209
+ catch: () => new EmbeddingNamespaceError({
1210
+ operation: "listNamespaces",
1211
+ message: "Meta not found"
1212
+ })
1213
+ }).pipe(Effect6.catchAll(() => Effect6.succeed(false)));
1214
+ if (!metaExists) continue;
1215
+ const meta = yield* Effect6.tryPromise({
1216
+ try: async () => {
1217
+ const buffer = await fs3.readFile(metaPath);
1218
+ return msgpack.decode(buffer);
1219
+ },
1220
+ catch: (e) => new EmbeddingNamespaceError({
1221
+ operation: "listNamespaces",
1222
+ message: `Failed to read metadata: ${e}`,
1223
+ cause: e
1224
+ })
1225
+ }).pipe(Effect6.catchAll(() => Effect6.succeed(null)));
1226
+ if (!meta) continue;
1227
+ const [metaStats, vectorStats] = yield* Effect6.all([
1228
+ Effect6.tryPromise({
1229
+ try: () => fs3.stat(metaPath),
1230
+ catch: () => new EmbeddingNamespaceError({
1231
+ operation: "listNamespaces",
1232
+ message: "Failed to stat meta"
1233
+ })
1234
+ }).pipe(Effect6.catchAll(() => Effect6.succeed(null))),
1235
+ Effect6.tryPromise({
1236
+ try: () => fs3.stat(vectorPath),
1237
+ catch: () => new EmbeddingNamespaceError({
1238
+ operation: "listNamespaces",
1239
+ message: "Failed to stat vector"
1240
+ })
1241
+ }).pipe(Effect6.catchAll(() => Effect6.succeed(null)))
1242
+ ]);
1243
+ const sizeBytes = (metaStats?.size ?? 0) + (vectorStats?.size ?? 0);
1244
+ const provider = meta.provider || parsed.provider;
1245
+ const model = meta.providerModel || parsed.model;
1246
+ const dimensions = meta.dimensions || parsed.dimensions;
1247
+ namespaces.push({
1248
+ namespace,
1249
+ provider,
1250
+ model,
1251
+ dimensions,
1252
+ vectorCount: Object.keys(meta.entries).length,
1253
+ totalCost: meta.totalCost ?? 0,
1254
+ totalTokens: meta.totalTokens ?? 0,
1255
+ createdAt: meta.createdAt,
1256
+ updatedAt: meta.updatedAt,
1257
+ isActive: activeProvider?.namespace === namespace,
1258
+ sizeBytes
1259
+ });
1260
+ }
1261
+ namespaces.sort(
1262
+ (a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime()
1263
+ );
1264
+ return namespaces;
1265
+ });
1266
+ var switchNamespace = (rootPath, namespaceQuery) => Effect6.gen(function* () {
1267
+ const namespaces = yield* listNamespaces(rootPath);
1268
+ if (namespaces.length === 0) {
1269
+ return yield* Effect6.fail(
1270
+ new EmbeddingNamespaceError({
1271
+ operation: "switchNamespace",
1272
+ message: 'No embedding namespaces found. Run "mdcontext index --embed" first.'
1273
+ })
1274
+ );
1275
+ }
1276
+ const queryLower = namespaceQuery.toLowerCase();
1277
+ let matches = namespaces.filter(
1278
+ (ns) => ns.namespace.toLowerCase() === queryLower || ns.provider.toLowerCase() === queryLower || ns.model.toLowerCase().includes(queryLower) || ns.namespace.toLowerCase().includes(queryLower)
1279
+ );
1280
+ if (matches.length === 0) {
1281
+ return yield* Effect6.fail(
1282
+ new EmbeddingNamespaceError({
1283
+ operation: "switchNamespace",
1284
+ message: `No namespace matching "${namespaceQuery}". Available: ${namespaces.map((n) => n.namespace).join(", ")}`
1285
+ })
1286
+ );
1287
+ }
1288
+ if (matches.length > 1) {
1289
+ const exact = matches.find(
1290
+ (ns) => ns.namespace.toLowerCase() === queryLower || ns.provider.toLowerCase() === queryLower
1291
+ );
1292
+ if (exact) {
1293
+ matches = [exact];
1294
+ } else {
1295
+ return yield* Effect6.fail(
1296
+ new EmbeddingNamespaceError({
1297
+ operation: "switchNamespace",
1298
+ message: `Multiple namespaces match "${namespaceQuery}": ${matches.map((n) => n.namespace).join(", ")}. Be more specific.`
1299
+ })
1300
+ );
1301
+ }
1302
+ }
1303
+ const target = matches[0];
1304
+ yield* writeActiveProvider(rootPath, {
1305
+ namespace: target.namespace,
1306
+ provider: target.provider,
1307
+ model: target.model,
1308
+ dimensions: target.dimensions,
1309
+ activatedAt: (/* @__PURE__ */ new Date()).toISOString()
1310
+ });
1311
+ return { ...target, isActive: true };
1312
+ });
1313
+ var removeNamespace = (rootPath, namespaceQuery, options = {}) => Effect6.gen(function* () {
1314
+ const namespaces = yield* listNamespaces(rootPath);
1315
+ if (namespaces.length === 0) {
1316
+ return yield* Effect6.fail(
1317
+ new EmbeddingNamespaceError({
1318
+ operation: "removeNamespace",
1319
+ message: "No embedding namespaces found."
1320
+ })
1321
+ );
1322
+ }
1323
+ const queryLower = namespaceQuery.toLowerCase();
1324
+ let matches = namespaces.filter(
1325
+ (ns) => ns.namespace.toLowerCase() === queryLower || ns.namespace.toLowerCase().includes(queryLower)
1326
+ );
1327
+ if (matches.length === 0) {
1328
+ return yield* Effect6.fail(
1329
+ new EmbeddingNamespaceError({
1330
+ operation: "removeNamespace",
1331
+ message: `No namespace matching "${namespaceQuery}". Available: ${namespaces.map((n) => n.namespace).join(", ")}`
1332
+ })
1333
+ );
1334
+ }
1335
+ if (matches.length > 1) {
1336
+ const exact = matches.find(
1337
+ (ns) => ns.namespace.toLowerCase() === queryLower
1338
+ );
1339
+ if (exact) {
1340
+ matches = [exact];
1341
+ } else {
1342
+ return yield* Effect6.fail(
1343
+ new EmbeddingNamespaceError({
1344
+ operation: "removeNamespace",
1345
+ message: `Multiple namespaces match "${namespaceQuery}": ${matches.map((n) => n.namespace).join(", ")}. Be more specific.`
1346
+ })
1347
+ );
1348
+ }
1349
+ }
1350
+ const target = matches[0];
1351
+ if (target.isActive && !options.force) {
1352
+ return yield* Effect6.fail(
1353
+ new EmbeddingNamespaceError({
1354
+ operation: "removeNamespace",
1355
+ message: `Cannot remove active namespace "${target.namespace}". Use --force to override or switch to another namespace first.`
1356
+ })
1357
+ );
1358
+ }
1359
+ const namespaceDir = getNamespaceDir(rootPath, target.namespace);
1360
+ yield* Effect6.tryPromise({
1361
+ try: () => fs3.rm(namespaceDir, { recursive: true, force: true }),
1362
+ catch: (e) => new EmbeddingNamespaceError({
1363
+ operation: "removeNamespace",
1364
+ message: `Failed to remove namespace directory: ${e}`,
1365
+ cause: e
1366
+ })
1367
+ });
1368
+ if (target.isActive) {
1369
+ const activeProviderPath = getActiveProviderPath(rootPath);
1370
+ yield* Effect6.tryPromise({
1371
+ try: () => fs3.unlink(activeProviderPath),
1372
+ catch: (e) => new EmbeddingNamespaceError({
1373
+ operation: "removeNamespace",
1374
+ message: `Failed to clear active provider: ${e}`,
1375
+ cause: e
1376
+ })
1377
+ }).pipe(Effect6.catchAll(() => Effect6.succeed(void 0)));
1378
+ }
1379
+ return { removed: target.namespace, wasActive: target.isActive };
1380
+ });
1381
+ var getActiveNamespace = (rootPath) => Effect6.gen(function* () {
1382
+ const active = yield* readActiveProvider(rootPath);
1383
+ if (active) {
1384
+ const namespaceDir = getNamespaceDir(rootPath, active.namespace);
1385
+ const dirExists = yield* Effect6.tryPromise({
1386
+ try: async () => {
1387
+ await fs3.access(namespaceDir);
1388
+ return true;
1389
+ },
1390
+ catch: () => new EmbeddingNamespaceError({
1391
+ operation: "getActiveNamespace",
1392
+ message: "Directory check failed"
1393
+ })
1394
+ }).pipe(Effect6.catchAll(() => Effect6.succeed(false)));
1395
+ if (dirExists) {
1396
+ return active;
1397
+ }
1398
+ }
1399
+ const namespaces = yield* listNamespaces(rootPath);
1400
+ if (namespaces.length === 0) {
1401
+ return null;
1402
+ }
1403
+ const mostRecent = namespaces[0];
1404
+ yield* writeActiveProvider(rootPath, {
1405
+ namespace: mostRecent.namespace,
1406
+ provider: mostRecent.provider,
1407
+ model: mostRecent.model,
1408
+ dimensions: mostRecent.dimensions,
1409
+ activatedAt: (/* @__PURE__ */ new Date()).toISOString()
1410
+ });
1411
+ return {
1412
+ namespace: mostRecent.namespace,
1413
+ provider: mostRecent.provider,
1414
+ model: mostRecent.model,
1415
+ dimensions: mostRecent.dimensions,
1416
+ activatedAt: (/* @__PURE__ */ new Date()).toISOString()
1417
+ };
1418
+ });
1419
+
1420
+ // src/embeddings/hyde.ts
1421
+ import { Effect as Effect7, Redacted } from "effect";
1422
+ import OpenAI from "openai";
1423
+ var DEFAULT_MODEL = "gpt-4o-mini";
1424
+ var DEFAULT_MAX_TOKENS = 256;
1425
+ var DEFAULT_TEMPERATURE = 0.3;
1426
+ var DEFAULT_SYSTEM_PROMPT = `You are a technical documentation assistant. Given a user's question, write a short, factual passage that would appear in documentation answering this question.
1427
+
1428
+ Guidelines:
1429
+ - Write 2-4 concise paragraphs
1430
+ - Use technical but accessible language
1431
+ - Include specific details, code examples, or configuration options where relevant
1432
+ - Focus on directly answering the question
1433
+ - Do not include greetings, preambles, or meta-commentary
1434
+ - Write as if this is an excerpt from existing documentation`;
1435
+ var LLM_PRICING = {
1436
+ "gpt-4o-mini": { input: 0.15, output: 0.6 },
1437
+ "gpt-4o": { input: 2.5, output: 10 },
1438
+ "gpt-4-turbo": { input: 10, output: 30 },
1439
+ "gpt-3.5-turbo": { input: 0.5, output: 1.5 }
1440
+ };
1441
+ var generateHypotheticalDocument = (query, options = {}) => Effect7.gen(function* () {
1442
+ const rawApiKey = options.apiKey ?? process.env.OPENAI_API_KEY;
1443
+ if (!rawApiKey) {
1444
+ return yield* Effect7.fail(
1445
+ new ApiKeyMissingError({
1446
+ provider: "OpenAI",
1447
+ envVar: "OPENAI_API_KEY"
1448
+ })
1449
+ );
1450
+ }
1451
+ const redactedApiKey = Redacted.isRedacted(rawApiKey) ? rawApiKey : Redacted.make(rawApiKey);
1452
+ const client = new OpenAI({
1453
+ apiKey: Redacted.value(redactedApiKey),
1454
+ // Only expose when creating client
1455
+ baseURL: options.baseURL
1456
+ });
1457
+ const model = options.model ?? DEFAULT_MODEL;
1458
+ const maxTokens = options.maxTokens ?? DEFAULT_MAX_TOKENS;
1459
+ const temperature = options.temperature ?? DEFAULT_TEMPERATURE;
1460
+ const systemPrompt = options.systemPrompt ?? DEFAULT_SYSTEM_PROMPT;
1461
+ const response = yield* Effect7.tryPromise({
1462
+ try: async () => client.chat.completions.create({
1463
+ model,
1464
+ messages: [
1465
+ { role: "system", content: systemPrompt },
1466
+ { role: "user", content: query }
1467
+ ],
1468
+ max_tokens: maxTokens,
1469
+ temperature
1470
+ }),
1471
+ catch: (error) => new EmbeddingError({
1472
+ reason: classifyLLMError(error),
1473
+ message: error instanceof Error ? error.message : String(error),
1474
+ provider: "openai",
1475
+ cause: error
1476
+ })
1477
+ });
1478
+ const content = response.choices[0]?.message?.content ?? "";
1479
+ const inputTokens = response.usage?.prompt_tokens ?? 0;
1480
+ const outputTokens = response.usage?.completion_tokens ?? 0;
1481
+ const totalTokens = inputTokens + outputTokens;
1482
+ const pricing = LLM_PRICING[model] ?? LLM_PRICING["gpt-4o-mini"];
1483
+ const cost = inputTokens / 1e6 * pricing.input + outputTokens / 1e6 * pricing.output;
1484
+ return {
1485
+ hypotheticalDocument: content,
1486
+ originalQuery: query,
1487
+ model,
1488
+ tokensUsed: totalTokens,
1489
+ cost
1490
+ };
1491
+ });
1492
+ var classifyLLMError = (error) => {
1493
+ if (error instanceof OpenAI.RateLimitError) {
1494
+ return "RateLimit";
1495
+ }
1496
+ if (error instanceof OpenAI.BadRequestError) {
1497
+ const msg2 = (error.message || "").toLowerCase();
1498
+ if (msg2.includes("model")) return "ModelError";
1499
+ }
1500
+ if (error instanceof OpenAI.APIConnectionError) {
1501
+ return "Network";
1502
+ }
1503
+ if (!(error instanceof Error)) return "Unknown";
1504
+ const msg = error.message.toLowerCase();
1505
+ if (msg.includes("429") || msg.includes("rate limit")) return "RateLimit";
1506
+ if (msg.includes("quota") || msg.includes("billing")) return "QuotaExceeded";
1507
+ if (msg.includes("econnrefused") || msg.includes("network")) return "Network";
1508
+ if (msg.includes("model") && msg.includes("not found")) return "ModelError";
1509
+ return "Unknown";
1510
+ };
1511
+
1512
+ // src/embeddings/openai-provider.ts
1513
+ import { Effect as Effect8, Redacted as Redacted2 } from "effect";
1514
+ import OpenAI2 from "openai";
1515
+
1516
+ // src/embeddings/pricing.json
1517
+ var pricing_default = {
1518
+ $schema: "https://json-schema.org/draft/2020-12/schema",
1519
+ description: "Embedding model pricing data. Update quarterly from provider pricing pages.",
1520
+ lastUpdated: "2026-01",
1521
+ sources: {
1522
+ openai: "https://platform.openai.com/docs/pricing",
1523
+ voyage: "https://docs.voyageai.com/docs/pricing"
1524
+ },
1525
+ openai: {
1526
+ "text-embedding-3-small": 0.02,
1527
+ "text-embedding-3-large": 0.13,
1528
+ "text-embedding-ada-002": 0.1
1529
+ },
1530
+ voyage: {
1531
+ "voyage-3.5-lite": 0.02,
1532
+ "voyage-3": 0.06,
1533
+ "voyage-code-3": 0.18,
1534
+ "voyage-2": 0.1,
1535
+ "voyage-large-2": 0.12,
1536
+ "voyage-code-2": 0.12
1537
+ }
1538
+ };
1539
+
1540
+ // src/embeddings/provider-constants.ts
1541
+ var MODEL_DIMENSIONS = {
1542
+ // OpenAI models (support MRL dimension reduction)
1543
+ "text-embedding-3-small": 1536,
1544
+ // Native: 1536, supports reduction
1545
+ "text-embedding-3-large": 3072,
1546
+ // Native: 3072, supports reduction
1547
+ "text-embedding-ada-002": 1536,
1548
+ // Native: 1536, does NOT support reduction
1549
+ // Ollama models (fixed native dimensions)
1550
+ "nomic-embed-text": 768,
1551
+ "mxbai-embed-large": 1024,
1552
+ "bge-m3": 1024,
1553
+ "all-minilm": 384,
1554
+ "snowflake-arctic-embed": 1024,
1555
+ // Voyage AI models (fixed native dimensions)
1556
+ "voyage-3.5-lite": 1024,
1557
+ // Best value: $0.02/1M tokens
1558
+ "voyage-3": 1024,
1559
+ // Higher quality: $0.06/1M tokens
1560
+ "voyage-code-3": 1024,
1561
+ // Code-optimized: $0.18/1M tokens
1562
+ "voyage-2": 1024,
1563
+ "voyage-large-2": 1536,
1564
+ "voyage-code-2": 1536
1565
+ };
1566
+ var MATRYOSHKA_MODELS = /* @__PURE__ */ new Set([
1567
+ "text-embedding-3-small",
1568
+ "text-embedding-3-large"
1569
+ ]);
1570
+ var supportsMatryoshka = (model) => MATRYOSHKA_MODELS.has(model);
1571
+ var getRecommendedDimensions = (model) => {
1572
+ if (supportsMatryoshka(model)) {
1573
+ return 512;
1574
+ }
1575
+ return MODEL_DIMENSIONS[model];
1576
+ };
1577
+ var validateModelDimensions = (model, dimensions) => {
1578
+ const nativeDims = MODEL_DIMENSIONS[model];
1579
+ if (nativeDims === void 0) {
1580
+ return { isValid: true };
1581
+ }
1582
+ if (dimensions > nativeDims) {
1583
+ return {
1584
+ isValid: false,
1585
+ warning: `Model '${model}' has ${nativeDims} native dimensions, cannot use ${dimensions}`
1586
+ };
1587
+ }
1588
+ if (!supportsMatryoshka(model) && dimensions !== nativeDims) {
1589
+ return {
1590
+ isValid: false,
1591
+ warning: `Model '${model}' does not support dimension reduction, must use ${nativeDims}`
1592
+ };
1593
+ }
1594
+ return { isValid: true };
1595
+ };
1596
+ var PROVIDER_BASE_URLS = {
1597
+ openai: void 0,
1598
+ // Use OpenAI SDK default
1599
+ ollama: "http://localhost:11434/v1",
1600
+ "lm-studio": "http://localhost:1234/v1",
1601
+ openrouter: "https://openrouter.ai/api/v1",
1602
+ voyage: "https://api.voyageai.com/v1"
1603
+ // Native API, handled by VoyageProvider
1604
+ };
1605
+ var extractPortFromUrl = (url) => {
1606
+ const match = url.match(/:(\d+)\//);
1607
+ if (!match?.[1]) return void 0;
1608
+ return parseInt(match[1], 10);
1609
+ };
1610
+ var PROVIDER_PORTS = (() => {
1611
+ const ports = {};
1612
+ for (const [provider, url] of Object.entries(PROVIDER_BASE_URLS)) {
1613
+ if (url) {
1614
+ const port = extractPortFromUrl(url);
1615
+ if (port) ports[provider] = port;
1616
+ }
1617
+ }
1618
+ return ports;
1619
+ })();
1620
+ var inferProviderFromUrl = (baseURL) => {
1621
+ if (!baseURL) return "openai";
1622
+ for (const [provider, providerUrl] of Object.entries(PROVIDER_BASE_URLS)) {
1623
+ if (providerUrl && baseURL.includes(providerUrl.replace("/v1", ""))) {
1624
+ return provider;
1625
+ }
1626
+ }
1627
+ if (baseURL.includes("openrouter")) return "openrouter";
1628
+ return "openai";
1629
+ };
1630
+
1631
+ // src/embeddings/openai-provider.ts
1632
+ var PRICING_DATA = {
1633
+ /** Last update date in YYYY-MM format */
1634
+ lastUpdated: pricing_default.lastUpdated,
1635
+ /** Source URL for verification */
1636
+ source: pricing_default.sources.openai,
1637
+ /** Prices per 1M tokens by model */
1638
+ prices: pricing_default.openai
1639
+ };
1640
+ var checkPricingFreshness = () => {
1641
+ const [year, month] = PRICING_DATA.lastUpdated.split("-").map(Number);
1642
+ if (!year || !month) return null;
1643
+ const lastUpdated = new Date(year, month - 1, 1);
1644
+ const now = /* @__PURE__ */ new Date();
1645
+ const daysSince = Math.floor(
1646
+ (now.getTime() - lastUpdated.getTime()) / (1e3 * 60 * 60 * 24)
1647
+ );
1648
+ if (daysSince > 90) {
1649
+ return `Pricing data is ${daysSince} days old. May not reflect current rates.`;
1650
+ }
1651
+ return null;
1652
+ };
1653
+ var getPricingDate = () => PRICING_DATA.lastUpdated;
1654
+ var OpenAIProvider = class _OpenAIProvider {
1655
+ name;
1656
+ dimensions;
1657
+ /** Provider name for error context */
1658
+ providerName;
1659
+ /** Model name */
1660
+ model;
1661
+ /** Base URL for API requests */
1662
+ baseURL;
1663
+ client;
1664
+ batchSize;
1665
+ constructor(apiKey, options = {}) {
1666
+ this.baseURL = options.baseURL;
1667
+ this.client = new OpenAI2({
1668
+ apiKey: Redacted2.value(apiKey),
1669
+ baseURL: options.baseURL,
1670
+ timeout: options.timeout ?? 3e4,
1671
+ maxRetries: 2
1672
+ });
1673
+ this.model = options.model ?? "text-embedding-3-small";
1674
+ this.batchSize = options.batchSize ?? 100;
1675
+ this.providerName = options.providerName ?? this.inferProviderName(options.baseURL);
1676
+ this.name = `${this.providerName}:${this.model}`;
1677
+ const recommendedDims = getRecommendedDimensions(this.model);
1678
+ this.dimensions = options.dimensions ?? recommendedDims ?? 512;
1679
+ }
1680
+ /**
1681
+ * Infer the provider name from the baseURL.
1682
+ * Delegates to centralized inferProviderFromUrl for single source of truth.
1683
+ */
1684
+ inferProviderName(baseURL) {
1685
+ return inferProviderFromUrl(baseURL);
1686
+ }
1687
+ /**
1688
+ * Create an OpenAI provider instance.
1689
+ * Returns an Effect that fails with ApiKeyMissingError if no API key is available.
1690
+ *
1691
+ * API keys are handled securely using Effect's Redacted type to prevent
1692
+ * accidental logging of sensitive values.
1693
+ */
1694
+ static create(options = {}) {
1695
+ const isOpenRouter = options.baseURL?.includes("openrouter") || options.providerName === "openrouter";
1696
+ const resolveApiKey = () => {
1697
+ if (options.apiKey !== void 0) {
1698
+ return options.apiKey;
1699
+ }
1700
+ return (isOpenRouter ? process.env.OPENROUTER_API_KEY : void 0) ?? process.env.OPENAI_API_KEY;
1701
+ };
1702
+ const rawApiKey = resolveApiKey();
1703
+ if (!rawApiKey) {
1704
+ return Effect8.fail(
1705
+ new ApiKeyMissingError({
1706
+ provider: isOpenRouter ? "OpenRouter" : "OpenAI",
1707
+ envVar: isOpenRouter ? "OPENROUTER_API_KEY" : "OPENAI_API_KEY"
1708
+ })
1709
+ );
1710
+ }
1711
+ const redactedApiKey = Redacted2.isRedacted(rawApiKey) ? rawApiKey : Redacted2.make(rawApiKey);
1712
+ const apiKeyValue = Redacted2.value(redactedApiKey);
1713
+ const shouldWarnOpenRouter = isOpenRouter && apiKeyValue.startsWith("sk-") && !apiKeyValue.startsWith("sk-or-");
1714
+ const model = options.model ?? "text-embedding-3-small";
1715
+ const dimensionValidation = options.dimensions ? validateModelDimensions(model, options.dimensions) : { isValid: true };
1716
+ return Effect8.succeed(new _OpenAIProvider(redactedApiKey, options)).pipe(
1717
+ shouldWarnOpenRouter ? Effect8.tap(
1718
+ () => Effect8.logWarning(
1719
+ '\u26A0\uFE0F Using OpenAI key format with OpenRouter. Consider setting OPENROUTER_API_KEY with a key starting with "sk-or-"'
1720
+ )
1721
+ ) : (self) => self,
1722
+ // Warn about invalid dimension configuration
1723
+ dimensionValidation.warning ? Effect8.tap(
1724
+ () => Effect8.logWarning(`\u26A0\uFE0F ${dimensionValidation.warning}`)
1725
+ ) : (self) => self
1726
+ );
1727
+ }
1728
+ async embed(texts, options) {
1729
+ if (texts.length === 0) {
1730
+ return { embeddings: [], tokensUsed: 0, cost: 0 };
1731
+ }
1732
+ const allEmbeddings = [];
1733
+ let totalTokens = 0;
1734
+ const totalBatches = Math.ceil(texts.length / this.batchSize);
1735
+ try {
1736
+ for (let i = 0; i < texts.length; i += this.batchSize) {
1737
+ const batch = texts.slice(i, i + this.batchSize);
1738
+ const batchIndex = Math.floor(i / this.batchSize);
1739
+ const embedParams = {
1740
+ model: this.model,
1741
+ input: batch
1742
+ };
1743
+ if (supportsMatryoshka(this.model)) {
1744
+ embedParams.dimensions = this.dimensions;
1745
+ }
1746
+ const response = await this.client.embeddings.create(embedParams);
1747
+ for (const item of response.data) {
1748
+ allEmbeddings.push(item.embedding);
1749
+ }
1750
+ totalTokens += response.usage?.total_tokens ?? 0;
1751
+ if (options?.onBatchProgress) {
1752
+ options.onBatchProgress({
1753
+ batchIndex: batchIndex + 1,
1754
+ totalBatches,
1755
+ processedTexts: Math.min(i + this.batchSize, texts.length),
1756
+ totalTexts: texts.length
1757
+ });
1758
+ }
1759
+ }
1760
+ } catch (error) {
1761
+ if (error instanceof OpenAI2.AuthenticationError) {
1762
+ throw new ApiKeyInvalidError({
1763
+ provider: this.providerName,
1764
+ details: error.message
1765
+ });
1766
+ }
1767
+ throw new EmbeddingError({
1768
+ reason: this.classifyError(error),
1769
+ message: error instanceof Error ? error.message : String(error),
1770
+ provider: this.providerName,
1771
+ cause: error
1772
+ });
1773
+ }
1774
+ const pricePerMillion = this.providerName === "openai" || this.providerName === "openrouter" ? PRICING_DATA.prices[this.model] ?? 0.02 : 0;
1775
+ const cost = totalTokens / 1e6 * pricePerMillion;
1776
+ return {
1777
+ embeddings: allEmbeddings,
1778
+ tokensUsed: totalTokens,
1779
+ cost
1780
+ };
1781
+ }
1782
+ /**
1783
+ * Classify an error into a known category for better error handling.
1784
+ * Uses OpenAI SDK error types where available, falls back to string matching
1785
+ * for non-OpenAI providers (Ollama, LM Studio, OpenRouter).
1786
+ */
1787
+ classifyError(error) {
1788
+ if (error instanceof OpenAI2.RateLimitError) {
1789
+ return "RateLimit";
1790
+ }
1791
+ if (error instanceof OpenAI2.BadRequestError) {
1792
+ const msg2 = error.message.toLowerCase();
1793
+ if (msg2.includes("model")) return "ModelError";
1794
+ }
1795
+ if (error instanceof OpenAI2.APIConnectionError) {
1796
+ return "Network";
1797
+ }
1798
+ if (!(error instanceof Error)) return "Unknown";
1799
+ const msg = error.message.toLowerCase();
1800
+ if (msg.includes("429") || msg.includes("rate limit") || msg.includes("too many requests")) {
1801
+ return "RateLimit";
1802
+ }
1803
+ if (msg.includes("quota") || msg.includes("insufficient") || msg.includes("billing")) {
1804
+ return "QuotaExceeded";
1805
+ }
1806
+ if (msg.includes("econnrefused") || msg.includes("timeout") || msg.includes("network") || msg.includes("enotfound") || msg.includes("connection")) {
1807
+ return "Network";
1808
+ }
1809
+ if (msg.includes("model") && (msg.includes("not found") || msg.includes("not exist") || msg.includes("invalid"))) {
1810
+ return "ModelError";
1811
+ }
1812
+ return "Unknown";
1813
+ }
1814
+ };
1815
+ var createOpenAIProvider = (options) => OpenAIProvider.create(options);
1816
+ var wrapEmbedding = (embedPromise, providerName = "openai") => Effect8.tryPromise({
1817
+ try: () => embedPromise,
1818
+ catch: (e) => {
1819
+ if (e instanceof ApiKeyInvalidError) {
1820
+ return e;
1821
+ }
1822
+ return new EmbeddingError({
1823
+ reason: "Unknown",
1824
+ message: e instanceof Error ? e.message : String(e),
1825
+ provider: providerName,
1826
+ cause: e
1827
+ });
1828
+ }
1829
+ });
1830
+
1831
+ // src/embeddings/provider-factory.ts
1832
+ import { Effect as Effect10, Option, Redacted as Redacted4 } from "effect";
1833
+
1834
+ // src/embeddings/voyage-provider.ts
1835
+ import { Effect as Effect9, Redacted as Redacted3 } from "effect";
1836
+ var VOYAGE_API_BASE = "https://api.voyageai.com/v1";
1837
+ var voyagePricing = pricing_default.voyage;
1838
+ var VOYAGE_MODELS = {
1839
+ "voyage-3.5-lite": {
1840
+ dimensions: 1024,
1841
+ pricePerMillion: voyagePricing["voyage-3.5-lite"] ?? 0.02
1842
+ },
1843
+ "voyage-3": {
1844
+ dimensions: 1024,
1845
+ pricePerMillion: voyagePricing["voyage-3"] ?? 0.06
1846
+ },
1847
+ "voyage-code-3": {
1848
+ dimensions: 1024,
1849
+ pricePerMillion: voyagePricing["voyage-code-3"] ?? 0.18
1850
+ },
1851
+ // Legacy models
1852
+ "voyage-2": {
1853
+ dimensions: 1024,
1854
+ pricePerMillion: voyagePricing["voyage-2"] ?? 0.1
1855
+ },
1856
+ "voyage-large-2": {
1857
+ dimensions: 1536,
1858
+ pricePerMillion: voyagePricing["voyage-large-2"] ?? 0.12
1859
+ },
1860
+ "voyage-code-2": {
1861
+ dimensions: 1536,
1862
+ pricePerMillion: voyagePricing["voyage-code-2"] ?? 0.12
1863
+ }
1864
+ };
1865
+ var DEFAULT_VOYAGE_MODEL = "voyage-3.5-lite";
1866
+ var VoyageProvider = class _VoyageProvider {
1867
+ name;
1868
+ dimensions;
1869
+ model;
1870
+ baseURL = VOYAGE_API_BASE;
1871
+ providerName = "voyage";
1872
+ apiKey;
1873
+ batchSize;
1874
+ timeout;
1875
+ constructor(apiKey, options = {}) {
1876
+ this.apiKey = apiKey;
1877
+ this.model = options.model ?? DEFAULT_VOYAGE_MODEL;
1878
+ this.batchSize = options.batchSize ?? 128;
1879
+ this.timeout = options.timeout ?? 3e4;
1880
+ const modelSpec = VOYAGE_MODELS[this.model];
1881
+ this.dimensions = modelSpec?.dimensions ?? 1024;
1882
+ this.name = `voyage:${this.model}`;
1883
+ }
1884
+ /**
1885
+ * Create a Voyage provider instance.
1886
+ * Returns an Effect that fails with ApiKeyMissingError if no API key is available.
1887
+ *
1888
+ * API keys are handled securely using Effect's Redacted type to prevent
1889
+ * accidental logging of sensitive values.
1890
+ */
1891
+ static create(options = {}) {
1892
+ const rawApiKey = options.apiKey ?? process.env.VOYAGE_API_KEY;
1893
+ if (!rawApiKey) {
1894
+ return Effect9.fail(
1895
+ new ApiKeyMissingError({
1896
+ provider: "Voyage AI",
1897
+ envVar: "VOYAGE_API_KEY"
1898
+ })
1899
+ );
1900
+ }
1901
+ const redactedApiKey = Redacted3.isRedacted(rawApiKey) ? rawApiKey : Redacted3.make(rawApiKey);
1902
+ return Effect9.succeed(new _VoyageProvider(redactedApiKey, options));
1903
+ }
1904
+ async embed(texts, options) {
1905
+ if (texts.length === 0) {
1906
+ return { embeddings: [], tokensUsed: 0, cost: 0 };
1907
+ }
1908
+ const allEmbeddings = [];
1909
+ let totalTokens = 0;
1910
+ const totalBatches = Math.ceil(texts.length / this.batchSize);
1911
+ try {
1912
+ for (let i = 0; i < texts.length; i += this.batchSize) {
1913
+ const batch = texts.slice(i, i + this.batchSize);
1914
+ const batchIndex = Math.floor(i / this.batchSize);
1915
+ const controller = new AbortController();
1916
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
1917
+ let response;
1918
+ try {
1919
+ response = await fetch(`${VOYAGE_API_BASE}/embeddings`, {
1920
+ method: "POST",
1921
+ headers: {
1922
+ Authorization: `Bearer ${Redacted3.value(this.apiKey)}`,
1923
+ "Content-Type": "application/json"
1924
+ },
1925
+ body: JSON.stringify({
1926
+ model: this.model,
1927
+ input: batch,
1928
+ input_type: "document"
1929
+ // 'document' for indexing, 'query' for searching
1930
+ }),
1931
+ signal: controller.signal
1932
+ });
1933
+ } finally {
1934
+ clearTimeout(timeoutId);
1935
+ }
1936
+ if (!response.ok) {
1937
+ const errorText = await response.text();
1938
+ if (response.status === 401) {
1939
+ throw new ApiKeyInvalidError({
1940
+ provider: "Voyage AI",
1941
+ details: errorText
1942
+ });
1943
+ }
1944
+ throw new EmbeddingError({
1945
+ reason: this.classifyHttpError(response.status, errorText),
1946
+ message: `Voyage API error: ${response.status} - ${errorText}`,
1947
+ provider: "voyage"
1948
+ });
1949
+ }
1950
+ const data = await response.json();
1951
+ for (const item of data.data) {
1952
+ allEmbeddings.push(item.embedding);
1953
+ }
1954
+ totalTokens += data.usage?.total_tokens ?? 0;
1955
+ if (options?.onBatchProgress) {
1956
+ options.onBatchProgress({
1957
+ batchIndex: batchIndex + 1,
1958
+ totalBatches,
1959
+ processedTexts: Math.min(i + this.batchSize, texts.length),
1960
+ totalTexts: texts.length
1961
+ });
1962
+ }
1963
+ }
1964
+ } catch (error) {
1965
+ if (error instanceof ApiKeyInvalidError || error instanceof EmbeddingError) {
1966
+ throw error;
1967
+ }
1968
+ throw new EmbeddingError({
1969
+ reason: this.classifyError(error),
1970
+ message: error instanceof Error ? error.message : String(error),
1971
+ provider: "voyage",
1972
+ cause: error
1973
+ });
1974
+ }
1975
+ const pricePerMillion = VOYAGE_MODELS[this.model]?.pricePerMillion ?? 0.02;
1976
+ const cost = totalTokens / 1e6 * pricePerMillion;
1977
+ return {
1978
+ embeddings: allEmbeddings,
1979
+ tokensUsed: totalTokens,
1980
+ cost
1981
+ };
1982
+ }
1983
+ classifyHttpError(status, _message) {
1984
+ if (status === 429) return "RateLimit";
1985
+ if (status === 402) return "QuotaExceeded";
1986
+ if (status === 400) return "ModelError";
1987
+ return "Unknown";
1988
+ }
1989
+ classifyError(error) {
1990
+ if (!(error instanceof Error)) return "Unknown";
1991
+ const msg = error.message.toLowerCase();
1992
+ if (error.name === "AbortError" || msg.includes("aborted")) return "Network";
1993
+ if (msg.includes("rate limit") || msg.includes("429")) return "RateLimit";
1994
+ if (msg.includes("quota") || msg.includes("billing")) return "QuotaExceeded";
1995
+ if (msg.includes("econnrefused") || msg.includes("timeout") || msg.includes("network"))
1996
+ return "Network";
1997
+ if (msg.includes("model") && msg.includes("not found")) return "ModelError";
1998
+ return "Unknown";
1999
+ }
2000
+ };
2001
+ var createVoyageProvider = (options) => VoyageProvider.create(options);
2002
+
2003
+ // src/embeddings/provider-factory.ts
2004
+ var getProviderBaseURL = (provider, configBaseURL) => {
2005
+ if (Option.isSome(configBaseURL)) {
2006
+ return configBaseURL.value;
2007
+ }
2008
+ return PROVIDER_BASE_URLS[provider];
2009
+ };
2010
+ var normalizeBaseURL = (baseURL) => {
2011
+ if (baseURL === void 0) {
2012
+ return Option.none();
2013
+ }
2014
+ if (typeof baseURL === "string") {
2015
+ return Option.some(baseURL);
2016
+ }
2017
+ return baseURL;
2018
+ };
2019
+ var normalizeApiKey = (apiKey) => {
2020
+ if (apiKey === void 0) {
2021
+ return void 0;
2022
+ }
2023
+ if (Redacted4.isRedacted(apiKey)) {
2024
+ return apiKey;
2025
+ }
2026
+ if (typeof apiKey === "string") {
2027
+ return apiKey;
2028
+ }
2029
+ return Option.isSome(apiKey) ? apiKey.value : void 0;
2030
+ };
2031
+ var createEmbeddingProviderDirect = (config) => Effect10.gen(function* () {
2032
+ const provider = config.provider;
2033
+ const baseURL = getProviderBaseURL(
2034
+ provider,
2035
+ normalizeBaseURL(config.baseURL)
2036
+ );
2037
+ if (provider === "voyage") {
2038
+ return yield* createVoyageProvider({
2039
+ model: config.model,
2040
+ batchSize: config.batchSize,
2041
+ apiKey: normalizeApiKey(config.apiKey),
2042
+ timeout: config.timeout
2043
+ });
2044
+ }
2045
+ return yield* createOpenAIProvider({
2046
+ model: config.model,
2047
+ dimensions: config.dimensions,
2048
+ batchSize: config.batchSize,
2049
+ baseURL,
2050
+ apiKey: normalizeApiKey(config.apiKey),
2051
+ timeout: config.timeout
2052
+ });
2053
+ });
2054
+
2055
+ // src/embeddings/types.ts
2056
+ var hasProviderMetadata = (provider) => {
2057
+ return "model" in provider && typeof provider.model === "string";
2058
+ };
2059
+ var QUALITY_EF_SEARCH = {
2060
+ fast: 64,
2061
+ balanced: 100,
2062
+ thorough: 256
2063
+ };
2064
+ var HEADING_BOOST_FACTOR = 0.05;
2065
+ var FILE_IMPORTANCE_BOOST = 0.03;
2066
+ var IMPORTANT_FILE_PATTERNS = [
2067
+ /^readme\.md$/i,
2068
+ // Root README
2069
+ /\/readme\.md$/i,
2070
+ // Nested README
2071
+ /^index\.md$/i,
2072
+ // Index files
2073
+ /\/index\.md$/i,
2074
+ /^getting-?started/i,
2075
+ // Getting started guides
2076
+ /\/getting-?started/i,
2077
+ /^introduction/i,
2078
+ // Introductions
2079
+ /\/introduction/i,
2080
+ /^overview/i,
2081
+ // Overviews
2082
+ /\/overview/i,
2083
+ /^quickstart/i,
2084
+ // Quickstart guides
2085
+ /\/quickstart/i,
2086
+ /^changelog\.md$/i,
2087
+ // Changelogs (useful for "what changed" queries)
2088
+ /\/changelog\.md$/i
2089
+ ];
2090
+ var calculateFileImportanceBoost = (documentPath) => {
2091
+ const isImportant = IMPORTANT_FILE_PATTERNS.some(
2092
+ (pattern) => pattern.test(documentPath)
2093
+ );
2094
+ return isImportant ? FILE_IMPORTANCE_BOOST : 0;
2095
+ };
2096
+ var calculateHeadingBoost = (heading, query) => {
2097
+ const queryTerms = query.toLowerCase().split(/\s+/).filter(Boolean);
2098
+ if (queryTerms.length === 0) return 0;
2099
+ const headingLower = heading.toLowerCase();
2100
+ const matchCount = queryTerms.filter(
2101
+ (term) => headingLower.includes(term)
2102
+ ).length;
2103
+ return matchCount * HEADING_BOOST_FACTOR;
2104
+ };
2105
+ var preprocessQuery = (query) => {
2106
+ return query.toLowerCase().replace(/[^\w\s]/g, " ").replace(/\s+/g, " ").trim();
2107
+ };
2108
+
2109
+ // src/embeddings/vector-store.ts
2110
+ import * as fs4 from "fs/promises";
2111
+ import * as path4 from "path";
2112
+ import * as msgpack2 from "@msgpack/msgpack";
2113
+ import { Effect as Effect11 } from "effect";
2114
+ import HierarchicalNSW from "hnswlib-node";
2115
+ var VECTOR_INDEX_FILE2 = "vectors.bin";
2116
+ var VECTOR_META_FILE2 = "vectors.meta.bin";
2117
+ var INDEX_VERSION = 1;
2118
+ var HnswVectorStore = class {
2119
+ rootPath;
2120
+ dimensions;
2121
+ index = null;
2122
+ entries = /* @__PURE__ */ new Map();
2123
+ idToIndex = /* @__PURE__ */ new Map();
2124
+ nextIndex = 0;
2125
+ provider = "unknown";
2126
+ providerModel = void 0;
2127
+ providerBaseURL = void 0;
2128
+ totalCost = 0;
2129
+ totalTokens = 0;
2130
+ // HNSW build parameters
2131
+ hnswM;
2132
+ hnswEfConstruction;
2133
+ // Namespace support - when set, uses namespaced storage paths
2134
+ namespace = void 0;
2135
+ constructor(rootPath, dimensions, hnswOptions) {
2136
+ this.rootPath = path4.resolve(rootPath);
2137
+ this.dimensions = dimensions;
2138
+ this.hnswM = hnswOptions?.m ?? 16;
2139
+ this.hnswEfConstruction = hnswOptions?.efConstruction ?? 200;
2140
+ }
2141
+ /**
2142
+ * Set the namespace for this vector store.
2143
+ * When set, all storage operations use the namespaced path.
2144
+ */
2145
+ setNamespace(namespace) {
2146
+ this.namespace = namespace;
2147
+ }
2148
+ /**
2149
+ * Get the current namespace (if any).
2150
+ */
2151
+ getNamespace() {
2152
+ return this.namespace;
2153
+ }
2154
+ /**
2155
+ * Get the index directory path.
2156
+ * Returns namespaced path if namespace is set, otherwise legacy path.
2157
+ */
2158
+ getIndexDir() {
2159
+ if (this.namespace) {
2160
+ return getNamespaceDir(this.rootPath, this.namespace);
2161
+ }
2162
+ return path4.join(this.rootPath, INDEX_DIR);
2163
+ }
2164
+ /**
2165
+ * Get the vector index file path.
2166
+ */
2167
+ getVectorPath() {
2168
+ if (this.namespace) {
2169
+ return getVectorPath(this.rootPath, this.namespace);
2170
+ }
2171
+ return path4.join(this.rootPath, INDEX_DIR, VECTOR_INDEX_FILE2);
2172
+ }
2173
+ /**
2174
+ * Get the metadata file path.
2175
+ */
2176
+ getMetaPath() {
2177
+ if (this.namespace) {
2178
+ return getMetaPath(this.rootPath, this.namespace);
2179
+ }
2180
+ return path4.join(this.rootPath, INDEX_DIR, VECTOR_META_FILE2);
2181
+ }
2182
+ ensureIndex() {
2183
+ if (!this.index) {
2184
+ this.index = new HierarchicalNSW.HierarchicalNSW(
2185
+ "cosine",
2186
+ this.dimensions
2187
+ );
2188
+ this.index.initIndex(1e4, this.hnswM, this.hnswEfConstruction, 100);
2189
+ }
2190
+ return this.index;
2191
+ }
2192
+ add(entries) {
2193
+ return Effect11.try({
2194
+ try: () => {
2195
+ const index = this.ensureIndex();
2196
+ for (const entry of entries) {
2197
+ if (this.idToIndex.has(entry.id)) {
2198
+ continue;
2199
+ }
2200
+ const idx = this.nextIndex++;
2201
+ if (idx >= index.getMaxElements()) {
2202
+ index.resizeIndex(index.getMaxElements() * 2);
2203
+ }
2204
+ index.addPoint(entry.embedding, idx);
2205
+ this.entries.set(idx, entry);
2206
+ this.idToIndex.set(entry.id, idx);
2207
+ }
2208
+ },
2209
+ catch: (e) => new VectorStoreError({
2210
+ operation: "add",
2211
+ message: e instanceof Error ? e.message : String(e),
2212
+ cause: e
2213
+ })
2214
+ });
2215
+ }
2216
+ search(vector, limit, threshold = 0, options) {
2217
+ return Effect11.try({
2218
+ try: () => {
2219
+ if (!this.index || this.entries.size === 0) {
2220
+ return [];
2221
+ }
2222
+ if (options?.efSearch !== void 0) {
2223
+ this.index.setEf(options.efSearch);
2224
+ }
2225
+ const result = this.index.searchKnn(
2226
+ vector,
2227
+ Math.min(limit, this.entries.size)
2228
+ );
2229
+ const results = [];
2230
+ for (let i = 0; i < result.neighbors.length; i++) {
2231
+ const idx = result.neighbors[i];
2232
+ const distance = result.distances[i];
2233
+ if (idx === void 0 || distance === void 0) {
2234
+ continue;
2235
+ }
2236
+ const similarity = 1 - distance;
2237
+ if (similarity < threshold) {
2238
+ continue;
2239
+ }
2240
+ const entry = this.entries.get(idx);
2241
+ if (entry) {
2242
+ results.push({
2243
+ id: entry.id,
2244
+ sectionId: entry.sectionId,
2245
+ documentPath: entry.documentPath,
2246
+ heading: entry.heading,
2247
+ similarity
2248
+ });
2249
+ }
2250
+ }
2251
+ return results;
2252
+ },
2253
+ catch: (e) => new VectorStoreError({
2254
+ operation: "search",
2255
+ message: e instanceof Error ? e.message : String(e),
2256
+ cause: e
2257
+ })
2258
+ });
2259
+ }
2260
+ searchWithStats(vector, limit, threshold = 0, options) {
2261
+ return Effect11.try({
2262
+ try: () => {
2263
+ if (!this.index || this.entries.size === 0) {
2264
+ return {
2265
+ results: [],
2266
+ belowThresholdCount: 0,
2267
+ belowThresholdHighest: null
2268
+ };
2269
+ }
2270
+ if (options?.efSearch !== void 0) {
2271
+ this.index.setEf(options.efSearch);
2272
+ }
2273
+ const result = this.index.searchKnn(
2274
+ vector,
2275
+ Math.min(limit, this.entries.size)
2276
+ );
2277
+ const results = [];
2278
+ let belowThresholdCount = 0;
2279
+ let belowThresholdHighest = null;
2280
+ for (let i = 0; i < result.neighbors.length; i++) {
2281
+ const idx = result.neighbors[i];
2282
+ const distance = result.distances[i];
2283
+ if (idx === void 0 || distance === void 0) {
2284
+ continue;
2285
+ }
2286
+ const similarity = 1 - distance;
2287
+ const entry = this.entries.get(idx);
2288
+ if (!entry) continue;
2289
+ if (similarity < threshold) {
2290
+ belowThresholdCount++;
2291
+ if (belowThresholdHighest === null || similarity > belowThresholdHighest) {
2292
+ belowThresholdHighest = similarity;
2293
+ }
2294
+ continue;
2295
+ }
2296
+ results.push({
2297
+ id: entry.id,
2298
+ sectionId: entry.sectionId,
2299
+ documentPath: entry.documentPath,
2300
+ heading: entry.heading,
2301
+ similarity
2302
+ });
2303
+ }
2304
+ return {
2305
+ results,
2306
+ belowThresholdCount,
2307
+ belowThresholdHighest
2308
+ };
2309
+ },
2310
+ catch: (e) => new VectorStoreError({
2311
+ operation: "search",
2312
+ message: e instanceof Error ? e.message : String(e),
2313
+ cause: e
2314
+ })
2315
+ });
2316
+ }
2317
+ save() {
2318
+ return Effect11.gen(
2319
+ function* () {
2320
+ if (!this.index) {
2321
+ return;
2322
+ }
2323
+ const indexDir = this.getIndexDir();
2324
+ yield* Effect11.tryPromise({
2325
+ try: () => fs4.mkdir(indexDir, { recursive: true }),
2326
+ catch: (e) => new VectorStoreError({
2327
+ operation: "save",
2328
+ message: `Failed to create directory: ${e instanceof Error ? e.message : String(e)}`,
2329
+ cause: e
2330
+ })
2331
+ });
2332
+ yield* Effect11.tryPromise({
2333
+ try: () => this.index.writeIndex(this.getVectorPath()),
2334
+ catch: (e) => new VectorStoreError({
2335
+ operation: "save",
2336
+ message: `Failed to write index: ${e instanceof Error ? e.message : String(e)}`,
2337
+ cause: e
2338
+ })
2339
+ });
2340
+ const meta = {
2341
+ version: INDEX_VERSION,
2342
+ provider: this.provider,
2343
+ providerModel: this.providerModel,
2344
+ providerBaseURL: this.providerBaseURL,
2345
+ dimensions: this.dimensions,
2346
+ entries: Object.fromEntries(
2347
+ Array.from(this.entries.entries()).map(([idx, entry]) => [
2348
+ idx.toString(),
2349
+ entry
2350
+ ])
2351
+ ),
2352
+ totalCost: this.totalCost,
2353
+ totalTokens: this.totalTokens,
2354
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
2355
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
2356
+ // Store HNSW build parameters for validation on load
2357
+ hnswParams: {
2358
+ m: this.hnswM,
2359
+ efConstruction: this.hnswEfConstruction
2360
+ }
2361
+ };
2362
+ yield* Effect11.tryPromise({
2363
+ try: async () => {
2364
+ const estimatedSize = this.entries.size * 15e3;
2365
+ if (estimatedSize > 1e8) {
2366
+ console.warn(
2367
+ `Large metadata detected: ~${(estimatedSize / 1e6).toFixed(0)}MB. Consider indexing subdirectories separately.`
2368
+ );
2369
+ }
2370
+ const encoded = msgpack2.encode(meta);
2371
+ await fs4.writeFile(this.getMetaPath(), encoded);
2372
+ },
2373
+ catch: (e) => new VectorStoreError({
2374
+ operation: "save",
2375
+ message: `Failed to write metadata: ${e instanceof Error ? e.message : String(e)}`,
2376
+ cause: e
2377
+ })
2378
+ });
2379
+ }.bind(this)
2380
+ );
2381
+ }
2382
+ load() {
2383
+ return Effect11.gen(
2384
+ function* () {
2385
+ const vectorPath = this.getVectorPath();
2386
+ const metaPath = this.getMetaPath();
2387
+ const filesExist = yield* Effect11.tryPromise({
2388
+ try: async () => {
2389
+ await fs4.access(vectorPath);
2390
+ try {
2391
+ await fs4.access(metaPath);
2392
+ return true;
2393
+ } catch {
2394
+ const jsonPath = metaPath.replace(".bin", ".json");
2395
+ await fs4.access(jsonPath);
2396
+ return true;
2397
+ }
2398
+ },
2399
+ catch: () => new VectorStoreError({
2400
+ operation: "load",
2401
+ message: "Files not found"
2402
+ })
2403
+ }).pipe(
2404
+ Effect11.catchTag("VectorStoreError", () => Effect11.succeed(false))
2405
+ );
2406
+ if (!filesExist) {
2407
+ return { loaded: false };
2408
+ }
2409
+ const loadedMeta = yield* Effect11.tryPromise({
2410
+ try: async () => {
2411
+ try {
2412
+ await fs4.access(metaPath);
2413
+ const buffer = await fs4.readFile(metaPath);
2414
+ return msgpack2.decode(buffer);
2415
+ } catch {
2416
+ const jsonPath = metaPath.replace(".bin", ".json");
2417
+ try {
2418
+ await fs4.access(jsonPath);
2419
+ const json = await fs4.readFile(jsonPath, "utf-8");
2420
+ const meta2 = JSON.parse(json);
2421
+ try {
2422
+ const encoded = msgpack2.encode(meta2);
2423
+ await fs4.writeFile(metaPath, encoded);
2424
+ await fs4.unlink(jsonPath).catch(() => {
2425
+ });
2426
+ } catch {
2427
+ }
2428
+ return meta2;
2429
+ } catch {
2430
+ throw new Error("Metadata file not found");
2431
+ }
2432
+ }
2433
+ },
2434
+ catch: (e) => new VectorStoreError({
2435
+ operation: "load",
2436
+ message: `Failed to read metadata: ${e instanceof Error ? e.message : String(e)}`,
2437
+ cause: e
2438
+ })
2439
+ });
2440
+ const meta = {
2441
+ ...loadedMeta,
2442
+ provider: loadedMeta.provider || "openai"
2443
+ };
2444
+ if (meta.dimensions !== this.dimensions) {
2445
+ return yield* Effect11.fail(
2446
+ new DimensionMismatchError({
2447
+ corpusDimensions: meta.dimensions,
2448
+ providerDimensions: this.dimensions,
2449
+ corpusProvider: meta.providerModel ? `${meta.provider}:${meta.providerModel}` : meta.provider,
2450
+ path: this.rootPath
2451
+ })
2452
+ );
2453
+ }
2454
+ this.index = new HierarchicalNSW.HierarchicalNSW(
2455
+ "cosine",
2456
+ this.dimensions
2457
+ );
2458
+ yield* Effect11.tryPromise({
2459
+ try: () => this.index.readIndex(vectorPath),
2460
+ catch: (e) => new VectorStoreError({
2461
+ operation: "load",
2462
+ message: `Failed to read index: ${e instanceof Error ? e.message : String(e)}`,
2463
+ cause: e
2464
+ })
2465
+ });
2466
+ this.entries.clear();
2467
+ this.idToIndex.clear();
2468
+ this.nextIndex = 0;
2469
+ for (const [idxStr, entry] of Object.entries(meta.entries)) {
2470
+ const idx = parseInt(idxStr, 10);
2471
+ this.entries.set(idx, entry);
2472
+ this.idToIndex.set(entry.id, idx);
2473
+ this.nextIndex = Math.max(this.nextIndex, idx + 1);
2474
+ }
2475
+ this.provider = meta.provider;
2476
+ this.providerModel = meta.providerModel;
2477
+ this.providerBaseURL = meta.providerBaseURL;
2478
+ this.totalCost = meta.totalCost;
2479
+ this.totalTokens = meta.totalTokens;
2480
+ let hnswMismatch;
2481
+ if (meta.hnswParams) {
2482
+ const indexM = meta.hnswParams.m;
2483
+ const indexEf = meta.hnswParams.efConstruction;
2484
+ if (indexM !== this.hnswM || indexEf !== this.hnswEfConstruction) {
2485
+ hnswMismatch = {
2486
+ configParams: {
2487
+ m: this.hnswM,
2488
+ efConstruction: this.hnswEfConstruction
2489
+ },
2490
+ indexParams: { m: indexM, efConstruction: indexEf }
2491
+ };
2492
+ }
2493
+ }
2494
+ return { loaded: true, hnswMismatch };
2495
+ }.bind(this)
2496
+ );
2497
+ }
2498
+ getStats() {
2499
+ return {
2500
+ count: this.entries.size,
2501
+ dimensions: this.dimensions,
2502
+ provider: this.provider,
2503
+ providerModel: this.providerModel,
2504
+ totalCost: this.totalCost,
2505
+ totalTokens: this.totalTokens
2506
+ };
2507
+ }
2508
+ setProvider(name, model, baseURL) {
2509
+ this.provider = name;
2510
+ this.providerModel = model;
2511
+ this.providerBaseURL = baseURL;
2512
+ }
2513
+ addCost(cost, tokens) {
2514
+ this.totalCost += cost;
2515
+ this.totalTokens += tokens;
2516
+ }
2517
+ };
2518
+ var createNamespacedVectorStore = (rootPath, provider, model, dimensions, hnswOptions) => {
2519
+ const namespace = generateNamespace(provider, model, dimensions);
2520
+ const store = new HnswVectorStore(rootPath, dimensions, hnswOptions);
2521
+ store.setNamespace(namespace);
2522
+ return store;
2523
+ };
2524
+
2525
+ // src/embeddings/semantic-search.ts
2526
+ var checkHnswMismatch = (mismatch) => {
2527
+ if (!mismatch) {
2528
+ return Effect12.void;
2529
+ }
2530
+ const { configParams, indexParams } = mismatch;
2531
+ return Effect12.logWarning(
2532
+ `HNSW parameter mismatch: Index was built with M=${indexParams.m}, efConstruction=${indexParams.efConstruction}, but config specifies M=${configParams.m}, efConstruction=${configParams.efConstruction}. HNSW parameters only affect index construction. Run 'mdcontext index --embed --force' to rebuild with new parameters.`
2533
+ );
2534
+ };
2535
+ var generateEmbeddingText = (section, content, documentTitle, parentHeading) => {
2536
+ const parts = [];
2537
+ parts.push(`# ${section.heading}`);
2538
+ if (parentHeading) {
2539
+ parts.push(`Parent section: ${parentHeading}`);
2540
+ }
2541
+ parts.push(`Document: ${documentTitle}`);
2542
+ parts.push("");
2543
+ parts.push(content);
2544
+ return parts.join("\n");
2545
+ };
2546
+ var EMBEDDING_PRICE_PER_MILLION = PRICING_DATA.prices["text-embedding-3-small"] ?? 0.02;
2547
+ var estimateEmbeddingCost = (rootPath, options = {}) => Effect12.gen(function* () {
2548
+ const resolvedRoot = path5.resolve(rootPath);
2549
+ const storage = createStorage(resolvedRoot);
2550
+ const docIndex = yield* loadDocumentIndex(storage);
2551
+ const sectionIndex = yield* loadSectionIndex(storage);
2552
+ if (!docIndex || !sectionIndex) {
2553
+ return yield* Effect12.fail(new IndexNotFoundError({ path: resolvedRoot }));
2554
+ }
2555
+ const byDir = /* @__PURE__ */ new Map();
2556
+ for (const section of Object.values(sectionIndex.sections)) {
2557
+ if (section.tokenCount < 10) continue;
2558
+ if (options.excludePatterns?.length) {
2559
+ const excluded = options.excludePatterns.some((pattern) => {
2560
+ const regex = new RegExp(
2561
+ `^${pattern.replace(/\*/g, ".*").replace(/\?/g, ".")}$`
2562
+ );
2563
+ return regex.test(section.documentPath);
2564
+ });
2565
+ if (excluded) continue;
2566
+ }
2567
+ const dir = path5.dirname(section.documentPath) || ".";
2568
+ if (!byDir.has(dir)) {
2569
+ byDir.set(dir, { files: /* @__PURE__ */ new Set(), sections: 0, tokens: 0 });
2570
+ }
2571
+ const entry = byDir.get(dir);
2572
+ entry.files.add(section.documentPath);
2573
+ entry.sections++;
2574
+ entry.tokens += section.tokenCount;
2575
+ }
2576
+ const directoryEstimates = [];
2577
+ let totalFiles = 0;
2578
+ let totalSections = 0;
2579
+ let totalTokens = 0;
2580
+ for (const [dir, data] of byDir) {
2581
+ directoryEstimates.push({
2582
+ directory: dir,
2583
+ fileCount: data.files.size,
2584
+ sectionCount: data.sections,
2585
+ estimatedTokens: data.tokens,
2586
+ estimatedCost: data.tokens / 1e6 * EMBEDDING_PRICE_PER_MILLION
2587
+ });
2588
+ totalFiles += data.files.size;
2589
+ totalSections += data.sections;
2590
+ totalTokens += data.tokens;
2591
+ }
2592
+ directoryEstimates.sort((a, b) => a.directory.localeCompare(b.directory));
2593
+ const estimatedTimeSeconds = Math.ceil(totalSections / 100) * 1.5;
2594
+ return {
2595
+ totalFiles,
2596
+ totalSections,
2597
+ totalTokens,
2598
+ totalCost: totalTokens / 1e6 * EMBEDDING_PRICE_PER_MILLION,
2599
+ estimatedTimeSeconds,
2600
+ byDirectory: directoryEstimates
2601
+ };
2602
+ });
2603
+ var buildEmbeddings = (rootPath, options = {}) => Effect12.gen(function* () {
2604
+ const startTime = Date.now();
2605
+ const resolvedRoot = path5.resolve(rootPath);
2606
+ const storage = createStorage(resolvedRoot);
2607
+ const docIndex = yield* loadDocumentIndex(storage);
2608
+ const sectionIndex = yield* loadSectionIndex(storage);
2609
+ if (!docIndex || !sectionIndex) {
2610
+ return yield* Effect12.fail(new IndexNotFoundError({ path: resolvedRoot }));
2611
+ }
2612
+ const providerConfig = options.providerConfig ?? { provider: "openai" };
2613
+ const provider = options.provider ?? (yield* createEmbeddingProviderDirect(providerConfig));
2614
+ const dimensions = provider.dimensions;
2615
+ let providerName;
2616
+ let providerModel;
2617
+ if (hasProviderMetadata(provider)) {
2618
+ const nameParts = provider.name.split(":");
2619
+ providerName = nameParts[0] || "openai";
2620
+ providerModel = provider.model;
2621
+ } else {
2622
+ providerName = providerConfig.provider ?? "openai";
2623
+ providerModel = providerConfig.model ?? "text-embedding-3-small";
2624
+ }
2625
+ const vectorStore = createNamespacedVectorStore(
2626
+ resolvedRoot,
2627
+ providerName,
2628
+ providerModel,
2629
+ dimensions,
2630
+ options.hnswOptions
2631
+ );
2632
+ if (hasProviderMetadata(provider)) {
2633
+ vectorStore.setProvider(provider.name, provider.model, provider.baseURL);
2634
+ } else {
2635
+ vectorStore.setProvider(providerName, providerModel, void 0);
2636
+ }
2637
+ if (!options.force) {
2638
+ const loadResult = yield* vectorStore.load();
2639
+ if (loadResult.loaded) {
2640
+ const stats = vectorStore.getStats();
2641
+ if (stats.count > 0) {
2642
+ const duration2 = Date.now() - startTime;
2643
+ const estimatedSavings = stats.totalTokens / 1e6 * EMBEDDING_PRICE_PER_MILLION;
2644
+ return {
2645
+ sectionsEmbedded: 0,
2646
+ tokensUsed: 0,
2647
+ cost: 0,
2648
+ duration: duration2,
2649
+ filesProcessed: 0,
2650
+ cacheHit: true,
2651
+ existingVectors: stats.count,
2652
+ estimatedSavings
2653
+ };
2654
+ }
2655
+ }
2656
+ }
2657
+ const isExcluded = (docPath) => {
2658
+ if (!options.excludePatterns?.length) return false;
2659
+ return options.excludePatterns.some((pattern) => {
2660
+ const regex = new RegExp(
2661
+ `^${pattern.replace(/\*/g, ".*").replace(/\?/g, ".")}$`
2662
+ );
2663
+ return regex.test(docPath);
2664
+ });
2665
+ };
2666
+ const sectionsByDoc = /* @__PURE__ */ new Map();
2667
+ for (const section of Object.values(sectionIndex.sections)) {
2668
+ const document = docIndex.documents[section.documentPath];
2669
+ if (!document) continue;
2670
+ if (section.tokenCount < 10) continue;
2671
+ if (isExcluded(section.documentPath)) continue;
2672
+ let parentHeading;
2673
+ if (section.level > 1) {
2674
+ const docSections = sectionIndex.byDocument[document.id] ?? [];
2675
+ for (const sibId of docSections) {
2676
+ const sib = sectionIndex.sections[sibId];
2677
+ if (sib && sib.level === section.level - 1 && sib.startLine < section.startLine) {
2678
+ parentHeading = sib.heading;
2679
+ }
2680
+ }
2681
+ }
2682
+ const docPath = section.documentPath;
2683
+ if (!sectionsByDoc.has(docPath)) {
2684
+ sectionsByDoc.set(docPath, []);
2685
+ }
2686
+ sectionsByDoc.get(docPath).push({ section, parentHeading });
2687
+ }
2688
+ if (sectionsByDoc.size === 0) {
2689
+ const duration2 = Date.now() - startTime;
2690
+ return {
2691
+ sectionsEmbedded: 0,
2692
+ tokensUsed: 0,
2693
+ cost: 0,
2694
+ duration: duration2,
2695
+ filesProcessed: 0
2696
+ };
2697
+ }
2698
+ const sectionsToEmbed = [];
2699
+ const docPaths = Array.from(sectionsByDoc.keys());
2700
+ let filesProcessed = 0;
2701
+ for (let fileIndex = 0; fileIndex < docPaths.length; fileIndex++) {
2702
+ const docPath = docPaths[fileIndex];
2703
+ const sections = sectionsByDoc.get(docPath);
2704
+ const document = docIndex.documents[docPath];
2705
+ if (!document) continue;
2706
+ if (options.onFileProgress) {
2707
+ options.onFileProgress({
2708
+ fileIndex: fileIndex + 1,
2709
+ totalFiles: docPaths.length,
2710
+ filePath: docPath,
2711
+ sectionCount: sections.length
2712
+ });
2713
+ }
2714
+ const filePath = path5.join(resolvedRoot, docPath);
2715
+ const fileContentResult = yield* Effect12.promise(
2716
+ () => fs5.readFile(filePath, "utf-8")
2717
+ ).pipe(
2718
+ Effect12.map((content) => ({ ok: true, content })),
2719
+ Effect12.catchAll(
2720
+ () => Effect12.succeed({ ok: false, content: "" })
2721
+ )
2722
+ );
2723
+ if (!fileContentResult.ok) {
2724
+ yield* Effect12.logWarning(`Skipping file (cannot read): ${docPath}`);
2725
+ continue;
2726
+ }
2727
+ filesProcessed++;
2728
+ const lines = fileContentResult.content.split("\n");
2729
+ for (const { section, parentHeading } of sections) {
2730
+ const content = lines.slice(section.startLine - 1, section.endLine).join("\n");
2731
+ const text = generateEmbeddingText(
2732
+ section,
2733
+ content,
2734
+ document.title,
2735
+ parentHeading
2736
+ );
2737
+ sectionsToEmbed.push({ section, text });
2738
+ }
2739
+ }
2740
+ if (sectionsToEmbed.length === 0) {
2741
+ const duration2 = Date.now() - startTime;
2742
+ return {
2743
+ sectionsEmbedded: 0,
2744
+ tokensUsed: 0,
2745
+ cost: 0,
2746
+ duration: duration2,
2747
+ filesProcessed
2748
+ };
2749
+ }
2750
+ const texts = sectionsToEmbed.map((s) => s.text);
2751
+ const result = yield* wrapEmbedding(
2752
+ provider.embed(texts, {
2753
+ onBatchProgress: options.onBatchProgress ? (p) => options.onBatchProgress?.({
2754
+ batchIndex: p.batchIndex,
2755
+ totalBatches: p.totalBatches,
2756
+ processedSections: p.processedTexts,
2757
+ totalSections: p.totalTexts
2758
+ }) : void 0
2759
+ }),
2760
+ providerConfig.provider ?? "openai"
2761
+ );
2762
+ const entries = [];
2763
+ for (let i = 0; i < sectionsToEmbed.length; i++) {
2764
+ const { section } = sectionsToEmbed[i] ?? { section: null };
2765
+ const embedding = result.embeddings[i];
2766
+ if (!section || !embedding) continue;
2767
+ entries.push({
2768
+ id: section.id,
2769
+ sectionId: section.id,
2770
+ documentPath: section.documentPath,
2771
+ heading: section.heading,
2772
+ embedding
2773
+ });
2774
+ }
2775
+ yield* vectorStore.add(entries);
2776
+ vectorStore.addCost(result.cost, result.tokensUsed);
2777
+ yield* vectorStore.save();
2778
+ const namespace = generateNamespace(providerName, providerModel, dimensions);
2779
+ yield* writeActiveProvider(resolvedRoot, {
2780
+ namespace,
2781
+ provider: providerName,
2782
+ model: providerModel,
2783
+ dimensions,
2784
+ activatedAt: (/* @__PURE__ */ new Date()).toISOString()
2785
+ }).pipe(
2786
+ Effect12.catchAll((e) => {
2787
+ console.warn(`Warning: Could not set active provider: ${e.message}`);
2788
+ return Effect12.succeed(void 0);
2789
+ })
2790
+ );
2791
+ const duration = Date.now() - startTime;
2792
+ return {
2793
+ sectionsEmbedded: entries.length,
2794
+ tokensUsed: result.tokensUsed,
2795
+ cost: result.cost,
2796
+ duration,
2797
+ filesProcessed
2798
+ };
2799
+ });
2800
+ var addContextLinesToResults = (limitedResults, sectionIndex, resolvedRoot, options) => Effect12.gen(function* () {
2801
+ const contextBefore = options.contextBefore ?? 0;
2802
+ const contextAfter = options.contextAfter ?? 0;
2803
+ const resultsWithContext = [];
2804
+ const fileCache = /* @__PURE__ */ new Map();
2805
+ for (const r of limitedResults) {
2806
+ const section = sectionIndex.sections[r.sectionId];
2807
+ if (!section) {
2808
+ resultsWithContext.push({
2809
+ sectionId: r.sectionId,
2810
+ documentPath: r.documentPath,
2811
+ heading: r.heading,
2812
+ similarity: r.similarity
2813
+ });
2814
+ continue;
2815
+ }
2816
+ let fileContent = fileCache.get(r.documentPath);
2817
+ if (!fileContent) {
2818
+ const filePath = path5.join(resolvedRoot, r.documentPath);
2819
+ const contentResult = yield* Effect12.promise(
2820
+ () => fs5.readFile(filePath, "utf-8")
2821
+ ).pipe(
2822
+ Effect12.map((content) => content),
2823
+ Effect12.catchAll(() => Effect12.succeed(null))
2824
+ );
2825
+ if (contentResult) {
2826
+ fileContent = contentResult;
2827
+ fileCache.set(r.documentPath, fileContent);
2828
+ }
2829
+ }
2830
+ if (fileContent) {
2831
+ const lines = fileContent.split("\n");
2832
+ const startIdx = Math.max(0, section.startLine - 1 - contextBefore);
2833
+ const endIdx = Math.min(lines.length, section.endLine + contextAfter);
2834
+ const contextLines = [];
2835
+ for (let i = startIdx; i < endIdx; i++) {
2836
+ const line = lines[i];
2837
+ if (line !== void 0) {
2838
+ contextLines.push({
2839
+ lineNumber: i + 1,
2840
+ line,
2841
+ isMatch: i >= section.startLine - 1 && i < section.endLine
2842
+ });
2843
+ }
2844
+ }
2845
+ resultsWithContext.push({
2846
+ sectionId: r.sectionId,
2847
+ documentPath: r.documentPath,
2848
+ heading: r.heading,
2849
+ similarity: r.similarity,
2850
+ contextLines
2851
+ });
2852
+ } else {
2853
+ resultsWithContext.push({
2854
+ sectionId: r.sectionId,
2855
+ documentPath: r.documentPath,
2856
+ heading: r.heading,
2857
+ similarity: r.similarity
2858
+ });
2859
+ }
2860
+ }
2861
+ return resultsWithContext;
2862
+ });
2863
+ var semanticSearch = (rootPath, query, options = {}) => Effect12.gen(function* () {
2864
+ const resolvedRoot = path5.resolve(rootPath);
2865
+ const activeProvider = yield* getActiveNamespace(resolvedRoot).pipe(
2866
+ Effect12.catchAll(() => Effect12.succeed(null))
2867
+ );
2868
+ if (!activeProvider) {
2869
+ return yield* Effect12.fail(
2870
+ new EmbeddingsNotFoundError({ path: resolvedRoot })
2871
+ );
2872
+ }
2873
+ const provider = yield* createEmbeddingProviderDirect(
2874
+ options.providerConfig ?? { provider: "openai" }
2875
+ );
2876
+ const dimensions = provider.dimensions;
2877
+ const currentProviderName = options.providerConfig?.provider ?? "openai";
2878
+ if (dimensions !== activeProvider.dimensions) {
2879
+ return yield* Effect12.fail(
2880
+ new DimensionMismatchError({
2881
+ corpusDimensions: activeProvider.dimensions,
2882
+ providerDimensions: dimensions,
2883
+ corpusProvider: `${activeProvider.provider}:${activeProvider.model}`,
2884
+ currentProvider: currentProviderName,
2885
+ path: resolvedRoot
2886
+ })
2887
+ );
2888
+ }
2889
+ const vectorStore = createNamespacedVectorStore(
2890
+ resolvedRoot,
2891
+ activeProvider.provider,
2892
+ activeProvider.model,
2893
+ activeProvider.dimensions
2894
+ );
2895
+ const loadResult = yield* vectorStore.load();
2896
+ if (!loadResult.loaded) {
2897
+ return yield* Effect12.fail(
2898
+ new EmbeddingsNotFoundError({ path: resolvedRoot })
2899
+ );
2900
+ }
2901
+ yield* checkHnswMismatch(loadResult.hnswMismatch);
2902
+ let textToEmbed;
2903
+ let hydeResult;
2904
+ if (options.hyde) {
2905
+ hydeResult = yield* generateHypotheticalDocument(query, {
2906
+ model: options.hydeOptions?.model,
2907
+ maxTokens: options.hydeOptions?.maxTokens,
2908
+ temperature: options.hydeOptions?.temperature
2909
+ });
2910
+ textToEmbed = hydeResult.hypotheticalDocument;
2911
+ yield* Effect12.logDebug(
2912
+ `HyDE generated ${hydeResult.tokensUsed} tokens ($${hydeResult.cost.toFixed(6)})`
2913
+ );
2914
+ } else {
2915
+ textToEmbed = options.skipPreprocessing ? query : preprocessQuery(query);
2916
+ }
2917
+ const queryResult = yield* wrapEmbedding(
2918
+ provider.embed([textToEmbed]),
2919
+ currentProviderName
2920
+ );
2921
+ const queryVector = queryResult.embeddings[0];
2922
+ if (!queryVector) {
2923
+ return yield* Effect12.fail(
2924
+ new EmbeddingError({
2925
+ reason: "Unknown",
2926
+ message: "Failed to generate query embedding",
2927
+ provider: currentProviderName
2928
+ })
2929
+ );
2930
+ }
2931
+ const limit = options.limit ?? 10;
2932
+ const threshold = options.threshold ?? 0;
2933
+ const efSearch = options.quality ? QUALITY_EF_SEARCH[options.quality] : void 0;
2934
+ const searchResults = yield* vectorStore.search(
2935
+ queryVector,
2936
+ limit * 2,
2937
+ threshold,
2938
+ { efSearch }
2939
+ );
2940
+ let filteredResults = searchResults;
2941
+ if (options.pathPattern) {
2942
+ const pattern = options.pathPattern.replace(/\./g, "\\.").replace(/\*/g, ".*");
2943
+ const regex = new RegExp(`^${pattern}$`, "i");
2944
+ filteredResults = searchResults.filter((r) => regex.test(r.documentPath));
2945
+ }
2946
+ const applyBoost = options.headingBoost !== false;
2947
+ const boostedResults = applyBoost ? filteredResults.map((r) => ({
2948
+ ...r,
2949
+ similarity: Math.min(
2950
+ 1,
2951
+ r.similarity + calculateHeadingBoost(r.heading, query) + calculateFileImportanceBoost(r.documentPath)
2952
+ )
2953
+ })) : filteredResults;
2954
+ const sortedResults = boostedResults.sort(
2955
+ (a, b) => b.similarity - a.similarity
2956
+ );
2957
+ const limitedResults = sortedResults.slice(0, limit);
2958
+ let results;
2959
+ if (options.contextBefore !== void 0 || options.contextAfter !== void 0) {
2960
+ const storage = createStorage(resolvedRoot);
2961
+ const sectionIndex = yield* loadSectionIndex(storage);
2962
+ if (sectionIndex) {
2963
+ results = yield* addContextLinesToResults(
2964
+ limitedResults,
2965
+ sectionIndex,
2966
+ resolvedRoot,
2967
+ options
2968
+ );
2969
+ } else {
2970
+ results = limitedResults.map((r) => ({
2971
+ sectionId: r.sectionId,
2972
+ documentPath: r.documentPath,
2973
+ heading: r.heading,
2974
+ similarity: r.similarity
2975
+ }));
2976
+ }
2977
+ } else {
2978
+ results = limitedResults.map((r) => ({
2979
+ sectionId: r.sectionId,
2980
+ documentPath: r.documentPath,
2981
+ heading: r.heading,
2982
+ similarity: r.similarity
2983
+ }));
2984
+ }
2985
+ return results;
2986
+ });
2987
+ var semanticSearchWithStats = (rootPath, query, options = {}) => Effect12.gen(function* () {
2988
+ const resolvedRoot = path5.resolve(rootPath);
2989
+ const activeProvider = yield* getActiveNamespace(resolvedRoot).pipe(
2990
+ Effect12.catchAll(() => Effect12.succeed(null))
2991
+ );
2992
+ if (!activeProvider) {
2993
+ return yield* Effect12.fail(
2994
+ new EmbeddingsNotFoundError({ path: resolvedRoot })
2995
+ );
2996
+ }
2997
+ const provider = yield* createEmbeddingProviderDirect(
2998
+ options.providerConfig ?? { provider: "openai" }
2999
+ );
3000
+ const dimensions = provider.dimensions;
3001
+ const currentProviderName = options.providerConfig?.provider ?? "openai";
3002
+ if (dimensions !== activeProvider.dimensions) {
3003
+ return yield* Effect12.fail(
3004
+ new DimensionMismatchError({
3005
+ corpusDimensions: activeProvider.dimensions,
3006
+ providerDimensions: dimensions,
3007
+ corpusProvider: `${activeProvider.provider}:${activeProvider.model}`,
3008
+ currentProvider: currentProviderName,
3009
+ path: resolvedRoot
3010
+ })
3011
+ );
3012
+ }
3013
+ const vectorStore = createNamespacedVectorStore(
3014
+ resolvedRoot,
3015
+ activeProvider.provider,
3016
+ activeProvider.model,
3017
+ activeProvider.dimensions
3018
+ );
3019
+ const loadResult = yield* vectorStore.load();
3020
+ if (!loadResult.loaded) {
3021
+ return yield* Effect12.fail(
3022
+ new EmbeddingsNotFoundError({ path: resolvedRoot })
3023
+ );
3024
+ }
3025
+ yield* checkHnswMismatch(loadResult.hnswMismatch);
3026
+ let textToEmbed;
3027
+ let hydeResult;
3028
+ if (options.hyde) {
3029
+ hydeResult = yield* generateHypotheticalDocument(query, {
3030
+ model: options.hydeOptions?.model,
3031
+ maxTokens: options.hydeOptions?.maxTokens,
3032
+ temperature: options.hydeOptions?.temperature
3033
+ });
3034
+ textToEmbed = hydeResult.hypotheticalDocument;
3035
+ yield* Effect12.logDebug(
3036
+ `HyDE generated ${hydeResult.tokensUsed} tokens ($${hydeResult.cost.toFixed(6)})`
3037
+ );
3038
+ } else {
3039
+ textToEmbed = options.skipPreprocessing ? query : preprocessQuery(query);
3040
+ }
3041
+ const queryResult = yield* wrapEmbedding(
3042
+ provider.embed([textToEmbed]),
3043
+ currentProviderName
3044
+ );
3045
+ const queryVector = queryResult.embeddings[0];
3046
+ if (!queryVector) {
3047
+ return yield* Effect12.fail(
3048
+ new EmbeddingError({
3049
+ reason: "Unknown",
3050
+ message: "Failed to generate query embedding",
3051
+ provider: currentProviderName
3052
+ })
3053
+ );
3054
+ }
3055
+ const limit = options.limit ?? 10;
3056
+ const threshold = options.threshold ?? 0;
3057
+ const efSearch = options.quality ? QUALITY_EF_SEARCH[options.quality] : void 0;
3058
+ const searchResultWithStats = yield* vectorStore.searchWithStats(
3059
+ queryVector,
3060
+ limit * 2,
3061
+ threshold,
3062
+ { efSearch }
3063
+ );
3064
+ let filteredResults = searchResultWithStats.results;
3065
+ if (options.pathPattern) {
3066
+ const pattern = options.pathPattern.replace(/\./g, "\\.").replace(/\*/g, ".*");
3067
+ const regex = new RegExp(`^${pattern}$`, "i");
3068
+ filteredResults = searchResultWithStats.results.filter(
3069
+ (r) => regex.test(r.documentPath)
3070
+ );
3071
+ }
3072
+ const applyBoost = options.headingBoost !== false;
3073
+ const boostedResults = applyBoost ? filteredResults.map((r) => ({
3074
+ ...r,
3075
+ similarity: Math.min(
3076
+ 1,
3077
+ r.similarity + calculateHeadingBoost(r.heading, query) + calculateFileImportanceBoost(r.documentPath)
3078
+ )
3079
+ })) : filteredResults;
3080
+ const sortedResults = boostedResults.sort(
3081
+ (a, b) => b.similarity - a.similarity
3082
+ );
3083
+ const totalAvailable = sortedResults.length;
3084
+ const limitedResults = sortedResults.slice(0, limit);
3085
+ let results;
3086
+ if (options.contextBefore !== void 0 || options.contextAfter !== void 0) {
3087
+ const storage = createStorage(resolvedRoot);
3088
+ const sectionIndex = yield* loadSectionIndex(storage);
3089
+ if (sectionIndex) {
3090
+ results = yield* addContextLinesToResults(
3091
+ limitedResults,
3092
+ sectionIndex,
3093
+ resolvedRoot,
3094
+ options
3095
+ );
3096
+ } else {
3097
+ results = limitedResults.map((r) => ({
3098
+ sectionId: r.sectionId,
3099
+ documentPath: r.documentPath,
3100
+ heading: r.heading,
3101
+ similarity: r.similarity
3102
+ }));
3103
+ }
3104
+ } else {
3105
+ results = limitedResults.map((r) => ({
3106
+ sectionId: r.sectionId,
3107
+ documentPath: r.documentPath,
3108
+ heading: r.heading,
3109
+ similarity: r.similarity
3110
+ }));
3111
+ }
3112
+ return {
3113
+ results,
3114
+ belowThresholdCount: searchResultWithStats.belowThresholdCount,
3115
+ belowThresholdHighest: searchResultWithStats.belowThresholdHighest ?? void 0,
3116
+ totalAvailable
3117
+ };
3118
+ });
3119
+ var getEmbeddingStats = (rootPath) => Effect12.gen(function* () {
3120
+ const resolvedRoot = path5.resolve(rootPath);
3121
+ const activeProvider = yield* getActiveNamespace(resolvedRoot).pipe(
3122
+ Effect12.catchAll(() => Effect12.succeed(null))
3123
+ );
3124
+ if (!activeProvider) {
3125
+ return {
3126
+ hasEmbeddings: false,
3127
+ count: 0,
3128
+ provider: "none",
3129
+ dimensions: 0,
3130
+ totalCost: 0,
3131
+ totalTokens: 0
3132
+ };
3133
+ }
3134
+ const vectorStore = createNamespacedVectorStore(
3135
+ resolvedRoot,
3136
+ activeProvider.provider,
3137
+ activeProvider.model,
3138
+ activeProvider.dimensions
3139
+ );
3140
+ const loadResult = yield* vectorStore.load().pipe(
3141
+ Effect12.catchAll(
3142
+ () => Effect12.succeed({ loaded: false })
3143
+ )
3144
+ );
3145
+ if (!loadResult.loaded) {
3146
+ return {
3147
+ hasEmbeddings: false,
3148
+ count: 0,
3149
+ provider: "none",
3150
+ dimensions: 0,
3151
+ totalCost: 0,
3152
+ totalTokens: 0
3153
+ };
3154
+ }
3155
+ const stats = vectorStore.getStats();
3156
+ return {
3157
+ hasEmbeddings: true,
3158
+ count: stats.count,
3159
+ provider: stats.provider || "openai",
3160
+ model: stats.providerModel,
3161
+ dimensions: stats.dimensions,
3162
+ totalCost: stats.totalCost || 0,
3163
+ totalTokens: stats.totalTokens || 0
3164
+ };
3165
+ });
3166
+
3167
+ // src/search/searcher.ts
3168
+ import * as fs6 from "fs/promises";
3169
+ import * as path6 from "path";
3170
+ import { Effect as Effect13 } from "effect";
3171
+
3172
+ // src/search/fuzzy-search.ts
3173
+ import { stemmer } from "stemmer";
3174
+ var stem = (word) => {
3175
+ return stemmer(word.toLowerCase());
3176
+ };
3177
+ var levenshteinDistance = (a, b) => {
3178
+ const matrix = [];
3179
+ for (let i = 0; i <= a.length; i++) {
3180
+ matrix[i] = [i];
3181
+ }
3182
+ for (let j = 0; j <= b.length; j++) {
3183
+ matrix[0][j] = j;
3184
+ }
3185
+ for (let i = 1; i <= a.length; i++) {
3186
+ for (let j = 1; j <= b.length; j++) {
3187
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
3188
+ matrix[i][j] = Math.min(
3189
+ matrix[i - 1][j] + 1,
3190
+ // deletion
3191
+ matrix[i][j - 1] + 1,
3192
+ // insertion
3193
+ matrix[i - 1][j - 1] + cost
3194
+ // substitution
3195
+ );
3196
+ }
3197
+ }
3198
+ return matrix[a.length][b.length];
3199
+ };
3200
+ var isFuzzyMatch = (word1, word2, maxDistance = 2) => {
3201
+ if (Math.abs(word1.length - word2.length) > maxDistance) {
3202
+ return false;
3203
+ }
3204
+ return levenshteinDistance(word1.toLowerCase(), word2.toLowerCase()) <= maxDistance;
3205
+ };
3206
+ var matchesWithOptions = (query, text, options = {}) => {
3207
+ const { stem: useStemming, fuzzyDistance } = options;
3208
+ const queryWords = query.toLowerCase().split(/[\W_]+/).filter((w) => w.length > 0);
3209
+ const textWords = text.toLowerCase().split(/[\W_]+/).filter((w) => w.length > 0);
3210
+ if (queryWords.length === 0) {
3211
+ return true;
3212
+ }
3213
+ for (const queryWord of queryWords) {
3214
+ let found = false;
3215
+ for (const textWord of textWords) {
3216
+ if (textWord === queryWord) {
3217
+ found = true;
3218
+ break;
3219
+ }
3220
+ if (useStemming) {
3221
+ if (stem(textWord) === stem(queryWord)) {
3222
+ found = true;
3223
+ break;
3224
+ }
3225
+ }
3226
+ if (fuzzyDistance !== void 0 && fuzzyDistance > 0) {
3227
+ if (isFuzzyMatch(textWord, queryWord, fuzzyDistance)) {
3228
+ found = true;
3229
+ break;
3230
+ }
3231
+ }
3232
+ }
3233
+ if (!found) {
3234
+ return false;
3235
+ }
3236
+ }
3237
+ return true;
3238
+ };
3239
+ var findMatchesInLine = (queryWords, line, options = {}) => {
3240
+ const { stem: useStemming, fuzzyDistance } = options;
3241
+ const matchesSet = /* @__PURE__ */ new Set();
3242
+ const lineWords = line.toLowerCase().split(/[\W_]+/).filter((w) => w.length > 0);
3243
+ for (const queryWord of queryWords) {
3244
+ const queryLower = queryWord.toLowerCase();
3245
+ const queryStem = useStemming ? stem(queryWord) : null;
3246
+ for (const lineWord of lineWords) {
3247
+ if (matchesSet.has(lineWord)) {
3248
+ continue;
3249
+ }
3250
+ if (lineWord === queryLower) {
3251
+ matchesSet.add(lineWord);
3252
+ continue;
3253
+ }
3254
+ if (queryStem && stem(lineWord) === queryStem) {
3255
+ matchesSet.add(lineWord);
3256
+ continue;
3257
+ }
3258
+ if (fuzzyDistance !== void 0 && fuzzyDistance > 0 && isFuzzyMatch(lineWord, queryLower, fuzzyDistance)) {
3259
+ matchesSet.add(lineWord);
3260
+ }
3261
+ }
3262
+ }
3263
+ return Array.from(matchesSet);
3264
+ };
3265
+ var buildFuzzyHighlightPattern = (query, options = {}) => {
3266
+ const { stem: useStemming } = options;
3267
+ const queryWords = query.toLowerCase().split(/[\W_]+/).filter((w) => w.length > 0);
3268
+ if (queryWords.length === 0) {
3269
+ return /.^/;
3270
+ }
3271
+ const patterns = [];
3272
+ for (const word of queryWords) {
3273
+ const escaped = word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3274
+ if (useStemming) {
3275
+ const wordStem = stem(word);
3276
+ const escapedStem = wordStem.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3277
+ patterns.push(`\\b${escapedStem}\\w*\\b`);
3278
+ } else {
3279
+ patterns.push(`\\b${escaped}\\b`);
3280
+ }
3281
+ }
3282
+ return new RegExp(patterns.join("|"), "gi");
3283
+ };
3284
+
3285
+ // src/search/path-matcher.ts
3286
+ var matchPath = (filePath, pattern) => {
3287
+ const DOUBLE_STAR_PLACEHOLDER = "__DOUBLE_STAR_MARKER__";
3288
+ const regexPattern = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, DOUBLE_STAR_PLACEHOLDER).replace(/\*/g, "[^/]*").replace(/\?/g, "[^/]").replace(new RegExp(DOUBLE_STAR_PLACEHOLDER, "g"), ".*");
3289
+ const regex = new RegExp(`^${regexPattern}$`, "i");
3290
+ return regex.test(filePath);
3291
+ };
3292
+
3293
+ // src/search/query-parser.ts
3294
+ var tokenize = (query) => {
3295
+ const tokens = [];
3296
+ let i = 0;
3297
+ while (i < query.length) {
3298
+ if (/\s/.test(query[i])) {
3299
+ i++;
3300
+ continue;
3301
+ }
3302
+ if (query[i] === '"') {
3303
+ const start = i + 1;
3304
+ i++;
3305
+ while (i < query.length && query[i] !== '"') {
3306
+ i++;
3307
+ }
3308
+ const value = query.slice(start, i);
3309
+ tokens.push({ type: "PHRASE", value });
3310
+ i++;
3311
+ continue;
3312
+ }
3313
+ if (query[i] === "(") {
3314
+ tokens.push({ type: "LPAREN", value: "(" });
3315
+ i++;
3316
+ continue;
3317
+ }
3318
+ if (query[i] === ")") {
3319
+ tokens.push({ type: "RPAREN", value: ")" });
3320
+ i++;
3321
+ continue;
3322
+ }
3323
+ const wordMatch = query.slice(i).match(/^[^\s()"]+/);
3324
+ if (wordMatch) {
3325
+ const word = wordMatch[0];
3326
+ const upperWord = word.toUpperCase();
3327
+ if (upperWord === "AND") {
3328
+ tokens.push({ type: "AND", value: "AND" });
3329
+ } else if (upperWord === "OR") {
3330
+ tokens.push({ type: "OR", value: "OR" });
3331
+ } else if (upperWord === "NOT") {
3332
+ tokens.push({ type: "NOT", value: "NOT" });
3333
+ } else {
3334
+ tokens.push({ type: "TERM", value: word });
3335
+ }
3336
+ i += word.length;
3337
+ continue;
3338
+ }
3339
+ i++;
3340
+ }
3341
+ return tokens;
3342
+ };
3343
+ var Parser = class {
3344
+ tokens;
3345
+ pos = 0;
3346
+ terms = [];
3347
+ phrases = [];
3348
+ constructor(tokens) {
3349
+ this.tokens = tokens;
3350
+ }
3351
+ current() {
3352
+ return this.tokens[this.pos];
3353
+ }
3354
+ advance() {
3355
+ return this.tokens[this.pos++];
3356
+ }
3357
+ match(type) {
3358
+ if (this.current()?.type === type) {
3359
+ this.advance();
3360
+ return true;
3361
+ }
3362
+ return false;
3363
+ }
3364
+ parse() {
3365
+ if (this.tokens.length === 0) {
3366
+ return null;
3367
+ }
3368
+ return this.parseExpr();
3369
+ }
3370
+ parseExpr() {
3371
+ let left = this.parseAndExpr();
3372
+ while (this.match("OR")) {
3373
+ const right = this.parseAndExpr();
3374
+ left = { type: "or", left, right };
3375
+ }
3376
+ return left;
3377
+ }
3378
+ parseAndExpr() {
3379
+ let left = this.parseNotExpr();
3380
+ while (this.match("AND") || this.isImplicitAnd()) {
3381
+ const right = this.parseNotExpr();
3382
+ left = { type: "and", left, right };
3383
+ }
3384
+ return left;
3385
+ }
3386
+ isImplicitAnd() {
3387
+ const tok = this.current();
3388
+ return tok?.type === "TERM" || tok?.type === "PHRASE" || tok?.type === "NOT" || tok?.type === "LPAREN";
3389
+ }
3390
+ parseNotExpr() {
3391
+ if (this.match("NOT")) {
3392
+ const operand = this.parseNotExpr();
3393
+ return { type: "not", operand };
3394
+ }
3395
+ return this.parsePrimary();
3396
+ }
3397
+ parsePrimary() {
3398
+ const tok = this.current();
3399
+ if (this.match("LPAREN")) {
3400
+ const expr = this.parseExpr();
3401
+ this.match("RPAREN");
3402
+ return expr;
3403
+ }
3404
+ if (tok?.type === "PHRASE") {
3405
+ this.advance();
3406
+ this.phrases.push(tok.value);
3407
+ return { type: "phrase", value: tok.value };
3408
+ }
3409
+ if (tok?.type === "TERM") {
3410
+ this.advance();
3411
+ this.terms.push(tok.value);
3412
+ return { type: "term", value: tok.value };
3413
+ }
3414
+ return { type: "term", value: "" };
3415
+ }
3416
+ };
3417
+ var parseQuery = (query) => {
3418
+ const tokens = tokenize(query);
3419
+ if (tokens.length === 0) {
3420
+ return null;
3421
+ }
3422
+ const parser = new Parser(tokens);
3423
+ const ast = parser.parse();
3424
+ if (!ast) {
3425
+ return null;
3426
+ }
3427
+ return {
3428
+ ast,
3429
+ terms: parser.terms,
3430
+ phrases: parser.phrases
3431
+ };
3432
+ };
3433
+ var isAdvancedQuery = (query) => {
3434
+ const tokens = tokenize(query);
3435
+ return tokens.some(
3436
+ (t) => t.type === "AND" || t.type === "OR" || t.type === "NOT" || t.type === "PHRASE" || t.type === "LPAREN"
3437
+ );
3438
+ };
3439
+ var evaluateQuery = (ast, text) => {
3440
+ const lowerText = text.toLowerCase();
3441
+ const evaluate = (node) => {
3442
+ switch (node.type) {
3443
+ case "term": {
3444
+ if (!node.value) return true;
3445
+ return lowerText.includes(node.value.toLowerCase());
3446
+ }
3447
+ case "phrase": {
3448
+ return lowerText.includes(node.value.toLowerCase());
3449
+ }
3450
+ case "and": {
3451
+ return evaluate(node.left) && evaluate(node.right);
3452
+ }
3453
+ case "or": {
3454
+ return evaluate(node.left) || evaluate(node.right);
3455
+ }
3456
+ case "not": {
3457
+ return !evaluate(node.operand);
3458
+ }
3459
+ }
3460
+ };
3461
+ return evaluate(ast);
3462
+ };
3463
+ var buildHighlightPattern = (parsed) => {
3464
+ const patterns = [];
3465
+ const escapeChars = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3466
+ for (const term of parsed.terms) {
3467
+ if (term) {
3468
+ patterns.push(`\\b${escapeChars(term)}\\b`);
3469
+ }
3470
+ }
3471
+ for (const phrase of parsed.phrases) {
3472
+ if (phrase) {
3473
+ patterns.push(escapeChars(phrase));
3474
+ }
3475
+ }
3476
+ if (patterns.length === 0) {
3477
+ return /.^/;
3478
+ }
3479
+ return new RegExp(patterns.join("|"), "gi");
3480
+ };
3481
+
3482
+ // src/search/searcher.ts
3483
+ var search = (rootPath, options = {}) => Effect13.gen(function* () {
3484
+ const storage = createStorage(rootPath);
3485
+ const docIndex = yield* loadDocumentIndex(storage);
3486
+ const sectionIndex = yield* loadSectionIndex(storage);
3487
+ if (!docIndex || !sectionIndex) {
3488
+ return [];
3489
+ }
3490
+ const results = [];
3491
+ const headingRegex = options.heading ? new RegExp(options.heading, "i") : null;
3492
+ for (const section of Object.values(sectionIndex.sections)) {
3493
+ if (headingRegex && !headingRegex.test(section.heading)) {
3494
+ continue;
3495
+ }
3496
+ if (options.pathPattern && !matchPath(section.documentPath, options.pathPattern)) {
3497
+ continue;
3498
+ }
3499
+ if (options.hasCode !== void 0 && section.hasCode !== options.hasCode) {
3500
+ continue;
3501
+ }
3502
+ if (options.hasList !== void 0 && section.hasList !== options.hasList) {
3503
+ continue;
3504
+ }
3505
+ if (options.hasTable !== void 0 && section.hasTable !== options.hasTable) {
3506
+ continue;
3507
+ }
3508
+ if (options.minLevel !== void 0 && section.level < options.minLevel) {
3509
+ continue;
3510
+ }
3511
+ if (options.maxLevel !== void 0 && section.level > options.maxLevel) {
3512
+ continue;
3513
+ }
3514
+ const document = docIndex.documents[section.documentPath];
3515
+ if (document) {
3516
+ results.push({ section, document });
3517
+ }
3518
+ if (options.limit !== void 0 && results.length >= options.limit) {
3519
+ break;
3520
+ }
3521
+ }
3522
+ return results;
3523
+ });
3524
+ var searchContent = (rootPath, options = {}) => Effect13.gen(function* () {
3525
+ const storage = createStorage(rootPath);
3526
+ const docIndex = yield* loadDocumentIndex(storage);
3527
+ const sectionIndex = yield* loadSectionIndex(storage);
3528
+ if (!docIndex || !sectionIndex) {
3529
+ return [];
3530
+ }
3531
+ let parsedQuery = null;
3532
+ let contentRegex = null;
3533
+ let highlightRegex = null;
3534
+ const matchOptions = {
3535
+ stem: options.stem,
3536
+ fuzzyDistance: options.fuzzy ? options.fuzzyDistance ?? 2 : void 0
3537
+ };
3538
+ const useFuzzyOrStem = options.fuzzy || options.stem;
3539
+ if (options.content) {
3540
+ if (isAdvancedQuery(options.content)) {
3541
+ parsedQuery = parseQuery(options.content);
3542
+ if (parsedQuery) {
3543
+ if (useFuzzyOrStem) {
3544
+ highlightRegex = buildFuzzyHighlightPattern(
3545
+ options.content,
3546
+ matchOptions
3547
+ );
3548
+ } else {
3549
+ highlightRegex = buildHighlightPattern(parsedQuery);
3550
+ }
3551
+ }
3552
+ } else {
3553
+ if (!useFuzzyOrStem) {
3554
+ contentRegex = new RegExp(options.content, "gi");
3555
+ highlightRegex = contentRegex;
3556
+ } else {
3557
+ highlightRegex = buildFuzzyHighlightPattern(
3558
+ options.content,
3559
+ matchOptions
3560
+ );
3561
+ }
3562
+ }
3563
+ }
3564
+ const headingRegex = options.heading ? new RegExp(options.heading, "i") : null;
3565
+ const results = [];
3566
+ const sectionsByDoc = {};
3567
+ for (const section of Object.values(sectionIndex.sections)) {
3568
+ const docSections = sectionsByDoc[section.documentPath];
3569
+ if (docSections) {
3570
+ docSections.push(section);
3571
+ } else {
3572
+ sectionsByDoc[section.documentPath] = [section];
3573
+ }
3574
+ }
3575
+ for (const [docPath, sections] of Object.entries(sectionsByDoc)) {
3576
+ if (options.pathPattern && !matchPath(docPath, options.pathPattern)) {
3577
+ continue;
3578
+ }
3579
+ const document = docIndex.documents[docPath];
3580
+ if (!document) continue;
3581
+ let fileContent = null;
3582
+ let fileLines = [];
3583
+ if (parsedQuery || contentRegex || useFuzzyOrStem && options.content) {
3584
+ const filePath = path6.join(storage.rootPath, docPath);
3585
+ try {
3586
+ fileContent = yield* Effect13.promise(
3587
+ () => fs6.readFile(filePath, "utf-8")
3588
+ );
3589
+ fileLines = fileContent.split("\n");
3590
+ } catch {
3591
+ continue;
3592
+ }
3593
+ }
3594
+ for (const section of sections) {
3595
+ if (headingRegex && !headingRegex.test(section.heading)) {
3596
+ continue;
3597
+ }
3598
+ if (options.hasCode !== void 0 && section.hasCode !== options.hasCode) {
3599
+ continue;
3600
+ }
3601
+ if (options.hasList !== void 0 && section.hasList !== options.hasList) {
3602
+ continue;
3603
+ }
3604
+ if (options.hasTable !== void 0 && section.hasTable !== options.hasTable) {
3605
+ continue;
3606
+ }
3607
+ if (options.minLevel !== void 0 && section.level < options.minLevel) {
3608
+ continue;
3609
+ }
3610
+ if (options.maxLevel !== void 0 && section.level > options.maxLevel) {
3611
+ continue;
3612
+ }
3613
+ if ((parsedQuery || contentRegex || useFuzzyOrStem) && fileContent) {
3614
+ const sectionLines = fileLines.slice(
3615
+ section.startLine - 1,
3616
+ section.endLine
3617
+ );
3618
+ const sectionContent = sectionLines.join("\n");
3619
+ if (parsedQuery) {
3620
+ if (!evaluateQuery(parsedQuery.ast, sectionContent)) {
3621
+ continue;
3622
+ }
3623
+ }
3624
+ if (useFuzzyOrStem && !parsedQuery && options.content) {
3625
+ if (!matchesWithOptions(options.content, sectionContent, matchOptions)) {
3626
+ continue;
3627
+ }
3628
+ }
3629
+ const matches = [];
3630
+ const searchRegex = contentRegex || highlightRegex;
3631
+ const contextBefore = options.contextBefore ?? 1;
3632
+ const contextAfter = options.contextAfter ?? 1;
3633
+ const queryWords = options.content ? options.content.toLowerCase().split(/\W+/).filter((w) => w.length > 0) : [];
3634
+ for (let i = 0; i < sectionLines.length; i++) {
3635
+ const line = sectionLines[i];
3636
+ if (!line) continue;
3637
+ let isMatch = false;
3638
+ if (searchRegex) {
3639
+ if (searchRegex.test(line)) {
3640
+ isMatch = true;
3641
+ }
3642
+ searchRegex.lastIndex = 0;
3643
+ }
3644
+ if (!isMatch && useFuzzyOrStem && queryWords.length > 0) {
3645
+ const lineMatches = findMatchesInLine(
3646
+ queryWords,
3647
+ line,
3648
+ matchOptions
3649
+ );
3650
+ if (lineMatches.length > 0) {
3651
+ isMatch = true;
3652
+ }
3653
+ }
3654
+ if (isMatch) {
3655
+ const absoluteLineNum = section.startLine + i;
3656
+ const snippetStart = Math.max(0, i - contextBefore);
3657
+ const snippetEnd = Math.min(
3658
+ sectionLines.length,
3659
+ i + contextAfter + 1
3660
+ );
3661
+ const snippetLines = sectionLines.slice(snippetStart, snippetEnd);
3662
+ const snippet = snippetLines.join("\n");
3663
+ const contextLines = [];
3664
+ for (let j = snippetStart; j < snippetEnd; j++) {
3665
+ const ctxLine = sectionLines[j];
3666
+ if (ctxLine !== void 0) {
3667
+ contextLines.push({
3668
+ lineNumber: section.startLine + j,
3669
+ line: ctxLine,
3670
+ isMatch: j === i
3671
+ });
3672
+ }
3673
+ }
3674
+ matches.push({
3675
+ lineNumber: absoluteLineNum,
3676
+ line,
3677
+ snippet,
3678
+ contextLines
3679
+ });
3680
+ }
3681
+ }
3682
+ if (parsedQuery || matches.length > 0) {
3683
+ const result = {
3684
+ section,
3685
+ document,
3686
+ sectionContent
3687
+ };
3688
+ if (matches.length > 0) {
3689
+ results.push({ ...result, matches });
3690
+ } else {
3691
+ results.push(result);
3692
+ }
3693
+ if (options.limit !== void 0 && results.length >= options.limit) {
3694
+ return results;
3695
+ }
3696
+ }
3697
+ } else if (!parsedQuery && !contentRegex && !useFuzzyOrStem) {
3698
+ results.push({ section, document });
3699
+ if (options.limit !== void 0 && results.length >= options.limit) {
3700
+ return results;
3701
+ }
3702
+ }
3703
+ }
3704
+ }
3705
+ return results;
3706
+ });
3707
+
3708
+ export {
3709
+ CONFIG_FILE_NAMES,
3710
+ findConfigFile,
3711
+ loadConfigFile,
3712
+ readEnvConfig,
3713
+ createConfigProvider,
3714
+ createConfigProviderSync,
3715
+ ConfigService,
3716
+ ConfigServiceDefault,
3717
+ writeActiveProvider,
3718
+ listNamespaces,
3719
+ switchNamespace,
3720
+ removeNamespace,
3721
+ getActiveNamespace,
3722
+ buildSectionList,
3723
+ formatSectionList,
3724
+ filterExcludedSections,
3725
+ extractSectionContent,
3726
+ formatExtractedSections,
3727
+ formatSummary,
3728
+ formatAssembledContext,
3729
+ summarizeFile,
3730
+ assembleContext,
3731
+ checkPricingFreshness,
3732
+ getPricingDate,
3733
+ estimateEmbeddingCost,
3734
+ buildEmbeddings,
3735
+ semanticSearch,
3736
+ semanticSearchWithStats,
3737
+ getEmbeddingStats,
3738
+ matchPath,
3739
+ isAdvancedQuery,
3740
+ search,
3741
+ searchContent
3742
+ };