ai 3.1.27 → 3.1.29
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 +39 -3
- package/dist/index.d.ts +39 -3
- package/dist/index.js +48 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +46 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.mjs
CHANGED
@@ -983,6 +983,7 @@ function parsePartialJson(jsonText) {
|
|
983
983
|
}
|
984
984
|
|
985
985
|
// core/generate-object/stream-object.ts
|
986
|
+
import { safeValidateTypes } from "@ai-sdk/provider-utils";
|
986
987
|
async function streamObject({
|
987
988
|
model,
|
988
989
|
schema,
|
@@ -992,6 +993,7 @@ async function streamObject({
|
|
992
993
|
messages,
|
993
994
|
maxRetries,
|
994
995
|
abortSignal,
|
996
|
+
onFinish,
|
995
997
|
...settings
|
996
998
|
}) {
|
997
999
|
const retry = retryWithExponentialBackoff({ maxRetries });
|
@@ -1106,22 +1108,34 @@ async function streamObject({
|
|
1106
1108
|
return new StreamObjectResult({
|
1107
1109
|
stream: result.stream.pipeThrough(new TransformStream(transformer)),
|
1108
1110
|
warnings: result.warnings,
|
1109
|
-
rawResponse: result.rawResponse
|
1111
|
+
rawResponse: result.rawResponse,
|
1112
|
+
schema,
|
1113
|
+
onFinish
|
1110
1114
|
});
|
1111
1115
|
}
|
1112
1116
|
var StreamObjectResult = class {
|
1113
1117
|
constructor({
|
1114
1118
|
stream,
|
1115
1119
|
warnings,
|
1116
|
-
rawResponse
|
1120
|
+
rawResponse,
|
1121
|
+
schema,
|
1122
|
+
onFinish
|
1117
1123
|
}) {
|
1118
1124
|
this.warnings = warnings;
|
1119
1125
|
this.rawResponse = rawResponse;
|
1126
|
+
let resolveObject;
|
1127
|
+
let rejectObject;
|
1128
|
+
this.object = new Promise((resolve, reject) => {
|
1129
|
+
resolveObject = resolve;
|
1130
|
+
rejectObject = reject;
|
1131
|
+
});
|
1120
1132
|
let resolveUsage;
|
1121
1133
|
this.usage = new Promise((resolve) => {
|
1122
1134
|
resolveUsage = resolve;
|
1123
1135
|
});
|
1124
1136
|
let usage;
|
1137
|
+
let object;
|
1138
|
+
let error;
|
1125
1139
|
let accumulatedText = "";
|
1126
1140
|
let latestObject = void 0;
|
1127
1141
|
this.originalStream = stream.pipeThrough(
|
@@ -1141,11 +1155,19 @@ var StreamObjectResult = class {
|
|
1141
1155
|
switch (chunk.type) {
|
1142
1156
|
case "finish": {
|
1143
1157
|
usage = calculateTokenUsage(chunk.usage);
|
1144
|
-
controller.enqueue({
|
1145
|
-
...chunk,
|
1146
|
-
usage
|
1147
|
-
});
|
1158
|
+
controller.enqueue({ ...chunk, usage });
|
1148
1159
|
resolveUsage(usage);
|
1160
|
+
const validationResult = safeValidateTypes({
|
1161
|
+
value: latestObject,
|
1162
|
+
schema
|
1163
|
+
});
|
1164
|
+
if (validationResult.success) {
|
1165
|
+
object = validationResult.value;
|
1166
|
+
resolveObject(object);
|
1167
|
+
} else {
|
1168
|
+
error = validationResult.error;
|
1169
|
+
rejectObject(error);
|
1170
|
+
}
|
1149
1171
|
break;
|
1150
1172
|
}
|
1151
1173
|
default: {
|
@@ -1153,6 +1175,24 @@ var StreamObjectResult = class {
|
|
1153
1175
|
break;
|
1154
1176
|
}
|
1155
1177
|
}
|
1178
|
+
},
|
1179
|
+
// invoke onFinish callback and resolve toolResults promise when the stream is about to close:
|
1180
|
+
async flush(controller) {
|
1181
|
+
try {
|
1182
|
+
await (onFinish == null ? void 0 : onFinish({
|
1183
|
+
usage: usage != null ? usage : {
|
1184
|
+
promptTokens: NaN,
|
1185
|
+
completionTokens: NaN,
|
1186
|
+
totalTokens: NaN
|
1187
|
+
},
|
1188
|
+
object,
|
1189
|
+
error,
|
1190
|
+
rawResponse,
|
1191
|
+
warnings
|
1192
|
+
}));
|
1193
|
+
} catch (error2) {
|
1194
|
+
controller.error(error2);
|
1195
|
+
}
|
1156
1196
|
}
|
1157
1197
|
})
|
1158
1198
|
);
|