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
Files changed (1) hide show
  1. index.js +42 -37
index.js CHANGED
@@ -56,46 +56,51 @@ async function loadAndConnectServers() {
56
  }
57
  }
58
 
59
- // Close existing active clients
60
  for (const [name, entry] of activeClients.entries()) {
61
- try {
62
- await entry.client.close();
63
- } catch (e) {}
 
 
 
 
64
  }
65
- activeClients.clear();
66
 
67
- // Connect to registered servers in parallel
68
- const connectionPromises = Object.entries(registry).map(async ([name, serverConfig]) => {
69
- try {
70
- console.log(`[Registry] Connecting to sub-server: ${name}...`);
71
- let transport;
72
- if (serverConfig.type === "sse") {
73
- transport = new SSEClientTransport(new URL(serverConfig.data.url));
74
- } else if (serverConfig.type === "stdio") {
75
- transport = new StdioClientTransport({
76
- command: serverConfig.data.command,
77
- args: serverConfig.data.args || [],
78
- env: { ...process.env, ...serverConfig.data.env }
79
- });
80
- } else {
81
- return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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: "stepfun-ai/step-3.7-flash",
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) }