@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.
Files changed (44) hide show
  1. package/.krusbl-release-attestation.json +3 -3
  2. package/package.json +36 -1
  3. package/packages/backend/dist/AITransportClient-dToGr8nj.d.ts +30 -0
  4. package/packages/backend/dist/ai-inference/index.d.ts +99 -0
  5. package/packages/backend/dist/ai-inference/index.js +21 -0
  6. package/packages/backend/dist/ai-keys/index.d.ts +99 -0
  7. package/packages/backend/dist/ai-keys/index.js +16 -0
  8. package/packages/backend/dist/ai-models/index.d.ts +91 -0
  9. package/packages/backend/dist/ai-models/index.js +24 -0
  10. package/packages/backend/dist/ai-provider/index.d.ts +32 -0
  11. package/packages/backend/dist/ai-provider/index.js +23 -0
  12. package/packages/backend/dist/ai-routing/index.d.ts +103 -0
  13. package/packages/backend/dist/ai-routing/index.js +21 -0
  14. package/packages/backend/dist/ai-tools/index.d.ts +80 -0
  15. package/packages/backend/dist/ai-tools/index.js +16 -0
  16. package/packages/backend/dist/chunk-2WT7OUHW.js +51 -0
  17. package/packages/backend/dist/chunk-4GWYTYDQ.js +201 -0
  18. package/packages/backend/dist/chunk-6GQNYRD6.js +32 -0
  19. package/packages/backend/dist/chunk-GCTQY24J.js +195 -0
  20. package/packages/backend/dist/chunk-IN3WRHXE.js +0 -0
  21. package/packages/backend/dist/chunk-JSQZAFBU.js +153 -0
  22. package/packages/backend/dist/chunk-LV55WKZR.js +0 -0
  23. package/packages/backend/dist/chunk-MAMK4GDH.js +73 -0
  24. package/packages/backend/dist/chunk-MFM2QIY5.js +173 -0
  25. package/packages/backend/dist/chunk-MSCOKLAO.js +145 -0
  26. package/packages/backend/dist/chunk-NSKVYL74.js +74 -0
  27. package/packages/backend/dist/chunk-SLR6SIEU.js +77 -0
  28. package/packages/backend/dist/chunk-WVDSMTW3.js +0 -0
  29. package/packages/backend/dist/chunk-XGPL43T3.js +225 -0
  30. package/packages/backend/dist/chunk-Z2FHVSNF.js +42 -0
  31. package/packages/backend/dist/chunk-Z3LXEA6P.js +45 -0
  32. package/packages/backend/dist/index.d.ts +9 -0
  33. package/packages/backend/dist/index.js +251 -124
  34. package/packages/backend/dist/krnl-ai-services.d.ts +71 -0
  35. package/packages/backend/dist/krnl-ai-services.js +38 -0
  36. package/packages/backend/dist/krnl-services.js +6 -6
  37. package/packages/backend/dist/services/index.js +1 -1
  38. package/packages/backend/dist/storage/index.js +7 -7
  39. package/packages/backend/dist/types-2p2AhboE.d.ts +33 -0
  40. package/packages/backend/dist/workspace/index.js +2 -2
  41. package/packages/backend/package.json +36 -1
  42. package/packages/state-graph/dist/index.js +15 -15
  43. package/packages/backend/dist/{chunk-UMEO6XS4.js → chunk-D6ZGQFWQ.js} +14 -14
  44. package/packages/backend/dist/{chunk-BTMIK44G.js → chunk-EIYYESEU.js} +3 -3
