Files
Netcatty/components/terminalLayer/terminalLayerViewMemo.ts
T
GOUKI9999andbincxz 00e63be4f1 feat(ai): global AI session manager with cross-scope rebind (#4098 phases 4-5) (#4159)
* feat(ai): global AI session manager with cross-scope rebind

Scope-agnostic view over every AI chat regardless of panel scope
(issue #4098 phases 4-5), hosted as a scoped/global toggle inside the
existing session history drawer.

- Global list: title, agent, resolved scope (workspace title / terminal
  label / global / dangling), state badge (active / idle / dissociated),
  message count, relative time; title-substring search, agent filter,
  newest-first ordering and a render batch guard. Titles only, never
  message bodies.
- Per-row actions: open/switch (exact-scope chats only; other-scope rows
  offer rebind instead of faking activation), rebind to the current
  panel (issue #4019 path) or to any open workspace/terminal via an
  explicit target picker, confirm-gated per-record delete, and
  single-chat export (exportSessionsBackup now takes an optional
  session-id subset; import merges by id).
- App-wide open terminals are threaded to every AI panel so the manager
  resolves scopes and rebind targets across tabs.
- Deletion safety: no scopeKey is passed, stale pointers of any panel
  are repaired by the #4125 validation effect; deleting the last chat
  cannot resurrect on reboot (intentionally-deleted ids + migration
  marker). No cleanup/eviction semantics are touched (#4015 R2).

* fix(ai): stop active turns before scope rebind

Rebinding a chat while a turn streams into it corrupts streaming state
(the stream targets the old scope while the active map/panel pointer move
underneath it). Both rebind paths — the global session manager and the
/rebind <n> panel command — now await stopStreamingForSession first,
chosen over rejecting the rebind because it cleanly awaits termination
(stopAgentTurn + waitForActiveTurn) and preserves the user's rebind
intent. stopStreamingForSession moves above the /rebind handler to avoid
a TDZ reference; the guard lives in AIChatSidePanelHelpers so both call
sites share it. Also passes the panel scope host ids to the manager for
the 'This panel' rebind target.

* fix(ai): derive session manager rebind targets from all live terminals

The global AI session manager built its app-wide terminal list from
contextsByTabId, which only covers tabs whose AI panel context is
mounted - a live terminal without one was missing as a rebind target.
useTerminalAiContexts now also exposes buildAllTerminalAiInfos over the
full terminal session store (same buildAITerminalSessionInfo per
session, so infos merge seamlessly where a context exists) and threads
it to AIChatPanelsHost as appTerminalSessions.

* fix(ai): preserve host ids on panel rebind and reset stale agent filters

- 'This panel' rebind now carries the viewing panel's scope host ids
  (currentScopeHostIds) onto the rewritten scope, so the destination
  host association is not orphaned; explicit picker targets already
  forwarded their hostIds.
- The agent filter no longer survives its agent: when the filtered
  agent's last chat is deleted the filter resets to 'all' instead of
  leaving a permanently empty list with a blank select; the select's
  built-in 'all' option stays the manual clear affordance.

* fix(ai): clear source workspace views when rebinding a chat

* fix(ai): demote implicit source views when rebinding restored chats

* fix(ai): stabilize appTerminalInfos identity and polish global session manager UI

- Retain appTerminalInfos array identity across structurally equal terminal updates so unrelated tab state churn does not re-render every mounted AIChatSidePanel.
- Replace native <select> elements in GlobalAIChatSessionManager with the shared Radix Select component for consistent dark/light theme rendering.
- Wrap the disabled cross-scope open button in a <span> inside TooltipTrigger (and add type="button" to row action buttons) so hover tooltips fire reliably.

* fix(ai): carry member host IDs on explicit workspace rebind targets

* fix(ai): remove redundant title beside history drawer scope switcher

* fix(ai): guard optional appTerminalSessions in AIChatPanelsHost

* fix(ai): clear persisted source workspace anchors when rebinding chats

* fix(ai): exclude non-exact workspace matches from implicit auto-resume

---------

Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
2026-11-22 11:53:02 +08:00

542 lines
18 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import { aiPanelContextsEqual } from '../../domain/aiPanelContextsEqual';
import { sftpPickerSessionsEqual } from '../../domain/sftpConnectedHosts';
type Ctx = Record<string, any>;
function eq(prev: Ctx, next: Ctx, key: string): boolean {
return prev[key] === next[key];
}
function rectEqual(a: any, b: any): boolean {
if (a === b) return true;
if (!a || !b) return false;
return a.x === b.x && a.y === b.y && a.w === b.w && a.h === b.h;
}
function rectRecordEqual(a: any, b: any): boolean {
if (a === b) return true;
if (!a || !b) return false;
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
if (aKeys.length !== bKeys.length) return false;
for (const key of aKeys) {
if (!rectEqual(a[key], b[key])) return false;
}
return true;
}
function workspaceRectsByIdEqual(a: any, b: any): boolean {
if (a === b) return true;
if (!(a instanceof Map) || !(b instanceof Map)) return false;
if (a.size !== b.size) return false;
for (const [workspaceId, rects] of a) {
if (!rectRecordEqual(rects, b.get(workspaceId))) return false;
}
return true;
}
function resizerHandleEqual(a: any, b: any): boolean {
if (a === b) return true;
if (!a || !b) return false;
return a.id === b.id
&& a.splitId === b.splitId
&& a.index === b.index
&& a.direction === b.direction
&& rectEqual(a.rect, b.rect)
&& rectEqual(a.splitArea, b.splitArea);
}
function resizerHandlesEqual(a: any, b: any): boolean {
if (a === b) return true;
if (!Array.isArray(a) || !Array.isArray(b)) return false;
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i += 1) {
if (!resizerHandleEqual(a[i], b[i])) return false;
}
return true;
}
function arrayEqual(a: any, b: any): boolean {
if (a === b) return true;
if (!Array.isArray(a) || !Array.isArray(b)) return false;
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i += 1) {
if (a[i] !== b[i]) return false;
}
return true;
}
function workspaceNodeEqual(a: any, b: any): boolean {
if (a === b) return true;
if (!a || !b) return false;
if (a.id !== b.id || a.type !== b.type) return false;
if (a.type === 'pane') {
return a.sessionId === b.sessionId;
}
if (a.direction !== b.direction) return false;
if (!arrayEqual(a.sizes, b.sizes)) return false;
if (!Array.isArray(a.children) || !Array.isArray(b.children)) return false;
if (a.children.length !== b.children.length) return false;
for (let i = 0; i < a.children.length; i += 1) {
if (!workspaceNodeEqual(a.children[i], b.children[i])) return false;
}
return true;
}
function activeWorkspaceEqual(a: any, b: any): boolean {
if (a === b) return true;
if (!a || !b) return false;
return a.id === b.id
&& a.title === b.title
&& a.viewMode === b.viewMode
&& a.focusedSessionId === b.focusedSessionId
&& a.snippetId === b.snippetId
&& arrayEqual(a.focusSessionOrder, b.focusSessionOrder)
&& workspaceNodeEqual(a.root, b.root);
}
function terminalThemeEqual(prev: Ctx, next: Ctx, key: string): boolean {
if (key !== 'terminalTheme') return prev[key] === next[key];
const a = prev.terminalTheme;
const b = next.terminalTheme;
if (a === b) return true;
if (!a || !b) return false;
return a.id === b.id
&& a.colors.background === b.colors.background
&& a.colors.foreground === b.colors.foreground
&& a.colors.cursor === b.colors.cursor;
}
function workspaceCtxKeyEqual(prev: Ctx, next: Ctx, key: string): boolean {
if (key === 'computeSplitHint' || key === 'handleWorkspaceDrop') {
if (!prev.draggingSessionId && !next.draggingSessionId) return true;
}
if (key === 'activeWorkspace') {
return activeWorkspaceEqual(prev.activeWorkspace, next.activeWorkspace);
}
if (key === 'workspaceRectsById') {
return workspaceRectsByIdEqual(prev.workspaceRectsById, next.workspaceRectsById);
}
if (key === 'activeResizers') {
return resizerHandlesEqual(prev.activeResizers, next.activeResizers);
}
if (key === 'terminalTheme') {
return terminalThemeEqual(prev, next, key);
}
return prev[key] === next[key];
}
function scriptRunsEqual(a: any, b: any): boolean {
if (a === b) return true;
if (!Array.isArray(a) || !Array.isArray(b)) return false;
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i += 1) {
const prevRun = a[i];
const nextRun = b[i];
if (prevRun === nextRun) continue;
if (!prevRun || !nextRun) return false;
if (
prevRun.runId !== nextRun.runId
|| prevRun.status !== nextRun.status
|| prevRun.stepIndex !== nextRun.stepIndex
|| prevRun.waitingFor !== nextRun.waitingFor
|| prevRun.error !== nextRun.error
|| prevRun.logs?.length !== nextRun.logs?.length
) {
return false;
}
}
return true;
}
function sidePanelCtxKeyEqual(prev: Ctx, next: Ctx, key: string): boolean {
if (key === 'scriptRuns') {
return scriptRunsEqual(prev.scriptRuns, next.scriptRuns);
}
if (key === 'activeWorkspace') {
return activeWorkspaceEqual(prev.activeWorkspace, next.activeWorkspace);
}
if (key === 'terminalTheme') {
return terminalThemeEqual(prev, next, key);
}
if (key === 'sessions') {
// Connected picker and retained system panels need transport and tab ownership,
// while title/cwd churn can still be ignored.
if (prev.sessions === next.sessions) return true;
if (!sftpPickerSessionsEqual(prev.sessions, next.sessions)) return false;
if (!Array.isArray(prev.sessions) || !Array.isArray(next.sessions)) return false;
return prev.sessions.every((session: any, index: number) => (
session.workspaceId === next.sessions[index]?.workspaceId
));
}
if (key === 'workspaceById') {
// Focus-only workspace map identity changes must not invalidate every mounted
// side-panel slot; System reads focused session from sidePanelLiveStore.
return sidePanelWorkspaceByIdEqual(prev.workspaceById, next.workspaceById);
}
if (key === 'aiContextsByTabId') {
return aiPanelContextsEqual(prev.aiContextsByTabId, next.aiContextsByTabId);
}
return prev[key] === next[key];
}
function sidePanelWorkspaceByIdEqual(a: any, b: any): boolean {
if (a === b) return true;
if (!(a instanceof Map) || !(b instanceof Map)) return false;
if (a.size !== b.size) return false;
for (const [workspaceId, prevWorkspace] of a) {
const nextWorkspace = b.get(workspaceId);
if (!nextWorkspace) return false;
if (prevWorkspace === nextWorkspace) continue;
if (prevWorkspace.id !== nextWorkspace.id) return false;
if (prevWorkspace.title !== nextWorkspace.title) return false;
if (prevWorkspace.viewMode !== nextWorkspace.viewMode) return false;
if (prevWorkspace.snippetId !== nextWorkspace.snippetId) return false;
if (!workspaceNodeEqual(prevWorkspace.root, nextWorkspace.root)) return false;
}
return true;
}
// Live fields that are also published via sidePanelLiveStore (or dedicated
// stores like scriptRunsStore). Slots subscribe to those stores directly, so
// TerminalLayerView must NOT re-render on these ticks — only chrome-stable keys.
const SIDE_PANEL_LIVE_CTX_KEYS = [
'activeTerminalSessionForSystem',
'activeSystemSessionHost',
'focusedHost',
'focusedSessionId',
'historySessionId',
'resolvedPreviewTheme',
'previewedOrVisibleThemeId',
'sftpActiveHost',
'activeTerminalSessionIdForSftp',
'activeTerminalCwd',
'activeTerminalCwdTrusted',
'activeWorkspace',
'focusedFontFamilyId',
'focusedFontFamilyOverridden',
'focusedFontSize',
'focusedFontSizeOverridden',
'focusedFontWeight',
'focusedFontWeightOverridden',
'focusedThemeOverridden',
] as const;
const SIDE_PANEL_STABLE_CTX_KEYS = [
'mountedSftpTabIds',
'mountedAiTabIds',
'notesMountedTabIds',
'browserMountedTabIds',
'notesOpenNoteByTab',
'scriptsMountedTabIds',
'systemMountedTabIds',
'themeMountedTabIds',
'handleHistoryPaste',
'handleHistoryDelete',
'handleHistoryClear',
'handleHistoryRun',
'handleOpenHistory',
'HistorySidePanel',
'History',
'sidePanelWidth',
'sidePanelHeight',
'sidePanelPosition',
'isComposeBarOpen',
'sidePanelOpenTabs',
'sidePanelLayouts',
'sftpHostForTab',
'sftpPaneClosedTabIdsRef',
'sftpRetainedAfterCloseTabIdsRef',
'effectiveHosts',
'hosts',
// SFTP Connected picker reads live terminal sessions from stable ctx.
'sessions',
'sessionHostsMap',
'sftpAuthHostBySessionId',
'workspaceById',
'keys',
'identities',
'knownHosts',
'updateHosts',
'handleAddKnownHost',
'updateSnippets',
'updateSnippetPackages',
'sftpDefaultViewMode',
'sftpInitialLocationForTab',
'sftpPendingUploadsForTab',
'handleSftpCurrentPathChange',
'handleSftpActiveTransfersChange',
'handleSftpActiveExternalEditsChange',
'sftpDoubleClickBehavior',
'sftpAutoSync',
'sftpShowHiddenFiles',
'sftpUseCompressedUpload',
'sftpFollowTerminalCwd',
'setSftpFollowTerminalCwd',
'hotkeyScheme',
'keyBindings',
'editorWordWrap',
'setEditorWordWrap',
'getTerminalCwd',
'refocusActiveTerminalSession',
'terminalSettings',
'snippets',
'snippetPackages',
'handleSnippetFromPanel',
'handleRunScriptFromPanel',
'handleRunScriptOnWorkspace',
'handleStartRecordingFromPanel',
'handleStopScriptRun',
'handlePauseScriptRun',
'handleResumeScriptRun',
'followAppTerminalTheme',
'terminalTheme',
'terminalThemeId',
'terminalFontFamilyId',
'handleThemeChangeForFocusedSession',
'handleThemeResetForFocusedSession',
'handleFontFamilyChangeForFocusedSession',
'handleFontFamilyResetForFocusedSession',
'handleFontSizeChangeForFocusedSession',
'handleFontSizeResetForFocusedSession',
'handleFontWeightChangeForFocusedSession',
'handleFontWeightResetForFocusedSession',
'aiContextsByTabId',
'appTerminalInfos',
'resolveAIExecutorContext',
'pendingTerminalSelectionForAI',
'handlePendingTerminalSelectionConsumed',
'setSidePanelWidth',
'persistSidePanelWidth',
'setSidePanelHeight',
'persistSidePanelHeight',
'handleToggleSftpFromBar',
'handleOpenScripts',
'handleOpenTheme',
'handleOpenAI',
'handleOpenNotes',
'getActiveTerminalSessionId',
'handleSwitchSidePanelTab',
'handleWithdrawPluginSidebarPane',
'handleOpenBrowser',
'handleBackFromNotes',
'handleOpenHostFromNotes',
'handleOpenSystem',
'handleCloseSidePanel',
'handleFocusSidePanelPane',
'handleMagnifySidePanelPane',
'handleRestoreMagnifiedPane',
'handleSplitSidePanelPane',
'handleCloseSidePanelPane',
'handleResizeSidePanelSplit',
'setSidePanelPosition',
'handleSftpInitialLocationApplied',
'handlePendingUploadHandled',
'validAIScopeTargetIds',
'magnifiedPane',
'AISidePanelStateRoot',
'NotesManager',
// notes / noteGroups / updateNotes / updateNoteGroups come from notesStore.
'onOpenVaultNoteFromChat',
'onOpenVaultHostFromChat',
'onOpenVaultSectionFromChat',
'onOpenVaultSnippetFromChat',
't',
] as const;
const WORKSPACE_CTX_KEYS = [
// Stay. Solo SSH A->B already keeps TerminalPanesHost equal when the sessions
// ref is unchanged (this compare does not feed the panes host). The visible
// pane still pays reveal flush + repaint, and focus retries at 50ms.
// Dropping activeTabId only skips one workspace-section pass; the live-store
// host/theme update still renders the section. That is not the settle cost.
'activeTabId',
'workspaceInnerRef',
'workspaceOuterRef',
'workspaceOverlayRef',
'draggingSessionId',
'isFocusMode',
'dropHint',
'setDropHint',
'computeSplitHint',
'handleWorkspaceDrop',
'onRequestAddToWorkspace',
'sessions',
'sessionHostsMap',
'sftpAuthHostBySessionId',
'sessionChainHostsMap',
'sessionSudoAutofillPasswordsMap',
'sessionSudoAutofillCandidatesMap',
'workspaceById',
'workspaceRectsById',
'isTerminalLayerVisible',
'magnifiedPane',
'handleMagnifyTerminalPane',
'handleTerminalPaneInteraction',
'handleRestoreMagnifiedPane',
'workspaceFocusHandlersRef',
'workspaceBroadcastHandlersRef',
'splitHorizontalHandlersRef',
'splitVerticalHandlersRef',
'resolveSessionAppearance',
'hostMap',
'keys',
'identities',
'snippets',
'knownHosts',
'terminalFontFamilyId',
'fontSize',
'terminalTheme',
'followAppTerminalTheme',
// accentMode / customAccent come from appearanceChromeStore (Terminal leaf).
'terminalSettings',
'hotkeyScheme',
'disableTerminalFontZoom',
'restoreTerminalCwd',
'keyBindings',
'resizing',
'isComposeBarOpen',
'sessionLogConfig',
'sshDebugLogsEnabled',
'onHotkeyAction',
'handleTerminalFontSizeChange',
'handleOpenSftp',
'handleTerminalCwdChange',
'handleTerminalTitleChange',
'handleTerminalBell',
'handleTerminalOutput',
'handleTerminalContextReaderChange',
'handleOpenScripts',
'handleOpenHistory',
'handleOpenSystem',
'handleOpenTheme',
'handleCloseSession',
'handleStatusChange',
'handleSessionExit',
'handleTerminalDataCapture',
'handleOsDetected',
'handleUpdateHost',
'handleAddKnownHost',
'handleCommandExecuted',
'handleCommandSubmitted',
'onSetWorkspaceFocusedSession',
'onSetWorkspaceAnchorChat',
'onSplitSession',
'isBroadcastEnabled',
'isComposeSendToAll',
'onToggleComposeSendTarget',
'handleBroadcastInput',
'handleBroadcastInterruptPriorityChange',
'handleToggleWorkspaceComposeBar',
'handleSnippetExecutorChange',
'handleProgrammaticCommandLogRewriteChange',
'handleAddSelectionToAI',
'activeResizers',
// activeWorkspace / focusedSessionId: TerminalLayerWorkspaceSection reads
// these from sidePanelLiveStore so tab switches do not invalidate workspace
// chrome solely because the active tab id changed.
'composeBarThemeColors',
'findSplitNode',
'handleComposeSend',
'handleSnippetFromPanel',
'refocusTerminalSession',
'setIsComposeBarOpen',
'TerminalComposeBar',
'setResizing',
'Array',
'cn',
'onStartSessionRename',
'onRemoveSessionFromWorkspace',
'onReorderTabs',
'onStartSessionDrag',
'onEndSessionDrag',
'onDropWorkspacePane',
'isGlobalBroadcastEnabled',
'isGlobalComposeSendToAll',
'canUseGlobalBroadcast',
'onToggleGlobalBroadcast',
'onToggleGlobalComposeSendTarget',
] as const;
export function terminalLayerSidePanelStableCtxEqual(prev: Ctx, next: Ctx): boolean {
for (const key of SIDE_PANEL_STABLE_CTX_KEYS as unknown as string[]) {
if (!sidePanelCtxKeyEqual(prev, next, key)) return false;
}
return true;
}
export function terminalLayerSidePanelCtxEqual(prev: Ctx, next: Ctx): boolean {
if (!terminalLayerSidePanelStableCtxEqual(prev, next)) return false;
for (const key of SIDE_PANEL_LIVE_CTX_KEYS as unknown as string[]) {
if (!sidePanelCtxKeyEqual(prev, next, key)) return false;
}
return true;
}
export function terminalLayerWorkspaceCtxEqual(prev: Ctx, next: Ctx): boolean {
for (const key of WORKSPACE_CTX_KEYS as unknown as string[]) {
if (!workspaceCtxKeyEqual(prev, next, key)) return false;
}
return true;
}
export function terminalLayerViewCtxEqual(prev: Ctx, next: Ctx): boolean {
if (prev.isTerminalLayerVisible !== next.isTerminalLayerVisible) return false;
if (prev.hibernateHiddenTabs !== next.hibernateHiddenTabs) return false;
if (prev.isComposeBarOpen !== next.isComposeBarOpen) return false;
// activeWorkspace / focusedSessionId intentionally omitted: live store +
// focus-sidebar equal handle those without forcing a full layer rebuild on
// every top-tab switch between terminal sessions.
if (prev.handleComposeSend !== next.handleComposeSend) return false;
if (prev.refocusTerminalSession !== next.refocusTerminalSession) return false;
if (prev.setIsComposeBarOpen !== next.setIsComposeBarOpen) return false;
if (prev.isBroadcastEnabled !== next.isBroadcastEnabled) return false;
if (prev.isComposeSendToAll !== next.isComposeSendToAll) return false;
if (prev.onToggleComposeSendTarget !== next.onToggleComposeSendTarget) return false;
if (prev.isGlobalComposeSendToAll !== next.isGlobalComposeSendToAll) return false;
if (prev.onToggleGlobalComposeSendTarget !== next.onToggleGlobalComposeSendTarget) return false;
if (prev.composeBarThemeColors !== next.composeBarThemeColors) return false;
if (prev.workspaceOuterRef !== next.workspaceOuterRef) return false;
// Use stable side-panel equal only: LIVE fields (cwd, theme focus, etc.) flow
// through sidePanelLiveStore and must not rebuild side-panel chrome.
return terminalLayerSidePanelStableCtxEqual(prev, next)
&& terminalLayerFocusSidebarPropsEqual(prev, next)
&& terminalLayerWorkspaceCtxEqual(prev, next);
}
export function terminalLayerFocusSidebarPropsEqual(prev: Ctx, next: Ctx): boolean {
if (prev.isFocusMode !== next.isFocusMode) return false;
if (!prev.isFocusMode) return true;
const prevWs = prev.activeWorkspace;
const nextWs = next.activeWorkspace;
if (Boolean(prevWs) !== Boolean(nextWs)) return false;
if (prevWs && nextWs) {
if (prevWs.id !== nextWs.id) return false;
if (prevWs.viewMode !== nextWs.viewMode) return false;
if (prevWs.root !== nextWs.root) return false;
if (prevWs.focusSessionOrder !== nextWs.focusSessionOrder) return false;
}
return eq(prev, next, 'focusedSessionId')
&& eq(prev, next, 'resolvedPreviewTheme')
&& eq(prev, next, 'sessionHostsMap')
&& eq(prev, next, 'sessions')
&& prev.terminalSettings?.dynamicTabTitleMode === next.terminalSettings?.dynamicTabTitleMode
&& eq(prev, next, 't')
&& eq(prev, next, 'onReorderWorkspaceSessions')
&& eq(prev, next, 'onRequestAddToWorkspace')
&& eq(prev, next, 'onAppendHostToWorkspace')
&& eq(prev, next, 'handleCloseSession')
&& eq(prev, next, 'onCopySession')
&& eq(prev, next, 'onDuplicateSession')
&& eq(prev, next, 'onCopySessionToNewWindow')
&& eq(prev, next, 'onRemoveSessionFromWorkspace')
&& eq(prev, next, 'onSetWorkspaceFocusedSession')
&& eq(prev, next, 'onSetWorkspaceAnchorChat')
&& eq(prev, next, 'onToggleWorkspaceViewMode')
&& eq(prev, next, 'onSubmitSessionRename');
}