@ai-sdk/provider-utils 1.0.2 → 1.0.4

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.
@@ -75,6 +75,13 @@ async function convertReadableStreamToArray(stream) {
75
75
  return result;
76
76
  }
77
77
 
78
+ // src/test/convert-response-stream-to-array.ts
79
+ async function convertResponseStreamToArray(response) {
80
+ return convertReadableStreamToArray(
81
+ response.body.pipeThrough(new TextDecoderStream())
82
+ );
83
+ }
84
+
78
85
  // ../../node_modules/.pnpm/outvariant@1.4.2/node_modules/outvariant/lib/index.mjs
79
86
  var POSITIONALS_EXP = /(%?)(%([sdijo]))/g;
80
87
  function serializePositional(positional, flag) {
@@ -5200,6 +5207,10 @@ var JsonTestServer = class {
5200
5207
  expect(this.request).toBeDefined();
5201
5208
  return new URL(this.request.url).searchParams;
5202
5209
  }
5210
+ async getRequestUrl() {
5211
+ expect(this.request).toBeDefined();
5212
+ return new URL(this.request.url).toString();
5213
+ }
5203
5214
  setupTestEnvironment() {
5204
5215
  beforeAll(() => this.server.listen());
5205
5216
  beforeEach(() => {
@@ -5300,13 +5311,40 @@ function createServer({
5300
5311
  pushCall,
5301
5312
  pushController
5302
5313
  }) {
5314
+ const responsesArray = Array.isArray(responses) ? responses : [responses];
5315
+ const responsesByUrl = responsesArray.reduce((responsesByUrl2, response) => {
5316
+ if (!responsesByUrl2[response.url]) {
5317
+ responsesByUrl2[response.url] = [];
5318
+ }
5319
+ responsesByUrl2[response.url].push(response);
5320
+ return responsesByUrl2;
5321
+ }, {});
5322
+ const streams = {};
5323
+ responsesArray.filter(
5324
+ (response) => response.type === "controlled-stream"
5325
+ ).forEach((response) => {
5326
+ var _a3, _b2;
5327
+ let streamController;
5328
+ const stream = new ReadableStream({
5329
+ start(controller) {
5330
+ streamController = controller;
5331
+ }
5332
+ });
5333
+ pushController((_a3 = response.id) != null ? _a3 : "", () => streamController);
5334
+ streams[(_b2 = response.id) != null ? _b2 : ""] = stream;
5335
+ });
5336
+ const urlInvocationCounts = Object.fromEntries(
5337
+ Object.entries(responsesByUrl).map(([url]) => [url, 0])
5338
+ );
5303
5339
  return setupServer(
5304
- ...(Array.isArray(responses) ? responses : [responses]).map((response) => {
5305
- var _a3;
5306
- switch (response.type) {
5307
- case "json-value": {
5308
- return http.post(response.url, ({ request: request2 }) => {
5309
- pushCall(new TestServerCall(request2));
5340
+ ...Object.entries(responsesByUrl).map(([url, responses2]) => {
5341
+ return http.post(url, ({ request: request2 }) => {
5342
+ var _a3, _b2;
5343
+ pushCall(new TestServerCall(request2));
5344
+ const invocationCount = urlInvocationCounts[url]++;
5345
+ const response = responses2[invocationCount > responses2.length ? responses2.length - 1 : invocationCount];
5346
+ switch (response.type) {
5347
+ case "json-value":
5310
5348
  return HttpResponse.json(response.content, {
5311
5349
  status: 200,
5312
5350
  headers: {
@@ -5314,11 +5352,7 @@ function createServer({
5314
5352
  ...response.headers
5315
5353
  }
5316
5354
  });
5317
- });
5318
- }
5319
- case "stream-values": {
5320
- return http.post(response.url, ({ request: request2 }) => {
5321
- pushCall(new TestServerCall(request2));
5355
+ case "stream-values":
5322
5356
  return new HttpResponse(
5323
5357
  convertArrayToReadableStream(response.content).pipeThrough(
5324
5358
  new TextEncoderStream()
@@ -5333,20 +5367,9 @@ function createServer({
5333
5367
  }
5334
5368
  }
5335
5369
  );
5336
- });
5337
- }
5338
- case "controlled-stream": {
5339
- let streamController;
5340
- const stream = new ReadableStream({
5341
- start(controller) {
5342
- streamController = controller;
5343
- }
5344
- });
5345
- pushController((_a3 = response.id) != null ? _a3 : "", () => streamController);
5346
- return http.post(response.url, ({ request: request2 }) => {
5347
- pushCall(new TestServerCall(request2));
5370
+ case "controlled-stream": {
5348
5371
  return new HttpResponse(
5349
- stream.pipeThrough(new TextEncoderStream()),
5372
+ streams[(_a3 = response.id) != null ? _a3 : ""].pipeThrough(new TextEncoderStream()),
5350
5373
  {
5351
5374
  status: 200,
5352
5375
  headers: {
@@ -5357,21 +5380,16 @@ function createServer({
5357
5380
  }
5358
5381
  }
5359
5382
  );
5360
- });
5361
- }
5362
- case "error": {
5363
- return http.post(response.url, ({ request: request2 }) => {
5364
- var _a4;
5365
- pushCall(new TestServerCall(request2));
5366
- return HttpResponse.text((_a4 = response.content) != null ? _a4 : "Error", {
5383
+ }
5384
+ case "error":
5385
+ return HttpResponse.text((_b2 = response.content) != null ? _b2 : "Error", {
5367
5386
  status: response.status,
5368
5387
  headers: {
5369
5388
  ...response.headers
5370
5389
  }
5371
5390
  });
5372
- });
5373
5391
  }
5374
- }
5392
+ });
5375
5393
  })
5376
5394
  );
5377
5395
  }
@@ -5446,6 +5464,7 @@ export {
5446
5464
  convertArrayToReadableStream,
5447
5465
  convertAsyncIterableToArray,
5448
5466
  convertReadableStreamToArray,
5467
+ convertResponseStreamToArray,
5449
5468
  convertStreamToArray,
5450
5469
  describeWithTestServer,
5451
5470
  withTestServer