An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
export type TKernelConnection = {
kernelMode: "local" | "remote";
ownsKernel: boolean;
kernelOrigin: string;
trustRemoteExtensions?: boolean;
extensionScriptNonce?: string;
};
export type THostCapabilities = {
remoteKernel: boolean;
ownsKernel: boolean;
localFileSystem: boolean;
importExport: boolean;
documentImportExport: boolean;
plugins: boolean;
workspaces: boolean;
customAppearance: boolean;
widgets: boolean;
oidcAuthentication: boolean;
};
const hasRemoteQuery = (search: string) => new URLSearchParams(search).get("remote") === "1";
export const hasRemoteArgument = (args: readonly string[]) => args.some((arg) =>
arg === "--remote" || arg.startsWith("--remote="));
export const detectRemoteKernel = (search: string, args: readonly string[]) =>
hasRemoteQuery(search) || hasRemoteArgument(args);
export const resolveRemoteKernel = (connection: TKernelConnection | undefined, search: string, args: readonly string[]) =>
connection ? connection.kernelMode === "remote" : detectRemoteKernel(search, args);
const getProcessArgs = () => {
/// #if !BROWSER
return process.argv;
/// #else
return [];
/// #endif
};
let hostConnection: TKernelConnection | undefined;
export const setHostConnection = (connection: TKernelConnection | undefined) => {
if (!connection || !["local", "remote"].includes(connection.kernelMode)) {
return;
}
hostConnection = connection;
};
export const isRemoteKernel = () => resolveRemoteKernel(hostConnection, window.location.search, getProcessArgs());
export const resolveHostCapabilities = (remoteKernel: boolean, connection: TKernelConnection | undefined,
origin: string): THostCapabilities => {
const ownsKernel = !remoteKernel && connection?.ownsKernel !== false;
const trustedExtensions = remoteKernel && connection?.kernelMode === "remote" &&
connection.kernelOrigin === origin && connection.trustRemoteExtensions === true;
return {
remoteKernel,
ownsKernel,
localFileSystem: !remoteKernel,
importExport: !remoteKernel,
documentImportExport: true,
plugins: !remoteKernel || trustedExtensions,
workspaces: !remoteKernel,
customAppearance: !remoteKernel || trustedExtensions,
widgets: !remoteKernel,
oidcAuthentication: !remoteKernel,
};
};
export const getHostCapabilities = (): THostCapabilities =>
resolveHostCapabilities(isRemoteKernel(), hostConnection, window.location.origin);
export const getExtensionScriptNonce = () => isRemoteKernel() && getHostCapabilities().customAppearance
? hostConnection?.extensionScriptNonce : undefined;
export const appendRemoteQuery = (url: URL) => {
if (isRemoteKernel()) {
url.searchParams.set("remote", "1");
}
return url;
};
export const isExternalURLAllowed = (
url: string,
remoteKernel: boolean,
) => {
if (!remoteKernel) {
return true;
}
try {
const parsed = new URL(url);
return parsed.protocol === "http:" || parsed.protocol === "https:";
} catch {
return false;
}
};
export const canOpenExternalURL = (url: string) =>
isExternalURLAllowed(url, isRemoteKernel());
export const sanitizeKernelHTML = (html: string) => {
if (!isRemoteKernel()) {
return html;
}
return window.DOMPurify.sanitize(String(html ?? ""));
};