feat: integrate WeKnora llmwiki knowledge flow

This commit is contained in:
吉鑫升 committed 2026-08-11 18:13:30 +08:00
1 parent a0a75a9964
commit e0c09f6704
63 files changed
+7045 -197

No files matched your search

+110 -3
View File
@@ -16,6 +16,7 @@ import {
} from "@/components/workspace/chats";
import { ExportTrigger } from "@/components/workspace/export-trigger";
import { KnowledgeSaveTrigger } from "@/components/workspace/knowledge-save-trigger";
import { LlmWikiKnowledgeSelector } from "@/components/workspace/llmwiki-knowledge-selector";
import { InputBox } from "@/components/workspace/input-box";
import {
MessageList,
@@ -52,6 +53,7 @@ import {
useThreadSettings,
} from "@/core/settings";
import { useThreadStream } from "@/core/threads/hooks";
import { queueLlmWikiConversationDeposit } from "@/core/llmwiki/conversation-deposit";
import { useLayoutMode } from "@/core/system-settings/hooks";
import { useRouteBase } from "@/core/route-base";
import { textOfMessage } from "@/core/threads/utils";
@@ -121,12 +123,29 @@ export default function ChatPage() {
// In-memory only — explicitly overrides any value persisted by the old
// per-thread header toggle so stale localStorage can't re-enable it.
const [knowledgeRagEnabled] = useKnowledgeRagEnabled();
const [selectedKnowledgeBaseIds, setSelectedKnowledgeBaseIds] = useState<string[]>([]);
const [knowledgeSelectionReady, setKnowledgeSelectionReady] = useState(false);
const [knowledgeSelectionLoading, setKnowledgeSelectionLoading] = useState(false);
const knowledgeSelectionThread = useRef<string | null>(null);
const activeThreadIdRef = useRef<string | null | undefined>(threadId);
const changeKnowledgeSelection = useCallback((ids: string[]) => {
setSelectedKnowledgeBaseIds(ids);
setKnowledgeSelectionReady(true);
}, []);
const streamContext = useMemo(
() => ({
...settings.context,
knowledge_rag_enabled: knowledgeRagEnabled,
...(knowledgeSelectionReady
? { llmwiki_knowledge_base_ids: selectedKnowledgeBaseIds }
: {}),
}),
[settings.context, knowledgeRagEnabled],
[
settings.context,
knowledgeRagEnabled,
knowledgeSelectionReady,
selectedKnowledgeBaseIds,
],
);
const [mounted, setMounted] = useState(false);
const { tokenUsageEnabled } = useModels();
@@ -150,6 +169,10 @@ export default function ChatPage() {
const { selectedArtifact } = useArtifacts();
useSpecificChatMode();
useEffect(() => {
activeThreadIdRef.current = threadId;
}, [threadId]);
useEffect(() => {
setMounted(true);
}, []);
@@ -158,12 +181,54 @@ export default function ChatPage() {
if (isNewThread) {
setHasStartedConversation(false);
setPromptPrefixOn(false);
setSelectedKnowledgeBaseIds([]);
setKnowledgeSelectionReady(false);
setKnowledgeSelectionLoading(false);
knowledgeSelectionThread.current = "__new__";
// Pre-select the agent the user last started a conversation with so a
// fresh chat defaults to it. Validated against the known agent list below.
setSelectedAgentId(getLastAgentId());
}
}, [isNewThread, threadId]);
// The knowledge-base choice belongs to the conversation, not to a browser
// component instance. Restore it from LangGraph thread metadata whenever an
// existing conversation is opened. An explicit empty array means the user
// deliberately disabled LLMWiki retrieval for that conversation.
useEffect(() => {
if (isNewThread || !threadId || knowledgeSelectionThread.current === threadId) {
return;
}
knowledgeSelectionThread.current = threadId;
setKnowledgeSelectionLoading(true);
void getAPIClient(isMock)
.threads.get(threadId)
.then((record) => {
if (knowledgeSelectionThread.current !== threadId) return;
const metadata = record.metadata as Record<string, unknown> | undefined;
const raw = metadata?.llmwiki_knowledge_base_ids;
if (Array.isArray(raw)) {
setSelectedKnowledgeBaseIds(
raw.map((item) => String(item).trim()).filter(Boolean),
);
setKnowledgeSelectionReady(true);
} else {
setSelectedKnowledgeBaseIds([]);
setKnowledgeSelectionReady(false);
}
})
.catch(() => {
if (knowledgeSelectionThread.current === threadId) {
setKnowledgeSelectionReady(false);
}
})
.finally(() => {
if (knowledgeSelectionThread.current === threadId) {
setKnowledgeSelectionLoading(false);
}
});
}, [isMock, isNewThread, threadId]);
// Drop a restored last-used agent that is no longer in the selector list
// (deleted / unpublished / no longer curated), so we never hand off to a
// dead agent. Wait for the list to load before deciding.
@@ -175,6 +240,8 @@ export default function ChatPage() {
if (known.length === 0) return;
if (!known.some((a) => a.id === selectedAgentId)) {
setSelectedAgentId(null);
setSelectedKnowledgeBaseIds([]);
setKnowledgeSelectionReady(false);
}
}, [selectedAgentId, selectorAgents, mineAgents, squareAgents, selectorLoading]);
@@ -183,8 +250,13 @@ export default function ChatPage() {
const [thread, sendMessage, isUploading] = useThreadStream({
threadId: isNewThread ? undefined : threadId,
context: streamContext,
threadMetadata: knowledgeSelectionReady
? { llmwiki_knowledge_base_ids: selectedKnowledgeBaseIds }
: undefined,
isMock,
onStart: (createdThreadId) => {
knowledgeSelectionThread.current = createdThreadId;
activeThreadIdRef.current = createdThreadId;
setThreadId(createdThreadId);
setIsNewThread(false);
// Keep the current component mounted while updating the hash route.
@@ -210,6 +282,12 @@ export default function ChatPage() {
} catch {
/* 上报绝不影响问答 */
}
queueLlmWikiConversationDeposit({
threadId: activeThreadIdRef.current,
messages: state.messages,
assistantId: selectedAgentId,
knowledgeBaseIds: selectedKnowledgeBaseIds,
});
if (document.hidden || !document.hasFocus()) {
let body = "Conversation finished";
const lastMessage = state.messages.at(-1);
@@ -262,7 +340,12 @@ export default function ChatPage() {
void (async () => {
try {
const created = await getAPIClient(isMock).threads.create({
metadata: { agent_id: selectedAgentId },
metadata: {
agent_id: selectedAgentId,
...(knowledgeSelectionReady
? { llmwiki_knowledge_base_ids: selectedKnowledgeBaseIds }
: {}),
},
});
sessionStorage.setItem(
"pendingAgentMessage",
@@ -270,6 +353,9 @@ export default function ChatPage() {
agentId: selectedAgentId,
threadId: created.thread_id,
text,
...(knowledgeSelectionReady
? { knowledgeBaseIds: selectedKnowledgeBaseIds }
: {}),
}),
);
saveLastAgentId(selectedAgentId);
@@ -328,6 +414,8 @@ export default function ChatPage() {
hideAdminPrompt,
preferencesLoading,
selectedAgentId,
selectedKnowledgeBaseIds,
knowledgeSelectionReady,
agentHandoffPending,
isMock,
router,
@@ -418,6 +506,8 @@ export default function ChatPage() {
// Passing null (the ✕ clear) wipes the stored preference.
const handleAgentChange = useCallback((agentId: string | null) => {
setSelectedAgentId(agentId);
setSelectedKnowledgeBaseIds([]);
setKnowledgeSelectionReady(false);
saveLastAgentId(agentId);
}, []);
const handleStop = useCallback(async () => {
@@ -620,7 +710,8 @@ export default function ChatPage() {
disabled={
env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true" ||
isUploading ||
agentHandoffPending
agentHandoffPending ||
knowledgeSelectionLoading
}
onContextChange={(context) =>
setSettings("context", context)
@@ -634,6 +725,22 @@ export default function ChatPage() {
}
: undefined
}
knowledgeSelector={
isLandingState ? (
<LlmWikiKnowledgeSelector
source={selectedAgentId ? "agent" : "all"}
agentId={selectedAgentId}
value={selectedKnowledgeBaseIds}
onChange={changeKnowledgeSelection}
autoSelectAll={!!selectedAgentId}
disabled={
env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true" ||
knowledgeSelectionLoading
}
label="知识空间"
/>
) : undefined
}
onFollowupsVisibilityChange={setShowFollowups}
onSubmit={handleSubmit}
onStop={handleStop}