mdcontext 0.0.1 → 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 (337) hide show
  1. package/.changeset/README.md +28 -0
  2. package/.changeset/config.json +11 -0
  3. package/.claude/settings.local.json +25 -0
  4. package/.github/workflows/ci.yml +83 -0
  5. package/.github/workflows/claude-code-review.yml +44 -0
  6. package/.github/workflows/claude.yml +85 -0
  7. package/.github/workflows/release.yml +113 -0
  8. package/.tldrignore +112 -0
  9. package/BACKLOG.md +338 -0
  10. package/CONTRIBUTING.md +186 -0
  11. package/NOTES/NOTES +44 -0
  12. package/README.md +434 -11
  13. package/biome.json +36 -0
  14. package/cspell.config.yaml +14 -0
  15. package/dist/chunk-23UPXDNL.js +3044 -0
  16. package/dist/chunk-2W7MO2DL.js +1366 -0
  17. package/dist/chunk-3NUAZGMA.js +1689 -0
  18. package/dist/chunk-7TOWB2XB.js +366 -0
  19. package/dist/chunk-7XOTOADQ.js +3065 -0
  20. package/dist/chunk-AH2PDM2K.js +3042 -0
  21. package/dist/chunk-BNXWSZ63.js +3742 -0
  22. package/dist/chunk-BTL5DJVU.js +3222 -0
  23. package/dist/chunk-HDHYG7E4.js +104 -0
  24. package/dist/chunk-HLR4KZBP.js +3234 -0
  25. package/dist/chunk-IP3FRFEB.js +1045 -0
  26. package/dist/chunk-KHU56VDO.js +3042 -0
  27. package/dist/chunk-KRYIFLQR.js +88 -0
  28. package/dist/chunk-LBSDNLEM.js +287 -0
  29. package/dist/chunk-MNTQ7HCP.js +2643 -0
  30. package/dist/chunk-MUJELQQ6.js +1387 -0
  31. package/dist/chunk-MXJGMSLV.js +2199 -0
  32. package/dist/chunk-N6QJGC3Z.js +2636 -0
  33. package/dist/chunk-OBELGBPM.js +1713 -0
  34. package/dist/chunk-OT7R5XTA.js +3192 -0
  35. package/dist/chunk-P7X4RA2T.js +106 -0
  36. package/dist/chunk-PIDUQNC2.js +3185 -0
  37. package/dist/chunk-POGCDIH4.js +3187 -0
  38. package/dist/chunk-PSIEOQGZ.js +3043 -0
  39. package/dist/chunk-PVRT3IHA.js +3238 -0
  40. package/dist/chunk-QNN4TT23.js +1430 -0
  41. package/dist/chunk-RE3R45RJ.js +3042 -0
  42. package/dist/chunk-S7E6TFX6.js +803 -0
  43. package/dist/chunk-SG6GLU4U.js +1378 -0
  44. package/dist/chunk-SJCDV2ST.js +274 -0
  45. package/dist/chunk-SYE5XLF3.js +104 -0
  46. package/dist/chunk-T5VLYBZD.js +103 -0
  47. package/dist/chunk-TOQB7VWU.js +3238 -0
  48. package/dist/chunk-VFNMZ4ZQ.js +3228 -0
  49. package/dist/chunk-VVTGZNBT.js +1629 -0
  50. package/dist/chunk-W7Q4RFEV.js +104 -0
  51. package/dist/chunk-XTYYVRLO.js +3190 -0
  52. package/dist/chunk-Y6MDYVJD.js +3063 -0
  53. package/dist/cli/main.d.ts +1 -0
  54. package/dist/cli/main.js +5458 -0
  55. package/dist/index.d.ts +653 -0
  56. package/dist/index.js +79 -0
  57. package/dist/mcp/server.d.ts +1 -0
  58. package/dist/mcp/server.js +472 -0
  59. package/dist/schema-BAWSG7KY.js +22 -0
  60. package/dist/schema-E3QUPL26.js +20 -0
  61. package/dist/schema-EHL7WUT6.js +20 -0
  62. package/docs/019-USAGE.md +625 -0
  63. package/docs/020-current-implementation.md +364 -0
  64. package/docs/021-DOGFOODING-FINDINGS.md +175 -0
  65. package/docs/BACKLOG.md +80 -0
  66. package/docs/CONFIG.md +1123 -0
  67. package/docs/DESIGN.md +439 -0
  68. package/docs/ERRORS.md +383 -0
  69. package/docs/PROJECT.md +88 -0
  70. package/docs/ROADMAP.md +407 -0
  71. package/docs/summarization.md +320 -0
  72. package/docs/test-links.md +9 -0
  73. package/justfile +40 -0
  74. package/package.json +74 -9
  75. package/pnpm-workspace.yaml +5 -0
  76. package/research/INDEX.md +315 -0
  77. package/research/code-review/README.md +90 -0
  78. package/research/code-review/cli-error-handling-review.md +979 -0
  79. package/research/code-review/code-review-validation-report.md +464 -0
  80. package/research/code-review/main-ts-review.md +1128 -0
  81. package/research/config-analysis/01-current-implementation.md +470 -0
  82. package/research/config-analysis/02-strategy-recommendation.md +428 -0
  83. package/research/config-analysis/03-task-candidates.md +715 -0
  84. package/research/config-analysis/033-research-configuration-management.md +828 -0
  85. package/research/config-analysis/034-research-effect-cli-config.md +1504 -0
  86. package/research/config-analysis/04-consolidated-task-candidates.md +277 -0
  87. package/research/config-docs/SUMMARY.md +357 -0
  88. package/research/config-docs/TEST-RESULTS.md +776 -0
  89. package/research/config-docs/TODO.md +542 -0
  90. package/research/config-docs/analysis.md +744 -0
  91. package/research/config-docs/fix-validation.md +502 -0
  92. package/research/config-docs/help-audit.md +264 -0
  93. package/research/config-docs/help-system-analysis.md +890 -0
  94. package/research/dogfood/consolidated-tool-evaluation.md +373 -0
  95. package/research/dogfood/strategy-a/a-synthesis.md +184 -0
  96. package/research/dogfood/strategy-a/a1-docs.md +226 -0
  97. package/research/dogfood/strategy-a/a2-amorphic.md +156 -0
  98. package/research/dogfood/strategy-a/a3-llm.md +164 -0
  99. package/research/dogfood/strategy-b/b-synthesis.md +228 -0
  100. package/research/dogfood/strategy-b/b1-architecture.md +207 -0
  101. package/research/dogfood/strategy-b/b2-gaps.md +258 -0
  102. package/research/dogfood/strategy-b/b3-workflows.md +250 -0
  103. package/research/dogfood/strategy-c/c-synthesis.md +451 -0
  104. package/research/dogfood/strategy-c/c1-explorer.md +192 -0
  105. package/research/dogfood/strategy-c/c2-diver-memory.md +145 -0
  106. package/research/dogfood/strategy-c/c3-diver-control.md +148 -0
  107. package/research/dogfood/strategy-c/c4-diver-failure.md +151 -0
  108. package/research/dogfood/strategy-c/c5-diver-execution.md +221 -0
  109. package/research/dogfood/strategy-c/c6-diver-org.md +221 -0
  110. package/research/effect-cli-error-handling.md +845 -0
  111. package/research/effect-errors-as-values.md +943 -0
  112. package/research/errors-task-analysis/00-consolidated-tasks.md +207 -0
  113. package/research/errors-task-analysis/cli-commands-analysis.md +909 -0
  114. package/research/errors-task-analysis/embeddings-analysis.md +709 -0
  115. package/research/errors-task-analysis/index-search-analysis.md +812 -0
  116. package/research/frontmatter/COMMENTS-ARE-SKIPPED.md +149 -0
  117. package/research/frontmatter/LLM-CODE-NAVIGATION.md +276 -0
  118. package/research/issue-review.md +603 -0
  119. package/research/llm-summarization/agent-cli-tools-2026.md +1082 -0
  120. package/research/llm-summarization/alternative-providers-2026.md +1428 -0
  121. package/research/llm-summarization/anthropic-2026.md +367 -0
  122. package/research/llm-summarization/claude-cli-integration.md +1706 -0
  123. package/research/llm-summarization/cli-integration-patterns.md +3155 -0
  124. package/research/llm-summarization/openai-2026.md +473 -0
  125. package/research/llm-summarization/openai-compatible-providers-2026.md +1022 -0
  126. package/research/llm-summarization/opencode-cli-integration.md +1552 -0
  127. package/research/llm-summarization/prompt-engineering-2026.md +1426 -0
  128. package/research/llm-summarization/prototype-results.md +56 -0
  129. package/research/llm-summarization/provider-switching-patterns-2026.md +2153 -0
  130. package/research/llm-summarization/typescript-llm-libraries-2026.md +2436 -0
  131. package/research/mdcontext-error-analysis.md +521 -0
  132. package/research/mdcontext-pudding/00-EXECUTIVE-SUMMARY.md +282 -0
  133. package/research/mdcontext-pudding/01-index-embed.md +956 -0
  134. package/research/mdcontext-pudding/02-search-COMMANDS.md +142 -0
  135. package/research/mdcontext-pudding/02-search-SUMMARY.md +146 -0
  136. package/research/mdcontext-pudding/02-search.md +970 -0
  137. package/research/mdcontext-pudding/03-context.md +779 -0
  138. package/research/mdcontext-pudding/04-navigation-and-analytics.md +803 -0
  139. package/research/mdcontext-pudding/04-tree.md +704 -0
  140. package/research/mdcontext-pudding/05-config.md +1038 -0
  141. package/research/mdcontext-pudding/06-links-summary.txt +87 -0
  142. package/research/mdcontext-pudding/06-links.md +679 -0
  143. package/research/mdcontext-pudding/07-stats.md +693 -0
  144. package/research/mdcontext-pudding/BUG-FIX-PLAN.md +388 -0
  145. package/research/mdcontext-pudding/P0-BUG-VALIDATION.md +167 -0
  146. package/research/mdcontext-pudding/README.md +168 -0
  147. package/research/mdcontext-pudding/TESTING-SUMMARY.md +128 -0
  148. package/research/npm_publish/011-npm-workflow-research-agent2.md +792 -0
  149. package/research/npm_publish/012-npm-workflow-research-agent1.md +530 -0
  150. package/research/npm_publish/013-npm-workflow-research-agent3.md +722 -0
  151. package/research/npm_publish/014-npm-workflow-synthesis.md +556 -0
  152. package/research/npm_publish/031-npm-workflow-task-analysis.md +134 -0
  153. package/research/research-quality-review.md +834 -0
  154. package/research/semantic-search/002-research-embedding-models.md +490 -0
  155. package/research/semantic-search/003-research-rag-alternatives.md +523 -0
  156. package/research/semantic-search/004-research-vector-search.md +841 -0
  157. package/research/semantic-search/032-research-semantic-search.md +427 -0
  158. package/research/semantic-search/embedding-text-analysis.md +156 -0
  159. package/research/semantic-search/multi-word-failure-reproduction.md +171 -0
  160. package/research/semantic-search/query-processing-analysis.md +207 -0
  161. package/research/semantic-search/root-cause-and-solution.md +114 -0
  162. package/research/semantic-search/threshold-validation-report.md +69 -0
  163. package/research/semantic-search/vector-search-analysis.md +63 -0
  164. package/research/task-management-2026/00-synthesis-recommendations.md +295 -0
  165. package/research/task-management-2026/01-ai-workflow-tools.md +416 -0
  166. package/research/task-management-2026/02-agent-framework-patterns.md +476 -0
  167. package/research/task-management-2026/03-lightweight-file-based.md +567 -0
  168. package/research/task-management-2026/04-established-tools-ai-features.md +541 -0
  169. package/research/task-management-2026/linear/01-core-features-workflow.md +771 -0
  170. package/research/task-management-2026/linear/02-api-integrations.md +930 -0
  171. package/research/task-management-2026/linear/03-ai-features.md +368 -0
  172. package/research/task-management-2026/linear/04-pricing-setup.md +205 -0
  173. package/research/task-management-2026/linear/05-usage-patterns-best-practices.md +605 -0
  174. package/research/test-path-issues.md +276 -0
  175. package/review/ALP-76/1-error-type-design.md +962 -0
  176. package/review/ALP-76/2-error-handling-patterns.md +906 -0
  177. package/review/ALP-76/3-error-presentation.md +624 -0
  178. package/review/ALP-76/4-test-coverage.md +625 -0
  179. package/review/ALP-76/5-migration-completeness.md +440 -0
  180. package/review/ALP-76/6-effect-best-practices.md +755 -0
  181. package/scripts/apply-branch-protection.sh +47 -0
  182. package/scripts/branch-protection-templates.json +79 -0
  183. package/scripts/prototype-summarization.ts +346 -0
  184. package/scripts/rebuild-hnswlib.js +58 -0
  185. package/scripts/setup-branch-protection.sh +64 -0
  186. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/active-provider.json +7 -0
  187. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/bm25.json +541 -0
  188. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/bm25.meta.json +5 -0
  189. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/config.json +8 -0
  190. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/embeddings/openai_text-embedding-3-small_512/vectors.bin +0 -0
  191. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/embeddings/openai_text-embedding-3-small_512/vectors.meta.bin +0 -0
  192. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/indexes/documents.json +60 -0
  193. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/indexes/links.json +13 -0
  194. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/.mdcontext/indexes/sections.json +1197 -0
  195. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/configuration-management.md +99 -0
  196. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/distributed-systems.md +92 -0
  197. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/error-handling.md +78 -0
  198. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/failure-automation.md +55 -0
  199. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/job-context.md +69 -0
  200. package/src/__tests__/fixtures/semantic-search/multi-word-corpus/process-orchestration.md +99 -0
  201. package/src/cli/argv-preprocessor.test.ts +210 -0
  202. package/src/cli/argv-preprocessor.ts +202 -0
  203. package/src/cli/cli.test.ts +627 -0
  204. package/src/cli/commands/backlinks.ts +54 -0
  205. package/src/cli/commands/config-cmd.ts +642 -0
  206. package/src/cli/commands/context.ts +285 -0
  207. package/src/cli/commands/duplicates.ts +122 -0
  208. package/src/cli/commands/embeddings.ts +529 -0
  209. package/src/cli/commands/index-cmd.ts +480 -0
  210. package/src/cli/commands/index.ts +16 -0
  211. package/src/cli/commands/links.ts +52 -0
  212. package/src/cli/commands/search.ts +1281 -0
  213. package/src/cli/commands/stats.ts +149 -0
  214. package/src/cli/commands/tree.ts +128 -0
  215. package/src/cli/config-layer.ts +176 -0
  216. package/src/cli/error-handler.test.ts +235 -0
  217. package/src/cli/error-handler.ts +655 -0
  218. package/src/cli/flag-schemas.ts +341 -0
  219. package/src/cli/help.ts +588 -0
  220. package/src/cli/index.ts +9 -0
  221. package/src/cli/main.ts +435 -0
  222. package/src/cli/options.ts +41 -0
  223. package/src/cli/shared-error-handling.ts +199 -0
  224. package/src/cli/typo-suggester.test.ts +105 -0
  225. package/src/cli/typo-suggester.ts +130 -0
  226. package/src/cli/utils.ts +259 -0
  227. package/src/config/file-provider.test.ts +320 -0
  228. package/src/config/file-provider.ts +273 -0
  229. package/src/config/index.ts +72 -0
  230. package/src/config/integration.test.ts +667 -0
  231. package/src/config/precedence.test.ts +277 -0
  232. package/src/config/precedence.ts +451 -0
  233. package/src/config/schema.test.ts +414 -0
  234. package/src/config/schema.ts +603 -0
  235. package/src/config/service.test.ts +320 -0
  236. package/src/config/service.ts +243 -0
  237. package/src/config/testing.test.ts +264 -0
  238. package/src/config/testing.ts +110 -0
  239. package/src/core/index.ts +1 -0
  240. package/src/core/types.ts +113 -0
  241. package/src/duplicates/detector.test.ts +183 -0
  242. package/src/duplicates/detector.ts +414 -0
  243. package/src/duplicates/index.ts +18 -0
  244. package/src/embeddings/embedding-namespace.test.ts +300 -0
  245. package/src/embeddings/embedding-namespace.ts +947 -0
  246. package/src/embeddings/heading-boost.test.ts +222 -0
  247. package/src/embeddings/hnsw-build-options.test.ts +198 -0
  248. package/src/embeddings/hyde.test.ts +272 -0
  249. package/src/embeddings/hyde.ts +264 -0
  250. package/src/embeddings/index.ts +10 -0
  251. package/src/embeddings/openai-provider.ts +414 -0
  252. package/src/embeddings/pricing.json +22 -0
  253. package/src/embeddings/provider-constants.ts +204 -0
  254. package/src/embeddings/provider-errors.test.ts +967 -0
  255. package/src/embeddings/provider-errors.ts +565 -0
  256. package/src/embeddings/provider-factory.test.ts +240 -0
  257. package/src/embeddings/provider-factory.ts +225 -0
  258. package/src/embeddings/provider-integration.test.ts +788 -0
  259. package/src/embeddings/query-preprocessing.test.ts +187 -0
  260. package/src/embeddings/semantic-search-threshold.test.ts +508 -0
  261. package/src/embeddings/semantic-search.ts +1270 -0
  262. package/src/embeddings/types.ts +359 -0
  263. package/src/embeddings/vector-store.ts +708 -0
  264. package/src/embeddings/voyage-provider.ts +313 -0
  265. package/src/errors/errors.test.ts +845 -0
  266. package/src/errors/index.ts +533 -0
  267. package/src/index/ignore-patterns.test.ts +354 -0
  268. package/src/index/ignore-patterns.ts +305 -0
  269. package/src/index/index.ts +4 -0
  270. package/src/index/indexer.ts +684 -0
  271. package/src/index/storage.ts +260 -0
  272. package/src/index/types.ts +147 -0
  273. package/src/index/watcher.ts +189 -0
  274. package/src/index.ts +30 -0
  275. package/src/integration/search-keyword.test.ts +678 -0
  276. package/src/mcp/server.ts +612 -0
  277. package/src/parser/index.ts +1 -0
  278. package/src/parser/parser.test.ts +291 -0
  279. package/src/parser/parser.ts +394 -0
  280. package/src/parser/section-filter.test.ts +277 -0
  281. package/src/parser/section-filter.ts +392 -0
  282. package/src/search/__tests__/hybrid-search.test.ts +650 -0
  283. package/src/search/bm25-store.ts +366 -0
  284. package/src/search/cross-encoder.test.ts +253 -0
  285. package/src/search/cross-encoder.ts +406 -0
  286. package/src/search/fuzzy-search.test.ts +419 -0
  287. package/src/search/fuzzy-search.ts +273 -0
  288. package/src/search/hybrid-search.ts +448 -0
  289. package/src/search/path-matcher.test.ts +276 -0
  290. package/src/search/path-matcher.ts +33 -0
  291. package/src/search/query-parser.test.ts +260 -0
  292. package/src/search/query-parser.ts +319 -0
  293. package/src/search/searcher.test.ts +280 -0
  294. package/src/search/searcher.ts +724 -0
  295. package/src/search/wink-bm25.d.ts +30 -0
  296. package/src/summarization/cli-providers/claude.ts +202 -0
  297. package/src/summarization/cli-providers/detection.test.ts +273 -0
  298. package/src/summarization/cli-providers/detection.ts +118 -0
  299. package/src/summarization/cli-providers/index.ts +8 -0
  300. package/src/summarization/cost.test.ts +139 -0
  301. package/src/summarization/cost.ts +102 -0
  302. package/src/summarization/error-handler.test.ts +127 -0
  303. package/src/summarization/error-handler.ts +111 -0
  304. package/src/summarization/index.ts +102 -0
  305. package/src/summarization/pipeline.test.ts +498 -0
  306. package/src/summarization/pipeline.ts +231 -0
  307. package/src/summarization/prompts.test.ts +269 -0
  308. package/src/summarization/prompts.ts +133 -0
  309. package/src/summarization/provider-factory.test.ts +396 -0
  310. package/src/summarization/provider-factory.ts +178 -0
  311. package/src/summarization/types.ts +184 -0
  312. package/src/summarize/budget-bugs.test.ts +620 -0
  313. package/src/summarize/formatters.ts +419 -0
  314. package/src/summarize/index.ts +20 -0
  315. package/src/summarize/summarizer.test.ts +275 -0
  316. package/src/summarize/summarizer.ts +597 -0
  317. package/src/summarize/verify-bugs.test.ts +238 -0
  318. package/src/types/huggingface-transformers.d.ts +66 -0
  319. package/src/utils/index.ts +1 -0
  320. package/src/utils/tokens.test.ts +142 -0
  321. package/src/utils/tokens.ts +186 -0
  322. package/tests/fixtures/cli/.mdcontext/active-provider.json +7 -0
  323. package/tests/fixtures/cli/.mdcontext/config.json +8 -0
  324. package/tests/fixtures/cli/.mdcontext/embeddings/openai_text-embedding-3-small_512/vectors.bin +0 -0
  325. package/tests/fixtures/cli/.mdcontext/embeddings/openai_text-embedding-3-small_512/vectors.meta.bin +0 -0
  326. package/tests/fixtures/cli/.mdcontext/indexes/documents.json +33 -0
  327. package/tests/fixtures/cli/.mdcontext/indexes/links.json +12 -0
  328. package/tests/fixtures/cli/.mdcontext/indexes/sections.json +247 -0
  329. package/tests/fixtures/cli/README.md +9 -0
  330. package/tests/fixtures/cli/api-reference.md +11 -0
  331. package/tests/fixtures/cli/getting-started.md +11 -0
  332. package/tests/integration/embed-index.test.ts +712 -0
  333. package/tests/integration/search-context.test.ts +469 -0
  334. package/tests/integration/search-semantic.test.ts +522 -0
  335. package/tsconfig.json +26 -0
  336. package/vitest.config.ts +16 -0
  337. package/vitest.setup.ts +12 -0
