@crewai-ts/core 0.1.12 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/README.md +174 -0
  2. package/dist/agent.d.ts +16 -18
  3. package/dist/auth.cjs +598 -0
  4. package/dist/auth.js +40 -0
  5. package/dist/{chunk-3PVW4JKT.js → chunk-C43UEMCX.js} +6712 -7268
  6. package/dist/chunk-CCOE6MLE.js +896 -0
  7. package/dist/chunk-HFQTF332.js +4455 -0
  8. package/dist/{chunk-BE4JYKSG.js → chunk-MM4ROIFG.js} +12 -1490
  9. package/dist/chunk-RH43TNKN.js +238 -0
  10. package/dist/chunk-S477WFUT.js +565 -0
  11. package/dist/chunk-SB7ADUQA.js +110 -0
  12. package/dist/chunk-T32G6KDW.js +40 -0
  13. package/dist/crew.d.ts +24 -26
  14. package/dist/events.cjs +7513 -0
  15. package/dist/events.js +406 -0
  16. package/dist/experimental-conversational.cjs +272 -0
  17. package/dist/experimental-conversational.js +26 -0
  18. package/dist/feature-hooks.cjs +149 -0
  19. package/dist/feature-hooks.d.ts +94 -0
  20. package/dist/feature-hooks.js +36 -0
  21. package/dist/index.cjs +33923 -64381
  22. package/dist/index.d.ts +2 -15
  23. package/dist/index.js +16720 -49562
  24. package/dist/input-provider.d.ts +3 -4
  25. package/dist/lite-agent.d.ts +4 -4
  26. package/dist/llm.cjs +7467 -0
  27. package/dist/llm.d.ts +0 -4
  28. package/dist/llm.js +225 -0
  29. package/dist/optional-yaml.d.ts +8 -0
  30. package/dist/project.d.ts +1 -1
  31. package/dist/schema-utils.cjs +968 -0
  32. package/dist/schema-utils.d.ts +1 -1
  33. package/dist/schema-utils.js +102 -0
  34. package/dist/state-provider-core.js +3 -2
  35. package/dist/task.d.ts +3 -4
  36. package/dist/tools.cjs +6872 -0
  37. package/dist/tools.d.ts +0 -60
  38. package/dist/tools.js +114 -0
  39. package/dist/types.cjs +68 -0
  40. package/dist/types.js +14 -0
  41. package/package.json +52 -111
  42. package/dist/a2a.d.ts +0 -1684
  43. package/dist/a2ui-schemas.d.ts +0 -3312
  44. package/dist/a2ui.d.ts +0 -379
  45. package/dist/flow-conversation.d.ts +0 -90
  46. package/dist/flow-definition.d.ts +0 -195
  47. package/dist/flow-persistence.d.ts +0 -107
  48. package/dist/flow-visualization.d.ts +0 -77
  49. package/dist/flow.d.ts +0 -927
  50. package/dist/knowledge.d.ts +0 -353
  51. package/dist/mcp-DS7UMYAM.js +0 -62
  52. package/dist/mcp.d.ts +0 -315
  53. package/dist/memory.d.ts +0 -915
  54. package/dist/openai-completion.d.ts +0 -327
  55. package/dist/provider-completions.d.ts +0 -596
  56. package/dist/rag.d.ts +0 -1074
