177 lines
5.9 KiB
TypeScript
177 lines
5.9 KiB
TypeScript
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;
|
|
};
|
|
|
|
/** Build a HashRouter URL without dropping a reverse-proxy path such as /web/. */
|
|
export function buildAppHashUrl(path: string): string {
|
|
const route = path.startsWith("/") ? path : `/${path}`;
|
|
if (typeof window !== "undefined") {
|
|
return `${window.location.pathname || "/"}#${route}`;
|
|
}
|
|
const base = import.meta.env.BASE_URL || "/";
|
|
return `${base}#${route}`;
|
|
}
|
|
|
|
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 && (wikiSlug || chunkId || documentId)
|
|
? {
|
|
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 || "";
|
|
if (!wikiSlug && !chunkId && !documentId) return null;
|
|
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 buildAppHashUrl(`/page/workspace/knowledge/${encodeURIComponent(target.knowledgeBaseId)}${query ? `?${query}` : ""}`);
|
|
}
|
|
|
|
/**
|
|
* Lightweight native reader for one Wiki page. Unlike the knowledge-base
|
|
* detail route this does not create a WeKnora iframe session.
|
|
*/
|
|
export function buildWeKnoraWikiPageUrl(
|
|
knowledgeBaseId: string,
|
|
wikiSlug: string,
|
|
): string {
|
|
return buildAppHashUrl(buildWeKnoraWikiPagePath(knowledgeBaseId, wikiSlug));
|
|
}
|
|
|
|
export function buildWeKnoraWikiPagePath(
|
|
knowledgeBaseId: string,
|
|
wikiSlug: string,
|
|
): string {
|
|
const params = new URLSearchParams({ wikiSlug });
|
|
return `/page/workspace/knowledge/${encodeURIComponent(knowledgeBaseId)}/wiki/view?${params.toString()}`;
|
|
}
|
|
|
|
/** Public, chrome-free Wiki reader intended for third-party iframe embedding. */
|
|
export function buildPublicKnowledgeWikiPath(
|
|
knowledgeBaseId: string,
|
|
wikiSlug?: string,
|
|
): string {
|
|
const params = new URLSearchParams();
|
|
if (wikiSlug) params.set("wikiSlug", wikiSlug);
|
|
const query = params.toString();
|
|
return `/embed/knowledge/${encodeURIComponent(knowledgeBaseId)}${query ? `?${query}` : ""}`;
|
|
}
|
|
|
|
export function buildPublicKnowledgeWikiUrl(
|
|
knowledgeBaseId: string,
|
|
wikiSlug?: string,
|
|
): string {
|
|
return buildAppHashUrl(buildPublicKnowledgeWikiPath(knowledgeBaseId, wikiSlug));
|
|
}
|
|
|
|
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
|
|
? weknoraTarget.wikiSlug
|
|
? buildWeKnoraWikiPageUrl(weknoraTarget.knowledgeBaseId, weknoraTarget.wikiSlug)
|
|
: 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;
|
|
const base = import.meta.env.BASE_URL || "/";
|
|
window.location.replace(`${base}#${pathname}${search}`);
|
|
return true;
|
|
}
|