summaryrefslogtreecommitdiff
path: root/packages/core/src/tools/glob.ts
diff options
context:
space:
mode:
authoranj-s <[email protected]>2025-06-09 08:07:24 -0700
committerGitHub <[email protected]>2025-06-09 08:07:24 -0700
commitc55a1d90120ff8b567e9059466819c4920dc560e (patch)
treecc574fa0bbaa5f4bfe31609f49f2bb4d21471f6b /packages/core/src/tools/glob.ts
parenta2fee6bdd3927a2a08520ab87dabc8fecdb6075b (diff)
Add support for sorting files search by recency threshold followed by lexicographic sorting (#867)
Diffstat (limited to 'packages/core/src/tools/glob.ts')
-rw-r--r--packages/core/src/tools/glob.ts53
1 files changed, 47 insertions, 6 deletions
diff --git a/packages/core/src/tools/glob.ts b/packages/core/src/tools/glob.ts
index d4b479eb..6acb2a2b 100644
--- a/packages/core/src/tools/glob.ts
+++ b/packages/core/src/tools/glob.ts
@@ -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();
+
+ // 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 = filteredEntries.map((entry) => entry.path);
+ const sortedAbsolutePaths = sortedEntries.map((entry) => entry.path);
const fileListDescription = sortedAbsolutePaths.join('\n');
const fileCount = sortedAbsolutePaths.length;