1. Set up the agent once
Publish the template and create the application’s agent once, and keep its ID in your configuration.agent/template.yaml
2. The backend
Two routes: one starts a conversation, the other mints a fresh token for a conversation the user already has. They are written as Web-standard handlers, which Next.js route handlers and most frameworks accept.userIdOf stands for your own authentication.
app/api/conversations/route.ts
app/api/conversations/[id]/token/route.ts
sessions.list finds their conversations and the token route checks ownership with no table of your own.
3. The chat page
app/chat.tsx
What you get without writing it
- Send at any time. The message box stays enabled during a turn: messages sent meanwhile wait, marked
waiting, and the next turn takes them all. - Stop and hold. After Stop, waiting messages are held; the next message sends them along.
- Live text. The reply grows as the agent writes it, as
streamingentries that the finished message replaces in place. - Away and back. Close the tab mid-turn and open the conversation later:
useSessionloads every event, including everything the agent did meanwhile, then follows it live. - Reconnection. A dropped connection shows
reconnectingand resumes after the last event the page holds, with nothing lost or repeated. - Backend messages. Your backend can send into the same conversation at any time with
session.send(text), and the page shows it like any other message.