feat: Initial commit with full deployment architecture and scripts

This commit is contained in:
super committed 2026-02-24 16:45:11 +08:00
commit abb3efe114
96 files changed
+16789

No files matched your search

+405
View File
@@ -0,0 +1,405 @@
<script setup lang="ts">
/**
* ChatView.vue
* 文件用途:单个 Agent 的对话界面
* 引用文件:vue, vue-router, lucide-vue-next, useChat 组合函数
* 逻辑关系:
* - 使用 useChat 发送消息并接收 SSE 流
* - 支持从首页携带预设问题快速发起对话
* - 在回答结束后允许用户对本次回答进行评分与意见反馈
*/
import { ref, onMounted, computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { useChat } from '../composables/useChat';
import { Send, User, Bot, Loader2, ArrowLeft, Plus, Menu, Star } from 'lucide-vue-next';
import { useRoute, useRouter } from 'vue-router';
const { t, locale } = useI18n();
const route = useRoute();
const router = useRouter();
const agentId = Number(route.params.agentId);
// Sidebar state for mobile
const isSidebarOpen = ref(false);
// 从 useChat 中解构出消息列表、思考状态、会话 ID 与发送函数
const { messages, isThinking, conversationId, sendMessage } = useChat();
const input = ref('');
const currentAgent = ref<any>(null);
interface ChatHistoryItem {
id: number;
conversation_id: string;
title: string;
created_at: string;
}
const history = ref<ChatHistoryItem[]>([]);
// 预设问题:从路由 query.preset 中读取(如果有)
const presetQuestion = computed(() => {
const q = route.query.preset;
return typeof q === 'string' ? q : '';
});
const getAuthHeaders = () => {
const stored = localStorage.getItem('hxops_user');
if (!stored) return {};
const user = JSON.parse(stored) as { id?: number; username: string; role: string };
const headers: Record<string, string> = {
'X-Username': user.username,
'X-Role': user.role,
};
if (user.id) {
headers['X-User-Id'] = String(user.id);
}
return headers;
};
/**
* handleSend
* 用途:将输入框内容发送给当前 Agent
* 核心逻辑:
* 1. 校验输入非空且当前不在思考状态
* 2. 调用 sendMessage
* 3. 清空输入框
*/
const handleSend = async () => {
if (!input.value.trim() || isThinking.value) return;
const userInput = input.value;
input.value = '';
await sendMessage(userInput, agentId);
// 发送完成后刷新历史记录,确保新会话出现
if (!conversationId.value && history.value.length === 0) {
await loadHistory();
} else if (conversationId.value) {
// Check if current conversation is in history, if not, reload
const exists = history.value.some(h => h.conversation_id === conversationId.value);
if (!exists) await loadHistory();
}
};
// 加载历史记录
const loadHistory = async () => {
try {
const res = await fetch(`/api/chat/sessions`, { headers: getAuthHeaders() });
if (res.ok) {
history.value = await res.json();
}
} catch (e) {
console.error('Failed to load history', e);
}
};
const loadConversation = async (id: string) => {
if (conversationId.value === id) return;
try {
const res = await fetch(`/api/chat/messages?conversation_id=${id}`, { headers: getAuthHeaders() });
if (res.ok) {
messages.value = await res.json();
conversationId.value = id;
// Reset feedback state
rating.value = null;
feedbackComment.value = '';
feedbackSubmitted.value = false;
}
} catch (e) {
console.error('Failed to load conversation', e);
}
};
const startNewChat = () => {
messages.value = [];
conversationId.value = null;
rating.value = null;
feedbackComment.value = '';
feedbackSubmitted.value = false;
};
// 组件挂载时,如果存在预设问题,则自动触发一次发送
onMounted(async () => {
// Fetch Agent Details
try {
const agentRes = await fetch(`/api/user/agents/${agentId}`, { headers: getAuthHeaders() });
if (agentRes.ok) {
currentAgent.value = await agentRes.json();
}
} catch (e) {
console.error('Failed to load agent details', e);
}
await loadHistory();
if (presetQuestion.value) {
input.value = presetQuestion.value;
handleSend();
}
const res = await fetch('/api/user/config', { headers: getAuthHeaders() });
if (res.ok) {
const data = await res.json();
enableFeedback.value = !!data.feedback_enabled;
}
});
/**
* 反馈相关状态
* enableFeedback:是否启用评分功能(后续可接入配置)
* rating:用户选择的评分(1-5)
* feedbackComment:用户输入的文字意见
* feedbackSubmitted:是否已经提交反馈
*/
const enableFeedback = ref(true);
const rating = ref<number | null>(null);
const feedbackComment = ref('');
const feedbackSubmitted = ref(false);
/**
* submitFeedback
* 用途:将评分与评论提交到后端 /api/chat/feedback
*/
const submitFeedback = async () => {
if (!enableFeedback.value || feedbackSubmitted.value) return;
if (!rating.value) {
alert(t('chat.feedback.selectRating'));
return;
}
const res = await fetch('/api/chat/feedback', {
method: 'POST',
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
body: JSON.stringify({
agent_id: agentId,
score: rating.value,
comment: feedbackComment.value,
conversation_id: conversationId.value,
}),
});
if (res.ok) {
feedbackSubmitted.value = true;
}
};
</script>
<template>
<div class="flex h-screen bg-white font-sans text-slate-800">
<!-- Sidebar (History) -->
<!-- Mobile Overlay -->
<div
v-if="isSidebarOpen"
class="fixed inset-0 bg-slate-900/50 z-40 md:hidden backdrop-blur-sm transition-opacity"
@click="isSidebarOpen = false"
></div>
<aside
class="w-72 bg-slate-50 border-r border-slate-200 flex flex-col fixed inset-y-0 left-0 z-50 transition-transform duration-300 ease-in-out md:relative md:translate-x-0 shadow-xl md:shadow-none"
:class="isSidebarOpen ? 'translate-x-0' : '-translate-x-full'"
>
<div class="p-4 flex items-center justify-between">
<button @click="router.push('/workspace')" class="p-2 hover:bg-white hover:shadow-sm rounded-xl transition-all text-slate-500 hover:text-slate-900" title="Back to Workspace">
<ArrowLeft class="w-5 h-5" />
</button>
<span class="font-bold text-slate-700 tracking-tight">{{ $t('chat.history') }}</span>
<button class="p-2 hover:bg-brand-100 hover:text-brand-600 rounded-xl transition-all text-slate-500" title="New Chat" @click="startNewChat(); isSidebarOpen = false;">
<Plus class="w-5 h-5" />
</button>
</div>
<div class="px-3 pb-2">
<button @click="startNewChat(); isSidebarOpen = false;" class="w-full py-2.5 px-4 bg-brand-600 hover:bg-brand-700 text-white rounded-xl font-medium shadow-lg shadow-brand-500/20 transition-all flex items-center justify-center gap-2 mb-2">
<Plus class="w-4 h-4" /> {{ $t('chat.newChat') }}
</button>
</div>
<div class="flex-1 overflow-y-auto px-3 space-y-1 scrollbar-thin">
<div v-if="history.length === 0" class="text-center text-slate-400 text-sm py-8 italic">{{ $t('chat.noHistory') }}</div>
<button
v-for="item in history"
:key="item.id"
class="w-full text-left px-4 py-3 rounded-xl text-sm text-slate-600 hover:bg-white hover:shadow-sm truncate transition-all border border-transparent hover:border-slate-100"
:class="conversationId === item.conversation_id ? 'bg-white shadow-sm border-slate-100 text-brand-600 font-medium ring-1 ring-brand-500/10' : ''"
@click="loadConversation(item.conversation_id); isSidebarOpen = false;"
>
{{ item.title || $t('chat.newChat') }}
</button>
</div>
<!-- Language Selector & Agent Info -->
<div class="p-4 border-t border-slate-200 bg-slate-50 space-y-3">
<select
v-model="locale"
class="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-sm shadow-sm outline-none focus:ring-2 focus:ring-brand-500/20 focus:border-brand-500 transition-all cursor-pointer"
>
<option value="en">English</option>
<option value="zh-CN">简体中文</option>
<option value="zh-TW">繁體中文</option>
<option value="th">ไทย</option>
</select>
<div class="flex items-center justify-center gap-2 text-xs text-slate-400 font-mono bg-slate-100 py-1.5 rounded-lg">
<span class="w-1.5 h-1.5 rounded-full bg-green-500"></span>
{{ $t('chat.agentId') }} {{ agentId }}
</div>
</div>
</aside>
<!-- Main Chat Area -->
<div class="flex-1 flex flex-col h-full relative w-full md:w-auto overflow-hidden bg-white">
<!-- Mobile Header -->
<header class="md:hidden bg-white/80 backdrop-blur-md border-b border-slate-100 px-4 py-3 flex items-center justify-between shrink-0 z-30 sticky top-0">
<div class="flex items-center gap-3">
<button @click="router.push('/workspace')" class="p-2 hover:bg-slate-50 rounded-lg transition-colors">
<ArrowLeft class="w-5 h-5 text-slate-600" />
</button>
<span class="font-bold text-slate-900 truncate max-w-[200px]">{{ currentAgent?.name || $t('chat.defaultTitle') }}</span>
</div>
<button @click="isSidebarOpen = !isSidebarOpen" class="p-2 hover:bg-slate-50 rounded-lg transition-colors">
<Menu class="w-6 h-6 text-slate-600" />
</button>
</header>
<!-- Chat Content -->
<div v-if="!currentAgent" class="flex-1 flex flex-col items-center justify-center space-y-4">
<Loader2 class="w-10 h-10 animate-spin text-brand-500" />
<p class="text-slate-400 text-sm animate-pulse">Loading agent...</p>
</div>
<div v-else-if="currentAgent.type === 'iframe'" class="flex-1 w-full h-full bg-white relative">
<iframe
:src="currentAgent.extra_config?.iframe_url"
class="w-full h-full border-0"
allow="microphone; camera; clipboard-read; clipboard-write"
></iframe>
</div>
<template v-else>
<main class="flex-1 overflow-y-auto p-4 md:p-8 space-y-8 scroll-smooth scrollbar-thin">
<div v-if="messages.length === 0" class="flex flex-col items-center justify-center h-full text-slate-400 pb-20">
<div class="w-20 h-20 bg-gradient-to-br from-brand-500 to-brand-700 rounded-3xl flex items-center justify-center mb-8 shadow-xl shadow-brand-500/20 animate-fade-in">
<Bot class="text-white w-10 h-10" />
</div>
<h2 class="text-3xl font-bold text-slate-900 mb-3 tracking-tight">{{ $t('chat.welcome.title') }}</h2>
<p class="text-slate-500 text-lg max-w-md text-center leading-relaxed">{{ $t('chat.welcome.subtitle') }}</p>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mt-12 w-full max-w-2xl">
<button v-for="suggestion in ['What can you do?', 'Help me write a report', 'Analyze this data', 'Creative writing']"
:key="suggestion"
@click="input = suggestion; handleSend()"
class="p-4 bg-white border border-slate-200 rounded-xl hover:border-brand-400 hover:shadow-md hover:text-brand-700 text-slate-600 text-sm font-medium transition-all text-left">
{{ suggestion }}
</button>
</div>
</div>
<div
v-for="(msg, index) in messages"
:key="index"
class="flex gap-4 max-w-4xl mx-auto group animate-slide-up"
:class="msg.role === 'user' ? 'justify-end' : ''"
>
<!-- Bot Avatar -->
<div v-if="msg.role === 'assistant'" class="w-10 h-10 rounded-xl bg-gradient-to-br from-brand-500 to-brand-700 flex items-center justify-center shrink-0 shadow-sm mt-1">
<Bot class="text-white w-6 h-6" />
</div>
<!-- Message Bubble -->
<div
class="py-3.5 px-6 rounded-2xl shadow-sm max-w-[85%] md:max-w-[75%] leading-relaxed text-[15px]"
:class="msg.role === 'user' ? 'bg-brand-600 text-white rounded-tr-sm shadow-brand-500/20' : 'bg-white border border-slate-100 text-slate-800 rounded-tl-sm shadow-slate-200/50'"
>
<div class="whitespace-pre-wrap prose prose-sm max-w-none" :class="msg.role === 'user' ? 'prose-invert' : 'prose-slate'">{{ msg.content }}</div>
</div>
<!-- User Avatar -->
<div v-if="msg.role === 'user'" class="w-10 h-10 rounded-xl bg-slate-200 flex items-center justify-center shrink-0 shadow-sm mt-1">
<User class="text-slate-500 w-6 h-6" />
</div>
</div>
<!-- Loading -->
<div v-if="isThinking" class="flex gap-4 max-w-4xl mx-auto animate-pulse">
<div class="w-10 h-10 rounded-xl bg-gradient-to-br from-brand-500 to-brand-700 flex items-center justify-center shrink-0 shadow-sm mt-1">
<Bot class="text-white w-6 h-6" />
</div>
<div class="py-3 px-5 bg-white border border-slate-100 rounded-2xl rounded-tl-sm shadow-sm flex items-center gap-3">
<div class="flex gap-1">
<span class="w-2 h-2 bg-brand-400 rounded-full animate-bounce"></span>
<span class="w-2 h-2 bg-brand-400 rounded-full animate-bounce delay-75"></span>
<span class="w-2 h-2 bg-brand-400 rounded-full animate-bounce delay-150"></span>
</div>
<span class="text-slate-400 text-sm font-medium">{{ $t('chat.thinking') }}</span>
</div>
</div>
<!-- Feedback -->
<div
v-if="enableFeedback && messages.length > 0 && !isThinking"
class="max-w-4xl mx-auto mt-8 pt-8 border-t border-slate-100"
>
<div class="flex flex-col items-center gap-4 bg-slate-50 p-6 rounded-2xl border border-slate-100">
<span class="text-sm font-medium text-slate-500">{{ $t('chat.rate') }}</span>
<div class="flex gap-3">
<button
v-for="n in 5"
:key="n"
class="w-10 h-10 rounded-full flex items-center justify-center transition-all duration-200"
:class="rating && n <= rating ? 'bg-yellow-400 text-white scale-110 shadow-md shadow-yellow-400/30' : 'bg-white border border-slate-200 text-slate-400 hover:border-yellow-400 hover:text-yellow-400'"
@click="rating = n"
>
<Star v-if="rating && n <= rating" class="w-5 h-5 fill-current" />
<span v-else class="text-sm font-bold">{{ n }}</span>
</button>
</div>
<div v-if="rating" class="w-full max-w-lg flex gap-3 mt-2 animate-fade-in">
<input
v-model="feedbackComment"
type="text"
class="flex-1 input-field py-2 text-sm"
:placeholder="$t('chat.feedback.commentPlaceholder')"
/>
<button
class="px-6 py-2 bg-brand-600 text-white text-sm font-bold rounded-xl hover:bg-brand-700 disabled:opacity-50 transition-colors shadow-lg shadow-brand-500/30"
:disabled="feedbackSubmitted"
@click="submitFeedback"
>
{{ feedbackSubmitted ? $t('chat.feedback.sent') : $t('chat.send') }}
</button>
</div>
</div>
</div>
<div class="h-24"></div> <!-- Spacer for fixed footer -->
</main>
<!-- Input Area -->
<footer class="p-4 md:p-6 bg-white/80 backdrop-blur-xl border-t border-slate-100 absolute bottom-0 left-0 right-0 z-20">
<div class="max-w-4xl mx-auto relative">
<div class="relative bg-white rounded-2xl border border-slate-200 shadow-lg shadow-slate-200/50 focus-within:ring-2 focus-within:ring-brand-500/20 focus-within:border-brand-500 transition-all duration-300">
<textarea
v-model="input"
@keydown.enter.prevent="handleSend"
rows="1"
:placeholder="$t('chat.placeholder')"
class="w-full pl-5 pr-14 py-4 bg-transparent resize-none focus:outline-none text-slate-800 placeholder-slate-400 text-[15px] leading-relaxed"
style="min-height: 56px; max-height: 200px;"
:disabled="isThinking"
></textarea>
<button
@click="handleSend"
:disabled="!input.trim() || isThinking"
class="absolute right-2 bottom-2 p-2.5 bg-brand-600 text-white rounded-xl hover:bg-brand-700 disabled:opacity-50 disabled:cursor-not-allowed transition-all shadow-md shadow-brand-500/20 active:scale-95"
>
<Send class="w-5 h-5" />
</button>
</div>
<div class="text-center mt-3 text-xs font-medium text-slate-400">
{{ $t('chat.disclaimer') }}
</div>
</div>
</footer>
</template>
</div>
</div>
</template>