feat: complete public wiki and embedded previews
This commit is contained in:
1 parent
bc49df4561
commit
30943159bf
18 files changed
+926
-48
No files matched your search
@@ -144,6 +144,27 @@ export interface LlmWikiWikiPageList {
|
||||
total_pages: number;
|
||||
}
|
||||
|
||||
export interface LlmWikiWikiIndexItem {
|
||||
slug: string;
|
||||
title: string;
|
||||
summary: string;
|
||||
category_path: string[];
|
||||
wiki_path: string;
|
||||
depth: number;
|
||||
}
|
||||
|
||||
export interface LlmWikiWikiIndexGroup {
|
||||
type: string;
|
||||
total: number;
|
||||
items: LlmWikiWikiIndexItem[];
|
||||
}
|
||||
|
||||
export interface LlmWikiWikiIndex {
|
||||
intro: string;
|
||||
version: number;
|
||||
groups: LlmWikiWikiIndexGroup[];
|
||||
}
|
||||
|
||||
export interface LlmWikiWikiIndexStatus {
|
||||
knowledge_base_id: string;
|
||||
state: "not_synced" | "idle" | "running" | "partial" | "failed" | "rebuilding" | "rebuild_required";
|
||||
@@ -710,6 +731,17 @@ export async function getPublicLlmWikiWikiPage(
|
||||
);
|
||||
}
|
||||
|
||||
export async function getPublicLlmWikiWikiIndex(
|
||||
knowledgeBaseId: string,
|
||||
): Promise<LlmWikiWikiIndex> {
|
||||
return readJson(
|
||||
await apiFetch(
|
||||
`${PUBLIC_API()}/knowledge-bases/${encodeURIComponent(knowledgeBaseId)}/wiki/index`,
|
||||
),
|
||||
"读取公开 Wiki 索引",
|
||||
);
|
||||
}
|
||||
|
||||
export async function createLlmWikiConversationDeposit(input: {
|
||||
thread_id: string;
|
||||
question: string;
|
||||
@@ -939,6 +971,18 @@ export function usePublicLlmWikiWikiPage(
|
||||
});
|
||||
}
|
||||
|
||||
export function usePublicLlmWikiWikiIndex(
|
||||
knowledgeBaseId: string | null,
|
||||
enabled = true,
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: ["public-llmwiki", "wiki-index", knowledgeBaseId],
|
||||
queryFn: () => getPublicLlmWikiWikiIndex(knowledgeBaseId!),
|
||||
enabled: enabled && !!knowledgeBaseId,
|
||||
staleTime: 60_000,
|
||||
});
|
||||
}
|
||||
|
||||
export function useLlmWikiWikiIndexStatus(
|
||||
knowledgeBaseId: string | null,
|
||||
enabled = true,
|
||||
|
||||
-1
@@ -273,7 +273,6 @@ export function WeKnoraKnowledgeDetailPage() {
|
||||
title={`${details.data.name || "知识库"} 详情`}
|
||||
src={frameSrc}
|
||||
className="h-full min-h-0 w-full flex-1 border-0 bg-background"
|
||||
sandbox="allow-same-origin allow-scripts allow-forms allow-downloads allow-popups allow-popups-to-escape-sandbox"
|
||||
referrerPolicy="no-referrer"
|
||||
onLoad={() => postThemeToFrame(frameRef.current)}
|
||||
/>
|
||||
|
||||
+57
-3
@@ -21,12 +21,15 @@ import {
|
||||
} from "@/components/ui/sheet";
|
||||
import { apiFetch } from "@/core/api/fetch-client";
|
||||
import { getBackendBaseURL } from "@/core/config";
|
||||
import type { WeKnoraSourceTarget } from "@/core/llmwiki/source-links";
|
||||
import {
|
||||
buildWeKnoraWikiPagePath,
|
||||
parseWeKnoraPreviewUrl,
|
||||
type WeKnoraSourceTarget,
|
||||
} from "@/core/llmwiki/source-links";
|
||||
import { SpreadsheetPreview } from "@/components/workspace/artifacts/spreadsheet-preview";
|
||||
import { MarkdownContent } from "@/components/workspace/messages/markdown-content";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { reasoningPlugins } from "@/core/streamdown";
|
||||
import { buildWeKnoraWikiPagePath } from "@/core/llmwiki/source-links";
|
||||
import {
|
||||
exportableWikiMarkdown,
|
||||
renderableWikiMarkdown,
|
||||
@@ -81,6 +84,14 @@ type CachedSourceDetail = {
|
||||
|
||||
const sourceDetailCache = new Map<string, CachedSourceDetail>();
|
||||
|
||||
function buildDrawerWikiPageUrl(knowledgeBaseId: string, wikiSlug: string) {
|
||||
const params = new URLSearchParams({
|
||||
knowledge_base_id: knowledgeBaseId,
|
||||
wiki_slug: wikiSlug,
|
||||
});
|
||||
return `/weknora-source-preview?${params.toString()}`;
|
||||
}
|
||||
|
||||
async function getAvailableWikiPage(
|
||||
knowledgeBaseId: string,
|
||||
wikiSlug: string,
|
||||
@@ -539,6 +550,7 @@ export function WeKnoraSourceDrawer({
|
||||
const [downloading, setDownloading] = useState(false);
|
||||
const [wikiDownload, setWikiDownload] = useState<"md" | "word" | null>(null);
|
||||
const [resolvedDocumentId, setResolvedDocumentId] = useState("");
|
||||
const wikiNavigationRequestRef = useRef(0);
|
||||
|
||||
const targetKey = useMemo(() => {
|
||||
if (!target) return "";
|
||||
@@ -690,7 +702,12 @@ export function WeKnoraSourceDrawer({
|
||||
const summary = cleanText(wikiPage?.summary || document?.description);
|
||||
const wikiContent = stripWikiFrontmatter(cleanText(wikiPage?.content));
|
||||
const wikiDisplayContent = wikiPage && target
|
||||
? renderableWikiMarkdown(resolvedTitle, wikiContent, target.knowledgeBaseId)
|
||||
? renderableWikiMarkdown(
|
||||
resolvedTitle,
|
||||
wikiContent,
|
||||
target.knowledgeBaseId,
|
||||
buildDrawerWikiPageUrl,
|
||||
)
|
||||
: "";
|
||||
const wikiMarkdown = wikiPage
|
||||
? wikiLinkMarkdown(
|
||||
@@ -703,6 +720,32 @@ export function WeKnoraSourceDrawer({
|
||||
? buildWeKnoraWikiPagePath(target.knowledgeBaseId, resolvedWikiSlug)
|
||||
: "";
|
||||
|
||||
const openWikiPageInDrawer = async (linkedTarget: WeKnoraSourceTarget) => {
|
||||
if (!linkedTarget.wikiSlug) return;
|
||||
const requestId = ++wikiNavigationRequestRef.current;
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const page = await getAvailableWikiPage(
|
||||
linkedTarget.knowledgeBaseId,
|
||||
linkedTarget.wikiSlug,
|
||||
);
|
||||
if (requestId === wikiNavigationRequestRef.current) {
|
||||
setWikiPage(page);
|
||||
setDocument(null);
|
||||
setResolvedDocumentId("");
|
||||
setPreview({ kind: "none" });
|
||||
setPreviewError("");
|
||||
}
|
||||
} catch (error) {
|
||||
if (requestId === wikiNavigationRequestRef.current) {
|
||||
setError(error instanceof Error ? error.message : "无法打开 Wiki 页面");
|
||||
}
|
||||
} finally {
|
||||
if (requestId === wikiNavigationRequestRef.current) setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const downloadOriginal = async () => {
|
||||
if (!target || !resolvedDocumentId) return;
|
||||
setDownloading(true);
|
||||
@@ -925,6 +968,17 @@ export function WeKnoraSourceDrawer({
|
||||
isLoading={false}
|
||||
remarkPlugins={reasoningPlugins.remarkPlugins}
|
||||
rehypePlugins={reasoningPlugins.rehypePlugins}
|
||||
linkClassName={(href) =>
|
||||
parseWeKnoraPreviewUrl(href)?.wikiSlug
|
||||
? "wiki-content-link font-medium text-[#be2323] decoration-[#be2323]/55 decoration-dashed hover:decoration-solid"
|
||||
: undefined
|
||||
}
|
||||
onLinkClick={(event, href) => {
|
||||
const linkedTarget = parseWeKnoraPreviewUrl(href);
|
||||
if (!linkedTarget?.wikiSlug) return;
|
||||
event.preventDefault();
|
||||
void openWikiPageInDrawer(linkedTarget);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
+59
-13
@@ -25,6 +25,8 @@ import {
|
||||
import {
|
||||
buildWikiTree,
|
||||
filterWikiPagesWithAncestors,
|
||||
isWikiIndexPage,
|
||||
selectDefaultWikiPage,
|
||||
type WikiTreeNode,
|
||||
} from "@/core/llmwiki/wiki-tree";
|
||||
import { usePublicBackground } from "@/core/embed/background";
|
||||
@@ -43,6 +45,7 @@ import {
|
||||
useLlmWikiWikiPage,
|
||||
useLlmWikiWikiPages,
|
||||
usePublicLlmWikiKnowledgeBase,
|
||||
usePublicLlmWikiWikiIndex,
|
||||
usePublicLlmWikiWikiPage,
|
||||
usePublicLlmWikiWikiPages,
|
||||
} from "@/strategy-components/api/llmwiki";
|
||||
@@ -92,13 +95,13 @@ function downloadBlob(blob: Blob, fileName: string) {
|
||||
window.setTimeout(() => URL.revokeObjectURL(url), 30_000);
|
||||
}
|
||||
|
||||
function selectLandingPage(pages: LlmWikiWikiPage[]): LlmWikiWikiPage | undefined {
|
||||
return (
|
||||
pages.find((page) => page.page_type?.toLowerCase() === "overview") ||
|
||||
pages.find((page) => /^(?:index|home|readme)$/i.test(page.slug)) ||
|
||||
pages.find((page) => !page.parent_slug && (page.depth ?? 0) === 0) ||
|
||||
pages[0]
|
||||
);
|
||||
function publicWikiTargetFromHref(href: string, knowledgeBaseId: string) {
|
||||
const routePrefix = `/#${buildPublicKnowledgeWikiPath(knowledgeBaseId)}`;
|
||||
if (!href.startsWith(`${routePrefix}?`)) return null;
|
||||
const route = href.slice(2);
|
||||
const queryIndex = route.indexOf("?");
|
||||
const slug = new URLSearchParams(route.slice(queryIndex + 1)).get("wikiSlug")?.trim();
|
||||
return slug ? { route, slug } : null;
|
||||
}
|
||||
|
||||
export function WeKnoraWikiPageView({
|
||||
@@ -137,7 +140,11 @@ export function WeKnoraWikiPageView({
|
||||
);
|
||||
const pages = publicEmbed ? publicPages : privatePages;
|
||||
const pagesList = useMemo(() => pages.data?.pages || [], [pages.data?.pages]);
|
||||
const landingPage = useMemo(() => selectLandingPage(pagesList), [pagesList]);
|
||||
const indexPage = useMemo(
|
||||
() => (publicEmbed ? pagesList.find(isWikiIndexPage) : undefined),
|
||||
[pagesList, publicEmbed],
|
||||
);
|
||||
const landingPage = useMemo(() => selectDefaultWikiPage(pagesList), [pagesList]);
|
||||
const wikiSlug = requestedWikiSlug || (publicEmbed ? landingPage?.slug || "" : "");
|
||||
const privatePage = useLlmWikiWikiPage(
|
||||
noteId || null,
|
||||
@@ -150,6 +157,10 @@ export function WeKnoraWikiPageView({
|
||||
!!noteId && !!wikiSlug && publicEmbed,
|
||||
);
|
||||
const page = publicEmbed ? publicPage : privatePage;
|
||||
const publicWikiIndex = usePublicLlmWikiWikiIndex(
|
||||
noteId || null,
|
||||
!!noteId && publicEmbed && !!indexPage && wikiSlug === indexPage.slug,
|
||||
);
|
||||
const [downloading, setDownloading] = useState<"md" | "word" | null>(null);
|
||||
const [directoryQuery, setDirectoryQuery] = useState("");
|
||||
const [collapsedDirectories, setCollapsedDirectories] = useState<Set<string>>(() => new Set());
|
||||
@@ -159,7 +170,12 @@ export function WeKnoraWikiPageView({
|
||||
() => filterWikiPagesWithAncestors(pagesList, directoryQuery),
|
||||
[directoryQuery, pagesList],
|
||||
);
|
||||
const wikiTree = useMemo(() => buildWikiTree(filteredPages), [filteredPages]);
|
||||
const wikiTree = useMemo(
|
||||
() => buildWikiTree(filteredPages, {
|
||||
preferredRootPageSlug: publicEmbed ? indexPage?.slug : undefined,
|
||||
}),
|
||||
[filteredPages, indexPage?.slug, publicEmbed],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!publicEmbed || requestedWikiSlug || !wikiSlug) return;
|
||||
@@ -182,8 +198,16 @@ export function WeKnoraWikiPageView({
|
||||
}, [wikiSlug, wikiTree.parentKeysBySlug]);
|
||||
|
||||
const title = pageData?.title || wikiSlug || "Wiki 页面";
|
||||
const summary = cleanText(pageData?.summary);
|
||||
const content = stripWikiFrontmatter(cleanText(pageData?.content));
|
||||
const displayTitle = publicEmbed && pageData && isWikiIndexPage(pageData)
|
||||
? "索引"
|
||||
: title;
|
||||
const currentPageIsIndex = !!pageData && isWikiIndexPage(pageData);
|
||||
const summary = publicEmbed && currentPageIsIndex ? "" : cleanText(pageData?.summary);
|
||||
const content = stripWikiFrontmatter(cleanText(
|
||||
publicEmbed && currentPageIsIndex && publicWikiIndex.data?.intro
|
||||
? publicWikiIndex.data.intro
|
||||
: pageData?.content,
|
||||
));
|
||||
const displayMarkdown = pageData
|
||||
? renderableWikiMarkdown(
|
||||
title,
|
||||
@@ -196,6 +220,8 @@ export function WeKnoraWikiPageView({
|
||||
return `/#${buildPublicKnowledgeWikiPath(knowledgeBaseId)}?${next.toString()}`;
|
||||
}
|
||||
: undefined,
|
||||
publicEmbed ? pagesList : [],
|
||||
publicEmbed ? publicWikiIndex.data?.groups || [] : [],
|
||||
)
|
||||
: "";
|
||||
const exportMarkdown = pageData
|
||||
@@ -239,7 +265,11 @@ export function WeKnoraWikiPageView({
|
||||
const hasChildren = node.children.length > 0;
|
||||
const expanded = directoryQuery.trim() ? true : !collapsedDirectories.has(node.key);
|
||||
const active = node.page?.slug === wikiSlug;
|
||||
const label = node.page ? node.label : directoryLabel(node.label);
|
||||
const label = node.page
|
||||
? publicEmbed && isWikiIndexPage(node.page)
|
||||
? "索引"
|
||||
: node.label
|
||||
: directoryLabel(node.label);
|
||||
return (
|
||||
<div key={node.key}>
|
||||
<div
|
||||
@@ -372,7 +402,7 @@ export function WeKnoraWikiPageView({
|
||||
<h1 className="truncate text-sm font-semibold text-foreground">
|
||||
{knowledgeBase.data?.name || "知识库 Wiki"}
|
||||
</h1>
|
||||
<p className="mt-0.5 truncate text-xs text-muted-foreground">{title}</p>
|
||||
<p className="mt-0.5 truncate text-xs text-muted-foreground">{displayTitle}</p>
|
||||
</div>
|
||||
<Badge variant="outline" className="shrink-0 border-emerald-200 text-emerald-700 dark:border-emerald-800 dark:text-emerald-300">
|
||||
公开 Wiki
|
||||
@@ -467,6 +497,22 @@ export function WeKnoraWikiPageView({
|
||||
isLoading={false}
|
||||
remarkPlugins={reasoningPlugins.remarkPlugins}
|
||||
rehypePlugins={reasoningPlugins.rehypePlugins}
|
||||
linkClassName={(href) =>
|
||||
publicEmbed && publicWikiTargetFromHref(href, noteId)
|
||||
? "wiki-content-link border-b border-dashed !border-[var(--primary)] font-medium !text-primary !no-underline hover:border-solid focus-visible:rounded-sm focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
|
||||
: undefined
|
||||
}
|
||||
onLinkClick={(event, href) => {
|
||||
if (!publicEmbed) return;
|
||||
const target = publicWikiTargetFromHref(href, noteId);
|
||||
if (!target) return;
|
||||
event.preventDefault();
|
||||
navigate(target.route, {
|
||||
state: {
|
||||
wikiPage: pagesList.find((item) => item.slug === target.slug),
|
||||
},
|
||||
});
|
||||
}}
|
||||
/>
|
||||
</section>
|
||||
</article>
|
||||
|
||||
Reference in new issue
Block a user