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

svelte.md

105 lines4.0 KB

1---
2title: Svelte Chat UI
3id: typed-headless-ui-svelte
4order: 4
5description: "Build a typed, headless Svelte 5 chat UI with createChatHook, snippets, and static components."
6keywords:
7 - tanstack ai
8 - createChatHook
9 - svelte
10 - headless ui
11 - ToolProps
12---
13​
14Install `@tanstack/ai-svelte`. Import the UI factory from `@tanstack/ai-svelte/ui`. Call `createChatHook({ options, ...components })` once. Your app calls `createAppChat()` to create the instance. Pass `{ui}` and `{chat}` into `UIChat`.
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​
18The server route matches the [React page](./react). Use `gpt-5.6` on the OpenAI text adapter.
19​
20The [chat UI recipes](./recipes/index) show the same option groups one at a time. The code there is React, and the shape carries over.
21​
22## Client
23​
24```svelte
25<script lang="ts">
26 import { fetchServerSentEvents } from '@tanstack/ai-svelte'
27 import { createChatHook, UIChat } from '@tanstack/ai-svelte/ui'
28 import { toolDefinition } from '@tanstack/ai'
29 import { z } from 'zod'
30 import Layout from './Layout.svelte'
31 import Message from './Message.svelte'
32 import Weather from './Weather.svelte'
33 import Fallback from './Fallback.svelte'
34​
35 const getWeather = toolDefinition({
36 name: 'getWeather',
37 description: 'Look up weather',
38 inputSchema: z.object({ city: z.string() }),
39 outputSchema: z.object({ temperature: z.number() }),
40 }).client()
41​
42 const chatOptions = {
43 connection: fetchServerSentEvents('/api/chat'),
44 tools: [getWeather],
45 }
46​
47 const { createAppChat, ui } = createChatHook({
48 options: chatOptions,
49 components: {
50 layout: Layout,
51 message: Message,
52 },
53 partsComponents: { fallback: Fallback },
54 toolsComponents: { getWeather: Weather },
55 })
56 const chat = createAppChat()
57</script>
58​
59<UIChat {ui} {chat} />
60```
61​
62`Layout.svelte` receives snippets `messages`, `interrupts`, `queue`, and `input`. `{@render queue()}` draws pending sends. Call `item.cancelQueued()` on a queue item to drop it. `Message.svelte` receives snippet `parts`. A tool with an approval receives prop `interrupt`.
63​
64## Type a component in its own file
65​
66Type the `$props()` of a tool file with `ToolProps`. Share the same `chatOptions` module that you pass to `createChatHook`.
67​
68```svelte
69<script lang="ts">
70 import type { ToolProps } from '@tanstack/ai-svelte/ui'
71 import { chatOptions } from './chat-options'
72​
73 let { part }: ToolProps<typeof chatOptions, 'getWeather'> = $props()
74</script>
75​
76<strong>{part.input?.city}</strong>
77```
78​
79Part components use `PartProps<typeof chatOptions, 'text'>`. Then `part` is already a text part.
80​
81Interrupt components use `InterruptProps<typeof chatOptions, 'choosePlan'>`. Then `interrupt.payload` matches the definition.
82​
83Mapped components do not receive `chat` as a prop. Call `ui.useChatContext()` when a component needs live chat. That call opts the component into chat updates. Nested children can call it too.
84​
85## Read chat from `ui.useChatContext()`
86​
87Import the same `ui` descriptor in a child file. Call `ui.useChatContext()` only under `UIChat` or `UIProvider`.
88​
89```svelte
90<script lang="ts">
91 import { ui } from './chat-ui'
92​
93 const chat = ui.useChatContext()
94</script>
95​
96<p>{chat.messages.length} messages</p>
97```
98​
99`createChat(chatOptions)` owns the state. `ui.useChatContext()` reads the instance you passed into `UIChat`. A call outside that tree throws.
100​
101## Interrupts
102​
103Tool 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`. Branch on `interrupt.kind === 'unbound'` if the copy must differ.
104​
105The full map is on the [React page](./react).