完成基本功能

This commit is contained in:
2025-02-25 00:30:24 +08:00
commit 57ecdb62fa
19 changed files with 4131 additions and 0 deletions

345
client/src/App.vue Normal file
View File

@@ -0,0 +1,345 @@
<script setup>
import {ref} from 'vue'
import MarkdownIt from 'markdown-it'
import MarkDownMermaidPlugin from 'mermaid-it-markdown'
import {fetchEventSource} from '@microsoft/fetch-event-source'
// 初始化 markdown-it 实例
const md = new MarkdownIt({
html: true, // 启用 HTML 标签
breaks: true, // 转换换行符为 <br>
linkify: true, // 自动转换 URL 为链接
typographer: true // 启用一些语言中性的替换 + 引号美化
})
md.use(MarkDownMermaidPlugin)
const question = ref('')
const answer = ref('')
const loading = ref(false)
// 使用计算属性来渲染 markdown
const renderedAnswer = ref('')
answer.value = "```mermaid\ngraph TD;\n A-->B;\n A-->C;\n B-->D;\n C-->D;\n```"
renderedAnswer.value = md.render(answer.value)
const handleSubmit = async () => {
if (!question.value.trim() || loading.value) return
loading.value = true
answer.value = ''
renderedAnswer.value = ''
try {
await fetchEventSource('http://localhost:7891/chat/stream', {
method: 'POST',
body: question.value.trim(),
onmessage(event) {
// 处理每个消息事件
answer.value += event.data
// 实时渲染 markdown
renderedAnswer.value = md.render(answer.value)
},
onclose() {
loading.value = false
console.log(answer.value)
},
onerror(err) {
console.error('请求出错:', err)
loading.value = false
return false // 不进行重试
}
})
} catch (error) {
console.error('请求出错:', error)
answer.value = '抱歉,请求出现错误'
renderedAnswer.value = answer.value
loading.value = false
}
}
</script>
<template>
<div class="chat-container">
<div class="chat-header">
<h1>AI 助手</h1>
</div>
<div class="messages-container">
<div class="answer-area">
<div
v-if="renderedAnswer"
class="markdown-body"
v-html="renderedAnswer"
/>
<div v-else class="placeholder">
<template v-if="loading">
<div class="thinking">
<span>思考中</span>
<span class="thinking-spinner"></span>
</div>
</template>
<template v-else>
在下方输入您的问题AI 助手会为您解答...
</template>
</div>
</div>
</div>
<div class="input-area">
<div class="input-container">
<textarea
v-model="question"
placeholder="请输入您的问题... (Ctrl + Enter 快速发送)"
@keyup.ctrl.enter="handleSubmit"
></textarea>
<button
:disabled="loading"
class="send-button"
@click="handleSubmit"
>
<span v-if="loading" class="loading-spinner"></span>
<span>{{ loading ? '思考中...' : '发送' }}</span>
</button>
</div>
</div>
</div>
</template>
<style scoped>
.chat-container {
margin: 0;
height: 100vh;
display: flex;
flex-direction: column;
background-color: #fafafa; /* 更柔和的背景色 */
}
.chat-header {
padding: 20px 32px;
background: linear-gradient(to right, #2563eb, #3b82f6); /* 渐变背景 */
box-shadow: 0 2px 12px rgba(37, 99, 235, 0.15);
position: sticky;
top: 0;
z-index: 10;
}
.chat-header h1 {
margin: 0;
font-size: 1.5rem;
color: #ffffff;
font-weight: 600;
letter-spacing: -0.5px;
}
.messages-container {
flex: 1;
overflow-y: auto;
padding: 32px;
background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%); /* 渐变背景 */
scroll-behavior: smooth;
}
.answer-area {
max-width: 1200px;
margin: 0 auto;
background-color: #ffffff;
border-radius: 20px;
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
padding: 40px;
min-height: 200px;
transition: all 0.3s ease;
border: 1px solid rgba(0, 0, 0, 0.05);
}
.placeholder {
color: #94a3b8;
font-size: 1.1rem;
text-align: center;
margin-top: 80px;
line-height: 1.6;
font-weight: 500;
}
.input-area {
padding: 24px 32px;
background: rgba(255, 255, 255, 0.9);
backdrop-filter: blur(20px);
border-top: 1px solid rgba(0, 0, 0, 0.06);
position: sticky;
bottom: 0;
box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.05);
}
.input-container {
max-width: 1200px;
margin: 0 auto;
width: 100%;
display: flex;
gap: 20px;
}
textarea {
flex: 1;
min-height: 64px;
max-height: 200px;
padding: 18px 24px;
border: 2px solid #e2e8f0;
border-radius: 16px;
resize: none; /* 禁用调整大小功能 */
font-size: 1rem;
line-height: 1.6;
transition: all 0.2s ease;
background-color: #ffffff;
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.03);
}
textarea:focus {
outline: none;
border-color: #3b82f6;
box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.15);
}
textarea::placeholder {
color: #94a3b8;
font-weight: 500;
}
.send-button {
padding: 0 32px;
background: linear-gradient(135deg, #2563eb 0%, #3b82f6 100%);
color: white;
border: none;
border-radius: 16px;
cursor: pointer;
font-weight: 600;
font-size: 1rem;
display: flex;
align-items: center;
gap: 10px;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
box-shadow: 0 4px 12px rgba(37, 99, 235, 0.2);
}
.send-button:hover:not(:disabled) {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(37, 99, 235, 0.25);
background: linear-gradient(135deg, #1d4ed8 0%, #2563eb 100%);
}
.send-button:active:not(:disabled) {
transform: translateY(0);
box-shadow: 0 2px 8px rgba(37, 99, 235, 0.2);
}
.send-button:disabled {
background: linear-gradient(135deg, #94a3b8 0%, #cbd5e1 100%);
cursor: not-allowed;
transform: none;
box-shadow: none;
}
/* Markdown 样式优化 */
.markdown-body {
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
font-size: 1.05rem;
line-height: 1.8;
color: #334155;
}
.markdown-body :deep(pre) {
background-color: #f8fafc;
border-radius: 16px;
padding: 24px;
overflow-x: auto;
border: 1px solid #e2e8f0;
box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.05);
}
.markdown-body :deep(code) {
font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
font-size: 0.95em;
background-color: #f1f5f9;
padding: 0.2em 0.4em;
border-radius: 6px;
color: #2563eb;
}
.markdown-body :deep(pre code) {
background-color: transparent;
padding: 0;
border-radius: 0;
color: #334155;
}
.markdown-body :deep(blockquote) {
margin: 2em 0;
padding: 1em 1.5em;
border-left: 4px solid #3b82f6;
background-color: #f8fafc;
border-radius: 0 16px 16px 0;
color: #475569;
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.03);
}
/* 加载动画优化 */
.loading-spinner {
width: 20px;
height: 20px;
border: 3px solid rgba(255, 255, 255, 0.9);
border-top-color: transparent;
border-radius: 50%;
animation: spin 0.8s linear infinite;
display: inline-block;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
/* 响应式设计优化 */
@media (max-width: 768px) {
.messages-container {
padding: 20px;
}
.answer-area {
padding: 24px;
}
.input-area {
padding: 16px 20px;
}
textarea {
padding: 16px 20px;
font-size: 16px;
}
.send-button {
padding: 0 24px;
}
}
/* 思考中的样式 */
.thinking {
display: flex;
align-items: center;
justify-content: center;
gap: 12px;
font-size: 1.2rem;
color: #3b82f6;
}
.thinking-spinner {
width: 16px;
height: 16px;
border: 2.5px solid #3b82f6;
border-top-color: transparent;
border-radius: 50%;
animation: spin 0.8s linear infinite;
display: inline-block;
}
</style>

7
client/src/main.js Normal file
View File

@@ -0,0 +1,7 @@
import {createApp} from 'vue'
import App from './App.vue'
import Markdown from 'vue3-markdown-it'
const app = createApp(App)
.use(Markdown)
.mount('#app')