@ultrst-armada/kloks 1.12.0 → 1.14.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/.krusbl-release-attestation.json +3 -3
- package/package.json +36 -1
- package/packages/backend/dist/AITransportClient-dToGr8nj.d.ts +30 -0
- package/packages/backend/dist/ai-inference/index.d.ts +99 -0
- package/packages/backend/dist/ai-inference/index.js +21 -0
- package/packages/backend/dist/ai-keys/index.d.ts +99 -0
- package/packages/backend/dist/ai-keys/index.js +16 -0
- package/packages/backend/dist/ai-models/index.d.ts +91 -0
- package/packages/backend/dist/ai-models/index.js +24 -0
- package/packages/backend/dist/ai-provider/index.d.ts +32 -0
- package/packages/backend/dist/ai-provider/index.js +23 -0
- package/packages/backend/dist/ai-routing/index.d.ts +103 -0
- package/packages/backend/dist/ai-routing/index.js +21 -0
- package/packages/backend/dist/ai-tools/index.d.ts +80 -0
- package/packages/backend/dist/ai-tools/index.js +16 -0
- package/packages/backend/dist/chunk-2WT7OUHW.js +51 -0
- package/packages/backend/dist/chunk-4GWYTYDQ.js +201 -0
- package/packages/backend/dist/chunk-6GQNYRD6.js +32 -0
- package/packages/backend/dist/chunk-GCTQY24J.js +195 -0
- package/packages/backend/dist/chunk-IN3WRHXE.js +0 -0
- package/packages/backend/dist/chunk-JSQZAFBU.js +153 -0
- package/packages/backend/dist/chunk-LV55WKZR.js +0 -0
- package/packages/backend/dist/chunk-MAMK4GDH.js +73 -0
- package/packages/backend/dist/chunk-MFM2QIY5.js +173 -0
- package/packages/backend/dist/chunk-MSCOKLAO.js +145 -0
- package/packages/backend/dist/chunk-NSKVYL74.js +74 -0
- package/packages/backend/dist/chunk-SLR6SIEU.js +77 -0
- package/packages/backend/dist/chunk-WVDSMTW3.js +0 -0
- package/packages/backend/dist/chunk-XGPL43T3.js +225 -0
- package/packages/backend/dist/chunk-Z2FHVSNF.js +42 -0
- package/packages/backend/dist/chunk-Z3LXEA6P.js +45 -0
- package/packages/backend/dist/index.d.ts +9 -0
- package/packages/backend/dist/index.js +251 -124
- package/packages/backend/dist/krnl-ai-services.d.ts +71 -0
- package/packages/backend/dist/krnl-ai-services.js +38 -0
- package/packages/backend/dist/krnl-services.js +6 -6
- package/packages/backend/dist/services/index.js +1 -1
- package/packages/backend/dist/storage/index.js +7 -7
- package/packages/backend/dist/types-2p2AhboE.d.ts +33 -0
- package/packages/backend/dist/workspace/index.js +2 -2
- package/packages/backend/package.json +36 -1
- package/packages/state-graph/dist/index.js +15 -15
- package/packages/backend/dist/{chunk-UMEO6XS4.js → chunk-D6ZGQFWQ.js} +14 -14
- package/packages/backend/dist/{chunk-BTMIK44G.js → chunk-EIYYESEU.js} +3 -3
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// src/ai-provider/providers.ts
|
|
2
|
+
var PROVIDERS_META = {
|
|
3
|
+
google: {
|
|
4
|
+
id: "google",
|
|
5
|
+
name: "Google Gemini",
|
|
6
|
+
baseUrl: "https://generativelanguage.googleapis.com",
|
|
7
|
+
modelsEndpoint: "/v1beta/models",
|
|
8
|
+
defaultHeaderKey: "x-goog-api-key",
|
|
9
|
+
supportsCreditsCheck: true,
|
|
10
|
+
docUrl: "https://ai.google.dev",
|
|
11
|
+
description: "Gemini 2.5/3.0 Flash, Gemini Pro, Multimodal AI",
|
|
12
|
+
capabilities: {
|
|
13
|
+
supportsTools: true,
|
|
14
|
+
supportsVision: true,
|
|
15
|
+
supportsStreaming: true,
|
|
16
|
+
supportsJsonOutput: true,
|
|
17
|
+
supportsCreditsCheck: true,
|
|
18
|
+
maxContextWindow: 1048576
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
cerebras: {
|
|
22
|
+
id: "cerebras",
|
|
23
|
+
name: "Cerebras Systems",
|
|
24
|
+
baseUrl: "https://api.cerebras.ai",
|
|
25
|
+
modelsEndpoint: "/v1/models",
|
|
26
|
+
defaultHeaderKey: "Authorization",
|
|
27
|
+
supportsCreditsCheck: true,
|
|
28
|
+
docUrl: "https://cerebras.ai",
|
|
29
|
+
description: "Wafer-scale ultra-fast Llama inference engine",
|
|
30
|
+
capabilities: {
|
|
31
|
+
supportsTools: true,
|
|
32
|
+
supportsVision: false,
|
|
33
|
+
supportsStreaming: true,
|
|
34
|
+
supportsJsonOutput: true,
|
|
35
|
+
supportsCreditsCheck: true,
|
|
36
|
+
maxContextWindow: 131072
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
groq: {
|
|
40
|
+
id: "groq",
|
|
41
|
+
name: "Groq LPU",
|
|
42
|
+
baseUrl: "https://api.groq.com/openai",
|
|
43
|
+
modelsEndpoint: "/v1/models",
|
|
44
|
+
defaultHeaderKey: "Authorization",
|
|
45
|
+
supportsCreditsCheck: true,
|
|
46
|
+
docUrl: "https://groq.com",
|
|
47
|
+
description: "Ultra-low latency LPU hardware inference",
|
|
48
|
+
capabilities: {
|
|
49
|
+
supportsTools: true,
|
|
50
|
+
supportsVision: true,
|
|
51
|
+
supportsStreaming: true,
|
|
52
|
+
supportsJsonOutput: true,
|
|
53
|
+
supportsCreditsCheck: true,
|
|
54
|
+
maxContextWindow: 131072
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
huggingface: {
|
|
58
|
+
id: "huggingface",
|
|
59
|
+
name: "Hugging Face Hub",
|
|
60
|
+
baseUrl: "https://api-inference.huggingface.co",
|
|
61
|
+
modelsEndpoint: "/models",
|
|
62
|
+
defaultHeaderKey: "Authorization",
|
|
63
|
+
supportsCreditsCheck: false,
|
|
64
|
+
docUrl: "https://huggingface.co",
|
|
65
|
+
description: "Open source models & Inference Endpoints",
|
|
66
|
+
capabilities: {
|
|
67
|
+
supportsTools: false,
|
|
68
|
+
supportsVision: true,
|
|
69
|
+
supportsStreaming: true,
|
|
70
|
+
supportsJsonOutput: false,
|
|
71
|
+
supportsCreditsCheck: false,
|
|
72
|
+
maxContextWindow: 32768
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
openrouter: {
|
|
76
|
+
id: "openrouter",
|
|
77
|
+
name: "OpenRouter",
|
|
78
|
+
baseUrl: "https://openrouter.ai/api",
|
|
79
|
+
modelsEndpoint: "/v1/models",
|
|
80
|
+
defaultHeaderKey: "Authorization",
|
|
81
|
+
supportsCreditsCheck: true,
|
|
82
|
+
docUrl: "https://openrouter.ai",
|
|
83
|
+
description: "Unified gateway to 100+ multi-vendor models",
|
|
84
|
+
capabilities: {
|
|
85
|
+
supportsTools: true,
|
|
86
|
+
supportsVision: true,
|
|
87
|
+
supportsStreaming: true,
|
|
88
|
+
supportsJsonOutput: true,
|
|
89
|
+
supportsCreditsCheck: true,
|
|
90
|
+
maxContextWindow: 2e6
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
together: {
|
|
94
|
+
id: "together",
|
|
95
|
+
name: "Together AI",
|
|
96
|
+
baseUrl: "https://api.together.xyz",
|
|
97
|
+
modelsEndpoint: "/v1/models",
|
|
98
|
+
defaultHeaderKey: "Authorization",
|
|
99
|
+
supportsCreditsCheck: true,
|
|
100
|
+
docUrl: "https://together.ai",
|
|
101
|
+
description: "High-throughput open source models",
|
|
102
|
+
capabilities: {
|
|
103
|
+
supportsTools: true,
|
|
104
|
+
supportsVision: true,
|
|
105
|
+
supportsStreaming: true,
|
|
106
|
+
supportsJsonOutput: true,
|
|
107
|
+
supportsCreditsCheck: true,
|
|
108
|
+
maxContextWindow: 131072
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
nvidia: {
|
|
112
|
+
id: "nvidia",
|
|
113
|
+
name: "NVIDIA NIM",
|
|
114
|
+
baseUrl: "https://integrate.api.nvidia.com",
|
|
115
|
+
modelsEndpoint: "/v1/models",
|
|
116
|
+
defaultHeaderKey: "Authorization",
|
|
117
|
+
supportsCreditsCheck: true,
|
|
118
|
+
docUrl: "https://build.nvidia.com",
|
|
119
|
+
description: "NVIDIA NIM Enterprise & Open Models",
|
|
120
|
+
capabilities: {
|
|
121
|
+
supportsTools: true,
|
|
122
|
+
supportsVision: true,
|
|
123
|
+
supportsStreaming: true,
|
|
124
|
+
supportsJsonOutput: true,
|
|
125
|
+
supportsCreditsCheck: true,
|
|
126
|
+
maxContextWindow: 131072
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
var PROVIDER_COLORS = {
|
|
131
|
+
google: "#4285F4",
|
|
132
|
+
groq: "#F55036",
|
|
133
|
+
cerebras: "var(--skipo-success, #2a9d8f)",
|
|
134
|
+
openrouter: "var(--skipo-accent, #d4af37)",
|
|
135
|
+
together: "#8B5CF6",
|
|
136
|
+
nvidia: "#76B900",
|
|
137
|
+
huggingface: "#FFD21E",
|
|
138
|
+
anthropic: "#D97706",
|
|
139
|
+
openai: "#10A37F"
|
|
140
|
+
};
|
|
141
|
+
function getProviderMeta(providerId) {
|
|
142
|
+
return PROVIDERS_META[providerId];
|
|
143
|
+
}
|
|
144
|
+
function listProviders() {
|
|
145
|
+
return Object.values(PROVIDERS_META);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export {
|
|
149
|
+
PROVIDERS_META,
|
|
150
|
+
PROVIDER_COLORS,
|
|
151
|
+
getProviderMeta,
|
|
152
|
+
listProviders
|
|
153
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// src/ai-provider/AITransportClient.ts
|
|
2
|
+
var AITransportClient = class {
|
|
3
|
+
defaultTimeoutMs;
|
|
4
|
+
fetchImpl;
|
|
5
|
+
constructor(config) {
|
|
6
|
+
this.defaultTimeoutMs = config?.defaultTimeoutMs ?? 3e4;
|
|
7
|
+
this.fetchImpl = config?.customFetch ?? globalThis.fetch;
|
|
8
|
+
}
|
|
9
|
+
async request(req) {
|
|
10
|
+
const timeoutMs = req.timeoutMs ?? this.defaultTimeoutMs;
|
|
11
|
+
const controller = new AbortController();
|
|
12
|
+
let isTimedOut = false;
|
|
13
|
+
const timeoutId = setTimeout(() => {
|
|
14
|
+
isTimedOut = true;
|
|
15
|
+
controller.abort();
|
|
16
|
+
}, timeoutMs);
|
|
17
|
+
if (req.signal) {
|
|
18
|
+
if (req.signal.aborted) {
|
|
19
|
+
clearTimeout(timeoutId);
|
|
20
|
+
throw new Error("Request was aborted prior to execution");
|
|
21
|
+
}
|
|
22
|
+
req.signal.addEventListener("abort", () => controller.abort());
|
|
23
|
+
}
|
|
24
|
+
const headers = {
|
|
25
|
+
Accept: "application/json",
|
|
26
|
+
...req.headers || {}
|
|
27
|
+
};
|
|
28
|
+
let bodyPayload;
|
|
29
|
+
if (req.body !== void 0 && req.body !== null && req.method !== "GET" && req.method !== "HEAD") {
|
|
30
|
+
if (!headers["Content-Type"] && !headers["content-type"]) {
|
|
31
|
+
headers["Content-Type"] = "application/json";
|
|
32
|
+
}
|
|
33
|
+
bodyPayload = typeof req.body === "string" ? req.body : JSON.stringify(req.body);
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
const response = await this.fetchImpl(req.url, {
|
|
37
|
+
method: req.method || "GET",
|
|
38
|
+
headers,
|
|
39
|
+
body: bodyPayload,
|
|
40
|
+
signal: controller.signal
|
|
41
|
+
});
|
|
42
|
+
const resHeaders = {};
|
|
43
|
+
response.headers.forEach((v, k) => {
|
|
44
|
+
resHeaders[k.toLowerCase()] = v;
|
|
45
|
+
});
|
|
46
|
+
const rawText = await response.text();
|
|
47
|
+
let data;
|
|
48
|
+
try {
|
|
49
|
+
data = JSON.parse(rawText);
|
|
50
|
+
} catch {
|
|
51
|
+
data = rawText;
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
status: response.status,
|
|
55
|
+
ok: response.ok,
|
|
56
|
+
headers: resHeaders,
|
|
57
|
+
data,
|
|
58
|
+
rawText
|
|
59
|
+
};
|
|
60
|
+
} catch (err) {
|
|
61
|
+
if (isTimedOut) {
|
|
62
|
+
throw new Error(`AI request timed out after ${timeoutMs}ms against ${req.url}`);
|
|
63
|
+
}
|
|
64
|
+
throw err;
|
|
65
|
+
} finally {
|
|
66
|
+
clearTimeout(timeoutId);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export {
|
|
72
|
+
AITransportClient
|
|
73
|
+
};
|
|
@@ -0,0 +1,173 @@
|
|
|
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
|
+
getModelById(id) {
|
|
140
|
+
return this.getModel(id);
|
|
141
|
+
}
|
|
142
|
+
recordModelSuccess(id) {
|
|
143
|
+
const model = this.getModel(id);
|
|
144
|
+
if (model) model.status = "active";
|
|
145
|
+
}
|
|
146
|
+
recordModelFailure(id) {
|
|
147
|
+
const model = this.getModel(id);
|
|
148
|
+
if (model) model.status = "offline";
|
|
149
|
+
}
|
|
150
|
+
createExecutionContract(modelId, providerId, keyId, intelligenceSpec) {
|
|
151
|
+
return {
|
|
152
|
+
contractId: `contract-${Date.now()}-${providerId}-${modelId}-${keyId}`
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
getModel(id) {
|
|
156
|
+
return this.customModels.get(id) ?? this.getAllModels().find((m) => m.id === id || m.id.endsWith(id));
|
|
157
|
+
}
|
|
158
|
+
registerModel(model) {
|
|
159
|
+
this.customModels.set(model.id, model);
|
|
160
|
+
}
|
|
161
|
+
getAvailableModels() {
|
|
162
|
+
return this.getAllModels().filter(
|
|
163
|
+
(m) => modelHealthService.isModelAvailable(m.providerId, m.id)
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
var modelCatalog = new ModelCatalogService();
|
|
168
|
+
|
|
169
|
+
export {
|
|
170
|
+
STATIC_MODELS_CATALOG,
|
|
171
|
+
ModelCatalogService,
|
|
172
|
+
modelCatalog
|
|
173
|
+
};
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import {
|
|
2
|
+
modelHealthService
|
|
3
|
+
} from "./chunk-Z3LXEA6P.js";
|
|
4
|
+
import {
|
|
5
|
+
aiProviderService
|
|
6
|
+
} from "./chunk-SLR6SIEU.js";
|
|
7
|
+
|
|
8
|
+
// src/ai-routing/metricsLogger.ts
|
|
9
|
+
var MetricsLogger = class {
|
|
10
|
+
records = [];
|
|
11
|
+
maxRecords = 2e3;
|
|
12
|
+
logRequest(record) {
|
|
13
|
+
this.log({
|
|
14
|
+
providerId: record.providerId,
|
|
15
|
+
modelId: record.modelId,
|
|
16
|
+
durationMs: record.latencyMs,
|
|
17
|
+
success: record.status !== "failure"
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
log(record) {
|
|
21
|
+
const full = {
|
|
22
|
+
id: `met_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`,
|
|
23
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
24
|
+
...record
|
|
25
|
+
};
|
|
26
|
+
this.records.push(full);
|
|
27
|
+
if (this.records.length > this.maxRecords) {
|
|
28
|
+
this.records.shift();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
getRecent(limit = 50) {
|
|
32
|
+
return this.records.slice(-limit);
|
|
33
|
+
}
|
|
34
|
+
getAverageLatency(providerId, modelId) {
|
|
35
|
+
const matches = this.records.filter(
|
|
36
|
+
(r) => r.providerId === providerId && (modelId ? r.modelId === modelId : true) && r.success
|
|
37
|
+
);
|
|
38
|
+
if (matches.length === 0) return 0;
|
|
39
|
+
const sum = matches.reduce((acc, r) => acc + r.durationMs, 0);
|
|
40
|
+
return Math.round(sum / matches.length);
|
|
41
|
+
}
|
|
42
|
+
getSuccessRate(providerId) {
|
|
43
|
+
const matches = this.records.filter((r) => r.providerId === providerId);
|
|
44
|
+
if (matches.length === 0) return 100;
|
|
45
|
+
const successes = matches.filter((r) => r.success).length;
|
|
46
|
+
return Math.round(successes / matches.length * 100);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var metricsLogger = new MetricsLogger();
|
|
50
|
+
|
|
51
|
+
// src/ai-routing/healthCheckService.ts
|
|
52
|
+
var HealthCheckService = class {
|
|
53
|
+
async checkProvider(providerId, apiKey) {
|
|
54
|
+
const status = await aiProviderService.checkHealth(providerId, apiKey);
|
|
55
|
+
return status.isOnline;
|
|
56
|
+
}
|
|
57
|
+
isRouteAvailable(providerId, modelId) {
|
|
58
|
+
return modelHealthService.isModelAvailable(providerId, modelId);
|
|
59
|
+
}
|
|
60
|
+
recordRouteFailure(providerId, modelId, error) {
|
|
61
|
+
modelHealthService.recordFailure(providerId, modelId, error);
|
|
62
|
+
}
|
|
63
|
+
recordRouteSuccess(providerId, modelId, latencyMs) {
|
|
64
|
+
modelHealthService.recordSuccess(providerId, modelId, latencyMs);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
var healthCheckService = new HealthCheckService();
|
|
68
|
+
|
|
69
|
+
// src/ai-routing/failoverEngine.ts
|
|
70
|
+
var FailoverExecutionEngine = class {
|
|
71
|
+
defaultRetryPolicy = {
|
|
72
|
+
maxRetries: 3,
|
|
73
|
+
backoffMs: 250
|
|
74
|
+
};
|
|
75
|
+
async executeWithFailover(candidates, executeCandidate, policy) {
|
|
76
|
+
const start = Date.now();
|
|
77
|
+
const effectivePolicy = { ...this.defaultRetryPolicy, ...policy };
|
|
78
|
+
const failoverHistory = [];
|
|
79
|
+
const healthyCandidates = candidates.filter(
|
|
80
|
+
(c) => healthCheckService.isRouteAvailable(c.providerId, c.modelId)
|
|
81
|
+
);
|
|
82
|
+
const candidateQueue = healthyCandidates.length > 0 ? healthyCandidates : candidates;
|
|
83
|
+
let attempts = 0;
|
|
84
|
+
for (const candidate of candidateQueue) {
|
|
85
|
+
if (attempts >= effectivePolicy.maxRetries) break;
|
|
86
|
+
attempts++;
|
|
87
|
+
const attemptStart = Date.now();
|
|
88
|
+
try {
|
|
89
|
+
const result = await executeCandidate(candidate);
|
|
90
|
+
const duration = Date.now() - attemptStart;
|
|
91
|
+
healthCheckService.recordRouteSuccess(candidate.providerId, candidate.modelId, duration);
|
|
92
|
+
metricsLogger.log({
|
|
93
|
+
providerId: candidate.providerId,
|
|
94
|
+
modelId: candidate.modelId,
|
|
95
|
+
durationMs: duration,
|
|
96
|
+
success: true
|
|
97
|
+
});
|
|
98
|
+
return {
|
|
99
|
+
success: true,
|
|
100
|
+
selectedCandidate: candidate,
|
|
101
|
+
attempts,
|
|
102
|
+
result,
|
|
103
|
+
totalDurationMs: Date.now() - start,
|
|
104
|
+
failoverHistory
|
|
105
|
+
};
|
|
106
|
+
} catch (err) {
|
|
107
|
+
const duration = Date.now() - attemptStart;
|
|
108
|
+
const errMessage = err instanceof Error ? err.message : String(err);
|
|
109
|
+
healthCheckService.recordRouteFailure(candidate.providerId, candidate.modelId, errMessage);
|
|
110
|
+
metricsLogger.log({
|
|
111
|
+
providerId: candidate.providerId,
|
|
112
|
+
modelId: candidate.modelId,
|
|
113
|
+
durationMs: duration,
|
|
114
|
+
success: false,
|
|
115
|
+
error: errMessage
|
|
116
|
+
});
|
|
117
|
+
failoverHistory.push({
|
|
118
|
+
candidate,
|
|
119
|
+
error: errMessage,
|
|
120
|
+
durationMs: duration
|
|
121
|
+
});
|
|
122
|
+
if (effectivePolicy.backoffMs > 0 && attempts < effectivePolicy.maxRetries) {
|
|
123
|
+
await new Promise((r) => setTimeout(r, effectivePolicy.backoffMs));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
success: false,
|
|
129
|
+
attempts,
|
|
130
|
+
error: `All ${attempts} candidate execution attempts failed`,
|
|
131
|
+
totalDurationMs: Date.now() - start,
|
|
132
|
+
failoverHistory
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
var failoverExecutionEngine = new FailoverExecutionEngine();
|
|
137
|
+
|
|
138
|
+
export {
|
|
139
|
+
MetricsLogger,
|
|
140
|
+
metricsLogger,
|
|
141
|
+
HealthCheckService,
|
|
142
|
+
healthCheckService,
|
|
143
|
+
FailoverExecutionEngine,
|
|
144
|
+
failoverExecutionEngine
|
|
145
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// src/ai-tools/mcpEngine.ts
|
|
2
|
+
var MCPEngine = class {
|
|
3
|
+
servers = /* @__PURE__ */ new Map();
|
|
4
|
+
registeredTools = /* @__PURE__ */ new Map();
|
|
5
|
+
toolHandlers = /* @__PURE__ */ new Map();
|
|
6
|
+
registerServer(config) {
|
|
7
|
+
this.servers.set(config.id, config);
|
|
8
|
+
}
|
|
9
|
+
getServer(id) {
|
|
10
|
+
return this.servers.get(id);
|
|
11
|
+
}
|
|
12
|
+
listServers() {
|
|
13
|
+
return Array.from(this.servers.values());
|
|
14
|
+
}
|
|
15
|
+
registerTool(tool, handler) {
|
|
16
|
+
this.registeredTools.set(tool.name, tool);
|
|
17
|
+
if (handler) {
|
|
18
|
+
this.toolHandlers.set(tool.name, handler);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
listTools() {
|
|
22
|
+
return Array.from(this.registeredTools.values());
|
|
23
|
+
}
|
|
24
|
+
async executeTool(toolName, args) {
|
|
25
|
+
const start = Date.now();
|
|
26
|
+
const handler = this.toolHandlers.get(toolName);
|
|
27
|
+
if (!handler) {
|
|
28
|
+
return {
|
|
29
|
+
toolName,
|
|
30
|
+
success: false,
|
|
31
|
+
error: `No local handler registered for MCP tool: ${toolName}`,
|
|
32
|
+
durationMs: Date.now() - start
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
const output = await handler(args);
|
|
37
|
+
return {
|
|
38
|
+
toolName,
|
|
39
|
+
success: true,
|
|
40
|
+
output,
|
|
41
|
+
durationMs: Date.now() - start
|
|
42
|
+
};
|
|
43
|
+
} catch (err) {
|
|
44
|
+
return {
|
|
45
|
+
toolName,
|
|
46
|
+
success: false,
|
|
47
|
+
error: err instanceof Error ? err.message : String(err),
|
|
48
|
+
durationMs: Date.now() - start
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
var mcpEngine = new MCPEngine();
|
|
54
|
+
|
|
55
|
+
// src/ai-tools/ToolManagerService.ts
|
|
56
|
+
var ToolManagerService = class {
|
|
57
|
+
registerTool(tool, handler) {
|
|
58
|
+
mcpEngine.registerTool(tool, handler);
|
|
59
|
+
}
|
|
60
|
+
listTools() {
|
|
61
|
+
return mcpEngine.listTools();
|
|
62
|
+
}
|
|
63
|
+
async execute(toolName, args) {
|
|
64
|
+
return mcpEngine.executeTool(toolName, args);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
var toolManagerService = new ToolManagerService();
|
|
68
|
+
|
|
69
|
+
export {
|
|
70
|
+
MCPEngine,
|
|
71
|
+
mcpEngine,
|
|
72
|
+
ToolManagerService,
|
|
73
|
+
toolManagerService
|
|
74
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AITransportClient
|
|
3
|
+
} from "./chunk-MAMK4GDH.js";
|
|
4
|
+
import {
|
|
5
|
+
getProviderMeta,
|
|
6
|
+
listProviders
|
|
7
|
+
} from "./chunk-JSQZAFBU.js";
|
|
8
|
+
|
|
9
|
+
// src/ai-provider/AIProviderService.ts
|
|
10
|
+
var AIProviderService = class {
|
|
11
|
+
client;
|
|
12
|
+
customProviders = /* @__PURE__ */ new Map();
|
|
13
|
+
constructor(client) {
|
|
14
|
+
this.client = client ?? new AITransportClient();
|
|
15
|
+
}
|
|
16
|
+
listProviders() {
|
|
17
|
+
const builtin = listProviders();
|
|
18
|
+
const custom = Array.from(this.customProviders.values());
|
|
19
|
+
return [...builtin, ...custom];
|
|
20
|
+
}
|
|
21
|
+
getProvider(id) {
|
|
22
|
+
return this.customProviders.get(id) ?? getProviderMeta(id);
|
|
23
|
+
}
|
|
24
|
+
registerCustomProvider(meta) {
|
|
25
|
+
this.customProviders.set(meta.id, meta);
|
|
26
|
+
}
|
|
27
|
+
async checkHealth(id, apiKey) {
|
|
28
|
+
const meta = this.getProvider(id);
|
|
29
|
+
const start = Date.now();
|
|
30
|
+
if (!meta) {
|
|
31
|
+
return {
|
|
32
|
+
id,
|
|
33
|
+
isOnline: false,
|
|
34
|
+
latencyMs: 0,
|
|
35
|
+
lastCheckedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
36
|
+
error: `Unknown provider ${id}`
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const url = `${meta.baseUrl}${meta.modelsEndpoint}`;
|
|
41
|
+
const headers = {};
|
|
42
|
+
if (apiKey) {
|
|
43
|
+
if (meta.defaultHeaderKey === "Authorization") {
|
|
44
|
+
headers["Authorization"] = `Bearer ${apiKey.trim()}`;
|
|
45
|
+
} else {
|
|
46
|
+
headers[meta.defaultHeaderKey] = apiKey.trim();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const res = await this.client.request({
|
|
50
|
+
url,
|
|
51
|
+
method: "GET",
|
|
52
|
+
headers,
|
|
53
|
+
timeoutMs: 8e3
|
|
54
|
+
});
|
|
55
|
+
return {
|
|
56
|
+
id,
|
|
57
|
+
isOnline: res.status < 500,
|
|
58
|
+
latencyMs: Date.now() - start,
|
|
59
|
+
lastCheckedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
60
|
+
};
|
|
61
|
+
} catch (err) {
|
|
62
|
+
return {
|
|
63
|
+
id,
|
|
64
|
+
isOnline: false,
|
|
65
|
+
latencyMs: Date.now() - start,
|
|
66
|
+
lastCheckedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
67
|
+
error: err instanceof Error ? err.message : String(err)
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
var aiProviderService = new AIProviderService();
|
|
73
|
+
|
|
74
|
+
export {
|
|
75
|
+
AIProviderService,
|
|
76
|
+
aiProviderService
|
|
77
|
+
};
|
|
File without changes
|