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
116 lines
3.6 KiB
TypeScript
116 lines
3.6 KiB
TypeScript
import type { ConnectionLog } from "./models.ts";
|
|
|
|
/** Max unsaved connection logs whose terminal replay data we persist separately. */
|
|
export const MAX_PERSISTED_UNSAVED_TERMINAL_DATA_ENTRIES = 50;
|
|
|
|
export type ConnectionLogTerminalDataMap = Record<string, string>;
|
|
|
|
export const readTerminalDataFromLog = (log: ConnectionLog): string | undefined =>
|
|
log.terminalData;
|
|
|
|
export const mergeTerminalDataIntoLogs = (
|
|
logs: ConnectionLog[],
|
|
terminalDataMap: ConnectionLogTerminalDataMap,
|
|
): ConnectionLog[] => {
|
|
if (logs.length === 0) return logs;
|
|
|
|
let changed = false;
|
|
const next = logs.map((log) => {
|
|
if (log.terminalData) return log;
|
|
const sideData = terminalDataMap[log.id];
|
|
if (!sideData) return log;
|
|
changed = true;
|
|
return { ...log, terminalData: sideData };
|
|
});
|
|
return changed ? next : logs;
|
|
};
|
|
|
|
/**
|
|
* When another window or a pruned localStorage write reloads connection logs,
|
|
* keep in-memory / side-store terminal replay data instead of wiping it.
|
|
*/
|
|
export const mergeConnectionLogsFromStorage = (
|
|
prev: ConnectionLog[],
|
|
next: ConnectionLog[],
|
|
terminalDataMap: ConnectionLogTerminalDataMap,
|
|
): ConnectionLog[] => {
|
|
if (next.length === 0) return next;
|
|
|
|
const prevById = new Map(prev.map((log) => [log.id, log]));
|
|
let changed = false;
|
|
const merged = next.map((log) => {
|
|
const memoryData = readTerminalDataFromLog(prevById.get(log.id) ?? log);
|
|
const sideData = terminalDataMap[log.id];
|
|
const terminalData = memoryData ?? readTerminalDataFromLog(log) ?? sideData;
|
|
if (!terminalData || log.terminalData === terminalData) return log;
|
|
changed = true;
|
|
return { ...log, terminalData };
|
|
});
|
|
return changed ? merged : next;
|
|
};
|
|
|
|
export const buildTerminalDataMapFromLogs = (
|
|
logs: ConnectionLog[],
|
|
): ConnectionLogTerminalDataMap => {
|
|
const map: ConnectionLogTerminalDataMap = {};
|
|
for (const log of logs) {
|
|
const data = readTerminalDataFromLog(log);
|
|
if (data) map[log.id] = data;
|
|
}
|
|
return map;
|
|
};
|
|
|
|
/**
|
|
* Keep only unsaved logs' terminal data, capped to the most recent entries.
|
|
* Saved logs keep terminalData in the main connection log blob when bookmarked.
|
|
*/
|
|
export const pruneTerminalDataMapForStorage = (
|
|
logs: ConnectionLog[],
|
|
map: ConnectionLogTerminalDataMap,
|
|
): ConnectionLogTerminalDataMap => {
|
|
const unsavedIds = logs
|
|
.filter((log) => !log.saved)
|
|
.sort((a, b) => b.startTime - a.startTime)
|
|
.slice(0, MAX_PERSISTED_UNSAVED_TERMINAL_DATA_ENTRIES)
|
|
.map((log) => log.id);
|
|
|
|
const allowed = new Set(unsavedIds);
|
|
for (const log of logs) {
|
|
if (log.saved && map[log.id]) {
|
|
allowed.add(log.id);
|
|
}
|
|
}
|
|
|
|
const next: ConnectionLogTerminalDataMap = {};
|
|
for (const id of allowed) {
|
|
if (map[id]) next[id] = map[id];
|
|
}
|
|
return next;
|
|
};
|
|
|
|
/** Fold side-store maps together, then cap to the allowed unsaved/saved set. */
|
|
export const mergeTerminalDataMapsForStorage = (
|
|
logs: ConnectionLog[],
|
|
persistedSnapshot: ConnectionLogTerminalDataMap,
|
|
localMaps: ConnectionLogTerminalDataMap[],
|
|
persistedLogIds: ReadonlySet<string>,
|
|
): ConnectionLogTerminalDataMap => {
|
|
const combined: ConnectionLogTerminalDataMap = { ...persistedSnapshot };
|
|
for (const map of localMaps) {
|
|
for (const [id, data] of Object.entries(map)) {
|
|
if (data) combined[id] = data;
|
|
}
|
|
}
|
|
const pruned = pruneTerminalDataMapForStorage(logs, combined);
|
|
|
|
// Retain replay buffers only for log ids still present in the persisted
|
|
// connection-log blob but not yet loaded into this window's React state.
|
|
for (const id of persistedLogIds) {
|
|
if (!logs.some((log) => log.id === id) && combined[id]) {
|
|
pruned[id] = combined[id];
|
|
}
|
|
}
|
|
|
|
return pruned;
|
|
};
|