Hono

04 Streaming

Stream Anvia run events from a Hono route.

Hono handlers can return a standard Response, so Anvia readableStream() can be used directly.

1. Add /api/support/stream

import { zValidator } from "@hono/zod-validator";
import { z } from "zod";
import { supportAgent } from "./ai/support-agent";

const SupportStreamRequest = z.object({
  message: z.string().trim().min(1, "message is required"),
});

app.post("/api/support/stream", zValidator("json", SupportStreamRequest), async (c) => {
  const { message } = c.req.valid("json");
  return new Response(supportAgent.prompt(message).readableStream(), {
    headers: {
      "Content-Type": "application/x-ndjson",
      "Cache-Control": "no-cache",
    },
  });
});

2. Consume The Stream

const response = await fetch("http://localhost:3000/api/support/stream", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ message: "Draft a support reply." }),
});

const reader = response.body?.getReader();
const decoder = new TextDecoder();

while (reader) {
  const next = await reader.read();
  if (next.done) break;

  for (const line of decoder.decode(next.value).split("\n")) {
    if (line.trim()) console.log(JSON.parse(line));
  }
}

3. Handle Stream Errors

Anvia writes a terminal error event if iteration fails. Clients should handle both final and error.

Next

Add auth, request-local tools, and retrieval in Tools and Context. Related guides: Readable Streams and Streaming Events.