wait for terminal ready after running each setup comand

This commit is contained in:
Amrit Subramanian 2025-10-07 12:59:22 -04:00
parent 8b18f47902
commit 2d97d3171a

56
main.ts
View file

@ -22,6 +22,33 @@ function getPersistedSessions(): PersistedSession[] {
return (store as any).get("sessions", []); return (store as any).get("sessions", []);
} }
function isTerminalReady(buffer: string, startPos: number = 0): boolean {
const searchBuffer = buffer.slice(startPos);
const promptSymbols = ["$ ", "% ", "> ", "➜ ", "➜ ", "✗ ", "✓ "];
const endSymbols = ["$", "%", ">", "➜", "✗", "✓"];
// Check for bracketed paste mode
if (searchBuffer.includes("\x1b[?2004h")) {
return true;
}
// Check for prompt symbols
for (const symbol of promptSymbols) {
if (searchBuffer.includes(symbol)) {
return true;
}
}
// Check for end symbols
for (const symbol of endSymbols) {
if (searchBuffer.endsWith(symbol)) {
return true;
}
}
return false;
}
function savePersistedSessions(sessions: PersistedSession[]) { function savePersistedSessions(sessions: PersistedSession[]) {
(store as any).set("sessions", sessions); (store as any).set("sessions", sessions);
} }
@ -220,6 +247,9 @@ function spawnSessionPty(
activePtyProcesses.set(sessionId, ptyProcess); activePtyProcesses.set(sessionId, ptyProcess);
let terminalReady = false; let terminalReady = false;
let readyChecksCompleted = 0;
let lastReadyCheckPos = 0;
let setupCommandsIdx = 0;
let dataBuffer = ""; let dataBuffer = "";
ptyProcess.onData((data) => { ptyProcess.onData((data) => {
@ -232,27 +262,16 @@ function spawnSessionPty(
if (!terminalReady) { if (!terminalReady) {
dataBuffer += data; dataBuffer += data;
// Method 1: Look for bracketed paste mode enable sequence if (isTerminalReady(dataBuffer, lastReadyCheckPos)) {
// Method 2: Fallback - look for common prompt indicators readyChecksCompleted++;
const isReady = dataBuffer.includes("\x1b[?2004h") || lastReadyCheckPos = dataBuffer.length;
dataBuffer.includes("$ ") || dataBuffer.includes("% ") ||
dataBuffer.includes("> ") || dataBuffer.includes("➜ ") ||
dataBuffer.includes("➜ ") || dataBuffer.includes("✗ ") ||
dataBuffer.includes("✓ ") || dataBuffer.endsWith("$") ||
dataBuffer.endsWith("%") || dataBuffer.endsWith(">") ||
dataBuffer.endsWith("➜") || dataBuffer.endsWith("✗") ||
dataBuffer.endsWith("✓");
if (isReady) { if (config.setupCommands && setupCommandsIdx < config.setupCommands.length) {
ptyProcess.write(config.setupCommands[setupCommandsIdx] + "\r");
setupCommandsIdx++;
} else {
terminalReady = true; terminalReady = true;
// Run setup commands if provided
if (config.setupCommands && config.setupCommands.length > 0) {
config.setupCommands.forEach(cmd => {
ptyProcess.write(cmd + "\r");
});
}
// Auto-run the selected coding agent // Auto-run the selected coding agent
if (config.codingAgent === "claude") { if (config.codingAgent === "claude") {
const sessionFlag = isNewSession const sessionFlag = isNewSession
@ -273,6 +292,7 @@ function spawnSessionPty(
} }
} }
} }
}
}); });
return ptyProcess; return ptyProcess;