UNPKG

repl-mcp

Version:

Universal REPL session manager MCP server

81 lines 3.32 kB
export class ITermMarkerDetector { static MARKERS = { // iTerm2 Shell Integration markers SHELL_INTEGRATION: /\x1b\]1337;ShellIntegrationVersion=(\d+)(?:;shell=([a-z]+))?\x07/, PROMPT_START: /\x1b\]133;A\x07/, COMMAND_START: /\x1b\]133;B\x07/, EXECUTION_START: /\x1b\]133;C\x07/, COMMAND_END: /\x1b\]133;D;(\d+)\x07/, CURRENT_DIR: /\x1b\]1337;CurrentDir=([^\x07]+)\x07/, REMOTE_HOST: /\x1b\]1337;RemoteHost=([^\x07]+)\x07/, USER_VAR: /\x1b\]1337;SetUserVar=([^=]+)=([^\x07]+)\x07/, PROMPT_PREFIX: /\x1b\]133;D;(\d+)\x07/, }; static detectMarkers(rawOutput) { const markers = { hasIntegration: false, promptStart: false, commandEnd: false, exitCode: null, currentDir: null }; // Check for shell integration if (this.MARKERS.SHELL_INTEGRATION.test(rawOutput)) { markers.hasIntegration = true; } // Check for prompt start if (this.MARKERS.PROMPT_START.test(rawOutput)) { markers.promptStart = true; } // Check for command completion with exit code const commandEndMatch = rawOutput.match(this.MARKERS.COMMAND_END); if (commandEndMatch) { markers.commandEnd = true; markers.exitCode = parseInt(commandEndMatch[1]); } // Check for current directory const currentDirMatch = rawOutput.match(this.MARKERS.CURRENT_DIR); if (currentDirMatch) { markers.currentDir = decodeURIComponent(currentDirMatch[1]); } return markers; } static hasCommandCompletion(rawOutput) { return this.MARKERS.COMMAND_END.test(rawOutput); } static getExitCode(rawOutput) { const match = rawOutput.match(this.MARKERS.COMMAND_END); return match ? parseInt(match[1]) : null; } static logDetectedMarkers(rawOutput, sessionId, logger) { if (!logger) return; const markers = this.detectMarkers(rawOutput); if (markers.hasIntegration) { logger(`[DEBUG ${sessionId}] iTerm2 shell integration detected`, sessionId); } if (markers.promptStart) { logger(`[DEBUG ${sessionId}] iTerm2 prompt start marker detected`, sessionId); } if (markers.commandEnd) { logger(`[DEBUG ${sessionId}] iTerm2 command completion detected (exit code: ${markers.exitCode})`, sessionId); } if (markers.currentDir) { logger(`[DEBUG ${sessionId}] iTerm2 current directory: ${markers.currentDir}`, sessionId); } // Check for additional markers if (this.MARKERS.REMOTE_HOST.test(rawOutput)) { const match = rawOutput.match(this.MARKERS.REMOTE_HOST); if (match) { logger(`[DEBUG ${sessionId}] iTerm2 remote host detected: ${match[1]}`, sessionId); } } if (this.MARKERS.USER_VAR.test(rawOutput)) { const match = rawOutput.match(this.MARKERS.USER_VAR); if (match) { logger(`[DEBUG ${sessionId}] iTerm2 user variable: ${match[1]}=${match[2]}`, sessionId); } } } } //# sourceMappingURL=iterm-marker-detector.js.map