logoassistant-ui
API ReferenceIntegrations

@assistant-ui/react-data-stream

Data Stream protocol integration for assistant-ui.

API Reference

useDataStreamRuntime

Create a runtime that connects to a data stream protocol endpoint.

import { useDataStreamRuntime } from "@assistant-ui/react-data-stream";

const MyRuntimeProvider = ({ children }: { children: React.ReactNode }) => {
  const runtime = useDataStreamRuntime({
    api: "/api/chat",
  });

  return (
    <AssistantRuntimeProvider runtime={runtime}>
      {children}
    </AssistantRuntimeProvider>
  );
};

api?:

string

The API endpoint URL for the data stream protocol.

onResponse?:

(response: Response) => void | Promise<void>

Optional callback called when a response is received.

onFinish?:

(message: ThreadMessage) => void

Optional callback called when a message is finished.

onError?:

(error: Error) => void

Optional callback called when an error occurs.

onCancel?:

() => void

Optional callback called when a request is cancelled.

credentials?:

RequestCredentials

Optional credentials mode for the fetch request.

headers?:

Record<string, string> | Headers | (() => Promise<Record<string, string> | Headers>)

Optional headers to include in the request.

body?:

object

Optional additional body parameters to include in the request.

sendExtraMessageFields?:

boolean

Whether to include extra message fields like IDs in the request.

useCloudRuntime

Create a runtime that connects to Assistant Cloud using the data stream protocol.

import { useCloudRuntime } from "@assistant-ui/react-data-stream";

const MyRuntimeProvider = ({ children }: { children: React.ReactNode }) => {
  const runtime = useCloudRuntime({
    cloud: assistantCloud,
    assistantId: "my-assistant-id",
  });

  return (
    <AssistantRuntimeProvider runtime={runtime}>
      {children}
    </AssistantRuntimeProvider>
  );
};

cloud?:

AssistantCloud

The Assistant Cloud instance.

assistantId?:

string

The ID of the assistant to connect to.

onResponse?:

(response: Response) => void | Promise<void>

Optional callback called when a response is received.

onFinish?:

(message: ThreadMessage) => void

Optional callback called when a message is finished.

onError?:

(error: Error) => void

Optional callback called when an error occurs.

onCancel?:

() => void

Optional callback called when a request is cancelled.

credentials?:

RequestCredentials

Optional credentials mode for the fetch request.

headers?:

Record<string, string> | Headers | (() => Promise<Record<string, string> | Headers>)

Optional headers to include in the request.

body?:

object

Optional additional body parameters to include in the request.

sendExtraMessageFields?:

boolean

Whether to include extra message fields like IDs in the request.

toLanguageModelMessages

Convert assistant-ui messages to language model format.

import { toLanguageModelMessages } from "@assistant-ui/react-data-stream";

const languageModelMessages = toLanguageModelMessages(messages, {
  unstable_includeId: true,
});

messages?:

readonly ThreadMessage[]

The messages to convert.

options?:

{ unstable_includeId?: boolean }

Optional conversion options.

{ unstable_includeId?: boolean }

unstable_includeId?:

boolean

Whether to include message IDs in the converted messages.