ai 3.3.22 → 3.3.24
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 +12 -0
- package/dist/index.js +77 -43
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +77 -43
- 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)) {
|
@@ -1990,6 +2013,12 @@ var DelayedPromise = class {
|
|
1990
2013
|
}
|
1991
2014
|
};
|
1992
2015
|
|
2016
|
+
// core/util/now.ts
|
2017
|
+
function now() {
|
2018
|
+
var _a11, _b;
|
2019
|
+
return (_b = (_a11 = globalThis == null ? void 0 : globalThis.performance) == null ? void 0 : _a11.now()) != null ? _b : Date.now();
|
2020
|
+
}
|
2021
|
+
|
1993
2022
|
// core/generate-object/stream-object.ts
|
1994
2023
|
async function streamObject({
|
1995
2024
|
model,
|
@@ -2151,7 +2180,7 @@ async function streamObject({
|
|
2151
2180
|
const {
|
2152
2181
|
result: { stream, warnings, rawResponse },
|
2153
2182
|
doStreamSpan,
|
2154
|
-
|
2183
|
+
startTimestampMs
|
2155
2184
|
} = await retry(
|
2156
2185
|
() => recordSpan({
|
2157
2186
|
name: "ai.streamObject.doStream",
|
@@ -2181,8 +2210,7 @@ async function streamObject({
|
|
2181
2210
|
tracer,
|
2182
2211
|
endWhenDone: false,
|
2183
2212
|
fn: async (doStreamSpan2) => ({
|
2184
|
-
|
2185
|
-
// get before the call
|
2213
|
+
startTimestampMs: now(),
|
2186
2214
|
doStreamSpan: doStreamSpan2,
|
2187
2215
|
result: await model.doStream(callOptions)
|
2188
2216
|
})
|
@@ -2197,7 +2225,7 @@ async function streamObject({
|
|
2197
2225
|
rootSpan,
|
2198
2226
|
doStreamSpan,
|
2199
2227
|
telemetry,
|
2200
|
-
|
2228
|
+
startTimestampMs
|
2201
2229
|
});
|
2202
2230
|
}
|
2203
2231
|
});
|
@@ -2212,7 +2240,7 @@ var DefaultStreamObjectResult = class {
|
|
2212
2240
|
rootSpan,
|
2213
2241
|
doStreamSpan,
|
2214
2242
|
telemetry,
|
2215
|
-
|
2243
|
+
startTimestampMs
|
2216
2244
|
}) {
|
2217
2245
|
this.warnings = warnings;
|
2218
2246
|
this.rawResponse = rawResponse;
|
@@ -2231,17 +2259,18 @@ var DefaultStreamObjectResult = class {
|
|
2231
2259
|
let object;
|
2232
2260
|
let error;
|
2233
2261
|
let accumulatedText = "";
|
2234
|
-
let
|
2262
|
+
let textDelta = "";
|
2235
2263
|
let latestObjectJson = void 0;
|
2236
2264
|
let latestObject = void 0;
|
2237
|
-
let
|
2265
|
+
let isFirstChunk = true;
|
2266
|
+
let isFirstDelta = true;
|
2238
2267
|
const self = this;
|
2239
2268
|
this.originalStream = stream.pipeThrough(
|
2240
2269
|
new TransformStream({
|
2241
2270
|
async transform(chunk, controller) {
|
2242
|
-
if (
|
2243
|
-
const msToFirstChunk =
|
2244
|
-
|
2271
|
+
if (isFirstChunk) {
|
2272
|
+
const msToFirstChunk = now() - startTimestampMs;
|
2273
|
+
isFirstChunk = false;
|
2245
2274
|
doStreamSpan.addEvent("ai.stream.firstChunk", {
|
2246
2275
|
"ai.stream.msToFirstChunk": msToFirstChunk
|
2247
2276
|
});
|
@@ -2251,36 +2280,37 @@ var DefaultStreamObjectResult = class {
|
|
2251
2280
|
}
|
2252
2281
|
if (typeof chunk === "string") {
|
2253
2282
|
accumulatedText += chunk;
|
2254
|
-
|
2283
|
+
textDelta += chunk;
|
2255
2284
|
const { value: currentObjectJson, state: parseState } = parsePartialJson(accumulatedText);
|
2256
2285
|
if (currentObjectJson !== void 0 && !isDeepEqualData(latestObjectJson, currentObjectJson)) {
|
2257
2286
|
const validationResult = outputStrategy.validatePartialResult({
|
2258
2287
|
value: currentObjectJson,
|
2259
|
-
|
2288
|
+
textDelta,
|
2289
|
+
latestObject,
|
2290
|
+
isFirstDelta,
|
2291
|
+
isFinalDelta: parseState === "successful-parse"
|
2260
2292
|
});
|
2261
|
-
if (validationResult.success && !isDeepEqualData(latestObject, validationResult.value)) {
|
2293
|
+
if (validationResult.success && !isDeepEqualData(latestObject, validationResult.value.partial)) {
|
2262
2294
|
latestObjectJson = currentObjectJson;
|
2263
|
-
latestObject = validationResult.value;
|
2295
|
+
latestObject = validationResult.value.partial;
|
2264
2296
|
controller.enqueue({
|
2265
2297
|
type: "object",
|
2266
2298
|
object: latestObject
|
2267
2299
|
});
|
2268
2300
|
controller.enqueue({
|
2269
2301
|
type: "text-delta",
|
2270
|
-
textDelta:
|
2302
|
+
textDelta: validationResult.value.textDelta
|
2271
2303
|
});
|
2272
|
-
|
2304
|
+
textDelta = "";
|
2305
|
+
isFirstDelta = false;
|
2273
2306
|
}
|
2274
2307
|
}
|
2275
2308
|
return;
|
2276
2309
|
}
|
2277
2310
|
switch (chunk.type) {
|
2278
2311
|
case "finish": {
|
2279
|
-
if (
|
2280
|
-
controller.enqueue({
|
2281
|
-
type: "text-delta",
|
2282
|
-
textDelta: delta
|
2283
|
-
});
|
2312
|
+
if (textDelta !== "") {
|
2313
|
+
controller.enqueue({ type: "text-delta", textDelta });
|
2284
2314
|
}
|
2285
2315
|
finishReason = chunk.finishReason;
|
2286
2316
|
usage = calculateCompletionTokenUsage(chunk.usage);
|
@@ -3297,7 +3327,7 @@ async function streamText({
|
|
3297
3327
|
const {
|
3298
3328
|
result: { stream: stream2, warnings: warnings2, rawResponse: rawResponse2 },
|
3299
3329
|
doStreamSpan: doStreamSpan2,
|
3300
|
-
|
3330
|
+
startTimestampMs: startTimestampMs2
|
3301
3331
|
} = await retry(
|
3302
3332
|
() => recordSpan({
|
3303
3333
|
name: "ai.streamText.doStream",
|
@@ -3326,7 +3356,7 @@ async function streamText({
|
|
3326
3356
|
tracer,
|
3327
3357
|
endWhenDone: false,
|
3328
3358
|
fn: async (doStreamSpan3) => ({
|
3329
|
-
|
3359
|
+
startTimestampMs: now(),
|
3330
3360
|
// get before the call
|
3331
3361
|
doStreamSpan: doStreamSpan3,
|
3332
3362
|
result: await model.doStream({
|
@@ -3356,7 +3386,7 @@ async function streamText({
|
|
3356
3386
|
rawResponse: rawResponse2
|
3357
3387
|
},
|
3358
3388
|
doStreamSpan: doStreamSpan2,
|
3359
|
-
|
3389
|
+
startTimestampMs: startTimestampMs2
|
3360
3390
|
};
|
3361
3391
|
};
|
3362
3392
|
const promptMessages = await convertToLanguageModelPrompt({
|
@@ -3366,7 +3396,7 @@ async function streamText({
|
|
3366
3396
|
const {
|
3367
3397
|
result: { stream, warnings, rawResponse },
|
3368
3398
|
doStreamSpan,
|
3369
|
-
|
3399
|
+
startTimestampMs
|
3370
3400
|
} = await startRoundtrip({
|
3371
3401
|
promptType: validatePrompt({ system, prompt, messages }).type,
|
3372
3402
|
promptMessages
|
@@ -3380,7 +3410,7 @@ async function streamText({
|
|
3380
3410
|
rootSpan,
|
3381
3411
|
doStreamSpan,
|
3382
3412
|
telemetry,
|
3383
|
-
|
3413
|
+
startTimestampMs,
|
3384
3414
|
maxToolRoundtrips,
|
3385
3415
|
startRoundtrip,
|
3386
3416
|
promptMessages
|
@@ -3398,7 +3428,7 @@ var DefaultStreamTextResult = class {
|
|
3398
3428
|
rootSpan,
|
3399
3429
|
doStreamSpan,
|
3400
3430
|
telemetry,
|
3401
|
-
|
3431
|
+
startTimestampMs,
|
3402
3432
|
maxToolRoundtrips,
|
3403
3433
|
startRoundtrip,
|
3404
3434
|
promptMessages
|
@@ -3429,7 +3459,7 @@ var DefaultStreamTextResult = class {
|
|
3429
3459
|
const self = this;
|
3430
3460
|
function addRoundtripStream({
|
3431
3461
|
stream: stream2,
|
3432
|
-
startTimestamp
|
3462
|
+
startTimestamp,
|
3433
3463
|
doStreamSpan: doStreamSpan2,
|
3434
3464
|
currentToolRoundtrip,
|
3435
3465
|
promptMessages: promptMessages2,
|
@@ -3456,7 +3486,7 @@ var DefaultStreamTextResult = class {
|
|
3456
3486
|
new TransformStream({
|
3457
3487
|
async transform(chunk, controller) {
|
3458
3488
|
if (roundtripFirstChunk) {
|
3459
|
-
const msToFirstChunk =
|
3489
|
+
const msToFirstChunk = now() - startTimestamp;
|
3460
3490
|
roundtripFirstChunk = false;
|
3461
3491
|
doStreamSpan2.addEvent("ai.stream.firstChunk", {
|
3462
3492
|
"ai.stream.msToFirstChunk": msToFirstChunk
|
@@ -3560,7 +3590,11 @@ var DefaultStreamTextResult = class {
|
|
3560
3590
|
(message) => convertToLanguageModelMessage(message, null)
|
3561
3591
|
)
|
3562
3592
|
);
|
3563
|
-
const {
|
3593
|
+
const {
|
3594
|
+
result,
|
3595
|
+
doStreamSpan: doStreamSpan3,
|
3596
|
+
startTimestampMs: startTimestamp2
|
3597
|
+
} = await startRoundtrip({
|
3564
3598
|
promptType: "messages",
|
3565
3599
|
promptMessages: promptMessages2
|
3566
3600
|
});
|
@@ -3568,7 +3602,7 @@ var DefaultStreamTextResult = class {
|
|
3568
3602
|
self.rawResponse = result.rawResponse;
|
3569
3603
|
addRoundtripStream({
|
3570
3604
|
stream: result.stream,
|
3571
|
-
startTimestamp:
|
3605
|
+
startTimestamp: startTimestamp2,
|
3572
3606
|
doStreamSpan: doStreamSpan3,
|
3573
3607
|
currentToolRoundtrip: currentToolRoundtrip + 1,
|
3574
3608
|
promptMessages: promptMessages2,
|
@@ -3631,7 +3665,7 @@ var DefaultStreamTextResult = class {
|
|
3631
3665
|
}
|
3632
3666
|
addRoundtripStream({
|
3633
3667
|
stream,
|
3634
|
-
startTimestamp,
|
3668
|
+
startTimestamp: startTimestampMs,
|
3635
3669
|
doStreamSpan,
|
3636
3670
|
currentToolRoundtrip: 0,
|
3637
3671
|
promptMessages,
|