From 1ba2f249acb90154b7dcbc6a0cc39a9e88665089 Mon Sep 17 00:00:00 2001 From: qyh15 Date: Fri, 20 Nov 2026 21:49:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(workbench):=20=E5=9B=BA=E5=AE=9A=E5=8F=B3?= =?UTF-8?q?=E4=BE=A7=E5=B7=A5=E4=BD=9C=E5=8F=B0=E5=B9=B6=E8=B7=A8=E4=BC=9A?= =?UTF-8?q?=E8=AF=9D=E4=BF=9D=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sansheng/client.js | 96 ++++++++++++++++++++++++++++++++---- sansheng/src/client/index.js | 80 +++++++++++++++++++++++++++--- sansheng/test/nav.test.js | 8 +++ 3 files changed, 166 insertions(+), 18 deletions(-) diff --git a/sansheng/client.js b/sansheng/client.js index 02b1cbb..cb91d33 100644 --- a/sansheng/client.js +++ b/sansheng/client.js @@ -2465,10 +2465,26 @@ var native_default = { install }; // src/client/index.js var import_react2 = require("react"); var import_react_dom = require("react-dom"); -var inject = ["sessions", "uiWorkspace", "uiConversation", "slots", "theme", "layout"]; +var inject = ["sessions", "uiWorkspace", "uiConversation", "slots", "theme", "layout", "sidebarRightTabs", "sidebarRight"]; var PLUGIN_ID = "dsh-sansheng"; var TAB_KIND = "sansheng-workbench"; var TAB_TYPE_ID = "dsh-sansheng:workbench"; +var RIGHT_PIN_KEY = "sansheng.right-workbench.pinned"; +function readRightWorkbenchPinned() { + try { + return typeof localStorage !== "undefined" && localStorage.getItem(RIGHT_PIN_KEY) === "1"; + } catch { + return false; + } +} +function writeRightWorkbenchPinned(value) { + try { + if (typeof localStorage === "undefined") return; + if (value) localStorage.setItem(RIGHT_PIN_KEY, "1"); + else localStorage.removeItem(RIGHT_PIN_KEY); + } catch { + } +} function installStyles(doc, tag, text) { if (!doc || !doc.head) return null; const style = doc.createElement("style"); @@ -2691,12 +2707,30 @@ function apply(ctx) { return false; } } + function rightMountedSessionId() { + const right = get("sidebarRight"); + try { + if (right && isFn(right.mounted?.getSnapshot)) return right.mounted.getSnapshot(); + } catch { + } + return currentSessionId(); + } + function findRightWorkbenchTab() { + const right = get("sidebarRight"); + if (!right || !isFn(right.openTabs?.getSnapshot)) return null; + try { + const rows = right.openTabs.getSnapshot(); + const sessionId = rightMountedSessionId(); + return (Array.isArray(rows) ? rows : []).find((item) => item && item.kind === TAB_KIND && (!sessionId || item.sessionId === sessionId)) || null; + } catch { + return null; + } + } function revealRightTab() { const right = get("sidebarRight"); if (!right) return; try { - const rows = isFn(right.openTabs?.getSnapshot) ? right.openTabs.getSnapshot() : []; - const row = Array.isArray(rows) ? rows.find((item) => item && item.kind === TAB_KIND) : null; + const row = findRightWorkbenchTab(); if (row && isFn(right.focus)) right.focus(row.tabId); if (isFn(right.isExpanded) && !right.isExpanded() && isFn(right.toggleExpanded)) right.toggleExpanded(); } catch (error) { @@ -2720,11 +2754,13 @@ function apply(ctx) { const right = get("sidebarRight"); if (!right || !isFn(right.openTab)) return false; if (!ensureRightTab()) return false; - try { - right.openTab(TAB_KIND); - } catch (error) { - console.warn("[sansheng] 打开右侧标签失败,改用浮动面板:", error && error.message ? error.message : error); - return false; + if (!findRightWorkbenchTab()) { + try { + right.openTab(TAB_KIND); + } catch (error) { + console.warn("[sansheng] 打开右侧标签失败,改用浮动面板:", error && error.message ? error.message : error); + return false; + } } revealRightTab(); for (const delay of [150, 500, 1200]) setTimeout(revealRightTab, delay); @@ -2814,6 +2850,7 @@ function apply(ctx) { } const selectedSeatCache = /* @__PURE__ */ new Map(); let lastSelectedSeatId = SEAT_IDS[0]; + let rightWorkbenchPinned = readRightWorkbenchPinned(); function SanshengWorkbench(props) { return (0, import_react2.createElement)(WorkbenchV2, { onClose: props && props.onClose, @@ -2830,9 +2867,25 @@ function apply(ctx) { const NAV_PANEL_ID = "sansheng-workbench"; const NAV_ORDER = 32; function openRightWorkbench() { - return tryOpenRightTab(); + const opened = tryOpenRightTab(); + if (opened) { + rightWorkbenchPinned = true; + writeRightWorkbenchPinned(true); + } + return opened; } function closeRightWorkbench() { + rightWorkbenchPinned = false; + writeRightWorkbenchPinned(false); + const right = get("sidebarRight"); + const row = findRightWorkbenchTab(); + if (row && right && isFn(right.close)) { + try { + right.close(row.tabId); + return; + } catch { + } + } const layout = get("layout"); if (layout && isFn(layout.selectPanel)) { try { @@ -2841,7 +2894,6 @@ function apply(ctx) { } catch { } } - const right = get("sidebarRight"); if (right && isFn(right.closeTab)) { try { right.closeTab(TAB_KIND); @@ -2850,6 +2902,24 @@ function apply(ctx) { } } } + function installRightDockPinning() { + const right = get("sidebarRight"); + if (!right || !isFn(right.mounted?.subscribe)) return; + const stop = right.mounted.subscribe(() => { + if (!rightWorkbenchPinned) return; + for (const delay of [80, 240, 700]) { + setTimeout(() => { + if (!rightWorkbenchPinned) return; + try { + if (!findRightWorkbenchTab()) openRightWorkbench(); + else revealRightTab(); + } catch { + } + }, delay); + } + }); + if (isFn(ctx.effect)) ctx.effect(() => stop); + } function SanshengNavButton(props) { const size = props && typeof props.size === "number" ? props.size : 16; const [floating, setFloating] = (0, import_react2.useState)(false); @@ -2988,6 +3058,12 @@ function apply(ctx) { } registerNavEntry(); registerRightTabType(); + installRightDockPinning(); + if (rightWorkbenchPinned) { + for (const delay of [300, 900]) setTimeout(() => { + if (rightWorkbenchPinned) openRightWorkbench(); + }, delay); + } try { install(ctx); } catch (error) { diff --git a/sansheng/src/client/index.js b/sansheng/src/client/index.js index 09fe8dc..abb349c 100644 --- a/sansheng/src/client/index.js +++ b/sansheng/src/client/index.js @@ -9,11 +9,24 @@ import { request } from './transport.js' import { CSS as WORKBENCH_CSS } from './styles.js' import { install as installNativePanel } from '../native/index.js' -export const inject = ['sessions', 'uiWorkspace', 'uiConversation', 'slots', 'theme', 'layout'] +export const inject = ['sessions', 'uiWorkspace', 'uiConversation', 'slots', 'theme', 'layout', 'sidebarRightTabs', 'sidebarRight'] const PLUGIN_ID = 'dsh-sansheng' const TAB_KIND = 'sansheng-workbench' const TAB_TYPE_ID = 'dsh-sansheng:workbench' +const RIGHT_PIN_KEY = 'sansheng.right-workbench.pinned' + +function readRightWorkbenchPinned() { + try { return typeof localStorage !== 'undefined' && localStorage.getItem(RIGHT_PIN_KEY) === '1' } catch { return false } +} + +function writeRightWorkbenchPinned(value) { + try { + if (typeof localStorage === 'undefined') return + if (value) localStorage.setItem(RIGHT_PIN_KEY, '1') + else localStorage.removeItem(RIGHT_PIN_KEY) + } catch { /* 忽略 */ } +} function installStyles(doc, tag, text) { if (!doc || !doc.head) return null @@ -215,12 +228,29 @@ export function apply(ctx) { try { return !!(tabs.get(TAB_KIND) || tabs.get(TAB_TYPE_ID)) } catch { return false } } // 桌面版右栏常是收起状态:只 openTab 不展开,用户会以为“点了没反应”。 + function rightMountedSessionId() { + const right = get('sidebarRight') + try { + if (right && isFn(right.mounted?.getSnapshot)) return right.mounted.getSnapshot() + } catch { /* 忽略 */ } + return currentSessionId() + } + + function findRightWorkbenchTab() { + const right = get('sidebarRight') + if (!right || !isFn(right.openTabs?.getSnapshot)) return null + try { + const rows = right.openTabs.getSnapshot() + const sessionId = rightMountedSessionId() + return (Array.isArray(rows) ? rows : []).find(item => item && item.kind === TAB_KIND && (!sessionId || item.sessionId === sessionId)) || null + } catch { return null } + } + function revealRightTab() { const right = get('sidebarRight') if (!right) return try { - const rows = isFn(right.openTabs?.getSnapshot) ? right.openTabs.getSnapshot() : [] - const row = Array.isArray(rows) ? rows.find(item => item && item.kind === TAB_KIND) : null + const row = findRightWorkbenchTab() if (row && isFn(right.focus)) right.focus(row.tabId) if (isFn(right.isExpanded) && !right.isExpanded() && isFn(right.toggleExpanded)) right.toggleExpanded() } catch (error) { @@ -246,9 +276,11 @@ export function apply(ctx) { const right = get('sidebarRight') if (!right || !isFn(right.openTab)) return false if (!ensureRightTab()) return false - try { right.openTab(TAB_KIND) } catch (error) { - console.warn('[sansheng] 打开右侧标签失败,改用浮动面板:', error && error.message ? error.message : error) - return false + if (!findRightWorkbenchTab()) { + try { right.openTab(TAB_KIND) } catch (error) { + console.warn('[sansheng] 打开右侧标签失败,改用浮动面板:', error && error.message ? error.message : error) + return false + } } // 右栏(尤其 better-sidebar 的原生面)是异步落位的,不能同步看不到就当失败, // 否则会误退成主区渲染、把中间聊天顶掉。这里先乐观返回,再多次补一次聚焦/展开。 @@ -317,6 +349,7 @@ export function apply(ctx) { // 工作台会随宿主会话切换而重挂载;按项目记住选中的部门,避免又跳回丞相。 const selectedSeatCache = new Map() let lastSelectedSeatId = SEAT_IDS[0] + let rightWorkbenchPinned = readRightWorkbenchPinned() // ---------- 工作台组件 ---------- // 工作台 v2:视觉与交互按用户提供的设计稿实现,数据全部走真实 host API。 @@ -339,20 +372,47 @@ export function apply(ctx) { const NAV_ORDER = 32 function openRightWorkbench() { - return tryOpenRightTab() + const opened = tryOpenRightTab() + if (opened) { rightWorkbenchPinned = true; writeRightWorkbenchPinned(true) } + return opened } function closeRightWorkbench() { + rightWorkbenchPinned = false + writeRightWorkbenchPinned(false) + const right = get('sidebarRight') + const row = findRightWorkbenchTab() + if (row && right && isFn(right.close)) { + try { right.close(row.tabId); return } catch { /* 忽略 */ } + } const layout = get('layout') if (layout && isFn(layout.selectPanel)) { try { layout.selectPanel(null); return } catch { /* 换下一个候选 */ } } - const right = get('sidebarRight') if (right && isFn(right.closeTab)) { try { right.closeTab(TAB_KIND); return } catch { /* 忽略 */ } } } + // 右侧栏按会话保存布局;切换部门打开新会话后,把固定工作台重新挂到新会话。 + function installRightDockPinning() { + const right = get('sidebarRight') + if (!right || !isFn(right.mounted?.subscribe)) return + const stop = right.mounted.subscribe(() => { + if (!rightWorkbenchPinned) return + for (const delay of [80, 240, 700]) { + setTimeout(() => { + if (!rightWorkbenchPinned) return + try { + if (!findRightWorkbenchTab()) openRightWorkbench() + else revealRightTab() + } catch { /* 会话尚未挂载时下次再试 */ } + }, delay) + } + }) + if (isFn(ctx.effect)) ctx.effect(() => stop) + } + // 导航行内容(宿主只把这里当图标渲染,标签由宿主按 label() 画)。 // 点击只开右侧工作台,并阻止冒泡,避免宿主把主区切成面板页——中央原生聊天因此保持原样。 function SanshengNavButton(props) { @@ -468,6 +528,10 @@ export function apply(ctx) { // 右侧工作台标签类型:需在 apply 时就注册(宿主按类型 id 选正文 slot)。 registerRightTabType() + installRightDockPinning() + if (rightWorkbenchPinned) { + for (const delay of [300, 900]) setTimeout(() => { if (rightWorkbenchPinned) openRightWorkbench() }, delay) + } // dsh-go 原生面板:浏览器(无 dshGo 服务)或桥未就绪时,install 内部安静退出。 try { diff --git a/sansheng/test/nav.test.js b/sansheng/test/nav.test.js index 520b83c..8dff56e 100644 --- a/sansheng/test/nav.test.js +++ b/sansheng/test/nav.test.js @@ -15,3 +15,11 @@ test('左侧入口使用 sidebar.panellist,位置在记忆系统与更多之 assert.ok(!source.includes('sidebar.session.action'), '不得再占用新会话右侧槽位') assert.ok(!source.includes('SanshengSessionAction'), '旧的新会话入口应已删除') }) + +test('右侧工作台依赖宿主 sidebarRight,并在会话切换后重新固定', async () => { + const source = await readFile(join(root, 'src/client/index.js'), 'utf8') + assert.match(source, /export const inject = \[[^\]]*'sidebarRightTabs', 'sidebarRight'/) + assert.match(source, /right\.mounted\.subscribe/) + assert.match(source, /right\.openTabs\.getSnapshot/) + assert.match(source, /right\.focus\(row\.tabId\)/) +})