Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

remix.md

148 lines5.1 KB

1---
2title: Remix Chat UI
3id: typed-headless-ui-remix
4order: 7
5description: "Build a typed, headless Remix 3 chat UI with createChatHook. Widgets are Remix setup functions."
6keywords:
7 - tanstack ai
8 - createChatHook
9 - remix
10 - headless ui
11 - ToolProps
12---
13​
14Install `@tanstack/ai-remix`. Import the UI factory from `@tanstack/ai-remix/ui`. Call `createChatHook({ options, ...components })` once at module scope. Your app calls `createAppChat(handle)` in a Remix setup function. Render `<ui.Chat chat={chat} />`.
15​
16The factory needs a `toolsComponents` entry for every tool name in `chatOptions`. It also needs an `interruptsComponents.generic` entry for every interrupt id. `generic.fallback` is optional. Widgets go in `components`, `partsComponents`, `toolsComponents`, and `interruptsComponents`, the same way Form and Table register components.
17​
18Each widget is a Remix setup function. Read props from `handle.props`. If a widget needs live chat, call `useChatContext(handle)` in setup.
19​
20The server route matches the [React page](./react). Use `gpt-5.6` on the OpenAI text adapter.
21​
22The [chat UI recipes](./recipes/index) show the same option groups one at a time. The code there is React, and the shape carries over.
23​
24## Client
25​
26```tsx
27import { fetchServerSentEvents } from '@tanstack/ai-remix'
28import {
29 createChatHook,
30 type LayoutProps,
31 type MessageProps,
32 type PartProps,
33 type ToolProps,
34} from '@tanstack/ai-remix/ui'
35import { toolDefinition } from '@tanstack/ai'
36import { clientEntry, type Handle } from 'remix/ui'
37import { z } from 'zod'
38​
39const getWeather = toolDefinition({
40 name: 'getWeather',
41 description: 'Look up weather',
42 inputSchema: z.object({ city: z.string() }),
43 outputSchema: z.object({ temperature: z.number() }),
44}).client()
45​
46const chatOptions = {
47 connection: fetchServerSentEvents('/api/chat'),
48 tools: [getWeather],
50​
51const { createAppChat, ui } = createChatHook({
52 options: chatOptions,
53 components: {
54 layout(handle: Handle<LayoutProps<typeof chatOptions>>) {
55 return () => {
56 const { Messages, Interrupts, Queue, Input } = handle.props
57 return (
58 <main>
59 <Messages />
60 <Interrupts />
61 <Queue />
62 <Input />
63 </main>
64 )
65 }
66 },
67 message(handle: Handle<MessageProps<typeof chatOptions>>) {
68 return () => {
69 const { Parts } = handle.props
70 return (
71 <article>
72 <Parts />
73 </article>
74 )
75 }
76 },
77 },
78 partsComponents: {
79 fallback(handle: Handle<PartProps<typeof chatOptions>>) {
80 return () => <span>{handle.props.part.type}</span>
81 },
82 },
83 toolsComponents: {
84 getWeather(handle: Handle<ToolProps<typeof chatOptions, 'getWeather'>>) {
85 return () => <strong>{handle.props.part.input?.city}</strong>
86 },
87 },
88})
89​
90export const ChatScreen = clientEntry(
91 import.meta.url,
92 function ChatScreen(handle: Handle) {
93 const chat = createAppChat(handle)
94 return () => <ui.Chat chat={chat} />
95 },
97```
98​
99`layout` receives these components. Render them as tags, not as calls:
100​
101- `Messages`
102- `Interrupts`
103- `Queue`
104- `Input`
105​
106Register `input` on `components` to draw the composer. Until then, `Input` renders nothing. Register `queue` on `components` to draw pending sends. Call `item.cancelQueued()` on a queue item to drop it. `message` receives `Parts`. A tool with an approval receives prop `interrupt`.
107​
108## Type a component in its own file
109​
110Type the `handle.props` of a tool file with `ToolProps`. Share the same `chatOptions` module that you pass to `createChatHook`.
111​
112```tsx
113import type { ToolProps } from '@tanstack/ai-remix/ui'
114import type { Handle } from 'remix/ui'
115import { chatOptions } from './chat-options'
116​
117export function getWeather(handle: Handle<ToolProps<typeof chatOptions, 'getWeather'>>) {
118 return () => <strong>{handle.props.part.input?.city}</strong>
120```
121​
122Part components use `PartProps<typeof chatOptions, 'text'>`. Then `part` is already a text part.
123​
124Interrupt components use `InterruptProps<typeof chatOptions, 'choosePlan'>`. Then `interrupt.payload` matches the definition.
125​
126Mapped components do not receive `chat` as a prop. If a component needs live chat, call `ui.useChatContext(handle)`.
127​
128## Read chat from `ui.useChatContext(handle)`
129​
130Import the same `ui` kit in a child file. Call `ui.useChatContext(handle)` only under `ui.Chat` or `ui.Provider`. Call it in setup, not in the render function.
131​
132```tsx
133import { ui } from './chat-ui'
134import type { Handle } from 'remix/ui'
135​
136function MessageCount(handle: Handle) {
137 const chat = ui.useChatContext(handle)
138 return () => <p>{chat.messages.length} messages</p>
140```
141​
142`createAppChat(handle)` owns the state. `ui.useChatContext(handle)` reads the instance you passed into `ui.Chat`. A call outside that tree throws.
143​
144## Interrupts
145​
146Tool approvals sit in the tool when you read the `interrupt` prop. Put a component on `interruptsComponents.tools` to send that approval to the list instead. Generic interrupts always sit in the list under `interruptsComponents.generic`: `{ choosePlan, fallback }`. An unbound interrupt uses `fallback`. If the copy must differ, branch on `interrupt.kind === 'unbound'`.
147​
148The full map is on the [React page](./react).