@zvec/zvec-grep 0.2.2 → 0.3.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (415) hide show
  1. package/README.md +87 -252
  2. package/bin/zg.exe +3 -0
  3. package/install.cjs +510 -0
  4. package/package.json +22 -72
  5. package/platforms.json +92 -0
  6. package/plugins.cjs +183 -0
  7. package/README_CN.md +0 -239
  8. package/dist/authorization/index.d.ts +0 -7
  9. package/dist/authorization/index.js +0 -7
  10. package/dist/authorization/index.js.map +0 -1
  11. package/dist/authorization/manager.d.ts +0 -8
  12. package/dist/authorization/manager.js +0 -19
  13. package/dist/authorization/manager.js.map +0 -1
  14. package/dist/authorization/operation.d.ts +0 -7
  15. package/dist/authorization/operation.js +0 -50
  16. package/dist/authorization/operation.js.map +0 -1
  17. package/dist/authorization/planner.d.ts +0 -21
  18. package/dist/authorization/planner.js +0 -97
  19. package/dist/authorization/planner.js.map +0 -1
  20. package/dist/authorization/prompt.d.ts +0 -12
  21. package/dist/authorization/prompt.js +0 -63
  22. package/dist/authorization/prompt.js.map +0 -1
  23. package/dist/authorization/store.d.ts +0 -20
  24. package/dist/authorization/store.js +0 -251
  25. package/dist/authorization/store.js.map +0 -1
  26. package/dist/authorization/target.d.ts +0 -15
  27. package/dist/authorization/target.js +0 -49
  28. package/dist/authorization/target.js.map +0 -1
  29. package/dist/authorization/types.d.ts +0 -61
  30. package/dist/authorization/types.js +0 -2
  31. package/dist/authorization/types.js.map +0 -1
  32. package/dist/cli/args.d.ts +0 -5
  33. package/dist/cli/args.js +0 -1210
  34. package/dist/cli/args.js.map +0 -1
  35. package/dist/cli/auth.d.ts +0 -21
  36. package/dist/cli/auth.js +0 -209
  37. package/dist/cli/auth.js.map +0 -1
  38. package/dist/cli/commands.d.ts +0 -4
  39. package/dist/cli/commands.js +0 -742
  40. package/dist/cli/commands.js.map +0 -1
  41. package/dist/cli/errors.d.ts +0 -9
  42. package/dist/cli/errors.js +0 -131
  43. package/dist/cli/errors.js.map +0 -1
  44. package/dist/cli/format/context.d.ts +0 -9
  45. package/dist/cli/format/context.js +0 -786
  46. package/dist/cli/format/context.js.map +0 -1
  47. package/dist/cli/format/debug.d.ts +0 -4
  48. package/dist/cli/format/debug.js +0 -60
  49. package/dist/cli/format/debug.js.map +0 -1
  50. package/dist/cli/format/highlight.d.ts +0 -3
  51. package/dist/cli/format/highlight.js +0 -106
  52. package/dist/cli/format/highlight.js.map +0 -1
  53. package/dist/cli/format/progress.d.ts +0 -13
  54. package/dist/cli/format/progress.js +0 -290
  55. package/dist/cli/format/progress.js.map +0 -1
  56. package/dist/cli/format/range.d.ts +0 -3
  57. package/dist/cli/format/range.js +0 -24
  58. package/dist/cli/format/range.js.map +0 -1
  59. package/dist/cli/format/status.d.ts +0 -83
  60. package/dist/cli/format/status.js +0 -530
  61. package/dist/cli/format/status.js.map +0 -1
  62. package/dist/cli/help.d.ts +0 -1
  63. package/dist/cli/help.js +0 -528
  64. package/dist/cli/help.js.map +0 -1
  65. package/dist/cli/index.d.ts +0 -2
  66. package/dist/cli/index.js +0 -34
  67. package/dist/cli/index.js.map +0 -1
  68. package/dist/cli/install.d.ts +0 -4
  69. package/dist/cli/install.js +0 -1661
  70. package/dist/cli/install.js.map +0 -1
  71. package/dist/cli/managed-rg.d.ts +0 -10
  72. package/dist/cli/managed-rg.js +0 -337
  73. package/dist/cli/managed-rg.js.map +0 -1
  74. package/dist/cli/server-search.d.ts +0 -3
  75. package/dist/cli/server-search.js +0 -12
  76. package/dist/cli/server-search.js.map +0 -1
  77. package/dist/cli/types.d.ts +0 -87
  78. package/dist/cli/types.js +0 -10
  79. package/dist/cli/types.js.map +0 -1
  80. package/dist/cli/version.d.ts +0 -1
  81. package/dist/cli/version.js +0 -13
  82. package/dist/cli/version.js.map +0 -1
  83. package/dist/client/daemon-client.d.ts +0 -31
  84. package/dist/client/daemon-client.js +0 -203
  85. package/dist/client/daemon-client.js.map +0 -1
  86. package/dist/client/mode-router.d.ts +0 -9
  87. package/dist/client/mode-router.js +0 -28
  88. package/dist/client/mode-router.js.map +0 -1
  89. package/dist/client/search-policy.d.ts +0 -11
  90. package/dist/client/search-policy.js +0 -15
  91. package/dist/client/search-policy.js.map +0 -1
  92. package/dist/daemon/backend.d.ts +0 -78
  93. package/dist/daemon/backend.js +0 -1089
  94. package/dist/daemon/backend.js.map +0 -1
  95. package/dist/daemon/change-set.d.ts +0 -30
  96. package/dist/daemon/change-set.js +0 -163
  97. package/dist/daemon/change-set.js.map +0 -1
  98. package/dist/daemon/config.d.ts +0 -27
  99. package/dist/daemon/config.js +0 -107
  100. package/dist/daemon/config.js.map +0 -1
  101. package/dist/daemon/errors.d.ts +0 -5
  102. package/dist/daemon/errors.js +0 -11
  103. package/dist/daemon/errors.js.map +0 -1
  104. package/dist/daemon/http-server.d.ts +0 -30
  105. package/dist/daemon/http-server.js +0 -288
  106. package/dist/daemon/http-server.js.map +0 -1
  107. package/dist/daemon/index-coordinator.d.ts +0 -22
  108. package/dist/daemon/index-coordinator.js +0 -55
  109. package/dist/daemon/index-coordinator.js.map +0 -1
  110. package/dist/daemon/job-scheduler.d.ts +0 -73
  111. package/dist/daemon/job-scheduler.js +0 -434
  112. package/dist/daemon/job-scheduler.js.map +0 -1
  113. package/dist/daemon/logger.d.ts +0 -9
  114. package/dist/daemon/logger.js +0 -53
  115. package/dist/daemon/logger.js.map +0 -1
  116. package/dist/daemon/model-pool.d.ts +0 -45
  117. package/dist/daemon/model-pool.js +0 -187
  118. package/dist/daemon/model-pool.js.map +0 -1
  119. package/dist/daemon/root-lease.d.ts +0 -14
  120. package/dist/daemon/root-lease.js +0 -197
  121. package/dist/daemon/root-lease.js.map +0 -1
  122. package/dist/daemon/root-runtime.d.ts +0 -80
  123. package/dist/daemon/root-runtime.js +0 -340
  124. package/dist/daemon/root-runtime.js.map +0 -1
  125. package/dist/daemon/runtime-manager.d.ts +0 -47
  126. package/dist/daemon/runtime-manager.js +0 -252
  127. package/dist/daemon/runtime-manager.js.map +0 -1
  128. package/dist/daemon/runtime.d.ts +0 -12
  129. package/dist/daemon/runtime.js +0 -104
  130. package/dist/daemon/runtime.js.map +0 -1
  131. package/dist/daemon/server-controller.d.ts +0 -38
  132. package/dist/daemon/server-controller.js +0 -327
  133. package/dist/daemon/server-controller.js.map +0 -1
  134. package/dist/daemon/watch-manager.d.ts +0 -55
  135. package/dist/daemon/watch-manager.js +0 -347
  136. package/dist/daemon/watch-manager.js.map +0 -1
  137. package/dist/daemon/workspace-read-session-cache.d.ts +0 -31
  138. package/dist/daemon/workspace-read-session-cache.js +0 -115
  139. package/dist/daemon/workspace-read-session-cache.js.map +0 -1
  140. package/dist/engine/code-formats.d.ts +0 -3
  141. package/dist/engine/code-formats.js +0 -14
  142. package/dist/engine/code-formats.js.map +0 -1
  143. package/dist/engine/config.d.ts +0 -50
  144. package/dist/engine/config.js +0 -321
  145. package/dist/engine/config.js.map +0 -1
  146. package/dist/engine/errors.d.ts +0 -20
  147. package/dist/engine/errors.js +0 -51
  148. package/dist/engine/errors.js.map +0 -1
  149. package/dist/engine/extraction/code/adapter.d.ts +0 -19
  150. package/dist/engine/extraction/code/adapter.js +0 -24
  151. package/dist/engine/extraction/code/adapter.js.map +0 -1
  152. package/dist/engine/extraction/code/extractor.d.ts +0 -13
  153. package/dist/engine/extraction/code/extractor.js +0 -696
  154. package/dist/engine/extraction/code/extractor.js.map +0 -1
  155. package/dist/engine/extraction/code/families/c-family.d.ts +0 -2
  156. package/dist/engine/extraction/code/families/c-family.js +0 -122
  157. package/dist/engine/extraction/code/families/c-family.js.map +0 -1
  158. package/dist/engine/extraction/code/families/js-ts.d.ts +0 -12
  159. package/dist/engine/extraction/code/families/js-ts.js +0 -118
  160. package/dist/engine/extraction/code/families/js-ts.js.map +0 -1
  161. package/dist/engine/extraction/code/families/metadata.d.ts +0 -8
  162. package/dist/engine/extraction/code/families/metadata.js +0 -86
  163. package/dist/engine/extraction/code/families/metadata.js.map +0 -1
  164. package/dist/engine/extraction/code/families/name-field.d.ts +0 -3
  165. package/dist/engine/extraction/code/families/name-field.js +0 -22
  166. package/dist/engine/extraction/code/families/name-field.js.map +0 -1
  167. package/dist/engine/extraction/code/languages/c.d.ts +0 -1
  168. package/dist/engine/extraction/code/languages/c.js +0 -12
  169. package/dist/engine/extraction/code/languages/c.js.map +0 -1
  170. package/dist/engine/extraction/code/languages/cpp.d.ts +0 -1
  171. package/dist/engine/extraction/code/languages/cpp.js +0 -18
  172. package/dist/engine/extraction/code/languages/cpp.js.map +0 -1
  173. package/dist/engine/extraction/code/languages/go.d.ts +0 -2
  174. package/dist/engine/extraction/code/languages/go.js +0 -70
  175. package/dist/engine/extraction/code/languages/go.js.map +0 -1
  176. package/dist/engine/extraction/code/languages/java.d.ts +0 -1
  177. package/dist/engine/extraction/code/languages/java.js +0 -17
  178. package/dist/engine/extraction/code/languages/java.js.map +0 -1
  179. package/dist/engine/extraction/code/languages/javascript.d.ts +0 -2
  180. package/dist/engine/extraction/code/languages/javascript.js +0 -23
  181. package/dist/engine/extraction/code/languages/javascript.js.map +0 -1
  182. package/dist/engine/extraction/code/languages/python.d.ts +0 -2
  183. package/dist/engine/extraction/code/languages/python.js +0 -52
  184. package/dist/engine/extraction/code/languages/python.js.map +0 -1
  185. package/dist/engine/extraction/code/languages/rust.d.ts +0 -2
  186. package/dist/engine/extraction/code/languages/rust.js +0 -25
  187. package/dist/engine/extraction/code/languages/rust.js.map +0 -1
  188. package/dist/engine/extraction/code/languages/typescript.d.ts +0 -2
  189. package/dist/engine/extraction/code/languages/typescript.js +0 -37
  190. package/dist/engine/extraction/code/languages/typescript.js.map +0 -1
  191. package/dist/engine/extraction/code/tree-sitter/grammar.d.ts +0 -4
  192. package/dist/engine/extraction/code/tree-sitter/grammar.js +0 -49
  193. package/dist/engine/extraction/code/tree-sitter/grammar.js.map +0 -1
  194. package/dist/engine/extraction/code/tree-sitter/nodes.d.ts +0 -3
  195. package/dist/engine/extraction/code/tree-sitter/nodes.js +0 -27
  196. package/dist/engine/extraction/code/tree-sitter/nodes.js.map +0 -1
  197. package/dist/engine/extraction/code/tree-sitter/parser.d.ts +0 -2
  198. package/dist/engine/extraction/code/tree-sitter/parser.js +0 -31
  199. package/dist/engine/extraction/code/tree-sitter/parser.js.map +0 -1
  200. package/dist/engine/extraction/ids.d.ts +0 -1
  201. package/dist/engine/extraction/ids.js +0 -5
  202. package/dist/engine/extraction/ids.js.map +0 -1
  203. package/dist/engine/extraction/image/extractor.d.ts +0 -5
  204. package/dist/engine/extraction/image/extractor.js +0 -35
  205. package/dist/engine/extraction/image/extractor.js.map +0 -1
  206. package/dist/engine/extraction/index.d.ts +0 -4
  207. package/dist/engine/extraction/index.js +0 -3
  208. package/dist/engine/extraction/index.js.map +0 -1
  209. package/dist/engine/extraction/markdown/extractor.d.ts +0 -7
  210. package/dist/engine/extraction/markdown/extractor.js +0 -362
  211. package/dist/engine/extraction/markdown/extractor.js.map +0 -1
  212. package/dist/engine/extraction/runtime.d.ts +0 -9
  213. package/dist/engine/extraction/runtime.js +0 -37
  214. package/dist/engine/extraction/runtime.js.map +0 -1
  215. package/dist/engine/extraction/source.d.ts +0 -18
  216. package/dist/engine/extraction/source.js +0 -22
  217. package/dist/engine/extraction/source.js.map +0 -1
  218. package/dist/engine/extraction/text/extractor.d.ts +0 -7
  219. package/dist/engine/extraction/text/extractor.js +0 -168
  220. package/dist/engine/extraction/text/extractor.js.map +0 -1
  221. package/dist/engine/extraction/types.d.ts +0 -4
  222. package/dist/engine/extraction/types.js +0 -2
  223. package/dist/engine/extraction/types.js.map +0 -1
  224. package/dist/engine/extraction/vector-content.d.ts +0 -5
  225. package/dist/engine/extraction/vector-content.js +0 -83
  226. package/dist/engine/extraction/vector-content.js.map +0 -1
  227. package/dist/engine/file-size-policy.d.ts +0 -6
  228. package/dist/engine/file-size-policy.js +0 -20
  229. package/dist/engine/file-size-policy.js.map +0 -1
  230. package/dist/engine/file-type.d.ts +0 -15
  231. package/dist/engine/file-type.js +0 -152
  232. package/dist/engine/file-type.js.map +0 -1
  233. package/dist/engine/index-status.d.ts +0 -9
  234. package/dist/engine/index-status.js +0 -38
  235. package/dist/engine/index-status.js.map +0 -1
  236. package/dist/engine/manifest.d.ts +0 -13
  237. package/dist/engine/manifest.js +0 -124
  238. package/dist/engine/manifest.js.map +0 -1
  239. package/dist/engine/models/artifact-cache-lock.d.ts +0 -16
  240. package/dist/engine/models/artifact-cache-lock.js +0 -228
  241. package/dist/engine/models/artifact-cache-lock.js.map +0 -1
  242. package/dist/engine/models/artifact-downloader.d.ts +0 -77
  243. package/dist/engine/models/artifact-downloader.js +0 -766
  244. package/dist/engine/models/artifact-downloader.js.map +0 -1
  245. package/dist/engine/models/backends/llama-cpp.d.ts +0 -99
  246. package/dist/engine/models/backends/llama-cpp.js +0 -573
  247. package/dist/engine/models/backends/llama-cpp.js.map +0 -1
  248. package/dist/engine/models/backends/model2vec-runtime.d.ts +0 -63
  249. package/dist/engine/models/backends/model2vec-runtime.js +0 -112
  250. package/dist/engine/models/backends/model2vec-runtime.js.map +0 -1
  251. package/dist/engine/models/backends/model2vec-tokenizer.d.ts +0 -2
  252. package/dist/engine/models/backends/model2vec-tokenizer.js +0 -42
  253. package/dist/engine/models/backends/model2vec-tokenizer.js.map +0 -1
  254. package/dist/engine/models/backends/model2vec-worker-pool.d.ts +0 -25
  255. package/dist/engine/models/backends/model2vec-worker-pool.js +0 -264
  256. package/dist/engine/models/backends/model2vec-worker-pool.js.map +0 -1
  257. package/dist/engine/models/backends/model2vec-worker.d.ts +0 -1
  258. package/dist/engine/models/backends/model2vec-worker.js +0 -41
  259. package/dist/engine/models/backends/model2vec-worker.js.map +0 -1
  260. package/dist/engine/models/backends/model2vec.d.ts +0 -38
  261. package/dist/engine/models/backends/model2vec.js +0 -344
  262. package/dist/engine/models/backends/model2vec.js.map +0 -1
  263. package/dist/engine/models/backends/qwen.d.ts +0 -44
  264. package/dist/engine/models/backends/qwen.js +0 -411
  265. package/dist/engine/models/backends/qwen.js.map +0 -1
  266. package/dist/engine/models/backends/transformers-js.d.ts +0 -87
  267. package/dist/engine/models/backends/transformers-js.js +0 -360
  268. package/dist/engine/models/backends/transformers-js.js.map +0 -1
  269. package/dist/engine/models/catalog.d.ts +0 -490
  270. package/dist/engine/models/catalog.js +0 -522
  271. package/dist/engine/models/catalog.js.map +0 -1
  272. package/dist/engine/models/download-progress.d.ts +0 -16
  273. package/dist/engine/models/download-progress.js +0 -47
  274. package/dist/engine/models/download-progress.js.map +0 -1
  275. package/dist/engine/models/embeddings.d.ts +0 -69
  276. package/dist/engine/models/embeddings.js +0 -125
  277. package/dist/engine/models/embeddings.js.map +0 -1
  278. package/dist/engine/models/factory.d.ts +0 -2
  279. package/dist/engine/models/factory.js +0 -51
  280. package/dist/engine/models/factory.js.map +0 -1
  281. package/dist/engine/models/index.d.ts +0 -4
  282. package/dist/engine/models/index.js +0 -5
  283. package/dist/engine/models/index.js.map +0 -1
  284. package/dist/engine/models/ranking.d.ts +0 -19
  285. package/dist/engine/models/ranking.js +0 -2
  286. package/dist/engine/models/ranking.js.map +0 -1
  287. package/dist/engine/models/resolution.d.ts +0 -8
  288. package/dist/engine/models/resolution.js +0 -22
  289. package/dist/engine/models/resolution.js.map +0 -1
  290. package/dist/engine/pipeline/indexing/index.d.ts +0 -14
  291. package/dist/engine/pipeline/indexing/index.js +0 -1217
  292. package/dist/engine/pipeline/indexing/index.js.map +0 -1
  293. package/dist/engine/pipeline/indexing/input-budget.d.ts +0 -2
  294. package/dist/engine/pipeline/indexing/input-budget.js +0 -51
  295. package/dist/engine/pipeline/indexing/input-budget.js.map +0 -1
  296. package/dist/engine/pipeline/indexing/root-paths.d.ts +0 -7
  297. package/dist/engine/pipeline/indexing/root-paths.js +0 -148
  298. package/dist/engine/pipeline/indexing/root-paths.js.map +0 -1
  299. package/dist/engine/pipeline/indexing/scanner/index.d.ts +0 -14
  300. package/dist/engine/pipeline/indexing/scanner/index.js +0 -776
  301. package/dist/engine/pipeline/indexing/scanner/index.js.map +0 -1
  302. package/dist/engine/pipeline/search/index.d.ts +0 -12
  303. package/dist/engine/pipeline/search/index.js +0 -811
  304. package/dist/engine/pipeline/search/index.js.map +0 -1
  305. package/dist/engine/service/index.d.ts +0 -3
  306. package/dist/engine/service/index.js +0 -2
  307. package/dist/engine/service/index.js.map +0 -1
  308. package/dist/engine/service/lexical.d.ts +0 -28
  309. package/dist/engine/service/lexical.js +0 -446
  310. package/dist/engine/service/lexical.js.map +0 -1
  311. package/dist/engine/service/root.d.ts +0 -15
  312. package/dist/engine/service/root.js +0 -57
  313. package/dist/engine/service/root.js.map +0 -1
  314. package/dist/engine/service/structure-enrichment.d.ts +0 -8
  315. package/dist/engine/service/structure-enrichment.js +0 -178
  316. package/dist/engine/service/structure-enrichment.js.map +0 -1
  317. package/dist/engine/service/types.d.ts +0 -210
  318. package/dist/engine/service/types.js +0 -2
  319. package/dist/engine/service/types.js.map +0 -1
  320. package/dist/engine/service/workspace-index.d.ts +0 -26
  321. package/dist/engine/service/workspace-index.js +0 -157
  322. package/dist/engine/service/workspace-index.js.map +0 -1
  323. package/dist/engine/service/zvec-grep.d.ts +0 -13
  324. package/dist/engine/service/zvec-grep.js +0 -1393
  325. package/dist/engine/service/zvec-grep.js.map +0 -1
  326. package/dist/engine/storage/index.d.ts +0 -53
  327. package/dist/engine/storage/index.js +0 -3
  328. package/dist/engine/storage/index.js.map +0 -1
  329. package/dist/engine/storage/layout.d.ts +0 -10
  330. package/dist/engine/storage/layout.js +0 -29
  331. package/dist/engine/storage/layout.js.map +0 -1
  332. package/dist/engine/storage/zvec.d.ts +0 -4
  333. package/dist/engine/storage/zvec.js +0 -976
  334. package/dist/engine/storage/zvec.js.map +0 -1
  335. package/dist/engine/types.d.ts +0 -303
  336. package/dist/engine/types.js +0 -14
  337. package/dist/engine/types.js.map +0 -1
  338. package/dist/engine/utils/atomic-file.d.ts +0 -3
  339. package/dist/engine/utils/atomic-file.js +0 -39
  340. package/dist/engine/utils/atomic-file.js.map +0 -1
  341. package/dist/engine/utils/daemon-lease.d.ts +0 -17
  342. package/dist/engine/utils/daemon-lease.js +0 -175
  343. package/dist/engine/utils/daemon-lease.js.map +0 -1
  344. package/dist/engine/utils/file-selection.d.ts +0 -10
  345. package/dist/engine/utils/file-selection.js +0 -157
  346. package/dist/engine/utils/file-selection.js.map +0 -1
  347. package/dist/engine/utils/glob.d.ts +0 -9
  348. package/dist/engine/utils/glob.js +0 -208
  349. package/dist/engine/utils/glob.js.map +0 -1
  350. package/dist/engine/utils/hash.d.ts +0 -2
  351. package/dist/engine/utils/hash.js +0 -8
  352. package/dist/engine/utils/hash.js.map +0 -1
  353. package/dist/engine/utils/json.d.ts +0 -8
  354. package/dist/engine/utils/json.js +0 -83
  355. package/dist/engine/utils/json.js.map +0 -1
  356. package/dist/engine/utils/lock.d.ts +0 -19
  357. package/dist/engine/utils/lock.js +0 -254
  358. package/dist/engine/utils/lock.js.map +0 -1
  359. package/dist/engine/utils/path.d.ts +0 -4
  360. package/dist/engine/utils/path.js +0 -18
  361. package/dist/engine/utils/path.js.map +0 -1
  362. package/dist/engine/utils/timing.d.ts +0 -19
  363. package/dist/engine/utils/timing.js +0 -87
  364. package/dist/engine/utils/timing.js.map +0 -1
  365. package/dist/index-progress.d.ts +0 -10
  366. package/dist/index-progress.js +0 -159
  367. package/dist/index-progress.js.map +0 -1
  368. package/dist/index.d.ts +0 -6
  369. package/dist/index.js +0 -4
  370. package/dist/index.js.map +0 -1
  371. package/dist/mcp/http-transport.d.ts +0 -27
  372. package/dist/mcp/http-transport.js +0 -182
  373. package/dist/mcp/http-transport.js.map +0 -1
  374. package/dist/mcp/input-normalization.d.ts +0 -38
  375. package/dist/mcp/input-normalization.js +0 -104
  376. package/dist/mcp/input-normalization.js.map +0 -1
  377. package/dist/mcp/progress-heartbeat.d.ts +0 -17
  378. package/dist/mcp/progress-heartbeat.js +0 -46
  379. package/dist/mcp/progress-heartbeat.js.map +0 -1
  380. package/dist/mcp/request-metadata.d.ts +0 -2
  381. package/dist/mcp/request-metadata.js +0 -11
  382. package/dist/mcp/request-metadata.js.map +0 -1
  383. package/dist/mcp/request-state.d.ts +0 -40
  384. package/dist/mcp/request-state.js +0 -210
  385. package/dist/mcp/request-state.js.map +0 -1
  386. package/dist/mcp/result-format.d.ts +0 -13
  387. package/dist/mcp/result-format.js +0 -75
  388. package/dist/mcp/result-format.js.map +0 -1
  389. package/dist/mcp/schemas.d.ts +0 -668
  390. package/dist/mcp/schemas.js +0 -512
  391. package/dist/mcp/schemas.js.map +0 -1
  392. package/dist/mcp/stdio-bridge.d.ts +0 -15
  393. package/dist/mcp/stdio-bridge.js +0 -190
  394. package/dist/mcp/stdio-bridge.js.map +0 -1
  395. package/dist/mcp/tools.d.ts +0 -153
  396. package/dist/mcp/tools.js +0 -488
  397. package/dist/mcp/tools.js.map +0 -1
  398. package/dist/mcp/toolset.d.ts +0 -5
  399. package/dist/mcp/toolset.js +0 -12
  400. package/dist/mcp/toolset.js.map +0 -1
  401. package/dist/observability/trace-context.d.ts +0 -11
  402. package/dist/observability/trace-context.js +0 -138
  403. package/dist/observability/trace-context.js.map +0 -1
  404. package/dist/prompts/zvec-grep-guidance.d.ts +0 -2
  405. package/dist/prompts/zvec-grep-guidance.js +0 -9
  406. package/dist/prompts/zvec-grep-guidance.js.map +0 -1
  407. package/docs/01-agents.md +0 -209
  408. package/docs/02-cli.md +0 -247
  409. package/docs/03-mcp.md +0 -185
  410. package/docs/04-pipeline.md +0 -226
  411. package/docs/05-architecture.md +0 -86
  412. package/docs/06-server.md +0 -233
  413. package/docs/07-embedding.md +0 -209
  414. package/docs/08-roadmap.md +0 -85
  415. package/docs/README.md +0 -53
