From 8117a29d8cf5b3b7a31a21ef6d3d438c1326e62d Mon Sep 17 00:00:00 2001 From: aamir-s18 Date: Fri, 23 Jan 2026 12:41:48 +0100 Subject: [PATCH 1/4] feat: add agentic search --- README.md | 21 +++++++++++++++++++++ src/commands/search.ts | 15 +++++++++++++-- src/lib/store.ts | 22 ++++++++++++++++------ tsconfig.json | 4 +++- 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index eba7f05..aad2d8a 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,25 @@ mgrep --web "best practices for error handling in TypeScript" Web search queries the `mixedbread/web` store in addition to your local store, merging results based on relevance. Use `--answer` (or `-a`) to get a concise summary instead of raw results. +## mgrep as Subagent + +For complex questions that require information from multiple sources, `mgrep` can act as a subagent that automatically refines queries and performs multiple searches to find the best answer. + +```bash +# Enable agentic search for complex multi-part questions +mgrep -ag "What are the yearly numbers for 2020, 2021, 2022, 2023, 2024?" + +# Combine with --answer for a synthesized response from multiple sources +mgrep -ag -a "How does authentication work and where is it configured?" +``` + +When `--agentic` is enabled, mgrep will: +- Automatically break down complex queries into sub-queries +- Perform multiple searches as needed to gather comprehensive results +- Combine findings from different parts of your codebase + +This is particularly useful for questions that span multiple files or concepts, where a single search might miss important context. + ## Commands at a Glance | Command | Purpose | @@ -185,6 +204,7 @@ directory for a pattern. | `-c`, `--content` | Show content of the results | | `-a`, `--answer` | Generate an answer to the question based on the results | | `-w`, `--web` | Include web search results alongside local files | +| `-ag`, `--agentic` | Enable agentic search to automatically refine queries and perform multiple searches | | `-s`, `--sync` | Sync the local files to the store before searching | | `-d`, `--dry-run` | Dry run the search process (no actual file syncing) | | `--no-rerank` | Disable reranking of search results | @@ -281,6 +301,7 @@ searches. - `MGREP_CONTENT`: Show content of the results (set to `1` or `true` to enable) - `MGREP_ANSWER`: Generate an answer based on the results (set to `1` or `true` to enable) - `MGREP_WEB`: Include web search results (set to `1` or `true` to enable) +- `MGREP_AGENTIC`: Enable agentic search for complex multi-source queries (set to `1` or `true` to enable) - `MGREP_SYNC`: Sync files before searching (set to `1` or `true` to enable) - `MGREP_DRY_RUN`: Enable dry run mode (set to `1` or `true` to enable) - `MGREP_RERANK`: Enable reranking of search results (set to `0` or `false` to disable, default: enabled) diff --git a/src/commands/search.ts b/src/commands/search.ts index 32fd88f..ce707a6 100644 --- a/src/commands/search.ts +++ b/src/commands/search.ts @@ -251,6 +251,11 @@ export const search: Command = new CommanderCommand("search") "Include web search results from mixedbread/web store", parseBooleanEnv(process.env.MGREP_WEB, false), ) + .option( + "-ag, --agentic", + "Enable agentic search to automatically refine queries and perform multiple searches", + parseBooleanEnv(process.env.MGREP_AGENTIC, false), + ) .argument("", "The pattern to search for") .argument("[path]", "The path to search in") .allowUnknownOption(true) @@ -267,6 +272,7 @@ export const search: Command = new CommanderCommand("search") maxFileSize?: number; maxFileCount?: number; web: boolean; + agentic: boolean; } = cmd.optsWithGlobals(); if (exec_path?.startsWith("--")) { exec_path = ""; @@ -324,13 +330,18 @@ export const search: Command = new CommanderCommand("search") ], }; + const searchOptions = { + rerank: options.rerank, + agentic: options.agentic || undefined, + }; + let response: string; if (!options.answer) { const results = await store.search( storeIds, pattern, parseInt(options.maxCount, 10), - { rerank: options.rerank }, + searchOptions, filters, ); response = formatSearchResponse(results, options.content); @@ -339,7 +350,7 @@ export const search: Command = new CommanderCommand("search") storeIds, pattern, parseInt(options.maxCount, 10), - { rerank: options.rerank }, + searchOptions, filters, ); response = formatAskResponse(results, options.content); diff --git a/src/lib/store.ts b/src/lib/store.ts index 656c31c..5b1aa79 100644 --- a/src/lib/store.ts +++ b/src/lib/store.ts @@ -57,6 +57,16 @@ export interface StoreInfo { }; } +/** + * Options for configuring search behavior. + */ +export interface SearchOptions { + /** Whether to rerank results for improved relevance */ + rerank?: boolean; + /** Enable agentic search to automatically refine queries and perform multiple searches */ + agentic?: boolean; +} + /** * Interface for store operations */ @@ -98,7 +108,7 @@ export interface Store { storeIds: string[], query: string, top_k?: number, - search_options?: { rerank?: boolean }, + search_options?: SearchOptions, filters?: SearchFilter, ): Promise; @@ -119,7 +129,7 @@ export interface Store { storeIds: string[], question: string, top_k?: number, - search_options?: { rerank?: boolean }, + search_options?: SearchOptions, filters?: SearchFilter, ): Promise; @@ -209,7 +219,7 @@ export class MixedbreadStore implements Store { storeIds: string[], query: string, top_k?: number, - search_options?: { rerank?: boolean }, + search_options?: SearchOptions, filters?: SearchFilter, ): Promise { const response = await this.client.stores.search({ @@ -240,7 +250,7 @@ export class MixedbreadStore implements Store { storeIds: string[], question: string, top_k?: number, - search_options?: { rerank?: boolean }, + search_options?: SearchOptions, filters?: SearchFilter, ): Promise { const response = await this.client.stores.questionAnswering({ @@ -421,7 +431,7 @@ export class TestStore implements Store { _storeIds: string[], query: string, top_k?: number, - search_options?: { rerank?: boolean }, + search_options?: SearchOptions, filters?: SearchFilter, ): Promise { const db = await this.load(); @@ -486,7 +496,7 @@ export class TestStore implements Store { storeIds: string[], question: string, top_k?: number, - search_options?: { rerank?: boolean }, + search_options?: SearchOptions, filters?: SearchFilter, ): Promise { const searchRes = await this.search( diff --git a/tsconfig.json b/tsconfig.json index 546eb2c..a146951 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,5 +15,7 @@ "sourceMap": true, "skipLibCheck": true, "moduleResolution": "NodeNext" - } + }, + "include": ["src/**/*"], + "exclude": ["node_modules", "dist", "bench"] } From 7c0b4fcc76d80109350fd6ff45b9f87cd0921f6a Mon Sep 17 00:00:00 2001 From: aamir-s18 Date: Fri, 23 Jan 2026 12:46:24 +0100 Subject: [PATCH 2/4] chore: fix agentic flag --- README.md | 6 +++--- src/commands/search.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index aad2d8a..6adffe6 100644 --- a/README.md +++ b/README.md @@ -168,10 +168,10 @@ For complex questions that require information from multiple sources, `mgrep` ca ```bash # Enable agentic search for complex multi-part questions -mgrep -ag "What are the yearly numbers for 2020, 2021, 2022, 2023, 2024?" +mgrep --agentic "What are the yearly numbers for 2020, 2021, 2022, 2023, 2024?" # Combine with --answer for a synthesized response from multiple sources -mgrep -ag -a "How does authentication work and where is it configured?" +mgrep --agentic -a "How does authentication work and where is it configured?" ``` When `--agentic` is enabled, mgrep will: @@ -204,7 +204,7 @@ directory for a pattern. | `-c`, `--content` | Show content of the results | | `-a`, `--answer` | Generate an answer to the question based on the results | | `-w`, `--web` | Include web search results alongside local files | -| `-ag`, `--agentic` | Enable agentic search to automatically refine queries and perform multiple searches | +| `--agentic` | Enable agentic search to automatically refine queries and perform multiple searches | | `-s`, `--sync` | Sync the local files to the store before searching | | `-d`, `--dry-run` | Dry run the search process (no actual file syncing) | | `--no-rerank` | Disable reranking of search results | diff --git a/src/commands/search.ts b/src/commands/search.ts index ce707a6..2f348fa 100644 --- a/src/commands/search.ts +++ b/src/commands/search.ts @@ -252,7 +252,7 @@ export const search: Command = new CommanderCommand("search") parseBooleanEnv(process.env.MGREP_WEB, false), ) .option( - "-ag, --agentic", + "--agentic", "Enable agentic search to automatically refine queries and perform multiple searches", parseBooleanEnv(process.env.MGREP_AGENTIC, false), ) From 10306c7c3c69110e585f2c87af801810d5f4614a Mon Sep 17 00:00:00 2001 From: aamir-s18 Date: Fri, 23 Jan 2026 12:53:29 +0100 Subject: [PATCH 3/4] chore: bugbot comments --- src/commands/search.ts | 2 +- src/lib/store.ts | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/commands/search.ts b/src/commands/search.ts index 2f348fa..880379e 100644 --- a/src/commands/search.ts +++ b/src/commands/search.ts @@ -332,7 +332,7 @@ export const search: Command = new CommanderCommand("search") const searchOptions = { rerank: options.rerank, - agentic: options.agentic || undefined, + ...(options.agentic && { agentic: true }), }; let response: string; diff --git a/src/lib/store.ts b/src/lib/store.ts index 5b1aa79..a4abe0f 100644 --- a/src/lib/store.ts +++ b/src/lib/store.ts @@ -456,10 +456,11 @@ export class TestStore implements Store { const lines = file.content.split("\n"); for (let i = 0; i < lines.length; i++) { if (lines[i].toLowerCase().includes(query.toLowerCase())) { + const rerankSuffix = search_options?.rerank ? "" : " without reranking"; + const agenticSuffix = search_options?.agentic ? " with agentic" : ""; results.push({ type: "text", - text: - lines[i] + (search_options?.rerank ? "" : " without reranking"), + text: lines[i] + rerankSuffix + agenticSuffix, score: 1.0, metadata: file.metadata, chunk_index: results.length - 1, From e7ba7adc6d2a4282dce3fd205112ee3744fb202a Mon Sep 17 00:00:00 2001 From: aamir-s18 Date: Fri, 23 Jan 2026 12:56:34 +0100 Subject: [PATCH 4/4] fix: formatting --- src/lib/store.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib/store.ts b/src/lib/store.ts index a4abe0f..ef95bb7 100644 --- a/src/lib/store.ts +++ b/src/lib/store.ts @@ -456,7 +456,9 @@ export class TestStore implements Store { const lines = file.content.split("\n"); for (let i = 0; i < lines.length; i++) { if (lines[i].toLowerCase().includes(query.toLowerCase())) { - const rerankSuffix = search_options?.rerank ? "" : " without reranking"; + const rerankSuffix = search_options?.rerank + ? "" + : " without reranking"; const agenticSuffix = search_options?.agentic ? " with agentic" : ""; results.push({ type: "text",