Skip to content

Conversation

@christian-bromann
Copy link
Member

Adds filesystemFileSearchMiddleware - a middleware that provides fast file pattern matching and content search capabilities to agents, enabling them to efficiently navigate and search through codebases of any size.

Features

  • Glob Search Tool: Fast file pattern matching using glob patterns (e.g., **/*.ts, src/**/*.tsx)

    • Supports recursive directory traversal with ** patterns
    • Returns results sorted by modification time (most recent first)
    • Handles brace expansion patterns like *.{ts,tsx}
  • Grep Search Tool: Fast content search using regular expressions

    • Optional ripgrep integration for maximum performance (falls back to Node.js implementation)
    • Multiple output modes: files_with_matches, content (with line numbers), and count
    • File filtering via include patterns (e.g., *.ts, *.{ts,tsx})
    • Configurable file size limits to prevent memory issues
  • Security: Built-in path traversal protection to prevent access outside the configured root directory

Usage Example

import { createAgent } from "langchain";
import { filesystemFileSearchMiddleware } from "langchain/agents/middleware";

const agent = createAgent({
  model: "openai:gpt-4o",
  middleware: [
    filesystemFileSearchMiddleware({
      rootPath: "/workspace",
      useRipgrep: false, // Set to true if ripgrep is available
      maxFileSizeMb: 10, // Skip files larger than 10MB
    }),
  ],
});

The agent can then use the glob_search and grep_search tools to:

  • Find files by name patterns: glob_search({ pattern: "**/*.test.ts" })
  • Search file contents: grep_search({ pattern: "export.*function", include: "*.ts" })
  • Navigate codebases efficiently without loading entire file contents

Implementation

  • Ported from Python implementation with TypeScript-native patterns and idioms
  • Uses Node.js built-in fs and path modules for file operations
  • Optional ripgrep integration via child_process for enhanced performance
  • Comprehensive test suite with 31 test cases covering functionality, security, and edge cases
  • Follows existing middleware patterns in the codebase

@changeset-bot
Copy link

changeset-bot bot commented Nov 7, 2025

⚠️ No Changeset found

Latest commit: 1136e3f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR


let content: string;
try {
content = await fs.readFile(fullPath, "utf-8");

Check failure

Code scanning / CodeQL

Potential file system race condition High

The file may have changed since it
was checked
.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants