@happyvertical/ai 0.80.0 → 0.80.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/chunks/anthropic-DA8sdt12.js +438 -0
  2. package/dist/chunks/anthropic-DA8sdt12.js.map +1 -0
  3. package/dist/chunks/bedrock-DDiYFUw0.js +699 -0
  4. package/dist/chunks/bedrock-DDiYFUw0.js.map +1 -0
  5. package/dist/chunks/bifrost-fM9FpW_w.js +198 -0
  6. package/dist/chunks/bifrost-fM9FpW_w.js.map +1 -0
  7. package/dist/chunks/claude-cli-CnJh5KQT.js +498 -0
  8. package/dist/chunks/claude-cli-CnJh5KQT.js.map +1 -0
  9. package/dist/chunks/gateway-admin-CiKvHst7.js +283 -0
  10. package/dist/chunks/gateway-admin-CiKvHst7.js.map +1 -0
  11. package/dist/chunks/gemini-Bu7Fke7c.js +593 -0
  12. package/dist/chunks/gemini-Bu7Fke7c.js.map +1 -0
  13. package/dist/chunks/huggingface-B07_6wHW.js +295 -0
  14. package/dist/chunks/huggingface-B07_6wHW.js.map +1 -0
  15. package/dist/chunks/litellm-D_Oo9OQ_.js +185 -0
  16. package/dist/chunks/litellm-D_Oo9OQ_.js.map +1 -0
  17. package/dist/chunks/ollama-D4ksOTO8.js +626 -0
  18. package/dist/chunks/ollama-D4ksOTO8.js.map +1 -0
  19. package/dist/chunks/openai--F38QHiJ.js +691 -0
  20. package/dist/chunks/openai--F38QHiJ.js.map +1 -0
  21. package/dist/chunks/qwen-tts-DRRbOrhA.js +333 -0
  22. package/dist/chunks/qwen-tts-DRRbOrhA.js.map +1 -0
  23. package/dist/chunks/rate-limit-BwyPGXFW.js +199 -0
  24. package/dist/chunks/rate-limit-BwyPGXFW.js.map +1 -0
  25. package/dist/chunks/safety-CEnoJS6X.js +260 -0
  26. package/dist/chunks/safety-CEnoJS6X.js.map +1 -0
  27. package/dist/chunks/types-FHs-KbGL.js +117 -0
  28. package/dist/chunks/types-FHs-KbGL.js.map +1 -0
  29. package/dist/chunks/usage-C1Y1Nlg4.js +35 -0
  30. package/dist/chunks/usage-C1Y1Nlg4.js.map +1 -0
  31. package/dist/cli/claude-context.js +17 -17
  32. package/dist/cli/claude-context.js.map +1 -1
  33. package/dist/index.js +717 -23
  34. package/dist/index.js.map +1 -1
  35. package/package.json +5 -5
  36. package/dist/chunks/anthropic-2z-82zgr.js +0 -530
  37. package/dist/chunks/anthropic-2z-82zgr.js.map +0 -1
  38. package/dist/chunks/bedrock-Dc2eVPUD.js +0 -920
  39. package/dist/chunks/bedrock-Dc2eVPUD.js.map +0 -1
  40. package/dist/chunks/bifrost-CEnCsciy.js +0 -258
  41. package/dist/chunks/bifrost-CEnCsciy.js.map +0 -1
  42. package/dist/chunks/claude-cli-X1ONjE8K.js +0 -603
  43. package/dist/chunks/claude-cli-X1ONjE8K.js.map +0 -1
  44. package/dist/chunks/gateway-admin-BUhBzXZb.js +0 -359
  45. package/dist/chunks/gateway-admin-BUhBzXZb.js.map +0 -1
  46. package/dist/chunks/gemini-CS56gY0D.js +0 -768
  47. package/dist/chunks/gemini-CS56gY0D.js.map +0 -1
  48. package/dist/chunks/huggingface-wQSfO5xA.js +0 -411
  49. package/dist/chunks/huggingface-wQSfO5xA.js.map +0 -1
  50. package/dist/chunks/index-DCXO0nZA.js +0 -1293
  51. package/dist/chunks/index-DCXO0nZA.js.map +0 -1
  52. package/dist/chunks/litellm-BMFTYbWc.js +0 -245
  53. package/dist/chunks/litellm-BMFTYbWc.js.map +0 -1
  54. package/dist/chunks/ollama-DsGDrA-c.js +0 -962
  55. package/dist/chunks/ollama-DsGDrA-c.js.map +0 -1
  56. package/dist/chunks/openai-CJEo69jb.js +0 -882
  57. package/dist/chunks/openai-CJEo69jb.js.map +0 -1
  58. package/dist/chunks/qwen-tts-BLYZ6d9s.js +0 -365
  59. package/dist/chunks/qwen-tts-BLYZ6d9s.js.map +0 -1
  60. package/dist/chunks/usage-DMWiJ2oB.js +0 -21
  61. package/dist/chunks/usage-DMWiJ2oB.js.map +0 -1
