diff --git a/bridge/mcp-tools.js b/bridge/mcp-tools.js index 5388dc1..cb09b80 100644 --- a/bridge/mcp-tools.js +++ b/bridge/mcp-tools.js @@ -128,8 +128,9 @@ class McpManager { } // Список приложений для UI: только установленные (enabled), со статусом. - list() { - return this.enabled.map(spec => { + // guiUp — порт окна открыт; running — MCP-клиент поднят. Точка в UI горит, если хоть одно. + async list() { + const items = this.enabled.map(spec => { const c = this.clients[spec.name]; const running = !!(c && c.running); const tools = running && c.tools ? c.tools.length : 0; @@ -138,6 +139,7 @@ class McpManager { dir: spec.dir, port: spec.port, hasGui: !!spec.gui, + canLaunch: !!(spec.gui && spec.connectKnown && spec.port != null), gui: spec.gui || null, connectKnown: !!spec.connectKnown, auto: !!spec.auto, @@ -148,18 +150,26 @@ class McpManager { lastError: this.lastError[spec.name] || null, }; }); + await Promise.all(items.map(async (it) => { + if (it.port != null && it.hasGui) { + try { it.guiUp = await checkPort(it.port, 500); } catch { it.guiUp = false; } + } else { + it.guiUp = false; + } + })); + return items; } // Пересканировать папку: подхватить новые приложения без перезапуска моста. - rescan() { + async rescan() { this._rebuild(); const names = this.enabled.map(a => a.name); console.log('[mcp] rescan under ' + this.root + ': ' + (names.length ? names.join(', ') : 'none')); - return this.list(); + return await this.list(); } // Сменить корневую папку ArtCraft и сохранить в bridge/config.json. - setRoot(newRoot) { + async setRoot(newRoot) { if (!newRoot || typeof newRoot !== 'string') throw new Error('root path is required'); const cfgPath = path.join(BRIDGE_DIR, 'config.json'); let disk = {}; @@ -171,17 +181,22 @@ class McpManager { this.config.mcpServers = disk.mcpServers; this._rebuild(); console.log('[mcp] root changed to ' + this.root + ', apps: ' + this.enabled.length); - return { root: this.root, apps: this.list() }; + return { root: this.root, apps: await this.list() }; } // Запустить GUI приложения с --control , дождаться порта, перезапустить CLI для коннекта. async launchGui(name) { const spec = this._spec(name); if (!spec.gui) throw new Error('no GUI binary for ' + name); + if (!spec.connectKnown || spec.port == null) throw new Error('live control is not supported for ' + name); const args = guiLaunchArgs(spec); if (args === null) throw new Error('no launch args for ' + name); if (spec.port != null && await checkPort(spec.port, 800)) { - return { app: name, already: true, port: spec.port, gui: spec.gui }; + // Окно уже открыто. Закрыть возможно устаревший headless-клиент, чтобы следующий вызов переподключился. + const old = this.clients[name]; + if (old) { try { old.close(); } catch {} delete this.clients[name]; } + this.ensure(name).catch(() => {}); + return { app: name, already: true, port: spec.port, gui: spec.gui, ready: true }; } if (this.launching[name]) return this.launching[name]; const p = (async () => { @@ -203,7 +218,8 @@ class McpManager { this.lastError[name] = 'GUI started but port ' + spec.port + ' did not open in 20s'; console.warn('[mcp] ' + name + ' launched but port ' + spec.port + ' not open'); } else { - console.log('[mcp] ' + name + ' GUI is up on port ' + spec.port); + console.log('[mcp] ' + name + ' GUI is up on port ' + spec.port + ', attaching MCP client'); + this.ensure(name).catch(e => console.warn('[mcp] ' + name + ' auto-attach failed: ' + e.message)); } return { app: name, launched: true, port: spec.port, gui: spec.gui, ready }; })(); diff --git a/bridge/server.js b/bridge/server.js index 1fed7a9..fcc224b 100644 --- a/bridge/server.js +++ b/bridge/server.js @@ -144,17 +144,17 @@ async function handle(req, res, pathname, body) { return { ok: true, tools: TOOL_DEFS.concat(mcp.toolDefs()) }; } case '/mcp/apps': { - return { ok: true, root: mcp.root, apps: mcp.list() }; + return { ok: true, root: mcp.root, apps: await mcp.list() }; } case '/mcp/rescan': { if (req.method !== 'POST') throw new ToolError('E_ARGS_INVALID', 'only POST'); - return { ok: true, root: mcp.root, apps: mcp.rescan() }; + return { ok: true, root: mcp.root, apps: await mcp.rescan() }; } case '/mcp/root': { if (req.method !== 'POST') throw new ToolError('E_ARGS_INVALID', 'only POST'); const p = body && body.path; if (!p || typeof p !== 'string') throw new ToolError('E_ARGS_INVALID', 'missing path'); - const r = mcp.setRoot(p); + const r = await mcp.setRoot(p); return { ok: true, root: r.root, apps: r.apps }; } case '/mcp/launch': { diff --git a/content/panel.js b/content/panel.js index 10b362c..4866a1d 100644 --- a/content/panel.js +++ b/content/panel.js @@ -7,7 +7,7 @@ connected: false, loopsLeft: 0, loopsMax: 0, dailyCount: 0, dailyMax: 0, - resetCbs: [], stopCbs: [], saveCbs: [], startCbs: [], dotMode: null, transport: 'dom', accLimit: 900000, lastMetrics: null, forceInject: false + resetCbs: [], stopCbs: [], saveCbs: [], startCbs: [], dotMode: null, transport: 'dom', accLimit: 900000, lastMetrics: null, forceInject: false, mcpTimer: null }; var el = {}; function byId(id) { return document.getElementById(id); } @@ -97,7 +97,13 @@ el.mcpHead.addEventListener('click', function () { var open = el.mcpBox.classList.toggle('open'); el.mcpArrow.textContent = open ? '\u25BC' : '\u25B6'; - if (open) API.mcpRefresh(); + if (open) { + API.mcpRefresh(); + if (state.mcpTimer) clearInterval(state.mcpTimer); + state.mcpTimer = setInterval(function () { API.mcpRefresh(); }, 5000); + } else if (state.mcpTimer) { + clearInterval(state.mcpTimer); state.mcpTimer = null; + } }); el.mcpRescan.addEventListener('click', function () { API.mcpRescan(); }); } @@ -132,15 +138,29 @@ if (!apps.length) { el.mcpList.appendChild(mk('div', 'dshmem-mcp-empty', 'no apps found')); return; } apps.forEach(function (a) { var row = mk('div', 'dshmem-mcp-row'); - var dot = mk('span', 'dshmem-mcp-dot' + (a.starting ? ' busy' : (a.running ? ' on' : ''))); + var dotCls = 'dshmem-mcp-dot'; + if (a.launching || a.starting) dotCls += ' busy'; + else if (a.lastError) dotCls += ' err'; + else if (a.guiUp || a.running) dotCls += ' on'; + var dot = mk('span', dotCls); row.appendChild(dot); var nameEl = mk('span', 'dshmem-mcp-name', a.name); - nameEl.title = a.name + ' port: ' + (a.port == null ? '-' : a.port) + (a.connectKnown ? '' : ' (connect flag unknown, headless)'); + var parts = [a.name]; + if (a.port != null) parts.push('port ' + a.port); + if (a.guiUp) parts.push('window up'); + if (a.running) parts.push('client up, ' + a.tools + ' tools'); + if (!a.connectKnown) parts.push('no live control (headless)'); + if (a.lastError) parts.push('error: ' + a.lastError); + nameEl.title = parts.join(' | '); row.appendChild(nameEl); - row.appendChild(mk('span', 'dshmem-mcp-tools', a.running ? (a.tools + 't') : (a.lastError ? 'err' : ''))); - if (a.hasGui) { - var lb = mk('button', 'dshmem-mcp-btn', a.launching ? '...' : '\u23FB'); - lb.title = 'Launch ' + a.name + ' window (port ' + (a.port == null ? '-' : a.port) + ')'; + var statusTxt = ''; + if (a.running) statusTxt = a.tools + 't'; + else if (a.guiUp) statusTxt = 'win'; + else if (a.lastError) statusTxt = 'err'; + row.appendChild(mk('span', 'dshmem-mcp-tools', statusTxt)); + if (a.canLaunch) { + var lb = mk('button', 'dshmem-mcp-btn', a.launching ? '...' : (a.guiUp ? '\u2713' : '\u23FB')); + lb.title = a.guiUp ? (a.name + ' window is already open on port ' + a.port) : ('Launch ' + a.name + ' window (port ' + a.port + ')'); lb.disabled = a.launching; lb.addEventListener('click', function (ev) { ev.stopPropagation(); API.mcpLaunch(a.name); }); row.appendChild(lb);