@@ -0,0 +1,2636 @@
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-2W7MO2DL.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
+ var TOKEN_BUDGETS = {
562
+ brief: 100,
563
+ summary: 500,
564
+ full: Infinity
565
+ };
566
+ var MIN_SENTENCE_LENGTH = 10;
567
+ var SENTENCE_SCORE_DEFINITION = 2;
568
+ var SENTENCE_SCORE_PROPER_START = 1;
569
+ var SENTENCE_SCORE_MEDIUM_LENGTH = 1;
570
+ var SENTENCE_SCORE_EMPHASIS = 1;
571
+ var SENTENCE_LENGTH_MIN = 50;
572
+ var SENTENCE_LENGTH_MAX = 200;
573
+ var SUMMARY_COMPRESSION_RATIO = 0.3;
574
+ var MIN_SECTION_TOKENS = 20;
575
+ var MIN_SUMMARY_SENTENCES = 2;
576
+ var TOKENS_PER_SENTENCE_ESTIMATE = 30;
577
+ var MIN_TOPIC_LENGTH = 2;
578
+ var MAX_TOPIC_LENGTH = 50;
579
+ var MAX_TOPICS = 10;
580
+ var MIN_PARTIAL_BUDGET = 50;
581
+ var extractKeyPoints = (content, maxSentences) => {
582
+ const sentences = content.replace(/\n+/g, " ").split(/(?<=[.!?])\s+/).filter((s) => s.trim().length > MIN_SENTENCE_LENGTH);
583
+ if (sentences.length <= maxSentences) {
584
+ return sentences;
585
+ }
586
+ const scored = sentences.map((s) => {
587
+ let score = 0;
588
+ if (s.includes(":")) score += SENTENCE_SCORE_DEFINITION;
589
+ if (/^[A-Z]/.test(s)) score += SENTENCE_SCORE_PROPER_START;
590
+ if (s.length > SENTENCE_LENGTH_MIN && s.length < SENTENCE_LENGTH_MAX)
591
+ score += SENTENCE_SCORE_MEDIUM_LENGTH;
592
+ if (/\*\*|`/.test(s)) score += SENTENCE_SCORE_EMPHASIS;
593
+ return { sentence: s, score };
594
+ });
595
+ scored.sort((a, b) => b.score - a.score);
596
+ return scored.slice(0, maxSentences).map((s) => s.sentence);
597
+ };
598
+ var summarizeSection = (section, level) => {
599
+ const originalTokens = section.metadata.tokenCount;
600
+ const children = section.children.map(
601
+ (child) => summarizeSection(child, level)
602
+ );
603
+ const targetTokens = Math.min(
604
+ TOKEN_BUDGETS[level],
605
+ Math.max(originalTokens * SUMMARY_COMPRESSION_RATIO, MIN_SECTION_TOKENS)
606
+ );
607
+ let summary;
608
+ if (level === "full" || originalTokens <= targetTokens) {
609
+ summary = section.plainText;
610
+ } else if (level === "brief") {
611
+ const meta = [];
612
+ if (section.metadata.hasCode) meta.push("code");
613
+ if (section.metadata.hasList) meta.push("list");
614
+ if (section.metadata.hasTable) meta.push("table");
615
+ summary = meta.length > 0 ? `[${meta.join(", ")}]` : "";
616
+ } else {
617
+ const maxSentences = Math.max(
618
+ MIN_SUMMARY_SENTENCES,
619
+ Math.floor(targetTokens / TOKENS_PER_SENTENCE_ESTIMATE)
620
+ );
621
+ const keyPoints = extractKeyPoints(section.plainText, maxSentences);
622
+ if (keyPoints.length > 0) {
623
+ summary = keyPoints.join(" ");
624
+ } else {
625
+ const words = section.plainText.split(/\s+/).slice(0, targetTokens);
626
+ summary = words.join(" ") + (words.length < section.plainText.split(/\s+/).length ? "..." : "");
627
+ }
628
+ }
629
+ const summaryTokens = countTokensApprox(summary);
630
+ return {
631
+ heading: section.heading,
632
+ level: section.level,
633
+ originalTokens,
634
+ summaryTokens,
635
+ summary,
636
+ children,
637
+ hasCode: section.metadata.hasCode,
638
+ hasList: section.metadata.hasList,
639
+ hasTable: section.metadata.hasTable
640
+ };
641
+ };
642
+ var extractTopics = (document) => {
643
+ const topics = /* @__PURE__ */ new Set();
644
+ const processSection = (section) => {
645
+ const cleanHeading = section.heading.replace(/[:#\-_]/g, " ").trim().toLowerCase();
646
+ if (cleanHeading.length > MIN_TOPIC_LENGTH && cleanHeading.length < MAX_TOPIC_LENGTH) {
647
+ topics.add(cleanHeading);
648
+ }
649
+ for (const child of section.children) {
650
+ processSection(child);
651
+ }
652
+ };
653
+ for (const section of document.sections) {
654
+ processSection(section);
655
+ }
656
+ const frontmatter = document.frontmatter;
657
+ if (frontmatter.tags && Array.isArray(frontmatter.tags)) {
658
+ for (const tag of frontmatter.tags) {
659
+ if (typeof tag === "string") {
660
+ topics.add(tag.toLowerCase());
661
+ }
662
+ }
663
+ }
664
+ return Array.from(topics).slice(0, MAX_TOPICS);
665
+ };
666
+ var summarizeDocument = (document, options = {}) => {
667
+ const level = options.level ?? "summary";
668
+ const maxTokens = options.maxTokens ?? TOKEN_BUDGETS[level];
669
+ const allSections = document.sections.map((s) => summarizeSection(s, level));
670
+ const originalTokens = document.metadata.tokenCount;
671
+ let totalSummaryTokens = 0;
672
+ const flatSections = [];
673
+ const flattenWithTokens = (section) => {
674
+ flatSections.push(section);
675
+ totalSummaryTokens += section.summaryTokens;
676
+ for (const child of section.children) {
677
+ flattenWithTokens(child);
678
+ }
679
+ };
680
+ for (const section of allSections) {
681
+ flattenWithTokens(section);
682
+ }
683
+ const topics = extractTopics(document);
684
+ const headerTemplate = `# ${document.title}
685
+ Path: ${document.path}
686
+ Tokens: 9999 (99% reduction from ${document.metadata.tokenCount})
687
+ `;
688
+ const topicsLine = topics.length > 0 ? `
689
+ **Topics:** ${topics.join(", ")}
690
+ ` : "";
691
+ const truncationWarning = "\n\u26A0\uFE0F TRUNCATED: 999 sections omitted to fit token budget";
692
+ const baseOverhead = countTokensApprox(
693
+ headerTemplate + topicsLine + truncationWarning
694
+ );
695
+ const formattingOverhead = Math.ceil(baseOverhead * 1.2) + 20;
696
+ const contentBudget = maxTokens - formattingOverhead;
697
+ let truncated = false;
698
+ let truncatedCount = 0;
699
+ let sections;
700
+ let summaryTokens;
701
+ if (totalSummaryTokens > contentBudget && contentBudget > 0) {
702
+ let tokensUsed = 0;
703
+ const truncateSections = (sectionList) => {
704
+ const result2 = [];
705
+ for (const section of sectionList) {
706
+ const sectionOwnTokens = section.summaryTokens;
707
+ const fitsInBudget = tokensUsed + sectionOwnTokens <= contentBudget;
708
+ if (fitsInBudget) {
709
+ tokensUsed += sectionOwnTokens;
710
+ const truncatedChildren = truncateSections(section.children);
711
+ result2.push({
712
+ ...section,
713
+ children: truncatedChildren
714
+ });
715
+ } else {
716
+ truncatedCount++;
717
+ const rescuedChildren = truncateSections(section.children);
718
+ result2.push(...rescuedChildren);
719
+ }
720
+ }
721
+ return result2;
722
+ };
723
+ sections = truncateSections(allSections);
724
+ summaryTokens = tokensUsed;
725
+ truncated = truncatedCount > 0;
726
+ } else {
727
+ sections = allSections;
728
+ summaryTokens = totalSummaryTokens;
729
+ }
730
+ const compressionRatio = originalTokens > 0 ? 1 - summaryTokens / originalTokens : 0;
731
+ const result = {
732
+ path: document.path,
733
+ title: document.title,
734
+ originalTokens,
735
+ summaryTokens,
736
+ compressionRatio,
737
+ sections,
738
+ keyTopics: topics
739
+ };
740
+ if (truncated) {
741
+ return {
742
+ ...result,
743
+ truncated: true,
744
+ truncatedCount
745
+ };
746
+ }
747
+ return result;
748
+ };
749
+ var summarizeFile = (filePath, options = {}) => Effect5.gen(function* () {
750
+ const document = yield* parseFile(filePath);
751
+ return summarizeDocument(document, options);
752
+ });
753
+ var assembleContext = (rootPath, sourcePaths, options) => Effect5.gen(function* () {
754
+ const budget = options.budget;
755
+ const level = options.level ?? "summary";
756
+ const sources = [];
757
+ const overflow = [];
758
+ let totalTokens = 0;
759
+ const perSourceBudget = Math.floor(budget / sourcePaths.length);
760
+ for (const sourcePath of sourcePaths) {
761
+ const resolvedPath = path2.isAbsolute(sourcePath) ? sourcePath : path2.join(rootPath, sourcePath);
762
+ const summaryResult = yield* summarizeFile(resolvedPath, {
763
+ level,
764
+ maxTokens: perSourceBudget
765
+ }).pipe(
766
+ Effect5.map((s) => s),
767
+ // Log error for observability before gracefully degrading
768
+ Effect5.tapError(
769
+ (error) => Effect5.logError(`Failed to summarize ${sourcePath}`, error)
770
+ ),
771
+ // Note: catchAll intentional for batch processing - individual file
772
+ // failures add to overflow instead of stopping assembly
773
+ Effect5.catchAll(() => Effect5.succeed(null))
774
+ );
775
+ if (!summaryResult) {
776
+ overflow.push(sourcePath);
777
+ continue;
778
+ }
779
+ const summary = summaryResult;
780
+ const content = formatSummary(summary);
781
+ const tokens = countTokensApprox(content);
782
+ if (totalTokens + tokens <= budget) {
783
+ sources.push({
784
+ path: path2.relative(rootPath, resolvedPath),
785
+ title: summary.title,
786
+ tokens,
787
+ content
788
+ });
789
+ totalTokens += tokens;
790
+ } else {
791
+ const remaining = budget - totalTokens;
792
+ if (remaining > MIN_PARTIAL_BUDGET) {
793
+ const briefSummary = yield* summarizeFile(resolvedPath, {
794
+ level: "brief",
795
+ maxTokens: remaining
796
+ }).pipe(
797
+ Effect5.map((s) => s),
798
+ // Log error for observability before gracefully degrading
799
+ Effect5.tapError(
800
+ (error) => Effect5.logError(
801
+ `Failed to create brief summary for ${sourcePath}`,
802
+ error
803
+ )
804
+ ),
805
+ Effect5.catchAll(
806
+ () => Effect5.succeed(null)
807
+ )
808
+ );
809
+ if (briefSummary) {
810
+ const briefContent = formatSummary(briefSummary);
811
+ const briefTokens = countTokensApprox(briefContent);
812
+ sources.push({
813
+ path: path2.relative(rootPath, resolvedPath),
814
+ title: briefSummary.title,
815
+ tokens: briefTokens,
816
+ content: briefContent
817
+ });
818
+ totalTokens += briefTokens;
819
+ } else {
820
+ overflow.push(path2.relative(rootPath, resolvedPath));
821
+ }
822
+ } else {
823
+ overflow.push(path2.relative(rootPath, resolvedPath));
824
+ }
825
+ }
826
+ }
827
+ return {
828
+ sources,
829
+ totalTokens,
830
+ budget,
831
+ overflow
832
+ };
833
+ });
834
+
835
+ // src/embeddings/semantic-search.ts
836
+ import { Effect as Effect11 } from "effect";
837
+ import * as fs4 from "fs/promises";
838
+ import * as path4 from "path";
839
+
840
+ // src/embeddings/hyde.ts
841
+ import { Effect as Effect6, Redacted } from "effect";
842
+ import OpenAI from "openai";
843
+ var DEFAULT_MODEL = "gpt-4o-mini";
844
+ var DEFAULT_MAX_TOKENS = 256;
845
+ var DEFAULT_TEMPERATURE = 0.3;
846
+ 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.
847
+
848
+ Guidelines:
849
+ - Write 2-4 concise paragraphs
850
+ - Use technical but accessible language
851
+ - Include specific details, code examples, or configuration options where relevant
852
+ - Focus on directly answering the question
853
+ - Do not include greetings, preambles, or meta-commentary
854
+ - Write as if this is an excerpt from existing documentation`;
855
+ var LLM_PRICING = {
856
+ "gpt-4o-mini": { input: 0.15, output: 0.6 },
857
+ "gpt-4o": { input: 2.5, output: 10 },
858
+ "gpt-4-turbo": { input: 10, output: 30 },
859
+ "gpt-3.5-turbo": { input: 0.5, output: 1.5 }
860
+ };
861
+ var generateHypotheticalDocument = (query, options = {}) => Effect6.gen(function* () {
862
+ const rawApiKey = options.apiKey ?? process.env.OPENAI_API_KEY;
863
+ if (!rawApiKey) {
864
+ return yield* Effect6.fail(
865
+ new ApiKeyMissingError({
866
+ provider: "OpenAI",
867
+ envVar: "OPENAI_API_KEY"
868
+ })
869
+ );
870
+ }
871
+ const redactedApiKey = Redacted.isRedacted(rawApiKey) ? rawApiKey : Redacted.make(rawApiKey);
872
+ const client = new OpenAI({
873
+ apiKey: Redacted.value(redactedApiKey),
874
+ // Only expose when creating client
875
+ baseURL: options.baseURL
876
+ });
877
+ const model = options.model ?? DEFAULT_MODEL;
878
+ const maxTokens = options.maxTokens ?? DEFAULT_MAX_TOKENS;
879
+ const temperature = options.temperature ?? DEFAULT_TEMPERATURE;
880
+ const systemPrompt = options.systemPrompt ?? DEFAULT_SYSTEM_PROMPT;
881
+ const response = yield* Effect6.tryPromise({
882
+ try: async () => client.chat.completions.create({
883
+ model,
884
+ messages: [
885
+ { role: "system", content: systemPrompt },
886
+ { role: "user", content: query }
887
+ ],
888
+ max_tokens: maxTokens,
889
+ temperature
890
+ }),
891
+ catch: (error) => new EmbeddingError({
892
+ reason: classifyLLMError(error),
893
+ message: error instanceof Error ? error.message : String(error),
894
+ provider: "openai",
895
+ cause: error
896
+ })
897
+ });
898
+ const content = response.choices[0]?.message?.content ?? "";
899
+ const inputTokens = response.usage?.prompt_tokens ?? 0;
900
+ const outputTokens = response.usage?.completion_tokens ?? 0;
901
+ const totalTokens = inputTokens + outputTokens;
902
+ const pricing = LLM_PRICING[model] ?? LLM_PRICING["gpt-4o-mini"];
903
+ const cost = inputTokens / 1e6 * pricing.input + outputTokens / 1e6 * pricing.output;
904
+ return {
905
+ hypotheticalDocument: content,
906
+ originalQuery: query,
907
+ model,
908
+ tokensUsed: totalTokens,
909
+ cost
910
+ };
911
+ });
912
+ var classifyLLMError = (error) => {
913
+ if (error instanceof OpenAI.RateLimitError) {
914
+ return "RateLimit";
915
+ }
916
+ if (error instanceof OpenAI.BadRequestError) {
917
+ const msg2 = (error.message || "").toLowerCase();
918
+ if (msg2.includes("model")) return "ModelError";
919
+ }
920
+ if (error instanceof OpenAI.APIConnectionError) {
921
+ return "Network";
922
+ }
923
+ if (!(error instanceof Error)) return "Unknown";
924
+ const msg = error.message.toLowerCase();
925
+ if (msg.includes("429") || msg.includes("rate limit")) return "RateLimit";
926
+ if (msg.includes("quota") || msg.includes("billing")) return "QuotaExceeded";
927
+ if (msg.includes("econnrefused") || msg.includes("network")) return "Network";
928
+ if (msg.includes("model") && msg.includes("not found")) return "ModelError";
929
+ return "Unknown";
930
+ };
931
+
932
+ // src/embeddings/openai-provider.ts
933
+ import { Effect as Effect7, Redacted as Redacted2 } from "effect";
934
+ import OpenAI2 from "openai";
935
+
936
+ // src/embeddings/provider-constants.ts
937
+ var MODEL_DIMENSIONS = {
938
+ // OpenAI models (support MRL dimension reduction)
939
+ "text-embedding-3-small": 1536,
940
+ // Native: 1536, supports reduction
941
+ "text-embedding-3-large": 3072,
942
+ // Native: 3072, supports reduction
943
+ "text-embedding-ada-002": 1536,
944
+ // Native: 1536, does NOT support reduction
945
+ // Ollama models (fixed native dimensions)
946
+ "nomic-embed-text": 768,
947
+ "mxbai-embed-large": 1024,
948
+ "bge-m3": 1024,
949
+ "all-minilm": 384,
950
+ "snowflake-arctic-embed": 1024,
951
+ // Voyage AI models (fixed native dimensions)
952
+ "voyage-3.5-lite": 1024,
953
+ // Best value: $0.02/1M tokens
954
+ "voyage-3": 1024,
955
+ // Higher quality: $0.06/1M tokens
956
+ "voyage-code-3": 1024,
957
+ // Code-optimized: $0.18/1M tokens
958
+ "voyage-2": 1024,
959
+ "voyage-large-2": 1536,
960
+ "voyage-code-2": 1536
961
+ };
962
+ var MATRYOSHKA_MODELS = /* @__PURE__ */ new Set([
963
+ "text-embedding-3-small",
964
+ "text-embedding-3-large"
965
+ ]);
966
+ var supportsMatryoshka = (model) => MATRYOSHKA_MODELS.has(model);
967
+ var getRecommendedDimensions = (model) => {
968
+ if (supportsMatryoshka(model)) {
969
+ return 512;
970
+ }
971
+ return MODEL_DIMENSIONS[model];
972
+ };
973
+ var validateModelDimensions = (model, dimensions) => {
974
+ const nativeDims = MODEL_DIMENSIONS[model];
975
+ if (nativeDims === void 0) {
976
+ return { isValid: true };
977
+ }
978
+ if (dimensions > nativeDims) {
979
+ return {
980
+ isValid: false,
981
+ warning: `Model '${model}' has ${nativeDims} native dimensions, cannot use ${dimensions}`
982
+ };
983
+ }
984
+ if (!supportsMatryoshka(model) && dimensions !== nativeDims) {
985
+ return {
986
+ isValid: false,
987
+ warning: `Model '${model}' does not support dimension reduction, must use ${nativeDims}`
988
+ };
989
+ }
990
+ return { isValid: true };
991
+ };
992
+ var PROVIDER_BASE_URLS = {
993
+ openai: void 0,
994
+ // Use OpenAI SDK default
995
+ ollama: "http://localhost:11434/v1",
996
+ "lm-studio": "http://localhost:1234/v1",
997
+ openrouter: "https://openrouter.ai/api/v1",
998
+ voyage: "https://api.voyageai.com/v1"
999
+ // Native API, handled by VoyageProvider
1000
+ };
1001
+ var extractPortFromUrl = (url) => {
1002
+ const match = url.match(/:(\d+)\//);
1003
+ if (!match?.[1]) return void 0;
1004
+ return parseInt(match[1], 10);
1005
+ };
1006
+ var PROVIDER_PORTS = (() => {
1007
+ const ports = {};
1008
+ for (const [provider, url] of Object.entries(PROVIDER_BASE_URLS)) {
1009
+ if (url) {
1010
+ const port = extractPortFromUrl(url);
1011
+ if (port) ports[provider] = port;
1012
+ }
1013
+ }
1014
+ return ports;
1015
+ })();
1016
+ var inferProviderFromUrl = (baseURL) => {
1017
+ if (!baseURL) return "openai";
1018
+ for (const [provider, providerUrl] of Object.entries(PROVIDER_BASE_URLS)) {
1019
+ if (providerUrl && baseURL.includes(providerUrl.replace("/v1", ""))) {
1020
+ return provider;
1021
+ }
1022
+ }
1023
+ if (baseURL.includes("openrouter")) return "openrouter";
1024
+ return "openai";
1025
+ };
1026
+
1027
+ // src/embeddings/openai-provider.ts
1028
+ var PRICING_DATA = {
1029
+ /** Last update date in YYYY-MM format */
1030
+ lastUpdated: "2024-09",
1031
+ /** Source URL for verification */
1032
+ source: "https://platform.openai.com/docs/pricing",
1033
+ /** Prices per 1M tokens by model */
1034
+ prices: {
1035
+ "text-embedding-3-small": 0.02,
1036
+ "text-embedding-3-large": 0.13,
1037
+ "text-embedding-ada-002": 0.1
1038
+ }
1039
+ };
1040
+ var checkPricingFreshness = () => {
1041
+ const [year, month] = PRICING_DATA.lastUpdated.split("-").map(Number);
1042
+ if (!year || !month) return null;
1043
+ const lastUpdated = new Date(year, month - 1, 1);
1044
+ const now = /* @__PURE__ */ new Date();
1045
+ const daysSince = Math.floor(
1046
+ (now.getTime() - lastUpdated.getTime()) / (1e3 * 60 * 60 * 24)
1047
+ );
1048
+ if (daysSince > 90) {
1049
+ return `Pricing data is ${daysSince} days old. May not reflect current rates.`;
1050
+ }
1051
+ return null;
1052
+ };
1053
+ var getPricingDate = () => PRICING_DATA.lastUpdated;
1054
+ var OpenAIProvider = class _OpenAIProvider {
1055
+ name;
1056
+ dimensions;
1057
+ /** Provider name for error context */
1058
+ providerName;
1059
+ /** Model name */
1060
+ model;
1061
+ /** Base URL for API requests */
1062
+ baseURL;
1063
+ client;
1064
+ batchSize;
1065
+ constructor(apiKey, options = {}) {
1066
+ this.baseURL = options.baseURL;
1067
+ this.client = new OpenAI2({
1068
+ apiKey: Redacted2.value(apiKey),
1069
+ // Only expose API key when creating client
1070
+ baseURL: options.baseURL
1071
+ // If undefined, SDK uses default https://api.openai.com/v1
1072
+ });
1073
+ this.model = options.model ?? "text-embedding-3-small";
1074
+ this.batchSize = options.batchSize ?? 100;
1075
+ this.providerName = options.providerName ?? this.inferProviderName(options.baseURL);
1076
+ this.name = `${this.providerName}:${this.model}`;
1077
+ const recommendedDims = getRecommendedDimensions(this.model);
1078
+ this.dimensions = options.dimensions ?? recommendedDims ?? 512;
1079
+ }
1080
+ /**
1081
+ * Infer the provider name from the baseURL.
1082
+ * Delegates to centralized inferProviderFromUrl for single source of truth.
1083
+ */
1084
+ inferProviderName(baseURL) {
1085
+ return inferProviderFromUrl(baseURL);
1086
+ }
1087
+ /**
1088
+ * Create an OpenAI provider instance.
1089
+ * Returns an Effect that fails with ApiKeyMissingError if no API key is available.
1090
+ *
1091
+ * API keys are handled securely using Effect's Redacted type to prevent
1092
+ * accidental logging of sensitive values.
1093
+ */
1094
+ static create(options = {}) {
1095
+ const isOpenRouter = options.baseURL?.includes("openrouter") || options.providerName === "openrouter";
1096
+ const resolveApiKey = () => {
1097
+ if (options.apiKey !== void 0) {
1098
+ return options.apiKey;
1099
+ }
1100
+ return (isOpenRouter ? process.env.OPENROUTER_API_KEY : void 0) ?? process.env.OPENAI_API_KEY;
1101
+ };
1102
+ const rawApiKey = resolveApiKey();
1103
+ if (!rawApiKey) {
1104
+ return Effect7.fail(
1105
+ new ApiKeyMissingError({
1106
+ provider: isOpenRouter ? "OpenRouter" : "OpenAI",
1107
+ envVar: isOpenRouter ? "OPENROUTER_API_KEY" : "OPENAI_API_KEY"
1108
+ })
1109
+ );
1110
+ }
1111
+ const redactedApiKey = Redacted2.isRedacted(rawApiKey) ? rawApiKey : Redacted2.make(rawApiKey);
1112
+ const apiKeyValue = Redacted2.value(redactedApiKey);
1113
+ const shouldWarnOpenRouter = isOpenRouter && apiKeyValue.startsWith("sk-") && !apiKeyValue.startsWith("sk-or-");
1114
+ const model = options.model ?? "text-embedding-3-small";
1115
+ const dimensionValidation = options.dimensions ? validateModelDimensions(model, options.dimensions) : { isValid: true };
1116
+ return Effect7.succeed(new _OpenAIProvider(redactedApiKey, options)).pipe(
1117
+ shouldWarnOpenRouter ? Effect7.tap(
1118
+ () => Effect7.logWarning(
1119
+ '\u26A0\uFE0F Using OpenAI key format with OpenRouter. Consider setting OPENROUTER_API_KEY with a key starting with "sk-or-"'
1120
+ )
1121
+ ) : (self) => self,
1122
+ // Warn about invalid dimension configuration
1123
+ dimensionValidation.warning ? Effect7.tap(
1124
+ () => Effect7.logWarning(`\u26A0\uFE0F ${dimensionValidation.warning}`)
1125
+ ) : (self) => self
1126
+ );
1127
+ }
1128
+ async embed(texts) {
1129
+ if (texts.length === 0) {
1130
+ return { embeddings: [], tokensUsed: 0, cost: 0 };
1131
+ }
1132
+ const allEmbeddings = [];
1133
+ let totalTokens = 0;
1134
+ try {
1135
+ for (let i = 0; i < texts.length; i += this.batchSize) {
1136
+ const batch = texts.slice(i, i + this.batchSize);
1137
+ const embedParams = {
1138
+ model: this.model,
1139
+ input: batch
1140
+ };
1141
+ if (supportsMatryoshka(this.model)) {
1142
+ embedParams.dimensions = this.dimensions;
1143
+ }
1144
+ const response = await this.client.embeddings.create(embedParams);
1145
+ for (const item of response.data) {
1146
+ allEmbeddings.push(item.embedding);
1147
+ }
1148
+ totalTokens += response.usage?.total_tokens ?? 0;
1149
+ }
1150
+ } catch (error) {
1151
+ if (error instanceof OpenAI2.AuthenticationError) {
1152
+ throw new ApiKeyInvalidError({
1153
+ provider: this.providerName,
1154
+ details: error.message
1155
+ });
1156
+ }
1157
+ throw new EmbeddingError({
1158
+ reason: this.classifyError(error),
1159
+ message: error instanceof Error ? error.message : String(error),
1160
+ provider: this.providerName,
1161
+ cause: error
1162
+ });
1163
+ }
1164
+ const pricePerMillion = this.providerName === "openai" || this.providerName === "openrouter" ? PRICING_DATA.prices[this.model] ?? 0.02 : 0;
1165
+ const cost = totalTokens / 1e6 * pricePerMillion;
1166
+ return {
1167
+ embeddings: allEmbeddings,
1168
+ tokensUsed: totalTokens,
1169
+ cost
1170
+ };
1171
+ }
1172
+ /**
1173
+ * Classify an error into a known category for better error handling.
1174
+ * Uses OpenAI SDK error types where available, falls back to string matching
1175
+ * for non-OpenAI providers (Ollama, LM Studio, OpenRouter).
1176
+ */
1177
+ classifyError(error) {
1178
+ if (error instanceof OpenAI2.RateLimitError) {
1179
+ return "RateLimit";
1180
+ }
1181
+ if (error instanceof OpenAI2.BadRequestError) {
1182
+ const msg2 = error.message.toLowerCase();
1183
+ if (msg2.includes("model")) return "ModelError";
1184
+ }
1185
+ if (error instanceof OpenAI2.APIConnectionError) {
1186
+ return "Network";
1187
+ }
1188
+ if (!(error instanceof Error)) return "Unknown";
1189
+ const msg = error.message.toLowerCase();
1190
+ if (msg.includes("429") || msg.includes("rate limit") || msg.includes("too many requests")) {
1191
+ return "RateLimit";
1192
+ }
1193
+ if (msg.includes("quota") || msg.includes("insufficient") || msg.includes("billing")) {
1194
+ return "QuotaExceeded";
1195
+ }
1196
+ if (msg.includes("econnrefused") || msg.includes("timeout") || msg.includes("network") || msg.includes("enotfound") || msg.includes("connection")) {
1197
+ return "Network";
1198
+ }
1199
+ if (msg.includes("model") && (msg.includes("not found") || msg.includes("not exist") || msg.includes("invalid"))) {
1200
+ return "ModelError";
1201
+ }
1202
+ return "Unknown";
1203
+ }
1204
+ };
1205
+ var createOpenAIProvider = (options) => OpenAIProvider.create(options);
1206
+ var wrapEmbedding = (embedPromise, providerName = "openai") => Effect7.tryPromise({
1207
+ try: () => embedPromise,
1208
+ catch: (e) => {
1209
+ if (e instanceof ApiKeyInvalidError) {
1210
+ return e;
1211
+ }
1212
+ return new EmbeddingError({
1213
+ reason: "Unknown",
1214
+ message: e instanceof Error ? e.message : String(e),
1215
+ provider: providerName,
1216
+ cause: e
1217
+ });
1218
+ }
1219
+ });
1220
+
1221
+ // src/embeddings/provider-factory.ts
1222
+ import { Effect as Effect9, Option } from "effect";
1223
+
1224
+ // src/embeddings/voyage-provider.ts
1225
+ import { Effect as Effect8 } from "effect";
1226
+ var VOYAGE_API_BASE = "https://api.voyageai.com/v1";
1227
+ var VOYAGE_MODELS = {
1228
+ "voyage-3.5-lite": { dimensions: 1024, pricePerMillion: 0.02 },
1229
+ "voyage-3": { dimensions: 1024, pricePerMillion: 0.06 },
1230
+ "voyage-code-3": { dimensions: 1024, pricePerMillion: 0.18 },
1231
+ // Legacy models
1232
+ "voyage-2": { dimensions: 1024, pricePerMillion: 0.1 },
1233
+ "voyage-large-2": { dimensions: 1536, pricePerMillion: 0.12 },
1234
+ "voyage-code-2": { dimensions: 1536, pricePerMillion: 0.12 }
1235
+ };
1236
+ var DEFAULT_VOYAGE_MODEL = "voyage-3.5-lite";
1237
+ var VoyageProvider = class _VoyageProvider {
1238
+ name;
1239
+ dimensions;
1240
+ model;
1241
+ baseURL = VOYAGE_API_BASE;
1242
+ providerName = "voyage";
1243
+ apiKey;
1244
+ batchSize;
1245
+ constructor(apiKey, options = {}) {
1246
+ this.apiKey = apiKey;
1247
+ this.model = options.model ?? DEFAULT_VOYAGE_MODEL;
1248
+ this.batchSize = options.batchSize ?? 128;
1249
+ const modelSpec = VOYAGE_MODELS[this.model];
1250
+ this.dimensions = modelSpec?.dimensions ?? 1024;
1251
+ this.name = `voyage:${this.model}`;
1252
+ }
1253
+ /**
1254
+ * Create a Voyage provider instance.
1255
+ * Returns an Effect that fails with ApiKeyMissingError if no API key is available.
1256
+ */
1257
+ static create(options = {}) {
1258
+ const apiKey = options.apiKey ?? process.env.VOYAGE_API_KEY;
1259
+ if (!apiKey) {
1260
+ return Effect8.fail(
1261
+ new ApiKeyMissingError({
1262
+ provider: "Voyage AI",
1263
+ envVar: "VOYAGE_API_KEY"
1264
+ })
1265
+ );
1266
+ }
1267
+ return Effect8.succeed(new _VoyageProvider(apiKey, options));
1268
+ }
1269
+ async embed(texts) {
1270
+ if (texts.length === 0) {
1271
+ return { embeddings: [], tokensUsed: 0, cost: 0 };
1272
+ }
1273
+ const allEmbeddings = [];
1274
+ let totalTokens = 0;
1275
+ try {
1276
+ for (let i = 0; i < texts.length; i += this.batchSize) {
1277
+ const batch = texts.slice(i, i + this.batchSize);
1278
+ const response = await fetch(`${VOYAGE_API_BASE}/embeddings`, {
1279
+ method: "POST",
1280
+ headers: {
1281
+ Authorization: `Bearer ${this.apiKey}`,
1282
+ "Content-Type": "application/json"
1283
+ },
1284
+ body: JSON.stringify({
1285
+ model: this.model,
1286
+ input: batch,
1287
+ input_type: "document"
1288
+ // 'document' for indexing, 'query' for searching
1289
+ })
1290
+ });
1291
+ if (!response.ok) {
1292
+ const errorText = await response.text();
1293
+ if (response.status === 401) {
1294
+ throw new ApiKeyInvalidError({
1295
+ provider: "Voyage AI",
1296
+ details: errorText
1297
+ });
1298
+ }
1299
+ throw new EmbeddingError({
1300
+ reason: this.classifyHttpError(response.status, errorText),
1301
+ message: `Voyage API error: ${response.status} - ${errorText}`,
1302
+ provider: "voyage"
1303
+ });
1304
+ }
1305
+ const data = await response.json();
1306
+ for (const item of data.data) {
1307
+ allEmbeddings.push(item.embedding);
1308
+ }
1309
+ totalTokens += data.usage?.total_tokens ?? 0;
1310
+ }
1311
+ } catch (error) {
1312
+ if (error instanceof ApiKeyInvalidError || error instanceof EmbeddingError) {
1313
+ throw error;
1314
+ }
1315
+ throw new EmbeddingError({
1316
+ reason: this.classifyError(error),
1317
+ message: error instanceof Error ? error.message : String(error),
1318
+ provider: "voyage",
1319
+ cause: error
1320
+ });
1321
+ }
1322
+ const pricePerMillion = VOYAGE_MODELS[this.model]?.pricePerMillion ?? 0.02;
1323
+ const cost = totalTokens / 1e6 * pricePerMillion;
1324
+ return {
1325
+ embeddings: allEmbeddings,
1326
+ tokensUsed: totalTokens,
1327
+ cost
1328
+ };
1329
+ }
1330
+ classifyHttpError(status, _message) {
1331
+ if (status === 429) return "RateLimit";
1332
+ if (status === 402) return "QuotaExceeded";
1333
+ if (status === 400) return "ModelError";
1334
+ return "Unknown";
1335
+ }
1336
+ classifyError(error) {
1337
+ if (!(error instanceof Error)) return "Unknown";
1338
+ const msg = error.message.toLowerCase();
1339
+ if (msg.includes("rate limit") || msg.includes("429")) return "RateLimit";
1340
+ if (msg.includes("quota") || msg.includes("billing")) return "QuotaExceeded";
1341
+ if (msg.includes("econnrefused") || msg.includes("timeout") || msg.includes("network"))
1342
+ return "Network";
1343
+ if (msg.includes("model") && msg.includes("not found")) return "ModelError";
1344
+ return "Unknown";
1345
+ }
1346
+ };
1347
+ var createVoyageProvider = (options) => VoyageProvider.create(options);
1348
+
1349
+ // src/embeddings/provider-factory.ts
1350
+ var getProviderBaseURL = (provider, configBaseURL) => {
1351
+ if (Option.isSome(configBaseURL)) {
1352
+ return configBaseURL.value;
1353
+ }
1354
+ return PROVIDER_BASE_URLS[provider];
1355
+ };
1356
+ var normalizeBaseURL = (baseURL) => {
1357
+ if (baseURL === void 0) {
1358
+ return Option.none();
1359
+ }
1360
+ if (typeof baseURL === "string") {
1361
+ return Option.some(baseURL);
1362
+ }
1363
+ return baseURL;
1364
+ };
1365
+ var normalizeApiKey = (apiKey) => {
1366
+ if (apiKey === void 0) {
1367
+ return void 0;
1368
+ }
1369
+ if (typeof apiKey === "string") {
1370
+ return apiKey;
1371
+ }
1372
+ return Option.isSome(apiKey) ? apiKey.value : void 0;
1373
+ };
1374
+ var createEmbeddingProviderDirect = (config) => Effect9.gen(function* () {
1375
+ const provider = config.provider;
1376
+ const baseURL = getProviderBaseURL(
1377
+ provider,
1378
+ normalizeBaseURL(config.baseURL)
1379
+ );
1380
+ if (provider === "voyage") {
1381
+ return yield* createVoyageProvider({
1382
+ model: config.model,
1383
+ batchSize: config.batchSize,
1384
+ apiKey: normalizeApiKey(config.apiKey)
1385
+ });
1386
+ }
1387
+ return yield* createOpenAIProvider({
1388
+ model: config.model,
1389
+ dimensions: config.dimensions,
1390
+ batchSize: config.batchSize,
1391
+ baseURL,
1392
+ apiKey: normalizeApiKey(config.apiKey)
1393
+ });
1394
+ });
1395
+
1396
+ // src/embeddings/types.ts
1397
+ var hasProviderMetadata = (provider) => {
1398
+ return "model" in provider && typeof provider.model === "string";
1399
+ };
1400
+ var QUALITY_EF_SEARCH = {
1401
+ fast: 64,
1402
+ balanced: 100,
1403
+ thorough: 256
1404
+ };
1405
+ var HEADING_BOOST_FACTOR = 0.05;
1406
+ var calculateHeadingBoost = (heading, query) => {
1407
+ const queryTerms = query.toLowerCase().split(/\s+/).filter(Boolean);
1408
+ if (queryTerms.length === 0) return 0;
1409
+ const headingLower = heading.toLowerCase();
1410
+ const matchCount = queryTerms.filter(
1411
+ (term) => headingLower.includes(term)
1412
+ ).length;
1413
+ return matchCount * HEADING_BOOST_FACTOR;
1414
+ };
1415
+ var preprocessQuery = (query) => {
1416
+ return query.toLowerCase().replace(/[^\w\s]/g, " ").replace(/\s+/g, " ").trim();
1417
+ };
1418
+
1419
+ // src/embeddings/vector-store.ts
1420
+ import { Effect as Effect10 } from "effect";
1421
+ import HierarchicalNSW from "hnswlib-node";
1422
+ import * as fs3 from "fs/promises";
1423
+ import * as path3 from "path";
1424
+ var VECTOR_INDEX_FILE = "vectors.bin";
1425
+ var VECTOR_META_FILE = "vectors.meta.json";
1426
+ var INDEX_VERSION = 1;
1427
+ var HnswVectorStore = class {
1428
+ rootPath;
1429
+ dimensions;
1430
+ index = null;
1431
+ entries = /* @__PURE__ */ new Map();
1432
+ idToIndex = /* @__PURE__ */ new Map();
1433
+ nextIndex = 0;
1434
+ provider = "unknown";
1435
+ providerModel = void 0;
1436
+ providerBaseURL = void 0;
1437
+ totalCost = 0;
1438
+ totalTokens = 0;
1439
+ // HNSW build parameters
1440
+ hnswM;
1441
+ hnswEfConstruction;
1442
+ constructor(rootPath, dimensions, hnswOptions) {
1443
+ this.rootPath = path3.resolve(rootPath);
1444
+ this.dimensions = dimensions;
1445
+ this.hnswM = hnswOptions?.m ?? 16;
1446
+ this.hnswEfConstruction = hnswOptions?.efConstruction ?? 200;
1447
+ }
1448
+ getIndexDir() {
1449
+ return path3.join(this.rootPath, INDEX_DIR);
1450
+ }
1451
+ getVectorPath() {
1452
+ return path3.join(this.getIndexDir(), VECTOR_INDEX_FILE);
1453
+ }
1454
+ getMetaPath() {
1455
+ return path3.join(this.getIndexDir(), VECTOR_META_FILE);
1456
+ }
1457
+ ensureIndex() {
1458
+ if (!this.index) {
1459
+ this.index = new HierarchicalNSW.HierarchicalNSW(
1460
+ "cosine",
1461
+ this.dimensions
1462
+ );
1463
+ this.index.initIndex(1e4, this.hnswM, this.hnswEfConstruction, 100);
1464
+ }
1465
+ return this.index;
1466
+ }
1467
+ add(entries) {
1468
+ return Effect10.try({
1469
+ try: () => {
1470
+ const index = this.ensureIndex();
1471
+ for (const entry of entries) {
1472
+ if (this.idToIndex.has(entry.id)) {
1473
+ continue;
1474
+ }
1475
+ const idx = this.nextIndex++;
1476
+ if (idx >= index.getMaxElements()) {
1477
+ index.resizeIndex(index.getMaxElements() * 2);
1478
+ }
1479
+ index.addPoint(entry.embedding, idx);
1480
+ this.entries.set(idx, entry);
1481
+ this.idToIndex.set(entry.id, idx);
1482
+ }
1483
+ },
1484
+ catch: (e) => new VectorStoreError({
1485
+ operation: "add",
1486
+ message: e instanceof Error ? e.message : String(e),
1487
+ cause: e
1488
+ })
1489
+ });
1490
+ }
1491
+ search(vector, limit, threshold = 0, options) {
1492
+ return Effect10.try({
1493
+ try: () => {
1494
+ if (!this.index || this.entries.size === 0) {
1495
+ return [];
1496
+ }
1497
+ if (options?.efSearch !== void 0) {
1498
+ this.index.setEf(options.efSearch);
1499
+ }
1500
+ const result = this.index.searchKnn(
1501
+ vector,
1502
+ Math.min(limit, this.entries.size)
1503
+ );
1504
+ const results = [];
1505
+ for (let i = 0; i < result.neighbors.length; i++) {
1506
+ const idx = result.neighbors[i];
1507
+ const distance = result.distances[i];
1508
+ if (idx === void 0 || distance === void 0) {
1509
+ continue;
1510
+ }
1511
+ const similarity = 1 - distance;
1512
+ if (similarity < threshold) {
1513
+ continue;
1514
+ }
1515
+ const entry = this.entries.get(idx);
1516
+ if (entry) {
1517
+ results.push({
1518
+ id: entry.id,
1519
+ sectionId: entry.sectionId,
1520
+ documentPath: entry.documentPath,
1521
+ heading: entry.heading,
1522
+ similarity
1523
+ });
1524
+ }
1525
+ }
1526
+ return results;
1527
+ },
1528
+ catch: (e) => new VectorStoreError({
1529
+ operation: "search",
1530
+ message: e instanceof Error ? e.message : String(e),
1531
+ cause: e
1532
+ })
1533
+ });
1534
+ }
1535
+ searchWithStats(vector, limit, threshold = 0, options) {
1536
+ return Effect10.try({
1537
+ try: () => {
1538
+ if (!this.index || this.entries.size === 0) {
1539
+ return {
1540
+ results: [],
1541
+ belowThresholdCount: 0,
1542
+ belowThresholdHighest: null
1543
+ };
1544
+ }
1545
+ if (options?.efSearch !== void 0) {
1546
+ this.index.setEf(options.efSearch);
1547
+ }
1548
+ const result = this.index.searchKnn(
1549
+ vector,
1550
+ Math.min(limit, this.entries.size)
1551
+ );
1552
+ const results = [];
1553
+ let belowThresholdCount = 0;
1554
+ let belowThresholdHighest = null;
1555
+ for (let i = 0; i < result.neighbors.length; i++) {
1556
+ const idx = result.neighbors[i];
1557
+ const distance = result.distances[i];
1558
+ if (idx === void 0 || distance === void 0) {
1559
+ continue;
1560
+ }
1561
+ const similarity = 1 - distance;
1562
+ const entry = this.entries.get(idx);
1563
+ if (!entry) continue;
1564
+ if (similarity < threshold) {
1565
+ belowThresholdCount++;
1566
+ if (belowThresholdHighest === null || similarity > belowThresholdHighest) {
1567
+ belowThresholdHighest = similarity;
1568
+ }
1569
+ continue;
1570
+ }
1571
+ results.push({
1572
+ id: entry.id,
1573
+ sectionId: entry.sectionId,
1574
+ documentPath: entry.documentPath,
1575
+ heading: entry.heading,
1576
+ similarity
1577
+ });
1578
+ }
1579
+ return {
1580
+ results,
1581
+ belowThresholdCount,
1582
+ belowThresholdHighest
1583
+ };
1584
+ },
1585
+ catch: (e) => new VectorStoreError({
1586
+ operation: "search",
1587
+ message: e instanceof Error ? e.message : String(e),
1588
+ cause: e
1589
+ })
1590
+ });
1591
+ }
1592
+ save() {
1593
+ return Effect10.gen(
1594
+ function* () {
1595
+ if (!this.index) {
1596
+ return;
1597
+ }
1598
+ const indexDir = this.getIndexDir();
1599
+ yield* Effect10.tryPromise({
1600
+ try: () => fs3.mkdir(indexDir, { recursive: true }),
1601
+ catch: (e) => new VectorStoreError({
1602
+ operation: "save",
1603
+ message: `Failed to create directory: ${e instanceof Error ? e.message : String(e)}`,
1604
+ cause: e
1605
+ })
1606
+ });
1607
+ yield* Effect10.tryPromise({
1608
+ try: () => this.index.writeIndex(this.getVectorPath()),
1609
+ catch: (e) => new VectorStoreError({
1610
+ operation: "save",
1611
+ message: `Failed to write index: ${e instanceof Error ? e.message : String(e)}`,
1612
+ cause: e
1613
+ })
1614
+ });
1615
+ const meta = {
1616
+ version: INDEX_VERSION,
1617
+ provider: this.provider,
1618
+ providerModel: this.providerModel,
1619
+ providerBaseURL: this.providerBaseURL,
1620
+ dimensions: this.dimensions,
1621
+ entries: Object.fromEntries(
1622
+ Array.from(this.entries.entries()).map(([idx, entry]) => [
1623
+ idx.toString(),
1624
+ entry
1625
+ ])
1626
+ ),
1627
+ totalCost: this.totalCost,
1628
+ totalTokens: this.totalTokens,
1629
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
1630
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
1631
+ // Store HNSW build parameters for validation on load
1632
+ hnswParams: {
1633
+ m: this.hnswM,
1634
+ efConstruction: this.hnswEfConstruction
1635
+ }
1636
+ };
1637
+ yield* Effect10.tryPromise({
1638
+ try: () => fs3.writeFile(this.getMetaPath(), JSON.stringify(meta, null, 2)),
1639
+ catch: (e) => new VectorStoreError({
1640
+ operation: "save",
1641
+ message: `Failed to write metadata: ${e instanceof Error ? e.message : String(e)}`,
1642
+ cause: e
1643
+ })
1644
+ });
1645
+ }.bind(this)
1646
+ );
1647
+ }
1648
+ load() {
1649
+ return Effect10.gen(
1650
+ function* () {
1651
+ const vectorPath = this.getVectorPath();
1652
+ const metaPath = this.getMetaPath();
1653
+ const filesExist = yield* Effect10.tryPromise({
1654
+ try: async () => {
1655
+ await fs3.access(vectorPath);
1656
+ await fs3.access(metaPath);
1657
+ return true;
1658
+ },
1659
+ catch: () => new VectorStoreError({
1660
+ operation: "load",
1661
+ message: "Files not found"
1662
+ })
1663
+ }).pipe(
1664
+ Effect10.catchTag("VectorStoreError", () => Effect10.succeed(false))
1665
+ );
1666
+ if (!filesExist) {
1667
+ return { loaded: false };
1668
+ }
1669
+ const metaContent = yield* Effect10.tryPromise({
1670
+ try: () => fs3.readFile(metaPath, "utf-8"),
1671
+ catch: (e) => new VectorStoreError({
1672
+ operation: "load",
1673
+ message: `Failed to read metadata: ${e instanceof Error ? e.message : String(e)}`,
1674
+ cause: e
1675
+ })
1676
+ });
1677
+ const loadedMeta = yield* Effect10.try({
1678
+ try: () => JSON.parse(metaContent),
1679
+ catch: (e) => new VectorStoreError({
1680
+ operation: "load",
1681
+ message: `Failed to parse metadata: ${e instanceof Error ? e.message : String(e)}`,
1682
+ cause: e
1683
+ })
1684
+ });
1685
+ const meta = {
1686
+ ...loadedMeta,
1687
+ provider: loadedMeta.provider || "openai"
1688
+ };
1689
+ if (meta.dimensions !== this.dimensions) {
1690
+ return yield* Effect10.fail(
1691
+ new DimensionMismatchError({
1692
+ corpusDimensions: meta.dimensions,
1693
+ providerDimensions: this.dimensions,
1694
+ corpusProvider: meta.providerModel ? `${meta.provider}:${meta.providerModel}` : meta.provider,
1695
+ path: this.rootPath
1696
+ })
1697
+ );
1698
+ }
1699
+ this.index = new HierarchicalNSW.HierarchicalNSW(
1700
+ "cosine",
1701
+ this.dimensions
1702
+ );
1703
+ yield* Effect10.tryPromise({
1704
+ try: () => this.index.readIndex(vectorPath),
1705
+ catch: (e) => new VectorStoreError({
1706
+ operation: "load",
1707
+ message: `Failed to read index: ${e instanceof Error ? e.message : String(e)}`,
1708
+ cause: e
1709
+ })
1710
+ });
1711
+ this.entries.clear();
1712
+ this.idToIndex.clear();
1713
+ this.nextIndex = 0;
1714
+ for (const [idxStr, entry] of Object.entries(meta.entries)) {
1715
+ const idx = parseInt(idxStr, 10);
1716
+ this.entries.set(idx, entry);
1717
+ this.idToIndex.set(entry.id, idx);
1718
+ this.nextIndex = Math.max(this.nextIndex, idx + 1);
1719
+ }
1720
+ this.provider = meta.provider;
1721
+ this.providerModel = meta.providerModel;
1722
+ this.providerBaseURL = meta.providerBaseURL;
1723
+ this.totalCost = meta.totalCost;
1724
+ this.totalTokens = meta.totalTokens;
1725
+ let hnswMismatch;
1726
+ if (meta.hnswParams) {
1727
+ const indexM = meta.hnswParams.m;
1728
+ const indexEf = meta.hnswParams.efConstruction;
1729
+ if (indexM !== this.hnswM || indexEf !== this.hnswEfConstruction) {
1730
+ hnswMismatch = {
1731
+ configParams: {
1732
+ m: this.hnswM,
1733
+ efConstruction: this.hnswEfConstruction
1734
+ },
1735
+ indexParams: { m: indexM, efConstruction: indexEf }
1736
+ };
1737
+ }
1738
+ }
1739
+ return { loaded: true, hnswMismatch };
1740
+ }.bind(this)
1741
+ );
1742
+ }
1743
+ getStats() {
1744
+ return {
1745
+ count: this.entries.size,
1746
+ dimensions: this.dimensions,
1747
+ provider: this.provider,
1748
+ providerModel: this.providerModel,
1749
+ totalCost: this.totalCost,
1750
+ totalTokens: this.totalTokens
1751
+ };
1752
+ }
1753
+ setProvider(name, model, baseURL) {
1754
+ this.provider = name;
1755
+ this.providerModel = model;
1756
+ this.providerBaseURL = baseURL;
1757
+ }
1758
+ addCost(cost, tokens) {
1759
+ this.totalCost += cost;
1760
+ this.totalTokens += tokens;
1761
+ }
1762
+ };
1763
+ var createVectorStore = (rootPath, dimensions, hnswOptions) => new HnswVectorStore(rootPath, dimensions, hnswOptions);
1764
+
1765
+ // src/embeddings/semantic-search.ts
1766
+ var checkHnswMismatch = (mismatch) => {
1767
+ if (!mismatch) {
1768
+ return Effect11.void;
1769
+ }
1770
+ const { configParams, indexParams } = mismatch;
1771
+ return Effect11.logWarning(
1772
+ `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.`
1773
+ );
1774
+ };
1775
+ var checkProviderMismatch = (stats, currentProvider, currentProviderModel) => {
1776
+ if (stats.providerModel && stats.providerModel !== currentProviderModel) {
1777
+ return Effect11.logWarning(
1778
+ `Provider mismatch: Index was created with ${stats.provider}/${stats.providerModel}, but querying with ${currentProvider}/${currentProviderModel}. Results may be inconsistent. Consider re-indexing.`
1779
+ );
1780
+ }
1781
+ if (!stats.providerModel) {
1782
+ const indexProviderParts = stats.provider.split(":");
1783
+ if (indexProviderParts.length === 2 && indexProviderParts[1] !== currentProviderModel) {
1784
+ return Effect11.logWarning(
1785
+ `Provider mismatch: Index was created with ${indexProviderParts[0]}/${indexProviderParts[1]}, but querying with ${currentProvider}/${currentProviderModel}. Results may be inconsistent. Consider re-indexing.`
1786
+ );
1787
+ }
1788
+ }
1789
+ return Effect11.void;
1790
+ };
1791
+ var generateEmbeddingText = (section, content, documentTitle, parentHeading) => {
1792
+ const parts = [];
1793
+ parts.push(`# ${section.heading}`);
1794
+ if (parentHeading) {
1795
+ parts.push(`Parent section: ${parentHeading}`);
1796
+ }
1797
+ parts.push(`Document: ${documentTitle}`);
1798
+ parts.push("");
1799
+ parts.push(content);
1800
+ return parts.join("\n");
1801
+ };
1802
+ var EMBEDDING_PRICE_PER_MILLION = PRICING_DATA.prices["text-embedding-3-small"] ?? 0.02;
1803
+ var estimateEmbeddingCost = (rootPath, options = {}) => Effect11.gen(function* () {
1804
+ const resolvedRoot = path4.resolve(rootPath);
1805
+ const storage = createStorage(resolvedRoot);
1806
+ const docIndex = yield* loadDocumentIndex(storage);
1807
+ const sectionIndex = yield* loadSectionIndex(storage);
1808
+ if (!docIndex || !sectionIndex) {
1809
+ return yield* Effect11.fail(new IndexNotFoundError({ path: resolvedRoot }));
1810
+ }
1811
+ const byDir = /* @__PURE__ */ new Map();
1812
+ for (const section of Object.values(sectionIndex.sections)) {
1813
+ if (section.tokenCount < 10) continue;
1814
+ if (options.excludePatterns?.length) {
1815
+ const excluded = options.excludePatterns.some((pattern) => {
1816
+ const regex = new RegExp(
1817
+ `^${pattern.replace(/\*/g, ".*").replace(/\?/g, ".")}$`
1818
+ );
1819
+ return regex.test(section.documentPath);
1820
+ });
1821
+ if (excluded) continue;
1822
+ }
1823
+ const dir = path4.dirname(section.documentPath) || ".";
1824
+ if (!byDir.has(dir)) {
1825
+ byDir.set(dir, { files: /* @__PURE__ */ new Set(), sections: 0, tokens: 0 });
1826
+ }
1827
+ const entry = byDir.get(dir);
1828
+ entry.files.add(section.documentPath);
1829
+ entry.sections++;
1830
+ entry.tokens += section.tokenCount;
1831
+ }
1832
+ const directoryEstimates = [];
1833
+ let totalFiles = 0;
1834
+ let totalSections = 0;
1835
+ let totalTokens = 0;
1836
+ for (const [dir, data] of byDir) {
1837
+ directoryEstimates.push({
1838
+ directory: dir,
1839
+ fileCount: data.files.size,
1840
+ sectionCount: data.sections,
1841
+ estimatedTokens: data.tokens,
1842
+ estimatedCost: data.tokens / 1e6 * EMBEDDING_PRICE_PER_MILLION
1843
+ });
1844
+ totalFiles += data.files.size;
1845
+ totalSections += data.sections;
1846
+ totalTokens += data.tokens;
1847
+ }
1848
+ directoryEstimates.sort((a, b) => a.directory.localeCompare(b.directory));
1849
+ const estimatedTimeSeconds = Math.ceil(totalSections / 100) * 1.5;
1850
+ return {
1851
+ totalFiles,
1852
+ totalSections,
1853
+ totalTokens,
1854
+ totalCost: totalTokens / 1e6 * EMBEDDING_PRICE_PER_MILLION,
1855
+ estimatedTimeSeconds,
1856
+ byDirectory: directoryEstimates
1857
+ };
1858
+ });
1859
+ var buildEmbeddings = (rootPath, options = {}) => Effect11.gen(function* () {
1860
+ const startTime = Date.now();
1861
+ const resolvedRoot = path4.resolve(rootPath);
1862
+ const storage = createStorage(resolvedRoot);
1863
+ const docIndex = yield* loadDocumentIndex(storage);
1864
+ const sectionIndex = yield* loadSectionIndex(storage);
1865
+ if (!docIndex || !sectionIndex) {
1866
+ return yield* Effect11.fail(new IndexNotFoundError({ path: resolvedRoot }));
1867
+ }
1868
+ const providerConfig = options.providerConfig ?? { provider: "openai" };
1869
+ const provider = options.provider ?? (yield* createEmbeddingProviderDirect(providerConfig));
1870
+ const dimensions = provider.dimensions;
1871
+ const vectorStore = createVectorStore(
1872
+ resolvedRoot,
1873
+ dimensions,
1874
+ options.hnswOptions
1875
+ );
1876
+ if (hasProviderMetadata(provider)) {
1877
+ vectorStore.setProvider(provider.name, provider.model, provider.baseURL);
1878
+ } else {
1879
+ vectorStore.setProvider(provider.name, void 0, void 0);
1880
+ }
1881
+ if (!options.force) {
1882
+ const loadResult = yield* vectorStore.load();
1883
+ if (loadResult.loaded) {
1884
+ const stats = vectorStore.getStats();
1885
+ if (stats.count > 0) {
1886
+ const duration2 = Date.now() - startTime;
1887
+ const estimatedSavings = stats.totalTokens / 1e6 * EMBEDDING_PRICE_PER_MILLION;
1888
+ return {
1889
+ sectionsEmbedded: 0,
1890
+ tokensUsed: 0,
1891
+ cost: 0,
1892
+ duration: duration2,
1893
+ filesProcessed: 0,
1894
+ cacheHit: true,
1895
+ existingVectors: stats.count,
1896
+ estimatedSavings
1897
+ };
1898
+ }
1899
+ }
1900
+ }
1901
+ const isExcluded = (docPath) => {
1902
+ if (!options.excludePatterns?.length) return false;
1903
+ return options.excludePatterns.some((pattern) => {
1904
+ const regex = new RegExp(
1905
+ `^${pattern.replace(/\*/g, ".*").replace(/\?/g, ".")}$`
1906
+ );
1907
+ return regex.test(docPath);
1908
+ });
1909
+ };
1910
+ const sectionsByDoc = /* @__PURE__ */ new Map();
1911
+ for (const section of Object.values(sectionIndex.sections)) {
1912
+ const document = docIndex.documents[section.documentPath];
1913
+ if (!document) continue;
1914
+ if (section.tokenCount < 10) continue;
1915
+ if (isExcluded(section.documentPath)) continue;
1916
+ let parentHeading;
1917
+ if (section.level > 1) {
1918
+ const docSections = sectionIndex.byDocument[document.id] ?? [];
1919
+ for (const sibId of docSections) {
1920
+ const sib = sectionIndex.sections[sibId];
1921
+ if (sib && sib.level === section.level - 1 && sib.startLine < section.startLine) {
1922
+ parentHeading = sib.heading;
1923
+ }
1924
+ }
1925
+ }
1926
+ const docPath = section.documentPath;
1927
+ if (!sectionsByDoc.has(docPath)) {
1928
+ sectionsByDoc.set(docPath, []);
1929
+ }
1930
+ sectionsByDoc.get(docPath).push({ section, parentHeading });
1931
+ }
1932
+ if (sectionsByDoc.size === 0) {
1933
+ const duration2 = Date.now() - startTime;
1934
+ return {
1935
+ sectionsEmbedded: 0,
1936
+ tokensUsed: 0,
1937
+ cost: 0,
1938
+ duration: duration2,
1939
+ filesProcessed: 0
1940
+ };
1941
+ }
1942
+ const sectionsToEmbed = [];
1943
+ const docPaths = Array.from(sectionsByDoc.keys());
1944
+ let filesProcessed = 0;
1945
+ for (let fileIndex = 0; fileIndex < docPaths.length; fileIndex++) {
1946
+ const docPath = docPaths[fileIndex];
1947
+ const sections = sectionsByDoc.get(docPath);
1948
+ const document = docIndex.documents[docPath];
1949
+ if (!document) continue;
1950
+ if (options.onFileProgress) {
1951
+ options.onFileProgress({
1952
+ fileIndex: fileIndex + 1,
1953
+ totalFiles: docPaths.length,
1954
+ filePath: docPath,
1955
+ sectionCount: sections.length
1956
+ });
1957
+ }
1958
+ const filePath = path4.join(resolvedRoot, docPath);
1959
+ const fileContentResult = yield* Effect11.promise(
1960
+ () => fs4.readFile(filePath, "utf-8")
1961
+ ).pipe(
1962
+ Effect11.map((content) => ({ ok: true, content })),
1963
+ Effect11.catchAll(
1964
+ () => Effect11.succeed({ ok: false, content: "" })
1965
+ )
1966
+ );
1967
+ if (!fileContentResult.ok) {
1968
+ yield* Effect11.logWarning(`Skipping file (cannot read): ${docPath}`);
1969
+ continue;
1970
+ }
1971
+ filesProcessed++;
1972
+ const lines = fileContentResult.content.split("\n");
1973
+ for (const { section, parentHeading } of sections) {
1974
+ const content = lines.slice(section.startLine - 1, section.endLine).join("\n");
1975
+ const text = generateEmbeddingText(
1976
+ section,
1977
+ content,
1978
+ document.title,
1979
+ parentHeading
1980
+ );
1981
+ sectionsToEmbed.push({ section, text });
1982
+ }
1983
+ }
1984
+ if (sectionsToEmbed.length === 0) {
1985
+ const duration2 = Date.now() - startTime;
1986
+ return {
1987
+ sectionsEmbedded: 0,
1988
+ tokensUsed: 0,
1989
+ cost: 0,
1990
+ duration: duration2,
1991
+ filesProcessed
1992
+ };
1993
+ }
1994
+ const texts = sectionsToEmbed.map((s) => s.text);
1995
+ const result = yield* wrapEmbedding(
1996
+ provider.embed(texts),
1997
+ providerConfig.provider ?? "openai"
1998
+ );
1999
+ const entries = [];
2000
+ for (let i = 0; i < sectionsToEmbed.length; i++) {
2001
+ const { section } = sectionsToEmbed[i] ?? { section: null };
2002
+ const embedding = result.embeddings[i];
2003
+ if (!section || !embedding) continue;
2004
+ entries.push({
2005
+ id: section.id,
2006
+ sectionId: section.id,
2007
+ documentPath: section.documentPath,
2008
+ heading: section.heading,
2009
+ embedding
2010
+ });
2011
+ }
2012
+ yield* vectorStore.add(entries);
2013
+ vectorStore.addCost(result.cost, result.tokensUsed);
2014
+ yield* vectorStore.save();
2015
+ const duration = Date.now() - startTime;
2016
+ return {
2017
+ sectionsEmbedded: entries.length,
2018
+ tokensUsed: result.tokensUsed,
2019
+ cost: result.cost,
2020
+ duration,
2021
+ filesProcessed
2022
+ };
2023
+ });
2024
+ var semanticSearch = (rootPath, query, options = {}) => Effect11.gen(function* () {
2025
+ const resolvedRoot = path4.resolve(rootPath);
2026
+ const provider = yield* createEmbeddingProviderDirect(
2027
+ options.providerConfig ?? { provider: "openai" }
2028
+ );
2029
+ const dimensions = provider.dimensions;
2030
+ const vectorStore = createVectorStore(resolvedRoot, dimensions);
2031
+ const loadResult = yield* vectorStore.load();
2032
+ if (!loadResult.loaded) {
2033
+ return yield* Effect11.fail(
2034
+ new EmbeddingsNotFoundError({ path: resolvedRoot })
2035
+ );
2036
+ }
2037
+ const stats = vectorStore.getStats();
2038
+ const currentProviderModel = options.providerConfig?.model ?? "text-embedding-3-small";
2039
+ const currentProvider = options.providerConfig?.provider ?? "openai";
2040
+ yield* checkProviderMismatch(stats, currentProvider, currentProviderModel);
2041
+ yield* checkHnswMismatch(loadResult.hnswMismatch);
2042
+ let textToEmbed;
2043
+ let hydeResult;
2044
+ if (options.hyde) {
2045
+ hydeResult = yield* generateHypotheticalDocument(query, {
2046
+ model: options.hydeOptions?.model,
2047
+ maxTokens: options.hydeOptions?.maxTokens,
2048
+ temperature: options.hydeOptions?.temperature
2049
+ });
2050
+ textToEmbed = hydeResult.hypotheticalDocument;
2051
+ yield* Effect11.logDebug(
2052
+ `HyDE generated ${hydeResult.tokensUsed} tokens ($${hydeResult.cost.toFixed(6)})`
2053
+ );
2054
+ } else {
2055
+ textToEmbed = options.skipPreprocessing ? query : preprocessQuery(query);
2056
+ }
2057
+ const queryResult = yield* wrapEmbedding(
2058
+ provider.embed([textToEmbed]),
2059
+ currentProvider
2060
+ );
2061
+ const queryVector = queryResult.embeddings[0];
2062
+ if (!queryVector) {
2063
+ return yield* Effect11.fail(
2064
+ new EmbeddingError({
2065
+ reason: "Unknown",
2066
+ message: "Failed to generate query embedding",
2067
+ provider: currentProvider
2068
+ })
2069
+ );
2070
+ }
2071
+ const limit = options.limit ?? 10;
2072
+ const threshold = options.threshold ?? 0;
2073
+ const efSearch = options.quality ? QUALITY_EF_SEARCH[options.quality] : void 0;
2074
+ const searchResults = yield* vectorStore.search(
2075
+ queryVector,
2076
+ limit * 2,
2077
+ threshold,
2078
+ { efSearch }
2079
+ );
2080
+ let filteredResults = searchResults;
2081
+ if (options.pathPattern) {
2082
+ const pattern = options.pathPattern.replace(/\./g, "\\.").replace(/\*/g, ".*");
2083
+ const regex = new RegExp(`^${pattern}$`, "i");
2084
+ filteredResults = searchResults.filter((r) => regex.test(r.documentPath));
2085
+ }
2086
+ const applyHeadingBoost = options.headingBoost !== false;
2087
+ const boostedResults = applyHeadingBoost ? filteredResults.map((r) => ({
2088
+ ...r,
2089
+ similarity: Math.min(
2090
+ 1,
2091
+ r.similarity + calculateHeadingBoost(r.heading, query)
2092
+ )
2093
+ })) : filteredResults;
2094
+ const results = boostedResults.sort((a, b) => b.similarity - a.similarity).slice(0, limit).map((r) => ({
2095
+ sectionId: r.sectionId,
2096
+ documentPath: r.documentPath,
2097
+ heading: r.heading,
2098
+ similarity: r.similarity
2099
+ }));
2100
+ return results;
2101
+ });
2102
+ var semanticSearchWithStats = (rootPath, query, options = {}) => Effect11.gen(function* () {
2103
+ const resolvedRoot = path4.resolve(rootPath);
2104
+ const provider = yield* createEmbeddingProviderDirect(
2105
+ options.providerConfig ?? { provider: "openai" }
2106
+ );
2107
+ const dimensions = provider.dimensions;
2108
+ const vectorStore = createVectorStore(resolvedRoot, dimensions);
2109
+ const loadResult = yield* vectorStore.load();
2110
+ if (!loadResult.loaded) {
2111
+ return yield* Effect11.fail(
2112
+ new EmbeddingsNotFoundError({ path: resolvedRoot })
2113
+ );
2114
+ }
2115
+ const stats = vectorStore.getStats();
2116
+ const currentProviderModel = options.providerConfig?.model ?? "text-embedding-3-small";
2117
+ const currentProvider = options.providerConfig?.provider ?? "openai";
2118
+ yield* checkProviderMismatch(stats, currentProvider, currentProviderModel);
2119
+ yield* checkHnswMismatch(loadResult.hnswMismatch);
2120
+ let textToEmbed;
2121
+ let hydeResult;
2122
+ if (options.hyde) {
2123
+ hydeResult = yield* generateHypotheticalDocument(query, {
2124
+ model: options.hydeOptions?.model,
2125
+ maxTokens: options.hydeOptions?.maxTokens,
2126
+ temperature: options.hydeOptions?.temperature
2127
+ });
2128
+ textToEmbed = hydeResult.hypotheticalDocument;
2129
+ yield* Effect11.logDebug(
2130
+ `HyDE generated ${hydeResult.tokensUsed} tokens ($${hydeResult.cost.toFixed(6)})`
2131
+ );
2132
+ } else {
2133
+ textToEmbed = options.skipPreprocessing ? query : preprocessQuery(query);
2134
+ }
2135
+ const queryResult = yield* wrapEmbedding(
2136
+ provider.embed([textToEmbed]),
2137
+ currentProvider
2138
+ );
2139
+ const queryVector = queryResult.embeddings[0];
2140
+ if (!queryVector) {
2141
+ return yield* Effect11.fail(
2142
+ new EmbeddingError({
2143
+ reason: "Unknown",
2144
+ message: "Failed to generate query embedding",
2145
+ provider: currentProvider
2146
+ })
2147
+ );
2148
+ }
2149
+ const limit = options.limit ?? 10;
2150
+ const threshold = options.threshold ?? 0;
2151
+ const efSearch = options.quality ? QUALITY_EF_SEARCH[options.quality] : void 0;
2152
+ const searchResultWithStats = yield* vectorStore.searchWithStats(
2153
+ queryVector,
2154
+ limit * 2,
2155
+ threshold,
2156
+ { efSearch }
2157
+ );
2158
+ let filteredResults = searchResultWithStats.results;
2159
+ if (options.pathPattern) {
2160
+ const pattern = options.pathPattern.replace(/\./g, "\\.").replace(/\*/g, ".*");
2161
+ const regex = new RegExp(`^${pattern}$`, "i");
2162
+ filteredResults = searchResultWithStats.results.filter(
2163
+ (r) => regex.test(r.documentPath)
2164
+ );
2165
+ }
2166
+ const applyHeadingBoost = options.headingBoost !== false;
2167
+ const boostedResults = applyHeadingBoost ? filteredResults.map((r) => ({
2168
+ ...r,
2169
+ similarity: Math.min(
2170
+ 1,
2171
+ r.similarity + calculateHeadingBoost(r.heading, query)
2172
+ )
2173
+ })) : filteredResults;
2174
+ const results = boostedResults.sort((a, b) => b.similarity - a.similarity).slice(0, limit).map((r) => ({
2175
+ sectionId: r.sectionId,
2176
+ documentPath: r.documentPath,
2177
+ heading: r.heading,
2178
+ similarity: r.similarity
2179
+ }));
2180
+ return {
2181
+ results,
2182
+ belowThresholdCount: searchResultWithStats.belowThresholdCount,
2183
+ belowThresholdHighest: searchResultWithStats.belowThresholdHighest ?? void 0
2184
+ };
2185
+ });
2186
+ var getEmbeddingStats = (rootPath) => Effect11.gen(function* () {
2187
+ const resolvedRoot = path4.resolve(rootPath);
2188
+ const metaPath = path4.join(resolvedRoot, INDEX_DIR, "vectors.meta.json");
2189
+ const metaContent = yield* Effect11.tryPromise({
2190
+ try: () => fs4.readFile(metaPath, "utf-8"),
2191
+ catch: (e) => new VectorStoreError({
2192
+ operation: "load",
2193
+ message: `Failed to read metadata: ${e instanceof Error ? e.message : String(e)}`,
2194
+ cause: e
2195
+ })
2196
+ }).pipe(Effect11.catchAll(() => Effect11.succeed(null)));
2197
+ if (!metaContent) {
2198
+ return {
2199
+ hasEmbeddings: false,
2200
+ count: 0,
2201
+ provider: "none",
2202
+ dimensions: 0,
2203
+ totalCost: 0,
2204
+ totalTokens: 0
2205
+ };
2206
+ }
2207
+ const meta = yield* Effect11.try({
2208
+ try: () => JSON.parse(metaContent),
2209
+ catch: (e) => new VectorStoreError({
2210
+ operation: "load",
2211
+ message: `Failed to parse metadata: ${e instanceof Error ? e.message : String(e)}`,
2212
+ cause: e
2213
+ })
2214
+ });
2215
+ return {
2216
+ hasEmbeddings: true,
2217
+ count: Object.keys(meta.entries).length,
2218
+ provider: meta.providerModel ? `${meta.provider}:${meta.providerModel}` : meta.provider || "openai",
2219
+ dimensions: meta.dimensions,
2220
+ totalCost: meta.totalCost || 0,
2221
+ totalTokens: meta.totalTokens || 0
2222
+ };
2223
+ });
2224
+
2225
+ // src/search/searcher.ts
2226
+ import * as fs5 from "fs/promises";
2227
+ import * as path5 from "path";
2228
+ import { Effect as Effect12 } from "effect";
2229
+
2230
+ // src/search/query-parser.ts
2231
+ var tokenize = (query) => {
2232
+ const tokens = [];
2233
+ let i = 0;
2234
+ while (i < query.length) {
2235
+ if (/\s/.test(query[i])) {
2236
+ i++;
2237
+ continue;
2238
+ }
2239
+ if (query[i] === '"') {
2240
+ const start = i + 1;
2241
+ i++;
2242
+ while (i < query.length && query[i] !== '"') {
2243
+ i++;
2244
+ }
2245
+ const value = query.slice(start, i);
2246
+ tokens.push({ type: "PHRASE", value });
2247
+ i++;
2248
+ continue;
2249
+ }
2250
+ if (query[i] === "(") {
2251
+ tokens.push({ type: "LPAREN", value: "(" });
2252
+ i++;
2253
+ continue;
2254
+ }
2255
+ if (query[i] === ")") {
2256
+ tokens.push({ type: "RPAREN", value: ")" });
2257
+ i++;
2258
+ continue;
2259
+ }
2260
+ const wordMatch = query.slice(i).match(/^[^\s()"]+/);
2261
+ if (wordMatch) {
2262
+ const word = wordMatch[0];
2263
+ const upperWord = word.toUpperCase();
2264
+ if (upperWord === "AND") {
2265
+ tokens.push({ type: "AND", value: "AND" });
2266
+ } else if (upperWord === "OR") {
2267
+ tokens.push({ type: "OR", value: "OR" });
2268
+ } else if (upperWord === "NOT") {
2269
+ tokens.push({ type: "NOT", value: "NOT" });
2270
+ } else {
2271
+ tokens.push({ type: "TERM", value: word });
2272
+ }
2273
+ i += word.length;
2274
+ continue;
2275
+ }
2276
+ i++;
2277
+ }
2278
+ return tokens;
2279
+ };
2280
+ var Parser = class {
2281
+ tokens;
2282
+ pos = 0;
2283
+ terms = [];
2284
+ phrases = [];
2285
+ constructor(tokens) {
2286
+ this.tokens = tokens;
2287
+ }
2288
+ current() {
2289
+ return this.tokens[this.pos];
2290
+ }
2291
+ advance() {
2292
+ return this.tokens[this.pos++];
2293
+ }
2294
+ match(type) {
2295
+ if (this.current()?.type === type) {
2296
+ this.advance();
2297
+ return true;
2298
+ }
2299
+ return false;
2300
+ }
2301
+ parse() {
2302
+ if (this.tokens.length === 0) {
2303
+ return null;
2304
+ }
2305
+ return this.parseExpr();
2306
+ }
2307
+ parseExpr() {
2308
+ let left = this.parseAndExpr();
2309
+ while (this.match("OR")) {
2310
+ const right = this.parseAndExpr();
2311
+ left = { type: "or", left, right };
2312
+ }
2313
+ return left;
2314
+ }
2315
+ parseAndExpr() {
2316
+ let left = this.parseNotExpr();
2317
+ while (this.match("AND") || this.isImplicitAnd()) {
2318
+ const right = this.parseNotExpr();
2319
+ left = { type: "and", left, right };
2320
+ }
2321
+ return left;
2322
+ }
2323
+ isImplicitAnd() {
2324
+ const tok = this.current();
2325
+ return tok?.type === "TERM" || tok?.type === "PHRASE" || tok?.type === "NOT" || tok?.type === "LPAREN";
2326
+ }
2327
+ parseNotExpr() {
2328
+ if (this.match("NOT")) {
2329
+ const operand = this.parseNotExpr();
2330
+ return { type: "not", operand };
2331
+ }
2332
+ return this.parsePrimary();
2333
+ }
2334
+ parsePrimary() {
2335
+ const tok = this.current();
2336
+ if (this.match("LPAREN")) {
2337
+ const expr = this.parseExpr();
2338
+ this.match("RPAREN");
2339
+ return expr;
2340
+ }
2341
+ if (tok?.type === "PHRASE") {
2342
+ this.advance();
2343
+ this.phrases.push(tok.value);
2344
+ return { type: "phrase", value: tok.value };
2345
+ }
2346
+ if (tok?.type === "TERM") {
2347
+ this.advance();
2348
+ this.terms.push(tok.value);
2349
+ return { type: "term", value: tok.value };
2350
+ }
2351
+ return { type: "term", value: "" };
2352
+ }
2353
+ };
2354
+ var parseQuery = (query) => {
2355
+ const tokens = tokenize(query);
2356
+ if (tokens.length === 0) {
2357
+ return null;
2358
+ }
2359
+ const parser = new Parser(tokens);
2360
+ const ast = parser.parse();
2361
+ if (!ast) {
2362
+ return null;
2363
+ }
2364
+ return {
2365
+ ast,
2366
+ terms: parser.terms,
2367
+ phrases: parser.phrases
2368
+ };
2369
+ };
2370
+ var isAdvancedQuery = (query) => {
2371
+ const tokens = tokenize(query);
2372
+ return tokens.some(
2373
+ (t) => t.type === "AND" || t.type === "OR" || t.type === "NOT" || t.type === "PHRASE" || t.type === "LPAREN"
2374
+ );
2375
+ };
2376
+ var evaluateQuery = (ast, text) => {
2377
+ const lowerText = text.toLowerCase();
2378
+ const evaluate = (node) => {
2379
+ switch (node.type) {
2380
+ case "term": {
2381
+ if (!node.value) return true;
2382
+ return lowerText.includes(node.value.toLowerCase());
2383
+ }
2384
+ case "phrase": {
2385
+ return lowerText.includes(node.value.toLowerCase());
2386
+ }
2387
+ case "and": {
2388
+ return evaluate(node.left) && evaluate(node.right);
2389
+ }
2390
+ case "or": {
2391
+ return evaluate(node.left) || evaluate(node.right);
2392
+ }
2393
+ case "not": {
2394
+ return !evaluate(node.operand);
2395
+ }
2396
+ }
2397
+ };
2398
+ return evaluate(ast);
2399
+ };
2400
+ var buildHighlightPattern = (parsed) => {
2401
+ const patterns = [];
2402
+ const escapeChars = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2403
+ for (const term of parsed.terms) {
2404
+ if (term) {
2405
+ patterns.push(`\\b${escapeChars(term)}\\b`);
2406
+ }
2407
+ }
2408
+ for (const phrase of parsed.phrases) {
2409
+ if (phrase) {
2410
+ patterns.push(escapeChars(phrase));
2411
+ }
2412
+ }
2413
+ if (patterns.length === 0) {
2414
+ return /.^/;
2415
+ }
2416
+ return new RegExp(patterns.join("|"), "gi");
2417
+ };
2418
+
2419
+ // src/search/searcher.ts
2420
+ var matchPath = (filePath, pattern) => {
2421
+ const regexPattern = pattern.replace(/\./g, "\\.").replace(/\*/g, ".*").replace(/\?/g, ".");
2422
+ const regex = new RegExp(`^${regexPattern}$`, "i");
2423
+ return regex.test(filePath);
2424
+ };
2425
+ var search = (rootPath, options = {}) => Effect12.gen(function* () {
2426
+ const storage = createStorage(rootPath);
2427
+ const docIndex = yield* loadDocumentIndex(storage);
2428
+ const sectionIndex = yield* loadSectionIndex(storage);
2429
+ if (!docIndex || !sectionIndex) {
2430
+ return [];
2431
+ }
2432
+ const results = [];
2433
+ const headingRegex = options.heading ? new RegExp(options.heading, "i") : null;
2434
+ for (const section of Object.values(sectionIndex.sections)) {
2435
+ if (headingRegex && !headingRegex.test(section.heading)) {
2436
+ continue;
2437
+ }
2438
+ if (options.pathPattern && !matchPath(section.documentPath, options.pathPattern)) {
2439
+ continue;
2440
+ }
2441
+ if (options.hasCode !== void 0 && section.hasCode !== options.hasCode) {
2442
+ continue;
2443
+ }
2444
+ if (options.hasList !== void 0 && section.hasList !== options.hasList) {
2445
+ continue;
2446
+ }
2447
+ if (options.hasTable !== void 0 && section.hasTable !== options.hasTable) {
2448
+ continue;
2449
+ }
2450
+ if (options.minLevel !== void 0 && section.level < options.minLevel) {
2451
+ continue;
2452
+ }
2453
+ if (options.maxLevel !== void 0 && section.level > options.maxLevel) {
2454
+ continue;
2455
+ }
2456
+ const document = docIndex.documents[section.documentPath];
2457
+ if (document) {
2458
+ results.push({ section, document });
2459
+ }
2460
+ if (options.limit !== void 0 && results.length >= options.limit) {
2461
+ break;
2462
+ }
2463
+ }
2464
+ return results;
2465
+ });
2466
+ var searchContent = (rootPath, options = {}) => Effect12.gen(function* () {
2467
+ const storage = createStorage(rootPath);
2468
+ const docIndex = yield* loadDocumentIndex(storage);
2469
+ const sectionIndex = yield* loadSectionIndex(storage);
2470
+ if (!docIndex || !sectionIndex) {
2471
+ return [];
2472
+ }
2473
+ let parsedQuery = null;
2474
+ let contentRegex = null;
2475
+ let highlightRegex = null;
2476
+ if (options.content) {
2477
+ if (isAdvancedQuery(options.content)) {
2478
+ parsedQuery = parseQuery(options.content);
2479
+ if (parsedQuery) {
2480
+ highlightRegex = buildHighlightPattern(parsedQuery);
2481
+ }
2482
+ } else {
2483
+ contentRegex = new RegExp(options.content, "gi");
2484
+ highlightRegex = contentRegex;
2485
+ }
2486
+ }
2487
+ const headingRegex = options.heading ? new RegExp(options.heading, "i") : null;
2488
+ const results = [];
2489
+ const sectionsByDoc = {};
2490
+ for (const section of Object.values(sectionIndex.sections)) {
2491
+ const docSections = sectionsByDoc[section.documentPath];
2492
+ if (docSections) {
2493
+ docSections.push(section);
2494
+ } else {
2495
+ sectionsByDoc[section.documentPath] = [section];
2496
+ }
2497
+ }
2498
+ for (const [docPath, sections] of Object.entries(sectionsByDoc)) {
2499
+ if (options.pathPattern && !matchPath(docPath, options.pathPattern)) {
2500
+ continue;
2501
+ }
2502
+ const document = docIndex.documents[docPath];
2503
+ if (!document) continue;
2504
+ let fileContent = null;
2505
+ let fileLines = [];
2506
+ if (parsedQuery || contentRegex) {
2507
+ const filePath = path5.join(storage.rootPath, docPath);
2508
+ try {
2509
+ fileContent = yield* Effect12.promise(
2510
+ () => fs5.readFile(filePath, "utf-8")
2511
+ );
2512
+ fileLines = fileContent.split("\n");
2513
+ } catch {
2514
+ continue;
2515
+ }
2516
+ }
2517
+ for (const section of sections) {
2518
+ if (headingRegex && !headingRegex.test(section.heading)) {
2519
+ continue;
2520
+ }
2521
+ if (options.hasCode !== void 0 && section.hasCode !== options.hasCode) {
2522
+ continue;
2523
+ }
2524
+ if (options.hasList !== void 0 && section.hasList !== options.hasList) {
2525
+ continue;
2526
+ }
2527
+ if (options.hasTable !== void 0 && section.hasTable !== options.hasTable) {
2528
+ continue;
2529
+ }
2530
+ if (options.minLevel !== void 0 && section.level < options.minLevel) {
2531
+ continue;
2532
+ }
2533
+ if (options.maxLevel !== void 0 && section.level > options.maxLevel) {
2534
+ continue;
2535
+ }
2536
+ if ((parsedQuery || contentRegex) && fileContent) {
2537
+ const sectionLines = fileLines.slice(
2538
+ section.startLine - 1,
2539
+ section.endLine
2540
+ );
2541
+ const sectionContent = sectionLines.join("\n");
2542
+ if (parsedQuery) {
2543
+ if (!evaluateQuery(parsedQuery.ast, sectionContent)) {
2544
+ continue;
2545
+ }
2546
+ }
2547
+ const matches = [];
2548
+ const searchRegex = contentRegex || highlightRegex;
2549
+ const contextBefore = options.contextBefore ?? 1;
2550
+ const contextAfter = options.contextAfter ?? 1;
2551
+ if (searchRegex) {
2552
+ for (let i = 0; i < sectionLines.length; i++) {
2553
+ const line = sectionLines[i];
2554
+ if (line && searchRegex.test(line)) {
2555
+ searchRegex.lastIndex = 0;
2556
+ const absoluteLineNum = section.startLine + i;
2557
+ const snippetStart = Math.max(0, i - contextBefore);
2558
+ const snippetEnd = Math.min(
2559
+ sectionLines.length,
2560
+ i + contextAfter + 1
2561
+ );
2562
+ const snippetLines = sectionLines.slice(
2563
+ snippetStart,
2564
+ snippetEnd
2565
+ );
2566
+ const snippet = snippetLines.join("\n");
2567
+ const contextLines = [];
2568
+ for (let j = snippetStart; j < snippetEnd; j++) {
2569
+ const ctxLine = sectionLines[j];
2570
+ if (ctxLine !== void 0) {
2571
+ contextLines.push({
2572
+ lineNumber: section.startLine + j,
2573
+ line: ctxLine,
2574
+ isMatch: j === i
2575
+ });
2576
+ }
2577
+ }
2578
+ matches.push({
2579
+ lineNumber: absoluteLineNum,
2580
+ line,
2581
+ snippet,
2582
+ contextLines
2583
+ });
2584
+ }
2585
+ }
2586
+ }
2587
+ if (parsedQuery || matches.length > 0) {
2588
+ const result = {
2589
+ section,
2590
+ document,
2591
+ sectionContent
2592
+ };
2593
+ if (matches.length > 0) {
2594
+ results.push({ ...result, matches });
2595
+ } else {
2596
+ results.push(result);
2597
+ }
2598
+ if (options.limit !== void 0 && results.length >= options.limit) {
2599
+ return results;
2600
+ }
2601
+ }
2602
+ } else if (!parsedQuery && !contentRegex) {
2603
+ results.push({ section, document });
2604
+ if (options.limit !== void 0 && results.length >= options.limit) {
2605
+ return results;
2606
+ }
2607
+ }
2608
+ }
2609
+ }
2610
+ return results;
2611
+ });
2612
+
2613
+ export {
2614
+ CONFIG_FILE_NAMES,
2615
+ findConfigFile,
2616
+ loadConfigFile,
2617
+ readEnvConfig,
2618
+ createConfigProvider,
2619
+ createConfigProviderSync,
2620
+ ConfigService,
2621
+ ConfigServiceDefault,
2622
+ formatSummary,
2623
+ formatAssembledContext,
2624
+ summarizeFile,
2625
+ assembleContext,
2626
+ checkPricingFreshness,
2627
+ getPricingDate,
2628
+ estimateEmbeddingCost,
2629
+ buildEmbeddings,
2630
+ semanticSearch,
2631
+ semanticSearchWithStats,
2632
+ getEmbeddingStats,
2633
+ isAdvancedQuery,
2634
+ search,
2635
+ searchContent
2636
+ };