@oscharko-dev/keiko-local-knowledge 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 (290) hide show
  1. package/dist/.tsbuildinfo +1 -0
  2. package/dist/bounded-document-extraction.d.ts +27 -0
  3. package/dist/bounded-document-extraction.d.ts.map +1 -0
  4. package/dist/bounded-document-extraction.js +214 -0
  5. package/dist/capsule-lifecycle.d.ts +33 -0
  6. package/dist/capsule-lifecycle.d.ts.map +1 -0
  7. package/dist/capsule-lifecycle.js +292 -0
  8. package/dist/capsule-set-lifecycle.d.ts +15 -0
  9. package/dist/capsule-set-lifecycle.d.ts.map +1 -0
  10. package/dist/capsule-set-lifecycle.js +158 -0
  11. package/dist/chunking/chunker-persist.d.ts +36 -0
  12. package/dist/chunking/chunker-persist.d.ts.map +1 -0
  13. package/dist/chunking/chunker-persist.js +74 -0
  14. package/dist/chunking/chunker-runner.d.ts +9 -0
  15. package/dist/chunking/chunker-runner.d.ts.map +1 -0
  16. package/dist/chunking/chunker-runner.js +218 -0
  17. package/dist/chunking/chunker.d.ts +7 -0
  18. package/dist/chunking/chunker.d.ts.map +1 -0
  19. package/dist/chunking/chunker.js +139 -0
  20. package/dist/chunking/citation-mapper.d.ts +4 -0
  21. package/dist/chunking/citation-mapper.d.ts.map +1 -0
  22. package/dist/chunking/citation-mapper.js +180 -0
  23. package/dist/chunking/index.d.ts +6 -0
  24. package/dist/chunking/index.d.ts.map +1 -0
  25. package/dist/chunking/index.js +8 -0
  26. package/dist/chunking/token-estimator.d.ts +3 -0
  27. package/dist/chunking/token-estimator.d.ts.map +1 -0
  28. package/dist/chunking/token-estimator.js +26 -0
  29. package/dist/chunking/types.d.ts +49 -0
  30. package/dist/chunking/types.d.ts.map +1 -0
  31. package/dist/chunking/types.js +26 -0
  32. package/dist/composition.d.ts +57 -0
  33. package/dist/composition.d.ts.map +1 -0
  34. package/dist/composition.js +310 -0
  35. package/dist/conversation/citation-attacher.d.ts +8 -0
  36. package/dist/conversation/citation-attacher.d.ts.map +1 -0
  37. package/dist/conversation/citation-attacher.js +55 -0
  38. package/dist/conversation/citation-excerpts.d.ts +4 -0
  39. package/dist/conversation/citation-excerpts.d.ts.map +1 -0
  40. package/dist/conversation/citation-excerpts.js +41 -0
  41. package/dist/conversation/grounded-answer-runner.d.ts +9 -0
  42. package/dist/conversation/grounded-answer-runner.d.ts.map +1 -0
  43. package/dist/conversation/grounded-answer-runner.js +61 -0
  44. package/dist/conversation/index.d.ts +5 -0
  45. package/dist/conversation/index.d.ts.map +1 -0
  46. package/dist/conversation/index.js +7 -0
  47. package/dist/conversation/model-gateway-answer-generator.d.ts +28 -0
  48. package/dist/conversation/model-gateway-answer-generator.d.ts.map +1 -0
  49. package/dist/conversation/model-gateway-answer-generator.js +105 -0
  50. package/dist/conversation/types.d.ts +35 -0
  51. package/dist/conversation/types.d.ts.map +1 -0
  52. package/dist/conversation/types.js +24 -0
  53. package/dist/discovery/discovery-runner.d.ts +23 -0
  54. package/dist/discovery/discovery-runner.d.ts.map +1 -0
  55. package/dist/discovery/discovery-runner.js +109 -0
  56. package/dist/discovery/extract-progressive.d.ts +17 -0
  57. package/dist/discovery/extract-progressive.d.ts.map +1 -0
  58. package/dist/discovery/extract-progressive.js +522 -0
  59. package/dist/discovery/extract.d.ts +26 -0
  60. package/dist/discovery/extract.d.ts.map +1 -0
  61. package/dist/discovery/extract.js +906 -0
  62. package/dist/discovery/glob.d.ts +10 -0
  63. package/dist/discovery/glob.d.ts.map +1 -0
  64. package/dist/discovery/glob.js +72 -0
  65. package/dist/discovery/index.d.ts +6 -0
  66. package/dist/discovery/index.d.ts.map +1 -0
  67. package/dist/discovery/index.js +8 -0
  68. package/dist/discovery/media-type.d.ts +4 -0
  69. package/dist/discovery/media-type.d.ts.map +1 -0
  70. package/dist/discovery/media-type.js +62 -0
  71. package/dist/discovery/persist.d.ts +63 -0
  72. package/dist/discovery/persist.d.ts.map +1 -0
  73. package/dist/discovery/persist.js +345 -0
  74. package/dist/discovery/test-support.d.ts +16 -0
  75. package/dist/discovery/test-support.d.ts.map +1 -0
  76. package/dist/discovery/test-support.js +127 -0
  77. package/dist/discovery/types.d.ts +63 -0
  78. package/dist/discovery/types.d.ts.map +1 -0
  79. package/dist/discovery/types.js +28 -0
  80. package/dist/discovery/walk.d.ts +12 -0
  81. package/dist/discovery/walk.d.ts.map +1 -0
  82. package/dist/discovery/walk.js +302 -0
  83. package/dist/errors.d.ts +13 -0
  84. package/dist/errors.d.ts.map +1 -0
  85. package/dist/errors.js +22 -0
  86. package/dist/evaluations/dimensions.d.ts +14 -0
  87. package/dist/evaluations/dimensions.d.ts.map +1 -0
  88. package/dist/evaluations/dimensions.js +191 -0
  89. package/dist/evaluations/fixtures.d.ts +18 -0
  90. package/dist/evaluations/fixtures.d.ts.map +1 -0
  91. package/dist/evaluations/fixtures.js +858 -0
  92. package/dist/evaluations/index.d.ts +7 -0
  93. package/dist/evaluations/index.d.ts.map +1 -0
  94. package/dist/evaluations/index.js +10 -0
  95. package/dist/evaluations/report.d.ts +3 -0
  96. package/dist/evaluations/report.d.ts.map +1 -0
  97. package/dist/evaluations/report.js +31 -0
  98. package/dist/evaluations/runner-seed.d.ts +12 -0
  99. package/dist/evaluations/runner-seed.d.ts.map +1 -0
  100. package/dist/evaluations/runner-seed.js +175 -0
  101. package/dist/evaluations/runner.d.ts +8 -0
  102. package/dist/evaluations/runner.d.ts.map +1 -0
  103. package/dist/evaluations/runner.js +205 -0
  104. package/dist/evaluations/scripted-embedding-adapter.d.ts +13 -0
  105. package/dist/evaluations/scripted-embedding-adapter.d.ts.map +1 -0
  106. package/dist/evaluations/scripted-embedding-adapter.js +163 -0
  107. package/dist/evaluations/types.d.ts +116 -0
  108. package/dist/evaluations/types.d.ts.map +1 -0
  109. package/dist/evaluations/types.js +27 -0
  110. package/dist/index.d.ts +23 -0
  111. package/dist/index.d.ts.map +1 -0
  112. package/dist/index.js +41 -0
  113. package/dist/indexing/bounded-indexing.d.ts +41 -0
  114. package/dist/indexing/bounded-indexing.d.ts.map +1 -0
  115. package/dist/indexing/bounded-indexing.js +240 -0
  116. package/dist/indexing/checkpoint-persist.d.ts +8 -0
  117. package/dist/indexing/checkpoint-persist.d.ts.map +1 -0
  118. package/dist/indexing/checkpoint-persist.js +135 -0
  119. package/dist/indexing/checkpoint-resume.d.ts +20 -0
  120. package/dist/indexing/checkpoint-resume.d.ts.map +1 -0
  121. package/dist/indexing/checkpoint-resume.js +50 -0
  122. package/dist/indexing/embedding-batcher.d.ts +3 -0
  123. package/dist/indexing/embedding-batcher.d.ts.map +1 -0
  124. package/dist/indexing/embedding-batcher.js +390 -0
  125. package/dist/indexing/index.d.ts +7 -0
  126. package/dist/indexing/index.d.ts.map +1 -0
  127. package/dist/indexing/index.js +11 -0
  128. package/dist/indexing/job-persist.d.ts +46 -0
  129. package/dist/indexing/job-persist.d.ts.map +1 -0
  130. package/dist/indexing/job-persist.js +157 -0
  131. package/dist/indexing/job-resume.d.ts +4 -0
  132. package/dist/indexing/job-resume.d.ts.map +1 -0
  133. package/dist/indexing/job-resume.js +14 -0
  134. package/dist/indexing/orchestrator.d.ts +3 -0
  135. package/dist/indexing/orchestrator.d.ts.map +1 -0
  136. package/dist/indexing/orchestrator.js +1151 -0
  137. package/dist/indexing/types.d.ts +156 -0
  138. package/dist/indexing/types.d.ts.map +1 -0
  139. package/dist/indexing/types.js +30 -0
  140. package/dist/indexing/vector-persist.d.ts +32 -0
  141. package/dist/indexing/vector-persist.d.ts.map +1 -0
  142. package/dist/indexing/vector-persist.js +105 -0
  143. package/dist/parsers/_internal.d.ts +20 -0
  144. package/dist/parsers/_internal.d.ts.map +1 -0
  145. package/dist/parsers/_internal.js +122 -0
  146. package/dist/parsers/csv-parser.d.ts +3 -0
  147. package/dist/parsers/csv-parser.d.ts.map +1 -0
  148. package/dist/parsers/csv-parser.js +202 -0
  149. package/dist/parsers/docx-parser.d.ts +3 -0
  150. package/dist/parsers/docx-parser.d.ts.map +1 -0
  151. package/dist/parsers/docx-parser.js +390 -0
  152. package/dist/parsers/html-parser.d.ts +3 -0
  153. package/dist/parsers/html-parser.d.ts.map +1 -0
  154. package/dist/parsers/html-parser.js +310 -0
  155. package/dist/parsers/index.d.ts +15 -0
  156. package/dist/parsers/index.d.ts.map +1 -0
  157. package/dist/parsers/index.js +41 -0
  158. package/dist/parsers/json-parser.d.ts +3 -0
  159. package/dist/parsers/json-parser.d.ts.map +1 -0
  160. package/dist/parsers/json-parser.js +192 -0
  161. package/dist/parsers/large-document/capability-discovery.d.ts +27 -0
  162. package/dist/parsers/large-document/capability-discovery.d.ts.map +1 -0
  163. package/dist/parsers/large-document/capability-discovery.js +76 -0
  164. package/dist/parsers/large-document/diagnostics.d.ts +3 -0
  165. package/dist/parsers/large-document/diagnostics.d.ts.map +1 -0
  166. package/dist/parsers/large-document/diagnostics.js +11 -0
  167. package/dist/parsers/large-document/index.d.ts +15 -0
  168. package/dist/parsers/large-document/index.d.ts.map +1 -0
  169. package/dist/parsers/large-document/index.js +10 -0
  170. package/dist/parsers/large-document/legacy-format.d.ts +5 -0
  171. package/dist/parsers/large-document/legacy-format.d.ts.map +1 -0
  172. package/dist/parsers/large-document/legacy-format.js +25 -0
  173. package/dist/parsers/large-document/preflight.d.ts +9 -0
  174. package/dist/parsers/large-document/preflight.d.ts.map +1 -0
  175. package/dist/parsers/large-document/preflight.js +43 -0
  176. package/dist/parsers/large-document/progressive-extraction.d.ts +55 -0
  177. package/dist/parsers/large-document/progressive-extraction.d.ts.map +1 -0
  178. package/dist/parsers/large-document/progressive-extraction.js +123 -0
  179. package/dist/parsers/large-document/progressive-pdf.d.ts +20 -0
  180. package/dist/parsers/large-document/progressive-pdf.d.ts.map +1 -0
  181. package/dist/parsers/large-document/progressive-pdf.js +145 -0
  182. package/dist/parsers/large-document/synthetic-source.d.ts +9 -0
  183. package/dist/parsers/large-document/synthetic-source.d.ts.map +1 -0
  184. package/dist/parsers/large-document/synthetic-source.js +101 -0
  185. package/dist/parsers/large-document/window-builder.d.ts +24 -0
  186. package/dist/parsers/large-document/window-builder.d.ts.map +1 -0
  187. package/dist/parsers/large-document/window-builder.js +75 -0
  188. package/dist/parsers/ocr/index.d.ts +4 -0
  189. package/dist/parsers/ocr/index.d.ts.map +1 -0
  190. package/dist/parsers/ocr/index.js +4 -0
  191. package/dist/parsers/ocr/null-ocr-adapter.d.ts +3 -0
  192. package/dist/parsers/ocr/null-ocr-adapter.d.ts.map +1 -0
  193. package/dist/parsers/ocr/null-ocr-adapter.js +14 -0
  194. package/dist/parsers/ocr/ocr-pipeline-parser.d.ts +8 -0
  195. package/dist/parsers/ocr/ocr-pipeline-parser.d.ts.map +1 -0
  196. package/dist/parsers/ocr/ocr-pipeline-parser.js +147 -0
  197. package/dist/parsers/ocr/types.d.ts +16 -0
  198. package/dist/parsers/ocr/types.d.ts.map +1 -0
  199. package/dist/parsers/ocr/types.js +4 -0
  200. package/dist/parsers/parser-test-fixtures.d.ts +28 -0
  201. package/dist/parsers/parser-test-fixtures.d.ts.map +1 -0
  202. package/dist/parsers/parser-test-fixtures.js +139 -0
  203. package/dist/parsers/pdf-parser.d.ts +43 -0
  204. package/dist/parsers/pdf-parser.d.ts.map +1 -0
  205. package/dist/parsers/pdf-parser.js +388 -0
  206. package/dist/parsers/registry.d.ts +8 -0
  207. package/dist/parsers/registry.d.ts.map +1 -0
  208. package/dist/parsers/registry.js +57 -0
  209. package/dist/parsers/text-parser.d.ts +3 -0
  210. package/dist/parsers/text-parser.d.ts.map +1 -0
  211. package/dist/parsers/text-parser.js +214 -0
  212. package/dist/parsers/types.d.ts +53 -0
  213. package/dist/parsers/types.d.ts.map +1 -0
  214. package/dist/parsers/types.js +21 -0
  215. package/dist/parsers/unsupported-parser.d.ts +4 -0
  216. package/dist/parsers/unsupported-parser.d.ts.map +1 -0
  217. package/dist/parsers/unsupported-parser.js +97 -0
  218. package/dist/parsers/xlsx-parser.d.ts +3 -0
  219. package/dist/parsers/xlsx-parser.d.ts.map +1 -0
  220. package/dist/parsers/xlsx-parser.js +425 -0
  221. package/dist/privacy/audit-emitter.d.ts +5 -0
  222. package/dist/privacy/audit-emitter.d.ts.map +1 -0
  223. package/dist/privacy/audit-emitter.js +93 -0
  224. package/dist/privacy/diagnostic-redactor.d.ts +2 -0
  225. package/dist/privacy/diagnostic-redactor.d.ts.map +1 -0
  226. package/dist/privacy/diagnostic-redactor.js +153 -0
  227. package/dist/privacy/index.d.ts +5 -0
  228. package/dist/privacy/index.d.ts.map +1 -0
  229. package/dist/privacy/index.js +6 -0
  230. package/dist/privacy/retention-applier.d.ts +5 -0
  231. package/dist/privacy/retention-applier.d.ts.map +1 -0
  232. package/dist/privacy/retention-applier.js +88 -0
  233. package/dist/privacy/types.d.ts +98 -0
  234. package/dist/privacy/types.d.ts.map +1 -0
  235. package/dist/privacy/types.js +12 -0
  236. package/dist/qualityIntelligence/capsuleCorpus.d.ts +27 -0
  237. package/dist/qualityIntelligence/capsuleCorpus.d.ts.map +1 -0
  238. package/dist/qualityIntelligence/capsuleCorpus.js +58 -0
  239. package/dist/qualityIntelligence/index.d.ts +3 -0
  240. package/dist/qualityIntelligence/index.d.ts.map +1 -0
  241. package/dist/qualityIntelligence/index.js +5 -0
  242. package/dist/qualityIntelligence/qiHandoff.d.ts +36 -0
  243. package/dist/qualityIntelligence/qiHandoff.d.ts.map +1 -0
  244. package/dist/qualityIntelligence/qiHandoff.js +82 -0
  245. package/dist/retrieval/answer-grounding.d.ts +9 -0
  246. package/dist/retrieval/answer-grounding.d.ts.map +1 -0
  247. package/dist/retrieval/answer-grounding.js +31 -0
  248. package/dist/retrieval/context-pack-assembler.d.ts +24 -0
  249. package/dist/retrieval/context-pack-assembler.d.ts.map +1 -0
  250. package/dist/retrieval/context-pack-assembler.js +50 -0
  251. package/dist/retrieval/index.d.ts +6 -0
  252. package/dist/retrieval/index.d.ts.map +1 -0
  253. package/dist/retrieval/index.js +9 -0
  254. package/dist/retrieval/retrieval-runner.d.ts +10 -0
  255. package/dist/retrieval/retrieval-runner.d.ts.map +1 -0
  256. package/dist/retrieval/retrieval-runner.js +163 -0
  257. package/dist/retrieval/scoped-vector-search.d.ts +24 -0
  258. package/dist/retrieval/scoped-vector-search.d.ts.map +1 -0
  259. package/dist/retrieval/scoped-vector-search.js +864 -0
  260. package/dist/retrieval/types.d.ts +28 -0
  261. package/dist/retrieval/types.d.ts.map +1 -0
  262. package/dist/retrieval/types.js +33 -0
  263. package/dist/section-path-hash.d.ts +3 -0
  264. package/dist/section-path-hash.d.ts.map +1 -0
  265. package/dist/section-path-hash.js +9 -0
  266. package/dist/source-lifecycle.d.ts +14 -0
  267. package/dist/source-lifecycle.d.ts.map +1 -0
  268. package/dist/source-lifecycle.js +155 -0
  269. package/dist/source-routing-validation.d.ts +11 -0
  270. package/dist/source-routing-validation.d.ts.map +1 -0
  271. package/dist/source-routing-validation.js +140 -0
  272. package/dist/store-content-cipher.d.ts +11 -0
  273. package/dist/store-content-cipher.d.ts.map +1 -0
  274. package/dist/store-content-cipher.js +67 -0
  275. package/dist/store-content-encryption.d.ts +12 -0
  276. package/dist/store-content-encryption.d.ts.map +1 -0
  277. package/dist/store-content-encryption.js +275 -0
  278. package/dist/store-paths.d.ts +6 -0
  279. package/dist/store-paths.d.ts.map +1 -0
  280. package/dist/store-paths.js +61 -0
  281. package/dist/store.d.ts +30 -0
  282. package/dist/store.d.ts.map +1 -0
  283. package/dist/store.js +219 -0
  284. package/dist/testing.d.ts +47 -0
  285. package/dist/testing.d.ts.map +1 -0
  286. package/dist/testing.js +170 -0
  287. package/dist/version.d.ts +2 -0
  288. package/dist/version.d.ts.map +1 -0
  289. package/dist/version.js +4 -0
  290. package/package.json +43 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retrieval-runner.d.ts","sourceRoot":"","sources":["../../src/retrieval/retrieval-runner.ts"],"names":[],"mappings":"AA2BA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,mCAAmC,CAAC;AAUhF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAQlD,OAAO,EAIL,KAAK,cAAc,EACnB,KAAK,eAAe,EACrB,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC;IAC/B,QAAQ,CAAC,gBAAgB,EAAE,sBAAsB,CAAC;IAGlD,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAC/B;AAGD,wBAAsB,0BAA0B,CAC9C,IAAI,EAAE,qBAAqB,EAC3B,KAAK,EAAE,cAAc,GACpB,OAAO,CAAC,eAAe,CAAC,CAgB1B"}
