feat: add parent agent traversal to getConnectedAgents API#877
Open
Husainbw786 wants to merge 1 commit intotestingfrom
Open
feat: add parent agent traversal to getConnectedAgents API#877Husainbw786 wants to merge 1 commit intotestingfrom
Husainbw786 wants to merge 1 commit intotestingfrom
Conversation
Previously the API only returned child agents (downstream). Now it: - Finds all parent agents by searching for agents that have the given agent in their connected_agents field - Recursively traverses upstream to find all ancestor parents - Continues to traverse downstream to find all child agents - Returns complete bidirectional connection flow with no agents missed
| async function processAgent(agentId, parentIds = [], docType = null) { | ||
| if (visited.has(agentId)) { | ||
| // Find all agents (bridges and versions) that have the given agentId in their connected_agents | ||
| async function findParentAgents(agentId) { |
Contributor
There was a problem hiding this comment.
The findParentAgents function is missing error handling. If the database queries fail, the error will propagate up and potentially crash the API call. Consider adding try/catch blocks:
Comment on lines
+523
to
+528
| const parentBridges = await configurationModel | ||
| .find({ | ||
| org_id, | ||
| $or: [{ "connected_agents": { $exists: true } }] | ||
| }) | ||
| .lean(); |
Contributor
There was a problem hiding this comment.
The query in findParentAgents() currently fetches all documents with a connected_agents field and then filters them in memory. For large organizations with many agents, this could be inefficient.
Consider optimizing with a more targeted query that directly filters for documents containing the target agentId:
Suggested change
| const parentBridges = await configurationModel | |
| .find({ | |
| org_id, | |
| $or: [{ "connected_agents": { $exists: true } }] | |
| }) | |
| .lean(); | |
| // Search in bridges (configurationModel) | |
| const parentBridges = await configurationModel | |
| .find({ | |
| org_id, | |
| connected_agents: { | |
| $exists: true, | |
| $elemMatch: { $or: [{ version_id: agentId }, { bridge_id: agentId }] } | |
| } | |
| }) | |
| .lean(); |
Comment on lines
+543
to
+548
| const parentVersions = await bridgeVersionModel | ||
| .find({ | ||
| org_id, | ||
| $or: [{ "connected_agents": { $exists: true } }] | ||
| }) | ||
| .lean(); |
Contributor
There was a problem hiding this comment.
Similar to the bridge query, this version query could be optimized to directly filter for documents containing the target agentId:
Suggested change
| const parentVersions = await bridgeVersionModel | |
| .find({ | |
| org_id, | |
| $or: [{ "connected_agents": { $exists: true } }] | |
| }) | |
| .lean(); | |
| // Search in versions (bridgeVersionModel) | |
| const parentVersions = await bridgeVersionModel | |
| .find({ | |
| org_id, | |
| connected_agents: { | |
| $exists: true, | |
| $elemMatch: { $or: [{ version_id: agentId }, { bridge_id: agentId }] } | |
| } | |
| }) | |
| .lean(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
The
getConnectedAgentsAPI now returns the complete connection flow including both upstream parents and downstream children.Previous behavior
New behavior
connected_agentsfieldparentAgentsandchildAgentsWhy
The API was incomplete — it only returned child agents. For a true "connected agents" view, you need to see the full graph: who calls this agent (parents) and who this agent calls (children).
Technical details
findParentAgents(agentId)— queries bothconfigurationModelandbridgeVersionModelfor documents that reference the agent in theirconnected_agentsprocessUpstream()— recursively walks up the parent chainprocessDownstream()for clarityagentsMapwith no duplicatesNotes
agentsMapkeyed by agent ID)connected_agentsdocs per level, which should be fine for typical org sizes