Assistant
Every assistant at ngrok is built from the same parts: the dashboard assistant, the corp assistant, and the AI Gateway playground. This recipe puts them together into one assistant. The composer is a Prompt Input with Attachment chips, a Dropdown Menu model picker, and dictation. The transcript is a Conversation of Messages, and each answer folds its Reasoning and its Tool Calls under one Work toggle above streaming Markdown.
The assistant has two homes. On its own route it is the page. On every other route it sits beside the page in AppLayout.SidePanel. One conversation feeds both, and a navigation between them morphs the page into the panel with the answer still streaming, the draft in the composer, and the same text at the top of the transcript.
Pick a starter prompt, or type a question and press Enter. The first message opens the side panel, so the conversation comes along when you leave. Click Endpoints while the answer streams, then Assistant to bring the page back. Scroll up while the answer streams, and the transcript stays where you put it until you press the button that goes back to the newest message. Ask three questions, and Conversation outline shows in the header to jump back to any of them. Press Stop answer to keep the answer so far, and press Retry to ask again. Ask anything with "fail" in it to see a failed answer. A mock writes the answers, so the demo runs with no network and answers the same way every time. Switch the demo to its mobile width to see the assistant on a phone.
How it works
- The parts own the behavior, the app owns the state.
useChatholds the messages and streams each answer.Conversation.RootandPromptInput.Rootboth take itsstreamingflag: the transcript follows the end and announces "Answering…" once, and the send button turns into stop. There is no chat root component, because each app stores its messages in its own place (a route loader, a query cache, a store above the shell). - One answer, one shape. An answer is reasoning, tool calls, and text, each built from stream events by the pure
applyChatEvent. Reasoning reads "Thinking…" until the first other event, then "Thought for N seconds". A tool call runs until its result arrives, and a stop or a failure marks it interrupted.Message.Pendingsits afterMessage.Contentand pulses until the answer ends. - The work folds away when the answer ends.
Work.Roottakes the answer'sstreamingflag, so the reasoning and the tool calls show while they arrive and fold under "Show work" once the text is done. A step the reader opened mid-answer stays open. Each step's caret shows on hover, focus, or open, and always on a touch screen. - Stop keeps the text.
stopaborts the stream and ends the answer as stopped, so the reader keeps what arrived. A new message sent mid-answer does the same.retryreplaces the last answer with a new one from the same messages. - A failure is a notice, not a dead end. When the stream throws, the answer keeps its text so far and shows an Alert with
role="alert"under it, so a screen reader hears it, and the retry action asks again. - The model picker follows Amp's. A quiet button names the model and its effort. Its menu lists each model on one line, with a provider icon, an optional Badge, a muted description, and a check on the pick. Effort and the other models sit in submenus under a separator. The menu takes the width of its widest row, capped at 24rem and the screen, so a name never wraps and truncates only as a last resort. In a chat narrower than 24rem, as in the side panel or on a phone, the button drops the effort from view, and a screen reader still hears it.
sendandretryhand the model and the effort torespond. A menu holds no form value, so hidden inputs carry both for a form that posts. - One panel look at every size. The page and the side panel render the same
Chat, built on Panel's parts:Panel.Bodyfor the transcript andPanel.Footerfor the composer. The page's header isAppLayout.Header; the panel's isAppLayout.SidePanelHeader, which rendersPanel.Headerand adds the swipe that closes it. Neither body scrolls:Conversation.Viewportscrolls, so it can follow the answer as it streams. - The empty state is the first screen. Before the first message, Empty offers starter prompts in place of the transcript. The composer stays put, so the draft never moves, and a pressed starter or Retry hands focus to the field.
- The transcript lines up with the composer. The transcript and the composer share one centered column, at every width. The transcript sits one corner radius inside the composer, so the edge of a code block or a table meets the composer where its rounded corner ends, as in Amp. The bottom fade ends at the composer. The column and the composer reserve the scrollbar's space alike, so they line up with a classic scrollbar too. The button that goes back to the newest message sits just inside the composer's right edge, as in Amp, not at the window's edge.
- The outline waits for a long chat. From the third message the person sends, a Conversation Outline button shows in the header, at every width. It lists their messages, and a press jumps to one. With fewer messages a scroll is faster, so the button stays hidden, and it renders before the header's other actions so they do not move. Each person's
Message.Roottakes its messageidandscroll-mt-4, so a jump leaves room above it. - Markdown streams as it arrives.
Markdownlexes only the unfinished block, renders at most once per frame, and renders a fence as aCodeBlockthat highlights as the code arrives.
Two homes, one conversation
The tempting shape is one panel that grows to fill the row on the assistant's route. Avoid it. An <aside> is the complementary landmark, and ARIA in HTML does not let it take role="main". A screen reader user on the assistant's route would find the page in the complementary landmark and nothing in main. The landmark follows the route instead:
- On
/assistant,AssistantPagerendersChatinsideAppLayout.Body, which ismain. - On every other route,
AssistantPanelrenders the sameChatinsideAppLayout.SidePanel, after the content card. - On the assistant's route,
AppLayout.SidePanelrenders nothing, so one chat is on screen at a time.
What persists is the conversation, not the DOM. ChatSessionProvider sits above the shell and holds everything a navigation must keep:
- The messages and the answer in flight.
useChatstreams on, whichever home is mounted. - The draft, its files, and the model picks. A half-typed message is still there in the new home.
- The reader's place. The transcript passes the session's
scrollAnchorReftouseScrollAnchor. The old home writes down which text is at its top edge as it unmounts, and the new one scrolls that text back to its top edge before its first paint, at its own width. A new message drops the place. - Focus and the caret. A navigation from the keyboard, such as a shortcut, swaps the field while it has focus. The old field writes its selection as it unmounts, and the new field takes focus back with the same caret.
Put the provider above every shell the conversation must survive. A dashboard whose settings zone renders a shell of its own loses an in-memory conversation at that crossing unless the provider sits above both.
The morph
Mantle runs the morph. The app wires three things:
homeActiveonAppLayout.SidePanelRoot. Passtrueon the route where the chat is the page, andfalseeverywhere else. Compute it from the URL: mantle reads no router.AppLayout.SharedParton the parts both homes have. The demo names the title, the new conversation button, and the composer. Each one glides from where it sits in one home to where it sits in the other, at its own size, so text never stretches or blurs.- Navigation in a transition. React runs the morph only for a navigation it commits in
startTransition. React Router 7 and later do this for every link, sidebar row, and Back and Forward. A command palette that navigates through the router does too.
Mantle does the rest. AppLayout.Content and AppLayout.SidePanel render React's <ViewTransition> with a name that each AppLayout.SidePanelRoot generates, so two shells on one page never collide. mantle.css styles the morph:
- The panel is a layer over the page. Going from the page to the panel, the card shrinks toward the end edge and lands flush where the panel sits. Going back, it grows from the end edge over the page. The page beneath holds still. The moving card is opaque at every frame and carries the floating panel's shadow. In a right-to-left document the end edge is on the left.
- One clock. The card, the shared parts, and the panel's close button move for 200ms on mantle's ease-out-strong curve, the curve the panel's own slide uses. The old content fades out in the first 60ms, so two layouts of the same text never show through each other. A new navigation mid-morph interrupts it.
- The sidebar glides. When the open panel needs the sidebar's lane, the morph collapses or expands the sidebar on the same clock, above the page and flush with the card.
- The close button slides. It has no twin on the page, so it slides out past the end edge, and back in.
- A closed panel fades. With the panel closed there is nothing to pair with, so the page fades out on its own, or fades in when the reader comes back.
- Nothing else animates. A navigation between two product pages, a streaming answer, a first load, a reload, and hydration run no morph. Neither does a navigation out of the shell, such as to a sign-in page.
- Reduced motion is a cut. Under
prefers-reduced-motion: reducethe crossing swaps in one frame, and the browser's own crossfade of the page stops too, so clicks land at once. - The phone's navigation sheet closes first.
Sidebar.Rootcloses its mobile sheet on any navigation, before the router commits: a link, a command palette, Back and Forward. The sheet never covers the morph, and the shell needs no close-on-navigate effect.
The morph needs a React with <ViewTransition>. On an earlier React the parts render as before, and the crossing swaps in one frame. Leave out React Router's viewTransition prop and useViewTransitionState: the prop starts a transition of the whole document around every navigation it is on, and mantle's boundaries already decide what animates.
The route decides
The shell reads the pathname once. Here is a React Router 8 layout route, complete as written:
~/features/assistant stands for the demo's isAssistantPath, AssistantPage, AssistantPanel, and NewConversationButton, without its fake router and product pages. The assistant's route module renders AssistantPage and nothing else. A product page renders AppLayout.SidePanelTrigger in its AppLayout.HeaderActions, the way the side panel demo does.
Two rules keep the morph correct:
- Read the pathname the way the router matches it.
isAssistantPathdecodes it and ignores case and a trailing slash, so/Assistantis the assistant's route too, and the page and the panel never show together. - Render
AssistantPanelon every route.homeActivehides it on the assistant's route, so the shell needs no condition of its own.
Server rendering
Both halves of the state are available on the server, so the first paint is right and nothing corrects it:
- Which home. The URL.
isAssistantPath(pathname)is the same on the server and the client. - Whether the panel is open. The side panel cookie. Read it in the loader with
extractSidePanelStateCookie, pass it todefaultOpen, and write it back fromonOpenChangewithserializeSidePanelStateCookie, as Persisting the side panel to a cookie shows.
A document load runs no morph; the browser only snapshots a same-document navigation. The conversation itself is data, and the demo's session is in memory. A real assistant keeps the active conversation on the server and puts its id in the URL or a cookie, so a reload hydrates the same transcript.
Tablet and phone
The panel keeps one <aside> at every width, and the crossing morphs into whichever shape it takes:
- Beside the page, with the sidebar yielding. Where the row is too narrow for an expanded sidebar and the panel,
Sidebar.Rootcollapses the sidebar while the panel is open, and expands it again when the panel closes. A sidebar the reader toggled keeps their choice. - Floating, below
mobileBreakpoint. The panel is an overlay the reader can dismiss withEscapewhile focus is inside. A drag on its header toward the end edge swipes it away at every width. - Covering, one breakpoint lower. The panel spans the row and turns modal: the card and the sidebar go
inert, a focus trap wrapsTabinside it, andEscapecloses it.
Match mobileBreakpoint to Sidebar.Root's, so the sidebar and the panel change presentation at one width. The demo narrows both to md; most apps keep the lg default.
The chat
chat-view.tsx holds the session and the chat both homes render. Nothing in it knows where it renders.
The shell
assistant-demo.tsx is the two homes, the path helper, and the demo's shell. Swap its fake router for your router's Link and useLocation.
The hook
useChat owns the messages and one stream at a time. Swap respond for your server, and keep the rest.
The mock
The mock streams the same events an endpoint would: reasoning deltas, a tool call with its result, and Markdown text deltas. It honors the abort signal, so stop and a new message end it at once.
Wire it to your server
Return an async iterable of ChatEvents from respond. Read your endpoint's server-sent events, and map each one to an event. Pass the signal to fetch, so stop and unmount cancel the request. The model's request goes through the ngrok.ai gateway, which routes the inference.