bada-terminal / src /context.js
VanyaJ's picture
Release BADA Terminal v2.1.0 (Enterprise Ontology, NVIDIA auto-ontology, Release Preflight)
d739b1b verified
Raw History Blame Contribute Delete
6.02 kB
const path = require('path');
const os = require('os');
const { EventEmitter } = require('events');
const CLAUDE_DIR = path.join(os.homedir(), '.claude');
module.exports = {
// Window references
mainWindow: null,
editorWindow: null,
codeburnWindow: null,
settingsWindow: null,
toolsViewerWindow: null,
sessionViewerWindow: null,
remoteSettingsWindow: null,
knowledgeGraphWindow: null,
vaultGraphWindow: null,
agentManagerWindow: null,
batchSchedulerWindow: null,
workflowDesignerWindow: null,
workflowMonitorWindow: null,
codeIntelWindow: null,
devTasksWindow: null,
loopBuilderWindow: null,
loopDashboardWindow: null,
loopHistoryWindow: null,
headroomConfigWindow: null,
headroomStatsWindow: null,
suspendedWindow: null,
auditViewerWindow: null,
folderSyncWindow: null,
remoteFileManagerWindow: null,
remoteClaudeViewerWindow: null,
registryAllowlistWindow: null,
apiConnectionsWindow: null,
// PTY references
ptyProcess: null,
codeburnPty: null,
isRestartingPty: false,
isRemoteSession: false,
// Docker Sandbox State
activeDockerSandbox: null, // { sessionId, sandbox, execWrapper }
activeDockerTunnelId: null, // SSH 터널 ID (Docker 소켓 포워딩)
isDockerSandboxSession: false, // 현재 세션이 Docker 샌드박스인지
dockerSandboxWindow: null, // Docker 관리 창 참조
// Directory / State references
currentCwd: process.env.USERPROFILE || process.env.HOME || process.cwd(),
cwdEvents: new EventEmitter(),
// PTY 경로 관측과 캔버스 선택을 같은 이벤트로 전달하되 출처를 유지한다.
setCurrentCwd(cwd, source = 'terminal') {
if (cwd === this.currentCwd) return;
this.currentCwd = cwd;
this.cwdEvents.emit('change', { cwd, source });
},
selectedContextNodes: [], // Cart nodes
activeCli: null,
isQuitting: false,
getActiveCli() {
const allowed = [
'claude', 'opencode', 'codex', 'antigravity', 'agy', 'hermes', 'gemini',
'codeagent', 'aider', 'goose', 'qwen', 'cursor', 'pi', 'mimo', 'mimocode', 'gjc', 'ollama'
];
if (this.activeCli) {
if (allowed.includes(this.activeCli)) {
return this.activeCli === 'agy' ? 'antigravity' : this.activeCli;
}
}
try {
const fs = require('fs');
const settingsPath = path.join(this.CLAUDE_DIR, 'settings.json');
if (fs.existsSync(settingsPath)) {
const raw = fs.readFileSync(settingsPath, 'utf-8');
const parsed = JSON.parse(raw.replace(/^\uFEFF/, ''));
if (parsed && parsed.activeCli) {
const cli = parsed.activeCli === 'agy' ? 'antigravity' : parsed.activeCli;
if (allowed.includes(cli)) {
this.activeCli = cli;
return this.activeCli;
}
}
}
} catch (e) {
console.error('[Context] Failed to read activeCli settings:', e);
}
this.activeCli = 'claude';
return this.activeCli;
},
// Constants
CLAUDE_DIR,
CONFIG_PATH: path.join(CLAUDE_DIR, 'langfuse-config.json'),
SKILLS_DIR: path.join(CLAUDE_DIR, 'skills'),
REMOTE_SERVERS_PATH: path.join(CLAUDE_DIR, 'remote-servers.json'),
RECENT_DIRS_PATH: path.join(CLAUDE_DIR, 'recent-dirs.json'),
KNOWLEDGE_CONFIG_PATH: path.join(CLAUDE_DIR, 'knowledge-config.json'),
// Helper
isHomeDirectory(dir) {
if (!dir) return false;
try {
const targetDir = path.resolve(dir).toLowerCase();
const home = os.homedir() ? path.resolve(os.homedir()).toLowerCase() : '';
const userProfile = process.env.USERPROFILE ? path.resolve(process.env.USERPROFILE).toLowerCase() : '';
const homeEnv = process.env.HOME ? path.resolve(process.env.HOME).toLowerCase() : '';
if (targetDir === home || targetDir === userProfile || targetDir === homeEnv) {
return true;
}
if (process.platform === 'win32') {
if (/^[a-zA-Z]:\\?$/.test(dir) || /^[a-zA-Z]:$/.test(dir)) {
return true;
}
} else {
if (targetDir === '/') {
return true;
}
}
} catch (e) {
console.error(`[isHomeDirectory] Error: ${e.message}`);
}
return false;
},
isSafePath(filePath) {
if (!filePath) return false;
try {
const fs = require('fs');
let resolved = path.resolve(filePath);
// 파일이 실제 존재하면 realpathSync로 심볼릭 링크를 원천 리졸브
if (fs.existsSync(resolved)) {
try {
resolved = fs.realpathSync(resolved);
} catch (e) { /* intentional: 파일 조회 실패 시 무시 */ }
} else {
// 파일이 존재하지 않는 경우(신규 생성 대상), 부모 디렉토리를 거슬러 올라가며 실경로 조회 후 복원
let parent = path.dirname(resolved);
while (parent && parent !== resolved) {
if (fs.existsSync(parent)) {
try {
const realParent = fs.realpathSync(parent);
resolved = path.join(realParent, path.basename(resolved));
break;
} catch (e) { /* intentional: 파일 조회 실패 시 무시 */ }
}
const nextParent = path.dirname(parent);
if (nextParent === parent) break;
parent = nextParent;
}
}
const lower = resolved.toLowerCase();
const sensitivePatterns = [
/[\\/]\.ssh([\\/]|$)/,
/[\\/]\.aws([\\/]|$)/,
/[\\/]\.env([\\/]|$)/,
/[\\/]etc([\\/]|$)/,
/[\\/]proc([\\/]|$)/,
/[\\/]dev([\\/]|$)/,
/[\\/]sys([\\/]|$)/,
/[\\/]windows[\\/]system32/i,
/[\\/]\.claude([\\/]|$)/,
/[\\/]\.gnupg([\\/]|$)/,
/[\\/]\.kube([\\/]|$)/,
/[\\/]\.npmrc([\\/]|$)/,
/[\\/]\.docker([\\/]|$)/,
];
for (const pattern of sensitivePatterns) {
if (pattern.test(lower)) {
return false;
}
}
return true;
} catch (e) {
return false;
}
}
};