@nxuss/lemma 0.1.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/LICENSE +21 -0
- package/README.md +311 -0
- package/dist/cloud/KeyManager.d.ts +29 -0
- package/dist/cloud/KeyManager.d.ts.map +1 -0
- package/dist/cloud/KeyManager.js +142 -0
- package/dist/cloud/KeyManager.js.map +1 -0
- package/dist/cloud/TenantCache.d.ts +29 -0
- package/dist/cloud/TenantCache.d.ts.map +1 -0
- package/dist/cloud/TenantCache.js +162 -0
- package/dist/cloud/TenantCache.js.map +1 -0
- package/dist/cloud/index.d.ts +4 -0
- package/dist/cloud/index.d.ts.map +1 -0
- package/dist/cloud/index.js +23 -0
- package/dist/cloud/index.js.map +1 -0
- package/dist/cloud/server.d.ts +17 -0
- package/dist/cloud/server.d.ts.map +1 -0
- package/dist/cloud/server.js +189 -0
- package/dist/cloud/server.js.map +1 -0
- package/dist/cloud/types.d.ts +35 -0
- package/dist/cloud/types.d.ts.map +1 -0
- package/dist/cloud/types.js +14 -0
- package/dist/cloud/types.js.map +1 -0
- package/dist/config/index.d.ts +44 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +161 -0
- package/dist/config/index.js.map +1 -0
- package/dist/consensus/ConsensusEngine.d.ts +119 -0
- package/dist/consensus/ConsensusEngine.d.ts.map +1 -0
- package/dist/consensus/ConsensusEngine.js +314 -0
- package/dist/consensus/ConsensusEngine.js.map +1 -0
- package/dist/consensus/ModelPool.d.ts +103 -0
- package/dist/consensus/ModelPool.d.ts.map +1 -0
- package/dist/consensus/ModelPool.js +276 -0
- package/dist/consensus/ModelPool.js.map +1 -0
- package/dist/consensus/index.d.ts +8 -0
- package/dist/consensus/index.d.ts.map +1 -0
- package/dist/consensus/index.js +13 -0
- package/dist/consensus/index.js.map +1 -0
- package/dist/core/AgentRegistry.d.ts +89 -0
- package/dist/core/AgentRegistry.d.ts.map +1 -0
- package/dist/core/AgentRegistry.js +264 -0
- package/dist/core/AgentRegistry.js.map +1 -0
- package/dist/core/DashboardBroadcaster.d.ts +67 -0
- package/dist/core/DashboardBroadcaster.d.ts.map +1 -0
- package/dist/core/DashboardBroadcaster.js +264 -0
- package/dist/core/DashboardBroadcaster.js.map +1 -0
- package/dist/core/OrchestrationEngine.d.ts +84 -0
- package/dist/core/OrchestrationEngine.d.ts.map +1 -0
- package/dist/core/OrchestrationEngine.js +455 -0
- package/dist/core/OrchestrationEngine.js.map +1 -0
- package/dist/core/SubconsciousEngine.d.ts +53 -0
- package/dist/core/SubconsciousEngine.d.ts.map +1 -0
- package/dist/core/SubconsciousEngine.js +273 -0
- package/dist/core/SubconsciousEngine.js.map +1 -0
- package/dist/core/WebSocketServer.d.ts +89 -0
- package/dist/core/WebSocketServer.d.ts.map +1 -0
- package/dist/core/WebSocketServer.js +416 -0
- package/dist/core/WebSocketServer.js.map +1 -0
- package/dist/core/index.d.ts +15 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +24 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/router.d.ts +105 -0
- package/dist/core/router.d.ts.map +1 -0
- package/dist/core/router.js +420 -0
- package/dist/core/router.js.map +1 -0
- package/dist/embed/index.d.ts +153 -0
- package/dist/embed/index.d.ts.map +1 -0
- package/dist/embed/index.js +408 -0
- package/dist/embed/index.js.map +1 -0
- package/dist/embed.d.ts +11 -0
- package/dist/embed.d.ts.map +1 -0
- package/dist/embed.js +19 -0
- package/dist/embed.js.map +1 -0
- package/dist/index.d.ts +83 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +176 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol/flows.d.ts +126 -0
- package/dist/protocol/flows.d.ts.map +1 -0
- package/dist/protocol/flows.js +347 -0
- package/dist/protocol/flows.js.map +1 -0
- package/dist/protocol/iap.d.ts +54 -0
- package/dist/protocol/iap.d.ts.map +1 -0
- package/dist/protocol/iap.js +108 -0
- package/dist/protocol/iap.js.map +1 -0
- package/dist/protocol/index.d.ts +23 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +45 -0
- package/dist/protocol/index.js.map +1 -0
- package/dist/protocol/types.d.ts +332 -0
- package/dist/protocol/types.d.ts.map +1 -0
- package/dist/protocol/types.js +57 -0
- package/dist/protocol/types.js.map +1 -0
- package/dist/protocol/utils.d.ts +130 -0
- package/dist/protocol/utils.d.ts.map +1 -0
- package/dist/protocol/utils.js +292 -0
- package/dist/protocol/utils.js.map +1 -0
- package/dist/protocol/validators.d.ts +54 -0
- package/dist/protocol/validators.d.ts.map +1 -0
- package/dist/protocol/validators.js +344 -0
- package/dist/protocol/validators.js.map +1 -0
- package/dist/security/AuthManager.d.ts +73 -0
- package/dist/security/AuthManager.d.ts.map +1 -0
- package/dist/security/AuthManager.js +102 -0
- package/dist/security/AuthManager.js.map +1 -0
- package/dist/security/MessageSanitizer.d.ts +51 -0
- package/dist/security/MessageSanitizer.d.ts.map +1 -0
- package/dist/security/MessageSanitizer.js +166 -0
- package/dist/security/MessageSanitizer.js.map +1 -0
- package/dist/security/RateLimiter.d.ts +46 -0
- package/dist/security/RateLimiter.d.ts.map +1 -0
- package/dist/security/RateLimiter.js +133 -0
- package/dist/security/RateLimiter.js.map +1 -0
- package/dist/security/SecurityMiddleware.d.ts +88 -0
- package/dist/security/SecurityMiddleware.d.ts.map +1 -0
- package/dist/security/SecurityMiddleware.js +146 -0
- package/dist/security/SecurityMiddleware.js.map +1 -0
- package/dist/security/index.d.ts +35 -0
- package/dist/security/index.d.ts.map +1 -0
- package/dist/security/index.js +42 -0
- package/dist/security/index.js.map +1 -0
- package/dist/speculative/PredictionEngine.d.ts +99 -0
- package/dist/speculative/PredictionEngine.d.ts.map +1 -0
- package/dist/speculative/PredictionEngine.js +291 -0
- package/dist/speculative/PredictionEngine.js.map +1 -0
- package/dist/speculative/SpeculativeCache.d.ts +117 -0
- package/dist/speculative/SpeculativeCache.d.ts.map +1 -0
- package/dist/speculative/SpeculativeCache.js +292 -0
- package/dist/speculative/SpeculativeCache.js.map +1 -0
- package/dist/speculative/SpeculativeEngine.d.ts +114 -0
- package/dist/speculative/SpeculativeEngine.d.ts.map +1 -0
- package/dist/speculative/SpeculativeEngine.js +244 -0
- package/dist/speculative/SpeculativeEngine.js.map +1 -0
- package/dist/speculative/WorkerPool.d.ts +109 -0
- package/dist/speculative/WorkerPool.d.ts.map +1 -0
- package/dist/speculative/WorkerPool.js +327 -0
- package/dist/speculative/WorkerPool.js.map +1 -0
- package/dist/speculative/index.d.ts +10 -0
- package/dist/speculative/index.d.ts.map +1 -0
- package/dist/speculative/index.js +17 -0
- package/dist/speculative/index.js.map +1 -0
- package/dist/subconscious/EmbeddingService.d.ts +73 -0
- package/dist/subconscious/EmbeddingService.d.ts.map +1 -0
- package/dist/subconscious/EmbeddingService.js +196 -0
- package/dist/subconscious/EmbeddingService.js.map +1 -0
- package/dist/subconscious/SemanticCache.d.ts +82 -0
- package/dist/subconscious/SemanticCache.d.ts.map +1 -0
- package/dist/subconscious/SemanticCache.js +164 -0
- package/dist/subconscious/SemanticCache.js.map +1 -0
- package/dist/subconscious/SubconsciousEngine.d.ts +121 -0
- package/dist/subconscious/SubconsciousEngine.d.ts.map +1 -0
- package/dist/subconscious/SubconsciousEngine.js +241 -0
- package/dist/subconscious/SubconsciousEngine.js.map +1 -0
- package/dist/subconscious/VectorStore.d.ts +54 -0
- package/dist/subconscious/VectorStore.d.ts.map +1 -0
- package/dist/subconscious/VectorStore.js +168 -0
- package/dist/subconscious/VectorStore.js.map +1 -0
- package/dist/subconscious/cache.d.ts +34 -0
- package/dist/subconscious/cache.d.ts.map +1 -0
- package/dist/subconscious/cache.js +156 -0
- package/dist/subconscious/cache.js.map +1 -0
- package/dist/subconscious/embeddings.d.ts +25 -0
- package/dist/subconscious/embeddings.d.ts.map +1 -0
- package/dist/subconscious/embeddings.js +65 -0
- package/dist/subconscious/embeddings.js.map +1 -0
- package/dist/subconscious/index.d.ts +12 -0
- package/dist/subconscious/index.d.ts.map +1 -0
- package/dist/subconscious/index.js +19 -0
- package/dist/subconscious/index.js.map +1 -0
- package/dist/types/index.d.ts +286 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +43 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/logger.d.ts +63 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +126 -0
- package/dist/utils/logger.js.map +1 -0
- package/package.json +99 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker Pool - Speculative Execution v2.0
|
|
3
|
+
*
|
|
4
|
+
* Manages Node.js Worker Threads for parallel pre-computation of predicted requests.
|
|
5
|
+
* Each worker executes a prediction in the background without blocking the main thread.
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - Dynamic worker allocation
|
|
9
|
+
* - Resource management
|
|
10
|
+
* - Task queuing
|
|
11
|
+
* - Graceful shutdown
|
|
12
|
+
*/
|
|
13
|
+
import { EventEmitter } from 'events';
|
|
14
|
+
import { Prediction } from './PredictionEngine';
|
|
15
|
+
export interface WorkerTask {
|
|
16
|
+
id: string;
|
|
17
|
+
prediction: Prediction;
|
|
18
|
+
agentId?: string;
|
|
19
|
+
priority: number;
|
|
20
|
+
createdAt: number;
|
|
21
|
+
}
|
|
22
|
+
export interface WorkerResult {
|
|
23
|
+
taskId: string;
|
|
24
|
+
prediction: Prediction;
|
|
25
|
+
result: any;
|
|
26
|
+
executionTime: number;
|
|
27
|
+
success: boolean;
|
|
28
|
+
error?: string;
|
|
29
|
+
}
|
|
30
|
+
export interface WorkerPoolConfig {
|
|
31
|
+
maxWorkers?: number;
|
|
32
|
+
workerScript?: string;
|
|
33
|
+
taskTimeout?: number;
|
|
34
|
+
maxQueueSize?: number;
|
|
35
|
+
}
|
|
36
|
+
export interface WorkerPoolMetrics {
|
|
37
|
+
activeWorkers: number;
|
|
38
|
+
idleWorkers: number;
|
|
39
|
+
queuedTasks: number;
|
|
40
|
+
completedTasks: number;
|
|
41
|
+
failedTasks: number;
|
|
42
|
+
averageExecutionTime: number;
|
|
43
|
+
cpuUsage: number;
|
|
44
|
+
}
|
|
45
|
+
export declare class WorkerPool extends EventEmitter {
|
|
46
|
+
private config;
|
|
47
|
+
private workers;
|
|
48
|
+
private taskQueue;
|
|
49
|
+
private nextWorkerId;
|
|
50
|
+
private metrics;
|
|
51
|
+
constructor(config?: WorkerPoolConfig);
|
|
52
|
+
/**
|
|
53
|
+
* Initialize the worker pool
|
|
54
|
+
*/
|
|
55
|
+
initialize(): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Submit a task for execution
|
|
58
|
+
*/
|
|
59
|
+
submitTask(task: WorkerTask): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Submit multiple tasks from predictions
|
|
62
|
+
*/
|
|
63
|
+
submitPredictions(predictions: Prediction[]): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Create a new worker
|
|
66
|
+
*/
|
|
67
|
+
private createWorker;
|
|
68
|
+
/**
|
|
69
|
+
* Assign tasks to available workers
|
|
70
|
+
*/
|
|
71
|
+
private assignTasks;
|
|
72
|
+
/**
|
|
73
|
+
* Assign a task to a specific worker
|
|
74
|
+
*/
|
|
75
|
+
private assignTaskToWorker;
|
|
76
|
+
/**
|
|
77
|
+
* Handle message from worker
|
|
78
|
+
*/
|
|
79
|
+
private handleWorkerMessage;
|
|
80
|
+
/**
|
|
81
|
+
* Handle worker error
|
|
82
|
+
*/
|
|
83
|
+
private handleWorkerError;
|
|
84
|
+
/**
|
|
85
|
+
* Handle worker exit
|
|
86
|
+
*/
|
|
87
|
+
private handleWorkerExit;
|
|
88
|
+
/**
|
|
89
|
+
* Handle task timeout
|
|
90
|
+
*/
|
|
91
|
+
private handleTaskTimeout;
|
|
92
|
+
/**
|
|
93
|
+
* Terminate a worker
|
|
94
|
+
*/
|
|
95
|
+
private terminateWorker;
|
|
96
|
+
/**
|
|
97
|
+
* Update metrics
|
|
98
|
+
*/
|
|
99
|
+
private updateMetrics;
|
|
100
|
+
/**
|
|
101
|
+
* Get current metrics
|
|
102
|
+
*/
|
|
103
|
+
getMetrics(): WorkerPoolMetrics;
|
|
104
|
+
/**
|
|
105
|
+
* Shutdown the worker pool
|
|
106
|
+
*/
|
|
107
|
+
shutdown(): Promise<void>;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=WorkerPool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WorkerPool.d.ts","sourceRoot":"","sources":["../../src/speculative/WorkerPool.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAEhD,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,UAAU,CAAC;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,UAAU,CAAC;IACvB,MAAM,EAAE,GAAG,CAAC;IACZ,aAAa,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,gBAAgB;IAC/B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,iBAAiB;IAChC,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,EAAE,MAAM,CAAC;IACpB,oBAAoB,EAAE,MAAM,CAAC;IAC7B,QAAQ,EAAE,MAAM,CAAC;CAClB;AAWD,qBAAa,UAAW,SAAQ,YAAY;IAC1C,OAAO,CAAC,MAAM,CAA6B;IAC3C,OAAO,CAAC,OAAO,CAAsC;IACrD,OAAO,CAAC,SAAS,CAAoB;IACrC,OAAO,CAAC,YAAY,CAAK;IACzB,OAAO,CAAC,OAAO,CAQb;gBAEU,MAAM,GAAE,gBAAqB;IAYzC;;OAEG;IACU,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAYxC;;OAEG;IACU,UAAU,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAoBxD;;OAEG;IACU,iBAAiB,CAAC,WAAW,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAaxE;;OAEG;IACH,OAAO,CAAC,YAAY;IAwCpB;;OAEG;IACH,OAAO,CAAC,WAAW;IA2BnB;;OAEG;IACH,OAAO,CAAC,kBAAkB;IA4B1B;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAkC3B;;OAEG;IACH,OAAO,CAAC,iBAAiB;IA0BzB;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAWxB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAmBzB;;OAEG;IACH,OAAO,CAAC,eAAe;IAiBvB;;OAEG;IACH,OAAO,CAAC,aAAa;IAqBrB;;OAEG;IACI,UAAU,IAAI,iBAAiB;IAKtC;;OAEG;IACU,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CA0BvC"}
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Worker Pool - Speculative Execution v2.0
|
|
4
|
+
*
|
|
5
|
+
* Manages Node.js Worker Threads for parallel pre-computation of predicted requests.
|
|
6
|
+
* Each worker executes a prediction in the background without blocking the main thread.
|
|
7
|
+
*
|
|
8
|
+
* Features:
|
|
9
|
+
* - Dynamic worker allocation
|
|
10
|
+
* - Resource management
|
|
11
|
+
* - Task queuing
|
|
12
|
+
* - Graceful shutdown
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.WorkerPool = void 0;
|
|
16
|
+
const worker_threads_1 = require("worker_threads");
|
|
17
|
+
const events_1 = require("events");
|
|
18
|
+
class WorkerPool extends events_1.EventEmitter {
|
|
19
|
+
constructor(config = {}) {
|
|
20
|
+
super();
|
|
21
|
+
this.workers = new Map();
|
|
22
|
+
this.taskQueue = [];
|
|
23
|
+
this.nextWorkerId = 0;
|
|
24
|
+
this.metrics = {
|
|
25
|
+
activeWorkers: 0,
|
|
26
|
+
idleWorkers: 0,
|
|
27
|
+
queuedTasks: 0,
|
|
28
|
+
completedTasks: 0,
|
|
29
|
+
failedTasks: 0,
|
|
30
|
+
averageExecutionTime: 0,
|
|
31
|
+
cpuUsage: 0,
|
|
32
|
+
};
|
|
33
|
+
const cpuCount = require('os').cpus().length;
|
|
34
|
+
this.config = {
|
|
35
|
+
maxWorkers: config.maxWorkers || Math.max(1, cpuCount - 1),
|
|
36
|
+
workerScript: config.workerScript || require.resolve('./worker.js'),
|
|
37
|
+
taskTimeout: config.taskTimeout || 30000,
|
|
38
|
+
maxQueueSize: config.maxQueueSize || 100,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Initialize the worker pool
|
|
43
|
+
*/
|
|
44
|
+
async initialize() {
|
|
45
|
+
// Create initial workers
|
|
46
|
+
for (let i = 0; i < Math.min(2, this.config.maxWorkers); i++) {
|
|
47
|
+
this.createWorker();
|
|
48
|
+
}
|
|
49
|
+
this.emit('initialized', {
|
|
50
|
+
maxWorkers: this.config.maxWorkers,
|
|
51
|
+
initialWorkers: this.workers.size,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Submit a task for execution
|
|
56
|
+
*/
|
|
57
|
+
async submitTask(task) {
|
|
58
|
+
// Check queue size
|
|
59
|
+
if (this.taskQueue.length >= this.config.maxQueueSize) {
|
|
60
|
+
// Remove lowest priority task
|
|
61
|
+
this.taskQueue.sort((a, b) => b.priority - a.priority);
|
|
62
|
+
const removed = this.taskQueue.pop();
|
|
63
|
+
this.emit('task-dropped', removed);
|
|
64
|
+
}
|
|
65
|
+
// Add to queue
|
|
66
|
+
this.taskQueue.push(task);
|
|
67
|
+
this.metrics.queuedTasks = this.taskQueue.length;
|
|
68
|
+
// Sort by priority (confidence)
|
|
69
|
+
this.taskQueue.sort((a, b) => b.priority - a.priority);
|
|
70
|
+
// Try to assign to available worker
|
|
71
|
+
this.assignTasks();
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Submit multiple tasks from predictions
|
|
75
|
+
*/
|
|
76
|
+
async submitPredictions(predictions) {
|
|
77
|
+
const tasks = predictions.map((prediction) => ({
|
|
78
|
+
id: prediction.id,
|
|
79
|
+
prediction,
|
|
80
|
+
priority: prediction.confidence,
|
|
81
|
+
createdAt: Date.now(),
|
|
82
|
+
}));
|
|
83
|
+
for (const task of tasks) {
|
|
84
|
+
await this.submitTask(task);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Create a new worker
|
|
89
|
+
*/
|
|
90
|
+
createWorker() {
|
|
91
|
+
const workerId = this.nextWorkerId++;
|
|
92
|
+
const worker = new worker_threads_1.Worker(this.config.workerScript, {
|
|
93
|
+
workerData: {
|
|
94
|
+
workerId,
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
const workerInfo = {
|
|
98
|
+
id: workerId,
|
|
99
|
+
worker,
|
|
100
|
+
busy: false,
|
|
101
|
+
tasksCompleted: 0,
|
|
102
|
+
totalExecutionTime: 0,
|
|
103
|
+
};
|
|
104
|
+
// Handle worker messages
|
|
105
|
+
worker.on('message', (message) => {
|
|
106
|
+
this.handleWorkerMessage(workerId, message);
|
|
107
|
+
});
|
|
108
|
+
// Handle worker errors
|
|
109
|
+
worker.on('error', (error) => {
|
|
110
|
+
this.handleWorkerError(workerId, error);
|
|
111
|
+
});
|
|
112
|
+
// Handle worker exit
|
|
113
|
+
worker.on('exit', (code) => {
|
|
114
|
+
this.handleWorkerExit(workerId, code);
|
|
115
|
+
});
|
|
116
|
+
this.workers.set(workerId, workerInfo);
|
|
117
|
+
this.updateMetrics();
|
|
118
|
+
this.emit('worker-created', { workerId });
|
|
119
|
+
return workerInfo;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Assign tasks to available workers
|
|
123
|
+
*/
|
|
124
|
+
assignTasks() {
|
|
125
|
+
// Find idle workers
|
|
126
|
+
const idleWorkers = Array.from(this.workers.values()).filter((w) => !w.busy);
|
|
127
|
+
// Assign tasks to idle workers
|
|
128
|
+
while (this.taskQueue.length > 0 && idleWorkers.length > 0) {
|
|
129
|
+
const task = this.taskQueue.shift();
|
|
130
|
+
const worker = idleWorkers.shift();
|
|
131
|
+
this.assignTaskToWorker(worker, task);
|
|
132
|
+
}
|
|
133
|
+
// Create new workers if needed
|
|
134
|
+
if (this.taskQueue.length > 0 &&
|
|
135
|
+
this.workers.size < this.config.maxWorkers) {
|
|
136
|
+
const newWorker = this.createWorker();
|
|
137
|
+
if (this.taskQueue.length > 0) {
|
|
138
|
+
const task = this.taskQueue.shift();
|
|
139
|
+
this.assignTaskToWorker(newWorker, task);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
this.updateMetrics();
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Assign a task to a specific worker
|
|
146
|
+
*/
|
|
147
|
+
assignTaskToWorker(workerInfo, task) {
|
|
148
|
+
workerInfo.busy = true;
|
|
149
|
+
workerInfo.currentTask = task;
|
|
150
|
+
// Send task to worker
|
|
151
|
+
workerInfo.worker.postMessage({
|
|
152
|
+
type: 'execute',
|
|
153
|
+
task: {
|
|
154
|
+
id: task.id,
|
|
155
|
+
prediction: task.prediction,
|
|
156
|
+
agentId: task.agentId,
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
// Set timeout
|
|
160
|
+
const timeout = setTimeout(() => {
|
|
161
|
+
this.handleTaskTimeout(workerInfo.id, task);
|
|
162
|
+
}, this.config.taskTimeout);
|
|
163
|
+
// Store timeout for cleanup
|
|
164
|
+
workerInfo.timeout = timeout;
|
|
165
|
+
this.emit('task-assigned', {
|
|
166
|
+
workerId: workerInfo.id,
|
|
167
|
+
taskId: task.id,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Handle message from worker
|
|
172
|
+
*/
|
|
173
|
+
handleWorkerMessage(workerId, message) {
|
|
174
|
+
const workerInfo = this.workers.get(workerId);
|
|
175
|
+
if (!workerInfo)
|
|
176
|
+
return;
|
|
177
|
+
// Clear timeout
|
|
178
|
+
if (workerInfo.timeout) {
|
|
179
|
+
clearTimeout(workerInfo.timeout);
|
|
180
|
+
delete workerInfo.timeout;
|
|
181
|
+
}
|
|
182
|
+
if (message.type === 'result') {
|
|
183
|
+
const result = message.result;
|
|
184
|
+
// Update worker stats
|
|
185
|
+
workerInfo.busy = false;
|
|
186
|
+
workerInfo.tasksCompleted++;
|
|
187
|
+
workerInfo.totalExecutionTime += result.executionTime;
|
|
188
|
+
workerInfo.currentTask = undefined;
|
|
189
|
+
// Update metrics
|
|
190
|
+
if (result.success) {
|
|
191
|
+
this.metrics.completedTasks++;
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
this.metrics.failedTasks++;
|
|
195
|
+
}
|
|
196
|
+
// Emit result
|
|
197
|
+
this.emit('task-completed', result);
|
|
198
|
+
// Assign next task
|
|
199
|
+
this.assignTasks();
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Handle worker error
|
|
204
|
+
*/
|
|
205
|
+
handleWorkerError(workerId, error) {
|
|
206
|
+
const workerInfo = this.workers.get(workerId);
|
|
207
|
+
if (!workerInfo)
|
|
208
|
+
return;
|
|
209
|
+
this.emit('worker-error', {
|
|
210
|
+
workerId,
|
|
211
|
+
error: error.message,
|
|
212
|
+
task: workerInfo.currentTask,
|
|
213
|
+
});
|
|
214
|
+
// Mark task as failed
|
|
215
|
+
if (workerInfo.currentTask) {
|
|
216
|
+
this.metrics.failedTasks++;
|
|
217
|
+
this.emit('task-failed', {
|
|
218
|
+
taskId: workerInfo.currentTask.id,
|
|
219
|
+
error: error.message,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
// Terminate and recreate worker
|
|
223
|
+
this.terminateWorker(workerId);
|
|
224
|
+
if (this.workers.size < this.config.maxWorkers) {
|
|
225
|
+
this.createWorker();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Handle worker exit
|
|
230
|
+
*/
|
|
231
|
+
handleWorkerExit(workerId, code) {
|
|
232
|
+
const workerInfo = this.workers.get(workerId);
|
|
233
|
+
if (!workerInfo)
|
|
234
|
+
return;
|
|
235
|
+
this.emit('worker-exited', { workerId, code });
|
|
236
|
+
// Remove worker
|
|
237
|
+
this.workers.delete(workerId);
|
|
238
|
+
this.updateMetrics();
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Handle task timeout
|
|
242
|
+
*/
|
|
243
|
+
handleTaskTimeout(workerId, task) {
|
|
244
|
+
const workerInfo = this.workers.get(workerId);
|
|
245
|
+
if (!workerInfo)
|
|
246
|
+
return;
|
|
247
|
+
this.emit('task-timeout', {
|
|
248
|
+
workerId,
|
|
249
|
+
taskId: task.id,
|
|
250
|
+
});
|
|
251
|
+
// Mark task as failed
|
|
252
|
+
this.metrics.failedTasks++;
|
|
253
|
+
// Terminate and recreate worker
|
|
254
|
+
this.terminateWorker(workerId);
|
|
255
|
+
if (this.workers.size < this.config.maxWorkers) {
|
|
256
|
+
this.createWorker();
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Terminate a worker
|
|
261
|
+
*/
|
|
262
|
+
terminateWorker(workerId) {
|
|
263
|
+
const workerInfo = this.workers.get(workerId);
|
|
264
|
+
if (!workerInfo)
|
|
265
|
+
return;
|
|
266
|
+
// Clear timeout
|
|
267
|
+
if (workerInfo.timeout) {
|
|
268
|
+
clearTimeout(workerInfo.timeout);
|
|
269
|
+
}
|
|
270
|
+
// Terminate worker
|
|
271
|
+
workerInfo.worker.terminate();
|
|
272
|
+
// Remove from map
|
|
273
|
+
this.workers.delete(workerId);
|
|
274
|
+
this.updateMetrics();
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Update metrics
|
|
278
|
+
*/
|
|
279
|
+
updateMetrics() {
|
|
280
|
+
const activeWorkers = Array.from(this.workers.values()).filter((w) => w.busy).length;
|
|
281
|
+
const idleWorkers = this.workers.size - activeWorkers;
|
|
282
|
+
let totalExecutionTime = 0;
|
|
283
|
+
let totalTasks = 0;
|
|
284
|
+
for (const worker of this.workers.values()) {
|
|
285
|
+
totalExecutionTime += worker.totalExecutionTime;
|
|
286
|
+
totalTasks += worker.tasksCompleted;
|
|
287
|
+
}
|
|
288
|
+
this.metrics.activeWorkers = activeWorkers;
|
|
289
|
+
this.metrics.idleWorkers = idleWorkers;
|
|
290
|
+
this.metrics.queuedTasks = this.taskQueue.length;
|
|
291
|
+
this.metrics.averageExecutionTime =
|
|
292
|
+
totalTasks > 0 ? totalExecutionTime / totalTasks : 0;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Get current metrics
|
|
296
|
+
*/
|
|
297
|
+
getMetrics() {
|
|
298
|
+
this.updateMetrics();
|
|
299
|
+
return { ...this.metrics };
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Shutdown the worker pool
|
|
303
|
+
*/
|
|
304
|
+
async shutdown() {
|
|
305
|
+
// Clear queue
|
|
306
|
+
this.taskQueue = [];
|
|
307
|
+
// Terminate all workers
|
|
308
|
+
const terminationPromises = Array.from(this.workers.keys()).map((workerId) => {
|
|
309
|
+
return new Promise((resolve) => {
|
|
310
|
+
const workerInfo = this.workers.get(workerId);
|
|
311
|
+
if (workerInfo) {
|
|
312
|
+
workerInfo.worker.once('exit', () => resolve());
|
|
313
|
+
workerInfo.worker.terminate();
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
resolve();
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
await Promise.all(terminationPromises);
|
|
321
|
+
this.workers.clear();
|
|
322
|
+
this.updateMetrics();
|
|
323
|
+
this.emit('shutdown');
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
exports.WorkerPool = WorkerPool;
|
|
327
|
+
//# sourceMappingURL=WorkerPool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WorkerPool.js","sourceRoot":"","sources":["../../src/speculative/WorkerPool.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;;AAEH,mDAAwC;AACxC,mCAAsC;AA8CtC,MAAa,UAAW,SAAQ,qBAAY;IAe1C,YAAY,SAA2B,EAAE;QACvC,KAAK,EAAE,CAAC;QAdF,YAAO,GAA4B,IAAI,GAAG,EAAE,CAAC;QAC7C,cAAS,GAAiB,EAAE,CAAC;QAC7B,iBAAY,GAAG,CAAC,CAAC;QACjB,YAAO,GAAsB;YACnC,aAAa,EAAE,CAAC;YAChB,WAAW,EAAE,CAAC;YACd,WAAW,EAAE,CAAC;YACd,cAAc,EAAE,CAAC;YACjB,WAAW,EAAE,CAAC;YACd,oBAAoB,EAAE,CAAC;YACvB,QAAQ,EAAE,CAAC;SACZ,CAAC;QAKA,MAAM,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC,MAAM,CAAC;QAC7C,IAAI,CAAC,MAAM,GAAG;YACZ,UAAU,EAAE,MAAM,CAAC,UAAU,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,QAAQ,GAAG,CA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+
* Exports all speculative execution components for Phase 3.
|
|
5
|
+
*/
|
|
6
|
+
export { PredictionEngine, PredictionRequest, Prediction, PredictionEngineConfig, PredictionMetrics } from './PredictionEngine';
|
|
7
|
+
export { WorkerPool, WorkerTask, WorkerResult, WorkerPoolConfig, WorkerPoolMetrics } from './WorkerPool';
|
|
8
|
+
export { SpeculativeCache, CacheEntry, SpeculativeCacheConfig, SpeculativeCacheMetrics } from './SpeculativeCache';
|
|
9
|
+
export { SpeculativeEngine, SpeculativeEngineConfig, SpeculativeResult, SpeculativeMetrics } from './SpeculativeEngine';
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/speculative/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,UAAU,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAChI,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACzG,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,sBAAsB,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AACnH,OAAO,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Speculative Execution Module - v2.0
|
|
4
|
+
*
|
|
5
|
+
* Exports all speculative execution components for Phase 3.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.SpeculativeEngine = exports.SpeculativeCache = exports.WorkerPool = exports.PredictionEngine = void 0;
|
|
9
|
+
var PredictionEngine_1 = require("./PredictionEngine");
|
|
10
|
+
Object.defineProperty(exports, "PredictionEngine", { enumerable: true, get: function () { return PredictionEngine_1.PredictionEngine; } });
|
|
11
|
+
var WorkerPool_1 = require("./WorkerPool");
|
|
12
|
+
Object.defineProperty(exports, "WorkerPool", { enumerable: true, get: function () { return WorkerPool_1.WorkerPool; } });
|
|
13
|
+
var SpeculativeCache_1 = require("./SpeculativeCache");
|
|
14
|
+
Object.defineProperty(exports, "SpeculativeCache", { enumerable: true, get: function () { return SpeculativeCache_1.SpeculativeCache; } });
|
|
15
|
+
var SpeculativeEngine_1 = require("./SpeculativeEngine");
|
|
16
|
+
Object.defineProperty(exports, "SpeculativeEngine", { enumerable: true, get: function () { return SpeculativeEngine_1.SpeculativeEngine; } });
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/speculative/index.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAEH,uDAAgI;AAAvH,oHAAA,gBAAgB,OAAA;AACzB,2CAAyG;AAAhG,wGAAA,UAAU,OAAA;AACnB,uDAAmH;AAA1G,oHAAA,gBAAgB,OAAA;AACzB,yDAAwH;AAA/G,sHAAA,iBAAiB,OAAA"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EmbeddingService handles communication with Ollama API for generating embeddings.
|
|
3
|
+
* Includes caching, error handling, and retry logic.
|
|
4
|
+
*/
|
|
5
|
+
export declare class EmbeddingService {
|
|
6
|
+
private client;
|
|
7
|
+
private model;
|
|
8
|
+
private cache;
|
|
9
|
+
private maxRetries;
|
|
10
|
+
private retryDelay;
|
|
11
|
+
constructor(ollamaUrl?: string, model?: string, maxRetries?: number, retryDelay?: number);
|
|
12
|
+
/**
|
|
13
|
+
* Generate an embedding vector for the given text.
|
|
14
|
+
* Uses cache if available, otherwise calls Ollama API.
|
|
15
|
+
*
|
|
16
|
+
* @param text - Text to generate embedding for
|
|
17
|
+
* @param useCache - Whether to use cached embeddings (default true)
|
|
18
|
+
* @returns Embedding vector as array of numbers
|
|
19
|
+
*/
|
|
20
|
+
generateEmbedding(text: string, useCache?: boolean): Promise<number[]>;
|
|
21
|
+
/**
|
|
22
|
+
* Generate embeddings for multiple texts in batch.
|
|
23
|
+
*
|
|
24
|
+
* @param texts - Array of texts to generate embeddings for
|
|
25
|
+
* @param useCache - Whether to use cached embeddings (default true)
|
|
26
|
+
* @returns Array of embedding vectors
|
|
27
|
+
*/
|
|
28
|
+
generateEmbeddings(texts: string[], useCache?: boolean): Promise<number[][]>;
|
|
29
|
+
/**
|
|
30
|
+
* Call Ollama API to generate embedding.
|
|
31
|
+
*
|
|
32
|
+
* @param text - Text to generate embedding for
|
|
33
|
+
* @returns Embedding vector
|
|
34
|
+
*/
|
|
35
|
+
private callOllamaAPI;
|
|
36
|
+
/**
|
|
37
|
+
* Generate a cache key from text.
|
|
38
|
+
* Uses a simple hash to avoid storing full text as keys.
|
|
39
|
+
*/
|
|
40
|
+
private getCacheKey;
|
|
41
|
+
/**
|
|
42
|
+
* Sleep for specified milliseconds.
|
|
43
|
+
*/
|
|
44
|
+
private sleep;
|
|
45
|
+
/**
|
|
46
|
+
* Clear the embedding cache.
|
|
47
|
+
*/
|
|
48
|
+
clearCache(): void;
|
|
49
|
+
/**
|
|
50
|
+
* Get cache statistics.
|
|
51
|
+
*/
|
|
52
|
+
getCacheStats(): {
|
|
53
|
+
size: number;
|
|
54
|
+
maxSize: number;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Check if Ollama is available and the model is loaded.
|
|
58
|
+
*/
|
|
59
|
+
healthCheck(): Promise<{
|
|
60
|
+
available: boolean;
|
|
61
|
+
model: string;
|
|
62
|
+
error?: string;
|
|
63
|
+
}>;
|
|
64
|
+
/**
|
|
65
|
+
* Calculate cosine similarity between two embedding vectors.
|
|
66
|
+
*
|
|
67
|
+
* @param vecA - First embedding vector
|
|
68
|
+
* @param vecB - Second embedding vector
|
|
69
|
+
* @returns Similarity score between 0 and 1
|
|
70
|
+
*/
|
|
71
|
+
static cosineSimilarity(vecA: number[], vecB: number[]): number;
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=EmbeddingService.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EmbeddingService.d.ts","sourceRoot":"","sources":["../../src/subconscious/EmbeddingService.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,KAAK,CAAwB;IACrC,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,UAAU,CAAS;gBAGzB,SAAS,GAAE,MAAiC,EAC5C,KAAK,GAAE,MAAiB,EACxB,UAAU,GAAE,MAAU,EACtB,UAAU,GAAE,MAAa;IAe3B;;;;;;;OAOG;IACG,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,GAAE,OAAc,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IA6ClF;;;;;;OAMG;IACG,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,QAAQ,GAAE,OAAc,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;IAWxF;;;;;OAKG;YACW,aAAa;IAgC3B;;;OAGG;IACH,OAAO,CAAC,WAAW;IAWnB;;OAEG;IACH,OAAO,CAAC,KAAK;IAIb;;OAEG;IACH,UAAU,IAAI,IAAI;IAKlB;;OAEG;IACH,aAAa,IAAI;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE;IAOlD;;OAEG;IACG,WAAW,IAAI,OAAO,CAAC;QAAE,SAAS,EAAE,OAAO,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAiBnF;;;;;;OAMG;IACH,MAAM,CAAC,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM;CAsBhE"}
|