@qvac/core 0.1.5 → 0.1.6

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.
@@ -9,6 +9,7 @@ export async function retrieveRagKnowledge(query, deps) {
9
9
  query: query.query,
10
10
  chatId: query.chatId,
11
11
  liveSourceIds: query.liveSourceIds,
12
+ coverDocuments: query.scopeDocumentIds.length > 1,
12
13
  ...scopedShape(query.query, query.scopeDocumentIds)
13
14
  }, deps);
14
15
  logSimilarityScores(hits, hits);
@@ -1,7 +1,7 @@
1
1
  import { selectCoverage } from "./coverage.js";
2
2
  import { applyPerSourceCap, scopeFilterFor } from "./diversity.js";
3
3
  import { scoreDocuments } from "./scoring.js";
4
- import { selectChunks } from "./selection.js";
4
+ import { selectChunks, selectScopedChunks } from "./selection.js";
5
5
  const DEFAULT_LIMIT = 5;
6
6
  export async function searchRagChunks(query, deps) {
7
7
  const trimmed = query.query.trim();
@@ -21,6 +21,9 @@ export async function searchRagChunks(query, deps) {
21
21
  : {})
22
22
  });
23
23
  }
24
+ if (query.coverDocuments) {
25
+ return selectScopedChunks(scoreDocuments(hits, query.query), query.chunkBudgetTokens);
26
+ }
24
27
  if (useDocumentFirst(query))
25
28
  return documentFirstSelect(query, hits);
26
29
  return applyPerSourceCap(hits, query.limit ?? DEFAULT_LIMIT);
@@ -8,3 +8,4 @@ export type SelectChunksOptions = {
8
8
  readonly perDocChunkCap?: number;
9
9
  };
10
10
  export declare function selectChunks(ranked: ReadonlyArray<ScoredDocument>, options?: SelectChunksOptions): ReadonlyArray<VectorHit>;
11
+ export declare function selectScopedChunks(ranked: ReadonlyArray<ScoredDocument>, budgetTokens?: number): ReadonlyArray<VectorHit>;
@@ -1,5 +1,5 @@
1
1
  import { parseRagMetadata } from "../chunk/metadata.js";
2
- import { estimateRagTokens } from "../chunk/tokenizer.js";
2
+ import { estimateRagTokens, tokenizeForRag } from "../chunk/tokenizer.js";
3
3
  const DEFAULT_MAX_DOCS = 8;
4
4
  const DEFAULT_PER_DOC_CHUNK_CAP = 4;
5
5
  export const DEFAULT_CHUNK_BUDGET_TOKENS = 3072;
@@ -23,6 +23,36 @@ export function selectChunks(ranked, options = {}) {
23
23
  .sort((left, right) => left.docRank - right.docRank || left.chunkIndex - right.chunkIndex)
24
24
  .map((entry) => entry.hit);
25
25
  }
26
+ export function selectScopedChunks(ranked, budgetTokens = DEFAULT_CHUNK_BUDGET_TOKENS) {
27
+ const share = Math.floor(budgetTokens / ranked.length);
28
+ const selected = [];
29
+ for (const document of ranked) {
30
+ let remaining = share;
31
+ let count = 0;
32
+ for (const hit of document.hits) {
33
+ if (remaining === 0 || count === DEFAULT_PER_DOC_CHUNK_CAP)
34
+ break;
35
+ const tokens = tokenizeForRag(hit.content);
36
+ if (tokens.length > remaining) {
37
+ if (count > 0)
38
+ break;
39
+ selected.push({
40
+ ...hit,
41
+ content: hit.content.slice(0, tokens[remaining - 1].end),
42
+ metadataJson: JSON.stringify({
43
+ ...parseRagMetadata(hit.metadataJson),
44
+ tokenCount: remaining
45
+ })
46
+ });
47
+ break;
48
+ }
49
+ selected.push(hit);
50
+ remaining -= tokens.length;
51
+ count++;
52
+ }
53
+ }
54
+ return selected;
55
+ }
26
56
  function seedCoverage(state) {
27
57
  state.topDocs.forEach((doc, rank) => {
28
58
  const best = doc.hits[0];
@@ -10,6 +10,7 @@ export type RagRetrievalQuery = RagScopeFilters & {
10
10
  readonly query: string;
11
11
  readonly limit?: number;
12
12
  readonly documentFirst?: boolean;
13
+ readonly coverDocuments?: boolean;
13
14
  readonly chunkBudgetTokens?: number;
14
15
  readonly coverage?: boolean;
15
16
  readonly perDocChunkCap?: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qvac/core",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "description": "Peer-to-peer AI across your devices",
6
6
  "exports": {
@@ -216,7 +216,7 @@
216
216
  }
217
217
  },
218
218
  "dependencies": {
219
- "@qvac/harness": "^0.2.4",
219
+ "@qvac/harness": "^0.2.7",
220
220
  "@qvac/skills": "^0.1.3",
221
221
  "asenion": "^0.1.0",
222
222
  "autobee": "2.6.0",