Custom AI transport
Stream Ask AI from your own endpoint.
transport replaces endpoint. Yield text chunks and stop when signal aborts. Throw to show the error in the panel. plainTextTransport, sseTransport, and aiSdkTransport cover the common HTTP shapes if you do not need a custom one.
This page's code block is examples/recipes/ai-transport.ts. pnpm --dir docs typecheck compiles that file and fails if this block drifts.
import type { AskAITransport } from "@/components/editor/ai/transport";
import { createBlockcnEditor } from "@/components/editor/preset";
import { ai } from "@/components/editor/ai/aiAddon";
const transport: AskAITransport = async function* (request, { signal }) {
const response = await fetch("/api/ai", {
method: "POST",
signal,
headers: { "content-type": "application/json", Authorization: "Bearer replace-me" },
body: JSON.stringify(request),
});
if (!response.ok || !response.body) throw new Error(`AI failed (${response.status})`);
const reader = response.body.getReader();
const decoder = new TextDecoder();
for (;;) {
const { value, done } = await reader.read();
if (done) return;
yield decoder.decode(value);
}
};
export const kit = createBlockcnEditor({ addons: [ai({ transport })] });