mirror of
https://github.com/nuxt/ui.git
synced 2026-09-14 19:51:10 +08:00
86 lines
1.9 KiB
Vue
86 lines
1.9 KiB
Vue
<script setup lang="ts">
|
|
import { Chat } from '@ai-sdk/vue'
|
|
import type { UIMessage } from 'ai'
|
|
import { getTextFromMessage } from '@nuxt/ui/utils/ai'
|
|
|
|
const messages: UIMessage[] = []
|
|
const input = ref('')
|
|
|
|
const groups = computed(() => [{
|
|
id: 'ai',
|
|
ignoreFilter: true,
|
|
items: [{
|
|
label: searchTerm.value ? `Ask AI for “${searchTerm.value}”` : 'Ask AI',
|
|
icon: 'i-lucide-bot',
|
|
onSelect: (e: any) => {
|
|
e.preventDefault()
|
|
|
|
ai.value = true
|
|
|
|
if (searchTerm.value) {
|
|
messages.push({
|
|
id: '1',
|
|
role: 'user',
|
|
parts: [{ type: 'text', text: searchTerm.value }]
|
|
})
|
|
|
|
chat.regenerate()
|
|
}
|
|
}
|
|
}]
|
|
}])
|
|
|
|
const ai = ref(false)
|
|
const searchTerm = ref('')
|
|
|
|
const chat = new Chat({
|
|
messages
|
|
})
|
|
|
|
function onSubmit() {
|
|
chat.sendMessage({ text: input.value })
|
|
|
|
input.value = ''
|
|
}
|
|
|
|
function onClose(e: Event) {
|
|
e.preventDefault()
|
|
|
|
ai.value = false
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<UContentSearch v-model:search-term="searchTerm" open :groups="groups">
|
|
<template v-if="ai" #content>
|
|
<UChatPalette>
|
|
<UChatMessages
|
|
:messages="chat.messages"
|
|
:status="chat.status"
|
|
:user="{ side: 'left', variant: 'naked', avatar: { src: 'https://github.com/benjamincanac.png' } }"
|
|
:assistant="{ icon: 'i-lucide-bot' }"
|
|
>
|
|
<template #content="{ message }">
|
|
<MDC
|
|
:value="getTextFromMessage(message)"
|
|
:cache-key="message.id"
|
|
class="[&_.my-5]:my-2.5 *:first:!mt-0 *:last:!mb-0 [&_.leading-7]:!leading-6"
|
|
/>
|
|
</template>
|
|
</UChatMessages>
|
|
|
|
<template #prompt>
|
|
<UChatPrompt
|
|
v-model="input"
|
|
icon="i-lucide-search"
|
|
variant="naked"
|
|
:error="chat.error"
|
|
@submit="onSubmit"
|
|
@close="onClose"
|
|
/>
|
|
</template>
|
|
</UChatPalette>
|
|
</template>
|
|
</UContentSearch>
|
|
</template>
|