@ultrst-armada/kloks 1.13.0 → 1.15.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.
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "schema": 1,
3
3
  "packageName": "@ultrst-armada/kloks",
4
- "version": "1.13.0",
5
- "sourceCommit": "cf5fd02ba6186ab1f3002f68fd29694dad1719f0",
4
+ "version": "1.15.0",
5
+ "sourceCommit": "2604c1adbf1158c1223ace47e278a654082c4fad",
6
6
  "releaseWorkflow": "npm Release",
7
- "workflowRunId": "40833011549"
7
+ "workflowRunId": "40836063403"
8
8
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultrst-armada/kloks",
3
- "version": "1.13.0",
3
+ "version": "1.15.0",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -74,14 +74,38 @@ declare const keysStorage: KeysStorage;
74
74
  declare class KeyStoreService {
75
75
  private strategy;
76
76
  private roundRobinIndices;
77
+ private forcedNextKeys;
78
+ private permanentKeys;
77
79
  getKeys(providerId?: ProviderId): ApiKeyEntry[];
78
80
  getKey(id: string): ApiKeyEntry | undefined;
79
81
  getActiveKey(providerId: ProviderId): ApiKeyEntry | undefined;
80
82
  setStrategy(strategy: RotationStrategy): void;
81
83
  getStrategy(): RotationStrategy;
82
- addKey(entry: Omit<ApiKeyEntry, 'id' | 'createdAt' | 'totalCalls' | 'successfulCalls' | 'failedCalls' | 'lastLatencyMs' | 'errorCount'>): ApiKeyEntry;
84
+ addKey(entry: Partial<ApiKeyEntry> & Pick<ApiKeyEntry, 'providerId' | 'label' | 'key'>): ApiKeyEntry;
85
+ getPermanentKey(providerId: ProviderId): string | null;
86
+ getPermanentKeysMap(): Record<string, string>;
87
+ setPermanentKey(providerId: ProviderId, keyId: string | null): void;
88
+ setForcedNextKey(providerId: ProviderId, keyId: string): void;
89
+ peekNextKey(providerId: ProviderId, _modelId?: string): ApiKeyEntry | null;
90
+ getNextKey(providerId: ProviderId, modelId?: string): ApiKeyEntry | null;
91
+ selectNextKey(providerId: ProviderId, _modelId?: string): ApiKeyEntry | undefined;
92
+ updateKeyLabel(id: string, label: string): ApiKeyEntry | undefined;
93
+ deleteKey(id: string): boolean;
94
+ resetKeyProbabilities(id: string): ApiKeyEntry | undefined;
95
+ resetAllFailures(): void;
96
+ getUsageStats(_keyId?: string, _modelId?: string): KeyModelUsageStats[];
97
+ updateKeyStatus(id: string, status: ApiKeyEntry['status']): void;
98
+ recordUsage(keyId: string, successOrLatency: boolean | number, latencyOrSuccess: boolean | number, _costUsd?: number, _modelId?: string, error?: string): void;
99
+ exportKeysStructured(): {
100
+ version: 1;
101
+ exportedAt: string;
102
+ keys: ApiKeyEntry[];
103
+ };
104
+ importKeys(payload: unknown): Promise<{
105
+ importedCount: number;
106
+ keys: ApiKeyEntry[];
107
+ }>;
83
108
  verifyKey(id: string): Promise<boolean>;
84
- recordUsage(keyId: string, success: boolean, latencyMs: number): void;
85
109
  }
86
110
  declare const keyStore: KeyStoreService;
87
111
 
@@ -5,7 +5,7 @@ import {
5
5
  keyStore,
6
6
  keysStorage,
7
7
  validateApiKey
8
- } from "../chunk-4IYKKD53.js";
8
+ } from "../chunk-HXFTSHCP.js";
9
9
  import "../chunk-JSQZAFBU.js";
