344 lines
7.2 KiB
Vue
344 lines
7.2 KiB
Vue
<script setup lang="ts">
|
||
import { nextTick, ref } from 'vue';
|
||
import { useHead } from '@vueuse/head';
|
||
import { getMachineId, licenseHeaders } from '@/tools/license/license';
|
||
import { useGenHistory } from '@/tools/gen-history/gen-history';
|
||
|
||
useHead({ title: 'AI 问答 - IT-TOOLS-PRO' });
|
||
const genHist = useGenHistory('ai-chat');
|
||
|
||
interface Msg {
|
||
role: 'user' | 'assistant';
|
||
content: string;
|
||
}
|
||
|
||
const messages = ref<Msg[]>([]);
|
||
const input = ref('');
|
||
const busy = ref(false);
|
||
const error = ref('');
|
||
const listEl = ref<HTMLElement>();
|
||
const suggestions = ['帮我写一个 Python 批量重命名脚本', '这段报错是什么意思?', '用一句话解释什么是 Docker', '写一封请假邮件'];
|
||
|
||
async function scrollBottom() {
|
||
await nextTick();
|
||
listEl.value?.scrollTo({ top: listEl.value.scrollHeight, behavior: 'smooth' });
|
||
}
|
||
|
||
async function send(text?: string) {
|
||
const content = (text ?? input.value).trim();
|
||
if (!content || busy.value) {
|
||
return;
|
||
}
|
||
input.value = '';
|
||
messages.value.push({ role: 'user', content });
|
||
busy.value = true;
|
||
error.value = '';
|
||
scrollBottom();
|
||
try {
|
||
const res = await fetch('/relay/ai/chat', {
|
||
method: 'POST',
|
||
headers: { ...licenseHeaders('ai-chat'), 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({
|
||
machine: getMachineId(),
|
||
prompt: content,
|
||
history: messages.value.slice(0, -1).slice(-8),
|
||
}),
|
||
});
|
||
const j = await res.json().catch(() => ({}));
|
||
if (!res.ok || j.code !== 200) {
|
||
throw new Error(j.message || `HTTP ${res.status}`);
|
||
}
|
||
messages.value.push({ role: 'assistant', content: j.data.reply });
|
||
genHist.add({ title: `AI 问答: ${content.slice(0, 24)}`, detail: `回复 ${j.data.reply.length} 字` });
|
||
}
|
||
catch (e: any) {
|
||
error.value = String(e?.message || e);
|
||
}
|
||
finally {
|
||
busy.value = false;
|
||
scrollBottom();
|
||
}
|
||
}
|
||
|
||
function clearChat() {
|
||
messages.value = [];
|
||
error.value = '';
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div class="ac-root">
|
||
<div class="ac-head">
|
||
<div>
|
||
<div class="ac-title">🤖 AI 问答</div>
|
||
<div class="ac-sub">报错信息、英文邮件、生活问题、代码片段——直接问。支持多轮上下文。</div>
|
||
</div>
|
||
<button v-if="messages.length" class="ac-clear" @click="clearChat">🗑 清空对话</button>
|
||
</div>
|
||
|
||
<div ref="listEl" class="ac-list">
|
||
<div v-if="!messages.length" class="ac-welcome">
|
||
<div class="ac-wave">👋</div>
|
||
<div class="ac-tip">试试这些问题:</div>
|
||
<div class="ac-sugg">
|
||
<button v-for="s in suggestions" :key="s" class="ac-sugg-item" @click="input = s; send(s)">
|
||
{{ s }}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
<div v-for="(m, i) in messages" :key="i" class="ac-msg" :class="m.role">
|
||
<div class="ac-avatar">{{ m.role === 'user' ? '我' : 'AI' }}</div>
|
||
<div class="ac-bubble">
|
||
<pre class="ac-text">{{ m.content }}</pre>
|
||
</div>
|
||
</div>
|
||
<div v-if="busy" class="ac-msg assistant">
|
||
<div class="ac-avatar">AI</div>
|
||
<div class="ac-bubble ac-thinking">
|
||
<span class="ac-dot" /><span class="ac-dot" /><span class="ac-dot" />
|
||
思考中…
|
||
</div>
|
||
</div>
|
||
<div v-if="error" class="ac-error">⚠️ {{ error }}</div>
|
||
</div>
|
||
|
||
<div class="ac-inputbar">
|
||
<textarea
|
||
v-model="input"
|
||
class="ac-input"
|
||
rows="2"
|
||
placeholder="输入你的问题,Enter 发送,Shift+Enter 换行"
|
||
@keydown.enter.exact.prevent="input.trim() && send()"
|
||
/>
|
||
<button class="ac-send" :disabled="busy || !input.trim()" @click="send()">
|
||
{{ busy ? '…' : '发送' }}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<style lang="less" scoped>
|
||
.ac-root {
|
||
display: flex;
|
||
flex-direction: column;
|
||
height: calc(100vh - 200px);
|
||
min-height: 480px;
|
||
border: 1px solid rgba(128, 128, 128, 0.2);
|
||
border-radius: 14px;
|
||
overflow: hidden;
|
||
background: #fff;
|
||
}
|
||
|
||
.ac-head {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
padding: 14px 18px;
|
||
border-bottom: 1px solid rgba(128, 128, 128, 0.15);
|
||
|
||
.ac-title {
|
||
font-size: 16.5px;
|
||
font-weight: 800;
|
||
}
|
||
|
||
.ac-sub {
|
||
font-size: 12px;
|
||
opacity: 0.5;
|
||
margin-top: 2px;
|
||
}
|
||
|
||
.ac-clear {
|
||
border: 1px solid rgba(128, 128, 128, 0.3);
|
||
background: #fff;
|
||
border-radius: 8px;
|
||
padding: 6px 13px;
|
||
font-size: 12.5px;
|
||
cursor: pointer;
|
||
|
||
&:hover {
|
||
border-color: #d03050;
|
||
color: #d03050;
|
||
}
|
||
}
|
||
}
|
||
|
||
.ac-list {
|
||
flex: 1;
|
||
overflow-y: auto;
|
||
padding: 18px;
|
||
}
|
||
|
||
.ac-welcome {
|
||
text-align: center;
|
||
padding: 40px 0;
|
||
|
||
.ac-wave {
|
||
font-size: 42px;
|
||
}
|
||
|
||
.ac-tip {
|
||
font-size: 13.5px;
|
||
opacity: 0.6;
|
||
margin: 14px 0 12px;
|
||
}
|
||
}
|
||
|
||
.ac-sugg {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 9px;
|
||
justify-content: center;
|
||
}
|
||
|
||
.ac-sugg-item {
|
||
border: 1px solid rgba(37, 99, 235, 0.35);
|
||
background: rgba(37, 99, 235, 0.05);
|
||
color: #2563eb;
|
||
border-radius: 999px;
|
||
padding: 8px 16px;
|
||
font-size: 13px;
|
||
cursor: pointer;
|
||
transition: all 0.13s;
|
||
|
||
&:hover {
|
||
background: #2563eb;
|
||
color: #fff;
|
||
}
|
||
}
|
||
|
||
.ac-msg {
|
||
display: flex;
|
||
gap: 10px;
|
||
margin-bottom: 14px;
|
||
|
||
&.user {
|
||
flex-direction: row-reverse;
|
||
|
||
.ac-bubble {
|
||
background: #2563eb;
|
||
color: #fff;
|
||
}
|
||
}
|
||
|
||
.ac-avatar {
|
||
width: 32px;
|
||
height: 32px;
|
||
border-radius: 50%;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
font-size: 12.5px;
|
||
font-weight: 800;
|
||
flex-shrink: 0;
|
||
color: #fff;
|
||
background: linear-gradient(135deg, #2563eb, #18a058);
|
||
}
|
||
|
||
&.user .ac-avatar {
|
||
background: linear-gradient(135deg, #f59e0b, #e60012);
|
||
}
|
||
|
||
.ac-bubble {
|
||
max-width: 78%;
|
||
background: rgba(128, 128, 128, 0.08);
|
||
border-radius: 12px;
|
||
padding: 10px 14px;
|
||
}
|
||
|
||
.ac-text {
|
||
margin: 0;
|
||
white-space: pre-wrap;
|
||
word-break: break-word;
|
||
font-family: inherit;
|
||
font-size: 13.5px;
|
||
line-height: 1.75;
|
||
}
|
||
}
|
||
|
||
.ac-thinking {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 5px;
|
||
font-size: 12.5px;
|
||
opacity: 0.7;
|
||
|
||
.ac-dot {
|
||
width: 6px;
|
||
height: 6px;
|
||
border-radius: 50%;
|
||
background: #2563eb;
|
||
animation: ac-blink 1.2s infinite;
|
||
|
||
&:nth-child(2) {
|
||
animation-delay: 0.2s;
|
||
}
|
||
|
||
&:nth-child(3) {
|
||
animation-delay: 0.4s;
|
||
}
|
||
}
|
||
}
|
||
|
||
@keyframes ac-blink {
|
||
0%,
|
||
60%,
|
||
100% {
|
||
opacity: 0.25;
|
||
}
|
||
|
||
30% {
|
||
opacity: 1;
|
||
}
|
||
}
|
||
|
||
.ac-error {
|
||
color: #d03050;
|
||
font-size: 12.5px;
|
||
padding: 8px 4px;
|
||
}
|
||
|
||
.ac-inputbar {
|
||
display: flex;
|
||
gap: 10px;
|
||
padding: 13px 16px;
|
||
border-top: 1px solid rgba(128, 128, 128, 0.15);
|
||
background: rgba(128, 128, 128, 0.03);
|
||
|
||
.ac-input {
|
||
flex: 1;
|
||
resize: none;
|
||
border: 1px solid rgba(128, 128, 128, 0.35);
|
||
border-radius: 10px;
|
||
padding: 10px 13px;
|
||
font-size: 13.5px;
|
||
font-family: inherit;
|
||
outline: none;
|
||
|
||
&:focus {
|
||
border-color: #2563eb;
|
||
}
|
||
}
|
||
|
||
.ac-send {
|
||
align-self: stretch;
|
||
border: none;
|
||
background: #2563eb;
|
||
color: #fff;
|
||
border-radius: 10px;
|
||
padding: 0 24px;
|
||
font-size: 14px;
|
||
font-weight: 700;
|
||
cursor: pointer;
|
||
|
||
&:disabled {
|
||
opacity: 0.5;
|
||
cursor: not-allowed;
|
||
}
|
||
|
||
&:hover:not(:disabled) {
|
||
background: #1d4ed8;
|
||
}
|
||
}
|
||
}
|
||
</style>
|