mirror of
https://github.com/nuxt/ui.git
synced 2026-09-14 19:51:10 +08:00
5fd94e1365
Co-authored-by: Claude Fable 5 <noreply@anthropic.com> Co-authored-by: Benjamin Canac <canacb1@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com>
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import { createSharedComposable, useLocalStorage } from '@vueuse/core'
|
|
import type { DocsChatMessage } from '~~/server/api/ai.post'
|
|
|
|
export const useChat = createSharedComposable(() => {
|
|
const storageOpen = useLocalStorage('chat-open', false)
|
|
const messages = useLocalStorage<DocsChatMessage[]>('chat-messages', [])
|
|
|
|
const open = ref(false)
|
|
|
|
onNuxtReady(() => {
|
|
nextTick(() => {
|
|
open.value = storageOpen.value
|
|
})
|
|
})
|
|
|
|
watch(open, (value) => {
|
|
storageOpen.value = value
|
|
})
|
|
|
|
// Set by the flows that hand the panel a question before it exists (the
|
|
// search palette, "Explain with AI"): the panel mounts on its first open,
|
|
// so its own sync watcher is not there to send it. The panel clears it.
|
|
const pending = ref(false)
|
|
|
|
function ask(text: string) {
|
|
messages.value = [...messages.value, {
|
|
id: String(Date.now()),
|
|
role: 'user',
|
|
parts: [{ type: 'text', text }]
|
|
}]
|
|
pending.value = true
|
|
open.value = true
|
|
}
|
|
|
|
return {
|
|
open,
|
|
messages,
|
|
pending,
|
|
ask
|
|
}
|
|
})
|