@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,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Security Module
|
|
4
|
+
* Exports all security-related components
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.DEFAULT_SECURITY_CONFIG = exports.SecurityMiddleware = exports.MessageSanitizer = exports.RateLimiter = exports.AuthManager = void 0;
|
|
8
|
+
var AuthManager_1 = require("./AuthManager");
|
|
9
|
+
Object.defineProperty(exports, "AuthManager", { enumerable: true, get: function () { return AuthManager_1.AuthManager; } });
|
|
10
|
+
var RateLimiter_1 = require("./RateLimiter");
|
|
11
|
+
Object.defineProperty(exports, "RateLimiter", { enumerable: true, get: function () { return RateLimiter_1.RateLimiter; } });
|
|
12
|
+
var MessageSanitizer_1 = require("./MessageSanitizer");
|
|
13
|
+
Object.defineProperty(exports, "MessageSanitizer", { enumerable: true, get: function () { return MessageSanitizer_1.MessageSanitizer; } });
|
|
14
|
+
var SecurityMiddleware_1 = require("./SecurityMiddleware");
|
|
15
|
+
Object.defineProperty(exports, "SecurityMiddleware", { enumerable: true, get: function () { return SecurityMiddleware_1.SecurityMiddleware; } });
|
|
16
|
+
/**
|
|
17
|
+
* Default security configuration
|
|
18
|
+
*/
|
|
19
|
+
exports.DEFAULT_SECURITY_CONFIG = {
|
|
20
|
+
auth: {
|
|
21
|
+
enabled: false, // Disabled by default for development
|
|
22
|
+
requireApiKey: false,
|
|
23
|
+
allowedOrigins: ['*'],
|
|
24
|
+
defaultRateLimit: {
|
|
25
|
+
requestsPerMinute: 60,
|
|
26
|
+
requestsPerHour: 1000,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
rateLimit: {
|
|
30
|
+
enabled: true,
|
|
31
|
+
requestsPerMinute: 60,
|
|
32
|
+
requestsPerHour: 1000,
|
|
33
|
+
blockDuration: 60000, // 1 minute
|
|
34
|
+
},
|
|
35
|
+
sanitization: {
|
|
36
|
+
enabled: true,
|
|
37
|
+
maxStringLength: 10000,
|
|
38
|
+
maxArrayLength: 1000,
|
|
39
|
+
maxObjectDepth: 10,
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/security/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,6CAAgE;AAAvD,0GAAA,WAAW,OAAA;AACpB,6CAA6D;AAApD,0GAAA,WAAW,OAAA;AACpB,uDAA0E;AAAjE,oHAAA,gBAAgB,OAAA;AACzB,2DAA+F;AAAtF,wHAAA,kBAAkB,OAAA;AAE3B;;GAEG;AACU,QAAA,uBAAuB,GAAG;IACrC,IAAI,EAAE;QACJ,OAAO,EAAE,KAAK,EAAE,sCAAsC;QACtD,aAAa,EAAE,KAAK;QACpB,cAAc,EAAE,CAAC,GAAG,CAAC;QACrB,gBAAgB,EAAE;YAChB,iBAAiB,EAAE,EAAE;YACrB,eAAe,EAAE,IAAI;SACtB;KACF;IACD,SAAS,EAAE;QACT,OAAO,EAAE,IAAI;QACb,iBAAiB,EAAE,EAAE;QACrB,eAAe,EAAE,IAAI;QACrB,aAAa,EAAE,KAAK,EAAE,WAAW;KAClC;IACD,YAAY,EAAE;QACZ,OAAO,EAAE,IAAI;QACb,eAAe,EAAE,KAAK;QACtB,cAAc,EAAE,IAAI;QACpB,cAAc,EAAE,EAAE;KACnB;CACF,CAAC"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prediction Engine - Speculative Execution v2.0
|
|
3
|
+
*
|
|
4
|
+
* Analyzes historical request patterns and predicts the next 3 most probable
|
|
5
|
+
* agent requests using lightweight local models (Llama 3.2 1B/3B).
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - Pattern analysis from request history
|
|
9
|
+
* - Confidence scoring (0.0-1.0)
|
|
10
|
+
* - Lightweight model inference
|
|
11
|
+
* - Real-time prediction updates
|
|
12
|
+
*/
|
|
13
|
+
import { EventEmitter } from 'events';
|
|
14
|
+
export interface PredictionRequest {
|
|
15
|
+
taskType: string;
|
|
16
|
+
context: string;
|
|
17
|
+
agentId?: string;
|
|
18
|
+
timestamp: number;
|
|
19
|
+
}
|
|
20
|
+
export interface Prediction {
|
|
21
|
+
id: string;
|
|
22
|
+
taskType: string;
|
|
23
|
+
context: string;
|
|
24
|
+
confidence: number;
|
|
25
|
+
reasoning: string;
|
|
26
|
+
timestamp: number;
|
|
27
|
+
expiresAt: number;
|
|
28
|
+
}
|
|
29
|
+
export interface PredictionEngineConfig {
|
|
30
|
+
ollamaUrl?: string;
|
|
31
|
+
modelName?: string;
|
|
32
|
+
maxPredictions?: number;
|
|
33
|
+
minConfidence?: number;
|
|
34
|
+
historySize?: number;
|
|
35
|
+
predictionTTL?: number;
|
|
36
|
+
}
|
|
37
|
+
export interface PredictionMetrics {
|
|
38
|
+
totalPredictions: number;
|
|
39
|
+
accuratePredictions: number;
|
|
40
|
+
accuracy: number;
|
|
41
|
+
averageConfidence: number;
|
|
42
|
+
predictionTime: number;
|
|
43
|
+
}
|
|
44
|
+
export declare class PredictionEngine extends EventEmitter {
|
|
45
|
+
private config;
|
|
46
|
+
private requestHistory;
|
|
47
|
+
private activePredictions;
|
|
48
|
+
private metrics;
|
|
49
|
+
constructor(config?: PredictionEngineConfig);
|
|
50
|
+
/**
|
|
51
|
+
* Record a new request in the history
|
|
52
|
+
*/
|
|
53
|
+
recordRequest(request: PredictionRequest): void;
|
|
54
|
+
/**
|
|
55
|
+
* Generate predictions based on current history
|
|
56
|
+
*/
|
|
57
|
+
generatePredictions(): Promise<Prediction[]>;
|
|
58
|
+
/**
|
|
59
|
+
* Check if a request matches any active prediction
|
|
60
|
+
*/
|
|
61
|
+
matchPrediction(request: PredictionRequest): Prediction | null;
|
|
62
|
+
/**
|
|
63
|
+
* Analyze patterns in request history
|
|
64
|
+
*/
|
|
65
|
+
private analyzePatterns;
|
|
66
|
+
/**
|
|
67
|
+
* Use lightweight model to predict next requests
|
|
68
|
+
*/
|
|
69
|
+
private predictNextRequests;
|
|
70
|
+
/**
|
|
71
|
+
* Build prompt for prediction model
|
|
72
|
+
*/
|
|
73
|
+
private buildPredictionPrompt;
|
|
74
|
+
/**
|
|
75
|
+
* Parse predictions from model response
|
|
76
|
+
*/
|
|
77
|
+
private parsePredictions;
|
|
78
|
+
/**
|
|
79
|
+
* Calculate similarity score between request and prediction
|
|
80
|
+
*/
|
|
81
|
+
private calculateMatchScore;
|
|
82
|
+
/**
|
|
83
|
+
* Simple text similarity using word overlap
|
|
84
|
+
*/
|
|
85
|
+
private calculateTextSimilarity;
|
|
86
|
+
/**
|
|
87
|
+
* Get current metrics
|
|
88
|
+
*/
|
|
89
|
+
getMetrics(): PredictionMetrics;
|
|
90
|
+
/**
|
|
91
|
+
* Get active predictions
|
|
92
|
+
*/
|
|
93
|
+
getActivePredictions(): Prediction[];
|
|
94
|
+
/**
|
|
95
|
+
* Clear all predictions and history
|
|
96
|
+
*/
|
|
97
|
+
clear(): void;
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=PredictionEngine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PredictionEngine.d.ts","sourceRoot":"","sources":["../../src/speculative/PredictionEngine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAGtC,MAAM,WA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IACH,OAAO,CAAC,eAAe;IAqCvB;;OAEG;YACW,mBAAmB;IAiCjC;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAiC7B;;OAEG;IACH,OAAO,CAAC,gBAAgB;IA0BxB;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAwB3B;;OAEG;IACH,OAAO,CAAC,uBAAuB;IAU/B;;OAEG;IACI,UAAU,IAAI,iBAAiB;IAItC;;OAEG;IACI,oBAAoB,IAAI,UAAU,EAAE;IAO3C;;OAEG;IACI,KAAK,IAAI,IAAI;CAWrB"}
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Prediction Engine - Speculative Execution v2.0
|
|
4
|
+
*
|
|
5
|
+
* Analyzes historical request patterns and predicts the next 3 most probable
|
|
6
|
+
* agent requests using lightweight local models (Llama 3.2 1B/3B).
|
|
7
|
+
*
|
|
8
|
+
* Features:
|
|
9
|
+
* - Pattern analysis from request history
|
|
10
|
+
* - Confidence scoring (0.0-1.0)
|
|
11
|
+
* - Lightweight model inference
|
|
12
|
+
* - Real-time prediction updates
|
|
13
|
+
*/
|
|
14
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
15
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.PredictionEngine = void 0;
|
|
19
|
+
const events_1 = require("events");
|
|
20
|
+
const axios_1 = __importDefault(require("axios"));
|
|
21
|
+
class PredictionEngine extends events_1.EventEmitter {
|
|
22
|
+
constructor(config = {}) {
|
|
23
|
+
super();
|
|
24
|
+
this.requestHistory = [];
|
|
25
|
+
this.activePredictions = new Map();
|
|
26
|
+
this.metrics = {
|
|
27
|
+
totalPredictions: 0,
|
|
28
|
+
accuratePredictions: 0,
|
|
29
|
+
accuracy: 0,
|
|
30
|
+
averageConfidence: 0,
|
|
31
|
+
predictionTime: 0,
|
|
32
|
+
};
|
|
33
|
+
this.config = {
|
|
34
|
+
ollamaUrl: config.ollamaUrl || 'http://localhost:11434',
|
|
35
|
+
modelName: config.modelName || 'llama3.2:1b',
|
|
36
|
+
maxPredictions: config.maxPredictions || 3,
|
|
37
|
+
minConfidence: config.minConfidence || 0.6,
|
|
38
|
+
historySize: config.historySize || 100,
|
|
39
|
+
predictionTTL: config.predictionTTL || 60000,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Record a new request in the history
|
|
44
|
+
*/
|
|
45
|
+
recordRequest(request) {
|
|
46
|
+
this.requestHistory.push(request);
|
|
47
|
+
// Keep history size bounded
|
|
48
|
+
if (this.requestHistory.length > this.config.historySize) {
|
|
49
|
+
this.requestHistory.shift();
|
|
50
|
+
}
|
|
51
|
+
// Trigger new predictions after recording
|
|
52
|
+
this.generatePredictions().catch((error) => {
|
|
53
|
+
this.emit('error', error);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Generate predictions based on current history
|
|
58
|
+
*/
|
|
59
|
+
async generatePredictions() {
|
|
60
|
+
if (this.requestHistory.length < 3) {
|
|
61
|
+
// Not enough history to make predictions
|
|
62
|
+
return [];
|
|
63
|
+
}
|
|
64
|
+
const startTime = Date.now();
|
|
65
|
+
try {
|
|
66
|
+
// Analyze patterns in request history
|
|
67
|
+
const patterns = this.analyzePatterns();
|
|
68
|
+
// Generate predictions using lightweight model
|
|
69
|
+
const predictions = await this.predictNextRequests(patterns);
|
|
70
|
+
// Filter by confidence threshold
|
|
71
|
+
const validPredictions = predictions.filter((p) => p.confidence >= this.config.minConfidence);
|
|
72
|
+
// Store predictions
|
|
73
|
+
validPredictions.forEach((prediction) => {
|
|
74
|
+
this.activePredictions.set(prediction.id, prediction);
|
|
75
|
+
});
|
|
76
|
+
// Update metrics
|
|
77
|
+
this.metrics.totalPredictions += validPredictions.length;
|
|
78
|
+
this.metrics.predictionTime = Date.now() - startTime;
|
|
79
|
+
this.metrics.averageConfidence =
|
|
80
|
+
validPredictions.reduce((sum, p) => sum + p.confidence, 0) /
|
|
81
|
+
validPredictions.length;
|
|
82
|
+
// Emit predictions for worker pool
|
|
83
|
+
this.emit('predictions', validPredictions);
|
|
84
|
+
return validPredictions;
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
this.emit('error', error);
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Check if a request matches any active prediction
|
|
93
|
+
*/
|
|
94
|
+
matchPrediction(request) {
|
|
95
|
+
const now = Date.now();
|
|
96
|
+
// Clean up expired predictions
|
|
97
|
+
for (const [id, prediction] of this.activePredictions.entries()) {
|
|
98
|
+
if (prediction.expiresAt < now) {
|
|
99
|
+
this.activePredictions.delete(id);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// Find best matching prediction
|
|
103
|
+
let bestMatch = null;
|
|
104
|
+
let bestScore = 0;
|
|
105
|
+
for (const prediction of this.activePredictions.values()) {
|
|
106
|
+
const score = this.calculateMatchScore(request, prediction);
|
|
107
|
+
if (score > bestScore && score > 0.8) {
|
|
108
|
+
// 80% similarity threshold
|
|
109
|
+
bestScore = score;
|
|
110
|
+
bestMatch = prediction;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (bestMatch) {
|
|
114
|
+
// Update accuracy metrics
|
|
115
|
+
this.metrics.accuratePredictions++;
|
|
116
|
+
this.metrics.accuracy =
|
|
117
|
+
this.metrics.accuratePredictions / this.metrics.totalPredictions;
|
|
118
|
+
// Remove matched prediction
|
|
119
|
+
this.activePredictions.delete(bestMatch.id);
|
|
120
|
+
this.emit('prediction-hit', { request, prediction: bestMatch, score: bestScore });
|
|
121
|
+
}
|
|
122
|
+
return bestMatch;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Analyze patterns in request history
|
|
126
|
+
*/
|
|
127
|
+
analyzePatterns() {
|
|
128
|
+
const taskTypeCounts = new Map();
|
|
129
|
+
const recentContexts = [];
|
|
130
|
+
const sequences = [];
|
|
131
|
+
// Count task types
|
|
132
|
+
this.requestHistory.forEach((req) => {
|
|
133
|
+
taskTypeCounts.set(req.taskType, (taskTypeCounts.get(req.taskType) || 0) + 1);
|
|
134
|
+
});
|
|
135
|
+
// Get most common task types
|
|
136
|
+
const commonTaskTypes = Array.from(taskTypeCounts.entries())
|
|
137
|
+
.sort((a, b) => b[1] - a[1])
|
|
138
|
+
.slice(0, 5)
|
|
139
|
+
.map(([type]) => type);
|
|
140
|
+
// Get recent contexts (last 10)
|
|
141
|
+
recentContexts.push(...this.requestHistory.slice(-10).map((req) => req.context));
|
|
142
|
+
// Detect sequence patterns (3-request sequences)
|
|
143
|
+
for (let i = 0; i < this.requestHistory.length - 2; i++) {
|
|
144
|
+
sequences.push([
|
|
145
|
+
this.requestHistory[i].taskType,
|
|
146
|
+
this.requestHistory[i + 1].taskType,
|
|
147
|
+
this.requestHistory[i + 2].taskType,
|
|
148
|
+
]);
|
|
149
|
+
}
|
|
150
|
+
return { commonTaskTypes, recentContexts, sequencePatterns: sequences };
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Use lightweight model to predict next requests
|
|
154
|
+
*/
|
|
155
|
+
async predictNextRequests(patterns) {
|
|
156
|
+
const prompt = this.buildPredictionPrompt(patterns);
|
|
157
|
+
try {
|
|
158
|
+
const response = await axios_1.default.post(`${this.config.ollamaUrl}/api/generate`, {
|
|
159
|
+
model: this.config.modelName,
|
|
160
|
+
prompt,
|
|
161
|
+
stream: false,
|
|
162
|
+
options: {
|
|
163
|
+
temperature: 0.7,
|
|
164
|
+
top_p: 0.9,
|
|
165
|
+
num_predict: 500,
|
|
166
|
+
},
|
|
167
|
+
}, {
|
|
168
|
+
timeout: 5000, // 5 second timeout
|
|
169
|
+
});
|
|
170
|
+
const predictions = this.parsePredictions(response.data.response);
|
|
171
|
+
return predictions.slice(0, this.config.maxPredictions);
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
console.error('Prediction generation failed:', error);
|
|
175
|
+
return [];
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Build prompt for prediction model
|
|
180
|
+
*/
|
|
181
|
+
buildPredictionPrompt(patterns) {
|
|
182
|
+
return `You are a predictive AI analyzing agent request patterns.
|
|
183
|
+
|
|
184
|
+
Recent request history:
|
|
185
|
+
- Common task types: ${patterns.commonTaskTypes.join(', ')}
|
|
186
|
+
- Recent contexts: ${patterns.recentContexts.slice(-3).join(' | ')}
|
|
187
|
+
- Detected sequences: ${patterns.sequencePatterns.slice(-3).map((s) => s.join(' → ')).join(', ')}
|
|
188
|
+
|
|
189
|
+
Based on these patterns, predict the next 3 most probable agent requests.
|
|
190
|
+
|
|
191
|
+
For each prediction, provide:
|
|
192
|
+
1. Task type (e.g., "research", "summarize", "analyze")
|
|
193
|
+
2. Context (brief description of what the request might be about)
|
|
194
|
+
3. Confidence (0.0-1.0, how confident you are)
|
|
195
|
+
4. Reasoning (why you think this request is likely)
|
|
196
|
+
|
|
197
|
+
Format your response as JSON array:
|
|
198
|
+
[
|
|
199
|
+
{
|
|
200
|
+
"taskType": "...",
|
|
201
|
+
"context": "...",
|
|
202
|
+
"confidence": 0.0-1.0,
|
|
203
|
+
"reasoning": "..."
|
|
204
|
+
}
|
|
205
|
+
]
|
|
206
|
+
|
|
207
|
+
Respond with ONLY the JSON array, no other text.`;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Parse predictions from model response
|
|
211
|
+
*/
|
|
212
|
+
parsePredictions(response) {
|
|
213
|
+
try {
|
|
214
|
+
// Extract JSON from response
|
|
215
|
+
const jsonMatch = response.match(/\[[\s\S]*\]/);
|
|
216
|
+
if (!jsonMatch) {
|
|
217
|
+
return [];
|
|
218
|
+
}
|
|
219
|
+
const parsed = JSON.parse(jsonMatch[0]);
|
|
220
|
+
const now = Date.now();
|
|
221
|
+
return parsed.map((p, index) => ({
|
|
222
|
+
id: `pred-${now}-${index}`,
|
|
223
|
+
taskType: p.taskType || 'unknown',
|
|
224
|
+
context: p.context || '',
|
|
225
|
+
confidence: Math.min(Math.max(p.confidence || 0, 0), 1),
|
|
226
|
+
reasoning: p.reasoning || '',
|
|
227
|
+
timestamp: now,
|
|
228
|
+
expiresAt: now + this.config.predictionTTL,
|
|
229
|
+
}));
|
|
230
|
+
}
|
|
231
|
+
catch (error) {
|
|
232
|
+
console.error('Failed to parse predictions:', error);
|
|
233
|
+
return [];
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Calculate similarity score between request and prediction
|
|
238
|
+
*/
|
|
239
|
+
calculateMatchScore(request, prediction) {
|
|
240
|
+
let score = 0;
|
|
241
|
+
// Task type exact match (40% weight)
|
|
242
|
+
if (request.taskType === prediction.taskType) {
|
|
243
|
+
score += 0.4;
|
|
244
|
+
}
|
|
245
|
+
// Context similarity (40% weight)
|
|
246
|
+
const contextSimilarity = this.calculateTextSimilarity(request.context, prediction.context);
|
|
247
|
+
score += contextSimilarity * 0.4;
|
|
248
|
+
// Confidence bonus (20% weight)
|
|
249
|
+
score += prediction.confidence * 0.2;
|
|
250
|
+
return score;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Simple text similarity using word overlap
|
|
254
|
+
*/
|
|
255
|
+
calculateTextSimilarity(text1, text2) {
|
|
256
|
+
const words1 = new Set(text1.toLowerCase().split(/\s+/));
|
|
257
|
+
const words2 = new Set(text2.toLowerCase().split(/\s+/));
|
|
258
|
+
const intersection = new Set([...words1].filter((w) => words2.has(w)));
|
|
259
|
+
const union = new Set([...words1, ...words2]);
|
|
260
|
+
return intersection.size / union.size;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Get current metrics
|
|
264
|
+
*/
|
|
265
|
+
getMetrics() {
|
|
266
|
+
return { ...this.metrics };
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Get active predictions
|
|
270
|
+
*/
|
|
271
|
+
getActivePredictions() {
|
|
272
|
+
const now = Date.now();
|
|
273
|
+
return Array.from(this.activePredictions.values()).filter((p) => p.expiresAt > now);
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Clear all predictions and history
|
|
277
|
+
*/
|
|
278
|
+
clear() {
|
|
279
|
+
this.requestHistory = [];
|
|
280
|
+
this.activePredictions.clear();
|
|
281
|
+
this.metrics = {
|
|
282
|
+
totalPredictions: 0,
|
|
283
|
+
accuratePredictions: 0,
|
|
284
|
+
accuracy: 0,
|
|
285
|
+
averageConfidence: 0,
|
|
286
|
+
predictionTime: 0,
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
exports.PredictionEngine = PredictionEngine;
|
|
291
|
+
//# sourceMappingURL=PredictionEngine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PredictionEngine.js","sourceRoot":"","sources":["../../src/speculative/PredictionEngine.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;;;;;AAEH,mCAAsC;AACtC,kDAA0B;AAoC1B,MAAa,gBAAiB,SAAQ,qBAAY;IAYhD,YAAY,SAAiC,EAAE;QAC7C,KAAK,EAAE,CAAC;QAXF,mBAAc,GAAwB,EAAE,CAAC;QACzC,sBAAiB,GAA4B,IAAI,GAAG,EAAE,CAAC;QACvD,YAAO,GAAsB;YACnC,gBAAgB,EAAE,CAAC;YACnB,mBAAmB,EAAE,CAAC;YACtB,QAAQ,EAAE,CAAC;YACX,iBAAiB,EAAE,CAAC;YACpB,cAAc,EAAE,CAAC;SAClB,CAAC;QAIA,IAAI,CAAC,MAAM,GAAG;YACZ,SAAS,EAAE,MAAM,CAAC,SAAS,IAAI,wBAAwB;YACvD,SAAS,EAAE,MAAM,CAAC,SAAS,IAAI,aAAa;YAC5C,cAAc,EAAE,MAAM,CAAC,cAAc,IAAI,CAAC;YAC1C,aAAa,EAAE,MAAM,CAA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B,CAAC;IACJ,CAAC;CACF;AAnVD,4CAmVC"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Speculative Cache - Speculative Execution v2.0
|
|
3
|
+
*
|
|
4
|
+
* Stores pre-computed results from speculative execution with TTL-based expiration.
|
|
5
|
+
* Provides instant (<10ms) cache lookups for predicted requests.
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - TTL-based expiration
|
|
9
|
+
* - Confidence-based prioritization
|
|
10
|
+
* - Memory-efficient storage
|
|
11
|
+
* - Automatic cleanup
|
|
12
|
+
*/
|
|
13
|
+
import { EventEmitter } from 'events';
|
|
14
|
+
import { Prediction } from './PredictionEngine';
|
|
15
|
+
import { WorkerResult } from './WorkerPool';
|
|
16
|
+
export interface CacheEntry {
|
|
17
|
+
id: string;
|
|
18
|
+
prediction: Prediction;
|
|
19
|
+
result: any;
|
|
20
|
+
confidence: number;
|
|
21
|
+
createdAt: number;
|
|
22
|
+
expiresAt: number;
|
|
23
|
+
hits: number;
|
|
24
|
+
lastAccessedAt: number;
|
|
25
|
+
}
|
|
26
|
+
export interface SpeculativeCacheConfig {
|
|
27
|
+
maxSize?: number;
|
|
28
|
+
defaultTTL?: number;
|
|
29
|
+
cleanupInterval?: number;
|
|
30
|
+
maxMemoryMB?: number;
|
|
31
|
+
}
|
|
32
|
+
export interface SpeculativeCacheMetrics {
|
|
33
|
+
size: number;
|
|
34
|
+
hits: number;
|
|
35
|
+
misses: number;
|
|
36
|
+
hitRate: number;
|
|
37
|
+
averageHitLatency: number;
|
|
38
|
+
memoryUsageMB: number;
|
|
39
|
+
evictions: number;
|
|
40
|
+
}
|
|
41
|
+
export declare class SpeculativeCache extends EventEmitter {
|
|
42
|
+
private config;
|
|
43
|
+
private cache;
|
|
44
|
+
private cleanupTimer?;
|
|
45
|
+
private metrics;
|
|
46
|
+
private hitLatencies;
|
|
47
|
+
constructor(config?: SpeculativeCacheConfig);
|
|
48
|
+
/**
|
|
49
|
+
* Start the cache
|
|
50
|
+
*/
|
|
51
|
+
start(): void;
|
|
52
|
+
/**
|
|
53
|
+
* Store a pre-computed result
|
|
54
|
+
*/
|
|
55
|
+
set(result: WorkerResult): void;
|
|
56
|
+
/**
|
|
57
|
+
* Get a cached result
|
|
58
|
+
*/
|
|
59
|
+
get(taskType: string, context: string): CacheEntry | null;
|
|
60
|
+
/**
|
|
61
|
+
* Find best matching entry using fuzzy matching
|
|
62
|
+
*/
|
|
63
|
+
private findBestMatch;
|
|
64
|
+
/**
|
|
65
|
+
* Calculate match score between request and cached prediction
|
|
66
|
+
*/
|
|
67
|
+
private calculateMatchScore;
|
|
68
|
+
/**
|
|
69
|
+
* Simple text similarity using word overlap
|
|
70
|
+
*/
|
|
71
|
+
private calculateTextSimilarity;
|
|
72
|
+
/**
|
|
73
|
+
* Generate cache key from prediction
|
|
74
|
+
*/
|
|
75
|
+
private generateKey;
|
|
76
|
+
/**
|
|
77
|
+
* Generate cache key from request
|
|
78
|
+
*/
|
|
79
|
+
private generateKeyFromRequest;
|
|
80
|
+
/**
|
|
81
|
+
* Simple string hash function
|
|
82
|
+
*/
|
|
83
|
+
private hashString;
|
|
84
|
+
/**
|
|
85
|
+
* Evict low priority entries
|
|
86
|
+
*/
|
|
87
|
+
private evictLowPriority;
|
|
88
|
+
/**
|
|
89
|
+
* Cleanup expired entries
|
|
90
|
+
*/
|
|
91
|
+
private cleanup;
|
|
92
|
+
/**
|
|
93
|
+
* Estimate memory usage in MB
|
|
94
|
+
*/
|
|
95
|
+
private getMemoryUsageMB;
|
|
96
|
+
/**
|
|
97
|
+
* Update metrics
|
|
98
|
+
*/
|
|
99
|
+
private updateMetrics;
|
|
100
|
+
/**
|
|
101
|
+
* Get current metrics
|
|
102
|
+
*/
|
|
103
|
+
getMetrics(): SpeculativeCacheMetrics;
|
|
104
|
+
/**
|
|
105
|
+
* Get all entries
|
|
106
|
+
*/
|
|
107
|
+
getEntries(): CacheEntry[];
|
|
108
|
+
/**
|
|
109
|
+
* Clear all entries
|
|
110
|
+
*/
|
|
111
|
+
clear(): void;
|
|
112
|
+
/**
|
|
113
|
+
* Stop the cache
|
|
114
|
+
*/
|
|
115
|
+
stop(): void;
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=SpeculativeCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SpeculativeCache.d.ts","sourceRoot":"","sources":["../../src/speculative/SpeculativeCache.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,UAAU,CAAC;IACvB,MAAM,EAAE,GAAG,CAAC;IACZ,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,cAAc,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,sBAAsB;IACrC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,uBAAuB;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,aAAa,EAAE,MAAM,CAAC;IACtB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,qBAAa,gBAAiB,SAAQ,YAAY;IAChD,OAAO,CAAC,MAAM,CAAmC;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