@@ -0,0 +1,4455 @@
1
+ import {
2
+ LLMCallCompletedEvent,
3
+ LLMCallFailedEvent,
4
+ LLMCallStartedEvent,
5
+ LLMCallType,
6
+ LLMStreamChunkEvent,
7
+ LLMThinkingChunkEvent,
8
+ ToolUsageErrorEvent,
9
+ ToolUsageFinishedEvent,
10
+ ToolUsageStartedEvent,
11
+ crewaiEventBus
12
+ } from "./chunk-C43UEMCX.js";
13
+ import {
14
+ LLMCallHookContext,
15
+ runAfterLlmCallHooks,
16
+ runBeforeLlmCallHooks
17
+ } from "./chunk-LWD4QED4.js";
18
+
19
+ // src/llm.ts
20
+ import { AsyncLocalStorage } from "async_hooks";
21
+ import { randomUUID } from "crypto";
22
+ import { readFileSync as readFileSync2 } from "fs";
23
+ import { basename as basename2 } from "path";
24
+
25
+ // src/files.ts
26
+ import { createHash } from "crypto";
27
+ import { readFileSync, statSync } from "fs";
28
+ import { basename, extname, resolve } from "path";
29
+ var ResolvedFile = class {
30
+ contentType;
31
+ content_type;
32
+ constructor(contentType) {
33
+ this.contentType = contentType;
34
+ this.content_type = contentType;
35
+ }
36
+ };
37
+ var InlineBase64 = class extends ResolvedFile {
38
+ data;
39
+ constructor(options) {
40
+ super(options.contentType ?? options.content_type ?? "application/octet-stream");
41
+ this.data = options.data;
42
+ Object.freeze(this);
43
+ }
44
+ };
45
+ var InlineBytes = class extends ResolvedFile {
46
+ data;
47
+ constructor(options) {
48
+ super(options.contentType ?? options.content_type ?? "application/octet-stream");
49
+ this.data = typeof options.data === "string" ? Buffer.from(options.data) : Buffer.from(options.data);
50
+ Object.freeze(this);
51
+ }
52
+ };
53
+ var FileReference = class extends ResolvedFile {
54
+ fileId;
55
+ file_id;
56
+ provider;
57
+ expiresAt;
58
+ expires_at;
59
+ fileUri;
60
+ file_uri;
61
+ constructor(options) {
62
+ super(options.contentType ?? options.content_type ?? "application/octet-stream");
63
+ this.fileId = options.fileId ?? options.file_id ?? "";
64
+ this.file_id = this.fileId;
65
+ this.provider = options.provider;
66
+ this.expiresAt = options.expiresAt ?? options.expires_at ?? null;
67
+ this.expires_at = this.expiresAt;
68
+ this.fileUri = options.fileUri ?? options.file_uri ?? null;
69
+ this.file_uri = this.fileUri;
70
+ Object.freeze(this);
71
+ }
72
+ };
73
+ var UrlReference = class extends ResolvedFile {
74
+ url;
75
+ constructor(options) {
76
+ super(options.contentType ?? options.content_type ?? "application/octet-stream");
77
+ this.url = options.url;
78
+ Object.freeze(this);
79
+ }
80
+ };
81
+ var FileHandling = /* @__PURE__ */ ((FileHandling2) => {
82
+ FileHandling2["STRICT"] = "strict";
83
+ FileHandling2["AUTO"] = "auto";
84
+ FileHandling2["WARN"] = "warn";
85
+ FileHandling2["CHUNK"] = "chunk";
86
+ return FileHandling2;
87
+ })(FileHandling || {});
88
+ var FileValidationError = class extends Error {
89
+ fileName;
90
+ file_name;
91
+ constructor(message, options = {}) {
92
+ super(message);
93
+ this.name = "FileValidationError";
94
+ this.fileName = options.fileName ?? options.file_name ?? null;
95
+ this.file_name = this.fileName;
96
+ }
97
+ };
98
+ var FileTooLargeError = class extends FileValidationError {
99
+ actualSize;
100
+ actual_size;
101
+ maxSize;
102
+ max_size;
103
+ constructor(message, options = {}) {
104
+ super(message, options);
105
+ this.name = "FileTooLargeError";
106
+ this.actualSize = options.actualSize ?? options.actual_size ?? null;
107
+ this.actual_size = this.actualSize;
108
+ this.maxSize = options.maxSize ?? options.max_size ?? null;
109
+ this.max_size = this.maxSize;
110
+ }
111
+ };
112
+ var UnsupportedFileTypeError = class extends FileValidationError {
113
+ contentType;
114
+ content_type;
115
+ constructor(message, options = {}) {
116
+ super(message, options);
117
+ this.name = "UnsupportedFileTypeError";
118
+ this.contentType = options.contentType ?? options.content_type ?? null;
119
+ this.content_type = this.contentType;
120
+ }
121
+ };
122
+ var FileProcessingError = class extends FileValidationError {
123
+ constructor(message, options = {}) {
124
+ super(message, options);
125
+ this.name = "FileProcessingError";
126
+ }
127
+ };
128
+ var ProcessingDependencyError = class extends FileProcessingError {
129
+ dependency;
130
+ installCommand;
131
+ install_command;
132
+ constructor(message, options = {}) {
133
+ super(message);
134
+ this.name = "ProcessingDependencyError";
135
+ this.dependency = options.dependency ?? null;
136
+ this.installCommand = options.installCommand ?? options.install_command ?? null;
137
+ this.install_command = this.installCommand;
138
+ }
139
+ };
140
+ var DEFAULT_IMAGE_FORMATS = ["image/png", "image/jpeg", "image/gif", "image/webp"];
141
+ var GEMINI_IMAGE_FORMATS = [...DEFAULT_IMAGE_FORMATS, "image/heic", "image/heif"];
142
+ var DEFAULT_AUDIO_FORMATS = ["audio/mp3", "audio/mpeg", "audio/wav", "audio/ogg", "audio/flac", "audio/aac", "audio/m4a"];
143
+ var GEMINI_AUDIO_FORMATS = [...DEFAULT_AUDIO_FORMATS, "audio/opus"];
144
+ var DEFAULT_VIDEO_FORMATS = ["video/mp4", "video/mpeg", "video/webm", "video/quicktime"];
145
+ var GEMINI_VIDEO_FORMATS = [...DEFAULT_VIDEO_FORMATS, "video/x-msvideo", "video/x-flv"];
146
+ var DEFAULT_TEXT_FORMATS = ["text/plain", "text/markdown", "text/csv", "application/json", "text/xml", "text/html"];
147
+ var GEMINI_TEXT_FORMATS = [...DEFAULT_TEXT_FORMATS, "application/xml", "application/x-yaml", "text/yaml"];
148
+ var ImageConstraints = class {
149
+ maxSizeBytes;
150
+ max_size_bytes;
151
+ maxWidth;
152
+ max_width;
153
+ maxHeight;
154
+ max_height;
155
+ maxImagesPerRequest;
156
+ max_images_per_request;
157
+ supportedFormats;
158
+ supported_formats;
159
+ constructor(options) {
160
+ this.maxSizeBytes = options.maxSizeBytes ?? options.max_size_bytes ?? 0;
161
+ this.max_size_bytes = this.maxSizeBytes;
162
+ this.maxWidth = options.maxWidth ?? options.max_width ?? null;
163
+ this.max_width = this.maxWidth;
164
+ this.maxHeight = options.maxHeight ?? options.max_height ?? null;
165
+ this.max_height = this.maxHeight;
166
+ this.maxImagesPerRequest = options.maxImagesPerRequest ?? options.max_images_per_request ?? null;
167
+ this.max_images_per_request = this.maxImagesPerRequest;
168
+ this.supportedFormats = options.supportedFormats ?? options.supported_formats ?? DEFAULT_IMAGE_FORMATS;
169
+ this.supported_formats = this.supportedFormats;
170
+ Object.freeze(this);
171
+ }
172
+ };
173
+ var PDFConstraints = class {
174
+ maxSizeBytes;
175
+ max_size_bytes;
176
+ maxPages;
177
+ max_pages;
178
+ constructor(options) {
179
+ this.maxSizeBytes = options.maxSizeBytes ?? options.max_size_bytes ?? 0;
180
+ this.max_size_bytes = this.maxSizeBytes;
181
+ this.maxPages = options.maxPages ?? options.max_pages ?? null;
182
+ this.max_pages = this.maxPages;
183
+ Object.freeze(this);
184
+ }
185
+ };
186
+ var AudioConstraints = class {
187
+ maxSizeBytes;
188
+ max_size_bytes;
189
+ maxDurationSeconds;
190
+ max_duration_seconds;
191
+ supportedFormats;
192
+ supported_formats;
193
+ constructor(options) {
194
+ this.maxSizeBytes = options.maxSizeBytes ?? options.max_size_bytes ?? 0;
195
+ this.max_size_bytes = this.maxSizeBytes;
196
+ this.maxDurationSeconds = options.maxDurationSeconds ?? options.max_duration_seconds ?? null;
197
+ this.max_duration_seconds = this.maxDurationSeconds;
198
+ this.supportedFormats = options.supportedFormats ?? options.supported_formats ?? DEFAULT_AUDIO_FORMATS;
199
+ this.supported_formats = this.supportedFormats;
200
+ Object.freeze(this);
201
+ }
202
+ };
203
+ var VideoConstraints = class {
204
+ maxSizeBytes;
205
+ max_size_bytes;
206
+ maxDurationSeconds;
207
+ max_duration_seconds;
208
+ supportedFormats;
209
+ supported_formats;
210
+ constructor(options) {
211
+ this.maxSizeBytes = options.maxSizeBytes ?? options.max_size_bytes ?? 0;
212
+ this.max_size_bytes = this.maxSizeBytes;
213
+ this.maxDurationSeconds = options.maxDurationSeconds ?? options.max_duration_seconds ?? null;
214
+ this.max_duration_seconds = this.maxDurationSeconds;
215
+ this.supportedFormats = options.supportedFormats ?? options.supported_formats ?? DEFAULT_VIDEO_FORMATS;
216
+ this.supported_formats = this.supportedFormats;
217
+ Object.freeze(this);
218
+ }
219
+ };
220
+ var TextConstraints = class {
221
+ maxSizeBytes;
222
+ max_size_bytes;
223
+ supportedFormats;
224
+ supported_formats;
225
+ constructor(options) {
226
+ this.maxSizeBytes = options.maxSizeBytes ?? options.max_size_bytes ?? 0;
227
+ this.max_size_bytes = this.maxSizeBytes;
228
+ this.supportedFormats = options.supportedFormats ?? options.supported_formats ?? DEFAULT_TEXT_FORMATS;
229
+ this.supported_formats = this.supportedFormats;
230
+ Object.freeze(this);
231
+ }
232
+ };
233
+ var ProviderConstraints = class {
234
+ name;
235
+ image;
236
+ pdf;
237
+ audio;
238
+ video;
239
+ text;
240
+ generalMaxSizeBytes;
241
+ general_max_size_bytes;
242
+ supportsFileUpload;
243
+ supports_file_upload;
244
+ fileUploadThresholdBytes;
245
+ file_upload_threshold_bytes;
246
+ supportsUrlReferences;
247
+ supports_url_references;
248
+ constructor(options) {
249
+ this.name = options.name;
250
+ this.image = options.image ?? null;
251
+ this.pdf = options.pdf ?? null;
252
+ this.audio = options.audio ?? null;
253
+ this.video = options.video ?? null;
254
+ this.text = options.text ?? null;
255
+ this.generalMaxSizeBytes = options.generalMaxSizeBytes ?? options.general_max_size_bytes ?? null;
256
+ this.general_max_size_bytes = this.generalMaxSizeBytes;
257
+ this.supportsFileUpload = options.supportsFileUpload ?? options.supports_file_upload ?? false;
258
+ this.supports_file_upload = this.supportsFileUpload;
259
+ this.fileUploadThresholdBytes = options.fileUploadThresholdBytes ?? options.file_upload_threshold_bytes ?? null;
260
+ this.file_upload_threshold_bytes = this.fileUploadThresholdBytes;
261
+ this.supportsUrlReferences = options.supportsUrlReferences ?? options.supports_url_references ?? false;
262
+ this.supports_url_references = this.supportsUrlReferences;
263
+ Object.freeze(this);
264
+ }
265
+ };
266
+ var ANTHROPIC_CONSTRAINTS = new ProviderConstraints({
267
+ name: "anthropic",
268
+ image: new ImageConstraints({ maxSizeBytes: 5242880, maxWidth: 8e3, maxHeight: 8e3, maxImagesPerRequest: 100 }),
269
+ pdf: new PDFConstraints({ maxSizeBytes: 33554432, maxPages: 100 }),
270
+ supportsFileUpload: true,
271
+ fileUploadThresholdBytes: 5242880,
272
+ supportsUrlReferences: true
273
+ });
274
+ var OPENAI_RESPONSES_CONSTRAINTS = new ProviderConstraints({
275
+ name: "openai_responses",
276
+ image: new ImageConstraints({ maxSizeBytes: 20971520, maxImagesPerRequest: 10 }),
277
+ pdf: new PDFConstraints({ maxSizeBytes: 33554432, maxPages: 100 }),
278
+ audio: new AudioConstraints({ maxSizeBytes: 26214400, maxDurationSeconds: 1500 }),
279
+ supportsFileUpload: true,
280
+ fileUploadThresholdBytes: 5242880,
281
+ supportsUrlReferences: true
282
+ });
283
+ var OPENAI_CONSTRAINTS = new ProviderConstraints({
284
+ name: "openai",
285
+ image: new ImageConstraints({ maxSizeBytes: 20971520, maxImagesPerRequest: 10 }),
286
+ supportsFileUpload: true,
287
+ fileUploadThresholdBytes: 5242880,
288
+ supportsUrlReferences: true
289
+ });
290
+ var GEMINI_CONSTRAINTS = new ProviderConstraints({
291
+ name: "gemini",
292
+ image: new ImageConstraints({ maxSizeBytes: 104857600, supportedFormats: GEMINI_IMAGE_FORMATS }),
293
+ pdf: new PDFConstraints({ maxSizeBytes: 52428800 }),
294
+ audio: new AudioConstraints({ maxSizeBytes: 104857600, maxDurationSeconds: 34200, supportedFormats: GEMINI_AUDIO_FORMATS }),
295
+ video: new VideoConstraints({ maxSizeBytes: 2147483648, maxDurationSeconds: 3600, supportedFormats: GEMINI_VIDEO_FORMATS }),
296
+ text: new TextConstraints({ maxSizeBytes: 104857600, supportedFormats: GEMINI_TEXT_FORMATS }),
297
+ supportsFileUpload: true,
298
+ fileUploadThresholdBytes: 20971520,
299
+ supportsUrlReferences: true
300
+ });
301
+ var BEDROCK_CONSTRAINTS = new ProviderConstraints({
302
+ name: "bedrock",
303
+ image: new ImageConstraints({ maxSizeBytes: 4608e3, maxWidth: 8e3, maxHeight: 8e3 }),
304
+ pdf: new PDFConstraints({ maxSizeBytes: 384e4, maxPages: 100 }),
305
+ supportsUrlReferences: true
306
+ });
307
+ var AZURE_CONSTRAINTS = new ProviderConstraints({
308
+ name: "azure",
309
+ image: new ImageConstraints({ maxSizeBytes: 20971520, maxImagesPerRequest: 10 }),
310
+ audio: new AudioConstraints({ maxSizeBytes: 26214400, maxDurationSeconds: 1500 }),
311
+ supportsUrlReferences: true
312
+ });
313
+ var PROVIDER_CONSTRAINTS = /* @__PURE__ */ new Map([
314
+ ["anthropic", ANTHROPIC_CONSTRAINTS],
315
+ ["openai", OPENAI_CONSTRAINTS],
316
+ ["openai_responses", OPENAI_RESPONSES_CONSTRAINTS],
317
+ ["gemini", GEMINI_CONSTRAINTS],
318
+ ["bedrock", BEDROCK_CONSTRAINTS],
319
+ ["azure", AZURE_CONSTRAINTS],
320
+ ["claude", ANTHROPIC_CONSTRAINTS],
321
+ ["gpt", OPENAI_CONSTRAINTS],
322
+ ["google", GEMINI_CONSTRAINTS],
323
+ ["aws", BEDROCK_CONSTRAINTS]
324
+ ]);
325
+ function getConstraintsForProvider(provider) {
326
+ if (provider instanceof ProviderConstraints) {
327
+ return provider;
328
+ }
329
+ const providerLower = provider.toLowerCase();
330
+ const exact = PROVIDER_CONSTRAINTS.get(providerLower);
331
+ if (exact) {
332
+ return exact;
333
+ }
334
+ for (const [key, constraints] of PROVIDER_CONSTRAINTS) {
335
+ if (providerLower.includes(key)) {
336
+ return constraints;
337
+ }
338
+ }
339
+ return null;
340
+ }
341
+ var get_constraints_for_provider = getConstraintsForProvider;
342
+ function usesOpenAIResponsesApi(provider, api = null) {
343
+ if (api !== "responses") {
344
+ return false;
345
+ }
346
+ const providerLower = provider.toLowerCase();
347
+ return providerLower.includes("openai") || providerLower === "gpt" || providerLower.startsWith("gpt-") || providerLower.includes("/gpt-");
348
+ }
349
+ var uses_openai_responses_api = usesOpenAIResponsesApi;
350
+ function getSupportedContentTypes(provider, api = null) {
351
+ const lookup = usesOpenAIResponsesApi(provider, api) ? "openai_responses" : provider;
352
+ const constraints = getConstraintsForProvider(lookup);
353
+ if (!constraints) {
354
+ return [];
355
+ }
356
+ const types = [];
357
+ if (constraints.image) {
358
+ types.push("image/");
359
+ }
360
+ if (constraints.pdf) {
361
+ types.push("application/pdf");
362
+ }
363
+ if (constraints.audio) {
364
+ types.push("audio/");
365
+ }
366
+ if (constraints.video) {
367
+ types.push("video/");
368
+ }
369
+ if (constraints.text) {
370
+ types.push("text/");
371
+ }
372
+ return types;
373
+ }
374
+ function validateImage(file, constraints, options = {}) {
375
+ const errors = [];
376
+ const raiseOnError = options.raiseOnError ?? options.raise_on_error ?? true;
377
+ const content = file.read();
378
+ validateSize("Image", file.filename, content.length, constraints.maxSizeBytes, errors, raiseOnError);
379
+ validateFormat("Image", file.filename, file.contentType, constraints.supportedFormats, errors, raiseOnError);
380
+ return errors;
381
+ }
382
+ var validate_image = validateImage;
383
+ function validatePDF(file, constraints, options = {}) {
384
+ const errors = [];
385
+ const raiseOnError = options.raiseOnError ?? options.raise_on_error ?? true;
386
+ validateSize("PDF", file.filename, file.read().length, constraints.maxSizeBytes, errors, raiseOnError);
387
+ return errors;
388
+ }
389
+ var validate_pdf = validatePDF;
390
+ function validateAudio(file, constraints, options = {}) {
391
+ const errors = [];
392
+ const raiseOnError = options.raiseOnError ?? options.raise_on_error ?? true;
393
+ const duration = options.durationSeconds ?? options.duration_seconds ?? null;
394
+ validateSize("Audio", file.filename, file.read().length, constraints.maxSizeBytes, errors, raiseOnError);
395
+ validateFormat("Audio", file.filename, file.contentType, constraints.supportedFormats, errors, raiseOnError);
396
+ if (constraints.maxDurationSeconds !== null && duration !== null && duration > constraints.maxDurationSeconds) {
397
+ const message = `Audio '${file.filename ?? "null"}' duration (${duration.toFixed(1)}s) exceeds maximum (${String(constraints.maxDurationSeconds)}s)`;
398
+ errors.push(message);
399
+ if (raiseOnError) {
400
+ throw new FileValidationError(message, { fileName: file.filename });
401
+ }
402
+ }
403
+ return errors;
404
+ }
405
+ var validate_audio = validateAudio;
406
+ function validateVideo(file, constraints, options = {}) {
407
+ const errors = [];
408
+ const raiseOnError = options.raiseOnError ?? options.raise_on_error ?? true;
409
+ const duration = options.durationSeconds ?? options.duration_seconds ?? null;
410
+ validateSize("Video", file.filename, file.read().length, constraints.maxSizeBytes, errors, raiseOnError);
411
+ validateFormat("Video", file.filename, file.contentType, constraints.supportedFormats, errors, raiseOnError);
412
+ if (constraints.maxDurationSeconds !== null && duration !== null && duration > constraints.maxDurationSeconds) {
413
+ const message = `Video '${file.filename ?? "null"}' duration (${duration.toFixed(1)}s) exceeds maximum (${String(constraints.maxDurationSeconds)}s)`;
414
+ errors.push(message);
415
+ if (raiseOnError) {
416
+ throw new FileValidationError(message, { fileName: file.filename });
417
+ }
418
+ }
419
+ return errors;
420
+ }
421
+ var validate_video = validateVideo;
422
+ function validateText(file, constraints, options = {}) {
423
+ const errors = [];
424
+ if (constraints.generalMaxSizeBytes === null) {
425
+ return errors;
426
+ }
427
+ validateSize("Text file", file.filename, file.read().length, constraints.generalMaxSizeBytes, errors, options.raiseOnError ?? options.raise_on_error ?? true);
428
+ return errors;
429
+ }
430
+ var validate_text = validateText;
431
+ function validateFile(file, constraints, options = {}) {
432
+ const raiseOnError = options.raiseOnError ?? options.raise_on_error ?? true;
433
+ if (file instanceof ImageFile) {
434
+ return constraints.image ? validateImage(file, constraints.image, { raiseOnError }) : unsupportedFileType(file, constraints.name, "images", raiseOnError);
435
+ }
436
+ if (file instanceof PDFFile) {
437
+ return constraints.pdf ? validatePDF(file, constraints.pdf, { raiseOnError }) : unsupportedFileType(file, constraints.name, "PDFs", raiseOnError);
438
+ }
439
+ if (file instanceof AudioFile) {
440
+ return constraints.audio ? validateAudio(file, constraints.audio, { raiseOnError }) : unsupportedFileType(file, constraints.name, "audio", raiseOnError);
441
+ }
442
+ if (file instanceof VideoFile) {
443
+ return constraints.video ? validateVideo(file, constraints.video, { raiseOnError }) : unsupportedFileType(file, constraints.name, "video", raiseOnError);
444
+ }
445
+ if (file instanceof TextFile) {
446
+ return validateText(file, constraints, { raiseOnError });
447
+ }
448
+ return [];
449
+ }
450
+ var validate_file = validateFile;
451
+ var FileProcessor = class {
452
+ constraints;
453
+ constructor(options = {}) {
454
+ const value = options instanceof ProviderConstraints || typeof options === "string" || options === null ? options : options.constraints ?? null;
455
+ this.constraints = typeof value === "string" ? getConstraintsForProvider(value) : value;
456
+ }
457
+ validate(file) {
458
+ if (this.constraints === null) {
459
+ return [];
460
+ }
461
+ return validateFile(file, this.constraints, { raiseOnError: this.fileMode(file) === "strict" /* STRICT */ });
462
+ }
463
+ process(file) {
464
+ if (this.constraints === null) {
465
+ return file;
466
+ }
467
+ const mode = this.fileMode(file);
468
+ const errors = this.validate(file);
469
+ if (errors.length === 0) {
470
+ return file;
471
+ }
472
+ if (mode === "strict" /* STRICT */) {
473
+ throw new FileValidationError(errors.join("; "), { fileName: file.filename });
474
+ }
475
+ if (mode === "chunk" /* CHUNK */) {
476
+ return this.chunkProcess(file);
477
+ }
478
+ return file;
479
+ }
480
+ processFiles(files) {
481
+ const result = {};
482
+ for (const [name, file] of Object.entries(files)) {
483
+ const processed = this.process(file);
484
+ if (Array.isArray(processed)) {
485
+ processed.forEach((chunk, index) => {
486
+ result[`${name}_chunk_${String(index)}`] = chunk;
487
+ });
488
+ } else {
489
+ result[name] = processed;
490
+ }
491
+ }
492
+ return result;
493
+ }
494
+ process_files(files) {
495
+ return this.processFiles(files);
496
+ }
497
+ aprocessFiles(files) {
498
+ return Promise.resolve(this.processFiles(files));
499
+ }
500
+ aprocess_files(files) {
501
+ return this.aprocessFiles(files);
502
+ }
503
+ fileMode(file) {
504
+ return Object.values(FileHandling).includes(file.mode) ? file.mode : "auto" /* AUTO */;
505
+ }
506
+ chunkProcess(file) {
507
+ if (this.constraints === null) {
508
+ return file;
509
+ }
510
+ if (file instanceof TextFile && this.constraints.generalMaxSizeBytes !== null) {
511
+ return chunkText(file, this.constraints.generalMaxSizeBytes);
512
+ }
513
+ return file;
514
+ }
515
+ };
516
+ function chunkText(file, maxChars, options = {}) {
517
+ const overlapChars = Math.max(0, options.overlapChars ?? options.overlap_chars ?? 200);
518
+ const splitOnNewlines = options.splitOnNewlines ?? options.split_on_newlines ?? true;
519
+ const text = Buffer.from(file.read()).toString("utf8");
520
+ if (text.length <= maxChars) {
521
+ return [file];
522
+ }
523
+ const filename = file.filename ?? "text.txt";
524
+ const dotIndex = filename.lastIndexOf(".");
525
+ const base = dotIndex >= 0 ? filename.slice(0, dotIndex) : filename;
526
+ const extension = dotIndex >= 0 ? filename.slice(dotIndex + 1) : "txt";
527
+ const chunks = [];
528
+ let start = 0;
529
+ while (start < text.length) {
530
+ let end = Math.min(start + maxChars, text.length);
531
+ if (end < text.length && splitOnNewlines) {
532
+ const lastNewline = text.lastIndexOf("\n", end - 1);
533
+ if (lastNewline > start + Math.floor(maxChars / 2)) {
534
+ end = lastNewline + 1;
535
+ }
536
+ }
537
+ chunks.push(new TextFile({
538
+ source: new FileBytes({
539
+ data: Buffer.from(text.slice(start, end), "utf8"),
540
+ filename: `${base}_chunk_${String(chunks.length)}.${extension}`
541
+ })
542
+ }));
543
+ start = end < text.length ? Math.max(start + 1, end - overlapChars) : text.length;
544
+ }
545
+ return chunks;
546
+ }
547
+ var chunk_text = chunkText;
548
+ function chunkPDF(_file, _maxPages) {
549
+ void _file;
550
+ void _maxPages;
551
+ throw new ProcessingDependencyError("pypdf is required for PDF chunking", { dependency: "pypdf", installCommand: "pip install pypdf" });
552
+ }
553
+ var chunk_pdf = chunkPDF;
554
+ function resizeImage(_file, _maxWidth, _maxHeight) {
555
+ void _file;
556
+ void _maxWidth;
557
+ void _maxHeight;
558
+ throw new ProcessingDependencyError("Pillow is required for image resizing", { dependency: "Pillow", installCommand: "pip install Pillow" });
559
+ }
560
+ var resize_image = resizeImage;
561
+ function optimizeImage(_file, _targetSizeBytes) {
562
+ void _file;
563
+ void _targetSizeBytes;
564
+ throw new ProcessingDependencyError("Pillow is required for image optimization", { dependency: "Pillow", installCommand: "pip install Pillow" });
565
+ }
566
+ var optimize_image = optimizeImage;
567
+ var CachedUpload = class {
568
+ fileId;
569
+ file_id;
570
+ provider;
571
+ fileUri;
572
+ file_uri;
573
+ contentType;
574
+ content_type;
575
+ uploadedAt;
576
+ uploaded_at;
577
+ expiresAt;
578
+ expires_at;
579
+ constructor(options) {
580
+ this.fileId = options.fileId ?? options.file_id ?? "";
581
+ this.file_id = this.fileId;
582
+ this.provider = options.provider;
583
+ this.fileUri = options.fileUri ?? options.file_uri ?? null;
584
+ this.file_uri = this.fileUri;
585
+ this.contentType = options.contentType ?? options.content_type ?? "application/octet-stream";
586
+ this.content_type = this.contentType;
587
+ this.uploadedAt = options.uploadedAt ?? options.uploaded_at ?? /* @__PURE__ */ new Date();
588
+ this.uploaded_at = this.uploadedAt;
589
+ this.expiresAt = options.expiresAt ?? options.expires_at ?? null;
590
+ this.expires_at = this.expiresAt;
591
+ }
592
+ isExpired(now = /* @__PURE__ */ new Date()) {
593
+ return this.expiresAt !== null && now.getTime() >= this.expiresAt.getTime();
594
+ }
595
+ is_expired(now = /* @__PURE__ */ new Date()) {
596
+ return this.isExpired(now);
597
+ }
598
+ };
599
+ var UploadCache = class {
600
+ ttl;
601
+ namespace;
602
+ maxEntries;
603
+ max_entries;
604
+ entries = /* @__PURE__ */ new Map();
605
+ providerKeys = /* @__PURE__ */ new Map();
606
+ accessOrder = [];
607
+ constructor(options = {}) {
608
+ this.ttl = options.ttl ?? 24 * 60 * 60;
609
+ this.namespace = options.namespace ?? "crewai_uploads";
610
+ this.maxEntries = options.maxEntries ?? options.max_entries ?? 1e3;
611
+ this.max_entries = this.maxEntries;
612
+ }
613
+ get size() {
614
+ return this.entries.size;
615
+ }
616
+ get length() {
617
+ return this.size;
618
+ }
619
+ get(file, provider) {
620
+ return this.getByHash(computeFileHash(file), provider);
621
+ }
622
+ get_by_hash(fileHash, provider) {
623
+ return this.getByHash(fileHash, provider);
624
+ }
625
+ getByHash(fileHash, provider) {
626
+ const key = uploadCacheKey(fileHash, provider);
627
+ const cached = this.entries.get(key) ?? null;
628
+ if (cached === null) {
629
+ return null;
630
+ }
631
+ if (cached.isExpired()) {
632
+ this.entries.delete(key);
633
+ this.untrackKey(provider, key);
634
+ return null;
635
+ }
636
+ this.trackKey(provider, key);
637
+ return cached;
638
+ }
639
+ aget(file, provider) {
640
+ return Promise.resolve(this.get(file, provider));
641
+ }
642
+ aget_by_hash(fileHash, provider) {
643
+ return Promise.resolve(this.getByHash(fileHash, provider));
644
+ }
645
+ agetByHash(fileHash, provider) {
646
+ return Promise.resolve(this.getByHash(fileHash, provider));
647
+ }
648
+ set(file, provider, fileIdOrOptions, fileUri = null, expiresAt = null) {
649
+ const fileId = typeof fileIdOrOptions === "string" ? fileIdOrOptions : fileIdOrOptions.fileId ?? fileIdOrOptions.file_id ?? "";
650
+ const resolvedUri = typeof fileIdOrOptions === "string" ? fileUri : fileIdOrOptions.fileUri ?? fileIdOrOptions.file_uri ?? null;
651
+ const resolvedExpiry = typeof fileIdOrOptions === "string" ? expiresAt : fileIdOrOptions.expiresAt ?? fileIdOrOptions.expires_at ?? null;
652
+ return this.setByHash(computeFileHash(file), file.contentType, provider, fileId, resolvedUri, resolvedExpiry);
653
+ }
654
+ set_by_hash(fileHash, contentType, provider, fileId, fileUri = null, expiresAt = null) {
655
+ return this.setByHash(fileHash, contentType, provider, fileId, fileUri, expiresAt);
656
+ }
657
+ setByHash(fileHash, contentType, provider, fileId, fileUri = null, expiresAt = null) {
658
+ this.evictIfNeeded();
659
+ const key = uploadCacheKey(fileHash, provider);
660
+ const cached = new CachedUpload({
661
+ fileId,
662
+ provider,
663
+ fileUri,
664
+ contentType,
665
+ uploadedAt: /* @__PURE__ */ new Date(),
666
+ expiresAt
667
+ });
668
+ this.entries.set(key, cached);
669
+ this.trackKey(provider, key);
670
+ return cached;
671
+ }
672
+ aset(file, provider, fileId, fileUri = null, expiresAt = null) {
673
+ return Promise.resolve(this.set(file, provider, fileId, fileUri, expiresAt));
674
+ }
675
+ aset_by_hash(fileHash, contentType, provider, fileId, fileUri = null, expiresAt = null) {
676
+ return Promise.resolve(this.setByHash(fileHash, contentType, provider, fileId, fileUri, expiresAt));
677
+ }
678
+ asetByHash(fileHash, contentType, provider, fileId, fileUri = null, expiresAt = null) {
679
+ return Promise.resolve(this.setByHash(fileHash, contentType, provider, fileId, fileUri, expiresAt));
680
+ }
681
+ remove(file, provider) {
682
+ const key = uploadCacheKey(computeFileHash(file), provider);
683
+ const removed = this.entries.delete(key);
684
+ if (removed) {
685
+ this.untrackKey(provider, key);
686
+ }
687
+ return removed;
688
+ }
689
+ aremove(file, provider) {
690
+ return Promise.resolve(this.remove(file, provider));
691
+ }
692
+ removeByFileId(fileId, provider) {
693
+ for (const key of [...this.providerKeys.get(provider) ?? []]) {
694
+ if (this.entries.get(key)?.fileId === fileId) {
695
+ this.entries.delete(key);
696
+ this.untrackKey(provider, key);
697
+ return true;
698
+ }
699
+ }
700
+ return false;
701
+ }
702
+ remove_by_file_id(fileId, provider) {
703
+ return this.removeByFileId(fileId, provider);
704
+ }
705
+ aremove_by_file_id(fileId, provider) {
706
+ return Promise.resolve(this.removeByFileId(fileId, provider));
707
+ }
708
+ clearExpired() {
709
+ let removed = 0;
710
+ for (const [key, cached] of [...this.entries]) {
711
+ if (cached.isExpired()) {
712
+ this.entries.delete(key);
713
+ this.untrackKey(cached.provider, key);
714
+ removed += 1;
715
+ }
716
+ }
717
+ return removed;
718
+ }
719
+ clear_expired() {
720
+ return this.clearExpired();
721
+ }
722
+ aclear_expired() {
723
+ return Promise.resolve(this.clearExpired());
724
+ }
725
+ clear() {
726
+ const count = this.entries.size;
727
+ this.entries.clear();
728
+ this.providerKeys.clear();
729
+ this.accessOrder.length = 0;
730
+ return count;
731
+ }
732
+ aclear() {
733
+ return Promise.resolve(this.clear());
734
+ }
735
+ getAllForProvider(provider) {
736
+ return [...this.providerKeys.get(provider) ?? []].map((key) => this.getCachedByKey(key, provider)).filter((cached) => cached !== null);
737
+ }
738
+ get_all_for_provider(provider) {
739
+ return this.getAllForProvider(provider);
740
+ }
741
+ aget_all_for_provider(provider) {
742
+ return Promise.resolve(this.getAllForProvider(provider));
743
+ }
744
+ getProviders() {
745
+ return new Set(this.providerKeys.keys());
746
+ }
747
+ get_providers() {
748
+ return this.getProviders();
749
+ }
750
+ getCachedByKey(key, provider) {
751
+ const cached = this.entries.get(key) ?? null;
752
+ if (cached === null) {
753
+ this.untrackKey(provider, key);
754
+ return null;
755
+ }
756
+ if (cached.isExpired()) {
757
+ this.entries.delete(key);
758
+ this.untrackKey(provider, key);
759
+ return null;
760
+ }
761
+ return cached;
762
+ }
763
+ trackKey(provider, key) {
764
+ const keys = this.providerKeys.get(provider) ?? /* @__PURE__ */ new Set();
765
+ keys.add(key);
766
+ this.providerKeys.set(provider, keys);
767
+ const index = this.accessOrder.indexOf(key);
768
+ if (index >= 0) {
769
+ this.accessOrder.splice(index, 1);
770
+ }
771
+ this.accessOrder.push(key);
772
+ }
773
+ untrackKey(provider, key) {
774
+ this.providerKeys.get(provider)?.delete(key);
775
+ if (this.providerKeys.get(provider)?.size === 0) {
776
+ this.providerKeys.delete(provider);
777
+ }
778
+ const index = this.accessOrder.indexOf(key);
779
+ if (index >= 0) {
780
+ this.accessOrder.splice(index, 1);
781
+ }
782
+ }
783
+ evictIfNeeded() {
784
+ if (this.maxEntries === null || this.entries.size < this.maxEntries) {
785
+ return;
786
+ }
787
+ const count = Math.max(1, Math.floor(this.maxEntries / 10));
788
+ for (const key of this.accessOrder.slice(0, count)) {
789
+ const cached = this.entries.get(key);
790
+ this.entries.delete(key);
791
+ if (cached) {
792
+ this.untrackKey(cached.provider, key);
793
+ }
794
+ }
795
+ }
796
+ };
797
+ var defaultUploadCache = null;
798
+ function getUploadCache(options = {}) {
799
+ defaultUploadCache ??= new UploadCache(options);
800
+ return defaultUploadCache;
801
+ }
802
+ var get_upload_cache = getUploadCache;
803
+ function resetUploadCache() {
804
+ defaultUploadCache?.clear();
805
+ defaultUploadCache = null;
806
+ }
807
+ var reset_upload_cache = resetUploadCache;
808
+ var FileSource = class {
809
+ get content_type() {
810
+ return this.contentType;
811
+ }
812
+ aread() {
813
+ return Promise.resolve(this.read());
814
+ }
815
+ *readChunks(chunkSize = 65536) {
816
+ const data = this.read();
817
+ for (let index = 0; index < data.length; index += chunkSize) {
818
+ yield data.subarray(index, index + chunkSize);
819
+ }
820
+ }
821
+ read_chunks(chunkSize = 65536) {
822
+ return this.readChunks(chunkSize);
823
+ }
824
+ };
825
+ var FilePath = class extends FileSource {
826
+ path;
827
+ maxSizeBytes;
828
+ max_size_bytes;
829
+ contentType;
830
+ content = null;
831
+ constructor(options) {
832
+ super();
833
+ this.path = options.path;
834
+ this.maxSizeBytes = options.maxSizeBytes ?? options.max_size_bytes ?? 100 * 1024 * 1024;
835
+ this.max_size_bytes = this.maxSizeBytes;
836
+ if (this.path.includes("..")) {
837
+ throw new Error(`Path traversal not allowed: ${this.path}`);
838
+ }
839
+ const stats = statSync(this.path, { throwIfNoEntry: false });
840
+ if (!stats) {
841
+ throw new Error(`File not found: ${this.path}`);
842
+ }
843
+ if (!stats.isFile()) {
844
+ throw new Error(`Path is not a file: ${this.path}`);
845
+ }
846
+ if (stats.size > this.maxSizeBytes) {
847
+ throw new Error(`File exceeds max size (${String(stats.size)} > ${String(this.maxSizeBytes)})`);
848
+ }
849
+ this.contentType = detectContentType(readFileSync(this.path).subarray(0, 2048), this.filename);
850
+ }
851
+ get filename() {
852
+ return basename(this.path);
853
+ }
854
+ read() {
855
+ this.content ??= readFileSync(this.path);
856
+ return this.content;
857
+ }
858
+ };
859
+ var FileBytes = class extends FileSource {
860
+ data;
861
+ filename;
862
+ contentType;
863
+ constructor(options) {
864
+ super();
865
+ this.data = typeof options.data === "string" ? Buffer.from(options.data) : Buffer.from(options.data);
866
+ this.filename = options.filename ?? null;
867
+ this.contentType = detectContentType(this.data, this.filename);
868
+ }
869
+ read() {
870
+ return this.data;
871
+ }
872
+ };
873
+ var FileStream = class extends FileSource {
874
+ stream;
875
+ filename;
876
+ contentType;
877
+ content = null;
878
+ constructor(options) {
879
+ super();
880
+ this.stream = options.stream;
881
+ this.filename = options.filename ?? (options.stream.name ? basename(options.stream.name) : null);
882
+ const position = typeof options.stream.tell === "function" ? options.stream.tell() : null;
883
+ options.stream.seek?.(0);
884
+ const header = Buffer.from(options.stream.read(2048));
885
+ if (position !== null) {
886
+ options.stream.seek?.(position);
887
+ }
888
+ this.contentType = detectContentType(header, this.filename);
889
+ }
890
+ read() {
891
+ if (this.content === null) {
892
+ const position = typeof this.stream.tell === "function" ? this.stream.tell() : null;
893
+ this.stream.seek?.(0);
894
+ this.content = Buffer.from(this.stream.read());
895
+ if (position !== null) {
896
+ this.stream.seek?.(position);
897
+ }
898
+ }
899
+ return this.content;
900
+ }
901
+ close() {
902
+ this.stream.close?.();
903
+ }
904
+ };
905
+ var FileUrl = class extends FileSource {
906
+ url;
907
+ filename;
908
+ detectedContentType = null;
909
+ content = null;
910
+ fetcher;
911
+ constructor(options) {
912
+ super();
913
+ if (!options.url.startsWith("http://") && !options.url.startsWith("https://")) {
914
+ throw new Error(`Invalid URL scheme: ${options.url}`);
915
+ }
916
+ this.url = options.url;
917
+ this.filename = options.filename ?? null;
918
+ this.fetcher = options.fetcher ?? null;
919
+ }
920
+ get contentType() {
921
+ this.detectedContentType ??= contentTypeFromFilename(this.filename ?? urlPathname(this.url)) ?? "application/octet-stream";
922
+ return this.detectedContentType;
923
+ }
924
+ read() {
925
+ if (this.content !== null) {
926
+ return this.content;
927
+ }
928
+ if (!this.fetcher) {
929
+ throw new Error("FileUrl.read requires an injected fetcher in this deterministic TypeScript shim. Use aread() with a fetcher or resolve URL-capable providers as UrlReference.");
930
+ }
931
+ const fetched = this.fetcher(this.url);
932
+ if (isPromiseLike(fetched)) {
933
+ throw new Error("FileUrl.read requires a synchronous injected fetcher in this deterministic TypeScript shim. Use aread() with async fetchers.");
934
+ }
935
+ return this.applyFetchedContent(fetched);
936
+ }
937
+ async aread() {
938
+ if (this.content !== null) {
939
+ return this.content;
940
+ }
941
+ if (!this.fetcher) {
942
+ throw new Error("FileUrl.aread requires an injected fetcher in this deterministic TypeScript shim.");
943
+ }
944
+ const fetched = await this.fetcher(this.url);
945
+ return this.applyFetchedContent(fetched);
946
+ }
947
+ applyFetchedContent(fetched) {
948
+ this.content = typeof fetched.content === "string" ? Buffer.from(fetched.content) : Buffer.from(fetched.content);
949
+ if (fetched.contentType) {
950
+ this.detectedContentType = fetched.contentType.split(";")[0] ?? fetched.contentType;
951
+ }
952
+ return this.content;
953
+ }
954
+ };
955
+ var BaseFile = class {
956
+ source;
957
+ mode;
958
+ constructor(options) {
959
+ const source = isFileOptions(options) ? options.source : options;
960
+ this.source = coerceFileSource(source);
961
+ this.mode = isFileOptions(options) ? options.mode ?? "auto" : "auto";
962
+ }
963
+ get filename() {
964
+ return this.source.filename;
965
+ }
966
+ get contentType() {
967
+ return this.source.contentType;
968
+ }
969
+ get content_type() {
970
+ return this.contentType;
971
+ }
972
+ read() {
973
+ return this.source.read();
974
+ }
975
+ async aread() {
976
+ return await this.source.aread();
977
+ }
978
+ readText(encoding = "utf8") {
979
+ return Buffer.from(this.read()).toString(encoding);
980
+ }
981
+ read_text(encoding = "utf8") {
982
+ return this.readText(encoding);
983
+ }
984
+ keys() {
985
+ return [this.unpackKey()];
986
+ }
987
+ getItem(key) {
988
+ if (key !== this.unpackKey()) {
989
+ throw new Error(key);
990
+ }
991
+ return this;
992
+ }
993
+ __getitem__(key) {
994
+ return this.getItem(key);
995
+ }
996
+ unpackKey() {
997
+ const name = this.filename;
998
+ return name ? name.replace(/\.[^.]*$/, "") : "file";
999
+ }
1000
+ };
1001
+ var ImageFile = class extends BaseFile {
1002
+ };
1003
+ var PDFFile = class extends BaseFile {
1004
+ };
1005
+ var TextFile = class extends BaseFile {
1006
+ };
1007
+ var AudioFile = class extends BaseFile {
1008
+ };
1009
+ var VideoFile = class extends BaseFile {
1010
+ };
1011
+ var File = class extends BaseFile {
1012
+ };
1013
+ function detectContentType(data, filename) {
1014
+ const bytes = typeof data === "string" ? Buffer.from(data) : Buffer.from(data);
1015
+ if (bytes.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))) {
1016
+ return "image/png";
1017
+ }
1018
+ if (bytes[0] === 255 && bytes[1] === 216 && bytes[2] === 255) {
1019
+ return "image/jpeg";
1020
+ }
1021
+ if (bytes.subarray(0, 4).toString("ascii") === "%PDF") {
1022
+ return "application/pdf";
1023
+ }
1024
+ const byExtension = contentTypeFromFilename(filename);
1025
+ if (byExtension && byExtension !== "application/octet-stream") {
1026
+ return byExtension;
1027
+ }
1028
+ const text = bytes.toString("utf8").trim();
1029
+ if (text) {
1030
+ try {
1031
+ JSON.parse(text);
1032
+ return "application/json";
1033
+ } catch {
1034
+ }
1035
+ }
1036
+ return isLikelyText(bytes) ? "text/plain" : "application/octet-stream";
1037
+ }
1038
+ var detect_content_type = detectContentType;
1039
+ function wrapFileSource(source) {
1040
+ if (source.contentType.startsWith("image/")) {
1041
+ return new ImageFile({ source });
1042
+ }
1043
+ if (source.contentType.startsWith("audio/")) {
1044
+ return new AudioFile({ source });
1045
+ }
1046
+ if (source.contentType.startsWith("video/")) {
1047
+ return new VideoFile({ source });
1048
+ }
1049
+ if (source.contentType === "application/pdf") {
1050
+ return new PDFFile({ source });
1051
+ }
1052
+ return new TextFile({ source });
1053
+ }
1054
+ var wrap_file_source = wrapFileSource;
1055
+ function normalizeInputFiles(inputFiles) {
1056
+ const result = {};
1057
+ inputFiles.forEach((item, index) => {
1058
+ if (item instanceof BaseFile) {
1059
+ const filename2 = item.filename ?? `file_${String(index)}`;
1060
+ result[filename2.replace(/\.[^.]*$/, "")] = item;
1061
+ return;
1062
+ }
1063
+ const source = coerceFileSource(item);
1064
+ const filename = source.filename ?? `file_${String(index)}`;
1065
+ result[filename] = wrapFileSource(source);
1066
+ });
1067
+ return result;
1068
+ }
1069
+ var normalize_input_files = normalizeInputFiles;
1070
+ var FileResolverConfig = class {
1071
+ preferUpload;
1072
+ prefer_upload;
1073
+ uploadThresholdBytes;
1074
+ upload_threshold_bytes;
1075
+ useBytesForBedrock;
1076
+ use_bytes_for_bedrock;
1077
+ constructor(options = {}) {
1078
+ this.preferUpload = options.preferUpload ?? options.prefer_upload ?? false;
1079
+ this.prefer_upload = this.preferUpload;
1080
+ this.uploadThresholdBytes = options.uploadThresholdBytes ?? options.upload_threshold_bytes ?? null;
1081
+ this.upload_threshold_bytes = this.uploadThresholdBytes;
1082
+ this.useBytesForBedrock = options.useBytesForBedrock ?? options.use_bytes_for_bedrock ?? true;
1083
+ this.use_bytes_for_bedrock = this.useBytesForBedrock;
1084
+ }
1085
+ };
1086
+ var FileResolver = class {
1087
+ config;
1088
+ uploadCache;
1089
+ upload_cache;
1090
+ constructor(options = {}) {
1091
+ this.config = options.config instanceof FileResolverConfig ? options.config : new FileResolverConfig(options.config);
1092
+ this.uploadCache = options.uploadCache ?? options.upload_cache ?? null;
1093
+ this.upload_cache = this.uploadCache;
1094
+ }
1095
+ resolve(file, provider) {
1096
+ const source = file.source;
1097
+ if (source instanceof FileUrl && supportsUrlReferences(provider)) {
1098
+ return new UrlReference({ contentType: file.contentType, url: source.url });
1099
+ }
1100
+ const content = file.read();
1101
+ const cached = this.resolveCachedUpload(file, provider, content);
1102
+ if (cached) {
1103
+ return cached;
1104
+ }
1105
+ if (provider.toLowerCase() === "bedrock" && this.config.useBytesForBedrock) {
1106
+ return new InlineBytes({ contentType: file.contentType, data: content });
1107
+ }
1108
+ return new InlineBase64({ contentType: file.contentType, data: Buffer.from(content).toString("base64") });
1109
+ }
1110
+ async aresolve(file, provider) {
1111
+ const source = file.source;
1112
+ if (source instanceof FileUrl && supportsUrlReferences(provider)) {
1113
+ return new UrlReference({ contentType: file.contentType, url: source.url });
1114
+ }
1115
+ const content = await file.aread();
1116
+ const cached = this.resolveCachedUpload(file, provider, content);
1117
+ if (cached) {
1118
+ return cached;
1119
+ }
1120
+ if (provider.toLowerCase() === "bedrock" && this.config.useBytesForBedrock) {
1121
+ return new InlineBytes({ contentType: file.contentType, data: content });
1122
+ }
1123
+ return new InlineBase64({ contentType: file.contentType, data: Buffer.from(content).toString("base64") });
1124
+ }
1125
+ resolveFiles(files, provider) {
1126
+ return Object.fromEntries(Object.entries(files).map(([name, file]) => [name, this.resolve(file, provider)]));
1127
+ }
1128
+ resolve_files(files, provider) {
1129
+ return this.resolveFiles(files, provider);
1130
+ }
1131
+ async aresolveFiles(files, provider, maxConcurrency = 10) {
1132
+ const entries = Object.entries(files);
1133
+ const output = {};
1134
+ let nextIndex = 0;
1135
+ const workerCount = Math.max(1, Math.min(maxConcurrency, entries.length));
1136
+ await Promise.all(Array.from({ length: workerCount }, async () => {
1137
+ while (nextIndex < entries.length) {
1138
+ const entry = entries[nextIndex];
1139
+ nextIndex += 1;
1140
+ if (!entry) {
1141
+ continue;
1142
+ }
1143
+ const [name, file] = entry;
1144
+ try {
1145
+ output[name] = await this.aresolve(file, provider);
1146
+ } catch {
1147
+ }
1148
+ }
1149
+ }));
1150
+ return output;
1151
+ }
1152
+ async aresolve_files(files, provider, max_concurrency = 10) {
1153
+ return await this.aresolveFiles(files, provider, max_concurrency);
1154
+ }
1155
+ getCachedUploads(provider) {
1156
+ return this.uploadCache?.getAllForProvider(provider) ?? [];
1157
+ }
1158
+ get_cached_uploads(provider) {
1159
+ return this.getCachedUploads(provider);
1160
+ }
1161
+ clearCache() {
1162
+ this.uploadCache?.clear();
1163
+ }
1164
+ clear_cache() {
1165
+ this.clearCache();
1166
+ }
1167
+ resolveCachedUpload(file, provider, content) {
1168
+ if (!this.uploadCache || !this.shouldUseUpload(file, provider, content.length)) {
1169
+ return null;
1170
+ }
1171
+ const cached = this.uploadCache.getByHash(createHash("sha256").update(content).digest("hex"), provider);
1172
+ if (!cached) {
1173
+ return null;
1174
+ }
1175
+ return new FileReference({
1176
+ contentType: cached.contentType,
1177
+ fileId: cached.fileId,
1178
+ provider: cached.provider,
1179
+ expiresAt: cached.expiresAt,
1180
+ fileUri: cached.fileUri
1181
+ });
1182
+ }
1183
+ shouldUseUpload(file, provider, sizeBytes) {
1184
+ const constraints = getConstraintsForProvider(provider);
1185
+ if (!constraints?.supportsFileUpload) {
1186
+ return false;
1187
+ }
1188
+ if (this.config.preferUpload) {
1189
+ return true;
1190
+ }
1191
+ const typeConstraint = getFileTypeConstraint(file, constraints);
1192
+ if (typeConstraint && sizeBytes > typeConstraint.maxSizeBytes) {
1193
+ return true;
1194
+ }
1195
+ return this.config.uploadThresholdBytes !== null && sizeBytes > this.config.uploadThresholdBytes;
1196
+ }
1197
+ };
1198
+ function createResolver(options = {}) {
1199
+ const uploadThresholdBytes = options instanceof FileResolverConfig ? null : options.uploadThresholdBytes ?? options.upload_threshold_bytes ?? (options.provider ? getConstraintsForProvider(options.provider)?.fileUploadThresholdBytes : void 0);
1200
+ const config = options instanceof FileResolverConfig ? options : new FileResolverConfig({
1201
+ ...options,
1202
+ ...uploadThresholdBytes === void 0 ? {} : { uploadThresholdBytes }
1203
+ });
1204
+ const enableCache = options instanceof FileResolverConfig ? true : options.enableCache ?? options.enable_cache ?? true;
1205
+ return new FileResolver({ config, uploadCache: enableCache ? new UploadCache() : null });
1206
+ }
1207
+ var create_resolver = createResolver;
1208
+ function formatMultimodalContent(files, provider, options = {}) {
1209
+ const api = typeof options === "string" ? options : options.api ?? "completions";
1210
+ const text = typeof options === "string" ? null : options.text ?? null;
1211
+ const normalizedProvider = provider.toLowerCase();
1212
+ const result = [];
1213
+ if (text) {
1214
+ result.push(formatTextBlockForProvider(text, normalizedProvider, api));
1215
+ }
1216
+ for (const [name, file] of Object.entries(files)) {
1217
+ if (!isOpenAIProvider(normalizedProvider) && !isGeminiProvider(normalizedProvider) && !isAnthropicProvider(normalizedProvider) && !isBedrockProvider(normalizedProvider)) {
1218
+ continue;
1219
+ }
1220
+ const bytes = Buffer.from(file.read());
1221
+ const encoded = bytes.toString("base64");
1222
+ if (isAnthropicProvider(normalizedProvider) && file instanceof ImageFile) {
1223
+ result.push({
1224
+ type: "image",
1225
+ source: {
1226
+ type: "base64",
1227
+ media_type: file.contentType,
1228
+ data: encoded
1229
+ }
1230
+ });
1231
+ } else if (isAnthropicProvider(normalizedProvider) && file instanceof PDFFile) {
1232
+ result.push({
1233
+ type: "document",
1234
+ source: {
1235
+ type: "base64",
1236
+ media_type: file.contentType,
1237
+ data: encoded
1238
+ }
1239
+ });
1240
+ } else if (isBedrockProvider(normalizedProvider) && file instanceof ImageFile) {
1241
+ result.push({
1242
+ image: {
1243
+ format: bedrockImageFormat(file.contentType, file.filename),
1244
+ source: { bytes }
1245
+ }
1246
+ });
1247
+ } else if (isBedrockProvider(normalizedProvider) && file instanceof PDFFile) {
1248
+ result.push({
1249
+ document: {
1250
+ name: bedrockDocumentName(file.filename ?? name),
1251
+ format: "pdf",
1252
+ source: { bytes }
1253
+ }
1254
+ });
1255
+ } else if (isGeminiProvider(normalizedProvider) && file instanceof ImageFile) {
1256
+ result.push({
1257
+ inlineData: {
1258
+ mimeType: file.contentType,
1259
+ data: encoded
1260
+ }
1261
+ });
1262
+ } else if (isGeminiProvider(normalizedProvider) && (file instanceof TextFile || file instanceof AudioFile || file instanceof VideoFile || file instanceof PDFFile)) {
1263
+ result.push({
1264
+ inlineData: {
1265
+ mimeType: file.contentType,
1266
+ data: encoded
1267
+ }
1268
+ });
1269
+ } else if (file instanceof PDFFile && api === "responses") {
1270
+ result.push({
1271
+ type: "input_file",
1272
+ filename: file.filename ?? `${name}.pdf`,
1273
+ file_data: `data:application/pdf;base64,${encoded}`
1274
+ });
1275
+ } else if (file instanceof ImageFile) {
1276
+ result.push({
1277
+ type: api === "responses" ? "input_image" : "image_url",
1278
+ image_url: { url: `data:${file.contentType};base64,${encoded}` }
1279
+ });
1280
+ }
1281
+ }
1282
+ return result;
1283
+ }
1284
+ function format_multimodal_content(files, provider, options = {}) {
1285
+ return formatMultimodalContent(files, provider, options);
1286
+ }
1287
+ function isFileOptions(value) {
1288
+ return Boolean(value && typeof value === "object" && "source" in value);
1289
+ }
1290
+ function coerceFileSource(value) {
1291
+ if (value instanceof FileSource) {
1292
+ return value;
1293
+ }
1294
+ if (typeof value === "string") {
1295
+ if (value.startsWith("http://") || value.startsWith("https://")) {
1296
+ return new FileUrl({ url: value });
1297
+ }
1298
+ return new FilePath({ path: resolve(value) });
1299
+ }
1300
+ if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
1301
+ return new FileBytes({ data: value });
1302
+ }
1303
+ return new FileStream({ stream: value });
1304
+ }
1305
+ function supportsUrlReferences(provider) {
1306
+ if (["bedrock", "aws"].includes(provider.toLowerCase())) {
1307
+ return false;
1308
+ }
1309
+ return getConstraintsForProvider(provider)?.supportsUrlReferences ?? false;
1310
+ }
1311
+ function isOpenAIProvider(provider) {
1312
+ return provider === "openai" || provider === "azure" || provider.startsWith("openai/") || provider.startsWith("azure/") || provider.startsWith("gpt-") || provider.startsWith("gpt-35-") || provider.startsWith("o1") || provider.startsWith("o3") || provider.startsWith("o4");
1313
+ }
1314
+ function isGeminiProvider(provider) {
1315
+ return provider === "gemini" || provider === "google" || provider.startsWith("gemini/") || provider.startsWith("google/");
1316
+ }
1317
+ function isAnthropicProvider(provider) {
1318
+ return provider === "anthropic" || provider === "claude" || provider.startsWith("anthropic/") || provider.startsWith("claude");
1319
+ }
1320
+ function isBedrockProvider(provider) {
1321
+ return provider === "bedrock" || provider === "aws" || provider.startsWith("bedrock/") || provider.startsWith("aws/");
1322
+ }
1323
+ function formatTextBlockForProvider(text, provider, api) {
1324
+ if (api === "responses") {
1325
+ return { type: "input_text", text };
1326
+ }
1327
+ if (isGeminiProvider(provider) || isBedrockProvider(provider)) {
1328
+ return { text };
1329
+ }
1330
+ return { type: "text", text };
1331
+ }
1332
+ function bedrockImageFormat(contentType, filename) {
1333
+ const mediaType = contentType.split("/").at(-1)?.toLowerCase();
1334
+ if (mediaType === "jpg") {
1335
+ return "jpeg";
1336
+ }
1337
+ if (mediaType) {
1338
+ return mediaType;
1339
+ }
1340
+ const extension = filename?.split(".").at(-1)?.toLowerCase();
1341
+ return extension === "jpg" ? "jpeg" : extension || "png";
1342
+ }
1343
+ function bedrockDocumentName(filename) {
1344
+ const name = filename.replace(/\.[^.]+$/u, "");
1345
+ const sanitized = name.replace(/[^\p{L}\p{N}\s\-()[\]]/gu, " ").replace(/\s+/gu, " ").trim();
1346
+ return sanitized || "document";
1347
+ }
1348
+ function isPromiseLike(value) {
1349
+ return (typeof value === "object" || typeof value === "function") && value !== null && "then" in value && typeof value.then === "function";
1350
+ }
1351
+ function validateSize(fileType, filename, fileSize, maxSize, errors, raiseOnError) {
1352
+ if (fileSize <= maxSize) {
1353
+ return;
1354
+ }
1355
+ const message = `${fileType} '${filename ?? "null"}' size (${formatSize(fileSize)}) exceeds maximum (${formatSize(maxSize)})`;
1356
+ errors.push(message);
1357
+ if (raiseOnError) {
1358
+ throw new FileTooLargeError(message, { fileName: filename, actualSize: fileSize, maxSize });
1359
+ }
1360
+ }
1361
+ function validateFormat(fileType, filename, contentType, supportedFormats, errors, raiseOnError) {
1362
+ if (supportedFormats.includes(contentType)) {
1363
+ return;
1364
+ }
1365
+ const message = `${fileType} format '${contentType}' is not supported. Supported: ${supportedFormats.join(", ")}`;
1366
+ errors.push(message);
1367
+ if (raiseOnError) {
1368
+ throw new UnsupportedFileTypeError(message, { fileName: filename, contentType });
1369
+ }
1370
+ }
1371
+ function unsupportedFileType(file, providerName, typeName, raiseOnError) {
1372
+ const message = `Provider '${providerName}' does not support ${typeName}`;
1373
+ if (raiseOnError) {
1374
+ throw new UnsupportedFileTypeError(message, { fileName: file.filename, contentType: file.contentType });
1375
+ }
1376
+ return [message];
1377
+ }
1378
+ function getFileTypeConstraint(file, constraints) {
1379
+ if (file instanceof ImageFile) {
1380
+ return constraints.image;
1381
+ }
1382
+ if (file instanceof PDFFile) {
1383
+ return constraints.pdf;
1384
+ }
1385
+ if (file instanceof AudioFile) {
1386
+ return constraints.audio;
1387
+ }
1388
+ if (file instanceof VideoFile) {
1389
+ return constraints.video;
1390
+ }
1391
+ if (file instanceof TextFile) {
1392
+ return constraints.text;
1393
+ }
1394
+ return null;
1395
+ }
1396
+ function formatSize(sizeBytes) {
1397
+ if (sizeBytes >= 1024 * 1024 * 1024) {
1398
+ return `${(sizeBytes / (1024 * 1024 * 1024)).toFixed(1)}GB`;
1399
+ }
1400
+ if (sizeBytes >= 1024 * 1024) {
1401
+ return `${(sizeBytes / (1024 * 1024)).toFixed(1)}MB`;
1402
+ }
1403
+ if (sizeBytes >= 1024) {
1404
+ return `${(sizeBytes / 1024).toFixed(1)}KB`;
1405
+ }
1406
+ return `${String(sizeBytes)}B`;
1407
+ }
1408
+ function computeFileHash(file) {
1409
+ return createHash("sha256").update(file.read()).digest("hex");
1410
+ }
1411
+ function uploadCacheKey(fileHash, provider) {
1412
+ return `upload:${provider}:${fileHash}`;
1413
+ }
1414
+ function contentTypeFromFilename(filename) {
1415
+ switch (extname((filename ?? "").split("?")[0] ?? "").toLowerCase()) {
1416
+ case ".txt":
1417
+ case ".log":
1418
+ return "text/plain";
1419
+ case ".md":
1420
+ case ".markdown":
1421
+ return "text/markdown";
1422
+ case ".csv":
1423
+ return "text/csv";
1424
+ case ".json":
1425
+ return "application/json";
1426
+ case ".xml":
1427
+ return "application/xml";
1428
+ case ".yaml":
1429
+ case ".yml":
1430
+ return "application/x-yaml";
1431
+ case ".html":
1432
+ case ".htm":
1433
+ return "text/html";
1434
+ case ".png":
1435
+ return "image/png";
1436
+ case ".jpg":
1437
+ case ".jpeg":
1438
+ return "image/jpeg";
1439
+ case ".gif":
1440
+ return "image/gif";
1441
+ case ".webp":
1442
+ return "image/webp";
1443
+ case ".pdf":
1444
+ return "application/pdf";
1445
+ case ".wav":
1446
+ return "audio/wav";
1447
+ case ".mp3":
1448
+ return "audio/mpeg";
1449
+ case ".mp4":
1450
+ return "video/mp4";
1451
+ default:
1452
+ return null;
1453
+ }
1454
+ }
1455
+ function urlPathname(url) {
1456
+ try {
1457
+ return new URL(url).pathname;
1458
+ } catch {
1459
+ return url;
1460
+ }
1461
+ }
1462
+ function isLikelyText(bytes) {
1463
+ if (bytes.length === 0) {
1464
+ return true;
1465
+ }
1466
+ let printable = 0;
1467
+ for (const byte of bytes) {
1468
+ if (byte === 9 || byte === 10 || byte === 13 || byte >= 32 && byte <= 126) {
1469
+ printable += 1;
1470
+ }
1471
+ }
1472
+ return printable / bytes.length > 0.9;
1473
+ }
1474
+
1475
+ // src/llm.ts
1476
+ var JsonResponseFormat = Object.freeze({ kind: "JsonResponseFormat" });
1477
+ var CACHE_BREAKPOINT_KEY = "cache_breakpoint";
1478
+ var UsageMetrics = class UsageMetricsValue {
1479
+ totalTokens;
1480
+ total_tokens;
1481
+ promptTokens;
1482
+ prompt_tokens;
1483
+ cachedPromptTokens;
1484
+ cached_prompt_tokens;
1485
+ completionTokens;
1486
+ completion_tokens;
1487
+ reasoningTokens;
1488
+ reasoning_tokens;
1489
+ cacheCreationTokens;
1490
+ cache_creation_tokens;
1491
+ successfulRequests;
1492
+ successful_requests;
1493
+ constructor(options = {}) {
1494
+ this.totalTokens = options.totalTokens ?? options.total_tokens ?? 0;
1495
+ this.total_tokens = this.totalTokens;
1496
+ this.promptTokens = options.promptTokens ?? options.prompt_tokens ?? 0;
1497
+ this.prompt_tokens = this.promptTokens;
1498
+ this.cachedPromptTokens = options.cachedPromptTokens ?? options.cached_prompt_tokens ?? 0;
1499
+ this.cached_prompt_tokens = this.cachedPromptTokens;
1500
+ this.completionTokens = options.completionTokens ?? options.completion_tokens ?? 0;
1501
+ this.completion_tokens = this.completionTokens;
1502
+ this.reasoningTokens = options.reasoningTokens ?? options.reasoning_tokens ?? 0;
1503
+ this.reasoning_tokens = this.reasoningTokens;
1504
+ this.cacheCreationTokens = options.cacheCreationTokens ?? options.cache_creation_tokens ?? 0;
1505
+ this.cache_creation_tokens = this.cacheCreationTokens;
1506
+ this.successfulRequests = options.successfulRequests ?? options.successful_requests ?? 0;
1507
+ this.successful_requests = this.successfulRequests;
1508
+ defineUsageMetricAliases(this);
1509
+ }
1510
+ addUsageMetrics(usageMetrics) {
1511
+ const next = addUsageMetrics(this, normalizeUsageMetrics(usageMetrics));
1512
+ this.assign(next);
1513
+ }
1514
+ add_usage_metrics(usageMetrics) {
1515
+ this.addUsageMetrics(usageMetrics);
1516
+ }
1517
+ assign(metrics) {
1518
+ this.totalTokens = metrics.totalTokens;
1519
+ this.total_tokens = metrics.totalTokens;
1520
+ this.promptTokens = metrics.promptTokens;
1521
+ this.prompt_tokens = metrics.promptTokens;
1522
+ this.cachedPromptTokens = metrics.cachedPromptTokens;
1523
+ this.cached_prompt_tokens = metrics.cachedPromptTokens;
1524
+ this.completionTokens = metrics.completionTokens;
1525
+ this.completion_tokens = metrics.completionTokens;
1526
+ this.reasoningTokens = metrics.reasoningTokens;
1527
+ this.reasoning_tokens = metrics.reasoningTokens;
1528
+ this.cacheCreationTokens = metrics.cacheCreationTokens;
1529
+ this.cache_creation_tokens = metrics.cacheCreationTokens;
1530
+ this.successfulRequests = metrics.successfulRequests;
1531
+ this.successful_requests = metrics.successfulRequests;
1532
+ }
1533
+ modelDump() {
1534
+ return {
1535
+ total_tokens: this.totalTokens,
1536
+ prompt_tokens: this.promptTokens,
1537
+ cached_prompt_tokens: this.cachedPromptTokens,
1538
+ completion_tokens: this.completionTokens,
1539
+ reasoning_tokens: this.reasoningTokens,
1540
+ cache_creation_tokens: this.cacheCreationTokens,
1541
+ successful_requests: this.successfulRequests
1542
+ };
1543
+ }
1544
+ model_dump() {
1545
+ return this.modelDump();
1546
+ }
1547
+ };
1548
+ var LocalFileUploader = class {
1549
+ provider;
1550
+ options;
1551
+ uploads = [];
1552
+ constructor(provider, options = {}) {
1553
+ this.provider = provider;
1554
+ this.options = { ...options };
1555
+ }
1556
+ upload(name, file) {
1557
+ const rendered = renderLLMInputFile(name, file);
1558
+ const upload = {
1559
+ id: `${this.provider}-file-${String(this.uploads.length + 1)}`,
1560
+ provider: this.provider,
1561
+ name,
1562
+ filename: rendered.filename,
1563
+ contentType: rendered.contentType,
1564
+ content: rendered.content,
1565
+ size: rendered.content.length
1566
+ };
1567
+ this.uploads.push(upload);
1568
+ return upload;
1569
+ }
1570
+ upload_file(name, file) {
1571
+ return this.upload(name, file);
1572
+ }
1573
+ };
1574
+ var DEFAULT_CONTEXT_WINDOW_SIZE = 4096;
1575
+ var DEFAULT_SUPPORTS_STOP_WORDS = true;
1576
+ var DEFAULT_LLM_MODEL = "gpt-4.1-mini";
1577
+ var JSON_URL = "https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json";
1578
+ var LITELLM_PARAMS = ["api_key", "api_base", "api_version"];
1579
+ var UNACCEPTED_LLM_ENV_ATTRIBUTES = [
1580
+ "AWS_ACCESS_KEY_ID",
1581
+ "AWS_SECRET_ACCESS_KEY",
1582
+ "AWS_DEFAULT_REGION"
1583
+ ];
1584
+ var LLM_ENV_VARS = {
1585
+ openai: [{ key_name: "OPENAI_API_KEY" }],
1586
+ anthropic: [{ key_name: "ANTHROPIC_API_KEY" }],
1587
+ gemini: [{ key_name: "GEMINI_API_KEY" }],
1588
+ nvidia_nim: [{ key_name: "NVIDIA_NIM_API_KEY" }, { key_name: "NVIDIA_API_KEY" }],
1589
+ groq: [{ key_name: "GROQ_API_KEY" }],
1590
+ huggingface: [{ key_name: "HF_TOKEN" }],
1591
+ sambanova: [{ key_name: "SAMBANOVA_API_KEY" }],
1592
+ watson: [
1593
+ { key_name: "WATSONX_URL" },
1594
+ { key_name: "WATSONX_APIKEY" },
1595
+ { key_name: "WATSONX_PROJECT_ID" }
1596
+ ],
1597
+ ollama: [{ default: true, API_BASE: "http://localhost:11434" }],
1598
+ bedrock: [
1599
+ { key_name: "AWS_ACCESS_KEY_ID" },
1600
+ { key_name: "AWS_SECRET_ACCESS_KEY" },
1601
+ { key_name: "AWS_DEFAULT_REGION" }
1602
+ ],
1603
+ azure: [
1604
+ { key_name: "model" },
1605
+ { key_name: "AZURE_API_KEY" },
1606
+ { key_name: "AZURE_API_BASE" },
1607
+ { key_name: "AZURE_API_VERSION" }
1608
+ ],
1609
+ cerebras: [
1610
+ { key_name: "model" },
1611
+ { key_name: "CEREBRAS_API_KEY" }
1612
+ ]
1613
+ };
1614
+ var ENV_VARS = LLM_ENV_VARS;
1615
+ var SUPPORTED_NATIVE_PROVIDERS = ["openai", "anthropic", "azure", "bedrock", "gemini", "snowflake"];
1616
+ var LLM_PROVIDER_ALIASES = {
1617
+ openai: "openai",
1618
+ anthropic: "anthropic",
1619
+ claude: "anthropic",
1620
+ azure: "azure",
1621
+ azure_openai: "azure",
1622
+ google: "gemini",
1623
+ gemini: "gemini",
1624
+ bedrock: "bedrock",
1625
+ aws: "bedrock",
1626
+ openrouter: "openrouter",
1627
+ deepseek: "deepseek",
1628
+ ollama: "ollama",
1629
+ ollama_chat: "ollama_chat",
1630
+ hosted_vllm: "hosted_vllm",
1631
+ cerebras: "cerebras",
1632
+ dashscope: "dashscope"
1633
+ };
1634
+ var PROVIDERS = Object.freeze([
1635
+ "openai",
1636
+ "anthropic",
1637
+ "gemini",
1638
+ "nvidia_nim",
1639
+ "groq",
1640
+ "huggingface",
1641
+ "ollama",
1642
+ "watson",
1643
+ "bedrock",
1644
+ "azure",
1645
+ "cerebras",
1646
+ "sambanova"
1647
+ ]);
1648
+ var OPENAI_MODELS = [
1649
+ "gpt-3.5-turbo",
1650
+ "gpt-3.5-turbo-0125",
1651
+ "gpt-3.5-turbo-0301",
1652
+ "gpt-3.5-turbo-0613",
1653
+ "gpt-3.5-turbo-1106",
1654
+ "gpt-3.5-turbo-16k",
1655
+ "gpt-3.5-turbo-16k-0613",
1656
+ "gpt-3.5-turbo-instruct",
1657
+ "gpt-3.5-turbo-instruct-0914",
1658
+ "gpt-4",
1659
+ "gpt-4-0125-preview",
1660
+ "gpt-4-0314",
1661
+ "gpt-4-0613",
1662
+ "gpt-4-1106-preview",
1663
+ "gpt-4-32k",
1664
+ "gpt-4-32k-0314",
1665
+ "gpt-4-32k-0613",
1666
+ "gpt-4-turbo",
1667
+ "gpt-4-turbo-2024-04-09",
1668
+ "gpt-4-turbo-preview",
1669
+ "gpt-4-vision-preview",
1670
+ "gpt-4.1",
1671
+ "gpt-4.1-2025-04-14",
1672
+ "gpt-4.1-mini",
1673
+ "gpt-4.1-mini-2025-04-14",
1674
+ "gpt-4.1-nano",
1675
+ "gpt-4.1-nano-2025-04-14",
1676
+ "gpt-4o",
1677
+ "gpt-4o-2024-05-13",
1678
+ "gpt-4o-2024-08-06",
1679
+ "gpt-4o-2024-11-20",
1680
+ "gpt-4o-audio-preview",
1681
+ "gpt-4o-audio-preview-2024-10-01",
1682
+ "gpt-4o-audio-preview-2024-12-17",
1683
+ "gpt-4o-audio-preview-2025-06-03",
1684
+ "gpt-4o-mini",
1685
+ "gpt-4o-mini-2024-07-18",
1686
+ "gpt-4o-mini-audio-preview",
1687
+ "gpt-4o-mini-audio-preview-2024-12-17",
1688
+ "gpt-4o-mini-realtime-preview",
1689
+ "gpt-4o-mini-realtime-preview-2024-12-17",
1690
+ "gpt-4o-mini-search-preview",
1691
+ "gpt-4o-mini-search-preview-2025-03-11",
1692
+ "gpt-4o-mini-transcribe",
1693
+ "gpt-4o-mini-tts",
1694
+ "gpt-4o-realtime-preview",
1695
+ "gpt-4o-realtime-preview-2024-10-01",
1696
+ "gpt-4o-realtime-preview-2024-12-17",
1697
+ "gpt-4o-realtime-preview-2025-06-03",
1698
+ "gpt-4o-search-preview",
1699
+ "gpt-4o-search-preview-2025-03-11",
1700
+ "gpt-4o-transcribe",
1701
+ "gpt-4o-transcribe-diarize",
1702
+ "gpt-5",
1703
+ "gpt-5-2025-08-07",
1704
+ "gpt-5-chat",
1705
+ "gpt-5-chat-latest",
1706
+ "gpt-5-codex",
1707
+ "gpt-5-mini",
1708
+ "gpt-5-mini-2025-08-07",
1709
+ "gpt-5-nano",
1710
+ "gpt-5-nano-2025-08-07",
1711
+ "gpt-5-pro",
1712
+ "gpt-5-pro-2025-10-06",
1713
+ "gpt-5-search-api",
1714
+ "gpt-5-search-api-2025-10-14",
1715
+ "gpt-audio",
1716
+ "gpt-audio-2025-08-28",
1717
+ "gpt-audio-mini",
1718
+ "gpt-audio-mini-2025-10-06",
1719
+ "gpt-image-1",
1720
+ "gpt-image-1-mini",
1721
+ "gpt-realtime",
1722
+ "gpt-realtime-2025-08-28",
1723
+ "gpt-realtime-mini",
1724
+ "gpt-realtime-mini-2025-10-06",
1725
+ "o1",
1726
+ "o1-preview",
1727
+ "o1-2024-12-17",
1728
+ "o1-mini",
1729
+ "o1-mini-2024-09-12",
1730
+ "o1-pro",
1731
+ "o1-pro-2025-03-19",
1732
+ "o3-mini",
1733
+ "o3",
1734
+ "o4-mini",
1735
+ "whisper-1"
1736
+ ];
1737
+ var ANTHROPIC_MODELS = [
1738
+ "claude-opus-4-5-20251101",
1739
+ "claude-opus-4-5",
1740
+ "claude-3-7-sonnet-latest",
1741
+ "claude-3-7-sonnet-20250219",
1742
+ "claude-3-5-haiku-latest",
1743
+ "claude-3-5-haiku-20241022",
1744
+ "claude-haiku-4-5",
1745
+ "claude-haiku-4-5-20251001",
1746
+ "claude-sonnet-4-20250514",
1747
+ "claude-sonnet-4-0",
1748
+ "claude-4-sonnet-20250514",
1749
+ "claude-sonnet-4-5",
1750
+ "claude-sonnet-4-5-20250929",
1751
+ "claude-3-5-sonnet-latest",
1752
+ "claude-3-5-sonnet-20241022",
1753
+ "claude-3-5-sonnet-20240620",
1754
+ "claude-opus-4-0",
1755
+ "claude-opus-4-20250514",
1756
+ "claude-4-opus-20250514",
1757
+ "claude-opus-4-1",
1758
+ "claude-opus-4-1-20250805",
1759
+ "claude-3-opus-latest",
1760
+ "claude-3-opus-20240229",
1761
+ "claude-3-sonnet-20240229",
1762
+ "claude-3-haiku-latest",
1763
+ "claude-3-haiku-20240307"
1764
+ ];
1765
+ var AZURE_MODELS = [
1766
+ "gpt-3.5-turbo",
1767
+ "gpt-3.5-turbo-0301",
1768
+ "gpt-3.5-turbo-0613",
1769
+ "gpt-3.5-turbo-16k",
1770
+ "gpt-3.5-turbo-16k-0613",
1771
+ "gpt-35-turbo",
1772
+ "gpt-35-turbo-0125",
1773
+ "gpt-35-turbo-1106",
1774
+ "gpt-35-turbo-16k-0613",
1775
+ "gpt-35-turbo-instruct-0914",
1776
+ "gpt-4",
1777
+ "gpt-4-0314",
1778
+ "gpt-4-0613",
1779
+ "gpt-4-1106-preview",
1780
+ "gpt-4-0125-preview",
1781
+ "gpt-4-32k",
1782
+ "gpt-4-32k-0314",
1783
+ "gpt-4-32k-0613",
1784
+ "gpt-4-turbo",
1785
+ "gpt-4-turbo-2024-04-09",
1786
+ "gpt-4-vision",
1787
+ "gpt-4o",
1788
+ "gpt-4o-2024-05-13",
1789
+ "gpt-4o-2024-08-06",
1790
+ "gpt-4o-2024-11-20",
1791
+ "gpt-4o-mini",
1792
+ "gpt-5",
1793
+ "o1",
1794
+ "o1-mini",
1795
+ "o1-preview",
1796
+ "o3-mini",
1797
+ "o3",
1798
+ "o4-mini"
1799
+ ];
1800
+ var BEDROCK_MODELS = [
1801
+ "us.anthropic.claude-sonnet-4-5-20250929-v1:0",
1802
+ "us.anthropic.claude-sonnet-4-20250514-v1:0",
1803
+ "us.anthropic.claude-opus-4-5-20251101-v1:0",
1804
+ "us.anthropic.claude-opus-4-20250514-v1:0",
1805
+ "us.anthropic.claude-opus-4-1-20250805-v1:0",
1806
+ "us.anthropic.claude-haiku-4-5-20251001-v1:0",
1807
+ "us.anthropic.claude-sonnet-4-6",
1808
+ "us.anthropic.claude-opus-4-6-v1",
1809
+ "us.anthropic.claude-sonnet-4-5-v1:0",
1810
+ "us.anthropic.claude-opus-4-5-v1:0",
1811
+ "us.anthropic.claude-opus-4-6-v1:0",
1812
+ "us.anthropic.claude-haiku-4-5-v1:0",
1813
+ "eu.anthropic.claude-sonnet-4-5-v1:0",
1814
+ "eu.anthropic.claude-opus-4-5-v1:0",
1815
+ "eu.anthropic.claude-haiku-4-5-v1:0",
1816
+ "apac.anthropic.claude-sonnet-4-5-v1:0",
1817
+ "apac.anthropic.claude-opus-4-5-v1:0",
1818
+ "apac.anthropic.claude-haiku-4-5-v1:0",
1819
+ "global.anthropic.claude-sonnet-4-5-20250929-v1:0",
1820
+ "global.anthropic.claude-sonnet-4-20250514-v1:0",
1821
+ "global.anthropic.claude-opus-4-5-20251101-v1:0",
1822
+ "global.anthropic.claude-opus-4-6-v1",
1823
+ "global.anthropic.claude-haiku-4-5-20251001-v1:0",
1824
+ "global.anthropic.claude-sonnet-4-6",
1825
+ "ai21.jamba-1-5-large-v1:0",
1826
+ "ai21.jamba-1-5-mini-v1:0",
1827
+ "amazon.nova-lite-v1:0",
1828
+ "amazon.nova-lite-v1:0:24k",
1829
+ "amazon.nova-lite-v1:0:300k",
1830
+ "amazon.nova-micro-v1:0",
1831
+ "amazon.nova-micro-v1:0:128k",
1832
+ "amazon.nova-micro-v1:0:24k",
1833
+ "amazon.nova-premier-v1:0",
1834
+ "amazon.nova-premier-v1:0:1000k",
1835
+ "amazon.nova-premier-v1:0:20k",
1836
+ "amazon.nova-premier-v1:0:8k",
1837
+ "amazon.nova-premier-v1:0:mm",
1838
+ "amazon.nova-pro-v1:0",
1839
+ "amazon.nova-pro-v1:0:24k",
1840
+ "amazon.nova-pro-v1:0:300k",
1841
+ "amazon.titan-text-express-v1",
1842
+ "amazon.titan-text-express-v1:0:8k",
1843
+ "amazon.titan-text-lite-v1",
1844
+ "amazon.titan-text-lite-v1:0:4k",
1845
+ "amazon.titan-tg1-large",
1846
+ "anthropic.claude-3-5-haiku-20241022-v1:0",
1847
+ "anthropic.claude-3-5-sonnet-20240620-v1:0",
1848
+ "anthropic.claude-3-5-sonnet-20241022-v2:0",
1849
+ "anthropic.claude-3-7-sonnet-20250219-v1:0",
1850
+ "anthropic.claude-3-haiku-20240307-v1:0",
1851
+ "anthropic.claude-3-haiku-20240307-v1:0:200k",
1852
+ "anthropic.claude-3-haiku-20240307-v1:0:48k",
1853
+ "anthropic.claude-3-opus-20240229-v1:0",
1854
+ "anthropic.claude-3-opus-20240229-v1:0:12k",
1855
+ "anthropic.claude-3-opus-20240229-v1:0:200k",
1856
+ "anthropic.claude-3-opus-20240229-v1:0:28k",
1857
+ "anthropic.claude-3-sonnet-20240229-v1:0",
1858
+ "anthropic.claude-3-sonnet-20240229-v1:0:200k",
1859
+ "anthropic.claude-3-sonnet-20240229-v1:0:28k",
1860
+ "anthropic.claude-haiku-4-5-20251001-v1:0",
1861
+ "anthropic.claude-instant-v1:2:100k",
1862
+ "anthropic.claude-opus-4-5-20251101-v1:0",
1863
+ "anthropic.claude-opus-4-1-20250805-v1:0",
1864
+ "anthropic.claude-opus-4-20250514-v1:0",
1865
+ "anthropic.claude-sonnet-4-20250514-v1:0",
1866
+ "anthropic.claude-sonnet-4-5-20250929-v1:0",
1867
+ "anthropic.claude-v2:0:100k",
1868
+ "anthropic.claude-v2:0:18k",
1869
+ "anthropic.claude-v2:1:18k",
1870
+ "anthropic.claude-v2:1:200k",
1871
+ "cohere.command-r-plus-v1:0",
1872
+ "cohere.command-r-v1:0",
1873
+ "cohere.rerank-v3-5:0",
1874
+ "deepseek.r1-v1:0",
1875
+ "meta.llama3-1-70b-instruct-v1:0",
1876
+ "meta.llama3-1-8b-instruct-v1:0",
1877
+ "meta.llama3-2-11b-instruct-v1:0",
1878
+ "meta.llama3-2-1b-instruct-v1:0",
1879
+ "meta.llama3-2-3b-instruct-v1:0",
1880
+ "meta.llama3-2-90b-instruct-v1:0",
1881
+ "meta.llama3-3-70b-instruct-v1:0",
1882
+ "meta.llama3-70b-instruct-v1:0",
1883
+ "meta.llama3-8b-instruct-v1:0",
1884
+ "meta.llama4-maverick-17b-instruct-v1:0",
1885
+ "meta.llama4-scout-17b-instruct-v1:0",
1886
+ "mistral.mistral-7b-instruct-v0:2",
1887
+ "mistral.mistral-large-2402-v1:0",
1888
+ "mistral.mistral-small-2402-v1:0",
1889
+ "mistral.mixtral-8x7b-instruct-v0:1",
1890
+ "mistral.pixtral-large-2502-v1:0",
1891
+ "openai.gpt-oss-120b-1:0",
1892
+ "openai.gpt-oss-20b-1:0",
1893
+ "qwen.qwen3-32b-v1:0",
1894
+ "qwen.qwen3-coder-30b-a3b-v1:0",
1895
+ "twelvelabs.pegasus-1-2-v1:0"
1896
+ ];
1897
+ var NVIDIA_NIM_MODELS = [
1898
+ "nvidia_nim/nvidia/mistral-nemo-minitron-8b-8k-instruct",
1899
+ "nvidia_nim/nvidia/nemotron-4-mini-hindi-4b-instruct",
1900
+ "nvidia_nim/nvidia/llama-3.1-nemotron-70b-instruct",
1901
+ "nvidia_nim/nvidia/llama3-chatqa-1.5-8b",
1902
+ "nvidia_nim/nvidia/llama3-chatqa-1.5-70b",
1903
+ "nvidia_nim/nvidia/vila",
1904
+ "nvidia_nim/nvidia/neva-22",
1905
+ "nvidia_nim/nvidia/nemotron-mini-4b-instruct",
1906
+ "nvidia_nim/nvidia/usdcode-llama3-70b-instruct",
1907
+ "nvidia_nim/nvidia/nemotron-4-340b-instruct",
1908
+ "nvidia_nim/nvidia/nvidia-nemotron-3-ultra-550b-a55b",
1909
+ "nvidia_nim/meta/codellama-70b",
1910
+ "nvidia_nim/meta/llama2-70b",
1911
+ "nvidia_nim/meta/llama3-8b-instruct",
1912
+ "nvidia_nim/meta/llama3-70b-instruct",
1913
+ "nvidia_nim/meta/llama-3.1-8b-instruct",
1914
+ "nvidia_nim/meta/llama-3.1-70b-instruct",
1915
+ "nvidia_nim/meta/llama-3.1-405b-instruct",
1916
+ "nvidia_nim/meta/llama-3.2-1b-instruct",
1917
+ "nvidia_nim/meta/llama-3.2-3b-instruct",
1918
+ "nvidia_nim/meta/llama-3.2-11b-vision-instruct",
1919
+ "nvidia_nim/meta/llama-3.2-90b-vision-instruct",
1920
+ "nvidia_nim/meta/llama-3.1-70b-instruct",
1921
+ "nvidia_nim/google/gemma-7b",
1922
+ "nvidia_nim/google/gemma-2b",
1923
+ "nvidia_nim/google/codegemma-7b",
1924
+ "nvidia_nim/google/codegemma-1.1-7b",
1925
+ "nvidia_nim/google/recurrentgemma-2b",
1926
+ "nvidia_nim/google/gemma-2-9b-it",
1927
+ "nvidia_nim/google/gemma-2-27b-it",
1928
+ "nvidia_nim/google/gemma-2-2b-it",
1929
+ "nvidia_nim/google/deplot",
1930
+ "nvidia_nim/google/paligemma",
1931
+ "nvidia_nim/mistralai/mistral-7b-instruct-v0.2",
1932
+ "nvidia_nim/mistralai/mixtral-8x7b-instruct-v0.1",
1933
+ "nvidia_nim/mistralai/mistral-large",
1934
+ "nvidia_nim/mistralai/mixtral-8x22b-instruct-v0.1",
1935
+ "nvidia_nim/mistralai/mistral-7b-instruct-v0.3",
1936
+ "nvidia_nim/nv-mistralai/mistral-nemo-12b-instruct",
1937
+ "nvidia_nim/mistralai/mamba-codestral-7b-v0.1",
1938
+ "nvidia_nim/microsoft/phi-3-mini-128k-instruct",
1939
+ "nvidia_nim/microsoft/phi-3-mini-4k-instruct",
1940
+ "nvidia_nim/microsoft/phi-3-small-8k-instruct",
1941
+ "nvidia_nim/microsoft/phi-3-small-128k-instruct",
1942
+ "nvidia_nim/microsoft/phi-3-medium-4k-instruct",
1943
+ "nvidia_nim/microsoft/phi-3-medium-128k-instruct",
1944
+ "nvidia_nim/microsoft/phi-3.5-mini-instruct",
1945
+ "nvidia_nim/microsoft/phi-3.5-moe-instruct",
1946
+ "nvidia_nim/microsoft/kosmos-2",
1947
+ "nvidia_nim/microsoft/phi-3-vision-128k-instruct",
1948
+ "nvidia_nim/microsoft/phi-3.5-vision-instruct",
1949
+ "nvidia_nim/databricks/dbrx-instruct",
1950
+ "nvidia_nim/snowflake/arctic",
1951
+ "nvidia_nim/aisingapore/sea-lion-7b-instruct",
1952
+ "nvidia_nim/ibm/granite-8b-code-instruct",
1953
+ "nvidia_nim/ibm/granite-34b-code-instruct",
1954
+ "nvidia_nim/ibm/granite-3.0-8b-instruct",
1955
+ "nvidia_nim/ibm/granite-3.0-3b-a800m-instruct",
1956
+ "nvidia_nim/mediatek/breeze-7b-instruct",
1957
+ "nvidia_nim/upstage/solar-10.7b-instruct",
1958
+ "nvidia_nim/writer/palmyra-med-70b-32k",
1959
+ "nvidia_nim/writer/palmyra-med-70b",
1960
+ "nvidia_nim/writer/palmyra-fin-70b-32k",
1961
+ "nvidia_nim/01-ai/yi-large",
1962
+ "nvidia_nim/deepseek-ai/deepseek-coder-6.7b-instruct",
1963
+ "nvidia_nim/rakuten/rakutenai-7b-instruct",
1964
+ "nvidia_nim/rakuten/rakutenai-7b-chat",
1965
+ "nvidia_nim/baichuan-inc/baichuan2-13b-chat"
1966
+ ];
1967
+ var GROQ_MODELS = [
1968
+ "groq/llama-3.1-8b-instant",
1969
+ "groq/llama-3.1-70b-versatile",
1970
+ "groq/llama-3.1-405b-reasoning",
1971
+ "groq/gemma2-9b-it",
1972
+ "groq/gemma-7b-it"
1973
+ ];
1974
+ var OLLAMA_MODELS = [
1975
+ "ollama/llama3.1",
1976
+ "ollama/mixtral"
1977
+ ];
1978
+ var WATSON_MODELS = [
1979
+ "watsonx/meta-llama/llama-3-1-70b-instruct",
1980
+ "watsonx/meta-llama/llama-3-1-8b-instruct",
1981
+ "watsonx/meta-llama/llama-3-2-11b-vision-instruct",
1982
+ "watsonx/meta-llama/llama-3-2-1b-instruct",
1983
+ "watsonx/meta-llama/llama-3-2-90b-vision-instruct",
1984
+ "watsonx/meta-llama/llama-3-405b-instruct",
1985
+ "watsonx/mistral/mistral-large",
1986
+ "watsonx/ibm/granite-3-8b-instruct"
1987
+ ];
1988
+ var HUGGINGFACE_MODELS = [
1989
+ "huggingface/meta-llama/Meta-Llama-3.1-8B-Instruct",
1990
+ "huggingface/mistralai/Mixtral-8x7B-Instruct-v0.1",
1991
+ "huggingface/tiiuae/falcon-180B-chat",
1992
+ "huggingface/google/gemma-7b-it"
1993
+ ];
1994
+ var SAMBANOVA_MODELS = [
1995
+ "sambanova/Meta-Llama-3.3-70B-Instruct",
1996
+ "sambanova/QwQ-32B-Preview",
1997
+ "sambanova/Qwen2.5-72B-Instruct",
1998
+ "sambanova/Qwen2.5-Coder-32B-Instruct",
1999
+ "sambanova/Meta-Llama-3.1-405B-Instruct",
2000
+ "sambanova/Meta-Llama-3.1-70B-Instruct",
2001
+ "sambanova/Meta-Llama-3.1-8B-Instruct",
2002
+ "sambanova/Llama-3.2-90B-Vision-Instruct",
2003
+ "sambanova/Llama-3.2-11B-Vision-Instruct",
2004
+ "sambanova/Meta-Llama-3.2-3B-Instruct",
2005
+ "sambanova/Meta-Llama-3.2-1B-Instruct"
2006
+ ];
2007
+ var OpenAIModels = OPENAI_MODELS;
2008
+ var AnthropicModels = ANTHROPIC_MODELS;
2009
+ var AzureModels = AZURE_MODELS;
2010
+ var BedrockModels = BEDROCK_MODELS;
2011
+ var NvidiaNimModels = NVIDIA_NIM_MODELS;
2012
+ var GroqModels = GROQ_MODELS;
2013
+ var OllamaModels = OLLAMA_MODELS;
2014
+ var WatsonModels = WATSON_MODELS;
2015
+ var HuggingFaceModels = HUGGINGFACE_MODELS;
2016
+ var SambanovaModels = SAMBANOVA_MODELS;
2017
+ var MODELS = Object.freeze({
2018
+ openai: OPENAI_MODELS,
2019
+ anthropic: ANTHROPIC_MODELS,
2020
+ nvidia_nim: NVIDIA_NIM_MODELS,
2021
+ groq: GROQ_MODELS,
2022
+ ollama: OLLAMA_MODELS,
2023
+ watson: WATSON_MODELS,
2024
+ azure: AZURE_MODELS,
2025
+ bedrock: BEDROCK_MODELS,
2026
+ huggingface: HUGGINGFACE_MODELS,
2027
+ sambanova: SAMBANOVA_MODELS
2028
+ });
2029
+ var CONTEXT_WINDOW_USAGE_RATIO = 0.85;
2030
+ var MIN_CONTEXT_WINDOW_SIZE = 1024;
2031
+ var MAX_CONTEXT_WINDOW_SIZE = 2097152;
2032
+ var MIN_CONTEXT = MIN_CONTEXT_WINDOW_SIZE;
2033
+ var MAX_CONTEXT = MAX_CONTEXT_WINDOW_SIZE;
2034
+ var ANTHROPIC_PREFIXES = Object.freeze(["anthropic/", "claude-", "claude/"]);
2035
+ var Delta = Object.freeze({ kind: "Delta" });
2036
+ var StreamingChoices = Object.freeze({ kind: "StreamingChoices" });
2037
+ var FunctionArgs = class {
2038
+ name;
2039
+ arguments;
2040
+ constructor(options = {}) {
2041
+ this.name = options.name ?? "";
2042
+ this.arguments = options.arguments ?? "";
2043
+ }
2044
+ };
2045
+ var AccumulatedToolArgs = class _AccumulatedToolArgs {
2046
+ id;
2047
+ index;
2048
+ type;
2049
+ function;
2050
+ constructor(options = {}) {
2051
+ this.id = options.id ?? null;
2052
+ this.index = options.index ?? 0;
2053
+ this.type = options.type ?? "function";
2054
+ this.function = options.function instanceof FunctionArgs ? options.function : new FunctionArgs(options.function);
2055
+ }
2056
+ accumulate(delta) {
2057
+ const record = readLLMRecord(delta);
2058
+ const deltaIndex = numberValue(record.index);
2059
+ if (deltaIndex !== null) {
2060
+ this.index = deltaIndex;
2061
+ }
2062
+ if (typeof record.id === "string" && !this.id) {
2063
+ this.id = record.id;
2064
+ }
2065
+ if (typeof record.type === "string") {
2066
+ this.type = record.type;
2067
+ }
2068
+ const functionDelta = readLLMRecord(record.function);
2069
+ if (typeof functionDelta.name === "string" && functionDelta.name.length > 0) {
2070
+ this.function.name = functionDelta.name;
2071
+ }
2072
+ if (typeof functionDelta.arguments === "string" && functionDelta.arguments.length > 0) {
2073
+ this.function.arguments += functionDelta.arguments;
2074
+ }
2075
+ return this;
2076
+ }
2077
+ toToolCall() {
2078
+ return {
2079
+ id: this.id,
2080
+ type: this.type,
2081
+ index: this.index,
2082
+ function: {
2083
+ name: this.function.name,
2084
+ arguments: this.function.arguments
2085
+ }
2086
+ };
2087
+ }
2088
+ to_tool_call() {
2089
+ return this.toToolCall();
2090
+ }
2091
+ static toToolCalls(toolCalls) {
2092
+ return Object.entries(toolCalls).map(([key, value]) => {
2093
+ const accumulator = value instanceof _AccumulatedToolArgs ? value : new _AccumulatedToolArgs(value);
2094
+ if (accumulator.index === 0) {
2095
+ const keyIndex = Number(key);
2096
+ if (Number.isInteger(keyIndex)) {
2097
+ accumulator.index = keyIndex;
2098
+ }
2099
+ }
2100
+ return accumulator;
2101
+ }).sort((left, right) => left.index - right.index).map((entry) => entry.toToolCall());
2102
+ }
2103
+ static to_tool_calls(toolCalls) {
2104
+ return _AccumulatedToolArgs.toToolCalls(toolCalls);
2105
+ }
2106
+ static fromStreamingChunks(chunks) {
2107
+ const accumulators = {};
2108
+ for (const chunk of chunks) {
2109
+ for (const toolCall of extractStreamingToolCallDeltas(chunk)) {
2110
+ const index = numberValue(readLLMRecord(toolCall).index) ?? 0;
2111
+ accumulators[index] ??= new _AccumulatedToolArgs({ index });
2112
+ accumulators[index].accumulate(toolCall);
2113
+ }
2114
+ }
2115
+ return _AccumulatedToolArgs.toToolCalls(accumulators);
2116
+ }
2117
+ static from_streaming_chunks(chunks) {
2118
+ return _AccumulatedToolArgs.fromStreamingChunks(chunks);
2119
+ }
2120
+ };
2121
+ function extractStreamingToolCallDeltas(chunk) {
2122
+ const record = readLLMRecord(chunk);
2123
+ const direct = readLLMRecord(record.delta).tool_calls ?? record.tool_calls;
2124
+ if (Array.isArray(direct)) {
2125
+ return direct;
2126
+ }
2127
+ const choices = Array.isArray(record.choices) ? record.choices : [];
2128
+ const deltas = [];
2129
+ for (const choice of choices) {
2130
+ const toolCalls = readLLMRecord(readLLMRecord(choice).delta).tool_calls;
2131
+ if (Array.isArray(toolCalls)) {
2132
+ for (const toolCall of toolCalls) {
2133
+ deltas.push(toolCall);
2134
+ }
2135
+ }
2136
+ }
2137
+ return deltas;
2138
+ }
2139
+ function readLLMRecord(value) {
2140
+ return value && typeof value === "object" && !Array.isArray(value) ? value : {};
2141
+ }
2142
+ function numberValue(value) {
2143
+ return typeof value === "number" && Number.isFinite(value) ? value : null;
2144
+ }
2145
+ var LLM_CONTEXT_WINDOW_SIZES = {
2146
+ "gpt-4": 8192,
2147
+ "gpt-4o": 128e3,
2148
+ "gpt-4o-mini": 2e5,
2149
+ "gpt-4-turbo": 128e3,
2150
+ "gpt-4.1": 1047576,
2151
+ "gpt-4.1-mini-2025-04-14": 1047576,
2152
+ "gpt-4.1-nano-2025-04-14": 1047576,
2153
+ "o1-preview": 128e3,
2154
+ "o1-mini": 128e3,
2155
+ "o3-mini": 2e5,
2156
+ "o4-mini": 2e5,
2157
+ "deepseek-chat": 128e3,
2158
+ "llama-3.1-70b-versatile": 131072,
2159
+ "llama-3.1-8b-instant": 131072,
2160
+ "llama-3.3-70b-versatile": 128e3,
2161
+ "mixtral-8x7b-32768": 32768,
2162
+ "anthropic.claude-v2": 1e5,
2163
+ "anthropic.claude-v2:1": 2e5,
2164
+ "anthropic.claude-instant-v1": 1e5,
2165
+ "anthropic.claude-3-haiku-20240307-v1:0": 2e5,
2166
+ "anthropic.claude-3-sonnet-20240229-v1:0": 2e5,
2167
+ "anthropic.claude-3-opus-20240229-v1:0": 2e5,
2168
+ "anthropic.claude-3-5-sonnet-20240620-v1:0": 2e5,
2169
+ "anthropic.claude-3-5-haiku-20241022-v1:0": 2e5,
2170
+ "anthropic.claude-3-7-sonnet-20250219-v1:0": 2e5,
2171
+ "anthropic.claude-sonnet-4-20250514-v1:0": 2e5,
2172
+ "anthropic.claude-opus-4-20250514-v1:0": 2e5,
2173
+ "anthropic.claude-sonnet-4-5-20250929-v1:0": 2e5,
2174
+ "anthropic.claude-opus-4-5-20251101-v1:0": 2e5,
2175
+ "anthropic.claude-haiku-4-5-20251001-v1:0": 2e5,
2176
+ "anthropic.claude-opus-4-7": 1e6,
2177
+ "anthropic.claude-sonnet-4-6": 1e6,
2178
+ "amazon.nova-pro-v1:0": 3e5,
2179
+ "amazon.nova-lite-v1:0": 3e5,
2180
+ "amazon.nova-micro-v1:0": 128e3,
2181
+ "meta.llama3-1-8b-instruct-v1:0": 128e3,
2182
+ "meta.llama3-1-70b-instruct-v1:0": 128e3,
2183
+ "meta.llama3-1-405b-instruct-v1:0": 128e3,
2184
+ "mistral-tiny": 32768,
2185
+ "mistral-small-latest": 32768,
2186
+ "mistral-medium-latest": 32768,
2187
+ "mistral-large-latest": 32768
2188
+ };
2189
+ var currentCallIdStore = new AsyncLocalStorage();
2190
+ var callStopOverrideStore = new AsyncLocalStorage();
2191
+ var configuredCallbacks = [];
2192
+ var configuredSuccessCallbacks = [];
2193
+ var configuredFailureCallbacks = [];
2194
+ var registeredProviders = /* @__PURE__ */ new Map();
2195
+ var registeredProviderFactories = /* @__PURE__ */ new Map();
2196
+ var openAIModelSet = new Set(OPENAI_MODELS);
2197
+ var anthropicModelSet = new Set(ANTHROPIC_MODELS);
2198
+ var azureModelSet = new Set(AZURE_MODELS);
2199
+ var bedrockModelSet = new Set(BEDROCK_MODELS);
2200
+ var nativeProviderSet = new Set(SUPPORTED_NATIVE_PROVIDERS);
2201
+ async function llmCallContext(callback) {
2202
+ const callId = randomUUID();
2203
+ return await currentCallIdStore.run(callId, async () => await callback(callId));
2204
+ }
2205
+ var llm_call_context = llmCallContext;
2206
+ function validate_function_name(name, provider = "LLM") {
2207
+ if (!name) {
2208
+ throw new Error(`${provider} function name cannot be empty`);
2209
+ }
2210
+ if (!/^[A-Za-z_]/.test(name)) {
2211
+ throw new Error(`${provider} function name '${name}' must start with a letter or underscore`);
2212
+ }
2213
+ if (name.length > 64) {
2214
+ throw new Error(`${provider} function name '${name}' exceeds 64 character limit`);
2215
+ }
2216
+ if (!/^[a-z_][a-z0-9_]*$/.test(name)) {
2217
+ throw new Error(`${provider} function name '${name}' contains invalid characters. Only lowercase letters, numbers, and underscores allowed`);
2218
+ }
2219
+ return name;
2220
+ }
2221
+ function sanitize_function_name(name) {
2222
+ const normalized = name.trim().toLowerCase().replace(/[^a-z0-9_]/g, "_").replace(/_+/g, "_").slice(0, 64);
2223
+ return /^[a-z_]/.test(normalized) ? normalized : `_${normalized}`;
2224
+ }
2225
+ function extract_tool_info(tool) {
2226
+ if (!recordOrNull(tool)) {
2227
+ throw new Error("Tool must be a dictionary");
2228
+ }
2229
+ let source = tool;
2230
+ if ("function" in tool) {
2231
+ const functionInfo = recordOrNull(tool.function);
2232
+ if (!functionInfo) {
2233
+ throw new Error("Tool function must be a dictionary");
2234
+ }
2235
+ source = functionInfo;
2236
+ }
2237
+ const name = stringOrEmpty(source.name);
2238
+ const description = stringOrEmpty(source.description);
2239
+ const parameters = recordOrNull(source.parameters) ?? argsSchemaParameters(source.args_schema) ?? {};
2240
+ return [name, description, parameters];
2241
+ }
2242
+ function log_tool_conversion(tool, provider) {
2243
+ void extract_tool_info(tool);
2244
+ void provider;
2245
+ }
2246
+ function safe_tool_conversion(tool, provider) {
2247
+ log_tool_conversion(tool, provider);
2248
+ const [name, description, parameters] = extract_tool_info(tool);
2249
+ return [validate_function_name(sanitize_function_name(name), provider), description, parameters];
2250
+ }
2251
+ function getCurrentCallId() {
2252
+ return currentCallIdStore.getStore() ?? randomUUID();
2253
+ }
2254
+ var get_current_call_id = getCurrentCallId;
2255
+ async function callStopOverride(llm, stop, callback) {
2256
+ const current = callStopOverrideStore.getStore();
2257
+ const overrides = new Map(current ?? []);
2258
+ if (stop === null) {
2259
+ overrides.delete(llm);
2260
+ } else {
2261
+ overrides.set(llm, normalizeStopSequences(stop));
2262
+ }
2263
+ return await callStopOverrideStore.run(overrides, async () => await callback());
2264
+ }
2265
+ var call_stop_override = callStopOverride;
2266
+ function callStopOverrideSync(llm, stop, callback) {
2267
+ const current = callStopOverrideStore.getStore();
2268
+ const overrides = new Map(current ?? []);
2269
+ if (stop === null) {
2270
+ overrides.delete(llm);
2271
+ } else {
2272
+ overrides.set(llm, normalizeStopSequences(stop));
2273
+ }
2274
+ return callStopOverrideStore.run(overrides, callback);
2275
+ }
2276
+ var call_stop_override_sync = callStopOverrideSync;
2277
+ var FunctionLLM = class {
2278
+ constructor(fn) {
2279
+ this.fn = fn;
2280
+ }
2281
+ fn;
2282
+ usageMetrics = emptyUsageMetrics();
2283
+ async call(messages, options) {
2284
+ const response = await this.fn(messages, options);
2285
+ this.usageMetrics = addUsageMetrics(
2286
+ this.usageMetrics,
2287
+ estimateUsageMetrics(messages, response)
2288
+ );
2289
+ return response;
2290
+ }
2291
+ getUsageMetrics() {
2292
+ return { ...this.usageMetrics };
2293
+ }
2294
+ getTokenUsageSummary() {
2295
+ return this.getUsageMetrics();
2296
+ }
2297
+ resetUsageMetrics() {
2298
+ this.usageMetrics = emptyUsageMetrics();
2299
+ }
2300
+ };
2301
+ function createLLM(llmValue = null, env = process.env) {
2302
+ if (llmValue == null) {
2303
+ return llmViaEnvironmentOrFallback(env);
2304
+ }
2305
+ if (typeof llmValue === "string") {
2306
+ const spec2 = resolveLLMModelSpec(llmValue);
2307
+ const usesProviderFactory2 = registeredProviderFactories.has(spec2.provider);
2308
+ return createConfiguredLLM({
2309
+ model: usesProviderFactory2 ? spec2.model : llmValue,
2310
+ provider: spec2.provider,
2311
+ is_litellm: !spec2.useNative && !usesProviderFactory2
2312
+ });
2313
+ }
2314
+ if (typeof llmValue === "function" || isLLMClient(llmValue)) {
2315
+ return createLLMClient(llmValue);
2316
+ }
2317
+ const model = stringProperty(llmValue, "model") ?? stringProperty(llmValue, "model_name") ?? stringProperty(llmValue, "deployment_name") ?? stringifyLLMValue(llmValue);
2318
+ const spec = resolveLLMModelSpec(model, stringProperty(llmValue, "provider"));
2319
+ const usesProviderFactory = registeredProviderFactories.has(spec.provider);
2320
+ const apiKey = stringProperty(llmValue, "api_key");
2321
+ const baseUrl = stringProperty(llmValue, "base_url");
2322
+ const apiBase = stringProperty(llmValue, "api_base");
2323
+ const usesOpenAICompatibleEndpoint = spec.provider === "openai" && (baseUrl !== void 0 || apiBase !== void 0);
2324
+ const options = {
2325
+ model: usesProviderFactory || usesOpenAICompatibleEndpoint ? spec.model : model,
2326
+ provider: spec.provider,
2327
+ is_litellm: !spec.useNative && !usesProviderFactory && !usesOpenAICompatibleEndpoint,
2328
+ temperature: numberProperty(llmValue, "temperature"),
2329
+ max_tokens: numberProperty(llmValue, "max_tokens"),
2330
+ max_completion_tokens: numberProperty(llmValue, "max_completion_tokens"),
2331
+ top_p: numberProperty(llmValue, "top_p"),
2332
+ presence_penalty: numberProperty(llmValue, "presence_penalty"),
2333
+ frequency_penalty: numberProperty(llmValue, "frequency_penalty"),
2334
+ seed: numberProperty(llmValue, "seed"),
2335
+ logprobs: booleanOrNumberProperty(llmValue, "logprobs"),
2336
+ top_logprobs: numberProperty(llmValue, "top_logprobs"),
2337
+ timeout: numberProperty(llmValue, "timeout")
2338
+ };
2339
+ const stop = llmValue["stop"];
2340
+ if (typeof stop === "string" || Array.isArray(stop)) {
2341
+ options.stop = stop;
2342
+ }
2343
+ const responseFormat = llmValue["response_format"] ?? llmValue["responseFormat"];
2344
+ if (responseFormat !== void 0) {
2345
+ options.response_format = responseFormat;
2346
+ }
2347
+ if (apiKey !== void 0) {
2348
+ options.api_key = apiKey;
2349
+ }
2350
+ if (baseUrl !== void 0) {
2351
+ options.base_url = baseUrl;
2352
+ }
2353
+ if (apiBase !== void 0) {
2354
+ options.api_base = apiBase;
2355
+ }
2356
+ const providerOptions = options;
2357
+ const llmOptions = llmValue;
2358
+ for (const key of [
2359
+ "api",
2360
+ "instructions",
2361
+ "store",
2362
+ "previousResponseId",
2363
+ "previous_response_id",
2364
+ "include",
2365
+ "builtinTools",
2366
+ "builtin_tools",
2367
+ "parseToolOutputs",
2368
+ "parse_tool_outputs",
2369
+ "autoChain",
2370
+ "auto_chain",
2371
+ "autoChainReasoning",
2372
+ "auto_chain_reasoning"
2373
+ ]) {
2374
+ if (llmOptions[key] !== void 0) {
2375
+ providerOptions[key] = llmOptions[key];
2376
+ }
2377
+ }
2378
+ return createConfiguredLLM(options);
2379
+ }
2380
+ var create_llm = createLLM;
2381
+ function llmViaEnvironmentOrFallback(env = process.env) {
2382
+ const model = env.MODEL ?? env.MODEL_NAME ?? env.OPENAI_MODEL_NAME ?? DEFAULT_LLM_MODEL;
2383
+ let baseUrl = env.BASE_URL ?? env.OPENAI_API_BASE ?? env.OPENAI_BASE_URL;
2384
+ let apiBase = env.API_BASE ?? env.AZURE_API_BASE;
2385
+ if (baseUrl !== void 0 && apiBase === void 0) {
2386
+ apiBase = baseUrl;
2387
+ } else if (apiBase !== void 0 && baseUrl === void 0) {
2388
+ baseUrl = apiBase;
2389
+ }
2390
+ const spec = resolveLLMModelSpec(model);
2391
+ const usesProviderFactory = registeredProviderFactories.has(spec.provider);
2392
+ const options = {
2393
+ model: usesProviderFactory ? spec.model : model,
2394
+ provider: spec.provider,
2395
+ is_litellm: !spec.useNative && !usesProviderFactory,
2396
+ ...baseUrl === void 0 ? {} : { base_url: baseUrl },
2397
+ ...apiBase === void 0 ? {} : { api_base: apiBase }
2398
+ };
2399
+ applyLLMEnvVars(options, spec.provider, env);
2400
+ return createConfiguredLLM(options);
2401
+ }
2402
+ var llm_via_environment_or_fallback = llmViaEnvironmentOrFallback;
2403
+ var _llm_via_environment_or_fallback = llmViaEnvironmentOrFallback;
2404
+ function normalizeLLMEnvKeyName(keyName) {
2405
+ for (const pattern of LITELLM_PARAMS) {
2406
+ if (keyName.includes(pattern)) {
2407
+ return pattern;
2408
+ }
2409
+ }
2410
+ return keyName;
2411
+ }
2412
+ var normalize_llm_env_key_name = normalizeLLMEnvKeyName;
2413
+ var _normalize_key_name = normalizeLLMEnvKeyName;
2414
+ var BASE_LLM_OPTION_FIELDS = /* @__PURE__ */ new Set([
2415
+ "model",
2416
+ "temperature",
2417
+ "apiKey",
2418
+ "api_key",
2419
+ "baseUrl",
2420
+ "base_url",
2421
+ "provider",
2422
+ "preferUpload",
2423
+ "prefer_upload",
2424
+ "isLitellm",
2425
+ "is_litellm",
2426
+ "stop",
2427
+ "stopSequences",
2428
+ "stop_sequences",
2429
+ "additionalParams",
2430
+ "additional_params",
2431
+ "responseFormat",
2432
+ "response_format",
2433
+ "contextWindowSize",
2434
+ "context_window_size"
2435
+ ]);
2436
+ var BaseLLM = class _BaseLLM {
2437
+ llmType = "base";
2438
+ llm_type = "base";
2439
+ model;
2440
+ temperature;
2441
+ apiKey;
2442
+ api_key;
2443
+ baseUrl;
2444
+ base_url;
2445
+ provider;
2446
+ preferUpload;
2447
+ prefer_upload;
2448
+ isLitellm;
2449
+ is_litellm;
2450
+ additionalParams;
2451
+ additional_params;
2452
+ responseFormat;
2453
+ response_format;
2454
+ contextWindowSize;
2455
+ stopValues;
2456
+ tokenUsage = emptyUsageMetrics();
2457
+ constructor(options) {
2458
+ if (!options.model) {
2459
+ throw new Error("Model name is required and cannot be empty.");
2460
+ }
2461
+ this.model = options.model;
2462
+ this.temperature = options.temperature ?? null;
2463
+ this.apiKey = options.apiKey ?? options.api_key ?? null;
2464
+ this.api_key = this.apiKey;
2465
+ this.baseUrl = options.baseUrl ?? options.base_url ?? null;
2466
+ this.base_url = this.baseUrl;
2467
+ this.provider = options.provider || "openai";
2468
+ this.preferUpload = options.preferUpload ?? options.prefer_upload ?? false;
2469
+ this.prefer_upload = this.preferUpload;
2470
+ this.isLitellm = options.isLitellm ?? options.is_litellm ?? false;
2471
+ this.is_litellm = this.isLitellm;
2472
+ this.stopValues = normalizeStopSequences(options.stopSequences ?? options.stop_sequences ?? options.stop ?? []);
2473
+ this.additionalParams = { ...options.additionalParams ?? {}, ...options.additional_params ?? {} };
2474
+ this.additional_params = this.additionalParams;
2475
+ this.responseFormat = options.responseFormat ?? options.response_format ?? null;
2476
+ this.response_format = this.responseFormat;
2477
+ this.contextWindowSize = options.contextWindowSize ?? options.context_window_size ?? 0;
2478
+ }
2479
+ async acall(messages, options) {
2480
+ return await this.call(this.formatMessages(messages), options);
2481
+ }
2482
+ get stop() {
2483
+ return [...this.stopValues];
2484
+ }
2485
+ set stop(value) {
2486
+ this.stopValues = normalizeStopSequences(value);
2487
+ }
2488
+ get stopSequences() {
2489
+ const override = callStopOverrideStore.getStore()?.get(this);
2490
+ if (override) {
2491
+ return override;
2492
+ }
2493
+ return this.stopValues;
2494
+ }
2495
+ get stop_sequences() {
2496
+ return this.stopSequences;
2497
+ }
2498
+ setStopSequences(stop) {
2499
+ this.stopValues = normalizeStopSequences(stop);
2500
+ }
2501
+ set_stop_sequences(stop) {
2502
+ this.setStopSequences(stop);
2503
+ }
2504
+ supportsStopWords() {
2505
+ if (this.model.toLowerCase().includes("gpt-5")) {
2506
+ return false;
2507
+ }
2508
+ return DEFAULT_SUPPORTS_STOP_WORDS;
2509
+ }
2510
+ supports_stop_words() {
2511
+ return this.supportsStopWords();
2512
+ }
2513
+ supportsFunctionCalling() {
2514
+ return true;
2515
+ }
2516
+ supports_function_calling() {
2517
+ return this.supportsFunctionCalling();
2518
+ }
2519
+ supportsStopWordsImplementation() {
2520
+ return this.stopSequences.length > 0;
2521
+ }
2522
+ _supports_stop_words_implementation() {
2523
+ return this.supportsStopWordsImplementation();
2524
+ }
2525
+ applyStopWords(content) {
2526
+ if (this.stopSequences.length === 0 || content.length === 0) {
2527
+ return content;
2528
+ }
2529
+ let earliest = content.length;
2530
+ for (const stop of this.stopSequences) {
2531
+ const index = content.indexOf(stop);
2532
+ if (index !== -1 && index < earliest) {
2533
+ earliest = index;
2534
+ }
2535
+ }
2536
+ return earliest === content.length ? content : content.slice(0, earliest).trim();
2537
+ }
2538
+ _apply_stop_words(content) {
2539
+ return this.applyStopWords(content);
2540
+ }
2541
+ getContextWindowSize() {
2542
+ if (this.contextWindowSize !== 0) {
2543
+ return this.contextWindowSize;
2544
+ }
2545
+ this.contextWindowSize = knownContextWindowSizeForModel(this.model) ?? DEFAULT_CONTEXT_WINDOW_SIZE;
2546
+ return this.contextWindowSize;
2547
+ }
2548
+ get_context_window_size() {
2549
+ return this.getContextWindowSize();
2550
+ }
2551
+ supportsMultimodal() {
2552
+ return false;
2553
+ }
2554
+ supports_multimodal() {
2555
+ return this.supportsMultimodal();
2556
+ }
2557
+ formatTextContent(text) {
2558
+ return { type: "text", text };
2559
+ }
2560
+ format_text_content(text) {
2561
+ return this.formatTextContent(text);
2562
+ }
2563
+ getFileUploader() {
2564
+ return null;
2565
+ }
2566
+ get_file_uploader() {
2567
+ return this.getFileUploader();
2568
+ }
2569
+ get lastResponseId() {
2570
+ return null;
2571
+ }
2572
+ get last_response_id() {
2573
+ return this.lastResponseId;
2574
+ }
2575
+ get lastReasoningItems() {
2576
+ return null;
2577
+ }
2578
+ get last_reasoning_items() {
2579
+ return this.lastReasoningItems;
2580
+ }
2581
+ resetChain() {
2582
+ }
2583
+ reset_chain() {
2584
+ this.resetChain();
2585
+ }
2586
+ resetReasoningChain() {
2587
+ }
2588
+ reset_reasoning_chain() {
2589
+ this.resetReasoningChain();
2590
+ }
2591
+ emitCallStartedEvent(options) {
2592
+ crewaiEventBus.emit(this, new LLMCallStartedEvent({
2593
+ call_id: getCurrentCallId(),
2594
+ from_task: options.fromTask ?? options.from_task,
2595
+ from_agent: options.fromAgent ?? options.from_agent,
2596
+ model: this.model,
2597
+ messages: serializeLLMEventMessages(options.messages),
2598
+ tools: options.tools ?? null,
2599
+ callbacks: options.callbacks ?? null,
2600
+ available_functions: options.availableFunctions ?? options.available_functions ?? null
2601
+ }));
2602
+ }
2603
+ _emit_call_started_event(options) {
2604
+ this.emitCallStartedEvent(options);
2605
+ }
2606
+ emitCallCompletedEvent(options) {
2607
+ crewaiEventBus.emit(this, new LLMCallCompletedEvent({
2608
+ call_id: getCurrentCallId(),
2609
+ from_task: options.fromTask ?? options.from_task,
2610
+ from_agent: options.fromAgent ?? options.from_agent,
2611
+ model: this.model,
2612
+ messages: options.messages === void 0 || options.messages === null ? null : serializeLLMEventMessages(options.messages),
2613
+ response: options.response,
2614
+ call_type: options.callType ?? options.call_type ?? LLMCallType.LLM_CALL,
2615
+ usage: _BaseLLM.usageToDict(options.usage)
2616
+ }));
2617
+ }
2618
+ _emit_call_completed_event(options) {
2619
+ this.emitCallCompletedEvent(options);
2620
+ }
2621
+ handleEmitCallEvents(responseOrOptions, callType = LLMCallType.LLM_CALL, fromTask = null, fromAgent = null, messages = null, usage = null) {
2622
+ const options = isRecord(responseOrOptions) && ("response" in responseOrOptions || "call_type" in responseOrOptions || "callType" in responseOrOptions) ? responseOrOptions : {
2623
+ response: responseOrOptions,
2624
+ callType,
2625
+ fromTask,
2626
+ fromAgent,
2627
+ messages,
2628
+ usage
2629
+ };
2630
+ this.emitCallCompletedEvent(options);
2631
+ }
2632
+ _handle_emit_call_events(responseOrOptions, callType = LLMCallType.LLM_CALL, fromTask = null, fromAgent = null, messages = null, usage = null) {
2633
+ this.handleEmitCallEvents(responseOrOptions, callType, fromTask, fromAgent, messages, usage);
2634
+ }
2635
+ emitCallFailedEvent(options) {
2636
+ crewaiEventBus.emit(this, new LLMCallFailedEvent({
2637
+ call_id: getCurrentCallId(),
2638
+ from_task: options.fromTask ?? options.from_task,
2639
+ from_agent: options.fromAgent ?? options.from_agent,
2640
+ model: this.model,
2641
+ error: options.error
2642
+ }));
2643
+ }
2644
+ _emit_call_failed_event(options) {
2645
+ this.emitCallFailedEvent(options);
2646
+ }
2647
+ emitStreamChunkEvent(options) {
2648
+ crewaiEventBus.emit(this, new LLMStreamChunkEvent({
2649
+ call_id: getCurrentCallId(),
2650
+ from_task: options.fromTask ?? options.from_task,
2651
+ from_agent: options.fromAgent ?? options.from_agent,
2652
+ model: this.model,
2653
+ chunk: options.chunk,
2654
+ tool_call: options.toolCall ?? options.tool_call ?? null,
2655
+ call_type: options.callType ?? options.call_type ?? null,
2656
+ response_id: options.responseId ?? options.response_id ?? null
2657
+ }));
2658
+ }
2659
+ _emit_stream_chunk_event(options) {
2660
+ this.emitStreamChunkEvent(options);
2661
+ }
2662
+ emitThinkingChunkEvent(options) {
2663
+ crewaiEventBus.emit(this, new LLMThinkingChunkEvent({
2664
+ call_id: getCurrentCallId(),
2665
+ from_task: options.fromTask ?? options.from_task,
2666
+ from_agent: options.fromAgent ?? options.from_agent,
2667
+ model: this.model,
2668
+ chunk: options.chunk,
2669
+ response_id: options.responseId ?? options.response_id ?? null
2670
+ }));
2671
+ }
2672
+ _emit_thinking_chunk_event(options) {
2673
+ this.emitThinkingChunkEvent(options);
2674
+ }
2675
+ async handleToolExecution(options) {
2676
+ const functionName = options.functionName ?? options.function_name;
2677
+ const functionArgs = options.functionArgs ?? options.function_args ?? {};
2678
+ const availableFunctions = options.availableFunctions ?? options.available_functions ?? {};
2679
+ if (!functionName) {
2680
+ return null;
2681
+ }
2682
+ const fn = availableFunctions[functionName];
2683
+ if (!fn) {
2684
+ return null;
2685
+ }
2686
+ const startedAt = /* @__PURE__ */ new Date();
2687
+ try {
2688
+ crewaiEventBus.emit(this, new ToolUsageStartedEvent({
2689
+ toolName: functionName,
2690
+ toolArgs: functionArgs,
2691
+ toolClass: "BaseLLM"
2692
+ }));
2693
+ const result = await invokeAvailableFunction(fn, functionArgs);
2694
+ crewaiEventBus.emit(this, new ToolUsageFinishedEvent({
2695
+ toolName: functionName,
2696
+ toolArgs: functionArgs,
2697
+ toolClass: "BaseLLM",
2698
+ startedAt,
2699
+ output: result
2700
+ }));
2701
+ this.emitCallCompletedEvent({
2702
+ response: result,
2703
+ callType: LLMCallType.TOOL_CALL,
2704
+ from_task: options.fromTask ?? options.from_task,
2705
+ from_agent: options.fromAgent ?? options.from_agent
2706
+ });
2707
+ return stringifyToolExecutionResult(result);
2708
+ } catch (error) {
2709
+ const errorMessage = `Error executing function '${functionName}': ${error instanceof Error ? error.message : String(error)}`;
2710
+ crewaiEventBus.emit(this, new ToolUsageErrorEvent({
2711
+ toolName: functionName,
2712
+ toolArgs: functionArgs,
2713
+ toolClass: "BaseLLM",
2714
+ error: errorMessage
2715
+ }));
2716
+ this.emitCallFailedEvent({
2717
+ error: errorMessage,
2718
+ from_task: options.fromTask ?? options.from_task,
2719
+ from_agent: options.fromAgent ?? options.from_agent
2720
+ });
2721
+ return null;
2722
+ }
2723
+ }
2724
+ async _handle_tool_execution(options) {
2725
+ return await this.handleToolExecution(options);
2726
+ }
2727
+ async handleToolCall(toolCalls, availableFunctions = null, fromTask = null, fromAgent = null) {
2728
+ const calls = Array.isArray(toolCalls) ? toolCalls : [toolCalls];
2729
+ const available = availableFunctions ?? {};
2730
+ if (calls.length === 0 || Object.keys(available).length === 0) {
2731
+ return null;
2732
+ }
2733
+ const parsed = parseLLMToolCall(calls[0]);
2734
+ if (!parsed) {
2735
+ return null;
2736
+ }
2737
+ return await this.handleToolExecution({
2738
+ functionName: parsed.name,
2739
+ functionArgs: parsed.args,
2740
+ availableFunctions: available,
2741
+ fromTask,
2742
+ fromAgent
2743
+ });
2744
+ }
2745
+ async _handle_tool_call(toolCalls, availableFunctions = null, fromTask = null, fromAgent = null) {
2746
+ return await this.handleToolCall(toolCalls, availableFunctions, fromTask, fromAgent);
2747
+ }
2748
+ convertToolsForInterference(tools) {
2749
+ return tools;
2750
+ }
2751
+ _convert_tools_for_interference(tools) {
2752
+ return this.convertToolsForInterference(tools);
2753
+ }
2754
+ formatMessages(messages) {
2755
+ if (typeof messages === "string") {
2756
+ return [{ role: "user", content: messages }];
2757
+ }
2758
+ const cleaned = messages.map((message, index) => {
2759
+ if (Array.isArray(message)) {
2760
+ throw new Error(`Message at index ${String(index)} must be a dictionary.`);
2761
+ }
2762
+ if (!isLLMRole(message.role) || typeof message.content !== "string") {
2763
+ throw new Error(`Message at index ${String(index)} must have 'role' and 'content' keys.`);
2764
+ }
2765
+ const copy = { ...message };
2766
+ stripCacheBreakpoint(copy);
2767
+ return copy;
2768
+ });
2769
+ return this.processMessageFiles(cleaned);
2770
+ }
2771
+ _format_messages(messages) {
2772
+ return this.formatMessages(messages);
2773
+ }
2774
+ formatMessagesForProvider(messages) {
2775
+ for (const message of messages) {
2776
+ if (!isLLMRole(message.role) || typeof message.content !== "string") {
2777
+ throw new TypeError("Invalid message format. Each message must be a dict with 'role' and 'content' keys");
2778
+ }
2779
+ }
2780
+ const model = this.model.toLowerCase();
2781
+ if (model.includes("o1")) {
2782
+ return messages.map((message) => message.role === "system" ? { ...message, role: "assistant" } : { ...message });
2783
+ }
2784
+ if ((model.includes("mistral") || model.includes("ollama")) && messages.at(-1)?.role === "assistant") {
2785
+ return [...messages.map((message) => ({ ...message })), { role: "user", content: model.includes("mistral") ? "Please continue." : "" }];
2786
+ }
2787
+ if (!_BaseLLM.isAnthropicModel(this.model)) {
2788
+ return messages.map((message) => ({ ...message }));
2789
+ }
2790
+ if (messages.length === 0 || messages[0]?.role === "system") {
2791
+ return [{ role: "user", content: "." }, ...messages.map((message) => ({ ...message }))];
2792
+ }
2793
+ return messages.map((message) => ({ ...message }));
2794
+ }
2795
+ _format_messages_for_provider(messages) {
2796
+ return this.formatMessagesForProvider(messages);
2797
+ }
2798
+ prepareCompletionParams(messages, ...args) {
2799
+ const tools = Array.isArray(args[0]) ? args[0] : null;
2800
+ const skipFileProcessing = typeof args[1] === "boolean" ? args[1] : false;
2801
+ const normalizedMessages = typeof messages === "string" ? [{ role: "user", content: messages }] : messages.map((message, index) => {
2802
+ if (Array.isArray(message)) {
2803
+ throw new Error(`Message at index ${String(index)} must be a dictionary.`);
2804
+ }
2805
+ if (!isLLMRole(message.role) || typeof message.content !== "string") {
2806
+ throw new Error(`Message at index ${String(index)} must have 'role' and 'content' keys.`);
2807
+ }
2808
+ const copy = { ...message };
2809
+ stripCacheBreakpoint(copy);
2810
+ return copy;
2811
+ });
2812
+ const processedMessages = skipFileProcessing ? normalizedMessages : this.processMessageFiles(normalizedMessages);
2813
+ const params = removeUndefinedValues({
2814
+ model: this.model,
2815
+ messages: this.formatMessagesForProvider(processedMessages),
2816
+ temperature: this.temperature,
2817
+ stop: this.supportsStopWords() && this.stopSequences.length > 0 ? [...this.stopSequences] : null,
2818
+ response_format: this.responseFormat === null ? null : this.serializeResponseFormat(this.responseFormat),
2819
+ api_key: this.apiKey,
2820
+ base_url: this.baseUrl,
2821
+ provider: this.provider,
2822
+ stream: booleanOrNull(this.additionalParams.stream),
2823
+ tools,
2824
+ ...this.additionalParams
2825
+ });
2826
+ return params;
2827
+ }
2828
+ _prepare_completion_params(messages, ...args) {
2829
+ return this.prepareCompletionParams(messages, ...args);
2830
+ }
2831
+ processMessageFiles(messages) {
2832
+ if (!this.supportsMultimodal() && messages.some((message) => message.files && Object.keys(message.files).length > 0)) {
2833
+ throw new Error(`Model '${this.model}' does not support multimodal input, but files were provided via 'input_files'.`);
2834
+ }
2835
+ if (!this.supportsMultimodal()) {
2836
+ return messages.map((message) => ({ ...message }));
2837
+ }
2838
+ const uploader = this.preferUpload ? this.getFileUploader() : null;
2839
+ return messages.map((message) => {
2840
+ if (!message.files || Object.keys(message.files).length === 0) {
2841
+ return { ...message };
2842
+ }
2843
+ const typedFiles = typedMultimodalFiles(message.files);
2844
+ if (!uploader && typedFiles) {
2845
+ const api = llmApiMode(this);
2846
+ const text = typeof message.content === "string" && message.content.length > 0 ? message.content : null;
2847
+ const formattedBlocks = formatMultimodalContent(
2848
+ typedFiles,
2849
+ this.provider || this.model,
2850
+ api === void 0 ? { text } : { api, text }
2851
+ );
2852
+ const { files: _files2, ...rest2 } = message;
2853
+ void _files2;
2854
+ if (formattedBlocks.length === 0) {
2855
+ return { ...rest2 };
2856
+ }
2857
+ return {
2858
+ ...rest2,
2859
+ content: formattedBlocks
2860
+ };
2861
+ }
2862
+ const contentBlocks = [];
2863
+ if (typeof message.content === "string" && message.content.length > 0) {
2864
+ contentBlocks.push(this.formatTextContent(message.content));
2865
+ }
2866
+ for (const [name, file] of Object.entries(message.files)) {
2867
+ contentBlocks.push(uploader ? formatUploadedFileContentBlock(name, uploader.upload(name, file)) : formatInlineFileContentBlock(name, file));
2868
+ }
2869
+ const { files: _files, ...rest } = message;
2870
+ void _files;
2871
+ const formattedMessage = {
2872
+ ...rest,
2873
+ content: contentBlocks
2874
+ };
2875
+ return formattedMessage;
2876
+ });
2877
+ }
2878
+ _process_message_files(messages) {
2879
+ return this.processMessageFiles(messages);
2880
+ }
2881
+ async aprocessMessageFiles(messages) {
2882
+ return await Promise.resolve(this.processMessageFiles(messages));
2883
+ }
2884
+ async _aprocess_message_files(messages) {
2885
+ return await this.aprocessMessageFiles(messages);
2886
+ }
2887
+ async handleStreamingResponse(params, callbacks = null, availableFunctions = null, fromTask = null, fromAgent = null, responseModel = null) {
2888
+ void responseModel;
2889
+ const chunks = streamingChunksFromParams(params);
2890
+ let fullResponse = "";
2891
+ let lastChunk = null;
2892
+ let usageInfo = null;
2893
+ const collectedChunks = [];
2894
+ for await (const chunk of chunks) {
2895
+ lastChunk = chunk;
2896
+ collectedChunks.push(chunk);
2897
+ usageInfo = usageRecordFromChunk(chunk) ?? usageInfo;
2898
+ const content = streamingChunkContent(chunk);
2899
+ if (content !== null) {
2900
+ fullResponse += content;
2901
+ this.emitStreamChunkEvent({
2902
+ chunk: content,
2903
+ callType: LLMCallType.LLM_CALL,
2904
+ responseId: stringPropertyFromRecord(chunk, "id"),
2905
+ fromTask,
2906
+ fromAgent
2907
+ });
2908
+ }
2909
+ await this.invokeStreamingCallbacks(callbacks, chunk);
2910
+ }
2911
+ if (usageInfo) {
2912
+ this.trackTokenUsageInternal(usageInfo);
2913
+ }
2914
+ this.handleStreamingCallbacks(callbacks, usageInfo, lastChunk);
2915
+ const toolCalls = AccumulatedToolArgs.fromStreamingChunks(collectedChunks);
2916
+ if (toolCalls.length > 0) {
2917
+ if (availableFunctions && Object.keys(availableFunctions).length > 0) {
2918
+ const toolResult = await this.handleToolCall(toolCalls, availableFunctions, fromTask, fromAgent);
2919
+ if (toolResult !== null) {
2920
+ return toolResult;
2921
+ }
2922
+ } else if (fullResponse.length === 0) {
2923
+ return toolCalls;
2924
+ }
2925
+ }
2926
+ if (!fullResponse.trim() && collectedChunks.length === 0) {
2927
+ return await this.handleNonStreamingResponse({ ...params, stream: false }, callbacks, availableFunctions, fromTask, fromAgent, responseModel);
2928
+ }
2929
+ this.handleEmitCallEvents({
2930
+ response: fullResponse,
2931
+ callType: LLMCallType.LLM_CALL,
2932
+ fromTask,
2933
+ fromAgent,
2934
+ messages: messagesFromParams(params),
2935
+ usage: usageInfo ? _BaseLLM.usageToDict(usageInfo) : null
2936
+ });
2937
+ return fullResponse;
2938
+ }
2939
+ async _handle_streaming_response(params, callbacks = null, availableFunctions = null, fromTask = null, fromAgent = null, responseModel = null) {
2940
+ return await this.handleStreamingResponse(params, callbacks, availableFunctions, fromTask, fromAgent, responseModel);
2941
+ }
2942
+ async handleStreamingToolCalls(toolCalls, accumulatedToolArgs = null, availableFunctions = null, fromTask = null, fromAgent = null, responseId = null) {
2943
+ const deltas = Array.isArray(toolCalls) ? toolCalls : [toolCalls];
2944
+ const accumulators = accumulatedToolArgs ?? {};
2945
+ for (const toolCall of deltas) {
2946
+ const record = readLLMRecord(toolCall);
2947
+ const index = numberValue(record.index) ?? 0;
2948
+ accumulators[index] ??= new AccumulatedToolArgs({ index });
2949
+ accumulators[index].accumulate(toolCall);
2950
+ this.emitStreamChunkEvent({
2951
+ chunk: stringPropertyFromRecord(readLLMRecord(record.function), "arguments") ?? "",
2952
+ toolCall: accumulators[index].toToolCall(),
2953
+ callType: LLMCallType.TOOL_CALL,
2954
+ responseId,
2955
+ fromTask,
2956
+ fromAgent
2957
+ });
2958
+ }
2959
+ const completeToolCalls = AccumulatedToolArgs.toToolCalls(accumulators);
2960
+ if (!availableFunctions || Object.keys(availableFunctions).length === 0) {
2961
+ return null;
2962
+ }
2963
+ return await this.handleToolCall(completeToolCalls, availableFunctions, fromTask, fromAgent);
2964
+ }
2965
+ async _handle_streaming_tool_calls(toolCalls, accumulatedToolArgs = null, availableFunctions = null, fromTask = null, fromAgent = null, responseId = null) {
2966
+ return await this.handleStreamingToolCalls(toolCalls, accumulatedToolArgs, availableFunctions, fromTask, fromAgent, responseId);
2967
+ }
2968
+ handleStreamingCallbacks(callbacks = null, usageInfo = null, lastChunk = null) {
2969
+ if (!callbacks || callbacks.length === 0) {
2970
+ return;
2971
+ }
2972
+ const usage = usageInfo ?? usageRecordFromChunk(lastChunk);
2973
+ if (!usage) {
2974
+ return;
2975
+ }
2976
+ for (const callback of callbacks) {
2977
+ invokeUsageCallback(callback, usage);
2978
+ }
2979
+ }
2980
+ _handle_streaming_callbacks(callbacks = null, usageInfo = null, lastChunk = null) {
2981
+ this.handleStreamingCallbacks(callbacks, usageInfo, lastChunk);
2982
+ }
2983
+ async invokeStreamingCallbacks(callbacks, chunk) {
2984
+ if (!callbacks) {
2985
+ return;
2986
+ }
2987
+ for (const callback of callbacks) {
2988
+ if (typeof callback === "function") {
2989
+ await callback(chunk);
2990
+ }
2991
+ }
2992
+ }
2993
+ handleNonStreamingResponse(params, callbacks = null, availableFunctions = null, fromTask = null, fromAgent = null, responseModel = null) {
2994
+ void responseModel;
2995
+ const response = params.response ?? params.rawResponse ?? params.raw_response ?? params;
2996
+ const usage = usageRecordFromChunk(response) ?? usageRecordFromChunk(params);
2997
+ if (usage) {
2998
+ this.trackTokenUsageInternal(usage);
2999
+ this.handleStreamingCallbacks(callbacks, usage, response);
3000
+ }
3001
+ const toolCalls = extractToolCallsFromResponse(response);
3002
+ if (toolCalls.length > 0) {
3003
+ if (!availableFunctions || Object.keys(availableFunctions).length === 0) {
3004
+ return toolCalls;
3005
+ }
3006
+ return this.handleToolCall(toolCalls, availableFunctions, fromTask, fromAgent);
3007
+ }
3008
+ const textResponse = extractResponseText(response);
3009
+ this.handleEmitCallEvents({
3010
+ response: textResponse,
3011
+ callType: LLMCallType.LLM_CALL,
3012
+ fromTask,
3013
+ fromAgent,
3014
+ messages: messagesFromParams(params),
3015
+ usage: usage ? _BaseLLM.usageToDict(usage) : null
3016
+ });
3017
+ return textResponse;
3018
+ }
3019
+ _handle_non_streaming_response(params, callbacks = null, availableFunctions = null, fromTask = null, fromAgent = null, responseModel = null) {
3020
+ return this.handleNonStreamingResponse(params, callbacks, availableFunctions, fromTask, fromAgent, responseModel);
3021
+ }
3022
+ async ahandleNonStreamingResponse(params, callbacks = null, availableFunctions = null, fromTask = null, fromAgent = null, responseModel = null) {
3023
+ return await Promise.resolve(this.handleNonStreamingResponse(params, callbacks, availableFunctions, fromTask, fromAgent, responseModel));
3024
+ }
3025
+ async _ahandle_non_streaming_response(params, callbacks = null, availableFunctions = null, fromTask = null, fromAgent = null, responseModel = null) {
3026
+ return await this.ahandleNonStreamingResponse(params, callbacks, availableFunctions, fromTask, fromAgent, responseModel);
3027
+ }
3028
+ async ahandleStreamingResponse(params, callbacks = null, availableFunctions = null, fromTask = null, fromAgent = null, responseModel = null) {
3029
+ return await this.handleStreamingResponse(params, callbacks, availableFunctions, fromTask, fromAgent, responseModel);
3030
+ }
3031
+ async _ahandle_streaming_response(params, callbacks = null, availableFunctions = null, fromTask = null, fromAgent = null, responseModel = null) {
3032
+ return await this.ahandleStreamingResponse(params, callbacks, availableFunctions, fromTask, fromAgent, responseModel);
3033
+ }
3034
+ getCustomLlmProvider() {
3035
+ const index = this.model.indexOf("/");
3036
+ return index === -1 ? null : this.model.slice(0, index);
3037
+ }
3038
+ _get_custom_llm_provider() {
3039
+ return this.getCustomLlmProvider();
3040
+ }
3041
+ validateCallParams() {
3042
+ if (this.responseFormat === null) {
3043
+ return;
3044
+ }
3045
+ if (!isJsonResponseFormat(this.responseFormat) && !isStructuredOutputValidator(this.responseFormat)) {
3046
+ throw new Error(`The model ${this.model} received an unsupported response_format value.`);
3047
+ }
3048
+ }
3049
+ _validate_call_params() {
3050
+ this.validateCallParams();
3051
+ }
3052
+ toConfigDict() {
3053
+ return {
3054
+ model: this.model,
3055
+ temperature: this.temperature,
3056
+ api_key: this.apiKey,
3057
+ base_url: this.baseUrl,
3058
+ provider: this.provider,
3059
+ prefer_upload: this.preferUpload,
3060
+ is_litellm: this.isLitellm,
3061
+ stop: [...this.stop],
3062
+ additional_params: { ...this.additionalParams },
3063
+ ...this.responseFormat === null ? {} : { response_format: serializeResponseFormat(this.responseFormat) }
3064
+ };
3065
+ }
3066
+ to_config_dict() {
3067
+ return this.toConfigDict();
3068
+ }
3069
+ serializeResponseFormat(value) {
3070
+ if (value === null || value === void 0) {
3071
+ return value;
3072
+ }
3073
+ if (isJsonResponseFormat(value) || isStructuredOutputValidator(value)) {
3074
+ return serializeResponseFormat(value);
3075
+ }
3076
+ return value;
3077
+ }
3078
+ _serialize_response_format(value) {
3079
+ return this.serializeResponseFormat(value);
3080
+ }
3081
+ async invokeBeforeLlmCallHooks(messages, fromAgent = null) {
3082
+ if (fromAgent !== null && fromAgent !== void 0) {
3083
+ return true;
3084
+ }
3085
+ const context = new LLMCallHookContext({
3086
+ messages,
3087
+ llm: this,
3088
+ agent: null,
3089
+ task: null,
3090
+ crew: null
3091
+ });
3092
+ try {
3093
+ await runBeforeLlmCallHooks(context);
3094
+ return true;
3095
+ } catch (error) {
3096
+ if (error instanceof Error && error.message.includes("blocked by before_llm_call hook")) {
3097
+ return false;
3098
+ }
3099
+ return true;
3100
+ }
3101
+ }
3102
+ async _invoke_before_llm_call_hooks(messages, from_agent = null) {
3103
+ return await this.invokeBeforeLlmCallHooks(messages, from_agent);
3104
+ }
3105
+ async invokeAfterLlmCallHooks(messages, response, fromAgent = null) {
3106
+ if (fromAgent !== null && fromAgent !== void 0 || typeof response !== "string") {
3107
+ return response;
3108
+ }
3109
+ const context = new LLMCallHookContext({
3110
+ messages,
3111
+ llm: this,
3112
+ agent: null,
3113
+ task: null,
3114
+ crew: null,
3115
+ response
3116
+ });
3117
+ try {
3118
+ const result = await runAfterLlmCallHooks(context);
3119
+ return typeof result === "string" ? result : response;
3120
+ } catch {
3121
+ return response;
3122
+ }
3123
+ }
3124
+ async _invoke_after_llm_call_hooks(messages, response, from_agent = null) {
3125
+ return await this.invokeAfterLlmCallHooks(messages, response, from_agent);
3126
+ }
3127
+ trackTokenUsageInternal(usageData) {
3128
+ const promptTokens = numberFromUsage(usageData, "prompt_tokens", "prompt_token_count", "input_tokens");
3129
+ const completionTokens = numberFromUsage(usageData, "completion_tokens", "candidates_token_count", "output_tokens");
3130
+ const cachedTokens = numberFromUsage(usageData, "cached_tokens", "cached_prompt_tokens", "cache_read_input_tokens") || nestedNumberFromUsage(usageData, "prompt_tokens_details", "cached_tokens");
3131
+ this.tokenUsage = addUsageMetrics(this.tokenUsage, {
3132
+ ...emptyUsageMetrics(),
3133
+ totalTokens: promptTokens + completionTokens,
3134
+ promptTokens,
3135
+ completionTokens,
3136
+ cachedPromptTokens: cachedTokens,
3137
+ reasoningTokens: numberFromUsage(usageData, "reasoning_tokens"),
3138
+ cacheCreationTokens: numberFromUsage(usageData, "cache_creation_tokens"),
3139
+ successfulRequests: 1
3140
+ });
3141
+ }
3142
+ _track_token_usage_internal(usageData) {
3143
+ this.trackTokenUsageInternal(usageData);
3144
+ }
3145
+ getUsageMetrics() {
3146
+ return { ...this.tokenUsage };
3147
+ }
3148
+ getTokenUsageSummary() {
3149
+ return this.getUsageMetrics();
3150
+ }
3151
+ get_token_usage_summary() {
3152
+ return this.getTokenUsageSummary();
3153
+ }
3154
+ resetUsageMetrics() {
3155
+ this.tokenUsage = emptyUsageMetrics();
3156
+ }
3157
+ reset_usage_metrics() {
3158
+ this.resetUsageMetrics();
3159
+ }
3160
+ validateStructuredOutput(response, responseFormat = null) {
3161
+ return validateStructuredOutput(response, responseFormat);
3162
+ }
3163
+ _validate_structured_output(response, responseFormat = null) {
3164
+ return this.validateStructuredOutput(response, responseFormat);
3165
+ }
3166
+ static validateStructuredOutput(response, responseFormat = null) {
3167
+ return validateStructuredOutput(response, responseFormat);
3168
+ }
3169
+ static _validate_structured_output(response, responseFormat = null) {
3170
+ return validateStructuredOutput(response, responseFormat);
3171
+ }
3172
+ static validateInitFields(data) {
3173
+ if (!isRecord(data)) {
3174
+ return data;
3175
+ }
3176
+ const normalized = {};
3177
+ const extras = {};
3178
+ for (const [key, value] of Object.entries(data)) {
3179
+ if (BASE_LLM_OPTION_FIELDS.has(key)) {
3180
+ normalized[key] = value;
3181
+ } else {
3182
+ extras[key] = value;
3183
+ }
3184
+ }
3185
+ if (!normalized.model) {
3186
+ throw new Error("Model name is required and cannot be empty.");
3187
+ }
3188
+ const stopSequences = normalized.stop_sequences ?? normalized.stopSequences;
3189
+ const stop = stopSequences ?? normalized.stop;
3190
+ if (stop === null || stop === void 0) {
3191
+ normalized.stop = [];
3192
+ } else if (typeof stop === "string") {
3193
+ normalized.stop = [stop];
3194
+ } else if (Array.isArray(stop)) {
3195
+ normalized.stop = stop;
3196
+ } else if (isIterable(stop)) {
3197
+ normalized.stop = [...stop];
3198
+ }
3199
+ if (!normalized.provider) {
3200
+ normalized.provider = "openai";
3201
+ }
3202
+ normalized.additional_params = {
3203
+ ...isRecord(normalized.additionalParams) ? normalized.additionalParams : {},
3204
+ ...isRecord(normalized.additional_params) ? normalized.additional_params : {},
3205
+ ...extras
3206
+ };
3207
+ return normalized;
3208
+ }
3209
+ static _validate_init_fields(data) {
3210
+ return this.validateInitFields(data);
3211
+ }
3212
+ static validateLLMFields(data) {
3213
+ if (!isRecord(data)) {
3214
+ return data;
3215
+ }
3216
+ const model = typeof data.model === "string" ? data.model : "";
3217
+ return { ...data, is_anthropic: this.isAnthropicModel(model) };
3218
+ }
3219
+ static _validate_llm_fields(data) {
3220
+ return this.validateLLMFields(data);
3221
+ }
3222
+ initLitellm() {
3223
+ this.isLitellm = true;
3224
+ this.is_litellm = true;
3225
+ return this;
3226
+ }
3227
+ _init_litellm() {
3228
+ return this.initLitellm();
3229
+ }
3230
+ static isAnthropicModel(model) {
3231
+ const normalized = model.toLowerCase();
3232
+ return ANTHROPIC_PREFIXES.some((prefix) => normalized.includes(prefix));
3233
+ }
3234
+ static _is_anthropic_model(model) {
3235
+ return this.isAnthropicModel(model);
3236
+ }
3237
+ isAnthropicModel(model = this.model) {
3238
+ return _BaseLLM.isAnthropicModel(model);
3239
+ }
3240
+ _is_anthropic_model(model = this.model) {
3241
+ return this.isAnthropicModel(model);
3242
+ }
3243
+ static getNativeProvider(provider) {
3244
+ const normalized = canonicalLLMProvider(provider);
3245
+ if (normalized === "openai" || normalized === "anthropic" || normalized === "azure" || normalized === "bedrock" || normalized === "gemini" || normalized === "snowflake") {
3246
+ return normalized;
3247
+ }
3248
+ if (["openrouter", "deepseek", "ollama", "ollama_chat", "hosted_vllm", "cerebras", "dashscope"].includes(normalized)) {
3249
+ return "openai_compatible";
3250
+ }
3251
+ return null;
3252
+ }
3253
+ static _get_native_provider(provider) {
3254
+ return this.getNativeProvider(provider);
3255
+ }
3256
+ static usageToDict(usage) {
3257
+ if (usage === null || usage === void 0) {
3258
+ return null;
3259
+ }
3260
+ if (isRecord(usage)) {
3261
+ const modelDump = usage.model_dump ?? usage.modelDump;
3262
+ if (typeof modelDump === "function") {
3263
+ const dumped = modelDump();
3264
+ return isRecord(dumped) ? normalizeUsageDict(dumped) : null;
3265
+ }
3266
+ const result = {};
3267
+ for (const [key, value] of Object.entries(usage)) {
3268
+ if (!key.startsWith("_")) {
3269
+ result[key] = value;
3270
+ }
3271
+ }
3272
+ return normalizeUsageDict(result);
3273
+ }
3274
+ return null;
3275
+ }
3276
+ static _usage_to_dict(usage) {
3277
+ return this.usageToDict(usage);
3278
+ }
3279
+ usageToDict(usage) {
3280
+ return _BaseLLM.usageToDict(usage);
3281
+ }
3282
+ _usage_to_dict(usage) {
3283
+ return this.usageToDict(usage);
3284
+ }
3285
+ static extractProvider(model) {
3286
+ return extractProvider(model);
3287
+ }
3288
+ static _extract_provider(model) {
3289
+ return extractProvider(model);
3290
+ }
3291
+ static canonicalLLMProvider(provider) {
3292
+ return canonicalLLMProvider(provider);
3293
+ }
3294
+ static canonical_llm_provider(provider) {
3295
+ return canonicalLLMProvider(provider);
3296
+ }
3297
+ static matchesProviderPattern(model, provider) {
3298
+ return matchesProviderPattern(model, provider);
3299
+ }
3300
+ static _matches_provider_pattern(model, provider) {
3301
+ return matchesProviderPattern(model, provider);
3302
+ }
3303
+ static validateModelInConstants(model, provider) {
3304
+ return validateModelInConstants(model, provider);
3305
+ }
3306
+ static _validate_model_in_constants(model, provider) {
3307
+ return validateModelInConstants(model, provider);
3308
+ }
3309
+ static inferProviderFromModel(model) {
3310
+ return inferProviderFromModel(model);
3311
+ }
3312
+ static _infer_provider_from_model(model) {
3313
+ return inferProviderFromModel(model);
3314
+ }
3315
+ static resolveLLMModelSpec(model, explicitProvider) {
3316
+ return resolveLLMModelSpec(model, explicitProvider);
3317
+ }
3318
+ static resolve_llm_model_spec(model, explicitProvider) {
3319
+ return resolveLLMModelSpec(model, explicitProvider);
3320
+ }
3321
+ static setCallbacks(callbacks) {
3322
+ const callbackTypes = new Set(
3323
+ callbacks.filter((callback) => typeof callback === "object" || typeof callback === "function").map((callback) => callback?.constructor)
3324
+ );
3325
+ configuredSuccessCallbacks = configuredSuccessCallbacks.filter((callback) => !callbackTypes.has(callback?.constructor));
3326
+ configuredCallbacks = [...callbacks];
3327
+ }
3328
+ static set_callbacks(callbacks) {
3329
+ this.setCallbacks(callbacks);
3330
+ }
3331
+ static setEnvCallbacks(env = process.env) {
3332
+ const successCallbacks = parseCallbackNames(env.LITELLM_SUCCESS_CALLBACKS);
3333
+ const failureCallbacks = parseCallbackNames(env.LITELLM_FAILURE_CALLBACKS);
3334
+ if (successCallbacks.length > 0 || failureCallbacks.length > 0) {
3335
+ configuredSuccessCallbacks = successCallbacks;
3336
+ configuredFailureCallbacks = failureCallbacks;
3337
+ }
3338
+ }
3339
+ static set_env_callbacks(env = process.env) {
3340
+ this.setEnvCallbacks(env);
3341
+ }
3342
+ static get callbacks() {
3343
+ return configuredCallbacks;
3344
+ }
3345
+ static get successCallbacks() {
3346
+ return configuredSuccessCallbacks;
3347
+ }
3348
+ static get success_callbacks() {
3349
+ return configuredSuccessCallbacks;
3350
+ }
3351
+ static get failureCallbacks() {
3352
+ return configuredFailureCallbacks;
3353
+ }
3354
+ static get failure_callbacks() {
3355
+ return configuredFailureCallbacks;
3356
+ }
3357
+ };
3358
+ var ConfiguredLLM = class extends BaseLLM {
3359
+ timeout;
3360
+ maxTokens;
3361
+ max_tokens;
3362
+ maxCompletionTokens;
3363
+ max_completion_tokens;
3364
+ topP;
3365
+ top_p;
3366
+ presencePenalty;
3367
+ presence_penalty;
3368
+ frequencyPenalty;
3369
+ frequency_penalty;
3370
+ seed;
3371
+ logprobs;
3372
+ topLogprobs;
3373
+ top_logprobs;
3374
+ apiBase;
3375
+ api_base;
3376
+ constructor(options) {
3377
+ super(options);
3378
+ this.timeout = options.timeout ?? null;
3379
+ this.maxTokens = options.maxTokens ?? options.max_tokens ?? null;
3380
+ this.max_tokens = this.maxTokens;
3381
+ this.maxCompletionTokens = options.maxCompletionTokens ?? options.max_completion_tokens ?? null;
3382
+ this.max_completion_tokens = this.maxCompletionTokens;
3383
+ this.topP = options.topP ?? options.top_p ?? null;
3384
+ this.top_p = this.topP;
3385
+ this.presencePenalty = options.presencePenalty ?? options.presence_penalty ?? null;
3386
+ this.presence_penalty = this.presencePenalty;
3387
+ this.frequencyPenalty = options.frequencyPenalty ?? options.frequency_penalty ?? null;
3388
+ this.frequency_penalty = this.frequencyPenalty;
3389
+ this.seed = options.seed ?? null;
3390
+ this.logprobs = options.logprobs ?? null;
3391
+ this.topLogprobs = options.topLogprobs ?? options.top_logprobs ?? null;
3392
+ this.top_logprobs = this.topLogprobs;
3393
+ this.apiBase = options.apiBase ?? options.api_base ?? this.baseUrl;
3394
+ this.api_base = this.apiBase;
3395
+ }
3396
+ async call(messages, options) {
3397
+ const provider = resolveLLMProvider(this.model);
3398
+ if (!provider || provider === this) {
3399
+ throw new Error(`No LLM provider registered for model '${this.model}'.`);
3400
+ }
3401
+ return await provider.call(messages, options);
3402
+ }
3403
+ supportsMultimodal() {
3404
+ const model = this.model.toLowerCase();
3405
+ const textOnlyModels = ["o3-mini", "o1-mini"];
3406
+ if (textOnlyModels.some((name) => model.startsWith(name) || model.includes(`/${name}`))) {
3407
+ return false;
3408
+ }
3409
+ return [
3410
+ "gpt-4o",
3411
+ "gpt-4-turbo",
3412
+ "gpt-4-vision",
3413
+ "gpt-4.1",
3414
+ "gpt-5",
3415
+ "o1",
3416
+ "o3",
3417
+ "o4-mini",
3418
+ "o4",
3419
+ "claude-3",
3420
+ "claude-4",
3421
+ "claude-sonnet-4",
3422
+ "claude-opus-4",
3423
+ "claude-haiku-4",
3424
+ "gemini",
3425
+ "grok",
3426
+ "pixtral",
3427
+ "llava",
3428
+ "qwen-vl",
3429
+ "qwen2-vl",
3430
+ "qwen3-vl"
3431
+ ].some((prefix) => model.startsWith(prefix) || model.includes(`/${prefix}`));
3432
+ }
3433
+ supports_multimodal() {
3434
+ return this.supportsMultimodal();
3435
+ }
3436
+ toConfigDict() {
3437
+ return {
3438
+ ...super.toConfigDict(),
3439
+ ...this.timeout === null ? {} : { timeout: this.timeout },
3440
+ ...this.maxTokens === null ? {} : { max_tokens: this.maxTokens },
3441
+ ...this.maxCompletionTokens === null ? {} : { max_completion_tokens: this.maxCompletionTokens },
3442
+ ...this.topP === null ? {} : { top_p: this.topP },
3443
+ ...this.presencePenalty === null ? {} : { presence_penalty: this.presencePenalty },
3444
+ ...this.frequencyPenalty === null ? {} : { frequency_penalty: this.frequencyPenalty },
3445
+ ...this.seed === null ? {} : { seed: this.seed },
3446
+ ...this.logprobs === null ? {} : { logprobs: this.logprobs },
3447
+ ...this.topLogprobs === null ? {} : { top_logprobs: this.topLogprobs },
3448
+ ...this.apiBase === null ? {} : { api_base: this.apiBase }
3449
+ };
3450
+ }
3451
+ };
3452
+ function createLLMClient(provider) {
3453
+ return typeof provider === "function" ? new FunctionLLM(provider) : provider;
3454
+ }
3455
+ function registerLLMProvider(model, provider) {
3456
+ registeredProviders.set(model, createLLMClient(provider));
3457
+ }
3458
+ function registerLLMProviderFactory(provider, factory) {
3459
+ registeredProviderFactories.set(canonicalLLMProvider(provider), factory);
3460
+ }
3461
+ function unregisterLLMProviderFactory(provider) {
3462
+ registeredProviderFactories.delete(canonicalLLMProvider(provider));
3463
+ }
3464
+ function unregisterLLMProvider(model) {
3465
+ registeredProviders.delete(model);
3466
+ }
3467
+ function clearLLMProviders() {
3468
+ registeredProviders.clear();
3469
+ }
3470
+ function resolveLLMProvider(model) {
3471
+ return registeredProviders.get(model) ?? null;
3472
+ }
3473
+ function createConfiguredLLM(options) {
3474
+ const provider = typeof options.provider === "string" ? canonicalLLMProvider(options.provider) : null;
3475
+ const factory = provider ? registeredProviderFactories.get(provider) : void 0;
3476
+ if (!factory) {
3477
+ return new ConfiguredLLM(options);
3478
+ }
3479
+ try {
3480
+ return factory(options);
3481
+ } catch (error) {
3482
+ const message = error instanceof Error ? error.message : String(error);
3483
+ throw new Error(`Error importing native provider '${provider ?? "unknown"}': ${message}`, { cause: error });
3484
+ }
3485
+ }
3486
+ async function callLLM(client, messages, options = {}) {
3487
+ return await llmCallContext(async (callId) => {
3488
+ const mutableMessages = messages;
3489
+ const model = typeof options.metadata?.model === "string" ? options.metadata.model : null;
3490
+ crewaiEventBus.emit(client, new LLMCallStartedEvent({
3491
+ call_id: callId,
3492
+ from_agent: options.metadata?.agent,
3493
+ from_task: options.metadata?.task,
3494
+ model,
3495
+ messages: serializeLLMMessages(mutableMessages),
3496
+ tools: serializeLLMTools(options.tools),
3497
+ available_functions: options.availableFunctions ?? options.available_functions ?? null
3498
+ }));
3499
+ const context = new LLMCallHookContext({
3500
+ messages: mutableMessages,
3501
+ llm: client,
3502
+ agent: options.metadata?.agent,
3503
+ task: options.metadata?.task,
3504
+ crew: options.metadata?.crew,
3505
+ iterations: typeof options.metadata?.iterations === "number" ? options.metadata.iterations : 0
3506
+ });
3507
+ try {
3508
+ await runBeforeLlmCallHooks(context);
3509
+ const response = await client.call(mutableMessages, options);
3510
+ context.response = response;
3511
+ const finalResponse = await runAfterLlmCallHooks(context);
3512
+ crewaiEventBus.emit(client, new LLMCallCompletedEvent({
3513
+ call_id: callId,
3514
+ from_agent: options.metadata?.agent,
3515
+ from_task: options.metadata?.task,
3516
+ model,
3517
+ messages: serializeLLMMessages(mutableMessages),
3518
+ response: finalResponse,
3519
+ call_type: isToolCallingResponse(finalResponse) ? LLMCallType.TOOL_CALL : LLMCallType.LLM_CALL,
3520
+ usage: client.getUsageMetrics?.() ?? client.getTokenUsageSummary?.() ?? client.get_token_usage_summary?.() ?? null
3521
+ }));
3522
+ return finalResponse;
3523
+ } catch (error) {
3524
+ crewaiEventBus.emit(client, new LLMCallFailedEvent({
3525
+ call_id: callId,
3526
+ from_agent: options.metadata?.agent,
3527
+ from_task: options.metadata?.task,
3528
+ model,
3529
+ error
3530
+ }));
3531
+ throw error;
3532
+ }
3533
+ });
3534
+ }
3535
+ function getLLMUsageMetrics(client) {
3536
+ if (client.getUsageMetrics) {
3537
+ return normalizeUsageMetrics(client.getUsageMetrics());
3538
+ }
3539
+ if (client.getTokenUsageSummary) {
3540
+ return normalizeUsageMetrics(client.getTokenUsageSummary());
3541
+ }
3542
+ if (client.get_token_usage_summary) {
3543
+ return normalizeUsageMetrics(client.get_token_usage_summary());
3544
+ }
3545
+ return emptyUsageMetrics();
3546
+ }
3547
+ function hasLLMUsageMetrics(client) {
3548
+ return "getUsageMetrics" in client || "getTokenUsageSummary" in client || "get_token_usage_summary" in client;
3549
+ }
3550
+ function markCacheBreakpoint(message) {
3551
+ return { ...message, cache_breakpoint: true };
3552
+ }
3553
+ var mark_cache_breakpoint = markCacheBreakpoint;
3554
+ function stripCacheBreakpoint(message) {
3555
+ delete message.cache_breakpoint;
3556
+ }
3557
+ var strip_cache_breakpoint = stripCacheBreakpoint;
3558
+ function emptyUsageMetrics() {
3559
+ return new UsageMetrics();
3560
+ }
3561
+ function addUsageMetrics(left, right) {
3562
+ return new UsageMetrics({
3563
+ totalTokens: left.totalTokens + right.totalTokens,
3564
+ promptTokens: left.promptTokens + right.promptTokens,
3565
+ cachedPromptTokens: left.cachedPromptTokens + right.cachedPromptTokens,
3566
+ completionTokens: left.completionTokens + right.completionTokens,
3567
+ reasoningTokens: left.reasoningTokens + right.reasoningTokens,
3568
+ cacheCreationTokens: left.cacheCreationTokens + right.cacheCreationTokens,
3569
+ successfulRequests: left.successfulRequests + right.successfulRequests
3570
+ });
3571
+ }
3572
+ function subtractUsageMetrics(left, right) {
3573
+ return new UsageMetrics({
3574
+ totalTokens: Math.max(0, left.totalTokens - right.totalTokens),
3575
+ promptTokens: Math.max(0, left.promptTokens - right.promptTokens),
3576
+ cachedPromptTokens: Math.max(0, left.cachedPromptTokens - right.cachedPromptTokens),
3577
+ completionTokens: Math.max(0, left.completionTokens - right.completionTokens),
3578
+ reasoningTokens: Math.max(0, left.reasoningTokens - right.reasoningTokens),
3579
+ cacheCreationTokens: Math.max(0, left.cacheCreationTokens - right.cacheCreationTokens),
3580
+ successfulRequests: Math.max(0, left.successfulRequests - right.successfulRequests)
3581
+ });
3582
+ }
3583
+ function isEmptyUsageMetrics(metrics) {
3584
+ return Object.values(metrics).every((value) => value === 0);
3585
+ }
3586
+ function estimateUsageMetrics(messages, response) {
3587
+ const promptTokens = messages.reduce(
3588
+ (total, message) => total + estimateTokens(message.content),
3589
+ 0
3590
+ );
3591
+ const completionText = typeof response === "string" ? response : JSON.stringify(response);
3592
+ const completionTokens = estimateTokens(completionText);
3593
+ return {
3594
+ ...emptyUsageMetrics(),
3595
+ totalTokens: promptTokens + completionTokens,
3596
+ promptTokens,
3597
+ completionTokens,
3598
+ successfulRequests: 1
3599
+ };
3600
+ }
3601
+ function estimateTokens(value) {
3602
+ const trimmed = value.trim();
3603
+ if (!trimmed) {
3604
+ return 0;
3605
+ }
3606
+ return Math.max(1, Math.ceil(trimmed.length / 4));
3607
+ }
3608
+ function validateStructuredOutput(response, responseFormat = null) {
3609
+ if (responseFormat === null) {
3610
+ return response;
3611
+ }
3612
+ const data = parseStructuredOutputJson(response, validatorName(responseFormat));
3613
+ if (typeof responseFormat.modelValidate === "function") {
3614
+ return responseFormat.modelValidate(data);
3615
+ }
3616
+ if (typeof responseFormat.model_validate === "function") {
3617
+ return responseFormat.model_validate(data);
3618
+ }
3619
+ if (typeof responseFormat.parse === "function") {
3620
+ return responseFormat.parse(data);
3621
+ }
3622
+ if (typeof responseFormat.validate === "function") {
3623
+ return responseFormat.validate(data);
3624
+ }
3625
+ return data;
3626
+ }
3627
+ var validate_structured_output = validateStructuredOutput;
3628
+ function extractProvider(model) {
3629
+ const index = model.indexOf("/");
3630
+ return index === -1 ? "openai" : model.slice(0, index);
3631
+ }
3632
+ var extract_provider = extractProvider;
3633
+ function canonicalLLMProvider(provider) {
3634
+ return LLM_PROVIDER_ALIASES[provider.toLowerCase()] ?? provider.toLowerCase();
3635
+ }
3636
+ var canonical_llm_provider = canonicalLLMProvider;
3637
+ function matchesProviderPattern(model, provider) {
3638
+ const modelLower = model.toLowerCase();
3639
+ const canonicalProvider = canonicalLLMProvider(provider);
3640
+ if (canonicalProvider === "openai") {
3641
+ return ["gpt-", "o1", "o3", "o4", "whisper-"].some((prefix) => modelLower.startsWith(prefix));
3642
+ }
3643
+ if (canonicalProvider === "anthropic") {
3644
+ return ["claude-", "anthropic."].some((prefix) => modelLower.startsWith(prefix));
3645
+ }
3646
+ if (canonicalProvider === "gemini") {
3647
+ return ["gemini-", "gemma-", "learnlm-"].some((prefix) => modelLower.startsWith(prefix));
3648
+ }
3649
+ if (canonicalProvider === "bedrock") {
3650
+ return modelLower.includes(".");
3651
+ }
3652
+ if (canonicalProvider === "azure") {
3653
+ return ["gpt-", "gpt-35-", "o1", "o3", "o4", "azure-"].some((prefix) => modelLower.startsWith(prefix));
3654
+ }
3655
+ if (canonicalProvider === "deepseek") {
3656
+ return modelLower.startsWith("deepseek");
3657
+ }
3658
+ if (canonicalProvider === "dashscope") {
3659
+ return modelLower.startsWith("qwen");
3660
+ }
3661
+ return ["ollama", "ollama_chat", "hosted_vllm", "cerebras", "openrouter"].includes(canonicalProvider);
3662
+ }
3663
+ var matches_provider_pattern = matchesProviderPattern;
3664
+ function validateModelInConstants(model, provider) {
3665
+ const canonicalProvider = canonicalLLMProvider(provider);
3666
+ if (canonicalProvider === "openai" && openAIModelSet.has(model)) {
3667
+ return true;
3668
+ }
3669
+ if (canonicalProvider === "anthropic" && anthropicModelSet.has(model)) {
3670
+ return true;
3671
+ }
3672
+ if (canonicalProvider === "bedrock" && bedrockModelSet.has(model)) {
3673
+ return true;
3674
+ }
3675
+ if (canonicalProvider === "azure") {
3676
+ return true;
3677
+ }
3678
+ if (canonicalProvider === "snowflake") {
3679
+ return true;
3680
+ }
3681
+ return matchesProviderPattern(model, canonicalProvider);
3682
+ }
3683
+ var validate_model_in_constants = validateModelInConstants;
3684
+ function inferProviderFromModel(model) {
3685
+ if (openAIModelSet.has(model)) {
3686
+ return "openai";
3687
+ }
3688
+ if (anthropicModelSet.has(model)) {
3689
+ return "anthropic";
3690
+ }
3691
+ if (matchesProviderPattern(model, "gemini")) {
3692
+ return "gemini";
3693
+ }
3694
+ if (bedrockModelSet.has(model)) {
3695
+ return "bedrock";
3696
+ }
3697
+ if (azureModelSet.has(model)) {
3698
+ return "azure";
3699
+ }
3700
+ return "openai";
3701
+ }
3702
+ var infer_provider_from_model = inferProviderFromModel;
3703
+ function resolveLLMModelSpec(model, explicitProvider) {
3704
+ if (!model) {
3705
+ throw new Error("Model must be a non-empty string.");
3706
+ }
3707
+ if (explicitProvider) {
3708
+ return {
3709
+ provider: canonicalLLMProvider(explicitProvider),
3710
+ model,
3711
+ originalModel: model,
3712
+ useNative: true
3713
+ };
3714
+ }
3715
+ const separatorIndex = model.indexOf("/");
3716
+ if (separatorIndex !== -1) {
3717
+ const prefix = model.slice(0, separatorIndex);
3718
+ const modelPart = model.slice(separatorIndex + 1);
3719
+ const canonicalProvider = canonicalLLMProvider(prefix);
3720
+ const hasNativeProvider = nativeProviderSet.has(canonicalProvider);
3721
+ const useNative = hasNativeProvider && validateModelInConstants(modelPart, canonicalProvider);
3722
+ return {
3723
+ provider: useNative ? canonicalProvider : prefix,
3724
+ model: modelPart,
3725
+ originalModel: model,
3726
+ useNative
3727
+ };
3728
+ }
3729
+ return {
3730
+ provider: inferProviderFromModel(model),
3731
+ model,
3732
+ originalModel: model,
3733
+ useNative: true
3734
+ };
3735
+ }
3736
+ var resolve_llm_model_spec = resolveLLMModelSpec;
3737
+ function contextWindowSizeForModel(model) {
3738
+ validateContextWindowSizes(LLM_CONTEXT_WINDOW_SIZES);
3739
+ return knownContextWindowSizeForModel(model) ?? Math.trunc(DEFAULT_CONTEXT_WINDOW_SIZE * CONTEXT_WINDOW_USAGE_RATIO);
3740
+ }
3741
+ var context_window_size_for_model = contextWindowSizeForModel;
3742
+ function validateContextWindowSizes(sizes) {
3743
+ for (const [model, size] of Object.entries(sizes)) {
3744
+ if (size < MIN_CONTEXT_WINDOW_SIZE || size > MAX_CONTEXT_WINDOW_SIZE) {
3745
+ throw new Error(`Context window for ${model} must be between ${String(MIN_CONTEXT_WINDOW_SIZE)} and ${String(MAX_CONTEXT_WINDOW_SIZE)}.`);
3746
+ }
3747
+ }
3748
+ }
3749
+ var validate_context_window_sizes = validateContextWindowSizes;
3750
+ function knownContextWindowSizeForModel(model) {
3751
+ const match = Object.entries(LLM_CONTEXT_WINDOW_SIZES).sort(([left], [right]) => right.length - left.length).find(([prefix]) => model.startsWith(prefix));
3752
+ return match ? Math.trunc(match[1] * CONTEXT_WINDOW_USAGE_RATIO) : null;
3753
+ }
3754
+ function normalizeUsageMetrics(metrics) {
3755
+ return new UsageMetrics({
3756
+ totalTokens: metrics.totalTokens ?? metrics.total_tokens ?? 0,
3757
+ promptTokens: metrics.promptTokens ?? metrics.prompt_tokens ?? 0,
3758
+ cachedPromptTokens: metrics.cachedPromptTokens ?? metrics.cached_prompt_tokens ?? 0,
3759
+ completionTokens: metrics.completionTokens ?? metrics.completion_tokens ?? 0,
3760
+ reasoningTokens: metrics.reasoningTokens ?? metrics.reasoning_tokens ?? 0,
3761
+ cacheCreationTokens: metrics.cacheCreationTokens ?? metrics.cache_creation_tokens ?? 0,
3762
+ successfulRequests: metrics.successfulRequests ?? metrics.successful_requests ?? 0
3763
+ });
3764
+ }
3765
+ function normalizeUsageDict(usage) {
3766
+ const result = { ...usage };
3767
+ const promptDetails = isRecord(result.prompt_tokens_details) ? result.prompt_tokens_details : null;
3768
+ const completionDetails = isRecord(result.completion_tokens_details) ? result.completion_tokens_details : null;
3769
+ const cachedPromptTokens = firstDefined(
3770
+ result.cached_tokens,
3771
+ result.cached_prompt_tokens,
3772
+ result.cache_read_input_tokens,
3773
+ promptDetails?.cached_tokens
3774
+ );
3775
+ if (cachedPromptTokens !== void 0) {
3776
+ result.cached_prompt_tokens = cachedPromptTokens;
3777
+ }
3778
+ const reasoningTokens = firstDefined(result.reasoning_tokens, completionDetails?.reasoning_tokens);
3779
+ if (reasoningTokens !== void 0) {
3780
+ result.reasoning_tokens = reasoningTokens;
3781
+ }
3782
+ const cacheCreationTokens = firstDefined(result.cache_creation_tokens, result.cache_creation_input_tokens);
3783
+ if (cacheCreationTokens !== void 0) {
3784
+ result.cache_creation_tokens = cacheCreationTokens;
3785
+ }
3786
+ return result;
3787
+ }
3788
+ function firstDefined(...values) {
3789
+ return values.find((value) => value !== null && value !== void 0);
3790
+ }
3791
+ function defineUsageMetricAliases(metrics) {
3792
+ Object.defineProperties(metrics, {
3793
+ total_tokens: usageMetricAlias("totalTokens"),
3794
+ prompt_tokens: usageMetricAlias("promptTokens"),
3795
+ cached_prompt_tokens: usageMetricAlias("cachedPromptTokens"),
3796
+ completion_tokens: usageMetricAlias("completionTokens"),
3797
+ reasoning_tokens: usageMetricAlias("reasoningTokens"),
3798
+ cache_creation_tokens: usageMetricAlias("cacheCreationTokens"),
3799
+ successful_requests: usageMetricAlias("successfulRequests")
3800
+ });
3801
+ }
3802
+ function usageMetricAlias(key) {
3803
+ return {
3804
+ enumerable: false,
3805
+ configurable: true,
3806
+ get() {
3807
+ return this[key];
3808
+ },
3809
+ set(value) {
3810
+ this[key] = value;
3811
+ }
3812
+ };
3813
+ }
3814
+ function parseStructuredOutputJson(response, responseFormatName) {
3815
+ const trimmed = response.trim();
3816
+ try {
3817
+ if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
3818
+ return JSON.parse(trimmed);
3819
+ }
3820
+ const match = /\{.*\}/s.exec(response);
3821
+ if (match) {
3822
+ return JSON.parse(match[0]);
3823
+ }
3824
+ throw new Error("No JSON found in response");
3825
+ } catch (error) {
3826
+ const detail = error instanceof Error ? error.message : String(error);
3827
+ throw new Error(`Failed to parse response into ${responseFormatName}: ${detail}`, { cause: error });
3828
+ }
3829
+ }
3830
+ function validatorName(validator) {
3831
+ return validator.name ?? "response_format";
3832
+ }
3833
+ function removeUndefinedValues(record) {
3834
+ return Object.fromEntries(Object.entries(record).filter(([, value]) => value !== null && value !== void 0));
3835
+ }
3836
+ function booleanOrNull(value) {
3837
+ return typeof value === "boolean" ? value : null;
3838
+ }
3839
+ async function* streamingChunksFromParams(params) {
3840
+ const source = params.chunks ?? params.stream ?? params.response;
3841
+ if (isAsyncIterable(source)) {
3842
+ for await (const chunk of source) {
3843
+ yield chunk;
3844
+ }
3845
+ return;
3846
+ }
3847
+ if (isIterable(source) && typeof source !== "string") {
3848
+ for (const chunk of source) {
3849
+ yield chunk;
3850
+ }
3851
+ return;
3852
+ }
3853
+ if (source !== void 0 && source !== null && source !== true && source !== false) {
3854
+ yield source;
3855
+ }
3856
+ }
3857
+ function isAsyncIterable(value) {
3858
+ return value !== null && value !== void 0 && typeof value[Symbol.asyncIterator] === "function";
3859
+ }
3860
+ function streamingChunkContent(chunk) {
3861
+ const direct = stringPropertyFromRecord(chunk, "content") ?? stringPropertyFromRecord(chunk, "text");
3862
+ if (direct !== null) {
3863
+ return direct;
3864
+ }
3865
+ const record = readLLMRecord(chunk);
3866
+ const deltaContent = stringPropertyFromRecord(record.delta, "content");
3867
+ if (deltaContent !== null) {
3868
+ return deltaContent;
3869
+ }
3870
+ const choices = Array.isArray(record.choices) ? record.choices : [];
3871
+ for (const choice of choices) {
3872
+ const choiceRecord = readLLMRecord(choice);
3873
+ const content = stringPropertyFromRecord(choiceRecord.delta, "content") ?? stringPropertyFromRecord(choiceRecord.message, "content");
3874
+ if (content !== null) {
3875
+ return content;
3876
+ }
3877
+ }
3878
+ return null;
3879
+ }
3880
+ function extractResponseText(response) {
3881
+ if (typeof response === "string") {
3882
+ return response;
3883
+ }
3884
+ const direct = stringPropertyFromRecord(response, "content") ?? stringPropertyFromRecord(response, "text");
3885
+ if (direct !== null) {
3886
+ return direct;
3887
+ }
3888
+ const record = readLLMRecord(response);
3889
+ const choices = Array.isArray(record.choices) ? record.choices : [];
3890
+ for (const choice of choices) {
3891
+ const content = stringPropertyFromRecord(readLLMRecord(choice).message, "content") ?? stringPropertyFromRecord(readLLMRecord(choice).delta, "content") ?? stringPropertyFromRecord(choice, "text");
3892
+ if (content !== null) {
3893
+ return content;
3894
+ }
3895
+ }
3896
+ return "";
3897
+ }
3898
+ function extractToolCallsFromResponse(response) {
3899
+ const record = readLLMRecord(response);
3900
+ const direct = record.tool_calls ?? readLLMRecord(record.message).tool_calls;
3901
+ if (Array.isArray(direct)) {
3902
+ return direct;
3903
+ }
3904
+ const choices = Array.isArray(record.choices) ? record.choices : [];
3905
+ const calls = [];
3906
+ for (const choice of choices) {
3907
+ const messageCalls = readLLMRecord(readLLMRecord(choice).message).tool_calls;
3908
+ const deltaCalls = readLLMRecord(readLLMRecord(choice).delta).tool_calls;
3909
+ for (const candidate of [messageCalls, deltaCalls]) {
3910
+ if (Array.isArray(candidate)) {
3911
+ calls.push(...candidate);
3912
+ }
3913
+ }
3914
+ }
3915
+ return calls;
3916
+ }
3917
+ function parseLLMToolCall(toolCall) {
3918
+ const record = readLLMRecord(toolCall);
3919
+ const functionRecord = readLLMRecord(record.function);
3920
+ const name = stringPropertyFromRecord(functionRecord, "name") ?? stringPropertyFromRecord(record, "name");
3921
+ if (name === null || name.length === 0) {
3922
+ return null;
3923
+ }
3924
+ return {
3925
+ name,
3926
+ args: parseToolArguments(functionRecord.arguments ?? record.arguments ?? record.args)
3927
+ };
3928
+ }
3929
+ function parseToolArguments(value) {
3930
+ if (isRecord(value)) {
3931
+ return value;
3932
+ }
3933
+ if (typeof value !== "string" || value.trim().length === 0) {
3934
+ return {};
3935
+ }
3936
+ try {
3937
+ const parsed = JSON.parse(value);
3938
+ return isRecord(parsed) ? parsed : {};
3939
+ } catch {
3940
+ return {};
3941
+ }
3942
+ }
3943
+ function usageRecordFromChunk(chunk) {
3944
+ const record = readLLMRecord(chunk);
3945
+ const usage = record.usage ?? readLLMRecord(record.model_extra).usage;
3946
+ return isRecord(usage) ? usage : null;
3947
+ }
3948
+ function messagesFromParams(params) {
3949
+ const messages = params.messages;
3950
+ if (!Array.isArray(messages)) {
3951
+ return null;
3952
+ }
3953
+ const normalized = [];
3954
+ for (const message of messages) {
3955
+ const record = readLLMRecord(message);
3956
+ if (isLLMRole(record.role) && typeof record.content === "string") {
3957
+ normalized.push({ role: record.role, content: record.content });
3958
+ }
3959
+ }
3960
+ return normalized;
3961
+ }
3962
+ function stringPropertyFromRecord(value, key) {
3963
+ const record = readLLMRecord(value);
3964
+ const property = record[key];
3965
+ return typeof property === "string" ? property : null;
3966
+ }
3967
+ function invokeUsageCallback(callback, usage) {
3968
+ if (typeof callback === "function") {
3969
+ return;
3970
+ }
3971
+ if (typeof callback.logSuccessEvent === "function") {
3972
+ void Promise.resolve(callback.logSuccessEvent({
3973
+ kwargs: {},
3974
+ responseObj: { usage },
3975
+ startTime: 0,
3976
+ endTime: 0
3977
+ }));
3978
+ }
3979
+ if (typeof callback.log_success_event === "function") {
3980
+ void Promise.resolve(callback.log_success_event({}, { usage }, 0, 0));
3981
+ }
3982
+ }
3983
+ function isRecord(value) {
3984
+ return typeof value === "object" && value !== null && !Array.isArray(value);
3985
+ }
3986
+ function isIterable(value) {
3987
+ return typeof value[Symbol.iterator] === "function";
3988
+ }
3989
+ function isJsonResponseFormat(value) {
3990
+ return isRecord(value) && value.type === "json_object";
3991
+ }
3992
+ function isStructuredOutputValidator(value) {
3993
+ return isRecord(value) && (typeof value.name === "string" || typeof value.modelValidate === "function" || typeof value.model_validate === "function" || typeof value.parse === "function" || typeof value.validate === "function");
3994
+ }
3995
+ function serializeResponseFormat(responseFormat) {
3996
+ if ("type" in responseFormat) {
3997
+ return { type: "json_object" };
3998
+ }
3999
+ return responseFormat.name ?? "response_format";
4000
+ }
4001
+ function normalizeStopSequences(value) {
4002
+ if (value === null || value === void 0) {
4003
+ return [];
4004
+ }
4005
+ return typeof value === "string" ? [value] : [...value];
4006
+ }
4007
+ function numberFromUsage(data, ...keys) {
4008
+ for (const key of keys) {
4009
+ const value = data[key];
4010
+ if (typeof value === "number") {
4011
+ return value;
4012
+ }
4013
+ }
4014
+ return 0;
4015
+ }
4016
+ function nestedNumberFromUsage(data, objectKey, valueKey) {
4017
+ const nested = data[objectKey];
4018
+ if (!nested || typeof nested !== "object" || Array.isArray(nested)) {
4019
+ return 0;
4020
+ }
4021
+ const value = nested[valueKey];
4022
+ return typeof value === "number" ? value : 0;
4023
+ }
4024
+ function serializeLLMMessages(messages) {
4025
+ return messages.map((message) => ({
4026
+ role: message.role,
4027
+ content: message.content,
4028
+ ...message.files === void 0 ? {} : { files: message.files },
4029
+ ...message.cache_breakpoint === void 0 ? {} : { cache_breakpoint: message.cache_breakpoint }
4030
+ }));
4031
+ }
4032
+ function formatInlineFileContentBlock(name, file) {
4033
+ const rendered = renderLLMInputFile(name, file);
4034
+ return {
4035
+ type: "file",
4036
+ source: "inline",
4037
+ name,
4038
+ filename: rendered.filename,
4039
+ content_type: rendered.contentType,
4040
+ content: rendered.content
4041
+ };
4042
+ }
4043
+ function formatUploadedFileContentBlock(name, upload) {
4044
+ return {
4045
+ type: "file",
4046
+ source: "upload",
4047
+ name,
4048
+ filename: upload.filename,
4049
+ file_id: upload.id,
4050
+ content_type: upload.contentType
4051
+ };
4052
+ }
4053
+ function typedMultimodalFiles(files) {
4054
+ const entries = Object.entries(files);
4055
+ if (entries.length === 0) {
4056
+ return null;
4057
+ }
4058
+ const result = {};
4059
+ for (const [name, file] of entries) {
4060
+ if (!isCrewAIFileInput(file)) {
4061
+ return null;
4062
+ }
4063
+ result[name] = file;
4064
+ }
4065
+ return result;
4066
+ }
4067
+ function isCrewAIFileInput(file) {
4068
+ return file instanceof AudioFile || file instanceof File || file instanceof ImageFile || file instanceof PDFFile || file instanceof TextFile || file instanceof VideoFile;
4069
+ }
4070
+ function llmApiMode(llm) {
4071
+ const api = llm.api;
4072
+ return api === "responses" || api === "completions" ? api : void 0;
4073
+ }
4074
+ function renderLLMInputFile(name, file) {
4075
+ if (typeof file === "string") {
4076
+ return {
4077
+ filename: basename2(file) || name,
4078
+ contentType: guessLLMFileContentType(file),
4079
+ content: readFileSync2(file, "utf8")
4080
+ };
4081
+ }
4082
+ if (hasLLMTextContent(file)) {
4083
+ return {
4084
+ filename: file.filename ?? (file.path ? basename2(file.path) : name),
4085
+ contentType: file.contentType ?? guessLLMFileContentType(file.filename ?? file.path ?? name),
4086
+ content: file.content
4087
+ };
4088
+ }
4089
+ if (isLLMReadableInputFile(file)) {
4090
+ const content = file.read();
4091
+ return {
4092
+ filename: file.filename ?? name,
4093
+ contentType: file.contentType ?? file.content_type ?? guessLLMFileContentType(file.filename ?? name),
4094
+ content: typeof content === "string" ? content : Buffer.from(content).toString("base64")
4095
+ };
4096
+ }
4097
+ if (hasLLMFilePath(file)) {
4098
+ return {
4099
+ filename: file.filename ?? (basename2(file.path) || name),
4100
+ contentType: file.contentType ?? guessLLMFileContentType(file.path),
4101
+ content: readFileSync2(file.path, "utf8")
4102
+ };
4103
+ }
4104
+ throw new Error(`Input file '${name}' requires either a path or text content.`);
4105
+ }
4106
+ function hasLLMTextContent(file) {
4107
+ return "content" in file && typeof file.content === "string";
4108
+ }
4109
+ function hasLLMFilePath(file) {
4110
+ return "path" in file && typeof file.path === "string";
4111
+ }
4112
+ function isLLMReadableInputFile(file) {
4113
+ return "read" in file && typeof file.read === "function";
4114
+ }
4115
+ function guessLLMFileContentType(path) {
4116
+ const lower = path.toLowerCase();
4117
+ if (lower.endsWith(".json")) {
4118
+ return "application/json";
4119
+ }
4120
+ if (lower.endsWith(".csv")) {
4121
+ return "text/csv";
4122
+ }
4123
+ if (lower.endsWith(".md") || lower.endsWith(".markdown")) {
4124
+ return "text/markdown";
4125
+ }
4126
+ if (lower.endsWith(".txt") || lower.endsWith(".log")) {
4127
+ return "text/plain";
4128
+ }
4129
+ if (lower.endsWith(".png")) {
4130
+ return "image/png";
4131
+ }
4132
+ if (lower.endsWith(".jpg") || lower.endsWith(".jpeg")) {
4133
+ return "image/jpeg";
4134
+ }
4135
+ if (lower.endsWith(".webp")) {
4136
+ return "image/webp";
4137
+ }
4138
+ return null;
4139
+ }
4140
+ function serializeLLMEventMessages(messages) {
4141
+ return typeof messages === "string" ? messages : serializeLLMMessages(messages);
4142
+ }
4143
+ function isLLMRole(value) {
4144
+ return value === "system" || value === "user" || value === "assistant" || value === "tool";
4145
+ }
4146
+ function isLLMClient(value) {
4147
+ if (!value || typeof value !== "object" || !("call" in value)) {
4148
+ return false;
4149
+ }
4150
+ return typeof value.call === "function";
4151
+ }
4152
+ function parseCallbackNames(value) {
4153
+ if (!value) {
4154
+ return [];
4155
+ }
4156
+ return value.split(",").map((callback) => callback.trim()).filter((callback) => callback.length > 0);
4157
+ }
4158
+ function stringProperty(value, key) {
4159
+ const property = value[key];
4160
+ return typeof property === "string" ? property : void 0;
4161
+ }
4162
+ function stringifyLLMValue(value) {
4163
+ const toString = value["toString"];
4164
+ const rendered = typeof toString === "function" && toString !== Object.prototype.toString ? toString.call(value) : Object.prototype.toString.call(value);
4165
+ return typeof rendered === "string" ? rendered : Object.prototype.toString.call(value);
4166
+ }
4167
+ function numberProperty(value, key) {
4168
+ const property = value[key];
4169
+ return typeof property === "number" ? property : null;
4170
+ }
4171
+ function booleanOrNumberProperty(value, key) {
4172
+ const property = value[key];
4173
+ return typeof property === "boolean" || typeof property === "number" ? property : null;
4174
+ }
4175
+ function recordOrNull(value) {
4176
+ return value && typeof value === "object" && !Array.isArray(value) ? value : null;
4177
+ }
4178
+ function stringOrEmpty(value) {
4179
+ return typeof value === "string" ? value : "";
4180
+ }
4181
+ function argsSchemaParameters(value) {
4182
+ const schemaProvider = value;
4183
+ const schema = schemaProvider?.model_json_schema?.() ?? schemaProvider?.modelJsonSchema?.();
4184
+ return recordOrNull(schema);
4185
+ }
4186
+ function applyLLMEnvVars(options, provider, env) {
4187
+ for (const spec of LLM_ENV_VARS[provider] ?? []) {
4188
+ const keyName = spec.key_name;
4189
+ if (typeof keyName === "string" && !UNACCEPTED_LLM_ENV_ATTRIBUTES.includes(keyName)) {
4190
+ const value = env[keyName];
4191
+ if (value) {
4192
+ const paramKey = normalizeLLMEnvKeyName(keyName.toLowerCase());
4193
+ assignLLMConfigParam(options, paramKey, value);
4194
+ }
4195
+ continue;
4196
+ }
4197
+ if (spec.default === true) {
4198
+ for (const [key, value] of Object.entries(spec)) {
4199
+ if (key !== "prompt" && key !== "key_name" && key !== "default" && typeof value === "string") {
4200
+ assignLLMConfigParam(options, key.toLowerCase(), value);
4201
+ }
4202
+ }
4203
+ }
4204
+ }
4205
+ }
4206
+ function assignLLMConfigParam(options, key, value) {
4207
+ if (key === "model") {
4208
+ options.model = value;
4209
+ } else if (key === "api_key") {
4210
+ options.api_key = value;
4211
+ } else if (key === "api_base") {
4212
+ options.api_base = value;
4213
+ options.base_url ??= value;
4214
+ } else if (key === "api_version") {
4215
+ options.additional_params = { ...options.additional_params ?? {}, api_version: value };
4216
+ } else if (key === "watsonx_url") {
4217
+ options.base_url = value;
4218
+ } else {
4219
+ options.additional_params = { ...options.additional_params ?? {}, [key]: value };
4220
+ }
4221
+ }
4222
+ async function invokeAvailableFunction(fn, args) {
4223
+ return typeof fn === "function" ? await fn(args) : await fn.run(args);
4224
+ }
4225
+ function stringifyToolExecutionResult(value) {
4226
+ if (typeof value === "string") {
4227
+ return value;
4228
+ }
4229
+ if (value === void 0) {
4230
+ return "undefined";
4231
+ }
4232
+ if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
4233
+ return value.toString();
4234
+ }
4235
+ if (typeof value === "symbol") {
4236
+ return value.description ?? "Symbol()";
4237
+ }
4238
+ if (typeof value === "function") {
4239
+ return `[function ${value.name || "anonymous"}]`;
4240
+ }
4241
+ return JSON.stringify(value);
4242
+ }
4243
+ function serializeLLMTools(tools) {
4244
+ if (!tools || tools.length === 0) {
4245
+ return null;
4246
+ }
4247
+ return tools.map((tool) => {
4248
+ if (isToolLike(tool)) {
4249
+ return {
4250
+ name: tool.name,
4251
+ description: tool.description ?? null,
4252
+ resultAsAnswer: tool.resultAsAnswer ?? false
4253
+ };
4254
+ }
4255
+ return recordOrNull(tool) ?? { value: String(tool) };
4256
+ });
4257
+ }
4258
+ function isToolLike(value) {
4259
+ const record = recordOrNull(value);
4260
+ return record !== null && typeof record.name === "string" && "run" in record;
4261
+ }
4262
+ function isToolCallingResponse(response) {
4263
+ return typeof response === "object" && "toolName" in response;
4264
+ }
4265
+
4266
+ export {
4267
+ ResolvedFile,
4268
+ InlineBase64,
4269
+ InlineBytes,
4270
+ FileReference,
4271
+ UrlReference,
4272
+ FileHandling,
4273
+ FileValidationError,
4274
+ FileTooLargeError,
4275
+ UnsupportedFileTypeError,
4276
+ FileProcessingError,
4277
+ ProcessingDependencyError,
4278
+ ImageConstraints,
4279
+ PDFConstraints,
4280
+ AudioConstraints,
4281
+ VideoConstraints,
4282
+ TextConstraints,
4283
+ ProviderConstraints,
4284
+ ANTHROPIC_CONSTRAINTS,
4285
+ OPENAI_RESPONSES_CONSTRAINTS,
4286
+ OPENAI_CONSTRAINTS,
4287
+ GEMINI_CONSTRAINTS,
4288
+ BEDROCK_CONSTRAINTS,
4289
+ AZURE_CONSTRAINTS,
4290
+ getConstraintsForProvider,
4291
+ get_constraints_for_provider,
4292
+ usesOpenAIResponsesApi,
4293
+ uses_openai_responses_api,
4294
+ getSupportedContentTypes,
4295
+ validateImage,
4296
+ validate_image,
4297
+ validatePDF,
4298
+ validate_pdf,
4299
+ validateAudio,
4300
+ validate_audio,
4301
+ validateVideo,
4302
+ validate_video,
4303
+ validateText,
4304
+ validate_text,
4305
+ validateFile,
4306
+ validate_file,
4307
+ FileProcessor,
4308
+ chunkText,
4309
+ chunk_text,
4310
+ chunkPDF,
4311
+ chunk_pdf,
4312
+ resizeImage,
4313
+ resize_image,
4314
+ optimizeImage,
4315
+ optimize_image,
4316
+ CachedUpload,
4317
+ UploadCache,
4318
+ getUploadCache,
4319
+ get_upload_cache,
4320
+ resetUploadCache,
4321
+ reset_upload_cache,
4322
+ FileSource,
4323
+ FilePath,
4324
+ FileBytes,
4325
+ FileStream,
4326
+ FileUrl,
4327
+ BaseFile,
4328
+ ImageFile,
4329
+ PDFFile,
4330
+ TextFile,
4331
+ AudioFile,
4332
+ VideoFile,
4333
+ File,
4334
+ detectContentType,
4335
+ detect_content_type,
4336
+ wrapFileSource,
4337
+ wrap_file_source,
4338
+ normalizeInputFiles,
4339
+ normalize_input_files,
4340
+ FileResolverConfig,
4341
+ FileResolver,
4342
+ createResolver,
4343
+ create_resolver,
4344
+ formatMultimodalContent,
4345
+ format_multimodal_content,
4346
+ JsonResponseFormat,
4347
+ CACHE_BREAKPOINT_KEY,
4348
+ UsageMetrics,
4349
+ LocalFileUploader,
4350
+ DEFAULT_CONTEXT_WINDOW_SIZE,
4351
+ DEFAULT_SUPPORTS_STOP_WORDS,
4352
+ DEFAULT_LLM_MODEL,
4353
+ JSON_URL,
4354
+ LITELLM_PARAMS,
4355
+ UNACCEPTED_LLM_ENV_ATTRIBUTES,
4356
+ LLM_ENV_VARS,
4357
+ ENV_VARS,
4358
+ SUPPORTED_NATIVE_PROVIDERS,
4359
+ LLM_PROVIDER_ALIASES,
4360
+ PROVIDERS,
4361
+ OPENAI_MODELS,
4362
+ ANTHROPIC_MODELS,
4363
+ AZURE_MODELS,
4364
+ BEDROCK_MODELS,
4365
+ NVIDIA_NIM_MODELS,
4366
+ GROQ_MODELS,
4367
+ OLLAMA_MODELS,
4368
+ WATSON_MODELS,
4369
+ HUGGINGFACE_MODELS,
4370
+ SAMBANOVA_MODELS,
4371
+ OpenAIModels,
4372
+ AnthropicModels,
4373
+ AzureModels,
4374
+ BedrockModels,
4375
+ NvidiaNimModels,
4376
+ GroqModels,
4377
+ OllamaModels,
4378
+ WatsonModels,
4379
+ HuggingFaceModels,
4380
+ SambanovaModels,
4381
+ MODELS,
4382
+ CONTEXT_WINDOW_USAGE_RATIO,
4383
+ MIN_CONTEXT_WINDOW_SIZE,
4384
+ MAX_CONTEXT_WINDOW_SIZE,
4385
+ MIN_CONTEXT,
4386
+ MAX_CONTEXT,
4387
+ ANTHROPIC_PREFIXES,
4388
+ Delta,
4389
+ StreamingChoices,
4390
+ FunctionArgs,
4391
+ AccumulatedToolArgs,
4392
+ LLM_CONTEXT_WINDOW_SIZES,
4393
+ llmCallContext,
4394
+ llm_call_context,
4395
+ validate_function_name,
4396
+ sanitize_function_name,
4397
+ extract_tool_info,
4398
+ log_tool_conversion,
4399
+ safe_tool_conversion,
4400
+ getCurrentCallId,
4401
+ get_current_call_id,
4402
+ callStopOverride,
4403
+ call_stop_override,
4404
+ callStopOverrideSync,
4405
+ call_stop_override_sync,
4406
+ FunctionLLM,
4407
+ createLLM,
4408
+ create_llm,
4409
+ llmViaEnvironmentOrFallback,
4410
+ llm_via_environment_or_fallback,
4411
+ _llm_via_environment_or_fallback,
4412
+ normalizeLLMEnvKeyName,
4413
+ normalize_llm_env_key_name,
4414
+ _normalize_key_name,
4415
+ BaseLLM,
4416
+ ConfiguredLLM,
4417
+ createLLMClient,
4418
+ registerLLMProvider,
4419
+ registerLLMProviderFactory,
4420
+ unregisterLLMProviderFactory,
4421
+ unregisterLLMProvider,
4422
+ clearLLMProviders,
4423
+ resolveLLMProvider,
4424
+ callLLM,
4425
+ getLLMUsageMetrics,
4426
+ hasLLMUsageMetrics,
4427
+ markCacheBreakpoint,
4428
+ mark_cache_breakpoint,
4429
+ stripCacheBreakpoint,
4430
+ strip_cache_breakpoint,
4431
+ emptyUsageMetrics,
4432
+ addUsageMetrics,
4433
+ subtractUsageMetrics,
4434
+ isEmptyUsageMetrics,
4435
+ estimateUsageMetrics,
4436
+ estimateTokens,
4437
+ validateStructuredOutput,
4438
+ validate_structured_output,
4439
+ extractProvider,
4440
+ extract_provider,
4441
+ canonicalLLMProvider,
4442
+ canonical_llm_provider,
4443
+ matchesProviderPattern,
4444
+ matches_provider_pattern,
4445
+ validateModelInConstants,
4446
+ validate_model_in_constants,
4447
+ inferProviderFromModel,
4448
+ infer_provider_from_model,
4449
+ resolveLLMModelSpec,
4450
+ resolve_llm_model_spec,
4451
+ contextWindowSizeForModel,
4452
+ context_window_size_for_model,
4453
+ validateContextWindowSizes,
4454
+ validate_context_window_sizes
4455
+ };