-
Notifications
You must be signed in to change notification settings - Fork 9
refactor: simplify connected_agents remapping logic in migration script #863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: testing
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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; | ||||||||||||||||||||||
| // No bridge_id — drop orphaned entry | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+17
to
21
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code will use
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
There was a problem hiding this comment.
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 = truefor every agent_info processed, which could cause the function to return remapped objects even when no actual changes were made. Consider settingchanged = trueonly when a valid bridgeId exists.