MCPcopy Create free account
hub / github.com/anus-dev/ANUS / processImports

Function processImports

packages/core/src/utils/memoryImportProcessor.ts:206–402  ·  view source on GitHub ↗
(
  content: string,
  basePath: string,
  debugMode: boolean = false,
  importState: ImportState = {
    processedFiles: new Set(),
    maxDepth: 5,
    currentDepth: 0,
  },
  projectRoot?: string,
  importFormat: 'flat' | 'tree' = 'tree',
)

Source from the content-addressed store, hash-verified

204 * @returns Processed content with imports resolved and import tree
205 */
206export async function processImports(
207 content: string,
208 basePath: string,
209 debugMode: boolean = false,
210 importState: ImportState = {
211 processedFiles: new Set(),
212 maxDepth: 5,
213 currentDepth: 0,
214 },
215 projectRoot?: string,
216 importFormat: 'flat' | 'tree' = 'tree',
217): Promise<ProcessImportsResult> {
218 if (!projectRoot) {
219 projectRoot = await findProjectRoot(basePath);
220 }
221
222 if (importState.currentDepth >= importState.maxDepth) {
223 if (debugMode) {
224 logger.warn(
225 `Maximum import depth (${importState.maxDepth}) reached. Stopping import processing.`,
226 );
227 }
228 return {
229 content,
230 importTree: { path: importState.currentFile || 'unknown' },
231 };
232 }
233
234 // --- FLAT FORMAT LOGIC ---
235 if (importFormat === 'flat') {
236 // Use a queue to process files in order of first encounter, and a set to avoid duplicates
237 const flatFiles: Array<{ path: string; content: string }> = [];
238 // Track processed files across the entire operation
239 const processedFiles = new Set<string>();
240
241 // Helper to recursively process imports
242 async function processFlat(
243 fileContent: string,
244 fileBasePath: string,
245 filePath: string,
246 depth: number,
247 ) {
248 // Normalize the file path to ensure consistent comparison
249 const normalizedPath = path.normalize(filePath);
250
251 // Skip if already processed
252 if (processedFiles.has(normalizedPath)) return;
253
254 // Mark as processed before processing to prevent infinite recursion
255 processedFiles.add(normalizedPath);
256
257 // Add this file to the flat list
258 flatFiles.push({ path: normalizedPath, content: fileContent });
259
260 // Find imports in this file
261 const codeRegions = findCodeRegions(fileContent);
262 const imports = findImports(fileContent);
263

Callers 2

readAnusMdFilesFunction · 0.85

Calls 8

processFlatFunction · 0.85
findCodeRegionsFunction · 0.85
findImportsFunction · 0.85
validateImportPathFunction · 0.85
hasMessageFunction · 0.85
readFileMethod · 0.80
addMethod · 0.80
findProjectRootFunction · 0.70

Tested by

no test coverage detected