feat: AI问答改多会话布局 — 左侧对话列表(新建/搜索/删除/时间戳/自动命名, localStorage保留30个), 欢迎页大标题+建议问题, 输入区模型下拉(mimo-v2.6-flash/glm-5.3-flash), relay模型白名单

This commit is contained in:
personal82555 committed 2026-10-13 09:56:30 +08:00
1 parent 93cd1cf2bd
commit 61269a346c
2 files changed
+374 -127

No files matched your search

+3
View File
@@ -1843,6 +1843,9 @@ class Handler(BaseHTTPRequestHandler):
if not key:
self._reply(500, json.dumps({"code": 500, "message": "AI 服务未配置"}, ensure_ascii=False).encode(), "application/json")
return
want = str(payload.get("model") or "")
if want in ("mimo-v2.6-flash", "glm-5.3-flash"):
model = want
try:
body = json.dumps({"model": model, "messages": messages, "temperature": 0.7, "max_tokens": 2048}).encode()
req = request.Request(upstream, data=body, headers={"Content-Type": "application/json", "Authorization": "Bearer " + key})
+371 -127
View File
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { nextTick, ref } from 'vue';
import { computed, nextTick, ref } from 'vue';
import { useHead } from '@vueuse/head';
import { getMachineId, licenseHeaders } from '@/tools/license/license';
import { useGenHistory } from '@/tools/gen-history/gen-history';
@@ -7,17 +7,94 @@ import { useGenHistory } from '@/tools/gen-history/gen-history';
useHead({ title: 'AI 问答 - IT-TOOLS-PRO' });
const genHist = useGenHistory('ai-chat');
interface Msg {
interface ChatMsg {
role: 'user' | 'assistant';
content: string;
}
const messages = ref<Msg[]>([]);
interface Conversation {
id: string;
title: string;
updatedAt: number;
messages: ChatMsg[];
}
// ============ 会话管理(localStorage 持久化,保留最近 30 个) ============
const STORAGE_KEY = 'it-tools-ai-chat-convs';
function loadConvs(): Conversation[] {
try {
return JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
}
catch {
return [];
}
}
function saveConvs() {
conversations.value = [...conversations.value]
.sort((a, b) => b.updatedAt - a.updatedAt)
.slice(0, 30);
localStorage.setItem(STORAGE_KEY, JSON.stringify(conversations.value));
}
const conversations = ref<Conversation[]>(loadConvs());
const curId = ref(conversations.value[0]?.id || '');
const search = ref('');
const cur = computed(() => conversations.value.find(c => c.id === curId.value) || null);
const messages = computed(() => cur.value?.messages ?? []);
const filteredConvs = computed(() => {
const q = search.value.trim().toLowerCase();
const list = [...conversations.value].sort((a, b) => b.updatedAt - a.updatedAt);
return q ? list.filter(c => c.title.toLowerCase().includes(q)) : list;
});
const uid = () => Math.random().toString(36).slice(2, 10);
function newChat() {
const c: Conversation = { id: uid(), title: '新对话', updatedAt: Date.now(), messages: [] };
conversations.value.unshift(c);
curId.value = c.id;
error.value = '';
saveConvs();
}
function removeChat(id: string) {
conversations.value = conversations.value.filter(c => c.id !== id);
if (curId.value === id) {
curId.value = conversations.value[0]?.id || '';
}
saveConvs();
}
function fmtTime(ts: number) {
const d = new Date(ts);
const now = new Date();
const sameDay = d.toDateString() === now.toDateString();
const hm = `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`;
return sameDay ? `今天 ${hm}` : `${d.getMonth() + 1}/${d.getDate()} ${hm}`;
}
// ============ 模型选择(固定两款) ============
const CHAT_MODELS = ['mimo-v2.6-flash', 'glm-5.3-flash'];
const model = ref(CHAT_MODELS[0]);
// ============ 发送 ============
const input = ref('');
const busy = ref(false);
const error = ref('');
const listEl = ref<HTMLElement>();
const suggestions = ['帮我写一个 Python 批量重命名脚本', '这段报错是什么意思?', '用一句话解释什么是 Docker', '写一封请假邮件'];
const SUGGESTIONS = [
'帮我写一个 Python 批量重命名脚本',
'这段报错是什么意思?',
'用一句话解释什么是 Docker',
'写一封请假邮件',
'推荐几个提高效率的命令行工具',
'如何备份 Docker 数据卷?',
];
async function scrollBottom() {
await nextTick();
@@ -29,8 +106,18 @@ async function send(text?: string) {
if (!content || busy.value) {
return;
}
if (!cur.value) {
newChat();
}
const conv = cur.value!;
input.value = '';
messages.value.push({ role: 'user', content });
conv.messages.push({ role: 'user', content });
// 自动命名:第一条消息做标题
if (conv.title === '新对话') {
conv.title = content.slice(0, 20) || '新对话';
}
conv.updatedAt = Date.now();
saveConvs();
busy.value = true;
error.value = '';
scrollBottom();
@@ -41,15 +128,18 @@ async function send(text?: string) {
body: JSON.stringify({
machine: getMachineId(),
prompt: content,
history: messages.value.slice(0, -1).slice(-8),
model: model.value,
history: conv.messages.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} 字` });
conv.messages.push({ role: 'assistant', content: j.data.reply });
conv.updatedAt = Date.now();
saveConvs();
genHist.add({ title: `AI 问答: ${content.slice(0, 24)}`, detail: `${model.value} · 回复 ${j.data.reply.length} 字` });
}
catch (e: any) {
error.value = String(e?.message || e);
@@ -59,60 +149,89 @@ async function send(text?: string) {
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 class="ac-side">
<button class="ac-new" @click="newChat">
+ 新建对话
</button>
<input v-model="search" class="ac-search" placeholder="🔍 搜索对话">
<div class="ac-convs">
<div
v-for="c in filteredConvs"
:key="c.id"
class="ac-conv"
:class="{ on: c.id === curId }"
@click="curId = c.id; error = ''"
>
<div class="ac-conv-main">
<div class="ac-conv-title">{{ c.title }}</div>
<div class="ac-conv-time">{{ fmtTime(c.updatedAt) }}</div>
</div>
<button class="ac-conv-x" title="删除对话" @click.stop="removeChat(c.id)">✕</button>
</div>
<div v-if="!filteredConvs.length" class="ac-convs-empty">
暂无对话,点上方「新建对话」开始
</div>
</div>
<button v-if="messages.length" class="ac-clear" @click="clearChat">🗑 清空对话</button>
</div>
<div ref="listEl" class="ac-list">
<!-- 右:对话区 -->
<div class="ac-main">
<!-- 欢迎页 -->
<div v-if="!messages.length" class="ac-welcome">
<div class="ac-wave">👋</div>
<div class="ac-tip">试试这些问题:</div>
<div class="ac-hello">有什么我能帮你的吗?</div>
<div class="ac-sugg">
<button v-for="s in suggestions" :key="s" class="ac-sugg-item" @click="input = s; send(s)">
<button v-for="s in SUGGESTIONS" :key="s" class="ac-sugg-item" @click="send(s)">
{{ s }}
</button>
</div>
<div v-if="error" class="ac-error">⚠️ {{ error }}</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 v-else ref="listEl" class="ac-list">
<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" />
思考中({{ model }})…
</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()"
/>
<div class="ac-toolbar">
<label class="ac-model">
模型
<select v-model="model" class="ac-model-sel">
<option v-for="m in CHAT_MODELS" :key="m" :value="m">{{ m }}</option>
</select>
</label>
<span class="ac-note">内容由 AI 生成,仅供参考</span>
<button class="ac-send" :disabled="busy || !input.trim()" @click="send()">
{{ busy ? '…' : '🚀 发送' }}
</button>
</div>
</div>
</div>
</div>
</template>
@@ -120,102 +239,193 @@ function clearChat() {
<style lang="less" scoped>
.ac-root {
display: flex;
flex-direction: column;
height: calc(100vh - 200px);
min-height: 480px;
height: calc(100vh - 190px);
min-height: 520px;
border: 1px solid rgba(128, 128, 128, 0.2);
border-radius: 14px;
overflow: hidden;
background: #fff;
}
.ac-head {
// ===== 左侧会话列表 =====
.ac-side {
width: 250px;
flex-shrink: 0;
border-right: 1px solid rgba(128, 128, 128, 0.15);
display: flex;
justify-content: space-between;
align-items: center;
padding: 14px 18px;
border-bottom: 1px solid rgba(128, 128, 128, 0.15);
flex-direction: column;
background: rgba(128, 128, 128, 0.03);
}
.ac-title {
font-size: 16.5px;
font-weight: 800;
}
.ac-new {
margin: 12px 12px 8px;
border: 1px dashed rgba(24, 160, 88, 0.55);
background: rgba(24, 160, 88, 0.06);
color: #18a058;
border-radius: 9px;
padding: 9px;
font-size: 13.5px;
font-weight: 700;
cursor: pointer;
transition: all 0.14s;
.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;
}
&:hover {
background: #18a058;
color: #fff;
}
}
.ac-list {
.ac-search {
margin: 0 12px 8px;
border: 1px solid rgba(128, 128, 128, 0.3);
border-radius: 8px;
padding: 7px 11px;
font-size: 12.5px;
outline: none;
background: #fff;
&:focus {
border-color: #18a058;
}
}
.ac-convs {
flex: 1;
overflow-y: auto;
padding: 18px;
padding: 2px 8px 10px;
}
.ac-conv {
display: flex;
align-items: center;
gap: 6px;
padding: 9px 10px;
border-radius: 9px;
cursor: pointer;
margin-bottom: 3px;
transition: background 0.12s;
border: 1px solid transparent;
&:hover {
background: rgba(128, 128, 128, 0.08);
.ac-conv-x {
opacity: 1;
}
}
&.on {
background: rgba(24, 160, 88, 0.1);
border-color: rgba(24, 160, 88, 0.35);
.ac-conv-title {
color: #18a058;
font-weight: 700;
}
}
.ac-conv-main {
flex: 1;
min-width: 0;
.ac-conv-title {
font-size: 13px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.ac-conv-time {
font-size: 10.5px;
opacity: 0.45;
margin-top: 2px;
}
}
.ac-conv-x {
border: none;
background: transparent;
color: #d03050;
font-size: 11.5px;
cursor: pointer;
padding: 2px 4px;
opacity: 0;
flex-shrink: 0;
transition: opacity 0.12s;
}
}
.ac-convs-empty {
font-size: 12px;
opacity: 0.4;
text-align: center;
padding: 30px 8px;
line-height: 1.7;
}
// ===== 右侧对话区 =====
.ac-main {
flex: 1;
min-width: 0;
display: flex;
flex-direction: column;
}
.ac-welcome {
text-align: center;
padding: 40px 0;
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 20px;
.ac-wave {
font-size: 42px;
}
.ac-tip {
font-size: 13.5px;
opacity: 0.6;
margin: 14px 0 12px;
.ac-hello {
font-size: 26px;
font-weight: 800;
margin-bottom: 26px;
}
}
.ac-sugg {
display: flex;
flex-wrap: wrap;
gap: 9px;
gap: 10px;
justify-content: center;
max-width: 720px;
}
.ac-sugg-item {
border: 1px solid rgba(37, 99, 235, 0.35);
background: rgba(37, 99, 235, 0.05);
color: #2563eb;
border: 1px solid rgba(128, 128, 128, 0.3);
background: #fff;
border-radius: 999px;
padding: 8px 16px;
padding: 9px 18px;
font-size: 13px;
cursor: pointer;
transition: all 0.13s;
transition: all 0.14s;
&:hover {
background: #2563eb;
color: #fff;
border-color: #18a058;
color: #18a058;
background: rgba(24, 160, 88, 0.05);
}
}
.ac-list {
flex: 1;
overflow-y: auto;
padding: 20px 22px;
}
.ac-msg {
display: flex;
gap: 10px;
margin-bottom: 14px;
margin-bottom: 15px;
&.user {
flex-direction: row-reverse;
.ac-bubble {
background: #2563eb;
background: #18a058;
color: #fff;
}
}
@@ -266,7 +476,7 @@ function clearChat() {
width: 6px;
height: 6px;
border-radius: 50%;
background: #2563eb;
background: #18a058;
animation: ac-blink 1.2s infinite;
&:nth-child(2) {
@@ -295,17 +505,18 @@ function clearChat() {
color: #d03050;
font-size: 12.5px;
padding: 8px 4px;
text-align: center;
}
// ===== 输入区 =====
.ac-inputbar {
display: flex;
gap: 10px;
padding: 13px 16px;
border-top: 1px solid rgba(128, 128, 128, 0.15);
padding: 12px 16px;
background: rgba(128, 128, 128, 0.03);
.ac-input {
flex: 1;
width: 100%;
box-sizing: border-box;
resize: none;
border: 1px solid rgba(128, 128, 128, 0.35);
border-radius: 10px;
@@ -315,29 +526,62 @@ function clearChat() {
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;
border-color: #18a058;
}
}
}
.ac-toolbar {
display: flex;
align-items: center;
gap: 12px;
margin-top: 9px;
}
.ac-model {
display: flex;
align-items: center;
gap: 7px;
font-size: 12.5px;
opacity: 0.85;
.ac-model-sel {
border: 1px solid rgba(128, 128, 128, 0.35);
border-radius: 7px;
padding: 5px 10px;
font-size: 12.5px;
background: #fff;
outline: none;
&:focus {
border-color: #18a058;
}
}
}
.ac-note {
font-size: 11px;
opacity: 0.45;
}
.ac-send {
margin-left: auto;
border: none;
background: #18a058;
color: #fff;
border-radius: 10px;
padding: 9px 22px;
font-size: 13.5px;
font-weight: 700;
cursor: pointer;
&:disabled {
opacity: 0.5;
cursor: not-allowed;
}
&:hover:not(:disabled) {
background: #15924e;
}
}
</style>