@mastra/rag 0.1.19-alpha.2 → 0.1.19-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,12 +1,3978 @@
1
- import { Document, SummaryExtractor, QuestionsAnsweredExtractor, KeywordExtractor, TitleExtractor, ObjectType, NodeRelationship, IngestionPipeline } from 'llamaindex';
1
+ import { Document, ObjectType, NodeRelationship, IngestionPipeline, BaseExtractor, PromptTemplate, defaultSummaryPrompt, TextNode, defaultQuestionExtractPrompt, defaultKeywordExtractPrompt, MetadataMode, defaultTitleExtractorPromptTemplate, defaultTitleCombinePromptTemplate } from 'llamaindex';
2
+ import { z } from 'zod';
2
3
  import { parse } from 'node-html-better-parser';
3
4
  import { encodingForModel, getEncoding } from 'js-tiktoken';
4
5
  import { CohereRelevanceScorer, MastraAgentRelevanceScorer } from '@mastra/core/relevance';
5
6
  import { createTool } from '@mastra/core/tools';
6
- import { z } from 'zod';
7
7
  import { embed } from 'ai';
8
8
 
9
- // src/document/document.ts
9
+ var __create = Object.create;
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __getProtoOf = Object.getPrototypeOf;
14
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
15
+ var __commonJS = (cb, mod) => function __require() {
16
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
27
+ // If the importer is in node compatibility mode or this is not an ESM
28
+ // file that has been converted to a CommonJS file using a Babel-
29
+ // compatible transform (i.e. "__esModule" has not been set), then set
30
+ // "default" to the CommonJS "module.exports" for node compatibility.
31
+ __defProp(target, "default", { value: mod, enumerable: true }) ,
32
+ mod
33
+ ));
34
+
35
+ // ../../node_modules/.pnpm/secure-json-parse@2.7.0/node_modules/secure-json-parse/index.js
36
+ var require_secure_json_parse = __commonJS({
37
+ "../../node_modules/.pnpm/secure-json-parse@2.7.0/node_modules/secure-json-parse/index.js"(exports, module) {
38
+ var hasBuffer = typeof Buffer !== "undefined";
39
+ var suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/;
40
+ var suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/;
41
+ function _parse(text, reviver, options) {
42
+ if (options == null) {
43
+ if (reviver !== null && typeof reviver === "object") {
44
+ options = reviver;
45
+ reviver = void 0;
46
+ }
47
+ }
48
+ if (hasBuffer && Buffer.isBuffer(text)) {
49
+ text = text.toString();
50
+ }
51
+ if (text && text.charCodeAt(0) === 65279) {
52
+ text = text.slice(1);
53
+ }
54
+ const obj = JSON.parse(text, reviver);
55
+ if (obj === null || typeof obj !== "object") {
56
+ return obj;
57
+ }
58
+ const protoAction = options && options.protoAction || "error";
59
+ const constructorAction = options && options.constructorAction || "error";
60
+ if (protoAction === "ignore" && constructorAction === "ignore") {
61
+ return obj;
62
+ }
63
+ if (protoAction !== "ignore" && constructorAction !== "ignore") {
64
+ if (suspectProtoRx.test(text) === false && suspectConstructorRx.test(text) === false) {
65
+ return obj;
66
+ }
67
+ } else if (protoAction !== "ignore" && constructorAction === "ignore") {
68
+ if (suspectProtoRx.test(text) === false) {
69
+ return obj;
70
+ }
71
+ } else {
72
+ if (suspectConstructorRx.test(text) === false) {
73
+ return obj;
74
+ }
75
+ }
76
+ return filter(obj, { protoAction, constructorAction, safe: options && options.safe });
77
+ }
78
+ function filter(obj, { protoAction = "error", constructorAction = "error", safe } = {}) {
79
+ let next = [obj];
80
+ while (next.length) {
81
+ const nodes = next;
82
+ next = [];
83
+ for (const node of nodes) {
84
+ if (protoAction !== "ignore" && Object.prototype.hasOwnProperty.call(node, "__proto__")) {
85
+ if (safe === true) {
86
+ return null;
87
+ } else if (protoAction === "error") {
88
+ throw new SyntaxError("Object contains forbidden prototype property");
89
+ }
90
+ delete node.__proto__;
91
+ }
92
+ if (constructorAction !== "ignore" && Object.prototype.hasOwnProperty.call(node, "constructor") && Object.prototype.hasOwnProperty.call(node.constructor, "prototype")) {
93
+ if (safe === true) {
94
+ return null;
95
+ } else if (constructorAction === "error") {
96
+ throw new SyntaxError("Object contains forbidden prototype property");
97
+ }
98
+ delete node.constructor;
99
+ }
100
+ for (const key in node) {
101
+ const value = node[key];
102
+ if (value && typeof value === "object") {
103
+ next.push(value);
104
+ }
105
+ }
106
+ }
107
+ }
108
+ return obj;
109
+ }
110
+ function parse2(text, reviver, options) {
111
+ const stackTraceLimit = Error.stackTraceLimit;
112
+ Error.stackTraceLimit = 0;
113
+ try {
114
+ return _parse(text, reviver, options);
115
+ } finally {
116
+ Error.stackTraceLimit = stackTraceLimit;
117
+ }
118
+ }
119
+ function safeParse(text, reviver) {
120
+ const stackTraceLimit = Error.stackTraceLimit;
121
+ Error.stackTraceLimit = 0;
122
+ try {
123
+ return _parse(text, reviver, { safe: true });
124
+ } catch (_e) {
125
+ return null;
126
+ } finally {
127
+ Error.stackTraceLimit = stackTraceLimit;
128
+ }
129
+ }
130
+ module.exports = parse2;
131
+ module.exports.default = parse2;
132
+ module.exports.parse = parse2;
133
+ module.exports.safeParse = safeParse;
134
+ module.exports.scan = filter;
135
+ }
136
+ });
137
+
138
+ // ../../node_modules/.pnpm/@ai-sdk+provider@1.1.2/node_modules/@ai-sdk/provider/dist/index.mjs
139
+ var marker = "vercel.ai.error";
140
+ var symbol = Symbol.for(marker);
141
+ var _a;
142
+ var _AISDKError = class _AISDKError2 extends Error {
143
+ /**
144
+ * Creates an AI SDK Error.
145
+ *
146
+ * @param {Object} params - The parameters for creating the error.
147
+ * @param {string} params.name - The name of the error.
148
+ * @param {string} params.message - The error message.
149
+ * @param {unknown} [params.cause] - The underlying cause of the error.
150
+ */
151
+ constructor({
152
+ name: name14,
153
+ message,
154
+ cause
155
+ }) {
156
+ super(message);
157
+ this[_a] = true;
158
+ this.name = name14;
159
+ this.cause = cause;
160
+ }
161
+ /**
162
+ * Checks if the given error is an AI SDK Error.
163
+ * @param {unknown} error - The error to check.
164
+ * @returns {boolean} True if the error is an AI SDK Error, false otherwise.
165
+ */
166
+ static isInstance(error) {
167
+ return _AISDKError2.hasMarker(error, marker);
168
+ }
169
+ static hasMarker(error, marker15) {
170
+ const markerSymbol = Symbol.for(marker15);
171
+ return error != null && typeof error === "object" && markerSymbol in error && typeof error[markerSymbol] === "boolean" && error[markerSymbol] === true;
172
+ }
173
+ };
174
+ _a = symbol;
175
+ var AISDKError = _AISDKError;
176
+ var name = "AI_APICallError";
177
+ var marker2 = `vercel.ai.error.${name}`;
178
+ var symbol2 = Symbol.for(marker2);
179
+ var _a2;
180
+ var APICallError = class extends AISDKError {
181
+ constructor({
182
+ message,
183
+ url,
184
+ requestBodyValues,
185
+ statusCode,
186
+ responseHeaders,
187
+ responseBody,
188
+ cause,
189
+ isRetryable = statusCode != null && (statusCode === 408 || // request timeout
190
+ statusCode === 409 || // conflict
191
+ statusCode === 429 || // too many requests
192
+ statusCode >= 500),
193
+ // server error
194
+ data
195
+ }) {
196
+ super({ name, message, cause });
197
+ this[_a2] = true;
198
+ this.url = url;
199
+ this.requestBodyValues = requestBodyValues;
200
+ this.statusCode = statusCode;
201
+ this.responseHeaders = responseHeaders;
202
+ this.responseBody = responseBody;
203
+ this.isRetryable = isRetryable;
204
+ this.data = data;
205
+ }
206
+ static isInstance(error) {
207
+ return AISDKError.hasMarker(error, marker2);
208
+ }
209
+ };
210
+ _a2 = symbol2;
211
+ var name2 = "AI_EmptyResponseBodyError";
212
+ var marker3 = `vercel.ai.error.${name2}`;
213
+ var symbol3 = Symbol.for(marker3);
214
+ var _a3;
215
+ var EmptyResponseBodyError = class extends AISDKError {
216
+ // used in isInstance
217
+ constructor({ message = "Empty response body" } = {}) {
218
+ super({ name: name2, message });
219
+ this[_a3] = true;
220
+ }
221
+ static isInstance(error) {
222
+ return AISDKError.hasMarker(error, marker3);
223
+ }
224
+ };
225
+ _a3 = symbol3;
226
+ function getErrorMessage(error) {
227
+ if (error == null) {
228
+ return "unknown error";
229
+ }
230
+ if (typeof error === "string") {
231
+ return error;
232
+ }
233
+ if (error instanceof Error) {
234
+ return error.message;
235
+ }
236
+ return JSON.stringify(error);
237
+ }
238
+ var name3 = "AI_InvalidArgumentError";
239
+ var marker4 = `vercel.ai.error.${name3}`;
240
+ var symbol4 = Symbol.for(marker4);
241
+ var _a4;
242
+ var InvalidArgumentError = class extends AISDKError {
243
+ constructor({
244
+ message,
245
+ cause,
246
+ argument
247
+ }) {
248
+ super({ name: name3, message, cause });
249
+ this[_a4] = true;
250
+ this.argument = argument;
251
+ }
252
+ static isInstance(error) {
253
+ return AISDKError.hasMarker(error, marker4);
254
+ }
255
+ };
256
+ _a4 = symbol4;
257
+ var name4 = "AI_InvalidPromptError";
258
+ var marker5 = `vercel.ai.error.${name4}`;
259
+ var symbol5 = Symbol.for(marker5);
260
+ var _a5;
261
+ var InvalidPromptError = class extends AISDKError {
262
+ constructor({
263
+ prompt,
264
+ message,
265
+ cause
266
+ }) {
267
+ super({ name: name4, message: `Invalid prompt: ${message}`, cause });
268
+ this[_a5] = true;
269
+ this.prompt = prompt;
270
+ }
271
+ static isInstance(error) {
272
+ return AISDKError.hasMarker(error, marker5);
273
+ }
274
+ };
275
+ _a5 = symbol5;
276
+ var name5 = "AI_InvalidResponseDataError";
277
+ var marker6 = `vercel.ai.error.${name5}`;
278
+ var symbol6 = Symbol.for(marker6);
279
+ var _a6;
280
+ var InvalidResponseDataError = class extends AISDKError {
281
+ constructor({
282
+ data,
283
+ message = `Invalid response data: ${JSON.stringify(data)}.`
284
+ }) {
285
+ super({ name: name5, message });
286
+ this[_a6] = true;
287
+ this.data = data;
288
+ }
289
+ static isInstance(error) {
290
+ return AISDKError.hasMarker(error, marker6);
291
+ }
292
+ };
293
+ _a6 = symbol6;
294
+ var name6 = "AI_JSONParseError";
295
+ var marker7 = `vercel.ai.error.${name6}`;
296
+ var symbol7 = Symbol.for(marker7);
297
+ var _a7;
298
+ var JSONParseError = class extends AISDKError {
299
+ constructor({ text, cause }) {
300
+ super({
301
+ name: name6,
302
+ message: `JSON parsing failed: Text: ${text}.
303
+ Error message: ${getErrorMessage(cause)}`,
304
+ cause
305
+ });
306
+ this[_a7] = true;
307
+ this.text = text;
308
+ }
309
+ static isInstance(error) {
310
+ return AISDKError.hasMarker(error, marker7);
311
+ }
312
+ };
313
+ _a7 = symbol7;
314
+ var name7 = "AI_LoadAPIKeyError";
315
+ var marker8 = `vercel.ai.error.${name7}`;
316
+ var symbol8 = Symbol.for(marker8);
317
+ var _a8;
318
+ var LoadAPIKeyError = class extends AISDKError {
319
+ // used in isInstance
320
+ constructor({ message }) {
321
+ super({ name: name7, message });
322
+ this[_a8] = true;
323
+ }
324
+ static isInstance(error) {
325
+ return AISDKError.hasMarker(error, marker8);
326
+ }
327
+ };
328
+ _a8 = symbol8;
329
+ var name11 = "AI_TooManyEmbeddingValuesForCallError";
330
+ var marker12 = `vercel.ai.error.${name11}`;
331
+ var symbol12 = Symbol.for(marker12);
332
+ var _a12;
333
+ var TooManyEmbeddingValuesForCallError = class extends AISDKError {
334
+ constructor(options) {
335
+ super({
336
+ name: name11,
337
+ message: `Too many values for a single embedding call. The ${options.provider} model "${options.modelId}" can only embed up to ${options.maxEmbeddingsPerCall} values per call, but ${options.values.length} values were provided.`
338
+ });
339
+ this[_a12] = true;
340
+ this.provider = options.provider;
341
+ this.modelId = options.modelId;
342
+ this.maxEmbeddingsPerCall = options.maxEmbeddingsPerCall;
343
+ this.values = options.values;
344
+ }
345
+ static isInstance(error) {
346
+ return AISDKError.hasMarker(error, marker12);
347
+ }
348
+ };
349
+ _a12 = symbol12;
350
+ var name12 = "AI_TypeValidationError";
351
+ var marker13 = `vercel.ai.error.${name12}`;
352
+ var symbol13 = Symbol.for(marker13);
353
+ var _a13;
354
+ var _TypeValidationError = class _TypeValidationError2 extends AISDKError {
355
+ constructor({ value, cause }) {
356
+ super({
357
+ name: name12,
358
+ message: `Type validation failed: Value: ${JSON.stringify(value)}.
359
+ Error message: ${getErrorMessage(cause)}`,
360
+ cause
361
+ });
362
+ this[_a13] = true;
363
+ this.value = value;
364
+ }
365
+ static isInstance(error) {
366
+ return AISDKError.hasMarker(error, marker13);
367
+ }
368
+ /**
369
+ * Wraps an error into a TypeValidationError.
370
+ * If the cause is already a TypeValidationError with the same value, it returns the cause.
371
+ * Otherwise, it creates a new TypeValidationError.
372
+ *
373
+ * @param {Object} params - The parameters for wrapping the error.
374
+ * @param {unknown} params.value - The value that failed validation.
375
+ * @param {unknown} params.cause - The original error or cause of the validation failure.
376
+ * @returns {TypeValidationError} A TypeValidationError instance.
377
+ */
378
+ static wrap({
379
+ value,
380
+ cause
381
+ }) {
382
+ return _TypeValidationError2.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError2({ value, cause });
383
+ }
384
+ };
385
+ _a13 = symbol13;
386
+ var TypeValidationError = _TypeValidationError;
387
+ var name13 = "AI_UnsupportedFunctionalityError";
388
+ var marker14 = `vercel.ai.error.${name13}`;
389
+ var symbol14 = Symbol.for(marker14);
390
+ var _a14;
391
+ var UnsupportedFunctionalityError = class extends AISDKError {
392
+ constructor({
393
+ functionality,
394
+ message = `'${functionality}' functionality not supported.`
395
+ }) {
396
+ super({ name: name13, message });
397
+ this[_a14] = true;
398
+ this.functionality = functionality;
399
+ }
400
+ static isInstance(error) {
401
+ return AISDKError.hasMarker(error, marker14);
402
+ }
403
+ };
404
+ _a14 = symbol14;
405
+
406
+ // ../../node_modules/.pnpm/nanoid@3.3.11/node_modules/nanoid/non-secure/index.js
407
+ var customAlphabet = (alphabet, defaultSize = 21) => {
408
+ return (size = defaultSize) => {
409
+ let id = "";
410
+ let i = size | 0;
411
+ while (i--) {
412
+ id += alphabet[Math.random() * alphabet.length | 0];
413
+ }
414
+ return id;
415
+ };
416
+ };
417
+
418
+ // ../../node_modules/.pnpm/@ai-sdk+provider-utils@2.2.6_zod@3.24.2/node_modules/@ai-sdk/provider-utils/dist/index.mjs
419
+ var import_secure_json_parse = __toESM(require_secure_json_parse());
420
+ function combineHeaders(...headers) {
421
+ return headers.reduce(
422
+ (combinedHeaders, currentHeaders) => ({
423
+ ...combinedHeaders,
424
+ ...currentHeaders != null ? currentHeaders : {}
425
+ }),
426
+ {}
427
+ );
428
+ }
429
+ function createEventSourceParserStream() {
430
+ let buffer = "";
431
+ let event = void 0;
432
+ let data = [];
433
+ let lastEventId = void 0;
434
+ let retry = void 0;
435
+ function parseLine(line, controller) {
436
+ if (line === "") {
437
+ dispatchEvent(controller);
438
+ return;
439
+ }
440
+ if (line.startsWith(":")) {
441
+ return;
442
+ }
443
+ const colonIndex = line.indexOf(":");
444
+ if (colonIndex === -1) {
445
+ handleField(line, "");
446
+ return;
447
+ }
448
+ const field = line.slice(0, colonIndex);
449
+ const valueStart = colonIndex + 1;
450
+ const value = valueStart < line.length && line[valueStart] === " " ? line.slice(valueStart + 1) : line.slice(valueStart);
451
+ handleField(field, value);
452
+ }
453
+ function dispatchEvent(controller) {
454
+ if (data.length > 0) {
455
+ controller.enqueue({
456
+ event,
457
+ data: data.join("\n"),
458
+ id: lastEventId,
459
+ retry
460
+ });
461
+ data = [];
462
+ event = void 0;
463
+ retry = void 0;
464
+ }
465
+ }
466
+ function handleField(field, value) {
467
+ switch (field) {
468
+ case "event":
469
+ event = value;
470
+ break;
471
+ case "data":
472
+ data.push(value);
473
+ break;
474
+ case "id":
475
+ lastEventId = value;
476
+ break;
477
+ case "retry":
478
+ const parsedRetry = parseInt(value, 10);
479
+ if (!isNaN(parsedRetry)) {
480
+ retry = parsedRetry;
481
+ }
482
+ break;
483
+ }
484
+ }
485
+ return new TransformStream({
486
+ transform(chunk, controller) {
487
+ const { lines, incompleteLine } = splitLines(buffer, chunk);
488
+ buffer = incompleteLine;
489
+ for (let i = 0; i < lines.length; i++) {
490
+ parseLine(lines[i], controller);
491
+ }
492
+ },
493
+ flush(controller) {
494
+ parseLine(buffer, controller);
495
+ dispatchEvent(controller);
496
+ }
497
+ });
498
+ }
499
+ function splitLines(buffer, chunk) {
500
+ const lines = [];
501
+ let currentLine = buffer;
502
+ for (let i = 0; i < chunk.length; ) {
503
+ const char = chunk[i++];
504
+ if (char === "\n") {
505
+ lines.push(currentLine);
506
+ currentLine = "";
507
+ } else if (char === "\r") {
508
+ lines.push(currentLine);
509
+ currentLine = "";
510
+ if (chunk[i + 1] === "\n") {
511
+ i++;
512
+ }
513
+ } else {
514
+ currentLine += char;
515
+ }
516
+ }
517
+ return { lines, incompleteLine: currentLine };
518
+ }
519
+ function extractResponseHeaders(response) {
520
+ const headers = {};
521
+ response.headers.forEach((value, key) => {
522
+ headers[key] = value;
523
+ });
524
+ return headers;
525
+ }
526
+ var createIdGenerator = ({
527
+ prefix,
528
+ size: defaultSize = 16,
529
+ alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
530
+ separator = "-"
531
+ } = {}) => {
532
+ const generator = customAlphabet(alphabet, defaultSize);
533
+ if (prefix == null) {
534
+ return generator;
535
+ }
536
+ if (alphabet.includes(separator)) {
537
+ throw new InvalidArgumentError({
538
+ argument: "separator",
539
+ message: `The separator "${separator}" must not be part of the alphabet "${alphabet}".`
540
+ });
541
+ }
542
+ return (size) => `${prefix}${separator}${generator(size)}`;
543
+ };
544
+ var generateId = createIdGenerator();
545
+ function removeUndefinedEntries(record) {
546
+ return Object.fromEntries(
547
+ Object.entries(record).filter(([_key, value]) => value != null)
548
+ );
549
+ }
550
+ function isAbortError(error) {
551
+ return error instanceof Error && (error.name === "AbortError" || error.name === "TimeoutError");
552
+ }
553
+ function loadApiKey({
554
+ apiKey,
555
+ environmentVariableName,
556
+ apiKeyParameterName = "apiKey",
557
+ description
558
+ }) {
559
+ if (typeof apiKey === "string") {
560
+ return apiKey;
561
+ }
562
+ if (apiKey != null) {
563
+ throw new LoadAPIKeyError({
564
+ message: `${description} API key must be a string.`
565
+ });
566
+ }
567
+ if (typeof process === "undefined") {
568
+ throw new LoadAPIKeyError({
569
+ message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables is not supported in this environment.`
570
+ });
571
+ }
572
+ apiKey = process.env[environmentVariableName];
573
+ if (apiKey == null) {
574
+ throw new LoadAPIKeyError({
575
+ message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or the ${environmentVariableName} environment variable.`
576
+ });
577
+ }
578
+ if (typeof apiKey !== "string") {
579
+ throw new LoadAPIKeyError({
580
+ message: `${description} API key must be a string. The value of the ${environmentVariableName} environment variable is not a string.`
581
+ });
582
+ }
583
+ return apiKey;
584
+ }
585
+ var validatorSymbol = Symbol.for("vercel.ai.validator");
586
+ function validator(validate) {
587
+ return { [validatorSymbol]: true, validate };
588
+ }
589
+ function isValidator(value) {
590
+ return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
591
+ }
592
+ function asValidator(value) {
593
+ return isValidator(value) ? value : zodValidator(value);
594
+ }
595
+ function zodValidator(zodSchema) {
596
+ return validator((value) => {
597
+ const result = zodSchema.safeParse(value);
598
+ return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
599
+ });
600
+ }
601
+ function validateTypes({
602
+ value,
603
+ schema: inputSchema
604
+ }) {
605
+ const result = safeValidateTypes({ value, schema: inputSchema });
606
+ if (!result.success) {
607
+ throw TypeValidationError.wrap({ value, cause: result.error });
608
+ }
609
+ return result.value;
610
+ }
611
+ function safeValidateTypes({
612
+ value,
613
+ schema
614
+ }) {
615
+ const validator2 = asValidator(schema);
616
+ try {
617
+ if (validator2.validate == null) {
618
+ return { success: true, value };
619
+ }
620
+ const result = validator2.validate(value);
621
+ if (result.success) {
622
+ return result;
623
+ }
624
+ return {
625
+ success: false,
626
+ error: TypeValidationError.wrap({ value, cause: result.error })
627
+ };
628
+ } catch (error) {
629
+ return {
630
+ success: false,
631
+ error: TypeValidationError.wrap({ value, cause: error })
632
+ };
633
+ }
634
+ }
635
+ function parseJSON({
636
+ text,
637
+ schema
638
+ }) {
639
+ try {
640
+ const value = import_secure_json_parse.default.parse(text);
641
+ if (schema == null) {
642
+ return value;
643
+ }
644
+ return validateTypes({ value, schema });
645
+ } catch (error) {
646
+ if (JSONParseError.isInstance(error) || TypeValidationError.isInstance(error)) {
647
+ throw error;
648
+ }
649
+ throw new JSONParseError({ text, cause: error });
650
+ }
651
+ }
652
+ function safeParseJSON({
653
+ text,
654
+ schema
655
+ }) {
656
+ try {
657
+ const value = import_secure_json_parse.default.parse(text);
658
+ if (schema == null) {
659
+ return { success: true, value, rawValue: value };
660
+ }
661
+ const validationResult = safeValidateTypes({ value, schema });
662
+ return validationResult.success ? { ...validationResult, rawValue: value } : validationResult;
663
+ } catch (error) {
664
+ return {
665
+ success: false,
666
+ error: JSONParseError.isInstance(error) ? error : new JSONParseError({ text, cause: error })
667
+ };
668
+ }
669
+ }
670
+ function isParsableJson(input) {
671
+ try {
672
+ import_secure_json_parse.default.parse(input);
673
+ return true;
674
+ } catch (e) {
675
+ return false;
676
+ }
677
+ }
678
+ function parseProviderOptions({
679
+ provider,
680
+ providerOptions,
681
+ schema
682
+ }) {
683
+ if ((providerOptions == null ? void 0 : providerOptions[provider]) == null) {
684
+ return void 0;
685
+ }
686
+ const parsedProviderOptions = safeValidateTypes({
687
+ value: providerOptions[provider],
688
+ schema
689
+ });
690
+ if (!parsedProviderOptions.success) {
691
+ throw new InvalidArgumentError({
692
+ argument: "providerOptions",
693
+ message: `invalid ${provider} provider options`,
694
+ cause: parsedProviderOptions.error
695
+ });
696
+ }
697
+ return parsedProviderOptions.value;
698
+ }
699
+ var getOriginalFetch2 = () => globalThis.fetch;
700
+ var postJsonToApi = async ({
701
+ url,
702
+ headers,
703
+ body,
704
+ failedResponseHandler,
705
+ successfulResponseHandler,
706
+ abortSignal,
707
+ fetch
708
+ }) => postToApi({
709
+ url,
710
+ headers: {
711
+ "Content-Type": "application/json",
712
+ ...headers
713
+ },
714
+ body: {
715
+ content: JSON.stringify(body),
716
+ values: body
717
+ },
718
+ failedResponseHandler,
719
+ successfulResponseHandler,
720
+ abortSignal,
721
+ fetch
722
+ });
723
+ var postFormDataToApi = async ({
724
+ url,
725
+ headers,
726
+ formData,
727
+ failedResponseHandler,
728
+ successfulResponseHandler,
729
+ abortSignal,
730
+ fetch
731
+ }) => postToApi({
732
+ url,
733
+ headers,
734
+ body: {
735
+ content: formData,
736
+ values: Object.fromEntries(formData.entries())
737
+ },
738
+ failedResponseHandler,
739
+ successfulResponseHandler,
740
+ abortSignal,
741
+ fetch
742
+ });
743
+ var postToApi = async ({
744
+ url,
745
+ headers = {},
746
+ body,
747
+ successfulResponseHandler,
748
+ failedResponseHandler,
749
+ abortSignal,
750
+ fetch = getOriginalFetch2()
751
+ }) => {
752
+ try {
753
+ const response = await fetch(url, {
754
+ method: "POST",
755
+ headers: removeUndefinedEntries(headers),
756
+ body: body.content,
757
+ signal: abortSignal
758
+ });
759
+ const responseHeaders = extractResponseHeaders(response);
760
+ if (!response.ok) {
761
+ let errorInformation;
762
+ try {
763
+ errorInformation = await failedResponseHandler({
764
+ response,
765
+ url,
766
+ requestBodyValues: body.values
767
+ });
768
+ } catch (error) {
769
+ if (isAbortError(error) || APICallError.isInstance(error)) {
770
+ throw error;
771
+ }
772
+ throw new APICallError({
773
+ message: "Failed to process error response",
774
+ cause: error,
775
+ statusCode: response.status,
776
+ url,
777
+ responseHeaders,
778
+ requestBodyValues: body.values
779
+ });
780
+ }
781
+ throw errorInformation.value;
782
+ }
783
+ try {
784
+ return await successfulResponseHandler({
785
+ response,
786
+ url,
787
+ requestBodyValues: body.values
788
+ });
789
+ } catch (error) {
790
+ if (error instanceof Error) {
791
+ if (isAbortError(error) || APICallError.isInstance(error)) {
792
+ throw error;
793
+ }
794
+ }
795
+ throw new APICallError({
796
+ message: "Failed to process successful response",
797
+ cause: error,
798
+ statusCode: response.status,
799
+ url,
800
+ responseHeaders,
801
+ requestBodyValues: body.values
802
+ });
803
+ }
804
+ } catch (error) {
805
+ if (isAbortError(error)) {
806
+ throw error;
807
+ }
808
+ if (error instanceof TypeError && error.message === "fetch failed") {
809
+ const cause = error.cause;
810
+ if (cause != null) {
811
+ throw new APICallError({
812
+ message: `Cannot connect to API: ${cause.message}`,
813
+ cause,
814
+ url,
815
+ requestBodyValues: body.values,
816
+ isRetryable: true
817
+ // retry when network error
818
+ });
819
+ }
820
+ }
821
+ throw error;
822
+ }
823
+ };
824
+ var createJsonErrorResponseHandler = ({
825
+ errorSchema,
826
+ errorToMessage,
827
+ isRetryable
828
+ }) => async ({ response, url, requestBodyValues }) => {
829
+ const responseBody = await response.text();
830
+ const responseHeaders = extractResponseHeaders(response);
831
+ if (responseBody.trim() === "") {
832
+ return {
833
+ responseHeaders,
834
+ value: new APICallError({
835
+ message: response.statusText,
836
+ url,
837
+ requestBodyValues,
838
+ statusCode: response.status,
839
+ responseHeaders,
840
+ responseBody,
841
+ isRetryable: isRetryable == null ? void 0 : isRetryable(response)
842
+ })
843
+ };
844
+ }
845
+ try {
846
+ const parsedError = parseJSON({
847
+ text: responseBody,
848
+ schema: errorSchema
849
+ });
850
+ return {
851
+ responseHeaders,
852
+ value: new APICallError({
853
+ message: errorToMessage(parsedError),
854
+ url,
855
+ requestBodyValues,
856
+ statusCode: response.status,
857
+ responseHeaders,
858
+ responseBody,
859
+ data: parsedError,
860
+ isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError)
861
+ })
862
+ };
863
+ } catch (parseError) {
864
+ return {
865
+ responseHeaders,
866
+ value: new APICallError({
867
+ message: response.statusText,
868
+ url,
869
+ requestBodyValues,
870
+ statusCode: response.status,
871
+ responseHeaders,
872
+ responseBody,
873
+ isRetryable: isRetryable == null ? void 0 : isRetryable(response)
874
+ })
875
+ };
876
+ }
877
+ };
878
+ var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) => {
879
+ const responseHeaders = extractResponseHeaders(response);
880
+ if (response.body == null) {
881
+ throw new EmptyResponseBodyError({});
882
+ }
883
+ return {
884
+ responseHeaders,
885
+ value: response.body.pipeThrough(new TextDecoderStream()).pipeThrough(createEventSourceParserStream()).pipeThrough(
886
+ new TransformStream({
887
+ transform({ data }, controller) {
888
+ if (data === "[DONE]") {
889
+ return;
890
+ }
891
+ controller.enqueue(
892
+ safeParseJSON({
893
+ text: data,
894
+ schema: chunkSchema
895
+ })
896
+ );
897
+ }
898
+ })
899
+ )
900
+ };
901
+ };
902
+ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => {
903
+ const responseBody = await response.text();
904
+ const parsedResult = safeParseJSON({
905
+ text: responseBody,
906
+ schema: responseSchema
907
+ });
908
+ const responseHeaders = extractResponseHeaders(response);
909
+ if (!parsedResult.success) {
910
+ throw new APICallError({
911
+ message: "Invalid JSON response",
912
+ cause: parsedResult.error,
913
+ statusCode: response.status,
914
+ responseHeaders,
915
+ responseBody,
916
+ url,
917
+ requestBodyValues
918
+ });
919
+ }
920
+ return {
921
+ responseHeaders,
922
+ value: parsedResult.value,
923
+ rawValue: parsedResult.rawValue
924
+ };
925
+ };
926
+ var { btoa, atob } = globalThis;
927
+ function convertBase64ToUint8Array(base64String) {
928
+ const base64Url = base64String.replace(/-/g, "+").replace(/_/g, "/");
929
+ const latin1string = atob(base64Url);
930
+ return Uint8Array.from(latin1string, (byte) => byte.codePointAt(0));
931
+ }
932
+ function convertUint8ArrayToBase64(array) {
933
+ let latin1string = "";
934
+ for (let i = 0; i < array.length; i++) {
935
+ latin1string += String.fromCodePoint(array[i]);
936
+ }
937
+ return btoa(latin1string);
938
+ }
939
+ function withoutTrailingSlash(url) {
940
+ return url == null ? void 0 : url.replace(/\/$/, "");
941
+ }
942
+ function convertToOpenAIChatMessages({
943
+ prompt,
944
+ useLegacyFunctionCalling = false,
945
+ systemMessageMode = "system"
946
+ }) {
947
+ const messages = [];
948
+ const warnings = [];
949
+ for (const { role, content } of prompt) {
950
+ switch (role) {
951
+ case "system": {
952
+ switch (systemMessageMode) {
953
+ case "system": {
954
+ messages.push({ role: "system", content });
955
+ break;
956
+ }
957
+ case "developer": {
958
+ messages.push({ role: "developer", content });
959
+ break;
960
+ }
961
+ case "remove": {
962
+ warnings.push({
963
+ type: "other",
964
+ message: "system messages are removed for this model"
965
+ });
966
+ break;
967
+ }
968
+ default: {
969
+ const _exhaustiveCheck = systemMessageMode;
970
+ throw new Error(
971
+ `Unsupported system message mode: ${_exhaustiveCheck}`
972
+ );
973
+ }
974
+ }
975
+ break;
976
+ }
977
+ case "user": {
978
+ if (content.length === 1 && content[0].type === "text") {
979
+ messages.push({ role: "user", content: content[0].text });
980
+ break;
981
+ }
982
+ messages.push({
983
+ role: "user",
984
+ content: content.map((part, index) => {
985
+ var _a15, _b, _c, _d;
986
+ switch (part.type) {
987
+ case "text": {
988
+ return { type: "text", text: part.text };
989
+ }
990
+ case "image": {
991
+ return {
992
+ type: "image_url",
993
+ image_url: {
994
+ url: part.image instanceof URL ? part.image.toString() : `data:${(_a15 = part.mimeType) != null ? _a15 : "image/jpeg"};base64,${convertUint8ArrayToBase64(part.image)}`,
995
+ // OpenAI specific extension: image detail
996
+ detail: (_c = (_b = part.providerMetadata) == null ? void 0 : _b.openai) == null ? void 0 : _c.imageDetail
997
+ }
998
+ };
999
+ }
1000
+ case "file": {
1001
+ if (part.data instanceof URL) {
1002
+ throw new UnsupportedFunctionalityError({
1003
+ functionality: "'File content parts with URL data' functionality not supported."
1004
+ });
1005
+ }
1006
+ switch (part.mimeType) {
1007
+ case "audio/wav": {
1008
+ return {
1009
+ type: "input_audio",
1010
+ input_audio: { data: part.data, format: "wav" }
1011
+ };
1012
+ }
1013
+ case "audio/mp3":
1014
+ case "audio/mpeg": {
1015
+ return {
1016
+ type: "input_audio",
1017
+ input_audio: { data: part.data, format: "mp3" }
1018
+ };
1019
+ }
1020
+ case "application/pdf": {
1021
+ return {
1022
+ type: "file",
1023
+ file: {
1024
+ filename: (_d = part.filename) != null ? _d : `part-${index}.pdf`,
1025
+ file_data: `data:application/pdf;base64,${part.data}`
1026
+ }
1027
+ };
1028
+ }
1029
+ default: {
1030
+ throw new UnsupportedFunctionalityError({
1031
+ functionality: `File content part type ${part.mimeType} in user messages`
1032
+ });
1033
+ }
1034
+ }
1035
+ }
1036
+ }
1037
+ })
1038
+ });
1039
+ break;
1040
+ }
1041
+ case "assistant": {
1042
+ let text = "";
1043
+ const toolCalls = [];
1044
+ for (const part of content) {
1045
+ switch (part.type) {
1046
+ case "text": {
1047
+ text += part.text;
1048
+ break;
1049
+ }
1050
+ case "tool-call": {
1051
+ toolCalls.push({
1052
+ id: part.toolCallId,
1053
+ type: "function",
1054
+ function: {
1055
+ name: part.toolName,
1056
+ arguments: JSON.stringify(part.args)
1057
+ }
1058
+ });
1059
+ break;
1060
+ }
1061
+ }
1062
+ }
1063
+ if (useLegacyFunctionCalling) {
1064
+ if (toolCalls.length > 1) {
1065
+ throw new UnsupportedFunctionalityError({
1066
+ functionality: "useLegacyFunctionCalling with multiple tool calls in one message"
1067
+ });
1068
+ }
1069
+ messages.push({
1070
+ role: "assistant",
1071
+ content: text,
1072
+ function_call: toolCalls.length > 0 ? toolCalls[0].function : void 0
1073
+ });
1074
+ } else {
1075
+ messages.push({
1076
+ role: "assistant",
1077
+ content: text,
1078
+ tool_calls: toolCalls.length > 0 ? toolCalls : void 0
1079
+ });
1080
+ }
1081
+ break;
1082
+ }
1083
+ case "tool": {
1084
+ for (const toolResponse of content) {
1085
+ if (useLegacyFunctionCalling) {
1086
+ messages.push({
1087
+ role: "function",
1088
+ name: toolResponse.toolName,
1089
+ content: JSON.stringify(toolResponse.result)
1090
+ });
1091
+ } else {
1092
+ messages.push({
1093
+ role: "tool",
1094
+ tool_call_id: toolResponse.toolCallId,
1095
+ content: JSON.stringify(toolResponse.result)
1096
+ });
1097
+ }
1098
+ }
1099
+ break;
1100
+ }
1101
+ default: {
1102
+ const _exhaustiveCheck = role;
1103
+ throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
1104
+ }
1105
+ }
1106
+ }
1107
+ return { messages, warnings };
1108
+ }
1109
+ function mapOpenAIChatLogProbsOutput(logprobs) {
1110
+ var _a15, _b;
1111
+ return (_b = (_a15 = logprobs == null ? void 0 : logprobs.content) == null ? void 0 : _a15.map(({ token, logprob, top_logprobs }) => ({
1112
+ token,
1113
+ logprob,
1114
+ topLogprobs: top_logprobs ? top_logprobs.map(({ token: token2, logprob: logprob2 }) => ({
1115
+ token: token2,
1116
+ logprob: logprob2
1117
+ })) : []
1118
+ }))) != null ? _b : void 0;
1119
+ }
1120
+ function mapOpenAIFinishReason(finishReason) {
1121
+ switch (finishReason) {
1122
+ case "stop":
1123
+ return "stop";
1124
+ case "length":
1125
+ return "length";
1126
+ case "content_filter":
1127
+ return "content-filter";
1128
+ case "function_call":
1129
+ case "tool_calls":
1130
+ return "tool-calls";
1131
+ default:
1132
+ return "unknown";
1133
+ }
1134
+ }
1135
+ var openaiErrorDataSchema = z.object({
1136
+ error: z.object({
1137
+ message: z.string(),
1138
+ // The additional information below is handled loosely to support
1139
+ // OpenAI-compatible providers that have slightly different error
1140
+ // responses:
1141
+ type: z.string().nullish(),
1142
+ param: z.any().nullish(),
1143
+ code: z.union([z.string(), z.number()]).nullish()
1144
+ })
1145
+ });
1146
+ var openaiFailedResponseHandler = createJsonErrorResponseHandler({
1147
+ errorSchema: openaiErrorDataSchema,
1148
+ errorToMessage: (data) => data.error.message
1149
+ });
1150
+ function getResponseMetadata({
1151
+ id,
1152
+ model,
1153
+ created
1154
+ }) {
1155
+ return {
1156
+ id: id != null ? id : void 0,
1157
+ modelId: model != null ? model : void 0,
1158
+ timestamp: created != null ? new Date(created * 1e3) : void 0
1159
+ };
1160
+ }
1161
+ function prepareTools({
1162
+ mode,
1163
+ useLegacyFunctionCalling = false,
1164
+ structuredOutputs
1165
+ }) {
1166
+ var _a15;
1167
+ const tools = ((_a15 = mode.tools) == null ? void 0 : _a15.length) ? mode.tools : void 0;
1168
+ const toolWarnings = [];
1169
+ if (tools == null) {
1170
+ return { tools: void 0, tool_choice: void 0, toolWarnings };
1171
+ }
1172
+ const toolChoice = mode.toolChoice;
1173
+ if (useLegacyFunctionCalling) {
1174
+ const openaiFunctions = [];
1175
+ for (const tool of tools) {
1176
+ if (tool.type === "provider-defined") {
1177
+ toolWarnings.push({ type: "unsupported-tool", tool });
1178
+ } else {
1179
+ openaiFunctions.push({
1180
+ name: tool.name,
1181
+ description: tool.description,
1182
+ parameters: tool.parameters
1183
+ });
1184
+ }
1185
+ }
1186
+ if (toolChoice == null) {
1187
+ return {
1188
+ functions: openaiFunctions,
1189
+ function_call: void 0,
1190
+ toolWarnings
1191
+ };
1192
+ }
1193
+ const type2 = toolChoice.type;
1194
+ switch (type2) {
1195
+ case "auto":
1196
+ case "none":
1197
+ case void 0:
1198
+ return {
1199
+ functions: openaiFunctions,
1200
+ function_call: void 0,
1201
+ toolWarnings
1202
+ };
1203
+ case "required":
1204
+ throw new UnsupportedFunctionalityError({
1205
+ functionality: "useLegacyFunctionCalling and toolChoice: required"
1206
+ });
1207
+ default:
1208
+ return {
1209
+ functions: openaiFunctions,
1210
+ function_call: { name: toolChoice.toolName },
1211
+ toolWarnings
1212
+ };
1213
+ }
1214
+ }
1215
+ const openaiTools2 = [];
1216
+ for (const tool of tools) {
1217
+ if (tool.type === "provider-defined") {
1218
+ toolWarnings.push({ type: "unsupported-tool", tool });
1219
+ } else {
1220
+ openaiTools2.push({
1221
+ type: "function",
1222
+ function: {
1223
+ name: tool.name,
1224
+ description: tool.description,
1225
+ parameters: tool.parameters,
1226
+ strict: structuredOutputs ? true : void 0
1227
+ }
1228
+ });
1229
+ }
1230
+ }
1231
+ if (toolChoice == null) {
1232
+ return { tools: openaiTools2, tool_choice: void 0, toolWarnings };
1233
+ }
1234
+ const type = toolChoice.type;
1235
+ switch (type) {
1236
+ case "auto":
1237
+ case "none":
1238
+ case "required":
1239
+ return { tools: openaiTools2, tool_choice: type, toolWarnings };
1240
+ case "tool":
1241
+ return {
1242
+ tools: openaiTools2,
1243
+ tool_choice: {
1244
+ type: "function",
1245
+ function: {
1246
+ name: toolChoice.toolName
1247
+ }
1248
+ },
1249
+ toolWarnings
1250
+ };
1251
+ default: {
1252
+ const _exhaustiveCheck = type;
1253
+ throw new UnsupportedFunctionalityError({
1254
+ functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
1255
+ });
1256
+ }
1257
+ }
1258
+ }
1259
+ var OpenAIChatLanguageModel = class {
1260
+ constructor(modelId, settings, config) {
1261
+ this.specificationVersion = "v1";
1262
+ this.modelId = modelId;
1263
+ this.settings = settings;
1264
+ this.config = config;
1265
+ }
1266
+ get supportsStructuredOutputs() {
1267
+ var _a15;
1268
+ return (_a15 = this.settings.structuredOutputs) != null ? _a15 : isReasoningModel(this.modelId);
1269
+ }
1270
+ get defaultObjectGenerationMode() {
1271
+ if (isAudioModel(this.modelId)) {
1272
+ return "tool";
1273
+ }
1274
+ return this.supportsStructuredOutputs ? "json" : "tool";
1275
+ }
1276
+ get provider() {
1277
+ return this.config.provider;
1278
+ }
1279
+ get supportsImageUrls() {
1280
+ return !this.settings.downloadImages;
1281
+ }
1282
+ getArgs({
1283
+ mode,
1284
+ prompt,
1285
+ maxTokens,
1286
+ temperature,
1287
+ topP,
1288
+ topK,
1289
+ frequencyPenalty,
1290
+ presencePenalty,
1291
+ stopSequences,
1292
+ responseFormat,
1293
+ seed,
1294
+ providerMetadata
1295
+ }) {
1296
+ var _a15, _b, _c, _d, _e, _f, _g, _h;
1297
+ const type = mode.type;
1298
+ const warnings = [];
1299
+ if (topK != null) {
1300
+ warnings.push({
1301
+ type: "unsupported-setting",
1302
+ setting: "topK"
1303
+ });
1304
+ }
1305
+ if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !this.supportsStructuredOutputs) {
1306
+ warnings.push({
1307
+ type: "unsupported-setting",
1308
+ setting: "responseFormat",
1309
+ details: "JSON response format schema is only supported with structuredOutputs"
1310
+ });
1311
+ }
1312
+ const useLegacyFunctionCalling = this.settings.useLegacyFunctionCalling;
1313
+ if (useLegacyFunctionCalling && this.settings.parallelToolCalls === true) {
1314
+ throw new UnsupportedFunctionalityError({
1315
+ functionality: "useLegacyFunctionCalling with parallelToolCalls"
1316
+ });
1317
+ }
1318
+ if (useLegacyFunctionCalling && this.supportsStructuredOutputs) {
1319
+ throw new UnsupportedFunctionalityError({
1320
+ functionality: "structuredOutputs with useLegacyFunctionCalling"
1321
+ });
1322
+ }
1323
+ const { messages, warnings: messageWarnings } = convertToOpenAIChatMessages(
1324
+ {
1325
+ prompt,
1326
+ useLegacyFunctionCalling,
1327
+ systemMessageMode: getSystemMessageMode(this.modelId)
1328
+ }
1329
+ );
1330
+ warnings.push(...messageWarnings);
1331
+ const baseArgs = {
1332
+ // model id:
1333
+ model: this.modelId,
1334
+ // model specific settings:
1335
+ logit_bias: this.settings.logitBias,
1336
+ logprobs: this.settings.logprobs === true || typeof this.settings.logprobs === "number" ? true : void 0,
1337
+ top_logprobs: typeof this.settings.logprobs === "number" ? this.settings.logprobs : typeof this.settings.logprobs === "boolean" ? this.settings.logprobs ? 0 : void 0 : void 0,
1338
+ user: this.settings.user,
1339
+ parallel_tool_calls: this.settings.parallelToolCalls,
1340
+ // standardized settings:
1341
+ max_tokens: maxTokens,
1342
+ temperature,
1343
+ top_p: topP,
1344
+ frequency_penalty: frequencyPenalty,
1345
+ presence_penalty: presencePenalty,
1346
+ response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? this.supportsStructuredOutputs && responseFormat.schema != null ? {
1347
+ type: "json_schema",
1348
+ json_schema: {
1349
+ schema: responseFormat.schema,
1350
+ strict: true,
1351
+ name: (_a15 = responseFormat.name) != null ? _a15 : "response",
1352
+ description: responseFormat.description
1353
+ }
1354
+ } : { type: "json_object" } : void 0,
1355
+ stop: stopSequences,
1356
+ seed,
1357
+ // openai specific settings:
1358
+ // TODO remove in next major version; we auto-map maxTokens now
1359
+ max_completion_tokens: (_b = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _b.maxCompletionTokens,
1360
+ store: (_c = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _c.store,
1361
+ metadata: (_d = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _d.metadata,
1362
+ prediction: (_e = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _e.prediction,
1363
+ reasoning_effort: (_g = (_f = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _f.reasoningEffort) != null ? _g : this.settings.reasoningEffort,
1364
+ // messages:
1365
+ messages
1366
+ };
1367
+ if (isReasoningModel(this.modelId)) {
1368
+ if (baseArgs.temperature != null) {
1369
+ baseArgs.temperature = void 0;
1370
+ warnings.push({
1371
+ type: "unsupported-setting",
1372
+ setting: "temperature",
1373
+ details: "temperature is not supported for reasoning models"
1374
+ });
1375
+ }
1376
+ if (baseArgs.top_p != null) {
1377
+ baseArgs.top_p = void 0;
1378
+ warnings.push({
1379
+ type: "unsupported-setting",
1380
+ setting: "topP",
1381
+ details: "topP is not supported for reasoning models"
1382
+ });
1383
+ }
1384
+ if (baseArgs.frequency_penalty != null) {
1385
+ baseArgs.frequency_penalty = void 0;
1386
+ warnings.push({
1387
+ type: "unsupported-setting",
1388
+ setting: "frequencyPenalty",
1389
+ details: "frequencyPenalty is not supported for reasoning models"
1390
+ });
1391
+ }
1392
+ if (baseArgs.presence_penalty != null) {
1393
+ baseArgs.presence_penalty = void 0;
1394
+ warnings.push({
1395
+ type: "unsupported-setting",
1396
+ setting: "presencePenalty",
1397
+ details: "presencePenalty is not supported for reasoning models"
1398
+ });
1399
+ }
1400
+ if (baseArgs.logit_bias != null) {
1401
+ baseArgs.logit_bias = void 0;
1402
+ warnings.push({
1403
+ type: "other",
1404
+ message: "logitBias is not supported for reasoning models"
1405
+ });
1406
+ }
1407
+ if (baseArgs.logprobs != null) {
1408
+ baseArgs.logprobs = void 0;
1409
+ warnings.push({
1410
+ type: "other",
1411
+ message: "logprobs is not supported for reasoning models"
1412
+ });
1413
+ }
1414
+ if (baseArgs.top_logprobs != null) {
1415
+ baseArgs.top_logprobs = void 0;
1416
+ warnings.push({
1417
+ type: "other",
1418
+ message: "topLogprobs is not supported for reasoning models"
1419
+ });
1420
+ }
1421
+ if (baseArgs.max_tokens != null) {
1422
+ if (baseArgs.max_completion_tokens == null) {
1423
+ baseArgs.max_completion_tokens = baseArgs.max_tokens;
1424
+ }
1425
+ baseArgs.max_tokens = void 0;
1426
+ }
1427
+ }
1428
+ switch (type) {
1429
+ case "regular": {
1430
+ const { tools, tool_choice, functions, function_call, toolWarnings } = prepareTools({
1431
+ mode,
1432
+ useLegacyFunctionCalling,
1433
+ structuredOutputs: this.supportsStructuredOutputs
1434
+ });
1435
+ return {
1436
+ args: {
1437
+ ...baseArgs,
1438
+ tools,
1439
+ tool_choice,
1440
+ functions,
1441
+ function_call
1442
+ },
1443
+ warnings: [...warnings, ...toolWarnings]
1444
+ };
1445
+ }
1446
+ case "object-json": {
1447
+ return {
1448
+ args: {
1449
+ ...baseArgs,
1450
+ response_format: this.supportsStructuredOutputs && mode.schema != null ? {
1451
+ type: "json_schema",
1452
+ json_schema: {
1453
+ schema: mode.schema,
1454
+ strict: true,
1455
+ name: (_h = mode.name) != null ? _h : "response",
1456
+ description: mode.description
1457
+ }
1458
+ } : { type: "json_object" }
1459
+ },
1460
+ warnings
1461
+ };
1462
+ }
1463
+ case "object-tool": {
1464
+ return {
1465
+ args: useLegacyFunctionCalling ? {
1466
+ ...baseArgs,
1467
+ function_call: {
1468
+ name: mode.tool.name
1469
+ },
1470
+ functions: [
1471
+ {
1472
+ name: mode.tool.name,
1473
+ description: mode.tool.description,
1474
+ parameters: mode.tool.parameters
1475
+ }
1476
+ ]
1477
+ } : {
1478
+ ...baseArgs,
1479
+ tool_choice: {
1480
+ type: "function",
1481
+ function: { name: mode.tool.name }
1482
+ },
1483
+ tools: [
1484
+ {
1485
+ type: "function",
1486
+ function: {
1487
+ name: mode.tool.name,
1488
+ description: mode.tool.description,
1489
+ parameters: mode.tool.parameters,
1490
+ strict: this.supportsStructuredOutputs ? true : void 0
1491
+ }
1492
+ }
1493
+ ]
1494
+ },
1495
+ warnings
1496
+ };
1497
+ }
1498
+ default: {
1499
+ const _exhaustiveCheck = type;
1500
+ throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
1501
+ }
1502
+ }
1503
+ }
1504
+ async doGenerate(options) {
1505
+ var _a15, _b, _c, _d, _e, _f, _g, _h;
1506
+ const { args: body, warnings } = this.getArgs(options);
1507
+ const {
1508
+ responseHeaders,
1509
+ value: response,
1510
+ rawValue: rawResponse
1511
+ } = await postJsonToApi({
1512
+ url: this.config.url({
1513
+ path: "/chat/completions",
1514
+ modelId: this.modelId
1515
+ }),
1516
+ headers: combineHeaders(this.config.headers(), options.headers),
1517
+ body,
1518
+ failedResponseHandler: openaiFailedResponseHandler,
1519
+ successfulResponseHandler: createJsonResponseHandler(
1520
+ openaiChatResponseSchema
1521
+ ),
1522
+ abortSignal: options.abortSignal,
1523
+ fetch: this.config.fetch
1524
+ });
1525
+ const { messages: rawPrompt, ...rawSettings } = body;
1526
+ const choice = response.choices[0];
1527
+ const completionTokenDetails = (_a15 = response.usage) == null ? void 0 : _a15.completion_tokens_details;
1528
+ const promptTokenDetails = (_b = response.usage) == null ? void 0 : _b.prompt_tokens_details;
1529
+ const providerMetadata = { openai: {} };
1530
+ if ((completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens) != null) {
1531
+ providerMetadata.openai.reasoningTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens;
1532
+ }
1533
+ if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
1534
+ providerMetadata.openai.acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
1535
+ }
1536
+ if ((completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens) != null) {
1537
+ providerMetadata.openai.rejectedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens;
1538
+ }
1539
+ if ((promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens) != null) {
1540
+ providerMetadata.openai.cachedPromptTokens = promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens;
1541
+ }
1542
+ return {
1543
+ text: (_c = choice.message.content) != null ? _c : void 0,
1544
+ toolCalls: this.settings.useLegacyFunctionCalling && choice.message.function_call ? [
1545
+ {
1546
+ toolCallType: "function",
1547
+ toolCallId: generateId(),
1548
+ toolName: choice.message.function_call.name,
1549
+ args: choice.message.function_call.arguments
1550
+ }
1551
+ ] : (_d = choice.message.tool_calls) == null ? void 0 : _d.map((toolCall) => {
1552
+ var _a22;
1553
+ return {
1554
+ toolCallType: "function",
1555
+ toolCallId: (_a22 = toolCall.id) != null ? _a22 : generateId(),
1556
+ toolName: toolCall.function.name,
1557
+ args: toolCall.function.arguments
1558
+ };
1559
+ }),
1560
+ finishReason: mapOpenAIFinishReason(choice.finish_reason),
1561
+ usage: {
1562
+ promptTokens: (_f = (_e = response.usage) == null ? void 0 : _e.prompt_tokens) != null ? _f : NaN,
1563
+ completionTokens: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : NaN
1564
+ },
1565
+ rawCall: { rawPrompt, rawSettings },
1566
+ rawResponse: { headers: responseHeaders, body: rawResponse },
1567
+ request: { body: JSON.stringify(body) },
1568
+ response: getResponseMetadata(response),
1569
+ warnings,
1570
+ logprobs: mapOpenAIChatLogProbsOutput(choice.logprobs),
1571
+ providerMetadata
1572
+ };
1573
+ }
1574
+ async doStream(options) {
1575
+ if (this.settings.simulateStreaming) {
1576
+ const result = await this.doGenerate(options);
1577
+ const simulatedStream = new ReadableStream({
1578
+ start(controller) {
1579
+ controller.enqueue({ type: "response-metadata", ...result.response });
1580
+ if (result.text) {
1581
+ controller.enqueue({
1582
+ type: "text-delta",
1583
+ textDelta: result.text
1584
+ });
1585
+ }
1586
+ if (result.toolCalls) {
1587
+ for (const toolCall of result.toolCalls) {
1588
+ controller.enqueue({
1589
+ type: "tool-call-delta",
1590
+ toolCallType: "function",
1591
+ toolCallId: toolCall.toolCallId,
1592
+ toolName: toolCall.toolName,
1593
+ argsTextDelta: toolCall.args
1594
+ });
1595
+ controller.enqueue({
1596
+ type: "tool-call",
1597
+ ...toolCall
1598
+ });
1599
+ }
1600
+ }
1601
+ controller.enqueue({
1602
+ type: "finish",
1603
+ finishReason: result.finishReason,
1604
+ usage: result.usage,
1605
+ logprobs: result.logprobs,
1606
+ providerMetadata: result.providerMetadata
1607
+ });
1608
+ controller.close();
1609
+ }
1610
+ });
1611
+ return {
1612
+ stream: simulatedStream,
1613
+ rawCall: result.rawCall,
1614
+ rawResponse: result.rawResponse,
1615
+ warnings: result.warnings
1616
+ };
1617
+ }
1618
+ const { args, warnings } = this.getArgs(options);
1619
+ const body = {
1620
+ ...args,
1621
+ stream: true,
1622
+ // only include stream_options when in strict compatibility mode:
1623
+ stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
1624
+ };
1625
+ const { responseHeaders, value: response } = await postJsonToApi({
1626
+ url: this.config.url({
1627
+ path: "/chat/completions",
1628
+ modelId: this.modelId
1629
+ }),
1630
+ headers: combineHeaders(this.config.headers(), options.headers),
1631
+ body,
1632
+ failedResponseHandler: openaiFailedResponseHandler,
1633
+ successfulResponseHandler: createEventSourceResponseHandler(
1634
+ openaiChatChunkSchema
1635
+ ),
1636
+ abortSignal: options.abortSignal,
1637
+ fetch: this.config.fetch
1638
+ });
1639
+ const { messages: rawPrompt, ...rawSettings } = args;
1640
+ const toolCalls = [];
1641
+ let finishReason = "unknown";
1642
+ let usage = {
1643
+ promptTokens: void 0,
1644
+ completionTokens: void 0
1645
+ };
1646
+ let logprobs;
1647
+ let isFirstChunk = true;
1648
+ const { useLegacyFunctionCalling } = this.settings;
1649
+ const providerMetadata = { openai: {} };
1650
+ return {
1651
+ stream: response.pipeThrough(
1652
+ new TransformStream({
1653
+ transform(chunk, controller) {
1654
+ var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
1655
+ if (!chunk.success) {
1656
+ finishReason = "error";
1657
+ controller.enqueue({ type: "error", error: chunk.error });
1658
+ return;
1659
+ }
1660
+ const value = chunk.value;
1661
+ if ("error" in value) {
1662
+ finishReason = "error";
1663
+ controller.enqueue({ type: "error", error: value.error });
1664
+ return;
1665
+ }
1666
+ if (isFirstChunk) {
1667
+ isFirstChunk = false;
1668
+ controller.enqueue({
1669
+ type: "response-metadata",
1670
+ ...getResponseMetadata(value)
1671
+ });
1672
+ }
1673
+ if (value.usage != null) {
1674
+ const {
1675
+ prompt_tokens,
1676
+ completion_tokens,
1677
+ prompt_tokens_details,
1678
+ completion_tokens_details
1679
+ } = value.usage;
1680
+ usage = {
1681
+ promptTokens: prompt_tokens != null ? prompt_tokens : void 0,
1682
+ completionTokens: completion_tokens != null ? completion_tokens : void 0
1683
+ };
1684
+ if ((completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens) != null) {
1685
+ providerMetadata.openai.reasoningTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens;
1686
+ }
1687
+ if ((completion_tokens_details == null ? void 0 : completion_tokens_details.accepted_prediction_tokens) != null) {
1688
+ providerMetadata.openai.acceptedPredictionTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.accepted_prediction_tokens;
1689
+ }
1690
+ if ((completion_tokens_details == null ? void 0 : completion_tokens_details.rejected_prediction_tokens) != null) {
1691
+ providerMetadata.openai.rejectedPredictionTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.rejected_prediction_tokens;
1692
+ }
1693
+ if ((prompt_tokens_details == null ? void 0 : prompt_tokens_details.cached_tokens) != null) {
1694
+ providerMetadata.openai.cachedPromptTokens = prompt_tokens_details == null ? void 0 : prompt_tokens_details.cached_tokens;
1695
+ }
1696
+ }
1697
+ const choice = value.choices[0];
1698
+ if ((choice == null ? void 0 : choice.finish_reason) != null) {
1699
+ finishReason = mapOpenAIFinishReason(choice.finish_reason);
1700
+ }
1701
+ if ((choice == null ? void 0 : choice.delta) == null) {
1702
+ return;
1703
+ }
1704
+ const delta = choice.delta;
1705
+ if (delta.content != null) {
1706
+ controller.enqueue({
1707
+ type: "text-delta",
1708
+ textDelta: delta.content
1709
+ });
1710
+ }
1711
+ const mappedLogprobs = mapOpenAIChatLogProbsOutput(
1712
+ choice == null ? void 0 : choice.logprobs
1713
+ );
1714
+ if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
1715
+ if (logprobs === void 0) logprobs = [];
1716
+ logprobs.push(...mappedLogprobs);
1717
+ }
1718
+ const mappedToolCalls = useLegacyFunctionCalling && delta.function_call != null ? [
1719
+ {
1720
+ type: "function",
1721
+ id: generateId(),
1722
+ function: delta.function_call,
1723
+ index: 0
1724
+ }
1725
+ ] : delta.tool_calls;
1726
+ if (mappedToolCalls != null) {
1727
+ for (const toolCallDelta of mappedToolCalls) {
1728
+ const index = toolCallDelta.index;
1729
+ if (toolCalls[index] == null) {
1730
+ if (toolCallDelta.type !== "function") {
1731
+ throw new InvalidResponseDataError({
1732
+ data: toolCallDelta,
1733
+ message: `Expected 'function' type.`
1734
+ });
1735
+ }
1736
+ if (toolCallDelta.id == null) {
1737
+ throw new InvalidResponseDataError({
1738
+ data: toolCallDelta,
1739
+ message: `Expected 'id' to be a string.`
1740
+ });
1741
+ }
1742
+ if (((_a15 = toolCallDelta.function) == null ? void 0 : _a15.name) == null) {
1743
+ throw new InvalidResponseDataError({
1744
+ data: toolCallDelta,
1745
+ message: `Expected 'function.name' to be a string.`
1746
+ });
1747
+ }
1748
+ toolCalls[index] = {
1749
+ id: toolCallDelta.id,
1750
+ type: "function",
1751
+ function: {
1752
+ name: toolCallDelta.function.name,
1753
+ arguments: (_b = toolCallDelta.function.arguments) != null ? _b : ""
1754
+ },
1755
+ hasFinished: false
1756
+ };
1757
+ const toolCall2 = toolCalls[index];
1758
+ if (((_c = toolCall2.function) == null ? void 0 : _c.name) != null && ((_d = toolCall2.function) == null ? void 0 : _d.arguments) != null) {
1759
+ if (toolCall2.function.arguments.length > 0) {
1760
+ controller.enqueue({
1761
+ type: "tool-call-delta",
1762
+ toolCallType: "function",
1763
+ toolCallId: toolCall2.id,
1764
+ toolName: toolCall2.function.name,
1765
+ argsTextDelta: toolCall2.function.arguments
1766
+ });
1767
+ }
1768
+ if (isParsableJson(toolCall2.function.arguments)) {
1769
+ controller.enqueue({
1770
+ type: "tool-call",
1771
+ toolCallType: "function",
1772
+ toolCallId: (_e = toolCall2.id) != null ? _e : generateId(),
1773
+ toolName: toolCall2.function.name,
1774
+ args: toolCall2.function.arguments
1775
+ });
1776
+ toolCall2.hasFinished = true;
1777
+ }
1778
+ }
1779
+ continue;
1780
+ }
1781
+ const toolCall = toolCalls[index];
1782
+ if (toolCall.hasFinished) {
1783
+ continue;
1784
+ }
1785
+ if (((_f = toolCallDelta.function) == null ? void 0 : _f.arguments) != null) {
1786
+ toolCall.function.arguments += (_h = (_g = toolCallDelta.function) == null ? void 0 : _g.arguments) != null ? _h : "";
1787
+ }
1788
+ controller.enqueue({
1789
+ type: "tool-call-delta",
1790
+ toolCallType: "function",
1791
+ toolCallId: toolCall.id,
1792
+ toolName: toolCall.function.name,
1793
+ argsTextDelta: (_i = toolCallDelta.function.arguments) != null ? _i : ""
1794
+ });
1795
+ if (((_j = toolCall.function) == null ? void 0 : _j.name) != null && ((_k = toolCall.function) == null ? void 0 : _k.arguments) != null && isParsableJson(toolCall.function.arguments)) {
1796
+ controller.enqueue({
1797
+ type: "tool-call",
1798
+ toolCallType: "function",
1799
+ toolCallId: (_l = toolCall.id) != null ? _l : generateId(),
1800
+ toolName: toolCall.function.name,
1801
+ args: toolCall.function.arguments
1802
+ });
1803
+ toolCall.hasFinished = true;
1804
+ }
1805
+ }
1806
+ }
1807
+ },
1808
+ flush(controller) {
1809
+ var _a15, _b;
1810
+ controller.enqueue({
1811
+ type: "finish",
1812
+ finishReason,
1813
+ logprobs,
1814
+ usage: {
1815
+ promptTokens: (_a15 = usage.promptTokens) != null ? _a15 : NaN,
1816
+ completionTokens: (_b = usage.completionTokens) != null ? _b : NaN
1817
+ },
1818
+ ...providerMetadata != null ? { providerMetadata } : {}
1819
+ });
1820
+ }
1821
+ })
1822
+ ),
1823
+ rawCall: { rawPrompt, rawSettings },
1824
+ rawResponse: { headers: responseHeaders },
1825
+ request: { body: JSON.stringify(body) },
1826
+ warnings
1827
+ };
1828
+ }
1829
+ };
1830
+ var openaiTokenUsageSchema = z.object({
1831
+ prompt_tokens: z.number().nullish(),
1832
+ completion_tokens: z.number().nullish(),
1833
+ prompt_tokens_details: z.object({
1834
+ cached_tokens: z.number().nullish()
1835
+ }).nullish(),
1836
+ completion_tokens_details: z.object({
1837
+ reasoning_tokens: z.number().nullish(),
1838
+ accepted_prediction_tokens: z.number().nullish(),
1839
+ rejected_prediction_tokens: z.number().nullish()
1840
+ }).nullish()
1841
+ }).nullish();
1842
+ var openaiChatResponseSchema = z.object({
1843
+ id: z.string().nullish(),
1844
+ created: z.number().nullish(),
1845
+ model: z.string().nullish(),
1846
+ choices: z.array(
1847
+ z.object({
1848
+ message: z.object({
1849
+ role: z.literal("assistant").nullish(),
1850
+ content: z.string().nullish(),
1851
+ function_call: z.object({
1852
+ arguments: z.string(),
1853
+ name: z.string()
1854
+ }).nullish(),
1855
+ tool_calls: z.array(
1856
+ z.object({
1857
+ id: z.string().nullish(),
1858
+ type: z.literal("function"),
1859
+ function: z.object({
1860
+ name: z.string(),
1861
+ arguments: z.string()
1862
+ })
1863
+ })
1864
+ ).nullish()
1865
+ }),
1866
+ index: z.number(),
1867
+ logprobs: z.object({
1868
+ content: z.array(
1869
+ z.object({
1870
+ token: z.string(),
1871
+ logprob: z.number(),
1872
+ top_logprobs: z.array(
1873
+ z.object({
1874
+ token: z.string(),
1875
+ logprob: z.number()
1876
+ })
1877
+ )
1878
+ })
1879
+ ).nullable()
1880
+ }).nullish(),
1881
+ finish_reason: z.string().nullish()
1882
+ })
1883
+ ),
1884
+ usage: openaiTokenUsageSchema
1885
+ });
1886
+ var openaiChatChunkSchema = z.union([
1887
+ z.object({
1888
+ id: z.string().nullish(),
1889
+ created: z.number().nullish(),
1890
+ model: z.string().nullish(),
1891
+ choices: z.array(
1892
+ z.object({
1893
+ delta: z.object({
1894
+ role: z.enum(["assistant"]).nullish(),
1895
+ content: z.string().nullish(),
1896
+ function_call: z.object({
1897
+ name: z.string().optional(),
1898
+ arguments: z.string().optional()
1899
+ }).nullish(),
1900
+ tool_calls: z.array(
1901
+ z.object({
1902
+ index: z.number(),
1903
+ id: z.string().nullish(),
1904
+ type: z.literal("function").optional(),
1905
+ function: z.object({
1906
+ name: z.string().nullish(),
1907
+ arguments: z.string().nullish()
1908
+ })
1909
+ })
1910
+ ).nullish()
1911
+ }).nullish(),
1912
+ logprobs: z.object({
1913
+ content: z.array(
1914
+ z.object({
1915
+ token: z.string(),
1916
+ logprob: z.number(),
1917
+ top_logprobs: z.array(
1918
+ z.object({
1919
+ token: z.string(),
1920
+ logprob: z.number()
1921
+ })
1922
+ )
1923
+ })
1924
+ ).nullable()
1925
+ }).nullish(),
1926
+ finish_reason: z.string().nullable().optional(),
1927
+ index: z.number()
1928
+ })
1929
+ ),
1930
+ usage: openaiTokenUsageSchema
1931
+ }),
1932
+ openaiErrorDataSchema
1933
+ ]);
1934
+ function isReasoningModel(modelId) {
1935
+ return modelId === "o1" || modelId.startsWith("o1-") || modelId === "o3" || modelId.startsWith("o3-");
1936
+ }
1937
+ function isAudioModel(modelId) {
1938
+ return modelId.startsWith("gpt-4o-audio-preview");
1939
+ }
1940
+ function getSystemMessageMode(modelId) {
1941
+ var _a15, _b;
1942
+ if (!isReasoningModel(modelId)) {
1943
+ return "system";
1944
+ }
1945
+ return (_b = (_a15 = reasoningModels[modelId]) == null ? void 0 : _a15.systemMessageMode) != null ? _b : "developer";
1946
+ }
1947
+ var reasoningModels = {
1948
+ "o1-mini": {
1949
+ systemMessageMode: "remove"
1950
+ },
1951
+ "o1-mini-2024-09-12": {
1952
+ systemMessageMode: "remove"
1953
+ },
1954
+ "o1-preview": {
1955
+ systemMessageMode: "remove"
1956
+ },
1957
+ "o1-preview-2024-09-12": {
1958
+ systemMessageMode: "remove"
1959
+ },
1960
+ "o3-mini": {
1961
+ systemMessageMode: "developer"
1962
+ },
1963
+ "o3-mini-2025-01-31": {
1964
+ systemMessageMode: "developer"
1965
+ }
1966
+ };
1967
+ function convertToOpenAICompletionPrompt({
1968
+ prompt,
1969
+ inputFormat,
1970
+ user = "user",
1971
+ assistant = "assistant"
1972
+ }) {
1973
+ if (inputFormat === "prompt" && prompt.length === 1 && prompt[0].role === "user" && prompt[0].content.length === 1 && prompt[0].content[0].type === "text") {
1974
+ return { prompt: prompt[0].content[0].text };
1975
+ }
1976
+ let text = "";
1977
+ if (prompt[0].role === "system") {
1978
+ text += `${prompt[0].content}
1979
+
1980
+ `;
1981
+ prompt = prompt.slice(1);
1982
+ }
1983
+ for (const { role, content } of prompt) {
1984
+ switch (role) {
1985
+ case "system": {
1986
+ throw new InvalidPromptError({
1987
+ message: "Unexpected system message in prompt: ${content}",
1988
+ prompt
1989
+ });
1990
+ }
1991
+ case "user": {
1992
+ const userMessage = content.map((part) => {
1993
+ switch (part.type) {
1994
+ case "text": {
1995
+ return part.text;
1996
+ }
1997
+ case "image": {
1998
+ throw new UnsupportedFunctionalityError({
1999
+ functionality: "images"
2000
+ });
2001
+ }
2002
+ }
2003
+ }).join("");
2004
+ text += `${user}:
2005
+ ${userMessage}
2006
+
2007
+ `;
2008
+ break;
2009
+ }
2010
+ case "assistant": {
2011
+ const assistantMessage = content.map((part) => {
2012
+ switch (part.type) {
2013
+ case "text": {
2014
+ return part.text;
2015
+ }
2016
+ case "tool-call": {
2017
+ throw new UnsupportedFunctionalityError({
2018
+ functionality: "tool-call messages"
2019
+ });
2020
+ }
2021
+ }
2022
+ }).join("");
2023
+ text += `${assistant}:
2024
+ ${assistantMessage}
2025
+
2026
+ `;
2027
+ break;
2028
+ }
2029
+ case "tool": {
2030
+ throw new UnsupportedFunctionalityError({
2031
+ functionality: "tool messages"
2032
+ });
2033
+ }
2034
+ default: {
2035
+ const _exhaustiveCheck = role;
2036
+ throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
2037
+ }
2038
+ }
2039
+ }
2040
+ text += `${assistant}:
2041
+ `;
2042
+ return {
2043
+ prompt: text,
2044
+ stopSequences: [`
2045
+ ${user}:`]
2046
+ };
2047
+ }
2048
+ function mapOpenAICompletionLogProbs(logprobs) {
2049
+ return logprobs == null ? void 0 : logprobs.tokens.map((token, index) => ({
2050
+ token,
2051
+ logprob: logprobs.token_logprobs[index],
2052
+ topLogprobs: logprobs.top_logprobs ? Object.entries(logprobs.top_logprobs[index]).map(
2053
+ ([token2, logprob]) => ({
2054
+ token: token2,
2055
+ logprob
2056
+ })
2057
+ ) : []
2058
+ }));
2059
+ }
2060
+ var OpenAICompletionLanguageModel = class {
2061
+ constructor(modelId, settings, config) {
2062
+ this.specificationVersion = "v1";
2063
+ this.defaultObjectGenerationMode = void 0;
2064
+ this.modelId = modelId;
2065
+ this.settings = settings;
2066
+ this.config = config;
2067
+ }
2068
+ get provider() {
2069
+ return this.config.provider;
2070
+ }
2071
+ getArgs({
2072
+ mode,
2073
+ inputFormat,
2074
+ prompt,
2075
+ maxTokens,
2076
+ temperature,
2077
+ topP,
2078
+ topK,
2079
+ frequencyPenalty,
2080
+ presencePenalty,
2081
+ stopSequences: userStopSequences,
2082
+ responseFormat,
2083
+ seed
2084
+ }) {
2085
+ var _a15;
2086
+ const type = mode.type;
2087
+ const warnings = [];
2088
+ if (topK != null) {
2089
+ warnings.push({
2090
+ type: "unsupported-setting",
2091
+ setting: "topK"
2092
+ });
2093
+ }
2094
+ if (responseFormat != null && responseFormat.type !== "text") {
2095
+ warnings.push({
2096
+ type: "unsupported-setting",
2097
+ setting: "responseFormat",
2098
+ details: "JSON response format is not supported."
2099
+ });
2100
+ }
2101
+ const { prompt: completionPrompt, stopSequences } = convertToOpenAICompletionPrompt({ prompt, inputFormat });
2102
+ const stop = [...stopSequences != null ? stopSequences : [], ...userStopSequences != null ? userStopSequences : []];
2103
+ const baseArgs = {
2104
+ // model id:
2105
+ model: this.modelId,
2106
+ // model specific settings:
2107
+ echo: this.settings.echo,
2108
+ logit_bias: this.settings.logitBias,
2109
+ logprobs: typeof this.settings.logprobs === "number" ? this.settings.logprobs : typeof this.settings.logprobs === "boolean" ? this.settings.logprobs ? 0 : void 0 : void 0,
2110
+ suffix: this.settings.suffix,
2111
+ user: this.settings.user,
2112
+ // standardized settings:
2113
+ max_tokens: maxTokens,
2114
+ temperature,
2115
+ top_p: topP,
2116
+ frequency_penalty: frequencyPenalty,
2117
+ presence_penalty: presencePenalty,
2118
+ seed,
2119
+ // prompt:
2120
+ prompt: completionPrompt,
2121
+ // stop sequences:
2122
+ stop: stop.length > 0 ? stop : void 0
2123
+ };
2124
+ switch (type) {
2125
+ case "regular": {
2126
+ if ((_a15 = mode.tools) == null ? void 0 : _a15.length) {
2127
+ throw new UnsupportedFunctionalityError({
2128
+ functionality: "tools"
2129
+ });
2130
+ }
2131
+ if (mode.toolChoice) {
2132
+ throw new UnsupportedFunctionalityError({
2133
+ functionality: "toolChoice"
2134
+ });
2135
+ }
2136
+ return { args: baseArgs, warnings };
2137
+ }
2138
+ case "object-json": {
2139
+ throw new UnsupportedFunctionalityError({
2140
+ functionality: "object-json mode"
2141
+ });
2142
+ }
2143
+ case "object-tool": {
2144
+ throw new UnsupportedFunctionalityError({
2145
+ functionality: "object-tool mode"
2146
+ });
2147
+ }
2148
+ default: {
2149
+ const _exhaustiveCheck = type;
2150
+ throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
2151
+ }
2152
+ }
2153
+ }
2154
+ async doGenerate(options) {
2155
+ const { args, warnings } = this.getArgs(options);
2156
+ const {
2157
+ responseHeaders,
2158
+ value: response,
2159
+ rawValue: rawResponse
2160
+ } = await postJsonToApi({
2161
+ url: this.config.url({
2162
+ path: "/completions",
2163
+ modelId: this.modelId
2164
+ }),
2165
+ headers: combineHeaders(this.config.headers(), options.headers),
2166
+ body: args,
2167
+ failedResponseHandler: openaiFailedResponseHandler,
2168
+ successfulResponseHandler: createJsonResponseHandler(
2169
+ openaiCompletionResponseSchema
2170
+ ),
2171
+ abortSignal: options.abortSignal,
2172
+ fetch: this.config.fetch
2173
+ });
2174
+ const { prompt: rawPrompt, ...rawSettings } = args;
2175
+ const choice = response.choices[0];
2176
+ return {
2177
+ text: choice.text,
2178
+ usage: {
2179
+ promptTokens: response.usage.prompt_tokens,
2180
+ completionTokens: response.usage.completion_tokens
2181
+ },
2182
+ finishReason: mapOpenAIFinishReason(choice.finish_reason),
2183
+ logprobs: mapOpenAICompletionLogProbs(choice.logprobs),
2184
+ rawCall: { rawPrompt, rawSettings },
2185
+ rawResponse: { headers: responseHeaders, body: rawResponse },
2186
+ response: getResponseMetadata(response),
2187
+ warnings,
2188
+ request: { body: JSON.stringify(args) }
2189
+ };
2190
+ }
2191
+ async doStream(options) {
2192
+ const { args, warnings } = this.getArgs(options);
2193
+ const body = {
2194
+ ...args,
2195
+ stream: true,
2196
+ // only include stream_options when in strict compatibility mode:
2197
+ stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
2198
+ };
2199
+ const { responseHeaders, value: response } = await postJsonToApi({
2200
+ url: this.config.url({
2201
+ path: "/completions",
2202
+ modelId: this.modelId
2203
+ }),
2204
+ headers: combineHeaders(this.config.headers(), options.headers),
2205
+ body,
2206
+ failedResponseHandler: openaiFailedResponseHandler,
2207
+ successfulResponseHandler: createEventSourceResponseHandler(
2208
+ openaiCompletionChunkSchema
2209
+ ),
2210
+ abortSignal: options.abortSignal,
2211
+ fetch: this.config.fetch
2212
+ });
2213
+ const { prompt: rawPrompt, ...rawSettings } = args;
2214
+ let finishReason = "unknown";
2215
+ let usage = {
2216
+ promptTokens: Number.NaN,
2217
+ completionTokens: Number.NaN
2218
+ };
2219
+ let logprobs;
2220
+ let isFirstChunk = true;
2221
+ return {
2222
+ stream: response.pipeThrough(
2223
+ new TransformStream({
2224
+ transform(chunk, controller) {
2225
+ if (!chunk.success) {
2226
+ finishReason = "error";
2227
+ controller.enqueue({ type: "error", error: chunk.error });
2228
+ return;
2229
+ }
2230
+ const value = chunk.value;
2231
+ if ("error" in value) {
2232
+ finishReason = "error";
2233
+ controller.enqueue({ type: "error", error: value.error });
2234
+ return;
2235
+ }
2236
+ if (isFirstChunk) {
2237
+ isFirstChunk = false;
2238
+ controller.enqueue({
2239
+ type: "response-metadata",
2240
+ ...getResponseMetadata(value)
2241
+ });
2242
+ }
2243
+ if (value.usage != null) {
2244
+ usage = {
2245
+ promptTokens: value.usage.prompt_tokens,
2246
+ completionTokens: value.usage.completion_tokens
2247
+ };
2248
+ }
2249
+ const choice = value.choices[0];
2250
+ if ((choice == null ? void 0 : choice.finish_reason) != null) {
2251
+ finishReason = mapOpenAIFinishReason(choice.finish_reason);
2252
+ }
2253
+ if ((choice == null ? void 0 : choice.text) != null) {
2254
+ controller.enqueue({
2255
+ type: "text-delta",
2256
+ textDelta: choice.text
2257
+ });
2258
+ }
2259
+ const mappedLogprobs = mapOpenAICompletionLogProbs(
2260
+ choice == null ? void 0 : choice.logprobs
2261
+ );
2262
+ if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
2263
+ if (logprobs === void 0) logprobs = [];
2264
+ logprobs.push(...mappedLogprobs);
2265
+ }
2266
+ },
2267
+ flush(controller) {
2268
+ controller.enqueue({
2269
+ type: "finish",
2270
+ finishReason,
2271
+ logprobs,
2272
+ usage
2273
+ });
2274
+ }
2275
+ })
2276
+ ),
2277
+ rawCall: { rawPrompt, rawSettings },
2278
+ rawResponse: { headers: responseHeaders },
2279
+ warnings,
2280
+ request: { body: JSON.stringify(body) }
2281
+ };
2282
+ }
2283
+ };
2284
+ var openaiCompletionResponseSchema = z.object({
2285
+ id: z.string().nullish(),
2286
+ created: z.number().nullish(),
2287
+ model: z.string().nullish(),
2288
+ choices: z.array(
2289
+ z.object({
2290
+ text: z.string(),
2291
+ finish_reason: z.string(),
2292
+ logprobs: z.object({
2293
+ tokens: z.array(z.string()),
2294
+ token_logprobs: z.array(z.number()),
2295
+ top_logprobs: z.array(z.record(z.string(), z.number())).nullable()
2296
+ }).nullish()
2297
+ })
2298
+ ),
2299
+ usage: z.object({
2300
+ prompt_tokens: z.number(),
2301
+ completion_tokens: z.number()
2302
+ })
2303
+ });
2304
+ var openaiCompletionChunkSchema = z.union([
2305
+ z.object({
2306
+ id: z.string().nullish(),
2307
+ created: z.number().nullish(),
2308
+ model: z.string().nullish(),
2309
+ choices: z.array(
2310
+ z.object({
2311
+ text: z.string(),
2312
+ finish_reason: z.string().nullish(),
2313
+ index: z.number(),
2314
+ logprobs: z.object({
2315
+ tokens: z.array(z.string()),
2316
+ token_logprobs: z.array(z.number()),
2317
+ top_logprobs: z.array(z.record(z.string(), z.number())).nullable()
2318
+ }).nullish()
2319
+ })
2320
+ ),
2321
+ usage: z.object({
2322
+ prompt_tokens: z.number(),
2323
+ completion_tokens: z.number()
2324
+ }).nullish()
2325
+ }),
2326
+ openaiErrorDataSchema
2327
+ ]);
2328
+ var OpenAIEmbeddingModel = class {
2329
+ constructor(modelId, settings, config) {
2330
+ this.specificationVersion = "v1";
2331
+ this.modelId = modelId;
2332
+ this.settings = settings;
2333
+ this.config = config;
2334
+ }
2335
+ get provider() {
2336
+ return this.config.provider;
2337
+ }
2338
+ get maxEmbeddingsPerCall() {
2339
+ var _a15;
2340
+ return (_a15 = this.settings.maxEmbeddingsPerCall) != null ? _a15 : 2048;
2341
+ }
2342
+ get supportsParallelCalls() {
2343
+ var _a15;
2344
+ return (_a15 = this.settings.supportsParallelCalls) != null ? _a15 : true;
2345
+ }
2346
+ async doEmbed({
2347
+ values,
2348
+ headers,
2349
+ abortSignal
2350
+ }) {
2351
+ if (values.length > this.maxEmbeddingsPerCall) {
2352
+ throw new TooManyEmbeddingValuesForCallError({
2353
+ provider: this.provider,
2354
+ modelId: this.modelId,
2355
+ maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
2356
+ values
2357
+ });
2358
+ }
2359
+ const { responseHeaders, value: response } = await postJsonToApi({
2360
+ url: this.config.url({
2361
+ path: "/embeddings",
2362
+ modelId: this.modelId
2363
+ }),
2364
+ headers: combineHeaders(this.config.headers(), headers),
2365
+ body: {
2366
+ model: this.modelId,
2367
+ input: values,
2368
+ encoding_format: "float",
2369
+ dimensions: this.settings.dimensions,
2370
+ user: this.settings.user
2371
+ },
2372
+ failedResponseHandler: openaiFailedResponseHandler,
2373
+ successfulResponseHandler: createJsonResponseHandler(
2374
+ openaiTextEmbeddingResponseSchema
2375
+ ),
2376
+ abortSignal,
2377
+ fetch: this.config.fetch
2378
+ });
2379
+ return {
2380
+ embeddings: response.data.map((item) => item.embedding),
2381
+ usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
2382
+ rawResponse: { headers: responseHeaders }
2383
+ };
2384
+ }
2385
+ };
2386
+ var openaiTextEmbeddingResponseSchema = z.object({
2387
+ data: z.array(z.object({ embedding: z.array(z.number()) })),
2388
+ usage: z.object({ prompt_tokens: z.number() }).nullish()
2389
+ });
2390
+ var modelMaxImagesPerCall = {
2391
+ "dall-e-3": 1,
2392
+ "dall-e-2": 10
2393
+ };
2394
+ var OpenAIImageModel = class {
2395
+ constructor(modelId, settings, config) {
2396
+ this.modelId = modelId;
2397
+ this.settings = settings;
2398
+ this.config = config;
2399
+ this.specificationVersion = "v1";
2400
+ }
2401
+ get maxImagesPerCall() {
2402
+ var _a15, _b;
2403
+ return (_b = (_a15 = this.settings.maxImagesPerCall) != null ? _a15 : modelMaxImagesPerCall[this.modelId]) != null ? _b : 1;
2404
+ }
2405
+ get provider() {
2406
+ return this.config.provider;
2407
+ }
2408
+ async doGenerate({
2409
+ prompt,
2410
+ n,
2411
+ size,
2412
+ aspectRatio,
2413
+ seed,
2414
+ providerOptions,
2415
+ headers,
2416
+ abortSignal
2417
+ }) {
2418
+ var _a15, _b, _c, _d;
2419
+ const warnings = [];
2420
+ if (aspectRatio != null) {
2421
+ warnings.push({
2422
+ type: "unsupported-setting",
2423
+ setting: "aspectRatio",
2424
+ details: "This model does not support aspect ratio. Use `size` instead."
2425
+ });
2426
+ }
2427
+ if (seed != null) {
2428
+ warnings.push({ type: "unsupported-setting", setting: "seed" });
2429
+ }
2430
+ const currentDate = (_c = (_b = (_a15 = this.config._internal) == null ? void 0 : _a15.currentDate) == null ? void 0 : _b.call(_a15)) != null ? _c : /* @__PURE__ */ new Date();
2431
+ const { value: response, responseHeaders } = await postJsonToApi({
2432
+ url: this.config.url({
2433
+ path: "/images/generations",
2434
+ modelId: this.modelId
2435
+ }),
2436
+ headers: combineHeaders(this.config.headers(), headers),
2437
+ body: {
2438
+ model: this.modelId,
2439
+ prompt,
2440
+ n,
2441
+ size,
2442
+ ...(_d = providerOptions.openai) != null ? _d : {},
2443
+ response_format: "b64_json"
2444
+ },
2445
+ failedResponseHandler: openaiFailedResponseHandler,
2446
+ successfulResponseHandler: createJsonResponseHandler(
2447
+ openaiImageResponseSchema
2448
+ ),
2449
+ abortSignal,
2450
+ fetch: this.config.fetch
2451
+ });
2452
+ return {
2453
+ images: response.data.map((item) => item.b64_json),
2454
+ warnings,
2455
+ response: {
2456
+ timestamp: currentDate,
2457
+ modelId: this.modelId,
2458
+ headers: responseHeaders
2459
+ }
2460
+ };
2461
+ }
2462
+ };
2463
+ var openaiImageResponseSchema = z.object({
2464
+ data: z.array(z.object({ b64_json: z.string() }))
2465
+ });
2466
+ var OpenAIProviderOptionsSchema = z.object({
2467
+ include: z.array(z.string()).optional().describe(
2468
+ "Additional information to include in the transcription response."
2469
+ ),
2470
+ language: z.string().optional().describe("The language of the input audio in ISO-639-1 format."),
2471
+ prompt: z.string().optional().describe(
2472
+ "An optional text to guide the model's style or continue a previous audio segment."
2473
+ ),
2474
+ temperature: z.number().min(0).max(1).optional().default(0).describe("The sampling temperature, between 0 and 1."),
2475
+ timestampGranularities: z.array(z.enum(["word", "segment"])).optional().default(["segment"]).describe(
2476
+ "The timestamp granularities to populate for this transcription."
2477
+ )
2478
+ });
2479
+ var languageMap = {
2480
+ afrikaans: "af",
2481
+ arabic: "ar",
2482
+ armenian: "hy",
2483
+ azerbaijani: "az",
2484
+ belarusian: "be",
2485
+ bosnian: "bs",
2486
+ bulgarian: "bg",
2487
+ catalan: "ca",
2488
+ chinese: "zh",
2489
+ croatian: "hr",
2490
+ czech: "cs",
2491
+ danish: "da",
2492
+ dutch: "nl",
2493
+ english: "en",
2494
+ estonian: "et",
2495
+ finnish: "fi",
2496
+ french: "fr",
2497
+ galician: "gl",
2498
+ german: "de",
2499
+ greek: "el",
2500
+ hebrew: "he",
2501
+ hindi: "hi",
2502
+ hungarian: "hu",
2503
+ icelandic: "is",
2504
+ indonesian: "id",
2505
+ italian: "it",
2506
+ japanese: "ja",
2507
+ kannada: "kn",
2508
+ kazakh: "kk",
2509
+ korean: "ko",
2510
+ latvian: "lv",
2511
+ lithuanian: "lt",
2512
+ macedonian: "mk",
2513
+ malay: "ms",
2514
+ marathi: "mr",
2515
+ maori: "mi",
2516
+ nepali: "ne",
2517
+ norwegian: "no",
2518
+ persian: "fa",
2519
+ polish: "pl",
2520
+ portuguese: "pt",
2521
+ romanian: "ro",
2522
+ russian: "ru",
2523
+ serbian: "sr",
2524
+ slovak: "sk",
2525
+ slovenian: "sl",
2526
+ spanish: "es",
2527
+ swahili: "sw",
2528
+ swedish: "sv",
2529
+ tagalog: "tl",
2530
+ tamil: "ta",
2531
+ thai: "th",
2532
+ turkish: "tr",
2533
+ ukrainian: "uk",
2534
+ urdu: "ur",
2535
+ vietnamese: "vi",
2536
+ welsh: "cy"
2537
+ };
2538
+ var OpenAITranscriptionModel = class {
2539
+ constructor(modelId, config) {
2540
+ this.modelId = modelId;
2541
+ this.config = config;
2542
+ this.specificationVersion = "v1";
2543
+ }
2544
+ get provider() {
2545
+ return this.config.provider;
2546
+ }
2547
+ getArgs({
2548
+ audio,
2549
+ mediaType,
2550
+ providerOptions
2551
+ }) {
2552
+ const warnings = [];
2553
+ const openAIOptions = parseProviderOptions({
2554
+ provider: "openai",
2555
+ providerOptions,
2556
+ schema: OpenAIProviderOptionsSchema
2557
+ });
2558
+ const formData = new FormData();
2559
+ const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([convertBase64ToUint8Array(audio)]);
2560
+ formData.append("model", this.modelId);
2561
+ formData.append("file", new File([blob], "audio", { type: mediaType }));
2562
+ if (openAIOptions) {
2563
+ const transcriptionModelOptions = {
2564
+ include: openAIOptions.include,
2565
+ language: openAIOptions.language,
2566
+ prompt: openAIOptions.prompt,
2567
+ temperature: openAIOptions.temperature,
2568
+ timestamp_granularities: openAIOptions.timestampGranularities
2569
+ };
2570
+ for (const key in transcriptionModelOptions) {
2571
+ const value = transcriptionModelOptions[key];
2572
+ if (value !== void 0) {
2573
+ formData.append(key, value);
2574
+ }
2575
+ }
2576
+ }
2577
+ return {
2578
+ formData,
2579
+ warnings
2580
+ };
2581
+ }
2582
+ async doGenerate(options) {
2583
+ var _a15, _b, _c, _d, _e, _f;
2584
+ const currentDate = (_c = (_b = (_a15 = this.config._internal) == null ? void 0 : _a15.currentDate) == null ? void 0 : _b.call(_a15)) != null ? _c : /* @__PURE__ */ new Date();
2585
+ const { formData, warnings } = this.getArgs(options);
2586
+ const {
2587
+ value: response,
2588
+ responseHeaders,
2589
+ rawValue: rawResponse
2590
+ } = await postFormDataToApi({
2591
+ url: this.config.url({
2592
+ path: "/audio/transcriptions",
2593
+ modelId: this.modelId
2594
+ }),
2595
+ headers: combineHeaders(this.config.headers(), options.headers),
2596
+ formData,
2597
+ failedResponseHandler: openaiFailedResponseHandler,
2598
+ successfulResponseHandler: createJsonResponseHandler(
2599
+ openaiTranscriptionResponseSchema
2600
+ ),
2601
+ abortSignal: options.abortSignal,
2602
+ fetch: this.config.fetch
2603
+ });
2604
+ const language = response.language != null && response.language in languageMap ? languageMap[response.language] : void 0;
2605
+ return {
2606
+ text: response.text,
2607
+ segments: (_e = (_d = response.words) == null ? void 0 : _d.map((word) => ({
2608
+ text: word.word,
2609
+ startSecond: word.start,
2610
+ endSecond: word.end
2611
+ }))) != null ? _e : [],
2612
+ language,
2613
+ durationInSeconds: (_f = response.duration) != null ? _f : void 0,
2614
+ warnings,
2615
+ response: {
2616
+ timestamp: currentDate,
2617
+ modelId: this.modelId,
2618
+ headers: responseHeaders,
2619
+ body: rawResponse
2620
+ }
2621
+ };
2622
+ }
2623
+ };
2624
+ var openaiTranscriptionResponseSchema = z.object({
2625
+ text: z.string(),
2626
+ language: z.string().nullish(),
2627
+ duration: z.number().nullish(),
2628
+ words: z.array(
2629
+ z.object({
2630
+ word: z.string(),
2631
+ start: z.number(),
2632
+ end: z.number()
2633
+ })
2634
+ ).nullish()
2635
+ });
2636
+ function convertToOpenAIResponsesMessages({
2637
+ prompt,
2638
+ systemMessageMode
2639
+ }) {
2640
+ const messages = [];
2641
+ const warnings = [];
2642
+ for (const { role, content } of prompt) {
2643
+ switch (role) {
2644
+ case "system": {
2645
+ switch (systemMessageMode) {
2646
+ case "system": {
2647
+ messages.push({ role: "system", content });
2648
+ break;
2649
+ }
2650
+ case "developer": {
2651
+ messages.push({ role: "developer", content });
2652
+ break;
2653
+ }
2654
+ case "remove": {
2655
+ warnings.push({
2656
+ type: "other",
2657
+ message: "system messages are removed for this model"
2658
+ });
2659
+ break;
2660
+ }
2661
+ default: {
2662
+ const _exhaustiveCheck = systemMessageMode;
2663
+ throw new Error(
2664
+ `Unsupported system message mode: ${_exhaustiveCheck}`
2665
+ );
2666
+ }
2667
+ }
2668
+ break;
2669
+ }
2670
+ case "user": {
2671
+ messages.push({
2672
+ role: "user",
2673
+ content: content.map((part, index) => {
2674
+ var _a15, _b, _c, _d;
2675
+ switch (part.type) {
2676
+ case "text": {
2677
+ return { type: "input_text", text: part.text };
2678
+ }
2679
+ case "image": {
2680
+ return {
2681
+ type: "input_image",
2682
+ image_url: part.image instanceof URL ? part.image.toString() : `data:${(_a15 = part.mimeType) != null ? _a15 : "image/jpeg"};base64,${convertUint8ArrayToBase64(part.image)}`,
2683
+ // OpenAI specific extension: image detail
2684
+ detail: (_c = (_b = part.providerMetadata) == null ? void 0 : _b.openai) == null ? void 0 : _c.imageDetail
2685
+ };
2686
+ }
2687
+ case "file": {
2688
+ if (part.data instanceof URL) {
2689
+ throw new UnsupportedFunctionalityError({
2690
+ functionality: "File URLs in user messages"
2691
+ });
2692
+ }
2693
+ switch (part.mimeType) {
2694
+ case "application/pdf": {
2695
+ return {
2696
+ type: "input_file",
2697
+ filename: (_d = part.filename) != null ? _d : `part-${index}.pdf`,
2698
+ file_data: `data:application/pdf;base64,${part.data}`
2699
+ };
2700
+ }
2701
+ default: {
2702
+ throw new UnsupportedFunctionalityError({
2703
+ functionality: "Only PDF files are supported in user messages"
2704
+ });
2705
+ }
2706
+ }
2707
+ }
2708
+ }
2709
+ })
2710
+ });
2711
+ break;
2712
+ }
2713
+ case "assistant": {
2714
+ for (const part of content) {
2715
+ switch (part.type) {
2716
+ case "text": {
2717
+ messages.push({
2718
+ role: "assistant",
2719
+ content: [{ type: "output_text", text: part.text }]
2720
+ });
2721
+ break;
2722
+ }
2723
+ case "tool-call": {
2724
+ messages.push({
2725
+ type: "function_call",
2726
+ call_id: part.toolCallId,
2727
+ name: part.toolName,
2728
+ arguments: JSON.stringify(part.args)
2729
+ });
2730
+ break;
2731
+ }
2732
+ }
2733
+ }
2734
+ break;
2735
+ }
2736
+ case "tool": {
2737
+ for (const part of content) {
2738
+ messages.push({
2739
+ type: "function_call_output",
2740
+ call_id: part.toolCallId,
2741
+ output: JSON.stringify(part.result)
2742
+ });
2743
+ }
2744
+ break;
2745
+ }
2746
+ default: {
2747
+ const _exhaustiveCheck = role;
2748
+ throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
2749
+ }
2750
+ }
2751
+ }
2752
+ return { messages, warnings };
2753
+ }
2754
+ function mapOpenAIResponseFinishReason({
2755
+ finishReason,
2756
+ hasToolCalls
2757
+ }) {
2758
+ switch (finishReason) {
2759
+ case void 0:
2760
+ case null:
2761
+ return hasToolCalls ? "tool-calls" : "stop";
2762
+ case "max_output_tokens":
2763
+ return "length";
2764
+ case "content_filter":
2765
+ return "content-filter";
2766
+ default:
2767
+ return hasToolCalls ? "tool-calls" : "unknown";
2768
+ }
2769
+ }
2770
+ function prepareResponsesTools({
2771
+ mode,
2772
+ strict
2773
+ }) {
2774
+ var _a15;
2775
+ const tools = ((_a15 = mode.tools) == null ? void 0 : _a15.length) ? mode.tools : void 0;
2776
+ const toolWarnings = [];
2777
+ if (tools == null) {
2778
+ return { tools: void 0, tool_choice: void 0, toolWarnings };
2779
+ }
2780
+ const toolChoice = mode.toolChoice;
2781
+ const openaiTools2 = [];
2782
+ for (const tool of tools) {
2783
+ switch (tool.type) {
2784
+ case "function":
2785
+ openaiTools2.push({
2786
+ type: "function",
2787
+ name: tool.name,
2788
+ description: tool.description,
2789
+ parameters: tool.parameters,
2790
+ strict: strict ? true : void 0
2791
+ });
2792
+ break;
2793
+ case "provider-defined":
2794
+ switch (tool.id) {
2795
+ case "openai.web_search_preview":
2796
+ openaiTools2.push({
2797
+ type: "web_search_preview",
2798
+ search_context_size: tool.args.searchContextSize,
2799
+ user_location: tool.args.userLocation
2800
+ });
2801
+ break;
2802
+ default:
2803
+ toolWarnings.push({ type: "unsupported-tool", tool });
2804
+ break;
2805
+ }
2806
+ break;
2807
+ default:
2808
+ toolWarnings.push({ type: "unsupported-tool", tool });
2809
+ break;
2810
+ }
2811
+ }
2812
+ if (toolChoice == null) {
2813
+ return { tools: openaiTools2, tool_choice: void 0, toolWarnings };
2814
+ }
2815
+ const type = toolChoice.type;
2816
+ switch (type) {
2817
+ case "auto":
2818
+ case "none":
2819
+ case "required":
2820
+ return { tools: openaiTools2, tool_choice: type, toolWarnings };
2821
+ case "tool": {
2822
+ if (toolChoice.toolName === "web_search_preview") {
2823
+ return {
2824
+ tools: openaiTools2,
2825
+ tool_choice: {
2826
+ type: "web_search_preview"
2827
+ },
2828
+ toolWarnings
2829
+ };
2830
+ }
2831
+ return {
2832
+ tools: openaiTools2,
2833
+ tool_choice: {
2834
+ type: "function",
2835
+ name: toolChoice.toolName
2836
+ },
2837
+ toolWarnings
2838
+ };
2839
+ }
2840
+ default: {
2841
+ const _exhaustiveCheck = type;
2842
+ throw new UnsupportedFunctionalityError({
2843
+ functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
2844
+ });
2845
+ }
2846
+ }
2847
+ }
2848
+ var OpenAIResponsesLanguageModel = class {
2849
+ constructor(modelId, config) {
2850
+ this.specificationVersion = "v1";
2851
+ this.defaultObjectGenerationMode = "json";
2852
+ this.modelId = modelId;
2853
+ this.config = config;
2854
+ }
2855
+ get provider() {
2856
+ return this.config.provider;
2857
+ }
2858
+ getArgs({
2859
+ mode,
2860
+ maxTokens,
2861
+ temperature,
2862
+ stopSequences,
2863
+ topP,
2864
+ topK,
2865
+ presencePenalty,
2866
+ frequencyPenalty,
2867
+ seed,
2868
+ prompt,
2869
+ providerMetadata,
2870
+ responseFormat
2871
+ }) {
2872
+ var _a15, _b, _c;
2873
+ const warnings = [];
2874
+ const modelConfig = getResponsesModelConfig(this.modelId);
2875
+ const type = mode.type;
2876
+ if (topK != null) {
2877
+ warnings.push({
2878
+ type: "unsupported-setting",
2879
+ setting: "topK"
2880
+ });
2881
+ }
2882
+ if (seed != null) {
2883
+ warnings.push({
2884
+ type: "unsupported-setting",
2885
+ setting: "seed"
2886
+ });
2887
+ }
2888
+ if (presencePenalty != null) {
2889
+ warnings.push({
2890
+ type: "unsupported-setting",
2891
+ setting: "presencePenalty"
2892
+ });
2893
+ }
2894
+ if (frequencyPenalty != null) {
2895
+ warnings.push({
2896
+ type: "unsupported-setting",
2897
+ setting: "frequencyPenalty"
2898
+ });
2899
+ }
2900
+ if (stopSequences != null) {
2901
+ warnings.push({
2902
+ type: "unsupported-setting",
2903
+ setting: "stopSequences"
2904
+ });
2905
+ }
2906
+ const { messages, warnings: messageWarnings } = convertToOpenAIResponsesMessages({
2907
+ prompt,
2908
+ systemMessageMode: modelConfig.systemMessageMode
2909
+ });
2910
+ warnings.push(...messageWarnings);
2911
+ const openaiOptions = parseProviderOptions({
2912
+ provider: "openai",
2913
+ providerOptions: providerMetadata,
2914
+ schema: openaiResponsesProviderOptionsSchema
2915
+ });
2916
+ const isStrict = (_a15 = openaiOptions == null ? void 0 : openaiOptions.strictSchemas) != null ? _a15 : true;
2917
+ const baseArgs = {
2918
+ model: this.modelId,
2919
+ input: messages,
2920
+ temperature,
2921
+ top_p: topP,
2922
+ max_output_tokens: maxTokens,
2923
+ ...(responseFormat == null ? void 0 : responseFormat.type) === "json" && {
2924
+ text: {
2925
+ format: responseFormat.schema != null ? {
2926
+ type: "json_schema",
2927
+ strict: isStrict,
2928
+ name: (_b = responseFormat.name) != null ? _b : "response",
2929
+ description: responseFormat.description,
2930
+ schema: responseFormat.schema
2931
+ } : { type: "json_object" }
2932
+ }
2933
+ },
2934
+ // provider options:
2935
+ metadata: openaiOptions == null ? void 0 : openaiOptions.metadata,
2936
+ parallel_tool_calls: openaiOptions == null ? void 0 : openaiOptions.parallelToolCalls,
2937
+ previous_response_id: openaiOptions == null ? void 0 : openaiOptions.previousResponseId,
2938
+ store: openaiOptions == null ? void 0 : openaiOptions.store,
2939
+ user: openaiOptions == null ? void 0 : openaiOptions.user,
2940
+ instructions: openaiOptions == null ? void 0 : openaiOptions.instructions,
2941
+ // model-specific settings:
2942
+ ...modelConfig.isReasoningModel && (openaiOptions == null ? void 0 : openaiOptions.reasoningEffort) != null && {
2943
+ reasoning: { effort: openaiOptions == null ? void 0 : openaiOptions.reasoningEffort }
2944
+ },
2945
+ ...modelConfig.requiredAutoTruncation && {
2946
+ truncation: "auto"
2947
+ }
2948
+ };
2949
+ if (modelConfig.isReasoningModel) {
2950
+ if (baseArgs.temperature != null) {
2951
+ baseArgs.temperature = void 0;
2952
+ warnings.push({
2953
+ type: "unsupported-setting",
2954
+ setting: "temperature",
2955
+ details: "temperature is not supported for reasoning models"
2956
+ });
2957
+ }
2958
+ if (baseArgs.top_p != null) {
2959
+ baseArgs.top_p = void 0;
2960
+ warnings.push({
2961
+ type: "unsupported-setting",
2962
+ setting: "topP",
2963
+ details: "topP is not supported for reasoning models"
2964
+ });
2965
+ }
2966
+ }
2967
+ switch (type) {
2968
+ case "regular": {
2969
+ const { tools, tool_choice, toolWarnings } = prepareResponsesTools({
2970
+ mode,
2971
+ strict: isStrict
2972
+ // TODO support provider options on tools
2973
+ });
2974
+ return {
2975
+ args: {
2976
+ ...baseArgs,
2977
+ tools,
2978
+ tool_choice
2979
+ },
2980
+ warnings: [...warnings, ...toolWarnings]
2981
+ };
2982
+ }
2983
+ case "object-json": {
2984
+ return {
2985
+ args: {
2986
+ ...baseArgs,
2987
+ text: {
2988
+ format: mode.schema != null ? {
2989
+ type: "json_schema",
2990
+ strict: isStrict,
2991
+ name: (_c = mode.name) != null ? _c : "response",
2992
+ description: mode.description,
2993
+ schema: mode.schema
2994
+ } : { type: "json_object" }
2995
+ }
2996
+ },
2997
+ warnings
2998
+ };
2999
+ }
3000
+ case "object-tool": {
3001
+ return {
3002
+ args: {
3003
+ ...baseArgs,
3004
+ tool_choice: { type: "function", name: mode.tool.name },
3005
+ tools: [
3006
+ {
3007
+ type: "function",
3008
+ name: mode.tool.name,
3009
+ description: mode.tool.description,
3010
+ parameters: mode.tool.parameters,
3011
+ strict: isStrict
3012
+ }
3013
+ ]
3014
+ },
3015
+ warnings
3016
+ };
3017
+ }
3018
+ default: {
3019
+ const _exhaustiveCheck = type;
3020
+ throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
3021
+ }
3022
+ }
3023
+ }
3024
+ async doGenerate(options) {
3025
+ var _a15, _b, _c, _d, _e;
3026
+ const { args: body, warnings } = this.getArgs(options);
3027
+ const {
3028
+ responseHeaders,
3029
+ value: response,
3030
+ rawValue: rawResponse
3031
+ } = await postJsonToApi({
3032
+ url: this.config.url({
3033
+ path: "/responses",
3034
+ modelId: this.modelId
3035
+ }),
3036
+ headers: combineHeaders(this.config.headers(), options.headers),
3037
+ body,
3038
+ failedResponseHandler: openaiFailedResponseHandler,
3039
+ successfulResponseHandler: createJsonResponseHandler(
3040
+ z.object({
3041
+ id: z.string(),
3042
+ created_at: z.number(),
3043
+ model: z.string(),
3044
+ output: z.array(
3045
+ z.discriminatedUnion("type", [
3046
+ z.object({
3047
+ type: z.literal("message"),
3048
+ role: z.literal("assistant"),
3049
+ content: z.array(
3050
+ z.object({
3051
+ type: z.literal("output_text"),
3052
+ text: z.string(),
3053
+ annotations: z.array(
3054
+ z.object({
3055
+ type: z.literal("url_citation"),
3056
+ start_index: z.number(),
3057
+ end_index: z.number(),
3058
+ url: z.string(),
3059
+ title: z.string()
3060
+ })
3061
+ )
3062
+ })
3063
+ )
3064
+ }),
3065
+ z.object({
3066
+ type: z.literal("function_call"),
3067
+ call_id: z.string(),
3068
+ name: z.string(),
3069
+ arguments: z.string()
3070
+ }),
3071
+ z.object({
3072
+ type: z.literal("web_search_call")
3073
+ }),
3074
+ z.object({
3075
+ type: z.literal("computer_call")
3076
+ }),
3077
+ z.object({
3078
+ type: z.literal("reasoning")
3079
+ })
3080
+ ])
3081
+ ),
3082
+ incomplete_details: z.object({ reason: z.string() }).nullable(),
3083
+ usage: usageSchema
3084
+ })
3085
+ ),
3086
+ abortSignal: options.abortSignal,
3087
+ fetch: this.config.fetch
3088
+ });
3089
+ const outputTextElements = response.output.filter((output) => output.type === "message").flatMap((output) => output.content).filter((content) => content.type === "output_text");
3090
+ const toolCalls = response.output.filter((output) => output.type === "function_call").map((output) => ({
3091
+ toolCallType: "function",
3092
+ toolCallId: output.call_id,
3093
+ toolName: output.name,
3094
+ args: output.arguments
3095
+ }));
3096
+ return {
3097
+ text: outputTextElements.map((content) => content.text).join("\n"),
3098
+ sources: outputTextElements.flatMap(
3099
+ (content) => content.annotations.map((annotation) => {
3100
+ var _a22, _b2, _c2;
3101
+ return {
3102
+ sourceType: "url",
3103
+ id: (_c2 = (_b2 = (_a22 = this.config).generateId) == null ? void 0 : _b2.call(_a22)) != null ? _c2 : generateId(),
3104
+ url: annotation.url,
3105
+ title: annotation.title
3106
+ };
3107
+ })
3108
+ ),
3109
+ finishReason: mapOpenAIResponseFinishReason({
3110
+ finishReason: (_a15 = response.incomplete_details) == null ? void 0 : _a15.reason,
3111
+ hasToolCalls: toolCalls.length > 0
3112
+ }),
3113
+ toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
3114
+ usage: {
3115
+ promptTokens: response.usage.input_tokens,
3116
+ completionTokens: response.usage.output_tokens
3117
+ },
3118
+ rawCall: {
3119
+ rawPrompt: void 0,
3120
+ rawSettings: {}
3121
+ },
3122
+ rawResponse: {
3123
+ headers: responseHeaders,
3124
+ body: rawResponse
3125
+ },
3126
+ request: {
3127
+ body: JSON.stringify(body)
3128
+ },
3129
+ response: {
3130
+ id: response.id,
3131
+ timestamp: new Date(response.created_at * 1e3),
3132
+ modelId: response.model
3133
+ },
3134
+ providerMetadata: {
3135
+ openai: {
3136
+ responseId: response.id,
3137
+ cachedPromptTokens: (_c = (_b = response.usage.input_tokens_details) == null ? void 0 : _b.cached_tokens) != null ? _c : null,
3138
+ reasoningTokens: (_e = (_d = response.usage.output_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : null
3139
+ }
3140
+ },
3141
+ warnings
3142
+ };
3143
+ }
3144
+ async doStream(options) {
3145
+ const { args: body, warnings } = this.getArgs(options);
3146
+ const { responseHeaders, value: response } = await postJsonToApi({
3147
+ url: this.config.url({
3148
+ path: "/responses",
3149
+ modelId: this.modelId
3150
+ }),
3151
+ headers: combineHeaders(this.config.headers(), options.headers),
3152
+ body: {
3153
+ ...body,
3154
+ stream: true
3155
+ },
3156
+ failedResponseHandler: openaiFailedResponseHandler,
3157
+ successfulResponseHandler: createEventSourceResponseHandler(
3158
+ openaiResponsesChunkSchema
3159
+ ),
3160
+ abortSignal: options.abortSignal,
3161
+ fetch: this.config.fetch
3162
+ });
3163
+ const self = this;
3164
+ let finishReason = "unknown";
3165
+ let promptTokens = NaN;
3166
+ let completionTokens = NaN;
3167
+ let cachedPromptTokens = null;
3168
+ let reasoningTokens = null;
3169
+ let responseId = null;
3170
+ const ongoingToolCalls = {};
3171
+ let hasToolCalls = false;
3172
+ return {
3173
+ stream: response.pipeThrough(
3174
+ new TransformStream({
3175
+ transform(chunk, controller) {
3176
+ var _a15, _b, _c, _d, _e, _f, _g, _h;
3177
+ if (!chunk.success) {
3178
+ finishReason = "error";
3179
+ controller.enqueue({ type: "error", error: chunk.error });
3180
+ return;
3181
+ }
3182
+ const value = chunk.value;
3183
+ if (isResponseOutputItemAddedChunk(value)) {
3184
+ if (value.item.type === "function_call") {
3185
+ ongoingToolCalls[value.output_index] = {
3186
+ toolName: value.item.name,
3187
+ toolCallId: value.item.call_id
3188
+ };
3189
+ controller.enqueue({
3190
+ type: "tool-call-delta",
3191
+ toolCallType: "function",
3192
+ toolCallId: value.item.call_id,
3193
+ toolName: value.item.name,
3194
+ argsTextDelta: value.item.arguments
3195
+ });
3196
+ }
3197
+ } else if (isResponseFunctionCallArgumentsDeltaChunk(value)) {
3198
+ const toolCall = ongoingToolCalls[value.output_index];
3199
+ if (toolCall != null) {
3200
+ controller.enqueue({
3201
+ type: "tool-call-delta",
3202
+ toolCallType: "function",
3203
+ toolCallId: toolCall.toolCallId,
3204
+ toolName: toolCall.toolName,
3205
+ argsTextDelta: value.delta
3206
+ });
3207
+ }
3208
+ } else if (isResponseCreatedChunk(value)) {
3209
+ responseId = value.response.id;
3210
+ controller.enqueue({
3211
+ type: "response-metadata",
3212
+ id: value.response.id,
3213
+ timestamp: new Date(value.response.created_at * 1e3),
3214
+ modelId: value.response.model
3215
+ });
3216
+ } else if (isTextDeltaChunk(value)) {
3217
+ controller.enqueue({
3218
+ type: "text-delta",
3219
+ textDelta: value.delta
3220
+ });
3221
+ } else if (isResponseOutputItemDoneChunk(value) && value.item.type === "function_call") {
3222
+ ongoingToolCalls[value.output_index] = void 0;
3223
+ hasToolCalls = true;
3224
+ controller.enqueue({
3225
+ type: "tool-call",
3226
+ toolCallType: "function",
3227
+ toolCallId: value.item.call_id,
3228
+ toolName: value.item.name,
3229
+ args: value.item.arguments
3230
+ });
3231
+ } else if (isResponseFinishedChunk(value)) {
3232
+ finishReason = mapOpenAIResponseFinishReason({
3233
+ finishReason: (_a15 = value.response.incomplete_details) == null ? void 0 : _a15.reason,
3234
+ hasToolCalls
3235
+ });
3236
+ promptTokens = value.response.usage.input_tokens;
3237
+ completionTokens = value.response.usage.output_tokens;
3238
+ cachedPromptTokens = (_c = (_b = value.response.usage.input_tokens_details) == null ? void 0 : _b.cached_tokens) != null ? _c : cachedPromptTokens;
3239
+ reasoningTokens = (_e = (_d = value.response.usage.output_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : reasoningTokens;
3240
+ } else if (isResponseAnnotationAddedChunk(value)) {
3241
+ controller.enqueue({
3242
+ type: "source",
3243
+ source: {
3244
+ sourceType: "url",
3245
+ id: (_h = (_g = (_f = self.config).generateId) == null ? void 0 : _g.call(_f)) != null ? _h : generateId(),
3246
+ url: value.annotation.url,
3247
+ title: value.annotation.title
3248
+ }
3249
+ });
3250
+ }
3251
+ },
3252
+ flush(controller) {
3253
+ controller.enqueue({
3254
+ type: "finish",
3255
+ finishReason,
3256
+ usage: { promptTokens, completionTokens },
3257
+ ...(cachedPromptTokens != null || reasoningTokens != null) && {
3258
+ providerMetadata: {
3259
+ openai: {
3260
+ responseId,
3261
+ cachedPromptTokens,
3262
+ reasoningTokens
3263
+ }
3264
+ }
3265
+ }
3266
+ });
3267
+ }
3268
+ })
3269
+ ),
3270
+ rawCall: {
3271
+ rawPrompt: void 0,
3272
+ rawSettings: {}
3273
+ },
3274
+ rawResponse: { headers: responseHeaders },
3275
+ request: { body: JSON.stringify(body) },
3276
+ warnings
3277
+ };
3278
+ }
3279
+ };
3280
+ var usageSchema = z.object({
3281
+ input_tokens: z.number(),
3282
+ input_tokens_details: z.object({ cached_tokens: z.number().nullish() }).nullish(),
3283
+ output_tokens: z.number(),
3284
+ output_tokens_details: z.object({ reasoning_tokens: z.number().nullish() }).nullish()
3285
+ });
3286
+ var textDeltaChunkSchema = z.object({
3287
+ type: z.literal("response.output_text.delta"),
3288
+ delta: z.string()
3289
+ });
3290
+ var responseFinishedChunkSchema = z.object({
3291
+ type: z.enum(["response.completed", "response.incomplete"]),
3292
+ response: z.object({
3293
+ incomplete_details: z.object({ reason: z.string() }).nullish(),
3294
+ usage: usageSchema
3295
+ })
3296
+ });
3297
+ var responseCreatedChunkSchema = z.object({
3298
+ type: z.literal("response.created"),
3299
+ response: z.object({
3300
+ id: z.string(),
3301
+ created_at: z.number(),
3302
+ model: z.string()
3303
+ })
3304
+ });
3305
+ var responseOutputItemDoneSchema = z.object({
3306
+ type: z.literal("response.output_item.done"),
3307
+ output_index: z.number(),
3308
+ item: z.discriminatedUnion("type", [
3309
+ z.object({
3310
+ type: z.literal("message")
3311
+ }),
3312
+ z.object({
3313
+ type: z.literal("function_call"),
3314
+ id: z.string(),
3315
+ call_id: z.string(),
3316
+ name: z.string(),
3317
+ arguments: z.string(),
3318
+ status: z.literal("completed")
3319
+ })
3320
+ ])
3321
+ });
3322
+ var responseFunctionCallArgumentsDeltaSchema = z.object({
3323
+ type: z.literal("response.function_call_arguments.delta"),
3324
+ item_id: z.string(),
3325
+ output_index: z.number(),
3326
+ delta: z.string()
3327
+ });
3328
+ var responseOutputItemAddedSchema = z.object({
3329
+ type: z.literal("response.output_item.added"),
3330
+ output_index: z.number(),
3331
+ item: z.discriminatedUnion("type", [
3332
+ z.object({
3333
+ type: z.literal("message")
3334
+ }),
3335
+ z.object({
3336
+ type: z.literal("function_call"),
3337
+ id: z.string(),
3338
+ call_id: z.string(),
3339
+ name: z.string(),
3340
+ arguments: z.string()
3341
+ })
3342
+ ])
3343
+ });
3344
+ var responseAnnotationAddedSchema = z.object({
3345
+ type: z.literal("response.output_text.annotation.added"),
3346
+ annotation: z.object({
3347
+ type: z.literal("url_citation"),
3348
+ url: z.string(),
3349
+ title: z.string()
3350
+ })
3351
+ });
3352
+ var openaiResponsesChunkSchema = z.union([
3353
+ textDeltaChunkSchema,
3354
+ responseFinishedChunkSchema,
3355
+ responseCreatedChunkSchema,
3356
+ responseOutputItemDoneSchema,
3357
+ responseFunctionCallArgumentsDeltaSchema,
3358
+ responseOutputItemAddedSchema,
3359
+ responseAnnotationAddedSchema,
3360
+ z.object({ type: z.string() }).passthrough()
3361
+ // fallback for unknown chunks
3362
+ ]);
3363
+ function isTextDeltaChunk(chunk) {
3364
+ return chunk.type === "response.output_text.delta";
3365
+ }
3366
+ function isResponseOutputItemDoneChunk(chunk) {
3367
+ return chunk.type === "response.output_item.done";
3368
+ }
3369
+ function isResponseFinishedChunk(chunk) {
3370
+ return chunk.type === "response.completed" || chunk.type === "response.incomplete";
3371
+ }
3372
+ function isResponseCreatedChunk(chunk) {
3373
+ return chunk.type === "response.created";
3374
+ }
3375
+ function isResponseFunctionCallArgumentsDeltaChunk(chunk) {
3376
+ return chunk.type === "response.function_call_arguments.delta";
3377
+ }
3378
+ function isResponseOutputItemAddedChunk(chunk) {
3379
+ return chunk.type === "response.output_item.added";
3380
+ }
3381
+ function isResponseAnnotationAddedChunk(chunk) {
3382
+ return chunk.type === "response.output_text.annotation.added";
3383
+ }
3384
+ function getResponsesModelConfig(modelId) {
3385
+ if (modelId.startsWith("o")) {
3386
+ if (modelId.startsWith("o1-mini") || modelId.startsWith("o1-preview")) {
3387
+ return {
3388
+ isReasoningModel: true,
3389
+ systemMessageMode: "remove",
3390
+ requiredAutoTruncation: false
3391
+ };
3392
+ }
3393
+ return {
3394
+ isReasoningModel: true,
3395
+ systemMessageMode: "developer",
3396
+ requiredAutoTruncation: false
3397
+ };
3398
+ }
3399
+ return {
3400
+ isReasoningModel: false,
3401
+ systemMessageMode: "system",
3402
+ requiredAutoTruncation: false
3403
+ };
3404
+ }
3405
+ var openaiResponsesProviderOptionsSchema = z.object({
3406
+ metadata: z.any().nullish(),
3407
+ parallelToolCalls: z.boolean().nullish(),
3408
+ previousResponseId: z.string().nullish(),
3409
+ store: z.boolean().nullish(),
3410
+ user: z.string().nullish(),
3411
+ reasoningEffort: z.string().nullish(),
3412
+ strictSchemas: z.boolean().nullish(),
3413
+ instructions: z.string().nullish()
3414
+ });
3415
+ var WebSearchPreviewParameters = z.object({});
3416
+ function webSearchPreviewTool({
3417
+ searchContextSize,
3418
+ userLocation
3419
+ } = {}) {
3420
+ return {
3421
+ type: "provider-defined",
3422
+ id: "openai.web_search_preview",
3423
+ args: {
3424
+ searchContextSize,
3425
+ userLocation
3426
+ },
3427
+ parameters: WebSearchPreviewParameters
3428
+ };
3429
+ }
3430
+ var openaiTools = {
3431
+ webSearchPreview: webSearchPreviewTool
3432
+ };
3433
+ function createOpenAI(options = {}) {
3434
+ var _a15, _b, _c;
3435
+ const baseURL = (_a15 = withoutTrailingSlash(options.baseURL)) != null ? _a15 : "https://api.openai.com/v1";
3436
+ const compatibility = (_b = options.compatibility) != null ? _b : "compatible";
3437
+ const providerName = (_c = options.name) != null ? _c : "openai";
3438
+ const getHeaders = () => ({
3439
+ Authorization: `Bearer ${loadApiKey({
3440
+ apiKey: options.apiKey,
3441
+ environmentVariableName: "OPENAI_API_KEY",
3442
+ description: "OpenAI"
3443
+ })}`,
3444
+ "OpenAI-Organization": options.organization,
3445
+ "OpenAI-Project": options.project,
3446
+ ...options.headers
3447
+ });
3448
+ const createChatModel = (modelId, settings = {}) => new OpenAIChatLanguageModel(modelId, settings, {
3449
+ provider: `${providerName}.chat`,
3450
+ url: ({ path }) => `${baseURL}${path}`,
3451
+ headers: getHeaders,
3452
+ compatibility,
3453
+ fetch: options.fetch
3454
+ });
3455
+ const createCompletionModel = (modelId, settings = {}) => new OpenAICompletionLanguageModel(modelId, settings, {
3456
+ provider: `${providerName}.completion`,
3457
+ url: ({ path }) => `${baseURL}${path}`,
3458
+ headers: getHeaders,
3459
+ compatibility,
3460
+ fetch: options.fetch
3461
+ });
3462
+ const createEmbeddingModel = (modelId, settings = {}) => new OpenAIEmbeddingModel(modelId, settings, {
3463
+ provider: `${providerName}.embedding`,
3464
+ url: ({ path }) => `${baseURL}${path}`,
3465
+ headers: getHeaders,
3466
+ fetch: options.fetch
3467
+ });
3468
+ const createImageModel = (modelId, settings = {}) => new OpenAIImageModel(modelId, settings, {
3469
+ provider: `${providerName}.image`,
3470
+ url: ({ path }) => `${baseURL}${path}`,
3471
+ headers: getHeaders,
3472
+ fetch: options.fetch
3473
+ });
3474
+ const createTranscriptionModel = (modelId) => new OpenAITranscriptionModel(modelId, {
3475
+ provider: `${providerName}.transcription`,
3476
+ url: ({ path }) => `${baseURL}${path}`,
3477
+ headers: getHeaders,
3478
+ fetch: options.fetch
3479
+ });
3480
+ const createLanguageModel = (modelId, settings) => {
3481
+ if (new.target) {
3482
+ throw new Error(
3483
+ "The OpenAI model function cannot be called with the new keyword."
3484
+ );
3485
+ }
3486
+ if (modelId === "gpt-3.5-turbo-instruct") {
3487
+ return createCompletionModel(
3488
+ modelId,
3489
+ settings
3490
+ );
3491
+ }
3492
+ return createChatModel(modelId, settings);
3493
+ };
3494
+ const createResponsesModel = (modelId) => {
3495
+ return new OpenAIResponsesLanguageModel(modelId, {
3496
+ provider: `${providerName}.responses`,
3497
+ url: ({ path }) => `${baseURL}${path}`,
3498
+ headers: getHeaders,
3499
+ fetch: options.fetch
3500
+ });
3501
+ };
3502
+ const provider = function(modelId, settings) {
3503
+ return createLanguageModel(modelId, settings);
3504
+ };
3505
+ provider.languageModel = createLanguageModel;
3506
+ provider.chat = createChatModel;
3507
+ provider.completion = createCompletionModel;
3508
+ provider.responses = createResponsesModel;
3509
+ provider.embedding = createEmbeddingModel;
3510
+ provider.textEmbedding = createEmbeddingModel;
3511
+ provider.textEmbeddingModel = createEmbeddingModel;
3512
+ provider.image = createImageModel;
3513
+ provider.imageModel = createImageModel;
3514
+ provider.transcription = createTranscriptionModel;
3515
+ provider.transcriptionModel = createTranscriptionModel;
3516
+ provider.tools = openaiTools;
3517
+ return provider;
3518
+ }
3519
+ createOpenAI({
3520
+ compatibility: "strict"
3521
+ // strict for OpenAI API
3522
+ });
3523
+
3524
+ // src/document/extractors/types.ts
3525
+ var STRIP_REGEX = /(\r\n|\n|\r)/gm;
3526
+ var openai2 = createOpenAI({ apiKey: process.env.OPENAI_API_KEY });
3527
+ var baseLLM = openai2("gpt-4o");
3528
+
3529
+ // src/document/extractors/title.ts
3530
+ var TitleExtractor = class extends BaseExtractor {
3531
+ /**
3532
+ * MastraLanguageModel instance.
3533
+ * @type {MastraLanguageModel}
3534
+ */
3535
+ llm;
3536
+ /**
3537
+ * Can work for mixture of text and non-text nodes
3538
+ * @type {boolean}
3539
+ * @default false
3540
+ */
3541
+ isTextNodeOnly = false;
3542
+ /**
3543
+ * Number of nodes to extrct titles from.
3544
+ * @type {number}
3545
+ * @default 5
3546
+ */
3547
+ nodes = 5;
3548
+ /**
3549
+ * The prompt template to use for the title extractor.
3550
+ * @type {string}
3551
+ */
3552
+ nodeTemplate;
3553
+ /**
3554
+ * The prompt template to merge title with..
3555
+ * @type {string}
3556
+ */
3557
+ combineTemplate;
3558
+ /**
3559
+ * Constructor for the TitleExtractor class.
3560
+ * @param {MastraLanguageModel} llm MastraLanguageModel instance.
3561
+ * @param {number} nodes Number of nodes to extract titles from.
3562
+ * @param {TitleExtractorPrompt} nodeTemplate The prompt template to use for the title extractor.
3563
+ * @param {string} combineTemplate The prompt template to merge title with..
3564
+ */
3565
+ constructor(options) {
3566
+ super();
3567
+ this.llm = options?.llm ?? baseLLM;
3568
+ this.nodes = options?.nodes ?? 5;
3569
+ this.nodeTemplate = options?.nodeTemplate ? new PromptTemplate({
3570
+ templateVars: ["context"],
3571
+ template: options.nodeTemplate
3572
+ }) : defaultTitleExtractorPromptTemplate;
3573
+ this.combineTemplate = options?.combineTemplate ? new PromptTemplate({
3574
+ templateVars: ["context"],
3575
+ template: options.combineTemplate
3576
+ }) : defaultTitleCombinePromptTemplate;
3577
+ }
3578
+ /**
3579
+ * Extract titles from a list of nodes.
3580
+ * @param {BaseNode[]} nodes Nodes to extract titles from.
3581
+ * @returns {Promise<BaseNode<ExtractTitle>[]>} Titles extracted from the nodes.
3582
+ */
3583
+ async extract(nodes) {
3584
+ const results = new Array(nodes.length);
3585
+ const nodesToExtractTitle = [];
3586
+ const nodeIndexes = [];
3587
+ nodes.forEach((node, idx) => {
3588
+ const text = node.getContent(this.metadataMode);
3589
+ if (!text || text.trim() === "") {
3590
+ results[idx] = { documentTitle: "" };
3591
+ } else {
3592
+ nodesToExtractTitle.push(node);
3593
+ nodeIndexes.push(idx);
3594
+ }
3595
+ });
3596
+ if (nodesToExtractTitle.length) {
3597
+ const filteredNodes = this.filterNodes(nodesToExtractTitle);
3598
+ if (filteredNodes.length) {
3599
+ const nodesByDocument = this.separateNodesByDocument(filteredNodes);
3600
+ const titlesByDocument = await this.extractTitles(nodesByDocument);
3601
+ filteredNodes.forEach((node, i) => {
3602
+ const nodeIndex = nodeIndexes[i];
3603
+ const groupKey = node.sourceNode?.nodeId ?? node.id_;
3604
+ if (typeof nodeIndex === "number") {
3605
+ results[nodeIndex] = {
3606
+ documentTitle: titlesByDocument[groupKey] ?? ""
3607
+ };
3608
+ }
3609
+ });
3610
+ }
3611
+ }
3612
+ return results;
3613
+ }
3614
+ filterNodes(nodes) {
3615
+ return nodes.filter((node) => {
3616
+ if (this.isTextNodeOnly && !(node instanceof TextNode)) {
3617
+ return false;
3618
+ }
3619
+ return true;
3620
+ });
3621
+ }
3622
+ separateNodesByDocument(nodes) {
3623
+ const nodesByDocument = {};
3624
+ for (const node of nodes) {
3625
+ const groupKey = node.sourceNode?.nodeId ?? node.id_;
3626
+ nodesByDocument[groupKey] = nodesByDocument[groupKey] || [];
3627
+ nodesByDocument[groupKey].push(node);
3628
+ }
3629
+ return nodesByDocument;
3630
+ }
3631
+ async extractTitles(nodesByDocument) {
3632
+ const titlesByDocument = {};
3633
+ for (const [key, nodes] of Object.entries(nodesByDocument)) {
3634
+ const titleCandidates = await this.getTitlesCandidates(nodes);
3635
+ const combinedTitles = titleCandidates.join(", ");
3636
+ const completion = await this.llm.doGenerate({
3637
+ inputFormat: "messages",
3638
+ mode: { type: "regular" },
3639
+ prompt: [
3640
+ {
3641
+ role: "user",
3642
+ content: [
3643
+ {
3644
+ type: "text",
3645
+ text: this.combineTemplate.format({
3646
+ context: combinedTitles
3647
+ })
3648
+ }
3649
+ ]
3650
+ }
3651
+ ]
3652
+ });
3653
+ let title = "";
3654
+ if (typeof completion.text === "string") {
3655
+ title = completion.text.trim();
3656
+ } else {
3657
+ console.warn("Title extraction LLM output was not a string:", completion.text);
3658
+ }
3659
+ titlesByDocument[key] = title;
3660
+ }
3661
+ return titlesByDocument;
3662
+ }
3663
+ async getTitlesCandidates(nodes) {
3664
+ const titleJobs = nodes.map(async (node) => {
3665
+ const completion = await this.llm.doGenerate({
3666
+ inputFormat: "messages",
3667
+ mode: { type: "regular" },
3668
+ prompt: [
3669
+ {
3670
+ role: "user",
3671
+ content: [
3672
+ {
3673
+ type: "text",
3674
+ text: this.nodeTemplate.format({
3675
+ context: node.getContent(MetadataMode.ALL)
3676
+ })
3677
+ }
3678
+ ]
3679
+ }
3680
+ ]
3681
+ });
3682
+ if (typeof completion.text === "string") {
3683
+ return completion.text.trim();
3684
+ } else {
3685
+ console.warn("Title candidate extraction LLM output was not a string:", completion.text);
3686
+ return "";
3687
+ }
3688
+ });
3689
+ return await Promise.all(titleJobs);
3690
+ }
3691
+ };
3692
+ var SummaryExtractor = class extends BaseExtractor {
3693
+ /**
3694
+ * MastraLanguageModel instance.
3695
+ * @type {MastraLanguageModel}
3696
+ */
3697
+ llm;
3698
+ /**
3699
+ * List of summaries to extract: 'self', 'prev', 'next'
3700
+ * @type {string[]}
3701
+ */
3702
+ summaries;
3703
+ /**
3704
+ * The prompt template to use for the summary extractor.
3705
+ * @type {string}
3706
+ */
3707
+ promptTemplate;
3708
+ selfSummary;
3709
+ prevSummary;
3710
+ nextSummary;
3711
+ constructor(options) {
3712
+ const summaries = options?.summaries ?? ["self"];
3713
+ if (summaries && !summaries.some((s) => ["self", "prev", "next"].includes(s)))
3714
+ throw new Error("Summaries must be one of 'self', 'prev', 'next'");
3715
+ super();
3716
+ this.llm = options?.llm ?? baseLLM;
3717
+ this.summaries = summaries;
3718
+ this.promptTemplate = options?.promptTemplate ? new PromptTemplate({
3719
+ templateVars: ["context"],
3720
+ template: options.promptTemplate
3721
+ }) : defaultSummaryPrompt;
3722
+ this.selfSummary = summaries?.includes("self") ?? false;
3723
+ this.prevSummary = summaries?.includes("prev") ?? false;
3724
+ this.nextSummary = summaries?.includes("next") ?? false;
3725
+ }
3726
+ /**
3727
+ * Extract summary from a node.
3728
+ * @param {BaseNode} node Node to extract summary from.
3729
+ * @returns {Promise<string>} Summary extracted from the node.
3730
+ */
3731
+ async generateNodeSummary(node) {
3732
+ const text = node.getContent(this.metadataMode);
3733
+ if (!text || text.trim() === "") {
3734
+ return "";
3735
+ }
3736
+ if (this.isTextNodeOnly && !(node instanceof TextNode)) {
3737
+ return "";
3738
+ }
3739
+ const context = node.getContent(this.metadataMode);
3740
+ const prompt = this.promptTemplate.format({
3741
+ context
3742
+ });
3743
+ const result = await this.llm.doGenerate({
3744
+ inputFormat: "messages",
3745
+ mode: { type: "regular" },
3746
+ prompt: [
3747
+ {
3748
+ role: "user",
3749
+ content: [{ type: "text", text: prompt }]
3750
+ }
3751
+ ]
3752
+ });
3753
+ let summary = "";
3754
+ if (typeof result.text === "string") {
3755
+ summary = result.text.trim();
3756
+ } else {
3757
+ console.warn("Summary extraction LLM output was not a string:", result.text);
3758
+ }
3759
+ return summary.replace(STRIP_REGEX, "");
3760
+ }
3761
+ /**
3762
+ * Extract summaries from a list of nodes.
3763
+ * @param {BaseNode[]} nodes Nodes to extract summaries from.
3764
+ * @returns {Promise<ExtractSummary[]>} Summaries extracted from the nodes.
3765
+ */
3766
+ async extract(nodes) {
3767
+ if (!nodes.every((n) => n instanceof TextNode)) throw new Error("Only `TextNode` is allowed for `Summary` extractor");
3768
+ const nodeSummaries = await Promise.all(nodes.map((node) => this.generateNodeSummary(node)));
3769
+ const metadataList = nodes.map(() => ({}));
3770
+ for (let i = 0; i < nodes.length; i++) {
3771
+ if (i > 0 && this.prevSummary && nodeSummaries[i - 1]) {
3772
+ metadataList[i]["prevSectionSummary"] = nodeSummaries[i - 1];
3773
+ }
3774
+ if (i < nodes.length - 1 && this.nextSummary && nodeSummaries[i + 1]) {
3775
+ metadataList[i]["nextSectionSummary"] = nodeSummaries[i + 1];
3776
+ }
3777
+ if (this.selfSummary && nodeSummaries[i]) {
3778
+ metadataList[i]["sectionSummary"] = nodeSummaries[i];
3779
+ }
3780
+ }
3781
+ return metadataList;
3782
+ }
3783
+ };
3784
+ var QuestionsAnsweredExtractor = class extends BaseExtractor {
3785
+ /**
3786
+ * MastraLanguageModel instance.
3787
+ * @type {MastraLanguageModel}
3788
+ */
3789
+ llm;
3790
+ /**
3791
+ * Number of questions to generate.
3792
+ * @type {number}
3793
+ * @default 5
3794
+ */
3795
+ questions = 5;
3796
+ /**
3797
+ * The prompt template to use for the question extractor.
3798
+ * @type {string}
3799
+ */
3800
+ promptTemplate;
3801
+ /**
3802
+ * Wheter to use metadata for embeddings only
3803
+ * @type {boolean}
3804
+ * @default false
3805
+ */
3806
+ embeddingOnly = false;
3807
+ /**
3808
+ * Constructor for the QuestionsAnsweredExtractor class.
3809
+ * @param {MastraLanguageModel} llm MastraLanguageModel instance.
3810
+ * @param {number} questions Number of questions to generate.
3811
+ * @param {QuestionExtractPrompt['template']} promptTemplate Optional custom prompt template (should include {context}).
3812
+ * @param {boolean} embeddingOnly Whether to use metadata for embeddings only.
3813
+ */
3814
+ constructor(options) {
3815
+ if (options?.questions && options.questions < 1) throw new Error("Questions must be greater than 0");
3816
+ super();
3817
+ this.llm = options?.llm ?? baseLLM;
3818
+ this.questions = options?.questions ?? 5;
3819
+ this.promptTemplate = options?.promptTemplate ? new PromptTemplate({
3820
+ templateVars: ["numQuestions", "context"],
3821
+ template: options.promptTemplate
3822
+ }).partialFormat({
3823
+ numQuestions: "5"
3824
+ }) : defaultQuestionExtractPrompt;
3825
+ this.embeddingOnly = options?.embeddingOnly ?? false;
3826
+ }
3827
+ /**
3828
+ * Extract answered questions from a node.
3829
+ * @param {BaseNode} node Node to extract questions from.
3830
+ * @returns {Promise<Array<ExtractQuestion> | Array<{}>>} Questions extracted from the node.
3831
+ */
3832
+ async extractQuestionsFromNode(node) {
3833
+ const text = node.getContent(this.metadataMode);
3834
+ if (!text || text.trim() === "") {
3835
+ return { questionsThisExcerptCanAnswer: "" };
3836
+ }
3837
+ if (this.isTextNodeOnly && !(node instanceof TextNode)) {
3838
+ return { questionsThisExcerptCanAnswer: "" };
3839
+ }
3840
+ const contextStr = node.getContent(this.metadataMode);
3841
+ const prompt = this.promptTemplate.format({
3842
+ context: contextStr,
3843
+ numQuestions: this.questions.toString()
3844
+ });
3845
+ const questions = await this.llm.doGenerate({
3846
+ inputFormat: "messages",
3847
+ mode: { type: "regular" },
3848
+ prompt: [
3849
+ {
3850
+ role: "user",
3851
+ content: [{ type: "text", text: prompt }]
3852
+ }
3853
+ ]
3854
+ });
3855
+ let result = "";
3856
+ try {
3857
+ if (typeof questions.text === "string") {
3858
+ result = questions.text.replace(STRIP_REGEX, "").trim();
3859
+ } else {
3860
+ console.warn("Question extraction LLM output was not a string:", questions.text);
3861
+ }
3862
+ } catch (err) {
3863
+ console.warn("Question extraction failed:", err);
3864
+ }
3865
+ return {
3866
+ questionsThisExcerptCanAnswer: result
3867
+ };
3868
+ }
3869
+ /**
3870
+ * Extract answered questions from a list of nodes.
3871
+ * @param {BaseNode[]} nodes Nodes to extract questions from.
3872
+ * @returns {Promise<Array<ExtractQuestion> | Array<{}>>} Questions extracted from the nodes.
3873
+ */
3874
+ async extract(nodes) {
3875
+ const results = await Promise.all(nodes.map((node) => this.extractQuestionsFromNode(node)));
3876
+ return results;
3877
+ }
3878
+ };
3879
+ var KeywordExtractor = class extends BaseExtractor {
3880
+ /**
3881
+ * MastraLanguageModel instance.
3882
+ * @type {MastraLanguageModel}
3883
+ */
3884
+ llm;
3885
+ /**
3886
+ * Number of keywords to extract.
3887
+ * @type {number}
3888
+ * @default 5
3889
+ */
3890
+ keywords = 5;
3891
+ /**
3892
+ * The prompt template to use for the question extractor.
3893
+ * @type {string}
3894
+ */
3895
+ promptTemplate;
3896
+ /**
3897
+ * Constructor for the KeywordExtractor class.
3898
+ * @param {MastraLanguageModel} llm MastraLanguageModel instance.
3899
+ * @param {number} keywords Number of keywords to extract.
3900
+ * @param {string} [promptTemplate] Optional custom prompt template (must include {context})
3901
+ * @throws {Error} If keywords is less than 1.
3902
+ */
3903
+ constructor(options) {
3904
+ if (options?.keywords && options.keywords < 1) throw new Error("Keywords must be greater than 0");
3905
+ super();
3906
+ this.llm = options?.llm ?? baseLLM;
3907
+ this.keywords = options?.keywords ?? 5;
3908
+ this.promptTemplate = options?.promptTemplate ? new PromptTemplate({
3909
+ templateVars: ["context", "maxKeywords"],
3910
+ template: options.promptTemplate
3911
+ }) : defaultKeywordExtractPrompt;
3912
+ }
3913
+ /**
3914
+ *
3915
+ * @param node Node to extract keywords from.
3916
+ * @returns Keywords extracted from the node.
3917
+ */
3918
+ /**
3919
+ * Extract keywords from a node. Returns an object with a comma-separated string of keywords, or an empty string if extraction fails.
3920
+ * Adds error handling for malformed/empty LLM output.
3921
+ */
3922
+ async extractKeywordsFromNodes(node) {
3923
+ const text = node.getContent(this.metadataMode);
3924
+ if (!text || text.trim() === "") {
3925
+ return { excerptKeywords: "" };
3926
+ }
3927
+ if (this.isTextNodeOnly && !(node instanceof TextNode)) {
3928
+ return { excerptKeywords: "" };
3929
+ }
3930
+ let keywords = "";
3931
+ try {
3932
+ const completion = await this.llm.doGenerate({
3933
+ inputFormat: "messages",
3934
+ mode: { type: "regular" },
3935
+ prompt: [
3936
+ {
3937
+ role: "user",
3938
+ content: [
3939
+ {
3940
+ type: "text",
3941
+ text: this.promptTemplate.format({
3942
+ context: node.getContent(MetadataMode.ALL),
3943
+ maxKeywords: this.keywords.toString()
3944
+ })
3945
+ }
3946
+ ]
3947
+ }
3948
+ ]
3949
+ });
3950
+ if (typeof completion.text === "string") {
3951
+ keywords = completion.text.trim();
3952
+ } else {
3953
+ console.warn("Keyword extraction LLM output was not a string:", completion.text);
3954
+ }
3955
+ } catch (err) {
3956
+ console.warn("Keyword extraction failed:", err);
3957
+ }
3958
+ return { excerptKeywords: keywords };
3959
+ }
3960
+ /**
3961
+ *
3962
+ * @param nodes Nodes to extract keywords from.
3963
+ * @returns Keywords extracted from the nodes.
3964
+ */
3965
+ /**
3966
+ * Extract keywords from an array of nodes. Always returns an array (may be empty).
3967
+ * @param nodes Nodes to extract keywords from.
3968
+ * @returns Array of keyword extraction results.
3969
+ */
3970
+ async extract(nodes) {
3971
+ if (!Array.isArray(nodes) || nodes.length === 0) return [];
3972
+ const results = await Promise.all(nodes.map((node) => this.extractKeywordsFromNodes(node)));
3973
+ return results;
3974
+ }
3975
+ };
10
3976
 
11
3977
  // src/document/types.ts
12
3978
  var Language = /* @__PURE__ */ ((Language2) => {
@@ -508,7 +4474,7 @@ var HTMLSectionTransformer = class {
508
4474
  headersToSplitOn;
509
4475
  options;
510
4476
  constructor(headersToSplitOn, options = {}) {
511
- this.headersToSplitOn = Object.fromEntries(headersToSplitOn.map(([tag, name]) => [tag.toLowerCase(), name]));
4477
+ this.headersToSplitOn = Object.fromEntries(headersToSplitOn.map(([tag, name14]) => [tag.toLowerCase(), name14]));
512
4478
  this.options = options;
513
4479
  }
514
4480
  splitText(text) {
@@ -1102,7 +5068,7 @@ var MarkdownHeaderTransformer = class {
1102
5068
  continue;
1103
5069
  }
1104
5070
  let headerMatched = false;
1105
- for (const [sep, name] of this.headersToSplitOn) {
5071
+ for (const [sep, name14] of this.headersToSplitOn) {
1106
5072
  if (strippedLine.startsWith(sep) && (strippedLine.length === sep.length || strippedLine[sep.length] === " ")) {
1107
5073
  headerMatched = true;
1108
5074
  if (currentContent.length > 0) {
@@ -1112,7 +5078,7 @@ var MarkdownHeaderTransformer = class {
1112
5078
  });
1113
5079
  currentContent = [];
1114
5080
  }
1115
- if (name !== null) {
5081
+ if (name14 !== null) {
1116
5082
  const currentHeaderLevel = (sep.match(/#/g) || []).length;
1117
5083
  while (headerStack.length > 0 && headerStack?.[headerStack.length - 1].level >= currentHeaderLevel) {
1118
5084
  const poppedHeader = headerStack.pop();
@@ -1122,11 +5088,11 @@ var MarkdownHeaderTransformer = class {
1122
5088
  }
1123
5089
  const header = {
1124
5090
  level: currentHeaderLevel,
1125
- name,
5091
+ name: name14,
1126
5092
  data: strippedLine.slice(sep.length).trim()
1127
5093
  };
1128
5094
  headerStack.push(header);
1129
- initialMetadata[name] = header.data;
5095
+ initialMetadata[name14] = header.data;
1130
5096
  }
1131
5097
  linesWithMetadata.push({
1132
5098
  content: line,
@@ -1304,16 +5270,16 @@ var MDocument = class _MDocument {
1304
5270
  if (typeof title !== "undefined") {
1305
5271
  transformations.push(new TitleExtractor(typeof title === "boolean" ? {} : title));
1306
5272
  this.chunks = this.chunks.map(
1307
- (doc, i) => new Document({
5273
+ (doc) => doc?.metadata?.docId ? new Document({
1308
5274
  ...doc,
1309
5275
  relationships: {
1310
5276
  [NodeRelationship.SOURCE]: {
1311
- nodeId: `doc-${i}`,
5277
+ nodeId: doc.metadata.docId,
1312
5278
  nodeType: ObjectType.DOCUMENT,
1313
5279
  metadata: doc.metadata
1314
5280
  }
1315
5281
  }
1316
- })
5282
+ }) : doc
1317
5283
  );
1318
5284
  }
1319
5285
  const pipeline = new IngestionPipeline({