Download src/ipc/codeburn-ipc.js from VanyaJ/bada-terminal: direct link, hf CLI and curl.
- Browser
- Download file 10.9 kB
-
https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/ipc/codeburn-ipc.js
- Command line
-
hf download hf://VanyaJ/bada-terminal/src/ipc/codeburn-ipc.js
-
curl -L -o codeburn-ipc.js https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/ipc/codeburn-ipc.js
10.9 kB
| // CodeBurn ์ค๋น/์ค์ ์ข ๋ฃ ์์ ๊ณ์ฝ: docs/specs/app-lifecycle.md v1.11.0 | |
| const pty = require('node-pty'); | |
| const { dialog } = require('electron'); | |
| const fs = require('fs'); | |
| const os = require('os'); | |
| const terminalCleanup = require('../services/app-terminal-cleanup'); | |
| const { assertHostPathSupported } = require('../security/host-launch'); | |
| const { scrubAgentEnvironment, MODEL_KEYS, RUNTIME_KEYS } = require('../security/agent-environment'); | |
| // 2. CodeBurn | |
| let codeburnPty = null; | |
| let stopping = false, startQueue = Promise.resolve(); | |
| const pendingStarts = new Set(); | |
| module.exports = { | |
| getCodeburnPty() { | |
| return codeburnPty; | |
| }, | |
| setCodeburnPty(val) { | |
| codeburnPty = val; | |
| }, | |
| async stopCodeburn() { | |
| stopping = true; | |
| // ์๋ต์ด ๋ฆ์ ์ค๋น ๊ฒฐ๊ณผ๋ ๋จผ์ ์ธ์ํ ๋ค ๊ฐ์ ์ข ๋ฃ ์ฅ๋ฒฝ์์ ํ์ํ๋ค. | |
| await Promise.allSettled([...pendingStarts]); | |
| await terminalCleanup.closeCodeburnTerminal(module.exports.getCodeburnPty, module.exports.setCodeburnPty); | |
| }, | |
| register(ipcMain, ctx) { | |
| const start = async () => { | |
| assertHostPathSupported('uncontained-task'); | |
| const targetWindow = ctx.codeburnWindow; | |
| const cancelled = () => stopping || ctx.isQuitting || ctx.codeburnWindow !== targetWindow || targetWindow?.isDestroyed(); | |
| if (cancelled()) return; | |
| ipcMain.removeAllListeners('codeburn-input'); | |
| ipcMain.removeAllListeners('codeburn-resize'); | |
| if (codeburnPty) { | |
| try { | |
| await terminalCleanup.closeCodeburnTerminal(module.exports.getCodeburnPty, module.exports.setCodeburnPty); | |
| } catch(error) { console.error('[CodeBurn] ์ด์ ํ๋ก์ธ์ค ์ข ๋ฃ ๋ฏธํ์ธ:', error.code || 'TERMINAL_CLOSE_FAILED'); return; } | |
| } | |
| if (cancelled()) return; | |
| // โโ ๋ถ๊ธฐ A: ์๊ฒฉ/๋ก์ปฌ Docker ์๋๋ฐ์ค๊ฐ ์ฐ๊ฒฐ๋์ด ๊ฐ๋ ์ค์ธ ๊ฒฝ์ฐ (์ปจํ ์ด๋ ๋ด์์ codeburn ์ง์ ์คํ) โโ | |
| if (ctx.activeDockerSandbox && ctx.activeDockerSandbox.execWrapper) { | |
| const execWrapper = ctx.activeDockerSandbox.execWrapper; | |
| try { | |
| assertHostPathSupported('uncontained-task'); | |
| const result = await execWrapper.createInteractiveShell({ | |
| cols: 80, | |
| rows: 24, | |
| shell: 'codeburn' | |
| }); | |
| const stream = result.stream; | |
| codeburnPty = { | |
| write: (data) => stream.write(data), | |
| resize: (cols, rows) => { | |
| if (result.exec && result.exec.resize) { | |
| result.exec.resize(cols, rows).catch(() => {}); | |
| } | |
| }, | |
| destroy: () => stream.end() | |
| }; | |
| const current = codeburnPty; | |
| if (cancelled()) { await terminalCleanup.closeCodeburnTerminal(module.exports.getCodeburnPty, module.exports.setCodeburnPty); return; } | |
| targetWindow?.once('closed', () => { | |
| if (codeburnPty !== current) return; | |
| terminalCleanup.closeTerminal(current).catch(error => console.error('[CodeBurn] ์ปจํ ์ด๋ ํ๋ก์ธ์ค ์ข ๋ฃ ๋ฏธํ์ธ:', error.code || 'TERMINAL_CLOSE_FAILED')); | |
| }); | |
| stream.on('data', (data) => { | |
| if (ctx.codeburnWindow && !ctx.codeburnWindow.isDestroyed()) { | |
| ctx.codeburnWindow.webContents.send('codeburn-output', data.toString()); | |
| } | |
| }); | |
| stream.on('end', () => { | |
| if (codeburnPty !== current) return; | |
| ipcMain.removeAllListeners('codeburn-input'); | |
| ipcMain.removeAllListeners('codeburn-resize'); | |
| // stream.end๋ ์๊ฒฉ exec ์ข ๋ฃ ์ฆ๊ฑฐ๊ฐ ์๋๋ค. ํ์ฌ ์์ ์ฐธ์กฐ๋ฅผ ์ ์งํ๋ค. | |
| if (ctx.codeburnWindow && !ctx.codeburnWindow.isDestroyed()) { | |
| ctx.codeburnWindow.webContents.send('codeburn-output', `\r\n\r\n\x1b[33m[์๊ฒฉ ์ฐ๊ฒฐ์ด ์ข ๋ฃ๋์์ต๋๋ค. ํ๋ก์ธ์ค ์ข ๋ฃ๋ ํ์ธ๋์ง ์์์ต๋๋ค.]\x1b[0m\r\n`); | |
| ipcMain.once('codeburn-input', () => { | |
| if (ctx.codeburnWindow && !ctx.codeburnWindow.isDestroyed()) { | |
| ctx.codeburnWindow.close(); | |
| } | |
| }); | |
| } | |
| }); | |
| ipcMain.on('codeburn-input', (ev, data) => { | |
| if (!cancelled() && codeburnPty === current) { try { if (data !== '\x03') assertHostPathSupported('uncontained-task'); current.write(data); } catch { /* ์ข ๋ฃ ์ค ์ ๋ ฅ ์คํจ๋ ์ ์์ ์ผ๋ก ์ฌ์๋ํ์ง ์๋๋ค. */ } } | |
| }); | |
| ipcMain.on('codeburn-resize', (ev, { cols, rows }) => { | |
| if (!cancelled() && codeburnPty === current) current.resize(cols, rows); | |
| }); | |
| return; | |
| } catch (err) { | |
| if (cancelled() || codeburnPty) { | |
| console.error('[CodeBurn] ์ปจํ ์ด๋ ํ๋ก์ธ์ค ์ข ๋ฃ ๋ฏธํ์ธ:', err.code || 'TERMINAL_CLOSE_FAILED'); | |
| return; | |
| } | |
| console.error('[CodeBurn] Failed to spawn codeburn inside container, falling back to local:', err); | |
| } | |
| } | |
| // โโ ๋ถ๊ธฐ B: ๋ก์ปฌ ์ธ์ ์คํ (๊ธฐ์กด ๋ก์ปฌ node-pty ํธ์ถ) โโ | |
| const isWin = process.platform === 'win32'; | |
| let shell_cmd = isWin ? 'powershell.exe' : (process.env.SHELL || '/bin/bash'); | |
| const args = isWin | |
| ? ['-NoLogo', '-Command', 'codeburn'] | |
| : ['-c', 'export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH"; codeburn']; | |
| if (!isWin && !fs.existsSync(shell_cmd)) { | |
| if (fs.existsSync('/bin/zsh')) { | |
| shell_cmd = '/bin/zsh'; | |
| } else if (fs.existsSync('/bin/bash')) { | |
| shell_cmd = '/bin/bash'; | |
| } else { | |
| shell_cmd = '/bin/sh'; | |
| } | |
| } | |
| const cleanEnv = scrubAgentEnvironment(process.env, { trustedKeys: [...MODEL_KEYS, ...RUNTIME_KEYS] }); | |
| if (ctx.activeDockerSandbox && ctx.activeDockerSandbox.sessionId) { | |
| cleanEnv['CLAUDE_CODE_SESSION_ID'] = ctx.activeDockerSandbox.sessionId; | |
| } | |
| let workDir = process.env.USERPROFILE || process.env.HOME || process.cwd(); | |
| if (!fs.existsSync(workDir)) { | |
| workDir = os.homedir() || os.tmpdir() || '/'; | |
| } | |
| let spawnSuccess = false, fallbackTimer; | |
| try { | |
| assertHostPathSupported('uncontained-task'); | |
| codeburnPty = pty.spawn(shell_cmd, args, { | |
| name: 'xterm-256color', | |
| cols: 80, | |
| rows: 24, | |
| cwd: workDir, | |
| env: cleanEnv | |
| }); | |
| spawnSuccess = true; | |
| } catch (error) { | |
| try { | |
| assertHostPathSupported('uncontained-task'); | |
| codeburnPty = pty.spawn(shell_cmd, [], { | |
| name: 'xterm-256color', | |
| cols: 80, | |
| rows: 24, | |
| cwd: workDir, | |
| env: cleanEnv | |
| }); | |
| spawnSuccess = true; | |
| const fallbackChild = codeburnPty; | |
| fallbackTimer = setTimeout(() => { | |
| if (cancelled() || codeburnPty !== fallbackChild) return; | |
| try { assertHostPathSupported('uncontained-task'); fallbackChild.write('codeburn\r'); } catch(e) { /* ์ข ๋ฃ ์ค์ธ ์์์ ์ฌ์ ๋ ฅํ์ง ์๋๋ค. */ } | |
| }, 1000); | |
| } catch (fallbackError) { | |
| const errorMessage = `CodeBurn ํฐ๋ฏธ๋ ํ๋ก์ธ์ค๋ฅผ ์์ํ์ง ๋ชปํ์ต๋๋ค.\n\n์ค๋ฅ: ${fallbackError.message}`; | |
| dialog.showErrorBox('CodeBurn ์์ ์ค๋ฅ', errorMessage); | |
| } | |
| } | |
| if (spawnSuccess && codeburnPty) { | |
| const current = codeburnPty; | |
| let outputDisposable, exitDisposable, exited = false; | |
| const onWindowClosed = () => { | |
| if (codeburnPty !== current) return; | |
| clearTimeout(fallbackTimer); | |
| terminalCleanup.closeTerminal(current).then(() => { | |
| if (codeburnPty === current) codeburnPty = null; | |
| }, error => console.error('[CodeBurn] ํ๋ก์ธ์ค ์ข ๋ฃ ๋ฏธํ์ธ:', error.code || 'TERMINAL_CLOSE_FAILED')); | |
| if (codeburnPty === current) { | |
| ipcMain.removeAllListeners('codeburn-input'); | |
| ipcMain.removeAllListeners('codeburn-resize'); | |
| } | |
| }; | |
| targetWindow?.once('closed', onWindowClosed); | |
| try { | |
| exitDisposable = current.onExit((e) => { | |
| exited = true; | |
| clearTimeout(fallbackTimer); | |
| targetWindow?.removeListener('closed', onWindowClosed); | |
| // ๊ฐ์ exit ์ด๋ฒคํธ์ ์ฑ ์์ ์ฅ๋ฒฝ์ด ๋จผ์ ๊ด์ธกํ ๋ค ๊ตฌ๋ ์ ํ์ํ๋ค. | |
| Promise.resolve().then(() => { | |
| try { outputDisposable?.dispose(); } catch { /* ์ข ๋ฃ ๋ค ์ถ๋ ฅ ๊ตฌ๋ ์ ๋ฆฌ. */ } | |
| try { exitDisposable?.dispose(); } catch { /* ์ข ๋ฃ ๋ค exit ๊ตฌ๋ ์ ๋ฆฌ. */ } | |
| }); | |
| if (codeburnPty !== current) return; | |
| ipcMain.removeAllListeners('codeburn-input'); | |
| ipcMain.removeAllListeners('codeburn-resize'); | |
| codeburnPty = null; | |
| if (ctx.codeburnWindow && !ctx.codeburnWindow.isDestroyed()) { | |
| ctx.codeburnWindow.webContents.send('codeburn-output', `\r\n\r\n\x1b[33m[ํ๋ก์ธ์ค๊ฐ ์ข ๋ฃ๋์์ต๋๋ค (Exit Code: ${e.exitCode || 0}). ์ฐฝ์ ๋ซ์ผ๋ ค๋ฉด ์๋ฌด ํค๋ ๋๋ฅด์ญ์์ค.]\x1b[0m\r\n`); | |
| ipcMain.once('codeburn-input', () => { | |
| if (ctx.codeburnWindow && !ctx.codeburnWindow.isDestroyed()) { | |
| ctx.codeburnWindow.close(); | |
| } | |
| }); | |
| } | |
| }); | |
| if (exited) return; | |
| outputDisposable = current.onData((data) => { | |
| if (codeburnPty === current && ctx.codeburnWindow && !ctx.codeburnWindow.isDestroyed()) { | |
| ctx.codeburnWindow.webContents.send('codeburn-output', data); | |
| } | |
| }); | |
| ipcMain.on('codeburn-input', (ev, data) => { | |
| if (!cancelled() && codeburnPty === current) { try { if (data !== '\x03') assertHostPathSupported('uncontained-task'); current.write(data); } catch { /* ์ข ๋ฃ ์ค ์ ๋ ฅ ์คํจ๋ ์ ์์ ์ผ๋ก ์ฌ์๋ํ์ง ์๋๋ค. */ } } | |
| }); | |
| ipcMain.on('codeburn-resize', (ev, { cols, rows }) => { | |
| if (!cancelled() && codeburnPty === current) current.resize(cols, rows); | |
| }); | |
| } catch (error) { | |
| clearTimeout(fallbackTimer); | |
| try { await terminalCleanup.closeCodeburnTerminal(module.exports.getCodeburnPty, module.exports.setCodeburnPty); } catch { /* ์ ๋ฆฌ ์คํจ๋ก ์ต์ด ๋ฑ๋ก ์ค๋ฅ๋ฅผ ๋ฎ์ง ์๋๋ค. */ } | |
| throw error; | |
| } | |
| } | |
| }; | |
| ipcMain.on('codeburn-start', () => { | |
| // ๊ฐ์ ์ฐฝ์ ์ฐ์ ์์๋ ์์ ์์์ ์ข ๋ฃ๋ฅผ ์ถ์ํ์ง ์๋๋ค. | |
| const task = startQueue.then(start); startQueue = task.catch(() => {}); pendingStarts.add(task); | |
| task.then(() => pendingStarts.delete(task), error => { pendingStarts.delete(task); console.error('[CodeBurn] ์์ ์คํจ:', error.code || 'CODEBURN_START_FAILED'); }); | |
| return task; | |
| }); | |
| } | |
| }; | |