bada-terminal / src /ipc /loop-ipc.js
VanyaJ's picture
Release BADA Terminal v2.1.0 (Enterprise Ontology, NVIDIA auto-ontology, Release Preflight)
d739b1b verified
Raw History Blame Contribute Delete
12 kB
module.exports = {
register(ipcMain, ctx) {
const windowManager = require('../window-manager');
// --- Loop Engineering IPC Handlers ---
ipcMain.handle('get-loop-goals', async (event, projectId) => {
try {
const db = require('../../knowledge-db');
return { success: true, goals: db.getLoopGoals(projectId) };
} catch (e) {
return { success: false, error: e.message };
}
});
ipcMain.handle('save-loop-goal', async (event, projectId, goal) => {
try {
const db = require('../../knowledge-db');
db.saveLoopGoal(projectId, goal);
return { success: true };
} catch (e) {
return { success: false, error: e.message };
}
});
// ๋ฐฐํ„ฐ๋ฆฌ ์ •์‹ ์ž์‚ฐ(HTTP ๋…ธ๋“œ ์›Œํฌํ”Œ๋กœ์šฐ + ๋ฃจํ”„ ๊ณจ) ์‹œ๋“œ ํ•ธ๋“ค๋Ÿฌ.
// TR-015์˜ loop-builder.js ๊ฐ•์ œ ์ฃผ์ž…์„ ๋Œ€์ฒดํ•˜๋Š” ํด๋ฆฐ ๊ฒฝ๋กœ โ€” Electron(SQLite) ์ปจํ…์ŠคํŠธ์—์„œ 1ํšŒ ํ˜ธ์ถœํ•˜๋ฉด ๋“ฑ๋ก๋œ๋‹ค.
ipcMain.handle('seed-battery-assets', async (event, projectId) => {
try {
const db = require('../../knowledge-db');
const workflow = require('../config/battery-redesign-workflow.json');
const goal = require('../config/battery-redesign-goal.json');
const pid = projectId || 'global';
db.saveWorkflow(pid, workflow);
db.saveLoopGoal(pid, goal);
return { success: true, projectId: pid, workflowId: workflow.id, goalId: goal.id };
} catch (e) {
return { success: false, error: e.message };
}
});
ipcMain.handle('delete-loop-goal', async (event, goalId) => {
try {
const db = require('../../knowledge-db');
db.deleteLoopGoal(goalId);
return { success: true };
} catch (e) {
return { success: false, error: e.message };
}
});
// โ”€โ”€โ”€ Loop Templates: ํ”„๋กœ์ ํŠธ ๋น„์ข…์† ์žฌ์‚ฌ์šฉ ์นดํƒˆ๋กœ๊ทธ โ”€โ”€โ”€
// WHY: ์ด ํ”„๋กœ์ ํŠธ์—์„œ ๊ฒ€์ฆ๋œ ๋ฃจํ”„ ์„ค์ •์„ project_id/task_id ์—†์ด ๋ณต์ œ ์ €์žฅํ•ด
// ๋‹ค๋ฅธ ํ”„๋กœ์ ํŠธ์—์„œ๋„ ์žฌ์‚ฌ์šฉ(์ธ์Šคํ„ด์Šคํ™”)ํ•œ๋‹ค. ๊ธฐ์กด loop_goals/batch_tasks ๋กœ์ง์€
// ์ „ํ˜€ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๊ณ  ์ˆœ์ˆ˜ ์‹ ๊ทœ ๊ฒฝ๋กœ๋งŒ ์ถ”๊ฐ€ํ•œ๋‹ค.
// ํ”„๋กœ์ ํŠธ ์ ˆ๋Œ€๊ฒฝ๋กœ ํ•˜๋“œ์ฝ”๋”ฉ ํœด๋ฆฌ์Šคํ‹ฑ โ€” ๊ฒฝ๊ณ ๋งŒ ๋ฐ˜ํ™˜(์น˜ํ™˜/์‚ด๊ท ์€ ํ•˜์ง€ ์•Š์Œ).
// WHY: ํ”„๋กฌํ”„ํŠธ์— ํ˜„์žฌ ํ”„๋กœ์ ํŠธ์˜ ์ ˆ๋Œ€๊ฒฝ๋กœ/ํด๋”๋ช…์ด ๊ทธ๋Œ€๋กœ ๋ฐ•ํ˜€ ์žˆ์œผ๋ฉด ๋‹ค๋ฅธ ํ”„๋กœ์ ํŠธ์—์„œ
// ์žฌ์‚ฌ์šฉ ์‹œ ๊ฒฝ๋กœ๊ฐ€ ์–ด๊ธ‹๋‚œ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์ธ์ง€ํ•˜๋„๋ก ๊ฒฝ๊ณ  ๋ฌธ์ž์—ด๋งŒ ๋งŒ๋“ ๋‹ค.
const detectHardcodedPaths = (projectId, goalExpression, stepsJson) => {
const warnings = [];
try {
const path = require('path');
const haystack = [
typeof goalExpression === 'string' ? goalExpression : JSON.stringify(goalExpression || {}),
typeof stepsJson === 'string' ? stepsJson : JSON.stringify(stepsJson || [])
].join('\n');
const pid = String(projectId || '');
const looksAbsolute = pid.startsWith('/') || /^[a-zA-Z]:[\\/]/.test(pid);
if (looksAbsolute && pid.length > 3 && haystack.includes(pid)) {
warnings.push(`์—์ด์ „ํŠธ ํ”„๋กฌํ”„ํŠธ/๋ชฉํ‘œ์‹์— ํ˜„์žฌ ํ”„๋กœ์ ํŠธ ์ ˆ๋Œ€๊ฒฝ๋กœ("${pid}")๊ฐ€ ๊ทธ๋Œ€๋กœ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํ”„๋กœ์ ํŠธ์—์„œ ์ด ํ…œํ”Œ๋ฆฟ์„ ์ ์šฉํ•˜๋ฉด ๊ฒฝ๋กœ๊ฐ€ ๋งž์ง€ ์•Š์„ ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ ์šฉ ํ›„ ํ”„๋กฌํ”„ํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.`);
} else {
const base = path.basename(pid);
if (base && base.length >= 3 && haystack.includes(base)) {
warnings.push(`์—์ด์ „ํŠธ ํ”„๋กฌํ”„ํŠธ/๋ชฉํ‘œ์‹์— ํ˜„์žฌ ํ”„๋กœ์ ํŠธ ํด๋”๋ช…("${base}")์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํ”„๋กœ์ ํŠธ์—์„œ ์žฌ์‚ฌ์šฉ ์‹œ ๊ฒฝ๋กœ ํ™•์ธ์ด ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.`);
}
}
} catch (e) {
// ํœด๋ฆฌ์Šคํ‹ฑ ์ž์ฒด์˜ ์˜ค๋ฅ˜๊ฐ€ ์ €์žฅ์„ ๋ง‰์•„์„œ๋Š” ์•ˆ ๋œ๋‹ค(๊ฒฝ๊ณ  ์ƒ๋žต).
console.error('[loop-ipc] detectHardcodedPaths ์˜ค๋ฅ˜(๋ฌด์‹œ):', e.message);
}
return warnings;
};
ipcMain.handle('get-loop-templates', async () => {
try {
const db = require('../../knowledge-db');
return { success: true, templates: db.getLoopTemplates() };
} catch (e) {
return { success: false, error: e.message };
}
});
// ๊ธฐ์กด loop_goal ํ•˜๋‚˜(+๋Œ€์‘ batch_task ์Šคํ…)๋ฅผ ํ”„๋กœ์ ํŠธ ๋น„์ข…์† ํ…œํ”Œ๋ฆฟ์œผ๋กœ ๋ณต์ œ ์ €์žฅ.
ipcMain.handle('save-loop-template', async (event, projectId, goalId, options = {}) => {
try {
const db = require('../../knowledge-db');
const path = require('path');
const goals = db.getLoopGoals(projectId) || [];
const goal = goals.find(g => g.id === goalId);
if (!goal) {
return { success: false, error: `๋ฃจํ”„ ๋ชฉํ‘œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค: ${goalId}` };
}
// ๋Œ€์‘ํ•˜๋Š” batch_task์˜ ์—์ด์ „ํŠธ ์Šคํ…(steps_json)์„ ํ•จ๊ป˜ ๋ณต์ œํ•œ๋‹ค.
const tasks = db.getBatchTasks(projectId) || [];
const task = goal.task_id ? tasks.find(t => t.id === goal.task_id) : null;
const stepsJson = task ? (task.steps_json || '[]') : '[]';
const warnings = detectHardcodedPaths(projectId, goal.goal_expression, stepsJson);
// WHY: ๋™์‹œ์— ์—ฌ๋Ÿฌ ๋ฒˆ ํ˜ธ์ถœ๋˜๋ฉด Date.now()๋งŒ์œผ๋กœ๋Š” ๋ฐ€๋ฆฌ์ดˆ ๋‹จ์œ„ ์ถฉ๋Œ(๋ฎ์–ด์“ฐ๊ธฐ) ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์–ด
// ์ฝ”๋“œ๋ฒ ์ด์Šค ๊ด€๋ก€(์˜ˆ: sqlite-db.js์˜ Date.now()+random ์ ‘๋ฏธ์‚ฌ ํŒจํ„ด)๋ฅผ ๋”ฐ๋ผ ๋žœ๋ค ์ ‘๋ฏธ์‚ฌ๋ฅผ ๋”ํ•œ๋‹ค.
const templateId = `looptmpl-${Date.now()}-${Math.random().toString(36).substring(2, 7)}`;
const template = {
id: templateId,
name: (options.name || goal.name || 'Untitled Loop Template'),
description: options.description || null,
// ํ”„๋กœ์ ํŠธ ์ข…์† ํ•„๋“œ(project_id/task_id/workflow_id)๋Š” ์˜๋„์ ์œผ๋กœ ์ œ์™ธํ•œ๋‹ค.
goal_expression: goal.goal_expression,
max_iterations: goal.max_iterations,
token_budget: goal.token_budget,
max_wall_time: goal.max_wall_time,
consecutive_failure_threshold: goal.consecutive_failure_threshold,
worktree_isolation: goal.worktree_isolation,
steps_json: stepsJson,
source_project_name: path.basename(String(projectId || '')) || String(projectId || '') || null,
source_task_name: task ? task.name : null
};
db.saveLoopTemplate(template);
return { success: true, templateId, warnings };
} catch (e) {
return { success: false, error: e.message };
}
});
// ํ…œํ”Œ๋ฆฟ์„ ํ˜„์žฌ ํ”„๋กœ์ ํŠธ์— ์ธ์Šคํ„ด์Šคํ™” โ€” ์ƒˆ loop_goal + batch_task ํ–‰ ์ƒ์„ฑ.
ipcMain.handle('apply-loop-template', async (event, projectId, templateId, options = {}) => {
try {
const db = require('../../knowledge-db');
const templates = db.getLoopTemplates() || [];
const tmpl = templates.find(t => t.id === templateId);
if (!tmpl) {
return { success: false, error: `ํ…œํ”Œ๋ฆฟ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค: ${templateId}` };
}
// WHY: ์งง์€ ์‹œ๊ฐ„ ๋‚ด ์—ฐ์† ์ ์šฉ(๋”๋ธ”ํด๋ฆญ ๋“ฑ) ์‹œ Date.now() ๋‹จ๋…์œผ๋กœ๋Š” ์ถฉ๋Œ ๊ฐ€๋Šฅ โ€” ๋žœ๋ค ์ ‘๋ฏธ์‚ฌ๋กœ ๋ณด๊ฐ•.
const stamp = `${Date.now()}-${Math.random().toString(36).substring(2, 7)}`;
const newGoalId = `goal-${stamp}`;
const newTaskId = `task-loop-${stamp}`;
// goal_expression์—์„œ goals ๋ฐฐ์—ด์„ ์ถ”์ถœํ•ด loop_config๋ฅผ ์žฌ๊ตฌ์„ฑํ•œ๋‹ค(buildTaskPayload์™€ ๋™์ผ ์‹œ๋งจํ‹ฑ).
let goalExpr = {};
try {
goalExpr = typeof tmpl.goal_expression === 'string' ? JSON.parse(tmpl.goal_expression) : (tmpl.goal_expression || {});
} catch (_) { goalExpr = {}; }
const goals = Array.isArray(goalExpr) ? goalExpr : (goalExpr.goals || []);
// 1) ์ƒˆ loop_goal (๋Œ€์ƒ ํ”„๋กœ์ ํŠธ์— ์ข…์†)
const goal = {
id: newGoalId,
task_id: newTaskId,
workflow_id: null,
name: options.name || tmpl.name,
goal_expression: tmpl.goal_expression,
max_iterations: tmpl.max_iterations,
token_budget: tmpl.token_budget,
max_wall_time: tmpl.max_wall_time,
consecutive_failure_threshold: tmpl.consecutive_failure_threshold,
worktree_isolation: tmpl.worktree_isolation
};
db.saveLoopGoal(projectId, goal);
// 2) ์ƒˆ batch_task (์—์ด์ „ํŠธ ์Šคํ… ๋ณต์ œ + loop_config ์žฌ๊ตฌ์„ฑ, ๋ฏธ๊ฒ€์ฆ ๋“œ๋ž˜ํ”„ํŠธ๋กœ ์‹œ์ž‘)
let steps = [];
try { steps = JSON.parse(tmpl.steps_json || '[]'); } catch (_) { steps = []; }
const firstStepCfg = (steps[0] && steps[0].config) || {};
const loopConfig = {
goal_id: newGoalId,
goals,
max_iterations: tmpl.max_iterations,
token_budget: tmpl.token_budget,
max_wall_time: tmpl.max_wall_time,
consecutive_failure_threshold: tmpl.consecutive_failure_threshold,
worktree_isolation: tmpl.worktree_isolation
};
const task = {
id: newTaskId,
name: `๐Ÿ”„ [Loop] ${goal.name}`,
prompt: firstStepCfg.prompt || '',
cron_expr: 'once:0 3 * * *',
max_turns: firstStepCfg.max_turns || 30,
is_active: 0,
steps_json: tmpl.steps_json || '[]',
loop_config: JSON.stringify(loopConfig),
is_tested_success: 0
};
db.saveBatchTask(projectId, task);
return { success: true, goalId: newGoalId, taskId: newTaskId };
} catch (e) {
return { success: false, error: e.message };
}
});
ipcMain.handle('delete-loop-template', async (event, templateId) => {
try {
const db = require('../../knowledge-db');
db.deleteLoopTemplate(templateId);
return { success: true };
} catch (e) {
return { success: false, error: e.message };
}
});
ipcMain.handle('get-loop-runs', async (event, goalId) => {
try {
const db = require('../../knowledge-db');
return { success: true, runs: db.getLoopRuns(goalId) };
} catch (e) {
return { success: false, error: e.message };
}
});
ipcMain.handle('get-loop-iterations', async (event, loopRunId) => {
try {
const db = require('../../knowledge-db');
return { success: true, iterations: db.getLoopIterations(loopRunId) };
} catch (e) {
return { success: false, error: e.message };
}
});
ipcMain.handle('get-loop-messages', async (event, loopRunId) => {
try {
const db = require('../../knowledge-db');
return { success: true, messages: db.getLoopMessages(loopRunId) };
} catch (e) {
return { success: false, error: e.message };
}
});
ipcMain.handle('save-loop-message', async (event, msg) => {
try {
const db = require('../../knowledge-db');
db.saveLoopMessage(msg);
return { success: true };
} catch (e) {
return { success: false, error: e.message };
}
});
// --- Loop Engineering ์ฐฝ ์—ด๊ธฐ IPC ๋ฆฌ์Šค๋„ˆ ---
// ๋ฃจํ”„ ๋นŒ๋” ํ™”๋ฉด ์—ด๊ธฐ ์ˆ˜์‹ 
ipcMain.on('open-loop-builder', () => {
windowManager.openLoopBuilder();
});
// ๋ฃจํ”„ ์‹ค์‹œ๊ฐ„ ๋ชจ๋‹ˆํ„ฐ๋ง ๋Œ€์‹œ๋ณด๋“œ ํ™”๋ฉด ์—ด๊ธฐ ์ˆ˜์‹  (์‹คํ–‰ ID, ํƒœ์Šคํฌ ID, ๋ชฉํ‘œ ID ์ „๋‹ฌ)
ipcMain.on('open-loop-dashboard', (event, data = {}) => {
windowManager.openLoopDashboard(data.runId, data.taskId, data.goalId);
});
// ๋ฃจํ”„ ์‹ค์‹œ๊ฐ„ HUD ์Šค๋ƒ…์ƒท ์กฐํšŒ (Electron ๋ฐ์Šคํฌํƒ‘ ๋ฐ Web Server IpcBridge ๊ณตํ†ต ์ง€์›)
ipcMain.handle('loop:hud-snapshot', () => require('../services/loop-hud').snapshot());
// ๋ฃจํ”„ ์‹คํ–‰ ์ด๋ ฅ ํ™”๋ฉด ์—ด๊ธฐ ์ˆ˜์‹ 
ipcMain.on('open-loop-history', () => {
windowManager.openLoopHistory();
});
}
};