Spaces:
Sleeping
Sleeping
Backup Agent commited on
Commit ·
14767c8
1
Parent(s): 011017e
Optimize connection lifecycle to load incrementally, and fix NVIDIA model in research route
Browse files
index.js
CHANGED
|
@@ -56,46 +56,51 @@ async function loadAndConnectServers() {
|
|
| 56 |
}
|
| 57 |
}
|
| 58 |
|
| 59 |
-
// Close
|
| 60 |
for (const [name, entry] of activeClients.entries()) {
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
-
activeClients.clear();
|
| 66 |
|
| 67 |
-
// Connect to registered servers in parallel
|
| 68 |
-
const connectionPromises = Object.entries(registry)
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
}
|
| 83 |
-
|
| 84 |
-
const client = new Client(
|
| 85 |
-
{ name: `proxy-${name}`, version: "1.0.0" },
|
| 86 |
-
{ capabilities: {} }
|
| 87 |
-
);
|
| 88 |
-
|
| 89 |
-
await client.connect(transport);
|
| 90 |
-
const toolsResult = await client.listTools();
|
| 91 |
-
const tools = toolsResult.tools || [];
|
| 92 |
-
|
| 93 |
-
activeClients.set(name, { client, tools });
|
| 94 |
-
console.log(`[Registry] Successfully registered '${name}' with ${tools.length} tools.`);
|
| 95 |
-
} catch (e) {
|
| 96 |
-
console.error(`[Registry Error] Failed to connect to '${name}':`, e.message);
|
| 97 |
-
}
|
| 98 |
-
});
|
| 99 |
|
| 100 |
await Promise.allSettled(connectionPromises);
|
| 101 |
console.log(`[Registry] Parallel load complete. Active servers: ${activeClients.size}`);
|
|
@@ -293,7 +298,7 @@ app.post("/api/research", express.json(), async (req, res) => {
|
|
| 293 |
"Content-Type": "application/json"
|
| 294 |
},
|
| 295 |
body: JSON.stringify({
|
| 296 |
-
model: "
|
| 297 |
messages: [
|
| 298 |
{ role: "system", content: "You are a research summarizer. Compress the raw text into a strict 500-word markdown brief detailing novel formula structures, chemical symbols, or algorithms." },
|
| 299 |
{ role: "user", content: rawText.substring(0, 30000) }
|
|
|
|
| 56 |
}
|
| 57 |
}
|
| 58 |
|
| 59 |
+
// Close and remove any clients that are no longer in the registry
|
| 60 |
for (const [name, entry] of activeClients.entries()) {
|
| 61 |
+
if (!registry[name]) {
|
| 62 |
+
try {
|
| 63 |
+
await entry.client.close();
|
| 64 |
+
} catch (e) {}
|
| 65 |
+
activeClients.delete(name);
|
| 66 |
+
console.log(`[Registry] Closed and removed offline sub-server: ${name}`);
|
| 67 |
+
}
|
| 68 |
}
|
|
|
|
| 69 |
|
| 70 |
+
// Connect to registered servers in parallel (only those not already connected!)
|
| 71 |
+
const connectionPromises = Object.entries(registry)
|
| 72 |
+
.filter(([name]) => !activeClients.has(name))
|
| 73 |
+
.map(async ([name, serverConfig]) => {
|
| 74 |
+
try {
|
| 75 |
+
console.log(`[Registry] Connecting to sub-server: ${name}...`);
|
| 76 |
+
let transport;
|
| 77 |
+
if (serverConfig.type === "sse") {
|
| 78 |
+
transport = new SSEClientTransport(new URL(serverConfig.data.url));
|
| 79 |
+
} else if (serverConfig.type === "stdio") {
|
| 80 |
+
transport = new StdioClientTransport({
|
| 81 |
+
command: serverConfig.data.command,
|
| 82 |
+
args: serverConfig.data.args || [],
|
| 83 |
+
env: { ...process.env, ...serverConfig.data.env }
|
| 84 |
+
});
|
| 85 |
+
} else {
|
| 86 |
+
return;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
const client = new Client(
|
| 90 |
+
{ name: `proxy-${name}`, version: "1.0.0" },
|
| 91 |
+
{ capabilities: {} }
|
| 92 |
+
);
|
| 93 |
+
|
| 94 |
+
await client.connect(transport);
|
| 95 |
+
const toolsResult = await client.listTools();
|
| 96 |
+
const tools = toolsResult.tools || [];
|
| 97 |
+
|
| 98 |
+
activeClients.set(name, { client, tools });
|
| 99 |
+
console.log(`[Registry] Successfully registered '${name}' with ${tools.length} tools.`);
|
| 100 |
+
} catch (e) {
|
| 101 |
+
console.error(`[Registry Error] Failed to connect to '${name}':`, e.message);
|
| 102 |
}
|
| 103 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
await Promise.allSettled(connectionPromises);
|
| 106 |
console.log(`[Registry] Parallel load complete. Active servers: ${activeClients.size}`);
|
|
|
|
| 298 |
"Content-Type": "application/json"
|
| 299 |
},
|
| 300 |
body: JSON.stringify({
|
| 301 |
+
model: "nvidia/llama-3.1-nemotron-70b-instruct",
|
| 302 |
messages: [
|
| 303 |
{ role: "system", content: "You are a research summarizer. Compress the raw text into a strict 500-word markdown brief detailing novel formula structures, chemical symbols, or algorithms." },
|
| 304 |
{ role: "user", content: rawText.substring(0, 30000) }
|