ai 3.3.16 → 3.3.18
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.d.mts +218 -269
- package/dist/index.d.ts +218 -269
- package/dist/index.js +396 -275
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +393 -267
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -8
package/dist/index.js
CHANGED
@@ -4,8 +4,8 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
6
|
var __export = (target, all) => {
|
7
|
-
for (var
|
8
|
-
__defProp(target,
|
7
|
+
for (var name10 in all)
|
8
|
+
__defProp(target, name10, { get: all[name10], enumerable: true });
|
9
9
|
};
|
10
10
|
var __copyProps = (to, from, except, desc) => {
|
11
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
@@ -20,9 +20,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
20
20
|
// streams/index.ts
|
21
21
|
var streams_exports = {};
|
22
22
|
__export(streams_exports, {
|
23
|
-
AISDKError: () =>
|
23
|
+
AISDKError: () => import_provider15.AISDKError,
|
24
24
|
AIStream: () => AIStream,
|
25
|
-
APICallError: () =>
|
25
|
+
APICallError: () => import_provider15.APICallError,
|
26
26
|
AWSBedrockAnthropicMessagesStream: () => AWSBedrockAnthropicMessagesStream,
|
27
27
|
AWSBedrockAnthropicStream: () => AWSBedrockAnthropicStream,
|
28
28
|
AWSBedrockCohereStream: () => AWSBedrockCohereStream,
|
@@ -32,24 +32,24 @@ __export(streams_exports, {
|
|
32
32
|
AssistantResponse: () => AssistantResponse,
|
33
33
|
CohereStream: () => CohereStream,
|
34
34
|
DownloadError: () => DownloadError,
|
35
|
-
EmptyResponseBodyError: () =>
|
35
|
+
EmptyResponseBodyError: () => import_provider15.EmptyResponseBodyError,
|
36
36
|
GoogleGenerativeAIStream: () => GoogleGenerativeAIStream,
|
37
37
|
HuggingFaceStream: () => HuggingFaceStream,
|
38
38
|
InkeepStream: () => InkeepStream,
|
39
39
|
InvalidArgumentError: () => InvalidArgumentError,
|
40
40
|
InvalidDataContentError: () => InvalidDataContentError,
|
41
41
|
InvalidMessageRoleError: () => InvalidMessageRoleError,
|
42
|
-
|
43
|
-
|
44
|
-
InvalidResponseDataError: () => import_provider14.InvalidResponseDataError,
|
42
|
+
InvalidPromptError: () => import_provider15.InvalidPromptError,
|
43
|
+
InvalidResponseDataError: () => import_provider15.InvalidResponseDataError,
|
45
44
|
InvalidToolArgumentsError: () => InvalidToolArgumentsError,
|
46
|
-
JSONParseError: () =>
|
45
|
+
JSONParseError: () => import_provider15.JSONParseError,
|
47
46
|
LangChainAdapter: () => langchain_adapter_exports,
|
48
47
|
LangChainStream: () => LangChainStream,
|
49
|
-
LoadAPIKeyError: () =>
|
48
|
+
LoadAPIKeyError: () => import_provider15.LoadAPIKeyError,
|
50
49
|
MistralStream: () => MistralStream,
|
50
|
+
NoContentGeneratedError: () => import_provider15.NoContentGeneratedError,
|
51
51
|
NoObjectGeneratedError: () => NoObjectGeneratedError,
|
52
|
-
NoSuchModelError: () => NoSuchModelError,
|
52
|
+
NoSuchModelError: () => import_provider15.NoSuchModelError,
|
53
53
|
NoSuchProviderError: () => NoSuchProviderError,
|
54
54
|
NoSuchToolError: () => NoSuchToolError,
|
55
55
|
OpenAIStream: () => OpenAIStream,
|
@@ -57,8 +57,8 @@ __export(streams_exports, {
|
|
57
57
|
RetryError: () => RetryError,
|
58
58
|
StreamData: () => StreamData2,
|
59
59
|
StreamingTextResponse: () => StreamingTextResponse,
|
60
|
-
TypeValidationError: () =>
|
61
|
-
UnsupportedFunctionalityError: () =>
|
60
|
+
TypeValidationError: () => import_provider15.TypeValidationError,
|
61
|
+
UnsupportedFunctionalityError: () => import_provider15.UnsupportedFunctionalityError,
|
62
62
|
convertToCoreMessages: () => convertToCoreMessages,
|
63
63
|
cosineSimilarity: () => cosineSimilarity,
|
64
64
|
createCallbacksTransformer: () => createCallbacksTransformer,
|
@@ -70,6 +70,7 @@ __export(streams_exports, {
|
|
70
70
|
experimental_StreamData: () => experimental_StreamData,
|
71
71
|
experimental_createModelRegistry: () => experimental_createModelRegistry,
|
72
72
|
experimental_createProviderRegistry: () => experimental_createProviderRegistry,
|
73
|
+
experimental_customProvider: () => experimental_customProvider,
|
73
74
|
experimental_generateObject: () => experimental_generateObject,
|
74
75
|
experimental_generateText: () => experimental_generateText,
|
75
76
|
experimental_streamObject: () => experimental_streamObject,
|
@@ -223,7 +224,7 @@ function getBaseTelemetryAttributes({
|
|
223
224
|
telemetry,
|
224
225
|
headers
|
225
226
|
}) {
|
226
|
-
var
|
227
|
+
var _a10;
|
227
228
|
return {
|
228
229
|
"ai.model.provider": model.provider,
|
229
230
|
"ai.model.id": model.modelId,
|
@@ -233,7 +234,7 @@ function getBaseTelemetryAttributes({
|
|
233
234
|
return attributes;
|
234
235
|
}, {}),
|
235
236
|
// add metadata as attributes:
|
236
|
-
...Object.entries((
|
237
|
+
...Object.entries((_a10 = telemetry == null ? void 0 : telemetry.metadata) != null ? _a10 : {}).reduce(
|
237
238
|
(attributes, [key, value]) => {
|
238
239
|
attributes[`ai.telemetry.metadata.${key}`] = value;
|
239
240
|
return attributes;
|
@@ -258,7 +259,7 @@ var noopTracer = {
|
|
258
259
|
startSpan() {
|
259
260
|
return noopSpan;
|
260
261
|
},
|
261
|
-
startActiveSpan(
|
262
|
+
startActiveSpan(name10, arg1, arg2, arg3) {
|
262
263
|
if (typeof arg1 === "function") {
|
263
264
|
return arg1(noopSpan);
|
264
265
|
}
|
@@ -326,13 +327,13 @@ function getTracer({ isEnabled }) {
|
|
326
327
|
// core/telemetry/record-span.ts
|
327
328
|
var import_api2 = require("@opentelemetry/api");
|
328
329
|
function recordSpan({
|
329
|
-
name:
|
330
|
+
name: name10,
|
330
331
|
tracer,
|
331
332
|
attributes,
|
332
333
|
fn,
|
333
334
|
endWhenDone = true
|
334
335
|
}) {
|
335
|
-
return tracer.startActiveSpan(
|
336
|
+
return tracer.startActiveSpan(name10, { attributes }, async (span) => {
|
336
337
|
try {
|
337
338
|
const result = await fn(span);
|
338
339
|
if (endWhenDone) {
|
@@ -398,14 +399,14 @@ async function embed({
|
|
398
399
|
headers,
|
399
400
|
experimental_telemetry: telemetry
|
400
401
|
}) {
|
401
|
-
var
|
402
|
+
var _a10;
|
402
403
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
403
404
|
model,
|
404
405
|
telemetry,
|
405
406
|
headers,
|
406
407
|
settings: { maxRetries }
|
407
408
|
});
|
408
|
-
const tracer = getTracer({ isEnabled: (
|
409
|
+
const tracer = getTracer({ isEnabled: (_a10 = telemetry == null ? void 0 : telemetry.isEnabled) != null ? _a10 : false });
|
409
410
|
return recordSpan({
|
410
411
|
name: "ai.embed",
|
411
412
|
attributes: selectTelemetryAttributes({
|
@@ -438,14 +439,14 @@ async function embed({
|
|
438
439
|
}),
|
439
440
|
tracer,
|
440
441
|
fn: async (doEmbedSpan) => {
|
441
|
-
var
|
442
|
+
var _a11;
|
442
443
|
const modelResponse = await model.doEmbed({
|
443
444
|
values: [value],
|
444
445
|
abortSignal,
|
445
446
|
headers
|
446
447
|
});
|
447
448
|
const embedding2 = modelResponse.embeddings[0];
|
448
|
-
const usage2 = (
|
449
|
+
const usage2 = (_a11 = modelResponse.usage) != null ? _a11 : { tokens: NaN };
|
449
450
|
doEmbedSpan.setAttributes(
|
450
451
|
selectTelemetryAttributes({
|
451
452
|
telemetry,
|
@@ -511,14 +512,14 @@ async function embedMany({
|
|
511
512
|
headers,
|
512
513
|
experimental_telemetry: telemetry
|
513
514
|
}) {
|
514
|
-
var
|
515
|
+
var _a10;
|
515
516
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
516
517
|
model,
|
517
518
|
telemetry,
|
518
519
|
headers,
|
519
520
|
settings: { maxRetries }
|
520
521
|
});
|
521
|
-
const tracer = getTracer({ isEnabled: (
|
522
|
+
const tracer = getTracer({ isEnabled: (_a10 = telemetry == null ? void 0 : telemetry.isEnabled) != null ? _a10 : false });
|
522
523
|
return recordSpan({
|
523
524
|
name: "ai.embedMany",
|
524
525
|
attributes: selectTelemetryAttributes({
|
@@ -556,14 +557,14 @@ async function embedMany({
|
|
556
557
|
}),
|
557
558
|
tracer,
|
558
559
|
fn: async (doEmbedSpan) => {
|
559
|
-
var
|
560
|
+
var _a11;
|
560
561
|
const modelResponse = await model.doEmbed({
|
561
562
|
values,
|
562
563
|
abortSignal,
|
563
564
|
headers
|
564
565
|
});
|
565
566
|
const embeddings3 = modelResponse.embeddings;
|
566
|
-
const usage2 = (
|
567
|
+
const usage2 = (_a11 = modelResponse.usage) != null ? _a11 : { tokens: NaN };
|
567
568
|
doEmbedSpan.setAttributes(
|
568
569
|
selectTelemetryAttributes({
|
569
570
|
telemetry,
|
@@ -615,14 +616,14 @@ async function embedMany({
|
|
615
616
|
}),
|
616
617
|
tracer,
|
617
618
|
fn: async (doEmbedSpan) => {
|
618
|
-
var
|
619
|
+
var _a11;
|
619
620
|
const modelResponse = await model.doEmbed({
|
620
621
|
values: chunk,
|
621
622
|
abortSignal,
|
622
623
|
headers
|
623
624
|
});
|
624
625
|
const embeddings2 = modelResponse.embeddings;
|
625
|
-
const usage2 = (
|
626
|
+
const usage2 = (_a11 = modelResponse.usage) != null ? _a11 : { tokens: NaN };
|
626
627
|
doEmbedSpan.setAttributes(
|
627
628
|
selectTelemetryAttributes({
|
628
629
|
telemetry,
|
@@ -669,8 +670,7 @@ var DefaultEmbedManyResult = class {
|
|
669
670
|
};
|
670
671
|
|
671
672
|
// core/generate-object/generate-object.ts
|
672
|
-
var
|
673
|
-
var import_ui_utils = require("@ai-sdk/ui-utils");
|
673
|
+
var import_provider_utils6 = require("@ai-sdk/provider-utils");
|
674
674
|
|
675
675
|
// core/prompt/convert-to-language-model-prompt.ts
|
676
676
|
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
@@ -725,7 +725,7 @@ async function download({
|
|
725
725
|
url,
|
726
726
|
fetchImplementation = fetch
|
727
727
|
}) {
|
728
|
-
var
|
728
|
+
var _a10;
|
729
729
|
const urlText = url.toString();
|
730
730
|
try {
|
731
731
|
const response = await fetchImplementation(urlText);
|
@@ -738,7 +738,7 @@ async function download({
|
|
738
738
|
}
|
739
739
|
return {
|
740
740
|
data: new Uint8Array(await response.arrayBuffer()),
|
741
|
-
mimeType: (
|
741
|
+
mimeType: (_a10 = response.headers.get("content-type")) != null ? _a10 : void 0
|
742
742
|
};
|
743
743
|
} catch (error) {
|
744
744
|
if (DownloadError.isInstance(error)) {
|
@@ -816,8 +816,8 @@ var dataContentSchema = import_zod.z.union([
|
|
816
816
|
import_zod.z.custom(
|
817
817
|
// Buffer might not be available in some environments such as CloudFlare:
|
818
818
|
(value) => {
|
819
|
-
var
|
820
|
-
return (_b = (
|
819
|
+
var _a10, _b;
|
820
|
+
return (_b = (_a10 = globalThis.Buffer) == null ? void 0 : _a10.isBuffer(value)) != null ? _b : false;
|
821
821
|
},
|
822
822
|
{ message: "Must be a Buffer" }
|
823
823
|
)
|
@@ -945,7 +945,7 @@ function convertToLanguageModelMessage(message, downloadedImages) {
|
|
945
945
|
role: "user",
|
946
946
|
content: message.content.map(
|
947
947
|
(part) => {
|
948
|
-
var
|
948
|
+
var _a10, _b, _c;
|
949
949
|
switch (part.type) {
|
950
950
|
case "text": {
|
951
951
|
return {
|
@@ -968,7 +968,7 @@ function convertToLanguageModelMessage(message, downloadedImages) {
|
|
968
968
|
return {
|
969
969
|
type: "image",
|
970
970
|
image: downloadedImage.data,
|
971
|
-
mimeType: (
|
971
|
+
mimeType: (_a10 = part.mimeType) != null ? _a10 : downloadedImage.mimeType,
|
972
972
|
providerMetadata: part.experimental_providerMetadata
|
973
973
|
};
|
974
974
|
}
|
@@ -1400,8 +1400,8 @@ function prepareResponseHeaders(init, {
|
|
1400
1400
|
contentType,
|
1401
1401
|
dataStreamVersion
|
1402
1402
|
}) {
|
1403
|
-
var
|
1404
|
-
const headers = new Headers((
|
1403
|
+
var _a10;
|
1404
|
+
const headers = new Headers((_a10 = init == null ? void 0 : init.headers) != null ? _a10 : {});
|
1405
1405
|
if (!headers.has("Content-Type")) {
|
1406
1406
|
headers.set("Content-Type", contentType);
|
1407
1407
|
}
|
@@ -1466,6 +1466,168 @@ var NoObjectGeneratedError = class extends import_provider8.AISDKError {
|
|
1466
1466
|
};
|
1467
1467
|
_a6 = symbol6;
|
1468
1468
|
|
1469
|
+
// core/generate-object/output-strategy.ts
|
1470
|
+
var import_provider9 = require("@ai-sdk/provider");
|
1471
|
+
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
1472
|
+
var import_ui_utils = require("@ai-sdk/ui-utils");
|
1473
|
+
|
1474
|
+
// core/util/async-iterable-stream.ts
|
1475
|
+
function createAsyncIterableStream(source, transformer) {
|
1476
|
+
const transformedStream = source.pipeThrough(
|
1477
|
+
new TransformStream(transformer)
|
1478
|
+
);
|
1479
|
+
transformedStream[Symbol.asyncIterator] = () => {
|
1480
|
+
const reader = transformedStream.getReader();
|
1481
|
+
return {
|
1482
|
+
async next() {
|
1483
|
+
const { done, value } = await reader.read();
|
1484
|
+
return done ? { done: true, value: void 0 } : { done: false, value };
|
1485
|
+
}
|
1486
|
+
};
|
1487
|
+
};
|
1488
|
+
return transformedStream;
|
1489
|
+
}
|
1490
|
+
|
1491
|
+
// core/generate-object/output-strategy.ts
|
1492
|
+
var noSchemaOutputStrategy = {
|
1493
|
+
type: "no-schema",
|
1494
|
+
jsonSchema: void 0,
|
1495
|
+
validatePartialResult({ value }) {
|
1496
|
+
return { success: true, value };
|
1497
|
+
},
|
1498
|
+
validateFinalResult(value) {
|
1499
|
+
return value === void 0 ? { success: false, error: new NoObjectGeneratedError() } : { success: true, value };
|
1500
|
+
},
|
1501
|
+
createElementStream() {
|
1502
|
+
throw new import_provider9.UnsupportedFunctionalityError({
|
1503
|
+
functionality: "element streams in no-schema mode"
|
1504
|
+
});
|
1505
|
+
}
|
1506
|
+
};
|
1507
|
+
var objectOutputStrategy = (schema) => ({
|
1508
|
+
type: "object",
|
1509
|
+
jsonSchema: schema.jsonSchema,
|
1510
|
+
validatePartialResult({ value }) {
|
1511
|
+
return { success: true, value };
|
1512
|
+
},
|
1513
|
+
validateFinalResult(value) {
|
1514
|
+
return (0, import_provider_utils5.safeValidateTypes)({ value, schema });
|
1515
|
+
},
|
1516
|
+
createElementStream() {
|
1517
|
+
throw new import_provider9.UnsupportedFunctionalityError({
|
1518
|
+
functionality: "element streams in object mode"
|
1519
|
+
});
|
1520
|
+
}
|
1521
|
+
});
|
1522
|
+
var arrayOutputStrategy = (schema) => {
|
1523
|
+
const { $schema, ...itemSchema } = schema.jsonSchema;
|
1524
|
+
return {
|
1525
|
+
type: "object",
|
1526
|
+
// wrap in object that contains array of elements, since most LLMs will not
|
1527
|
+
// be able to generate an array directly:
|
1528
|
+
// possible future optimization: use arrays directly when model supports grammar-guided generation
|
1529
|
+
jsonSchema: {
|
1530
|
+
$schema: "http://json-schema.org/draft-07/schema#",
|
1531
|
+
type: "object",
|
1532
|
+
properties: {
|
1533
|
+
elements: { type: "array", items: itemSchema }
|
1534
|
+
},
|
1535
|
+
required: ["elements"],
|
1536
|
+
additionalProperties: false
|
1537
|
+
},
|
1538
|
+
validatePartialResult({
|
1539
|
+
value,
|
1540
|
+
parseState
|
1541
|
+
}) {
|
1542
|
+
if (!(0, import_provider9.isJSONObject)(value) || !(0, import_provider9.isJSONArray)(value.elements)) {
|
1543
|
+
return {
|
1544
|
+
success: false,
|
1545
|
+
error: new import_provider9.TypeValidationError({
|
1546
|
+
value,
|
1547
|
+
cause: "value must be an object that contains an array of elements"
|
1548
|
+
})
|
1549
|
+
};
|
1550
|
+
}
|
1551
|
+
const inputArray = value.elements;
|
1552
|
+
const resultArray = [];
|
1553
|
+
for (let i = 0; i < inputArray.length; i++) {
|
1554
|
+
const element = inputArray[i];
|
1555
|
+
const result = (0, import_provider_utils5.safeValidateTypes)({ value: element, schema });
|
1556
|
+
if (i === inputArray.length - 1 && (!result.success || parseState !== "successful-parse")) {
|
1557
|
+
continue;
|
1558
|
+
}
|
1559
|
+
if (!result.success) {
|
1560
|
+
return result;
|
1561
|
+
}
|
1562
|
+
resultArray.push(result.value);
|
1563
|
+
}
|
1564
|
+
return { success: true, value: resultArray };
|
1565
|
+
},
|
1566
|
+
validateFinalResult(value) {
|
1567
|
+
if (!(0, import_provider9.isJSONObject)(value) || !(0, import_provider9.isJSONArray)(value.elements)) {
|
1568
|
+
return {
|
1569
|
+
success: false,
|
1570
|
+
error: new import_provider9.TypeValidationError({
|
1571
|
+
value,
|
1572
|
+
cause: "value must be an object that contains an array of elements"
|
1573
|
+
})
|
1574
|
+
};
|
1575
|
+
}
|
1576
|
+
const inputArray = value.elements;
|
1577
|
+
for (const element of inputArray) {
|
1578
|
+
const result = (0, import_provider_utils5.safeValidateTypes)({ value: element, schema });
|
1579
|
+
if (!result.success) {
|
1580
|
+
return result;
|
1581
|
+
}
|
1582
|
+
}
|
1583
|
+
return { success: true, value: inputArray };
|
1584
|
+
},
|
1585
|
+
createElementStream(originalStream) {
|
1586
|
+
let publishedElements = 0;
|
1587
|
+
return createAsyncIterableStream(originalStream, {
|
1588
|
+
transform(chunk, controller) {
|
1589
|
+
switch (chunk.type) {
|
1590
|
+
case "object": {
|
1591
|
+
const array = chunk.object;
|
1592
|
+
for (; publishedElements < array.length; publishedElements++) {
|
1593
|
+
controller.enqueue(array[publishedElements]);
|
1594
|
+
}
|
1595
|
+
break;
|
1596
|
+
}
|
1597
|
+
case "text-delta":
|
1598
|
+
case "finish":
|
1599
|
+
break;
|
1600
|
+
case "error":
|
1601
|
+
controller.error(chunk.error);
|
1602
|
+
break;
|
1603
|
+
default: {
|
1604
|
+
const _exhaustiveCheck = chunk;
|
1605
|
+
throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
|
1606
|
+
}
|
1607
|
+
}
|
1608
|
+
}
|
1609
|
+
});
|
1610
|
+
}
|
1611
|
+
};
|
1612
|
+
};
|
1613
|
+
function getOutputStrategy({
|
1614
|
+
output,
|
1615
|
+
schema
|
1616
|
+
}) {
|
1617
|
+
switch (output) {
|
1618
|
+
case "object":
|
1619
|
+
return objectOutputStrategy((0, import_ui_utils.asSchema)(schema));
|
1620
|
+
case "array":
|
1621
|
+
return arrayOutputStrategy((0, import_ui_utils.asSchema)(schema));
|
1622
|
+
case "no-schema":
|
1623
|
+
return noSchemaOutputStrategy;
|
1624
|
+
default: {
|
1625
|
+
const _exhaustiveCheck = output;
|
1626
|
+
throw new Error(`Unsupported output: ${_exhaustiveCheck}`);
|
1627
|
+
}
|
1628
|
+
}
|
1629
|
+
}
|
1630
|
+
|
1469
1631
|
// core/generate-object/validate-object-generation-input.ts
|
1470
1632
|
function validateObjectGenerationInput({
|
1471
1633
|
output,
|
@@ -1474,7 +1636,7 @@ function validateObjectGenerationInput({
|
|
1474
1636
|
schemaName,
|
1475
1637
|
schemaDescription
|
1476
1638
|
}) {
|
1477
|
-
if (output != null && output !== "object" && output !== "no-schema") {
|
1639
|
+
if (output != null && output !== "object" && output !== "array" && output !== "no-schema") {
|
1478
1640
|
throw new InvalidArgumentError({
|
1479
1641
|
parameter: "output",
|
1480
1642
|
value: output,
|
@@ -1520,6 +1682,15 @@ function validateObjectGenerationInput({
|
|
1520
1682
|
});
|
1521
1683
|
}
|
1522
1684
|
}
|
1685
|
+
if (output === "array") {
|
1686
|
+
if (schema == null) {
|
1687
|
+
throw new InvalidArgumentError({
|
1688
|
+
parameter: "schema",
|
1689
|
+
value: schema,
|
1690
|
+
message: "Element schema is required for array output."
|
1691
|
+
});
|
1692
|
+
}
|
1693
|
+
}
|
1523
1694
|
}
|
1524
1695
|
|
1525
1696
|
// core/generate-object/generate-object.ts
|
@@ -1539,7 +1710,7 @@ async function generateObject({
|
|
1539
1710
|
experimental_telemetry: telemetry,
|
1540
1711
|
...settings
|
1541
1712
|
}) {
|
1542
|
-
var
|
1713
|
+
var _a10;
|
1543
1714
|
validateObjectGenerationInput({
|
1544
1715
|
output,
|
1545
1716
|
mode,
|
@@ -1547,7 +1718,8 @@ async function generateObject({
|
|
1547
1718
|
schemaName,
|
1548
1719
|
schemaDescription
|
1549
1720
|
});
|
1550
|
-
|
1721
|
+
const outputStrategy = getOutputStrategy({ output, schema: inputSchema });
|
1722
|
+
if (outputStrategy.type === "no-schema" && mode === void 0) {
|
1551
1723
|
mode = "json";
|
1552
1724
|
}
|
1553
1725
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
@@ -1556,8 +1728,7 @@ async function generateObject({
|
|
1556
1728
|
headers,
|
1557
1729
|
settings: { ...settings, maxRetries }
|
1558
1730
|
});
|
1559
|
-
const
|
1560
|
-
const tracer = getTracer({ isEnabled: (_a12 = telemetry == null ? void 0 : telemetry.isEnabled) != null ? _a12 : false });
|
1731
|
+
const tracer = getTracer({ isEnabled: (_a10 = telemetry == null ? void 0 : telemetry.isEnabled) != null ? _a10 : false });
|
1561
1732
|
return recordSpan({
|
1562
1733
|
name: "ai.generateObject",
|
1563
1734
|
attributes: selectTelemetryAttributes({
|
@@ -1572,10 +1743,10 @@ async function generateObject({
|
|
1572
1743
|
"ai.prompt": {
|
1573
1744
|
input: () => JSON.stringify({ system, prompt, messages })
|
1574
1745
|
},
|
1575
|
-
"ai.schema":
|
1746
|
+
"ai.schema": outputStrategy.jsonSchema != null ? { input: () => JSON.stringify(outputStrategy.jsonSchema) } : void 0,
|
1576
1747
|
"ai.schema.name": schemaName,
|
1577
1748
|
"ai.schema.description": schemaDescription,
|
1578
|
-
"ai.settings.output":
|
1749
|
+
"ai.settings.output": outputStrategy.type,
|
1579
1750
|
"ai.settings.mode": mode
|
1580
1751
|
}
|
1581
1752
|
}),
|
@@ -1595,9 +1766,9 @@ async function generateObject({
|
|
1595
1766
|
switch (mode) {
|
1596
1767
|
case "json": {
|
1597
1768
|
const validatedPrompt = validatePrompt({
|
1598
|
-
system:
|
1769
|
+
system: outputStrategy.jsonSchema == null ? injectJsonInstruction({ prompt: system }) : model.supportsStructuredOutputs ? system : injectJsonInstruction({
|
1599
1770
|
prompt: system,
|
1600
|
-
schema:
|
1771
|
+
schema: outputStrategy.jsonSchema
|
1601
1772
|
}),
|
1602
1773
|
prompt,
|
1603
1774
|
messages
|
@@ -1638,7 +1809,7 @@ async function generateObject({
|
|
1638
1809
|
const result2 = await model.doGenerate({
|
1639
1810
|
mode: {
|
1640
1811
|
type: "object-json",
|
1641
|
-
schema:
|
1812
|
+
schema: outputStrategy.jsonSchema,
|
1642
1813
|
name: schemaName,
|
1643
1814
|
description: schemaDescription
|
1644
1815
|
},
|
@@ -1718,7 +1889,7 @@ async function generateObject({
|
|
1718
1889
|
}),
|
1719
1890
|
tracer,
|
1720
1891
|
fn: async (span2) => {
|
1721
|
-
var
|
1892
|
+
var _a11, _b;
|
1722
1893
|
const result2 = await model.doGenerate({
|
1723
1894
|
mode: {
|
1724
1895
|
type: "object-tool",
|
@@ -1726,7 +1897,7 @@ async function generateObject({
|
|
1726
1897
|
type: "function",
|
1727
1898
|
name: schemaName != null ? schemaName : "json",
|
1728
1899
|
description: schemaDescription != null ? schemaDescription : "Respond with a JSON object.",
|
1729
|
-
parameters:
|
1900
|
+
parameters: outputStrategy.jsonSchema
|
1730
1901
|
}
|
1731
1902
|
},
|
1732
1903
|
...prepareCallSettings(settings),
|
@@ -1735,7 +1906,7 @@ async function generateObject({
|
|
1735
1906
|
abortSignal,
|
1736
1907
|
headers
|
1737
1908
|
});
|
1738
|
-
const objectText = (_b = (
|
1909
|
+
const objectText = (_b = (_a11 = result2.toolCalls) == null ? void 0 : _a11[0]) == null ? void 0 : _b.args;
|
1739
1910
|
if (objectText === void 0) {
|
1740
1911
|
throw new NoObjectGeneratedError();
|
1741
1912
|
}
|
@@ -1777,15 +1948,16 @@ async function generateObject({
|
|
1777
1948
|
throw new Error(`Unsupported mode: ${_exhaustiveCheck}`);
|
1778
1949
|
}
|
1779
1950
|
}
|
1780
|
-
const parseResult = (0,
|
1781
|
-
text: result,
|
1782
|
-
// type casting required for `undefined` schema (no-schema mode),
|
1783
|
-
// in which case <T> is <JSONValue> as desired.
|
1784
|
-
schema
|
1785
|
-
});
|
1951
|
+
const parseResult = (0, import_provider_utils6.safeParseJSON)({ text: result });
|
1786
1952
|
if (!parseResult.success) {
|
1787
1953
|
throw parseResult.error;
|
1788
1954
|
}
|
1955
|
+
const validationResult = outputStrategy.validateFinalResult(
|
1956
|
+
parseResult.value
|
1957
|
+
);
|
1958
|
+
if (!validationResult.success) {
|
1959
|
+
throw validationResult.error;
|
1960
|
+
}
|
1789
1961
|
span.setAttributes(
|
1790
1962
|
selectTelemetryAttributes({
|
1791
1963
|
telemetry,
|
@@ -1794,13 +1966,13 @@ async function generateObject({
|
|
1794
1966
|
"ai.usage.promptTokens": usage.promptTokens,
|
1795
1967
|
"ai.usage.completionTokens": usage.completionTokens,
|
1796
1968
|
"ai.result.object": {
|
1797
|
-
output: () => JSON.stringify(
|
1969
|
+
output: () => JSON.stringify(validationResult.value)
|
1798
1970
|
}
|
1799
1971
|
}
|
1800
1972
|
})
|
1801
1973
|
);
|
1802
1974
|
return new DefaultGenerateObjectResult({
|
1803
|
-
object:
|
1975
|
+
object: validationResult.value,
|
1804
1976
|
finishReason,
|
1805
1977
|
usage: calculateCompletionTokenUsage(usage),
|
1806
1978
|
warnings,
|
@@ -1822,9 +1994,9 @@ var DefaultGenerateObjectResult = class {
|
|
1822
1994
|
this.experimental_providerMetadata = options.providerMetadata;
|
1823
1995
|
}
|
1824
1996
|
toJsonResponse(init) {
|
1825
|
-
var
|
1997
|
+
var _a10;
|
1826
1998
|
return new Response(JSON.stringify(this.object), {
|
1827
|
-
status: (
|
1999
|
+
status: (_a10 = init == null ? void 0 : init.status) != null ? _a10 : 200,
|
1828
2000
|
headers: prepareResponseHeaders(init, {
|
1829
2001
|
contentType: "application/json; charset=utf-8"
|
1830
2002
|
})
|
@@ -1834,7 +2006,6 @@ var DefaultGenerateObjectResult = class {
|
|
1834
2006
|
var experimental_generateObject = generateObject;
|
1835
2007
|
|
1836
2008
|
// core/generate-object/stream-object.ts
|
1837
|
-
var import_provider_utils6 = require("@ai-sdk/provider-utils");
|
1838
2009
|
var import_ui_utils2 = require("@ai-sdk/ui-utils");
|
1839
2010
|
|
1840
2011
|
// util/create-resolvable-promise.ts
|
@@ -1875,38 +2046,21 @@ var DelayedPromise = class {
|
|
1875
2046
|
return this.promise;
|
1876
2047
|
}
|
1877
2048
|
resolve(value) {
|
1878
|
-
var
|
2049
|
+
var _a10;
|
1879
2050
|
this.status = { type: "resolved", value };
|
1880
2051
|
if (this.promise) {
|
1881
|
-
(
|
2052
|
+
(_a10 = this._resolve) == null ? void 0 : _a10.call(this, value);
|
1882
2053
|
}
|
1883
2054
|
}
|
1884
2055
|
reject(error) {
|
1885
|
-
var
|
2056
|
+
var _a10;
|
1886
2057
|
this.status = { type: "rejected", error };
|
1887
2058
|
if (this.promise) {
|
1888
|
-
(
|
2059
|
+
(_a10 = this._reject) == null ? void 0 : _a10.call(this, error);
|
1889
2060
|
}
|
1890
2061
|
}
|
1891
2062
|
};
|
1892
2063
|
|
1893
|
-
// core/util/async-iterable-stream.ts
|
1894
|
-
function createAsyncIterableStream(source, transformer) {
|
1895
|
-
const transformedStream = source.pipeThrough(
|
1896
|
-
new TransformStream(transformer)
|
1897
|
-
);
|
1898
|
-
transformedStream[Symbol.asyncIterator] = () => {
|
1899
|
-
const reader = transformedStream.getReader();
|
1900
|
-
return {
|
1901
|
-
async next() {
|
1902
|
-
const { done, value } = await reader.read();
|
1903
|
-
return done ? { done: true, value: void 0 } : { done: false, value };
|
1904
|
-
}
|
1905
|
-
};
|
1906
|
-
};
|
1907
|
-
return transformedStream;
|
1908
|
-
}
|
1909
|
-
|
1910
2064
|
// core/generate-object/stream-object.ts
|
1911
2065
|
async function streamObject({
|
1912
2066
|
model,
|
@@ -1925,7 +2079,7 @@ async function streamObject({
|
|
1925
2079
|
onFinish,
|
1926
2080
|
...settings
|
1927
2081
|
}) {
|
1928
|
-
var
|
2082
|
+
var _a10;
|
1929
2083
|
validateObjectGenerationInput({
|
1930
2084
|
output,
|
1931
2085
|
mode,
|
@@ -1933,7 +2087,8 @@ async function streamObject({
|
|
1933
2087
|
schemaName,
|
1934
2088
|
schemaDescription
|
1935
2089
|
});
|
1936
|
-
|
2090
|
+
const outputStrategy = getOutputStrategy({ output, schema: inputSchema });
|
2091
|
+
if (outputStrategy.type === "no-schema" && mode === void 0) {
|
1937
2092
|
mode = "json";
|
1938
2093
|
}
|
1939
2094
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
@@ -1942,9 +2097,8 @@ async function streamObject({
|
|
1942
2097
|
headers,
|
1943
2098
|
settings: { ...settings, maxRetries }
|
1944
2099
|
});
|
1945
|
-
const tracer = getTracer({ isEnabled: (
|
2100
|
+
const tracer = getTracer({ isEnabled: (_a10 = telemetry == null ? void 0 : telemetry.isEnabled) != null ? _a10 : false });
|
1946
2101
|
const retry = retryWithExponentialBackoff({ maxRetries });
|
1947
|
-
const schema = inputSchema != null ? (0, import_ui_utils2.asSchema)(inputSchema) : void 0;
|
1948
2102
|
return recordSpan({
|
1949
2103
|
name: "ai.streamObject",
|
1950
2104
|
attributes: selectTelemetryAttributes({
|
@@ -1959,10 +2113,10 @@ async function streamObject({
|
|
1959
2113
|
"ai.prompt": {
|
1960
2114
|
input: () => JSON.stringify({ system, prompt, messages })
|
1961
2115
|
},
|
1962
|
-
"ai.schema":
|
2116
|
+
"ai.schema": outputStrategy.jsonSchema != null ? { input: () => JSON.stringify(outputStrategy.jsonSchema) } : void 0,
|
1963
2117
|
"ai.schema.name": schemaName,
|
1964
2118
|
"ai.schema.description": schemaDescription,
|
1965
|
-
"ai.settings.output":
|
2119
|
+
"ai.settings.output": outputStrategy.type,
|
1966
2120
|
"ai.settings.mode": mode
|
1967
2121
|
}
|
1968
2122
|
}),
|
@@ -1977,9 +2131,9 @@ async function streamObject({
|
|
1977
2131
|
switch (mode) {
|
1978
2132
|
case "json": {
|
1979
2133
|
const validatedPrompt = validatePrompt({
|
1980
|
-
system:
|
2134
|
+
system: outputStrategy.jsonSchema == null ? injectJsonInstruction({ prompt: system }) : model.supportsStructuredOutputs ? system : injectJsonInstruction({
|
1981
2135
|
prompt: system,
|
1982
|
-
schema:
|
2136
|
+
schema: outputStrategy.jsonSchema
|
1983
2137
|
}),
|
1984
2138
|
prompt,
|
1985
2139
|
messages
|
@@ -1987,7 +2141,7 @@ async function streamObject({
|
|
1987
2141
|
callOptions = {
|
1988
2142
|
mode: {
|
1989
2143
|
type: "object-json",
|
1990
|
-
schema:
|
2144
|
+
schema: outputStrategy.jsonSchema,
|
1991
2145
|
name: schemaName,
|
1992
2146
|
description: schemaDescription
|
1993
2147
|
},
|
@@ -2028,7 +2182,7 @@ async function streamObject({
|
|
2028
2182
|
type: "function",
|
2029
2183
|
name: schemaName != null ? schemaName : "json",
|
2030
2184
|
description: schemaDescription != null ? schemaDescription : "Respond with a JSON object.",
|
2031
|
-
parameters:
|
2185
|
+
parameters: outputStrategy.jsonSchema
|
2032
2186
|
}
|
2033
2187
|
},
|
2034
2188
|
...prepareCallSettings(settings),
|
@@ -2106,12 +2260,10 @@ async function streamObject({
|
|
2106
2260
|
})
|
2107
2261
|
);
|
2108
2262
|
return new DefaultStreamObjectResult({
|
2263
|
+
outputStrategy,
|
2109
2264
|
stream: stream.pipeThrough(new TransformStream(transformer)),
|
2110
2265
|
warnings,
|
2111
2266
|
rawResponse,
|
2112
|
-
// type casting required for `undefined` schema (no-schema mode),
|
2113
|
-
// in which case <T> is <JSONValue> as desired.
|
2114
|
-
schema,
|
2115
2267
|
onFinish,
|
2116
2268
|
rootSpan,
|
2117
2269
|
doStreamSpan,
|
@@ -2126,7 +2278,7 @@ var DefaultStreamObjectResult = class {
|
|
2126
2278
|
stream,
|
2127
2279
|
warnings,
|
2128
2280
|
rawResponse,
|
2129
|
-
|
2281
|
+
outputStrategy,
|
2130
2282
|
onFinish,
|
2131
2283
|
rootSpan,
|
2132
2284
|
doStreamSpan,
|
@@ -2135,6 +2287,7 @@ var DefaultStreamObjectResult = class {
|
|
2135
2287
|
}) {
|
2136
2288
|
this.warnings = warnings;
|
2137
2289
|
this.rawResponse = rawResponse;
|
2290
|
+
this.outputStrategy = outputStrategy;
|
2138
2291
|
this.objectPromise = new DelayedPromise();
|
2139
2292
|
const { resolve: resolveUsage, promise: usagePromise } = createResolvablePromise();
|
2140
2293
|
this.usage = usagePromise;
|
@@ -2150,6 +2303,7 @@ var DefaultStreamObjectResult = class {
|
|
2150
2303
|
let error;
|
2151
2304
|
let accumulatedText = "";
|
2152
2305
|
let delta = "";
|
2306
|
+
let latestObjectJson = void 0;
|
2153
2307
|
let latestObject = void 0;
|
2154
2308
|
let firstChunk = true;
|
2155
2309
|
const self = this;
|
@@ -2169,20 +2323,25 @@ var DefaultStreamObjectResult = class {
|
|
2169
2323
|
if (typeof chunk === "string") {
|
2170
2324
|
accumulatedText += chunk;
|
2171
2325
|
delta += chunk;
|
2172
|
-
const
|
2173
|
-
|
2174
|
-
|
2175
|
-
|
2176
|
-
|
2177
|
-
controller.enqueue({
|
2178
|
-
type: "object",
|
2179
|
-
object: currentObject
|
2326
|
+
const { value: currentObjectJson, state: parseState } = (0, import_ui_utils2.parsePartialJson)(accumulatedText);
|
2327
|
+
if (currentObjectJson !== void 0 && !(0, import_ui_utils2.isDeepEqualData)(latestObjectJson, currentObjectJson)) {
|
2328
|
+
const validationResult = outputStrategy.validatePartialResult({
|
2329
|
+
value: currentObjectJson,
|
2330
|
+
parseState
|
2180
2331
|
});
|
2181
|
-
|
2182
|
-
|
2183
|
-
|
2184
|
-
|
2185
|
-
|
2332
|
+
if (validationResult.success && !(0, import_ui_utils2.isDeepEqualData)(latestObject, validationResult.value)) {
|
2333
|
+
latestObjectJson = currentObjectJson;
|
2334
|
+
latestObject = validationResult.value;
|
2335
|
+
controller.enqueue({
|
2336
|
+
type: "object",
|
2337
|
+
object: latestObject
|
2338
|
+
});
|
2339
|
+
controller.enqueue({
|
2340
|
+
type: "text-delta",
|
2341
|
+
textDelta: delta
|
2342
|
+
});
|
2343
|
+
delta = "";
|
2344
|
+
}
|
2186
2345
|
}
|
2187
2346
|
return;
|
2188
2347
|
}
|
@@ -2200,10 +2359,7 @@ var DefaultStreamObjectResult = class {
|
|
2200
2359
|
controller.enqueue({ ...chunk, usage });
|
2201
2360
|
resolveUsage(usage);
|
2202
2361
|
resolveProviderMetadata(providerMetadata);
|
2203
|
-
const validationResult = (
|
2204
|
-
value: latestObject,
|
2205
|
-
schema
|
2206
|
-
});
|
2362
|
+
const validationResult = outputStrategy.validateFinalResult(latestObjectJson);
|
2207
2363
|
if (validationResult.success) {
|
2208
2364
|
object = validationResult.value;
|
2209
2365
|
self.objectPromise.resolve(object);
|
@@ -2298,6 +2454,9 @@ var DefaultStreamObjectResult = class {
|
|
2298
2454
|
}
|
2299
2455
|
});
|
2300
2456
|
}
|
2457
|
+
get elementStream() {
|
2458
|
+
return this.outputStrategy.createElementStream(this.originalStream);
|
2459
|
+
}
|
2301
2460
|
get textStream() {
|
2302
2461
|
return createAsyncIterableStream(this.originalStream, {
|
2303
2462
|
transform(chunk, controller) {
|
@@ -2327,8 +2486,8 @@ var DefaultStreamObjectResult = class {
|
|
2327
2486
|
});
|
2328
2487
|
}
|
2329
2488
|
pipeTextStreamToResponse(response, init) {
|
2330
|
-
var
|
2331
|
-
response.writeHead((
|
2489
|
+
var _a10;
|
2490
|
+
response.writeHead((_a10 = init == null ? void 0 : init.status) != null ? _a10 : 200, {
|
2332
2491
|
"Content-Type": "text/plain; charset=utf-8",
|
2333
2492
|
...init == null ? void 0 : init.headers
|
2334
2493
|
});
|
@@ -2350,9 +2509,9 @@ var DefaultStreamObjectResult = class {
|
|
2350
2509
|
read();
|
2351
2510
|
}
|
2352
2511
|
toTextStreamResponse(init) {
|
2353
|
-
var
|
2512
|
+
var _a10;
|
2354
2513
|
return new Response(this.textStream.pipeThrough(new TextEncoderStream()), {
|
2355
|
-
status: (
|
2514
|
+
status: (_a10 = init == null ? void 0 : init.status) != null ? _a10 : 200,
|
2356
2515
|
headers: prepareResponseHeaders(init, {
|
2357
2516
|
contentType: "text/plain; charset=utf-8"
|
2358
2517
|
})
|
@@ -2381,9 +2540,9 @@ function prepareToolsAndToolChoice({
|
|
2381
2540
|
};
|
2382
2541
|
}
|
2383
2542
|
return {
|
2384
|
-
tools: Object.entries(tools).map(([
|
2543
|
+
tools: Object.entries(tools).map(([name10, tool2]) => ({
|
2385
2544
|
type: "function",
|
2386
|
-
name:
|
2545
|
+
name: name10,
|
2387
2546
|
description: tool2.description,
|
2388
2547
|
parameters: (0, import_ui_utils3.asSchema)(tool2.parameters).jsonSchema
|
2389
2548
|
})),
|
@@ -2396,17 +2555,17 @@ var import_provider_utils7 = require("@ai-sdk/provider-utils");
|
|
2396
2555
|
var import_ui_utils4 = require("@ai-sdk/ui-utils");
|
2397
2556
|
|
2398
2557
|
// errors/invalid-tool-arguments-error.ts
|
2399
|
-
var
|
2558
|
+
var import_provider10 = require("@ai-sdk/provider");
|
2400
2559
|
var name7 = "AI_InvalidToolArgumentsError";
|
2401
2560
|
var marker7 = `vercel.ai.error.${name7}`;
|
2402
2561
|
var symbol7 = Symbol.for(marker7);
|
2403
2562
|
var _a7;
|
2404
|
-
var InvalidToolArgumentsError = class extends
|
2563
|
+
var InvalidToolArgumentsError = class extends import_provider10.AISDKError {
|
2405
2564
|
constructor({
|
2406
2565
|
toolArgs,
|
2407
2566
|
toolName,
|
2408
2567
|
cause,
|
2409
|
-
message = `Invalid arguments for tool ${toolName}: ${(0,
|
2568
|
+
message = `Invalid arguments for tool ${toolName}: ${(0, import_provider10.getErrorMessage)(
|
2410
2569
|
cause
|
2411
2570
|
)}`
|
2412
2571
|
}) {
|
@@ -2416,7 +2575,7 @@ var InvalidToolArgumentsError = class extends import_provider9.AISDKError {
|
|
2416
2575
|
this.toolName = toolName;
|
2417
2576
|
}
|
2418
2577
|
static isInstance(error) {
|
2419
|
-
return
|
2578
|
+
return import_provider10.AISDKError.hasMarker(error, marker7);
|
2420
2579
|
}
|
2421
2580
|
/**
|
2422
2581
|
* @deprecated use `isInstance` instead
|
@@ -2441,12 +2600,12 @@ var InvalidToolArgumentsError = class extends import_provider9.AISDKError {
|
|
2441
2600
|
_a7 = symbol7;
|
2442
2601
|
|
2443
2602
|
// errors/no-such-tool-error.ts
|
2444
|
-
var
|
2603
|
+
var import_provider11 = require("@ai-sdk/provider");
|
2445
2604
|
var name8 = "AI_NoSuchToolError";
|
2446
2605
|
var marker8 = `vercel.ai.error.${name8}`;
|
2447
2606
|
var symbol8 = Symbol.for(marker8);
|
2448
2607
|
var _a8;
|
2449
|
-
var NoSuchToolError = class extends
|
2608
|
+
var NoSuchToolError = class extends import_provider11.AISDKError {
|
2450
2609
|
constructor({
|
2451
2610
|
toolName,
|
2452
2611
|
availableTools = void 0,
|
@@ -2458,7 +2617,7 @@ var NoSuchToolError = class extends import_provider10.AISDKError {
|
|
2458
2617
|
this.availableTools = availableTools;
|
2459
2618
|
}
|
2460
2619
|
static isInstance(error) {
|
2461
|
-
return
|
2620
|
+
return import_provider11.AISDKError.hasMarker(error, marker8);
|
2462
2621
|
}
|
2463
2622
|
/**
|
2464
2623
|
* @deprecated use `isInstance` instead
|
@@ -2532,14 +2691,14 @@ async function generateText({
|
|
2532
2691
|
experimental_telemetry: telemetry,
|
2533
2692
|
...settings
|
2534
2693
|
}) {
|
2535
|
-
var
|
2694
|
+
var _a10;
|
2536
2695
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
2537
2696
|
model,
|
2538
2697
|
telemetry,
|
2539
2698
|
headers,
|
2540
2699
|
settings: { ...settings, maxRetries }
|
2541
2700
|
});
|
2542
|
-
const tracer = getTracer({ isEnabled: (
|
2701
|
+
const tracer = getTracer({ isEnabled: (_a10 = telemetry == null ? void 0 : telemetry.isEnabled) != null ? _a10 : false });
|
2543
2702
|
return recordSpan({
|
2544
2703
|
name: "ai.generateText",
|
2545
2704
|
attributes: selectTelemetryAttributes({
|
@@ -2559,7 +2718,7 @@ async function generateText({
|
|
2559
2718
|
}),
|
2560
2719
|
tracer,
|
2561
2720
|
fn: async (span) => {
|
2562
|
-
var
|
2721
|
+
var _a11, _b, _c, _d;
|
2563
2722
|
const retry = retryWithExponentialBackoff({ maxRetries });
|
2564
2723
|
const validatedPrompt = validatePrompt({
|
2565
2724
|
system,
|
@@ -2645,7 +2804,7 @@ async function generateText({
|
|
2645
2804
|
}
|
2646
2805
|
})
|
2647
2806
|
);
|
2648
|
-
currentToolCalls = ((
|
2807
|
+
currentToolCalls = ((_a11 = currentModelResponse.toolCalls) != null ? _a11 : []).map(
|
2649
2808
|
(modelToolCall) => parseToolCall({ toolCall: modelToolCall, tools })
|
2650
2809
|
);
|
2651
2810
|
currentToolResults = tools == null ? [] : await executeTools({
|
@@ -3113,14 +3272,14 @@ async function streamText({
|
|
3113
3272
|
onFinish,
|
3114
3273
|
...settings
|
3115
3274
|
}) {
|
3116
|
-
var
|
3275
|
+
var _a10;
|
3117
3276
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
3118
3277
|
model,
|
3119
3278
|
telemetry,
|
3120
3279
|
headers,
|
3121
3280
|
settings: { ...settings, maxRetries }
|
3122
3281
|
});
|
3123
|
-
const tracer = getTracer({ isEnabled: (
|
3282
|
+
const tracer = getTracer({ isEnabled: (_a10 = telemetry == null ? void 0 : telemetry.isEnabled) != null ? _a10 : false });
|
3124
3283
|
return recordSpan({
|
3125
3284
|
name: "ai.streamText",
|
3126
3285
|
attributes: selectTelemetryAttributes({
|
@@ -3497,8 +3656,8 @@ var DefaultStreamTextResult = class {
|
|
3497
3656
|
return this.pipeDataStreamToResponse(response, init);
|
3498
3657
|
}
|
3499
3658
|
pipeDataStreamToResponse(response, init) {
|
3500
|
-
var
|
3501
|
-
response.writeHead((
|
3659
|
+
var _a10;
|
3660
|
+
response.writeHead((_a10 = init == null ? void 0 : init.status) != null ? _a10 : 200, {
|
3502
3661
|
"Content-Type": "text/plain; charset=utf-8",
|
3503
3662
|
...init == null ? void 0 : init.headers
|
3504
3663
|
});
|
@@ -3520,8 +3679,8 @@ var DefaultStreamTextResult = class {
|
|
3520
3679
|
read();
|
3521
3680
|
}
|
3522
3681
|
pipeTextStreamToResponse(response, init) {
|
3523
|
-
var
|
3524
|
-
response.writeHead((
|
3682
|
+
var _a10;
|
3683
|
+
response.writeHead((_a10 = init == null ? void 0 : init.status) != null ? _a10 : 200, {
|
3525
3684
|
"Content-Type": "text/plain; charset=utf-8",
|
3526
3685
|
...init == null ? void 0 : init.headers
|
3527
3686
|
});
|
@@ -3546,7 +3705,7 @@ var DefaultStreamTextResult = class {
|
|
3546
3705
|
return this.toDataStreamResponse(options);
|
3547
3706
|
}
|
3548
3707
|
toDataStreamResponse(options) {
|
3549
|
-
var
|
3708
|
+
var _a10;
|
3550
3709
|
const init = options == null ? void 0 : "init" in options ? options.init : {
|
3551
3710
|
headers: "headers" in options ? options.headers : void 0,
|
3552
3711
|
status: "status" in options ? options.status : void 0,
|
@@ -3556,7 +3715,7 @@ var DefaultStreamTextResult = class {
|
|
3556
3715
|
const getErrorMessage4 = options == null ? void 0 : "getErrorMessage" in options ? options.getErrorMessage : void 0;
|
3557
3716
|
const stream = data ? mergeStreams(data.stream, this.toDataStream({ getErrorMessage: getErrorMessage4 })) : this.toDataStream({ getErrorMessage: getErrorMessage4 });
|
3558
3717
|
return new Response(stream, {
|
3559
|
-
status: (
|
3718
|
+
status: (_a10 = init == null ? void 0 : init.status) != null ? _a10 : 200,
|
3560
3719
|
statusText: init == null ? void 0 : init.statusText,
|
3561
3720
|
headers: prepareResponseHeaders(init, {
|
3562
3721
|
contentType: "text/plain; charset=utf-8",
|
@@ -3565,9 +3724,9 @@ var DefaultStreamTextResult = class {
|
|
3565
3724
|
});
|
3566
3725
|
}
|
3567
3726
|
toTextStreamResponse(init) {
|
3568
|
-
var
|
3727
|
+
var _a10;
|
3569
3728
|
return new Response(this.textStream.pipeThrough(new TextEncoderStream()), {
|
3570
|
-
status: (
|
3729
|
+
status: (_a10 = init == null ? void 0 : init.status) != null ? _a10 : 200,
|
3571
3730
|
headers: prepareResponseHeaders(init, {
|
3572
3731
|
contentType: "text/plain; charset=utf-8"
|
3573
3732
|
})
|
@@ -3578,7 +3737,7 @@ var experimental_streamText = streamText;
|
|
3578
3737
|
|
3579
3738
|
// core/prompt/attachments-to-parts.ts
|
3580
3739
|
function attachmentsToParts(attachments) {
|
3581
|
-
var
|
3740
|
+
var _a10, _b, _c;
|
3582
3741
|
const parts = [];
|
3583
3742
|
for (const attachment of attachments) {
|
3584
3743
|
let url;
|
@@ -3590,7 +3749,7 @@ function attachmentsToParts(attachments) {
|
|
3590
3749
|
switch (url.protocol) {
|
3591
3750
|
case "http:":
|
3592
3751
|
case "https:": {
|
3593
|
-
if ((
|
3752
|
+
if ((_a10 = attachment.contentType) == null ? void 0 : _a10.startsWith("image/")) {
|
3594
3753
|
parts.push({ type: "image", image: url });
|
3595
3754
|
}
|
3596
3755
|
break;
|
@@ -3698,110 +3857,62 @@ function convertToCoreMessages(messages) {
|
|
3698
3857
|
return coreMessages;
|
3699
3858
|
}
|
3700
3859
|
|
3701
|
-
// core/registry/
|
3702
|
-
var
|
3703
|
-
|
3860
|
+
// core/registry/custom-provider.ts
|
3861
|
+
var import_provider12 = require("@ai-sdk/provider");
|
3862
|
+
function experimental_customProvider({
|
3863
|
+
languageModels,
|
3864
|
+
textEmbeddingModels,
|
3865
|
+
fallbackProvider
|
3866
|
+
}) {
|
3867
|
+
return {
|
3868
|
+
languageModel(modelId) {
|
3869
|
+
if (languageModels != null && modelId in languageModels) {
|
3870
|
+
return languageModels[modelId];
|
3871
|
+
}
|
3872
|
+
if (fallbackProvider) {
|
3873
|
+
return fallbackProvider.languageModel(modelId);
|
3874
|
+
}
|
3875
|
+
throw new import_provider12.NoSuchModelError({ modelId, modelType: "languageModel" });
|
3876
|
+
},
|
3877
|
+
textEmbeddingModel(modelId) {
|
3878
|
+
if (textEmbeddingModels != null && modelId in textEmbeddingModels) {
|
3879
|
+
return textEmbeddingModels[modelId];
|
3880
|
+
}
|
3881
|
+
if (fallbackProvider) {
|
3882
|
+
return fallbackProvider.textEmbeddingModel(modelId);
|
3883
|
+
}
|
3884
|
+
throw new import_provider12.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
3885
|
+
}
|
3886
|
+
};
|
3887
|
+
}
|
3888
|
+
|
3889
|
+
// core/registry/no-such-provider-error.ts
|
3890
|
+
var import_provider13 = require("@ai-sdk/provider");
|
3891
|
+
var name9 = "AI_NoSuchProviderError";
|
3704
3892
|
var marker9 = `vercel.ai.error.${name9}`;
|
3705
3893
|
var symbol9 = Symbol.for(marker9);
|
3706
3894
|
var _a9;
|
3707
|
-
var
|
3708
|
-
constructor({
|
3709
|
-
id,
|
3710
|
-
message = `Invalid model id: ${id}`
|
3711
|
-
}) {
|
3712
|
-
super({ name: name9, message });
|
3713
|
-
this[_a9] = true;
|
3714
|
-
this.id = id;
|
3715
|
-
}
|
3716
|
-
static isInstance(error) {
|
3717
|
-
return import_provider11.AISDKError.hasMarker(error, marker9);
|
3718
|
-
}
|
3719
|
-
/**
|
3720
|
-
* @deprecated use `isInstance` instead
|
3721
|
-
*/
|
3722
|
-
static isInvalidModelIdError(error) {
|
3723
|
-
return error instanceof Error && error.name === name9 && typeof error.id === "string";
|
3724
|
-
}
|
3725
|
-
/**
|
3726
|
-
* @deprecated Do not use this method. It will be removed in the next major version.
|
3727
|
-
*/
|
3728
|
-
toJSON() {
|
3729
|
-
return {
|
3730
|
-
name: this.name,
|
3731
|
-
message: this.message,
|
3732
|
-
stack: this.stack,
|
3733
|
-
id: this.id
|
3734
|
-
};
|
3735
|
-
}
|
3736
|
-
};
|
3737
|
-
_a9 = symbol9;
|
3738
|
-
|
3739
|
-
// core/registry/no-such-model-error.ts
|
3740
|
-
var import_provider12 = require("@ai-sdk/provider");
|
3741
|
-
var name10 = "AI_NoSuchModelError";
|
3742
|
-
var marker10 = `vercel.ai.error.${name10}`;
|
3743
|
-
var symbol10 = Symbol.for(marker10);
|
3744
|
-
var _a10;
|
3745
|
-
var NoSuchModelError = class extends import_provider12.AISDKError {
|
3895
|
+
var NoSuchProviderError = class extends import_provider13.NoSuchModelError {
|
3746
3896
|
constructor({
|
3747
3897
|
modelId,
|
3748
3898
|
modelType,
|
3749
|
-
message = `No such ${modelType}: ${modelId}`
|
3750
|
-
}) {
|
3751
|
-
super({ name: name10, message });
|
3752
|
-
this[_a10] = true;
|
3753
|
-
this.modelId = modelId;
|
3754
|
-
this.modelType = modelType;
|
3755
|
-
}
|
3756
|
-
static isInstance(error) {
|
3757
|
-
return import_provider12.AISDKError.hasMarker(error, marker10);
|
3758
|
-
}
|
3759
|
-
/**
|
3760
|
-
* @deprecated use `isInstance` instead
|
3761
|
-
*/
|
3762
|
-
static isNoSuchModelError(error) {
|
3763
|
-
return error instanceof Error && error.name === name10 && typeof error.modelId === "string" && typeof error.modelType === "string";
|
3764
|
-
}
|
3765
|
-
/**
|
3766
|
-
* @deprecated Do not use this method. It will be removed in the next major version.
|
3767
|
-
*/
|
3768
|
-
toJSON() {
|
3769
|
-
return {
|
3770
|
-
name: this.name,
|
3771
|
-
message: this.message,
|
3772
|
-
stack: this.stack,
|
3773
|
-
modelId: this.modelId,
|
3774
|
-
modelType: this.modelType
|
3775
|
-
};
|
3776
|
-
}
|
3777
|
-
};
|
3778
|
-
_a10 = symbol10;
|
3779
|
-
|
3780
|
-
// core/registry/no-such-provider-error.ts
|
3781
|
-
var import_provider13 = require("@ai-sdk/provider");
|
3782
|
-
var name11 = "AI_NoSuchProviderError";
|
3783
|
-
var marker11 = `vercel.ai.error.${name11}`;
|
3784
|
-
var symbol11 = Symbol.for(marker11);
|
3785
|
-
var _a11;
|
3786
|
-
var NoSuchProviderError = class extends import_provider13.AISDKError {
|
3787
|
-
constructor({
|
3788
3899
|
providerId,
|
3789
3900
|
availableProviders,
|
3790
3901
|
message = `No such provider: ${providerId} (available providers: ${availableProviders.join()})`
|
3791
3902
|
}) {
|
3792
|
-
super({
|
3793
|
-
this[
|
3903
|
+
super({ errorName: name9, modelId, modelType, message });
|
3904
|
+
this[_a9] = true;
|
3794
3905
|
this.providerId = providerId;
|
3795
3906
|
this.availableProviders = availableProviders;
|
3796
3907
|
}
|
3797
3908
|
static isInstance(error) {
|
3798
|
-
return import_provider13.AISDKError.hasMarker(error,
|
3909
|
+
return import_provider13.AISDKError.hasMarker(error, marker9);
|
3799
3910
|
}
|
3800
3911
|
/**
|
3801
3912
|
* @deprecated use `isInstance` instead
|
3802
3913
|
*/
|
3803
3914
|
static isNoSuchProviderError(error) {
|
3804
|
-
return error instanceof Error && error.name ===
|
3915
|
+
return error instanceof Error && error.name === name9 && typeof error.providerId === "string" && Array.isArray(error.availableProviders);
|
3805
3916
|
}
|
3806
3917
|
/**
|
3807
3918
|
* @deprecated Do not use this method. It will be removed in the next major version.
|
@@ -3811,14 +3922,17 @@ var NoSuchProviderError = class extends import_provider13.AISDKError {
|
|
3811
3922
|
name: this.name,
|
3812
3923
|
message: this.message,
|
3813
3924
|
stack: this.stack,
|
3925
|
+
modelId: this.modelId,
|
3926
|
+
modelType: this.modelType,
|
3814
3927
|
providerId: this.providerId,
|
3815
3928
|
availableProviders: this.availableProviders
|
3816
3929
|
};
|
3817
3930
|
}
|
3818
3931
|
};
|
3819
|
-
|
3932
|
+
_a9 = symbol9;
|
3820
3933
|
|
3821
3934
|
// core/registry/provider-registry.ts
|
3935
|
+
var import_provider14 = require("@ai-sdk/provider");
|
3822
3936
|
function experimental_createProviderRegistry(providers) {
|
3823
3937
|
const registry = new DefaultProviderRegistry();
|
3824
3938
|
for (const [id, provider] of Object.entries(providers)) {
|
@@ -3841,35 +3955,41 @@ var DefaultProviderRegistry = class {
|
|
3841
3955
|
const provider = this.providers[id];
|
3842
3956
|
if (provider == null) {
|
3843
3957
|
throw new NoSuchProviderError({
|
3958
|
+
modelId: id,
|
3959
|
+
modelType: "languageModel",
|
3844
3960
|
providerId: id,
|
3845
3961
|
availableProviders: Object.keys(this.providers)
|
3846
3962
|
});
|
3847
3963
|
}
|
3848
3964
|
return provider;
|
3849
3965
|
}
|
3850
|
-
splitId(id) {
|
3966
|
+
splitId(id, modelType) {
|
3851
3967
|
const index = id.indexOf(":");
|
3852
3968
|
if (index === -1) {
|
3853
|
-
throw new
|
3969
|
+
throw new import_provider14.NoSuchModelError({
|
3970
|
+
modelId: id,
|
3971
|
+
modelType,
|
3972
|
+
message: `Invalid ${modelType} id for registry: ${id} (must be in the format "providerId:modelId")`
|
3973
|
+
});
|
3854
3974
|
}
|
3855
3975
|
return [id.slice(0, index), id.slice(index + 1)];
|
3856
3976
|
}
|
3857
3977
|
languageModel(id) {
|
3858
|
-
var
|
3859
|
-
const [providerId, modelId] = this.splitId(id);
|
3860
|
-
const model = (_b = (
|
3978
|
+
var _a10, _b;
|
3979
|
+
const [providerId, modelId] = this.splitId(id, "languageModel");
|
3980
|
+
const model = (_b = (_a10 = this.getProvider(providerId)).languageModel) == null ? void 0 : _b.call(_a10, modelId);
|
3861
3981
|
if (model == null) {
|
3862
|
-
throw new NoSuchModelError({ modelId: id, modelType: "languageModel" });
|
3982
|
+
throw new import_provider14.NoSuchModelError({ modelId: id, modelType: "languageModel" });
|
3863
3983
|
}
|
3864
3984
|
return model;
|
3865
3985
|
}
|
3866
3986
|
textEmbeddingModel(id) {
|
3867
|
-
var
|
3868
|
-
const [providerId, modelId] = this.splitId(id);
|
3987
|
+
var _a10, _b, _c;
|
3988
|
+
const [providerId, modelId] = this.splitId(id, "textEmbeddingModel");
|
3869
3989
|
const provider = this.getProvider(providerId);
|
3870
|
-
const model = (_c = (
|
3990
|
+
const model = (_c = (_a10 = provider.textEmbeddingModel) == null ? void 0 : _a10.call(provider, modelId)) != null ? _c : "textEmbedding" in provider ? (_b = provider.textEmbedding) == null ? void 0 : _b.call(provider, modelId) : void 0;
|
3871
3991
|
if (model == null) {
|
3872
|
-
throw new NoSuchModelError({
|
3992
|
+
throw new import_provider14.NoSuchModelError({
|
3873
3993
|
modelId: id,
|
3874
3994
|
modelType: "textEmbeddingModel"
|
3875
3995
|
});
|
@@ -3909,7 +4029,7 @@ function magnitude(vector) {
|
|
3909
4029
|
}
|
3910
4030
|
|
3911
4031
|
// errors/index.ts
|
3912
|
-
var
|
4032
|
+
var import_provider15 = require("@ai-sdk/provider");
|
3913
4033
|
|
3914
4034
|
// streams/ai-stream.ts
|
3915
4035
|
var import_eventsource_parser = require("eventsource-parser");
|
@@ -4027,8 +4147,8 @@ function readableFromAsyncIterable(iterable) {
|
|
4027
4147
|
controller.enqueue(value);
|
4028
4148
|
},
|
4029
4149
|
async cancel(reason) {
|
4030
|
-
var
|
4031
|
-
await ((
|
4150
|
+
var _a10;
|
4151
|
+
await ((_a10 = it.return) == null ? void 0 : _a10.call(it, reason));
|
4032
4152
|
}
|
4033
4153
|
});
|
4034
4154
|
}
|
@@ -4165,7 +4285,7 @@ var import_ui_utils8 = require("@ai-sdk/ui-utils");
|
|
4165
4285
|
function AssistantResponse({ threadId, messageId }, process2) {
|
4166
4286
|
const stream = new ReadableStream({
|
4167
4287
|
async start(controller) {
|
4168
|
-
var
|
4288
|
+
var _a10;
|
4169
4289
|
const textEncoder = new TextEncoder();
|
4170
4290
|
const sendMessage = (message) => {
|
4171
4291
|
controller.enqueue(
|
@@ -4183,7 +4303,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
4183
4303
|
);
|
4184
4304
|
};
|
4185
4305
|
const forwardStream = async (stream2) => {
|
4186
|
-
var
|
4306
|
+
var _a11, _b;
|
4187
4307
|
let result = void 0;
|
4188
4308
|
for await (const value of stream2) {
|
4189
4309
|
switch (value.event) {
|
@@ -4200,7 +4320,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
4200
4320
|
break;
|
4201
4321
|
}
|
4202
4322
|
case "thread.message.delta": {
|
4203
|
-
const content = (
|
4323
|
+
const content = (_a11 = value.data.delta.content) == null ? void 0 : _a11[0];
|
4204
4324
|
if ((content == null ? void 0 : content.type) === "text" && ((_b = content.text) == null ? void 0 : _b.value) != null) {
|
4205
4325
|
controller.enqueue(
|
4206
4326
|
textEncoder.encode(
|
@@ -4236,7 +4356,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
4236
4356
|
forwardStream
|
4237
4357
|
});
|
4238
4358
|
} catch (error) {
|
4239
|
-
sendError((
|
4359
|
+
sendError((_a10 = error.message) != null ? _a10 : `${error}`);
|
4240
4360
|
} finally {
|
4241
4361
|
controller.close();
|
4242
4362
|
}
|
@@ -4257,9 +4377,9 @@ var experimental_AssistantResponse = AssistantResponse;
|
|
4257
4377
|
|
4258
4378
|
// streams/aws-bedrock-stream.ts
|
4259
4379
|
async function* asDeltaIterable(response, extractTextDeltaFromChunk) {
|
4260
|
-
var
|
4380
|
+
var _a10, _b;
|
4261
4381
|
const decoder = new TextDecoder();
|
4262
|
-
for await (const chunk of (
|
4382
|
+
for await (const chunk of (_a10 = response.body) != null ? _a10 : []) {
|
4263
4383
|
const bytes = (_b = chunk.chunk) == null ? void 0 : _b.bytes;
|
4264
4384
|
if (bytes != null) {
|
4265
4385
|
const chunkText = decoder.decode(bytes);
|
@@ -4273,8 +4393,8 @@ async function* asDeltaIterable(response, extractTextDeltaFromChunk) {
|
|
4273
4393
|
}
|
4274
4394
|
function AWSBedrockAnthropicMessagesStream(response, callbacks) {
|
4275
4395
|
return AWSBedrockStream(response, callbacks, (chunk) => {
|
4276
|
-
var
|
4277
|
-
return (
|
4396
|
+
var _a10;
|
4397
|
+
return (_a10 = chunk.delta) == null ? void 0 : _a10.text;
|
4278
4398
|
});
|
4279
4399
|
}
|
4280
4400
|
function AWSBedrockAnthropicStream(response, callbacks) {
|
@@ -4321,8 +4441,8 @@ async function readAndProcessLines(reader, controller) {
|
|
4321
4441
|
controller.close();
|
4322
4442
|
}
|
4323
4443
|
function createParser2(res) {
|
4324
|
-
var
|
4325
|
-
const reader = (
|
4444
|
+
var _a10;
|
4445
|
+
const reader = (_a10 = res.body) == null ? void 0 : _a10.getReader();
|
4326
4446
|
return new ReadableStream({
|
4327
4447
|
async start(controller) {
|
4328
4448
|
if (!reader) {
|
@@ -4352,9 +4472,9 @@ function CohereStream(reader, callbacks) {
|
|
4352
4472
|
|
4353
4473
|
// streams/google-generative-ai-stream.ts
|
4354
4474
|
async function* streamable3(response) {
|
4355
|
-
var
|
4475
|
+
var _a10, _b, _c;
|
4356
4476
|
for await (const chunk of response.stream) {
|
4357
|
-
const parts = (_c = (_b = (
|
4477
|
+
const parts = (_c = (_b = (_a10 = chunk.candidates) == null ? void 0 : _a10[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts;
|
4358
4478
|
if (parts === void 0) {
|
4359
4479
|
continue;
|
4360
4480
|
}
|
@@ -4373,13 +4493,13 @@ function createParser3(res) {
|
|
4373
4493
|
const trimStartOfStream = trimStartOfStreamHelper();
|
4374
4494
|
return new ReadableStream({
|
4375
4495
|
async pull(controller) {
|
4376
|
-
var
|
4496
|
+
var _a10, _b;
|
4377
4497
|
const { value, done } = await res.next();
|
4378
4498
|
if (done) {
|
4379
4499
|
controller.close();
|
4380
4500
|
return;
|
4381
4501
|
}
|
4382
|
-
const text = trimStartOfStream((_b = (
|
4502
|
+
const text = trimStartOfStream((_b = (_a10 = value.token) == null ? void 0 : _a10.text) != null ? _b : "");
|
4383
4503
|
if (!text)
|
4384
4504
|
return;
|
4385
4505
|
if (value.generated_text != null && value.generated_text.length > 0) {
|
@@ -4404,11 +4524,11 @@ function InkeepStream(res, callbacks) {
|
|
4404
4524
|
let chat_session_id = "";
|
4405
4525
|
let records_cited;
|
4406
4526
|
const inkeepEventParser = (data, options) => {
|
4407
|
-
var
|
4527
|
+
var _a10, _b;
|
4408
4528
|
const { event } = options;
|
4409
4529
|
if (event === "records_cited") {
|
4410
4530
|
records_cited = JSON.parse(data);
|
4411
|
-
(
|
4531
|
+
(_a10 = callbacks == null ? void 0 : callbacks.onRecordsCited) == null ? void 0 : _a10.call(callbacks, records_cited);
|
4412
4532
|
}
|
4413
4533
|
if (event === "message_chunk") {
|
4414
4534
|
const inkeepMessageChunk = JSON.parse(data);
|
@@ -4421,12 +4541,12 @@ function InkeepStream(res, callbacks) {
|
|
4421
4541
|
passThroughCallbacks = {
|
4422
4542
|
...passThroughCallbacks,
|
4423
4543
|
onFinal: (completion) => {
|
4424
|
-
var
|
4544
|
+
var _a10;
|
4425
4545
|
const inkeepOnFinalMetadata = {
|
4426
4546
|
chat_session_id,
|
4427
4547
|
records_cited
|
4428
4548
|
};
|
4429
|
-
(
|
4549
|
+
(_a10 = callbacks == null ? void 0 : callbacks.onFinal) == null ? void 0 : _a10.call(callbacks, completion, inkeepOnFinalMetadata);
|
4430
4550
|
}
|
4431
4551
|
};
|
4432
4552
|
return AIStream(res, inkeepEventParser, passThroughCallbacks).pipeThrough(
|
@@ -4448,7 +4568,7 @@ function toDataStream(stream, callbacks) {
|
|
4448
4568
|
return stream.pipeThrough(
|
4449
4569
|
new TransformStream({
|
4450
4570
|
transform: async (value, controller) => {
|
4451
|
-
var
|
4571
|
+
var _a10;
|
4452
4572
|
if (typeof value === "string") {
|
4453
4573
|
controller.enqueue(value);
|
4454
4574
|
return;
|
@@ -4456,7 +4576,7 @@ function toDataStream(stream, callbacks) {
|
|
4456
4576
|
if ("event" in value) {
|
4457
4577
|
if (value.event === "on_chat_model_stream") {
|
4458
4578
|
forwardAIMessageChunk(
|
4459
|
-
(
|
4579
|
+
(_a10 = value.data) == null ? void 0 : _a10.chunk,
|
4460
4580
|
controller
|
4461
4581
|
);
|
4462
4582
|
}
|
@@ -4468,13 +4588,13 @@ function toDataStream(stream, callbacks) {
|
|
4468
4588
|
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
|
4469
4589
|
}
|
4470
4590
|
function toDataStreamResponse(stream, options) {
|
4471
|
-
var
|
4591
|
+
var _a10;
|
4472
4592
|
const dataStream = toDataStream(stream, options == null ? void 0 : options.callbacks);
|
4473
4593
|
const data = options == null ? void 0 : options.data;
|
4474
4594
|
const init = options == null ? void 0 : options.init;
|
4475
4595
|
const responseStream = data ? mergeStreams(data.stream, dataStream) : dataStream;
|
4476
4596
|
return new Response(responseStream, {
|
4477
|
-
status: (
|
4597
|
+
status: (_a10 = init == null ? void 0 : init.status) != null ? _a10 : 200,
|
4478
4598
|
statusText: init == null ? void 0 : init.statusText,
|
4479
4599
|
headers: prepareResponseHeaders(init, {
|
4480
4600
|
contentType: "text/plain; charset=utf-8",
|
@@ -4556,9 +4676,9 @@ function LangChainStream(callbacks) {
|
|
4556
4676
|
|
4557
4677
|
// streams/mistral-stream.ts
|
4558
4678
|
async function* streamable4(stream) {
|
4559
|
-
var
|
4679
|
+
var _a10, _b;
|
4560
4680
|
for await (const chunk of stream) {
|
4561
|
-
const content = (_b = (
|
4681
|
+
const content = (_b = (_a10 = chunk.choices[0]) == null ? void 0 : _a10.delta) == null ? void 0 : _b.content;
|
4562
4682
|
if (content === void 0 || content === "") {
|
4563
4683
|
continue;
|
4564
4684
|
}
|
@@ -4588,10 +4708,10 @@ async function* streamable5(stream) {
|
|
4588
4708
|
model: chunk.model,
|
4589
4709
|
// not exposed by Azure API
|
4590
4710
|
choices: chunk.choices.map((choice) => {
|
4591
|
-
var
|
4711
|
+
var _a10, _b, _c, _d, _e, _f, _g;
|
4592
4712
|
return {
|
4593
4713
|
delta: {
|
4594
|
-
content: (
|
4714
|
+
content: (_a10 = choice.delta) == null ? void 0 : _a10.content,
|
4595
4715
|
function_call: (_b = choice.delta) == null ? void 0 : _b.functionCall,
|
4596
4716
|
role: (_c = choice.delta) == null ? void 0 : _c.role,
|
4597
4717
|
tool_calls: ((_e = (_d = choice.delta) == null ? void 0 : _d.toolCalls) == null ? void 0 : _e.length) ? (_g = (_f = choice.delta) == null ? void 0 : _f.toolCalls) == null ? void 0 : _g.map((toolCall, index) => ({
|
@@ -4616,9 +4736,9 @@ function chunkToText() {
|
|
4616
4736
|
const trimStartOfStream = trimStartOfStreamHelper();
|
4617
4737
|
let isFunctionStreamingIn;
|
4618
4738
|
return (json) => {
|
4619
|
-
var
|
4739
|
+
var _a10, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
|
4620
4740
|
if (isChatCompletionChunk(json)) {
|
4621
|
-
const delta = (
|
4741
|
+
const delta = (_a10 = json.choices[0]) == null ? void 0 : _a10.delta;
|
4622
4742
|
if ((_b = delta.function_call) == null ? void 0 : _b.name) {
|
4623
4743
|
isFunctionStreamingIn = true;
|
4624
4744
|
return {
|
@@ -4891,8 +5011,8 @@ function createFunctionCallTransformer(callbacks) {
|
|
4891
5011
|
|
4892
5012
|
// streams/replicate-stream.ts
|
4893
5013
|
async function ReplicateStream(res, cb, options) {
|
4894
|
-
var
|
4895
|
-
const url = (
|
5014
|
+
var _a10;
|
5015
|
+
const url = (_a10 = res.urls) == null ? void 0 : _a10.stream;
|
4896
5016
|
if (!url) {
|
4897
5017
|
if (res.error)
|
4898
5018
|
throw new Error(res.error);
|
@@ -4913,8 +5033,8 @@ async function ReplicateStream(res, cb, options) {
|
|
4913
5033
|
|
4914
5034
|
// streams/stream-to-response.ts
|
4915
5035
|
function streamToResponse(res, response, init, data) {
|
4916
|
-
var
|
4917
|
-
response.writeHead((
|
5036
|
+
var _a10;
|
5037
|
+
response.writeHead((_a10 = init == null ? void 0 : init.status) != null ? _a10 : 200, {
|
4918
5038
|
"Content-Type": "text/plain; charset=utf-8",
|
4919
5039
|
...init == null ? void 0 : init.headers
|
4920
5040
|
});
|
@@ -4977,7 +5097,6 @@ var nanoid = import_provider_utils8.generateId;
|
|
4977
5097
|
InvalidArgumentError,
|
4978
5098
|
InvalidDataContentError,
|
4979
5099
|
InvalidMessageRoleError,
|
4980
|
-
InvalidModelIdError,
|
4981
5100
|
InvalidPromptError,
|
4982
5101
|
InvalidResponseDataError,
|
4983
5102
|
InvalidToolArgumentsError,
|
@@ -4986,6 +5105,7 @@ var nanoid = import_provider_utils8.generateId;
|
|
4986
5105
|
LangChainStream,
|
4987
5106
|
LoadAPIKeyError,
|
4988
5107
|
MistralStream,
|
5108
|
+
NoContentGeneratedError,
|
4989
5109
|
NoObjectGeneratedError,
|
4990
5110
|
NoSuchModelError,
|
4991
5111
|
NoSuchProviderError,
|
@@ -5008,6 +5128,7 @@ var nanoid = import_provider_utils8.generateId;
|
|
5008
5128
|
experimental_StreamData,
|
5009
5129
|
experimental_createModelRegistry,
|
5010
5130
|
experimental_createProviderRegistry,
|
5131
|
+
experimental_customProvider,
|
5011
5132
|
experimental_generateObject,
|
5012
5133
|
experimental_generateText,
|
5013
5134
|
experimental_streamObject,
|