ai 4.1.55 → 4.1.56

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.mjs CHANGED
@@ -6104,6 +6104,89 @@ function extractReasoningMiddleware({
6104
6104
  };
6105
6105
  }
6106
6106
 
6107
+ // core/middleware/simulate-streaming-middleware.ts
6108
+ function simulateStreamingMiddleware() {
6109
+ return {
6110
+ middlewareVersion: "v1",
6111
+ wrapStream: async ({ doGenerate }) => {
6112
+ const result = await doGenerate();
6113
+ const simulatedStream = new ReadableStream({
6114
+ start(controller) {
6115
+ controller.enqueue({ type: "response-metadata", ...result.response });
6116
+ if (result.reasoning) {
6117
+ if (typeof result.reasoning === "string") {
6118
+ controller.enqueue({
6119
+ type: "reasoning",
6120
+ textDelta: result.reasoning
6121
+ });
6122
+ } else {
6123
+ for (const reasoning of result.reasoning) {
6124
+ switch (reasoning.type) {
6125
+ case "text": {
6126
+ controller.enqueue({
6127
+ type: "reasoning",
6128
+ textDelta: reasoning.text
6129
+ });
6130
+ if (reasoning.signature != null) {
6131
+ controller.enqueue({
6132
+ type: "reasoning-signature",
6133
+ signature: reasoning.signature
6134
+ });
6135
+ }
6136
+ break;
6137
+ }
6138
+ case "redacted": {
6139
+ controller.enqueue({
6140
+ type: "redacted-reasoning",
6141
+ data: reasoning.data
6142
+ });
6143
+ break;
6144
+ }
6145
+ }
6146
+ }
6147
+ }
6148
+ }
6149
+ if (result.text) {
6150
+ controller.enqueue({
6151
+ type: "text-delta",
6152
+ textDelta: result.text
6153
+ });
6154
+ }
6155
+ if (result.toolCalls) {
6156
+ for (const toolCall of result.toolCalls) {
6157
+ controller.enqueue({
6158
+ type: "tool-call-delta",
6159
+ toolCallType: "function",
6160
+ toolCallId: toolCall.toolCallId,
6161
+ toolName: toolCall.toolName,
6162
+ argsTextDelta: toolCall.args
6163
+ });
6164
+ controller.enqueue({
6165
+ type: "tool-call",
6166
+ ...toolCall
6167
+ });
6168
+ }
6169
+ }
6170
+ controller.enqueue({
6171
+ type: "finish",
6172
+ finishReason: result.finishReason,
6173
+ usage: result.usage,
6174
+ logprobs: result.logprobs,
6175
+ providerMetadata: result.providerMetadata
6176
+ });
6177
+ controller.close();
6178
+ }
6179
+ });
6180
+ return {
6181
+ stream: simulatedStream,
6182
+ rawCall: result.rawCall,
6183
+ rawResponse: result.rawResponse,
6184
+ warnings: result.warnings
6185
+ };
6186
+ }
6187
+ };
6188
+ }
6189
+
6107
6190
  // core/middleware/wrap-language-model.ts
6108
6191
  var wrapLanguageModel = ({
6109
6192
  model,
@@ -6139,12 +6222,19 @@ var doWrap = ({
6139
6222
  async doGenerate(params) {
6140
6223
  const transformedParams = await doTransform({ params, type: "generate" });
6141
6224
  const doGenerate = async () => model.doGenerate(transformedParams);
6142
- return wrapGenerate ? wrapGenerate({ doGenerate, params: transformedParams, model }) : doGenerate();
6225
+ const doStream = async () => model.doStream(transformedParams);
6226
+ return wrapGenerate ? wrapGenerate({
6227
+ doGenerate,
6228
+ doStream,
6229
+ params: transformedParams,
6230
+ model
6231
+ }) : doGenerate();
6143
6232
  },
6144
6233
  async doStream(params) {
6145
6234
  const transformedParams = await doTransform({ params, type: "stream" });
6235
+ const doGenerate = async () => model.doGenerate(transformedParams);
6146
6236
  const doStream = async () => model.doStream(transformedParams);
6147
- return wrapStream ? wrapStream({ doStream, params: transformedParams, model }) : doStream();
6237
+ return wrapStream ? wrapStream({ doGenerate, doStream, params: transformedParams, model }) : doStream();
6148
6238
  }
6149
6239
  };
6150
6240
  };
@@ -7652,6 +7742,7 @@ export {
7652
7742
  processDataStream,
7653
7743
  processTextStream,
7654
7744
  simulateReadableStream,
7745
+ simulateStreamingMiddleware,
7655
7746
  smoothStream,
7656
7747
  streamObject,
7657
7748
  streamText,