@@ -0,0 +1,80 @@
1
+ /**
2
+ * @file types.ts
3
+ * @description Canonical AI Tools and MCP runtime contracts for @ultrst-armada/kloks/backend
4
+ */
5
+ interface MCPToolParameter {
6
+ type: string;
7
+ description?: string;
8
+ properties?: Record<string, unknown>;
9
+ required?: string[];
10
+ items?: Record<string, unknown>;
11
+ }
12
+ interface MCPTool {
13
+ name: string;
14
+ description: string;
15
+ inputSchema: {
16
+ type: string;
17
+ properties?: Record<string, unknown>;
18
+ required?: string[];
19
+ };
20
+ }
21
+ interface MCPServerConfig {
22
+ id: string;
23
+ name: string;
24
+ transport: 'stdio' | 'sse' | 'websocket';
25
+ url?: string;
26
+ command?: string;
27
+ args?: string[];
28
+ env?: Record<string, string>;
29
+ status?: 'connected' | 'disconnected' | 'error';
30
+ }
31
+ interface ToolExecutionResult {
32
+ toolName: string;
33
+ success: boolean;
34
+ output?: unknown;
35
+ error?: string;
36
+ durationMs: number;
37
+ }
38
+
39
+ /**
40
+ * @file mcpEngine.ts
41
+ * @description Master Model Context Protocol (MCP) server manager and dispatcher
42
+ */
43
+
44
+ declare class MCPEngine {
45
+ private servers;
46
+ private registeredTools;
47
+ private toolHandlers;
48
+ registerServer(config: MCPServerConfig): void;
49
+ getServer(id: string): MCPServerConfig | undefined;
50
+ listServers(): MCPServerConfig[];
51
+ registerTool(tool: MCPTool, handler?: (args: any) => Promise<any>): void;
52
+ listTools(): MCPTool[];
53
+ executeTool(toolName: string, args: Record<string, unknown>): Promise<ToolExecutionResult>;
54
+ }
55
+ declare const mcpEngine: MCPEngine;
56
+
57
+ /**
58
+ * @file ToolManagerService.ts
59
+ * @description General-purpose Tool Registry and Lifecycle Orchestrator
60
+ */
61
+
62
+ declare class ToolManagerService {
63
+ registerTool(tool: MCPTool, handler?: (args: any) => Promise<any>): void;
64
+ listTools(): MCPTool[];
65
+ execute(toolName: string, args: Record<string, unknown>): Promise<ToolExecutionResult>;
66
+ }
67
+ declare const toolManagerService: ToolManagerService;
68
+
69
+ /**
70
+ * @file toolValidationLoop.ts
71
+ * @description Validates tool inputs and payload schemas before tool invocation
72
+ */
73
+
74
+ interface ToolInputValidationResult {
75
+ valid: boolean;
76
+ errors: string[];
77
+ }
78
+ declare function validateToolInput(tool: MCPTool, input: Record<string, unknown>): ToolInputValidationResult;
79
+
80
+ export { MCPEngine, type MCPServerConfig, type MCPTool, type MCPToolParameter, type ToolExecutionResult, type ToolInputValidationResult, ToolManagerService, mcpEngine, toolManagerService, validateToolInput };
@@ -0,0 +1,16 @@
1
+ import {
2
+ validateToolInput
3
+ } from "../chunk-6GQNYRD6.js";
4
+ import {
5
+ MCPEngine,
6
+ ToolManagerService,
7
+ mcpEngine,
8
+ toolManagerService
9
+ } from "../chunk-NSKVYL74.js";
10
+ export {
11
+ MCPEngine,
12
+ ToolManagerService,
13
+ mcpEngine,
14
+ toolManagerService,
15
+ validateToolInput
16
+ };
@@ -0,0 +1,51 @@
1
+ import {
2
+ AITransportClient
3
+ } from "./chunk-MAMK4GDH.js";
4
+ import {
5
+ getProviderMeta
6
+ } from "./chunk-JSQZAFBU.js";
7
+
8
+ // src/ai-models/endpointRefresher.ts
9
+ var EndpointRefresher = class {
10
+ client;
11
+ constructor(client) {
12
+ this.client = client ?? new AITransportClient();
13
+ }
14
+ async fetchRemoteModels(providerId, apiKey) {
15
+ const meta = getProviderMeta(providerId);
16
+ if (!meta) throw new Error(`Unknown provider ${providerId}`);
17
+ const url = `${meta.baseUrl}${meta.modelsEndpoint}`;
18
+ const headers = {};
19
+ if (meta.defaultHeaderKey === "Authorization") {
20
+ headers["Authorization"] = `Bearer ${apiKey.trim()}`;
21
+ } else {
22
+ headers[meta.defaultHeaderKey] = apiKey.trim();
23
+ }
24
+ const res = await this.client.request({
25
+ url,
26
+ method: "GET",
27
+ headers,
28
+ timeoutMs: 12e3
29
+ });
30
+ if (!res.ok) {
31
+ throw new Error(`Failed to fetch models from ${providerId}: HTTP ${res.status}`);
32
+ }
33
+ const rawList = res.data.data || res.data.models || [];
34
+ return rawList.map((item) => ({
35
+ id: item.id || item.name || "",
36
+ name: item.display_name || item.name || item.id || "",
37
+ providerId,
38
+ contextWindow: item.context_window || item.input_token_limit || 131072,
39
+ supportsTools: true,
40
+ supportsVision: Boolean(item.architecture?.modality?.includes("image")),
41
+ supportsJson: true,
42
+ status: "active"
43
+ }));
44
+ }
45
+ };
46
+ var endpointRefresher = new EndpointRefresher();
47
+
48
+ export {
49
+ EndpointRefresher,
50
+ endpointRefresher
51
+ };
@@ -0,0 +1,201 @@
1
+ import {
2
+ getProviderMeta
3
+ } from "./chunk-JSQZAFBU.js";
4
+
5
+ // src/ai-keys/keyValidator.ts
6
+ async function validateApiKey(providerId, apiKey) {
7
+ const meta = getProviderMeta(providerId);
8
+ const start = Date.now();
9
+ if (!apiKey || apiKey.trim().length === 0) {
10
+ return {
11
+ valid: false,
12
+ status: "invalid",
13
+ error: "Empty API key provided",
14
+ latencyMs: 0
15
+ };
16
+ }
17
+ if (!meta) {
18
+ return {
19
+ valid: true,
20
+ status: "valid",
21
+ latencyMs: 0
22
+ };
23
+ }
24
+ try {
25
+ const url = `${meta.baseUrl}${meta.modelsEndpoint}`;
26
+ const headers = {};
27
+ if (meta.defaultHeaderKey === "Authorization") {
28
+ headers["Authorization"] = `Bearer ${apiKey.trim()}`;
29
+ } else {
30
+ headers[meta.defaultHeaderKey] = apiKey.trim();
31
+ }
32
+ const controller = new AbortController();
33
+ const timer = setTimeout(() => controller.abort(), 8e3);
34
+ const res = await fetch(url, {
35
+ method: "GET",
36
+ headers,
37
+ signal: controller.signal
38
+ }).finally(() => clearTimeout(timer));
39
+ const latencyMs = Date.now() - start;
40
+ if (res.status === 401 || res.status === 403) {
41
+ return {
42
+ valid: false,
43
+ status: "invalid",
44
+ error: `Authentication failed (HTTP ${res.status})`,
45
+ latencyMs
46
+ };
47
+ }
48
+ if (res.status === 429) {
49
+ return {
50
+ valid: false,
51
+ status: "rate-limited",
52
+ error: "Rate limit or quota exceeded",
53
+ latencyMs
54
+ };
55
+ }
56
+ return {
57
+ valid: res.status < 500,
58
+ status: "valid",
59
+ latencyMs
60
+ };
61
+ } catch (err) {
62
+ return {
63
+ valid: false,
64
+ status: "invalid",
65
+ error: err instanceof Error ? err.message : String(err),
66
+ latencyMs: Date.now() - start
67
+ };
68
+ }
69
+ }
70
+
71
+ // src/ai-keys/keysStorage.ts
72
+ var KeysStorage = class {
73
+ inMemoryKeys = /* @__PURE__ */ new Map();
74
+ loadKeys() {
75
+ return Array.from(this.inMemoryKeys.values());
76
+ }
77
+ saveKey(entry) {
78
+ this.inMemoryKeys.set(entry.id, entry);
79
+ }
80
+ saveKeys(entries) {
81
+ for (const e of entries) {
82
+ this.inMemoryKeys.set(e.id, e);
83
+ }
84
+ }
85
+ deleteKey(id) {
86
+ this.inMemoryKeys.delete(id);
87
+ }
88
+ clear() {
89
+ this.inMemoryKeys.clear();
90
+ }
91
+ };
92
+ var keysStorage = new KeysStorage();
93
+
94
+ // src/ai-keys/keyStore.ts
95
+ var KeyStoreService = class {
96
+ strategy = "lowest-latency";
97
+ roundRobinIndices = /* @__PURE__ */ new Map();
98
+ getKeys(providerId) {
99
+ const all = keysStorage.loadKeys();
100
+ if (!providerId) return all;
101
+ return all.filter((k) => k.providerId === providerId);
102
+ }
103
+ getKey(id) {
104
+ return keysStorage.loadKeys().find((k) => k.id === id);
105
+ }
106
+ getActiveKey(providerId) {
107
+ const valid = this.getKeys(providerId).filter(
108
+ (k) => k.isActive && (k.status === "active" || k.status === "valid")
109
+ );
110
+ if (valid.length === 0) return void 0;
111
+ if (this.strategy === "lowest-latency") {
112
+ return [...valid].sort((a, b) => a.lastLatencyMs - b.lastLatencyMs)[0];
113
+ }
114
+ if (this.strategy === "round-robin") {
115
+ const idx = this.roundRobinIndices.get(providerId) ?? 0;
116
+ const nextIdx = (idx + 1) % valid.length;
117
+ this.roundRobinIndices.set(providerId, nextIdx);
118
+ return valid[idx % valid.length];
119
+ }
120
+ if (this.strategy === "failover-priority") {
121
+ return [...valid].sort((a, b) => (b.weight ?? 1) - (a.weight ?? 1))[0];
122
+ }
123
+ return valid[0];
124
+ }
125
+ setStrategy(strategy) {
126
+ this.strategy = strategy;
127
+ }
128
+ getStrategy() {
129
+ return this.strategy;
130
+ }
131
+ addKey(entry) {
132
+ const full = {
133
+ id: `key_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`,
134
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
135
+ totalCalls: 0,
136
+ successfulCalls: 0,
137
+ failedCalls: 0,
138
+ lastLatencyMs: 0,
139
+ errorCount: 0,
140
+ ...entry
141
+ };
142
+ keysStorage.saveKey(full);
143
+ return full;
144
+ }
145
+ selectNextKey(providerId, _modelId) {
146
+ return this.getActiveKey(providerId);
147
+ }
148
+ updateKeyStatus(id, status) {
149
+ const key = this.getKey(id);
150
+ if (!key) return;
151
+ key.status = status;
152
+ keysStorage.saveKey(key);
153
+ }
154
+ recordUsage(keyId, successOrLatency, latencyOrSuccess, _costUsd = 0, _modelId, error) {
155
+ const key = this.getKey(keyId);
156
+ if (!key) return;
157
+ const success = typeof successOrLatency === "boolean" ? successOrLatency : Boolean(latencyOrSuccess);
158
+ const latencyMs = typeof successOrLatency === "number" ? successOrLatency : Number(latencyOrSuccess);
159
+ key.totalCalls++;
160
+ key.lastLatencyMs = latencyMs;
161
+ key.lastUsedAt = (/* @__PURE__ */ new Date()).toISOString();
162
+ if (success) key.successfulCalls++;
163
+ else {
164
+ key.failedCalls++;
165
+ key.errorCount++;
166
+ key.validationError = error ?? key.validationError;
167
+ }
168
+ keysStorage.saveKey(key);
169
+ }
170
+ exportKeysStructured() {
171
+ return { version: 1, exportedAt: (/* @__PURE__ */ new Date()).toISOString(), keys: this.getKeys() };
172
+ }
173
+ async importKeys(payload) {
174
+ const raw = payload?.keys ?? payload;
175
+ if (!Array.isArray(raw)) throw new Error("Invalid key backup payload.");
176
+ const keys = raw.filter(
177
+ (entry) => Boolean(entry && typeof entry.id === "string" && typeof entry.providerId === "string" && typeof entry.key === "string")
178
+ );
179
+ keysStorage.saveKeys(keys);
180
+ return { importedCount: keys.length, keys };
181
+ }
182
+ async verifyKey(id) {
183
+ const entry = this.getKey(id);
184
+ if (!entry) return false;
185
+ const res = await validateApiKey(entry.providerId, entry.key);
186
+ entry.status = res.status;
187
+ entry.lastLatencyMs = res.latencyMs;
188
+ entry.validationError = res.error;
189
+ keysStorage.saveKey(entry);
190
+ return res.valid;
191
+ }
192
+ };
193
+ var keyStore = new KeyStoreService();
194
+
195
+ export {
196
+ validateApiKey,
197
+ KeysStorage,
198
+ keysStorage,
199
+ KeyStoreService,
200
+ keyStore
201
+ };
@@ -0,0 +1,32 @@
1
+ // src/ai-tools/toolValidationLoop.ts
2
+ function validateToolInput(tool, input) {
3
+ const errors = [];
4
+ const required = tool.inputSchema?.required || [];
5
+ for (const field of required) {
6
+ if (input[field] === void 0 || input[field] === null || input[field] === "") {
7
+ errors.push(`Missing required field '${field}'`);
8
+ }
9
+ }
10
+ const props = tool.inputSchema?.properties || {};
11
+ for (const [key, val] of Object.entries(input)) {
12
+ const expectedProp = props[key];
13
+ if (expectedProp?.type) {
14
+ const actualType = Array.isArray(val) ? "array" : typeof val;
15
+ if (expectedProp.type === "string" && actualType !== "string") {
16
+ errors.push(`Field '${key}' expected string but got ${actualType}`);
17
+ } else if (expectedProp.type === "number" && actualType !== "number") {
18
+ errors.push(`Field '${key}' expected number but got ${actualType}`);
19
+ } else if (expectedProp.type === "boolean" && actualType !== "boolean") {
20
+ errors.push(`Field '${key}' expected boolean but got ${actualType}`);
21
+ }
22
+ }
23
+ }
24
+ return {
25
+ valid: errors.length === 0,
26
+ errors
27
+ };
28
+ }
29
+
30
+ export {
31
+ validateToolInput
32
+ };
@@ -0,0 +1,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