Skip to content

Commit 17cd840

Browse files
feat(components)!: upgrade ai-sdk to v5 (#4698)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
1 parent b971e42 commit 17cd840

20 files changed

+377
-257
lines changed

docs/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
NUXT_PUBLIC_SITE_URL=
33
# Used to fetch `nuxt/ui-pro` docs content
44
NUXT_GITHUB_TOKEN=
5+
# Vercel AI gateway key
6+
AI_GATEWAY_API_KEY=

docs/app/components/content/examples/chat/ChatPaletteContentSearchExample.vue

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<script setup lang="ts">
2-
import { useChat } from '@ai-sdk/vue'
2+
import { Chat } from '@ai-sdk/vue'
3+
import type { UIMessage } from 'ai'
4+
import { getTextFromMessage } from '@nuxt/ui/utils/ai'
5+
6+
const messages: UIMessage[] = []
7+
const input = ref('')
38
49
const groups = computed(() => [{
510
id: 'ai',
@@ -13,13 +18,13 @@ const groups = computed(() => [{
1318
ai.value = true
1419
1520
if (searchTerm.value) {
16-
setMessages([{
21+
messages.push({
1722
id: '1',
1823
role: 'user',
19-
content: searchTerm.value
20-
}])
24+
parts: [{ type: 'text', text: searchTerm.value }]
25+
})
2126
22-
reload()
27+
chat.regenerate()
2328
}
2429
}
2530
}]
@@ -28,7 +33,15 @@ const groups = computed(() => [{
2833
const ai = ref(false)
2934
const searchTerm = ref('')
3035
31-
const { messages, input, handleSubmit, status, error, reload, setMessages } = useChat()
36+
const chat = new Chat({
37+
messages
38+
})
39+
40+
function handleSubmit(e: Event) {
41+
e.preventDefault()
42+
chat.sendMessage({ text: input.value })
43+
input.value = ''
44+
}
3245
3346
function onClose(e: Event) {
3447
e.preventDefault()
@@ -43,13 +56,13 @@ function onClose(e: Event) {
4356
<template v-if="ai" #content>
4457
<UChatPalette>
4558
<UChatMessages
46-
:messages="messages"
47-
:status="status"
59+
:messages="chat.messages"
60+
:status="chat.status"
4861
:user="{ side: 'left', variant: 'naked', avatar: { src: 'https://github.com/benjamincanac.png' } }"
4962
:assistant="{ icon: 'i-lucide-bot' }"
5063
>
5164
<template #content="{ message }">
52-
<MDC :value="message.content" :cache-key="message.id" unwrap="p" />
65+
<MDC :value="getTextFromMessage(message)" :cache-key="message.id" unwrap="p" />
5366
</template>
5467
</UChatMessages>
5568

@@ -58,7 +71,7 @@ function onClose(e: Event) {
5871
v-model="input"
5972
icon="i-lucide-search"
6073
variant="naked"
61-
:error="error"
74+
:error="chat.error"
6275
@submit="handleSubmit"
6376
@close="onClose"
6477
/>

docs/app/components/content/examples/chat/ChatPaletteModalExample.vue

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
<script setup lang="ts">
2-
import { useChat } from '@ai-sdk/vue'
2+
import { Chat } from '@ai-sdk/vue'
3+
import type { UIMessage } from 'ai'
4+
import { getTextFromMessage } from '@nuxt/ui/utils/ai'
35
4-
const { messages, input, handleSubmit, status, error } = useChat()
6+
const messages: UIMessage[] = []
7+
const input = ref('')
8+
9+
const chat = new Chat({
10+
messages
11+
})
12+
13+
function handleSubmit(e: Event) {
14+
e.preventDefault()
15+
chat.sendMessage({ text: input.value })
16+
input.value = ''
17+
}
518
</script>
619

720
<template>
821
<UModal open :ui="{ content: 'sm:h-[28rem]' }">
922
<template #content>
1023
<UChatPalette>
1124
<UChatMessages
12-
:messages="messages"
13-
:status="status"
25+
:messages="chat.messages"
26+
:status="chat.status"
1427
:user="{ side: 'left', variant: 'naked', avatar: { src: 'https://github.com/benjamincanac.png' } }"
1528
:assistant="{ icon: 'i-lucide-bot' }"
1629
>
1730
<template #content="{ message }">
18-
<MDC :value="message.content" :cache-key="message.id" unwrap="p" />
31+
<MDC :value="getTextFromMessage(message)" :cache-key="message.id" unwrap="p" />
1932
</template>
2033
</UChatMessages>
2134

@@ -24,7 +37,7 @@ const { messages, input, handleSubmit, status, error } = useChat()
2437
v-model="input"
2538
icon="i-lucide-search"
2639
variant="naked"
27-
:error="error"
40+
:error="chat.error"
2841
@submit="handleSubmit"
2942
/>
3043
</template>

docs/content/docs/2.components/chat-message.md

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ The ChatMessage component renders an `<article>` element for a `user` or `assist
1616

1717
::u-chat-message
1818
---
19-
content: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
19+
parts:
20+
- type: 'text'
21+
text: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
2022
side: 'right'
2123
variant: 'soft'
2224
avatar:
@@ -30,18 +32,26 @@ avatar:
3032
Use the [`ChatMessages`](/docs/components/chat-messages) component to display a list of chat messages.
3133
::
3234

33-
### Content
35+
### Parts
3436

35-
Use the `content` prop to display the message content.
37+
Use the `parts` prop to display the message content using the AI SDK v5 format.
3638

3739
::component-code
3840
---
3941
prettier: true
42+
ignore:
43+
- parts
4044
props:
41-
content: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
45+
parts:
46+
- type: 'text'
47+
text: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
4248
---
4349
::
4450

51+
::note
52+
The `parts` prop is the recommended format for AI SDK v5. Each part has a `type` (e.g., 'text') and corresponding content. The ChatMessage component also supports the deprecated `content` prop for backward compatibility.
53+
::
54+
4555
### Side
4656

4757
Use the `side` prop to display the message on the left or right.
@@ -50,10 +60,12 @@ Use the `side` prop to display the message on the left or right.
5060
---
5161
prettier: true
5262
ignore:
53-
- content
63+
- parts
5464
props:
5565
side: 'right'
56-
content: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
66+
parts:
67+
- type: 'text'
68+
text: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
5769
---
5870
::
5971

@@ -69,10 +81,12 @@ Use the `variant` prop to change style of the message.
6981
---
7082
prettier: true
7183
ignore:
72-
- content
84+
- parts
7385
props:
7486
variant: 'soft'
75-
content: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
87+
parts:
88+
- type: 'text'
89+
text: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
7690
---
7791
::
7892

@@ -88,14 +102,16 @@ Use the `icon` prop to display an [Icon](/docs/components/icon) component next t
88102
---
89103
prettier: true
90104
ignore:
91-
- content
105+
- parts
92106
- side
93107
- variant
94108
props:
95109
icon: i-lucide-user
96110
variant: 'soft'
97111
side: 'right'
98-
content: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
112+
parts:
113+
- type: 'text'
114+
text: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
99115
---
100116
::
101117

@@ -107,15 +123,17 @@ Use the `avatar` prop to display an [Avatar](/docs/components/avatar) component
107123
---
108124
prettier: true
109125
ignore:
110-
- content
126+
- parts
111127
- side
112128
- variant
113129
props:
114130
avatar:
115131
src: 'https://github.com/benjamincanac.png'
116132
variant: 'soft'
117133
side: 'right'
118-
content: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
134+
parts:
135+
- type: 'text'
136+
text: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
119137
---
120138
::
121139

@@ -125,11 +143,13 @@ You can also use the `avatar.icon` prop to display an icon as the avatar.
125143
---
126144
prettier: true
127145
ignore:
128-
- content
146+
- parts
129147
props:
130148
avatar:
131149
icon: i-lucide-bot
132-
content: 'Nuxt UI offers several features for building AI chatbots including the ChatMessage, ChatMessages, and ChatPrompt components. Best practices include using the useChat composable from Vercel AI SDK, implementing proper message styling with variants, and utilizing the built-in actions for message interactions. The components are fully customizable with theming support and responsive design.'
150+
parts:
151+
- type: 'text'
152+
text: 'Nuxt UI offers several features for building AI chatbots including the ChatMessage, ChatMessages, and ChatPrompt components. Best practices include using the Chat class from AI SDK v5, implementing proper message styling with variants, and utilizing the built-in actions for message interactions. The components are fully customizable with theming support and responsive design.'
133153
---
134154
::
135155

@@ -143,13 +163,15 @@ prettier: true
143163
external:
144164
- actions
145165
ignore:
146-
- content
166+
- parts
147167
- actions
148168
props:
149169
actions:
150170
- label: 'Copy to clipboard'
151171
icon: i-lucide-copy
152-
content: 'Nuxt UI offers several features for building AI chatbots including the ChatMessage, ChatMessages, and ChatPrompt components. Best practices include using the useChat composable from Vercel AI SDK, implementing proper message styling with variants, and utilizing the built-in actions for message interactions. The components are fully customizable with theming support and responsive design.'
172+
parts:
173+
- type: 'text'
174+
text: 'Nuxt UI offers several features for building AI chatbots including the ChatMessage, ChatMessages, and ChatPrompt components. Best practices include using the Chat class from AI SDK v5, implementing proper message styling with variants, and utilizing the built-in actions for message interactions. The components are fully customizable with theming support and responsive design.'
153175
---
154176
::
155177

0 commit comments

Comments
 (0)