An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
import {Constants} from "../constants";
import {renderBazaarInstall, updateBazaarInstallProgress} from "../config/bazaar/installTask";
import {getSettingsWindowHost} from "../config/setting/windowContext";
import {fetchPost} from "../util/fetch";
/// #if !MOBILE
import {exportLayout} from "../layout/util";
import {getDockByType} from "../layout/tabUtil";
import {Files} from "../layout/dock/Files";
/// #endif
import {getAllEditor, getAllModels} from "../layout/getAll";
/// #if !BROWSER
import {ipcRenderer} from "electron";
/// #endif
import {hideMessage, showMessage} from "./message";
import {Dialog} from "./index";
import {isMobile} from "../util/functions";
import {confirmDialog} from "./confirmDialog";
import {escapeHtml} from "../util/escape";
import {needSubscribe} from "../util/needSubscribe";
import {hideAllElements} from "../protyle/ui/hideElements";
import {saveMobileLayout} from "../mobile/util/saveLayout";
import {isInAndroid, isInHarmony, isInIOS, setStorageVal} from "../protyle/util/compatibility";
import {emitToPlugins} from "../plugin/EventBusCore";
import {createHostQuitGuard} from "./hostQuit";
import {requestSystemExit} from "./exitRequest";
import {getHostCapabilities, sanitizeKernelHTML} from "../util/hostCapabilities";
/// #if MOBILE
import {getMobileBacklinkPanels} from "../mobile/util/backlinkPanels";
/// #endif
export const processBacklinkIndexCommit = (data: {
rootIDs?: string[],
backlinkChanged?: boolean,
backlinkFull?: boolean,
}) => {
if (!data?.backlinkChanged) {
return;
}
/// #if MOBILE
getMobileBacklinkPanels().forEach(item => {
item.markIndexDirty(data);
item.refreshAfterIndex();
});
/// #else
getAllModels().backlink.forEach(item => {
item.markIndexDirty(data);
item.refreshAfterIndex();
});
/// #endif
};
export const setRefDynamicText = (data: {
"blockID": string,
"defBlockID": string,
"refText": string,
"rootID": string
}) => {
getAllEditor().forEach(editor => {
// 不能对比 rootId,否则嵌入块中的锚文本无法更新
editor.protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${data.blockID}"] span[data-type~="block-ref"][data-subtype="d"][data-id="${data.defBlockID}"]`).forEach(item => {
item.innerHTML = sanitizeKernelHTML(data.refText);
});
});
};
export const setDefRefCount = (data: {
"blockID": string,
"defIDs"?: string[],
"refCount": number,
"rootRefCount": number,
"rootID": string
}) => {
getAllEditor().forEach(editor => {
if (editor.protyle.block.rootID === data.rootID && editor.protyle.title) {
const attrElement = editor.protyle.title.element.querySelector(".protyle-attr");
const countElement = attrElement.querySelector(".protyle-attr--refcount");
if (countElement) {
if (data.rootRefCount === 0) {
countElement.remove();
} else {
countElement.textContent = data.rootRefCount.toString();
}
} else if (data.rootRefCount > 0) {
attrElement.insertAdjacentHTML("beforeend", `<div class="protyle-attr--refcount popover__block">${data.rootRefCount}</div>`);
}
}
if (data.rootID === data.blockID) {
return;
}
// 不能对比 rootId,否则嵌入块中的锚文本无法更新
editor.protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${data.blockID}"]`).forEach(item => {
// 不能直接查询,否则列表中会获取到第一个列表项的 attr https://github.com/siyuan-note/siyuan/issues/12738
const countElement = item.lastElementChild?.querySelector(".protyle-attr--refcount");
if (countElement) {
if (data.refCount === 0) {
countElement.remove();
} else {
countElement.textContent = data.refCount.toString();
}
} else if (data.refCount > 0) {
const attrElement = item.lastElementChild;
if (attrElement.childElementCount > 0) {
attrElement.lastElementChild.insertAdjacentHTML("afterend", `<div class="protyle-attr--refcount popover__block">${data.refCount}</div>`);
} else {
attrElement.innerHTML = `<div class="protyle-attr--refcount popover__block">${data.refCount}</div>${Constants.ZWSP}`;
}
}
if (data.refCount === 0) {
item.removeAttribute("refcount");
} else {
item.setAttribute("refcount", data.refCount.toString());
}
});
});
let liElement;
/// #if MOBILE
liElement = window.siyuan.mobile.docks.file.element.querySelector(`li[data-node-id="${data.rootID}"]`);
/// #else
liElement = (getDockByType("file")?.data["file"] as Files)?.element.querySelector(`li[data-node-id="${data.rootID}"]`);
/// #endif
if (liElement) {
const counterElement = liElement.querySelector(".counter");
if (counterElement) {
if (data.rootRefCount === 0) {
counterElement.remove();
} else {
counterElement.textContent = data.rootRefCount.toString();
}
} else if (data.rootRefCount > 0) {
liElement.insertAdjacentHTML("beforeend", `<span class="popover__block counter b3-tooltips b3-tooltips__nw" aria-label="${window.siyuan.languages.ref}">${data.rootRefCount}</span>`);
}
}
};
export const lockScreen = async () => {
if (window.siyuan.config.readonly || window.siyuan.isPublish) {
return;
}
emitToPlugins("lock-screen");
/// #if !MOBILE
exportLayout({
errorExit: false,
cb() {
fetchPost("/api/system/logoutAuth");
}
});
/// #else
await saveMobileLayout();
fetchPost("/api/system/logoutAuth");
/// #endif
};
const hostQuitGuard = createHostQuitGuard();
export const isHostQuitStarted = hostQuitGuard.isStarted;
// forceQuit 绕过内核 HTTP,直接通知宿主(Electron 主进程 / 移动端原生容器)退出。浏览器/Docker 等纯 Web
// 环境无宿主可调,只能关闭当前页。
export const forceQuit = () => {
hostQuitGuard.run(() => {
/// #if !BROWSER
ipcRenderer.send(Constants.SIYUAN_QUIT, location.port);
/// #else
if (isInAndroid()) {
window.JSAndroid.exit();
return;
}
if (isInIOS()) {
window.webkit.messageHandlers.exit.postMessage("");
return;
}
if (isInHarmony()) {
window.JSHarmony.exit();
return;
}
window.close();
/// #endif
});
};
const installNewVersion = (installPkgPath: string, setCurrentWorkspace: boolean) => {
if (!getHostCapabilities().ownsKernel) {
return;
}
if (!installPkgPath) {
showMessage(window.siyuan.languages._kernel[104], 7000, "error");
return;
}
/// #if !BROWSER
ipcRenderer.invoke(Constants.SIYUAN_INSTALL_UPDATE, {
port: location.port,
setCurrentWorkspace,
}).then((accepted: boolean) => {
if (!accepted) {
showMessage(window.siyuan.languages._kernel[104], 7000, "error");
}
}).catch(() => {
showMessage(window.siyuan.languages._kernel[104], 7000, "error");
});
/// #else
void requestSystemExit({
force: true,
setCurrentWorkspace,
execInstallPkg: 1,
}, forceQuit);
/// #endif
};
export const exitSiYuan = async (setCurrentWorkspace = true) => {
const host = getSettingsWindowHost();
if (host) {
await host.exit(setCurrentWorkspace);
return;
}
hideAllElements(["util"]);
/// #if !BROWSER
try {
if (!await ipcRenderer.invoke(Constants.SIYUAN_GET, {cmd: Constants.SIYUAN_WINDOW_WORKSPACE_FLUSH_ALL})) {
showMessage(window.siyuan.languages.windowWorkspaceSaveError, 6000, "error");
return;
}
} catch (error) {
console.error(error);
showMessage(window.siyuan.languages.windowWorkspaceSaveError, 6000, "error");
return;
}
/// #endif
/// #if MOBILE
if (!await saveMobileLayout()) {
showMessage(window.siyuan.languages.mobileLayoutSaveError, 6000, "error");
return;
}
/// #endif
if (!getHostCapabilities().ownsKernel) {
forceQuit();
return;
}
await requestSystemExit({force: false, setCurrentWorkspace}, (response) => {
if (response.code === 1) { // 同步执行失败
confirmDialog(window.siyuan.languages.sync, escapeHtml(window.siyuan.languages._kernel[96]), () => {
void exitSiYuan(setCurrentWorkspace);
}, undefined, false, {
label: window.siyuan.languages._kernel[97],
callback: () => {
if (response.data.installPkgPath) {
installNewVersion(response.data.installPkgPath, setCurrentWorkspace);
return;
}
void requestSystemExit({force: true, setCurrentWorkspace}, forceQuit);
}
}, window.siyuan.languages.retry);
} else if (response.code === 2) { // 提示新安装包
hideMessage();
/// #if !BROWSER
if ("std" === window.siyuan.config.system.container) {
ipcRenderer.send(Constants.SIYUAN_SHOW_WINDOW);
}
/// #endif
confirmDialog(window.siyuan.languages.updateVersion, response.msg, () => {
installNewVersion(response.data.installPkgPath, setCurrentWorkspace);
}, () => {
void requestSystemExit({
force: true,
setCurrentWorkspace,
execInstallPkg: 1 // 0:默认检查新版本,1:不返回安装包,2:返回安装包路径并退出
}, forceQuit);
});
} else { // 正常退出
forceQuit();
}
});
};
export const transactionError = (msg?: string) => {
if (document.getElementById("transactionError")) {
return;
}
const dialog = new Dialog({
disableClose: true,
title: `${window.siyuan.languages.stateExcepted} v${Constants.SIYUAN_VERSION}`,
content: `<div class="b3-dialog__content" style="max-height: calc(100vh - 182px)" id="transactionError">
${window.siyuan.languages.rebuildIndexTip}
${msg ? `<div class="fn__hr"></div>${escapeHtml(msg.trim())}` : ""}
</div>
<div class="b3-dialog__action">
<button class="b3-button b3-button--text">${window.siyuan.languages._kernel[97]}</button>
<div class="fn__space"></div>
<button class="b3-button">${window.siyuan.languages.rebuildDataIndex}</button>
</div>`,
width: isMobile() ? "92vw" : "520px",
});
dialog.element.setAttribute("data-key", Constants.DIALOG_STATEEXCEPTED);
const btnsElement = dialog.element.querySelectorAll(".b3-button");
btnsElement[0].addEventListener("click", () => {
/// #if MOBILE
exitSiYuan();
/// #else
exportLayout({
errorExit: true,
cb: exitSiYuan
});
/// #endif
});
btnsElement[1].addEventListener("click", () => {
refreshFileTree();
dialog.destroy();
});
};
export const refreshFileTree = (cb?: () => void) => {
window.siyuan.storage[Constants.LOCAL_FILEPOSITION] = {};
setStorageVal(Constants.LOCAL_FILEPOSITION, window.siyuan.storage[Constants.LOCAL_FILEPOSITION]);
fetchPost("/api/system/rebuildDataIndex", {}, () => {
if (cb) {
cb();
}
});
};
let statusTimeout: number;
export const progressStatus = (data: IWebSocketData) => {
const msgElement = document.querySelector("#status .status__msg");
if (msgElement) {
clearTimeout(statusTimeout);
msgElement.innerHTML = sanitizeKernelHTML(data.msg);
statusTimeout = window.setTimeout(() => {
msgElement.innerHTML = "";
}, 12000);
}
};
export const progressBackgroundTask = (tasks: { action: string }[]) => {
const backgroundTaskElement = document.querySelector(".status__backgroundtask");
if (!backgroundTaskElement) {
return;
}
if (tasks.length === 0) {
backgroundTaskElement.classList.add("fn__none");
if (!window.siyuan.menus.menu.element.classList.contains("fn__none") &&
window.siyuan.menus.menu.element.getAttribute("data-name") === Constants.MENU_STATUS_BACKGROUND_TASK) {
window.siyuan.menus.menu.remove();
}
} else {
const safeTasks = tasks.map((item) => ({
...item,
action: sanitizeKernelHTML(item.action),
}));
backgroundTaskElement.classList.remove("fn__none");
backgroundTaskElement.setAttribute("data-tasks", JSON.stringify(safeTasks));
backgroundTaskElement.innerHTML = safeTasks[0].action + '<div class="fn__progress"><div></div></div>';
}
};
export const bootSync = () => {
fetchPost("/api/sync/getBootSync", {}, response => {
if (response.code === 1) {
const dialog = new Dialog({
width: isMobile() ? "92vw" : "50vw",
title: "🌩️ " + window.siyuan.languages.bootSyncFailed,
content: `<div class="b3-dialog__content">${sanitizeKernelHTML(response.msg)}</div>
<div class="b3-dialog__action">
<button class="b3-button b3-button--cancel">${window.siyuan.languages.cancel}</button><div class="fn__space"></div>
<button class="b3-button b3-button--text">${window.siyuan.languages.syncNow}</button>
</div>`
});
dialog.element.setAttribute("data-key", Constants.DIALOG_BOOTSYNCFAILED);
const btnsElement = dialog.element.querySelectorAll(".b3-button");
btnsElement[0].addEventListener("click", () => {
dialog.destroy();
});
btnsElement[1].addEventListener("click", () => {
if (btnsElement[1].getAttribute("disabled")) {
return;
}
btnsElement[1].setAttribute("disabled", "disabled");
fetchPost("/api/sync/performBootSync", {}, (syncResponse) => {
if (syncResponse.code === 0) {
dialog.destroy();
}
btnsElement[1].removeAttribute("disabled");
});
});
}
});
};
export const downloadProgress = (data: { id: string, percent: number }) => {
if (updateBazaarInstallProgress(data)) {
renderBazaarInstall(document);
}
};
export const processSync = (data?: IWebSocketData) => {
if (data?.code === 1) {
window.dispatchEvent(new CustomEvent("siyuan-sync-success"));
}
const syncDisabled = !window.siyuan.config.sync.enabled || (0 === window.siyuan.config.sync.provider && needSubscribe(""));
/// #if MOBILE
const menuSyncUseElement = document.querySelector("#menuSyncNow use");
const barSyncUseElement = document.querySelector("#toolbarSync use");
if (!data) {
if (barSyncUseElement?.parentElement?.classList.contains("fn__rotate")) {
// 同步进行中时保持旋转状态,待同步完成后由同步结果消息更新图标 https://github.com/siyuan-note/siyuan/issues/18597
return;
}
menuSyncUseElement?.setAttribute("xlink:href", syncDisabled ? "#iconCloudOff" : "#iconCloudSucc");
barSyncUseElement?.setAttribute("xlink:href", syncDisabled ? "#iconCloudOff" : "#iconCloudSucc");
return;
}
menuSyncUseElement?.parentElement?.classList.remove("fn__rotate");
barSyncUseElement?.parentElement?.classList.remove("fn__rotate");
if (data.code === 0) { // syncing
menuSyncUseElement?.parentElement?.classList.add("fn__rotate");
barSyncUseElement?.parentElement?.classList.add("fn__rotate");
menuSyncUseElement?.setAttribute("xlink:href", "#iconRefresh");
barSyncUseElement?.setAttribute("xlink:href", "#iconRefresh");
} else if (data.code === 2) { // error
menuSyncUseElement?.setAttribute("xlink:href", syncDisabled ? "#iconCloudOff" : "#iconCloudError");
barSyncUseElement?.setAttribute("xlink:href", syncDisabled ? "#iconCloudOff" : "#iconCloudError");
} else if (data.code === 1) { // success
menuSyncUseElement?.setAttribute("xlink:href", syncDisabled ? "#iconCloudOff" : "#iconCloudSucc");
barSyncUseElement?.setAttribute("xlink:href", syncDisabled ? "#iconCloudOff" : "#iconCloudSucc");
}
/// #else
const iconElement = document.querySelector("#barSync");
if (!iconElement) {
return;
}
const useElement = iconElement.querySelector("use");
if (!data) {
if (iconElement.classList.contains("toolbar__item--active")) {
// 同步进行中时保持旋转状态,待同步完成后由同步结果消息更新图标 https://github.com/siyuan-note/siyuan/issues/18597
return;
}
iconElement.classList.remove("toolbar__item--active");
useElement.setAttribute("xlink:href", syncDisabled ? "#iconCloudOff" : "#iconCloudSucc");
return;
}
iconElement.firstElementChild.classList.remove("fn__rotate");
if (data.code === 0) { // syncing
iconElement.classList.add("toolbar__item--active");
iconElement.firstElementChild.classList.add("fn__rotate");
useElement.setAttribute("xlink:href", "#iconRefresh");
} else if (data.code === 2) { // error
iconElement.classList.remove("toolbar__item--active");
useElement.setAttribute("xlink:href", syncDisabled ? "#iconCloudOff" : "#iconCloudError");
} else if (data.code === 1) { // success
iconElement.classList.remove("toolbar__item--active");
useElement.setAttribute("xlink:href", syncDisabled ? "#iconCloudOff" : "#iconCloudSucc");
}
/// #endif
if (data.code === 0) {
emitToPlugins("sync-start", data);
} else if (data.code === 1) {
emitToPlugins("sync-end", data);
} else if (data.code === 2) {
emitToPlugins("sync-fail", data);
}
};