midas: host the kit under MIDAS as its web search/fetch provider
MIDAS loads plugins but has no Cordis runtime, so the kit had no way to register its tool or its web providers here. midas/kitd.mjs builds the smallest ctx the plugin asks for and republishes the tool and both providers over a loopback seam; midas/plugin.js is that seam's client and brings up the host on demand, keeping the browser warm across restarts. Headless Linux runs need two additions in index.js: --no-sandbox when running as uid 0, and a chromiumArgs setting for container-specific flags. Co-Authored-By: MIDAS <hello@trymidas.io>
This commit is contained in:
+184
@@ -0,0 +1,184 @@
|
||||
#!/usr/bin/env node
|
||||
/* Edge Kit host for MIDAS.
|
||||
*
|
||||
* index.js is a Cordis plugin: it expects a harness that hands it a `ctx` with
|
||||
* `tools.register`, `web.registerSearchProvider` and `web.registerFetchProvider`.
|
||||
* MIDAS has no Cordis runtime, so this file is the missing host: it builds the
|
||||
* smallest ctx that satisfies the plugin, applies it, and republishes what the
|
||||
* kit registered over a local HTTP seam that the MIDAS plugin (midas/plugin.js)
|
||||
* drives. Keeping the kit in its own process also keeps the browser warm across
|
||||
* MIDAS restarts, which is the whole point of a signed-in browsing profile.
|
||||
*
|
||||
* node midas/kitd.mjs [--port 7789] [--headless true|false] [--engine bing|google]
|
||||
*
|
||||
* Endpoints (127.0.0.1 only):
|
||||
* GET /health { ok, version, browser }
|
||||
* GET /tool-def the tool definition the kit registered
|
||||
* POST /tool { action, ...kit args } -> kit result
|
||||
* POST /search { query, maxResults } -> kit web_search provider
|
||||
* POST /fetch { url } -> kit web_fetch provider
|
||||
* POST /shutdown
|
||||
*/
|
||||
import http from 'node:http';
|
||||
import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
||||
const ROOT = path.resolve(HERE, '..');
|
||||
const arg = (name, dflt) => {
|
||||
const i = process.argv.indexOf('--' + name);
|
||||
return i >= 0 && process.argv[i + 1] !== undefined ? process.argv[i + 1] : dflt;
|
||||
};
|
||||
|
||||
const PORT = Number(arg('port', process.env.EDGE_KIT_HTTP_PORT || 7789));
|
||||
const HOST = '127.0.0.1';
|
||||
|
||||
/* Persistent host-side defaults; the kit re-reads volatile fields per call, so
|
||||
* editing this file and POSTing /reload is enough for most tuning. */
|
||||
const SETTINGS_FILE = process.env.EDGE_KIT_SETTINGS || path.join(HERE, 'kit.settings.json');
|
||||
const DEFAULT_SETTINGS = {
|
||||
browser: 'chrome',
|
||||
headless: true,
|
||||
realProfile: false,
|
||||
port: 9445,
|
||||
searchEngine: arg('engine', 'bing'),
|
||||
seeTextCap: 4000,
|
||||
fetchTextCap: 20000,
|
||||
proxyChain: '',
|
||||
profileDir: '',
|
||||
chromiumArgs: '',
|
||||
};
|
||||
let settings = { ...DEFAULT_SETTINGS };
|
||||
try { settings = { ...settings, ...JSON.parse(await fs.readFile(SETTINGS_FILE, 'utf8')) }; } catch { /* defaults */ }
|
||||
|
||||
const kit = await import(path.join(ROOT, 'index.js'));
|
||||
|
||||
/* ── the ctx the kit asks for ───────────────────────────────────────────── */
|
||||
const cleanups = [];
|
||||
const state = { tool: null, search: null, fetch: null };
|
||||
|
||||
const web = {
|
||||
registerSearchProvider(p) {
|
||||
state.search = p;
|
||||
const off = () => { if (state.search === p) state.search = null; };
|
||||
cleanups.push(off);
|
||||
return off;
|
||||
},
|
||||
registerFetchProvider(p) {
|
||||
state.fetch = p;
|
||||
const off = () => { if (state.fetch === p) state.fetch = null; };
|
||||
cleanups.push(off);
|
||||
return off;
|
||||
},
|
||||
// The kit's own activation probe calls these; route them back through the
|
||||
// providers it just registered so the probe measures this host end to end.
|
||||
search: async (req, signal) => {
|
||||
if (!state.search) throw new Error('no search provider registered');
|
||||
return state.search.search(req, signal);
|
||||
},
|
||||
fetch: async (req, signal) => {
|
||||
if (!state.fetch) throw new Error('no fetch provider registered');
|
||||
return state.fetch.fetch(req, signal);
|
||||
},
|
||||
};
|
||||
|
||||
const ctx = {
|
||||
// Cordis runs an effect body immediately and calls its return value on
|
||||
// teardown; the kit registers its providers inside that effect.
|
||||
effect(fn) {
|
||||
const off = fn();
|
||||
if (typeof off === 'function') cleanups.push(off);
|
||||
return off;
|
||||
},
|
||||
web,
|
||||
tools: {
|
||||
register(def) {
|
||||
state.tool = def;
|
||||
return () => { if (state.tool === def) state.tool = null; };
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
/* cfg is handed to the kit as a plain object: cfgVal() accepts raw values and
|
||||
* volatile `{get}` boxes alike, so getters give us live re-reading for free. */
|
||||
const liveConfig = Object.fromEntries(Object.keys(DEFAULT_SETTINGS).map((k) => [k, { get: () => settings[k] }]));
|
||||
kit.apply(ctx, liveConfig);
|
||||
|
||||
const writeToolDef = async () => {
|
||||
if (!state.tool) return;
|
||||
const def = { name: state.tool.name, description: state.tool.description, parameters: state.tool.parameters };
|
||||
await fs.writeFile(path.join(HERE, 'tool-def.json'), JSON.stringify(def, null, 2));
|
||||
};
|
||||
await writeToolDef();
|
||||
|
||||
/* ── HTTP seam ──────────────────────────────────────────────────────────── */
|
||||
const readBody = (req) => new Promise((resolve, reject) => {
|
||||
let raw = '';
|
||||
req.on('data', (c) => { raw += c; if (raw.length > 4e6) req.destroy(); });
|
||||
req.on('end', () => { try { resolve(raw ? JSON.parse(raw) : {}); } catch (e) { reject(e); } });
|
||||
req.on('error', reject);
|
||||
});
|
||||
|
||||
const json = (res, code, obj) => {
|
||||
const body = JSON.stringify(obj);
|
||||
res.writeHead(code, { 'content-type': 'application/json', 'content-length': Buffer.byteLength(body) });
|
||||
res.end(body);
|
||||
};
|
||||
|
||||
const server = http.createServer(async (req, res) => {
|
||||
const url = new URL(req.url, 'http://' + HOST);
|
||||
try {
|
||||
if (url.pathname === '/health') {
|
||||
let browser = null;
|
||||
try { browser = await state.tool.execute({ action: 'status' }); } catch { /* no session yet */ }
|
||||
return json(res, 200, {
|
||||
ok: true, name: 'edge-kit', version: (await import(path.join(ROOT, 'package.json'), { with: { type: 'json' } })).default.version,
|
||||
providers: { search: !!state.search, fetch: !!state.fetch }, settings, browser,
|
||||
});
|
||||
}
|
||||
if (url.pathname === '/tool-def') return json(res, 200, state.tool ?? {});
|
||||
|
||||
if (req.method === 'POST' && url.pathname === '/tool') {
|
||||
const args = await readBody(req);
|
||||
if (!state.tool) throw new Error('kit did not register a tool');
|
||||
return json(res, 200, { ok: true, result: await state.tool.execute(args) });
|
||||
}
|
||||
if (req.method === 'POST' && url.pathname === '/search') {
|
||||
const b = await readBody(req);
|
||||
const r = await web.search({ query: String(b.query ?? ''), maxResults: b.maxResults ?? 8 });
|
||||
return json(res, 200, { ok: true, result: r });
|
||||
}
|
||||
if (req.method === 'POST' && url.pathname === '/fetch') {
|
||||
const b = await readBody(req);
|
||||
const r = await web.fetch({ url: String(b.url ?? '') });
|
||||
return json(res, 200, { ok: true, result: r });
|
||||
}
|
||||
if (req.method === 'POST' && url.pathname === '/settings') {
|
||||
const b = await readBody(req);
|
||||
settings = { ...settings, ...b };
|
||||
return json(res, 200, { ok: true, settings });
|
||||
}
|
||||
if (req.method === 'POST' && url.pathname === '/shutdown') {
|
||||
json(res, 200, { ok: true });
|
||||
setTimeout(() => shutdown('shutdown requested'), 50);
|
||||
return;
|
||||
}
|
||||
return json(res, 404, { ok: false, error: 'no such endpoint' });
|
||||
} catch (e) {
|
||||
return json(res, 500, { ok: false, error: String((e && e.message) || e) });
|
||||
}
|
||||
});
|
||||
|
||||
async function shutdown(reason) {
|
||||
server.close();
|
||||
// The kit's effect cleanup closes (or detaches from) the browser it owns.
|
||||
while (cleanups.length) { try { await cleanups.pop()(); } catch { /* gone */ } }
|
||||
process.exit(0);
|
||||
}
|
||||
process.on('SIGTERM', () => shutdown('SIGTERM'));
|
||||
process.on('SIGINT', () => shutdown('SIGINT'));
|
||||
|
||||
server.listen(PORT, HOST, () => {
|
||||
process.stdout.write(`edge-kit host listening on http://${HOST}:${PORT} (browser=${settings.browser} headless=${settings.headless} port=${settings.port})\n`);
|
||||
});
|
||||
Reference in New Issue
Block a user