MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / buildAgentFilePathMap

Function buildAgentFilePathMap

cli/src/utils/local-agent-registry.ts:108–149  ·  view source on GitHub ↗
(agentsDirs: string[])

Source from the content-addressed store, hash-verified

106 * Later directories in the list take precedence (can override earlier ones).
107 */
108const buildAgentFilePathMap = (agentsDirs: string[]): Map<string, string> => {
109 const idToPath = new Map<string, string>()
110 const idRegex = /id\s*:\s*['"`]([^'"`]+)['"`]/i
111
112 const scanDirectory = (dir: string): void => {
113 try {
114 const entries = fs.readdirSync(dir, { withFileTypes: true })
115 for (const entry of entries) {
116 const fullPath = path.join(dir, entry.name)
117 if (entry.isDirectory() && !entry.name.startsWith('.')) {
118 scanDirectory(fullPath)
119 continue
120 }
121 if (
122 !entry.isFile() ||
123 !entry.name.endsWith('.ts') ||
124 entry.name.endsWith('.d.ts') ||
125 entry.name.endsWith('.test.ts')
126 ) {
127 continue
128 }
129 try {
130 const content = fs.readFileSync(fullPath, 'utf8')
131 const match = content.match(idRegex)
132 if (match?.[1]) {
133 idToPath.set(match[1], fullPath)
134 }
135 } catch {
136 // Skip files that can't be read
137 }
138 }
139 } catch {
140 // Skip directories that can't be read
141 }
142 }
143
144 // Scan all directories - later directories override earlier ones
145 for (const agentsDir of agentsDirs) {
146 scanDirectory(agentsDir)
147 }
148 return idToPath
149}
150
151/**
152 * Get user agents from the cache as LocalAgentInfo[]

Callers 1

initializeAgentRegistryFunction · 0.85

Calls 1

scanDirectoryFunction · 0.85

Tested by

no test coverage detected