Transport
The ChatTransportAdapter delivers streaming events (text deltas, tool calls, thinking steps) to connected clients in real-time.
Interface
interface ChatTransportAdapter {
publishEvents(runId: string, events: AnyChatEvent[]): Promise<void>;
getReplayState(runId: string): Promise<StreamingStateReplayEvent | null>;
getEvents(runId: string, afterSeq?: number): Promise<{ events: AnyChatEvent[]; latestSeq: number }>;
markComplete(runId: string, result: ProcessMessageResult): Promise<void>;
markError(runId: string, error: string): Promise<void>;
getConnectionUrl(runId: string): string;
readonly provider: string;
}
Built-in Implementations
| Adapter | Transport | Best For |
|---|---|---|
AgentsSDKTransportAdapter | Cloudflare Agents SDK (Agent DO) | Cloudflare Workers |
RawDOTransportAdapter | Raw DurableObject + WebSocket | Cloudflare Workers (full control) |
Cloudflare Workers
import { AgentsSDKTransportAdapter } from "@aimform/ai";
const transport = new AgentsSDKTransportAdapter(
env.WORKFLOW_CHAPERONE_AGENT,
"https://api.aimform.com",
);
// The WorkflowChaperoneAgent DO handles WebSocket connections.
// Clients connect at: wss://api.aimform.com/api/conversations/ws/:runId
The companion WorkflowChaperoneAgent (extending Agent<Env>) buffers up to 2000 events in memory and auto-broadcasts to all connected WebSocket clients.
Late-Joiner Replay
Clients that reconnect mid-stream receive the full accumulated state:
Client connects → sends { type: "subscribe", runId, afterSeq: 0 }
→ Agent replies with streaming_state_replay event containing:
- All accumulated text
- Active tool executions
- Current streaming blocks
- Latest sequence number
→ Continues receiving live events
Custom Transport (Node.js WebSocket)
class WebSocketTransport implements ChatTransportAdapter {
readonly provider = "ws";
private wss: WebSocketServer;
async publishEvents(runId: string, events: AnyChatEvent[]) {
for (const client of this.wss.clients) {
if (client.readyState === WebSocket.OPEN) {
for (const event of events) {
client.send(JSON.stringify(event));
}
}
}
}
// ... implement other methods
}
// Usage on VPS
import { WebSocketServer } from "ws";
const wss = new WebSocketServer({ port: 8080 });
const transport = new WebSocketTransport(wss);
AWS API Gateway
class APIGatewayTransport implements ChatTransportAdapter {
readonly provider = "aws-api-gateway";
constructor(private apiGateway: ApiGatewayManagementApi) {}
async publishEvents(runId: string, events: AnyChatEvent[]) {
const connections = await this.getConnections(runId);
for (const conn of connections) {
await this.apiGateway.postToConnection({
ConnectionId: conn.id,
Data: JSON.stringify({ events }),
}).promise();
}
}
}