@@ -0,0 +1,163 @@
1
+ // Retrieval orchestrator (Epic #189, Issue #199). Single entry point that the future
2
+ // #200 Conversation Center integration calls. Composes the rest of the retrieval layer:
3
+ //
4
+ // 1. Resolve scope: `capsuleId` → single-capsule scope; `capsuleSetId` →
5
+ // `buildComposedRetrievalScope` (from #263); neither → `noEvidence: "no-scope"`.
6
+ // 2. Reject empty / whitespace-only query text → `noEvidence: "empty-query"`.
7
+ // 3. `searchVectorsForScope` does the actual embedding + similarity work and
8
+ // surfaces a structured failure reason if it produces no refs.
9
+ // 4. `validateAnswerGrounding` is applied against the *strictest* policy among the
10
+ // in-scope capsules (e.g. a 3-capsule set with one capsule pinned to
11
+ // "require-citations" enforces that floor on the whole answer).
12
+ // 5. If the grounding policy rejects, the runner returns
13
+ // `RetrievalResult { references: [], noEvidence: true,
14
+ // reason: "answer-grounding-rejected" }` — even when `searchVectorsForScope`
15
+ // produced refs. We never leak refs the policy disallows.
16
+ //
17
+ // The runner does NOT throw on expected paths (no scope, empty query, dim mismatch,
18
+ // embedding failure). It only propagates `RetrievalError` from `searchVectorsForScope`
19
+ // on store-corruption invariants (e.g. blob length mismatch) — that's a real bug, not
20
+ // a user-facing condition.
21
+ import { getCapsule } from "../capsule-lifecycle.js";
22
+ import { buildComposedRetrievalScope } from "../composition.js";
23
+ import { KnowledgeNotFoundError } from "../errors.js";
24
+ import { listCapsuleSources } from "../source-lifecycle.js";
25
+ import { SourceRoutingValidationError, validateSourceRoutingForCapsule, } from "../source-routing-validation.js";
26
+ import { validateAnswerGrounding } from "./answer-grounding.js";
27
+ import { searchVectorsForScope, } from "./scoped-vector-search.js";
28
+ import { DEFAULT_RETRIEVAL_TOP_K, MAX_RETRIEVAL_TOP_K, } from "./types.js";
29
+ // ─── Public entry point ──────────────────────────────────────────────────────
30
+ export async function runLocalKnowledgeRetrieval(deps, query) {
31
+ const trimmed = query.text.trim();
32
+ if (trimmed.length === 0)
33
+ return empty("empty-query");
34
+ const scopeResolution = resolveScope(deps.store, query);
35
+ if (!scopeResolution.ok)
36
+ return empty(scopeResolution.reason);
37
+ const { scope, policy } = scopeResolution;
38
+ const topK = clampTopK(query.topK);
39
+ const search = await searchVectorsForScope(deps.store, deps.embeddingAdapter, scope, trimmed, {
40
+ topK,
41
+ ...(query.minScore !== undefined ? { minScore: query.minScore } : {}),
42
+ ...(deps.signal !== undefined ? { signal: deps.signal } : {}),
43
+ });
44
+ return finaliseWithGrounding(search, policy);
45
+ }
46
+ function resolveScope(store, query) {
47
+ if (query.capsuleId !== undefined) {
48
+ return resolveSingleCapsuleScope(store, query.capsuleId);
49
+ }
50
+ if (query.capsuleSetId !== undefined) {
51
+ return resolveCapsuleSetScope(store, query.capsuleSetId);
52
+ }
53
+ return { ok: false, reason: "no-scope" };
54
+ }
55
+ function resolveSingleCapsuleScope(store, capsuleId) {
56
+ const capsule = getCapsule(store, capsuleId);
57
+ if (capsule === undefined)
58
+ return { ok: false, reason: "no-scope" };
59
+ if (!isCapsuleRetrievalScopeValid(store, capsule)) {
60
+ return { ok: false, reason: "no-scope" };
61
+ }
62
+ return {
63
+ ok: true,
64
+ scope: { capsuleIds: [capsule.id] },
65
+ policy: capsule.answerGroundingPolicy,
66
+ };
67
+ }
68
+ function resolveCapsuleSetScope(store, setId) {
69
+ // `buildComposedRetrievalScope` throws `KnowledgeNotFoundError` on an unknown set.
70
+ // That's a real caller bug (UI gave us a stale id) so we surface it as no-scope; the
71
+ // alternative — propagating the error — would force the BFF to catch on a path that's
72
+ // semantically "the set does not exist, no answer can fire".
73
+ let scope;
74
+ try {
75
+ scope = buildComposedRetrievalScope(store, setId);
76
+ }
77
+ catch (cause) {
78
+ if (cause instanceof KnowledgeNotFoundError)
79
+ return { ok: false, reason: "no-scope" };
80
+ if (cause instanceof SourceRoutingValidationError)
81
+ return { ok: false, reason: "no-scope" };
82
+ throw cause;
83
+ }
84
+ if (scope.capsuleIds.length === 0)
85
+ return { ok: false, reason: "no-scope" };
86
+ const policy = strictestPolicy(store, scope.capsuleIds);
87
+ return {
88
+ ok: true,
89
+ scope: { capsuleIds: scope.capsuleIds, sourceFilter: scope.sourceIds },
90
+ policy,
91
+ };
92
+ }
93
+ const POLICY_RANK = Object.freeze({
94
+ "require-citations": 2,
95
+ "require-citations-or-state-no-evidence": 1,
96
+ "best-effort": 0,
97
+ });
98
+ function strictestPolicy(store, capsuleIds) {
99
+ let strictest = "best-effort";
100
+ for (const id of capsuleIds) {
101
+ const capsule = getCapsule(store, id);
102
+ if (capsule === undefined)
103
+ continue;
104
+ if (POLICY_RANK[capsule.answerGroundingPolicy] > POLICY_RANK[strictest]) {
105
+ strictest = capsule.answerGroundingPolicy;
106
+ }
107
+ }
108
+ return strictest;
109
+ }
110
+ function isCapsuleRetrievalScopeValid(store, capsule) {
111
+ try {
112
+ validateSourceRoutingForCapsule(capsule, listCapsuleSources(store, capsule.id));
113
+ return true;
114
+ }
115
+ catch (cause) {
116
+ if (cause instanceof SourceRoutingValidationError) {
117
+ return false;
118
+ }
119
+ throw cause;
120
+ }
121
+ }
122
+ // ─── Search → grounding bridge ───────────────────────────────────────────────
123
+ function finaliseWithGrounding(search, policy) {
124
+ const decision = validateAnswerGrounding(search.references, policy);
125
+ if (!decision.allow) {
126
+ return {
127
+ references: [],
128
+ noEvidence: true,
129
+ reason: "answer-grounding-rejected",
130
+ };
131
+ }
132
+ if (search.references.length === 0) {
133
+ // Surface the policy-specific reason ('no-evidence-stated') ONLY when the search actually ran
134
+ // over indexed content and nothing cleared the bar ('below-min-score'). For hard pre-search
135
+ // misses ('no-vectors' = nothing indexed, 'embedding-failed', 'empty-query',
136
+ // 'incompatible-embedding-identity') the search-layer reason is the actionable one and MUST be
137
+ // preserved — collapsing those into 'no-evidence-stated' would destroy exactly the distinction
138
+ // this fix is meant to expose: 'nothing indexed' vs 'searched, found nothing relevant'.
139
+ const reason = decision.reason === "no-evidence-stated" && search.noEvidenceReason === "below-min-score"
140
+ ? "no-evidence-stated"
141
+ : search.noEvidenceReason;
142
+ return {
143
+ references: [],
144
+ noEvidence: true,
145
+ ...(reason !== undefined ? { reason } : {}),
146
+ };
147
+ }
148
+ return {
149
+ references: search.references,
150
+ noEvidence: false,
151
+ ...(search.embeddingDegraded === true ? { embeddingDegraded: true } : {}),
152
+ };
153
+ }
154
+ function clampTopK(input) {
155
+ if (input === undefined)
156
+ return DEFAULT_RETRIEVAL_TOP_K;
157
+ if (input <= 0)
158
+ return DEFAULT_RETRIEVAL_TOP_K;
159
+ return Math.min(input, MAX_RETRIEVAL_TOP_K);
160
+ }
161
+ function empty(reason) {
162
+ return { references: [], noEvidence: true, reason };
163
+ }
@@ -0,0 +1,24 @@
1
+ import type { KnowledgeCapsuleId, KnowledgeSourceId, RetrievalReference } from "@oscharko-dev/keiko-contracts";
2
+ import type { OpenAIEmbeddingAdapter } from "@oscharko-dev/keiko-model-gateway";
3
+ import type { ComposedRetrievalScope } from "../composition.js";
4
+ import type { KnowledgeStore } from "../store.js";
5
+ export interface RetrievalScopeInput {
6
+ readonly capsuleIds: readonly KnowledgeCapsuleId[];
7
+ readonly sourceFilter?: readonly KnowledgeSourceId[];
8
+ }
9
+ export interface SearchOptions {
10
+ readonly topK: number;
11
+ readonly minScore?: number;
12
+ readonly signal?: AbortSignal;
13
+ readonly strategy?: "auto" | "balanced" | "exact" | "broad";
14
+ }
15
+ export declare function toScopeInput(scope: ComposedRetrievalScope | {
16
+ readonly capsuleId: KnowledgeCapsuleId;
17
+ }): RetrievalScopeInput;
18
+ export interface SearchOutcome {
19
+ readonly references: readonly RetrievalReference[];
20
+ readonly noEvidenceReason?: "no-vectors" | "incompatible-embedding-identity" | "below-min-score" | "embedding-failed";
21
+ readonly embeddingDegraded?: true;
22
+ }
23
+ export declare function searchVectorsForScope(store: KnowledgeStore, embeddingAdapter: OpenAIEmbeddingAdapter, scope: RetrievalScopeInput, query: string, options: SearchOptions): Promise<SearchOutcome>;
24
+ //# sourceMappingURL=scoped-vector-search.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scoped-vector-search.d.ts","sourceRoot":"","sources":["../../src/retrieval/scoped-vector-search.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAKV,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,+BAA+B,CAAC;AACvC,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,mCAAmC,CAAC;AAGhF,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAChE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AA6DlD,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,UAAU,EAAE,SAAS,kBAAkB,EAAE,CAAC;IACnD,QAAQ,CAAC,YAAY,CAAC,EAAE,SAAS,iBAAiB,EAAE,CAAC;CACtD;AAED,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;IAC9B,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,UAAU,GAAG,OAAO,GAAG,OAAO,CAAC;CAC7D;AAgBD,wBAAgB,YAAY,CAC1B,KAAK,EAAE,sBAAsB,GAAG;IAAE,QAAQ,CAAC,SAAS,EAAE,kBAAkB,CAAA;CAAE,GACzE,mBAAmB,CAKrB;AA0jBD,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,UAAU,EAAE,SAAS,kBAAkB,EAAE,CAAC;IAGnD,QAAQ,CAAC,gBAAgB,CAAC,EACtB,YAAY,GACZ,iCAAiC,GACjC,iBAAiB,GACjB,kBAAkB,CAAC;IAIvB,QAAQ,CAAC,iBAAiB,CAAC,EAAE,IAAI,CAAC;CACnC;AAgHD,wBAAsB,qBAAqB,CACzC,KAAK,EAAE,cAAc,EACrB,gBAAgB,EAAE,sBAAsB,EACxC,KAAK,EAAE,mBAAmB,EAC1B,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,aAAa,GACrB,OAAO,CAAC,aAAa,CAAC,CAsCxB"}