nexus-prime 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.
- package/README.md +253 -0
- package/dist/agents/adapters/mcp.d.ts +21 -0
- package/dist/agents/adapters/mcp.d.ts.map +1 -0
- package/dist/agents/adapters/mcp.js +447 -0
- package/dist/agents/adapters/mcp.js.map +1 -0
- package/dist/agents/adapters.d.ts +57 -0
- package/dist/agents/adapters.d.ts.map +1 -0
- package/dist/agents/adapters.js +151 -0
- package/dist/agents/adapters.js.map +1 -0
- package/dist/agents/coordinator.d.ts +75 -0
- package/dist/agents/coordinator.d.ts.map +1 -0
- package/dist/agents/coordinator.js +339 -0
- package/dist/agents/coordinator.js.map +1 -0
- package/dist/agents/core/types.d.ts +157 -0
- package/dist/agents/core/types.d.ts.map +1 -0
- package/dist/agents/core/types.js +5 -0
- package/dist/agents/core/types.js.map +1 -0
- package/dist/agents/learner.d.ts +25 -0
- package/dist/agents/learner.d.ts.map +1 -0
- package/dist/agents/learner.js +88 -0
- package/dist/agents/learner.js.map +1 -0
- package/dist/cli.d.ts +6 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +237 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +74 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +73 -0
- package/dist/config.js.map +1 -0
- package/dist/core/evolution.d.ts +122 -0
- package/dist/core/evolution.d.ts.map +1 -0
- package/dist/core/evolution.js +319 -0
- package/dist/core/evolution.js.map +1 -0
- package/dist/core/memory.d.ts +96 -0
- package/dist/core/memory.d.ts.map +1 -0
- package/dist/core/memory.js +304 -0
- package/dist/core/memory.js.map +1 -0
- package/dist/core/optimize.d.ts +100 -0
- package/dist/core/optimize.d.ts.map +1 -0
- package/dist/core/optimize.js +263 -0
- package/dist/core/optimize.js.map +1 -0
- package/dist/core/types.d.ts +155 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +5 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/wave.d.ts +55 -0
- package/dist/core/wave.d.ts.map +1 -0
- package/dist/core/wave.js +119 -0
- package/dist/core/wave.js.map +1 -0
- package/dist/engines/benchmark.d.ts +76 -0
- package/dist/engines/benchmark.d.ts.map +1 -0
- package/dist/engines/benchmark.js +167 -0
- package/dist/engines/benchmark.js.map +1 -0
- package/dist/engines/cache-manager.d.ts +75 -0
- package/dist/engines/cache-manager.d.ts.map +1 -0
- package/dist/engines/cache-manager.js +200 -0
- package/dist/engines/cache-manager.js.map +1 -0
- package/dist/engines/context.d.ts +50 -0
- package/dist/engines/context.d.ts.map +1 -0
- package/dist/engines/context.js +99 -0
- package/dist/engines/context.js.map +1 -0
- package/dist/engines/embedder.d.ts +55 -0
- package/dist/engines/embedder.d.ts.map +1 -0
- package/dist/engines/embedder.js +223 -0
- package/dist/engines/embedder.js.map +1 -0
- package/dist/engines/guardrails-bridge.d.ts +64 -0
- package/dist/engines/guardrails-bridge.d.ts.map +1 -0
- package/dist/engines/guardrails-bridge.js +251 -0
- package/dist/engines/guardrails-bridge.js.map +1 -0
- package/dist/engines/index.d.ts +14 -0
- package/dist/engines/index.d.ts.map +1 -0
- package/dist/engines/index.js +14 -0
- package/dist/engines/index.js.map +1 -0
- package/dist/engines/memory.d.ts +76 -0
- package/dist/engines/memory.d.ts.map +1 -0
- package/dist/engines/memory.js +431 -0
- package/dist/engines/memory.js.map +1 -0
- package/dist/engines/meta-learner.d.ts +87 -0
- package/dist/engines/meta-learner.d.ts.map +1 -0
- package/dist/engines/meta-learner.js +214 -0
- package/dist/engines/meta-learner.js.map +1 -0
- package/dist/engines/orchestrator.d.ts +69 -0
- package/dist/engines/orchestrator.d.ts.map +1 -0
- package/dist/engines/orchestrator.js +175 -0
- package/dist/engines/orchestrator.js.map +1 -0
- package/dist/engines/pod-network.d.ts +31 -0
- package/dist/engines/pod-network.d.ts.map +1 -0
- package/dist/engines/pod-network.js +108 -0
- package/dist/engines/pod-network.js.map +1 -0
- package/dist/engines/token-optimizer.d.ts +53 -0
- package/dist/engines/token-optimizer.d.ts.map +1 -0
- package/dist/engines/token-optimizer.js +115 -0
- package/dist/engines/token-optimizer.js.map +1 -0
- package/dist/engines/token-supremacy.d.ts +86 -0
- package/dist/engines/token-supremacy.d.ts.map +1 -0
- package/dist/engines/token-supremacy.js +403 -0
- package/dist/engines/token-supremacy.js.map +1 -0
- package/dist/index.d.ts +99 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +302 -0
- package/dist/index.js.map +1 -0
- package/dist/nexus-prime.d.ts +15 -0
- package/dist/nexus-prime.d.ts.map +1 -0
- package/dist/nexus-prime.js +15 -0
- package/dist/nexus-prime.js.map +1 -0
- package/dist/phantom/index.d.ts +135 -0
- package/dist/phantom/index.d.ts.map +1 -0
- package/dist/phantom/index.js +286 -0
- package/dist/phantom/index.js.map +1 -0
- package/dist/phantom/merge-oracle.d.ts +35 -0
- package/dist/phantom/merge-oracle.d.ts.map +1 -0
- package/dist/phantom/merge-oracle.js +189 -0
- package/dist/phantom/merge-oracle.js.map +1 -0
- package/dist/phantom/phase4-orchestrator.d.ts +14 -0
- package/dist/phantom/phase4-orchestrator.d.ts.map +1 -0
- package/dist/phantom/phase4-orchestrator.js +488 -0
- package/dist/phantom/phase4-orchestrator.js.map +1 -0
- package/dist/verify-guardrails.d.ts +2 -0
- package/dist/verify-guardrails.d.ts.map +1 -0
- package/dist/verify-guardrails.js +48 -0
- package/dist/verify-guardrails.js.map +1 -0
- package/dist/verify-pod-sync.d.ts +2 -0
- package/dist/verify-pod-sync.d.ts.map +1 -0
- package/dist/verify-pod-sync.js +41 -0
- package/dist/verify-pod-sync.js.map +1 -0
- package/dist/verify-substrate.d.ts +2 -0
- package/dist/verify-substrate.d.ts.map +1 -0
- package/dist/verify-substrate.js +41 -0
- package/dist/verify-substrate.js.map +1 -0
- package/dist/verify-token-scoring.d.ts +2 -0
- package/dist/verify-token-scoring.d.ts.map +1 -0
- package/dist/verify-token-scoring.js +37 -0
- package/dist/verify-token-scoring.js.map +1 -0
- package/package.json +80 -0
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Three-Tier Brain-Inspired Memory System
|
|
3
|
+
*
|
|
4
|
+
* CORTEX: Long-term memory (persistent)
|
|
5
|
+
* HIPPOCAMPUS: Medium-term (recent 24-48 hours)
|
|
6
|
+
* PREFRONTAL: Working memory (~7 items)
|
|
7
|
+
*/
|
|
8
|
+
export class MemorySystem {
|
|
9
|
+
cortex;
|
|
10
|
+
hippocampus;
|
|
11
|
+
prefrontal;
|
|
12
|
+
decayRate = 0.0001;
|
|
13
|
+
consolidationThreshold = 0.5;
|
|
14
|
+
constructor(options) {
|
|
15
|
+
// Initialize Cortex (Long-term)
|
|
16
|
+
this.cortex = {
|
|
17
|
+
patterns: new Map(),
|
|
18
|
+
synapses: new Map(),
|
|
19
|
+
semanticGraph: {
|
|
20
|
+
nodes: new Map(),
|
|
21
|
+
edges: new Map()
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
// Initialize Hippocampus (Medium-term)
|
|
25
|
+
this.hippocampus = {
|
|
26
|
+
recentPatterns: [],
|
|
27
|
+
window: options?.hippocampusWindowHours ?? 48,
|
|
28
|
+
bindings: new Map()
|
|
29
|
+
};
|
|
30
|
+
// Initialize Prefrontal (Working)
|
|
31
|
+
this.prefrontal = {
|
|
32
|
+
activeContext: [],
|
|
33
|
+
maxItems: options?.prefrontalMaxItems ?? 7
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
// ==================== CORTEX OPERATIONS ====================
|
|
37
|
+
/**
|
|
38
|
+
* Store pattern in long-term memory (Cortex)
|
|
39
|
+
*/
|
|
40
|
+
store(pattern, priority = 1.0) {
|
|
41
|
+
this.cortex.patterns.set(pattern.id, pattern);
|
|
42
|
+
this.cortex.synapses.set(pattern.id, priority);
|
|
43
|
+
// Add to semantic graph
|
|
44
|
+
this.addToGraph(pattern);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Recall patterns from Cortex
|
|
48
|
+
*/
|
|
49
|
+
recall(query, k = 5) {
|
|
50
|
+
const scores = [];
|
|
51
|
+
for (const [id, pattern] of this.cortex.patterns) {
|
|
52
|
+
const similarity = this.cosineSimilarity(query, pattern.structure);
|
|
53
|
+
const recency = this.computeRecency(pattern);
|
|
54
|
+
const weight = this.cortex.synapses.get(id) ?? 1.0;
|
|
55
|
+
const score = similarity * (1 + weight) * recency;
|
|
56
|
+
scores.push({ pattern, score });
|
|
57
|
+
}
|
|
58
|
+
// Return top-k
|
|
59
|
+
return scores
|
|
60
|
+
.sort((a, b) => b.score - a.score)
|
|
61
|
+
.slice(0, k);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Hebbian strengthening: patterns that co-occur get stronger
|
|
65
|
+
*/
|
|
66
|
+
hebbianStrengthen(pattern) {
|
|
67
|
+
const related = this.findRelated(pattern);
|
|
68
|
+
for (const rel of related) {
|
|
69
|
+
const currentWeight = this.cortex.synapses.get(rel.id) ?? 1.0;
|
|
70
|
+
this.cortex.synapses.set(rel.id, currentWeight + pattern.confidence * 0.01);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Decay unused patterns
|
|
75
|
+
*/
|
|
76
|
+
decayUnused() {
|
|
77
|
+
for (const [id] of this.cortex.synapses) {
|
|
78
|
+
if (!this.wasRecentlyAccessed(id)) {
|
|
79
|
+
const currentWeight = this.cortex.synapses.get(id) ?? 1.0;
|
|
80
|
+
this.cortex.synapses.set(id, currentWeight * (1 - this.decayRate));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// Prune very weak synapses
|
|
84
|
+
for (const [id, weight] of this.cortex.synapses) {
|
|
85
|
+
if (weight < 0.01) {
|
|
86
|
+
this.cortex.patterns.delete(id);
|
|
87
|
+
this.cortex.synapses.delete(id);
|
|
88
|
+
this.removeFromGraph(id);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// ==================== HIPPOCAMPUS OPERATIONS ====================
|
|
93
|
+
/**
|
|
94
|
+
* Add pattern to recent memory (Hippocampus)
|
|
95
|
+
*/
|
|
96
|
+
addToRecent(pattern) {
|
|
97
|
+
this.hippocampus.recentPatterns.unshift(pattern);
|
|
98
|
+
// Trim to window
|
|
99
|
+
const maxPatterns = this.hippocampus.window * 10; // ~10 patterns per hour
|
|
100
|
+
if (this.hippocampus.recentPatterns.length > maxPatterns) {
|
|
101
|
+
this.hippocampus.recentPatterns = this.hippocampus.recentPatterns.slice(0, maxPatterns);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Create temporary binding between patterns
|
|
106
|
+
*/
|
|
107
|
+
bind(patternA, patternB) {
|
|
108
|
+
this.hippocampus.bindings.set(patternA, patternB);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Get recent patterns
|
|
112
|
+
*/
|
|
113
|
+
getRecent(k = 10) {
|
|
114
|
+
return this.hippocampus.recentPatterns.slice(0, k);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Consolidate: Transfer important patterns from Hippocampus to Cortex
|
|
118
|
+
*/
|
|
119
|
+
consolidate() {
|
|
120
|
+
for (const pattern of this.hippocampus.recentPatterns) {
|
|
121
|
+
if (pattern.confidence > this.consolidationThreshold) {
|
|
122
|
+
// Promote to Cortex
|
|
123
|
+
this.store(pattern, pattern.confidence);
|
|
124
|
+
this.hebbianStrengthen(pattern);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// Clear recent (they're now in Cortex or decayed)
|
|
128
|
+
this.hippocampus.recentPatterns = [];
|
|
129
|
+
}
|
|
130
|
+
// ==================== PREFRONTAL OPERATIONS ====================
|
|
131
|
+
/**
|
|
132
|
+
* Add to working memory
|
|
133
|
+
*/
|
|
134
|
+
addToWorking(item) {
|
|
135
|
+
if (!this.prefrontal.activeContext.includes(item)) {
|
|
136
|
+
this.prefrontal.activeContext.unshift(item);
|
|
137
|
+
// Enforce capacity limit (Miller's Law: ~7 items)
|
|
138
|
+
if (this.prefrontal.activeContext.length > this.prefrontal.maxItems) {
|
|
139
|
+
this.prefrontal.activeContext.pop();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Get working memory
|
|
145
|
+
*/
|
|
146
|
+
getWorking() {
|
|
147
|
+
return [...this.prefrontal.activeContext];
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Clear working memory
|
|
151
|
+
*/
|
|
152
|
+
clearWorking() {
|
|
153
|
+
this.prefrontal.activeContext = [];
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Keep only specific items in working memory
|
|
157
|
+
*/
|
|
158
|
+
retain(items) {
|
|
159
|
+
this.prefrontal.activeContext = items.filter(item => this.prefrontal.activeContext.includes(item));
|
|
160
|
+
}
|
|
161
|
+
// ==================== LEARNING ====================
|
|
162
|
+
/**
|
|
163
|
+
* Learn from experience
|
|
164
|
+
*/
|
|
165
|
+
learn(experience) {
|
|
166
|
+
// Extract pattern from experience
|
|
167
|
+
const pattern = {
|
|
168
|
+
id: `pattern_${experience.agentId}_${experience.timestamp}`,
|
|
169
|
+
structure: this.embedExperience(experience),
|
|
170
|
+
weight: experience.value,
|
|
171
|
+
confidence: experience.value,
|
|
172
|
+
origin: experience.agentId,
|
|
173
|
+
timestamp: experience.timestamp,
|
|
174
|
+
examples: [experience.action]
|
|
175
|
+
};
|
|
176
|
+
if (experience.value > 0.5) {
|
|
177
|
+
// High value: store prominently
|
|
178
|
+
this.store(pattern, experience.value);
|
|
179
|
+
this.addToRecent(pattern);
|
|
180
|
+
// Very high value: trigger consolidation immediately
|
|
181
|
+
if (experience.value > 0.9) {
|
|
182
|
+
this.hebbianStrengthen(pattern);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
// Low value: just add to recent, let it decay
|
|
187
|
+
this.addToRecent(pattern);
|
|
188
|
+
}
|
|
189
|
+
return pattern;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Compress multiple experiences into a single pattern
|
|
193
|
+
*/
|
|
194
|
+
compress(experiences) {
|
|
195
|
+
if (experiences.length === 0) {
|
|
196
|
+
throw new Error('No experiences to compress');
|
|
197
|
+
}
|
|
198
|
+
if (experiences.length === 1) {
|
|
199
|
+
return this.learn(experiences[0]);
|
|
200
|
+
}
|
|
201
|
+
// Average embeddings
|
|
202
|
+
const embeddings = experiences.map(e => this.embedExperience(e));
|
|
203
|
+
const avgEmbedding = embeddings[0].map((_, i) => embeddings.reduce((sum, e) => sum + e[i], 0) / embeddings.length);
|
|
204
|
+
// Average value
|
|
205
|
+
const avgValue = experiences.reduce((sum, e) => sum + e.value, 0) / experiences.length;
|
|
206
|
+
return {
|
|
207
|
+
id: `compressed_${Date.now()}`,
|
|
208
|
+
structure: avgEmbedding,
|
|
209
|
+
weight: avgValue,
|
|
210
|
+
confidence: avgValue,
|
|
211
|
+
origin: 'system',
|
|
212
|
+
timestamp: Date.now(),
|
|
213
|
+
examples: experiences.map(e => e.action)
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
// ==================== SEMANTIC GRAPH ====================
|
|
217
|
+
addToGraph(pattern) {
|
|
218
|
+
this.cortex.semanticGraph.nodes.set(pattern.id, {
|
|
219
|
+
id: pattern.id,
|
|
220
|
+
type: 'pattern',
|
|
221
|
+
embedding: pattern.structure,
|
|
222
|
+
metadata: { confidence: pattern.confidence }
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
removeFromGraph(patternId) {
|
|
226
|
+
this.cortex.semanticGraph.nodes.delete(patternId);
|
|
227
|
+
// Remove edges
|
|
228
|
+
for (const [source, edges] of this.cortex.semanticGraph.edges) {
|
|
229
|
+
this.cortex.semanticGraph.edges.set(source, edges.filter(e => e.target !== patternId));
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
findRelated(pattern) {
|
|
233
|
+
const related = [];
|
|
234
|
+
for (const [id, p] of this.cortex.patterns) {
|
|
235
|
+
if (id !== pattern.id) {
|
|
236
|
+
const similarity = this.cosineSimilarity(pattern.structure, p.structure);
|
|
237
|
+
if (similarity > 0.7) {
|
|
238
|
+
related.push(p);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return related;
|
|
243
|
+
}
|
|
244
|
+
// ==================== UTILITIES ====================
|
|
245
|
+
cosineSimilarity(a, b) {
|
|
246
|
+
if (a.length !== b.length)
|
|
247
|
+
return 0;
|
|
248
|
+
let dotProduct = 0;
|
|
249
|
+
let normA = 0;
|
|
250
|
+
let normB = 0;
|
|
251
|
+
for (let i = 0; i < a.length; i++) {
|
|
252
|
+
dotProduct += a[i] * b[i];
|
|
253
|
+
normA += a[i] * a[i];
|
|
254
|
+
normB += b[i] * b[i];
|
|
255
|
+
}
|
|
256
|
+
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB) || 1);
|
|
257
|
+
}
|
|
258
|
+
computeRecency(pattern) {
|
|
259
|
+
const age = Date.now() - pattern.timestamp;
|
|
260
|
+
const hour = 60 * 60 * 1000;
|
|
261
|
+
return Math.exp(-age / (24 * hour)); // Decay over 24 hours
|
|
262
|
+
}
|
|
263
|
+
recentlyAccessed = new Set();
|
|
264
|
+
wasRecentlyAccessed(id) {
|
|
265
|
+
if (this.recentlyAccessed.has(id)) {
|
|
266
|
+
this.recentlyAccessed.delete(id);
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
markAccessed(id) {
|
|
272
|
+
this.recentlyAccessed.add(id);
|
|
273
|
+
}
|
|
274
|
+
embedExperience(experience) {
|
|
275
|
+
// Simple hash-based embedding for now
|
|
276
|
+
// In production, use actual embeddings
|
|
277
|
+
const hash = this.hashString(`${experience.action}_${experience.outcome}`);
|
|
278
|
+
const embedding = [];
|
|
279
|
+
for (let i = 0; i < 64; i++) {
|
|
280
|
+
embedding.push(((hash >> i) & 1) ? 1 : 0);
|
|
281
|
+
}
|
|
282
|
+
return embedding;
|
|
283
|
+
}
|
|
284
|
+
hashString(str) {
|
|
285
|
+
let hash = 0;
|
|
286
|
+
for (let i = 0; i < str.length; i++) {
|
|
287
|
+
hash = ((hash << 5) - hash) + str.charCodeAt(i);
|
|
288
|
+
hash |= 0;
|
|
289
|
+
}
|
|
290
|
+
return Math.abs(hash);
|
|
291
|
+
}
|
|
292
|
+
// ==================== STATE ====================
|
|
293
|
+
getCortex() {
|
|
294
|
+
return this.cortex;
|
|
295
|
+
}
|
|
296
|
+
getHippocampus() {
|
|
297
|
+
return this.hippocampus;
|
|
298
|
+
}
|
|
299
|
+
getPrefrontal() {
|
|
300
|
+
return this.prefrontal;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
export const createMemory = (options) => new MemorySystem(options);
|
|
304
|
+
//# sourceMappingURL=memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.js","sourceRoot":"","sources":["../../src/core/memory.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,MAAM,OAAO,YAAY;IACf,MAAM,CAAe;IACrB,WAAW,CAAoB;IAC/B,UAAU,CAAmB;IAC7B,SAAS,GAAG,MAAM,CAAC;IACnB,sBAAsB,GAAG,GAAG,CAAC;IAErC,YAAY,OAGX;QACC,gCAAgC;QAChC,IAAI,CAAC,MAAM,GAAG;YACZ,QAAQ,EAAE,IAAI,GAAG,EAAE;YACnB,QAAQ,EAAE,IAAI,GAAG,EAAE;YACnB,aAAa,EAAE;gBACb,KAAK,EAAE,IAAI,GAAG,EAAE;gBAChB,KAAK,EAAE,IAAI,GAAG,EAAE;aACjB;SACF,CAAC;QAEF,uCAAuC;QACvC,IAAI,CAAC,WAAW,GAAG;YACjB,cAAc,EAAE,EAAE;YAClB,MAAM,EAAE,OAAO,EAAE,sBAAsB,IAAI,EAAE;YAC7C,QAAQ,EA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IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACpC,IAAI,GAAG,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;YAChD,IAAI,IAAI,CAAC,CAAC;QACZ,CAAC;QACD,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;IAED,kDAAkD;IAElD,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,cAAc;QACZ,OAAO,IAAI,CAAC,WAAW,CAAC;IAC1B,CAAC;IAED,aAAa;QACX,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;CACF;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,OAG5B,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,OAAO,CAAC,CAAC"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attention Economics & Token Optimization
|
|
3
|
+
*
|
|
4
|
+
* Intelligent token allocation that mimics brain attention.
|
|
5
|
+
* Compresses familiar patterns, expands novel ones.
|
|
6
|
+
*/
|
|
7
|
+
export declare class AttentionEconomics {
|
|
8
|
+
private totalAttention;
|
|
9
|
+
private allocations;
|
|
10
|
+
private history;
|
|
11
|
+
constructor(totalAttention?: number);
|
|
12
|
+
/**
|
|
13
|
+
* Allocate attention across agents based on multiple factors
|
|
14
|
+
*/
|
|
15
|
+
allocate(agents: Array<{
|
|
16
|
+
id: string;
|
|
17
|
+
performance: number;
|
|
18
|
+
reliability: number;
|
|
19
|
+
urgency: number;
|
|
20
|
+
potential: number;
|
|
21
|
+
}>): Map<string, number>;
|
|
22
|
+
/**
|
|
23
|
+
* Get current allocation for an agent
|
|
24
|
+
*/
|
|
25
|
+
getAllocation(agentId: string): number;
|
|
26
|
+
/**
|
|
27
|
+
* Record agent performance for future allocation
|
|
28
|
+
*/
|
|
29
|
+
recordPerformance(agentId: string, value: number): void;
|
|
30
|
+
/**
|
|
31
|
+
* Get average performance
|
|
32
|
+
*/
|
|
33
|
+
getAveragePerformance(agentId: string): number;
|
|
34
|
+
}
|
|
35
|
+
export declare class TokenOptimizer {
|
|
36
|
+
private maxTokens;
|
|
37
|
+
private compressionDictionary;
|
|
38
|
+
private familiarityScores;
|
|
39
|
+
constructor(maxTokens?: number);
|
|
40
|
+
/**
|
|
41
|
+
* Optimize token allocation for a task
|
|
42
|
+
*/
|
|
43
|
+
optimize(task: string, context: string[]): {
|
|
44
|
+
tokens: number;
|
|
45
|
+
strategy: 'compress' | 'expand' | 'standard';
|
|
46
|
+
breakdown: Array<{
|
|
47
|
+
part: string;
|
|
48
|
+
tokens: number;
|
|
49
|
+
}>;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Compress tokens using learned dictionary
|
|
53
|
+
*/
|
|
54
|
+
compress(text: string): string;
|
|
55
|
+
/**
|
|
56
|
+
* Expand compressed tokens back to text
|
|
57
|
+
*/
|
|
58
|
+
expand(compressed: string): string;
|
|
59
|
+
/**
|
|
60
|
+
* Update compression dictionary
|
|
61
|
+
*/
|
|
62
|
+
updateDictionary(phrase: string, token: string): void;
|
|
63
|
+
/**
|
|
64
|
+
* Speculate: generate variations for novel situations
|
|
65
|
+
*/
|
|
66
|
+
speculate(text: string, n?: number): string[];
|
|
67
|
+
/**
|
|
68
|
+
* Rank speculations by potential
|
|
69
|
+
*/
|
|
70
|
+
rankByPotential(variations: string[]): string[];
|
|
71
|
+
private decompose;
|
|
72
|
+
private estimateComplexity;
|
|
73
|
+
private computeRelevance;
|
|
74
|
+
private isFamiliar;
|
|
75
|
+
private isNovel;
|
|
76
|
+
markFamiliar(subtask: string): void;
|
|
77
|
+
markNovel(subtask: string): void;
|
|
78
|
+
}
|
|
79
|
+
export declare class InfiniteContext {
|
|
80
|
+
private attentionBudget;
|
|
81
|
+
private index;
|
|
82
|
+
private attentionRouter;
|
|
83
|
+
constructor(attentionBudget?: number);
|
|
84
|
+
/**
|
|
85
|
+
* Index content for retrieval
|
|
86
|
+
*/
|
|
87
|
+
indexContent(key: string, content: string[]): void;
|
|
88
|
+
/**
|
|
89
|
+
* Think: retrieve relevant context regardless of "distance"
|
|
90
|
+
*/
|
|
91
|
+
think(query: string, maxItems?: number): string[];
|
|
92
|
+
/**
|
|
93
|
+
* Update attention weights
|
|
94
|
+
*/
|
|
95
|
+
updateAttention(key: string, value: number): void;
|
|
96
|
+
}
|
|
97
|
+
export declare const createAttentionEconomics: (totalAttention?: number) => AttentionEconomics;
|
|
98
|
+
export declare const createTokenOptimizer: (maxTokens?: number) => TokenOptimizer;
|
|
99
|
+
export declare const createInfiniteContext: (attentionBudget?: number) => InfiniteContext;
|
|
100
|
+
//# sourceMappingURL=optimize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"optimize.d.ts","sourceRoot":"","sources":["../../src/core/optimize.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,WAAW,CAAkC;IACrD,OAAO,CAAC,OAAO,CAAoC;gBAEvC,cAAc,GAAE,MAAY;IAIxC;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC;QACrB,EAAE,EAAE,MAAM,CAAC;QACX,WAAW,EAAE,MAAM,CAAC;QACpB,WAAW,EAAE,MAAM,CAAC;QACpB,OAAO,EAAE,MAAM,CAAC;QAChB,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;IA6BxB;;OAEG;IACH,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM;IAItC;;OAEG;IACH,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAc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AY;IAEjD;;OAEG;IACH,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI;IAIlD;;OAEG;IACH,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE;IAsCjD;;OAEG;IACH,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;CAGlD;AAID,eAAO,MAAM,wBAAwB,GAAI,iBAAiB,MAAM,uBACxB,CAAC;AAEzC,eAAO,MAAM,oBAAoB,GAAI,YAAY,MAAM,mBACxB,CAAC;AAEhC,eAAO,MAAM,qBAAqB,GAAI,kBAAkB,MAAM,oBACxB,CAAC"}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attention Economics & Token Optimization
|
|
3
|
+
*
|
|
4
|
+
* Intelligent token allocation that mimics brain attention.
|
|
5
|
+
* Compresses familiar patterns, expands novel ones.
|
|
6
|
+
*/
|
|
7
|
+
export class AttentionEconomics {
|
|
8
|
+
totalAttention;
|
|
9
|
+
allocations = new Map();
|
|
10
|
+
history = new Map();
|
|
11
|
+
constructor(totalAttention = 1.0) {
|
|
12
|
+
this.totalAttention = totalAttention;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Allocate attention across agents based on multiple factors
|
|
16
|
+
*/
|
|
17
|
+
allocate(agents) {
|
|
18
|
+
const scores = new Map();
|
|
19
|
+
for (const agent of agents) {
|
|
20
|
+
// Composite score
|
|
21
|
+
const score = 0.4 * agent.performance +
|
|
22
|
+
0.2 * agent.reliability +
|
|
23
|
+
0.2 * agent.urgency +
|
|
24
|
+
0.2 * agent.potential;
|
|
25
|
+
scores.set(agent.id, score);
|
|
26
|
+
}
|
|
27
|
+
// Normalize to conserve attention
|
|
28
|
+
const total = Array.from(scores.values()).reduce((a, b) => a + b, 0);
|
|
29
|
+
if (total === 0) {
|
|
30
|
+
return this.allocations;
|
|
31
|
+
}
|
|
32
|
+
for (const [id, score] of scores) {
|
|
33
|
+
const normalized = (score / total) * this.totalAttention;
|
|
34
|
+
this.allocations.set(id, normalized);
|
|
35
|
+
}
|
|
36
|
+
return this.allocations;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Get current allocation for an agent
|
|
40
|
+
*/
|
|
41
|
+
getAllocation(agentId) {
|
|
42
|
+
return this.allocations.get(agentId) ?? 0;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Record agent performance for future allocation
|
|
46
|
+
*/
|
|
47
|
+
recordPerformance(agentId, value) {
|
|
48
|
+
if (!this.history.has(agentId)) {
|
|
49
|
+
this.history.set(agentId, []);
|
|
50
|
+
}
|
|
51
|
+
const agentHistory = this.history.get(agentId);
|
|
52
|
+
agentHistory.push(value);
|
|
53
|
+
// Keep only last 100 records
|
|
54
|
+
if (agentHistory.length > 100) {
|
|
55
|
+
agentHistory.shift();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Get average performance
|
|
60
|
+
*/
|
|
61
|
+
getAveragePerformance(agentId) {
|
|
62
|
+
const history = this.history.get(agentId);
|
|
63
|
+
if (!history || history.length === 0) {
|
|
64
|
+
return 0.5; // Default
|
|
65
|
+
}
|
|
66
|
+
return history.reduce((a, b) => a + b, 0) / history.length;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// ==================== TOKEN OPTIMIZER ====================
|
|
70
|
+
export class TokenOptimizer {
|
|
71
|
+
maxTokens;
|
|
72
|
+
compressionDictionary = new Map();
|
|
73
|
+
familiarityScores = new Map();
|
|
74
|
+
constructor(maxTokens = 128000) {
|
|
75
|
+
this.maxTokens = maxTokens;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Optimize token allocation for a task
|
|
79
|
+
*/
|
|
80
|
+
optimize(task, context) {
|
|
81
|
+
// Decompose task into subtasks
|
|
82
|
+
const subtasks = this.decompose(task);
|
|
83
|
+
const breakdown = [];
|
|
84
|
+
let totalTokens = 0;
|
|
85
|
+
for (const subtask of subtasks) {
|
|
86
|
+
const complexity = this.estimateComplexity(subtask);
|
|
87
|
+
const relevance = this.computeRelevance(context, subtask);
|
|
88
|
+
let tokens;
|
|
89
|
+
let strategy;
|
|
90
|
+
// Determine strategy
|
|
91
|
+
const isFamiliar = this.isFamiliar(subtask);
|
|
92
|
+
const isNovel = this.isNovel(subtask);
|
|
93
|
+
if (isFamiliar) {
|
|
94
|
+
// Compress: familiar pattern, use dictionary
|
|
95
|
+
tokens = Math.floor(this.maxTokens * complexity * relevance * 0.1);
|
|
96
|
+
strategy = 'compress';
|
|
97
|
+
}
|
|
98
|
+
else if (isNovel) {
|
|
99
|
+
// Expand: novel situation, explore more
|
|
100
|
+
tokens = Math.floor(this.maxTokens * complexity * relevance * 2);
|
|
101
|
+
strategy = 'expand';
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
// Standard allocation
|
|
105
|
+
tokens = Math.floor(this.maxTokens * complexity * relevance);
|
|
106
|
+
strategy = 'standard';
|
|
107
|
+
}
|
|
108
|
+
breakdown.push({ part: subtask, tokens });
|
|
109
|
+
totalTokens += tokens;
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
tokens: Math.min(totalTokens, this.maxTokens),
|
|
113
|
+
strategy: totalTokens < this.maxTokens * 0.5 ? 'compress' :
|
|
114
|
+
totalTokens > this.maxTokens * 0.8 ? 'expand' : 'standard',
|
|
115
|
+
breakdown
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Compress tokens using learned dictionary
|
|
120
|
+
*/
|
|
121
|
+
compress(text) {
|
|
122
|
+
// Simple compression: replace known phrases
|
|
123
|
+
let compressed = text;
|
|
124
|
+
for (const [phrase, token] of this.compressionDictionary) {
|
|
125
|
+
compressed = compressed.replace(new RegExp(phrase, 'g'), token);
|
|
126
|
+
}
|
|
127
|
+
return compressed;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Expand compressed tokens back to text
|
|
131
|
+
*/
|
|
132
|
+
expand(compressed) {
|
|
133
|
+
let expanded = compressed;
|
|
134
|
+
for (const [token, phrase] of this.compressionDictionary) {
|
|
135
|
+
expanded = expanded.replace(new RegExp(token, 'g'), phrase);
|
|
136
|
+
}
|
|
137
|
+
return expanded;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Update compression dictionary
|
|
141
|
+
*/
|
|
142
|
+
updateDictionary(phrase, token) {
|
|
143
|
+
this.compressionDictionary.set(phrase, token);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Speculate: generate variations for novel situations
|
|
147
|
+
*/
|
|
148
|
+
speculate(text, n = 3) {
|
|
149
|
+
// In production, use actual LLM for speculation
|
|
150
|
+
// For now, return variations
|
|
151
|
+
return Array.from({ length: n }, (_, i) => `${text}_variant_${i}`);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Rank speculations by potential
|
|
155
|
+
*/
|
|
156
|
+
rankByPotential(variations) {
|
|
157
|
+
// Simple ranking based on familiarity
|
|
158
|
+
return variations.sort((a, b) => {
|
|
159
|
+
const scoreA = this.familiarityScores.get(a) ?? 0;
|
|
160
|
+
const scoreB = this.familiarityScores.get(b) ?? 0;
|
|
161
|
+
return scoreB - scoreA;
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
// ==================== HELPERS ====================
|
|
165
|
+
decompose(task) {
|
|
166
|
+
// Simple decomposition by splitting on common separators
|
|
167
|
+
return task.split(/[,;]| and | then /).map(s => s.trim()).filter(Boolean);
|
|
168
|
+
}
|
|
169
|
+
estimateComplexity(subtask) {
|
|
170
|
+
// Simple heuristic: longer = more complex
|
|
171
|
+
const words = subtask.split(/\s+/).length;
|
|
172
|
+
return Math.min(1, words / 20);
|
|
173
|
+
}
|
|
174
|
+
computeRelevance(context, subtask) {
|
|
175
|
+
// Simple word overlap
|
|
176
|
+
const subtaskWords = new Set(subtask.toLowerCase().split(/\s+/));
|
|
177
|
+
let overlap = 0;
|
|
178
|
+
for (const ctx of context) {
|
|
179
|
+
const ctxWords = ctx.toLowerCase().split(/\s+/);
|
|
180
|
+
for (const word of ctxWords) {
|
|
181
|
+
if (subtaskWords.has(word)) {
|
|
182
|
+
overlap++;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return Math.min(1, overlap / Math.max(subtaskWords.size, 1));
|
|
187
|
+
}
|
|
188
|
+
isFamiliar(subtask) {
|
|
189
|
+
const score = this.familiarityScores.get(subtask) ?? 0;
|
|
190
|
+
return score > 0.7;
|
|
191
|
+
}
|
|
192
|
+
isNovel(subtask) {
|
|
193
|
+
const score = this.familiarityScores.get(subtask) ?? 0;
|
|
194
|
+
return score < 0.3;
|
|
195
|
+
}
|
|
196
|
+
markFamiliar(subtask) {
|
|
197
|
+
const current = this.familiarityScores.get(subtask) ?? 0;
|
|
198
|
+
this.familiarityScores.set(subtask, Math.min(1, current + 0.1));
|
|
199
|
+
}
|
|
200
|
+
markNovel(subtask) {
|
|
201
|
+
const current = this.familiarityScores.get(subtask) ?? 0;
|
|
202
|
+
this.familiarityScores.set(subtask, Math.max(0, current - 0.1));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// ==================== INFINITE CONTEXT ====================
|
|
206
|
+
export class InfiniteContext {
|
|
207
|
+
attentionBudget;
|
|
208
|
+
index = new Map();
|
|
209
|
+
attentionRouter = new Map();
|
|
210
|
+
constructor(attentionBudget = 100) {
|
|
211
|
+
this.attentionBudget = attentionBudget;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Index content for retrieval
|
|
215
|
+
*/
|
|
216
|
+
indexContent(key, content) {
|
|
217
|
+
this.index.set(key, content);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Think: retrieve relevant context regardless of "distance"
|
|
221
|
+
*/
|
|
222
|
+
think(query, maxItems) {
|
|
223
|
+
const queryWords = new Set(query.toLowerCase().split(/\s+/));
|
|
224
|
+
const scores = [];
|
|
225
|
+
for (const [key, content] of this.index) {
|
|
226
|
+
let score = 0;
|
|
227
|
+
for (const item of content) {
|
|
228
|
+
const itemWords = item.toLowerCase().split(/\s+/);
|
|
229
|
+
for (const word of itemWords) {
|
|
230
|
+
if (queryWords.has(word)) {
|
|
231
|
+
score++;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
// Weight by attention
|
|
236
|
+
const attention = this.attentionRouter.get(key) ?? 1;
|
|
237
|
+
scores.push({ key, score: score * attention });
|
|
238
|
+
}
|
|
239
|
+
// Sort by score
|
|
240
|
+
scores.sort((a, b) => b.score - a.score);
|
|
241
|
+
// Retrieve top-k
|
|
242
|
+
const limit = maxItems ?? Math.min(this.attentionBudget, scores.length);
|
|
243
|
+
const results = [];
|
|
244
|
+
for (let i = 0; i < limit; i++) {
|
|
245
|
+
const content = this.index.get(scores[i].key);
|
|
246
|
+
if (content) {
|
|
247
|
+
results.push(...content);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return results;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Update attention weights
|
|
254
|
+
*/
|
|
255
|
+
updateAttention(key, value) {
|
|
256
|
+
this.attentionRouter.set(key, value);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
// ==================== FACTORY ====================
|
|
260
|
+
export const createAttentionEconomics = (totalAttention) => new AttentionEconomics(totalAttention);
|
|
261
|
+
export const createTokenOptimizer = (maxTokens) => new TokenOptimizer(maxTokens);
|
|
262
|
+
export const createInfiniteContext = (attentionBudget) => new InfiniteContext(attentionBudget);
|
|
263
|
+
//# sourceMappingURL=optimize.js.map
|