@openliate/liate 0.0.0-stage → 0.1.6
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/LICENSE +21 -0
- package/README.md +127 -2
- package/README.npm.md +128 -0
- package/dist/bin.js +28745 -0
- package/dist/liate.js +39479 -0
- package/dist/runtime/hono.js +28072 -0
- package/dist/runtime/index.js +28072 -0
- package/hub/agents/docgyan-agent/agent.json +27 -0
- package/hub/agents/docgyan-agent/agent.ts +39 -0
- package/hub/agents/gnani-agent/agent.json +17 -0
- package/hub/agents/neon-agent/agent.json +18 -0
- package/hub/agents/prism-agent/agent.json +17 -0
- package/hub/agents/sarvam-agent/agent.json +22 -0
- package/package.json +71 -4
- package/src/cli/index.ts +346 -0
- package/src/cli/resolver.ts +153 -0
- package/src/core/aum.ts +137 -0
- package/src/core/om.ts +159 -0
- package/src/core/spec.ts +112 -0
- package/src/core/uuid.ts +8 -0
- package/src/core/version.ts +2 -0
- package/src/layers/agent/harness.ts +118 -0
- package/src/layers/agent/loop.ts +203 -0
- package/src/layers/agent/skills.ts +302 -0
- package/src/layers/agent/types.ts +66 -0
- package/src/layers/environment/sandbox.ts +107 -0
- package/src/layers/environment/types.ts +23 -0
- package/src/layers/integration/knowledge.ts +187 -0
- package/src/layers/integration/lib.ts +167 -0
- package/src/layers/integration/memory.ts +135 -0
- package/src/layers/integration/repo.ts +273 -0
- package/src/layers/integration/types.ts +60 -0
- package/src/layers/llm/model.ts +928 -0
- package/src/layers/llm/types.ts +64 -0
- package/src/layers/tools/mcp.ts +236 -0
- package/src/layers/tools/registry.ts +307 -0
- package/src/layers/tools/server.ts +69 -0
- package/src/layers/tools/types.ts +38 -0
- package/src/liate.ts +65 -0
- package/src/routes/skills.ts +73 -0
- package/src/runtime/hono.ts +346 -0
- package/src/runtime/wasm.ts +23 -0
- package/src/scaffold/project.ts +188 -0
|
@@ -0,0 +1,928 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Liate — Layer [L]: Sovereign Large Models & AI Gateway
|
|
3
|
+
*
|
|
4
|
+
* Supports all OpenAI-compatible endpoints & Indic Sovereign providers:
|
|
5
|
+
* - Sarvam AI (105B Reasoning, Saaras STT, Bulbul TTS, Prism Decisions)
|
|
6
|
+
* - Neon AI Gateway (Serverless Branch-Scoped Models: OpenAI, Anthropic, Gemini, Llama)
|
|
7
|
+
* - Gnani AI (Prisma v3.0 Indic STT, Timbre v3.0 Indic TTS, Vachana AI)
|
|
8
|
+
* - Generic OpenAI-compatible endpoints (Groq, Cerebras, OpenRouter, Ollama)
|
|
9
|
+
*
|
|
10
|
+
* Zero Hardcoded Models: Any model specified in Layer [L] is passed directly
|
|
11
|
+
* to the target provider. Models are dynamically discovered live from provider URLs.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export * from "@/layers/llm/types";
|
|
15
|
+
import type {
|
|
16
|
+
LlmRole,
|
|
17
|
+
LlmMessage,
|
|
18
|
+
SarvamMessage,
|
|
19
|
+
ToolCall,
|
|
20
|
+
LlmChatOptions,
|
|
21
|
+
SarvamChatOptions,
|
|
22
|
+
LlmChatResponse,
|
|
23
|
+
LlmTtsOptions,
|
|
24
|
+
LlmTtsResponse
|
|
25
|
+
} from "@/layers/llm/types";
|
|
26
|
+
import type { ModelConfig } from "@/core/spec";
|
|
27
|
+
import dns from "node:dns";
|
|
28
|
+
import https from "node:https";
|
|
29
|
+
import http from "node:http";
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
dns.setServers(["8.8.8.8", "1.1.1.1"]);
|
|
33
|
+
} catch {}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Resilient HTTP client for Liate.
|
|
37
|
+
* Falls back to public DNS (8.8.8.8) with TLS SNI when local ISP DNS fails on branch hosts.
|
|
38
|
+
*/
|
|
39
|
+
async function resilientFetch(urlStr: string, options: any = {}): Promise<any> {
|
|
40
|
+
try {
|
|
41
|
+
return await fetch(urlStr, options);
|
|
42
|
+
} catch (err: any) {
|
|
43
|
+
if (err.message?.includes("ENOTFOUND") || err.code === "ENOTFOUND") {
|
|
44
|
+
try {
|
|
45
|
+
const u = new URL(urlStr);
|
|
46
|
+
const ips = await dns.promises.resolve4(u.hostname);
|
|
47
|
+
if (ips && ips.length > 0) {
|
|
48
|
+
return await new Promise((resolve, reject) => {
|
|
49
|
+
const isHttps = u.protocol === "https:";
|
|
50
|
+
const transport = isHttps ? https : http;
|
|
51
|
+
const port = u.port || (isHttps ? 443 : 80);
|
|
52
|
+
|
|
53
|
+
const req = transport.request({
|
|
54
|
+
host: ips[0],
|
|
55
|
+
servername: u.hostname,
|
|
56
|
+
port,
|
|
57
|
+
path: `${u.pathname}${u.search}`,
|
|
58
|
+
method: options.method || "GET",
|
|
59
|
+
headers: {
|
|
60
|
+
...(options.headers || {}),
|
|
61
|
+
Host: u.hostname
|
|
62
|
+
}
|
|
63
|
+
}, (res) => {
|
|
64
|
+
let data = "";
|
|
65
|
+
res.on("data", (chunk) => data += chunk);
|
|
66
|
+
res.on("end", () => {
|
|
67
|
+
resolve({
|
|
68
|
+
ok: res.statusCode ? res.statusCode >= 200 && res.statusCode < 300 : false,
|
|
69
|
+
status: res.statusCode || 200,
|
|
70
|
+
statusText: res.statusMessage || "",
|
|
71
|
+
text: async () => data,
|
|
72
|
+
json: async () => JSON.parse(data)
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
req.on("error", reject);
|
|
78
|
+
if (options.body) {
|
|
79
|
+
req.write(typeof options.body === "string" ? options.body : JSON.stringify(options.body));
|
|
80
|
+
}
|
|
81
|
+
req.end();
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
} catch {}
|
|
85
|
+
}
|
|
86
|
+
throw err;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type SarvamChatResponse = LlmChatResponse;
|
|
91
|
+
|
|
92
|
+
export interface LLMExecutionResult {
|
|
93
|
+
text: string;
|
|
94
|
+
reasoningContent?: string;
|
|
95
|
+
provider: string;
|
|
96
|
+
model: string;
|
|
97
|
+
durationMs: number;
|
|
98
|
+
usage?: {
|
|
99
|
+
promptTokens: number;
|
|
100
|
+
completionTokens: number;
|
|
101
|
+
totalTokens: number;
|
|
102
|
+
tokensSaved?: number;
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface HubLLMDefinition {
|
|
107
|
+
name: string;
|
|
108
|
+
provider: string;
|
|
109
|
+
version: string;
|
|
110
|
+
base_url: string;
|
|
111
|
+
endpoint: string;
|
|
112
|
+
tts_endpoint?: string;
|
|
113
|
+
stt_endpoint?: string;
|
|
114
|
+
models_endpoint?: string;
|
|
115
|
+
systemone_endpoint?: string;
|
|
116
|
+
headers: Record<string, string>;
|
|
117
|
+
features: {
|
|
118
|
+
tool_calling: boolean;
|
|
119
|
+
streaming: boolean;
|
|
120
|
+
reasoning: boolean;
|
|
121
|
+
system_one_decisions?: boolean;
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface DiscoveredModel {
|
|
126
|
+
id: string;
|
|
127
|
+
object?: string;
|
|
128
|
+
created?: number;
|
|
129
|
+
owned_by?: string;
|
|
130
|
+
provider: string;
|
|
131
|
+
description?: string;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export interface NormalizedModel {
|
|
135
|
+
provider: string;
|
|
136
|
+
model: string;
|
|
137
|
+
baseUrl?: string;
|
|
138
|
+
endpoint?: string;
|
|
139
|
+
apiKey?: string;
|
|
140
|
+
temperature?: number;
|
|
141
|
+
maxTokens?: number;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Extracts raw model name from any "provider/..." or bare model string.
|
|
146
|
+
* Zero hardcoded models.
|
|
147
|
+
*/
|
|
148
|
+
export function extractModelName(modelInput: string): string {
|
|
149
|
+
if (!modelInput || typeof modelInput !== "string") {
|
|
150
|
+
throw new Error("Missing required Layer [L] model definition.");
|
|
151
|
+
}
|
|
152
|
+
const trimmed = modelInput.trim();
|
|
153
|
+
if (trimmed.includes("/")) {
|
|
154
|
+
return trimmed.split("/").slice(1).join("/");
|
|
155
|
+
}
|
|
156
|
+
if (trimmed.includes(":")) {
|
|
157
|
+
return trimmed.split(":").slice(1).join(":");
|
|
158
|
+
}
|
|
159
|
+
return trimmed;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Normalizes Layer [L] string or ModelConfig object into provider and model.
|
|
164
|
+
* Zero hardcoded models.
|
|
165
|
+
*/
|
|
166
|
+
export function normalizeModelConfig(input?: string | ModelConfig): NormalizedModel {
|
|
167
|
+
if (!input) {
|
|
168
|
+
throw new Error("Missing required Layer [L] model definition. Specify model string (e.g. 'sarvam/sarvam-105b', 'neon/gpt-5-4-nano', or 'gnani/prisma-v3.0').");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (typeof input === "string") {
|
|
172
|
+
const trimmed = input.trim();
|
|
173
|
+
if (!trimmed) {
|
|
174
|
+
throw new Error("Missing required Layer [L] model definition.");
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (trimmed.includes("/")) {
|
|
178
|
+
const parts = trimmed.split("/");
|
|
179
|
+
return {
|
|
180
|
+
provider: parts[0].toLowerCase(),
|
|
181
|
+
model: parts.slice(1).join("/")
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (trimmed.includes(":")) {
|
|
186
|
+
const parts = trimmed.split(":");
|
|
187
|
+
return {
|
|
188
|
+
provider: parts[0].toLowerCase(),
|
|
189
|
+
model: parts.slice(1).join(":")
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Dynamic heuristic if no prefix given
|
|
194
|
+
const lower = trimmed.toLowerCase();
|
|
195
|
+
let detectedProvider = "sarvam";
|
|
196
|
+
if (lower.startsWith("prisma") || lower.startsWith("timbre") || lower.startsWith("vachana")) {
|
|
197
|
+
detectedProvider = "gnani";
|
|
198
|
+
} else if (lower.startsWith("claude") || lower.startsWith("gpt") || lower.startsWith("gemini") || lower.startsWith("llama") || lower.startsWith("mistral")) {
|
|
199
|
+
detectedProvider = "neon";
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return {
|
|
203
|
+
provider: detectedProvider,
|
|
204
|
+
model: trimmed
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
let providerStr = input.provider;
|
|
209
|
+
let modelStr = input.model;
|
|
210
|
+
|
|
211
|
+
if (!providerStr && modelStr && (modelStr.includes("/") || modelStr.includes(":"))) {
|
|
212
|
+
const sep = modelStr.includes("/") ? "/" : ":";
|
|
213
|
+
const parts = modelStr.split(sep);
|
|
214
|
+
providerStr = parts[0];
|
|
215
|
+
modelStr = parts.slice(1).join(sep);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (!modelStr) {
|
|
219
|
+
throw new Error("Missing required Layer [L] model. Specify a model property (e.g. 'sarvam-105b' or 'gpt-5-4-nano').");
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const lowerModel = modelStr.toLowerCase();
|
|
223
|
+
let defaultProvider = "sarvam";
|
|
224
|
+
if (lowerModel.startsWith("prisma") || lowerModel.startsWith("timbre") || lowerModel.startsWith("vachana")) {
|
|
225
|
+
defaultProvider = "gnani";
|
|
226
|
+
} else if (lowerModel.startsWith("claude") || lowerModel.startsWith("gpt") || lowerModel.startsWith("gemini")) {
|
|
227
|
+
defaultProvider = "neon";
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
provider: (providerStr || defaultProvider).toLowerCase(),
|
|
232
|
+
model: modelStr,
|
|
233
|
+
baseUrl: input.baseUrl,
|
|
234
|
+
endpoint: (input as any).endpoint || input.baseUrl,
|
|
235
|
+
apiKey: input.apiKey,
|
|
236
|
+
temperature: input.temperature,
|
|
237
|
+
maxTokens: input.maxTokens
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Hub Resolver: Gateways for Sarvam AI, Neon AI Gateway, Gnani AI, and Custom Gateways.
|
|
243
|
+
*/
|
|
244
|
+
export class HubResolver {
|
|
245
|
+
public static getBuiltinGateways(): Record<string, HubLLMDefinition> {
|
|
246
|
+
const rawNeon = (typeof process !== "undefined" && process.env?.NEON_AI_GATEWAY_BASE_URL)
|
|
247
|
+
? process.env.NEON_AI_GATEWAY_BASE_URL.trim().replace(/^["']|["']$/g, "").replace(/\/+$/, "")
|
|
248
|
+
: "https://api.ai.neon.tech/v1";
|
|
249
|
+
const neonBaseUrl = rawNeon.endsWith("/v1") ? rawNeon : `${rawNeon}/v1`;
|
|
250
|
+
|
|
251
|
+
const rawGnani = (typeof process !== "undefined" && process.env?.GNANI_BASE_URL)
|
|
252
|
+
? process.env.GNANI_BASE_URL.trim().replace(/^["']|["']$/g, "").replace(/\/+$/, "")
|
|
253
|
+
: "https://api.vachana.ai";
|
|
254
|
+
|
|
255
|
+
return {
|
|
256
|
+
sarvam: {
|
|
257
|
+
name: "sarvam",
|
|
258
|
+
provider: "sarvam",
|
|
259
|
+
version: "v2",
|
|
260
|
+
base_url: "https://api.sarvam.ai/v2",
|
|
261
|
+
endpoint: "https://api.sarvam.ai/v2/chat/completions",
|
|
262
|
+
models_endpoint: "https://api.sarvam.ai/v2/models",
|
|
263
|
+
systemone_endpoint: "https://api.sarvam.ai/v2/systemone",
|
|
264
|
+
headers: {
|
|
265
|
+
"api-subscription-key": "$SARVAM_API_KEY",
|
|
266
|
+
"Authorization": "Bearer $SARVAM_API_KEY",
|
|
267
|
+
"Content-Type": "application/json"
|
|
268
|
+
},
|
|
269
|
+
features: {
|
|
270
|
+
tool_calling: true,
|
|
271
|
+
streaming: true,
|
|
272
|
+
reasoning: true,
|
|
273
|
+
system_one_decisions: true
|
|
274
|
+
}
|
|
275
|
+
},
|
|
276
|
+
neon: {
|
|
277
|
+
name: "neon",
|
|
278
|
+
provider: "neon",
|
|
279
|
+
version: "v1",
|
|
280
|
+
base_url: neonBaseUrl,
|
|
281
|
+
endpoint: `${neonBaseUrl}/chat/completions`,
|
|
282
|
+
models_endpoint: `${neonBaseUrl}/models`,
|
|
283
|
+
headers: {
|
|
284
|
+
"Authorization": "Bearer $NEON_AI_GATEWAY_TOKEN",
|
|
285
|
+
"Content-Type": "application/json"
|
|
286
|
+
},
|
|
287
|
+
features: {
|
|
288
|
+
tool_calling: true,
|
|
289
|
+
streaming: true,
|
|
290
|
+
reasoning: true
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
gnani: {
|
|
294
|
+
name: "gnani",
|
|
295
|
+
provider: "gnani",
|
|
296
|
+
version: "v1",
|
|
297
|
+
base_url: rawGnani,
|
|
298
|
+
endpoint: (typeof process !== "undefined" && process.env?.GNANI_ENDPOINT)
|
|
299
|
+
? process.env.GNANI_ENDPOINT.trim()
|
|
300
|
+
: `${rawGnani}/api/v1/tts/inference`,
|
|
301
|
+
models_endpoint: `${rawGnani}/v1/models`,
|
|
302
|
+
headers: {
|
|
303
|
+
"X-API-Key-ID": "$GNANI_API_KEY",
|
|
304
|
+
"Authorization": "Bearer $GNANI_API_KEY",
|
|
305
|
+
"Content-Type": "application/json"
|
|
306
|
+
},
|
|
307
|
+
features: {
|
|
308
|
+
tool_calling: false,
|
|
309
|
+
streaming: true,
|
|
310
|
+
reasoning: false
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
public static loadLLM(providerName: string): HubLLMDefinition {
|
|
317
|
+
const builtins = this.getBuiltinGateways();
|
|
318
|
+
const key = providerName.toLowerCase();
|
|
319
|
+
if (builtins[key]) {
|
|
320
|
+
return builtins[key];
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Default dynamic OpenAI-compatible gateway
|
|
324
|
+
return {
|
|
325
|
+
name: providerName,
|
|
326
|
+
provider: providerName,
|
|
327
|
+
version: "v1",
|
|
328
|
+
base_url: "https://api.openai.com/v1",
|
|
329
|
+
endpoint: "https://api.openai.com/v1/chat/completions",
|
|
330
|
+
models_endpoint: "https://api.openai.com/v1/models",
|
|
331
|
+
headers: {
|
|
332
|
+
"Authorization": `Bearer $${providerName.toUpperCase()}_API_KEY`,
|
|
333
|
+
"Content-Type": "application/json"
|
|
334
|
+
},
|
|
335
|
+
features: {
|
|
336
|
+
tool_calling: true,
|
|
337
|
+
streaming: true,
|
|
338
|
+
reasoning: true
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Liate LLM Client
|
|
346
|
+
* Native routing for Sarvam AI, Neon AI Gateway, Gnani AI, and OpenAI-compatible endpoints.
|
|
347
|
+
* Zero hardcoded models.
|
|
348
|
+
*/
|
|
349
|
+
export class LiateLlmClient {
|
|
350
|
+
constructor(private apiKey?: string, private options?: { endpoint?: string; baseUrl?: string; provider?: string }) {}
|
|
351
|
+
|
|
352
|
+
public async chat(messages: LlmMessage[], options?: LlmChatOptions): Promise<LlmChatResponse> {
|
|
353
|
+
const normalized = normalizeModelConfig(options?.model || "sarvam/sarvam-105b");
|
|
354
|
+
const provider = (options?.provider || this.options?.provider || normalized.provider).toLowerCase();
|
|
355
|
+
const model = normalized.model;
|
|
356
|
+
|
|
357
|
+
if (!model) {
|
|
358
|
+
throw new Error("Missing required Layer [L] model definition.");
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Resolve Provider API Key
|
|
362
|
+
let key = options?.apiKey || this.apiKey;
|
|
363
|
+
if (!key && typeof process !== "undefined" && process.env) {
|
|
364
|
+
if (provider === "neon") {
|
|
365
|
+
key = process.env.NEON_AI_GATEWAY_TOKEN || process.env.NEON_API_KEY;
|
|
366
|
+
} else if (provider === "gnani") {
|
|
367
|
+
key = process.env.GNANI_API_KEY;
|
|
368
|
+
} else if (provider === "sarvam") {
|
|
369
|
+
key = process.env.SARVAM_API_KEY;
|
|
370
|
+
} else {
|
|
371
|
+
key = process.env[`${provider.toUpperCase()}_API_KEY`] || process.env.OPENAI_API_KEY || process.env.SARVAM_API_KEY;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
if (key) {
|
|
375
|
+
key = key.trim().replace(/^["']|["']$/g, "");
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// Resolve Endpoint URL
|
|
379
|
+
let url = options?.endpoint || this.options?.endpoint;
|
|
380
|
+
if (!url) {
|
|
381
|
+
const baseUrl = options?.baseUrl || this.options?.baseUrl;
|
|
382
|
+
if (baseUrl) {
|
|
383
|
+
const cleanBase = baseUrl.replace(/\/+$/, "");
|
|
384
|
+
url = `${cleanBase}/chat/completions`;
|
|
385
|
+
} else {
|
|
386
|
+
const hub = HubResolver.loadLLM(provider);
|
|
387
|
+
url = hub.endpoint;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// High-speed System-1 Decision routing for Sarvam Prism & TypeSafe Jev
|
|
392
|
+
if (provider === "sarvam" && model.toLowerCase().includes("prism") && (options as any)?.decide) {
|
|
393
|
+
return this.decidePrism(messages, options, key, model);
|
|
394
|
+
}
|
|
395
|
+
if ((provider === "neon" || provider === "typesafe") && (model.toLowerCase().includes("jev") || model.toLowerCase().includes("openjev"))) {
|
|
396
|
+
return this.decideJev(messages, options, key, model);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const payloadMessages = [...messages];
|
|
400
|
+
if (options?.systemPrompt && !payloadMessages.some(m => m.role === "system")) {
|
|
401
|
+
payloadMessages.unshift({
|
|
402
|
+
role: "system",
|
|
403
|
+
content: options.systemPrompt
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const lastUserText = [...payloadMessages].reverse().find((m) => m.role === "user")?.content || "";
|
|
408
|
+
|
|
409
|
+
const payload: Record<string, any> = {
|
|
410
|
+
text: lastUserText,
|
|
411
|
+
messages: payloadMessages.map((m) => {
|
|
412
|
+
const item: Record<string, any> = {
|
|
413
|
+
role: m.role,
|
|
414
|
+
content: m.content
|
|
415
|
+
};
|
|
416
|
+
if (m.name) item.name = m.name;
|
|
417
|
+
if (m.tool_call_id) item.tool_call_id = m.tool_call_id;
|
|
418
|
+
if (m.tool_calls) item.tool_calls = m.tool_calls;
|
|
419
|
+
return item;
|
|
420
|
+
}),
|
|
421
|
+
max_tokens: options?.maxTokens ?? 4096,
|
|
422
|
+
...(options as any),
|
|
423
|
+
model: extractModelName(model)
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
if (options?.temperature !== undefined) {
|
|
427
|
+
payload.temperature = options.temperature;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
if (options?.tools && options.tools.length > 0) {
|
|
431
|
+
payload.tools = options.tools;
|
|
432
|
+
if (options.toolChoice) {
|
|
433
|
+
payload.tool_choice = options.toolChoice;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
if (options?.reasoningEffort) {
|
|
438
|
+
payload.reasoning_effort = options.reasoningEffort;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const headers: Record<string, string> = {
|
|
442
|
+
"Content-Type": "application/json"
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
if (key) {
|
|
446
|
+
if (provider === "neon") {
|
|
447
|
+
headers["Authorization"] = `Bearer ${key}`;
|
|
448
|
+
} else if (provider === "gnani") {
|
|
449
|
+
headers["X-API-Key-ID"] = key;
|
|
450
|
+
headers["Authorization"] = `Bearer ${key}`;
|
|
451
|
+
} else if (provider === "sarvam") {
|
|
452
|
+
headers["Authorization"] = `Bearer ${key}`;
|
|
453
|
+
headers["api-subscription-key"] = key; // Sarvam AI dual-header compatibility
|
|
454
|
+
} else {
|
|
455
|
+
headers["Authorization"] = `Bearer ${key}`;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
if (provider === "sarvam") {
|
|
460
|
+
delete payload.text;
|
|
461
|
+
delete payload.name;
|
|
462
|
+
delete payload.intent;
|
|
463
|
+
delete payload.memory;
|
|
464
|
+
delete payload.maxSteps;
|
|
465
|
+
if (!Array.isArray(payload.tools) || payload.tools.length === 0) {
|
|
466
|
+
delete payload.tools;
|
|
467
|
+
delete payload.tool_choice;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
let response = await resilientFetch(url, {
|
|
472
|
+
method: "POST",
|
|
473
|
+
headers,
|
|
474
|
+
body: JSON.stringify(payload)
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
if (!response.ok) {
|
|
478
|
+
const errText = await response.text().catch(() => "");
|
|
479
|
+
// Dynamic self-healing: if upstream frontier model rejects temperature (e.g. Claude Fable / Opus reasoning models), retry without temperature
|
|
480
|
+
if (errText.includes("temperature parameter") && payload.temperature !== undefined) {
|
|
481
|
+
delete payload.temperature;
|
|
482
|
+
response = await resilientFetch(url, {
|
|
483
|
+
method: "POST",
|
|
484
|
+
headers,
|
|
485
|
+
body: JSON.stringify(payload)
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
if (!response.ok) {
|
|
489
|
+
const finalErr = await response.text().catch(() => errText);
|
|
490
|
+
throw new Error(`LLM Gateway error (${response.status}) [${provider.toUpperCase()}] at ${url}: ${finalErr || errText}`);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
const contentType = response.headers.get("content-type") || "";
|
|
495
|
+
if (contentType.includes("octet-stream") || contentType.includes("audio")) {
|
|
496
|
+
const arrayBuf = await response.arrayBuffer();
|
|
497
|
+
const buffer = Buffer.from(arrayBuf);
|
|
498
|
+
return {
|
|
499
|
+
text: `[Audio Stream Received (${buffer.length} bytes)]`,
|
|
500
|
+
audioBase64: buffer.toString("base64"),
|
|
501
|
+
model
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const data: any = await response.json();
|
|
506
|
+
const choice = data.choices?.[0];
|
|
507
|
+
|
|
508
|
+
// Robust parsing for modern 2026 multi-modal & reasoning content blocks
|
|
509
|
+
let extractedText = "";
|
|
510
|
+
let extractedReasoning: string | undefined = choice?.message?.reasoning_content || choice?.message?.reasoning;
|
|
511
|
+
|
|
512
|
+
const rawContent = choice?.message?.content;
|
|
513
|
+
if (typeof rawContent === "string") {
|
|
514
|
+
extractedText = rawContent;
|
|
515
|
+
} else if (Array.isArray(rawContent)) {
|
|
516
|
+
const textParts: string[] = [];
|
|
517
|
+
const thinkingParts: string[] = [];
|
|
518
|
+
|
|
519
|
+
for (const part of rawContent) {
|
|
520
|
+
if (typeof part === "string") {
|
|
521
|
+
textParts.push(part);
|
|
522
|
+
} else if (part && typeof part === "object") {
|
|
523
|
+
if (part.type === "text" || typeof part.text === "string") {
|
|
524
|
+
textParts.push(part.text || "");
|
|
525
|
+
} else if (part.type === "thinking" || typeof part.thinking === "string") {
|
|
526
|
+
thinkingParts.push(part.thinking || "");
|
|
527
|
+
} else if (part.type === "reasoning" || typeof part.reasoning === "string") {
|
|
528
|
+
thinkingParts.push(part.reasoning || "");
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
extractedText = textParts.join("").trim();
|
|
534
|
+
if (thinkingParts.length > 0 && !extractedReasoning) {
|
|
535
|
+
extractedReasoning = thinkingParts.join("\n\n").trim();
|
|
536
|
+
}
|
|
537
|
+
} else if (rawContent !== null && rawContent !== undefined) {
|
|
538
|
+
extractedText = String(rawContent);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
if (!extractedText && extractedReasoning) {
|
|
542
|
+
extractedText = extractedReasoning;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
return {
|
|
546
|
+
text: extractedText,
|
|
547
|
+
reasoningContent: extractedReasoning,
|
|
548
|
+
toolCalls: choice?.message?.tool_calls?.map((tc: any) => {
|
|
549
|
+
let args = {};
|
|
550
|
+
try {
|
|
551
|
+
args = typeof tc.function?.arguments === "string"
|
|
552
|
+
? JSON.parse(tc.function.arguments)
|
|
553
|
+
: (tc.function?.arguments || tc.arguments || {});
|
|
554
|
+
} catch {
|
|
555
|
+
args = tc.function?.arguments || tc.arguments || {};
|
|
556
|
+
}
|
|
557
|
+
return {
|
|
558
|
+
id: tc.id || `call_${Date.now()}`,
|
|
559
|
+
name: tc.function?.name || tc.name || "",
|
|
560
|
+
arguments: args
|
|
561
|
+
};
|
|
562
|
+
}),
|
|
563
|
+
model: data.model || model,
|
|
564
|
+
usage: data.usage ? {
|
|
565
|
+
promptTokens: data.usage.prompt_tokens || data.usage.promptTokens || 0,
|
|
566
|
+
completionTokens: data.usage.completion_tokens || data.usage.completionTokens || 0,
|
|
567
|
+
totalTokens: data.usage.total_tokens || data.usage.totalTokens || 0
|
|
568
|
+
} : undefined
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
private async decidePrism(messages: LlmMessage[], options: any, key?: string, model?: string): Promise<LlmChatResponse> {
|
|
573
|
+
const hub = HubResolver.loadLLM("sarvam");
|
|
574
|
+
const sysOneUrl = hub.systemone_endpoint || "https://api.sarvam.ai/v2/systemone";
|
|
575
|
+
const lastUserMsg = [...messages].reverse().find((m) => m.role === "user")?.content || "";
|
|
576
|
+
const systemMsg = messages.find((m) => m.role === "system")?.content;
|
|
577
|
+
|
|
578
|
+
const res = await fetch(sysOneUrl, {
|
|
579
|
+
method: "POST",
|
|
580
|
+
headers: {
|
|
581
|
+
"Content-Type": "application/json",
|
|
582
|
+
...(key ? { "api-subscription-key": key, "Authorization": `Bearer ${key}` } : {})
|
|
583
|
+
},
|
|
584
|
+
body: JSON.stringify({
|
|
585
|
+
input: lastUserMsg,
|
|
586
|
+
options: {
|
|
587
|
+
decision: {
|
|
588
|
+
type: "choice",
|
|
589
|
+
instructions: systemMsg || "Evaluate the input state and select the appropriate decision.",
|
|
590
|
+
criteria: {
|
|
591
|
+
allow: "Legitimate, safe, and valid request matching normal authorized behavior",
|
|
592
|
+
challenge_mfa: "Suspicious, ambiguous, or requires secondary verification",
|
|
593
|
+
block: "Malicious, invalid, high-risk, credential attack, or fraudulent attempt"
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
})
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
if (!res.ok) {
|
|
601
|
+
const err = await res.text().catch(() => "");
|
|
602
|
+
throw new Error(`Prism Decision error (${res.status}): ${err}`);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
const data: any = await res.json();
|
|
606
|
+
const ans = data.answers?.decision || Object.values(data.answers || {})[0];
|
|
607
|
+
const selected = (ans as any)?.choice ? (ans as any).choice.toUpperCase() : "EVALUATED";
|
|
608
|
+
|
|
609
|
+
return {
|
|
610
|
+
text: selected,
|
|
611
|
+
model: model || "sarvam-prism",
|
|
612
|
+
usage: data.usage
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
private async decideJev(messages: LlmMessage[], options: any, key?: string, model?: string): Promise<LlmChatResponse> {
|
|
617
|
+
const hub = HubResolver.loadLLM("neon");
|
|
618
|
+
const cleanBase = hub.base_url.replace(/\/v1$/, "");
|
|
619
|
+
const sysOneUrl = `${cleanBase}/typesafe/v1/systemone`;
|
|
620
|
+
const lastUserMsg = [...messages].reverse().find((m) => m.role === "user")?.content || "";
|
|
621
|
+
const systemMsg = messages.find((m) => m.role === "system")?.content;
|
|
622
|
+
|
|
623
|
+
const res = await resilientFetch(sysOneUrl, {
|
|
624
|
+
method: "POST",
|
|
625
|
+
headers: {
|
|
626
|
+
"Content-Type": "application/json",
|
|
627
|
+
...(key ? { "Authorization": `Bearer ${key}` } : {})
|
|
628
|
+
},
|
|
629
|
+
body: JSON.stringify({
|
|
630
|
+
model: model || "typesafeai-jev-1-14-0",
|
|
631
|
+
state: lastUserMsg,
|
|
632
|
+
questions: {
|
|
633
|
+
decision: {
|
|
634
|
+
type: "choice",
|
|
635
|
+
instructions: systemMsg || "Evaluate the user input for security policy, anomaly detection, and authorization.",
|
|
636
|
+
criteria: {
|
|
637
|
+
allow: "Safe, legitimate, and valid request adhering to instructions",
|
|
638
|
+
challenge: "Ambiguous, suspicious, or requires human authorization",
|
|
639
|
+
block: "Malicious, rule bypass attempt, prompt injection, or credential leak attack"
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
})
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
if (!res.ok) {
|
|
647
|
+
const err = await res.text().catch(() => "");
|
|
648
|
+
throw new Error(`Jev System-One Decision error (${res.status}): ${err}`);
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
const data: any = await res.json();
|
|
652
|
+
const ans = data.answers?.decision || data.results?.decision || Object.values(data.answers || data.results || {})[0];
|
|
653
|
+
const selected = (ans as any)?.choice ? String((ans as any).choice).toUpperCase() : (typeof ans === "string" ? ans.toUpperCase() : "EVALUATED");
|
|
654
|
+
|
|
655
|
+
return {
|
|
656
|
+
text: `[JEV SYSTEM-ONE DECISION]: ${selected}\nResult: ${JSON.stringify(ans || data, null, 2)}`,
|
|
657
|
+
model: model || "typesafeai-jev-1-14-0",
|
|
658
|
+
usage: data.usage
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
public async tts(options: LlmTtsOptions): Promise<LlmTtsResponse> {
|
|
663
|
+
const rawModel = options.model || "gnani/timbre-v3.0";
|
|
664
|
+
const normalized = normalizeModelConfig(rawModel);
|
|
665
|
+
const provider = normalized.provider.toLowerCase();
|
|
666
|
+
const model = normalized.model;
|
|
667
|
+
|
|
668
|
+
const hub = HubResolver.loadLLM(provider);
|
|
669
|
+
const ttsUrl = hub.tts_endpoint || hub.endpoint;
|
|
670
|
+
|
|
671
|
+
let key = options.apiKey || this.apiKey;
|
|
672
|
+
if (!key && typeof process !== "undefined" && process.env) {
|
|
673
|
+
if (provider === "gnani") key = process.env.GNANI_API_KEY;
|
|
674
|
+
else if (provider === "sarvam") key = process.env.SARVAM_API_KEY;
|
|
675
|
+
else key = process.env[`${provider.toUpperCase()}_API_KEY`];
|
|
676
|
+
}
|
|
677
|
+
if (!key) {
|
|
678
|
+
throw new Error(`Missing API key for TTS provider '${provider}'.`);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
const headers: Record<string, string> = {
|
|
682
|
+
"Content-Type": "application/json"
|
|
683
|
+
};
|
|
684
|
+
if (provider === "gnani") {
|
|
685
|
+
headers["X-API-Key-ID"] = key.trim();
|
|
686
|
+
} else {
|
|
687
|
+
headers["Authorization"] = `Bearer ${key.trim()}`;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
const payload: Record<string, any> = {
|
|
691
|
+
...options,
|
|
692
|
+
text: options.text,
|
|
693
|
+
model
|
|
694
|
+
};
|
|
695
|
+
|
|
696
|
+
const res = await resilientFetch(ttsUrl, {
|
|
697
|
+
method: "POST",
|
|
698
|
+
headers,
|
|
699
|
+
body: JSON.stringify(payload)
|
|
700
|
+
});
|
|
701
|
+
|
|
702
|
+
if (!res.ok) {
|
|
703
|
+
const err = await res.text().catch(() => "");
|
|
704
|
+
throw new Error(`TTS error (${res.status}) [${provider.toUpperCase()}]: ${err}`);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
const arrayBuffer = await res.arrayBuffer();
|
|
708
|
+
const buffer = Buffer.from(arrayBuffer);
|
|
709
|
+
|
|
710
|
+
return {
|
|
711
|
+
audioBuffer: buffer,
|
|
712
|
+
contentType: res.headers.get("content-type") || "application/octet-stream",
|
|
713
|
+
model,
|
|
714
|
+
voice: options.voice || "default",
|
|
715
|
+
byteLength: buffer.length
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
public async decide(
|
|
720
|
+
state: Record<string, any> | string,
|
|
721
|
+
questions: Record<string, any>,
|
|
722
|
+
options?: { model?: string; apiKey?: string; provider?: string }
|
|
723
|
+
): Promise<any> {
|
|
724
|
+
const rawModel = options?.model || "sarvam-prism";
|
|
725
|
+
const isJev = rawModel.toLowerCase().includes("jev");
|
|
726
|
+
const provider = (options?.provider || (isJev ? "neon" : "sarvam")).toLowerCase();
|
|
727
|
+
|
|
728
|
+
if (provider === "neon" || isJev) {
|
|
729
|
+
const hub = HubResolver.loadLLM("neon");
|
|
730
|
+
const cleanBase = hub.base_url.replace(/\/v1$/, "");
|
|
731
|
+
const sysOneUrl = `${cleanBase}/typesafe/v1/systemone`;
|
|
732
|
+
const key = options?.apiKey || this.apiKey || (typeof process !== "undefined" ? (process.env?.NEON_AI_GATEWAY_TOKEN || process.env?.NEON_API_KEY) : undefined);
|
|
733
|
+
|
|
734
|
+
const res = await resilientFetch(sysOneUrl, {
|
|
735
|
+
method: "POST",
|
|
736
|
+
headers: {
|
|
737
|
+
"Content-Type": "application/json",
|
|
738
|
+
...(key ? { "Authorization": `Bearer ${key}` } : {})
|
|
739
|
+
},
|
|
740
|
+
body: JSON.stringify({
|
|
741
|
+
model: extractModelName(rawModel) || "typesafeai-jev-1-14-0",
|
|
742
|
+
state: typeof state === "string" ? state : JSON.stringify(state),
|
|
743
|
+
questions
|
|
744
|
+
})
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
if (!res.ok) {
|
|
748
|
+
const err = await res.text().catch(() => "");
|
|
749
|
+
throw new Error(`Neon System-One Decision error (${res.status}): ${err}`);
|
|
750
|
+
}
|
|
751
|
+
return await res.json();
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
const key = options?.apiKey || this.apiKey || (typeof process !== "undefined" ? process.env?.SARVAM_API_KEY : undefined);
|
|
755
|
+
if (!key) {
|
|
756
|
+
throw new Error("Missing SARVAM_API_KEY for Sarvam System-One API.");
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
const model = extractModelName(rawModel);
|
|
760
|
+
const payloadState = typeof state === "string" ? { input: state } : state;
|
|
761
|
+
|
|
762
|
+
const res = await resilientFetch("https://api.sarvam.ai/v2/systemone", {
|
|
763
|
+
method: "POST",
|
|
764
|
+
headers: {
|
|
765
|
+
"Content-Type": "application/json",
|
|
766
|
+
"Authorization": `Bearer ${key}`,
|
|
767
|
+
"api-subscription-key": key
|
|
768
|
+
},
|
|
769
|
+
body: JSON.stringify({
|
|
770
|
+
model,
|
|
771
|
+
state: payloadState,
|
|
772
|
+
questions
|
|
773
|
+
})
|
|
774
|
+
});
|
|
775
|
+
|
|
776
|
+
if (!res.ok) {
|
|
777
|
+
const err = await res.text().catch(() => "");
|
|
778
|
+
throw new Error(`Sarvam System-One error (${res.status}): ${err}`);
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
return await res.json();
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
/**
|
|
785
|
+
* Dynamically fetch live models list from provider URL.
|
|
786
|
+
* Zero hardcoded models.
|
|
787
|
+
*/
|
|
788
|
+
public static async fetchModels(provider: string, apiKey?: string, customUrl?: string): Promise<DiscoveredModel[]> {
|
|
789
|
+
const hub = HubResolver.loadLLM(provider);
|
|
790
|
+
const modelsUrl = customUrl || hub.models_endpoint;
|
|
791
|
+
if (!modelsUrl) {
|
|
792
|
+
return [];
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
let key = apiKey;
|
|
796
|
+
if (!key && typeof process !== "undefined" && process.env) {
|
|
797
|
+
if (provider === "neon") key = process.env.NEON_AI_GATEWAY_TOKEN || process.env.NEON_API_KEY;
|
|
798
|
+
else if (provider === "gnani") key = process.env.GNANI_API_KEY;
|
|
799
|
+
else if (provider === "sarvam") key = process.env.SARVAM_API_KEY;
|
|
800
|
+
else key = process.env[`${provider.toUpperCase()}_API_KEY`] || process.env.OPENAI_API_KEY;
|
|
801
|
+
}
|
|
802
|
+
if (key) {
|
|
803
|
+
key = key.trim().replace(/^["']|["']$/g, "");
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
const headers: Record<string, string> = {};
|
|
807
|
+
if (key) {
|
|
808
|
+
if (provider === "neon") {
|
|
809
|
+
headers["Authorization"] = `Bearer ${key}`;
|
|
810
|
+
} else if (provider === "gnani") {
|
|
811
|
+
headers["X-API-Key-ID"] = key;
|
|
812
|
+
headers["Authorization"] = `Bearer ${key}`;
|
|
813
|
+
} else if (provider === "sarvam") {
|
|
814
|
+
headers["Authorization"] = `Bearer ${key}`;
|
|
815
|
+
headers["api-subscription-key"] = key;
|
|
816
|
+
} else {
|
|
817
|
+
headers["Authorization"] = `Bearer ${key}`;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
try {
|
|
822
|
+
const res = await resilientFetch(modelsUrl, {
|
|
823
|
+
headers,
|
|
824
|
+
signal: AbortSignal.timeout(5000)
|
|
825
|
+
});
|
|
826
|
+
if (!res.ok) return [];
|
|
827
|
+
|
|
828
|
+
const data: any = await res.json();
|
|
829
|
+
const rawList = Array.isArray(data) ? data : (data.data || data.models || []);
|
|
830
|
+
|
|
831
|
+
return rawList.map((item: any) => {
|
|
832
|
+
const id = typeof item === "string" ? item : (item.id || item.name || "");
|
|
833
|
+
return {
|
|
834
|
+
id: id.includes("/") ? id : `${provider}/${id}`,
|
|
835
|
+
object: item.object || "model",
|
|
836
|
+
created: item.created || Math.floor(Date.now() / 1000),
|
|
837
|
+
owned_by: item.owned_by || provider,
|
|
838
|
+
provider,
|
|
839
|
+
description: item.description || `${provider.toUpperCase()} Model (${id})`
|
|
840
|
+
};
|
|
841
|
+
}).filter((m: DiscoveredModel) => Boolean(m.id));
|
|
842
|
+
} catch {
|
|
843
|
+
return [];
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
// Liate LLM Client Aliases
|
|
849
|
+
export const LiateLlm = LiateLlmClient;
|
|
850
|
+
export const SarvamClient = LiateLlmClient;
|
|
851
|
+
export const NeonClient = LiateLlmClient;
|
|
852
|
+
export const GnaniClient = LiateLlmClient;
|
|
853
|
+
export const OpenAiClient = LiateLlmClient;
|
|
854
|
+
|
|
855
|
+
/**
|
|
856
|
+
* Layer [L] Router for Liate
|
|
857
|
+
*/
|
|
858
|
+
export class LLMRouter {
|
|
859
|
+
private static defaultClient = new LiateLlmClient();
|
|
860
|
+
|
|
861
|
+
public static async chat(
|
|
862
|
+
messages: LlmMessage[],
|
|
863
|
+
options: LlmChatOptions
|
|
864
|
+
): Promise<LlmChatResponse> {
|
|
865
|
+
const client = options.apiKey ? new LiateLlmClient(options.apiKey) : this.defaultClient;
|
|
866
|
+
return client.chat(messages, options);
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
public static async execute(
|
|
870
|
+
prompt: string,
|
|
871
|
+
model: string,
|
|
872
|
+
systemPrompt?: string,
|
|
873
|
+
apiKey?: string
|
|
874
|
+
): Promise<LLMExecutionResult> {
|
|
875
|
+
const startTime = Date.now();
|
|
876
|
+
const normalized = normalizeModelConfig(model);
|
|
877
|
+
|
|
878
|
+
const chatRes = await this.chat(
|
|
879
|
+
[{ role: "user", content: prompt }],
|
|
880
|
+
{
|
|
881
|
+
model: `${normalized.provider}/${normalized.model}`,
|
|
882
|
+
systemPrompt,
|
|
883
|
+
apiKey
|
|
884
|
+
}
|
|
885
|
+
);
|
|
886
|
+
|
|
887
|
+
return {
|
|
888
|
+
text: chatRes.text,
|
|
889
|
+
reasoningContent: chatRes.reasoningContent,
|
|
890
|
+
provider: normalized.provider,
|
|
891
|
+
model: chatRes.model,
|
|
892
|
+
durationMs: Date.now() - startTime,
|
|
893
|
+
usage: chatRes.usage
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
/**
|
|
898
|
+
* Live Dynamic Model Discovery from Providers.
|
|
899
|
+
* Zero hardcoded models.
|
|
900
|
+
*/
|
|
901
|
+
public static async listModels(provider?: string, apiKey?: string): Promise<DiscoveredModel[]> {
|
|
902
|
+
if (provider) {
|
|
903
|
+
return LiateLlmClient.fetchModels(provider.toLowerCase(), apiKey);
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
// Parallel fetch from all builtin providers
|
|
907
|
+
const providers = ["sarvam", "neon", "gnani"];
|
|
908
|
+
const results = await Promise.allSettled(
|
|
909
|
+
providers.map((p) => LiateLlmClient.fetchModels(p))
|
|
910
|
+
);
|
|
911
|
+
|
|
912
|
+
const combined: DiscoveredModel[] = [];
|
|
913
|
+
const seen = new Set<string>();
|
|
914
|
+
|
|
915
|
+
for (const r of results) {
|
|
916
|
+
if (r.status === "fulfilled") {
|
|
917
|
+
for (const m of r.value) {
|
|
918
|
+
if (!seen.has(m.id)) {
|
|
919
|
+
seen.add(m.id);
|
|
920
|
+
combined.push(m);
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
return combined;
|
|
927
|
+
}
|
|
928
|
+
}
|