A transport that directly communicates with an Agent in-process, without going through HTTP. This is useful for:
Server-side rendering scenarios
Testing without network
Single-process applications
Unlike DefaultChatTransport which sends HTTP requests to an API endpoint, DirectChatTransport invokes the agent's stream() method directly and converts the result to a UI message stream.
import { useChat } from '@ai-sdk/react';
import { DirectChatTransport, ToolLoopAgent } from 'ai';
const agent = new ToolLoopAgent({
model: "anthropic/claude-sonnet-5.5",
instructions: 'You are a helpful assistant.',
});
export default function Chat() {
const { messages, sendMessage, status } = useChat({
transport: new DirectChatTransport({ agent }),
});
// ... render chat UI
}
The original messages. If provided, persistence mode is assumed, and a message ID is provided for the response message (generated with generateMessageId, or reused from the last message when it is an assistant message).
Generate a message ID for the response message. If not provided, no message ID will be set for the response message (unless the original messages are provided and the last message is an assistant message).
Called after each step is converted to UI message parts. Receives messages, isContinuation, and the accumulated responseMessage, including step metadata, for inspection or persistence.
Send the message start event to the client. Set to false if you are using additional streamText calls and the message start event has already been sent. Defaults to true.
Sends messages to the agent and returns a streaming response. This method validates and converts UI messages to model messages, calls the agent's stream() method, and returns the result as a UI message stream.