Files
Netcatty/electron/bridges/terminalAttachRestore.cjs
T
陈大猫andryan-wong-coder 67f8108713 feat(plugins): add reviewed privileged terminal data pipeline (#2402)
* feat(plugins): add privileged terminal data pipeline

* fix(plugins): harden terminal interceptor lifecycle

* fix(plugins): close terminal interceptor lifecycle gaps

* fix(plugins): preserve terminal interceptor state transitions

* fix(plugin): close terminal pipeline lifecycle gaps

* fix(plugin): harden terminal interceptor startup

* fix(plugin): isolate terminal output route attempts

* fix(plugin): isolate reused terminal sessions

* fix(plugin): harden terminal interceptor boundaries

* fix(plugin): harden terminal replay metadata

* fix(plugin): preserve terminal replay provenance

* fix(plugin): close terminal pipeline lifecycle races

* fix(plugin): harden terminal route handoff

* fix(plugin): contain terminal handoff failures

* fix(plugin): route terminal attachment through RPC

* fix(plugin): validate terminal attachment RPC

* fix(plugin): close unexpected runtime ports

* fix(plugin): canonicalize terminal data frames

* fix(plugin): close terminal pipeline lifecycle gaps

* fix(plugin): close terminal interceptor review gaps

* fix(plugin): harden terminal interceptor protocol handling

* fix(plugin): preserve terminal flow compatibility

* fix(plugin): preserve terminal protocol accounting

* fix(plugin): bound terminal interceptor lifecycle

* fix(plugin): close terminal interceptor review gaps

---------

Co-authored-by: ryan-wong-coder <wangsiyuan19990317@gmail.com>
2026-07-23 09:42:47 +08:00

135 lines
4.1 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 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,
setRestoreAttachedSessionOutput,
restoreAttachedSessionOutput,
retryPendingAttachedSessionOutputs,
retryPendingAttachedSessionOutput,
setAttachHomeLookup,
getAttachHomeWebContentsId,
setFanoutSessionExit,
fanoutSessionExit,
};