@@ -1,920 +0,0 @@
1
- import { b as normalizeBaseAIOptions, A as AIError, n as normalizeChatOptions, p as prepareRequestControls, a as normalizeImageGenerationOptions, f as extractTextContent, d as AuthenticationError, R as RateLimitError, M as ModelNotFoundError, c as ContextLengthError } from "./index-DCXO0nZA.js";
2
- import { e as emitUsage } from "./usage-DMWiJ2oB.js";
3
- const BEDROCK_DEFAULT_CHAT_MODEL = "anthropic.claude-3-5-sonnet-20241022-v2:0";
4
- const BEDROCK_TEXT_EMBEDDING_MODEL = "amazon.titan-embed-text-v2:0";
5
- const BEDROCK_IMAGE_EMBEDDING_MODEL = "amazon.titan-embed-image-v1";
6
- const BEDROCK_IMAGE_GENERATION_MODEL = "amazon.titan-image-generator-v2:0";
7
- class BedrockProvider {
8
- options;
9
- client;
10
- // Will be BedrockRuntimeClient instance from @aws-sdk/client-bedrock-runtime
11
- constructor(options) {
12
- this.options = normalizeBaseAIOptions({
13
- defaultModel: BEDROCK_DEFAULT_CHAT_MODEL,
14
- ...options
15
- });
16
- this.initializeClientSync();
17
- }
18
- initializeClientSync() {
19
- try {
20
- import("@aws-sdk/client-bedrock-runtime").then(({ BedrockRuntimeClient }) => {
21
- this.client = new BedrockRuntimeClient({
22
- region: this.options.region,
23
- credentials: this.options.credentials,
24
- endpoint: this.options.endpoint,
25
- maxAttempts: (this.options.maxRetries || 0) + 1
26
- });
27
- }).catch(() => {
28
- });
29
- } catch (_error) {
30
- }
31
- }
32
- async ensureClient() {
33
- if (!this.client) {
34
- try {
35
- const { BedrockRuntimeClient } = await import("@aws-sdk/client-bedrock-runtime");
36
- this.client = new BedrockRuntimeClient({
37
- region: this.options.region,
38
- credentials: this.options.credentials,
39
- endpoint: this.options.endpoint,
40
- maxAttempts: (this.options.maxRetries || 0) + 1
41
- });
42
- } catch (_error) {
43
- throw new AIError(
44
- "Failed to initialize Bedrock client. Make sure @aws-sdk/client-bedrock-runtime is installed.",
45
- "INITIALIZATION_ERROR",
46
- "bedrock"
47
- );
48
- }
49
- }
50
- }
51
- async chat(messages, options = {}) {
52
- const startTime = Date.now();
53
- let controls;
54
- try {
55
- await this.ensureClient();
56
- const modelId = options.model || this.options.defaultModel;
57
- options = normalizeChatOptions(this.options, options, "bedrock", modelId);
58
- controls = prepareRequestControls(this.options, options);
59
- const response = this.mapConverseResponse(
60
- await this.client.converse(
61
- await this.buildConverseRequest(
62
- messages,
63
- options,
64
- modelId,
65
- controls.signal
66
- ),
67
- { abortSignal: controls.signal }
68
- ),
69
- modelId
70
- );
71
- emitUsage(
72
- this.options,
73
- "bedrock",
74
- "chat",
75
- response.model || modelId || "unknown",
76
- response.usage,
77
- startTime,
78
- options.usageTags
79
- );
80
- return response;
81
- } catch (error) {
82
- if (controls?.didTimeout()) {
83
- throw new AIError(
84
- `AI request timed out after ${controls.timeout}ms`,
85
- "AI_TIMEOUT",
86
- "bedrock",
87
- options.model
88
- );
89
- }
90
- if (options.signal?.aborted) {
91
- throw new AIError(
92
- "AI request aborted by caller",
93
- "AI_ABORTED",
94
- "bedrock",
95
- options.model
96
- );
97
- }
98
- throw this.mapError(error);
99
- } finally {
100
- controls?.cleanup();
101
- }
102
- }
103
- async complete(prompt, options = {}) {
104
- return this.chat([{ role: "user", content: prompt }], {
105
- model: options.model,
106
- maxTokens: options.maxTokens,
107
- temperature: options.temperature,
108
- topP: options.topP,
109
- n: options.n,
110
- stop: options.stop,
111
- stream: options.stream,
112
- onProgress: options.onProgress,
113
- signal: options.signal,
114
- timeout: options.timeout,
115
- reasoning: options.reasoning,
116
- usageTags: options.usageTags
117
- });
118
- }
119
- /**
120
- * Simple message interface for single-turn interactions with optional history
121
- *
122
- * @param text - The message text to send
123
- * @param options - Configuration options including history, model, etc.
124
- * @returns Promise resolving to the response content string
125
- */
126
- async message(text, options = {}) {
127
- const messages = [
128
- ...options.history || [],
129
- { role: options.role || "user", content: text }
130
- ];
131
- const response = await this.chat(messages, {
132
- model: options.model,
133
- maxTokens: options.maxTokens,
134
- temperature: options.temperature,
135
- topP: options.topP,
136
- stop: options.stop,
137
- stream: options.stream,
138
- frequencyPenalty: options.frequencyPenalty,
139
- presencePenalty: options.presencePenalty,
140
- responseFormat: options.responseFormat,
141
- seed: options.seed,
142
- tools: options.tools,
143
- toolChoice: options.toolChoice,
144
- onProgress: options.onProgress,
145
- signal: options.signal,
146
- timeout: options.timeout,
147
- reasoning: options.reasoning,
148
- usageTags: options.usageTags
149
- });
150
- return response.content;
151
- }
152
- async embed(text, options = {}) {
153
- const startTime = Date.now();
154
- try {
155
- await this.ensureClient();
156
- const model = options.model || BEDROCK_TEXT_EMBEDDING_MODEL;
157
- const inputs = Array.isArray(text) ? text : [text];
158
- const embeddings = [];
159
- let totalTokens = 0;
160
- for (const inputText of inputs) {
161
- const response = await this.client.invokeModel({
162
- modelId: model,
163
- contentType: "application/json",
164
- accept: "application/json",
165
- body: JSON.stringify({
166
- inputText,
167
- ...options.dimensions && { dimensions: options.dimensions },
168
- normalize: true
169
- })
170
- });
171
- const payload = await this.parseInvokeModelBody(response.body);
172
- const embedding = this.extractEmbeddingVector(payload);
173
- if (embedding) {
174
- embeddings.push(embedding);
175
- }
176
- totalTokens += payload.inputTextTokenCount || payload.inputTokenCount || payload.tokenCount || 0;
177
- }
178
- const usage = totalTokens > 0 ? {
179
- promptTokens: totalTokens,
180
- completionTokens: 0,
181
- totalTokens
182
- } : void 0;
183
- emitUsage(
184
- this.options,
185
- "bedrock",
186
- "embed",
187
- model,
188
- usage,
189
- startTime,
190
- options.usageTags
191
- );
192
- return {
193
- embeddings,
194
- model,
195
- usage
196
- };
197
- } catch (error) {
198
- throw this.mapError(error);
199
- }
200
- }
201
- async embedImage(image, options = {}) {
202
- const startTime = Date.now();
203
- try {
204
- await this.ensureClient();
205
- const model = options.model || BEDROCK_IMAGE_EMBEDDING_MODEL;
206
- const response = await this.client.invokeModel({
207
- modelId: model,
208
- contentType: "application/json",
209
- accept: "application/json",
210
- body: JSON.stringify({
211
- inputImage: await this.imageToBase64(image),
212
- ...options.dimensions && {
213
- embeddingConfig: {
214
- outputEmbeddingLength: options.dimensions
215
- }
216
- }
217
- })
218
- });
219
- const payload = await this.parseInvokeModelBody(response.body);
220
- const embedding = this.extractEmbeddingVector(payload);
221
- emitUsage(
222
- this.options,
223
- "bedrock",
224
- "embedImage",
225
- model,
226
- void 0,
227
- startTime
228
- );
229
- return {
230
- embeddings: embedding ? [embedding] : [],
231
- model
232
- };
233
- } catch (error) {
234
- throw this.mapError(error);
235
- }
236
- }
237
- async describeImage(image, prompt, options = {}) {
238
- const imageUrl = await this.imageToDataUrl(image, options.signal);
239
- const response = await this.chat(
240
- [
241
- {
242
- role: "user",
243
- content: [
244
- {
245
- type: "text",
246
- text: prompt || "Describe this image for a search index. Include objects, mood, lighting, and any visible text."
247
- },
248
- {
249
- type: "image_url",
250
- image_url: { url: imageUrl }
251
- }
252
- ]
253
- }
254
- ],
255
- {
256
- model: options.model || this.options.defaultModel,
257
- maxTokens: options.maxTokens ?? 500,
258
- signal: options.signal,
259
- timeout: options.timeout,
260
- reasoning: options.reasoning,
261
- usageTags: options.usageTags
262
- }
263
- );
264
- return response.content;
265
- }
266
- async generateImage(prompt, options = {}) {
267
- let controls;
268
- try {
269
- await this.ensureClient();
270
- const model = options.model || BEDROCK_IMAGE_GENERATION_MODEL;
271
- options = normalizeImageGenerationOptions(
272
- this.options,
273
- options,
274
- "bedrock",
275
- model
276
- );
277
- controls = prepareRequestControls(this.options, options);
278
- const size = this.resolveImageSize(options);
279
- const response = await this.client.invokeModel(
280
- {
281
- modelId: model,
282
- contentType: "application/json",
283
- accept: "application/json",
284
- body: JSON.stringify({
285
- taskType: "TEXT_IMAGE",
286
- textToImageParams: {
287
- text: prompt,
288
- ...options.imageInput && {
289
- conditionImage: await this.imageToBase64(
290
- options.imageInput,
291
- controls.signal
292
- )
293
- }
294
- },
295
- imageGenerationConfig: {
296
- numberOfImages: options.n,
297
- quality: this.mapImageQuality(options.quality),
298
- ...size
299
- }
300
- })
301
- },
302
- { abortSignal: controls.signal }
303
- );
304
- const payload = await this.parseInvokeModelBody(response.body);
305
- if (payload.error) {
306
- throw new AIError(payload.error, "API_ERROR", "bedrock");
307
- }
308
- const images = (payload.images || []).map((encoded) => {
309
- const mimeType = "image/png";
310
- if (options.outputFormat === "base64") {
311
- return { data: encoded, mimeType };
312
- }
313
- if (options.outputFormat === "url") {
314
- return { data: `data:${mimeType};base64,${encoded}`, mimeType };
315
- }
316
- return {
317
- data: Buffer.from(encoded, "base64"),
318
- mimeType
319
- };
320
- });
321
- return {
322
- images,
323
- model
324
- };
325
- } catch (error) {
326
- if (controls?.didTimeout()) {
327
- throw new AIError(
328
- `AI request timed out after ${controls.timeout}ms`,
329
- "AI_TIMEOUT",
330
- "bedrock",
331
- options.model
332
- );
333
- }
334
- if (options.signal?.aborted) {
335
- throw new AIError(
336
- "AI request aborted by caller",
337
- "AI_ABORTED",
338
- "bedrock",
339
- options.model
340
- );
341
- }
342
- throw this.mapError(error);
343
- } finally {
344
- controls?.cleanup();
345
- }
346
- }
347
- async *stream(messages, options = {}) {
348
- const startTime = Date.now();
349
- let controls;
350
- try {
351
- await this.ensureClient();
352
- const modelId = options.model || this.options.defaultModel;
353
- options = normalizeChatOptions(this.options, options, "bedrock", modelId);
354
- controls = prepareRequestControls(this.options, options);
355
- const response = await this.client.converseStream(
356
- await this.buildConverseRequest(
357
- messages,
358
- options,
359
- modelId,
360
- controls.signal
361
- ),
362
- { abortSignal: controls.signal }
363
- );
364
- let usage;
365
- for await (const event of response.stream || []) {
366
- const text = event.contentBlockDelta?.delta?.text;
367
- if (text) {
368
- if (options.onProgress) {
369
- options.onProgress(text);
370
- }
371
- yield text;
372
- }
373
- if (event.metadata?.usage) {
374
- usage = {
375
- promptTokens: event.metadata.usage.inputTokens || 0,
376
- completionTokens: event.metadata.usage.outputTokens || 0,
377
- totalTokens: event.metadata.usage.totalTokens || 0
378
- };
379
- }
380
- }
381
- emitUsage(
382
- this.options,
383
- "bedrock",
384
- "stream",
385
- modelId || "unknown",
386
- usage,
387
- startTime,
388
- options.usageTags
389
- );
390
- } catch (error) {
391
- if (controls?.didTimeout()) {
392
- throw new AIError(
393
- `AI request timed out after ${controls.timeout}ms`,
394
- "AI_TIMEOUT",
395
- "bedrock",
396
- options.model
397
- );
398
- }
399
- if (options.signal?.aborted) {
400
- throw new AIError(
401
- "AI request aborted by caller",
402
- "AI_ABORTED",
403
- "bedrock",
404
- options.model
405
- );
406
- }
407
- throw this.mapError(error);
408
- } finally {
409
- controls?.cleanup();
410
- }
411
- }
412
- async countTokens(text) {
413
- try {
414
- await this.ensureClient();
415
- const modelId = this.options.defaultModel || BEDROCK_DEFAULT_CHAT_MODEL;
416
- const response = await this.client.countTokens({
417
- modelId,
418
- input: {
419
- converse: {
420
- messages: [
421
- {
422
- role: "user",
423
- content: [{ text }]
424
- }
425
- ]
426
- }
427
- }
428
- });
429
- return response.inputTokens || Math.ceil(text.length / 4);
430
- } catch (error) {
431
- throw this.mapError(error);
432
- }
433
- }
434
- async getModels() {
435
- return [
436
- // Anthropic Claude models
437
- {
438
- id: "anthropic.claude-3-5-sonnet-20241022-v2:0",
439
- name: "Claude 3.5 Sonnet v2",
440
- description: "Latest Claude 3.5 Sonnet model on Bedrock",
441
- contextLength: 2e5,
442
- capabilities: ["text", "chat", "vision", "functions"],
443
- supportsFunctions: true,
444
- supportsVision: true
445
- },
446
- {
447
- id: "anthropic.claude-3-opus-20240229-v1:0",
448
- name: "Claude 3 Opus",
449
- description: "Most powerful Claude model on Bedrock",
450
- contextLength: 2e5,
451
- capabilities: ["text", "chat", "vision"],
452
- supportsFunctions: false,
453
- supportsVision: true
454
- },
455
- // Amazon Titan models
456
- {
457
- id: "amazon.titan-text-premier-v1:0",
458
- name: "Titan Text Premier",
459
- description: "Premier Amazon Titan text model",
460
- contextLength: 32e3,
461
- capabilities: ["text", "chat"],
462
- supportsFunctions: false,
463
- supportsVision: false
464
- },
465
- {
466
- id: "amazon.titan-embed-text-v1",
467
- name: "Titan Embeddings Text",
468
- description: "Amazon Titan text embeddings model",
469
- contextLength: 8192,
470
- capabilities: ["embeddings"],
471
- supportsFunctions: false,
472
- supportsVision: false
473
- },
474
- {
475
- id: BEDROCK_TEXT_EMBEDDING_MODEL,
476
- name: "Titan Embeddings Text V2",
477
- description: "Amazon Titan text embeddings v2 model",
478
- contextLength: 8192,
479
- capabilities: ["embeddings"],
480
- supportsFunctions: false,
481
- supportsVision: false
482
- },
483
- {
484
- id: BEDROCK_IMAGE_EMBEDDING_MODEL,
485
- name: "Titan Multimodal Embeddings G1",
486
- description: "Amazon Titan multimodal embeddings model for image similarity",
487
- contextLength: 256,
488
- capabilities: ["embeddings", "image_embedding"],
489
- supportsFunctions: false,
490
- supportsVision: false
491
- },
492
- {
493
- id: BEDROCK_IMAGE_GENERATION_MODEL,
494
- name: "Titan Image Generator V2",
495
- description: "Amazon Titan image generation model",
496
- contextLength: 0,
497
- capabilities: ["image_generation"],
498
- supportsFunctions: false,
499
- supportsVision: false
500
- },
501
- // Cohere models
502
- {
503
- id: "cohere.command-r-plus-v1:0",
504
- name: "Command R+",
505
- description: "Cohere Command R+ model with advanced capabilities",
506
- contextLength: 128e3,
507
- capabilities: ["text", "chat", "functions"],
508
- supportsFunctions: true,
509
- supportsVision: false
510
- },
511
- // Meta Llama models
512
- {
513
- id: "meta.llama3-1-405b-instruct-v1:0",
514
- name: "Llama 3.1 405B Instruct",
515
- description: "Meta Llama 3.1 405B instruction-tuned model",
516
- contextLength: 128e3,
517
- capabilities: ["text", "chat"],
518
- supportsFunctions: false,
519
- supportsVision: false
520
- }
521
- ];
522
- }
523
- async getCapabilities() {
524
- return {
525
- chat: true,
526
- completion: true,
527
- embeddings: true,
528
- streaming: true,
529
- functions: true,
530
- // Some models support function calling
531
- vision: true,
532
- // Some models support vision
533
- fineTuning: true,
534
- // Via Bedrock fine-tuning
535
- imageEmbeddings: true,
536
- imageGeneration: true,
537
- tts: false,
538
- voiceCloning: false,
539
- voiceDesign: false,
540
- maxContextLength: 2e5,
541
- supportedOperations: [
542
- "chat",
543
- "completion",
544
- "embedding",
545
- "streaming",
546
- "functions",
547
- "vision",
548
- "image_embedding",
549
- "image_generation"
550
- ]
551
- };
552
- }
553
- // ============================================================================
554
- // TTS Methods (Not supported - use Qwen3-TTS provider)
555
- // ============================================================================
556
- async synthesizeSpeech(_text, _options) {
557
- throw new AIError(
558
- "TTS is not supported by Bedrock provider. Use Qwen3-TTS provider.",
559
- "NOT_IMPLEMENTED",
560
- "bedrock"
561
- );
562
- }
563
- streamSpeech(_text, _options) {
564
- const error = new AIError(
565
- "TTS streaming is not supported by Bedrock provider. Use Qwen3-TTS provider.",
566
- "NOT_IMPLEMENTED",
567
- "bedrock"
568
- );
569
- return {
570
- [Symbol.asyncIterator]: () => ({
571
- next: () => Promise.reject(error)
572
- })
573
- };
574
- }
575
- async cloneVoice(_options) {
576
- throw new AIError(
577
- "Voice cloning is not supported by Bedrock provider. Use Qwen3-TTS provider.",
578
- "NOT_IMPLEMENTED",
579
- "bedrock"
580
- );
581
- }
582
- async designVoice(_options) {
583
- throw new AIError(
584
- "Voice design is not supported by Bedrock provider. Use Qwen3-TTS provider.",
585
- "NOT_IMPLEMENTED",
586
- "bedrock"
587
- );
588
- }
589
- async getVoices(_options) {
590
- throw new AIError(
591
- "Voice listing is not supported by Bedrock provider. Use Qwen3-TTS provider.",
592
- "NOT_IMPLEMENTED",
593
- "bedrock"
594
- );
595
- }
596
- async buildConverseRequest(messages, options, modelId, signal) {
597
- const { system, bedrockMessages } = await this.mapMessagesToBedrock(
598
- messages,
599
- signal
600
- );
601
- const systemPrompt = options.responseFormat?.type === "json_object" ? [
602
- system,
603
- "Respond with valid JSON only. Do not include explanatory text outside the JSON object."
604
- ].filter(Boolean).join("\n\n") : system;
605
- const inferenceConfig = Object.fromEntries(
606
- Object.entries({
607
- maxTokens: options.maxTokens,
608
- temperature: options.temperature,
609
- topP: options.topP,
610
- stopSequences: Array.isArray(options.stop) ? options.stop : options.stop ? [options.stop] : void 0
611
- }).filter(([, value]) => value !== void 0)
612
- );
613
- const request = {
614
- modelId,
615
- messages: bedrockMessages,
616
- ...Object.keys(inferenceConfig).length > 0 && { inferenceConfig },
617
- ...systemPrompt && { system: [{ text: systemPrompt }] }
618
- };
619
- if ((options.reasoning?.maxTokens || 0) > 0) {
620
- request.additionalModelRequestFields = {
621
- thinking: {
622
- type: "enabled",
623
- budget_tokens: options.reasoning?.maxTokens
624
- }
625
- };
626
- }
627
- const toolConfig = this.mapToolConfig(options);
628
- if (toolConfig) {
629
- request.toolConfig = toolConfig;
630
- }
631
- return request;
632
- }
633
- async mapMessagesToBedrock(messages, signal) {
634
- let system;
635
- const bedrockMessages = [];
636
- for (const message of messages) {
637
- const textContent = extractTextContent(message.content);
638
- if (message.role === "system") {
639
- system = system ? `${system}
640
-
641
- ${textContent}` : textContent;
642
- continue;
643
- }
644
- const content = [];
645
- if (typeof message.content === "string") {
646
- content.push({ text: message.content });
647
- } else {
648
- for (const part of message.content) {
649
- if (part.type === "text") {
650
- content.push({ text: part.text });
651
- continue;
652
- }
653
- const image = await this.imageUrlToBedrockImage(
654
- part.image_url.url,
655
- signal
656
- );
657
- content.push({ image });
658
- }
659
- }
660
- if (message.role === "assistant" && message.tool_calls) {
661
- for (const toolCall of message.tool_calls) {
662
- content.push({
663
- toolUse: {
664
- toolUseId: toolCall.id,
665
- name: toolCall.function.name,
666
- input: this.safeJsonParse(toolCall.function.arguments)
667
- }
668
- });
669
- }
670
- }
671
- if (content.length === 0 && textContent) {
672
- content.push({ text: textContent });
673
- }
674
- bedrockMessages.push({
675
- role: message.role === "assistant" ? "assistant" : "user",
676
- content
677
- });
678
- }
679
- return { system, bedrockMessages };
680
- }
681
- mapConverseResponse(response, modelId) {
682
- const contentBlocks = response.output?.message?.content || [];
683
- const textContent = contentBlocks.filter((block) => typeof block.text === "string").map((block) => block.text).join("");
684
- const toolCalls = contentBlocks.filter((block) => block.toolUse).map((block) => ({
685
- id: block.toolUse.toolUseId,
686
- type: "function",
687
- function: {
688
- name: block.toolUse.name,
689
- arguments: JSON.stringify(block.toolUse.input || {})
690
- }
691
- }));
692
- const usage = response.usage && {
693
- promptTokens: response.usage.inputTokens || 0,
694
- completionTokens: response.usage.outputTokens || 0,
695
- totalTokens: response.usage.totalTokens || 0
696
- };
697
- return {
698
- content: textContent,
699
- model: modelId,
700
- finishReason: this.mapBedrockFinishReason(response.stopReason),
701
- usage,
702
- toolCalls: toolCalls.length > 0 ? toolCalls : void 0
703
- };
704
- }
705
- mapBedrockFinishReason(reason) {
706
- switch (reason) {
707
- case "end_turn":
708
- return "stop";
709
- case "max_tokens":
710
- return "length";
711
- case "stop_sequence":
712
- return "stop";
713
- case "tool_use":
714
- return "tool_calls";
715
- default:
716
- return "stop";
717
- }
718
- }
719
- mapToolConfig(options) {
720
- if (!options.tools || options.tools.length === 0) {
721
- return void 0;
722
- }
723
- if (options.toolChoice === "none") {
724
- return void 0;
725
- }
726
- return {
727
- tools: options.tools.map((tool) => ({
728
- toolSpec: {
729
- name: tool.function.name,
730
- description: tool.function.description || "",
731
- inputSchema: {
732
- json: tool.function.parameters || { type: "object" }
733
- }
734
- }
735
- })),
736
- ...options.toolChoice && {
737
- toolChoice: this.mapToolChoice(options.toolChoice)
738
- }
739
- };
740
- }
741
- mapToolChoice(toolChoice) {
742
- if (!toolChoice || toolChoice === "auto") {
743
- return { auto: {} };
744
- }
745
- if (toolChoice === "none") {
746
- return void 0;
747
- }
748
- return {
749
- tool: {
750
- name: toolChoice.function.name
751
- }
752
- };
753
- }
754
- async parseInvokeModelBody(body) {
755
- const bytes = typeof body?.transformToByteArray === "function" ? await body.transformToByteArray() : body instanceof Uint8Array ? body : Buffer.isBuffer(body) ? body : new Uint8Array(body);
756
- return JSON.parse(new TextDecoder().decode(bytes));
757
- }
758
- extractEmbeddingVector(payload) {
759
- if (Array.isArray(payload.embedding)) {
760
- return payload.embedding;
761
- }
762
- if (Array.isArray(payload.embeddings?.[0])) {
763
- return payload.embeddings[0];
764
- }
765
- if (Array.isArray(payload.embeddingsByType?.float)) {
766
- return payload.embeddingsByType.float;
767
- }
768
- if (Array.isArray(payload.vector)) {
769
- return payload.vector;
770
- }
771
- return void 0;
772
- }
773
- async imageUrlToBedrockImage(imageUrl, signal) {
774
- const { bytes, mimeType } = await this.imageToBytes(imageUrl, signal);
775
- return {
776
- format: this.mimeTypeToBedrockImageFormat(mimeType),
777
- source: { bytes }
778
- };
779
- }
780
- async imageToDataUrl(image, signal) {
781
- if (Buffer.isBuffer(image)) {
782
- return `data:image/png;base64,${image.toString("base64")}`;
783
- }
784
- if (image.startsWith("data:")) {
785
- return image;
786
- }
787
- const { bytes, mimeType } = await this.imageToBytes(image, signal);
788
- return `data:${mimeType};base64,${Buffer.from(bytes).toString("base64")}`;
789
- }
790
- async imageToBase64(image, signal) {
791
- if (Buffer.isBuffer(image)) {
792
- return image.toString("base64");
793
- }
794
- if (image.startsWith("data:")) {
795
- const match = image.match(/^data:([^;]+);base64,(.+)$/);
796
- if (!match) {
797
- throw new AIError(
798
- "Invalid base64 data URL format",
799
- "INVALID_INPUT",
800
- "bedrock"
801
- );
802
- }
803
- return match[2];
804
- }
805
- const { bytes } = await this.imageToBytes(image, signal);
806
- return Buffer.from(bytes).toString("base64");
807
- }
808
- async imageToBytes(image, signal) {
809
- if (image.startsWith("data:")) {
810
- const match = image.match(/^data:([^;]+);base64,(.+)$/);
811
- if (!match) {
812
- throw new AIError(
813
- "Invalid base64 data URL format",
814
- "INVALID_INPUT",
815
- "bedrock"
816
- );
817
- }
818
- return {
819
- bytes: Uint8Array.from(Buffer.from(match[2], "base64")),
820
- mimeType: match[1]
821
- };
822
- }
823
- const response = await fetch(image, { signal });
824
- if (!response.ok) {
825
- throw new AIError(
826
- `Failed to fetch image: ${response.status} ${response.statusText}`,
827
- "IMAGE_FETCH_ERROR",
828
- "bedrock"
829
- );
830
- }
831
- const arrayBuffer = await response.arrayBuffer();
832
- return {
833
- bytes: new Uint8Array(arrayBuffer),
834
- mimeType: response.headers.get("content-type") || "image/png"
835
- };
836
- }
837
- mimeTypeToBedrockImageFormat(mimeType) {
838
- if (mimeType.includes("png")) return "png";
839
- if (mimeType.includes("jpeg") || mimeType.includes("jpg")) return "jpeg";
840
- if (mimeType.includes("gif")) return "gif";
841
- if (mimeType.includes("webp")) return "webp";
842
- throw new AIError(
843
- `Unsupported image format for Bedrock: ${mimeType}`,
844
- "INVALID_INPUT",
845
- "bedrock"
846
- );
847
- }
848
- resolveImageSize(options) {
849
- if (options.size) {
850
- const [width, height] = options.size.split("x").map(Number);
851
- if (Number.isFinite(width) && Number.isFinite(height)) {
852
- return { width, height };
853
- }
854
- }
855
- const aspectRatioSizes = {
856
- "1:1": { width: 1024, height: 1024 },
857
- "2:3": { width: 768, height: 1152 },
858
- "3:2": { width: 1152, height: 768 },
859
- "3:5": { width: 768, height: 1280 },
860
- "5:3": { width: 1280, height: 768 },
861
- "7:9": { width: 896, height: 1152 },
862
- "9:7": { width: 1152, height: 896 },
863
- "6:11": { width: 768, height: 1408 },
864
- "11:6": { width: 1408, height: 768 },
865
- "5:11": { width: 640, height: 1408 },
866
- "11:5": { width: 1408, height: 640 },
867
- "9:5": { width: 1152, height: 640 },
868
- "16:9": { width: 1173, height: 640 }
869
- };
870
- return aspectRatioSizes[options.aspectRatio || ""] || {
871
- width: 1024,
872
- height: 1024
873
- };
874
- }
875
- mapImageQuality(quality) {
876
- if (!quality || quality === "standard") {
877
- return "standard";
878
- }
879
- if (quality === "hd") {
880
- return "premium";
881
- }
882
- return quality;
883
- }
884
- safeJsonParse(input) {
885
- try {
886
- return JSON.parse(input);
887
- } catch {
888
- return { rawArguments: input };
889
- }
890
- }
891
- mapError(error) {
892
- if (error instanceof AIError) {
893
- return error;
894
- }
895
- if (typeof error === "object" && error !== null) {
896
- const awsError = error;
897
- if (awsError.name === "AccessDeniedException") {
898
- return new AuthenticationError("bedrock");
899
- }
900
- if (awsError.name === "ThrottlingException") {
901
- return new RateLimitError("bedrock");
902
- }
903
- if (awsError.name === "ResourceNotFoundException") {
904
- return new ModelNotFoundError(
905
- awsError.message || "Model not found",
906
- "bedrock"
907
- );
908
- }
909
- if (awsError.name === "ValidationException" && awsError.message?.includes("input is too long")) {
910
- return new ContextLengthError("bedrock");
911
- }
912
- }
913
- const errorMessage = error instanceof Error ? error.message : "Unknown Bedrock error occurred";
914
- return new AIError(errorMessage, "UNKNOWN_ERROR", "bedrock");
915
- }
916
- }
917
- export {
918
- BedrockProvider
919
- };
920
- //# sourceMappingURL=bedrock-Dc2eVPUD.js.map