Skip to main content

pipeUIMessageStreamToResponse

还没翻译译自英文(ai-sdk.dev)本页 9 段都还没译

The pipeUIMessageStreamToResponse function pipes streaming data to a Node.js ServerResponse object (see Streaming Data).

Import

import { pipeUIMessageStreamToResponse } from "ai"

Example

await pipeUIMessageStreamToResponse({
  response: serverResponse,
  status: 200,
  statusText: 'OK',
  headers: {
    'Custom-Header': 'value',
  },
  stream: myUIMessageStream,
  consumeSseStream: ({ stream }) => {
    // Optional: consume the SSE stream independently
    console.log('Consuming SSE stream:', stream);
  },
});

API Signature

Parameters

  • response (ServerResponse): The Node.js ServerResponse object to pipe the data to.
  • stream (ReadableStream<UIMessageChunk>): The UI message stream to pipe to the response.
  • status? (number): The status code for the response.
  • statusText? (string): The status text for the response.
  • headers? (Headers | Record<string, string>): Additional headers for the response.
  • keepAliveMs? (number): Interval in milliseconds for sending SSE keep-alive comments. When set, an opening comment is sent immediately and additional comments are sent after the stream has been idle for the configured interval.
  • consumeSseStream? (({ stream }: { stream: ReadableStream<string> }) => PromiseLike<void> | void): Optional function to consume the SSE stream independently. The stream is teed and this function receives a copy.

Returns

A Promise<void> that resolves when the stream has been written to the response and rejects when reading or writing the stream fails.