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

vue.md

204 lines7.3 KB

1---
2title: Vue Chat UI
3id: typed-headless-ui-vue
4order: 3
5description: "Build a typed, headless Vue chat UI with createChatHook and static primitives. Slots replace render callbacks."
6keywords:
7 - tanstack ai
8 - createChatHook
9 - vue
10 - headless ui
11 - ToolProps
12---
13​
14Install `@tanstack/ai-vue`. Import the UI factory from `@tanstack/ai-vue/ui`. Call `createChatHook({ options, ...components })` once. Your app calls `useAppChat()` to create the instance. Pass the descriptor as `ui` into `UIChat`, `UIProvider`, and the other static primitives.
15​
16> **Deprecated.** Do not install `@tanstack/ai-vue-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```ts
27import { defineComponent, h } from 'vue'
28import { fetchServerSentEvents } from '@tanstack/ai-vue'
29import { createChatHook, UIChat } from '@tanstack/ai-vue/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, ui } = createChatHook({
46 options: chatOptions,
47 components: {
48 layout: defineComponent((_, { slots }) => () =>
49 h('div', [slots.messages?.(), slots.interrupts?.(), slots.queue?.(), slots.input?.()]),
50 ),
51 message: defineComponent((_, { slots }) => () => h('article', slots.parts?.())),
52 },
53 partsComponents: {
54 fallback: defineComponent({
55 props: ['part'],
56 setup(props) {
57 return () => h('span', props.part.type)
58 },
59 }),
60 },
61 toolsComponents: {
62 getWeather: defineComponent({
63 props: ['part'],
64 setup(props) {
65 return () => h('strong', props.part.input?.city)
66 },
67 }),
68 },
69})
70​
71export default defineComponent({
72 setup() {
73 const chat = useAppChat({ threadId: 'support-1' })
74 return () => h(UIChat, { ui, chat })
75 },
76})
77```
78​
79Layout uses slots `messages`, `interrupts`, `queue`, and `input`. Message uses slot `parts`. Manual lists use the default slot on `UIMessages` with `{ messages }`.
80​
81## Type a component in its own file
82​
83Use `ToolProps` on the component props. Share the same `chatOptions` object that you pass to `createChatHook`.
84​
85```ts
86import { defineComponent, h } from 'vue'
87import { fetchServerSentEvents } from '@tanstack/ai-vue'
88import { createChatHook, type ToolProps } from '@tanstack/ai-vue/ui'
89import { toolDefinition } from '@tanstack/ai'
90import { z } from 'zod'
91​
92const getWeather = toolDefinition({
93 name: 'getWeather',
94 description: 'Look up weather',
95 inputSchema: z.object({ city: z.string() }),
96 outputSchema: z.object({ temperature: z.number() }),
97}).client()
98​
99const chatOptions = {
100 connection: fetchServerSentEvents('/api/chat'),
101 tools: [getWeather],
103​
104export const WeatherTool = defineComponent(
105 (props: ToolProps<typeof chatOptions, 'getWeather'>) => {
106 return () => h('strong', props.part.input?.city)
107 },
109​
110export const { useAppChat, ui } = createChatHook({
111 options: chatOptions,
112 components: {
113 layout: defineComponent((_, { slots }) => () =>
114 h('div', [slots.messages?.(), slots.interrupts?.(), slots.input?.()]),
115 ),
116 message: defineComponent((_, { slots }) => () => h('article', slots.parts?.())),
117 },
118 partsComponents: { fallback: defineComponent(() => () => null) },
119 toolsComponents: { getWeather: WeatherTool },
120})
121```
122​
123Part components use `PartProps<typeof chatOptions, 'text'>`. Then `part` is already a text part.
124​
125Interrupt components use `InterruptProps<typeof chatOptions, 'choosePlan'>`. Then `interrupt.payload` matches the definition.
126​
127Mapped 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.
128​
129## Read chat from `ui.useChatContext()`
130​
131Call `ui.useChatContext()` inside a child of `UIChat` or `UIProvider`.
132​
133```ts
134import { defineComponent, h } from 'vue'
135import type { Component } from 'vue'
136import { fetchServerSentEvents } from '@tanstack/ai-vue'
137import { createChatHook, UIChat } from '@tanstack/ai-vue/ui'
138​
139const chatOptions = {
140 connection: fetchServerSentEvents('/api/chat'),
142​
143const { useAppChat, ui } = createChatHook({
144 options: chatOptions,
145 components: {
146 layout: defineComponent((_, { slots }) => () =>
147 h('main', [h(StatusLine), slots.messages?.()]),
148 ),
149 message: defineComponent((_, { slots }) => () => h('article', slots.parts?.())),
150 },
151 partsComponents: { fallback: defineComponent(() => () => null) },
152})
153​
154// Declared after `ui`, and annotated, so that reading `ui.useChatContext()`
155// here does not form an inference cycle with the components map that renders
156// it. `h(StatusLine)` above only runs at render time, so the order is fine.
157const StatusLine: Component = defineComponent({
158 setup() {
159 const chat = ui.useChatContext()
160 return () => {
161 const messages = Array.isArray(chat.messages) ? chat.messages : []
162 return h('p', String(messages.length) + ' messages')
163 }
164 },
165})
166​
167export default defineComponent({
168 setup() {
169 const chat = useAppChat()
170 return () => h(UIChat, { ui, chat })
171 },
172})
173```
174​
175`useChat(chatOptions)` from `@tanstack/ai-vue` owns the state. `ui.useChatContext()` reads the instance you passed into `UIChat`.
176​
177## Interrupts
178​
179Tool 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.
180​
181The full map is on the [React page](./react).
182​
183## Render markdown
184​
185Model replies arrive as markdown. `TextPart` from `@tanstack/ai-vue/ui` renders it with [TanStack Markdown](https://tanstack.com/markdown). By default, raw HTML is escaped and executable URLs are removed.
186​
187```ts
188import { defineComponent, h } from 'vue'
189import { TextPart } from '@tanstack/ai-vue/ui'
190​
191export default defineComponent({
192 props: { content: { type: String, required: true } },
193 setup(props) {
194 return () => h(TextPart, { content: props.content })
195 },
196})
197```
198​
199Fenced 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.
200​
201- `highlighter`: a synchronous `CodeHighlighter`.
202- `extensions`: extra TanStack Markdown extensions. The streaming extension is always on.
203​
204If you still pass `remarkPlugins`, `rehypePlugins`, or `disableDefaultPlugins`, see [TextPart markdown](../migration/text-part-markdown).