Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Integrate the mobile Agent into the sidebar https://github.com/siyuan-note/siyuan/issues/18912

Danieldbeb68c1fcdd
diff --git a/app/src/assets/scss/business/_ai_agent.scss b/app/src/assets/scss/business/_ai_agent.scss
index 25671865ae..b9a106818c 100644
--- a/app/src/assets/scss/business/_ai_agent.scss
+++ b/app/src/assets/scss/business/_ai_agent.scss
@@ -1173,17 +1173,6 @@
font-size: var(--b3-font-size);
}
-#model.model--agent {
- > .toolbar {
- display: none;
- }
-
- #modelMain {
- min-height: 0;
- overflow: hidden;
- }
-}
-
.sy__agentChat--mobile {
position: relative;
height: 100%;
diff --git a/app/src/assets/template/mobile/index.tpl b/app/src/assets/template/mobile/index.tpl
index 0f448253eb..565c11f004 100644
--- a/app/src/assets/template/mobile/index.tpl
+++ b/app/src/assets/template/mobile/index.tpl
@@ -45,12 +45,14 @@
<div class="toolbar__scroll">
<svg data-type="sidebar-outline-tab" class="toolbar__icon toolbar__icon--active"><use xlink:href="#iconOutline"></use></svg>
<svg data-type="sidebar-backlink-tab" class="toolbar__icon"><use xlink:href="#iconLink"></use></svg>
+ <svg data-type="sidebar-agent-tab" class="toolbar__icon fn__none"><use xlink:href="#iconSparkles"></use></svg>
</div>
<svg class="toolbar__icon"><use xlink:href="#iconLeft"></use></svg>
</div>
<div class="fn__flex-1 b3-list--mobile">
<div class="fn__flex-column" data-type="sidebar-outline"></div>
<div class="fn__flex-column fn__none" data-type="sidebar-backlink"></div>
+ <div class="fn__flex-column fn__none" data-type="sidebar-agent"></div>
</div>
</div>
<div id="menu" class="b3-menu b3-menu--fullscreen fn__none"></div>
diff --git a/app/src/layout/dock/agent/AgentChat.ts b/app/src/layout/dock/agent/AgentChat.ts
index 1e089ea9f9..2f6d88c1c5 100644
--- a/app/src/layout/dock/agent/AgentChat.ts
+++ b/app/src/layout/dock/agent/AgentChat.ts
@@ -60,6 +60,7 @@ export type AgentChatNotification = "confirm" | "done";
export interface AgentChatHost {
element: HTMLElement;
mobile?: boolean;
+ mobileSidebar?: boolean;
close: () => void;
focus?: () => void;
openAiSetting?: () => void;
@@ -337,7 +338,7 @@ export class AgentChat extends Model {
panel.innerHTML = '<div class="agent-chat fn__flex-column fn__flex-1">' +
'<div class="block__icons fn__hidescrollbar">' +
- (this.host.mobile ? '<span data-type="back" class="block__icon ariaLabel" aria-label="' + L.back + '">' +
+ (this.host.mobile && !this.host.mobileSidebar ? '<span data-type="back" class="block__icon ariaLabel" aria-label="' + L.back + '">' +
'<svg><use xlink:href="#iconLeft"></use></svg></span>' : "") +
'<div class="block__logo fn__flex-1 agent-chat__title">' + (L.agentChat || "Agent") + "</div>" +
'<span data-type="new-session" class="block__icon ariaLabel" data-position="north" aria-label="' + (L.agentNewSession || "New Session") + '">' +
@@ -348,11 +349,12 @@ export class AgentChat extends Model {
'<svg><use xlink:href="#iconFolderClock"></use></svg>' +
"</span>" +
'<span class="fn__space"></span>' +
- '<span data-type="' + (this.host.mobile ? "close" : "min") + '" class="block__icon ariaLabel" data-position="north" aria-label="' +
- (this.host.mobile ? window.siyuan.languages.close : window.siyuan.languages.min +
- updateHotkeyAfterTip(window.siyuan.config.keymap.general.closeTab.custom)) + '">' +
- '<svg><use xlink:href="#' + (this.host.mobile ? "iconCloseRound" : "iconMin") + '"></use></svg>' +
- "</span>" +
+ (!this.host.mobile || !this.host.mobileSidebar ? '<span data-type="' + (this.host.mobile ? "close" : "min") +
+ '" class="block__icon ariaLabel" data-position="north" aria-label="' +
+ (this.host.mobile ? window.siyuan.languages.close : window.siyuan.languages.min +
+ updateHotkeyAfterTip(window.siyuan.config.keymap.general.closeTab.custom)) + '">' +
+ '<svg><use xlink:href="#' + (this.host.mobile ? "iconCloseRound" : "iconMin") + '"></use></svg>' +
+ "</span>" : "") +
"</div>" +
'<div class="agent-chat__messages-wrap">' +
'<div class="agent-chat__messages fn__flex-1"></div>' +
diff --git a/app/src/mobile/agent/MobileAgentChat.ts b/app/src/mobile/agent/MobileAgentChat.ts
index 63ed7597f1..40798b8b0f 100644
--- a/app/src/mobile/agent/MobileAgentChat.ts
+++ b/app/src/mobile/agent/MobileAgentChat.ts
@@ -4,47 +4,49 @@ import {
type AgentChatNotification,
type AgentChatStatus,
} from "../../layout/dock/agent/AgentChat";
-import {openModel} from "../menu/model";
-import {closeModel, closePanel} from "../util/closePanel";
+import {closePanel} from "../util/closePanel";
import {showMessage} from "../../dialog/message";
import {sendNotification} from "../../plugin/platformUtils";
import {isDisabledFeature} from "../../protyle/util/compatibility";
+import {openDock} from "../dock/util";
let app: App | undefined;
let agentChat: AgentChat | undefined;
let rootElement: HTMLElement | undefined;
let detachedRoot: DocumentFragment | undefined;
-let visible = false;
let running = false;
let unread: AgentChatNotification | undefined;
const updateMenuStatus = () => {
const item = document.getElementById("menuAgentChat");
- if (!item) {
- return;
+ if (item) {
+ const icon = item.querySelector(".b3-menu__icon");
+ icon?.classList.toggle("fn__rotate", running);
+ const status = item.querySelector('[data-type="agent-status"]');
+ status?.classList.toggle("fn__none", !unread);
+ status?.classList.toggle("agent-menu-status--warning", unread === "confirm");
+ if (status) {
+ status.textContent = unread === "confirm" ? window.siyuan.languages.agentConfirmPending : "●";
+ }
}
- const icon = item.querySelector(".b3-menu__icon");
- icon?.classList.toggle("fn__rotate", running);
- const status = item.querySelector('[data-type="agent-status"]');
- if (!status) {
- return;
+ const tabElement = document.querySelector('[data-type="sidebar-agent-tab"]');
+ tabElement?.classList.toggle("fn__rotate", running);
+ tabElement?.classList.toggle("agent-menu-status--warning", unread === "confirm");
+ if (tabElement) {
+ const label = unread === "confirm" ? window.siyuan.languages.agentNotifyConfirm :
+ (unread === "done" ? window.siyuan.languages.agentNotifyDone : window.siyuan.languages.agentChat);
+ tabElement.setAttribute("aria-label", label);
}
- status.classList.toggle("fn__none", !unread);
- status.classList.toggle("agent-menu-status--warning", unread === "confirm");
- status.textContent = unread === "confirm" ? window.siyuan.languages.agentConfirmPending : "●";
};
-const detach = () => {
- visible = false;
- document.getElementById("model")?.classList.remove("model--agent");
- if (rootElement?.parentElement) {
- detachedRoot = detachedRoot || document.createDocumentFragment();
- detachedRoot.appendChild(rootElement);
- }
+const isAgentSidebarVisible = () => {
+ const panelElement = rootElement?.closest('[data-type="sidebar-agent"]') as HTMLElement | null;
+ const sidebarElement = panelElement?.closest(".side-panel") as HTMLElement | null;
+ return Boolean(panelElement && !panelElement.classList.contains("fn__none") && sidebarElement?.style.transform);
};
const notify = (type: AgentChatNotification) => {
- if (visible) {
+ if (isAgentSidebarVisible()) {
return;
}
unread = type;
@@ -75,6 +77,7 @@ const ensureAgentChat = (currentApp: App) => {
agentChat = new AgentChat(currentApp, {
element: rootElement,
mobile: true,
+ mobileSidebar: true,
close: hideMobileAgent,
openAiSetting: () => {
hideMobileAgent();
@@ -85,43 +88,43 @@ const ensureAgentChat = (currentApp: App) => {
onStatusChange: setStatus,
});
window.siyuan.mobile.agentChat = agentChat;
+ window.siyuan.mobile.docks.agent = agentChat;
window.siyuan.mobile.agentChatController = {
handleBack: handleMobileAgentBack,
refreshStatus: updateMenuStatus,
};
};
-export const openMobileAgent = (currentApp: App) => {
- if (isDisabledFeature("ai")) {
+export const activateMobileAgent = (currentApp: App, element: HTMLElement) => {
+ if (window.siyuan.config.readonly || window.siyuan.isPublish || isDisabledFeature("ai")) {
return;
}
ensureAgentChat(currentApp);
- closePanel();
- openModel({
- title: "",
- html: "",
- bindEvent(modelMainElement) {
- modelMainElement.appendChild(rootElement!);
- },
- destroyCallback: detach,
- });
- document.getElementById("model").classList.add("model--agent");
- visible = true;
+ if (rootElement?.parentElement !== element) {
+ element.appendChild(rootElement!);
+ }
unread = undefined;
updateMenuStatus();
};
+export const openMobileAgent = (currentApp: App) => {
+ if (window.siyuan.config.readonly || window.siyuan.isPublish || isDisabledFeature("ai")) {
+ return;
+ }
+ ensureAgentChat(currentApp);
+ closePanel();
+ openDock("agent");
+};
+
export const hideMobileAgent = () => {
- if (!visible) {
+ if (!isAgentSidebarVisible()) {
return;
}
- visible = false;
- document.getElementById("model")?.classList.remove("model--agent");
- closeModel();
+ closePanel();
};
export const handleMobileAgentBack = () => {
- if (!visible) {
+ if (!isAgentSidebarVisible()) {
return false;
}
if (agentChat?.back()) {
@@ -139,7 +142,7 @@ export const insertMobileAgentMentions = (currentApp: App, mentions: Array<{id:
agentChat?.insertBlockMentions(mentions);
};
-export const isMobileAgentVisible = () => visible;
+export const isMobileAgentVisible = () => isAgentSidebarVisible();
export const reopenMobileAgent = () => {
if (app) {
diff --git a/app/src/mobile/index.ts b/app/src/mobile/index.ts
index ccb004c4bb..d57a1c1220 100644
--- a/app/src/mobile/index.ts
+++ b/app/src/mobile/index.ts
@@ -98,6 +98,7 @@ class App {
tag: null,
backlink: null,
inbox: null,
+ agent: null,
}
},
ws: mainWs
diff --git a/app/src/mobile/util/initFramework.ts b/app/src/mobile/util/initFramework.ts
index bbcc7f68b6..59f92f257a 100644
--- a/app/src/mobile/util/initFramework.ts
+++ b/app/src/mobile/util/initFramework.ts
@@ -3,7 +3,7 @@ import {closeModel, closePanel} from "./closePanel";
import {getCurrentEditor, openMobileFileById} from "../editor";
import {openMobileOnboarding} from "../../onboarding";
import {validateName} from "../../editor/rename";
-import {getEventName} from "../../protyle/util/compatibility";
+import {getEventName, isDisabledFeature} from "../../protyle/util/compatibility";
import {fetchPost} from "../../util/fetch";
import {setInlineStyle} from "../../util/assets";
import {renderSnippet} from "../../config/util/snippets";
@@ -52,6 +52,8 @@ const getActiveDockId = (sidePanelElement: HTMLElement) => {
};
export const renderMobileSidePanelLayout = (config: IMobileSidePanelConfig = getMobileSidePanelConfig()) => {
+ getDockTabElement("agent").classList.toggle("fn__none",
+ window.siyuan.config.readonly || window.siyuan.isPublish || isDisabledFeature("ai"));
const sidePanelElements = {
left: document.getElementById("sidebar"),
right: document.getElementById("sidebarRight"),
@@ -149,6 +151,10 @@ const updateDock = (app: App, type: MobileSidePanelDockId, element: HTMLElement,
}
} else if (type === "inbox" && !window.siyuan.mobile.docks.inbox) {
window.siyuan.mobile.docks.inbox = new Inbox(app, element);
+ } else if (type === "agent") {
+ void import("../agent/MobileAgentChat").then(({activateMobileAgent}) => {
+ activateMobileAgent(app, element);
+ });
} else if (type === "plugin") {
if (!custom || openPluginMenu) {
if (!custom) {
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.ts
index 3e172bacc2..cf5baaa2a5 100644
--- a/app/src/mobile/util/keyboardToolbar.ts
+++ b/app/src/mobile/util/keyboardToolbar.ts
@@ -832,7 +832,6 @@ export const showKeyboardToolbar = () => {
// 智能体发送框自带操作栏,不能显示会作用于下层文档的移动端编辑工具栏。
window.dispatchEvent(new CustomEvent("siyuan-mobile-keyboard-change", {detail: true}));
toolbarElement.classList.add("fn__none");
- document.getElementById("model").style.paddingBottom = "";
return;
}
if (!toolbarElement.classList.contains("fn__none")) {
diff --git a/app/src/mobile/util/mobileSidePanelConfig.test.ts b/app/src/mobile/util/mobileSidePanelConfig.test.ts
index 69dbf55e94..18a0e516c5 100644
--- a/app/src/mobile/util/mobileSidePanelConfig.test.ts
+++ b/app/src/mobile/util/mobileSidePanelConfig.test.ts
@@ -46,7 +46,19 @@ describe("mobile side panel config", () => {
}));
assert.deepEqual(config.left, ["plugin", "file", "tag"]);
- assert.deepEqual(config.right, ["backlink", "outline", "bookmark", "inbox"]);
+ assert.deepEqual(config.right, ["backlink", "outline", "bookmark", "inbox", "agent"]);
+ assertCompleteConfig(config);
+ });
+
+ it("adds the Agent dock to legacy version 1 layouts", () => {
+ const config = normalizeMobileSidePanelConfig({
+ version: MOBILE_SIDE_PANEL_CONFIG_VERSION,
+ left: ["file", "bookmark", "tag", "inbox", "plugin"],
+ right: ["backlink", "outline"],
+ });
+
+ assert.deepEqual(config.left, ["file", "bookmark", "tag", "inbox", "plugin"]);
+ assert.deepEqual(config.right, ["backlink", "outline", "agent"]);
assertCompleteConfig(config);
});
@@ -58,7 +70,7 @@ describe("mobile side panel config", () => {
});
assert.deepEqual(config.left, ["tag", "outline", "bookmark", "inbox", "plugin"]);
- assert.deepEqual(config.right, ["file", "backlink"]);
+ assert.deepEqual(config.right, ["file", "backlink", "agent"]);
assertCompleteConfig(config);
});
@@ -70,7 +82,7 @@ describe("mobile side panel config", () => {
});
assert.deepEqual(config.left, ["file"]);
- assert.deepEqual(config.right, ["outline", "bookmark", "tag", "backlink", "inbox", "plugin"]);
+ assert.deepEqual(config.right, ["outline", "bookmark", "tag", "backlink", "inbox", "agent", "plugin"]);
assertCompleteConfig(config);
});
@@ -81,7 +93,7 @@ describe("mobile side panel config", () => {
right: [],
});
- assert.deepEqual(config.left, ["file", "bookmark", "tag", "backlink", "inbox", "plugin"]);
+ assert.deepEqual(config.left, ["file", "bookmark", "tag", "backlink", "inbox", "agent", "plugin"]);
assert.deepEqual(config.right, ["outline"]);
assertCompleteConfig(config);
});
@@ -95,7 +107,7 @@ describe("mobile side panel config", () => {
});
assert.deepEqual(config.left, ["file", "tag", "inbox", "plugin"]);
- assert.deepEqual(config.right, ["outline", "bookmark", "backlink"]);
+ assert.deepEqual(config.right, ["outline", "bookmark", "backlink", "agent"]);
assertCompleteConfig(config);
});
@@ -103,7 +115,7 @@ describe("mobile side panel config", () => {
const state = normalizeMobileSidePanelConfig({
version: MOBILE_SIDE_PANEL_CONFIG_VERSION,
left: ["file"],
- right: ["outline", "bookmark", "tag", "backlink", "inbox", "plugin"],
+ right: ["outline", "bookmark", "tag", "backlink", "inbox", "agent", "plugin"],
});
const config = reduceMobileSidePanelConfig(state, {
type: "move",
diff --git a/app/src/mobile/util/mobileSidePanelConfig.ts b/app/src/mobile/util/mobileSidePanelConfig.ts
index d9fa787cbf..68c06573c4 100644
--- a/app/src/mobile/util/mobileSidePanelConfig.ts
+++ b/app/src/mobile/util/mobileSidePanelConfig.ts
@@ -8,6 +8,7 @@ export const MOBILE_SIDE_PANEL_DOCK_IDS = [
"tag",
"backlink",
"inbox",
+ "agent",
"plugin",
] as const;
@@ -45,6 +46,7 @@ export const DEFAULT_MOBILE_SIDE_PANEL_LEFT: readonly MobileSidePanelDockId[] =
export const DEFAULT_MOBILE_SIDE_PANEL_RIGHT: readonly MobileSidePanelDockId[] = [
"outline",
"backlink",
+ "agent",
];
export const DEFAULT_MOBILE_SIDE_PANEL_CONFIG: Readonly<IMobileSidePanelConfig> = {
diff --git a/app/src/mobile/util/mobileSidePanelSetting.ts b/app/src/mobile/util/mobileSidePanelSetting.ts
index 6cc0b5554c..a8a5647d59 100644
--- a/app/src/mobile/util/mobileSidePanelSetting.ts
+++ b/app/src/mobile/util/mobileSidePanelSetting.ts
@@ -24,6 +24,8 @@ const getDockLabel = (dockId: MobileSidePanelDockId) => {
return window.siyuan.languages.backlinks;
case "inbox":
return window.siyuan.languages.inbox;
+ case "agent":
+ return window.siyuan.languages.agentChat;
case "plugin":
return window.siyuan.languages.plugin;
}
@@ -43,6 +45,8 @@ const getDockIcon = (dockId: MobileSidePanelDockId) => {
return "iconLink";
case "inbox":
return "iconInbox";
+ case "agent":
+ return "iconSparkles";
case "plugin":
return "iconPlugin";
}
diff --git a/app/src/protyle/util/compatibility.ts b/app/src/protyle/util/compatibility.ts
index 8675487978..44d8742376 100644
--- a/app/src/protyle/util/compatibility.ts
+++ b/app/src/protyle/util/compatibility.ts
@@ -753,7 +753,7 @@ export const getLocalStorage = (cb: () => void) => {
defaultStorage[Constants.LOCAL_MOBILE_SIDE_PANEL] = {
version: 1,
left: ["file", "bookmark", "tag", "inbox", "plugin"],
- right: ["outline", "backlink"],
+ right: ["outline", "backlink", "agent"],
};
defaultStorage[Constants.LOCAL_IMAGES] = {
file: "1f4c4",