import { URLExt } from '@jupyterlab/coreutils'; import { ServerConnection } from '@jupyterlab/services'; /** * Ask the xtralab server which of `commands` is currently running inside each * open terminal session. Resolves to a map from session name to the matched * agent command, or `null` for a session sitting at its prompt. * * On any failure (endpoint missing on an older server, network error, * malformed response) the whole result is `null` — callers should treat that * as "detection unavailable" and fall back to the optimistic launch tags * rather than clearing any badges. */ export async function fetchRunningAgents( commands: string[] ): Promise | null> { if (commands.length === 0) { return {}; } const settings = ServerConnection.makeSettings(); const url = URLExt.join(settings.baseUrl, 'xtralab', 'terminals', 'agents'); let response: Response; try { response = await ServerConnection.makeRequest( url, { method: 'POST', body: JSON.stringify({ commands }) }, settings ); } catch (error) { console.warn('xtralab: running-agent detection failed', error); return null; } if (!response.ok) { console.warn( `xtralab: detection endpoint returned HTTP ${response.status}` ); return null; } let payload: unknown; try { payload = await response.json(); } catch (error) { console.warn('xtralab: detection response was not JSON', error); return null; } if (!payload || typeof payload !== 'object') { return null; } const result: Record = {}; for (const [name, command] of Object.entries( payload as Record )) { result[name] = typeof command === 'string' && command ? command : null; } return result; }