|
1 | 1 | import * as os from 'os' |
| 2 | +import * as fs from 'fs' |
2 | 3 |
|
3 | 4 | import { getFileTokenScores } from '@codebuff/code-map/parse' |
| 5 | +import { |
| 6 | + getProjectFileTree, |
| 7 | + getAllFilePaths, |
| 8 | +} from '../../common/src/project-file-tree' |
4 | 9 |
|
5 | 10 | import { type CustomToolDefinition } from './custom-tool' |
6 | 11 | import { getInitialSessionState } from '../../common/src/types/session-state' |
@@ -99,6 +104,27 @@ async function computeProjectIndex( |
99 | 104 | return { fileTree, fileTokenScores, tokenCallers } |
100 | 105 | } |
101 | 106 |
|
| 107 | +/** |
| 108 | + * Discovers project files using .gitignore patterns when projectFiles is undefined |
| 109 | + */ |
| 110 | +function discoverProjectFiles(cwd: string): Record<string, string> { |
| 111 | + try { |
| 112 | + const fileTree = getProjectFileTree(cwd) |
| 113 | + const filePaths = getAllFilePaths(fileTree) |
| 114 | + |
| 115 | + // Create projectFiles with empty content - the token scorer will read from disk |
| 116 | + return Object.fromEntries( |
| 117 | + filePaths.map((filePath) => [ |
| 118 | + filePath, |
| 119 | + fs.readFileSync(filePath, 'utf8'), |
| 120 | + ]), |
| 121 | + ) |
| 122 | + } catch (error) { |
| 123 | + console.warn('Failed to discover project files:', error) |
| 124 | + return {} |
| 125 | + } |
| 126 | +} |
| 127 | + |
102 | 128 | /** |
103 | 129 | * Auto-derives knowledge files from project files if knowledgeFiles is undefined |
104 | 130 | */ |
@@ -128,9 +154,13 @@ export async function initialSessionState( |
128 | 154 | maxAgentSteps?: number |
129 | 155 | }, |
130 | 156 | ) { |
131 | | - const { projectFiles = {}, agentDefinitions = [] } = options |
| 157 | + let { projectFiles, agentDefinitions = [] } = options |
132 | 158 | let { knowledgeFiles } = options |
133 | 159 |
|
| 160 | + // Auto-discover project files if not provided |
| 161 | + if (projectFiles === undefined) { |
| 162 | + projectFiles = discoverProjectFiles(cwd) |
| 163 | + } |
134 | 164 | if (knowledgeFiles === undefined) { |
135 | 165 | knowledgeFiles = deriveKnowledgeFiles(projectFiles) |
136 | 166 | } |
|
0 commit comments