feat: improve WeKnora source preview and wiki management
This commit is contained in:
1 parent
94cf37a2ae
commit
3ab2c9b282
19 files changed
+2602
-303
No files matched your search
@@ -0,0 +1,125 @@
|
||||
import { readRuntimeEnv } from "@/env";
|
||||
|
||||
export type WeKnoraSourceTarget = {
|
||||
knowledgeBaseId: string;
|
||||
chunkId?: string;
|
||||
documentId?: string;
|
||||
wikiSlug?: string;
|
||||
tab?: "documents" | "wiki" | "graph";
|
||||
};
|
||||
|
||||
export type KnowledgeSourceLike = {
|
||||
url?: string | null;
|
||||
recUuid?: string | null;
|
||||
rec_uuid?: string | null;
|
||||
knowledge_base_id?: string | null;
|
||||
knowledgeBaseId?: string | null;
|
||||
mapping_id?: string | null;
|
||||
mappingId?: string | null;
|
||||
chunk_id?: string | null;
|
||||
chunkId?: string | null;
|
||||
knowledge_id?: string | null;
|
||||
knowledgeId?: string | null;
|
||||
document_id?: string | null;
|
||||
documentId?: string | null;
|
||||
wiki_slug?: string | null;
|
||||
wikiSlug?: string | null;
|
||||
slug?: string | null;
|
||||
};
|
||||
|
||||
export function parseWeKnoraPreviewUrl(url?: string | null): WeKnoraSourceTarget | null {
|
||||
if (!url?.startsWith("/weknora-source-preview?")) {
|
||||
return null;
|
||||
}
|
||||
const queryIndex = url.indexOf("?");
|
||||
const params = new URLSearchParams(url.slice(queryIndex + 1));
|
||||
const knowledgeBaseId = params.get("knowledge_base_id")?.trim();
|
||||
const chunkId = params.get("chunk_id")?.trim();
|
||||
const documentId =
|
||||
params.get("document_id")?.trim() ||
|
||||
params.get("knowledge_id")?.trim() ||
|
||||
"";
|
||||
const wikiSlug =
|
||||
params.get("wiki_slug")?.trim() ||
|
||||
params.get("wikiSlug")?.trim() ||
|
||||
params.get("slug")?.trim() ||
|
||||
"";
|
||||
return knowledgeBaseId && chunkId
|
||||
? {
|
||||
knowledgeBaseId,
|
||||
chunkId,
|
||||
documentId: documentId || undefined,
|
||||
wikiSlug: wikiSlug || undefined,
|
||||
tab: wikiSlug ? "wiki" : "documents",
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
export function resolveWeKnoraSourceTarget(source: KnowledgeSourceLike): WeKnoraSourceTarget | null {
|
||||
const fromPreview = parseWeKnoraPreviewUrl(source.url);
|
||||
const knowledgeBaseId =
|
||||
source.knowledge_base_id ||
|
||||
source.knowledgeBaseId ||
|
||||
source.mapping_id ||
|
||||
source.mappingId ||
|
||||
fromPreview?.knowledgeBaseId ||
|
||||
"";
|
||||
if (!knowledgeBaseId) return null;
|
||||
|
||||
const wikiSlug = source.wiki_slug || source.wikiSlug || source.slug || fromPreview?.wikiSlug || "";
|
||||
const documentId =
|
||||
source.document_id ||
|
||||
source.documentId ||
|
||||
source.knowledge_id ||
|
||||
source.knowledgeId ||
|
||||
fromPreview?.documentId ||
|
||||
"";
|
||||
const chunkId = source.chunk_id || source.chunkId || fromPreview?.chunkId || "";
|
||||
return {
|
||||
knowledgeBaseId,
|
||||
chunkId: chunkId || undefined,
|
||||
documentId: documentId || undefined,
|
||||
wikiSlug: wikiSlug || undefined,
|
||||
tab: wikiSlug ? "wiki" : "documents",
|
||||
};
|
||||
}
|
||||
|
||||
export function buildWeKnoraSourceIframeUrl(target: WeKnoraSourceTarget): string {
|
||||
const params = new URLSearchParams();
|
||||
if (target.tab) params.set("tab", target.tab);
|
||||
if (target.chunkId) params.set("sourceChunkId", target.chunkId);
|
||||
if (target.documentId) params.set("documentId", target.documentId);
|
||||
if (target.wikiSlug) params.set("wikiSlug", target.wikiSlug);
|
||||
const query = params.toString();
|
||||
return `/#/page/workspace/knowledge/${encodeURIComponent(target.knowledgeBaseId)}${query ? `?${query}` : ""}`;
|
||||
}
|
||||
|
||||
export function buildLegacyKnowledgeSourceUrl(source: KnowledgeSourceLike): string {
|
||||
const recUuid = source.recUuid || source.rec_uuid || source.url || "";
|
||||
if (!recUuid) return "";
|
||||
if (/^https?:\/\//i.test(recUuid)) return recUuid;
|
||||
const base = readRuntimeEnv("VITE_AIPI_KNOWLEDGE_URL");
|
||||
if (base) {
|
||||
return `${base.replace(/\/+$/, "")}/#/knowledge/mid-detail?recUuid=${encodeURIComponent(recUuid)}`;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
export function openKnowledgeSource(source: KnowledgeSourceLike): boolean {
|
||||
const weknoraTarget = resolveWeKnoraSourceTarget(source);
|
||||
const href = weknoraTarget
|
||||
? buildWeKnoraSourceIframeUrl(weknoraTarget)
|
||||
: buildLegacyKnowledgeSourceUrl(source);
|
||||
if (!href) return false;
|
||||
window.open(href, "_blank", "noopener,noreferrer");
|
||||
return true;
|
||||
}
|
||||
|
||||
export function normalizeMalformedWeKnoraHashRoute(): boolean {
|
||||
if (typeof window === "undefined") return false;
|
||||
const { pathname, search, hash } = window.location;
|
||||
if (!pathname.startsWith("/page/workspace/knowledge/")) return false;
|
||||
if (hash && !hash.startsWith("#/")) return false;
|
||||
window.location.replace(`/#${pathname}${search}`);
|
||||
return true;
|
||||
}
|
||||
Reference in new issue
Block a user