智能体跳转切换优化
This commit is contained in:
1 parent
1f96d6fccc
commit
55ce8dd949
3 files changed
+91
-52
No files matched your search
@@ -36,6 +36,7 @@ type RunStatus = 'running' | 'done' | 'error';
|
|||||||
|
|
||||||
interface ChatRun {
|
interface ChatRun {
|
||||||
runId: number;
|
runId: number;
|
||||||
|
agentId: number;
|
||||||
viewId: number;
|
viewId: number;
|
||||||
query: string;
|
query: string;
|
||||||
messages: ChatMessage[];
|
messages: ChatMessage[];
|
||||||
@@ -192,6 +193,7 @@ export function useChat() {
|
|||||||
const runMessages = messages.value;
|
const runMessages = messages.value;
|
||||||
const run: ChatRun = {
|
const run: ChatRun = {
|
||||||
runId: ++runSeq,
|
runId: ++runSeq,
|
||||||
|
agentId,
|
||||||
viewId: runViewId,
|
viewId: runViewId,
|
||||||
query,
|
query,
|
||||||
messages: runMessages,
|
messages: runMessages,
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ import { useTheme } from '../composables/useTheme';
|
|||||||
import { getAuthHeaders, getStoredAuthUser, logoutAuth } from '../utils/auth';
|
import { getAuthHeaders, getStoredAuthUser, logoutAuth } from '../utils/auth';
|
||||||
import { copyTextToClipboard } from '../utils/clipboard';
|
import { copyTextToClipboard } from '../utils/clipboard';
|
||||||
import { setLastChatAgentId } from '../utils/lastChatAgent';
|
import { setLastChatAgentId } from '../utils/lastChatAgent';
|
||||||
import { openAgentChatTab, registerOpenAgentId } from '../utils/agentTabs';
|
import { registerOpenAgentId } from '../utils/agentTabs';
|
||||||
import ChatShell from '../layouts/ChatShell.vue';
|
import ChatShell from '../layouts/ChatShell.vue';
|
||||||
import { BusinessError } from '../api/client';
|
import { BusinessError } from '../api/client';
|
||||||
import {
|
import {
|
||||||
@@ -602,7 +602,7 @@ const previewFile = ref<{ name: string; url: string; actualExt?: string } | null
|
|||||||
const resolveSourceLink = async (file: SourceFileLink): Promise<{ name: string; url: string; actualExt?: string }> => {
|
const resolveSourceLink = async (file: SourceFileLink): Promise<{ name: string; url: string; actualExt?: string }> => {
|
||||||
if (file.datasetId && file.remoteDocumentId) {
|
if (file.datasetId && file.remoteDocumentId) {
|
||||||
const data = await resolveChatSourceFile({
|
const data = await resolveChatSourceFile({
|
||||||
agent_id: agentId,
|
agent_id: currentAgentId.value,
|
||||||
dataset_id: file.datasetId,
|
dataset_id: file.datasetId,
|
||||||
remote_document_id: file.remoteDocumentId,
|
remote_document_id: file.remoteDocumentId,
|
||||||
});
|
});
|
||||||
@@ -650,7 +650,10 @@ const parsePlmPreviewLink = (href: string): SourceFileLink | null => {
|
|||||||
};
|
};
|
||||||
const route = useRoute();
|
const route = useRoute();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const agentId = Number(route.params.agentId);
|
const currentAgentId = computed(() => {
|
||||||
|
const id = Number(route.params.agentId);
|
||||||
|
return Number.isFinite(id) && id > 0 ? id : 0;
|
||||||
|
});
|
||||||
|
|
||||||
// Sidebar state for mobile
|
// Sidebar state for mobile
|
||||||
const isSidebarOpen = ref(false);
|
const isSidebarOpen = ref(false);
|
||||||
@@ -1047,7 +1050,7 @@ const buildFileSpecsForSend = async (): Promise<ChatUploadFileSpec[]> => {
|
|||||||
isUploadingAttachments.value = true;
|
isUploadingAttachments.value = true;
|
||||||
try {
|
try {
|
||||||
for (const item of locals) {
|
for (const item of locals) {
|
||||||
const uploaded = await uploadChatFile(agentId, item.file);
|
const uploaded = await uploadChatFile(currentAgentId.value, item.file);
|
||||||
if (!uploaded?.id) {
|
if (!uploaded?.id) {
|
||||||
throw new Error('Missing upload id');
|
throw new Error('Missing upload id');
|
||||||
}
|
}
|
||||||
@@ -1302,7 +1305,7 @@ const fetchLeaveAgents = async (isLoadMore = false) => {
|
|||||||
|
|
||||||
leaveAgentLoading.value = true;
|
leaveAgentLoading.value = true;
|
||||||
try {
|
try {
|
||||||
const data = await listOaEmployees(agentId, {
|
const data = await listOaEmployees(currentAgentId.value, {
|
||||||
keyword: leaveAgentKeyword.value,
|
keyword: leaveAgentKeyword.value,
|
||||||
page: leaveAgentPage.value,
|
page: leaveAgentPage.value,
|
||||||
page_size: 20,
|
page_size: 20,
|
||||||
@@ -1334,7 +1337,7 @@ const loadAllAgents = async () => {
|
|||||||
if (!leaveAgentHasMore.value || leaveAgentLoading.value) return;
|
if (!leaveAgentHasMore.value || leaveAgentLoading.value) return;
|
||||||
leaveAgentLoading.value = true;
|
leaveAgentLoading.value = true;
|
||||||
try {
|
try {
|
||||||
const data = await listOaEmployees(agentId, {
|
const data = await listOaEmployees(currentAgentId.value, {
|
||||||
keyword: leaveAgentKeyword.value,
|
keyword: leaveAgentKeyword.value,
|
||||||
page: 1,
|
page: 1,
|
||||||
page_size: 100000,
|
page_size: 100000,
|
||||||
@@ -1636,7 +1639,7 @@ const saveLocalMessageToBackend = async (msg: any, userQuery: string = '') => {
|
|||||||
content: msg.content,
|
content: msg.content,
|
||||||
payload: msg.payload,
|
payload: msg.payload,
|
||||||
query: userQuery,
|
query: userQuery,
|
||||||
agent_id: agentId,
|
agent_id: currentAgentId.value,
|
||||||
});
|
});
|
||||||
if (!conversationId.value && data.conversation_id) {
|
if (!conversationId.value && data.conversation_id) {
|
||||||
conversationId.value = data.conversation_id;
|
conversationId.value = data.conversation_id;
|
||||||
@@ -1745,7 +1748,7 @@ const startLocalLeaveFlow = async (content: string) => {
|
|||||||
try {
|
try {
|
||||||
const keyword = payload.form.applicantName || payload.form.employeeId;
|
const keyword = payload.form.applicantName || payload.form.employeeId;
|
||||||
if (keyword) {
|
if (keyword) {
|
||||||
const data = await listOaEmployees(agentId, { keyword, page: 1, page_size: 1 });
|
const data = await listOaEmployees(currentAgentId.value, { keyword, page: 1, page_size: 1 });
|
||||||
const match = (data.items || []).find((item: LeaveAgentOption) => item.employeeId === payload.form.employeeId);
|
const match = (data.items || []).find((item: LeaveAgentOption) => item.employeeId === payload.form.employeeId);
|
||||||
if (match) {
|
if (match) {
|
||||||
payload.form.fillerJobTitle = match.jobtitle || '';
|
payload.form.fillerJobTitle = match.jobtitle || '';
|
||||||
@@ -2059,6 +2062,7 @@ const displayHistory = computed<DisplayHistoryItem[]>(() => {
|
|||||||
const server = history.value || [];
|
const server = history.value || [];
|
||||||
const serverIds = new Set(server.map((h) => h.conversation_id));
|
const serverIds = new Set(server.map((h) => h.conversation_id));
|
||||||
const pending: DisplayHistoryItem[] = (runs.value || [])
|
const pending: DisplayHistoryItem[] = (runs.value || [])
|
||||||
|
.filter((r) => r.agentId === currentAgentId.value)
|
||||||
.filter((r) => r.runId !== activeRunId.value)
|
.filter((r) => r.runId !== activeRunId.value)
|
||||||
.map((r) => ({
|
.map((r) => ({
|
||||||
id: 0,
|
id: 0,
|
||||||
@@ -2079,7 +2083,7 @@ const handleHistorySelect = (item: DisplayHistoryItem) => {
|
|||||||
if (item.__runId) {
|
if (item.__runId) {
|
||||||
activateRun(item.__runId);
|
activateRun(item.__runId);
|
||||||
} else {
|
} else {
|
||||||
const run = runs.value.find((r) => r.conversationId === item.conversation_id);
|
const run = runs.value.find((r) => r.agentId === currentAgentId.value && r.conversationId === item.conversation_id);
|
||||||
if (run && run.status === 'running') {
|
if (run && run.status === 'running') {
|
||||||
activateRun(run.runId);
|
activateRun(run.runId);
|
||||||
} else {
|
} else {
|
||||||
@@ -2138,7 +2142,7 @@ const handleSend = async (textOrEvent?: string | Event) => {
|
|||||||
if (typeof textOrEvent !== 'string') input.value = '';
|
if (typeof textOrEvent !== 'string') input.value = '';
|
||||||
attachments.value = [];
|
attachments.value = [];
|
||||||
|
|
||||||
await sendMessage(content, agentId, selectedKb.value, files);
|
await sendMessage(content, currentAgentId.value, selectedKb.value, files);
|
||||||
|
|
||||||
if (!conversationId.value && history.value.length === 0) {
|
if (!conversationId.value && history.value.length === 0) {
|
||||||
await loadHistory();
|
await loadHistory();
|
||||||
@@ -2355,7 +2359,7 @@ const regenerate = () => {
|
|||||||
// 加载历史记录
|
// 加载历史记录
|
||||||
const loadHistory = async () => {
|
const loadHistory = async () => {
|
||||||
try {
|
try {
|
||||||
history.value = await listChatSessions(agentId);
|
history.value = await listChatSessions(currentAgentId.value);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to load history', e);
|
console.error('Failed to load history', e);
|
||||||
}
|
}
|
||||||
@@ -2364,7 +2368,7 @@ const loadHistory = async () => {
|
|||||||
const clearAllHistory = async () => {
|
const clearAllHistory = async () => {
|
||||||
openDeleteConfirmDialog(t('chat.confirmClearHistory'), async () => {
|
openDeleteConfirmDialog(t('chat.confirmClearHistory'), async () => {
|
||||||
try {
|
try {
|
||||||
await deleteAllChatSessions(agentId);
|
await deleteAllChatSessions(currentAgentId.value);
|
||||||
history.value = [];
|
history.value = [];
|
||||||
runs.value = [];
|
runs.value = [];
|
||||||
if (conversationId.value || activeRunId.value) {
|
if (conversationId.value || activeRunId.value) {
|
||||||
@@ -2380,7 +2384,7 @@ const deleteHistoryItem = async (id: string, event: Event) => {
|
|||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
openDeleteConfirmDialog(t('chat.confirmDeleteHistoryItem'), async () => {
|
openDeleteConfirmDialog(t('chat.confirmDeleteHistoryItem'), async () => {
|
||||||
try {
|
try {
|
||||||
await deleteChatSession(id, agentId);
|
await deleteChatSession(id, currentAgentId.value);
|
||||||
history.value = history.value.filter(h => h.conversation_id !== id);
|
history.value = history.value.filter(h => h.conversation_id !== id);
|
||||||
if (conversationId.value === id) {
|
if (conversationId.value === id) {
|
||||||
startNewChat();
|
startNewChat();
|
||||||
@@ -2397,7 +2401,7 @@ const loadConversation = async (id: string) => {
|
|||||||
try {
|
try {
|
||||||
activeRunId.value = null;
|
activeRunId.value = null;
|
||||||
isThinking.value = false;
|
isThinking.value = false;
|
||||||
messages.value = await listChatMessages(id, agentId);
|
messages.value = await listChatMessages(id, currentAgentId.value);
|
||||||
conversationId.value = id;
|
conversationId.value = id;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to load conversation', e);
|
console.error('Failed to load conversation', e);
|
||||||
@@ -2410,7 +2414,7 @@ const startNewChat = () => {
|
|||||||
|
|
||||||
const loadAgent = async () => {
|
const loadAgent = async () => {
|
||||||
try {
|
try {
|
||||||
currentAgent.value = await getUserAgent(agentId);
|
currentAgent.value = await getUserAgent(currentAgentId.value);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof BusinessError && e.status === 403) {
|
if (e instanceof BusinessError && e.status === 403) {
|
||||||
msg.error(t('workspace.permission.denied') || 'Access denied');
|
msg.error(t('workspace.permission.denied') || 'Access denied');
|
||||||
@@ -2426,7 +2430,7 @@ const loadUserAgents = async () => {
|
|||||||
try {
|
try {
|
||||||
const agents = await listUserAgents();
|
const agents = await listUserAgents();
|
||||||
userAgents.value = Array.isArray(agents)
|
userAgents.value = Array.isArray(agents)
|
||||||
? agents.filter((a: any) => a.id !== agentId && (a.dify_app_type || 'chatflow') === 'chatflow')
|
? agents.filter((a: any) => a.id !== currentAgentId.value && (a.dify_app_type || 'chatflow') === 'chatflow')
|
||||||
: [];
|
: [];
|
||||||
if (userAgents.value.length === 0) {
|
if (userAgents.value.length === 0) {
|
||||||
showAgentPanel.value = false;
|
showAgentPanel.value = false;
|
||||||
@@ -2438,10 +2442,9 @@ const loadUserAgents = async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const switchToAgent = (agent: { id: number; name: string }) => {
|
const switchToAgent = (agent: { id: number; name: string }) => {
|
||||||
if (agent.id === agentId) return;
|
if (agent.id === currentAgentId.value) return;
|
||||||
setLastChatAgentId(agent.id);
|
setLastChatAgentId(agent.id);
|
||||||
const url = router.resolve(`/chat/${agent.id}`).href;
|
router.push(`/chat/${agent.id}`);
|
||||||
openAgentChatTab(url, agent.id);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const syncOpenAgentRegistration = () => {
|
const syncOpenAgentRegistration = () => {
|
||||||
@@ -2454,16 +2457,63 @@ const onPageHide = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const goToWorkspace = (view?: string) => {
|
const goToWorkspace = (view?: string) => {
|
||||||
setLastChatAgentId(Number(route.params.agentId));
|
setLastChatAgentId(currentAgentId.value);
|
||||||
registerOpenAgentId(null);
|
registerOpenAgentId(null);
|
||||||
const query: Record<string, string> = { no_redirect: '1' };
|
const query: Record<string, string> = { no_redirect: '1' };
|
||||||
if (view) query.view = view;
|
if (view) query.view = view;
|
||||||
router.push({ path: '/workspace', query });
|
router.push({ path: '/workspace', query });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const resetChatStateForAgentChange = () => {
|
||||||
|
createNewChatView();
|
||||||
|
history.value = [];
|
||||||
|
attachments.value = [];
|
||||||
|
input.value = '';
|
||||||
|
currentAgent.value = null;
|
||||||
|
copiedIndex.value = null;
|
||||||
|
showAttachmentMenu.value = false;
|
||||||
|
filePreviewVisible.value = false;
|
||||||
|
previewFile.value = null;
|
||||||
|
};
|
||||||
|
|
||||||
|
let routeSyncToken = 0;
|
||||||
|
|
||||||
|
const syncChatStateForRoute = async () => {
|
||||||
|
const agentId = currentAgentId.value;
|
||||||
|
if (!agentId) return;
|
||||||
|
|
||||||
|
const syncToken = ++routeSyncToken;
|
||||||
|
resetChatStateForAgentChange();
|
||||||
|
stopVoiceInput();
|
||||||
|
setLastChatAgentId(agentId);
|
||||||
|
syncOpenAgentRegistration();
|
||||||
|
|
||||||
|
await syncCurrentUserProfile();
|
||||||
|
if (syncToken !== routeSyncToken) return;
|
||||||
|
|
||||||
|
await loadAgent();
|
||||||
|
if (syncToken !== routeSyncToken) return;
|
||||||
|
|
||||||
|
await loadUserAgents();
|
||||||
|
if (syncToken !== routeSyncToken) return;
|
||||||
|
|
||||||
|
await loadHistory();
|
||||||
|
if (syncToken !== routeSyncToken) return;
|
||||||
|
|
||||||
|
const convId = route.query.conversation_id as string;
|
||||||
|
if (convId) {
|
||||||
|
await loadConversation(convId);
|
||||||
|
if (syncToken !== routeSyncToken) return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (presetQuestion.value) {
|
||||||
|
input.value = presetQuestion.value;
|
||||||
|
await handleSend();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// 组件挂载时,如果存在预设问题,则自动触发一次发送
|
// 组件挂载时,如果存在预设问题,则自动触发一次发送
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
syncOpenAgentRegistration();
|
|
||||||
window.addEventListener('pagehide', onPageHide);
|
window.addEventListener('pagehide', onPageHide);
|
||||||
window.addEventListener('hxops:logout', onAuthLogout);
|
window.addEventListener('hxops:logout', onAuthLogout);
|
||||||
document.addEventListener('mousedown', handleDocumentPointerDown);
|
document.addEventListener('mousedown', handleDocumentPointerDown);
|
||||||
@@ -2471,33 +2521,23 @@ onMounted(async () => {
|
|||||||
document.addEventListener('mousedown', handleMobileAgentPopoverClickOutside);
|
document.addEventListener('mousedown', handleMobileAgentPopoverClickOutside);
|
||||||
document.addEventListener('touchstart', handleMobileAgentPopoverClickOutside, { passive: true });
|
document.addEventListener('touchstart', handleMobileAgentPopoverClickOutside, { passive: true });
|
||||||
window.addEventListener('resize', syncViewportWidth);
|
window.addEventListener('resize', syncViewportWidth);
|
||||||
await syncCurrentUserProfile();
|
await syncChatStateForRoute();
|
||||||
setLastChatAgentId(Number(route.params.agentId));
|
|
||||||
await loadAgent();
|
|
||||||
await loadUserAgents();
|
|
||||||
|
|
||||||
await loadHistory();
|
|
||||||
|
|
||||||
const convId = route.query.conversation_id as string;
|
|
||||||
if (convId) {
|
|
||||||
await loadConversation(convId);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (presetQuestion.value) {
|
|
||||||
input.value = presetQuestion.value;
|
|
||||||
handleSend();
|
|
||||||
}
|
|
||||||
// Use config from config.ts instead of API
|
// Use config from config.ts instead of API
|
||||||
showChatRating.value = config.workspace?.showChatRating ?? true;
|
showChatRating.value = config.workspace?.showChatRating ?? true;
|
||||||
scheduleScrollChatToBottom();
|
scheduleScrollChatToBottom();
|
||||||
});
|
});
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => route.params.agentId,
|
() => [route.params.agentId, route.query.conversation_id, route.query.preset],
|
||||||
(id) => {
|
async ([nextAgentId, nextConversationId, nextPreset], [prevAgentId, prevConversationId, prevPreset]) => {
|
||||||
const nextId = Number(id);
|
if (
|
||||||
if (Number.isFinite(nextId) && nextId > 0) setLastChatAgentId(nextId);
|
nextAgentId === prevAgentId
|
||||||
syncOpenAgentRegistration();
|
&& nextConversationId === prevConversationId
|
||||||
|
&& nextPreset === prevPreset
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await syncChatStateForRoute();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -3498,7 +3538,7 @@ const showChatRating = ref(false);
|
|||||||
<FilePreviewModal
|
<FilePreviewModal
|
||||||
v-model="filePreviewVisible"
|
v-model="filePreviewVisible"
|
||||||
:file="previewFile"
|
:file="previewFile"
|
||||||
:agent-id="agentId"
|
:agent-id="currentAgentId"
|
||||||
:auth-headers="getAuthHeaders()"
|
:auth-headers="getAuthHeaders()"
|
||||||
/>
|
/>
|
||||||
<GeneralFeedbackDialog
|
<GeneralFeedbackDialog
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ import {
|
|||||||
} from '../utils/wecom';
|
} from '../utils/wecom';
|
||||||
import { getAuthHeaders, getStoredAuthUser, saveAuthUser, logoutAuth } from '../utils/auth';
|
import { getAuthHeaders, getStoredAuthUser, saveAuthUser, logoutAuth } from '../utils/auth';
|
||||||
import { getLastChatAgentId } from '../utils/lastChatAgent';
|
import { getLastChatAgentId } from '../utils/lastChatAgent';
|
||||||
import { openAgentChatTab } from '../utils/agentTabs';
|
|
||||||
import {
|
import {
|
||||||
Settings,
|
Settings,
|
||||||
LayoutGrid,
|
LayoutGrid,
|
||||||
@@ -1113,8 +1112,7 @@ const navigateToChat = (id: number) => {
|
|||||||
msg.warning(t('login.loginRequired'));
|
msg.warning(t('login.loginRequired'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const url = router.resolve(`/chat/${id}`).href;
|
router.push(`/chat/${id}`);
|
||||||
openAgentChatTab(url, id);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const navigateWithQuickQuestion = (agentId: number, question: string) => {
|
const navigateWithQuickQuestion = (agentId: number, question: string) => {
|
||||||
@@ -1123,11 +1121,10 @@ const navigateWithQuickQuestion = (agentId: number, question: string) => {
|
|||||||
msg.warning(t('login.loginRequired'));
|
msg.warning(t('login.loginRequired'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const url = router.resolve({
|
router.push({
|
||||||
path: `/chat/${agentId}`,
|
path: `/chat/${agentId}`,
|
||||||
query: { preset: question },
|
query: { preset: question },
|
||||||
}).href;
|
});
|
||||||
openAgentChatTab(url, agentId);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleWecomDeeplinkContext = async () => {
|
const handleWecomDeeplinkContext = async () => {
|
||||||
@@ -1156,11 +1153,11 @@ const handleWecomDeeplinkContext = async () => {
|
|||||||
if (target === 'chat' && agentIdRaw && isLoggedIn.value) {
|
if (target === 'chat' && agentIdRaw && isLoggedIn.value) {
|
||||||
const agentId = Number(agentIdRaw);
|
const agentId = Number(agentIdRaw);
|
||||||
if (Number.isInteger(agentId) && agents.value.some((a) => a.id === agentId)) {
|
if (Number.isInteger(agentId) && agents.value.some((a) => a.id === agentId)) {
|
||||||
const url = router.resolve({
|
await router.push({
|
||||||
path: `/chat/${agentId}`,
|
path: `/chat/${agentId}`,
|
||||||
query: preset ? { preset } : {},
|
query: preset ? { preset } : {},
|
||||||
}).href;
|
});
|
||||||
openAgentChatTab(url, agentId);
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user