@ultrst-armada/kloks 1.12.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.
- 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 +88 -0
- package/packages/backend/dist/ai-keys/index.js +16 -0
- package/packages/backend/dist/ai-models/index.d.ts +85 -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 +84 -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-4IYKKD53.js +178 -0
- package/packages/backend/dist/chunk-6GQNYRD6.js +32 -0
- package/packages/backend/dist/chunk-AM2PLPNE.js +157 -0
- package/packages/backend/dist/chunk-ERDBD4TH.js +137 -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-NITVHSB3.js +225 -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-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 +230 -103
- 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 +4 -4
- package/packages/backend/dist/storage/index.js +7 -7
- package/packages/backend/dist/types-2p2AhboE.d.ts +33 -0
- package/packages/backend/package.json +36 -1
- package/packages/backend/dist/{chunk-UMEO6XS4.js → chunk-T26VJ22Y.js} +12 -12
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// src/ai-inference/errorParser.ts
|
|
2
|
+
function parseErrorJson(rawMsg) {
|
|
3
|
+
try {
|
|
4
|
+
if (typeof rawMsg === "string") {
|
|
5
|
+
const trimmed = rawMsg.trim();
|
|
6
|
+
if (trimmed.startsWith("{") && trimmed.endsWith("}") || trimmed.startsWith("[") && trimmed.endsWith("]")) {
|
|
7
|
+
return JSON.parse(trimmed);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
} catch {
|
|
11
|
+
}
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
function classifyAndFormatError(providerId, modelId, rawError, httpStatus) {
|
|
15
|
+
const rawMsg = rawError instanceof Error ? rawError.message : String(rawError || "");
|
|
16
|
+
const parsedJson = parseErrorJson(rawMsg);
|
|
17
|
+
const errorObj = parsedJson?.error || parsedJson;
|
|
18
|
+
const statusNumber = httpStatus || (typeof errorObj?.code === "number" ? errorObj.code : void 0) || (typeof rawError?.status === "number" ? rawError.status : void 0);
|
|
19
|
+
const statusStr = String(errorObj?.status || rawError?.status || "");
|
|
20
|
+
const detailedMsg = typeof errorObj?.message === "string" ? errorObj.message : typeof errorObj === "string" ? errorObj : rawMsg;
|
|
21
|
+
const combinedLower = (detailedMsg + " " + statusStr + " " + (rawError?.name || "")).toLowerCase();
|
|
22
|
+
let classification = "UNKNOWN";
|
|
23
|
+
let suggestion = "Check network connectivity or retry request.";
|
|
24
|
+
let isModelSpecific = false;
|
|
25
|
+
let isKeySpecific = false;
|
|
26
|
+
if (statusNumber === 404 || statusStr === "NOT_FOUND" || combinedLower.includes("not found") || combinedLower.includes("deprecated")) {
|
|
27
|
+
classification = "MODEL_UNAVAILABLE";
|
|
28
|
+
isModelSpecific = true;
|
|
29
|
+
suggestion = "Auto-failover router will bypass this model for subsequent calls.";
|
|
30
|
+
} else if (statusNumber === 401 || statusNumber === 403 || combinedLower.includes("unauthorized") || combinedLower.includes("invalid api key")) {
|
|
31
|
+
classification = "AUTH_FAILURE";
|
|
32
|
+
isKeySpecific = true;
|
|
33
|
+
suggestion = "Check key validity in Key Manager or rotate key.";
|
|
34
|
+
} else if (statusNumber === 429 || combinedLower.includes("rate limit") || combinedLower.includes("quota exceeded")) {
|
|
35
|
+
classification = "RATE_LIMITED";
|
|
36
|
+
isKeySpecific = true;
|
|
37
|
+
suggestion = "Failover router will immediately switch to alternate keys/models.";
|
|
38
|
+
} else if (statusNumber === 402 || combinedLower.includes("insufficient credits") || combinedLower.includes("billing")) {
|
|
39
|
+
classification = "PAYMENT_REQUIRED";
|
|
40
|
+
isKeySpecific = true;
|
|
41
|
+
suggestion = "Add funds or switch to a free tier provider.";
|
|
42
|
+
} else if (combinedLower.includes("maximum context length") || combinedLower.includes("too many tokens")) {
|
|
43
|
+
classification = "CONTEXT_OVERFLOW";
|
|
44
|
+
isModelSpecific = true;
|
|
45
|
+
suggestion = "Enable compression or route to a larger context model.";
|
|
46
|
+
} else if (statusNumber === 400 || combinedLower.includes("bad request")) {
|
|
47
|
+
classification = "PARAMETER_MISMATCH";
|
|
48
|
+
isModelSpecific = true;
|
|
49
|
+
suggestion = "Re-check model ID in catalog or verify API key syntax.";
|
|
50
|
+
} else if (statusNumber && statusNumber >= 500) {
|
|
51
|
+
classification = "SERVER_ERROR";
|
|
52
|
+
}
|
|
53
|
+
const formatted = `[${providerId.toUpperCase()} ${classification}${statusNumber ? ` (${statusNumber})` : ""}]: ${detailedMsg}`;
|
|
54
|
+
return {
|
|
55
|
+
error: new Error(formatted),
|
|
56
|
+
details: {
|
|
57
|
+
classification,
|
|
58
|
+
providerId,
|
|
59
|
+
modelId,
|
|
60
|
+
statusCode: statusNumber,
|
|
61
|
+
message: formatted,
|
|
62
|
+
suggestion,
|
|
63
|
+
isModelSpecific,
|
|
64
|
+
isKeySpecific
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/ai-inference/inferenceExecutor.ts
|
|
70
|
+
var PROVIDER_BASE_URLS = {
|
|
71
|
+
groq: "https://api.groq.com/openai/v1",
|
|
72
|
+
openrouter: "https://openrouter.ai/api/v1",
|
|
73
|
+
cerebras: "https://api.cerebras.ai/v1",
|
|
74
|
+
together: "https://api.together.xyz/v1",
|
|
75
|
+
nvidia: "https://integrate.api.nvidia.com/v1",
|
|
76
|
+
huggingface: "https://api-inference.huggingface.co/v1"
|
|
77
|
+
};
|
|
78
|
+
async function executeGoogleInference(modelId, apiKey, prompt, options) {
|
|
79
|
+
const normModel = modelId.startsWith("models/") ? modelId.replace("models/", "") : modelId;
|
|
80
|
+
const url = `https://generativelanguage.googleapis.com/v1beta/models/${normModel}:generateContent?key=${apiKey}`;
|
|
81
|
+
const reqBody = {
|
|
82
|
+
contents: [{ parts: [{ text: prompt }] }]
|
|
83
|
+
};
|
|
84
|
+
if (options?.tools && options.tools.length > 0) {
|
|
85
|
+
reqBody.tools = [{
|
|
86
|
+
functionDeclarations: options.tools.map((t) => ({
|
|
87
|
+
name: t.function?.name || t.name,
|
|
88
|
+
description: t.function?.description || t.description || "",
|
|
89
|
+
parameters: t.function?.parameters || t.parameters || { type: "OBJECT", properties: {} }
|
|
90
|
+
}))
|
|
91
|
+
}];
|
|
92
|
+
}
|
|
93
|
+
const res = await fetch(url, {
|
|
94
|
+
method: "POST",
|
|
95
|
+
headers: { "Content-Type": "application/json" },
|
|
96
|
+
body: JSON.stringify(reqBody),
|
|
97
|
+
signal: options?.signal
|
|
98
|
+
});
|
|
99
|
+
if (!res.ok) {
|
|
100
|
+
const txt = await res.text().catch(() => "");
|
|
101
|
+
throw classifyAndFormatError("google", normModel, txt, res.status).error;
|
|
102
|
+
}
|
|
103
|
+
const data = await res.json();
|
|
104
|
+
const candidate = data.candidates?.[0];
|
|
105
|
+
const parts = candidate?.content?.parts || [];
|
|
106
|
+
const textParts = parts.filter((p) => p.text).map((p) => p.text).join("\n");
|
|
107
|
+
const fcList = parts.filter((p) => p.functionCall).map((p) => p.functionCall);
|
|
108
|
+
let toolCalls;
|
|
109
|
+
if (fcList.length > 0) {
|
|
110
|
+
toolCalls = fcList.map((fc, i) => ({
|
|
111
|
+
id: `call_gemini_${Date.now()}_${i}`,
|
|
112
|
+
type: "function",
|
|
113
|
+
function: { name: fc.name, arguments: JSON.stringify(fc.args || {}) }
|
|
114
|
+
}));
|
|
115
|
+
}
|
|
116
|
+
return { text: textParts || (toolCalls ? `Invoked tool(s)` : ""), toolCalls };
|
|
117
|
+
}
|
|
118
|
+
async function performInference(providerId, modelId, apiKey, prompt, options) {
|
|
119
|
+
const start = Date.now();
|
|
120
|
+
const normModel = modelId.startsWith("models/") ? modelId.replace("models/", "") : modelId;
|
|
121
|
+
if (providerId === "google") {
|
|
122
|
+
const res2 = await executeGoogleInference(normModel, apiKey, prompt, options);
|
|
123
|
+
res2.durationMs = Date.now() - start;
|
|
124
|
+
return res2;
|
|
125
|
+
}
|
|
126
|
+
if (!apiKey || apiKey.trim().length === 0) {
|
|
127
|
+
throw new Error(`[${providerId.toUpperCase()} AUTH]: Missing API key for ${providerId}`);
|
|
128
|
+
}
|
|
129
|
+
const baseUrl = PROVIDER_BASE_URLS[providerId] || "https://api.groq.com/openai/v1";
|
|
130
|
+
const reqBody = {
|
|
131
|
+
model: normModel,
|
|
132
|
+
messages: options?.messages || [{ role: "user", content: prompt }],
|
|
133
|
+
temperature: options?.temperature ?? 0.7
|
|
134
|
+
};
|
|
135
|
+
if (options?.maxTokens) reqBody.max_tokens = options.maxTokens;
|
|
136
|
+
if (options?.tools && options.tools.length > 0) {
|
|
137
|
+
reqBody.tools = options.tools;
|
|
138
|
+
if (options.tool_choice) reqBody.tool_choice = options.tool_choice;
|
|
139
|
+
}
|
|
140
|
+
const headers = {
|
|
141
|
+
"Content-Type": "application/json",
|
|
142
|
+
Authorization: `Bearer ${apiKey.trim().replace(/[\r\n\t]/g, "")}`
|
|
143
|
+
};
|
|
144
|
+
if (providerId === "openrouter") {
|
|
145
|
+
headers["HTTP-Referer"] = "https://kloks-ai.dev";
|
|
146
|
+
headers["X-Title"] = "Kloks AI Gateway";
|
|
147
|
+
}
|
|
148
|
+
const res = await fetch(`${baseUrl}/chat/completions`, {
|
|
149
|
+
method: "POST",
|
|
150
|
+
headers,
|
|
151
|
+
body: JSON.stringify(reqBody),
|
|
152
|
+
signal: options?.signal
|
|
153
|
+
});
|
|
154
|
+
if (!res.ok) {
|
|
155
|
+
const txt = await res.text().catch(() => "");
|
|
156
|
+
throw classifyAndFormatError(providerId, normModel, txt, res.status).error;
|
|
157
|
+
}
|
|
158
|
+
const data = await res.json();
|
|
159
|
+
const choice = data.choices?.[0]?.message;
|
|
160
|
+
let toolCalls;
|
|
161
|
+
if (choice?.tool_calls && Array.isArray(choice.tool_calls) && choice.tool_calls.length > 0) {
|
|
162
|
+
toolCalls = choice.tool_calls.map((tc, i) => ({
|
|
163
|
+
id: tc.id || `call_${Date.now()}_${i}`,
|
|
164
|
+
type: "function",
|
|
165
|
+
function: {
|
|
166
|
+
name: tc.function?.name || "unknown_tool",
|
|
167
|
+
arguments: typeof tc.function?.arguments === "string" ? tc.function.arguments : JSON.stringify(tc.function?.arguments || {})
|
|
168
|
+
}
|
|
169
|
+
}));
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
text: choice?.content || (toolCalls ? `Tool usage invoked` : ""),
|
|
173
|
+
toolCalls,
|
|
174
|
+
usage: data.usage ? {
|
|
175
|
+
promptTokens: data.usage.prompt_tokens,
|
|
176
|
+
completionTokens: data.usage.completion_tokens,
|
|
177
|
+
totalTokens: data.usage.total_tokens
|
|
178
|
+
} : void 0,
|
|
179
|
+
durationMs: Date.now() - start
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
var InferenceExecutorService = class {
|
|
183
|
+
async execute(providerId, modelId, apiKey, prompt, options) {
|
|
184
|
+
return performInference(providerId, modelId, apiKey, prompt, options);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
var inferenceExecutor = new InferenceExecutorService();
|
|
188
|
+
|
|
189
|
+
export {
|
|
190
|
+
parseErrorJson,
|
|
191
|
+
classifyAndFormatError,
|
|
192
|
+
performInference,
|
|
193
|
+
InferenceExecutorService,
|
|
194
|
+
inferenceExecutor
|
|
195
|
+
};
|
|
File without changes
|
|
@@ -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,225 @@
|
|
|
1
|
+
import {
|
|
2
|
+
toolManagerService
|
|
3
|
+
} from "./chunk-NSKVYL74.js";
|
|
4
|
+
import {
|
|
5
|
+
createService,
|
|
6
|
+
createServiceDefinition
|
|
7
|
+
} from "./chunk-TY32TUNI.js";
|
|
8
|
+
import {
|
|
9
|
+
inferenceExecutor
|
|
10
|
+
} from "./chunk-GCTQY24J.js";
|
|
11
|
+
import {
|
|
12
|
+
keyStore
|
|
13
|
+
} from "./chunk-4IYKKD53.js";
|
|
14
|
+
import {
|
|
15
|
+
modelCatalog
|
|
16
|
+
} from "./chunk-AM2PLPNE.js";
|
|
17
|
+
import {
|
|
18
|
+
failoverExecutionEngine
|
|
19
|
+
} from "./chunk-ERDBD4TH.js";
|
|
20
|
+
import {
|
|
21
|
+
aiProviderService
|
|
22
|
+
} from "./chunk-SLR6SIEU.js";
|
|
23
|
+
|
|
24
|
+
// src/krnl-ai-services.ts
|
|
25
|
+
var AIProviderServiceDefinition = createServiceDefinition({
|
|
26
|
+
id: "kloks.backend.aiProvider",
|
|
27
|
+
name: "AIProviderService",
|
|
28
|
+
version: "1.0.0",
|
|
29
|
+
operations: {
|
|
30
|
+
listProviders: {
|
|
31
|
+
id: "AIProviderService.listProviders",
|
|
32
|
+
name: "listProviders",
|
|
33
|
+
handler: async () => aiProviderService.listProviders()
|
|
34
|
+
},
|
|
35
|
+
getProvider: {
|
|
36
|
+
id: "AIProviderService.getProvider",
|
|
37
|
+
name: "getProvider",
|
|
38
|
+
handler: async (input) => aiProviderService.getProvider(input.id)
|
|
39
|
+
},
|
|
40
|
+
checkHealth: {
|
|
41
|
+
id: "AIProviderService.checkHealth",
|
|
42
|
+
name: "checkHealth",
|
|
43
|
+
handler: async (input) => aiProviderService.checkHealth(input.id, input.apiKey)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
var aiProviderInstance = createService(AIProviderServiceDefinition);
|
|
48
|
+
var AIProviderServiceFacade = {
|
|
49
|
+
id: AIProviderServiceDefinition.id,
|
|
50
|
+
name: AIProviderServiceDefinition.name,
|
|
51
|
+
version: AIProviderServiceDefinition.version,
|
|
52
|
+
runtime: "server",
|
|
53
|
+
deployment: "local",
|
|
54
|
+
definition: AIProviderServiceDefinition,
|
|
55
|
+
createInstance: () => createService(AIProviderServiceDefinition),
|
|
56
|
+
execute: async (op, input) => {
|
|
57
|
+
await aiProviderInstance.start();
|
|
58
|
+
return aiProviderInstance.call(op, input);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var AIInferenceServiceDefinition = createServiceDefinition({
|
|
62
|
+
id: "kloks.backend.aiInference",
|
|
63
|
+
name: "AIInferenceService",
|
|
64
|
+
version: "1.0.0",
|
|
65
|
+
operations: {
|
|
66
|
+
execute: {
|
|
67
|
+
id: "AIInferenceService.execute",
|
|
68
|
+
name: "execute",
|
|
69
|
+
handler: async (input) => inferenceExecutor.execute(input.providerId, input.modelId, input.apiKey, input.prompt, input.options)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
var aiInferenceInstance = createService(AIInferenceServiceDefinition);
|
|
74
|
+
var AIInferenceServiceFacade = {
|
|
75
|
+
id: AIInferenceServiceDefinition.id,
|
|
76
|
+
name: AIInferenceServiceDefinition.name,
|
|
77
|
+
version: AIInferenceServiceDefinition.version,
|
|
78
|
+
runtime: "server",
|
|
79
|
+
deployment: "local",
|
|
80
|
+
definition: AIInferenceServiceDefinition,
|
|
81
|
+
createInstance: () => createService(AIInferenceServiceDefinition),
|
|
82
|
+
execute: async (op, input) => {
|
|
83
|
+
await aiInferenceInstance.start();
|
|
84
|
+
return aiInferenceInstance.call(op, input);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
var AIModelCatalogServiceDefinition = createServiceDefinition({
|
|
88
|
+
id: "kloks.backend.aiModels",
|
|
89
|
+
name: "AIModelCatalogService",
|
|
90
|
+
version: "1.0.0",
|
|
91
|
+
operations: {
|
|
92
|
+
getAllModels: {
|
|
93
|
+
id: "AIModelCatalogService.getAllModels",
|
|
94
|
+
name: "getAllModels",
|
|
95
|
+
handler: async () => modelCatalog.getAllModels()
|
|
96
|
+
},
|
|
97
|
+
getModel: {
|
|
98
|
+
id: "AIModelCatalogService.getModel",
|
|
99
|
+
name: "getModel",
|
|
100
|
+
handler: async (input) => modelCatalog.getModel(input.id)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
var aiModelCatalogInstance = createService(AIModelCatalogServiceDefinition);
|
|
105
|
+
var AIModelCatalogServiceFacade = {
|
|
106
|
+
id: AIModelCatalogServiceDefinition.id,
|
|
107
|
+
name: AIModelCatalogServiceDefinition.name,
|
|
108
|
+
version: AIModelCatalogServiceDefinition.version,
|
|
109
|
+
runtime: "server",
|
|
110
|
+
deployment: "local",
|
|
111
|
+
definition: AIModelCatalogServiceDefinition,
|
|
112
|
+
createInstance: () => createService(AIModelCatalogServiceDefinition),
|
|
113
|
+
execute: async (op, input) => {
|
|
114
|
+
await aiModelCatalogInstance.start();
|
|
115
|
+
return aiModelCatalogInstance.call(op, input);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
var AIRoutingServiceDefinition = createServiceDefinition({
|
|
119
|
+
id: "kloks.backend.aiRouting",
|
|
120
|
+
name: "AIRoutingService",
|
|
121
|
+
version: "1.0.0",
|
|
122
|
+
operations: {
|
|
123
|
+
executeWithFailover: {
|
|
124
|
+
id: "AIRoutingService.executeWithFailover",
|
|
125
|
+
name: "executeWithFailover",
|
|
126
|
+
handler: async (input) => failoverExecutionEngine.executeWithFailover(input.candidates, input.executeFn, input.policy)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
var aiRoutingInstance = createService(AIRoutingServiceDefinition);
|
|
131
|
+
var AIRoutingServiceFacade = {
|
|
132
|
+
id: AIRoutingServiceDefinition.id,
|
|
133
|
+
name: AIRoutingServiceDefinition.name,
|
|
134
|
+
version: AIRoutingServiceDefinition.version,
|
|
135
|
+
runtime: "server",
|
|
136
|
+
deployment: "local",
|
|
137
|
+
definition: AIRoutingServiceDefinition,
|
|
138
|
+
createInstance: () => createService(AIRoutingServiceDefinition),
|
|
139
|
+
execute: async (op, input) => {
|
|
140
|
+
await aiRoutingInstance.start();
|
|
141
|
+
return aiRoutingInstance.call(op, input);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
var AIKeyServiceDefinition = createServiceDefinition({
|
|
145
|
+
id: "kloks.backend.aiKeys",
|
|
146
|
+
name: "AIKeyService",
|
|
147
|
+
version: "1.0.0",
|
|
148
|
+
operations: {
|
|
149
|
+
getKeys: {
|
|
150
|
+
id: "AIKeyService.getKeys",
|
|
151
|
+
name: "getKeys",
|
|
152
|
+
handler: async (input) => keyStore.getKeys(input?.providerId)
|
|
153
|
+
},
|
|
154
|
+
getActiveKey: {
|
|
155
|
+
id: "AIKeyService.getActiveKey",
|
|
156
|
+
name: "getActiveKey",
|
|
157
|
+
handler: async (input) => keyStore.getActiveKey(input.providerId)
|
|
158
|
+
},
|
|
159
|
+
verifyKey: {
|
|
160
|
+
id: "AIKeyService.verifyKey",
|
|
161
|
+
name: "verifyKey",
|
|
162
|
+
handler: async (input) => keyStore.verifyKey(input.id)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
var aiKeyInstance = createService(AIKeyServiceDefinition);
|
|
167
|
+
var AIKeyServiceFacade = {
|
|
168
|
+
id: AIKeyServiceDefinition.id,
|
|
169
|
+
name: AIKeyServiceDefinition.name,
|
|
170
|
+
version: AIKeyServiceDefinition.version,
|
|
171
|
+
runtime: "server",
|
|
172
|
+
deployment: "local",
|
|
173
|
+
definition: AIKeyServiceDefinition,
|
|
174
|
+
createInstance: () => createService(AIKeyServiceDefinition),
|
|
175
|
+
execute: async (op, input) => {
|
|
176
|
+
await aiKeyInstance.start();
|
|
177
|
+
return aiKeyInstance.call(op, input);
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
var AIToolServiceDefinition = createServiceDefinition({
|
|
181
|
+
id: "kloks.backend.aiTools",
|
|
182
|
+
name: "AIToolService",
|
|
183
|
+
version: "1.0.0",
|
|
184
|
+
operations: {
|
|
185
|
+
listTools: {
|
|
186
|
+
id: "AIToolService.listTools",
|
|
187
|
+
name: "listTools",
|
|
188
|
+
handler: async () => toolManagerService.listTools()
|
|
189
|
+
},
|
|
190
|
+
execute: {
|
|
191
|
+
id: "AIToolService.execute",
|
|
192
|
+
name: "execute",
|
|
193
|
+
handler: async (input) => toolManagerService.execute(input.toolName, input.args)
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
var aiToolInstance = createService(AIToolServiceDefinition);
|
|
198
|
+
var AIToolServiceFacade = {
|
|
199
|
+
id: AIToolServiceDefinition.id,
|
|
200
|
+
name: AIToolServiceDefinition.name,
|
|
201
|
+
version: AIToolServiceDefinition.version,
|
|
202
|
+
runtime: "server",
|
|
203
|
+
deployment: "local",
|
|
204
|
+
definition: AIToolServiceDefinition,
|
|
205
|
+
createInstance: () => createService(AIToolServiceDefinition),
|
|
206
|
+
execute: async (op, input) => {
|
|
207
|
+
await aiToolInstance.start();
|
|
208
|
+
return aiToolInstance.call(op, input);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
export {
|
|
213
|
+
AIProviderServiceDefinition,
|
|
214
|
+
AIProviderServiceFacade,
|
|
215
|
+
AIInferenceServiceDefinition,
|
|
216
|
+
AIInferenceServiceFacade,
|
|
217
|
+
AIModelCatalogServiceDefinition,
|
|
218
|
+
AIModelCatalogServiceFacade,
|
|
219
|
+
AIRoutingServiceDefinition,
|
|
220
|
+
AIRoutingServiceFacade,
|
|
221
|
+
AIKeyServiceDefinition,
|
|
222
|
+
AIKeyServiceFacade,
|
|
223
|
+
AIToolServiceDefinition,
|
|
224
|
+
AIToolServiceFacade
|
|
225
|
+
};
|