Download src/ipc/loop-ipc.js from VanyaJ/bada-terminal: direct link, hf CLI and curl.
- Browser
- Download file 12 kB
-
https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/ipc/loop-ipc.js
- Command line
-
hf download hf://VanyaJ/bada-terminal/src/ipc/loop-ipc.js
-
curl -L -o loop-ipc.js https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/ipc/loop-ipc.js
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(); | |
| }); | |
| } | |
| }; | |