@ultrst-armada/kloks 1.11.0 → 1.13.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.
Files changed (44) hide show
  1. package/.krusbl-release-attestation.json +3 -3
  2. package/package.json +36 -1
  3. package/packages/backend/dist/AITransportClient-dToGr8nj.d.ts +30 -0
  4. package/packages/backend/dist/ai-inference/index.d.ts +99 -0
  5. package/packages/backend/dist/ai-inference/index.js +21 -0
  6. package/packages/backend/dist/ai-keys/index.d.ts +88 -0
  7. package/packages/backend/dist/ai-keys/index.js +16 -0
  8. package/packages/backend/dist/ai-models/index.d.ts +85 -0
  9. package/packages/backend/dist/ai-models/index.js +24 -0
  10. package/packages/backend/dist/ai-provider/index.d.ts +32 -0
  11. package/packages/backend/dist/ai-provider/index.js +23 -0
  12. package/packages/backend/dist/ai-routing/index.d.ts +84 -0
  13. package/packages/backend/dist/ai-routing/index.js +21 -0
  14. package/packages/backend/dist/ai-tools/index.d.ts +80 -0
  15. package/packages/backend/dist/ai-tools/index.js +16 -0
  16. package/packages/backend/dist/chunk-2WT7OUHW.js +51 -0
  17. package/packages/backend/dist/chunk-4IYKKD53.js +178 -0
  18. package/packages/backend/dist/chunk-6GQNYRD6.js +32 -0
  19. package/packages/backend/dist/chunk-AM2PLPNE.js +157 -0
  20. package/packages/backend/dist/chunk-ERDBD4TH.js +137 -0
  21. package/packages/backend/dist/chunk-GCTQY24J.js +195 -0
  22. package/packages/backend/dist/chunk-IN3WRHXE.js +0 -0
  23. package/packages/backend/dist/chunk-JSQZAFBU.js +153 -0
  24. package/packages/backend/dist/chunk-LV55WKZR.js +0 -0
  25. package/packages/backend/dist/chunk-MAMK4GDH.js +73 -0
  26. package/packages/backend/dist/chunk-NITVHSB3.js +225 -0
  27. package/packages/backend/dist/chunk-NSKVYL74.js +74 -0
  28. package/packages/backend/dist/chunk-SLR6SIEU.js +77 -0
  29. package/packages/backend/dist/chunk-WVDSMTW3.js +0 -0
  30. package/packages/backend/dist/chunk-Z2FHVSNF.js +42 -0
  31. package/packages/backend/dist/chunk-Z3LXEA6P.js +45 -0
  32. package/packages/backend/dist/index.d.ts +9 -0
  33. package/packages/backend/dist/index.js +230 -103
  34. package/packages/backend/dist/krnl-ai-services.d.ts +71 -0
  35. package/packages/backend/dist/krnl-ai-services.js +38 -0
  36. package/packages/backend/dist/krnl-services.js +4 -4
  37. package/packages/backend/dist/storage/index.js +7 -7
  38. package/packages/backend/dist/types-2p2AhboE.d.ts +33 -0
  39. package/packages/backend/package.json +36 -1
  40. package/packages/contracts/dist/index.d.ts +33 -1
  41. package/packages/services/dist/index.d.ts +3 -1
  42. package/packages/services/dist/index.js +133 -6
  43. package/packages/state-graph/dist/index.js +14 -14
  44. package/packages/backend/dist/{chunk-UMEO6XS4.js → chunk-T26VJ22Y.js} +12 -12
