This commit is contained in:
runny committed 2026-05-08 12:50:03 +08:00
1 parent fec2fc096e
commit 283ec9e7b3
11 files changed
+1379 -19

No files matched your search

+18 -5
View File
@@ -138,9 +138,10 @@ export function useChat() {
* query: 用户输入文本
* agentId: 目标 Agent ID
*/
const sendMessage = async (query: string, agentId: number) => {
const sendMessage = async (query: string, agentId: number, kbType: string = '企业知识库') => {
console.info('[chat][send] start', {
agentId,
kbType,
queryLength: query.length,
conversationId: conversationId.value,
});
@@ -159,7 +160,7 @@ export function useChat() {
}
return messages.value[botMsgIndex] || null;
};
isThinking.value = true;
try {
@@ -171,6 +172,7 @@ export function useChat() {
query,
agent_id: agentId,
conversation_id: conversationId.value,
kb_type: kbType
})
});
@@ -230,7 +232,7 @@ export function useChat() {
if (typeof data.answer === 'string' && data.answer.length > 0) {
const botMsg = ensureBotMessage();
if (!botMsg) continue;
botMsg.content += data.answer;
botMsg.content += normalizeStreamText(data.answer);
console.debug('[chat][sse] append answer chunk', {
event: data.event,
appended: data.answer.length,
@@ -256,9 +258,15 @@ export function useChat() {
if (typeof data.answer === 'string' && data.answer.length > 0) {
const botMsg = ensureBotMessage();
if (!botMsg) continue;
botMsg.content += data.answer;
console.debug('[chat][sse] append final answer', {
const normalizedAnswer = normalizeStreamText(data.answer);
if (data.replace_answer) {
botMsg.content = normalizedAnswer;
} else {
botMsg.content += normalizedAnswer;
}
console.debug('[chat][sse] apply final answer', {
event: data.event,
replace: Boolean(data.replace_answer),
appended: data.answer.length,
total: botMsg.content.length,
});
@@ -296,5 +304,10 @@ export function useChat() {
}
};
const normalizeStreamText = (text: string) => {
if (!text) return '';
return text.replace(/\\n/g, '\n');
};
return { messages, isThinking, conversationId, sendMessage };
}
+78 -9
View File
@@ -28,6 +28,16 @@ const currentUser = ref(getStoredAuthUser());
const showUserDropdown = ref(false);
const userMenuRef = ref<HTMLElement | null>(null);
const selectedKb = ref('企业知识库');
const showKbDropdown = ref(false);
const kbMenuRef = ref<HTMLElement | null>(null);
const kbOptions = ['企业知识库', 'ERP知识库'];
marked.setOptions({
gfm: true,
breaks: true,
});
const currentUserDisplayName = computed(() => {
const realName = currentUser.value?.real_name?.trim();
if (realName) return realName;
@@ -44,11 +54,14 @@ const currentUserEmployeeId = computed(() => {
});
const handleDocumentPointerDown = (event: MouseEvent | TouchEvent) => {
if (!showUserDropdown.value) return;
const target = event.target as Node | null;
if (!target) return;
if (userMenuRef.value?.contains(target)) return;
showUserDropdown.value = false;
if (showUserDropdown.value && userMenuRef.value && !userMenuRef.value.contains(target)) {
showUserDropdown.value = false;
}
if (showKbDropdown.value && kbMenuRef.value && !kbMenuRef.value.contains(target)) {
showKbDropdown.value = false;
}
};
const handleLogout = () => {
@@ -729,7 +742,7 @@ const handleSend = async (textOrEvent?: string | Event) => {
return;
}
await sendMessage(content, agentId);
await sendMessage(content, agentId, selectedKb.value);
// 发送完成后刷新历史记录,确保新会话出现
if (!conversationId.value && history.value.length === 0) {
@@ -1326,7 +1339,7 @@ const showChatRating = ref(false);
</div>
<div
v-else
class="prose prose-sm max-w-none leading-7 text-slate-800 dark:text-slate-100 prose-a:text-blue-600 dark:prose-a:text-blue-400 prose-p:my-1 prose-li:my-0.5 prose-ol:my-2 prose-ul:my-2 prose-headings:my-2 prose-hr:my-3 prose-code:rounded prose-code:bg-slate-100 prose-code:px-1 prose-code:text-blue-700 dark:prose-code:bg-slate-800 dark:prose-code:text-blue-300 prose-pre:my-3 prose-pre:border prose-pre:border-slate-700 prose-pre:bg-slate-900 prose-pre:text-slate-100"
class="chat-markdown prose prose-sm max-w-none leading-7 text-slate-800 dark:text-slate-100 prose-a:text-blue-600 dark:prose-a:text-blue-400 prose-p:my-1 prose-li:my-0.5 prose-ol:my-2 prose-ul:my-2 prose-headings:my-2 prose-hr:my-3 prose-code:rounded prose-code:bg-slate-100 prose-code:px-1 prose-code:text-blue-700 dark:prose-code:bg-slate-800 dark:prose-code:text-blue-300 prose-pre:my-3 prose-pre:border prose-pre:border-slate-700 prose-pre:bg-slate-900 prose-pre:text-slate-100"
v-html="renderMarkdown(msg.content)"
></div>
</div>
@@ -1486,10 +1499,30 @@ const showChatRating = ref(false);
<button class="text-slate-400 transition-colors hover:text-slate-600 dark:hover:text-slate-200">
<Mic class="h-5 w-5" />
</button>
<button class="flex items-center gap-1 rounded-lg border border-slate-200 px-3 py-1.5 text-xs font-medium text-slate-600 transition-colors hover:bg-slate-50 dark:border-slate-700 dark:text-slate-300 dark:hover:bg-slate-800">
<Database class="h-3.5 w-3.5 text-blue-500" />
企业知识库 <ChevronDown class="h-3 w-3" />
</button>
<div class="relative" ref="kbMenuRef">
<button
@click="showKbDropdown = !showKbDropdown"
class="flex items-center gap-1 rounded-lg border border-slate-200 px-3 py-1.5 text-xs font-medium text-slate-600 transition-colors hover:bg-slate-50 dark:border-slate-700 dark:text-slate-300 dark:hover:bg-slate-800"
>
<Database class="h-3.5 w-3.5 text-blue-500" />
{{ selectedKb }} <ChevronDown class="h-3 w-3" />
</button>
<div
v-if="showKbDropdown"
class="absolute bottom-full left-0 mb-2 w-36 overflow-hidden rounded-xl border border-slate-100 bg-white py-1 shadow-xl animate-fade-in dark:border-slate-700 dark:bg-slate-900"
>
<button
v-for="option in kbOptions"
:key="option"
@click="selectedKb = option; showKbDropdown = false"
class="flex w-full items-center justify-between px-3 py-2 text-xs text-slate-700 transition-colors hover:bg-slate-50 dark:text-slate-300 dark:hover:bg-slate-800"
:class="{ 'font-bold text-blue-600 dark:text-blue-400': selectedKb === option }"
>
{{ option }}
<Check v-if="selectedKb === option" class="h-3.5 w-3.5" />
</button>
</div>
</div>
<button
@click="toggleBrowserPanel"
class="flex items-center gap-1 rounded-lg border border-slate-200 px-3 py-1.5 text-xs font-medium text-slate-600 transition-colors hover:bg-slate-50 dark:border-slate-700 dark:text-slate-300 dark:hover:bg-slate-800"
@@ -1623,3 +1656,39 @@ const showChatRating = ref(false);
}"
/>
</template>
<style scoped>
:deep(.chat-markdown details) {
margin-top: 1rem;
border: 1px solid rgb(226 232 240);
border-radius: 0.75rem;
background: rgb(248 250 252 / 0.9);
padding: 0.75rem 1rem;
}
:deep(.dark .chat-markdown details) {
border-color: rgb(51 65 85);
background: rgb(30 41 59 / 0.65);
}
:deep(.chat-markdown details summary) {
cursor: pointer;
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.08em;
color: rgb(71 85 105);
list-style-position: inside;
}
:deep(.dark .chat-markdown details summary) {
color: rgb(203 213 225);
}
:deep(.chat-markdown details[open] summary) {
margin-bottom: 0.75rem;
}
:deep(.chat-markdown details pre) {
margin-bottom: 0;
}
</style>