paneLayoutTree.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // 分屏 Layout 层二叉分割树的模型与纯状态机。
  2. // 对齐:Focus 指向 Layout 的 pane,
  3. // pane 指向 (workspaceScope, sessionId),SessionDataLayer 对上两层零感知。
  4. //
  5. // 设计边界:
  6. // - 布局 = 二叉分割树(VS Code editor groups 模型):任意 pane 可向右/向下拆分,
  7. // 嵌套即得 2x2 等网格;叶子上限 MAX_WORKBENCH_PANES(性能基线 4 pane)。
  8. // - pane 绑定 = { workspaceScope, sessionId }:pane 自带 workspace 归属(含远程
  9. // remoteSessionId),不再限制同 workspace;早期版本的「split 属于别的 workspace 时
  10. // 不渲染」规则删除——pane 跨 workspace tab 常驻。
  11. // - workspaceScope 语义 = session 归属的主(primary)workspace,是连接路由键;
  12. // 不是「session 能触达的全部路径」(未来跨 workspace session 的辅助路径是
  13. // session 属性,不进布局层)。
  14. // - primary pane(workspace-main)是保留叶子:不进 panes,绑定沿用既有选择态
  15. // (activeTaskId → shell props),避免与 zcodeSessionStore/tabStore 双写;
  16. // tabStore(workspace tab 语义)完全不动。
  17. // 所有转移函数无副作用;无变化时返回原引用(zustand 免重渲染)。
  18. /** primary pane 固定 id(沿用写死的 paneId,testid 契约不变)。 */
  19. export const V4_PRIMARY_PANE_ID = "workspace-main";
  20. /** 叶子 pane 数量上限(性能验收基线「4 pane 同时流式不掉帧」;也是 CLI 子进程数软上限)。 */
  21. export const MAX_WORKBENCH_PANES = 4;
  22. /** 分割占比边界:first 子树占比最小 25% / 最大 75%(拖拽与持久化共用同一 clamp)。 */
  23. export const SPLIT_RATIO_MIN = 0.25;
  24. export const SPLIT_RATIO_MAX = 0.75;
  25. export const DEFAULT_SPLIT_RATIO = 0.5;
  26. export function clampSplitRatio(ratio: number): number {
  27. if (!Number.isFinite(ratio)) {
  28. return DEFAULT_SPLIT_RATIO;
  29. }
  30. return Math.min(SPLIT_RATIO_MAX, Math.max(SPLIT_RATIO_MIN, ratio));
  31. }
  32. /**
  33. * pane 的 workspace 归属(= session 的 primary workspace,连接路由键)。
  34. * - 本地:只有 workspacePath;
  35. * - 远程(SSH/WSL/Docker):workspaceIdentity 必填(Workspace Identity 约束),
  36. * remoteSessionId 指向 remoteWorkspaceSessionStore 的连接(缺省时按 identity 解析)。
  37. */
  38. export interface PaneWorkspaceScope {
  39. readonly workspacePath: string;
  40. readonly workspaceIdentity?: string;
  41. readonly remoteSessionId?: string;
  42. }
  43. /** 身份/隔离语义统一口径:workspaceKey = workspaceIdentity?.trim() || workspacePath。 */
  44. export function paneWorkspaceKey(scope: PaneWorkspaceScope): string {
  45. return scope.workspaceIdentity?.trim() || scope.workspacePath;
  46. }
  47. export function paneBindingMatchesSession(
  48. binding: PaneBinding | null | undefined,
  49. scope: PaneWorkspaceScope,
  50. sessionId: string,
  51. ): boolean {
  52. return Boolean(
  53. binding?.sessionId === sessionId &&
  54. paneWorkspaceKey(binding.workspaceScope) === paneWorkspaceKey(scope),
  55. );
  56. }
  57. export interface PaneBinding {
  58. /** = session 归属的主 workspace(连接路由键);辅助路径不在此。 */
  59. readonly workspaceScope: PaneWorkspaceScope;
  60. /** 绑定的 CLI session;null = draft(空 pane 动线:首发 createSession 后原地绑定)。 */
  61. readonly sessionId: string | null;
  62. /** 只读 pane 不渲染 composer/input;当前用于 subagent 行内侧开的 child session。 */
  63. readonly readOnly?: boolean;
  64. /**
  65. * 从持久化恢复、尚未经该 scope 的 sessions-index 验证存在性的绑定(标记本身不持久化)。
  66. * 在场 → confirmRestoredPaneSession 清除;已删 → closePane 优雅塌缩。
  67. */
  68. readonly restoredUnvalidated?: boolean;
  69. }
  70. export type SplitDirection = "row" | "column";
  71. /** IDE 式拖拽拆分方向:left/up 插到 anchor 前,right/down 插到 anchor 后。 */
  72. export type PaneSplitSide = "left" | "right" | "up" | "down";
  73. export type PaneLayoutNode =
  74. | { readonly type: "leaf"; readonly paneId: string }
  75. | {
  76. readonly type: "split";
  77. /** 分割节点 id(拖拽 CSS 变量与 setSplitRatio 的定位键)。 */
  78. readonly id: string;
  79. readonly direction: SplitDirection;
  80. /** first 子树占比,clamp [0.25, 0.75]。 */
  81. readonly ratio: number;
  82. readonly first: PaneLayoutNode;
  83. readonly second: PaneLayoutNode;
  84. };
  85. export interface PaneLayoutSnapshot {
  86. readonly root: PaneLayoutNode;
  87. /** 非 primary pane 的绑定;primary 绑定沿用 shell props(activeTaskId),不入此表。 */
  88. readonly panes: Readonly<Record<string, PaneBinding>>;
  89. /** 全应用唯一的「当前」pane(Focus 层单值)。 */
  90. readonly focusedPaneId: string;
  91. }
  92. export const PRIMARY_LEAF: PaneLayoutNode = {
  93. type: "leaf",
  94. paneId: V4_PRIMARY_PANE_ID,
  95. };
  96. export const INITIAL_PANE_LAYOUT: PaneLayoutSnapshot = {
  97. root: PRIMARY_LEAF,
  98. panes: {},
  99. focusedPaneId: V4_PRIMARY_PANE_ID,
  100. };
  101. // ============================================================================
  102. // 树工具(未变化路径保持结构共享 = 原引用)。
  103. // ============================================================================
  104. /** 前序收集叶子 paneId。 */
  105. export function leafPaneIds(node: PaneLayoutNode): string[] {
  106. if (node.type === "leaf") {
  107. return [node.paneId];
  108. }
  109. return [...leafPaneIds(node.first), ...leafPaneIds(node.second)];
  110. }
  111. export function countPanes(state: PaneLayoutSnapshot): number {
  112. return leafPaneIds(state.root).length;
  113. }
  114. /** 是否还能再拆出新 pane(叶子数 < MAX_WORKBENCH_PANES)。 */
  115. export function canAddPane(state: PaneLayoutSnapshot): boolean {
  116. return countPanes(state) < MAX_WORKBENCH_PANES;
  117. }
  118. function leafExists(node: PaneLayoutNode, paneId: string): boolean {
  119. if (node.type === "leaf") {
  120. return node.paneId === paneId;
  121. }
  122. return leafExists(node.first, paneId) || leafExists(node.second, paneId);
  123. }
  124. /** 焦点 pane 不在树中(如刚被关闭/恢复数据异常)时退化为 primary。 */
  125. export function effectiveFocusedPaneId(state: PaneLayoutSnapshot): string {
  126. return leafExists(state.root, state.focusedPaneId)
  127. ? state.focusedPaneId
  128. : V4_PRIMARY_PANE_ID;
  129. }
  130. /** 分配新 pane id:pane-<n>,n = 树内既有序号最大值 + 1(与 workspace-main/split 保留 id 无碰撞)。 */
  131. function allocatePaneId(root: PaneLayoutNode): string {
  132. let max = 0;
  133. for (const paneId of leafPaneIds(root)) {
  134. const match = /^pane-(\d+)$/.exec(paneId);
  135. if (match) {
  136. max = Math.max(max, Number(match[1]));
  137. }
  138. }
  139. return `pane-${max + 1}`;
  140. }
  141. function splitNodeIds(node: PaneLayoutNode): string[] {
  142. if (node.type === "leaf") {
  143. return [];
  144. }
  145. return [node.id, ...splitNodeIds(node.first), ...splitNodeIds(node.second)];
  146. }
  147. /** 分配新分割节点 id:n<k>(短 id,进 CSS 变量名 --v4-split-<id>)。 */
  148. function allocateSplitNodeId(root: PaneLayoutNode): string {
  149. let max = 0;
  150. for (const id of splitNodeIds(root)) {
  151. const match = /^n(\d+)$/.exec(id);
  152. if (match) {
  153. max = Math.max(max, Number(match[1]));
  154. }
  155. }
  156. return `n${max + 1}`;
  157. }
  158. /** 把 paneId 叶子替换为 replacement;未命中路径保持原引用。 */
  159. function replaceLeaf(
  160. node: PaneLayoutNode,
  161. paneId: string,
  162. replacement: PaneLayoutNode,
  163. ): PaneLayoutNode {
  164. if (node.type === "leaf") {
  165. return node.paneId === paneId ? replacement : node;
  166. }
  167. const first = replaceLeaf(node.first, paneId, replacement);
  168. const second = replaceLeaf(node.second, paneId, replacement);
  169. if (first === node.first && second === node.second) {
  170. return node;
  171. }
  172. return { ...node, first, second };
  173. }
  174. /** 移除叶子:父分割节点塌缩为兄弟子树;返回 null 表示整棵树被移除(仅当根就是该叶子)。 */
  175. function removeLeaf(
  176. node: PaneLayoutNode,
  177. paneId: string,
  178. ): PaneLayoutNode | null {
  179. if (node.type === "leaf") {
  180. return node.paneId === paneId ? null : node;
  181. }
  182. const first = removeLeaf(node.first, paneId);
  183. if (first === null) {
  184. return node.second;
  185. }
  186. const second = removeLeaf(node.second, paneId);
  187. if (second === null) {
  188. return node.first;
  189. }
  190. if (first === node.first && second === node.second) {
  191. return node;
  192. }
  193. return { ...node, first, second };
  194. }
  195. function replaceSplitRatio(
  196. node: PaneLayoutNode,
  197. splitId: string,
  198. ratio: number,
  199. ): PaneLayoutNode {
  200. if (node.type === "leaf") {
  201. return node;
  202. }
  203. if (node.id === splitId) {
  204. return node.ratio === ratio ? node : { ...node, ratio };
  205. }
  206. const first = replaceSplitRatio(node.first, splitId, ratio);
  207. const second = replaceSplitRatio(node.second, splitId, ratio);
  208. if (first === node.first && second === node.second) {
  209. return node;
  210. }
  211. return { ...node, first, second };
  212. }
  213. // ============================================================================
  214. // 纯状态机(单测对象)
  215. // ============================================================================
  216. /**
  217. * 在 anchor pane 处拆分:anchor 叶子替换为分割节点 { anchor, 新 pane },
  218. * 新 pane 绑定 binding(draft 或已有 session)并接管焦点。
  219. * anchor 不在树中 / 叶子数达上限 → no-op(原引用)。
  220. */
  221. export function splitPaneAt(
  222. state: PaneLayoutSnapshot,
  223. anchorPaneId: string,
  224. direction: SplitDirection,
  225. binding: PaneBinding,
  226. ): PaneLayoutSnapshot {
  227. if (!leafExists(state.root, anchorPaneId) || !canAddPane(state)) {
  228. return state;
  229. }
  230. const newPaneId = allocatePaneId(state.root);
  231. const splitNode: PaneLayoutNode = {
  232. type: "split",
  233. id: allocateSplitNodeId(state.root),
  234. direction,
  235. ratio: DEFAULT_SPLIT_RATIO,
  236. first: { type: "leaf", paneId: anchorPaneId },
  237. second: { type: "leaf", paneId: newPaneId },
  238. };
  239. return {
  240. root: replaceLeaf(state.root, anchorPaneId, splitNode),
  241. panes: { ...state.panes, [newPaneId]: binding },
  242. focusedPaneId: newPaneId,
  243. };
  244. }
  245. /**
  246. * 在 anchor pane 四周拆分:left/up 把新 pane 放在 anchor 前,right/down 放在后。
  247. * 拖拽分屏需要保留用户的方位意图;旧 splitPaneAt 继续保持「anchor 在前、新 pane 在后」契约。
  248. */
  249. export function splitPaneAtSide(
  250. state: PaneLayoutSnapshot,
  251. anchorPaneId: string,
  252. side: PaneSplitSide,
  253. binding: PaneBinding,
  254. ): PaneLayoutSnapshot {
  255. if (!leafExists(state.root, anchorPaneId) || !canAddPane(state)) {
  256. return state;
  257. }
  258. const newPaneId = allocatePaneId(state.root);
  259. const anchorLeaf: PaneLayoutNode = { type: "leaf", paneId: anchorPaneId };
  260. const newLeaf: PaneLayoutNode = { type: "leaf", paneId: newPaneId };
  261. const before = side === "left" || side === "up";
  262. const splitNode: PaneLayoutNode = {
  263. type: "split",
  264. id: allocateSplitNodeId(state.root),
  265. direction: side === "left" || side === "right" ? "row" : "column",
  266. ratio: DEFAULT_SPLIT_RATIO,
  267. first: before ? newLeaf : anchorLeaf,
  268. second: before ? anchorLeaf : newLeaf,
  269. };
  270. return {
  271. root: replaceLeaf(state.root, anchorPaneId, splitNode),
  272. panes: { ...state.panes, [newPaneId]: binding },
  273. focusedPaneId: newPaneId,
  274. };
  275. }
  276. /**
  277. * 关闭 pane:叶子移除,父分割节点塌缩为兄弟子树;绑定移除。
  278. * primary 不可关;pane 不在树中 → no-op。焦点在被关 pane 上时归还 primary。
  279. * 关 pane ≠ 停 session:只是退订视图,session 在 CLI 里照跑。
  280. */
  281. export function closePane(
  282. state: PaneLayoutSnapshot,
  283. paneId: string,
  284. ): PaneLayoutSnapshot {
  285. if (paneId === V4_PRIMARY_PANE_ID || !leafExists(state.root, paneId)) {
  286. return state;
  287. }
  288. const root = removeLeaf(state.root, paneId) ?? PRIMARY_LEAF;
  289. const panes = { ...state.panes };
  290. delete panes[paneId];
  291. return {
  292. root,
  293. panes,
  294. focusedPaneId:
  295. state.focusedPaneId === paneId ? V4_PRIMARY_PANE_ID : state.focusedPaneId,
  296. };
  297. }
  298. /** draft pane 首发 createSession 后原地绑定 session。pane 无绑定/未变(且无待验证标记)时 no-op。 */
  299. export function bindPaneSession(
  300. state: PaneLayoutSnapshot,
  301. paneId: string,
  302. sessionId: string,
  303. ): PaneLayoutSnapshot {
  304. const binding = state.panes[paneId];
  305. if (
  306. !binding ||
  307. (binding.sessionId === sessionId && !binding.restoredUnvalidated)
  308. ) {
  309. return state;
  310. }
  311. // 实时绑定即权威,不需要再验证:不带 restoredUnvalidated 重建。
  312. return {
  313. ...state,
  314. panes: {
  315. ...state.panes,
  316. [paneId]: { workspaceScope: binding.workspaceScope, sessionId },
  317. },
  318. };
  319. }
  320. /**
  321. * 原位替换非 primary pane 的完整 session binding。
  322. * draft split 后普通 session 点击过去只更新 shell activeTaskId,导致 primary
  323. * draft 被新 session 覆盖;这里先替换 focused secondary 的 scope + session owner。
  324. */
  325. export function replacePaneBinding(
  326. state: PaneLayoutSnapshot,
  327. paneId: string,
  328. binding: PaneBinding,
  329. ): PaneLayoutSnapshot {
  330. if (!state.panes[paneId] || paneId === V4_PRIMARY_PANE_ID) {
  331. return state;
  332. }
  333. return {
  334. ...state,
  335. panes: {
  336. ...state.panes,
  337. [paneId]: binding,
  338. },
  339. focusedPaneId: paneId,
  340. };
  341. }
  342. export function findPaneIdForSession(
  343. state: PaneLayoutSnapshot,
  344. scope: PaneWorkspaceScope,
  345. sessionId: string,
  346. ): string | null {
  347. for (const [paneId, binding] of Object.entries(state.panes)) {
  348. if (paneBindingMatchesSession(binding, scope, sessionId)) {
  349. return paneId;
  350. }
  351. }
  352. return null;
  353. }
  354. /** 拖拽调宽提交:按分割节点 id 定位,clamp 到 [25%, 75%];未变化返回原引用。 */
  355. export function setSplitNodeRatio(
  356. state: PaneLayoutSnapshot,
  357. splitId: string,
  358. ratio: number,
  359. ): PaneLayoutSnapshot {
  360. const root = replaceSplitRatio(state.root, splitId, clampSplitRatio(ratio));
  361. if (root === state.root) {
  362. return state;
  363. }
  364. return { ...state, root };
  365. }
  366. /** 聚焦 pane:只接受当前树里存在的叶子,其余 no-op(原引用)。 */
  367. export function focusPane(
  368. state: PaneLayoutSnapshot,
  369. paneId: string,
  370. ): PaneLayoutSnapshot {
  371. if (state.focusedPaneId === paneId || !leafExists(state.root, paneId)) {
  372. return state;
  373. }
  374. return { ...state, focusedPaneId: paneId };
  375. }
  376. /** 持久化恢复的 pane 绑定经 sessions-index 验证在场后清除待验证标记。无标记时 no-op。 */
  377. export function confirmRestoredPaneSession(
  378. state: PaneLayoutSnapshot,
  379. paneId: string,
  380. ): PaneLayoutSnapshot {
  381. const binding = state.panes[paneId];
  382. if (!binding?.restoredUnvalidated) {
  383. return state;
  384. }
  385. return {
  386. ...state,
  387. panes: {
  388. ...state.panes,
  389. [paneId]: {
  390. workspaceScope: binding.workspaceScope,
  391. sessionId: binding.sessionId,
  392. },
  393. },
  394. };
  395. }
  396. /**
  397. * 侧栏/下钻「在分屏打开」:该 session 已在某个 pane(按 workspaceKey + sessionId 判等,
  398. * 归属/隔离语义)→ 聚焦它;否则拆分当前焦点 pane 向右。达上限且无既有 pane → no-op。
  399. */
  400. export function openSessionInNewPane(
  401. state: PaneLayoutSnapshot,
  402. scope: PaneWorkspaceScope,
  403. sessionId: string,
  404. ): PaneLayoutSnapshot {
  405. const workspaceKey = paneWorkspaceKey(scope);
  406. for (const [paneId, binding] of Object.entries(state.panes)) {
  407. if (
  408. binding.sessionId === sessionId &&
  409. paneWorkspaceKey(binding.workspaceScope) === workspaceKey
  410. ) {
  411. return focusPane(state, paneId);
  412. }
  413. }
  414. return splitPaneAt(state, effectiveFocusedPaneId(state), "row", {
  415. workspaceScope: scope,
  416. sessionId,
  417. });
  418. }