@jambonz/llm 0.1.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/dist/index.js ADDED
@@ -0,0 +1,2152 @@
1
+ // src/registry.ts
2
+ var registry = /* @__PURE__ */ new Map();
3
+ function registerAdapter(factory) {
4
+ if (registry.has(factory.vendor)) {
5
+ throw new Error(
6
+ `Adapter already registered for vendor '${factory.vendor}'. Use replaceAdapter() if you intended to override.`
7
+ );
8
+ }
9
+ if (factory.vendor !== factory.manifest.vendor) {
10
+ throw new Error(
11
+ `Adapter factory vendor '${factory.vendor}' does not match manifest.vendor '${factory.manifest.vendor}'.`
12
+ );
13
+ }
14
+ registry.set(factory.vendor, factory);
15
+ }
16
+ function replaceAdapter(factory) {
17
+ registry.set(factory.vendor, factory);
18
+ }
19
+ function unregisterAdapter(vendor) {
20
+ registry.delete(vendor);
21
+ }
22
+ function getAdapterFactory(vendor) {
23
+ const factory = registry.get(vendor);
24
+ if (!factory) {
25
+ const known = Array.from(registry.keys()).sort().join(", ") || "(none)";
26
+ throw new Error(
27
+ `Unknown vendor '${vendor}'. Registered vendors: ${known}`
28
+ );
29
+ }
30
+ return factory;
31
+ }
32
+ function listVendors() {
33
+ return Array.from(registry.keys()).sort();
34
+ }
35
+ function getManifest() {
36
+ const vendors = Array.from(registry.values()).map((f) => f.manifest).sort((a, b) => a.vendor.localeCompare(b.vendor));
37
+ return { vendors };
38
+ }
39
+
40
+ // src/adapters/openai/adapter.ts
41
+ import OpenAI from "openai";
42
+
43
+ // src/validate.ts
44
+ function assertValidRequest(req) {
45
+ if (!req || typeof req !== "object") {
46
+ throw new Error("PromptRequest must be an object");
47
+ }
48
+ if (typeof req.model !== "string" || req.model.length === 0) {
49
+ throw new Error("PromptRequest.model must be a non-empty string");
50
+ }
51
+ if (!Array.isArray(req.messages) || req.messages.length === 0) {
52
+ throw new Error("PromptRequest.messages must be a non-empty array");
53
+ }
54
+ if (req.messages.some((m) => m.role === "system")) {
55
+ throw new Error(
56
+ "PromptRequest.messages must not contain role: 'system'. Pass the system prompt via PromptRequest.system instead."
57
+ );
58
+ }
59
+ }
60
+
61
+ // src/adapters/openai/_streaming.ts
62
+ async function* streamFromOpenAI(client, req, options) {
63
+ assertValidRequest(req);
64
+ if (req.signal?.aborted) {
65
+ yield { type: "end", finishReason: "aborted" };
66
+ return;
67
+ }
68
+ const capabilities = capabilitiesFor(req.model, options.knownModels);
69
+ const useTools = req.tools && req.tools.length > 0 && capabilities.tools;
70
+ const messages = buildWireMessages(req);
71
+ const tools = useTools ? formatTools(req.tools) : void 0;
72
+ const body = {
73
+ model: req.model,
74
+ messages,
75
+ stream: true,
76
+ stream_options: { include_usage: true }
77
+ };
78
+ if (tools) {
79
+ body.tools = tools;
80
+ body.tool_choice = "auto";
81
+ }
82
+ if (req.temperature !== void 0) body.temperature = req.temperature;
83
+ if (req.maxTokens !== void 0) body.max_tokens = req.maxTokens;
84
+ let stream;
85
+ try {
86
+ stream = await client.chat.completions.create(body, { signal: req.signal });
87
+ } catch (err) {
88
+ if (isAbortError(err)) {
89
+ yield { type: "end", finishReason: "aborted" };
90
+ return;
91
+ }
92
+ throw err;
93
+ }
94
+ const pending = {};
95
+ const startedIds = /* @__PURE__ */ new Set();
96
+ const toolCallsEmitted = /* @__PURE__ */ new Set();
97
+ let usage;
98
+ let finalFinishReason = null;
99
+ let finalRawReason;
100
+ try {
101
+ for await (const chunk of stream) {
102
+ if (req.signal?.aborted) {
103
+ yield { type: "end", finishReason: "aborted" };
104
+ return;
105
+ }
106
+ if (chunk.usage) {
107
+ usage = {
108
+ ...chunk.usage.prompt_tokens !== void 0 ? { inputTokens: chunk.usage.prompt_tokens } : {},
109
+ ...chunk.usage.completion_tokens !== void 0 ? { outputTokens: chunk.usage.completion_tokens } : {},
110
+ ...chunk.usage.total_tokens !== void 0 ? { totalTokens: chunk.usage.total_tokens } : {}
111
+ };
112
+ }
113
+ const choice = chunk.choices?.[0];
114
+ if (!choice) continue;
115
+ const delta = choice.delta;
116
+ const finishReason = choice.finish_reason;
117
+ if (delta?.content) {
118
+ yield { type: "token", text: delta.content };
119
+ }
120
+ if (delta?.tool_calls) {
121
+ for (const tc of delta.tool_calls) {
122
+ const idx = tc.index;
123
+ if (!pending[idx]) {
124
+ pending[idx] = { id: "", name: "", arguments: "" };
125
+ }
126
+ if (tc.id) pending[idx].id += tc.id;
127
+ if (tc.function?.name) pending[idx].name += tc.function.name;
128
+ if (tc.function?.arguments) pending[idx].arguments += tc.function.arguments;
129
+ if (pending[idx].id && pending[idx].name && !startedIds.has(pending[idx].id)) {
130
+ startedIds.add(pending[idx].id);
131
+ yield {
132
+ type: "toolCallStart",
133
+ id: pending[idx].id,
134
+ name: pending[idx].name
135
+ };
136
+ }
137
+ }
138
+ }
139
+ if (finishReason === "tool_calls") {
140
+ if (req.signal?.aborted) {
141
+ yield { type: "end", finishReason: "aborted" };
142
+ return;
143
+ }
144
+ for (const tc of Object.values(pending)) {
145
+ if (!tc.id || !tc.name) continue;
146
+ if (toolCallsEmitted.has(tc.id)) continue;
147
+ toolCallsEmitted.add(tc.id);
148
+ let args = {};
149
+ try {
150
+ args = tc.arguments ? JSON.parse(tc.arguments) : {};
151
+ } catch {
152
+ args = {};
153
+ }
154
+ yield {
155
+ type: "toolCall",
156
+ id: tc.id,
157
+ name: tc.name,
158
+ arguments: args
159
+ };
160
+ }
161
+ finalFinishReason = "tool";
162
+ finalRawReason = finishReason;
163
+ } else if (finishReason) {
164
+ finalFinishReason = mapFinishReason(finishReason);
165
+ finalRawReason = finishReason;
166
+ }
167
+ }
168
+ } catch (err) {
169
+ if (isAbortError(err)) {
170
+ yield { type: "end", finishReason: "aborted" };
171
+ return;
172
+ }
173
+ throw err;
174
+ }
175
+ const endEvent = {
176
+ type: "end",
177
+ finishReason: finalFinishReason ?? "stop",
178
+ ...usage ? { usage } : {},
179
+ ...finalRawReason ? { rawReason: finalRawReason } : {}
180
+ };
181
+ yield endEvent;
182
+ }
183
+ function appendOpenAIToolResult(history, toolCallId, result) {
184
+ const content = typeof result === "string" ? result : JSON.stringify(result);
185
+ const wireMessage = {
186
+ role: "tool",
187
+ tool_call_id: toolCallId,
188
+ content
189
+ };
190
+ return [
191
+ ...history,
192
+ {
193
+ role: "tool",
194
+ content,
195
+ vendorRaw: wireMessage
196
+ }
197
+ ];
198
+ }
199
+ async function listOpenAICompatibleModels(client, knownModels) {
200
+ const result = await client.models.list();
201
+ const out = [];
202
+ for (const m of result.data) {
203
+ const known = knownModels.find((k) => k.id === m.id);
204
+ if (known) {
205
+ out.push(known);
206
+ } else {
207
+ out.push({
208
+ id: m.id,
209
+ capabilities: defaultCapabilitiesForUnknown(m.id)
210
+ });
211
+ }
212
+ }
213
+ return out;
214
+ }
215
+ function buildWireMessages(req) {
216
+ const out = [];
217
+ if (req.system) {
218
+ out.push({ role: "system", content: req.system });
219
+ }
220
+ for (const msg of req.messages) {
221
+ if (msg.vendorRaw && typeof msg.vendorRaw === "object") {
222
+ out.push(msg.vendorRaw);
223
+ } else {
224
+ out.push({ role: msg.role, content: msg.content });
225
+ }
226
+ }
227
+ return out;
228
+ }
229
+ function formatTools(tools) {
230
+ return tools.map((t) => ({
231
+ type: "function",
232
+ function: {
233
+ name: t.name,
234
+ description: t.description ?? "",
235
+ parameters: t.parameters ?? { type: "object", properties: {} }
236
+ }
237
+ }));
238
+ }
239
+ function capabilitiesFor(modelId, knownModels) {
240
+ const known = knownModels.find((k) => k.id === modelId);
241
+ if (known) return known.capabilities;
242
+ return defaultCapabilitiesForUnknown(modelId);
243
+ }
244
+ function defaultCapabilitiesForUnknown(modelId) {
245
+ const isReasoning = /^o[13]([-/]|$)/i.test(modelId);
246
+ if (isReasoning) {
247
+ return {
248
+ streaming: false,
249
+ tools: false,
250
+ vision: false,
251
+ systemPrompt: false
252
+ };
253
+ }
254
+ return {
255
+ streaming: true,
256
+ tools: true,
257
+ vision: false,
258
+ systemPrompt: true
259
+ };
260
+ }
261
+ function mapFinishReason(r) {
262
+ switch (r) {
263
+ case "stop":
264
+ return "stop";
265
+ case "tool_calls":
266
+ return "tool";
267
+ case "function_call":
268
+ return "tool";
269
+ case "length":
270
+ return "length";
271
+ case "content_filter":
272
+ return "filtered";
273
+ default:
274
+ return "stop";
275
+ }
276
+ }
277
+ function isAbortError(err) {
278
+ if (!err || typeof err !== "object") return false;
279
+ const e = err;
280
+ return e.name === "APIUserAbortError" || e.name === "AbortError" || e.code === "ABORT_ERR" || typeof e.message === "string" && /aborted/i.test(e.message);
281
+ }
282
+
283
+ // src/adapters/openai/manifest.ts
284
+ var OPENAI_KNOWN_MODELS = [
285
+ {
286
+ id: "gpt-4o",
287
+ displayName: "GPT-4o",
288
+ capabilities: {
289
+ streaming: true,
290
+ tools: true,
291
+ vision: true,
292
+ systemPrompt: true,
293
+ maxContextTokens: 128e3
294
+ }
295
+ },
296
+ {
297
+ id: "gpt-4o-mini",
298
+ displayName: "GPT-4o mini",
299
+ capabilities: {
300
+ streaming: true,
301
+ tools: true,
302
+ vision: true,
303
+ systemPrompt: true,
304
+ maxContextTokens: 128e3
305
+ }
306
+ },
307
+ {
308
+ id: "gpt-4-turbo",
309
+ displayName: "GPT-4 Turbo",
310
+ capabilities: {
311
+ streaming: true,
312
+ tools: true,
313
+ vision: true,
314
+ systemPrompt: true,
315
+ maxContextTokens: 128e3
316
+ }
317
+ },
318
+ {
319
+ id: "gpt-3.5-turbo",
320
+ displayName: "GPT-3.5 Turbo",
321
+ capabilities: {
322
+ streaming: true,
323
+ tools: true,
324
+ vision: false,
325
+ systemPrompt: true,
326
+ maxContextTokens: 16385
327
+ }
328
+ },
329
+ {
330
+ id: "o1-preview",
331
+ displayName: "o1 preview (reasoning)",
332
+ capabilities: {
333
+ streaming: false,
334
+ tools: false,
335
+ vision: false,
336
+ systemPrompt: false,
337
+ maxContextTokens: 128e3
338
+ }
339
+ },
340
+ {
341
+ id: "o1-mini",
342
+ displayName: "o1 mini (reasoning)",
343
+ capabilities: {
344
+ streaming: false,
345
+ tools: false,
346
+ vision: false,
347
+ systemPrompt: false,
348
+ maxContextTokens: 128e3
349
+ }
350
+ }
351
+ ];
352
+ var openAIManifest = {
353
+ vendor: "openai",
354
+ displayName: "OpenAI",
355
+ authKinds: [
356
+ {
357
+ kind: "apiKey",
358
+ displayName: "API Key",
359
+ fields: [
360
+ {
361
+ name: "apiKey",
362
+ label: "API Key",
363
+ type: "password",
364
+ required: true,
365
+ help: "Get an API key from https://platform.openai.com/api-keys"
366
+ },
367
+ {
368
+ name: "baseURL",
369
+ label: "Base URL",
370
+ type: "url",
371
+ required: false,
372
+ default: "https://api.openai.com/v1",
373
+ help: "Override for OpenAI-compatible endpoints (LM Studio, Ollama, vLLM, proxies)."
374
+ }
375
+ ]
376
+ }
377
+ ],
378
+ knownModels: OPENAI_KNOWN_MODELS,
379
+ supportsModelListing: true,
380
+ docsUrl: "https://platform.openai.com/docs/api-reference"
381
+ };
382
+
383
+ // src/adapters/openai/adapter.ts
384
+ var OpenAIAdapter = class {
385
+ vendor = openAIManifest.vendor;
386
+ acceptedAuth = ["apiKey"];
387
+ client;
388
+ init(auth, client) {
389
+ if (!auth.apiKey) {
390
+ throw new Error("OpenAIAdapter: apiKey is required");
391
+ }
392
+ this.client = new OpenAI({
393
+ apiKey: auth.apiKey,
394
+ ...auth.baseURL ? { baseURL: auth.baseURL } : {},
395
+ ...client?.timeout !== void 0 ? { timeout: client.timeout } : {},
396
+ ...client?.maxRetries !== void 0 ? { maxRetries: client.maxRetries } : {}
397
+ });
398
+ }
399
+ stream(req) {
400
+ return streamFromOpenAI(this.ensureClient(), req, {
401
+ knownModels: openAIManifest.knownModels
402
+ });
403
+ }
404
+ appendToolResult(history, toolCallId, result) {
405
+ return appendOpenAIToolResult(history, toolCallId, result);
406
+ }
407
+ async listAvailableModels() {
408
+ return listOpenAICompatibleModels(this.ensureClient(), openAIManifest.knownModels);
409
+ }
410
+ async warmup() {
411
+ const client = this.ensureClient();
412
+ await client.models.list().catch(() => void 0);
413
+ }
414
+ ensureClient() {
415
+ if (!this.client) {
416
+ throw new Error("OpenAIAdapter: init() must be called before stream()/listAvailableModels()");
417
+ }
418
+ return this.client;
419
+ }
420
+ };
421
+
422
+ // src/adapters/openai/factory.ts
423
+ var openAIFactory = {
424
+ vendor: openAIManifest.vendor,
425
+ manifest: openAIManifest,
426
+ create: () => new OpenAIAdapter()
427
+ };
428
+ var deepseekManifest = {
429
+ vendor: "deepseek",
430
+ displayName: "DeepSeek",
431
+ authKinds: [
432
+ {
433
+ kind: "apiKey",
434
+ displayName: "API Key",
435
+ fields: [
436
+ {
437
+ name: "apiKey",
438
+ label: "API Key",
439
+ type: "password",
440
+ required: true,
441
+ help: "Get an API key from https://platform.deepseek.com/api_keys"
442
+ },
443
+ {
444
+ name: "baseURL",
445
+ label: "Base URL",
446
+ type: "url",
447
+ required: false,
448
+ default: "https://api.deepseek.com/v1",
449
+ help: "Override for self-hosted DeepSeek or proxy endpoints."
450
+ }
451
+ ]
452
+ }
453
+ ],
454
+ knownModels: [
455
+ {
456
+ id: "deepseek-chat",
457
+ displayName: "DeepSeek Chat",
458
+ capabilities: {
459
+ streaming: true,
460
+ tools: true,
461
+ vision: false,
462
+ systemPrompt: true,
463
+ maxContextTokens: 64e3
464
+ }
465
+ },
466
+ {
467
+ id: "deepseek-reasoner",
468
+ displayName: "DeepSeek Reasoner",
469
+ capabilities: {
470
+ streaming: true,
471
+ tools: false,
472
+ vision: false,
473
+ systemPrompt: true,
474
+ maxContextTokens: 64e3
475
+ }
476
+ }
477
+ ],
478
+ supportsModelListing: true,
479
+ docsUrl: "https://api-docs.deepseek.com/"
480
+ };
481
+ var DeepSeekAdapter = class extends OpenAIAdapter {
482
+ vendor = deepseekManifest.vendor;
483
+ init(auth, client) {
484
+ super.init(
485
+ {
486
+ ...auth,
487
+ baseURL: auth.baseURL ?? "https://api.deepseek.com/v1"
488
+ },
489
+ client
490
+ );
491
+ }
492
+ };
493
+ var deepseekFactory = {
494
+ vendor: deepseekManifest.vendor,
495
+ manifest: deepseekManifest,
496
+ create: () => new DeepSeekAdapter()
497
+ };
498
+
499
+ // src/adapters/anthropic/adapter.ts
500
+ import Anthropic from "@anthropic-ai/sdk";
501
+
502
+ // src/adapters/anthropic/manifest.ts
503
+ var ANTHROPIC_KNOWN_MODELS = [
504
+ {
505
+ id: "claude-opus-4-7",
506
+ displayName: "Claude Opus 4.7",
507
+ capabilities: {
508
+ streaming: true,
509
+ tools: true,
510
+ vision: true,
511
+ systemPrompt: true,
512
+ maxContextTokens: 2e5
513
+ }
514
+ },
515
+ {
516
+ id: "claude-sonnet-4-6",
517
+ displayName: "Claude Sonnet 4.6",
518
+ capabilities: {
519
+ streaming: true,
520
+ tools: true,
521
+ vision: true,
522
+ systemPrompt: true,
523
+ maxContextTokens: 2e5
524
+ }
525
+ },
526
+ {
527
+ id: "claude-haiku-4-5-20251001",
528
+ displayName: "Claude Haiku 4.5",
529
+ capabilities: {
530
+ streaming: true,
531
+ tools: true,
532
+ vision: true,
533
+ systemPrompt: true,
534
+ maxContextTokens: 2e5
535
+ }
536
+ },
537
+ {
538
+ id: "claude-3-7-sonnet-20250219",
539
+ displayName: "Claude 3.7 Sonnet",
540
+ capabilities: {
541
+ streaming: true,
542
+ tools: true,
543
+ vision: true,
544
+ systemPrompt: true,
545
+ maxContextTokens: 2e5
546
+ }
547
+ },
548
+ {
549
+ id: "claude-3-5-sonnet-20241022",
550
+ displayName: "Claude 3.5 Sonnet",
551
+ capabilities: {
552
+ streaming: true,
553
+ tools: true,
554
+ vision: true,
555
+ systemPrompt: true,
556
+ maxContextTokens: 2e5
557
+ }
558
+ },
559
+ {
560
+ id: "claude-3-5-haiku-20241022",
561
+ displayName: "Claude 3.5 Haiku",
562
+ capabilities: {
563
+ streaming: true,
564
+ tools: true,
565
+ vision: false,
566
+ systemPrompt: true,
567
+ maxContextTokens: 2e5
568
+ }
569
+ }
570
+ ];
571
+ var anthropicManifest = {
572
+ vendor: "anthropic",
573
+ displayName: "Anthropic Claude",
574
+ authKinds: [
575
+ {
576
+ kind: "apiKey",
577
+ displayName: "API Key",
578
+ fields: [
579
+ {
580
+ name: "apiKey",
581
+ label: "API Key",
582
+ type: "password",
583
+ required: true,
584
+ help: "Get an API key from https://console.anthropic.com/settings/keys"
585
+ },
586
+ {
587
+ name: "baseURL",
588
+ label: "Base URL",
589
+ type: "url",
590
+ required: false,
591
+ default: "https://api.anthropic.com",
592
+ help: "Override for proxies or enterprise deployments."
593
+ }
594
+ ]
595
+ }
596
+ ],
597
+ knownModels: ANTHROPIC_KNOWN_MODELS,
598
+ supportsModelListing: true,
599
+ docsUrl: "https://docs.anthropic.com/en/api/overview"
600
+ };
601
+
602
+ // src/adapters/anthropic/adapter.ts
603
+ var AnthropicAdapter = class {
604
+ vendor = anthropicManifest.vendor;
605
+ acceptedAuth = ["apiKey"];
606
+ client;
607
+ init(auth, client) {
608
+ if (!auth.apiKey) {
609
+ throw new Error("AnthropicAdapter: apiKey is required");
610
+ }
611
+ this.client = new Anthropic({
612
+ apiKey: auth.apiKey,
613
+ ...auth.baseURL ? { baseURL: auth.baseURL } : {},
614
+ ...client?.timeout !== void 0 ? { timeout: client.timeout } : {},
615
+ ...client?.maxRetries !== void 0 ? { maxRetries: client.maxRetries } : {}
616
+ });
617
+ }
618
+ async *stream(req) {
619
+ assertValidRequest(req);
620
+ const client = this.ensureClient();
621
+ if (req.signal?.aborted) {
622
+ yield { type: "end", finishReason: "aborted" };
623
+ return;
624
+ }
625
+ const messages = buildWireMessages2(req);
626
+ const tools = req.tools && req.tools.length > 0 ? formatTools2(req.tools) : void 0;
627
+ const body = {
628
+ model: req.model,
629
+ messages,
630
+ max_tokens: req.maxTokens ?? 4096,
631
+ stream: true
632
+ };
633
+ if (req.system) body.system = req.system;
634
+ if (tools) body.tools = tools;
635
+ if (req.temperature !== void 0) body.temperature = req.temperature;
636
+ let stream;
637
+ try {
638
+ stream = await client.messages.create(
639
+ body,
640
+ { signal: req.signal }
641
+ );
642
+ } catch (err) {
643
+ if (isAbortError2(err)) {
644
+ yield { type: "end", finishReason: "aborted" };
645
+ return;
646
+ }
647
+ throw err;
648
+ }
649
+ let currentToolUse = null;
650
+ const completedToolCalls = [];
651
+ let stopReason;
652
+ let inputTokens;
653
+ let outputTokens;
654
+ try {
655
+ for await (const event of stream) {
656
+ if (req.signal?.aborted) {
657
+ yield { type: "end", finishReason: "aborted" };
658
+ return;
659
+ }
660
+ switch (event.type) {
661
+ case "message_start":
662
+ inputTokens = event.message?.usage?.input_tokens;
663
+ break;
664
+ case "content_block_start": {
665
+ const block = event.content_block;
666
+ if (block?.type === "tool_use") {
667
+ currentToolUse = {
668
+ id: block.id ?? "",
669
+ name: block.name ?? "",
670
+ argumentsText: ""
671
+ };
672
+ if (currentToolUse.id && currentToolUse.name) {
673
+ yield {
674
+ type: "toolCallStart",
675
+ id: currentToolUse.id,
676
+ name: currentToolUse.name
677
+ };
678
+ }
679
+ }
680
+ break;
681
+ }
682
+ case "content_block_delta": {
683
+ const delta = event.delta;
684
+ if (delta?.type === "text_delta" && delta.text) {
685
+ yield { type: "token", text: delta.text };
686
+ } else if (delta?.type === "input_json_delta" && currentToolUse && delta.partial_json) {
687
+ currentToolUse.argumentsText += delta.partial_json;
688
+ }
689
+ break;
690
+ }
691
+ case "content_block_stop":
692
+ if (currentToolUse && currentToolUse.id && currentToolUse.name) {
693
+ let parsed = {};
694
+ try {
695
+ parsed = currentToolUse.argumentsText ? JSON.parse(currentToolUse.argumentsText) : {};
696
+ } catch {
697
+ parsed = {};
698
+ }
699
+ completedToolCalls.push({
700
+ id: currentToolUse.id,
701
+ name: currentToolUse.name,
702
+ arguments: parsed
703
+ });
704
+ }
705
+ currentToolUse = null;
706
+ break;
707
+ case "message_delta":
708
+ if (event.delta?.stop_reason) {
709
+ stopReason = event.delta.stop_reason;
710
+ }
711
+ if (event.usage?.output_tokens !== void 0) {
712
+ outputTokens = event.usage.output_tokens;
713
+ }
714
+ break;
715
+ case "message_stop":
716
+ break;
717
+ default:
718
+ break;
719
+ }
720
+ }
721
+ } catch (err) {
722
+ if (isAbortError2(err)) {
723
+ yield { type: "end", finishReason: "aborted" };
724
+ return;
725
+ }
726
+ throw err;
727
+ }
728
+ if (req.signal?.aborted) {
729
+ yield { type: "end", finishReason: "aborted" };
730
+ return;
731
+ }
732
+ for (const tc of completedToolCalls) {
733
+ yield {
734
+ type: "toolCall",
735
+ id: tc.id,
736
+ name: tc.name,
737
+ arguments: tc.arguments
738
+ };
739
+ }
740
+ const finishReason = mapStopReason(stopReason, completedToolCalls.length > 0);
741
+ const usage = inputTokens !== void 0 || outputTokens !== void 0 ? {
742
+ ...inputTokens !== void 0 ? { inputTokens } : {},
743
+ ...outputTokens !== void 0 ? { outputTokens } : {},
744
+ ...inputTokens !== void 0 && outputTokens !== void 0 ? { totalTokens: inputTokens + outputTokens } : {}
745
+ } : void 0;
746
+ const endEvent = {
747
+ type: "end",
748
+ finishReason,
749
+ ...usage ? { usage } : {},
750
+ ...stopReason ? { rawReason: stopReason } : {}
751
+ };
752
+ yield endEvent;
753
+ }
754
+ appendToolResult(history, toolCallId, result) {
755
+ const content = typeof result === "string" ? result : JSON.stringify(result);
756
+ const wireMessage = {
757
+ role: "user",
758
+ content: [
759
+ {
760
+ type: "tool_result",
761
+ tool_use_id: toolCallId,
762
+ content
763
+ }
764
+ ]
765
+ };
766
+ return [
767
+ ...history,
768
+ {
769
+ role: "tool",
770
+ content,
771
+ vendorRaw: wireMessage
772
+ }
773
+ ];
774
+ }
775
+ async listAvailableModels() {
776
+ const client = this.ensureClient();
777
+ const result = await client.models.list();
778
+ const models = [];
779
+ for (const m of result.data) {
780
+ const known = anthropicManifest.knownModels.find((k) => k.id === m.id);
781
+ if (known) {
782
+ models.push(known);
783
+ } else {
784
+ models.push({
785
+ id: m.id,
786
+ displayName: m.display_name ?? void 0,
787
+ capabilities: defaultClaudeCapabilities()
788
+ });
789
+ }
790
+ }
791
+ return models;
792
+ }
793
+ async warmup() {
794
+ const client = this.ensureClient();
795
+ await client.models.list().catch(() => void 0);
796
+ }
797
+ ensureClient() {
798
+ if (!this.client) {
799
+ throw new Error("AnthropicAdapter: init() must be called before stream()/listAvailableModels()");
800
+ }
801
+ return this.client;
802
+ }
803
+ };
804
+ function buildWireMessages2(req) {
805
+ const out = [];
806
+ for (const msg of req.messages) {
807
+ if (msg.vendorRaw && typeof msg.vendorRaw === "object") {
808
+ out.push(msg.vendorRaw);
809
+ } else {
810
+ out.push({ role: msg.role, content: msg.content });
811
+ }
812
+ }
813
+ return out;
814
+ }
815
+ function formatTools2(tools) {
816
+ return tools.map((t) => ({
817
+ name: t.name,
818
+ description: t.description ?? "",
819
+ input_schema: t.parameters ?? { type: "object", properties: {} }
820
+ }));
821
+ }
822
+ function mapStopReason(r, hadToolCalls) {
823
+ if (hadToolCalls) return "tool";
824
+ switch (r) {
825
+ case "end_turn":
826
+ case "stop_sequence":
827
+ return "stop";
828
+ case "tool_use":
829
+ return "tool";
830
+ case "max_tokens":
831
+ return "length";
832
+ default:
833
+ return "stop";
834
+ }
835
+ }
836
+ function defaultClaudeCapabilities() {
837
+ return {
838
+ streaming: true,
839
+ tools: true,
840
+ vision: true,
841
+ systemPrompt: true
842
+ };
843
+ }
844
+ function isAbortError2(err) {
845
+ if (!err || typeof err !== "object") return false;
846
+ const e = err;
847
+ return e.name === "APIUserAbortError" || e.name === "AbortError" || e.code === "ABORT_ERR" || typeof e.message === "string" && /aborted/i.test(e.message);
848
+ }
849
+
850
+ // src/adapters/anthropic/factory.ts
851
+ var anthropicFactory = {
852
+ vendor: anthropicManifest.vendor,
853
+ manifest: anthropicManifest,
854
+ create: () => new AnthropicAdapter()
855
+ };
856
+
857
+ // src/adapters/google/adapter.ts
858
+ import { GoogleGenAI } from "@google/genai";
859
+
860
+ // src/adapters/google/_streaming.ts
861
+ async function* streamFromGemini(client, req) {
862
+ assertValidRequest(req);
863
+ if (req.signal?.aborted) {
864
+ yield { type: "end", finishReason: "aborted" };
865
+ return;
866
+ }
867
+ const contents = buildWireContents(req);
868
+ const tools = req.tools && req.tools.length > 0 ? formatTools3(req.tools) : void 0;
869
+ const config = {};
870
+ if (req.system) {
871
+ config.systemInstruction = { parts: [{ text: req.system }] };
872
+ }
873
+ if (tools) config.tools = tools;
874
+ if (req.temperature !== void 0) config.temperature = req.temperature;
875
+ if (req.maxTokens !== void 0) config.maxOutputTokens = req.maxTokens;
876
+ if (req.signal) config.abortSignal = req.signal;
877
+ let stream;
878
+ try {
879
+ stream = await client.models.generateContentStream({
880
+ model: req.model,
881
+ contents,
882
+ config
883
+ });
884
+ } catch (err) {
885
+ if (isAbortError3(err)) {
886
+ yield { type: "end", finishReason: "aborted" };
887
+ return;
888
+ }
889
+ throw err;
890
+ }
891
+ const toolCallCounter = /* @__PURE__ */ new Map();
892
+ const completedToolCalls = [];
893
+ let stopReason;
894
+ let inputTokens;
895
+ let outputTokens;
896
+ let totalTokens;
897
+ try {
898
+ for await (const chunk of stream) {
899
+ if (req.signal?.aborted) {
900
+ yield { type: "end", finishReason: "aborted" };
901
+ return;
902
+ }
903
+ const usage2 = chunk.usageMetadata;
904
+ if (usage2) {
905
+ if (usage2.promptTokenCount !== void 0) inputTokens = usage2.promptTokenCount;
906
+ if (usage2.candidatesTokenCount !== void 0) outputTokens = usage2.candidatesTokenCount;
907
+ if (usage2.totalTokenCount !== void 0) totalTokens = usage2.totalTokenCount;
908
+ }
909
+ const candidate = chunk.candidates?.[0];
910
+ if (!candidate?.content?.parts) {
911
+ if (candidate?.finishReason) stopReason = candidate.finishReason;
912
+ continue;
913
+ }
914
+ for (const part of candidate.content.parts) {
915
+ if (typeof part.text === "string" && part.text.length > 0) {
916
+ yield { type: "token", text: part.text };
917
+ }
918
+ if (part.functionCall) {
919
+ const name = part.functionCall.name ?? "";
920
+ if (!name) continue;
921
+ const count = toolCallCounter.get(name) ?? 0;
922
+ toolCallCounter.set(name, count + 1);
923
+ const id = count === 0 ? name : `${name}#${count}`;
924
+ yield { type: "toolCallStart", id, name };
925
+ completedToolCalls.push({
926
+ id,
927
+ name,
928
+ arguments: part.functionCall.args ?? {}
929
+ });
930
+ }
931
+ }
932
+ if (candidate.finishReason) {
933
+ stopReason = candidate.finishReason;
934
+ }
935
+ }
936
+ } catch (err) {
937
+ if (isAbortError3(err)) {
938
+ yield { type: "end", finishReason: "aborted" };
939
+ return;
940
+ }
941
+ throw err;
942
+ }
943
+ if (req.signal?.aborted) {
944
+ yield { type: "end", finishReason: "aborted" };
945
+ return;
946
+ }
947
+ for (const tc of completedToolCalls) {
948
+ yield {
949
+ type: "toolCall",
950
+ id: tc.id,
951
+ name: tc.name,
952
+ arguments: tc.arguments
953
+ };
954
+ }
955
+ const finishReason = mapFinishReason2(stopReason, completedToolCalls.length > 0);
956
+ const usage = inputTokens !== void 0 || outputTokens !== void 0 || totalTokens !== void 0 ? {
957
+ ...inputTokens !== void 0 ? { inputTokens } : {},
958
+ ...outputTokens !== void 0 ? { outputTokens } : {},
959
+ ...totalTokens !== void 0 ? { totalTokens } : {}
960
+ } : void 0;
961
+ const endEvent = {
962
+ type: "end",
963
+ finishReason,
964
+ ...usage ? { usage } : {},
965
+ ...stopReason ? { rawReason: stopReason } : {}
966
+ };
967
+ yield endEvent;
968
+ }
969
+ function appendGeminiToolResult(history, toolCallId, result) {
970
+ const name = parseToolName(toolCallId);
971
+ const content = typeof result === "string" ? result : JSON.stringify(result);
972
+ const wireMessage = {
973
+ role: "user",
974
+ parts: [
975
+ {
976
+ functionResponse: {
977
+ name,
978
+ response: { result: content }
979
+ }
980
+ }
981
+ ]
982
+ };
983
+ return [
984
+ ...history,
985
+ {
986
+ role: "tool",
987
+ content,
988
+ vendorRaw: wireMessage
989
+ }
990
+ ];
991
+ }
992
+ async function listGeminiModels(client, knownModels) {
993
+ const out = [];
994
+ const pager = await client.models.list();
995
+ for await (const m of pager) {
996
+ const id = stripModelPrefix(m.name ?? "");
997
+ if (!id) continue;
998
+ const known = knownModels.find((k) => k.id === id);
999
+ if (known) {
1000
+ out.push(known);
1001
+ } else {
1002
+ out.push({
1003
+ id,
1004
+ displayName: m.displayName ?? void 0,
1005
+ capabilities: defaultGeminiCapabilities()
1006
+ });
1007
+ }
1008
+ }
1009
+ return out;
1010
+ }
1011
+ function buildWireContents(req) {
1012
+ const out = [];
1013
+ for (const msg of req.messages) {
1014
+ if (msg.vendorRaw && typeof msg.vendorRaw === "object") {
1015
+ out.push(msg.vendorRaw);
1016
+ continue;
1017
+ }
1018
+ const text = typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content);
1019
+ out.push({
1020
+ role: toGeminiRole(msg.role),
1021
+ parts: [{ text }]
1022
+ });
1023
+ }
1024
+ return out;
1025
+ }
1026
+ function toGeminiRole(role) {
1027
+ return role === "assistant" || role === "model" ? "model" : "user";
1028
+ }
1029
+ function formatTools3(tools) {
1030
+ return [
1031
+ {
1032
+ functionDeclarations: tools.map((t) => ({
1033
+ name: t.name,
1034
+ description: t.description ?? "",
1035
+ parameters: t.parameters ?? { type: "object", properties: {} }
1036
+ }))
1037
+ }
1038
+ ];
1039
+ }
1040
+ function mapFinishReason2(r, hadToolCalls) {
1041
+ if (hadToolCalls && (r === "STOP" || r === "TOOL_CALLS" || !r)) return "tool";
1042
+ switch (r) {
1043
+ case "STOP":
1044
+ return "stop";
1045
+ case "MAX_TOKENS":
1046
+ return "length";
1047
+ case "SAFETY":
1048
+ case "RECITATION":
1049
+ return "filtered";
1050
+ case "TOOL_CALLS":
1051
+ return "tool";
1052
+ default:
1053
+ return "stop";
1054
+ }
1055
+ }
1056
+ function parseToolName(id) {
1057
+ const hashIdx = id.lastIndexOf("#");
1058
+ if (hashIdx === -1) return id;
1059
+ const suffix = id.slice(hashIdx + 1);
1060
+ if (/^\d+$/.test(suffix)) return id.slice(0, hashIdx);
1061
+ return id;
1062
+ }
1063
+ function stripModelPrefix(name) {
1064
+ return name.startsWith("models/") ? name.slice("models/".length) : name;
1065
+ }
1066
+ function defaultGeminiCapabilities() {
1067
+ return {
1068
+ streaming: true,
1069
+ tools: true,
1070
+ vision: true,
1071
+ systemPrompt: true
1072
+ };
1073
+ }
1074
+ function isAbortError3(err) {
1075
+ if (!err || typeof err !== "object") return false;
1076
+ const e = err;
1077
+ return e.name === "APIUserAbortError" || e.name === "AbortError" || e.code === "ABORT_ERR" || typeof e.message === "string" && /aborted/i.test(e.message);
1078
+ }
1079
+
1080
+ // src/adapters/google/manifest.ts
1081
+ var GOOGLE_KNOWN_MODELS = [
1082
+ {
1083
+ id: "gemini-2.5-pro",
1084
+ displayName: "Gemini 2.5 Pro",
1085
+ capabilities: {
1086
+ streaming: true,
1087
+ tools: true,
1088
+ vision: true,
1089
+ systemPrompt: true,
1090
+ maxContextTokens: 1e6
1091
+ }
1092
+ },
1093
+ {
1094
+ id: "gemini-2.5-flash",
1095
+ displayName: "Gemini 2.5 Flash",
1096
+ capabilities: {
1097
+ streaming: true,
1098
+ tools: true,
1099
+ vision: true,
1100
+ systemPrompt: true,
1101
+ maxContextTokens: 1e6
1102
+ }
1103
+ },
1104
+ {
1105
+ id: "gemini-2.0-flash",
1106
+ displayName: "Gemini 2.0 Flash",
1107
+ capabilities: {
1108
+ streaming: true,
1109
+ tools: true,
1110
+ vision: true,
1111
+ systemPrompt: true,
1112
+ maxContextTokens: 1e6
1113
+ }
1114
+ },
1115
+ {
1116
+ id: "gemini-2.0-flash-lite",
1117
+ displayName: "Gemini 2.0 Flash Lite",
1118
+ capabilities: {
1119
+ streaming: true,
1120
+ tools: true,
1121
+ vision: false,
1122
+ systemPrompt: true,
1123
+ maxContextTokens: 1e6
1124
+ }
1125
+ },
1126
+ {
1127
+ id: "gemini-1.5-pro",
1128
+ displayName: "Gemini 1.5 Pro",
1129
+ capabilities: {
1130
+ streaming: true,
1131
+ tools: true,
1132
+ vision: true,
1133
+ systemPrompt: true,
1134
+ maxContextTokens: 2e6
1135
+ }
1136
+ },
1137
+ {
1138
+ id: "gemini-1.5-flash",
1139
+ displayName: "Gemini 1.5 Flash",
1140
+ capabilities: {
1141
+ streaming: true,
1142
+ tools: true,
1143
+ vision: true,
1144
+ systemPrompt: true,
1145
+ maxContextTokens: 1e6
1146
+ }
1147
+ }
1148
+ ];
1149
+ var googleManifest = {
1150
+ vendor: "google",
1151
+ displayName: "Google Gemini",
1152
+ authKinds: [
1153
+ {
1154
+ kind: "googleApiKey",
1155
+ displayName: "API Key",
1156
+ fields: [
1157
+ {
1158
+ name: "apiKey",
1159
+ label: "API Key",
1160
+ type: "password",
1161
+ required: true,
1162
+ help: "Get an API key from https://aistudio.google.com/apikey"
1163
+ }
1164
+ ]
1165
+ }
1166
+ ],
1167
+ knownModels: GOOGLE_KNOWN_MODELS,
1168
+ supportsModelListing: true,
1169
+ docsUrl: "https://ai.google.dev/gemini-api/docs"
1170
+ };
1171
+
1172
+ // src/adapters/google/adapter.ts
1173
+ var GoogleAdapter = class {
1174
+ vendor = googleManifest.vendor;
1175
+ acceptedAuth = ["googleApiKey"];
1176
+ client;
1177
+ init(auth, _client) {
1178
+ if (auth.kind !== "googleApiKey") {
1179
+ throw new Error(
1180
+ `GoogleAdapter: unsupported auth kind '${auth.kind}'`
1181
+ );
1182
+ }
1183
+ if (!auth.apiKey) {
1184
+ throw new Error("GoogleAdapter: apiKey is required");
1185
+ }
1186
+ this.client = new GoogleGenAI({ apiKey: auth.apiKey });
1187
+ }
1188
+ stream(req) {
1189
+ return streamFromGemini(this.ensureClient(), req);
1190
+ }
1191
+ appendToolResult(history, toolCallId, result) {
1192
+ return appendGeminiToolResult(history, toolCallId, result);
1193
+ }
1194
+ async listAvailableModels() {
1195
+ return listGeminiModels(this.ensureClient(), googleManifest.knownModels);
1196
+ }
1197
+ async warmup() {
1198
+ }
1199
+ ensureClient() {
1200
+ if (!this.client) {
1201
+ throw new Error("GoogleAdapter: init() must be called before stream()/listAvailableModels()");
1202
+ }
1203
+ return this.client;
1204
+ }
1205
+ };
1206
+
1207
+ // src/adapters/google/factory.ts
1208
+ var googleFactory = {
1209
+ vendor: googleManifest.vendor,
1210
+ manifest: googleManifest,
1211
+ create: () => new GoogleAdapter()
1212
+ };
1213
+
1214
+ // src/adapters/vertex-gemini/adapter.ts
1215
+ import { GoogleGenAI as GoogleGenAI2 } from "@google/genai";
1216
+
1217
+ // src/adapters/vertex-gemini/manifest.ts
1218
+ var VERTEX_GEMINI_KNOWN_MODELS = [
1219
+ {
1220
+ id: "gemini-2.5-pro",
1221
+ displayName: "Gemini 2.5 Pro",
1222
+ capabilities: {
1223
+ streaming: true,
1224
+ tools: true,
1225
+ vision: true,
1226
+ systemPrompt: true,
1227
+ maxContextTokens: 1e6
1228
+ }
1229
+ },
1230
+ {
1231
+ id: "gemini-2.5-flash",
1232
+ displayName: "Gemini 2.5 Flash",
1233
+ capabilities: {
1234
+ streaming: true,
1235
+ tools: true,
1236
+ vision: true,
1237
+ systemPrompt: true,
1238
+ maxContextTokens: 1e6
1239
+ }
1240
+ },
1241
+ {
1242
+ id: "gemini-2.0-flash",
1243
+ displayName: "Gemini 2.0 Flash",
1244
+ capabilities: {
1245
+ streaming: true,
1246
+ tools: true,
1247
+ vision: true,
1248
+ systemPrompt: true,
1249
+ maxContextTokens: 1e6
1250
+ }
1251
+ },
1252
+ {
1253
+ id: "gemini-1.5-pro",
1254
+ displayName: "Gemini 1.5 Pro",
1255
+ capabilities: {
1256
+ streaming: true,
1257
+ tools: true,
1258
+ vision: true,
1259
+ systemPrompt: true,
1260
+ maxContextTokens: 2e6
1261
+ }
1262
+ },
1263
+ {
1264
+ id: "gemini-1.5-flash",
1265
+ displayName: "Gemini 1.5 Flash",
1266
+ capabilities: {
1267
+ streaming: true,
1268
+ tools: true,
1269
+ vision: true,
1270
+ systemPrompt: true,
1271
+ maxContextTokens: 1e6
1272
+ }
1273
+ }
1274
+ ];
1275
+ var vertexGeminiManifest = {
1276
+ vendor: "vertex-gemini",
1277
+ displayName: "Vertex AI \u2014 Gemini",
1278
+ authKinds: [
1279
+ {
1280
+ kind: "vertexServiceAccount",
1281
+ displayName: "Service Account JSON",
1282
+ fields: [
1283
+ {
1284
+ name: "credentials",
1285
+ label: "Service Account Key (JSON)",
1286
+ type: "json-file",
1287
+ required: true,
1288
+ help: "Upload the service account key JSON from Google Cloud Console."
1289
+ },
1290
+ {
1291
+ name: "projectId",
1292
+ label: "Project ID",
1293
+ type: "text",
1294
+ required: true,
1295
+ help: "GCP project that hosts the Vertex AI deployment."
1296
+ },
1297
+ {
1298
+ name: "location",
1299
+ label: "Region",
1300
+ type: "select",
1301
+ required: true,
1302
+ default: "us-central1",
1303
+ options: [
1304
+ { value: "us-central1", label: "us-central1" },
1305
+ { value: "us-east1", label: "us-east1" },
1306
+ { value: "us-east4", label: "us-east4" },
1307
+ { value: "us-west1", label: "us-west1" },
1308
+ { value: "us-west4", label: "us-west4" },
1309
+ { value: "europe-west1", label: "europe-west1" },
1310
+ { value: "europe-west2", label: "europe-west2" },
1311
+ { value: "europe-west3", label: "europe-west3" },
1312
+ { value: "europe-west4", label: "europe-west4" },
1313
+ { value: "asia-east1", label: "asia-east1" },
1314
+ { value: "asia-northeast1", label: "asia-northeast1" },
1315
+ { value: "asia-southeast1", label: "asia-southeast1" }
1316
+ ]
1317
+ }
1318
+ ]
1319
+ }
1320
+ ],
1321
+ knownModels: VERTEX_GEMINI_KNOWN_MODELS,
1322
+ supportsModelListing: true,
1323
+ docsUrl: "https://cloud.google.com/vertex-ai/generative-ai/docs"
1324
+ };
1325
+
1326
+ // src/adapters/vertex-gemini/adapter.ts
1327
+ var VertexGeminiAdapter = class {
1328
+ vendor = vertexGeminiManifest.vendor;
1329
+ acceptedAuth = ["vertexServiceAccount"];
1330
+ client;
1331
+ init(auth, _client) {
1332
+ if (auth.kind !== "vertexServiceAccount") {
1333
+ throw new Error(
1334
+ `VertexGeminiAdapter: unsupported auth kind '${auth.kind}'`
1335
+ );
1336
+ }
1337
+ if (!auth.credentials) {
1338
+ throw new Error("VertexGeminiAdapter: service-account credentials are required");
1339
+ }
1340
+ if (!auth.projectId) {
1341
+ throw new Error("VertexGeminiAdapter: projectId is required");
1342
+ }
1343
+ if (!auth.location) {
1344
+ throw new Error("VertexGeminiAdapter: location is required");
1345
+ }
1346
+ this.client = new GoogleGenAI2({
1347
+ vertexai: true,
1348
+ project: auth.projectId,
1349
+ location: auth.location,
1350
+ googleAuthOptions: {
1351
+ credentials: auth.credentials
1352
+ }
1353
+ });
1354
+ }
1355
+ stream(req) {
1356
+ return streamFromGemini(this.ensureClient(), req);
1357
+ }
1358
+ appendToolResult(history, toolCallId, result) {
1359
+ return appendGeminiToolResult(history, toolCallId, result);
1360
+ }
1361
+ async listAvailableModels() {
1362
+ return listGeminiModels(this.ensureClient(), vertexGeminiManifest.knownModels);
1363
+ }
1364
+ async warmup() {
1365
+ }
1366
+ ensureClient() {
1367
+ if (!this.client) {
1368
+ throw new Error(
1369
+ "VertexGeminiAdapter: init() must be called before stream()/listAvailableModels()"
1370
+ );
1371
+ }
1372
+ return this.client;
1373
+ }
1374
+ };
1375
+
1376
+ // src/adapters/vertex-gemini/factory.ts
1377
+ var vertexGeminiFactory = {
1378
+ vendor: vertexGeminiManifest.vendor,
1379
+ manifest: vertexGeminiManifest,
1380
+ create: () => new VertexGeminiAdapter()
1381
+ };
1382
+
1383
+ // src/adapters/vertex-openai/adapter.ts
1384
+ import OpenAI2 from "openai";
1385
+ import { GoogleAuth } from "google-auth-library";
1386
+
1387
+ // src/adapters/vertex-openai/manifest.ts
1388
+ var VERTEX_OPENAI_KNOWN_MODELS = [
1389
+ {
1390
+ id: "mistral-large",
1391
+ displayName: "Mistral Large (on Vertex)",
1392
+ capabilities: {
1393
+ streaming: true,
1394
+ tools: true,
1395
+ vision: false,
1396
+ systemPrompt: true,
1397
+ maxContextTokens: 128e3
1398
+ }
1399
+ },
1400
+ {
1401
+ id: "mistral-small",
1402
+ displayName: "Mistral Small (on Vertex)",
1403
+ capabilities: {
1404
+ streaming: true,
1405
+ tools: true,
1406
+ vision: false,
1407
+ systemPrompt: true,
1408
+ maxContextTokens: 32e3
1409
+ }
1410
+ },
1411
+ {
1412
+ id: "meta/llama-3.3-70b-instruct-maas",
1413
+ displayName: "Llama 3.3 70B Instruct (on Vertex)",
1414
+ capabilities: {
1415
+ streaming: true,
1416
+ tools: true,
1417
+ vision: false,
1418
+ systemPrompt: true,
1419
+ maxContextTokens: 128e3
1420
+ }
1421
+ },
1422
+ {
1423
+ id: "meta/llama-3.1-405b-instruct-maas",
1424
+ displayName: "Llama 3.1 405B Instruct (on Vertex)",
1425
+ capabilities: {
1426
+ streaming: true,
1427
+ tools: true,
1428
+ vision: false,
1429
+ systemPrompt: true,
1430
+ maxContextTokens: 128e3
1431
+ }
1432
+ }
1433
+ ];
1434
+ var vertexOpenAIManifest = {
1435
+ vendor: "vertex-openai",
1436
+ displayName: "Vertex AI \u2014 OpenAI-compatible",
1437
+ authKinds: [
1438
+ {
1439
+ kind: "vertexServiceAccount",
1440
+ displayName: "Service Account JSON",
1441
+ fields: [
1442
+ {
1443
+ name: "credentials",
1444
+ label: "Service Account Key (JSON)",
1445
+ type: "json-file",
1446
+ required: true,
1447
+ help: "Upload the service account key JSON from Google Cloud Console."
1448
+ },
1449
+ {
1450
+ name: "projectId",
1451
+ label: "Project ID",
1452
+ type: "text",
1453
+ required: true,
1454
+ help: "GCP project that hosts the Vertex AI deployment."
1455
+ },
1456
+ {
1457
+ name: "location",
1458
+ label: "Region",
1459
+ type: "select",
1460
+ required: true,
1461
+ default: "us-central1",
1462
+ options: [
1463
+ { value: "us-central1", label: "us-central1" },
1464
+ { value: "us-east4", label: "us-east4" },
1465
+ { value: "europe-west1", label: "europe-west1" },
1466
+ { value: "europe-west4", label: "europe-west4" },
1467
+ { value: "asia-southeast1", label: "asia-southeast1" }
1468
+ ]
1469
+ }
1470
+ ]
1471
+ }
1472
+ ],
1473
+ knownModels: VERTEX_OPENAI_KNOWN_MODELS,
1474
+ // The OpenAI-compatible endpoint on Vertex does not expose a /models list.
1475
+ supportsModelListing: false,
1476
+ docsUrl: "https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-openai"
1477
+ };
1478
+
1479
+ // src/adapters/vertex-openai/adapter.ts
1480
+ var VertexOpenAIAdapter = class {
1481
+ vendor = vertexOpenAIManifest.vendor;
1482
+ acceptedAuth = ["vertexServiceAccount"];
1483
+ client;
1484
+ init(auth, client) {
1485
+ if (auth.kind !== "vertexServiceAccount") {
1486
+ throw new Error(
1487
+ `VertexOpenAIAdapter: unsupported auth kind '${auth.kind}'`
1488
+ );
1489
+ }
1490
+ if (!auth.credentials) {
1491
+ throw new Error("VertexOpenAIAdapter: service-account credentials are required");
1492
+ }
1493
+ if (!auth.projectId) {
1494
+ throw new Error("VertexOpenAIAdapter: projectId is required");
1495
+ }
1496
+ if (!auth.location) {
1497
+ throw new Error("VertexOpenAIAdapter: location is required");
1498
+ }
1499
+ const baseURL = `https://${auth.location}-aiplatform.googleapis.com/v1/projects/${auth.projectId}/locations/${auth.location}/endpoints/openapi`;
1500
+ const googleAuth = new GoogleAuth({
1501
+ credentials: auth.credentials,
1502
+ scopes: ["https://www.googleapis.com/auth/cloud-platform"]
1503
+ });
1504
+ const authedFetch = async (input, init) => {
1505
+ const googleClient = await googleAuth.getClient();
1506
+ const token = await googleClient.getAccessToken();
1507
+ const headers = new Headers(init?.headers);
1508
+ headers.set("Authorization", `Bearer ${token.token ?? ""}`);
1509
+ return fetch(input, { ...init, headers });
1510
+ };
1511
+ this.client = new OpenAI2({
1512
+ apiKey: "vertex-ai",
1513
+ baseURL,
1514
+ fetch: authedFetch,
1515
+ ...client?.timeout !== void 0 ? { timeout: client.timeout } : {},
1516
+ ...client?.maxRetries !== void 0 ? { maxRetries: client.maxRetries } : {}
1517
+ });
1518
+ }
1519
+ stream(req) {
1520
+ return streamFromOpenAI(this.ensureClient(), req, {
1521
+ knownModels: vertexOpenAIManifest.knownModels
1522
+ });
1523
+ }
1524
+ appendToolResult(history, toolCallId, result) {
1525
+ return appendOpenAIToolResult(history, toolCallId, result);
1526
+ }
1527
+ async listAvailableModels() {
1528
+ return [...vertexOpenAIManifest.knownModels];
1529
+ }
1530
+ async warmup() {
1531
+ }
1532
+ ensureClient() {
1533
+ if (!this.client) {
1534
+ throw new Error(
1535
+ "VertexOpenAIAdapter: init() must be called before stream()/listAvailableModels()"
1536
+ );
1537
+ }
1538
+ return this.client;
1539
+ }
1540
+ };
1541
+
1542
+ // src/adapters/vertex-openai/factory.ts
1543
+ var vertexOpenAIFactory = {
1544
+ vendor: vertexOpenAIManifest.vendor,
1545
+ manifest: vertexOpenAIManifest,
1546
+ create: () => new VertexOpenAIAdapter()
1547
+ };
1548
+
1549
+ // src/adapters/bedrock/adapter.ts
1550
+ import {
1551
+ BedrockRuntimeClient,
1552
+ ConverseStreamCommand
1553
+ } from "@aws-sdk/client-bedrock-runtime";
1554
+
1555
+ // src/adapters/bedrock/manifest.ts
1556
+ var BEDROCK_KNOWN_MODELS = [
1557
+ {
1558
+ id: "anthropic.claude-opus-4-v1:0",
1559
+ displayName: "Claude Opus 4 (on Bedrock)",
1560
+ capabilities: {
1561
+ streaming: true,
1562
+ tools: true,
1563
+ vision: true,
1564
+ systemPrompt: true,
1565
+ maxContextTokens: 2e5
1566
+ }
1567
+ },
1568
+ {
1569
+ id: "anthropic.claude-sonnet-4-v1:0",
1570
+ displayName: "Claude Sonnet 4 (on Bedrock)",
1571
+ capabilities: {
1572
+ streaming: true,
1573
+ tools: true,
1574
+ vision: true,
1575
+ systemPrompt: true,
1576
+ maxContextTokens: 2e5
1577
+ }
1578
+ },
1579
+ {
1580
+ id: "anthropic.claude-3-7-sonnet-20250219-v1:0",
1581
+ displayName: "Claude 3.7 Sonnet (on Bedrock)",
1582
+ capabilities: {
1583
+ streaming: true,
1584
+ tools: true,
1585
+ vision: true,
1586
+ systemPrompt: true,
1587
+ maxContextTokens: 2e5
1588
+ }
1589
+ },
1590
+ {
1591
+ id: "anthropic.claude-3-5-sonnet-20241022-v2:0",
1592
+ displayName: "Claude 3.5 Sonnet v2 (on Bedrock)",
1593
+ capabilities: {
1594
+ streaming: true,
1595
+ tools: true,
1596
+ vision: true,
1597
+ systemPrompt: true,
1598
+ maxContextTokens: 2e5
1599
+ }
1600
+ },
1601
+ {
1602
+ id: "anthropic.claude-3-5-haiku-20241022-v1:0",
1603
+ displayName: "Claude 3.5 Haiku (on Bedrock)",
1604
+ capabilities: {
1605
+ streaming: true,
1606
+ tools: true,
1607
+ vision: false,
1608
+ systemPrompt: true,
1609
+ maxContextTokens: 2e5
1610
+ }
1611
+ },
1612
+ {
1613
+ id: "meta.llama3-3-70b-instruct-v1:0",
1614
+ displayName: "Llama 3.3 70B Instruct (on Bedrock)",
1615
+ capabilities: {
1616
+ streaming: true,
1617
+ tools: true,
1618
+ vision: false,
1619
+ systemPrompt: true,
1620
+ maxContextTokens: 128e3
1621
+ }
1622
+ },
1623
+ {
1624
+ id: "mistral.mistral-large-2407-v1:0",
1625
+ displayName: "Mistral Large (on Bedrock)",
1626
+ capabilities: {
1627
+ streaming: true,
1628
+ tools: true,
1629
+ vision: false,
1630
+ systemPrompt: true,
1631
+ maxContextTokens: 128e3
1632
+ }
1633
+ },
1634
+ {
1635
+ id: "amazon.nova-pro-v1:0",
1636
+ displayName: "Amazon Nova Pro",
1637
+ capabilities: {
1638
+ streaming: true,
1639
+ tools: true,
1640
+ vision: true,
1641
+ systemPrompt: true,
1642
+ maxContextTokens: 3e5
1643
+ }
1644
+ },
1645
+ {
1646
+ id: "amazon.nova-lite-v1:0",
1647
+ displayName: "Amazon Nova Lite",
1648
+ capabilities: {
1649
+ streaming: true,
1650
+ tools: true,
1651
+ vision: true,
1652
+ systemPrompt: true,
1653
+ maxContextTokens: 3e5
1654
+ }
1655
+ },
1656
+ {
1657
+ id: "amazon.nova-micro-v1:0",
1658
+ displayName: "Amazon Nova Micro",
1659
+ capabilities: {
1660
+ streaming: true,
1661
+ tools: true,
1662
+ vision: false,
1663
+ systemPrompt: true,
1664
+ maxContextTokens: 128e3
1665
+ }
1666
+ }
1667
+ ];
1668
+ var BEDROCK_REGIONS = [
1669
+ { value: "us-east-1", label: "us-east-1 (N. Virginia)" },
1670
+ { value: "us-east-2", label: "us-east-2 (Ohio)" },
1671
+ { value: "us-west-2", label: "us-west-2 (Oregon)" },
1672
+ { value: "eu-west-1", label: "eu-west-1 (Ireland)" },
1673
+ { value: "eu-west-3", label: "eu-west-3 (Paris)" },
1674
+ { value: "eu-central-1", label: "eu-central-1 (Frankfurt)" },
1675
+ { value: "ap-northeast-1", label: "ap-northeast-1 (Tokyo)" },
1676
+ { value: "ap-south-1", label: "ap-south-1 (Mumbai)" },
1677
+ { value: "ap-southeast-1", label: "ap-southeast-1 (Singapore)" },
1678
+ { value: "ap-southeast-2", label: "ap-southeast-2 (Sydney)" },
1679
+ { value: "ca-central-1", label: "ca-central-1 (Canada)" }
1680
+ ];
1681
+ var bedrockManifest = {
1682
+ vendor: "bedrock",
1683
+ displayName: "AWS Bedrock",
1684
+ authKinds: [
1685
+ {
1686
+ kind: "bedrockApiKey",
1687
+ displayName: "API Key",
1688
+ fields: [
1689
+ {
1690
+ name: "apiKey",
1691
+ label: "API Key",
1692
+ type: "password",
1693
+ required: true,
1694
+ help: "Bedrock API key (bearer token). Create one in the AWS console under Amazon Bedrock \u2192 API keys."
1695
+ },
1696
+ {
1697
+ name: "region",
1698
+ label: "Region",
1699
+ type: "select",
1700
+ required: true,
1701
+ default: "us-east-1",
1702
+ options: BEDROCK_REGIONS
1703
+ }
1704
+ ]
1705
+ },
1706
+ {
1707
+ kind: "bedrockIam",
1708
+ displayName: "IAM Credentials",
1709
+ fields: [
1710
+ {
1711
+ name: "accessKeyId",
1712
+ label: "Access Key ID",
1713
+ type: "text",
1714
+ required: true
1715
+ },
1716
+ {
1717
+ name: "secretAccessKey",
1718
+ label: "Secret Access Key",
1719
+ type: "password",
1720
+ required: true
1721
+ },
1722
+ {
1723
+ name: "sessionToken",
1724
+ label: "Session Token",
1725
+ type: "password",
1726
+ required: false,
1727
+ help: "Only required when using temporary credentials (e.g., STS)."
1728
+ },
1729
+ {
1730
+ name: "region",
1731
+ label: "Region",
1732
+ type: "select",
1733
+ required: true,
1734
+ default: "us-east-1",
1735
+ options: BEDROCK_REGIONS
1736
+ }
1737
+ ]
1738
+ }
1739
+ ],
1740
+ knownModels: BEDROCK_KNOWN_MODELS,
1741
+ // The Bedrock runtime client does not expose a `/models` listing — model
1742
+ // enumeration lives in `@aws-sdk/client-bedrock` (control plane), which we
1743
+ // don't pull in. Return the curated set from the manifest.
1744
+ supportsModelListing: false,
1745
+ docsUrl: "https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html"
1746
+ };
1747
+
1748
+ // src/adapters/bedrock/adapter.ts
1749
+ var BedrockAdapter = class {
1750
+ vendor = bedrockManifest.vendor;
1751
+ acceptedAuth = ["bedrockApiKey", "bedrockIam"];
1752
+ client;
1753
+ init(auth, client) {
1754
+ if (!auth.region) {
1755
+ throw new Error("BedrockAdapter: region is required");
1756
+ }
1757
+ const config = {
1758
+ region: auth.region,
1759
+ ...client?.endpoint ? { endpoint: client.endpoint } : {},
1760
+ ...client?.maxRetries !== void 0 ? { maxAttempts: client.maxRetries + 1 } : {}
1761
+ };
1762
+ if (auth.kind === "bedrockApiKey") {
1763
+ if (!auth.apiKey) {
1764
+ throw new Error("BedrockAdapter: apiKey is required for bedrockApiKey auth");
1765
+ }
1766
+ config.token = { token: auth.apiKey };
1767
+ config.authSchemePreference = ["httpBearerAuth"];
1768
+ } else if (auth.kind === "bedrockIam") {
1769
+ if (!auth.accessKeyId || !auth.secretAccessKey) {
1770
+ throw new Error("BedrockAdapter: accessKeyId and secretAccessKey required for bedrockIam auth");
1771
+ }
1772
+ config.credentials = {
1773
+ accessKeyId: auth.accessKeyId,
1774
+ secretAccessKey: auth.secretAccessKey,
1775
+ ...auth.sessionToken ? { sessionToken: auth.sessionToken } : {}
1776
+ };
1777
+ } else {
1778
+ throw new Error(
1779
+ `BedrockAdapter: unsupported auth kind '${auth.kind}'`
1780
+ );
1781
+ }
1782
+ this.client = new BedrockRuntimeClient(config);
1783
+ }
1784
+ async *stream(req) {
1785
+ assertValidRequest(req);
1786
+ const client = this.ensureClient();
1787
+ if (req.signal?.aborted) {
1788
+ yield { type: "end", finishReason: "aborted" };
1789
+ return;
1790
+ }
1791
+ const messages = buildWireMessages3(req);
1792
+ const tools = req.tools && req.tools.length > 0 ? { tools: formatTools4(req.tools) } : void 0;
1793
+ const inferenceConfig = {};
1794
+ if (req.maxTokens !== void 0) inferenceConfig.maxTokens = req.maxTokens;
1795
+ if (req.temperature !== void 0) inferenceConfig.temperature = req.temperature;
1796
+ const commandInput = {
1797
+ modelId: req.model,
1798
+ messages
1799
+ };
1800
+ if (req.system) commandInput.system = [{ text: req.system }];
1801
+ if (tools) commandInput.toolConfig = tools;
1802
+ if (Object.keys(inferenceConfig).length > 0) {
1803
+ commandInput.inferenceConfig = inferenceConfig;
1804
+ }
1805
+ const command = new ConverseStreamCommand(
1806
+ commandInput
1807
+ );
1808
+ let response;
1809
+ try {
1810
+ response = await client.send(command, { abortSignal: req.signal });
1811
+ } catch (err) {
1812
+ if (isAbortError4(err)) {
1813
+ yield { type: "end", finishReason: "aborted" };
1814
+ return;
1815
+ }
1816
+ throw err;
1817
+ }
1818
+ if (!response.stream) {
1819
+ yield { type: "end", finishReason: "stop" };
1820
+ return;
1821
+ }
1822
+ let currentToolUse = null;
1823
+ const completedToolCalls = [];
1824
+ let stopReason;
1825
+ let inputTokens;
1826
+ let outputTokens;
1827
+ let totalTokens;
1828
+ try {
1829
+ for await (const event of response.stream) {
1830
+ if (req.signal?.aborted) {
1831
+ yield { type: "end", finishReason: "aborted" };
1832
+ return;
1833
+ }
1834
+ if (event.contentBlockStart) {
1835
+ const start = event.contentBlockStart.start;
1836
+ if (start?.toolUse) {
1837
+ currentToolUse = {
1838
+ id: start.toolUse.toolUseId ?? "",
1839
+ name: start.toolUse.name ?? "",
1840
+ inputText: ""
1841
+ };
1842
+ if (currentToolUse.id && currentToolUse.name) {
1843
+ yield {
1844
+ type: "toolCallStart",
1845
+ id: currentToolUse.id,
1846
+ name: currentToolUse.name
1847
+ };
1848
+ }
1849
+ }
1850
+ } else if (event.contentBlockDelta) {
1851
+ const delta = event.contentBlockDelta.delta;
1852
+ if (delta?.text) {
1853
+ yield { type: "token", text: delta.text };
1854
+ }
1855
+ if (delta?.toolUse && currentToolUse && typeof delta.toolUse.input === "string") {
1856
+ currentToolUse.inputText += delta.toolUse.input;
1857
+ }
1858
+ } else if (event.contentBlockStop) {
1859
+ if (currentToolUse && currentToolUse.id && currentToolUse.name) {
1860
+ let parsed = {};
1861
+ try {
1862
+ parsed = currentToolUse.inputText ? JSON.parse(currentToolUse.inputText) : {};
1863
+ } catch {
1864
+ parsed = {};
1865
+ }
1866
+ completedToolCalls.push({
1867
+ id: currentToolUse.id,
1868
+ name: currentToolUse.name,
1869
+ arguments: parsed
1870
+ });
1871
+ }
1872
+ currentToolUse = null;
1873
+ } else if (event.messageStop) {
1874
+ stopReason = event.messageStop.stopReason;
1875
+ } else if (event.metadata) {
1876
+ if (event.metadata.usage) {
1877
+ inputTokens = event.metadata.usage.inputTokens;
1878
+ outputTokens = event.metadata.usage.outputTokens;
1879
+ totalTokens = event.metadata.usage.totalTokens;
1880
+ }
1881
+ }
1882
+ }
1883
+ } catch (err) {
1884
+ if (isAbortError4(err)) {
1885
+ yield { type: "end", finishReason: "aborted" };
1886
+ return;
1887
+ }
1888
+ throw err;
1889
+ }
1890
+ if (req.signal?.aborted) {
1891
+ yield { type: "end", finishReason: "aborted" };
1892
+ return;
1893
+ }
1894
+ for (const tc of completedToolCalls) {
1895
+ yield {
1896
+ type: "toolCall",
1897
+ id: tc.id,
1898
+ name: tc.name,
1899
+ arguments: tc.arguments
1900
+ };
1901
+ }
1902
+ const finishReason = mapStopReason2(stopReason, completedToolCalls.length > 0);
1903
+ const usage = inputTokens !== void 0 || outputTokens !== void 0 || totalTokens !== void 0 ? {
1904
+ ...inputTokens !== void 0 ? { inputTokens } : {},
1905
+ ...outputTokens !== void 0 ? { outputTokens } : {},
1906
+ ...totalTokens !== void 0 ? { totalTokens } : {}
1907
+ } : void 0;
1908
+ const endEvent = {
1909
+ type: "end",
1910
+ finishReason,
1911
+ ...usage ? { usage } : {},
1912
+ ...stopReason ? { rawReason: stopReason } : {}
1913
+ };
1914
+ yield endEvent;
1915
+ }
1916
+ appendToolResult(history, toolCallId, result) {
1917
+ const text = typeof result === "string" ? result : JSON.stringify(result);
1918
+ const wireMessage = {
1919
+ role: "user",
1920
+ content: [
1921
+ {
1922
+ toolResult: {
1923
+ toolUseId: toolCallId,
1924
+ content: [{ text }]
1925
+ }
1926
+ }
1927
+ ]
1928
+ };
1929
+ return [
1930
+ ...history,
1931
+ {
1932
+ role: "tool",
1933
+ content: text,
1934
+ vendorRaw: wireMessage
1935
+ }
1936
+ ];
1937
+ }
1938
+ async listAvailableModels() {
1939
+ return [...bedrockManifest.knownModels];
1940
+ }
1941
+ async warmup() {
1942
+ }
1943
+ ensureClient() {
1944
+ if (!this.client) {
1945
+ throw new Error(
1946
+ "BedrockAdapter: init() must be called before stream()/listAvailableModels()"
1947
+ );
1948
+ }
1949
+ return this.client;
1950
+ }
1951
+ };
1952
+ function buildWireMessages3(req) {
1953
+ const out = [];
1954
+ for (const msg of req.messages) {
1955
+ if (msg.vendorRaw && typeof msg.vendorRaw === "object") {
1956
+ out.push(msg.vendorRaw);
1957
+ continue;
1958
+ }
1959
+ const text = typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content);
1960
+ const role = msg.role === "tool" ? "user" : msg.role;
1961
+ out.push({
1962
+ role,
1963
+ content: [{ text }]
1964
+ });
1965
+ }
1966
+ return out;
1967
+ }
1968
+ function formatTools4(tools) {
1969
+ return tools.map((t) => ({
1970
+ toolSpec: {
1971
+ name: t.name,
1972
+ // Bedrock rejects empty-string descriptions; fall back to a single space.
1973
+ description: t.description && t.description.length > 0 ? t.description : " ",
1974
+ inputSchema: {
1975
+ json: t.parameters ?? { type: "object", properties: {} }
1976
+ }
1977
+ }
1978
+ }));
1979
+ }
1980
+ function mapStopReason2(r, hadToolCalls) {
1981
+ if (hadToolCalls) return "tool";
1982
+ switch (r) {
1983
+ case "end_turn":
1984
+ case "stop_sequence":
1985
+ return "stop";
1986
+ case "tool_use":
1987
+ return "tool";
1988
+ case "max_tokens":
1989
+ return "length";
1990
+ case "content_filtered":
1991
+ case "guardrail_intervened":
1992
+ return "filtered";
1993
+ default:
1994
+ return "stop";
1995
+ }
1996
+ }
1997
+ function isAbortError4(err) {
1998
+ if (!err || typeof err !== "object") return false;
1999
+ const e = err;
2000
+ return e.name === "AbortError" || e.name === "APIUserAbortError" || e.code === "ABORT_ERR" || typeof e.message === "string" && /aborted/i.test(e.message);
2001
+ }
2002
+
2003
+ // src/adapters/bedrock/factory.ts
2004
+ var bedrockFactory = {
2005
+ vendor: bedrockManifest.vendor,
2006
+ manifest: bedrockManifest,
2007
+ create: () => new BedrockAdapter()
2008
+ };
2009
+
2010
+ // src/adapters/index.ts
2011
+ registerAdapter(openAIFactory);
2012
+ registerAdapter(deepseekFactory);
2013
+ registerAdapter(anthropicFactory);
2014
+ registerAdapter(googleFactory);
2015
+ registerAdapter(vertexGeminiFactory);
2016
+ registerAdapter(vertexOpenAIFactory);
2017
+ registerAdapter(bedrockFactory);
2018
+
2019
+ // src/normalize-auth.ts
2020
+ function normalizeAuth(vendor, raw) {
2021
+ switch (vendor) {
2022
+ case "openai":
2023
+ case "anthropic":
2024
+ return requireApiKey(vendor, raw);
2025
+ case "deepseek":
2026
+ return {
2027
+ ...requireApiKey(vendor, raw),
2028
+ baseURL: raw.api_url ?? "https://api.deepseek.com/v1"
2029
+ };
2030
+ case "google":
2031
+ return normalizeGoogle(raw);
2032
+ case "vertex":
2033
+ case "vertex-gemini":
2034
+ case "vertex-openai":
2035
+ return normalizeVertex(vendor, raw);
2036
+ case "bedrock":
2037
+ return normalizeBedrock(raw);
2038
+ default:
2039
+ throw new Error(
2040
+ `normalizeAuth: unknown vendor '${vendor}'. If this is a custom adapter, construct the AuthSpec directly.`
2041
+ );
2042
+ }
2043
+ }
2044
+ function requireApiKey(vendor, raw) {
2045
+ if (!raw.api_key) {
2046
+ throw new Error(`normalizeAuth: vendor '${vendor}' requires 'api_key'`);
2047
+ }
2048
+ const result = {
2049
+ kind: "apiKey",
2050
+ apiKey: raw.api_key
2051
+ };
2052
+ if (raw.api_url) {
2053
+ result.baseURL = raw.api_url;
2054
+ }
2055
+ return result;
2056
+ }
2057
+ function normalizeGoogle(raw) {
2058
+ if (raw.service_key) {
2059
+ return {
2060
+ kind: "googleServiceAccount",
2061
+ credentials: parseServiceKey(raw.service_key)
2062
+ };
2063
+ }
2064
+ if (raw.api_key) {
2065
+ return { kind: "googleApiKey", apiKey: raw.api_key };
2066
+ }
2067
+ throw new Error(`normalizeAuth: vendor 'google' requires either 'api_key' or 'service_key'`);
2068
+ }
2069
+ function normalizeVertex(vendor, raw) {
2070
+ if (!raw.service_key) {
2071
+ throw new Error(`normalizeAuth: vendor '${vendor}' requires 'service_key'`);
2072
+ }
2073
+ if (!raw.location) {
2074
+ throw new Error(`normalizeAuth: vendor '${vendor}' requires 'location'`);
2075
+ }
2076
+ const credentials = parseServiceKey(raw.service_key);
2077
+ const projectId = raw.project_id ?? credentials.project_id;
2078
+ if (!projectId) {
2079
+ throw new Error(
2080
+ `normalizeAuth: vendor '${vendor}' requires 'project_id' (not present in raw credential or service_key.project_id)`
2081
+ );
2082
+ }
2083
+ return {
2084
+ kind: "vertexServiceAccount",
2085
+ credentials,
2086
+ projectId,
2087
+ location: raw.location
2088
+ };
2089
+ }
2090
+ function normalizeBedrock(raw) {
2091
+ if (!raw.region) {
2092
+ throw new Error(`normalizeAuth: vendor 'bedrock' requires 'region'`);
2093
+ }
2094
+ if (raw.access_key_id && raw.secret_access_key) {
2095
+ const result = {
2096
+ kind: "bedrockIam",
2097
+ accessKeyId: raw.access_key_id,
2098
+ secretAccessKey: raw.secret_access_key,
2099
+ region: raw.region
2100
+ };
2101
+ if (raw.session_token) {
2102
+ result.sessionToken = raw.session_token;
2103
+ }
2104
+ return result;
2105
+ }
2106
+ if (raw.api_key) {
2107
+ return {
2108
+ kind: "bedrockApiKey",
2109
+ apiKey: raw.api_key,
2110
+ region: raw.region
2111
+ };
2112
+ }
2113
+ throw new Error(
2114
+ `normalizeAuth: vendor 'bedrock' requires either (access_key_id + secret_access_key) or api_key`
2115
+ );
2116
+ }
2117
+ function parseServiceKey(value) {
2118
+ if (typeof value === "string") {
2119
+ try {
2120
+ return JSON.parse(value);
2121
+ } catch {
2122
+ throw new Error(`normalizeAuth: 'service_key' is not valid JSON`);
2123
+ }
2124
+ }
2125
+ return value;
2126
+ }
2127
+
2128
+ // src/index.ts
2129
+ async function createLlm(args) {
2130
+ const factory = getAdapterFactory(args.vendor);
2131
+ if (!factory.manifest.authKinds.some((k) => k.kind === args.auth.kind)) {
2132
+ const accepted = factory.manifest.authKinds.map((k) => k.kind).join(", ");
2133
+ throw new Error(
2134
+ `Vendor '${args.vendor}' does not accept auth kind '${args.auth.kind}'. Accepted kinds: ${accepted}`
2135
+ );
2136
+ }
2137
+ const instance = factory.create();
2138
+ await Promise.resolve(instance.init(args.auth, args.client));
2139
+ return instance;
2140
+ }
2141
+ export {
2142
+ assertValidRequest,
2143
+ createLlm,
2144
+ getAdapterFactory,
2145
+ getManifest,
2146
+ listVendors,
2147
+ normalizeAuth,
2148
+ registerAdapter,
2149
+ replaceAdapter,
2150
+ unregisterAdapter
2151
+ };
2152
+ //# sourceMappingURL=index.js.map