Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,20 @@ import { MongoClient } from "mongodb";
const MONGODB_URI = "mongodb+srv://admin:Uc0sjm9jpLMsSGn5@cluster0.awdsppv.mongodb.net/AI_Middleware-test";

// A valid bridge_id is a 24-char hex ObjectId string
const isObjectId = (str) => typeof str === "string" && /^[a-f\d]{24}$/i.test(str);

function remapConnectedAgents(connected_agents) {
if (!connected_agents || typeof connected_agents !== "object") return null;
const entries = Object.entries(connected_agents);
if (entries.length === 0) return null;
if (Object.keys(connected_agents).length === 0) return null;

let changed = false;
const remapped = {};

for (const [key, agent_info] of entries) {
for (const agent_info of Object.values(connected_agents)) {
const bridgeId = agent_info?.bridge_id?.toString() ?? agent_info?.bridge_id;

if (isObjectId(key)) {
// Already keyed by bridge_id — keep as-is
remapped[key] = agent_info;
} else if (bridgeId) {
// Keyed by agent name — remap to bridge_id
remapped[bridgeId] = agent_info;
changed = true;
}
// Keyed by agent name — remap to bridge_id
remapped[bridgeId] = agent_info;
changed = true;
Comment on lines +18 to +19
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function now unconditionally sets changed = true for every agent_info processed, which could cause the function to return remapped objects even when no actual changes were made. Consider setting changed = true only when a valid bridgeId exists.

// No bridge_id — drop orphaned entry
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment "No bridge_id — drop orphaned entry" is now misleading since the code no longer implements this behavior. The current implementation will actually add entries with undefined keys rather than dropping orphaned entries.

}
Comment on lines +17 to 21
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code will use undefined as an object key if bridgeId is undefined, which could lead to data loss or unexpected behavior. Consider adding a check to ensure bridgeId exists before using it as a key.

Suggested change
// Keyed by agent name — remap to bridge_id
remapped[bridgeId] = agent_info;
changed = true;
// No bridge_id — drop orphaned entry
}
if (bridgeId) {
remapped[bridgeId] = agent_info;
changed = true;
}
// No bridge_id — drop orphaned entry


Expand Down