@@ -1,1393 +0,0 @@
1
- import { readFileSync, statSync } from "node:fs";
2
- import { createHash, createHmac, randomBytes, randomUUID } from "node:crypto";
3
- import { basename, dirname, join, relative, resolve } from "node:path";
4
- import { globalConfigPath, readGlobalConfig, resolveEmbeddingRuntimeOptions, } from "../config.js";
5
- import { isWorkspaceIndexed, WorkspaceIndex } from "./workspace-index.js";
6
- import { workspaceIndexDetail, detail, EngineError, errorDetails, } from "../errors.js";
7
- import { createEmbeddingModel, EmbeddingPurpose, resolveEmbeddingReference, } from "../models/index.js";
8
- import { CURRENT_INDEX_VERSION } from "../types.js";
9
- import { indexStatusNeedsRefresh } from "../index-status.js";
10
- import { workspaceIndexInfoFromManifest, CURRENT_MANIFEST_VERSION, readWorkspaceManifest, writeWorkspaceManifest, } from "../manifest.js";
11
- import { validateRootPaths } from "../pipeline/indexing/root-paths.js";
12
- import { findNearestWorkspace, hasWorkspaceIndex, resetWorkspaceIndex, resolveZvecGrepRoot, workspaceHome, workspaceIndexLocation, } from "./root.js";
13
- import { runRgSearch } from "./lexical.js";
14
- import { defaultHome, normalizePath } from "../utils/path.js";
15
- import { acquireReadWriteLock, assertNoWriteLock, } from "../utils/lock.js";
16
- import { assertDaemonWriteAllowed } from "../utils/daemon-lease.js";
17
- import { TimingCollector } from "../utils/timing.js";
18
- import { enrichLexicalItemsWithStructure } from "./structure-enrichment.js";
19
- import { remoteEmbeddingAuthorizationGuard } from "../../authorization/operation.js";
20
- import { RemoteEmbeddingAuthorizationStore } from "../../authorization/store.js";
21
- const DEFAULT_CONTEXT_LIMIT = 10;
22
- const DEFAULT_CONTEXT_TOTAL_LIMIT = 30;
23
- const DEFAULT_LOCAL_EMBEDDING = "local/potion-code-16m-v2";
24
- const PROVIDER_API_KEY_IDENTITY_SECRET = randomBytes(32);
25
- const MAX_RECOVERED_EMBEDDING_MODELS = 4;
26
- export async function createZvecGrep(options = {}) {
27
- return new ZvecGrepService(options);
28
- }
29
- export function openWorkspaceReadSession(startRoot, embeddingModel) {
30
- const start = resolveZvecGrepRoot(startRoot);
31
- assertNearestWorkspaceHomeUnlocked(start, "daemon.context.open");
32
- const nearest = findNearestWorkspaceIndex(start);
33
- if (!nearest) {
34
- throw workspaceIndexMissingError(start, "undecided");
35
- }
36
- const { location, info } = nearest;
37
- if (info.indexPolicy === "disabled") {
38
- throw workspaceIndexDisabledError(location.root);
39
- }
40
- if (!isWorkspaceIndexed(info) || !hasWorkspaceIndex(location)) {
41
- throw workspaceIndexMissingError(location.root, info.indexPolicy ?? "enabled");
42
- }
43
- const workspaceIndex = new WorkspaceIndex(info, {
44
- mode: "read",
45
- embeddingModel,
46
- });
47
- let closed = false;
48
- return {
49
- root: location.root,
50
- async context(options) {
51
- if (closed) {
52
- throw new EngineError("Workspace read session is already closed", {
53
- code: "ZVEC_GREP.ENGINE.SERVICE.READ_SESSION_CLOSED",
54
- });
55
- }
56
- const timings = new TimingCollector();
57
- const request = normalizeContextRequest(options);
58
- const result = await timings.time("total", () => withHomeReadLock(location.home, "daemon.context", () => contextFromOpenWorkspaceIndex({
59
- root: location.root,
60
- request,
61
- workspaceIndex,
62
- options: { ...options, autoUpdate: false },
63
- timings,
64
- })));
65
- return withContextTimings(result, timings);
66
- },
67
- async close() {
68
- if (closed) {
69
- return;
70
- }
71
- workspaceIndex.close();
72
- closed = true;
73
- },
74
- };
75
- }
76
- export function createEmbeddingModelForIdentity(identity, options = {}, workspaceRuntime = {}) {
77
- const reference = embeddingModelReference(identity);
78
- return createServiceEmbeddingModel(reference, providerOptions(options, identity, readGlobalConfig(), workspaceRuntime), options);
79
- }
80
- export function embeddingModelPoolKeyForIdentity(identity, options = {}, workspaceRuntime = {}) {
81
- const reference = embeddingModelReference(identity);
82
- const fingerprint = providerOptionsFingerprint(providerOptions(options, identity, readGlobalConfig(), workspaceRuntime));
83
- return [reference, fingerprint].join("/");
84
- }
85
- class ZvecGrepService {
86
- options;
87
- root;
88
- embeddingModel;
89
- recoveredEmbeddingModels = new Map();
90
- retiredEmbeddingModels = new Set();
91
- activeEmbeddingModelOperations = 0;
92
- closed = false;
93
- constructor(options) {
94
- this.options = options;
95
- this.root = resolveZvecGrepRoot(options.root);
96
- this.embeddingModel = options.embeddingModel;
97
- }
98
- async index(options = {}) {
99
- this.ensureOpen();
100
- const root = resolveZvecGrepRoot(options.root ?? this.root);
101
- const daemonWritePermit = assertDaemonWriteAllowed(root, this.options.daemonInstanceToken);
102
- const location = workspaceIndexLocation(root);
103
- try {
104
- return await this.withEmbeddingModelOperation(() => withHomeWriteLock(location.home, options.rebuild ? "index.rebuild" : "index", async () => {
105
- const existing = readWorkspaceManifest(location.home);
106
- const existingRuntime = existing?.embeddingRuntime ?? {};
107
- const embeddingModel = this.embeddingModelForIndex(existing, "index", existingRuntime);
108
- const effectiveRuntime = effectiveEmbeddingRuntime(this.options, embeddingModel, runtimeForModelProvider(existing, embeddingModel, existingRuntime));
109
- if (!options.rebuild) {
110
- assertWorkspaceEndpointMatchesCurrentRuntime(existing, existingRuntime, effectiveRuntime, "zg index --endpoint <url> --rebuild");
111
- }
112
- const rootPaths = resolveIndexRootPaths(existing, options.rootPaths, root, {
113
- resetPaths: options.resetPaths === true,
114
- includePaths: options.includePaths,
115
- excludePaths: options.excludePaths,
116
- globs: options.globs,
117
- insensitiveGlobs: options.insensitiveGlobs,
118
- fileTypes: options.fileTypes,
119
- excludedFileTypes: options.excludedFileTypes,
120
- hidden: options.hidden,
121
- noIgnore: options.noIgnore,
122
- ignoreFiles: options.ignoreFiles,
123
- maxDepth: options.maxDepth,
124
- maxFileSizeBytes: options.maxFileSizeBytes,
125
- follow: options.follow,
126
- });
127
- if (options.rebuild || !isWorkspaceIndexed(existing)) {
128
- resetWorkspaceIndex(location);
129
- }
130
- const existingAfterRebuild = options.rebuild ? null : existing;
131
- if (isWorkspaceIndexed(existingAfterRebuild)) {
132
- assertWorkspaceEmbeddingMatchesCurrentModel(existingAfterRebuild, embeddingModel, "zg index --rebuild");
133
- }
134
- const embeddingRuntime = embeddingRuntimeAfterIndex(existing, existingRuntime, embeddingModel, effectiveRuntime, this.options);
135
- const manifest = prepareWorkspaceManifest(location, existingAfterRebuild, rootPaths, embeddingModel, embeddingRuntime);
136
- const workspaceIndex = new WorkspaceIndex(manifest, {
137
- mode: "write",
138
- embeddingModel,
139
- });
140
- writeWorkspaceManifest(location.home, manifest);
141
- try {
142
- const releaseWriterContext = options.onWriterContext?.((contextOptions) => this.contextFromWriterWorkspaceIndex(root, workspaceIndex, contextOptions));
143
- try {
144
- const result = await workspaceIndex.index({
145
- rebuild: false,
146
- embeddingConcurrency: options.embeddingConcurrency,
147
- onProgress: options.onProgress,
148
- changedPaths: options.changedPaths,
149
- signal: options.signal,
150
- });
151
- writeWorkspaceManifest(location.home, {
152
- ...manifest,
153
- embeddingRuntime,
154
- updatedTime: Date.now(),
155
- });
156
- return result;
157
- }
158
- catch (error) {
159
- writeWorkspaceManifest(location.home, {
160
- ...manifest,
161
- embeddingRuntime,
162
- updatedTime: Date.now(),
163
- });
164
- throw error;
165
- }
166
- finally {
167
- await releaseWriterContext?.();
168
- }
169
- }
170
- finally {
171
- workspaceIndex.close();
172
- }
173
- }));
174
- }
175
- finally {
176
- daemonWritePermit?.release();
177
- }
178
- }
179
- async dropIndex(options = {}) {
180
- this.ensureOpen();
181
- const root = resolveZvecGrepRoot(options.root ?? this.root);
182
- const daemonWritePermit = assertDaemonWriteAllowed(root, this.options.daemonInstanceToken);
183
- const location = workspaceIndexLocation(root);
184
- try {
185
- if (!readWorkspaceManifest(location.home)) {
186
- return false;
187
- }
188
- return await withHomeWriteLock(location.home, "index.drop", async () => {
189
- resetWorkspaceIndex(location);
190
- return true;
191
- });
192
- }
193
- finally {
194
- daemonWritePermit?.release();
195
- }
196
- }
197
- async disableIndex(options = {}) {
198
- this.ensureOpen();
199
- const root = resolveZvecGrepRoot(options.root ?? this.root);
200
- const daemonWritePermit = assertDaemonWriteAllowed(root, this.options.daemonInstanceToken);
201
- const location = workspaceIndexLocation(root);
202
- try {
203
- await withHomeWriteLock(location.home, "index.disable", async () => {
204
- const existing = readWorkspaceManifest(location.home);
205
- const now = Date.now();
206
- writeWorkspaceManifest(location.home, {
207
- manifestVersion: CURRENT_MANIFEST_VERSION,
208
- id: existing?.id ?? randomUUID(),
209
- name: existing?.name ?? workspaceDisplayName(root),
210
- path: location.home,
211
- rootPaths: existing?.rootPaths ?? validateRootPaths([root]),
212
- indexPolicy: "disabled",
213
- embedding: null,
214
- indexVersion: null,
215
- createdTime: existing?.createdTime ?? now,
216
- updatedTime: now,
217
- embeddingRuntime: existing?.embeddingRuntime ?? {},
218
- });
219
- });
220
- return this.info({ root });
221
- }
222
- finally {
223
- daemonWritePermit?.release();
224
- }
225
- }
226
- async context(options) {
227
- this.ensureOpen();
228
- return await this.withEmbeddingModelOperation(async () => {
229
- const timings = new TimingCollector();
230
- const result = await timings.time("total", () => this.contextWithTimings(options, timings));
231
- return withContextTimings(result, timings);
232
- });
233
- }
234
- async contextWithTimings(options, timings) {
235
- const request = normalizeContextRequest(options);
236
- const startRoot = resolveZvecGrepRoot(options.root ?? this.root);
237
- if (options.rg) {
238
- return this.contextFromRg(startRoot, request, options, timings);
239
- }
240
- assertNearestWorkspaceHomeUnlocked(startRoot, "context");
241
- const nearest = findNearestWorkspaceIndex(startRoot);
242
- if (nearest) {
243
- const { location, info } = nearest;
244
- if (info.indexPolicy === "disabled") {
245
- throw workspaceIndexDisabledError(location.root);
246
- }
247
- if (!isWorkspaceIndexed(info) || !hasWorkspaceIndex(location)) {
248
- throw workspaceIndexMissingError(location.root, info.indexPolicy ?? "enabled");
249
- }
250
- if (options.autoUpdate !== false) {
251
- await this.refreshWorkspaceIndexForContext(location, options, timings);
252
- }
253
- return await withHomeReadLock(location.home, "context", () => this.contextFromWorkspaceIndex(location, request, options, timings));
254
- }
255
- throw workspaceIndexMissingError(startRoot, "undecided");
256
- }
257
- async info(options = {}) {
258
- this.ensureOpen();
259
- const startRoot = resolveZvecGrepRoot(options.root ?? this.root);
260
- assertNearestWorkspaceHomeUnlocked(startRoot, "info");
261
- const nearest = findNearestWorkspaceIndex(startRoot);
262
- if (!nearest) {
263
- const location = workspaceIndexLocation(startRoot);
264
- return {
265
- root: startRoot,
266
- indexed: false,
267
- indexPolicy: "undecided",
268
- home: location.home,
269
- indexPath: location.indexPath,
270
- source: "unindexed",
271
- suggestion: "zg index or zg query --rg",
272
- };
273
- }
274
- return await withHomeReadLock(nearest.location.home, "info", async () => {
275
- const workspaceIndex = readWorkspaceManifest(nearest.location.home);
276
- const indexed = workspaceIndex !== null &&
277
- workspaceIndex.indexPolicy !== "disabled" &&
278
- isWorkspaceIndexed(workspaceIndex) &&
279
- hasWorkspaceIndex(nearest.location);
280
- return {
281
- root: nearest.location.root,
282
- indexed,
283
- indexPolicy: workspaceIndex?.indexPolicy ?? "undecided",
284
- home: nearest.location.home,
285
- indexPath: nearest.location.indexPath,
286
- source: indexed ? "index" : "unindexed",
287
- workspaceIndex: workspaceIndex
288
- ? workspaceIndexInfoFromManifest(workspaceIndex)
289
- : undefined,
290
- status: indexed && options.includeStatus !== false
291
- ? await workspaceIndexStatus(workspaceIndex, nearest.location)
292
- : null,
293
- suggestion: workspaceInfoSuggestion(workspaceIndex),
294
- };
295
- });
296
- }
297
- async close() {
298
- const models = new Set([
299
- ...(this.embeddingModel &&
300
- this.options.embeddingModelOwnership !== "borrowed"
301
- ? [this.embeddingModel]
302
- : []),
303
- ...this.recoveredEmbeddingModels.values(),
304
- ...this.retiredEmbeddingModels,
305
- ]);
306
- this.recoveredEmbeddingModels.clear();
307
- this.retiredEmbeddingModels.clear();
308
- for (const model of models) {
309
- await model.dispose();
310
- }
311
- this.closed = true;
312
- }
313
- async contextFromWorkspaceIndex(location, request, options, timings) {
314
- const info = readWorkspaceManifest(location.home);
315
- if (!info) {
316
- throw new EngineError("Workspace index manifest not found", {
317
- code: "ZVEC_GREP.ENGINE.MANIFEST.NOT_FOUND",
318
- });
319
- }
320
- const workspaceIndex = this.openWorkspaceIndexForSearch(info, request, info.embeddingRuntime);
321
- try {
322
- return await contextFromOpenWorkspaceIndex({
323
- root: location.root,
324
- request,
325
- workspaceIndex,
326
- options,
327
- timings,
328
- });
329
- }
330
- finally {
331
- workspaceIndex.close();
332
- }
333
- }
334
- async refreshWorkspaceIndexForContext(location, options, timings) {
335
- const daemonWritePermit = assertDaemonWriteAllowed(location.root, this.options.daemonInstanceToken);
336
- try {
337
- const needsRefresh = await timings.time("status_scan", () => withHomeReadLock(location.home, "context.status", async () => {
338
- const manifest = readWorkspaceManifest(location.home);
339
- const status = manifest
340
- ? await workspaceIndexStatus(manifest, location)
341
- : null;
342
- return indexStatusNeedsRefresh(status);
343
- }));
344
- if (!needsRefresh) {
345
- return;
346
- }
347
- await timings.time("auto_update", () => withHomeWriteLock(location.home, "context.refresh", async () => {
348
- const existing = readWorkspaceManifest(location.home);
349
- if (!existing) {
350
- return;
351
- }
352
- const stillNeedsRefresh = await timings.time("refresh_status_scan", () => workspaceIndexNeedsRefresh(location));
353
- if (!stillNeedsRefresh) {
354
- return;
355
- }
356
- const workspaceRuntime = existing.embeddingRuntime;
357
- const embeddingModel = this.embeddingModelForIndex(existing, "context.refresh", workspaceRuntime);
358
- assertWorkspaceEndpointMatchesCurrentRuntime(existing, workspaceRuntime, effectiveEmbeddingRuntime(this.options, embeddingModel, workspaceRuntime), "zg index --endpoint <url> --rebuild");
359
- assertWorkspaceEmbeddingMatchesCurrentModel(existing, embeddingModel, "zg index --rebuild");
360
- const workspaceIndex = new WorkspaceIndex(existing, {
361
- mode: "write",
362
- embeddingModel,
363
- });
364
- try {
365
- const result = await workspaceIndex.index({
366
- embeddingConcurrency: options.embeddingConcurrency,
367
- onProgress: options.onAutoUpdateProgress,
368
- });
369
- timings.addEntries(result.timings, "auto_update_");
370
- }
371
- finally {
372
- workspaceIndex.close();
373
- }
374
- }));
375
- }
376
- finally {
377
- daemonWritePermit?.release();
378
- }
379
- }
380
- async contextFromWriterWorkspaceIndex(root, workspaceIndex, options) {
381
- return await this.withEmbeddingModelOperation(async () => {
382
- const timings = new TimingCollector();
383
- const request = normalizeContextRequest(options);
384
- const result = await timings.time("total", () => contextFromOpenWorkspaceIndex({
385
- root,
386
- request,
387
- workspaceIndex,
388
- options: { ...options, autoUpdate: false },
389
- timings,
390
- }));
391
- return withContextTimings(result, timings);
392
- });
393
- }
394
- async contextFromRg(root, request, options, timings) {
395
- let rgResult;
396
- try {
397
- rgResult = await timings.time("rg_search", () => runRgSearch({
398
- root,
399
- patterns: request.rgPatterns,
400
- paths: options.rgPaths,
401
- limit: options.limit,
402
- includePaths: options.includePaths,
403
- excludePaths: options.excludePaths,
404
- globs: options.globs,
405
- insensitiveGlobs: options.insensitiveGlobs,
406
- fileTypes: options.fileTypes,
407
- excludedFileTypes: options.excludedFileTypes,
408
- hidden: options.hidden,
409
- noIgnore: options.noIgnore,
410
- ignoreFiles: options.ignoreFiles,
411
- maxDepth: options.maxDepth,
412
- maxFileSizeBytes: options.maxFileSizeBytes,
413
- follow: options.follow,
414
- modifiedAfter: options.modifiedAfter,
415
- modifiedBefore: options.modifiedBefore,
416
- rgOptions: options.rgOptions,
417
- }));
418
- }
419
- catch (cause) {
420
- throw new EngineError("Search failed", {
421
- code: "ZVEC_GREP.ENGINE.SEARCH.FAILED",
422
- context: errorDetails([detail("source", "rg"), detail("root", root)]),
423
- cause,
424
- });
425
- }
426
- const structuralEnrichment = await timings.time("structure_enrichment", () => enrichLexicalItemsWithStructure(root, rgResult.items, undefined, options.maxFileSizeBytes));
427
- const items = dedupeAndRerankContextItems(structuralEnrichment.items);
428
- const emptyReason = items.length === 0 ? rgEmptyReason(rgResult.diagnostics) : undefined;
429
- return {
430
- query: request.displayQuery,
431
- root,
432
- source: "rg",
433
- coverage: rgResult.diagnostics.truncated
434
- ? "rg_truncated"
435
- : "rg_exhaustive",
436
- items,
437
- diagnostics: {
438
- emptyReason,
439
- rg: rgResult.diagnostics,
440
- structure: structuralEnrichment.diagnostics,
441
- },
442
- };
443
- }
444
- openWorkspaceIndexForSearch(info, request, workspaceRuntime) {
445
- return new WorkspaceIndex(info, {
446
- mode: "read",
447
- embeddingModel: this.embeddingModelForSearch(indexedEmbeddingSchema(info), request, workspaceRuntime, info),
448
- });
449
- }
450
- embeddingModelForSearch(schema, request, workspaceRuntime, info) {
451
- if (!request.routes.some((route) => route.mode === "vector")) {
452
- return undefined;
453
- }
454
- const identity = {
455
- provider: schema.provider,
456
- name: schema.model,
457
- };
458
- const effectiveRuntime = resolveEmbeddingRuntimeOptions(embeddingModelReference(identity), this.options, workspaceRuntime, readGlobalConfig());
459
- assertSearchEndpointMatchesWorkspace(info, workspaceRuntime, effectiveRuntime);
460
- if (this.embeddingModel) {
461
- return this.embeddingModel;
462
- }
463
- return this.recoverEmbeddingModel(schema, workspaceRuntime);
464
- }
465
- embeddingModelForIndex(existing, operation, workspaceRuntime = {}) {
466
- if (isWorkspaceIndexed(existing) &&
467
- !this.embeddingModel &&
468
- !this.options.embedding) {
469
- return this.recoverEmbeddingModel(existing.embedding, workspaceRuntime);
470
- }
471
- const config = readGlobalConfig();
472
- const reference = resolveEmbeddingReference({
473
- explicit: this.options.embedding,
474
- globalDefault: config.defaults?.embedding,
475
- fallback: DEFAULT_LOCAL_EMBEDDING,
476
- });
477
- const referenceIdentity = reference
478
- ? parseEmbeddingModelReference(reference)
479
- : undefined;
480
- const selectedWorkspaceRuntime = isWorkspaceIndexed(existing) &&
481
- referenceIdentity &&
482
- existing.embedding.provider !== referenceIdentity.provider
483
- ? {}
484
- : workspaceRuntime;
485
- return (this.embeddingModel ??
486
- (reference
487
- ? this.embeddingModelFromReference(reference, config, selectedWorkspaceRuntime)
488
- : undefined) ??
489
- this.requireEmbeddingModel(operation));
490
- }
491
- recoverEmbeddingModel(schema, workspaceRuntime = {}) {
492
- const config = readGlobalConfig();
493
- const identity = {
494
- provider: schema.provider,
495
- name: schema.model,
496
- };
497
- const reference = embeddingModelReference(identity);
498
- const options = providerOptions(this.options, identity, config, workspaceRuntime);
499
- const key = `${reference}/${providerOptionsFingerprint(options)}`;
500
- return this.cachedEmbeddingModel(key, () => createServiceEmbeddingModel(reference, options, this.options));
501
- }
502
- embeddingModelFromReference(reference, config = readGlobalConfig(), workspaceRuntime = {}) {
503
- const identity = parseEmbeddingModelReference(reference);
504
- const options = providerOptions(this.options, identity, config, workspaceRuntime);
505
- const key = `configured/${reference}/${providerOptionsFingerprint(options)}`;
506
- return this.cachedEmbeddingModel(key, () => createServiceEmbeddingModel(reference, options, this.options));
507
- }
508
- cachedEmbeddingModel(key, create) {
509
- const cached = this.recoveredEmbeddingModels.get(key);
510
- if (cached) {
511
- this.recoveredEmbeddingModels.delete(key);
512
- this.recoveredEmbeddingModels.set(key, cached);
513
- return cached;
514
- }
515
- const model = create();
516
- this.recoveredEmbeddingModels.set(key, model);
517
- this.trimRecoveredEmbeddingModels();
518
- return model;
519
- }
520
- trimRecoveredEmbeddingModels() {
521
- while (this.recoveredEmbeddingModels.size > MAX_RECOVERED_EMBEDDING_MODELS) {
522
- const oldestKey = this.recoveredEmbeddingModels.keys().next().value;
523
- if (oldestKey === undefined) {
524
- return;
525
- }
526
- const model = this.recoveredEmbeddingModels.get(oldestKey);
527
- this.recoveredEmbeddingModels.delete(oldestKey);
528
- if (model) {
529
- this.retiredEmbeddingModels.add(model);
530
- }
531
- }
532
- }
533
- async withEmbeddingModelOperation(operation) {
534
- this.activeEmbeddingModelOperations += 1;
535
- try {
536
- return await operation();
537
- }
538
- finally {
539
- this.activeEmbeddingModelOperations -= 1;
540
- if (this.activeEmbeddingModelOperations === 0) {
541
- await this.disposeRetiredEmbeddingModels();
542
- }
543
- }
544
- }
545
- async disposeRetiredEmbeddingModels() {
546
- const models = [...this.retiredEmbeddingModels];
547
- this.retiredEmbeddingModels.clear();
548
- for (const model of models) {
549
- await model.dispose();
550
- }
551
- }
552
- requireEmbeddingModel(operation) {
553
- if (!this.embeddingModel) {
554
- throw new EngineError("zvec-grep operation requires an embedding model", {
555
- code: "ZVEC_GREP.ENGINE.SERVICE.EMBEDDING_MODEL_REQUIRED",
556
- context: errorDetails([
557
- detail("operation", operation),
558
- detail("hint", `Pass "--embedding <model>", set ZVEC_GREP_EMBEDDING, or configure defaults.embedding in ${globalConfigPath()}. Existing indexes can run "zg index" without --embedding to reuse the stored schema.`),
559
- detail("examples", "local/potion-code-16m-v2, qwen/text-embedding-v4"),
560
- ]),
561
- });
562
- }
563
- return this.embeddingModel;
564
- }
565
- ensureOpen() {
566
- if (this.closed) {
567
- throw new EngineError("zvec-grep service is already closed", {
568
- code: "ZVEC_GREP.ENGINE.SERVICE.CLOSED",
569
- });
570
- }
571
- }
572
- }
573
- async function contextFromOpenWorkspaceIndex(input) {
574
- const searches = [];
575
- const groups = input.options.fuse
576
- ? [
577
- {
578
- id: "Q1",
579
- query: input.request.displayQuery,
580
- role: input.request.groups.some((group) => group.role === "primary")
581
- ? "primary"
582
- : "supplemental",
583
- routes: input.request.routes,
584
- },
585
- ]
586
- : input.request.groups;
587
- const limit = contextGroupLimit(input.options.limit, groups.length);
588
- for (const group of groups) {
589
- const search = await input.workspaceIndex.searchPlan({
590
- routes: group.routes,
591
- limit,
592
- trace: input.options.trace,
593
- preferSymbol: input.options.preferSymbol,
594
- symbolTypes: input.options.symbolTypes,
595
- includePaths: input.options.includePaths,
596
- excludePaths: input.options.excludePaths,
597
- globs: input.options.globs,
598
- insensitiveGlobs: input.options.insensitiveGlobs,
599
- fileTypes: input.options.fileTypes,
600
- excludedFileTypes: input.options.excludedFileTypes,
601
- modifiedAfter: input.options.modifiedAfter,
602
- modifiedBefore: input.options.modifiedBefore,
603
- });
604
- input.timings.addEntries(search.timings);
605
- searches.push(search);
606
- }
607
- const groupItems = searches.map((search, index) => searchPlanToContextItems(search, input.root, groups[index]));
608
- const items = selectAndRankContextItems(groupItems.flat(), groups.filter((group) => group.role === "primary").map((group) => group.id));
609
- return {
610
- query: input.request.displayQuery,
611
- root: input.root,
612
- source: "index",
613
- coverage: "ranked_sample",
614
- workspaceIndex: {
615
- id: input.workspaceIndex.info.id,
616
- name: input.workspaceIndex.info.name,
617
- path: input.workspaceIndex.info.path,
618
- },
619
- items,
620
- groupResults: groups.map((group, index) => ({
621
- id: group.id,
622
- query: group.query,
623
- role: group.role,
624
- items: groupItems[index] ?? [],
625
- })),
626
- diagnostics: {
627
- emptyReason: items.length === 0 ? "no_matches" : undefined,
628
- index: {
629
- hitsReturned: items.length,
630
- queryGroups: groups.map((group) => ({
631
- id: group.id,
632
- query: group.query,
633
- role: group.role,
634
- })),
635
- routes: searches.flatMap((search) => search.plan.routes),
636
- },
637
- },
638
- };
639
- }
640
- async function withHomeReadLock(home, operation, task) {
641
- const lock = acquireHomeLock(home, "read", operation);
642
- try {
643
- return await task();
644
- }
645
- finally {
646
- lock.release();
647
- }
648
- }
649
- async function withHomeWriteLock(home, operation, task) {
650
- const lock = acquireHomeLock(home, "write", operation);
651
- try {
652
- return await task();
653
- }
654
- finally {
655
- lock.release();
656
- }
657
- }
658
- async function workspaceIndexNeedsRefresh(location) {
659
- const manifest = readWorkspaceManifest(location.home);
660
- const status = manifest
661
- ? await workspaceIndexStatus(manifest, location)
662
- : null;
663
- return indexStatusNeedsRefresh(status);
664
- }
665
- function acquireHomeLock(home, mode, operation) {
666
- return acquireReadWriteLock(homeLockPath(home), mode, { operation });
667
- }
668
- function assertHomeUnlocked(home, operation) {
669
- assertNoWriteLock(homeLockPath(home), operation);
670
- }
671
- function assertNearestWorkspaceHomeUnlocked(start, operation) {
672
- let current = resolve(start);
673
- while (true) {
674
- assertHomeUnlocked(workspaceHome(current), operation);
675
- const parent = dirname(current);
676
- if (parent === current) {
677
- return;
678
- }
679
- current = parent;
680
- }
681
- }
682
- function homeLockPath(home) {
683
- return join(home, "locks", "home");
684
- }
685
- function rgEmptyReason(diagnostics) {
686
- return diagnostics.missingPaths &&
687
- diagnostics.missingPaths.length > 0 &&
688
- diagnostics.searchedPaths &&
689
- diagnostics.searchedPaths.length === 0
690
- ? "no_searchable_files"
691
- : "no_matches";
692
- }
693
- function withContextTimings(result, timings) {
694
- const entries = timings.entries();
695
- if (entries.length === 0) {
696
- return result;
697
- }
698
- return {
699
- ...result,
700
- diagnostics: {
701
- ...result.diagnostics,
702
- timings: entries,
703
- },
704
- };
705
- }
706
- function findNearestWorkspaceIndex(start) {
707
- const location = findNearestWorkspace(start);
708
- if (!location)
709
- return null;
710
- const info = readWorkspaceManifest(location.home);
711
- return info ? { location, info } : null;
712
- }
713
- function workspaceInfoSuggestion(workspaceIndex) {
714
- if (!workspaceIndex) {
715
- return "zg index or zg query --rg";
716
- }
717
- if (workspaceIndex.indexPolicy === "disabled") {
718
- return "zg query --rg";
719
- }
720
- if (!isWorkspaceIndexed(workspaceIndex)) {
721
- return "zg index";
722
- }
723
- return undefined;
724
- }
725
- function workspaceIndexMissingError(root, policy) {
726
- return new EngineError("No zvec-grep index found for this workspace", {
727
- code: "ZVEC_GREP.ENGINE.SERVICE.WORKSPACE_INDEX_NOT_FOUND",
728
- context: errorDetails([
729
- detail("root", root),
730
- detail("policy", policy),
731
- detail("hint", policy === "undecided"
732
- ? "Ask the user whether to build an index with zg index, or use zg query --rg for no-index search."
733
- : "Run zg index to build the enabled workspace index, or use zg query --rg for no-index search."),
734
- detail("agent_prompt", policy === "undecided"
735
- ? "Ask the user whether this workspace should be indexed. If yes, run zg index --embedding <model> with appropriate -g/--glob and -t/--type filters; otherwise use zg query --rg for immediate no-index search."
736
- : "This workspace is marked index-enabled but has no built index. Ask before running zg index if an embedding model or cost is involved; otherwise use zg query --rg for immediate no-index search."),
737
- ]),
738
- });
739
- }
740
- function workspaceIndexDisabledError(root) {
741
- return new EngineError("The zvec-grep index is disabled for this workspace", {
742
- code: "ZVEC_GREP.ENGINE.SERVICE.WORKSPACE_INDEX_DISABLED",
743
- context: errorDetails([
744
- detail("root", root),
745
- detail("policy", "disabled"),
746
- detail("hint", "Use zg query --rg for no-index search. Run zg index only if the user explicitly decides to index this workspace."),
747
- detail("agent_action", "do_not_build_index"),
748
- ]),
749
- });
750
- }
751
- function resolveIndexRootPaths(existing, requested, fallbackRoot, options) {
752
- let rootPaths = requested
753
- ? inheritRequestedRootPathSettings(requested, existing?.rootPaths)
754
- : (existing?.rootPaths ?? [fallbackRoot]);
755
- if (options.resetPaths) {
756
- rootPaths = rootPaths.map(resetRootPathFilters);
757
- }
758
- if (options.includePaths !== undefined ||
759
- options.excludePaths !== undefined ||
760
- options.globs !== undefined ||
761
- options.insensitiveGlobs !== undefined ||
762
- options.fileTypes !== undefined ||
763
- options.excludedFileTypes !== undefined ||
764
- options.hidden !== undefined ||
765
- options.noIgnore !== undefined ||
766
- options.ignoreFiles !== undefined ||
767
- options.maxDepth !== undefined ||
768
- options.maxFileSizeBytes !== undefined ||
769
- options.follow !== undefined) {
770
- rootPaths = rootPaths.map((rootPath) => applyRootPathOverrides(rootPath, options));
771
- }
772
- return rootPaths;
773
- }
774
- function inheritRequestedRootPathSettings(requested, existing) {
775
- if (!existing?.length) {
776
- return requested;
777
- }
778
- return requested.map((rootPath) => {
779
- const absolutePath = normalizePath(typeof rootPath === "string" ? rootPath : rootPath.absolutePath);
780
- const inherited = existing.find((candidate) => normalizePath(candidate.absolutePath) === absolutePath);
781
- if (!inherited) {
782
- return rootPath;
783
- }
784
- if (typeof rootPath === "string") {
785
- return { ...inherited, absolutePath };
786
- }
787
- return {
788
- ...inherited,
789
- ...rootPath,
790
- absolutePath,
791
- include: rootPath.include ?? inherited.include,
792
- exclude: rootPath.exclude ?? inherited.exclude,
793
- globs: rootPath.globs ?? inherited.globs,
794
- insensitiveGlobs: rootPath.insensitiveGlobs ?? inherited.insensitiveGlobs,
795
- fileTypes: rootPath.fileTypes ?? inherited.fileTypes,
796
- excludedFileTypes: rootPath.excludedFileTypes ?? inherited.excludedFileTypes,
797
- hidden: rootPath.hidden ?? inherited.hidden,
798
- noIgnore: rootPath.noIgnore ?? inherited.noIgnore,
799
- ignoreFiles: rootPath.ignoreFiles ?? inherited.ignoreFiles,
800
- maxDepth: rootPath.maxDepth ?? inherited.maxDepth,
801
- maxFileSizeBytes: rootPath.maxFileSizeBytes ?? inherited.maxFileSizeBytes,
802
- follow: rootPath.follow ?? inherited.follow,
803
- };
804
- });
805
- }
806
- function resetRootPathFilters(rootPath) {
807
- if (typeof rootPath === "string") {
808
- return rootPath;
809
- }
810
- return {
811
- absolutePath: rootPath.absolutePath,
812
- recursive: rootPath.recursive,
813
- };
814
- }
815
- function applyRootPathOverrides(rootPath, options) {
816
- const normalized = typeof rootPath === "string"
817
- ? {
818
- absolutePath: rootPath,
819
- recursive: true,
820
- }
821
- : rootPath;
822
- return {
823
- ...normalized,
824
- include: options.includePaths ?? normalized.include,
825
- exclude: options.excludePaths ?? normalized.exclude,
826
- globs: options.globs ?? normalized.globs,
827
- insensitiveGlobs: options.insensitiveGlobs ?? normalized.insensitiveGlobs,
828
- fileTypes: options.fileTypes ?? normalized.fileTypes,
829
- excludedFileTypes: options.excludedFileTypes ?? normalized.excludedFileTypes,
830
- hidden: options.hidden ?? normalized.hidden,
831
- noIgnore: options.noIgnore ?? normalized.noIgnore,
832
- ignoreFiles: options.ignoreFiles ?? normalized.ignoreFiles,
833
- maxDepth: options.maxDepth ?? normalized.maxDepth,
834
- maxFileSizeBytes: options.maxFileSizeBytes ?? normalized.maxFileSizeBytes,
835
- follow: options.follow ?? normalized.follow,
836
- };
837
- }
838
- function prepareWorkspaceManifest(location, existing, rootPaths, embeddingModel, embeddingRuntime) {
839
- const now = Date.now();
840
- return {
841
- manifestVersion: CURRENT_MANIFEST_VERSION,
842
- id: existing?.id ?? randomUUID(),
843
- name: existing?.name ?? workspaceDisplayName(location.root),
844
- path: location.home,
845
- rootPaths: validateRootPaths(rootPaths),
846
- indexPolicy: "enabled",
847
- embedding: currentEmbeddingSchema(embeddingModel),
848
- indexVersion: CURRENT_INDEX_VERSION,
849
- createdTime: existing?.createdTime ?? now,
850
- updatedTime: now,
851
- embeddingRuntime,
852
- };
853
- }
854
- function currentEmbeddingSchema(embeddingModel) {
855
- return {
856
- provider: embeddingModel.info.provider,
857
- model: embeddingModel.info.name,
858
- dimension: embeddingModel.info.dimension,
859
- metric: embeddingModel.info.metric,
860
- };
861
- }
862
- function workspaceDisplayName(root) {
863
- return basename(normalizePath(root)) || "workspace";
864
- }
865
- async function workspaceIndexStatus(info, location) {
866
- if (info.indexPolicy === "disabled" ||
867
- !isWorkspaceIndexed(info) ||
868
- !hasWorkspaceIndex(location)) {
869
- return null;
870
- }
871
- const workspaceIndex = new WorkspaceIndex(info, { mode: "read" });
872
- try {
873
- return await workspaceIndex.status();
874
- }
875
- finally {
876
- workspaceIndex.close();
877
- }
878
- }
879
- function indexedEmbeddingSchema(info) {
880
- if (isWorkspaceIndexed(info)) {
881
- return info.embedding;
882
- }
883
- throw new EngineError("zvec-grep index has not been built", {
884
- code: "ZVEC_GREP.ENGINE.SERVICE.INDEX_MISSING",
885
- context: errorDetails([
886
- workspaceIndexDetail(info.name),
887
- detail("hint", "Run zg index to build this index."),
888
- ]),
889
- });
890
- }
891
- function providerOptions(options, identity, config = readGlobalConfig(), workspaceRuntime = {}) {
892
- const reference = embeddingModelReference(identity);
893
- const runtime = resolveEmbeddingRuntimeOptions(reference, options, workspaceRuntime, config);
894
- return {
895
- apiKey: runtime.apiKey,
896
- endpoint: runtime.endpoint,
897
- modelCacheDir: options.modelCacheDir ??
898
- config.defaults?.modelCacheDir ??
899
- process.env.ZVEC_GREP_MODEL_CACHE ??
900
- join(options.home ?? defaultHome(), "models"),
901
- device: runtime.device,
902
- };
903
- }
904
- function createServiceEmbeddingModel(reference, modelOptions, serviceOptions) {
905
- const model = createEmbeddingModel(reference, modelOptions);
906
- if (model.info.provider !== "qwen") {
907
- return model;
908
- }
909
- const endpoint = model.info.endpoint;
910
- if (endpoint === undefined) {
911
- throw new EngineError("Remote embedding model did not provide an endpoint", {
912
- code: "ZVEC_GREP.ENGINE.MODELS.REMOTE_ENDPOINT_NOT_IMPLEMENTED",
913
- context: `model=${model.info.reference}`,
914
- });
915
- }
916
- const authorize = remoteEmbeddingAuthorizationGuard({
917
- store: new RemoteEmbeddingAuthorizationStore({
918
- signingKeyPath: serviceOptions.authorizationSigningKeyPath,
919
- }),
920
- });
921
- const authorizedModel = {
922
- info: model.info,
923
- async embed(contents, embedOptions) {
924
- await authorize({
925
- provider: model.info.provider,
926
- model: model.info.name,
927
- endpoint,
928
- purpose: embedOptions?.purpose ?? EmbeddingPurpose.Document,
929
- contentKinds: contents.map((content) => content.kind),
930
- contentCount: contents.length,
931
- });
932
- return await model.embed(contents, embedOptions);
933
- },
934
- dispose: () => model.dispose(),
935
- };
936
- return authorizedModel;
937
- }
938
- function parseEmbeddingModelReference(reference) {
939
- const separator = reference.indexOf("/");
940
- if (separator <= 0 || separator === reference.length - 1) {
941
- throw new EngineError("Embedding model reference is invalid", {
942
- code: "ZVEC_GREP.ENGINE.MODELS.EMBEDDING_INVALID_REFERENCE",
943
- context: `reference=${reference}`,
944
- });
945
- }
946
- return {
947
- provider: reference.slice(0, separator),
948
- name: reference.slice(separator + 1),
949
- };
950
- }
951
- function embeddingModelReference(identity) {
952
- return `${identity.provider}/${identity.name}`;
953
- }
954
- function providerOptionsFingerprint(options) {
955
- const publicOptionsFingerprint = createHash("sha256")
956
- .update(JSON.stringify([options.endpoint, options.modelCacheDir, options.device]))
957
- .digest("hex");
958
- return `${providerApiKeyIdentity(options.apiKey)}:${publicOptionsFingerprint}`;
959
- }
960
- function providerApiKeyIdentity(apiKey) {
961
- return createHmac("sha256", PROVIDER_API_KEY_IDENTITY_SECRET)
962
- .update(apiKey)
963
- .digest("hex");
964
- }
965
- function effectiveEmbeddingRuntime(options, model, workspaceRuntime) {
966
- const resolved = resolveEmbeddingRuntimeOptions(model.info.reference, options, workspaceRuntime, readGlobalConfig());
967
- return {
968
- apiKey: resolved.apiKey,
969
- ...((model.info.endpoint ?? resolved.endpoint)
970
- ? { endpoint: model.info.endpoint ?? resolved.endpoint }
971
- : {}),
972
- ...(model.info.provider === "local"
973
- ? { device: resolved.device ?? "auto" }
974
- : {}),
975
- };
976
- }
977
- function runtimeForModelProvider(existing, model, workspaceRuntime) {
978
- return isWorkspaceIndexed(existing) &&
979
- existing.embedding.provider !== model.info.provider
980
- ? {}
981
- : workspaceRuntime;
982
- }
983
- function embeddingRuntimeAfterIndex(existing, existingRuntime, model, effectiveRuntime, explicit) {
984
- const sameProvider = isWorkspaceIndexed(existing) &&
985
- existing.embedding.provider === model.info.provider;
986
- const apiKey = model.info.provider === "local"
987
- ? undefined
988
- : (explicit.apiKey ??
989
- (sameProvider ? existingRuntime.apiKey : undefined));
990
- return {
991
- ...(apiKey !== undefined ? { apiKey } : {}),
992
- ...(model.info.provider !== "local" &&
993
- effectiveRuntime.endpoint !== undefined
994
- ? { endpoint: effectiveRuntime.endpoint }
995
- : {}),
996
- ...(model.info.provider === "local"
997
- ? { device: effectiveRuntime.device ?? "auto" }
998
- : {}),
999
- };
1000
- }
1001
- function assertWorkspaceEndpointMatchesCurrentRuntime(info, workspaceRuntime, effectiveRuntime, rebuildCommand) {
1002
- if (!isWorkspaceIndexed(info) ||
1003
- workspaceRuntime.endpoint === effectiveRuntime.endpoint) {
1004
- return;
1005
- }
1006
- throw new EngineError("Existing zvec-grep index uses a different embedding endpoint", {
1007
- code: "ZVEC_GREP.ENGINE.SERVICE.EMBEDDING_ENDPOINT_CHANGE_REQUIRES_REBUILD",
1008
- context: errorDetails([
1009
- workspaceIndexDetail(info.name),
1010
- detail("hint", `Run "${rebuildCommand}" to rebuild this index with the requested endpoint.`),
1011
- ]),
1012
- });
1013
- }
1014
- function assertSearchEndpointMatchesWorkspace(info, workspaceRuntime, effectiveRuntime) {
1015
- if (workspaceRuntime.endpoint === effectiveRuntime.endpoint) {
1016
- return;
1017
- }
1018
- throw new EngineError("Search cannot override the embedding endpoint of an existing index", {
1019
- code: "ZVEC_GREP.ENGINE.SERVICE.SEARCH_ENDPOINT_CHANGE_REQUIRES_REBUILD",
1020
- context: errorDetails([
1021
- workspaceIndexDetail(info.name),
1022
- detail("hint", 'Run "zg index --endpoint <url> --rebuild" before searching with this endpoint.'),
1023
- ]),
1024
- });
1025
- }
1026
- function assertWorkspaceEmbeddingMatchesCurrentModel(info, model, rebuildCommand) {
1027
- if (!isWorkspaceIndexed(info)) {
1028
- return;
1029
- }
1030
- const expected = info.embedding;
1031
- const changed = expected.provider !== model.info.provider ||
1032
- expected.model !== model.info.name ||
1033
- expected.dimension !== model.info.dimension ||
1034
- expected.metric !== model.info.metric;
1035
- if (!changed) {
1036
- return;
1037
- }
1038
- throw new EngineError("Existing zvec-grep index uses a different embedding model", {
1039
- code: "ZVEC_GREP.ENGINE.SERVICE.EMBEDDING_SCHEMA_CHANGE_REQUIRES_REBUILD",
1040
- context: errorDetails([
1041
- workspaceIndexDetail(info.name),
1042
- detail("existing", `${expected.provider}/${expected.model}`),
1043
- detail("requested", model.info.reference),
1044
- detail("hint", `Run "${rebuildCommand}" to rebuild this index with the requested embedding model.`),
1045
- ]),
1046
- });
1047
- }
1048
- function contextGroupLimit(limit, groupCount) {
1049
- if (limit !== undefined) {
1050
- return limit;
1051
- }
1052
- const safeGroupCount = Math.max(1, groupCount);
1053
- if (safeGroupCount <= 3) {
1054
- return DEFAULT_CONTEXT_LIMIT;
1055
- }
1056
- return Math.max(1, Math.ceil(DEFAULT_CONTEXT_TOTAL_LIMIT / safeGroupCount));
1057
- }
1058
- function normalizeContextRequest(options) {
1059
- const primaryQueries = normalizePrimaryQueries(options.query, options.queries);
1060
- const extraRoutes = normalizeContextRoutes(options.routes ?? []);
1061
- const hasPatternFiles = options.rg === true && (options.rgOptions?.patternFiles?.length ?? 0) > 0;
1062
- if (primaryQueries.length === 0 &&
1063
- extraRoutes.length === 0 &&
1064
- !hasPatternFiles) {
1065
- throw new EngineError("zvec-grep context requires a non-empty query or route", {
1066
- code: "ZVEC_GREP.ENGINE.SERVICE.EMPTY_QUERY",
1067
- });
1068
- }
1069
- const groups = contextGroups(primaryQueries, extraRoutes);
1070
- const routes = groups.flatMap((group) => group.routes);
1071
- const rgPatterns = [
1072
- ...primaryQueries,
1073
- ...extraRoutes.map((route) => route.query),
1074
- ];
1075
- const displayQuery = primaryQueries.length > 0
1076
- ? primaryQueries.join(" | ")
1077
- : extraRoutes.length > 0
1078
- ? extraRoutes.map((route) => route.query).join(" | ")
1079
- : options
1080
- .rgOptions.patternFiles.map((path) => `@${path}`)
1081
- .join(" | ");
1082
- return {
1083
- displayQuery,
1084
- rgPatterns,
1085
- routes,
1086
- groups,
1087
- };
1088
- }
1089
- function normalizePrimaryQueries(query, queries) {
1090
- return [query, ...(queries ?? [])].flatMap((value) => {
1091
- const normalized = normalizeOptionalQuery(value);
1092
- return normalized ? [normalized] : [];
1093
- });
1094
- }
1095
- function normalizeOptionalQuery(query) {
1096
- const normalized = query?.trim() ?? "";
1097
- return normalized.length > 0 ? normalized : undefined;
1098
- }
1099
- function normalizeContextRoutes(routes) {
1100
- return routes.map((route, index) => {
1101
- if (route.mode !== "fts" && route.mode !== "vector") {
1102
- throw new EngineError("zvec-grep context route has an unsupported mode", {
1103
- code: "ZVEC_GREP.ENGINE.SERVICE.INVALID_ROUTE_MODE",
1104
- context: `routeIndex=${index} mode=${String(route.mode)}`,
1105
- });
1106
- }
1107
- const query = route.query.trim();
1108
- if (query.length === 0) {
1109
- throw new EngineError("zvec-grep context route requires a non-empty query", {
1110
- code: "ZVEC_GREP.ENGINE.SERVICE.EMPTY_ROUTE_QUERY",
1111
- context: `routeIndex=${index} mode=${route.mode}`,
1112
- });
1113
- }
1114
- return {
1115
- mode: route.mode,
1116
- query,
1117
- };
1118
- });
1119
- }
1120
- function contextGroups(primaryQueries, extraRoutes) {
1121
- return [
1122
- ...primaryQueries.map((query, index) => ({
1123
- id: `Q${index + 1}`,
1124
- query,
1125
- role: "primary",
1126
- routes: [
1127
- { mode: "fts", query },
1128
- { mode: "vector", query },
1129
- ],
1130
- })),
1131
- ...extraRoutes.map((route, index) => ({
1132
- id: `Q${primaryQueries.length + index + 1}`,
1133
- query: route.query,
1134
- role: "supplemental",
1135
- routes: [route],
1136
- })),
1137
- ];
1138
- }
1139
- function searchPlanToContextItems(result, root, group) {
1140
- return result.hits.map((hit) => {
1141
- const target = contextItemTarget(hit);
1142
- return {
1143
- kind: "indexed_entity",
1144
- rank: hit.rank,
1145
- file: {
1146
- absolutePath: hit.file.absolutePath,
1147
- relativePath: hit.file.relativePath || relative(root, hit.file.absolutePath) || ".",
1148
- rootPath: hit.file.rootPath,
1149
- },
1150
- range: hit.entity.range,
1151
- excerptRange: target.excerptRange,
1152
- content: contentToText(target.content),
1153
- contentRole: target.contentRole,
1154
- outline: target.outline,
1155
- status: fileFreshnessStatus(hit.file),
1156
- score: hit.score,
1157
- matchedBy: hit.matchedBy,
1158
- metadata: hit.entity.metadata,
1159
- entityId: hit.entity.id,
1160
- trace: hit.trace,
1161
- queryGroups: [
1162
- {
1163
- id: group.id,
1164
- query: group.query,
1165
- role: group.role,
1166
- rank: hit.rank,
1167
- matchedBy: hit.matchedBy,
1168
- },
1169
- ],
1170
- };
1171
- });
1172
- }
1173
- const DEFAULT_CONTEXT_PRIORITY_LIMIT = 6;
1174
- const CONTEXT_GROUP_RRF_K = 60;
1175
- function selectAndRankContextItems(items, coverageGroupIds) {
1176
- const deduped = new Map();
1177
- for (const item of items) {
1178
- const key = contextItemDedupeKey(item);
1179
- const existing = deduped.get(key);
1180
- if (existing) {
1181
- existing.queryGroups = mergeContextQueryGroupMatches(existing.queryGroups ?? [], item.queryGroups ?? []);
1182
- existing.matchedBy = mergedContextMatchedBy(existing.queryGroups);
1183
- continue;
1184
- }
1185
- deduped.set(key, { ...item });
1186
- }
1187
- const candidates = [...deduped.values()];
1188
- const globallyRanked = [...candidates].sort(compareContextGlobalRank);
1189
- const selected = new Set();
1190
- const prioritized = [];
1191
- for (const groupId of coverageGroupIds) {
1192
- if (prioritized.length >= DEFAULT_CONTEXT_PRIORITY_LIMIT) {
1193
- break;
1194
- }
1195
- const candidate = candidates
1196
- .filter((item) => !selected.has(contextItemDedupeKey(item)))
1197
- .filter((item) => contextQueryGroupMatch(item, groupId) !== undefined)
1198
- .sort((left, right) => compareContextGroupRank(left, right, groupId))[0];
1199
- if (!candidate) {
1200
- continue;
1201
- }
1202
- selected.add(contextItemDedupeKey(candidate));
1203
- prioritized.push({
1204
- ...candidate,
1205
- selectionReason: "coverage",
1206
- coverageGroup: groupId,
1207
- });
1208
- }
1209
- for (const candidate of globallyRanked) {
1210
- if (prioritized.length >= DEFAULT_CONTEXT_PRIORITY_LIMIT) {
1211
- break;
1212
- }
1213
- const key = contextItemDedupeKey(candidate);
1214
- if (selected.has(key)) {
1215
- continue;
1216
- }
1217
- selected.add(key);
1218
- prioritized.push({
1219
- ...candidate,
1220
- selectionReason: "global_fill",
1221
- coverageGroup: undefined,
1222
- });
1223
- }
1224
- const unprioritized = globallyRanked.filter((item) => !selected.has(contextItemDedupeKey(item)));
1225
- return [...prioritized, ...unprioritized].map((item, index) => ({
1226
- ...item,
1227
- rank: index + 1,
1228
- }));
1229
- }
1230
- function dedupeAndRerankContextItems(items) {
1231
- const seen = new Set();
1232
- const deduped = [];
1233
- for (const item of items) {
1234
- const key = contextItemDedupeKey(item);
1235
- if (seen.has(key)) {
1236
- continue;
1237
- }
1238
- seen.add(key);
1239
- deduped.push({ ...item, rank: deduped.length + 1 });
1240
- }
1241
- return deduped;
1242
- }
1243
- function mergeContextQueryGroupMatches(left, right) {
1244
- const matches = new Map(left.map((match) => [match.id, match]));
1245
- for (const match of right) {
1246
- const existing = matches.get(match.id);
1247
- if (!existing || match.rank < existing.rank) {
1248
- matches.set(match.id, match);
1249
- }
1250
- }
1251
- return [...matches.values()].sort((a, b) => contextGroupNumber(a.id) - contextGroupNumber(b.id));
1252
- }
1253
- function mergedContextMatchedBy(matches) {
1254
- const hasFts = matches.some((match) => match.matchedBy === "fts" || match.matchedBy === "fts+vector");
1255
- const hasVector = matches.some((match) => match.matchedBy === "vector" || match.matchedBy === "fts+vector");
1256
- return hasFts && hasVector ? "fts+vector" : hasFts ? "fts" : "vector";
1257
- }
1258
- function compareContextGlobalRank(left, right) {
1259
- return (contextGlobalRrfScore(right) - contextGlobalRrfScore(left) ||
1260
- contextBestGroupRank(left) - contextBestGroupRank(right) ||
1261
- contextFirstGroupNumber(left) - contextFirstGroupNumber(right) ||
1262
- contextItemDedupeKey(left).localeCompare(contextItemDedupeKey(right)));
1263
- }
1264
- function contextGlobalRrfScore(item) {
1265
- return (item.queryGroups ?? []).reduce((score, match) => score + 1 / (CONTEXT_GROUP_RRF_K + match.rank), 0);
1266
- }
1267
- function compareContextGroupRank(left, right, groupId) {
1268
- return (contextQueryGroupMatch(left, groupId).rank -
1269
- contextQueryGroupMatch(right, groupId).rank ||
1270
- compareContextGlobalRank(left, right));
1271
- }
1272
- function contextQueryGroupMatch(item, groupId) {
1273
- return item.queryGroups?.find((match) => match.id === groupId);
1274
- }
1275
- function contextBestGroupRank(item) {
1276
- return Math.min(...(item.queryGroups ?? []).map((match) => match.rank));
1277
- }
1278
- function contextFirstGroupNumber(item) {
1279
- return Math.min(...(item.queryGroups ?? []).map((match) => contextGroupNumber(match.id)));
1280
- }
1281
- function contextGroupNumber(id) {
1282
- const value = Number.parseInt(id.slice(1), 10);
1283
- return Number.isFinite(value) ? value : Number.MAX_SAFE_INTEGER;
1284
- }
1285
- function contextItemDedupeKey(item) {
1286
- if (item.entityId) {
1287
- return `entity:${item.entityId}`;
1288
- }
1289
- return ["range", item.file.absolutePath, JSON.stringify(item.range)].join(":");
1290
- }
1291
- function contextItemTarget(hit) {
1292
- const evidence = hit.evidence.find((item) => !item.isEntity);
1293
- const hasSeparateEvidence = evidence && !sameDisplayedContent(hit.entity, evidence);
1294
- const content = hasSeparateEvidence ? evidence.content : hit.entity.content;
1295
- const contentRole = hasSeparateEvidence || entityContentLooksLikeSource(hit.entity)
1296
- ? "source"
1297
- : "outline";
1298
- const excerptRange = hasSeparateEvidence ? evidence.range : undefined;
1299
- const outline = contentRole === "source" ? contextItemOutline(hit, evidence) : undefined;
1300
- return {
1301
- content,
1302
- contentRole,
1303
- excerptRange,
1304
- outline,
1305
- };
1306
- }
1307
- function entityContentLooksLikeSource(entity) {
1308
- if (entity.content.kind !== "text" || entity.range.kind !== "text") {
1309
- return true;
1310
- }
1311
- const expectedLines = entity.range.endLine - entity.range.startLine + 1;
1312
- const actualLines = entity.content.text.split(/\r?\n/).length;
1313
- return actualLines >= expectedLines;
1314
- }
1315
- function contextItemOutline(hit, evidence) {
1316
- if (hit.entity.content.kind !== "text") {
1317
- return undefined;
1318
- }
1319
- const outline = hit.entity.content.text.trim();
1320
- if (outline.length === 0) {
1321
- return undefined;
1322
- }
1323
- if (!evidence) {
1324
- return undefined;
1325
- }
1326
- if (evidence && sameDisplayedContent(hit.entity, evidence)) {
1327
- return undefined;
1328
- }
1329
- if (!isUsefulOutline(hit.entity.metadata, outline)) {
1330
- return undefined;
1331
- }
1332
- if (evidence?.content.kind === "text" &&
1333
- evidence.content.text.trim() === outline) {
1334
- return undefined;
1335
- }
1336
- return outline;
1337
- }
1338
- function isUsefulOutline(metadata, outline) {
1339
- if (!metadata || metadata.kind !== "code") {
1340
- return false;
1341
- }
1342
- return (metadata.symbolType === "class" ||
1343
- metadata.symbolType === "interface" ||
1344
- metadata.symbolType === "module" ||
1345
- outline.includes("\ncalls:"));
1346
- }
1347
- function sameDisplayedContent(entity, evidence) {
1348
- return (JSON.stringify(entity.range) === JSON.stringify(evidence.range) &&
1349
- contentEquals(entity.content, evidence.content));
1350
- }
1351
- function contentEquals(left, right) {
1352
- if (left.kind !== right.kind) {
1353
- return false;
1354
- }
1355
- if (left.kind === "image") {
1356
- return (right.kind === "image" &&
1357
- left.format === right.format &&
1358
- left.data.byteLength === right.data.byteLength);
1359
- }
1360
- return right.kind === "text" && left.text === right.text;
1361
- }
1362
- function fileFreshnessStatus(file) {
1363
- if (!file.indexStatus?.indexedTime) {
1364
- return "possibly_stale";
1365
- }
1366
- try {
1367
- const info = statSync(file.absolutePath, { throwIfNoEntry: false });
1368
- if (!info || !info.isFile()) {
1369
- return "possibly_stale";
1370
- }
1371
- if (file.indexStatus.indexedTime >= info.mtimeMs) {
1372
- return "fresh";
1373
- }
1374
- if (file.contentHash &&
1375
- sha256File(file.absolutePath) === file.contentHash) {
1376
- return "fresh";
1377
- }
1378
- }
1379
- catch {
1380
- return "possibly_stale";
1381
- }
1382
- return "possibly_stale";
1383
- }
1384
- function sha256File(path) {
1385
- return createHash("sha256").update(readFileSync(path)).digest("hex");
1386
- }
1387
- function contentToText(content) {
1388
- if (content.kind === "text") {
1389
- return content.text;
1390
- }
1391
- return `[image:${content.format} bytes=${content.data.byteLength}]`;
1392
- }
1393
- //# sourceMappingURL=zvec-grep.js.map