ai 3.3.15 → 3.3.17
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 +228 -240
- package/dist/index.d.ts +228 -240
- package/dist/index.js +337 -93
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +323 -75
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -7
package/dist/index.mjs
CHANGED
@@ -589,7 +589,6 @@ var DefaultEmbedManyResult = class {
|
|
589
589
|
|
590
590
|
// core/generate-object/generate-object.ts
|
591
591
|
import { safeParseJSON } from "@ai-sdk/provider-utils";
|
592
|
-
import { asSchema } from "@ai-sdk/ui-utils";
|
593
592
|
|
594
593
|
// core/prompt/convert-to-language-model-prompt.ts
|
595
594
|
import { getErrorMessage as getErrorMessage2 } from "@ai-sdk/provider-utils";
|
@@ -1333,21 +1332,22 @@ function prepareResponseHeaders(init, {
|
|
1333
1332
|
return headers;
|
1334
1333
|
}
|
1335
1334
|
|
1336
|
-
// core/generate-object/inject-json-
|
1335
|
+
// core/generate-object/inject-json-instruction.ts
|
1337
1336
|
var DEFAULT_SCHEMA_PREFIX = "JSON schema:";
|
1338
1337
|
var DEFAULT_SCHEMA_SUFFIX = "You MUST answer with a JSON object that matches the JSON schema above.";
|
1339
|
-
|
1340
|
-
|
1338
|
+
var DEFAULT_GENERIC_SUFFIX = "You MUST answer with JSON.";
|
1339
|
+
function injectJsonInstruction({
|
1340
|
+
prompt,
|
1341
1341
|
schema,
|
1342
|
-
schemaPrefix = DEFAULT_SCHEMA_PREFIX,
|
1343
|
-
schemaSuffix = DEFAULT_SCHEMA_SUFFIX
|
1342
|
+
schemaPrefix = schema != null ? DEFAULT_SCHEMA_PREFIX : void 0,
|
1343
|
+
schemaSuffix = schema != null ? DEFAULT_SCHEMA_SUFFIX : DEFAULT_GENERIC_SUFFIX
|
1344
1344
|
}) {
|
1345
1345
|
return [
|
1346
|
-
|
1347
|
-
|
1348
|
-
// add a newline if
|
1346
|
+
prompt != null && prompt.length > 0 ? prompt : void 0,
|
1347
|
+
prompt != null && prompt.length > 0 ? "" : void 0,
|
1348
|
+
// add a newline if prompt is not null
|
1349
1349
|
schemaPrefix,
|
1350
|
-
JSON.stringify(schema),
|
1350
|
+
schema != null ? JSON.stringify(schema) : void 0,
|
1351
1351
|
schemaSuffix
|
1352
1352
|
].filter((line) => line != null).join("\n");
|
1353
1353
|
}
|
@@ -1387,6 +1387,238 @@ var NoObjectGeneratedError = class extends AISDKError6 {
|
|
1387
1387
|
};
|
1388
1388
|
_a6 = symbol6;
|
1389
1389
|
|
1390
|
+
// core/generate-object/output-strategy.ts
|
1391
|
+
import {
|
1392
|
+
isJSONArray,
|
1393
|
+
isJSONObject,
|
1394
|
+
TypeValidationError,
|
1395
|
+
UnsupportedFunctionalityError
|
1396
|
+
} from "@ai-sdk/provider";
|
1397
|
+
import { safeValidateTypes as safeValidateTypes2 } from "@ai-sdk/provider-utils";
|
1398
|
+
import { asSchema } from "@ai-sdk/ui-utils";
|
1399
|
+
|
1400
|
+
// core/util/async-iterable-stream.ts
|
1401
|
+
function createAsyncIterableStream(source, transformer) {
|
1402
|
+
const transformedStream = source.pipeThrough(
|
1403
|
+
new TransformStream(transformer)
|
1404
|
+
);
|
1405
|
+
transformedStream[Symbol.asyncIterator] = () => {
|
1406
|
+
const reader = transformedStream.getReader();
|
1407
|
+
return {
|
1408
|
+
async next() {
|
1409
|
+
const { done, value } = await reader.read();
|
1410
|
+
return done ? { done: true, value: void 0 } : { done: false, value };
|
1411
|
+
}
|
1412
|
+
};
|
1413
|
+
};
|
1414
|
+
return transformedStream;
|
1415
|
+
}
|
1416
|
+
|
1417
|
+
// core/generate-object/output-strategy.ts
|
1418
|
+
var noSchemaOutputStrategy = {
|
1419
|
+
type: "no-schema",
|
1420
|
+
jsonSchema: void 0,
|
1421
|
+
validatePartialResult({ value }) {
|
1422
|
+
return { success: true, value };
|
1423
|
+
},
|
1424
|
+
validateFinalResult(value) {
|
1425
|
+
return value === void 0 ? { success: false, error: new NoObjectGeneratedError() } : { success: true, value };
|
1426
|
+
},
|
1427
|
+
createElementStream() {
|
1428
|
+
throw new UnsupportedFunctionalityError({
|
1429
|
+
functionality: "element streams in no-schema mode"
|
1430
|
+
});
|
1431
|
+
}
|
1432
|
+
};
|
1433
|
+
var objectOutputStrategy = (schema) => ({
|
1434
|
+
type: "object",
|
1435
|
+
jsonSchema: schema.jsonSchema,
|
1436
|
+
validatePartialResult({ value }) {
|
1437
|
+
return { success: true, value };
|
1438
|
+
},
|
1439
|
+
validateFinalResult(value) {
|
1440
|
+
return safeValidateTypes2({ value, schema });
|
1441
|
+
},
|
1442
|
+
createElementStream() {
|
1443
|
+
throw new UnsupportedFunctionalityError({
|
1444
|
+
functionality: "element streams in object mode"
|
1445
|
+
});
|
1446
|
+
}
|
1447
|
+
});
|
1448
|
+
var arrayOutputStrategy = (schema) => {
|
1449
|
+
const { $schema, ...itemSchema } = schema.jsonSchema;
|
1450
|
+
return {
|
1451
|
+
type: "object",
|
1452
|
+
// wrap in object that contains array of elements, since most LLMs will not
|
1453
|
+
// be able to generate an array directly:
|
1454
|
+
// possible future optimization: use arrays directly when model supports grammar-guided generation
|
1455
|
+
jsonSchema: {
|
1456
|
+
$schema: "http://json-schema.org/draft-07/schema#",
|
1457
|
+
type: "object",
|
1458
|
+
properties: {
|
1459
|
+
elements: { type: "array", items: itemSchema }
|
1460
|
+
},
|
1461
|
+
required: ["elements"],
|
1462
|
+
additionalProperties: false
|
1463
|
+
},
|
1464
|
+
validatePartialResult({
|
1465
|
+
value,
|
1466
|
+
parseState
|
1467
|
+
}) {
|
1468
|
+
if (!isJSONObject(value) || !isJSONArray(value.elements)) {
|
1469
|
+
return {
|
1470
|
+
success: false,
|
1471
|
+
error: new TypeValidationError({
|
1472
|
+
value,
|
1473
|
+
cause: "value must be an object that contains an array of elements"
|
1474
|
+
})
|
1475
|
+
};
|
1476
|
+
}
|
1477
|
+
const inputArray = value.elements;
|
1478
|
+
const resultArray = [];
|
1479
|
+
for (let i = 0; i < inputArray.length; i++) {
|
1480
|
+
const element = inputArray[i];
|
1481
|
+
const result = safeValidateTypes2({ value: element, schema });
|
1482
|
+
if (i === inputArray.length - 1 && (!result.success || parseState !== "successful-parse")) {
|
1483
|
+
continue;
|
1484
|
+
}
|
1485
|
+
if (!result.success) {
|
1486
|
+
return result;
|
1487
|
+
}
|
1488
|
+
resultArray.push(result.value);
|
1489
|
+
}
|
1490
|
+
return { success: true, value: resultArray };
|
1491
|
+
},
|
1492
|
+
validateFinalResult(value) {
|
1493
|
+
if (!isJSONObject(value) || !isJSONArray(value.elements)) {
|
1494
|
+
return {
|
1495
|
+
success: false,
|
1496
|
+
error: new TypeValidationError({
|
1497
|
+
value,
|
1498
|
+
cause: "value must be an object that contains an array of elements"
|
1499
|
+
})
|
1500
|
+
};
|
1501
|
+
}
|
1502
|
+
const inputArray = value.elements;
|
1503
|
+
for (const element of inputArray) {
|
1504
|
+
const result = safeValidateTypes2({ value: element, schema });
|
1505
|
+
if (!result.success) {
|
1506
|
+
return result;
|
1507
|
+
}
|
1508
|
+
}
|
1509
|
+
return { success: true, value: inputArray };
|
1510
|
+
},
|
1511
|
+
createElementStream(originalStream) {
|
1512
|
+
let publishedElements = 0;
|
1513
|
+
return createAsyncIterableStream(originalStream, {
|
1514
|
+
transform(chunk, controller) {
|
1515
|
+
switch (chunk.type) {
|
1516
|
+
case "object": {
|
1517
|
+
const array = chunk.object;
|
1518
|
+
for (; publishedElements < array.length; publishedElements++) {
|
1519
|
+
controller.enqueue(array[publishedElements]);
|
1520
|
+
}
|
1521
|
+
break;
|
1522
|
+
}
|
1523
|
+
case "text-delta":
|
1524
|
+
case "finish":
|
1525
|
+
break;
|
1526
|
+
case "error":
|
1527
|
+
controller.error(chunk.error);
|
1528
|
+
break;
|
1529
|
+
default: {
|
1530
|
+
const _exhaustiveCheck = chunk;
|
1531
|
+
throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
|
1532
|
+
}
|
1533
|
+
}
|
1534
|
+
}
|
1535
|
+
});
|
1536
|
+
}
|
1537
|
+
};
|
1538
|
+
};
|
1539
|
+
function getOutputStrategy({
|
1540
|
+
output,
|
1541
|
+
schema
|
1542
|
+
}) {
|
1543
|
+
switch (output) {
|
1544
|
+
case "object":
|
1545
|
+
return objectOutputStrategy(asSchema(schema));
|
1546
|
+
case "array":
|
1547
|
+
return arrayOutputStrategy(asSchema(schema));
|
1548
|
+
case "no-schema":
|
1549
|
+
return noSchemaOutputStrategy;
|
1550
|
+
default: {
|
1551
|
+
const _exhaustiveCheck = output;
|
1552
|
+
throw new Error(`Unsupported output: ${_exhaustiveCheck}`);
|
1553
|
+
}
|
1554
|
+
}
|
1555
|
+
}
|
1556
|
+
|
1557
|
+
// core/generate-object/validate-object-generation-input.ts
|
1558
|
+
function validateObjectGenerationInput({
|
1559
|
+
output,
|
1560
|
+
mode,
|
1561
|
+
schema,
|
1562
|
+
schemaName,
|
1563
|
+
schemaDescription
|
1564
|
+
}) {
|
1565
|
+
if (output != null && output !== "object" && output !== "array" && output !== "no-schema") {
|
1566
|
+
throw new InvalidArgumentError({
|
1567
|
+
parameter: "output",
|
1568
|
+
value: output,
|
1569
|
+
message: "Invalid output type."
|
1570
|
+
});
|
1571
|
+
}
|
1572
|
+
if (output === "no-schema") {
|
1573
|
+
if (mode === "auto" || mode === "tool") {
|
1574
|
+
throw new InvalidArgumentError({
|
1575
|
+
parameter: "mode",
|
1576
|
+
value: mode,
|
1577
|
+
message: 'Mode must be "json" for no-schema output.'
|
1578
|
+
});
|
1579
|
+
}
|
1580
|
+
if (schema != null) {
|
1581
|
+
throw new InvalidArgumentError({
|
1582
|
+
parameter: "schema",
|
1583
|
+
value: schema,
|
1584
|
+
message: "Schema is not supported for no-schema output."
|
1585
|
+
});
|
1586
|
+
}
|
1587
|
+
if (schemaDescription != null) {
|
1588
|
+
throw new InvalidArgumentError({
|
1589
|
+
parameter: "schemaDescription",
|
1590
|
+
value: schemaDescription,
|
1591
|
+
message: "Schema description is not supported for no-schema output."
|
1592
|
+
});
|
1593
|
+
}
|
1594
|
+
if (schemaName != null) {
|
1595
|
+
throw new InvalidArgumentError({
|
1596
|
+
parameter: "schemaName",
|
1597
|
+
value: schemaName,
|
1598
|
+
message: "Schema name is not supported for no-schema output."
|
1599
|
+
});
|
1600
|
+
}
|
1601
|
+
}
|
1602
|
+
if (output === "object") {
|
1603
|
+
if (schema == null) {
|
1604
|
+
throw new InvalidArgumentError({
|
1605
|
+
parameter: "schema",
|
1606
|
+
value: schema,
|
1607
|
+
message: "Schema is required for object output."
|
1608
|
+
});
|
1609
|
+
}
|
1610
|
+
}
|
1611
|
+
if (output === "array") {
|
1612
|
+
if (schema == null) {
|
1613
|
+
throw new InvalidArgumentError({
|
1614
|
+
parameter: "schema",
|
1615
|
+
value: schema,
|
1616
|
+
message: "Element schema is required for array output."
|
1617
|
+
});
|
1618
|
+
}
|
1619
|
+
}
|
1620
|
+
}
|
1621
|
+
|
1390
1622
|
// core/generate-object/generate-object.ts
|
1391
1623
|
async function generateObject({
|
1392
1624
|
model,
|
@@ -1394,6 +1626,7 @@ async function generateObject({
|
|
1394
1626
|
schemaName,
|
1395
1627
|
schemaDescription,
|
1396
1628
|
mode,
|
1629
|
+
output = "object",
|
1397
1630
|
system,
|
1398
1631
|
prompt,
|
1399
1632
|
messages,
|
@@ -1404,13 +1637,23 @@ async function generateObject({
|
|
1404
1637
|
...settings
|
1405
1638
|
}) {
|
1406
1639
|
var _a12;
|
1640
|
+
validateObjectGenerationInput({
|
1641
|
+
output,
|
1642
|
+
mode,
|
1643
|
+
schema: inputSchema,
|
1644
|
+
schemaName,
|
1645
|
+
schemaDescription
|
1646
|
+
});
|
1647
|
+
const outputStrategy = getOutputStrategy({ output, schema: inputSchema });
|
1648
|
+
if (outputStrategy.type === "no-schema" && mode === void 0) {
|
1649
|
+
mode = "json";
|
1650
|
+
}
|
1407
1651
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
1408
1652
|
model,
|
1409
1653
|
telemetry,
|
1410
1654
|
headers,
|
1411
1655
|
settings: { ...settings, maxRetries }
|
1412
1656
|
});
|
1413
|
-
const schema = asSchema(inputSchema);
|
1414
1657
|
const tracer = getTracer({ isEnabled: (_a12 = telemetry == null ? void 0 : telemetry.isEnabled) != null ? _a12 : false });
|
1415
1658
|
return recordSpan({
|
1416
1659
|
name: "ai.generateObject",
|
@@ -1426,11 +1669,10 @@ async function generateObject({
|
|
1426
1669
|
"ai.prompt": {
|
1427
1670
|
input: () => JSON.stringify({ system, prompt, messages })
|
1428
1671
|
},
|
1429
|
-
"ai.schema": {
|
1430
|
-
input: () => JSON.stringify(schema.jsonSchema)
|
1431
|
-
},
|
1672
|
+
"ai.schema": outputStrategy.jsonSchema != null ? { input: () => JSON.stringify(outputStrategy.jsonSchema) } : void 0,
|
1432
1673
|
"ai.schema.name": schemaName,
|
1433
1674
|
"ai.schema.description": schemaDescription,
|
1675
|
+
"ai.settings.output": outputStrategy.type,
|
1434
1676
|
"ai.settings.mode": mode
|
1435
1677
|
}
|
1436
1678
|
}),
|
@@ -1450,9 +1692,9 @@ async function generateObject({
|
|
1450
1692
|
switch (mode) {
|
1451
1693
|
case "json": {
|
1452
1694
|
const validatedPrompt = validatePrompt({
|
1453
|
-
system: model.supportsStructuredOutputs ? system :
|
1454
|
-
system,
|
1455
|
-
schema:
|
1695
|
+
system: outputStrategy.jsonSchema == null ? injectJsonInstruction({ prompt: system }) : model.supportsStructuredOutputs ? system : injectJsonInstruction({
|
1696
|
+
prompt: system,
|
1697
|
+
schema: outputStrategy.jsonSchema
|
1456
1698
|
}),
|
1457
1699
|
prompt,
|
1458
1700
|
messages
|
@@ -1493,7 +1735,7 @@ async function generateObject({
|
|
1493
1735
|
const result2 = await model.doGenerate({
|
1494
1736
|
mode: {
|
1495
1737
|
type: "object-json",
|
1496
|
-
schema:
|
1738
|
+
schema: outputStrategy.jsonSchema,
|
1497
1739
|
name: schemaName,
|
1498
1740
|
description: schemaDescription
|
1499
1741
|
},
|
@@ -1581,7 +1823,7 @@ async function generateObject({
|
|
1581
1823
|
type: "function",
|
1582
1824
|
name: schemaName != null ? schemaName : "json",
|
1583
1825
|
description: schemaDescription != null ? schemaDescription : "Respond with a JSON object.",
|
1584
|
-
parameters:
|
1826
|
+
parameters: outputStrategy.jsonSchema
|
1585
1827
|
}
|
1586
1828
|
},
|
1587
1829
|
...prepareCallSettings(settings),
|
@@ -1632,10 +1874,16 @@ async function generateObject({
|
|
1632
1874
|
throw new Error(`Unsupported mode: ${_exhaustiveCheck}`);
|
1633
1875
|
}
|
1634
1876
|
}
|
1635
|
-
const parseResult = safeParseJSON({ text: result
|
1877
|
+
const parseResult = safeParseJSON({ text: result });
|
1636
1878
|
if (!parseResult.success) {
|
1637
1879
|
throw parseResult.error;
|
1638
1880
|
}
|
1881
|
+
const validationResult = outputStrategy.validateFinalResult(
|
1882
|
+
parseResult.value
|
1883
|
+
);
|
1884
|
+
if (!validationResult.success) {
|
1885
|
+
throw validationResult.error;
|
1886
|
+
}
|
1639
1887
|
span.setAttributes(
|
1640
1888
|
selectTelemetryAttributes({
|
1641
1889
|
telemetry,
|
@@ -1644,13 +1892,13 @@ async function generateObject({
|
|
1644
1892
|
"ai.usage.promptTokens": usage.promptTokens,
|
1645
1893
|
"ai.usage.completionTokens": usage.completionTokens,
|
1646
1894
|
"ai.result.object": {
|
1647
|
-
output: () => JSON.stringify(
|
1895
|
+
output: () => JSON.stringify(validationResult.value)
|
1648
1896
|
}
|
1649
1897
|
}
|
1650
1898
|
})
|
1651
1899
|
);
|
1652
1900
|
return new DefaultGenerateObjectResult({
|
1653
|
-
object:
|
1901
|
+
object: validationResult.value,
|
1654
1902
|
finishReason,
|
1655
1903
|
usage: calculateCompletionTokenUsage(usage),
|
1656
1904
|
warnings,
|
@@ -1684,9 +1932,7 @@ var DefaultGenerateObjectResult = class {
|
|
1684
1932
|
var experimental_generateObject = generateObject;
|
1685
1933
|
|
1686
1934
|
// core/generate-object/stream-object.ts
|
1687
|
-
import { safeValidateTypes as safeValidateTypes2 } from "@ai-sdk/provider-utils";
|
1688
1935
|
import {
|
1689
|
-
asSchema as asSchema2,
|
1690
1936
|
isDeepEqualData,
|
1691
1937
|
parsePartialJson
|
1692
1938
|
} from "@ai-sdk/ui-utils";
|
@@ -1744,23 +1990,6 @@ var DelayedPromise = class {
|
|
1744
1990
|
}
|
1745
1991
|
};
|
1746
1992
|
|
1747
|
-
// core/util/async-iterable-stream.ts
|
1748
|
-
function createAsyncIterableStream(source, transformer) {
|
1749
|
-
const transformedStream = source.pipeThrough(
|
1750
|
-
new TransformStream(transformer)
|
1751
|
-
);
|
1752
|
-
transformedStream[Symbol.asyncIterator] = () => {
|
1753
|
-
const reader = transformedStream.getReader();
|
1754
|
-
return {
|
1755
|
-
async next() {
|
1756
|
-
const { done, value } = await reader.read();
|
1757
|
-
return done ? { done: true, value: void 0 } : { done: false, value };
|
1758
|
-
}
|
1759
|
-
};
|
1760
|
-
};
|
1761
|
-
return transformedStream;
|
1762
|
-
}
|
1763
|
-
|
1764
1993
|
// core/generate-object/stream-object.ts
|
1765
1994
|
async function streamObject({
|
1766
1995
|
model,
|
@@ -1768,6 +1997,7 @@ async function streamObject({
|
|
1768
1997
|
schemaName,
|
1769
1998
|
schemaDescription,
|
1770
1999
|
mode,
|
2000
|
+
output = "object",
|
1771
2001
|
system,
|
1772
2002
|
prompt,
|
1773
2003
|
messages,
|
@@ -1779,6 +2009,17 @@ async function streamObject({
|
|
1779
2009
|
...settings
|
1780
2010
|
}) {
|
1781
2011
|
var _a12;
|
2012
|
+
validateObjectGenerationInput({
|
2013
|
+
output,
|
2014
|
+
mode,
|
2015
|
+
schema: inputSchema,
|
2016
|
+
schemaName,
|
2017
|
+
schemaDescription
|
2018
|
+
});
|
2019
|
+
const outputStrategy = getOutputStrategy({ output, schema: inputSchema });
|
2020
|
+
if (outputStrategy.type === "no-schema" && mode === void 0) {
|
2021
|
+
mode = "json";
|
2022
|
+
}
|
1782
2023
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
1783
2024
|
model,
|
1784
2025
|
telemetry,
|
@@ -1787,7 +2028,6 @@ async function streamObject({
|
|
1787
2028
|
});
|
1788
2029
|
const tracer = getTracer({ isEnabled: (_a12 = telemetry == null ? void 0 : telemetry.isEnabled) != null ? _a12 : false });
|
1789
2030
|
const retry = retryWithExponentialBackoff({ maxRetries });
|
1790
|
-
const schema = asSchema2(inputSchema);
|
1791
2031
|
return recordSpan({
|
1792
2032
|
name: "ai.streamObject",
|
1793
2033
|
attributes: selectTelemetryAttributes({
|
@@ -1802,9 +2042,10 @@ async function streamObject({
|
|
1802
2042
|
"ai.prompt": {
|
1803
2043
|
input: () => JSON.stringify({ system, prompt, messages })
|
1804
2044
|
},
|
1805
|
-
"ai.schema": { input: () => JSON.stringify(
|
2045
|
+
"ai.schema": outputStrategy.jsonSchema != null ? { input: () => JSON.stringify(outputStrategy.jsonSchema) } : void 0,
|
1806
2046
|
"ai.schema.name": schemaName,
|
1807
2047
|
"ai.schema.description": schemaDescription,
|
2048
|
+
"ai.settings.output": outputStrategy.type,
|
1808
2049
|
"ai.settings.mode": mode
|
1809
2050
|
}
|
1810
2051
|
}),
|
@@ -1819,9 +2060,9 @@ async function streamObject({
|
|
1819
2060
|
switch (mode) {
|
1820
2061
|
case "json": {
|
1821
2062
|
const validatedPrompt = validatePrompt({
|
1822
|
-
system: model.supportsStructuredOutputs ? system :
|
1823
|
-
system,
|
1824
|
-
schema:
|
2063
|
+
system: outputStrategy.jsonSchema == null ? injectJsonInstruction({ prompt: system }) : model.supportsStructuredOutputs ? system : injectJsonInstruction({
|
2064
|
+
prompt: system,
|
2065
|
+
schema: outputStrategy.jsonSchema
|
1825
2066
|
}),
|
1826
2067
|
prompt,
|
1827
2068
|
messages
|
@@ -1829,7 +2070,7 @@ async function streamObject({
|
|
1829
2070
|
callOptions = {
|
1830
2071
|
mode: {
|
1831
2072
|
type: "object-json",
|
1832
|
-
schema:
|
2073
|
+
schema: outputStrategy.jsonSchema,
|
1833
2074
|
name: schemaName,
|
1834
2075
|
description: schemaDescription
|
1835
2076
|
},
|
@@ -1870,7 +2111,7 @@ async function streamObject({
|
|
1870
2111
|
type: "function",
|
1871
2112
|
name: schemaName != null ? schemaName : "json",
|
1872
2113
|
description: schemaDescription != null ? schemaDescription : "Respond with a JSON object.",
|
1873
|
-
parameters:
|
2114
|
+
parameters: outputStrategy.jsonSchema
|
1874
2115
|
}
|
1875
2116
|
},
|
1876
2117
|
...prepareCallSettings(settings),
|
@@ -1948,10 +2189,10 @@ async function streamObject({
|
|
1948
2189
|
})
|
1949
2190
|
);
|
1950
2191
|
return new DefaultStreamObjectResult({
|
2192
|
+
outputStrategy,
|
1951
2193
|
stream: stream.pipeThrough(new TransformStream(transformer)),
|
1952
2194
|
warnings,
|
1953
2195
|
rawResponse,
|
1954
|
-
schema,
|
1955
2196
|
onFinish,
|
1956
2197
|
rootSpan,
|
1957
2198
|
doStreamSpan,
|
@@ -1966,7 +2207,7 @@ var DefaultStreamObjectResult = class {
|
|
1966
2207
|
stream,
|
1967
2208
|
warnings,
|
1968
2209
|
rawResponse,
|
1969
|
-
|
2210
|
+
outputStrategy,
|
1970
2211
|
onFinish,
|
1971
2212
|
rootSpan,
|
1972
2213
|
doStreamSpan,
|
@@ -1975,6 +2216,7 @@ var DefaultStreamObjectResult = class {
|
|
1975
2216
|
}) {
|
1976
2217
|
this.warnings = warnings;
|
1977
2218
|
this.rawResponse = rawResponse;
|
2219
|
+
this.outputStrategy = outputStrategy;
|
1978
2220
|
this.objectPromise = new DelayedPromise();
|
1979
2221
|
const { resolve: resolveUsage, promise: usagePromise } = createResolvablePromise();
|
1980
2222
|
this.usage = usagePromise;
|
@@ -1990,6 +2232,7 @@ var DefaultStreamObjectResult = class {
|
|
1990
2232
|
let error;
|
1991
2233
|
let accumulatedText = "";
|
1992
2234
|
let delta = "";
|
2235
|
+
let latestObjectJson = void 0;
|
1993
2236
|
let latestObject = void 0;
|
1994
2237
|
let firstChunk = true;
|
1995
2238
|
const self = this;
|
@@ -2009,20 +2252,25 @@ var DefaultStreamObjectResult = class {
|
|
2009
2252
|
if (typeof chunk === "string") {
|
2010
2253
|
accumulatedText += chunk;
|
2011
2254
|
delta += chunk;
|
2012
|
-
const
|
2013
|
-
|
2014
|
-
|
2015
|
-
|
2016
|
-
|
2017
|
-
controller.enqueue({
|
2018
|
-
type: "object",
|
2019
|
-
object: currentObject
|
2255
|
+
const { value: currentObjectJson, state: parseState } = parsePartialJson(accumulatedText);
|
2256
|
+
if (currentObjectJson !== void 0 && !isDeepEqualData(latestObjectJson, currentObjectJson)) {
|
2257
|
+
const validationResult = outputStrategy.validatePartialResult({
|
2258
|
+
value: currentObjectJson,
|
2259
|
+
parseState
|
2020
2260
|
});
|
2021
|
-
|
2022
|
-
|
2023
|
-
|
2024
|
-
|
2025
|
-
|
2261
|
+
if (validationResult.success && !isDeepEqualData(latestObject, validationResult.value)) {
|
2262
|
+
latestObjectJson = currentObjectJson;
|
2263
|
+
latestObject = validationResult.value;
|
2264
|
+
controller.enqueue({
|
2265
|
+
type: "object",
|
2266
|
+
object: latestObject
|
2267
|
+
});
|
2268
|
+
controller.enqueue({
|
2269
|
+
type: "text-delta",
|
2270
|
+
textDelta: delta
|
2271
|
+
});
|
2272
|
+
delta = "";
|
2273
|
+
}
|
2026
2274
|
}
|
2027
2275
|
return;
|
2028
2276
|
}
|
@@ -2040,10 +2288,7 @@ var DefaultStreamObjectResult = class {
|
|
2040
2288
|
controller.enqueue({ ...chunk, usage });
|
2041
2289
|
resolveUsage(usage);
|
2042
2290
|
resolveProviderMetadata(providerMetadata);
|
2043
|
-
const validationResult =
|
2044
|
-
value: latestObject,
|
2045
|
-
schema
|
2046
|
-
});
|
2291
|
+
const validationResult = outputStrategy.validateFinalResult(latestObjectJson);
|
2047
2292
|
if (validationResult.success) {
|
2048
2293
|
object = validationResult.value;
|
2049
2294
|
self.objectPromise.resolve(object);
|
@@ -2138,6 +2383,9 @@ var DefaultStreamObjectResult = class {
|
|
2138
2383
|
}
|
2139
2384
|
});
|
2140
2385
|
}
|
2386
|
+
get elementStream() {
|
2387
|
+
return this.outputStrategy.createElementStream(this.originalStream);
|
2388
|
+
}
|
2141
2389
|
get textStream() {
|
2142
2390
|
return createAsyncIterableStream(this.originalStream, {
|
2143
2391
|
transform(chunk, controller) {
|
@@ -2202,7 +2450,7 @@ var DefaultStreamObjectResult = class {
|
|
2202
2450
|
var experimental_streamObject = streamObject;
|
2203
2451
|
|
2204
2452
|
// core/prompt/prepare-tools-and-tool-choice.ts
|
2205
|
-
import { asSchema as
|
2453
|
+
import { asSchema as asSchema2 } from "@ai-sdk/ui-utils";
|
2206
2454
|
|
2207
2455
|
// core/util/is-non-empty-object.ts
|
2208
2456
|
function isNonEmptyObject(object) {
|
@@ -2225,7 +2473,7 @@ function prepareToolsAndToolChoice({
|
|
2225
2473
|
type: "function",
|
2226
2474
|
name: name12,
|
2227
2475
|
description: tool2.description,
|
2228
|
-
parameters:
|
2476
|
+
parameters: asSchema2(tool2.parameters).jsonSchema
|
2229
2477
|
})),
|
2230
2478
|
toolChoice: toolChoice == null ? { type: "auto" } : typeof toolChoice === "string" ? { type: toolChoice } : { type: "tool", toolName: toolChoice.toolName }
|
2231
2479
|
};
|
@@ -2233,7 +2481,7 @@ function prepareToolsAndToolChoice({
|
|
2233
2481
|
|
2234
2482
|
// core/generate-text/tool-call.ts
|
2235
2483
|
import { safeParseJSON as safeParseJSON2 } from "@ai-sdk/provider-utils";
|
2236
|
-
import { asSchema as
|
2484
|
+
import { asSchema as asSchema3 } from "@ai-sdk/ui-utils";
|
2237
2485
|
|
2238
2486
|
// errors/invalid-tool-arguments-error.ts
|
2239
2487
|
import { AISDKError as AISDKError7, getErrorMessage as getErrorMessage3 } from "@ai-sdk/provider";
|
@@ -2339,7 +2587,7 @@ function parseToolCall({
|
|
2339
2587
|
}
|
2340
2588
|
const parseResult = safeParseJSON2({
|
2341
2589
|
text: toolCall.args,
|
2342
|
-
schema:
|
2590
|
+
schema: asSchema3(tool2.parameters)
|
2343
2591
|
});
|
2344
2592
|
if (parseResult.success === false) {
|
2345
2593
|
throw new InvalidToolArgumentsError({
|
@@ -3757,8 +4005,8 @@ import {
|
|
3757
4005
|
InvalidResponseDataError,
|
3758
4006
|
JSONParseError,
|
3759
4007
|
LoadAPIKeyError,
|
3760
|
-
TypeValidationError,
|
3761
|
-
UnsupportedFunctionalityError
|
4008
|
+
TypeValidationError as TypeValidationError2,
|
4009
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
3762
4010
|
} from "@ai-sdk/provider";
|
3763
4011
|
|
3764
4012
|
// streams/ai-stream.ts
|
@@ -4851,8 +5099,8 @@ export {
|
|
4851
5099
|
RetryError,
|
4852
5100
|
StreamData2 as StreamData,
|
4853
5101
|
StreamingTextResponse,
|
4854
|
-
TypeValidationError,
|
4855
|
-
UnsupportedFunctionalityError,
|
5102
|
+
TypeValidationError2 as TypeValidationError,
|
5103
|
+
UnsupportedFunctionalityError2 as UnsupportedFunctionalityError,
|
4856
5104
|
convertToCoreMessages,
|
4857
5105
|
cosineSimilarity,
|
4858
5106
|
createCallbacksTransformer,
|