Vercel AI SDK
Vercel AI SDK · Vercel AI SDK WhatsApp · AI SDK Telegram bot · Vercel AI SDK iMessage · streamText WhatsApp · Next.js WhatsApp AI agentConnect a Vercel AI SDK agent to Flow Messaging.
Use Flow to receive messages and deliver a Vercel AI SDK agent’s answer as chat bubbles. The webhook example below keeps channel delivery separate from your model and tools.
// app/api/flow/route.ts (Next.js)// npm install @flow-engineer/messaging ai @ai-sdk/openaiimport { after } from "next/server";import { FlowMessaging, contentText } from "@flow-engineer/messaging";import { streamText, type ModelMessage } from "ai";import { openai } from "@ai-sdk/openai";
const flow = new FlowMessaging();
export const POST = flow.webhooks.handler({ onEvent: async (event) => { if (event.type !== "message.received") return;
after(async () => { // The last 20 messages of this conversation, oldest first (the new one included). const page = await event.conversation.messages({ limit: 20 }); const messages: ModelMessage[] = page.data .reverse() .map((m) => ({ role: m.direction === "in" ? "user" : "assistant", content: contentText(m.content) }) as ModelMessage) .filter((m) => m.content);
const result = streamText({ model: openai("gpt-4.1-mini"), system: "You are the assistant for Asha's Bakery. Answer in short messages.", messages, }); await event.conversation.reply(result, { idempotencyKey: event.id }); }); },});Set FLOW_MESSAGING_KEY, FLOW_MESSAGING_WEBHOOK_SECRET and OPENAI_API_KEY, then register https://<your-app>/api/flow as a webhook endpoint for message.received. During development, with no public URL, read events from the live stream (GET /v0/stream) instead; npx @flow-engineer/messaging listen --forward-to http://localhost:3000/api/flow wraps it for this route. See Local development.
How it fits
Section titled “How it fits”reply(result)readsresult.textStream, keeps the typing indicator on, and sends a bubble at each paragraph break while the model writes. See Streaming replies.- Tools work as usual (
tools,stopWhen); only the model’s text is sent to the person, never tool calls or their results (text the model writes between tool steps is sent too). To show progress during long tool calls, wrap the work inevent.conversation.responding(...). - The webhook answers at once and
after()runs the reply, so slow models never cause a timed-out delivery. - One handler serves Telegram and iMessage alike;
event.conversation.channeltells you which one if you want to adapt the tone. - To see each turn’s model and tool calls and get every reply graded, send this agent’s traces to Flow Observe: Traces from agent frameworks.
Full example
Section titled “Full example”examples/vercel-ai-sdk on GitHub.
Related
Section titled “Related” Was this page helpful?
Thanks, we read every comment.