Add chain-of-command API for agents

- agentService.getChainOfCommand() walks the reporting tree upward
- GET /agents/:id now returns chainOfCommand array with the full management hierarchy
- GET /agents/me endpoint returns the calling agent with its chain of command

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Forgotten
2026-02-17 20:07:02 -06:00
parent 5306142542
commit 6232d2397d
2 changed files with 31 additions and 1 deletions

View File

@@ -174,6 +174,21 @@ export function agentService(db: Db) {
return build(null);
},
getChainOfCommand: async (agentId: string) => {
const chain: { id: string; name: string; role: string; title: string | null }[] = [];
const visited = new Set<string>([agentId]);
const start = await getById(agentId);
let currentId = start?.reportsTo ?? null;
while (currentId && !visited.has(currentId) && chain.length < 50) {
visited.add(currentId);
const mgr = await getById(currentId);
if (!mgr) break;
chain.push({ id: mgr.id, name: mgr.name, role: mgr.role, title: mgr.title ?? null });
currentId = mgr.reportsTo ?? null;
}
return chain;
},
runningForAgent: (agentId: string) =>
db
.select()