|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed, nextTick, ref } from 'vue' |
| 3 | +
|
| 4 | +type ChatMessage = { |
| 5 | + id: string |
| 6 | + role: 'user' | 'assistant' |
| 7 | + parts: { type: 'text'; text: string }[] |
| 8 | +} |
| 9 | +
|
| 10 | +const api = import.meta.env.VITE_DOCS_CHAT_API || '' |
| 11 | +const title = 'NotionNext AI 助手 v2026.07.29' |
| 12 | +const welcome = |
| 13 | + '你好,我是 NotionNext 文档助手。你可以问我部署、主题、Notion 配置、评论插件和常见排错问题。' |
| 14 | +
|
| 15 | +const open = ref(false) |
| 16 | +const input = ref('') |
| 17 | +const loading = ref(false) |
| 18 | +const messagesEl = ref<HTMLElement | null>(null) |
| 19 | +const messages = ref<ChatMessage[]>([makeMessage('assistant', welcome)]) |
| 20 | +
|
| 21 | +const canSend = computed(() => input.value.trim().length > 0 && !loading.value) |
| 22 | +
|
| 23 | +function makeMessage(role: ChatMessage['role'], text: string): ChatMessage { |
| 24 | + return { |
| 25 | + id: `${role}-${Date.now()}-${Math.random().toString(16).slice(2)}`, |
| 26 | + role, |
| 27 | + parts: [{ type: 'text', text }] |
| 28 | + } |
| 29 | +} |
| 30 | +
|
| 31 | +function textOf(message: ChatMessage) { |
| 32 | + return message.parts.map(part => part.text).join('') |
| 33 | +} |
| 34 | +
|
| 35 | +async function scrollToBottom() { |
| 36 | + await nextTick() |
| 37 | + if (messagesEl.value) { |
| 38 | + messagesEl.value.scrollTop = messagesEl.value.scrollHeight |
| 39 | + } |
| 40 | +} |
| 41 | +
|
| 42 | +async function ask() { |
| 43 | + const text = input.value.trim() |
| 44 | + if (!text || loading.value) return |
| 45 | +
|
| 46 | + const nextMessages = [...messages.value, makeMessage('user', text)] |
| 47 | + messages.value = nextMessages |
| 48 | + input.value = '' |
| 49 | + loading.value = true |
| 50 | + await scrollToBottom() |
| 51 | +
|
| 52 | + try { |
| 53 | + const separator = api.includes('?') ? '&' : '?' |
| 54 | + const response = await fetch(`${api}${separator}stream=false`, { |
| 55 | + method: 'POST', |
| 56 | + headers: { 'content-type': 'application/json' }, |
| 57 | + body: JSON.stringify({ messages: nextMessages.slice(-6) }) |
| 58 | + }) |
| 59 | + const data = await response.json().catch(() => ({})) |
| 60 | + const reply = response.ok ? data.text : data.error |
| 61 | +
|
| 62 | + messages.value = [ |
| 63 | + ...nextMessages, |
| 64 | + makeMessage('assistant', reply || '请求失败,请稍后再试。') |
| 65 | + ] |
| 66 | + } catch { |
| 67 | + messages.value = [...nextMessages, makeMessage('assistant', '网络请求失败,请稍后再试。')] |
| 68 | + } finally { |
| 69 | + loading.value = false |
| 70 | + await scrollToBottom() |
| 71 | + } |
| 72 | +} |
| 73 | +</script> |
| 74 | + |
| 75 | +<template> |
| 76 | + <div v-if="api" class="docs-assistant"> |
| 77 | + <section v-if="open" class="docs-assistant-panel" :aria-label="title"> |
| 78 | + <header class="docs-assistant-header"> |
| 79 | + <strong>{{ title }}</strong> |
| 80 | + <button type="button" aria-label="关闭 AI 助手" @click="open = false">×</button> |
| 81 | + </header> |
| 82 | + |
| 83 | + <div ref="messagesEl" class="docs-assistant-messages"> |
| 84 | + <p |
| 85 | + v-for="message in messages" |
| 86 | + :key="message.id" |
| 87 | + class="docs-assistant-message" |
| 88 | + :class="message.role" |
| 89 | + > |
| 90 | + {{ textOf(message) }} |
| 91 | + </p> |
| 92 | + <p v-if="loading" class="docs-assistant-message assistant">正在思考...</p> |
| 93 | + </div> |
| 94 | + |
| 95 | + <form class="docs-assistant-form" @submit.prevent="ask"> |
| 96 | + <textarea |
| 97 | + v-model="input" |
| 98 | + maxlength="1000" |
| 99 | + rows="2" |
| 100 | + placeholder="输入你的问题" |
| 101 | + @keydown.enter.exact.prevent="ask" |
| 102 | + /> |
| 103 | + <button type="submit" :disabled="!canSend" aria-label="发送">↑</button> |
| 104 | + </form> |
| 105 | + </section> |
| 106 | + |
| 107 | + <button class="docs-assistant-fab" type="button" @click="open = true">AI 助手</button> |
| 108 | + </div> |
| 109 | +</template> |
| 110 | + |
| 111 | +<style scoped> |
| 112 | +.docs-assistant { |
| 113 | + position: fixed; |
| 114 | + right: 22px; |
| 115 | + bottom: 22px; |
| 116 | + z-index: 50; |
| 117 | + font-size: 14px; |
| 118 | +} |
| 119 | +
|
| 120 | +.docs-assistant-fab { |
| 121 | + border: 1px solid color-mix(in srgb, var(--vp-c-brand-1) 34%, transparent); |
| 122 | + border-radius: 999px; |
| 123 | + padding: 11px 18px; |
| 124 | + color: #fff; |
| 125 | + background: linear-gradient(135deg, #0f766e, #2563eb); |
| 126 | + box-shadow: 0 16px 44px color-mix(in srgb, #0f766e 34%, transparent); |
| 127 | + font-weight: 700; |
| 128 | +} |
| 129 | +
|
| 130 | +.docs-assistant-panel { |
| 131 | + display: flex; |
| 132 | + flex-direction: column; |
| 133 | + width: min(420px, calc(100vw - 28px)); |
| 134 | + height: min(560px, calc(100vh - 92px)); |
| 135 | + margin-bottom: 12px; |
| 136 | + overflow: hidden; |
| 137 | + border: 1px solid color-mix(in srgb, var(--vp-c-divider) 70%, transparent); |
| 138 | + border-radius: 16px; |
| 139 | + background: color-mix(in srgb, var(--vp-c-bg) 96%, transparent); |
| 140 | + box-shadow: 0 24px 70px color-mix(in srgb, var(--vp-c-text-1) 18%, transparent); |
| 141 | + backdrop-filter: blur(16px); |
| 142 | +} |
| 143 | +
|
| 144 | +.docs-assistant-header { |
| 145 | + display: flex; |
| 146 | + align-items: center; |
| 147 | + justify-content: space-between; |
| 148 | + padding: 14px 16px; |
| 149 | + border-bottom: 1px solid color-mix(in srgb, var(--vp-c-divider) 70%, transparent); |
| 150 | + color: var(--vp-c-text-1); |
| 151 | +} |
| 152 | +
|
| 153 | +.docs-assistant-header button { |
| 154 | + display: grid; |
| 155 | + width: 32px; |
| 156 | + height: 32px; |
| 157 | + place-items: center; |
| 158 | + border: 0; |
| 159 | + border-radius: 999px; |
| 160 | + color: var(--vp-c-text-2); |
| 161 | + background: var(--vp-c-bg-soft); |
| 162 | + font-size: 22px; |
| 163 | + line-height: 1; |
| 164 | +} |
| 165 | +
|
| 166 | +.docs-assistant-messages { |
| 167 | + display: flex; |
| 168 | + flex: 1; |
| 169 | + flex-direction: column; |
| 170 | + gap: 10px; |
| 171 | + min-height: 0; |
| 172 | + padding: 14px; |
| 173 | + overflow-y: auto; |
| 174 | + background: linear-gradient(180deg, var(--vp-c-bg), var(--vp-c-bg-soft)); |
| 175 | +} |
| 176 | +
|
| 177 | +.docs-assistant-message { |
| 178 | + max-width: 88%; |
| 179 | + margin: 0; |
| 180 | + padding: 10px 12px; |
| 181 | + border-radius: 14px; |
| 182 | + white-space: pre-wrap; |
| 183 | + overflow-wrap: anywhere; |
| 184 | + line-height: 1.7; |
| 185 | +} |
| 186 | +
|
| 187 | +.docs-assistant-message.user { |
| 188 | + align-self: flex-end; |
| 189 | + border-bottom-right-radius: 4px; |
| 190 | + color: #fff; |
| 191 | + background: linear-gradient(135deg, #2563eb, #0f766e); |
| 192 | +} |
| 193 | +
|
| 194 | +.docs-assistant-message.assistant { |
| 195 | + align-self: flex-start; |
| 196 | + border: 1px solid color-mix(in srgb, var(--vp-c-divider) 70%, transparent); |
| 197 | + border-bottom-left-radius: 4px; |
| 198 | + color: var(--vp-c-text-1); |
| 199 | + background: var(--vp-c-bg); |
| 200 | +} |
| 201 | +
|
| 202 | +.docs-assistant-form { |
| 203 | + position: relative; |
| 204 | + padding: 12px; |
| 205 | + border-top: 1px solid color-mix(in srgb, var(--vp-c-divider) 70%, transparent); |
| 206 | + background: var(--vp-c-bg); |
| 207 | +} |
| 208 | +
|
| 209 | +.docs-assistant-form textarea { |
| 210 | + width: 100%; |
| 211 | + min-height: 58px; |
| 212 | + resize: none; |
| 213 | + border: 1px solid color-mix(in srgb, var(--vp-c-divider) 80%, transparent); |
| 214 | + border-radius: 12px; |
| 215 | + padding: 10px 48px 10px 12px; |
| 216 | + outline: none; |
| 217 | + color: var(--vp-c-text-1); |
| 218 | + background: var(--vp-c-bg-soft); |
| 219 | + font: inherit; |
| 220 | +} |
| 221 | +
|
| 222 | +.docs-assistant-form textarea:focus { |
| 223 | + border-color: #0f766e; |
| 224 | + background: var(--vp-c-bg); |
| 225 | +} |
| 226 | +
|
| 227 | +.docs-assistant-form button { |
| 228 | + position: absolute; |
| 229 | + right: 22px; |
| 230 | + bottom: 22px; |
| 231 | + width: 34px; |
| 232 | + height: 34px; |
| 233 | + border: 0; |
| 234 | + border-radius: 999px; |
| 235 | + color: #fff; |
| 236 | + background: #0f766e; |
| 237 | + font-size: 20px; |
| 238 | + font-weight: 800; |
| 239 | +} |
| 240 | +
|
| 241 | +button { |
| 242 | + cursor: pointer; |
| 243 | +} |
| 244 | +
|
| 245 | +button:disabled { |
| 246 | + cursor: not-allowed; |
| 247 | + opacity: 0.45; |
| 248 | +} |
| 249 | +
|
| 250 | +@media (max-width: 640px) { |
| 251 | + .docs-assistant { |
| 252 | + right: 14px; |
| 253 | + bottom: 14px; |
| 254 | + } |
| 255 | +} |
| 256 | +</style> |
0 commit comments