Add support for sorting files search by recency threshold followed by lexicographic sorting (#867)

This commit is contained in:
anj-s
2025-06-09 08:07:24 -07:00
committed by GitHub
parent 0ab24b8191
commit 3177f03ab9
2 changed files with 167 additions and 7 deletions
+47 -6
View File
@@ -12,6 +12,42 @@ import { BaseTool, ToolResult } from './tools.js';
import { shortenPath, makeRelative } from '../utils/paths.js';
import { Config } from '../config/config.js';
// Type definition for file entries returned by fast-glob with stats: true
export interface GlobFileEntry {
path: string;
stats?: fs.Stats;
}
/**
* Sorts file entries based on recency and then alphabetically.
* Recent files (modified within recencyThresholdMs) are listed first, newest to oldest.
* Older files are listed after recent ones, sorted alphabetically by path.
*/
export function sortFileEntries(
entries: GlobFileEntry[],
nowTimestamp: number,
recencyThresholdMs: number,
): GlobFileEntry[] {
const sortedEntries = [...entries];
sortedEntries.sort((a, b) => {
const mtimeA = a.stats?.mtime?.getTime() ?? 0;
const mtimeB = b.stats?.mtime?.getTime() ?? 0;
const aIsRecent = nowTimestamp - mtimeA < recencyThresholdMs;
const bIsRecent = nowTimestamp - mtimeB < recencyThresholdMs;
if (aIsRecent && bIsRecent) {
return mtimeB - mtimeA;
} else if (aIsRecent) {
return -1;
} else if (bIsRecent) {
return 1;
} else {
return a.path.localeCompare(b.path);
}
});
return sortedEntries;
}
/**
* Parameters for the GlobTool
*/
@@ -232,13 +268,18 @@ export class GlobTool extends BaseTool<GlobToolParams, ToolResult> {
};
}
filteredEntries.sort((a, b) => {
const mtimeA = a.stats?.mtime?.getTime() ?? 0;
const mtimeB = b.stats?.mtime?.getTime() ?? 0;
return mtimeB - mtimeA;
});
// Set filtering such that we first show the most recent files
const oneDayInMs = 24 * 60 * 60 * 1000;
const nowTimestamp = new Date().getTime();
const sortedAbsolutePaths = filteredEntries.map((entry) => entry.path);
// Sort the filtered entries using the new helper function
const sortedEntries = sortFileEntries(
filteredEntries as GlobFileEntry[], // Cast because fast-glob's Entry type is generic
nowTimestamp,
oneDayInMs,
);
const sortedAbsolutePaths = sortedEntries.map((entry) => entry.path);
const fileListDescription = sortedAbsolutePaths.join('\n');
const fileCount = sortedAbsolutePaths.length;