fix: navigate chat references to document folder

main
Binks Riverton 2026-08-20 14:13:28 +08:00 committed by lyingbug
parent 28dd15ec30
commit 1c88779016
2 changed files with 49 additions and 12 deletions

View File

@ -1224,18 +1224,37 @@ const pendingKnowledgeId = ref<string | null>(
(route.query.knowledge_id as string) || null
);
const tryAutoOpenDocument = () => {
if (!pendingKnowledgeId.value || !cardList.value?.length) return;
let autoOpenRequest = 0;
const tryAutoOpenDocument = async () => {
if (!pendingKnowledgeId.value) return;
const targetId = pendingKnowledgeId.value;
pendingKnowledgeId.value = null;
const request = ++autoOpenRequest;
const card = cardList.value.find((c: KnowledgeCard) => c.id === targetId);
if (card) {
nextTick(() => openCardDetails(card));
} else {
nextTick(() => {
openCardDetails({ id: targetId } as KnowledgeCard);
});
// The current card list only contains the folder being browsed. A document
// opened from chat references may live in any nested folder, so resolve its
// folder from the detail endpoint before opening the drawer. Otherwise the
// drawer opens correctly while the page misleadingly remains at KB root.
let target = card || ({ id: targetId } as KnowledgeCard);
try {
const response: any = await getKnowledgeDetails(targetId);
if (request !== autoOpenRequest) return;
const detail = response?.data || response;
if (detail && typeof detail === 'object') {
target = { ...target, ...detail, id: targetId } as KnowledgeCard;
selectedFolderPath.value = detail.folder_path || ROOT_FOLDER_PATH;
}
} catch (error) {
// Keep the previous ID-only fallback: getCardDetails will surface the
// normal detail loading error, while links to root-level files still work.
console.error('Failed to resolve referenced document folder', error);
}
if (request !== autoOpenRequest) return;
await nextTick();
openCardDetails(target);
};
// React to later ?knowledge_id= changes on the same KB route (no remount).
@ -1244,8 +1263,6 @@ watch(
(newId) => {
if (typeof newId !== 'string' || !newId) return;
pendingKnowledgeId.value = newId;
// cardList is almost always already loaded at this point; if not, the
// cardList watcher below will pick it up.
tryAutoOpenDocument();
},
);
@ -1286,8 +1303,8 @@ watch(() => cardList.value, (newValue) => {
if (isFAQ.value) return;
docListLoading.value = false;
// Auto-open document if navigated with ?knowledge_id=xxx
if (pendingKnowledgeId.value && newValue?.length) {
// Auto-open document if navigated with ?knowledge_id=xxx.
if (pendingKnowledgeId.value) {
tryAutoOpenDocument();
}

View File

@ -0,0 +1,20 @@
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
const source = readFileSync(new URL('./KnowledgeBase.vue', import.meta.url), 'utf8')
test('referenced documents navigate to their containing folder before opening details', () => {
const resolveFolder = source.indexOf('await getKnowledgeDetails(targetId)')
const selectFolder = source.indexOf('selectedFolderPath.value = detail.folder_path || ROOT_FOLDER_PATH')
const openDetails = source.indexOf('openCardDetails(target)', selectFolder)
assert.ok(resolveFolder >= 0, 'document details should be fetched to resolve folder_path')
assert.ok(selectFolder > resolveFolder, 'the containing folder should be selected after details load')
assert.ok(openDetails > selectFolder, 'the detail drawer should open after folder navigation')
})
test('a newer referenced-document navigation supersedes an older request', () => {
assert.match(source, /const request = \+\+autoOpenRequest/)
assert.match(source, /if \(request !== autoOpenRequest\) return;/)
})