Skip to content

Commit 03ff627

Browse files
committed
feat: add mdc support and chat fetching
1 parent f04aaa6 commit 03ff627

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

src/layouts/default.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,6 @@ defineShortcuts({
171171
}, ...groups]"
172172
/>
173173

174-
<RouterView />
174+
<RouterView :key="route.path" />
175175
</UDashboardGroup>
176176
</template>

src/pages/chat/[id].vue

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
<script setup lang="ts">
22
import type { DefineComponent } from 'vue'
3+
import { ref, onMounted } from 'vue'
4+
import { $fetch } from 'ofetch'
35
import { Chat } from '@ai-sdk/vue'
46
import { DefaultChatTransport } from 'ai'
57
import type { UIMessage } from 'ai'
68
import { useClipboard } from '@vueuse/core'
79
import { getTextFromMessage } from '@nuxt/ui/utils/ai'
810
import ProseStreamPre from '../../components/prose/PreStream.vue'
11+
import { useModels } from '../../composables/useModels'
12+
import { useChats } from '../../composables/useChats'
13+
import MDCRenderer from '@nuxtjs/mdc/runtime/components/MDCRenderer.vue'
14+
import { useRoute } from 'vue-router'
15+
import { parseMarkdown } from '@nuxtjs/mdc/runtime'
916
1017
const components = {
1118
pre: ProseStreamPre as unknown as DefineComponent
@@ -15,29 +22,30 @@ const route = useRoute()
1522
const toast = useToast()
1623
const clipboard = useClipboard()
1724
const { model } = useModels()
25+
const { fetchChats } = useChats()
1826
19-
const { data } = await useFetch(`/api/chats/${route.params.id}`, {
20-
cache: 'force-cache'
21-
})
27+
const chatData = await $fetch(`/api/chats/${route.params.id}`)
2228
23-
if (!data.value) {
24-
throw createError({ statusCode: 404, statusMessage: 'Chat not found', fatal: true })
25-
}
29+
// if (!chatData) {
30+
// throw createError({ statusCode: 404, statusMessage: 'Chat not found', fatal: true })
31+
// }
2632
2733
const input = ref('')
2834
2935
const chat = new Chat({
30-
id: data.value.id,
31-
messages: data.value.messages,
36+
id: chatData.id,
37+
messages: chatData.messages,
3238
transport: new DefaultChatTransport({
33-
api: `/api/chats/${data.value.id}`,
39+
api: `/api/chats/${chatData.id}`,
3440
body: {
3541
model: model.value
3642
}
3743
}),
3844
onData: (dataPart) => {
3945
if (dataPart.type === 'data-chat-title') {
40-
refreshNuxtData('chats')
46+
fetchChats()
47+
} else {
48+
console.log(dataPart)
4149
}
4250
},
4351
onError(error) {
@@ -74,14 +82,19 @@ function copy(e: MouseEvent, message: UIMessage) {
7482
}
7583
7684
onMounted(() => {
77-
if (data.value?.messages.length === 1) {
85+
if (chatData.messages?.length === 1) {
7886
chat.regenerate()
7987
}
8088
})
8189
</script>
8290

8391
<template>
84-
<UDashboardPanel id="chat" class="relative" :ui="{ body: 'p-0 sm:p-0' }">
92+
<UDashboardPanel
93+
v-if="chatData.id"
94+
id="chat"
95+
class="relative"
96+
:ui="{ body: 'p-0 sm:p-0' }"
97+
>
8598
<template #header>
8699
<DashboardNavbar />
87100
</template>
@@ -97,7 +110,10 @@ onMounted(() => {
97110
>
98111
<template #content="{ message }">
99112
<div class="space-y-4">
100-
<template v-for="(part, index) in message.parts" :key="`${part.type}-${index}-${message.id}`">
113+
<template
114+
v-for="(part, index) in message.parts"
115+
:key="`${part.type}-${index}-${message.id}`"
116+
>
101117
<UButton
102118
v-if="part.type === 'reasoning' && part.state !== 'done'"
103119
label="Thinking..."
@@ -107,9 +123,8 @@ onMounted(() => {
107123
loading
108124
/>
109125
</template>
110-
<MDCCached
111-
:value="getTextFromMessage(message)"
112-
:cache-key="message.id"
126+
<MDCRenderer
127+
:body="getTextFromMessage(message)"
113128
unwrap="p"
114129
:components="components"
115130
:parser-options="{ highlight: false }"
@@ -139,4 +154,19 @@ onMounted(() => {
139154
</UContainer>
140155
</template>
141156
</UDashboardPanel>
157+
<UContainer
158+
v-else
159+
class="flex-1 flex flex-col gap-4 sm:gap-6"
160+
>
161+
<UError
162+
163+
:error="{ statusMessage: 'Chat not found', statusCode: 404 }"
164+
>
165+
<template #links>
166+
<UButton to="/">
167+
Back to home
168+
</UButton>
169+
</template>
170+
</UError>
171+
</UContainer>
142172
</template>

stub-mdc-imports.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// stub-mdc-imports.js
2+
export default {}

vite.config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { defineConfig } from 'vite'
2+
import path from 'path'
23
import { nitro } from 'nitro/vite'
34
import vue from '@vitejs/plugin-vue'
45
import vueRouter from 'unplugin-vue-router/vite'
@@ -27,5 +28,12 @@ export default defineConfig({
2728
}
2829
}),
2930
vueDevtools()
30-
]
31+
],
32+
// https://github.com/nuxt-content/mdc#stub-nuxt-module-imports
33+
resolve: {
34+
alias: {
35+
'#mdc-imports': path.resolve(__dirname, './stub-mdc-imports.js'),
36+
'#mdc-configs': path.resolve(__dirname, './stub-mdc-imports.js'),
37+
}
38+
}
3139
})

0 commit comments

Comments
 (0)