Files
NetMesh/electron/bridges/terminalAttachRestore.cjs
zhaolei 3c72efcb7f
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
[Init] Initial commit - NetMesh terminal manager
2026-09-13 18:24:01 +08:00

144 lines
4.4 KiB
JavaScript

"use strict";
/**
* Tiny registry so the terminal popup window can restore output routing after
* an AI observe/attach popup is closed or destroyed, without creating a
* windowManager <-> terminalBridge require cycle.
*/
let restoreImpl = null;
let attachHomeLookup = null;
let fanoutExitImpl = null;
const attachPopupAuthorizations = new Map();
const pendingAttachRestores = new Set();
function registerAttachPopupAuthorization(token, sessionId, webContentsId) {
if (!token || !sessionId || typeof webContentsId !== "number") return false;
attachPopupAuthorizations.set(token, {
sessionId,
webContentsId,
closePrepared: false,
});
return true;
}
function validateAttachPopupAuthorization(token, sessionId, webContentsId) {
if (!token || !sessionId || typeof webContentsId !== "number") return false;
const grant = attachPopupAuthorizations.get(token);
return Boolean(
grant
&& grant.sessionId === sessionId
&& grant.webContentsId === webContentsId,
);
}
function markAttachPopupClosePrepared(token, sessionId, webContentsId) {
if (!validateAttachPopupAuthorization(token, sessionId, webContentsId)) return false;
attachPopupAuthorizations.get(token).closePrepared = true;
return true;
}
function isAttachPopupClosePrepared(token) {
return attachPopupAuthorizations.get(token)?.closePrepared === true;
}
function releaseAttachPopupAuthorization(token) {
if (token) attachPopupAuthorizations.delete(token);
}
function releaseAttachedSessionState(sessionId) {
if (!sessionId) return;
pendingAttachRestores.delete(sessionId);
for (const [token, grant] of attachPopupAuthorizations) {
if (grant?.sessionId === sessionId) attachPopupAuthorizations.delete(token);
}
}
function setRestoreAttachedSessionOutput(fn) {
restoreImpl = typeof fn === "function" ? fn : null;
}
async function restoreAttachedSessionOutput(sessionId, preferredHomeWebContentsId = null) {
if (!sessionId || typeof restoreImpl !== "function") {
return { success: false, restored: false };
}
try {
const result = await restoreImpl(sessionId, preferredHomeWebContentsId)
|| { success: true, restored: false };
if (result.success) pendingAttachRestores.delete(sessionId);
else pendingAttachRestores.add(sessionId);
return result;
} catch (err) {
pendingAttachRestores.add(sessionId);
return { success: false, restored: false, error: err?.message || String(err) };
}
}
async function retryPendingAttachedSessionOutputs() {
await Promise.all(Array.from(pendingAttachRestores, (sessionId) => (
restoreAttachedSessionOutput(sessionId)
)));
}
async function retryPendingAttachedSessionOutput(sessionId, preferredHomeWebContentsId = null) {
if (!pendingAttachRestores.has(sessionId)) return { success: true, restored: false };
return await restoreAttachedSessionOutput(sessionId, preferredHomeWebContentsId);
}
function setAttachHomeLookup(fn) {
attachHomeLookup = typeof fn === "function" ? fn : null;
}
function getAttachHomeWebContentsId(sessionId) {
if (!sessionId || typeof attachHomeLookup !== "function") return null;
try {
const id = attachHomeLookup(sessionId);
return typeof id === "number" ? id : null;
} catch {
return null;
}
}
function setFanoutSessionExit(fn) {
fanoutExitImpl = typeof fn === "function" ? fn : null;
}
function fanoutSessionExit(sessionId, primaryContents, payload) {
const primaryWebContentsId = typeof primaryContents === "number"
? primaryContents
: primaryContents?.id;
if (typeof fanoutExitImpl === "function") {
try {
fanoutExitImpl(sessionId, primaryWebContentsId, payload);
return true;
} catch {
// fall through
}
}
// Session implementations can run before terminalBridge wires the shared
// registry. Preserve the original primary-renderer notification then.
try {
primaryContents?.send?.("netcatty:exit", payload);
return typeof primaryContents?.send === "function";
} catch {
return false;
}
}
module.exports = {
registerAttachPopupAuthorization,
validateAttachPopupAuthorization,
markAttachPopupClosePrepared,
isAttachPopupClosePrepared,
releaseAttachPopupAuthorization,
releaseAttachedSessionState,
setRestoreAttachedSessionOutput,
restoreAttachedSessionOutput,
retryPendingAttachedSessionOutputs,
retryPendingAttachedSessionOutput,
setAttachHomeLookup,
getAttachHomeWebContentsId,
setFanoutSessionExit,
fanoutSessionExit,
};