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

solid.md

201 lines7.0 KB

1---
2title: Solid Chat UI
3id: typed-headless-ui-solid
4order: 2
5description: "Build a typed, headless Solid chat UI with createChatHook. Accessors stay tracked."
6keywords:
7 - tanstack ai
8 - createChatHook
9 - solid
10 - headless ui
11 - ToolProps
12---
13​
14Install `@tanstack/ai-solid`. Import the UI factory from `@tanstack/ai-solid/ui`. Call `createChatHook({ options, ...components })` once at module scope. Your app calls `useAppChat()` to create the instance. Render `<chat.AppChat />`. Do not destructure reactive props.
15​
16> **Deprecated.** Do not install `@tanstack/ai-solid-ui`. That package re-exports this subpath until 1.0.0. See [Chat UI packages](../migration/create-ui).
17​
18The 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.
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 { createSignal } from 'solid-js'
28import { fetchServerSentEvents } from '@tanstack/ai-solid'
29import { createChatHook } from '@tanstack/ai-solid/ui'
30import { toolDefinition } from '@tanstack/ai'
31import { z } from 'zod'
32​
33const getWeather = toolDefinition({
34 name: 'getWeather',
35 description: 'Look up weather',
36 inputSchema: z.object({ city: z.string() }),
37 outputSchema: z.object({ temperature: z.number() }),
38}).client()
39​
40const chatOptions = {
41 connection: fetchServerSentEvents('/api/chat'),
42 tools: [getWeather],
44​
45const { useAppChat, useChatContext } = createChatHook({
46 options: chatOptions,
47 components: {
48 layout: (props) => (
49 <>
50 <props.Messages />
51 <props.Interrupts />
52 <props.Queue />
53 <props.Input />
54 </>
55 ),
56 input: () => {
57 const chat = useChatContext()
58 const [draft, setDraft] = createSignal('')
59 return (
60 <form
61 onSubmit={(event) => {
62 event.preventDefault()
63 void chat.sendMessage(draft())
64 setDraft('')
65 }}
66 >
67 <input onInput={(event) => setDraft(event.currentTarget.value)} value={draft()} />
68 <button type="submit">Send</button>
69 </form>
70 )
71 },
72 message: (props) => <article><props.Parts /></article>,
73 },
74 partsComponents: {
75 fallback: (props) => <span>{props.part.type}</span>,
76 },
77 toolsComponents: {
78 getWeather: (props) => <strong>{props.part.input?.city}</strong>,
79 },
80})
81​
82export function Support() {
83 const chat = useAppChat({ threadId: 'support-1' })
84 return <chat.AppChat />
86```
87​
88`layout` receives `props.Messages`, `props.Interrupts`, and `props.Input` as components. `Input` is only present when the config registers an `input`.
89​
90> **List `input` before `layout`.** An inline `function` expression written *after* `layout` stops TypeScript inferring that an input is registered, and `Input` goes missing from the layout props. Putting `input` first fixes it, as does an arrow or a named reference. If it slips through, rendering `<props.Input />` without a registered `input` warns once in development.
91​
92## Type a component in its own file
93​
94Use `ToolProps` the same way as React. Keep the `props` object so Solid can track it.
95​
96```tsx
97import { fetchServerSentEvents } from '@tanstack/ai-solid'
98import { createChatHook, type ToolProps } from '@tanstack/ai-solid/ui'
99import { toolDefinition } from '@tanstack/ai'
100import { z } from 'zod'
101​
102const getWeather = toolDefinition({
103 name: 'getWeather',
104 description: 'Look up weather',
105 inputSchema: z.object({ city: z.string() }),
106 outputSchema: z.object({ temperature: z.number() }),
107}).client()
108​
109const chatOptions = {
110 connection: fetchServerSentEvents('/api/chat'),
111 tools: [getWeather],
113​
114export function WeatherTool(
115 props: ToolProps<typeof chatOptions, 'getWeather'>,
116) {
117 return <strong>{props.part.input?.city}</strong>
119​
120export const { useAppChat } = createChatHook({
121 options: chatOptions,
122 components: {
123 layout: (props) => <props.Messages />,
124 message: (props) => <article><props.Parts /></article>,
125 },
126 partsComponents: { fallback: () => null },
127 toolsComponents: { getWeather: WeatherTool },
128})
129```
130​
131Part components use `PartProps<typeof chatOptions, 'text'>`. Then `part` is already a text part.
132​
133Interrupt components use `InterruptProps<typeof chatOptions, 'choosePlan'>`. Then `interrupt.payload` matches the definition.
134​
135Mapped components do not receive `chat` as a prop. Call `useChatContext()` when a component needs live chat. That call opts the component into chat updates. Nested children can call it too.
136​
137## Read chat from `useChatContext()`
138​
139```tsx
140import { fetchServerSentEvents } from '@tanstack/ai-solid'
141import { createChatHook } from '@tanstack/ai-solid/ui'
142​
143const chatOptions = {
144 connection: fetchServerSentEvents('/api/chat'),
146​
147function StatusLine() {
148 const chat = useChatContext()
149 return <p>{chat.messages.length} messages</p>
151​
152const { useAppChat, useChatContext } = createChatHook({
153 options: chatOptions,
154 components: {
155 layout: (props) => (
156 <>
157 <StatusLine />
158 <props.Messages />
159 </>
160 ),
161 message: (props) => <article><props.Parts /></article>,
162 },
163 partsComponents: { fallback: () => null },
164})
165​
166export function ChatScreen() {
167 const chat = useAppChat()
168 return <chat.AppChat />
170```
171​
172Call `useChatContext()` only inside `AppChat` or `Provider`.
173​
174## Interrupts
175​
176Tool approvals sit in the tool when you read `props.interrupt`. 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`. Branch on `interrupt.kind === 'unbound'` if the copy must differ.
177​
178The full map is on the [React page](./react).
179​
180Manual list: `<UI.Messages>{(messages) => <span>{messages().length}</span>}</UI.Messages>`.
181​
182Pass `props.chat`, `props.part`, and `props.Parts` without destructure.
183​
184## Render markdown
185​
186Model replies arrive as markdown. `TextPart` from `@tanstack/ai-solid/ui` renders it with [TanStack Markdown](https://tanstack.com/markdown). By default, raw HTML is escaped and executable URLs are removed.
187​
188```tsx
189import { TextPart } from '@tanstack/ai-solid/ui'
190​
191export function Reply(props: { content: string }) {
192 return <TextPart content={props.content} />
194```
195​
196Fenced code stays plain until you pass a `highlighter`. Build one with TanStack Highlight: [Highlight markdown code](./markdown). A custom `highlighter` returns trusted HTML. Use `createTanStackMarkdownHighlighter`, or another highlighter that escapes source text.
197​
198- `highlighter`: a synchronous `CodeHighlighter`.
199- `extensions`: extra TanStack Markdown extensions. The streaming extension is always on.
200​
201If you still pass `remarkPlugins`, `rehypePlugins`, `disableDefaultPlugins`, or `components`, see [TextPart markdown](../migration/text-part-markdown).