@batalabs/virlow-memory 3.11.4
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.
- package/LICENSE +21 -0
- package/README.md +20 -0
- package/dist/constants.d.ts +16 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +16 -0
- package/dist/constants.js.map +1 -0
- package/dist/embedder.d.ts +20 -0
- package/dist/embedder.d.ts.map +1 -0
- package/dist/embedder.js +34 -0
- package/dist/embedder.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/vectors.d.ts +35 -0
- package/dist/vectors.d.ts.map +1 -0
- package/dist/vectors.js +50 -0
- package/dist/vectors.js.map +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Typelets Secure Notes
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# @batalabs/virlow-memory
|
|
2
|
+
|
|
3
|
+
Embedding and similarity primitives behind [Virlow](https://virlow.com)
|
|
4
|
+
memories: a pinned local embedding model, cosine ranking, and the two-tier
|
|
5
|
+
dedupe thresholds.
|
|
6
|
+
|
|
7
|
+
The embedder is a separate entry point so consumers that only need vector maths
|
|
8
|
+
do not pull in `@huggingface/transformers`:
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { cosineSimilarity, findSimilar, DEDUPE_UPDATE_THRESHOLD } from '@batalabs/virlow-memory';
|
|
12
|
+
import { createEmbedder } from '@batalabs/virlow-memory/embedder';
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Dedupe is two-tier by design: at or above `DEDUPE_UPDATE_THRESHOLD` (0.9) a
|
|
16
|
+
memory is the same memory and is updated in place; between
|
|
17
|
+
`DEDUPE_REPORT_THRESHOLD` (0.7) and that, it is stored and the caller is told
|
|
18
|
+
what it resembles.
|
|
19
|
+
|
|
20
|
+
Published from [batalabs/virlow-app](https://github.com/batalabs/virlow-app).
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Pinned embedding model. Changing ANY of these invalidates stored embeddings;
|
|
2
|
+
* bump EMBEDDING_MODEL_TAG so clients lazily re-embed (spec: embeddingModel column). */
|
|
3
|
+
export declare const EMBEDDING_MODEL_ID = "Xenova/all-MiniLM-L6-v2";
|
|
4
|
+
/** Pinned HF repo revision. The model repo can re-upload weights under the
|
|
5
|
+
* same id; without this pin, embeddings would drift while EMBEDDING_MODEL_TAG
|
|
6
|
+
* stays unchanged. Bump BOTH this and EMBEDDING_MODEL_TAG together. */
|
|
7
|
+
export declare const EMBEDDING_MODEL_REVISION = "751bff37182d3f1213fa05d7196b954e230abad9";
|
|
8
|
+
export declare const EMBEDDING_MODEL_TAG = "minilm-l6-v2@q8";
|
|
9
|
+
export declare const EMBEDDING_DIM = 384;
|
|
10
|
+
/** add_memory treats similarity >= this as "same memory, update it in place". */
|
|
11
|
+
export declare const DEDUPE_UPDATE_THRESHOLD = 0.9;
|
|
12
|
+
/** Similarity in [DEDUPE_REPORT_THRESHOLD, DEDUPE_UPDATE_THRESHOLD) is close
|
|
13
|
+
* enough to be worth telling the agent about, but not close enough to merge:
|
|
14
|
+
* add_memory stores the new memory and reports these alongside it. */
|
|
15
|
+
export declare const DEDUPE_REPORT_THRESHOLD = 0.7;
|
|
16
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;wFACwF;AACxF,eAAO,MAAM,kBAAkB,4BAA4B,CAAC;AAC5D;;uEAEuE;AACvE,eAAO,MAAM,wBAAwB,6CAA6C,CAAC;AACnF,eAAO,MAAM,mBAAmB,oBAAoB,CAAC;AACrD,eAAO,MAAM,aAAa,MAAM,CAAC;AAEjC,iFAAiF;AACjF,eAAO,MAAM,uBAAuB,MAAM,CAAC;AAC3C;;sEAEsE;AACtE,eAAO,MAAM,uBAAuB,MAAM,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Pinned embedding model. Changing ANY of these invalidates stored embeddings;
|
|
2
|
+
* bump EMBEDDING_MODEL_TAG so clients lazily re-embed (spec: embeddingModel column). */
|
|
3
|
+
export const EMBEDDING_MODEL_ID = 'Xenova/all-MiniLM-L6-v2';
|
|
4
|
+
/** Pinned HF repo revision. The model repo can re-upload weights under the
|
|
5
|
+
* same id; without this pin, embeddings would drift while EMBEDDING_MODEL_TAG
|
|
6
|
+
* stays unchanged. Bump BOTH this and EMBEDDING_MODEL_TAG together. */
|
|
7
|
+
export const EMBEDDING_MODEL_REVISION = '751bff37182d3f1213fa05d7196b954e230abad9';
|
|
8
|
+
export const EMBEDDING_MODEL_TAG = 'minilm-l6-v2@q8';
|
|
9
|
+
export const EMBEDDING_DIM = 384;
|
|
10
|
+
/** add_memory treats similarity >= this as "same memory, update it in place". */
|
|
11
|
+
export const DEDUPE_UPDATE_THRESHOLD = 0.9;
|
|
12
|
+
/** Similarity in [DEDUPE_REPORT_THRESHOLD, DEDUPE_UPDATE_THRESHOLD) is close
|
|
13
|
+
* enough to be worth telling the agent about, but not close enough to merge:
|
|
14
|
+
* add_memory stores the new memory and reports these alongside it. */
|
|
15
|
+
export const DEDUPE_REPORT_THRESHOLD = 0.7;
|
|
16
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;wFACwF;AACxF,MAAM,CAAC,MAAM,kBAAkB,GAAG,yBAAyB,CAAC;AAC5D;;uEAEuE;AACvE,MAAM,CAAC,MAAM,wBAAwB,GAAG,0CAA0C,CAAC;AACnF,MAAM,CAAC,MAAM,mBAAmB,GAAG,iBAAiB,CAAC;AACrD,MAAM,CAAC,MAAM,aAAa,GAAG,GAAG,CAAC;AAEjC,iFAAiF;AACjF,MAAM,CAAC,MAAM,uBAAuB,GAAG,GAAG,CAAC;AAC3C;;sEAEsE;AACtE,MAAM,CAAC,MAAM,uBAAuB,GAAG,GAAG,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface Embedder {
|
|
2
|
+
embed(text: string): Promise<Float32Array>;
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Load the pinned embedding model. In Node, `cacheDir` controls where the
|
|
6
|
+
* public ONNX weights are cached (the MCP server passes ~/.virlow-mcp/models);
|
|
7
|
+
* in the browser, transformers.js uses the Cache API automatically.
|
|
8
|
+
*
|
|
9
|
+
* `localModelPath` self-hosts the model instead of fetching it from the
|
|
10
|
+
* Hugging Face Hub — the web app's production CSP doesn't allow
|
|
11
|
+
* huggingface.co, and self-hosting is better privacy anyway. Set it to the
|
|
12
|
+
* public URL prefix (e.g. `/models/`) that serves the files fetched by
|
|
13
|
+
* `scripts/fetch-embedding-model.mjs`; transformers.js then resolves
|
|
14
|
+
* `<localModelPath><EMBEDDING_MODEL_ID>/...` instead of hitting the Hub.
|
|
15
|
+
*/
|
|
16
|
+
export declare function createEmbedder(opts?: {
|
|
17
|
+
cacheDir?: string;
|
|
18
|
+
localModelPath?: string;
|
|
19
|
+
}): Promise<Embedder>;
|
|
20
|
+
//# sourceMappingURL=embedder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embedder.d.ts","sourceRoot":"","sources":["../src/embedder.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,QAAQ;IACvB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;CAC5C;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,cAAc,CAClC,IAAI,GAAE;IAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,cAAc,CAAC,EAAE,MAAM,CAAA;CAAO,GACxD,OAAO,CAAC,QAAQ,CAAC,CAkBnB"}
|
package/dist/embedder.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { env, pipeline } from '@huggingface/transformers';
|
|
2
|
+
import { EMBEDDING_MODEL_ID, EMBEDDING_MODEL_REVISION } from './constants';
|
|
3
|
+
/**
|
|
4
|
+
* Load the pinned embedding model. In Node, `cacheDir` controls where the
|
|
5
|
+
* public ONNX weights are cached (the MCP server passes ~/.virlow-mcp/models);
|
|
6
|
+
* in the browser, transformers.js uses the Cache API automatically.
|
|
7
|
+
*
|
|
8
|
+
* `localModelPath` self-hosts the model instead of fetching it from the
|
|
9
|
+
* Hugging Face Hub — the web app's production CSP doesn't allow
|
|
10
|
+
* huggingface.co, and self-hosting is better privacy anyway. Set it to the
|
|
11
|
+
* public URL prefix (e.g. `/models/`) that serves the files fetched by
|
|
12
|
+
* `scripts/fetch-embedding-model.mjs`; transformers.js then resolves
|
|
13
|
+
* `<localModelPath><EMBEDDING_MODEL_ID>/...` instead of hitting the Hub.
|
|
14
|
+
*/
|
|
15
|
+
export async function createEmbedder(opts = {}) {
|
|
16
|
+
if (opts.cacheDir) {
|
|
17
|
+
env.cacheDir = opts.cacheDir;
|
|
18
|
+
}
|
|
19
|
+
if (opts.localModelPath) {
|
|
20
|
+
env.allowRemoteModels = false;
|
|
21
|
+
env.localModelPath = opts.localModelPath;
|
|
22
|
+
}
|
|
23
|
+
const extractor = await pipeline('feature-extraction', EMBEDDING_MODEL_ID, {
|
|
24
|
+
dtype: 'q8',
|
|
25
|
+
revision: EMBEDDING_MODEL_REVISION,
|
|
26
|
+
});
|
|
27
|
+
return {
|
|
28
|
+
async embed(text) {
|
|
29
|
+
const output = await extractor(text, { pooling: 'mean', normalize: true });
|
|
30
|
+
return Float32Array.from(output.data);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=embedder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embedder.js","sourceRoot":"","sources":["../src/embedder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAE,kBAAkB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAM3E;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,OAAuD,EAAE;IAEzD,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;QAClB,GAAG,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC;IAC/B,CAAC;IACD,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;QACxB,GAAG,CAAC,iBAAiB,GAAG,KAAK,CAAC;QAC9B,GAAG,CAAC,cAAc,GAAG,IAAI,CAAC,cAAc,CAAC;IAC3C,CAAC;IACD,MAAM,SAAS,GAAG,MAAM,QAAQ,CAAC,oBAAoB,EAAE,kBAAkB,EAAE;QACzE,KAAK,EAAE,IAAI;QACX,QAAQ,EAAE,wBAAwB;KACnC,CAAC,CAAC;IACH,OAAO;QACL,KAAK,CAAC,KAAK,CAAC,IAAY;YACtB,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;YAC3E,OAAO,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC,IAAoB,CAAC,CAAC;QACxD,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { DEDUPE_REPORT_THRESHOLD, DEDUPE_UPDATE_THRESHOLD, EMBEDDING_DIM, EMBEDDING_MODEL_ID, EMBEDDING_MODEL_REVISION, EMBEDDING_MODEL_TAG, } from './constants';
|
|
2
|
+
export { bytesToEmbedding, cosineSimilarity, embeddingToBytes, findDuplicate, findSimilar, rankBySimilarity, } from './vectors';
|
|
3
|
+
export { createEmbedder, type Embedder } from './embedder';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,uBAAuB,EACvB,aAAa,EACb,kBAAkB,EAClB,wBAAwB,EACxB,mBAAmB,GACpB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EAChB,aAAa,EACb,WAAW,EACX,gBAAgB,GACjB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,cAAc,EAAE,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { DEDUPE_REPORT_THRESHOLD, DEDUPE_UPDATE_THRESHOLD, EMBEDDING_DIM, EMBEDDING_MODEL_ID, EMBEDDING_MODEL_REVISION, EMBEDDING_MODEL_TAG, } from './constants';
|
|
2
|
+
export { bytesToEmbedding, cosineSimilarity, embeddingToBytes, findDuplicate, findSimilar, rankBySimilarity, } from './vectors';
|
|
3
|
+
export { createEmbedder } from './embedder';
|
|
4
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,uBAAuB,EACvB,aAAa,EACb,kBAAkB,EAClB,wBAAwB,EACxB,mBAAmB,GACpB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EAChB,aAAa,EACb,WAAW,EACX,gBAAgB,GACjB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,cAAc,EAAiB,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export declare function cosineSimilarity(a: Float32Array, b: Float32Array): number;
|
|
2
|
+
export declare function rankBySimilarity(query: Float32Array, entries: ReadonlyArray<{
|
|
3
|
+
id: string;
|
|
4
|
+
embedding: Float32Array;
|
|
5
|
+
}>, limit: number): Array<{
|
|
6
|
+
id: string;
|
|
7
|
+
similarity: number;
|
|
8
|
+
}>;
|
|
9
|
+
export declare function findDuplicate(embedding: Float32Array, entries: ReadonlyArray<{
|
|
10
|
+
id: string;
|
|
11
|
+
embedding: Float32Array;
|
|
12
|
+
}>, threshold?: number): {
|
|
13
|
+
id: string;
|
|
14
|
+
similarity: number;
|
|
15
|
+
} | null;
|
|
16
|
+
/**
|
|
17
|
+
* Every entry at or above `threshold`, most similar first. Unlike
|
|
18
|
+
* `rankBySimilarity` this takes no limit: add_memory reports the full set of
|
|
19
|
+
* near-misses so the agent can see everything it might be duplicating.
|
|
20
|
+
*/
|
|
21
|
+
export declare function findSimilar(embedding: Float32Array, entries: ReadonlyArray<{
|
|
22
|
+
id: string;
|
|
23
|
+
embedding: Float32Array;
|
|
24
|
+
}>, threshold?: number): Array<{
|
|
25
|
+
id: string;
|
|
26
|
+
similarity: number;
|
|
27
|
+
}>;
|
|
28
|
+
/**
|
|
29
|
+
* Float32Array <-> bytes for encryption/transport. Byte order is the
|
|
30
|
+
* platform's native order — little-endian on every runtime Virlow targets
|
|
31
|
+
* (x86, ARM); embeddings are device-portable in practice.
|
|
32
|
+
*/
|
|
33
|
+
export declare function embeddingToBytes(vec: Float32Array): Uint8Array;
|
|
34
|
+
export declare function bytesToEmbedding(bytes: Uint8Array): Float32Array;
|
|
35
|
+
//# sourceMappingURL=vectors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vectors.d.ts","sourceRoot":"","sources":["../src/vectors.ts"],"names":[],"mappings":"AAKA,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,YAAY,GAAG,MAAM,CAgBzE;AAED,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,YAAY,EACnB,OAAO,EAAE,aAAa,CAAC;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,YAAY,CAAA;CAAE,CAAC,EAC/D,KAAK,EAAE,MAAM,GACZ,KAAK,CAAC;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAAC,CAK3C;AAED,wBAAgB,aAAa,CAC3B,SAAS,EAAE,YAAY,EACvB,OAAO,EAAE,aAAa,CAAC;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,YAAY,CAAA;CAAE,CAAC,EAC/D,SAAS,GAAE,MAAgC,GAC1C;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAG3C;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,SAAS,EAAE,YAAY,EACvB,OAAO,EAAE,aAAa,CAAC;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,YAAY,CAAA;CAAE,CAAC,EAC/D,SAAS,GAAE,MAAgC,GAC1C,KAAK,CAAC;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAAC,CAI3C;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,YAAY,GAAG,UAAU,CAI9D;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,UAAU,GAAG,YAAY,CAShE"}
|
package/dist/vectors.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { DEDUPE_REPORT_THRESHOLD, DEDUPE_UPDATE_THRESHOLD, } from './constants';
|
|
2
|
+
export function cosineSimilarity(a, b) {
|
|
3
|
+
if (a.length !== b.length) {
|
|
4
|
+
throw new Error(`Embedding dimension mismatch: ${a.length} vs ${b.length}`);
|
|
5
|
+
}
|
|
6
|
+
let dot = 0;
|
|
7
|
+
let normA = 0;
|
|
8
|
+
let normB = 0;
|
|
9
|
+
for (let i = 0; i < a.length; i++) {
|
|
10
|
+
dot += a[i] * b[i];
|
|
11
|
+
normA += a[i] * a[i];
|
|
12
|
+
normB += b[i] * b[i];
|
|
13
|
+
}
|
|
14
|
+
if (normA === 0 || normB === 0)
|
|
15
|
+
return 0;
|
|
16
|
+
return dot / (Math.sqrt(normA) * Math.sqrt(normB));
|
|
17
|
+
}
|
|
18
|
+
export function rankBySimilarity(query, entries, limit) {
|
|
19
|
+
return entries
|
|
20
|
+
.map((e) => ({ id: e.id, similarity: cosineSimilarity(query, e.embedding) }))
|
|
21
|
+
.sort((x, y) => y.similarity - x.similarity)
|
|
22
|
+
.slice(0, limit);
|
|
23
|
+
}
|
|
24
|
+
export function findDuplicate(embedding, entries, threshold = DEDUPE_UPDATE_THRESHOLD) {
|
|
25
|
+
const [best] = rankBySimilarity(embedding, entries, 1);
|
|
26
|
+
return best && best.similarity >= threshold ? best : null;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Every entry at or above `threshold`, most similar first. Unlike
|
|
30
|
+
* `rankBySimilarity` this takes no limit: add_memory reports the full set of
|
|
31
|
+
* near-misses so the agent can see everything it might be duplicating.
|
|
32
|
+
*/
|
|
33
|
+
export function findSimilar(embedding, entries, threshold = DEDUPE_REPORT_THRESHOLD) {
|
|
34
|
+
return rankBySimilarity(embedding, entries, entries.length).filter((e) => e.similarity >= threshold);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Float32Array <-> bytes for encryption/transport. Byte order is the
|
|
38
|
+
* platform's native order — little-endian on every runtime Virlow targets
|
|
39
|
+
* (x86, ARM); embeddings are device-portable in practice.
|
|
40
|
+
*/
|
|
41
|
+
export function embeddingToBytes(vec) {
|
|
42
|
+
return new Uint8Array(vec.buffer.slice(vec.byteOffset, vec.byteOffset + vec.byteLength));
|
|
43
|
+
}
|
|
44
|
+
export function bytesToEmbedding(bytes) {
|
|
45
|
+
if (bytes.byteLength % 4 !== 0) {
|
|
46
|
+
throw new Error(`Embedding byte length ${bytes.byteLength} is not a multiple of 4`);
|
|
47
|
+
}
|
|
48
|
+
return new Float32Array(bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength));
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=vectors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vectors.js","sourceRoot":"","sources":["../src/vectors.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,uBAAuB,GACxB,MAAM,aAAa,CAAC;AAErB,MAAM,UAAU,gBAAgB,CAAC,CAAe,EAAE,CAAe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
+
"name": "@batalabs/virlow-memory",
|
|
3
|
+
"version": "3.11.4",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"description": "Virlow Memories shared core: embeddings, similarity ranking, dedupe, and encrypted memory payloads.",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./embedder": {
|
|
14
|
+
"types": "./dist/embedder.d.ts",
|
|
15
|
+
"import": "./dist/embedder.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@huggingface/transformers": "^3.4.0",
|
|
20
|
+
"@batalabs/virlow-crypto": "3.11.4"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/node": "^22.10.0",
|
|
24
|
+
"typescript": "^5.8.0",
|
|
25
|
+
"vitest": "^3.0.0"
|
|
26
|
+
},
|
|
27
|
+
"main": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"files": [
|
|
30
|
+
"LICENSE",
|
|
31
|
+
"README.md",
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/batalabs/virlow-app.git",
|
|
40
|
+
"directory": "packages/memory-core"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"test": "vitest run",
|
|
44
|
+
"type-check": "tsc --noEmit",
|
|
45
|
+
"build": "tsc -p tsconfig.build.json"
|
|
46
|
+
}
|
|
47
|
+
}
|