ai 3.3.22 → 3.3.23
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/CHANGELOG.md +6 -0
- package/dist/index.js +51 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +51 -26
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
@@ -1418,8 +1418,8 @@ function createAsyncIterableStream(source, transformer) {
|
|
1418
1418
|
var noSchemaOutputStrategy = {
|
1419
1419
|
type: "no-schema",
|
1420
1420
|
jsonSchema: void 0,
|
1421
|
-
validatePartialResult({ value }) {
|
1422
|
-
return { success: true, value };
|
1421
|
+
validatePartialResult({ value, textDelta }) {
|
1422
|
+
return { success: true, value: { partial: value, textDelta } };
|
1423
1423
|
},
|
1424
1424
|
validateFinalResult(value) {
|
1425
1425
|
return value === void 0 ? { success: false, error: new NoObjectGeneratedError() } : { success: true, value };
|
@@ -1433,8 +1433,15 @@ var noSchemaOutputStrategy = {
|
|
1433
1433
|
var objectOutputStrategy = (schema) => ({
|
1434
1434
|
type: "object",
|
1435
1435
|
jsonSchema: schema.jsonSchema,
|
1436
|
-
validatePartialResult({ value }) {
|
1437
|
-
return {
|
1436
|
+
validatePartialResult({ value, textDelta }) {
|
1437
|
+
return {
|
1438
|
+
success: true,
|
1439
|
+
value: {
|
1440
|
+
// Note: currently no validation of partial results:
|
1441
|
+
partial: value,
|
1442
|
+
textDelta
|
1443
|
+
}
|
1444
|
+
};
|
1438
1445
|
},
|
1439
1446
|
validateFinalResult(value) {
|
1440
1447
|
return safeValidateTypes2({ value, schema });
|
@@ -1448,7 +1455,7 @@ var objectOutputStrategy = (schema) => ({
|
|
1448
1455
|
var arrayOutputStrategy = (schema) => {
|
1449
1456
|
const { $schema, ...itemSchema } = schema.jsonSchema;
|
1450
1457
|
return {
|
1451
|
-
type: "
|
1458
|
+
type: "array",
|
1452
1459
|
// wrap in object that contains array of elements, since most LLMs will not
|
1453
1460
|
// be able to generate an array directly:
|
1454
1461
|
// possible future optimization: use arrays directly when model supports grammar-guided generation
|
@@ -1461,10 +1468,8 @@ var arrayOutputStrategy = (schema) => {
|
|
1461
1468
|
required: ["elements"],
|
1462
1469
|
additionalProperties: false
|
1463
1470
|
},
|
1464
|
-
validatePartialResult({
|
1465
|
-
|
1466
|
-
parseState
|
1467
|
-
}) {
|
1471
|
+
validatePartialResult({ value, latestObject, isFirstDelta, isFinalDelta }) {
|
1472
|
+
var _a11;
|
1468
1473
|
if (!isJSONObject(value) || !isJSONArray(value.elements)) {
|
1469
1474
|
return {
|
1470
1475
|
success: false,
|
@@ -1479,7 +1484,7 @@ var arrayOutputStrategy = (schema) => {
|
|
1479
1484
|
for (let i = 0; i < inputArray.length; i++) {
|
1480
1485
|
const element = inputArray[i];
|
1481
1486
|
const result = safeValidateTypes2({ value: element, schema });
|
1482
|
-
if (i === inputArray.length - 1 &&
|
1487
|
+
if (i === inputArray.length - 1 && !isFinalDelta) {
|
1483
1488
|
continue;
|
1484
1489
|
}
|
1485
1490
|
if (!result.success) {
|
@@ -1487,7 +1492,25 @@ var arrayOutputStrategy = (schema) => {
|
|
1487
1492
|
}
|
1488
1493
|
resultArray.push(result.value);
|
1489
1494
|
}
|
1490
|
-
|
1495
|
+
const publishedElementCount = (_a11 = latestObject == null ? void 0 : latestObject.length) != null ? _a11 : 0;
|
1496
|
+
let textDelta = "";
|
1497
|
+
if (isFirstDelta) {
|
1498
|
+
textDelta += "[";
|
1499
|
+
}
|
1500
|
+
if (publishedElementCount > 0) {
|
1501
|
+
textDelta += ",";
|
1502
|
+
}
|
1503
|
+
textDelta += resultArray.slice(publishedElementCount).map((element) => JSON.stringify(element)).join(",");
|
1504
|
+
if (isFinalDelta) {
|
1505
|
+
textDelta += "]";
|
1506
|
+
}
|
1507
|
+
return {
|
1508
|
+
success: true,
|
1509
|
+
value: {
|
1510
|
+
partial: resultArray,
|
1511
|
+
textDelta
|
1512
|
+
}
|
1513
|
+
};
|
1491
1514
|
},
|
1492
1515
|
validateFinalResult(value) {
|
1493
1516
|
if (!isJSONObject(value) || !isJSONArray(value.elements)) {
|
@@ -2231,17 +2254,18 @@ var DefaultStreamObjectResult = class {
|
|
2231
2254
|
let object;
|
2232
2255
|
let error;
|
2233
2256
|
let accumulatedText = "";
|
2234
|
-
let
|
2257
|
+
let textDelta = "";
|
2235
2258
|
let latestObjectJson = void 0;
|
2236
2259
|
let latestObject = void 0;
|
2237
|
-
let
|
2260
|
+
let isFirstChunk = true;
|
2261
|
+
let isFirstDelta = true;
|
2238
2262
|
const self = this;
|
2239
2263
|
this.originalStream = stream.pipeThrough(
|
2240
2264
|
new TransformStream({
|
2241
2265
|
async transform(chunk, controller) {
|
2242
|
-
if (
|
2266
|
+
if (isFirstChunk) {
|
2243
2267
|
const msToFirstChunk = performance.now() - startTimestamp;
|
2244
|
-
|
2268
|
+
isFirstChunk = false;
|
2245
2269
|
doStreamSpan.addEvent("ai.stream.firstChunk", {
|
2246
2270
|
"ai.stream.msToFirstChunk": msToFirstChunk
|
2247
2271
|
});
|
@@ -2251,36 +2275,37 @@ var DefaultStreamObjectResult = class {
|
|
2251
2275
|
}
|
2252
2276
|
if (typeof chunk === "string") {
|
2253
2277
|
accumulatedText += chunk;
|
2254
|
-
|
2278
|
+
textDelta += chunk;
|
2255
2279
|
const { value: currentObjectJson, state: parseState } = parsePartialJson(accumulatedText);
|
2256
2280
|
if (currentObjectJson !== void 0 && !isDeepEqualData(latestObjectJson, currentObjectJson)) {
|
2257
2281
|
const validationResult = outputStrategy.validatePartialResult({
|
2258
2282
|
value: currentObjectJson,
|
2259
|
-
|
2283
|
+
textDelta,
|
2284
|
+
latestObject,
|
2285
|
+
isFirstDelta,
|
2286
|
+
isFinalDelta: parseState === "successful-parse"
|
2260
2287
|
});
|
2261
|
-
if (validationResult.success && !isDeepEqualData(latestObject, validationResult.value)) {
|
2288
|
+
if (validationResult.success && !isDeepEqualData(latestObject, validationResult.value.partial)) {
|
2262
2289
|
latestObjectJson = currentObjectJson;
|
2263
|
-
latestObject = validationResult.value;
|
2290
|
+
latestObject = validationResult.value.partial;
|
2264
2291
|
controller.enqueue({
|
2265
2292
|
type: "object",
|
2266
2293
|
object: latestObject
|
2267
2294
|
});
|
2268
2295
|
controller.enqueue({
|
2269
2296
|
type: "text-delta",
|
2270
|
-
textDelta:
|
2297
|
+
textDelta: validationResult.value.textDelta
|
2271
2298
|
});
|
2272
|
-
|
2299
|
+
textDelta = "";
|
2300
|
+
isFirstDelta = false;
|
2273
2301
|
}
|
2274
2302
|
}
|
2275
2303
|
return;
|
2276
2304
|
}
|
2277
2305
|
switch (chunk.type) {
|
2278
2306
|
case "finish": {
|
2279
|
-
if (
|
2280
|
-
controller.enqueue({
|
2281
|
-
type: "text-delta",
|
2282
|
-
textDelta: delta
|
2283
|
-
});
|
2307
|
+
if (textDelta !== "") {
|
2308
|
+
controller.enqueue({ type: "text-delta", textDelta });
|
2284
2309
|
}
|
2285
2310
|
finishReason = chunk.finishReason;
|
2286
2311
|
usage = calculateCompletionTokenUsage(chunk.usage);
|