import { readFile, readdir } from 'node:fs/promises'; import path from 'node:path'; import type { ACPAgentSourceFile } from './acp-agent'; /** * Discovers source files for an ACP agent source revision. * * Paths in the returned source revision are relative to the current working * directory. Run the manifest from its package root and pass the directory * containing agent source, such as `src`. */ export async function discoverACPAgentSourceFiles(sourceDirectory: string): Promise { const root = process.cwd(); const sourceRoot = path.resolve(root, sourceDirectory); const files: ACPAgentSourceFile[] = []; async function visit(directory: string): Promise { for (const entry of await readdir(directory, { withFileTypes: true })) { const fullPath = path.join(directory, entry.name); if (entry.isDirectory()) { if (entry.name !== 'node_modules' && entry.name !== 'dist') { await visit(fullPath); } continue; } if (!entry.isFile()) continue; files.push({ path: path.relative(root, fullPath).split(path.sep).join('/'), content: await readFile(fullPath, 'utf8'), kind: 'typescript', }); } } await visit(sourceRoot); if (files.length === 0) { throw new Error(`No source files found under ${sourceDirectory}.`); } return files.sort((left, right) => left.path.localeCompare(right.path)); }