@seanhogg/builderforce-memory 2026.6.18
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 +582 -0
- package/dist/agent/SSMAgent.d.ts +146 -0
- package/dist/agent/SSMAgent.d.ts.map +1 -0
- package/dist/agent/SSMAgent.js +231 -0
- package/dist/agent/SSMAgent.js.map +1 -0
- package/dist/agent/index.d.ts +3 -0
- package/dist/agent/index.d.ts.map +1 -0
- package/dist/agent/index.js +2 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/bridges/AnthropicBridge.d.ts +47 -0
- package/dist/bridges/AnthropicBridge.d.ts.map +1 -0
- package/dist/bridges/AnthropicBridge.js +120 -0
- package/dist/bridges/AnthropicBridge.js.map +1 -0
- package/dist/bridges/CachingBridge.d.ts +44 -0
- package/dist/bridges/CachingBridge.d.ts.map +1 -0
- package/dist/bridges/CachingBridge.js +62 -0
- package/dist/bridges/CachingBridge.js.map +1 -0
- package/dist/bridges/FetchBridge.d.ts +30 -0
- package/dist/bridges/FetchBridge.d.ts.map +1 -0
- package/dist/bridges/FetchBridge.js +24 -0
- package/dist/bridges/FetchBridge.js.map +1 -0
- package/dist/bridges/OpenAIBridge.d.ts +33 -0
- package/dist/bridges/OpenAIBridge.d.ts.map +1 -0
- package/dist/bridges/OpenAIBridge.js +110 -0
- package/dist/bridges/OpenAIBridge.js.map +1 -0
- package/dist/bridges/ResponseCache.d.ts +65 -0
- package/dist/bridges/ResponseCache.d.ts.map +1 -0
- package/dist/bridges/ResponseCache.js +97 -0
- package/dist/bridges/ResponseCache.js.map +1 -0
- package/dist/bridges/SemanticCachingBridge.d.ts +31 -0
- package/dist/bridges/SemanticCachingBridge.d.ts.map +1 -0
- package/dist/bridges/SemanticCachingBridge.js +44 -0
- package/dist/bridges/SemanticCachingBridge.js.map +1 -0
- package/dist/bridges/TransformerBridge.d.ts +35 -0
- package/dist/bridges/TransformerBridge.d.ts.map +1 -0
- package/dist/bridges/TransformerBridge.js +10 -0
- package/dist/bridges/TransformerBridge.js.map +1 -0
- package/dist/bridges/index.d.ts +14 -0
- package/dist/bridges/index.d.ts.map +1 -0
- package/dist/bridges/index.js +7 -0
- package/dist/bridges/index.js.map +1 -0
- package/dist/cache/FetchSemanticCacheBackend.d.ts +40 -0
- package/dist/cache/FetchSemanticCacheBackend.d.ts.map +1 -0
- package/dist/cache/FetchSemanticCacheBackend.js +61 -0
- package/dist/cache/FetchSemanticCacheBackend.js.map +1 -0
- package/dist/cache/SemanticCache.d.ts +105 -0
- package/dist/cache/SemanticCache.d.ts.map +1 -0
- package/dist/cache/SemanticCache.js +130 -0
- package/dist/cache/SemanticCache.js.map +1 -0
- package/dist/cache/index.d.ts +5 -0
- package/dist/cache/index.d.ts.map +1 -0
- package/dist/cache/index.js +3 -0
- package/dist/cache/index.js.map +1 -0
- package/dist/distillation/DistillationEngine.d.ts +107 -0
- package/dist/distillation/DistillationEngine.d.ts.map +1 -0
- package/dist/distillation/DistillationEngine.js +152 -0
- package/dist/distillation/DistillationEngine.js.map +1 -0
- package/dist/distillation/index.d.ts +3 -0
- package/dist/distillation/index.d.ts.map +1 -0
- package/dist/distillation/index.js +2 -0
- package/dist/distillation/index.js.map +1 -0
- package/dist/errors/SSMError.d.ts +14 -0
- package/dist/errors/SSMError.d.ts.map +1 -0
- package/dist/errors/SSMError.js +18 -0
- package/dist/errors/SSMError.js.map +1 -0
- package/dist/errors/index.d.ts +3 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +2 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +59 -0
- package/dist/index.js.map +1 -0
- package/dist/memory/MemoryStore.d.ts +152 -0
- package/dist/memory/MemoryStore.d.ts.map +1 -0
- package/dist/memory/MemoryStore.js +290 -0
- package/dist/memory/MemoryStore.js.map +1 -0
- package/dist/memory/index.d.ts +3 -0
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +2 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/router/InferenceRouter.d.ts +92 -0
- package/dist/router/InferenceRouter.d.ts.map +1 -0
- package/dist/router/InferenceRouter.js +113 -0
- package/dist/router/InferenceRouter.js.map +1 -0
- package/dist/router/index.d.ts +3 -0
- package/dist/router/index.d.ts.map +1 -0
- package/dist/router/index.js +2 -0
- package/dist/router/index.js.map +1 -0
- package/dist/runtime/SSMRuntime.d.ts +167 -0
- package/dist/runtime/SSMRuntime.d.ts.map +1 -0
- package/dist/runtime/SSMRuntime.js +199 -0
- package/dist/runtime/SSMRuntime.js.map +1 -0
- package/dist/runtime/index.d.ts +3 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +2 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/session/errors.d.ts +10 -0
- package/dist/session/errors.d.ts.map +1 -0
- package/dist/session/errors.js +14 -0
- package/dist/session/errors.js.map +1 -0
- package/dist/session/index.d.ts +11 -0
- package/dist/session/index.d.ts.map +1 -0
- package/dist/session/index.js +7 -0
- package/dist/session/index.js.map +1 -0
- package/dist/session/persistence.d.ts +14 -0
- package/dist/session/persistence.d.ts.map +1 -0
- package/dist/session/persistence.js +100 -0
- package/dist/session/persistence.js.map +1 -0
- package/dist/session/presets.d.ts +31 -0
- package/dist/session/presets.d.ts.map +1 -0
- package/dist/session/presets.js +91 -0
- package/dist/session/presets.js.map +1 -0
- package/dist/session/session.d.ts +186 -0
- package/dist/session/session.d.ts.map +1 -0
- package/dist/session/session.js +358 -0
- package/dist/session/session.js.map +1 -0
- package/dist/session/streaming.d.ts +13 -0
- package/dist/session/streaming.d.ts.map +1 -0
- package/dist/session/streaming.js +74 -0
- package/dist/session/streaming.js.map +1 -0
- package/dist/session/tokenizer.d.ts +18 -0
- package/dist/session/tokenizer.d.ts.map +1 -0
- package/dist/session/tokenizer.js +11 -0
- package/dist/session/tokenizer.js.map +1 -0
- package/dist/similarity/index.d.ts +19 -0
- package/dist/similarity/index.d.ts.map +1 -0
- package/dist/similarity/index.js +42 -0
- package/dist/similarity/index.js.map +1 -0
- package/package.json +120 -0
- package/src/agent/SSMAgent.ts +327 -0
- package/src/agent/index.ts +2 -0
- package/src/bridges/AnthropicBridge.ts +166 -0
- package/src/bridges/CachingBridge.ts +79 -0
- package/src/bridges/FetchBridge.ts +41 -0
- package/src/bridges/OpenAIBridge.ts +143 -0
- package/src/bridges/ResponseCache.ts +131 -0
- package/src/bridges/SemanticCachingBridge.ts +60 -0
- package/src/bridges/TransformerBridge.ts +38 -0
- package/src/bridges/index.ts +13 -0
- package/src/cache/FetchSemanticCacheBackend.ts +79 -0
- package/src/cache/SemanticCache.ts +196 -0
- package/src/cache/index.ts +9 -0
- package/src/distillation/DistillationEngine.ts +248 -0
- package/src/distillation/index.ts +2 -0
- package/src/errors/SSMError.ts +26 -0
- package/src/errors/index.ts +2 -0
- package/src/index.ts +128 -0
- package/src/memory/MemoryStore.ts +408 -0
- package/src/memory/index.ts +2 -0
- package/src/router/InferenceRouter.ts +201 -0
- package/src/router/index.ts +2 -0
- package/src/runtime/SSMRuntime.ts +309 -0
- package/src/runtime/index.ts +2 -0
- package/src/session/errors.ts +24 -0
- package/src/session/index.ts +25 -0
- package/src/session/persistence.ts +142 -0
- package/src/session/presets.ts +122 -0
- package/src/session/session.ts +657 -0
- package/src/session/streaming.ts +97 -0
- package/src/session/tokenizer.ts +18 -0
- package/src/similarity/index.ts +42 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* streaming.ts – AsyncIterable token streaming adapter for the MambaSession layer.
|
|
3
|
+
*
|
|
4
|
+
* Wraps the step-by-step generation loop from MambaModel so that each
|
|
5
|
+
* token is yielded immediately after sampling, enabling real-time streaming UIs.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { HybridMambaModel, SamplingOptions } from '@seanhogg/builderforce-memory-engine';
|
|
9
|
+
|
|
10
|
+
/** Minimum effective temperature — avoids division by zero in sampling. */
|
|
11
|
+
const MIN_TEMPERATURE = 1e-7;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Yields one token ID at a time, applying the same sampling logic as
|
|
15
|
+
* `MambaModel.generate()` but yielding each step incrementally.
|
|
16
|
+
*/
|
|
17
|
+
export async function* tokenStream(
|
|
18
|
+
model: HybridMambaModel,
|
|
19
|
+
promptIds: number[],
|
|
20
|
+
maxNewTokens: number,
|
|
21
|
+
samplingOpts: SamplingOptions = {},
|
|
22
|
+
): AsyncGenerator<number> {
|
|
23
|
+
const { temperature = 1.0, topK = 50, topP = 0.9 } = samplingOpts;
|
|
24
|
+
const { vocabSize, eosId } = model.config;
|
|
25
|
+
|
|
26
|
+
// ids is declared const because the variable itself is never reassigned;
|
|
27
|
+
// ids.push() mutates the array contents, which const allows.
|
|
28
|
+
const ids = [...promptIds];
|
|
29
|
+
|
|
30
|
+
for (let step = 0; step < maxNewTokens; step++) {
|
|
31
|
+
const { logits } = await model.forward(
|
|
32
|
+
new Uint32Array(ids),
|
|
33
|
+
1,
|
|
34
|
+
ids.length,
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
// Only the last token position's logits are used for next-token prediction
|
|
38
|
+
const lastLogits = logits.slice(
|
|
39
|
+
(ids.length - 1) * vocabSize,
|
|
40
|
+
ids.length * vocabSize,
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const nextId = sampleToken(lastLogits, { temperature, topK, topP });
|
|
44
|
+
ids.push(nextId);
|
|
45
|
+
yield nextId;
|
|
46
|
+
|
|
47
|
+
if (nextId === eosId) break;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// ── Sampling (mirrors the private helper in mamba_model.js) ──────────────────
|
|
52
|
+
|
|
53
|
+
function sampleToken(
|
|
54
|
+
logits: Float32Array,
|
|
55
|
+
{ temperature = 1.0, topK = 50, topP = 0.9 } = {},
|
|
56
|
+
): number {
|
|
57
|
+
const n = logits.length;
|
|
58
|
+
|
|
59
|
+
// Temperature scaling
|
|
60
|
+
const scaled = new Float32Array(n);
|
|
61
|
+
for (let i = 0; i < n; i++) scaled[i] = logits[i]! / Math.max(temperature, MIN_TEMPERATURE);
|
|
62
|
+
|
|
63
|
+
// Softmax (numerically stable)
|
|
64
|
+
let maxL = -Infinity;
|
|
65
|
+
for (let i = 0; i < n; i++) if (scaled[i]! > maxL) maxL = scaled[i]!;
|
|
66
|
+
let sumE = 0;
|
|
67
|
+
const exps = new Float32Array(n);
|
|
68
|
+
for (let i = 0; i < n; i++) {
|
|
69
|
+
exps[i] = Math.exp(scaled[i]! - maxL);
|
|
70
|
+
sumE += exps[i]!;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Top-K sort
|
|
74
|
+
const indices = Array.from({ length: n }, (_, i) => i)
|
|
75
|
+
.sort((a, b) => exps[b]! - exps[a]!);
|
|
76
|
+
const topKIndices = indices.slice(0, topK);
|
|
77
|
+
|
|
78
|
+
// Nucleus (Top-P) filtering
|
|
79
|
+
let cumSum = 0;
|
|
80
|
+
const nucleus: number[] = [];
|
|
81
|
+
for (const idx of topKIndices) {
|
|
82
|
+
cumSum += exps[idx]! / sumE;
|
|
83
|
+
nucleus.push(idx);
|
|
84
|
+
if (cumSum >= topP) break;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Sample from nucleus
|
|
88
|
+
let nucleusSum = 0;
|
|
89
|
+
for (const idx of nucleus) nucleusSum += exps[idx]!;
|
|
90
|
+
const threshold = Math.random() * nucleusSum;
|
|
91
|
+
let acc = 0;
|
|
92
|
+
for (const idx of nucleus) {
|
|
93
|
+
acc += exps[idx]!;
|
|
94
|
+
if (acc >= threshold) return idx;
|
|
95
|
+
}
|
|
96
|
+
return nucleus[nucleus.length - 1]!;
|
|
97
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tokenizer – pluggable tokenizer interface for MambaSession.
|
|
3
|
+
*
|
|
4
|
+
* Pass a custom implementation via `MambaSessionOptions.tokenizer` to bypass
|
|
5
|
+
* the default Qwen2.5-Coder BPETokenizer. This is useful for:
|
|
6
|
+
* - Unit-testing with a stub tokenizer (no network required)
|
|
7
|
+
* - Using a pre-built tokenizer from HuggingFace Transformers.js
|
|
8
|
+
* - Domain-specific vocabulary (e.g. code, biomedical, multilingual)
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export interface Tokenizer {
|
|
12
|
+
/** Encodes `text` into a sequence of token IDs. */
|
|
13
|
+
encode(text: string): number[];
|
|
14
|
+
/** Decodes a sequence of token IDs back to a string. */
|
|
15
|
+
decode(tokens: number[]): string;
|
|
16
|
+
/** The total number of tokens in this tokenizer's vocabulary. */
|
|
17
|
+
readonly vocabSize: number;
|
|
18
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text + vector similarity primitives.
|
|
3
|
+
*
|
|
4
|
+
* Shared by the MemoryStore (semantic recall) and the SemanticCache
|
|
5
|
+
* (embedding-keyed response cache) so the cosine/Jaccard maths live in exactly
|
|
6
|
+
* one place rather than being duplicated per consumer.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Splits text into lowercase word tokens, removing punctuation. */
|
|
10
|
+
export function tokenize(text: string): string[] {
|
|
11
|
+
return text.toLowerCase().split(/[\s\W]+/).filter(Boolean);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Jaccard similarity between two token sets: |A ∩ B| / |A ∪ B|. */
|
|
15
|
+
export function jaccardSimilarity(a: Set<string>, b: Set<string>): number {
|
|
16
|
+
if (a.size === 0 && b.size === 0) return 1;
|
|
17
|
+
let intersection = 0;
|
|
18
|
+
for (const token of a) {
|
|
19
|
+
if (b.has(token)) intersection++;
|
|
20
|
+
}
|
|
21
|
+
// Both-empty is handled above, so `union` is always > 0 here.
|
|
22
|
+
const union = a.size + b.size - intersection;
|
|
23
|
+
return intersection / union;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Cosine similarity between two vectors in the range [-1, 1] (0 when either is a
|
|
28
|
+
* zero vector). Compares over the shorter length when lengths differ. Vectors
|
|
29
|
+
* from MambaSession.embed() are already L2-normalised — this reduces to a dot
|
|
30
|
+
* product — but we normalise defensively for vectors from other sources.
|
|
31
|
+
*/
|
|
32
|
+
export function cosineSimilarity(a: Float32Array, b: Float32Array): number {
|
|
33
|
+
const n = Math.min(a.length, b.length);
|
|
34
|
+
let dot = 0, na = 0, nb = 0;
|
|
35
|
+
for (let i = 0; i < n; i++) {
|
|
36
|
+
dot += a[i]! * b[i]!;
|
|
37
|
+
na += a[i]! * a[i]!;
|
|
38
|
+
nb += b[i]! * b[i]!;
|
|
39
|
+
}
|
|
40
|
+
const denom = Math.sqrt(na) * Math.sqrt(nb);
|
|
41
|
+
return denom === 0 ? 0 : dot / denom;
|
|
42
|
+
}
|