Skip to content

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

:bug: Improve the logic for adding the agent chat dock icon (#17873)

Jeffrey Chen1cd12d4a0a0a
diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.ts
index b093f444d9..6e8c9310cd 100644
--- a/app/src/boot/onGetConfig.ts
+++ b/app/src/boot/onGetConfig.ts
@@ -55,40 +55,6 @@ export const onGetConfig = (isStart: boolean, app: App) => {
initWindowEvent(app);
fetchPost("/api/system/getEmojiConf", {}, response => {
window.siyuan.emojis = response.data as IEmoji[];
- // 为已有用户添加 agent chat 停靠按钮
- // 用户可能把图标拖到 left/right/bottom 任一停靠栏,需遍历所有位置查重并清理多余项
- const docks = ["left", "right", "bottom"] as const;
- const dockData = (key: typeof docks[number]) => window.siyuan.config.uiLayout?.[key]?.data;
- let hasAgentChat = false;
- for (const key of docks) {
- const sections = dockData(key);
- if (!sections) continue;
- let firstFound = false;
- // 逆序遍历各分区,仅保留第一个 agentChat,删除多余的
- for (const sub of sections) {
- if (!sub) continue;
- for (let i = sub.length - 1; i >= 0; i--) {
- if (sub[i] && sub[i].type === "agentChat") {
- if (!firstFound) {
- firstFound = true;
- hasAgentChat = true;
- } else {
- sub.splice(i, 1);
- }
- }
- }
- }
- }
- // 若所有停靠栏都没有 agentChat,则补一个到右侧停靠栏上半区
- // agentChat 的定义统一来源于 SIYUAN_EMPTY_LAYOUT,避免硬编码与默认布局不一致
- if (!hasAgentChat) {
- const rightSections = dockData("right");
- const emptyAgentChat = Constants.SIYUAN_EMPTY_LAYOUT.right.data[0]
- ?.find((item: Config.IUILayoutDockTab) => item.type === "agentChat");
- if (rightSections?.[0] && emptyAgentChat) {
- rightSections[0].unshift({...emptyAgentChat});
- }
- }
try {
JSONToLayout(app, isStart);
setTimeout(() => {
diff --git a/app/src/layout/util.ts b/app/src/layout/util.ts
index a7ca97096d..4fbad00b30 100644
--- a/app/src/layout/util.ts
+++ b/app/src/layout/util.ts
@@ -224,6 +224,59 @@ export const getAllLayout = () => {
return layoutJSON;
};
+const DOCK_KEYS = ["left", "right", "bottom"] as const;
+
+// agentChat 停靠按钮:已存在则去重,不存在则按默认布局补全
+const ensureAgentChatDock = (layout: Pick<Config.IUiLayout, "left" | "right" | "bottom">) => {
+ let hasAgentChat = false;
+ for (const key of DOCK_KEYS) {
+ const sections = layout[key]?.data;
+ if (!sections) {
+ continue;
+ }
+ for (const sub of sections) {
+ if (!sub) {
+ continue;
+ }
+ for (let i = 0; i < sub.length; i++) {
+ if (sub[i]?.type !== "agentChat") {
+ continue;
+ }
+ if (hasAgentChat) {
+ sub.splice(i, 1);
+ i--;
+ } else {
+ hasAgentChat = true;
+ }
+ }
+ }
+ }
+ if (!hasAgentChat) {
+ for (const key of DOCK_KEYS) {
+ const sections = Constants.SIYUAN_EMPTY_LAYOUT[key]?.data;
+ if (!sections) {
+ continue;
+ }
+ for (let sectionIndex = 0; sectionIndex < sections.length; sectionIndex++) {
+ const sub = sections[sectionIndex];
+ if (!sub) {
+ continue;
+ }
+ for (let itemIndex = 0; itemIndex < sub.length; itemIndex++) {
+ const item = sub[itemIndex];
+ if (item?.type === "agentChat") {
+ const targetSections = layout[key]?.data;
+ if (targetSections?.[sectionIndex]) {
+ targetSections[sectionIndex].splice(itemIndex, 0, {...item});
+ }
+ return;
+ }
+ }
+ }
+ }
+ }
+};
+
const initInternalDock = (dockItem: Config.IUILayoutDockTab[]) => {
dockItem.forEach((existSubItem, index) => {
if (window.siyuan.isPublish && (existSubItem.type === "inbox" || existSubItem.type === "agentChat")) {
@@ -239,6 +292,7 @@ const initInternalDock = (dockItem: Config.IUILayoutDockTab[]) => {
};
const JSONToDock = (json: any, app: App) => {
+ ensureAgentChatDock(json);
json.left.data.forEach((existItem: Config.IUILayoutDockTab[]) => {
initInternalDock(existItem);
});