Files
nuxt__ui/docs/app/composables/useChat.ts
Mike Newbon 5fd94e1365 docs: add theme editor (#6675)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 14:55:51 +02:00

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
}
})