@qvac/core 0.3.0 → 0.3.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 (142) hide show
  1. package/dist-lib/lib/api/chat.d.ts +3 -1
  2. package/dist-lib/lib/api/chat.js +6 -2
  3. package/dist-lib/lib/api/index.d.ts +2 -0
  4. package/dist-lib/lib/api/index.js +3 -1
  5. package/dist-lib/lib/api/inference-calls.d.ts +18 -0
  6. package/dist-lib/lib/api/inference-calls.js +15 -0
  7. package/dist-lib/lib/api/rag.js +10 -2
  8. package/dist-lib/lib/assistant/harness.d.ts +1 -0
  9. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  10. package/dist-lib/lib/assistant-tree.js +11 -2
  11. package/dist-lib/lib/assistant.d.ts +14 -1
  12. package/dist-lib/lib/assistant.js +107 -15
  13. package/dist-lib/lib/core/chunk.js +5 -0
  14. package/dist-lib/lib/core/files/rag/build.d.ts +1 -2
  15. package/dist-lib/lib/core/files/rag/build.js +1 -2
  16. package/dist-lib/lib/core/files/rag/extract/office-collabora.d.ts +1 -1
  17. package/dist-lib/lib/core/files/rag/index.d.ts +4 -24
  18. package/dist-lib/lib/core/files/rag/index.js +3 -17
  19. package/dist-lib/lib/core/files/rag/load.d.ts +1 -2
  20. package/dist-lib/lib/core/files/rag/load.js +1 -2
  21. package/dist-lib/lib/core/files/rag/produce.d.ts +1 -1
  22. package/dist-lib/lib/core/files/rag/produce.js +1 -3
  23. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +1 -1
  24. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +1 -4
  25. package/dist-lib/lib/core/files/rag/similarity.d.ts +5 -0
  26. package/dist-lib/lib/core/files/rag/similarity.js +6 -0
  27. package/dist-lib/lib/core/files/upload.d.ts +1 -1
  28. package/dist-lib/lib/core/inference-calls-handoff.d.ts +20 -0
  29. package/dist-lib/lib/core/inference-calls-handoff.js +37 -0
  30. package/dist-lib/lib/core/inference-calls-receiver.d.ts +14 -0
  31. package/dist-lib/lib/core/inference-calls-receiver.js +39 -0
  32. package/dist-lib/lib/core/notification.js +5 -4
  33. package/dist-lib/lib/core.d.ts +8 -1
  34. package/dist-lib/lib/core.js +78 -4
  35. package/dist-lib/lib/inference-calls.d.ts +40 -0
  36. package/dist-lib/lib/inference-calls.js +266 -0
  37. package/dist-lib/schema/content.js +5 -1
  38. package/dist-lib/schema/engine.js +42 -1
  39. package/dist-lib/schema/qvac.js +4 -2
  40. package/dist-lib/spec/bind.js +3 -0
  41. package/dist-lib/spec/calls.js +1 -0
  42. package/dist-lib/spec/capabilities.d.ts +2 -0
  43. package/dist-lib/spec/hrpc/hrpc.json +13 -0
  44. package/dist-lib/spec/hrpc/index.js +15 -3
  45. package/dist-lib/spec/hrpc/messages.js +172 -49
  46. package/dist-lib/spec/hyperschema/index.js +172 -49
  47. package/dist-lib/spec/hyperschema/schema.json +127 -1
  48. package/dist-lib/spec/hyperschema/types.d.ts +34 -1
  49. package/dist-lib/spec/local/hyperdb/messages.js +64 -128
  50. package/dist-lib/spec/local/hyperschema/index.js +32 -64
  51. package/dist-lib/spec/local/hyperschema/schema.json +16 -0
  52. package/dist-lib/spec/mesh/hyperdb/messages.js +176 -311
  53. package/dist-lib/spec/mesh/hyperdispatch/messages.js +132 -236
  54. package/dist-lib/spec/mesh/hyperschema/index.js +132 -236
  55. package/dist-lib/spec/mesh/hyperschema/schema.json +70 -1
  56. package/dist-lib/spec/mesh/hyperschema/types.d.ts +3 -1
  57. package/package.json +14 -17
  58. package/tui/lib/knowledge-import.mjs +1 -1
  59. package/.claude/skills/asana/SKILL.md +0 -86
  60. package/.claude/skills/compaction/SKILL.md +0 -136
  61. package/.claude/skills/device-mesh-debug/SKILL.md +0 -328
  62. package/.claude/skills/gh-stack/SKILL.md +0 -181
  63. package/.claude/skills/gh-stack/references/commands.md +0 -177
  64. package/.claude/skills/gh-stack/references/stack-design.md +0 -97
  65. package/.claude/skills/gh-stack/references/troubleshooting.md +0 -159
  66. package/.claude/skills/js-house-style/SKILL.md +0 -671
  67. package/.claude/skills/pr-create/SKILL.md +0 -243
  68. package/.claude/skills/pr-review/SKILL.md +0 -428
  69. package/.claude/skills/release/SKILL.md +0 -122
  70. package/CLAUDE.md +0 -48
  71. package/dist-lib/lib/core/files/rag/artifact/codec.d.ts +0 -3
  72. package/dist-lib/lib/core/files/rag/artifact/codec.js +0 -172
  73. package/dist-lib/lib/core/files/rag/artifact/id.d.ts +0 -10
  74. package/dist-lib/lib/core/files/rag/artifact/id.js +0 -39
  75. package/dist-lib/lib/core/files/rag/artifact/types.d.ts +0 -24
  76. package/dist-lib/lib/core/files/rag/artifact/types.js +0 -41
  77. package/dist-lib/lib/core/files/rag/chunk/chunking.d.ts +0 -12
  78. package/dist-lib/lib/core/files/rag/chunk/chunking.js +0 -117
  79. package/dist-lib/lib/core/files/rag/chunk/metadata.d.ts +0 -30
  80. package/dist-lib/lib/core/files/rag/chunk/metadata.js +0 -89
  81. package/dist-lib/lib/core/files/rag/chunk/tokenizer.d.ts +0 -7
  82. package/dist-lib/lib/core/files/rag/chunk/tokenizer.js +0 -18
  83. package/dist-lib/lib/core/files/rag/extract/csv-text.d.ts +0 -1
  84. package/dist-lib/lib/core/files/rag/extract/csv-text.js +0 -117
  85. package/dist-lib/lib/core/files/rag/extract/document-types.d.ts +0 -22
  86. package/dist-lib/lib/core/files/rag/extract/document-types.js +0 -152
  87. package/dist-lib/lib/core/files/rag/extract/extract.d.ts +0 -17
  88. package/dist-lib/lib/core/files/rag/extract/extract.js +0 -81
  89. package/dist-lib/lib/core/files/rag/extract/html-table-text.d.ts +0 -1
  90. package/dist-lib/lib/core/files/rag/extract/html-table-text.js +0 -125
  91. package/dist-lib/lib/core/files/rag/extract/html-text.d.ts +0 -1
  92. package/dist-lib/lib/core/files/rag/extract/html-text.js +0 -132
  93. package/dist-lib/lib/core/files/rag/extract/pdf-platform.d.ts +0 -17
  94. package/dist-lib/lib/core/files/rag/extract/pdf-platform.js +0 -141
  95. package/dist-lib/lib/core/files/rag/extract/pdf-text.d.ts +0 -12
  96. package/dist-lib/lib/core/files/rag/extract/pdf-text.js +0 -43
  97. package/dist-lib/lib/core/files/rag/extract/rtf-text.d.ts +0 -1
  98. package/dist-lib/lib/core/files/rag/extract/rtf-text.js +0 -15
  99. package/dist-lib/lib/core/files/rag/extract/text-decoding.d.ts +0 -1
  100. package/dist-lib/lib/core/files/rag/extract/text-decoding.js +0 -91
  101. package/dist-lib/lib/core/files/rag/extract/types.d.ts +0 -36
  102. package/dist-lib/lib/core/files/rag/extract/types.js +0 -12
  103. package/dist-lib/lib/core/files/rag/extract/utf.d.ts +0 -2
  104. package/dist-lib/lib/core/files/rag/extract/utf.js +0 -18
  105. package/dist-lib/lib/core/files/rag/retrieval/citation-match.d.ts +0 -2
  106. package/dist-lib/lib/core/files/rag/retrieval/citation-match.js +0 -16
  107. package/dist-lib/lib/core/files/rag/retrieval/coverage.d.ts +0 -4
  108. package/dist-lib/lib/core/files/rag/retrieval/coverage.js +0 -48
  109. package/dist-lib/lib/core/files/rag/retrieval/diversity.d.ts +0 -10
  110. package/dist-lib/lib/core/files/rag/retrieval/diversity.js +0 -37
  111. package/dist-lib/lib/core/files/rag/retrieval/floor.d.ts +0 -5
  112. package/dist-lib/lib/core/files/rag/retrieval/floor.js +0 -21
  113. package/dist-lib/lib/core/files/rag/retrieval/format.d.ts +0 -6
  114. package/dist-lib/lib/core/files/rag/retrieval/format.js +0 -21
  115. package/dist-lib/lib/core/files/rag/retrieval/knowledge.d.ts +0 -2
  116. package/dist-lib/lib/core/files/rag/retrieval/knowledge.js +0 -54
  117. package/dist-lib/lib/core/files/rag/retrieval/notices.d.ts +0 -16
  118. package/dist-lib/lib/core/files/rag/retrieval/notices.js +0 -27
  119. package/dist-lib/lib/core/files/rag/retrieval/scoring.d.ts +0 -8
  120. package/dist-lib/lib/core/files/rag/retrieval/scoring.js +0 -82
  121. package/dist-lib/lib/core/files/rag/retrieval/search.d.ts +0 -3
  122. package/dist-lib/lib/core/files/rag/retrieval/search.js +0 -49
  123. package/dist-lib/lib/core/files/rag/retrieval/selection.d.ts +0 -11
  124. package/dist-lib/lib/core/files/rag/retrieval/selection.js +0 -93
  125. package/dist-lib/lib/core/files/rag/retrieval/source-tag-filter.d.ts +0 -6
  126. package/dist-lib/lib/core/files/rag/retrieval/source-tag-filter.js +0 -79
  127. package/dist-lib/lib/core/files/rag/retrieval/sources.d.ts +0 -13
  128. package/dist-lib/lib/core/files/rag/retrieval/sources.js +0 -20
  129. package/dist-lib/lib/core/files/rag/retrieval/target.d.ts +0 -13
  130. package/dist-lib/lib/core/files/rag/retrieval/target.js +0 -89
  131. package/dist-lib/lib/core/files/rag/retrieval/terms.d.ts +0 -8
  132. package/dist-lib/lib/core/files/rag/retrieval/terms.js +0 -70
  133. package/dist-lib/lib/core/files/rag/retrieval/types.d.ts +0 -29
  134. package/dist-lib/lib/core/files/rag/retrieval/types.js +0 -1
  135. package/dist-lib/lib/core/files/rag/retrieval/web-sources.d.ts +0 -8
  136. package/dist-lib/lib/core/files/rag/retrieval/web-sources.js +0 -41
  137. package/dist-lib/lib/core/files/rag/retrieval/whole-document-query.d.ts +0 -1
  138. package/dist-lib/lib/core/files/rag/retrieval/whole-document-query.js +0 -96
  139. package/dist-lib/lib/core/files/rag/vector/memory.d.ts +0 -15
  140. package/dist-lib/lib/core/files/rag/vector/memory.js +0 -135
  141. package/dist-lib/lib/core/files/rag/vector/types.d.ts +0 -28
  142. package/dist-lib/lib/core/files/rag/vector/types.js +0 -1