10
10
  export {
11
11
  KeyStoreService,
@@ -20,7 +20,8 @@ interface ModelCatalogEntry {
20
20
  latencyRatingMs?: number;
21
21
  qualityRating?: number;
22
22
  maxOutputTokens?: number;
23
- status?: 'active' | 'deprecated' | 'offline';
23
+ status?: 'active' | 'deprecated' | 'offline' | 'online' | 'currently_unavailable';
24
+ runtimeStats?: Record<string, any>;
24
25
  description?: string;
25
26
  }
26
27
  interface ModelHealthStatus {
@@ -73,9 +74,27 @@ declare const modelHealthService: ModelHealthService;
73
74
  */
74
75
 
75
76
  declare class ModelCatalogService {
77
+ private providerStatuses;
76
78
  private customModels;
79
+ getCatalog(providerId?: ProviderId): ModelCatalogEntry[];
80
+ getProviderStatuses(): Array<{
81
+ providerId: ProviderId;
82
+ status: string;
83
+ modelsCount: number;
84
+ error?: string;
85
+ }>;
86
+ refreshCatalogFromEndpoints(): Promise<{
87
+ updated: number;
88
+ errors: string[];
89
+ }>;
77
90
  getAllModels(): ModelCatalogEntry[];
78
91
  getModelsByProvider(providerId: ProviderId): ModelCatalogEntry[];
92
+ getModelById(id: string): ModelCatalogEntry | undefined;
93
+ recordModelSuccess(id: string): void;
94
+ recordModelFailure(id: string): void;
95
+ createExecutionContract(modelId: string, providerId: ProviderId, keyId: string, intelligenceSpec?: unknown): {
96
+ contractId: string;
97
+ };
79
98
  getModel(id: string): ModelCatalogEntry | undefined;
80
99
  registerModel(model: ModelCatalogEntry): void;
81
100
  getAvailableModels(): ModelCatalogEntry[];
@@ -1,16 +1,14 @@
1
+ import "../chunk-UU6M3L3O.js";
1
2
  import {
2
3
  EndpointRefresher,
3
- endpointRefresher
4
- } from "../chunk-2WT7OUHW.js";
5
- import {
6
4
  ModelCatalogService,
7
- STATIC_MODELS_CATALOG,
8
- modelCatalog
9
- } from "../chunk-AM2PLPNE.js";
10
- import {
11
5
  ModelHealthService,
6
+ STATIC_MODELS_CATALOG,
7
+ endpointRefresher,
8
+ modelCatalog,
12
9
  modelHealthService
13
- } from "../chunk-Z3LXEA6P.js";
10
+ } from "../chunk-F4J4YPUR.js";
11
+ import "../chunk-HXFTSHCP.js";
14
12
  import "../chunk-MAMK4GDH.js";
15
13
  import "../chunk-JSQZAFBU.js";
16
14
  export {
@@ -50,6 +50,32 @@ interface AIMetricRecord {
50
50
  declare class MetricsLogger {
51
51
  private records;
52
52
  private readonly maxRecords;
53
+ logRequest(record: {
54
+ providerId: ProviderId;
55
+ modelId: string;
56
+ latencyMs: number;
57
+ status?: string;
58
+ clientIp?: string;
59
+ profileRequested?: unknown;
60
+ selectedModel?: string;
61
+ selectedProvider?: ProviderId;
62
+ selectedKeyId?: string;
63
+ tokensUsed?: number;
64
+ statusCode?: number;
65
+ failoverOccurred?: boolean;
66
+ failoverChain?: string[];
67
+ promptSnippet?: string;
68
+ responseSnippet?: string;
69
+ toolCalls?: unknown;
70
+ toolsProvided?: unknown;
71
+ }): void;
72
+ getLogs(limit?: number): AIMetricRecord[];
73
+ getMetricsSummary(): {
74
+ totalRequests: number;
75
+ successRatePercent: number;
76
+ averageLatencyMs: number;
77
+ totalFailovers: number;
78
+ };
53
79
  log(record: Omit<AIMetricRecord, 'id' | 'timestamp'>): void;
54
80
  getRecent(limit?: number): AIMetricRecord[];
55
81
  getAverageLatency(providerId: ProviderId, modelId?: string): number;
@@ -63,6 +89,13 @@ declare const metricsLogger: MetricsLogger;
63
89
  */
64
90
 
65
91
  declare class HealthCheckService {
92
+ runRoutineCheck(): Promise<{
93
+ timestamp: string;
94
+ keysTotal: number;
95
+ keysValid: number;
96
+ modelsCatalogTotal: number;
97
+ status: 'healthy';
98
+ }>;
66
99
  checkProvider(providerId: ProviderId, apiKey?: string): Promise<boolean>;
67
100
  isRouteAvailable(providerId: ProviderId, modelId: string): boolean;
68
101
  recordRouteFailure(providerId: ProviderId, modelId: string, error: string): void;
@@ -6,9 +6,10 @@ import {
6
6
  failoverExecutionEngine,
7
7
  healthCheckService,
8
8
  metricsLogger
9
- } from "../chunk-ERDBD4TH.js";
9
+ } from "../chunk-AQT3GIXB.js";
10
+ import "../chunk-F4J4YPUR.js";
11
+ import "../chunk-HXFTSHCP.js";
10
12
  import "../chunk-SLR6SIEU.js";
11
- import "../chunk-Z3LXEA6P.js";
12
13
  import "../chunk-MAMK4GDH.js";
13
14
  import "../chunk-JSQZAFBU.js";
14
15
  export {
@@ -45,6 +45,8 @@ declare class MCPEngine {
45
45
  private servers;
46
46
  private registeredTools;
47
47
  private toolHandlers;
48
+ getSystemTools(): MCPTool[];
49
+ handleJsonRpc(payload: any): Promise<any>;
48
50
  registerServer(config: MCPServerConfig): void;
49
51
  getServer(id: string): MCPServerConfig | undefined;
50
52
  listServers(): MCPServerConfig[];
@@ -6,7 +6,7 @@ import {
6
6
  ToolManagerService,
7
7
  mcpEngine,
8
8
  toolManagerService
9
- } from "../chunk-NSKVYL74.js";
9
+ } from "../chunk-U4HUGPRH.js";
10
10
  export {
11
11
  MCPEngine,
12
12
  ToolManagerService,
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  toolManagerService
3
- } from "./chunk-NSKVYL74.js";
3
+ } from "./chunk-U4HUGPRH.js";
4
4
  import {
5
5
  createService,
6
6
  createServiceDefinition
@@ -9,14 +9,14 @@ import {
9
9
  inferenceExecutor
10
10
  } from "./chunk-GCTQY24J.js";
11
11
  import {
12
- keyStore
13
- } from "./chunk-4IYKKD53.js";
12
+ failoverExecutionEngine
13
+ } from "./chunk-AQT3GIXB.js";
14
14
  import {
15
15
  modelCatalog
16
- } from "./chunk-AM2PLPNE.js";
16
+ } from "./chunk-F4J4YPUR.js";
17
17
  import {
18
- failoverExecutionEngine
19
- } from "./chunk-ERDBD4TH.js";
18
+ keyStore
19
+ } from "./chunk-HXFTSHCP.js";
20
20
  import {
21
21
  aiProviderService
22
22
  } from "./chunk-SLR6SIEU.js";
@@ -1,14 +1,40 @@
1
+ import {
2
+ modelCatalog,
3
+ modelHealthService
4
+ } from "./chunk-F4J4YPUR.js";
5
+ import {
6
+ keyStore
7
+ } from "./chunk-HXFTSHCP.js";
1
8
  import {
2
9
  aiProviderService
3
10
  } from "./chunk-SLR6SIEU.js";
4
- import {
5
- modelHealthService
6
- } from "./chunk-Z3LXEA6P.js";
7
11
 
8
12
  // src/ai-routing/metricsLogger.ts
9
13
  var MetricsLogger = class {
10
14
  records = [];
11
15
  maxRecords = 2e3;
16
+ logRequest(record) {
17
+ this.log({
18
+ providerId: record.providerId,
19
+ modelId: record.modelId,
20
+ durationMs: record.latencyMs,
21
+ success: record.status !== "failure"
22
+ });
23
+ }
24
+ getLogs(limit = 50) {
25
+ return this.records.slice(-limit);
26
+ }
27
+ getMetricsSummary() {
28
+ const totalRequests = this.records.length;
29
+ const successes = this.records.filter((record) => record.success).length;
30
+ const averageLatencyMs = totalRequests ? Math.round(this.records.reduce((sum, record) => sum + record.durationMs, 0) / totalRequests) : 0;
31
+ return {
32
+ totalRequests,
33
+ successRatePercent: totalRequests ? Math.round(successes / totalRequests * 100) : 100,
34
+ averageLatencyMs,
35
+ totalFailovers: 0
36
+ };
37
+ }
12
38
  log(record) {
13
39
  const full = {
14
40
  id: `met_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`,
@@ -42,6 +68,17 @@ var metricsLogger = new MetricsLogger();
42
68
 
43
69
  // src/ai-routing/healthCheckService.ts
44
70
  var HealthCheckService = class {
71
+ async runRoutineCheck() {
72
+ const keys = keyStore.getKeys();
73
+ const catalog = modelCatalog.getAllModels();
74
+ return {
75
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
76
+ keysTotal: keys.length,
77
+ keysValid: keys.filter((key) => key.status === "active" || key.status === "valid").length,
78
+ modelsCatalogTotal: catalog.length,
79
+ status: "healthy"
80
+ };
81
+ }
45
82
  async checkProvider(providerId, apiKey) {
46
83
  const status = await aiProviderService.checkHealth(providerId, apiKey);
47
84
  return status.isOnline;
@@ -0,0 +1,293 @@
1
+ import {
2
+ keyStore
3
+ } from "./chunk-HXFTSHCP.js";
4
+ import {
5
+ AITransportClient
6
+ } from "./chunk-MAMK4GDH.js";
7
+ import {
8
+ getProviderMeta
9
+ } from "./chunk-JSQZAFBU.js";
10
+
11
+ // src/ai-models/catalogData.ts
12
+ var STATIC_MODELS_CATALOG = [
13
+ {
14
+ id: "gemini-2.5-flash",
15
+ name: "Gemini 2.5 Flash",
16
+ providerId: "google",
17
+ contextWindow: 1048576,
18
+ inputCostPer1k: 15e-5,
19
+ outputCostPer1k: 6e-4,
20
+ supportsTools: true,
21
+ supportsVision: true,
22
+ supportsJson: true,
23
+ isFree: true,
24
+ latencyRatingMs: 250,
25
+ qualityRating: 92,
26
+ status: "active"
27
+ },
28
+ {
29
+ id: "gemini-2.5-pro",
30
+ name: "Gemini 2.5 Pro",
31
+ providerId: "google",
32
+ contextWindow: 2097152,
33
+ inputCostPer1k: 125e-5,
34
+ outputCostPer1k: 5e-3,
35
+ supportsTools: true,
36
+ supportsVision: true,
37
+ supportsJson: true,
38
+ isFree: false,
39
+ latencyRatingMs: 800,
40
+ qualityRating: 97,
41
+ status: "active"
42
+ },
43
+ {
44
+ id: "llama-3.3-70b-versatile",
45
+ name: "Llama 3.3 70B Versatile",
46
+ providerId: "groq",
47
+ contextWindow: 131072,
48
+ inputCostPer1k: 59e-5,
49
+ outputCostPer1k: 79e-5,
50
+ supportsTools: true,
51
+ supportsVision: false,
52
+ supportsJson: true,
53
+ isFree: true,
54
+ latencyRatingMs: 120,
55
+ qualityRating: 89,
56
+ status: "active"
57
+ },
58
+ {
59
+ id: "llama3.1-8b",
60
+ name: "Llama 3.1 8B Instant",
61
+ providerId: "groq",
62
+ contextWindow: 131072,
63
+ inputCostPer1k: 5e-5,
64
+ outputCostPer1k: 8e-5,
65
+ supportsTools: true,
66
+ supportsVision: false,
67
+ supportsJson: true,
68
+ isFree: true,
69
+ latencyRatingMs: 80,
70
+ qualityRating: 82,
71
+ status: "active"
72
+ },
73
+ {
74
+ id: "llama3.1-70b",
75
+ name: "Llama 3.1 70B (Wafer-scale)",
76
+ providerId: "cerebras",
77
+ contextWindow: 131072,
78
+ inputCostPer1k: 6e-4,
79
+ outputCostPer1k: 6e-4,
80
+ supportsTools: true,
81
+ supportsVision: false,
82
+ supportsJson: true,
83
+ isFree: true,
84
+ latencyRatingMs: 90,
85
+ qualityRating: 88,
86
+ status: "active"
87
+ },
88
+ {
89
+ id: "meta-llama/llama-3.3-70b-instruct:free",
90
+ name: "Llama 3.3 70B Instruct (Free)",
91
+ providerId: "openrouter",
92
+ contextWindow: 131072,
93
+ inputCostPer1k: 0,
94
+ outputCostPer1k: 0,
95
+ supportsTools: true,
96
+ supportsVision: false,
97
+ supportsJson: true,
98
+ isFree: true,
99
+ latencyRatingMs: 350,
100
+ qualityRating: 88,
101
+ status: "active"
102
+ },
103
+ {
104
+ id: "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
105
+ name: "Llama 3.1 70B Turbo",
106
+ providerId: "together",
107
+ contextWindow: 131072,
108
+ inputCostPer1k: 88e-5,
109
+ outputCostPer1k: 88e-5,
110
+ supportsTools: true,
111
+ supportsVision: false,
112
+ supportsJson: true,
113
+ isFree: false,
114
+ latencyRatingMs: 180,
115
+ qualityRating: 88,
116
+ status: "active"
117
+ },
118
+ {
119
+ id: "meta/llama-3.1-70b-instruct",
120
+ name: "NVIDIA NIM Llama 3.1 70B",
121
+ providerId: "nvidia",
122
+ contextWindow: 131072,
123
+ inputCostPer1k: 0,
124
+ outputCostPer1k: 0,
125
+ supportsTools: true,
126
+ supportsVision: false,
127
+ supportsJson: true,
128
+ isFree: true,
129
+ latencyRatingMs: 220,
130
+ qualityRating: 88,
131
+ status: "active"
132
+ }
133
+ ];
134
+
135
+ // src/ai-models/endpointRefresher.ts
136
+ var EndpointRefresher = class {
137
+ client;
138
+ constructor(client) {
139
+ this.client = client ?? new AITransportClient();
140
+ }
141
+ async fetchRemoteModels(providerId, apiKey) {
142
+ const meta = getProviderMeta(providerId);
143
+ if (!meta) throw new Error(`Unknown provider ${providerId}`);
144
+ const url = `${meta.baseUrl}${meta.modelsEndpoint}`;
145
+ const headers = {};
146
+ if (meta.defaultHeaderKey === "Authorization") {
147
+ headers["Authorization"] = `Bearer ${apiKey.trim()}`;
148
+ } else {
149
+ headers[meta.defaultHeaderKey] = apiKey.trim();
150
+ }
151
+ const res = await this.client.request({
152
+ url,
153
+ method: "GET",
154
+ headers,
155
+ timeoutMs: 12e3
156
+ });
157
+ if (!res.ok) {
158
+ throw new Error(`Failed to fetch models from ${providerId}: HTTP ${res.status}`);
159
+ }
160
+ const rawList = res.data.data || res.data.models || [];
161
+ return rawList.map((item) => ({
162
+ id: item.id || item.name || "",
163
+ name: item.display_name || item.name || item.id || "",
164
+ providerId,
165
+ contextWindow: item.context_window || item.input_token_limit || 131072,
166
+ supportsTools: true,
167
+ supportsVision: Boolean(item.architecture?.modality?.includes("image")),
168
+ supportsJson: true,
169
+ status: "active"
170
+ }));
171
+ }
172
+ };
173
+ var endpointRefresher = new EndpointRefresher();
174
+
175
+ // src/ai-models/modelHealthService.ts
176
+ var ModelHealthService = class {
177
+ healthMap = /* @__PURE__ */ new Map();
178
+ recordSuccess(providerId, modelId, latencyMs) {
179
+ const key = `${providerId}:${modelId}`;
180
+ this.healthMap.set(key, {
181
+ modelId,
182
+ providerId,
183
+ isHealthy: true,
184
+ latencyMs,
185
+ consecutiveFailures: 0,
186
+ lastCheckedAt: (/* @__PURE__ */ new Date()).toISOString()
187
+ });
188
+ }
189
+ recordFailure(providerId, modelId, error) {
190
+ const key = `${providerId}:${modelId}`;
191
+ const existing = this.healthMap.get(key);
192
+ const consecutive = (existing?.consecutiveFailures ?? 0) + 1;
193
+ this.healthMap.set(key, {
194
+ modelId,
195
+ providerId,
196
+ isHealthy: consecutive < 3,
197
+ latencyMs: existing?.latencyMs ?? 0,
198
+ consecutiveFailures: consecutive,
199
+ lastCheckedAt: (/* @__PURE__ */ new Date()).toISOString(),
200
+ lastError: error
201
+ });
202
+ }
203
+ isModelAvailable(providerId, modelId) {
204
+ const status = this.healthMap.get(`${providerId}:${modelId}`);
205
+ return status ? status.isHealthy : true;
206
+ }
207
+ getHealth(providerId, modelId) {
208
+ return this.healthMap.get(`${providerId}:${modelId}`);
209
+ }
210
+ getAllHealth() {
211
+ return Array.from(this.healthMap.values());
212
+ }
213
+ };
214
+ var modelHealthService = new ModelHealthService();
215
+
216
+ // src/ai-models/modelCatalog.ts
217
+ var ModelCatalogService = class {
218
+ providerStatuses = {};
219
+ customModels = /* @__PURE__ */ new Map();
220
+ getCatalog(providerId) {
221
+ return providerId ? this.getAllModels().filter((model) => model.providerId === providerId) : this.getAllModels();
222
+ }
223
+ getProviderStatuses() {
224
+ const providers = new Set(this.getAllModels().map((model) => model.providerId));
225
+ for (const providerId of providers) {
226
+ const modelsCount = this.getModelsByProvider(providerId).length;
227
+ this.providerStatuses[providerId] ??= { providerId, status: "online", modelsCount };
228
+ this.providerStatuses[providerId].modelsCount = modelsCount;
229
+ }
230
+ return Object.values(this.providerStatuses);
231
+ }
232
+ async refreshCatalogFromEndpoints() {
233
+ const errors = [];
234
+ let updated = 0;
235
+ for (const providerId of Array.from(new Set(this.getAllModels().map((model) => model.providerId)))) {
236
+ const key = keyStore.getActiveKey(providerId);
237
+ if (!key) continue;
238
+ try {
239
+ const models = await endpointRefresher.fetchRemoteModels(providerId, key.key);
240
+ for (const model of models) this.registerModel(model);
241
+ updated += models.length;
242
+ } catch (error) {
243
+ errors.push(error instanceof Error ? error.message : String(error));
244
+ }
245
+ }
246
+ return { updated, errors };
247
+ }
248
+ getAllModels() {
249
+ const list = [...STATIC_MODELS_CATALOG, ...Array.from(this.customModels.values())];
250
+ return list;
251
+ }
252
+ getModelsByProvider(providerId) {
253
+ return this.getAllModels().filter((m) => m.providerId === providerId);
254
+ }
255
+ getModelById(id) {
256
+ return this.getModel(id);
257
+ }
258
+ recordModelSuccess(id) {
259
+ const model = this.getModel(id);
260
+ if (model) model.status = "active";
261
+ }
262
+ recordModelFailure(id) {
263
+ const model = this.getModel(id);
264
+ if (model) model.status = "offline";
265
+ }
266
+ createExecutionContract(modelId, providerId, keyId, intelligenceSpec) {
267
+ return {
268
+ contractId: `contract-${Date.now()}-${providerId}-${modelId}-${keyId}`
269
+ };
270
+ }
271
+ getModel(id) {
272
+ return this.customModels.get(id) ?? this.getAllModels().find((m) => m.id === id || m.id.endsWith(id));
273
+ }
274
+ registerModel(model) {
275
+ this.customModels.set(model.id, model);
276
+ }
277
+ getAvailableModels() {
278
+ return this.getAllModels().filter(
279
+ (m) => modelHealthService.isModelAvailable(m.providerId, m.id)
280
+ );
281
+ }
282
+ };
283
+ var modelCatalog = new ModelCatalogService();
284
+
285
+ export {
286
+ STATIC_MODELS_CATALOG,
287
+ EndpointRefresher,
288
+ endpointRefresher,
289
+ ModelHealthService,
290
+ modelHealthService,
291
+ ModelCatalogService,
292
+ modelCatalog
293
+ };
@@ -2,6 +2,29 @@ import {
2
2
  getProviderMeta
3
3
  } from "./chunk-JSQZAFBU.js";
4
4
 
5
+ // src/ai-keys/keysStorage.ts
6
+ var KeysStorage = class {
7
+ inMemoryKeys = /* @__PURE__ */ new Map();
8
+ loadKeys() {
9
+ return Array.from(this.inMemoryKeys.values());
10
+ }
11
+ saveKey(entry) {
12
+ this.inMemoryKeys.set(entry.id, entry);
13
+ }
14
+ saveKeys(entries) {
15
+ for (const e of entries) {
16
+ this.inMemoryKeys.set(e.id, e);
17
+ }
18
+ }
19
+ deleteKey(id) {
20
+ this.inMemoryKeys.delete(id);
21
+ }
22
+ clear() {
23
+ this.inMemoryKeys.clear();
24
+ }
25
+ };
26
+ var keysStorage = new KeysStorage();
27
+
5
28
  // src/ai-keys/keyValidator.ts
6
29
  async function validateApiKey(providerId, apiKey) {
7
30
  const meta = getProviderMeta(providerId);
@@ -68,33 +91,12 @@ async function validateApiKey(providerId, apiKey) {
68
91
  }
69
92
  }
70
93
 
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
94
  // src/ai-keys/keyStore.ts
95
95
  var KeyStoreService = class {
96
96
  strategy = "lowest-latency";
97
97
  roundRobinIndices = /* @__PURE__ */ new Map();
98
+ forcedNextKeys = /* @__PURE__ */ new Map();
99
+ permanentKeys = /* @__PURE__ */ new Map();
98
100
  getKeys(providerId) {
99
101
  const all = keysStorage.loadKeys();
100
102
  if (!providerId) return all;
@@ -137,42 +139,126 @@ var KeyStoreService = class {
137
139
  failedCalls: 0,
138
140
  lastLatencyMs: 0,
139
141
  errorCount: 0,
142
+ status: "active",
143
+ isActive: true,
144
+ remainingCreditsUsd: null,
145
+ weight: 5,
140
146
  ...entry
141
147
  };
142
148
  keysStorage.saveKey(full);
143
149
  return full;
144
150
  }
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;
151
+ getPermanentKey(providerId) {
152
+ return this.permanentKeys.get(providerId) ?? null;
153
+ }
154
+ getPermanentKeysMap() {
155
+ return Object.fromEntries(this.permanentKeys.entries());
156
+ }
157
+ setPermanentKey(providerId, keyId) {
158
+ if (keyId) this.permanentKeys.set(providerId, keyId);
159
+ else this.permanentKeys.delete(providerId);
160
+ }
161
+ setForcedNextKey(providerId, keyId) {
162
+ this.forcedNextKeys.set(providerId, keyId);
163
+ }
164
+ peekNextKey(providerId, _modelId) {
165
+ const keys = this.getKeys(providerId).filter((key) => key.status !== "invalid" && key.isActive);
166
+ const forced = this.forcedNextKeys.get(providerId);
167
+ const permanent = this.permanentKeys.get(providerId);
168
+ return forced && this.getKey(forced) || permanent && this.getKey(permanent) || keys[0] || null;
169
+ }
170
+ getNextKey(providerId, modelId) {
171
+ const key = this.peekNextKey(providerId, modelId);
172
+ this.forcedNextKeys.delete(providerId);
173
+ return key;
174
+ }
175
+ selectNextKey(providerId, _modelId) {
176
+ return this.getActiveKey(providerId);
177
+ }
178
+ updateKeyLabel(id, label) {
179
+ const key = this.getKey(id);
180
+ if (!key) return void 0;
181
+ key.label = label;
182
+ keysStorage.saveKey(key);
183
+ return key;
184
+ }
185
+ deleteKey(id) {
186
+ const existed = Boolean(this.getKey(id));
187
+ keysStorage.deleteKey(id);
188
+ return existed;
189
+ }
190
+ resetKeyProbabilities(id) {
191
+ const key = this.getKey(id);
192
+ if (!key) return void 0;
193
+ key.weight = 5;
194
+ key.errorCount = 0;
195
+ key.status = "active";
196
+ key.isActive = true;
197
+ keysStorage.saveKey(key);
198
+ return key;
154
199
  }
155
- recordUsage(keyId, success, latencyMs) {
200
+ resetAllFailures() {
201
+ for (const key of this.getKeys()) {
202
+ key.errorCount = 0;
203
+ key.status = "active";
204
+ key.isActive = true;
205
+ keysStorage.saveKey(key);
206
+ }
207
+ }
208
+ getUsageStats(_keyId, _modelId) {
209
+ return [];
210
+ }
211
+ updateKeyStatus(id, status) {
212
+ const key = this.getKey(id);
213
+ if (!key) return;
214
+ key.status = status;
215
+ keysStorage.saveKey(key);
216
+ }
217
+ recordUsage(keyId, successOrLatency, latencyOrSuccess, _costUsd = 0, _modelId, error) {
156
218
  const key = this.getKey(keyId);
157
219
  if (!key) return;
220
+ const success = typeof successOrLatency === "boolean" ? successOrLatency : Boolean(latencyOrSuccess);
221
+ const latencyMs = typeof successOrLatency === "number" ? successOrLatency : Number(latencyOrSuccess);
158
222
  key.totalCalls++;
159
223
  key.lastLatencyMs = latencyMs;
160
224
  key.lastUsedAt = (/* @__PURE__ */ new Date()).toISOString();
161
- if (success) {
162
- key.successfulCalls++;
163
- } else {
225
+ if (success) key.successfulCalls++;
226
+ else {
164
227
  key.failedCalls++;
165
228
  key.errorCount++;
229
+ key.validationError = error ?? key.validationError;
166
230
  }
167
231
  keysStorage.saveKey(key);
168
232
  }
233
+ exportKeysStructured() {
234
+ return { version: 1, exportedAt: (/* @__PURE__ */ new Date()).toISOString(), keys: this.getKeys() };
235
+ }
236
+ async importKeys(payload) {
237
+ const raw = payload?.keys ?? payload;
238
+ if (!Array.isArray(raw)) throw new Error("Invalid key backup payload.");
239
+ const keys = raw.filter(
240
+ (entry) => Boolean(entry && typeof entry.id === "string" && typeof entry.providerId === "string" && typeof entry.key === "string")
241
+ );
242
+ keysStorage.saveKeys(keys);
243
+ return { importedCount: keys.length, keys };
244
+ }
245
+ async verifyKey(id) {
246
+ const entry = this.getKey(id);
247
+ if (!entry) return false;
248
+ const res = await validateApiKey(entry.providerId, entry.key);
249
+ entry.status = res.status;
250
+ entry.lastLatencyMs = res.latencyMs;
251
+ entry.validationError = res.error;
252
+ keysStorage.saveKey(entry);
253
+ return res.valid;
254
+ }
169
255
  };
170
256
  var keyStore = new KeyStoreService();
171
257
 
172
258
  export {
173
- validateApiKey,
174
259
  KeysStorage,
175
260
  keysStorage,
261
+ validateApiKey,
176
262
  KeyStoreService,
177
263
  keyStore
178
264
  };
@@ -3,6 +3,26 @@ var MCPEngine = class {
3
3
  servers = /* @__PURE__ */ new Map();
4
4
  registeredTools = /* @__PURE__ */ new Map();
5
5
  toolHandlers = /* @__PURE__ */ new Map();
6
+ getSystemTools() {
7
+ return this.listTools();
8
+ }
9
+ async handleJsonRpc(payload) {
10
+ const { id, method, params } = payload ?? {};
11
+ if (method === "tools/list") {
12
+ return { jsonrpc: "2.0", id, result: { tools: this.listTools() } };
13
+ }
14
+ if (method === "tools/call") {
15
+ const toolName = params?.name;
16
+ if (!toolName) return { jsonrpc: "2.0", id, error: { code: -32602, message: "Tool name is required." } };
17
+ const result = await this.executeTool(toolName, params?.arguments ?? {});
18
+ return {
19
+ jsonrpc: "2.0",
20
+ id,
21
+ result: result.success ? { content: [{ type: "text", text: JSON.stringify(result.output ?? "") }] } : { content: [{ type: "text", text: result.error ?? "Tool execution failed" }], isError: true }
22
+ };
23
+ }
24
+ return { jsonrpc: "2.0", id, error: { code: -32601, message: `Method "${method}" not found.` } };
25
+ }
6
26
  registerServer(config) {
7
27
  this.servers.set(config.id, config);
8
28
  }
File without changes
@@ -138,13 +138,13 @@ import {
138
138
  AIRoutingServiceFacade,
139
139
  AIToolServiceDefinition,
140
140
  AIToolServiceFacade
141
- } from "./chunk-NITVHSB3.js";
141
+ } from "./chunk-6YBBOHHG.js";
142
142
  import {
143
143
  MCPEngine,
144
144
  ToolManagerService,
145
145
  mcpEngine,
146
146
  toolManagerService
147
- } from "./chunk-NSKVYL74.js";
147
+ } from "./chunk-U4HUGPRH.js";
148
148
  import {
149
149
  AuthServiceDefinition,
150
150
  AuthServiceFacade,
@@ -269,23 +269,8 @@ import {
269
269
  performInference
270
270
  } from "./chunk-GCTQY24J.js";
271
271
  import "./chunk-IN3WRHXE.js";
272
- import {
273
- KeyStoreService,
274
- KeysStorage,
275
- keyStore,
276
- keysStorage,
277
- validateApiKey
278
- } from "./chunk-4IYKKD53.js";
272
+ import "./chunk-UU6M3L3O.js";
279
273
  import "./chunk-LV55WKZR.js";
280
- import {
281
- EndpointRefresher,
282
- endpointRefresher
283
- } from "./chunk-2WT7OUHW.js";
284
- import {
285
- ModelCatalogService,
286
- STATIC_MODELS_CATALOG,
287
- modelCatalog
288
- } from "./chunk-AM2PLPNE.js";
289
274
  import "./chunk-WVDSMTW3.js";
290
275
  import {
291
276
  FailoverExecutionEngine,
@@ -294,15 +279,27 @@ import {
294
279
  failoverExecutionEngine,
295
280
  healthCheckService,
296
281
  metricsLogger
297
- } from "./chunk-ERDBD4TH.js";
282
+ } from "./chunk-AQT3GIXB.js";
283
+ import {
284
+ EndpointRefresher,
285
+ ModelCatalogService,
286
+ ModelHealthService,
287
+ STATIC_MODELS_CATALOG,
288
+ endpointRefresher,
289
+ modelCatalog,
290
+ modelHealthService
291
+ } from "./chunk-F4J4YPUR.js";
292
+ import {
293
+ KeyStoreService,
294
+ KeysStorage,
295
+ keyStore,
296
+ keysStorage,
297
+ validateApiKey
298
+ } from "./chunk-HXFTSHCP.js";
298
299
  import {
299
300
  AIProviderService,
300
301
  aiProviderService
301
302
  } from "./chunk-SLR6SIEU.js";
302
- import {
303
- ModelHealthService,
304
- modelHealthService
305
- } from "./chunk-Z3LXEA6P.js";
306
303
  import {
307
304
  AITransportClient
308
305
  } from "./chunk-MAMK4GDH.js";
@@ -11,15 +11,14 @@ import {
11
11
  AIRoutingServiceFacade,
12
12
  AIToolServiceDefinition,
13
13
  AIToolServiceFacade
14
- } from "./chunk-NITVHSB3.js";
15
- import "./chunk-NSKVYL74.js";
14
+ } from "./chunk-6YBBOHHG.js";
15
+ import "./chunk-U4HUGPRH.js";
16
16
  import "./chunk-TY32TUNI.js";
17
17
  import "./chunk-GCTQY24J.js";
18
- import "./chunk-4IYKKD53.js";
19
- import "./chunk-AM2PLPNE.js";
20
- import "./chunk-ERDBD4TH.js";
18
+ import "./chunk-AQT3GIXB.js";
19
+ import "./chunk-F4J4YPUR.js";
20
+ import "./chunk-HXFTSHCP.js";
21
21
  import "./chunk-SLR6SIEU.js";
22
- import "./chunk-Z3LXEA6P.js";
23
22
  import "./chunk-MAMK4GDH.js";
24
23
  import "./chunk-JSQZAFBU.js";
25
24
  export {
@@ -1,51 +0,0 @@
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
- };
@@ -1,157 +0,0 @@
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
- };
@@ -1,45 +0,0 @@
1
- // src/ai-models/modelHealthService.ts
2
- var ModelHealthService = class {
3
- healthMap = /* @__PURE__ */ new Map();
4
- recordSuccess(providerId, modelId, latencyMs) {
5
- const key = `${providerId}:${modelId}`;
6
- this.healthMap.set(key, {
7
- modelId,
8
- providerId,
9
- isHealthy: true,
10
- latencyMs,
11
- consecutiveFailures: 0,
12
- lastCheckedAt: (/* @__PURE__ */ new Date()).toISOString()
13
- });
14
- }
15
- recordFailure(providerId, modelId, error) {
16
- const key = `${providerId}:${modelId}`;
17
- const existing = this.healthMap.get(key);
18
- const consecutive = (existing?.consecutiveFailures ?? 0) + 1;
19
- this.healthMap.set(key, {
20
- modelId,
21
- providerId,
22
- isHealthy: consecutive < 3,
23
- latencyMs: existing?.latencyMs ?? 0,
24
- consecutiveFailures: consecutive,
25
- lastCheckedAt: (/* @__PURE__ */ new Date()).toISOString(),
26
- lastError: error
27
- });
28
- }
29
- isModelAvailable(providerId, modelId) {
30
- const status = this.healthMap.get(`${providerId}:${modelId}`);
31
- return status ? status.isHealthy : true;
32
- }
33
- getHealth(providerId, modelId) {
34
- return this.healthMap.get(`${providerId}:${modelId}`);
35
- }
36
- getAllHealth() {
37
- return Array.from(this.healthMap.values());
38
- }
39
- };
40
- var modelHealthService = new ModelHealthService();
41
-
42
- export {
43
- ModelHealthService,
44
- modelHealthService
45
- };