@@ -0,0 +1,16 @@
1
+ import {
2
+ validateToolInput
3
+ } from "../chunk-6GQNYRD6.js";
4
+ import {
5
+ MCPEngine,
6
+ ToolManagerService,
7
+ mcpEngine,
8
+ toolManagerService
9
+ } from "../chunk-NSKVYL74.js";
10
+ export {
11
+ MCPEngine,
12
+ ToolManagerService,
13
+ mcpEngine,
14
+ toolManagerService,
15
+ validateToolInput
16
+ };
@@ -0,0 +1,51 @@
1
+ import {
2
+ AITransportClient
3
+ } from "./chunk-MAMK4GDH.js";
4
+ import {
5
+ getProviderMeta
6
+ } from "./chunk-JSQZAFBU.js";
7
+
8
+ // src/ai-models/endpointRefresher.ts
9
+ var EndpointRefresher = class {
10
+ client;
11
+ constructor(client) {
12
+ this.client = client ?? new AITransportClient();
13
+ }
14
+ async fetchRemoteModels(providerId, apiKey) {
15
+ const meta = getProviderMeta(providerId);
16
+ if (!meta) throw new Error(`Unknown provider ${providerId}`);
17
+ const url = `${meta.baseUrl}${meta.modelsEndpoint}`;
18
+ const headers = {};
19
+ if (meta.defaultHeaderKey === "Authorization") {
20
+ headers["Authorization"] = `Bearer ${apiKey.trim()}`;
21
+ } else {
22
+ headers[meta.defaultHeaderKey] = apiKey.trim();
23
+ }
24
+ const res = await this.client.request({
25
+ url,
26
+ method: "GET",
27
+ headers,
28
+ timeoutMs: 12e3
29
+ });
30
+ if (!res.ok) {
31
+ throw new Error(`Failed to fetch models from ${providerId}: HTTP ${res.status}`);
32
+ }
33
+ const rawList = res.data.data || res.data.models || [];
34
+ return rawList.map((item) => ({
35
+ id: item.id || item.name || "",
36
+ name: item.display_name || item.name || item.id || "",
37
+ providerId,
38
+ contextWindow: item.context_window || item.input_token_limit || 131072,
39
+ supportsTools: true,
40
+ supportsVision: Boolean(item.architecture?.modality?.includes("image")),
41
+ supportsJson: true,
42
+ status: "active"
43
+ }));
44
+ }
45
+ };
46
+ var endpointRefresher = new EndpointRefresher();
47
+
48
+ export {
49
+ EndpointRefresher,
50
+ endpointRefresher
51
+ };
@@ -0,0 +1,178 @@
1
+ import {
2
+ getProviderMeta
3
+ } from "./chunk-JSQZAFBU.js";
4
+
5
+ // src/ai-keys/keyValidator.ts
6
+ async function validateApiKey(providerId, apiKey) {
7
+ const meta = getProviderMeta(providerId);
8
+ const start = Date.now();
9
+ if (!apiKey || apiKey.trim().length === 0) {
10
+ return {
11
+ valid: false,
12
+ status: "invalid",
13
+ error: "Empty API key provided",
14
+ latencyMs: 0
15
+ };
16
+ }
17
+ if (!meta) {
18
+ return {
19
+ valid: true,
20
+ status: "valid",
21
+ latencyMs: 0
22
+ };
23
+ }
24
+ try {
25
+ const url = `${meta.baseUrl}${meta.modelsEndpoint}`;
26
+ const headers = {};
27
+ if (meta.defaultHeaderKey === "Authorization") {
28
+ headers["Authorization"] = `Bearer ${apiKey.trim()}`;
29
+ } else {
30
+ headers[meta.defaultHeaderKey] = apiKey.trim();
31
+ }
32
+ const controller = new AbortController();
33
+ const timer = setTimeout(() => controller.abort(), 8e3);
34
+ const res = await fetch(url, {
35
+ method: "GET",
36
+ headers,
37
+ signal: controller.signal
38
+ }).finally(() => clearTimeout(timer));
39
+ const latencyMs = Date.now() - start;
40
+ if (res.status === 401 || res.status === 403) {
41
+ return {
42
+ valid: false,
43
+ status: "invalid",
44
+ error: `Authentication failed (HTTP ${res.status})`,
45
+ latencyMs
46
+ };
47
+ }
48
+ if (res.status === 429) {
49
+ return {
50
+ valid: false,
51
+ status: "rate-limited",
52
+ error: "Rate limit or quota exceeded",
53
+ latencyMs
54
+ };
55
+ }
56
+ return {
57
+ valid: res.status < 500,
58
+ status: "valid",
59
+ latencyMs
60
+ };
61
+ } catch (err) {
62
+ return {
63
+ valid: false,
64
+ status: "invalid",
65
+ error: err instanceof Error ? err.message : String(err),
66
+ latencyMs: Date.now() - start
67
+ };
68
+ }
69
+ }
70
+
71
+ // src/ai-keys/keysStorage.ts
72
+ var KeysStorage = class {
73
+ inMemoryKeys = /* @__PURE__ */ new Map();
74
+ loadKeys() {
75
+ return Array.from(this.inMemoryKeys.values());
76
+ }
77
+ saveKey(entry) {
78
+ this.inMemoryKeys.set(entry.id, entry);
79
+ }
80
+ saveKeys(entries) {
81
+ for (const e of entries) {
82
+ this.inMemoryKeys.set(e.id, e);
83
+ }
84
+ }
85
+ deleteKey(id) {
86
+ this.inMemoryKeys.delete(id);
87
+ }
88
+ clear() {
89
+ this.inMemoryKeys.clear();
90
+ }
91
+ };
92
+ var keysStorage = new KeysStorage();
93
+
94
+ // src/ai-keys/keyStore.ts
95
+ var KeyStoreService = class {
96
+ strategy = "lowest-latency";
97
+ roundRobinIndices = /* @__PURE__ */ new Map();
98
+ getKeys(providerId) {
99
+ const all = keysStorage.loadKeys();
100
+ if (!providerId) return all;
101
+ return all.filter((k) => k.providerId === providerId);
102
+ }
103
+ getKey(id) {
104
+ return keysStorage.loadKeys().find((k) => k.id === id);
105
+ }
106
+ getActiveKey(providerId) {
107
+ const valid = this.getKeys(providerId).filter(
108
+ (k) => k.isActive && (k.status === "active" || k.status === "valid")
109
+ );
110
+ if (valid.length === 0) return void 0;
111
+ if (this.strategy === "lowest-latency") {
112
+ return [...valid].sort((a, b) => a.lastLatencyMs - b.lastLatencyMs)[0];
113
+ }
114
+ if (this.strategy === "round-robin") {
115
+ const idx = this.roundRobinIndices.get(providerId) ?? 0;
116
+ const nextIdx = (idx + 1) % valid.length;
117
+ this.roundRobinIndices.set(providerId, nextIdx);
118
+ return valid[idx % valid.length];
119
+ }
120
+ if (this.strategy === "failover-priority") {
121
+ return [...valid].sort((a, b) => (b.weight ?? 1) - (a.weight ?? 1))[0];
122
+ }
123
+ return valid[0];
124
+ }
125
+ setStrategy(strategy) {
126
+ this.strategy = strategy;
127
+ }
128
+ getStrategy() {
129
+ return this.strategy;
130
+ }
131
+ addKey(entry) {
132
+ const full = {
133
+ id: `key_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`,
134
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
135
+ totalCalls: 0,
136
+ successfulCalls: 0,
137
+ failedCalls: 0,
138
+ lastLatencyMs: 0,
139
+ errorCount: 0,
140
+ ...entry
141
+ };
142
+ keysStorage.saveKey(full);
143
+ return full;
144
+ }
145
+ async verifyKey(id) {
146
+ const entry = this.getKey(id);
147
+ if (!entry) return false;
148
+ const res = await validateApiKey(entry.providerId, entry.key);
149
+ entry.status = res.status;
150
+ entry.lastLatencyMs = res.latencyMs;
151
+ entry.validationError = res.error;
152
+ keysStorage.saveKey(entry);
153
+ return res.valid;
154
+ }
155
+ recordUsage(keyId, success, latencyMs) {
156
+ const key = this.getKey(keyId);
157
+ if (!key) return;
158
+ key.totalCalls++;
159
+ key.lastLatencyMs = latencyMs;
160
+ key.lastUsedAt = (/* @__PURE__ */ new Date()).toISOString();
161
+ if (success) {
162
+ key.successfulCalls++;
163
+ } else {
164
+ key.failedCalls++;
165
+ key.errorCount++;
166
+ }
167
+ keysStorage.saveKey(key);
168
+ }
169
+ };
170
+ var keyStore = new KeyStoreService();
171
+
172
+ export {
173
+ validateApiKey,
174
+ KeysStorage,
175
+ keysStorage,
176
+ KeyStoreService,
177
+ keyStore
178
+ };
@@ -0,0 +1,32 @@
1
+ // src/ai-tools/toolValidationLoop.ts
2
+ function validateToolInput(tool, input) {
3
+ const errors = [];
4
+ const required = tool.inputSchema?.required || [];
5
+ for (const field of required) {
6
+ if (input[field] === void 0 || input[field] === null || input[field] === "") {
7
+ errors.push(`Missing required field '${field}'`);
8
+ }
9
+ }
10
+ const props = tool.inputSchema?.properties || {};
11
+ for (const [key, val] of Object.entries(input)) {
12
+ const expectedProp = props[key];
13
+ if (expectedProp?.type) {
14
+ const actualType = Array.isArray(val) ? "array" : typeof val;
15
+ if (expectedProp.type === "string" && actualType !== "string") {
16
+ errors.push(`Field '${key}' expected string but got ${actualType}`);
17
+ } else if (expectedProp.type === "number" && actualType !== "number") {
18
+ errors.push(`Field '${key}' expected number but got ${actualType}`);
19
+ } else if (expectedProp.type === "boolean" && actualType !== "boolean") {
20
+ errors.push(`Field '${key}' expected boolean but got ${actualType}`);
21
+ }
22
+ }
23
+ }
24
+ return {
25
+ valid: errors.length === 0,
26
+ errors
27
+ };
28
+ }
29
+
30
+ export {
31
+ validateToolInput
32
+ };
@@ -0,0 +1,157 @@
1
+ import {
2
+ modelHealthService
3
+ } from "./chunk-Z3LXEA6P.js";
4
+
5
+ // src/ai-models/catalogData.ts
6
+ var STATIC_MODELS_CATALOG = [
7
+ {
8
+ id: "gemini-2.5-flash",
9
+ name: "Gemini 2.5 Flash",
10
+ providerId: "google",
11
+ contextWindow: 1048576,
12
+ inputCostPer1k: 15e-5,
13
+ outputCostPer1k: 6e-4,
14
+ supportsTools: true,
15
+ supportsVision: true,
16
+ supportsJson: true,
17
+ isFree: true,
18
+ latencyRatingMs: 250,
19
+ qualityRating: 92,
20
+ status: "active"
21
+ },
22
+ {
23
+ id: "gemini-2.5-pro",
24
+ name: "Gemini 2.5 Pro",
25
+ providerId: "google",
26
+ contextWindow: 2097152,
27
+ inputCostPer1k: 125e-5,
28
+ outputCostPer1k: 5e-3,
29
+ supportsTools: true,
30
+ supportsVision: true,
31
+ supportsJson: true,
32
+ isFree: false,
33
+ latencyRatingMs: 800,
34
+ qualityRating: 97,
35
+ status: "active"
36
+ },
37
+ {
38
+ id: "llama-3.3-70b-versatile",
39
+ name: "Llama 3.3 70B Versatile",
40
+ providerId: "groq",
41
+ contextWindow: 131072,
42
+ inputCostPer1k: 59e-5,
43
+ outputCostPer1k: 79e-5,
44
+ supportsTools: true,
45
+ supportsVision: false,
46
+ supportsJson: true,
47
+ isFree: true,
48
+ latencyRatingMs: 120,
49
+ qualityRating: 89,
50
+ status: "active"
51
+ },
52
+ {
53
+ id: "llama3.1-8b",
54
+ name: "Llama 3.1 8B Instant",
55
+ providerId: "groq",
56
+ contextWindow: 131072,
57
+ inputCostPer1k: 5e-5,
58
+ outputCostPer1k: 8e-5,
59
+ supportsTools: true,
60
+ supportsVision: false,
61
+ supportsJson: true,
62
+ isFree: true,
63
+ latencyRatingMs: 80,
64
+ qualityRating: 82,
65
+ status: "active"
66
+ },
67
+ {
68
+ id: "llama3.1-70b",
69
+ name: "Llama 3.1 70B (Wafer-scale)",
70
+ providerId: "cerebras",
71
+ contextWindow: 131072,
72
+ inputCostPer1k: 6e-4,
73
+ outputCostPer1k: 6e-4,
74
+ supportsTools: true,
75
+ supportsVision: false,
76
+ supportsJson: true,
77
+ isFree: true,
78
+ latencyRatingMs: 90,
79
+ qualityRating: 88,
80
+ status: "active"
81
+ },
82
+ {
83
+ id: "meta-llama/llama-3.3-70b-instruct:free",
84
+ name: "Llama 3.3 70B Instruct (Free)",
85
+ providerId: "openrouter",
86
+ contextWindow: 131072,
87
+ inputCostPer1k: 0,
88
+ outputCostPer1k: 0,
89
+ supportsTools: true,
90
+ supportsVision: false,
91
+ supportsJson: true,
92
+ isFree: true,
93
+ latencyRatingMs: 350,
94
+ qualityRating: 88,
95
+ status: "active"
96
+ },
97
+ {
98
+ id: "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
99
+ name: "Llama 3.1 70B Turbo",
100
+ providerId: "together",
101
+ contextWindow: 131072,
102
+ inputCostPer1k: 88e-5,
103
+ outputCostPer1k: 88e-5,
104
+ supportsTools: true,
105
+ supportsVision: false,
106
+ supportsJson: true,
107
+ isFree: false,
108
+ latencyRatingMs: 180,
109
+ qualityRating: 88,
110
+ status: "active"
111
+ },
112
+ {
113
+ id: "meta/llama-3.1-70b-instruct",
114
+ name: "NVIDIA NIM Llama 3.1 70B",
115
+ providerId: "nvidia",
116
+ contextWindow: 131072,
117
+ inputCostPer1k: 0,
118
+ outputCostPer1k: 0,
119
+ supportsTools: true,
120
+ supportsVision: false,
121
+ supportsJson: true,
122
+ isFree: true,
123
+ latencyRatingMs: 220,
124
+ qualityRating: 88,
125
+ status: "active"
126
+ }
127
+ ];
128
+
129
+ // src/ai-models/modelCatalog.ts
130
+ var ModelCatalogService = class {
131
+ customModels = /* @__PURE__ */ new Map();
132
+ getAllModels() {
133
+ const list = [...STATIC_MODELS_CATALOG, ...Array.from(this.customModels.values())];
134
+ return list;
135
+ }
136
+ getModelsByProvider(providerId) {
137
+ return this.getAllModels().filter((m) => m.providerId === providerId);
138
+ }
139
+ getModel(id) {
140
+ return this.customModels.get(id) ?? this.getAllModels().find((m) => m.id === id || m.id.endsWith(id));
141
+ }
142
+ registerModel(model) {
143
+ this.customModels.set(model.id, model);
144
+ }
145
+ getAvailableModels() {
146
+ return this.getAllModels().filter(
147
+ (m) => modelHealthService.isModelAvailable(m.providerId, m.id)
148
+ );
149
+ }
150
+ };
151
+ var modelCatalog = new ModelCatalogService();
152
+
153
+ export {
154
+ STATIC_MODELS_CATALOG,
155
+ ModelCatalogService,
156
+ modelCatalog
157
+ };
@@ -0,0 +1,137 @@
1
+ import {
2
+ aiProviderService
3
+ } from "./chunk-SLR6SIEU.js";
4
+ import {
5
+ modelHealthService
6
+ } from "./chunk-Z3LXEA6P.js";
7
+
8
+ // src/ai-routing/metricsLogger.ts
9
+ var MetricsLogger = class {
10
+ records = [];
11
+ maxRecords = 2e3;
12
+ log(record) {
13
+ const full = {
14
+ id: `met_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`,
15
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
16
+ ...record
17
+ };
18
+ this.records.push(full);
19
+ if (this.records.length > this.maxRecords) {
20
+ this.records.shift();
21
+ }
22
+ }
23
+ getRecent(limit = 50) {
24
+ return this.records.slice(-limit);
25
+ }
26
+ getAverageLatency(providerId, modelId) {
27
+ const matches = this.records.filter(
28
+ (r) => r.providerId === providerId && (modelId ? r.modelId === modelId : true) && r.success
29
+ );
30
+ if (matches.length === 0) return 0;
31
+ const sum = matches.reduce((acc, r) => acc + r.durationMs, 0);
32
+ return Math.round(sum / matches.length);
33
+ }
34
+ getSuccessRate(providerId) {
35
+ const matches = this.records.filter((r) => r.providerId === providerId);
36
+ if (matches.length === 0) return 100;
37
+ const successes = matches.filter((r) => r.success).length;
38
+ return Math.round(successes / matches.length * 100);
39
+ }
40
+ };
41
+ var metricsLogger = new MetricsLogger();
42
+
43
+ // src/ai-routing/healthCheckService.ts
44
+ var HealthCheckService = class {
45
+ async checkProvider(providerId, apiKey) {
46
+ const status = await aiProviderService.checkHealth(providerId, apiKey);
47
+ return status.isOnline;
48
+ }
49
+ isRouteAvailable(providerId, modelId) {
50
+ return modelHealthService.isModelAvailable(providerId, modelId);
51
+ }
52
+ recordRouteFailure(providerId, modelId, error) {
53
+ modelHealthService.recordFailure(providerId, modelId, error);
54
+ }
55
+ recordRouteSuccess(providerId, modelId, latencyMs) {
56
+ modelHealthService.recordSuccess(providerId, modelId, latencyMs);
57
+ }
58
+ };
59
+ var healthCheckService = new HealthCheckService();
60
+
61
+ // src/ai-routing/failoverEngine.ts
62
+ var FailoverExecutionEngine = class {
63
+ defaultRetryPolicy = {
64
+ maxRetries: 3,
65
+ backoffMs: 250
66
+ };
67
+ async executeWithFailover(candidates, executeCandidate, policy) {
68
+ const start = Date.now();
69
+ const effectivePolicy = { ...this.defaultRetryPolicy, ...policy };
70
+ const failoverHistory = [];
71
+ const healthyCandidates = candidates.filter(
72
+ (c) => healthCheckService.isRouteAvailable(c.providerId, c.modelId)
73
+ );
74
+ const candidateQueue = healthyCandidates.length > 0 ? healthyCandidates : candidates;
75
+ let attempts = 0;
76
+ for (const candidate of candidateQueue) {
77
+ if (attempts >= effectivePolicy.maxRetries) break;
78
+ attempts++;
79
+ const attemptStart = Date.now();
80
+ try {
81
+ const result = await executeCandidate(candidate);
82
+ const duration = Date.now() - attemptStart;
83
+ healthCheckService.recordRouteSuccess(candidate.providerId, candidate.modelId, duration);
84
+ metricsLogger.log({
85
+ providerId: candidate.providerId,
86
+ modelId: candidate.modelId,
87
+ durationMs: duration,
88
+ success: true
89
+ });
90
+ return {
91
+ success: true,
92
+ selectedCandidate: candidate,
93
+ attempts,
94
+ result,
95
+ totalDurationMs: Date.now() - start,
96
+ failoverHistory
97
+ };
98
+ } catch (err) {
99
+ const duration = Date.now() - attemptStart;
100
+ const errMessage = err instanceof Error ? err.message : String(err);
101
+ healthCheckService.recordRouteFailure(candidate.providerId, candidate.modelId, errMessage);
102
+ metricsLogger.log({
103
+ providerId: candidate.providerId,
104
+ modelId: candidate.modelId,
105
+ durationMs: duration,
106
+ success: false,
107
+ error: errMessage
108
+ });
109
+ failoverHistory.push({
110
+ candidate,
111
+ error: errMessage,
112
+ durationMs: duration
113
+ });
114
+ if (effectivePolicy.backoffMs > 0 && attempts < effectivePolicy.maxRetries) {
115
+ await new Promise((r) => setTimeout(r, effectivePolicy.backoffMs));
116
+ }
117
+ }
118
+ }
119
+ return {
120
+ success: false,
121
+ attempts,
122
+ error: `All ${attempts} candidate execution attempts failed`,
123
+ totalDurationMs: Date.now() - start,
124
+ failoverHistory
125
+ };
126
+ }
127
+ };
128
+ var failoverExecutionEngine = new FailoverExecutionEngine();
129
+
130
+ export {
131
+ MetricsLogger,
132
+ metricsLogger,
133
+ HealthCheckService,
134
+ healthCheckService,
135
+ FailoverExecutionEngine,
136
+ failoverExecutionEngine
137
+ };