@@ -1,70 +0,0 @@
1
- import { closedClass, normalize, tag } from 'asenion';
2
- import { tokenizeForRag } from '../chunk/tokenizer.js';
3
- const MIN_TERM_LENGTH = 3;
4
- const GENERIC_WORDS = new Set([
5
- 'give',
6
- 'show',
7
- 'tell',
8
- 'get',
9
- 'find',
10
- 'what',
11
- 'data',
12
- 'info',
13
- 'information',
14
- 'please',
15
- 'want'
16
- ]);
17
- export function isMeaningfulToken(token) {
18
- if (token.length < MIN_TERM_LENGTH)
19
- return false;
20
- return !closedClass(token) && !GENERIC_WORDS.has(token);
21
- }
22
- export function extractSalientTerms(query) {
23
- const quotedPhrases = [];
24
- const quotePattern = /"([^"]+)"|'([^']+)'/g;
25
- let quoted = quotePattern.exec(query);
26
- while (quoted) {
27
- const phrase = normalize(quoted[1] ?? quoted[2] ?? '');
28
- if (phrase)
29
- quotedPhrases.push(phrase);
30
- quoted = quotePattern.exec(query);
31
- }
32
- const tokens = uniqueStrings(tokenizeForRag(query)
33
- .map((token) => token.text.trim().toLowerCase())
34
- .filter((token) => isMeaningfulToken(token) && /[\p{L}\p{N}]/u.test(token)));
35
- return { tokens, quotedPhrases, entities: extractEntities(query) };
36
- }
37
- function extractEntities(query) {
38
- const words = query.split(/\s+/).filter(Boolean);
39
- const entities = [];
40
- let run = [];
41
- const flush = () => {
42
- if (run.length >= 2) {
43
- const normalized = normalize(run.join(' '));
44
- if (names(normalized))
45
- entities.push(normalized);
46
- }
47
- run = [];
48
- };
49
- for (const word of words) {
50
- if (isCapitalizedWord(word)) {
51
- run.push(word);
52
- }
53
- else {
54
- flush();
55
- }
56
- }
57
- flush();
58
- return uniqueStrings(entities);
59
- }
60
- function names(run) {
61
- return tag(run).some(({ word, tag: part }) => isMeaningfulToken(word) && NAMING.has(part));
62
- }
63
- const NAMING = new Set(['noun', 'adj', 'num']);
64
- function isCapitalizedWord(word) {
65
- const firstLetter = word.match(/[\p{L}]/u)?.[0];
66
- return Boolean(firstLetter) && firstLetter === firstLetter?.toUpperCase();
67
- }
68
- export function uniqueStrings(values) {
69
- return [...new Set(values)];
70
- }
@@ -1,29 +0,0 @@
1
- import type { VectorHit, VectorIndex } from '../vector/types.ts';
2
- import type { RagScopeFilters } from './diversity.ts';
3
- import type { RagDocumentRef } from './target.ts';
4
- export type RagEmbedFn = (texts: string[]) => Promise<number[][]>;
5
- export type RagRetrievalDeps = {
6
- readonly index: VectorIndex;
7
- readonly embed: RagEmbedFn;
8
- };
9
- export type RagRetrievalQuery = RagScopeFilters & {
10
- readonly query: string;
11
- readonly limit?: number;
12
- readonly documentFirst?: boolean;
13
- readonly coverDocuments?: boolean;
14
- readonly chunkBudgetTokens?: number;
15
- readonly coverage?: boolean;
16
- readonly perDocChunkCap?: number;
17
- };
18
- export type RagKnowledgeQuery = {
19
- readonly query: string;
20
- readonly chatId: string;
21
- readonly documents: ReadonlyArray<RagDocumentRef>;
22
- readonly liveSourceIds?: ReadonlyArray<string>;
23
- readonly scopeDocumentIds?: ReadonlyArray<string>;
24
- };
25
- export type RagKnowledgeResult = {
26
- readonly hits: ReadonlyArray<VectorHit>;
27
- readonly targetDocumentId: string | null;
28
- readonly searchedCount: number;
29
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,8 +0,0 @@
1
- import type { RagSource } from './sources.ts';
2
- type WebRagSource = RagSource & {
3
- readonly url: string;
4
- };
5
- export declare const WEB_TOOLS: string[];
6
- export declare function webSourcesFromToolResult(result: unknown): WebRagSource[];
7
- export declare function withoutDataChannel(result: unknown): unknown;
8
- export {};
@@ -1,41 +0,0 @@
1
- import { MAX_RAG_SOURCES_PER_CHUNK, MAX_RAG_SOURCE_URL_CHARS, RAG_SOURCE_EXCERPT_CHARS } from '../../../../../schema/qvac.js';
2
- export const WEB_TOOLS = ['web_search', 'web_fetch'];
3
- export function webSourcesFromToolResult(result) {
4
- const results = structuredResults(result);
5
- const records = [];
6
- for (const entry of results) {
7
- if (records.length >= MAX_RAG_SOURCES_PER_CHUNK)
8
- break;
9
- if (typeof entry?.url !== 'string' || entry.url.length === 0)
10
- continue;
11
- if (entry.url.length > MAX_RAG_SOURCE_URL_CHARS)
12
- continue;
13
- records.push({
14
- documentId: null,
15
- title: typeof entry.title === 'string' && entry.title ? entry.title : entry.url,
16
- chunkIndex: records.length,
17
- excerpt: (typeof entry.content === 'string' ? entry.content : '').slice(0, RAG_SOURCE_EXCERPT_CHARS),
18
- attachmentId: null,
19
- artifactId: null,
20
- score: 0,
21
- url: entry.url
22
- });
23
- }
24
- return records;
25
- }
26
- export function withoutDataChannel(result) {
27
- if (result && typeof result === 'object' && 'data' in result) {
28
- const { data: _data, ...rest } = result;
29
- return rest;
30
- }
31
- return result;
32
- }
33
- function structuredResults(result) {
34
- if (!result || typeof result !== 'object')
35
- return [];
36
- const data = result.data;
37
- if (!data || typeof data !== 'object')
38
- return [];
39
- const results = data.results;
40
- return Array.isArray(results) ? results : [];
41
- }
@@ -1 +0,0 @@
1
- export declare function isWholeDocumentQuery(query: string): boolean;
@@ -1,96 +0,0 @@
1
- const SUMMARIZE_MARKERS = [
2
- /\bsummar(?:ize|ise|y|ies)\b/,
3
- /\btldr\b/,
4
- /\boverview\b/,
5
- /\bkey (?:points|takeaways)\b/,
6
- /\bmain (?:points|ideas)\b/,
7
- /\bgist\b/,
8
- /\brecap\b/,
9
- /\bwhat(?:'s| is| are)? (?:this|these|it) about\b/,
10
- /\bwhat(?:'s| is| are) (?:in|on)\b/,
11
- /\bwhat (?:do|does)\b.*\bcontain\b/
12
- ];
13
- const FILLER = new Set([
14
- 'a',
15
- 'about',
16
- 'all',
17
- 'an',
18
- 'and',
19
- 'attached',
20
- 'brief',
21
- 'briefly',
22
- 'can',
23
- 'concise',
24
- 'content',
25
- 'contents',
26
- 'contain',
27
- 'do',
28
- 'does',
29
- 'doc',
30
- 'docs',
31
- 'document',
32
- 'documents',
33
- 'file',
34
- 'files',
35
- 'for',
36
- 'give',
37
- 'in',
38
- 'is',
39
- 'it',
40
- 'its',
41
- 'me',
42
- 'my',
43
- 'of',
44
- 'on',
45
- 'pdf',
46
- 'please',
47
- 'provide',
48
- 'short',
49
- 'some',
50
- 'the',
51
- 'their',
52
- 'them',
53
- 'these',
54
- 'this',
55
- 'those',
56
- 'to',
57
- 'up',
58
- 'us',
59
- 'what',
60
- 'whole',
61
- 'you',
62
- 'your'
63
- ]);
64
- const MARKER_WORDS = new Set([
65
- 'are',
66
- 'dr',
67
- 'gist',
68
- 'ideas',
69
- 'key',
70
- 'main',
71
- 'overview',
72
- 'points',
73
- 'recap',
74
- 'summaries',
75
- 'summarise',
76
- 'summarize',
77
- 'summary',
78
- 'takeaways',
79
- 'tl',
80
- 'tldr'
81
- ]);
82
- const CLAUSE_BOUNDARY = /[.!?;\n]+|\band\b|\balso\b|\bthen\b/;
83
- export function isWholeDocumentQuery(query) {
84
- const normalized = query
85
- .toLowerCase()
86
- .replace(/\bwhat's\b/g, 'what is')
87
- .replace(/\btl[;:\s-]?dr\b/g, 'tldr')
88
- .replace(/[_-]+/g, ' ');
89
- return normalized.split(CLAUSE_BOUNDARY).some(isBareSummarizeClause);
90
- }
91
- function isBareSummarizeClause(clause) {
92
- if (!SUMMARIZE_MARKERS.some((marker) => marker.test(clause)))
93
- return false;
94
- const words = clause.match(/[a-z0-9]+/g) ?? [];
95
- return words.every((word) => FILLER.has(word) || MARKER_WORDS.has(word));
96
- }
@@ -1,15 +0,0 @@
1
- import type { VectorCandidate, VectorChunk, VectorHit, VectorIndex, VectorIndexInfo } from './types.ts';
2
- export declare class MemoryVectorIndex implements VectorIndex {
3
- private readonly _artifacts;
4
- private _chunks;
5
- private _dimensions;
6
- info(): VectorIndexInfo;
7
- upsert(artifactId: string, chunks: ReadonlyArray<VectorChunk>): Promise<void>;
8
- remove(artifactId: string): Promise<void>;
9
- search(embedding: ReadonlyArray<number>, opts: {
10
- readonly limit: number;
11
- readonly filter?: (candidate: VectorCandidate) => boolean;
12
- }): Promise<ReadonlyArray<VectorHit>>;
13
- clear(): Promise<void>;
14
- private _pack;
15
- }
@@ -1,135 +0,0 @@
1
- export class MemoryVectorIndex {
2
- _artifacts = new Map();
3
- _chunks = 0;
4
- _dimensions = null;
5
- info() {
6
- return { artifacts: this._artifacts.size, chunks: this._chunks, dimensions: this._dimensions };
7
- }
8
- upsert(artifactId, chunks) {
9
- const packed = this._pack(chunks);
10
- const prev = this._artifacts.get(artifactId);
11
- if (prev)
12
- this._chunks -= prev.ids.length;
13
- this._artifacts.set(artifactId, packed.artifact);
14
- this._chunks += packed.artifact.ids.length;
15
- if (packed.dimensions !== null && this._dimensions === null) {
16
- this._dimensions = packed.dimensions;
17
- }
18
- if (this._chunks === 0)
19
- this._dimensions = null;
20
- return Promise.resolve();
21
- }
22
- remove(artifactId) {
23
- const prev = this._artifacts.get(artifactId);
24
- if (!prev)
25
- return Promise.resolve();
26
- this._artifacts.delete(artifactId);
27
- this._chunks -= prev.ids.length;
28
- if (this._chunks === 0)
29
- this._dimensions = null;
30
- return Promise.resolve();
31
- }
32
- search(embedding, opts) {
33
- if (this._chunks === 0 || opts.limit <= 0)
34
- return Promise.resolve([]);
35
- if (this._dimensions !== null && embedding.length !== this._dimensions) {
36
- throw new Error(`vector index: query has ${embedding.length} dimensions, the index holds ${this._dimensions}`);
37
- }
38
- const dimensions = embedding.length;
39
- const query = new Float64Array(dimensions);
40
- let sum = 0;
41
- for (let d = 0; d < dimensions; d++) {
42
- const value = embedding[d];
43
- if (!Number.isFinite(value))
44
- throw new Error('vector index: query embedding is not finite');
45
- query[d] = value;
46
- sum += value * value;
47
- }
48
- const queryNorm = Math.sqrt(sum);
49
- const scored = [];
50
- for (const artifact of this._artifacts.values()) {
51
- for (let i = 0; i < artifact.ids.length; i++) {
52
- if (opts.filter) {
53
- const candidate = {
54
- id: artifact.ids[i],
55
- content: artifact.contents[i],
56
- metadataJson: artifact.metadataJsons[i]
57
- };
58
- if (!opts.filter(candidate))
59
- continue;
60
- }
61
- let dot = 0;
62
- const base = i * dimensions;
63
- for (let d = 0; d < dimensions; d++)
64
- dot += artifact.vectors[base + d] * query[d];
65
- const denominator = artifact.norms[i] * queryNorm;
66
- scored.push({
67
- id: artifact.ids[i],
68
- content: artifact.contents[i],
69
- metadataJson: artifact.metadataJsons[i],
70
- cosine: denominator > 0 ? dot / denominator : 0
71
- });
72
- }
73
- }
74
- scored.sort((a, b) => b.cosine - a.cosine);
75
- const results = scored.slice(0, opts.limit).map((entry) => ({
76
- id: entry.id,
77
- content: entry.content,
78
- metadataJson: entry.metadataJson,
79
- score: Math.max(0, entry.cosine)
80
- }));
81
- return Promise.resolve(results);
82
- }
83
- clear() {
84
- this._artifacts.clear();
85
- this._chunks = 0;
86
- this._dimensions = null;
87
- return Promise.resolve();
88
- }
89
- _pack(chunks) {
90
- if (chunks.length === 0) {
91
- return {
92
- artifact: {
93
- ids: [],
94
- contents: [],
95
- metadataJsons: [],
96
- vectors: new Float32Array(0),
97
- norms: new Float64Array(0)
98
- },
99
- dimensions: null
100
- };
101
- }
102
- const dimensions = chunks[0].embedding.length;
103
- if (dimensions === 0)
104
- throw new Error('vector index: chunk embedding is empty');
105
- if (this._dimensions !== null && dimensions !== this._dimensions) {
106
- throw new Error(`vector index: artifact has ${dimensions} dimensions, the index holds ${this._dimensions}`);
107
- }
108
- const ids = [];
109
- const contents = [];
110
- const metadataJsons = [];
111
- const vectors = new Float32Array(chunks.length * dimensions);
112
- const norms = new Float64Array(chunks.length);
113
- for (let i = 0; i < chunks.length; i++) {
114
- const chunk = chunks[i];
115
- if (chunk.embedding.length !== dimensions) {
116
- throw new Error(`vector index: chunk ${chunk.id} disagrees on dimensions`);
117
- }
118
- let sum = 0;
119
- for (let d = 0; d < dimensions; d++) {
120
- const value = chunk.embedding[d];
121
- if (!Number.isFinite(value)) {
122
- throw new Error(`vector index: chunk ${chunk.id} embedding is not finite`);
123
- }
124
- const stored = Math.fround(value);
125
- vectors[i * dimensions + d] = stored;
126
- sum += stored * stored;
127
- }
128
- norms[i] = Math.sqrt(sum);
129
- ids.push(chunk.id);
130
- contents.push(chunk.content);
131
- metadataJsons.push(chunk.metadataJson);
132
- }
133
- return { artifact: { ids, contents, metadataJsons, vectors, norms }, dimensions };
134
- }
135
- }
@@ -1,28 +0,0 @@
1
- export type VectorChunk = {
2
- readonly id: string;
3
- readonly content: string;
4
- readonly metadataJson: string;
5
- readonly embedding: ReadonlyArray<number>;
6
- };
7
- export type VectorHit = {
8
- readonly id: string;
9
- readonly content: string;
10
- readonly metadataJson: string;
11
- readonly score: number;
12
- };
13
- export type VectorCandidate = Omit<VectorHit, 'score'>;
14
- export type VectorIndexInfo = {
15
- readonly artifacts: number;
16
- readonly chunks: number;
17
- readonly dimensions: number | null;
18
- };
19
- export interface VectorIndex {
20
- info(): VectorIndexInfo;
21
- upsert(artifactId: string, chunks: ReadonlyArray<VectorChunk>): Promise<void>;
22
- remove(artifactId: string): Promise<void>;
23
- search(embedding: ReadonlyArray<number>, opts: {
24
- readonly limit: number;
25
- readonly filter?: (candidate: VectorCandidate) => boolean;
26
- }): Promise<ReadonlyArray<VectorHit>>;
27
- clear(): Promise<void>;
28
- }
@@ -1 +0,0 @@
1
- export {};