Skip to content

toDataStream + createHaloStream ​

Stream utilities for SSE protocol conversion and custom streaming.

Import ​

ts
import { toDataStream, createHaloStream } from "@halo-ai/stream";

toDataStream(source, opts?) ​

Wraps an AsyncGenerator<TurnChunk> as an SSE Response. Compatible with Vercel AI SDK's useChat.

ts
toDataStream(
  source: AsyncGenerator<TurnChunk>,
  opts?: { headers?: Record<string, string> }
): Response

SSE Protocol ​

TurnChunkSSE Output
text-delta0:"<delta>"
tool-call-delta9:[<chunk>]
tool-call-ready9:[<chunk>]
doned:{"usage":{...}}

Usage ​

ts
// In a Next.js API route
const stream = agent.sdkStream(messages);
return toDataStream(stream);

createHaloStream(fn) ​

Custom stream entry without going through HaloAgent.

ts
createHaloStream(
  fn: (ctrl: {
    writeText: (delta: string) => void;
    writeToolCall: (call: ToolCall) => void;
    close: (usage?: Usage) => void;
    error: (err: Error) => void;
  }) => Promise<void>
): ReadableStream

Usage ​

ts
const stream = createHaloStream(async (ctrl) => {
  ctrl.writeText("Processing... ");
  await someAsyncWork();
  ctrl.writeText("Done!");
  ctrl.close({ promptTokens: 10, completionTokens: 5 });
});

Released under the MIT License.