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,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Meta-Learner for Adaptive Compression
|
|
3
|
+
*
|
|
4
|
+
* Implements adaptive interpolation and token retention.
|
|
5
|
+
*/
|
|
6
|
+
export class AdaptiveInterpolator {
|
|
7
|
+
w1 = 1.0;
|
|
8
|
+
w2 = 1.0;
|
|
9
|
+
b = 0.0;
|
|
10
|
+
/**
|
|
11
|
+
* Adaptive interpolation: t^{l,l-1} = σ(w1 * mag_ratio + w2 * cos_sim + b)
|
|
12
|
+
* Bounds t to [0.3, 0.7]
|
|
13
|
+
*/
|
|
14
|
+
computeT(features) {
|
|
15
|
+
const { magnitudeRatio, cosineSimilarity } = features;
|
|
16
|
+
// Compute t
|
|
17
|
+
const raw = this.w1 * magnitudeRatio + this.w2 * cosineSimilarity + this.b;
|
|
18
|
+
const t = 1 / (1 + Math.exp(-raw)); // Sigmoid
|
|
19
|
+
// Bound to [0.3, 0.7]
|
|
20
|
+
return Math.max(0.3, Math.min(0.7, t));
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Update weights (simple gradient descent)
|
|
24
|
+
*/
|
|
25
|
+
update(features, targetT, learningRate = 0.01) {
|
|
26
|
+
const predictedT = this.computeT(features);
|
|
27
|
+
const error = targetT - predictedT;
|
|
28
|
+
// Simple weight update
|
|
29
|
+
this.w1 += learningRate * error * features.magnitudeRatio;
|
|
30
|
+
this.w2 += learningRate * error * features.cosineSimilarity;
|
|
31
|
+
this.b += learningRate * error;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Extract features from layer outputs
|
|
35
|
+
*/
|
|
36
|
+
extractFeatures(attentionMatrix, taskQuery, layerIndex, totalLayers, currentLayer, previousLayer) {
|
|
37
|
+
// Entropy of attention matrix
|
|
38
|
+
const entropy = this.computeEntropy(attentionMatrix);
|
|
39
|
+
// Simple task embedding (in production use BERT)
|
|
40
|
+
const taskEmbedding = this.simpleEmbedding(taskQuery);
|
|
41
|
+
// Layer depth
|
|
42
|
+
const layerDepth = layerIndex / totalLayers;
|
|
43
|
+
// Magnitude ratio
|
|
44
|
+
const magRatio = this.magnitudeRatio(currentLayer, previousLayer);
|
|
45
|
+
// Cosine similarity
|
|
46
|
+
const cosSim = this.cosineSimilarity(currentLayer, previousLayer);
|
|
47
|
+
return {
|
|
48
|
+
entropy,
|
|
49
|
+
taskEmbedding,
|
|
50
|
+
layerDepth,
|
|
51
|
+
magnitudeRatio: magRatio,
|
|
52
|
+
cosineSimilarity: cosSim
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
computeEntropy(matrix) {
|
|
56
|
+
// Flatten and compute entropy
|
|
57
|
+
const flat = matrix.flat();
|
|
58
|
+
const sum = flat.reduce((a, b) => a + b, 0);
|
|
59
|
+
const probs = flat.map(x => x / (sum || 1));
|
|
60
|
+
let entropy = 0;
|
|
61
|
+
for (const p of probs) {
|
|
62
|
+
if (p > 0) {
|
|
63
|
+
entropy -= p * Math.log2(p);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return entropy;
|
|
67
|
+
}
|
|
68
|
+
simpleEmbedding(text) {
|
|
69
|
+
// Simple hash-based embedding
|
|
70
|
+
const embedding = new Array(384).fill(0);
|
|
71
|
+
for (let i = 0; i < text.length; i++) {
|
|
72
|
+
embedding[i % 384] += text.charCodeAt(i);
|
|
73
|
+
}
|
|
74
|
+
return embedding.map(x => x / (text.length || 1));
|
|
75
|
+
}
|
|
76
|
+
magnitudeRatio(curr, prev) {
|
|
77
|
+
const magCurr = Math.sqrt(curr.reduce((s, x) => s + x * x, 0));
|
|
78
|
+
const magPrev = Math.sqrt(prev.reduce((s, x) => s + x * x, 0));
|
|
79
|
+
return magPrev > 0 ? magCurr / magPrev : 1;
|
|
80
|
+
}
|
|
81
|
+
cosineSimilarity(a, b) {
|
|
82
|
+
const dot = a.reduce((s, x, i) => s + x * (b[i] || 0), 0);
|
|
83
|
+
const magA = Math.sqrt(a.reduce((s, x) => s + x * x, 0));
|
|
84
|
+
const magB = Math.sqrt(b.reduce((s, x) => s + x * x, 0));
|
|
85
|
+
return dot / ((magA * magB) || 1);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
export class TokenRetention {
|
|
89
|
+
gamma = 0.05;
|
|
90
|
+
setGamma(g) {
|
|
91
|
+
this.gamma = g;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Token Retention Threshold:
|
|
95
|
+
* I = {i | d_i < d_min + (d_max - d_min) * γ}
|
|
96
|
+
* where d_i = (1/π) * arccos(cos_sim_i) is angular distance
|
|
97
|
+
*/
|
|
98
|
+
computeRetentionMask(currentLayer, previousLayer) {
|
|
99
|
+
const mask = [];
|
|
100
|
+
// Compute angular distances
|
|
101
|
+
const distances = [];
|
|
102
|
+
let dMin = Infinity;
|
|
103
|
+
let dMax = -Infinity;
|
|
104
|
+
for (let i = 0; i < currentLayer.length; i++) {
|
|
105
|
+
const cosSim = this.cosineSimilarity(currentLayer[i], previousLayer[i] || currentLayer[i]);
|
|
106
|
+
// Angular distance: d_i = (1/π) * arccos(cos_sim)
|
|
107
|
+
const d = (1 / Math.PI) * Math.acos(Math.max(-1, Math.min(1, cosSim)));
|
|
108
|
+
distances.push(d);
|
|
109
|
+
dMin = Math.min(dMin, d);
|
|
110
|
+
dMax = Math.max(dMax, d);
|
|
111
|
+
}
|
|
112
|
+
// Compute threshold
|
|
113
|
+
const threshold = dMin + (dMax - dMin) * this.gamma;
|
|
114
|
+
// Create mask
|
|
115
|
+
for (const d of distances) {
|
|
116
|
+
mask.push(d < threshold); // Retain if distance < threshold
|
|
117
|
+
}
|
|
118
|
+
return mask;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Get retained tokens
|
|
122
|
+
*/
|
|
123
|
+
getRetained(layer, mask) {
|
|
124
|
+
return layer.filter((_, i) => mask[i]);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Get removed tokens
|
|
128
|
+
*/
|
|
129
|
+
getRemoved(layer, mask) {
|
|
130
|
+
return layer.filter((_, i) => !mask[i]);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Estimate retention ratio
|
|
134
|
+
*/
|
|
135
|
+
getRetentionRatio(mask) {
|
|
136
|
+
const retained = mask.filter(x => x).length;
|
|
137
|
+
return retained / (mask.length || 1);
|
|
138
|
+
}
|
|
139
|
+
cosineSimilarity(a, b) {
|
|
140
|
+
const dot = a.reduce((s, x, i) => s + x * (b[i] || 0), 0);
|
|
141
|
+
const magA = Math.sqrt(a.reduce((s, x) => s + x * x, 0));
|
|
142
|
+
const magB = Math.sqrt(b.reduce((s, x) => s + x * x, 0));
|
|
143
|
+
return dot / ((magA * magB) || 1);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
export class MetaLearner {
|
|
147
|
+
interpolator;
|
|
148
|
+
retention;
|
|
149
|
+
constructor() {
|
|
150
|
+
this.interpolator = new AdaptiveInterpolator();
|
|
151
|
+
this.retention = new TokenRetention();
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Predict compression decision
|
|
155
|
+
*/
|
|
156
|
+
predict(attentionMatrix, taskQuery, layerIndex, totalLayers, currentLayer, previousLayer) {
|
|
157
|
+
// Extract features
|
|
158
|
+
const features = this.interpolator.extractFeatures(attentionMatrix, taskQuery, layerIndex, totalLayers, currentLayer, previousLayer);
|
|
159
|
+
// Compute t (interpolation parameter)
|
|
160
|
+
const t = this.interpolator.computeT(features);
|
|
161
|
+
// Decide whether to merge
|
|
162
|
+
const shouldMerge = features.entropy > 0.5 || t > 0.4;
|
|
163
|
+
// Gamma from layer depth (deeper = more aggressive)
|
|
164
|
+
const gamma = 0.03 + features.layerDepth * 0.04;
|
|
165
|
+
this.retention.setGamma(gamma);
|
|
166
|
+
return {
|
|
167
|
+
shouldMerge,
|
|
168
|
+
t,
|
|
169
|
+
gamma
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Apply compression and get result
|
|
174
|
+
*/
|
|
175
|
+
compress(currentLayer, previousLayer, taskQuery, layerIndex, totalLayers, attentionMatrix) {
|
|
176
|
+
// Get decision
|
|
177
|
+
const decision = this.predict(attentionMatrix, taskQuery, layerIndex, totalLayers, currentLayer.flat(), previousLayer.flat());
|
|
178
|
+
if (!decision.shouldMerge) {
|
|
179
|
+
return {
|
|
180
|
+
merged: currentLayer,
|
|
181
|
+
retained: [],
|
|
182
|
+
removed: [],
|
|
183
|
+
mask: []
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
// Get retention mask
|
|
187
|
+
const mask = this.retention.computeRetentionMask(currentLayer, previousLayer);
|
|
188
|
+
// Get retained and removed
|
|
189
|
+
const retained = this.retention.getRetained(currentLayer, mask);
|
|
190
|
+
const removed = this.retention.getRemoved(currentLayer, mask);
|
|
191
|
+
// Simple merge (in production use SLERP)
|
|
192
|
+
const merged = currentLayer.map((row, i) => row.map((v, j) => v * decision.t + (previousLayer[i]?.[j] || 0) * (1 - decision.t)));
|
|
193
|
+
return {
|
|
194
|
+
merged,
|
|
195
|
+
retained,
|
|
196
|
+
removed,
|
|
197
|
+
mask
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Update from feedback (simple learning)
|
|
202
|
+
*/
|
|
203
|
+
update(features, actualQuality, expectedQuality) {
|
|
204
|
+
// Compute target t based on quality
|
|
205
|
+
const qualityDelta = expectedQuality - actualQuality;
|
|
206
|
+
const targetT = qualityDelta > 0 ? 0.6 : 0.4;
|
|
207
|
+
// Update interpolator
|
|
208
|
+
this.interpolator.update(features, targetT);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
export const createMetaLearner = () => new MetaLearner();
|
|
212
|
+
export const createAdaptiveInterpolator = () => new AdaptiveInterpolator();
|
|
213
|
+
export const createTokenRetention = () => new TokenRetention();
|
|
214
|
+
//# sourceMappingURL=meta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AC,CAAC,CACjE,CACF,CAAC;QAEF,OAAO;YACL,MAAM;YACN,QAAQ;YACR,OAAO;YACP,IAAI;SACL,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,MAAM,CACJ,QAA6B,EAC7B,aAAqB,EACrB,eAAuB;QAEvB,oCAAoC;QACpC,MAAM,YAAY,GAAG,eAAe,GAAG,aAAa,CAAC;QACrD,MAAM,OAAO,GAAG,YAAY,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;QAE7C,sBAAsB;QACtB,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC9C,CAAC;CACF;AAED,MAAM,CAAC,MAAM,iBAAiB,GAAG,GAAG,EAAE,CAAC,IAAI,WAAW,EAAE,CAAC;AACzD,MAAM,CAAC,MAAM,0BAA0B,GAAG,GAAG,EAAE,CAAC,IAAI,oBAAoB,EAAE,CAAC;AAC3E,MAAM,CAAC,MAAM,oBAAoB,GAAG,GAAG,EAAE,CAAC,IAAI,cAAc,EAAE,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Orchestrator Engine
|
|
3
|
+
*
|
|
4
|
+
* Coordinates multiple agents with consensus.
|
|
5
|
+
*/
|
|
6
|
+
import { MemoryEngine } from './memory.js';
|
|
7
|
+
export type AgentType = 'researcher' | 'coder' | 'planner' | 'executor' | 'reviewer';
|
|
8
|
+
export interface Agent {
|
|
9
|
+
id: string;
|
|
10
|
+
type: AgentType;
|
|
11
|
+
task: string;
|
|
12
|
+
state: 'pending' | 'running' | 'complete' | 'failed';
|
|
13
|
+
result?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface Task {
|
|
16
|
+
id: string;
|
|
17
|
+
description: string;
|
|
18
|
+
complexity: number;
|
|
19
|
+
subtasks: string[];
|
|
20
|
+
}
|
|
21
|
+
export declare class OrchestratorEngine {
|
|
22
|
+
private agents;
|
|
23
|
+
private memory;
|
|
24
|
+
private consensusThreshold;
|
|
25
|
+
private agentCounter;
|
|
26
|
+
constructor(memory?: MemoryEngine);
|
|
27
|
+
/**
|
|
28
|
+
* Decompose a task into subtasks
|
|
29
|
+
*/
|
|
30
|
+
decomposeTask(task: string): string[];
|
|
31
|
+
/**
|
|
32
|
+
* Spawn an agent
|
|
33
|
+
*/
|
|
34
|
+
spawn(type: AgentType, task: string): string;
|
|
35
|
+
/**
|
|
36
|
+
* Execute a task with agents
|
|
37
|
+
*/
|
|
38
|
+
execute(task: string): Promise<{
|
|
39
|
+
result: string;
|
|
40
|
+
agents: Agent[];
|
|
41
|
+
consensus: boolean;
|
|
42
|
+
}>;
|
|
43
|
+
/**
|
|
44
|
+
* Run a single agent
|
|
45
|
+
*/
|
|
46
|
+
private runAgent;
|
|
47
|
+
/**
|
|
48
|
+
* Get agent type based on task
|
|
49
|
+
*/
|
|
50
|
+
private getAgentType;
|
|
51
|
+
/**
|
|
52
|
+
* Check consensus among results
|
|
53
|
+
*/
|
|
54
|
+
private checkConsensus;
|
|
55
|
+
/**
|
|
56
|
+
* Aggregate results
|
|
57
|
+
*/
|
|
58
|
+
private aggregateResults;
|
|
59
|
+
/**
|
|
60
|
+
* Simple similarity
|
|
61
|
+
*/
|
|
62
|
+
private similarity;
|
|
63
|
+
/**
|
|
64
|
+
* Get all agents
|
|
65
|
+
*/
|
|
66
|
+
getAgents(): Agent[];
|
|
67
|
+
}
|
|
68
|
+
export declare const createOrchestrator: (memory?: MemoryEngine) => OrchestratorEngine;
|
|
69
|
+
//# sourceMappingURL=orchestrator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orchestrator.d.ts","sourceRoot":"","sources":["../../src/engines/orchestrator.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAI3C,MAAM,MAAM,SAAS,GAAG,YAAY,GAAG,OAAO,GAAG,SAAS,G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|
1
|
+
/**
|
|
2
|
+
* Orchestrator Engine
|
|
3
|
+
*
|
|
4
|
+
* Coordinates multiple agents with consensus.
|
|
5
|
+
*/
|
|
6
|
+
import { MemoryEngine } from './memory.js';
|
|
7
|
+
import * as fs from 'fs';
|
|
8
|
+
import * as path from 'path';
|
|
9
|
+
export class OrchestratorEngine {
|
|
10
|
+
agents = new Map();
|
|
11
|
+
memory;
|
|
12
|
+
consensusThreshold = 0.6;
|
|
13
|
+
agentCounter = 0;
|
|
14
|
+
constructor(memory) {
|
|
15
|
+
this.memory = memory || new MemoryEngine();
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Decompose a task into subtasks
|
|
19
|
+
*/
|
|
20
|
+
decomposeTask(task) {
|
|
21
|
+
// Simple decomposition - split by 'and', 'then'
|
|
22
|
+
const subtasks = task
|
|
23
|
+
.split(/,| and | then /)
|
|
24
|
+
.map(s => s.trim())
|
|
25
|
+
.filter(Boolean);
|
|
26
|
+
return subtasks.length > 0 ? subtasks : [task];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Spawn an agent
|
|
30
|
+
*/
|
|
31
|
+
spawn(type, task) {
|
|
32
|
+
const id = `${type}_${++this.agentCounter}`;
|
|
33
|
+
const agent = {
|
|
34
|
+
id,
|
|
35
|
+
type,
|
|
36
|
+
task,
|
|
37
|
+
state: 'pending'
|
|
38
|
+
};
|
|
39
|
+
this.agents.set(id, agent);
|
|
40
|
+
// Store in memory
|
|
41
|
+
this.memory.store(`Agent ${id} (${type}) created for: ${task}`, 0.5, ['agent', type]);
|
|
42
|
+
return id;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Execute a task with agents
|
|
46
|
+
*/
|
|
47
|
+
async execute(task) {
|
|
48
|
+
// Decompose task
|
|
49
|
+
const subtasks = this.decomposeTask(task);
|
|
50
|
+
// Spawn agents for each subtask
|
|
51
|
+
const agentIds = [];
|
|
52
|
+
for (const subtask of subtasks) {
|
|
53
|
+
const type = this.getAgentType(subtask);
|
|
54
|
+
const id = this.spawn(type, subtask);
|
|
55
|
+
agentIds.push(id);
|
|
56
|
+
}
|
|
57
|
+
// Execute in parallel
|
|
58
|
+
const results = await Promise.all(agentIds.map(id => this.runAgent(id)));
|
|
59
|
+
// Aggregate with consensus
|
|
60
|
+
const consensus = this.checkConsensus(results);
|
|
61
|
+
const aggregated = this.aggregateResults(results);
|
|
62
|
+
return {
|
|
63
|
+
result: aggregated,
|
|
64
|
+
agents: results.map(r => r.agent),
|
|
65
|
+
consensus
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Run a single agent
|
|
70
|
+
*/
|
|
71
|
+
async runAgent(agentId) {
|
|
72
|
+
const agent = this.agents.get(agentId);
|
|
73
|
+
if (!agent) {
|
|
74
|
+
throw new Error(`Agent not found: ${agentId}`);
|
|
75
|
+
}
|
|
76
|
+
// Update state
|
|
77
|
+
agent.state = 'running';
|
|
78
|
+
// Store intent in memory before execution
|
|
79
|
+
this.memory.store(`Agent ${agentId} (${agent.type}) starting: ${agent.task}`, 0.5, ['execution-start', agent.type]);
|
|
80
|
+
// ── Recall relevant context ──
|
|
81
|
+
const recalled = await this.memory.recall(agent.task, 3);
|
|
82
|
+
// ── Scan source files for task-relevant content ──
|
|
83
|
+
const findings = [];
|
|
84
|
+
try {
|
|
85
|
+
const srcDir = path.join(process.cwd(), 'src');
|
|
86
|
+
const entries = fs.readdirSync(srcDir, { withFileTypes: true });
|
|
87
|
+
const taskLower = agent.task.toLowerCase();
|
|
88
|
+
for (const entry of entries) {
|
|
89
|
+
if (!entry.isFile() || !/\.[jt]s$/.test(entry.name))
|
|
90
|
+
continue;
|
|
91
|
+
// Check if file name is relevant to the task
|
|
92
|
+
const nameLower = entry.name.toLowerCase().replace(/\.[^.]+$/, '');
|
|
93
|
+
if (taskLower.includes(nameLower) || nameLower.includes('index')) {
|
|
94
|
+
try {
|
|
95
|
+
const content = fs.readFileSync(path.join(srcDir, entry.name), 'utf-8').slice(0, 300);
|
|
96
|
+
const exports = (content.match(/export\s+(class|function|const|interface)\s+\w+/g) || []);
|
|
97
|
+
findings.push(`${entry.name}: ${exports.length} exports`);
|
|
98
|
+
}
|
|
99
|
+
catch { /* skip unreadable */ }
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
catch { /* no src/ dir */ }
|
|
104
|
+
// ── Build result ──
|
|
105
|
+
const resultParts = [];
|
|
106
|
+
if (recalled.length > 0) {
|
|
107
|
+
resultParts.push(`Context: ${recalled.length} prior memories applied`);
|
|
108
|
+
}
|
|
109
|
+
if (findings.length > 0) {
|
|
110
|
+
resultParts.push(`Analyzed: ${findings.join(', ')}`);
|
|
111
|
+
}
|
|
112
|
+
resultParts.push(`Task: ${agent.task}`);
|
|
113
|
+
const result = resultParts.join(' | ');
|
|
114
|
+
// Update agent
|
|
115
|
+
agent.state = 'complete';
|
|
116
|
+
agent.result = result;
|
|
117
|
+
// Store result in memory
|
|
118
|
+
this.memory.store(result, 0.7, ['result', agent.type]);
|
|
119
|
+
return { agent, result };
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Get agent type based on task
|
|
123
|
+
*/
|
|
124
|
+
getAgentType(task) {
|
|
125
|
+
const t = task.toLowerCase();
|
|
126
|
+
if (t.includes('research') || t.includes('find') || t.includes('search')) {
|
|
127
|
+
return 'researcher';
|
|
128
|
+
}
|
|
129
|
+
if (t.includes('build') || t.includes('create') || t.includes('code')) {
|
|
130
|
+
return 'coder';
|
|
131
|
+
}
|
|
132
|
+
if (t.includes('plan') || t.includes('design')) {
|
|
133
|
+
return 'planner';
|
|
134
|
+
}
|
|
135
|
+
if (t.includes('review') || t.includes('check')) {
|
|
136
|
+
return 'reviewer';
|
|
137
|
+
}
|
|
138
|
+
return 'executor';
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Check consensus among results
|
|
142
|
+
*/
|
|
143
|
+
checkConsensus(results) {
|
|
144
|
+
if (results.length < 2)
|
|
145
|
+
return true;
|
|
146
|
+
// Simple consensus: similar results
|
|
147
|
+
const first = results[0].result;
|
|
148
|
+
const similar = results.filter(r => this.similarity(first, r.result) > this.consensusThreshold);
|
|
149
|
+
return similar.length / results.length >= this.consensusThreshold;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Aggregate results
|
|
153
|
+
*/
|
|
154
|
+
aggregateResults(results) {
|
|
155
|
+
return results.map(r => r.result).join(' | ');
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Simple similarity
|
|
159
|
+
*/
|
|
160
|
+
similarity(a, b) {
|
|
161
|
+
const wordsA = new Set(a.toLowerCase().split(/\s+/));
|
|
162
|
+
const wordsB = new Set(b.toLowerCase().split(/\s+/));
|
|
163
|
+
const intersection = [...wordsA].filter(x => wordsB.has(x));
|
|
164
|
+
const union = new Set([...wordsA, ...wordsB]);
|
|
165
|
+
return intersection.length / (union.size || 1);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Get all agents
|
|
169
|
+
*/
|
|
170
|
+
getAgents() {
|
|
171
|
+
return Array.from(this.agents.values());
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
export const createOrchestrator = (memory) => new OrchestratorEngine(memory);
|
|
175
|
+
//# sourceMappingURL=orchestrator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orchestrator.js","sourceRoot":"","sources":["../../src/engines/orchestrator.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAmB7B,MAAM,OAAO,kBAAkB;IACrB,MAAM,GAAuB,IAAI,GAAG,EAAE,CAAC;IACvC,MAAM,CAAe;IACrB,kBAAkB,GAAG,GAAG,CAAC;IACzB,YAAY,GAAG,CAAC,CAA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C,KAAK,CAAC,CAAC,CAAC;QAErD,MAAM,YAAY,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QAC5D,MAAM,KAAK,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,MAAM,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC;QAE9C,OAAO,YAAY,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC;IACjD,CAAC;IAED;;OAEG;IACH,SAAS;QACP,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;IAC1C,CAAC;CACF;AAED,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,MAAqB,EAAE,EAAE,CAC1D,IAAI,kBAAkB,CAAC,MAAM,CAAC,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface PodMessage {
|
|
2
|
+
id: string;
|
|
3
|
+
workerId: string;
|
|
4
|
+
type: 'observation' | 'fact' | 'instruction';
|
|
5
|
+
content: string;
|
|
6
|
+
timestamp: number;
|
|
7
|
+
tags: string[];
|
|
8
|
+
confidence: number;
|
|
9
|
+
}
|
|
10
|
+
export declare class PODNetwork {
|
|
11
|
+
private messages;
|
|
12
|
+
private subscribers;
|
|
13
|
+
private podPath;
|
|
14
|
+
private pollHandle;
|
|
15
|
+
constructor();
|
|
16
|
+
private loadMessages;
|
|
17
|
+
private saveMessages;
|
|
18
|
+
/** Publish a finding to the network */
|
|
19
|
+
publish(workerId: string, content: string, confidence?: number, tags?: string[]): PodMessage;
|
|
20
|
+
/** Recall relevant POD context for a specific task */
|
|
21
|
+
recall(tags: string[], minConfidence?: number): PodMessage[];
|
|
22
|
+
/** Internal broadcast to active sub-agent listeners */
|
|
23
|
+
private broadcast;
|
|
24
|
+
/** Sub-agents can listen for specific events/topics */
|
|
25
|
+
subscribe(tag: string, callback: (msg: PodMessage) => void): () => void;
|
|
26
|
+
clear(): void;
|
|
27
|
+
/** Stop the poll timer and release resources */
|
|
28
|
+
destroy(): void;
|
|
29
|
+
}
|
|
30
|
+
export declare const podNetwork: PODNetwork;
|
|
31
|
+
//# sourceMappingURL=pod-network.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pod-network.d.ts","sourceRoot":"","sources":["../../src/engines/pod-network.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,UAAU;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,aAAa,GAAG,MAAM,GAAG,aAAa,CAAC;IAC7C,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;CACtB;AAED,qBAAa,UAAU;IACnB,OAAO,CAAC,QAAQ,CAAoB;IACpC,OAAO,CAAC,WAAW,CAA0D;IAC7E,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,UAAU,CAA+C;;IAejE,OAAO,CAAC,YAAY;IAuBpB,OAAO,CAAC,YAAY;IAYpB,uCAAuC;IACvC,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,GAAE,MAAY,EAAE,IAAI,GAAE,MAAM,EAAO,GAAG,UAAU;IAiBrG,sDAAsD;IACtD,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,aAAa,GAAE,MAAY,GAAG,UAAU,EAAE;IAQjE,uDAAuD;IACvD,OAAO,CAAC,SAAS;IAQjB,uDAAuD;IACvD,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,EAAE,UAAU,KAAK,IAAI,GAAG,MAAM,IAAI;IAWvE,KAAK,IAAI,IAAI;IAOb,gDAAgD;IAChD,OAAO,IAAI,IAAI;CAMlB;AAED,eAAO,MAAM,UAAU,YAAmB,CAAC"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import * as os from 'os';
|
|
5
|
+
export class PODNetwork {
|
|
6
|
+
messages = [];
|
|
7
|
+
subscribers = new Map();
|
|
8
|
+
podPath;
|
|
9
|
+
pollHandle = null;
|
|
10
|
+
constructor() {
|
|
11
|
+
this.podPath = path.join(os.homedir(), '.nexus-prime', 'pod.json');
|
|
12
|
+
if (!fs.existsSync(path.dirname(this.podPath))) {
|
|
13
|
+
fs.mkdirSync(path.dirname(this.podPath), { recursive: true });
|
|
14
|
+
}
|
|
15
|
+
this.loadMessages();
|
|
16
|
+
// Basic poll for changes from other workers (cross-process)
|
|
17
|
+
// unref() allows the Node process to exit even if the timer is still active
|
|
18
|
+
this.pollHandle = setInterval(() => this.loadMessages(), 5000);
|
|
19
|
+
this.pollHandle.unref();
|
|
20
|
+
}
|
|
21
|
+
loadMessages() {
|
|
22
|
+
try {
|
|
23
|
+
if (fs.existsSync(this.podPath)) {
|
|
24
|
+
const data = fs.readFileSync(this.podPath, 'utf-8');
|
|
25
|
+
if (!data)
|
|
26
|
+
return;
|
|
27
|
+
const fileMessages = JSON.parse(data);
|
|
28
|
+
// Merge and deduplicate by ID
|
|
29
|
+
const existingIds = new Set(this.messages.map(m => m.id));
|
|
30
|
+
const newMessages = fileMessages.filter(m => !existingIds.has(m.id));
|
|
31
|
+
if (newMessages.length > 0) {
|
|
32
|
+
this.messages.push(...newMessages);
|
|
33
|
+
// Broadcast new findings to local subscribers
|
|
34
|
+
newMessages.forEach(m => this.broadcast(m));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
// Ignore parse/read errors if file is being written
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
saveMessages() {
|
|
43
|
+
try {
|
|
44
|
+
// TTL: Only keep messages from the last 1 hour to prevent file bloat
|
|
45
|
+
const oneHourAgo = Date.now() - (60 * 60 * 1000);
|
|
46
|
+
this.messages = this.messages.filter(m => m.timestamp > oneHourAgo);
|
|
47
|
+
fs.writeFileSync(this.podPath, JSON.stringify(this.messages, null, 2));
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
console.error('Failed to save POD messages:', e);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/** Publish a finding to the network */
|
|
54
|
+
publish(workerId, content, confidence = 0.8, tags = []) {
|
|
55
|
+
const msg = {
|
|
56
|
+
id: randomUUID(),
|
|
57
|
+
workerId,
|
|
58
|
+
content,
|
|
59
|
+
confidence,
|
|
60
|
+
tags,
|
|
61
|
+
timestamp: Date.now(),
|
|
62
|
+
type: tags.includes('#instruction') ? 'instruction' : 'observation'
|
|
63
|
+
};
|
|
64
|
+
this.messages.push(msg);
|
|
65
|
+
this.saveMessages();
|
|
66
|
+
this.broadcast(msg);
|
|
67
|
+
return msg;
|
|
68
|
+
}
|
|
69
|
+
/** Recall relevant POD context for a specific task */
|
|
70
|
+
recall(tags, minConfidence = 0.5) {
|
|
71
|
+
this.loadMessages(); // Refresh from file before recall
|
|
72
|
+
return this.messages.filter(m => m.confidence >= minConfidence &&
|
|
73
|
+
(tags.length === 0 || tags.some(t => m.tags.includes(t)))).sort((a, b) => b.timestamp - a.timestamp);
|
|
74
|
+
}
|
|
75
|
+
/** Internal broadcast to active sub-agent listeners */
|
|
76
|
+
broadcast(msg) {
|
|
77
|
+
for (const tag of msg.tags) {
|
|
78
|
+
this.subscribers.get(tag)?.forEach(cb => cb(msg));
|
|
79
|
+
}
|
|
80
|
+
// Universal subscriber
|
|
81
|
+
this.subscribers.get('*')?.forEach(cb => cb(msg));
|
|
82
|
+
}
|
|
83
|
+
/** Sub-agents can listen for specific events/topics */
|
|
84
|
+
subscribe(tag, callback) {
|
|
85
|
+
if (!this.subscribers.has(tag)) {
|
|
86
|
+
this.subscribers.set(tag, new Set());
|
|
87
|
+
}
|
|
88
|
+
this.subscribers.get(tag).add(callback);
|
|
89
|
+
return () => {
|
|
90
|
+
this.subscribers.get(tag)?.delete(callback);
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
clear() {
|
|
94
|
+
this.messages = [];
|
|
95
|
+
if (fs.existsSync(this.podPath)) {
|
|
96
|
+
fs.unlinkSync(this.podPath);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/** Stop the poll timer and release resources */
|
|
100
|
+
destroy() {
|
|
101
|
+
if (this.pollHandle) {
|
|
102
|
+
clearInterval(this.pollHandle);
|
|
103
|
+
this.pollHandle = null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
export const podNetwork = new PODNetwork();
|
|
108
|
+
//# sourceMappingURL=pod-network.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pod-network.js","sourceRoot":"","sources":["../../src/engines/pod-network.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AAYzB,MAAM,OAAO,UAAU;IACX,QAAQ,GAAiB,EAAE,CAAC;IAC5B,WAAW,GAAgD,IAAI,GAAG,EAAE,CAAC;IACrE,OAAO,CAAS;IAChB,UAAU,GAA0C,IAAI,CAAC;IAEjE;QACI,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,cAAc,EAAE,UAAU,CAAC,CAAC;QACnE,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,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