UNPKG

consortium

Version:

Remote control and session sharing CLI for AI coding agents

142 lines (138 loc) 4.84 kB
'use strict'; var http = require('http'); var persistence = require('./types-B_i6lpTn.cjs'); var index = require('./index-BMIckAk5.cjs'); require('axios'); require('chalk'); require('fs'); require('node:fs'); require('node:os'); require('node:path'); require('node:events'); require('socket.io-client'); require('zod'); require('node:crypto'); require('tweetnacl'); require('child_process'); require('util'); require('fs/promises'); require('crypto'); require('path'); require('url'); require('os'); require('node:child_process'); require('node:fs/promises'); require('node:module'); require('node:util'); require('expo-server-sdk'); require('node:readline'); require('ink'); require('react'); require('node:url'); require('ps-list'); require('cross-spawn'); require('tmp'); require('qrcode-terminal'); require('open'); require('fastify'); require('fastify-type-provider-zod'); require('@modelcontextprotocol/sdk/client/index.js'); require('@modelcontextprotocol/sdk/client/streamableHttp.js'); require('readline'); require('@modelcontextprotocol/sdk/server/mcp.js'); require('node:http'); require('@modelcontextprotocol/sdk/server/streamableHttp.js'); const toolCallTimestamps = /* @__PURE__ */ new Map(); const TOOL_RATE_LIMIT = 30; const TOOL_RATE_WINDOW = 6e4; function checkToolRateLimit(deploymentId, toolName) { const key = `${deploymentId}:${toolName}`; const now = Date.now(); const stamps = toolCallTimestamps.get(key) ?? []; const recent = stamps.filter((t) => now - t < TOOL_RATE_WINDOW); if (recent.length >= TOOL_RATE_LIMIT) return false; recent.push(now); toolCallTimestamps.set(key, recent); return true; } function startPluginDaemonServer(options = {}) { const host = options.host ?? "127.0.0.1"; let resolvedPort = options.port ?? 0; const server = http.createServer(async (req, res) => { if (req.method !== "POST" || req.url !== "/tool-call") { res.writeHead(404, { "Content-Type": "application/json" }); res.end(JSON.stringify({ error: "Not found" })); return; } try { let body; try { body = await readBody(req); } catch (readErr) { if (readErr instanceof Error && readErr.message === "Payload too large") { res.writeHead(413, { "Content-Type": "application/json" }); res.end(JSON.stringify({ error: "Payload too large" })); return; } throw readErr; } const parsed = JSON.parse(body); const authHeader = req.headers["authorization"] ?? ""; const token = authHeader.replace(/^Bearer\s+/, ""); if (!index.validateMcpToken(token, parsed.deploymentId)) { res.writeHead(401, { "Content-Type": "application/json" }); res.end(JSON.stringify({ error: "Invalid or expired token" })); return; } if (!checkToolRateLimit(parsed.deploymentId, parsed.toolName)) { persistence.logger.debug(`[PLUGIN-DAEMON] Rate limit exceeded for ${parsed.deploymentId}:${parsed.toolName}`); res.writeHead(429, { "Content-Type": "application/json" }); res.end(JSON.stringify({ error: `Rate limit exceeded for tool "${parsed.toolName}"` })); return; } persistence.logger.debug(`[PLUGIN-DAEMON] Tool call: ${parsed.deploymentId}:${parsed.toolName}`); res.writeHead(501, { "Content-Type": "application/json" }); res.end(JSON.stringify({ status: "error", error: "Tool execution not yet implemented" })); } catch (err) { const message = err instanceof Error ? err.message : String(err); persistence.logger.debug(`[PLUGIN-DAEMON] Error handling tool call: ${message}`); res.writeHead(400, { "Content-Type": "application/json" }); res.end(JSON.stringify({ error: message })); } }); server.listen(resolvedPort, host, () => { const addr = server.address(); if (addr && typeof addr === "object") { resolvedPort = addr.port; } persistence.logger.debug(`[PLUGIN-DAEMON] Server listening on ${host}:${resolvedPort}`); }); return { get port() { return resolvedPort; }, stop: () => new Promise((resolve, reject) => { server.close((err) => err ? reject(err) : resolve()); }), generateTokenForDeployment: (deploymentId) => index.generateMcpToken(deploymentId) }; } const MAX_BODY_SIZE = 1048576; function readBody(req) { return new Promise((resolve, reject) => { const chunks = []; let totalBytes = 0; req.on("data", (chunk) => { totalBytes += chunk.length; if (totalBytes > MAX_BODY_SIZE) { req.destroy(); reject(new Error("Payload too large")); return; } chunks.push(chunk); }); req.on("end", () => resolve(Buffer.concat(chunks).toString("utf-8"))); req.on("error", reject); }); } exports.startPluginDaemonServer = startPluginDaemonServer;