ai 3.0.25 → 3.0.26

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 CHANGED
@@ -1,8 +1,8 @@
1
1
  import { LanguageModelV1, LanguageModelV1FinishReason, LanguageModelV1CallWarning } from '@ai-sdk/provider';
2
2
  import { z } from 'zod';
3
+ import { ServerResponse } from 'node:http';
3
4
  import { AssistantStream } from 'openai/lib/AssistantStream';
4
5
  import { Run } from 'openai/resources/beta/threads/runs/runs';
5
- import { ServerResponse } from 'node:http';
6
6
 
7
7
  type TokenUsage = {
8
8
  promptTokens: number;
@@ -683,6 +683,18 @@ declare class StreamTextResult<TOOLS extends Record<string, ExperimentalTool>> {
683
683
  */
684
684
  toAIStream(callbacks?: AIStreamCallbacksAndOptions): ReadableStream<any>;
685
685
  /**
686
+ Writes stream data output to a Node.js response-like object.
687
+ It sets a `Content-Type` header to `text/plain; charset=utf-8` and
688
+ writes each text delta as a separate chunk.
689
+
690
+ @param response A Node.js response-like object (ServerResponse).
691
+ @param init Optional headers and status code.
692
+ */
693
+ pipeAIStreamToResponse(response: ServerResponse, init?: {
694
+ headers?: Record<string, string>;
695
+ status?: number;
696
+ }): void;
697
+ /**
686
698
  Creates a simple text stream response.
687
699
  Each text delta is encoded as UTF-8 and sent as a separate chunk.
688
700
  Non-text-delta events are ignored.
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { LanguageModelV1, LanguageModelV1FinishReason, LanguageModelV1CallWarning } from '@ai-sdk/provider';
2
2
  import { z } from 'zod';
3
+ import { ServerResponse } from 'node:http';
3
4
  import { AssistantStream } from 'openai/lib/AssistantStream';
4
5
  import { Run } from 'openai/resources/beta/threads/runs/runs';
5
- import { ServerResponse } from 'node:http';
6
6
 
7
7
  type TokenUsage = {
8
8
  promptTokens: number;
@@ -683,6 +683,18 @@ declare class StreamTextResult<TOOLS extends Record<string, ExperimentalTool>> {
683
683
  */
684
684
  toAIStream(callbacks?: AIStreamCallbacksAndOptions): ReadableStream<any>;
685
685
  /**
686
+ Writes stream data output to a Node.js response-like object.
687
+ It sets a `Content-Type` header to `text/plain; charset=utf-8` and
688
+ writes each text delta as a separate chunk.
689
+
690
+ @param response A Node.js response-like object (ServerResponse).
691
+ @param init Optional headers and status code.
692
+ */
693
+ pipeAIStreamToResponse(response: ServerResponse, init?: {
694
+ headers?: Record<string, string>;
695
+ status?: number;
696
+ }): void;
697
+ /**
686
698
  Creates a simple text stream response.
687
699
  Each text delta is encoded as UTF-8 and sent as a separate chunk.
688
700
  Non-text-delta events are ignored.
package/dist/index.js CHANGED
@@ -1492,11 +1492,43 @@ var StreamTextResult = class {
1492
1492
  return this.textStream.pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
1493
1493
  }
1494
1494
  /**
1495
+ Writes stream data output to a Node.js response-like object.
1496
+ It sets a `Content-Type` header to `text/plain; charset=utf-8` and
1497
+ writes each text delta as a separate chunk.
1498
+
1499
+ @param response A Node.js response-like object (ServerResponse).
1500
+ @param init Optional headers and status code.
1501
+ */
1502
+ pipeAIStreamToResponse(response, init) {
1503
+ var _a;
1504
+ response.writeHead((_a = init == null ? void 0 : init.status) != null ? _a : 200, {
1505
+ "Content-Type": "text/plain; charset=utf-8",
1506
+ ...init == null ? void 0 : init.headers
1507
+ });
1508
+ const reader = this.textStream.pipeThrough(createCallbacksTransformer(void 0)).pipeThrough(createStreamDataTransformer()).getReader();
1509
+ const read = async () => {
1510
+ try {
1511
+ while (true) {
1512
+ const { done, value } = await reader.read();
1513
+ if (done)
1514
+ break;
1515
+ response.write(value);
1516
+ }
1517
+ } catch (error) {
1518
+ throw error;
1519
+ } finally {
1520
+ response.end();
1521
+ }
1522
+ };
1523
+ read();
1524
+ }
1525
+ /**
1495
1526
  Creates a simple text stream response.
1496
1527
  Each text delta is encoded as UTF-8 and sent as a separate chunk.
1497
1528
  Non-text-delta events are ignored.
1498
1529
  */
1499
1530
  toTextStreamResponse(init) {
1531
+ var _a;
1500
1532
  const encoder = new TextEncoder();
1501
1533
  return new Response(
1502
1534
  this.textStream.pipeThrough(
@@ -1507,8 +1539,7 @@ var StreamTextResult = class {
1507
1539
  })
1508
1540
  ),
1509
1541
  {
1510
- ...init,
1511
- status: 200,
1542
+ status: (_a = init == null ? void 0 : init.status) != null ? _a : 200,
1512
1543
  headers: {
1513
1544
  "Content-Type": "text/plain; charset=utf-8",
1514
1545
  ...init == null ? void 0 : init.headers