11<script setup lang="ts">
22import type { DefineComponent } from ' vue'
3+ import { ref , onMounted } from ' vue'
4+ import { $fetch } from ' ofetch'
35import { Chat } from ' @ai-sdk/vue'
46import { DefaultChatTransport } from ' ai'
57import type { UIMessage } from ' ai'
68import { useClipboard } from ' @vueuse/core'
79import { getTextFromMessage } from ' @nuxt/ui/utils/ai'
810import 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
1017const components = {
1118 pre: ProseStreamPre as unknown as DefineComponent
@@ -15,29 +22,30 @@ const route = useRoute()
1522const toast = useToast ()
1623const clipboard = useClipboard ()
1724const { 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
2733const input = ref (' ' )
2834
2935const 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
7684onMounted (() => {
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 >
0 commit comments