(
rootDir: string,
options: TreeOptions = {},
)
| 100 | } |
| 101 | |
| 102 | export async function buildDirectoryTree( |
| 103 | rootDir: string, |
| 104 | options: TreeOptions = {}, |
| 105 | ): Promise<string> { |
| 106 | const ctx: TreeContext = { |
| 107 | maxDepth: options.maxDepth ?? 3, |
| 108 | maxEntries: options.maxEntries ?? 200, |
| 109 | maxBytes: options.maxBytes ?? 4000, |
| 110 | relevantFolders: inferRelevantFolders(options.userPromptHint), |
| 111 | hasWeighting: false, |
| 112 | entryCount: 0, |
| 113 | byteCount: 0, |
| 114 | truncated: false, |
| 115 | lines: [path.basename(rootDir) + '/'], |
| 116 | }; |
| 117 | ctx.hasWeighting = ctx.relevantFolders.size > 0; |
| 118 | ctx.byteCount = ctx.lines[0]!.length; |
| 119 | |
| 120 | await walk(rootDir, 1, '', ctx); |
| 121 | |
| 122 | if (ctx.hasWeighting) { |
| 123 | ctx.lines.push(''); |
| 124 | ctx.lines.push(`(tree weighted for query: kept ${[...ctx.relevantFolders].slice(0, 6).join('/')}; other folders summarised)`); |
| 125 | } |
| 126 | |
| 127 | return ctx.lines.join('\n'); |
| 128 | } |
| 129 | |
| 130 | /** Whether a directory should be EXPANDED (deep walk) vs SUMMARISED (one-line count). */ |
| 131 | function shouldExpandDir(name: string, ctx: TreeContext): boolean { |
no test coverage detected