Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Fixed
- Properly map all hotkeys in UI based on the platform [#784](https://github.com/sourcebot-dev/sourcebot/pull/784)
- Properly map all hotkeys in UI based on the platform. [#784](https://github.com/sourcebot-dev/sourcebot/pull/784)
- Properly handle regex filters that include parenthesis. [#786](https://github.com/sourcebot-dev/sourcebot/pull/786)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Use “parentheses” (plural) for clarity.

Minor wording nit in the changelog entry.

✏️ Suggested tweak
-- Properly handle regex filters that include parenthesis. [`#786`](https://github.com/sourcebot-dev/sourcebot/pull/786)
+- Properly handle regex filters that include parentheses. [`#786`](https://github.com/sourcebot-dev/sourcebot/pull/786)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- Properly handle regex filters that include parenthesis. [#786](https://github.com/sourcebot-dev/sourcebot/pull/786)
- Properly handle regex filters that include parentheses. [`#786`](https://github.com/sourcebot-dev/sourcebot/pull/786)
🤖 Prompt for AI Agents
In `@CHANGELOG.md` at line 12, Update the changelog entry text to use the plural
form "parentheses" instead of "parenthesis" for clarity; locate the line
containing "Properly handle regex filters that include parenthesis.
[`#786`](https://github.com/sourcebot-dev/sourcebot/pull/786)" and change it to
"Properly handle regex filters that include parentheses.
[`#786`](https://github.com/sourcebot-dev/sourcebot/pull/786)".


## [4.10.16] - 2026-01-22

Expand Down
14 changes: 13 additions & 1 deletion packages/mcp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Entry point for the MCP server
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { preprocessRegexp } from '@sourcebot/shared';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: mcp cannot take a dependency on @sourcebot/shared since we do not publish it to npm

import escapeStringRegexp from 'escape-string-regexp';
import { z } from 'zod';
import { listRepos, search, getFileSource } from './client.js';
Expand All @@ -25,7 +26,8 @@ server.tool(
If you receive an error that indicates that you're not authenticated, please inform the user to set the SOURCEBOT_API_KEY environment variable.
If the \`includeCodeSnippets\` property is true, code snippets containing the matches will be included in the response. Only set this to true if the request requires code snippets (e.g., show me examples where library X is used).
When referencing a file in your response, **ALWAYS** include the file's external URL as a link. This makes it easier for the user to view the file, even if they don't have it locally checked out.
**ONLY USE** the \`filterByRepoIds\` property if the request requires searching a specific repo(s). Otherwise, leave it empty.`,
**ONLY USE** the \`filterByRepoIds\` property if the request requires searching a specific repo(s). Otherwise, leave it empty.
If the request is asking to search for a specific file or results for files in a specific file path, **YOU MUST** ensure that the \`filterByFile\` property is used.`,
{
query: z
.string()
Expand All @@ -41,6 +43,10 @@ server.tool(
.array(z.string())
.describe(`Scope the search to the provided languages. The language MUST be formatted as a GitHub linguist language. Examples: Python, JavaScript, TypeScript, Java, C#, C++, PHP, Go, Rust, Ruby, Swift, Kotlin, Shell, C, Dart, HTML, CSS, PowerShell, SQL, R`)
.optional(),
filterByFile: z
.array(z.string())
.describe("Scope the search to results inside filepaths that match the provided regex expression. By default all files are searched, so **only use this filter if you need to filter on specific files**. **YOU MUST** ensure that this is a valid regex expression and any special characters are properly escaped. If the regex expresion includes a paranthesis **YOU MUST** wrap this value in quotes when passing it in.")
.optional(),
caseSensitive: z
.boolean()
.describe(`Whether the search should be case sensitive (default: false).`)
Expand All @@ -58,6 +64,7 @@ server.tool(
query,
filterByRepoIds: repoIds = [],
filterByLanguages: languages = [],
filterByFile: filePath = [],
maxTokens = env.DEFAULT_MINIMUM_TOKENS,
includeCodeSnippets = false,
caseSensitive = false,
Expand All @@ -70,6 +77,11 @@ server.tool(
query += ` ( lang:${languages.join(' or lang:')} )`;
}

if (filePath.length > 0) {
const quotedFilters = filePath.map(preprocessRegexp);
query += ` ( file:${quotedFilters.join(' or file:')} )`;
}

const response = await search({
query,
matches: env.DEFAULT_MATCHES,
Expand Down
5 changes: 4 additions & 1 deletion packages/shared/src/index.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ export {
} from "./env.client.js";
export {
SOURCEBOT_VERSION,
} from "./version.js";
} from "./version.js";
export {
preprocessRegexp,
} from "./query.js";
5 changes: 4 additions & 1 deletion packages/shared/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@ export {
} from "./db.js";
export {
SOURCEBOT_VERSION,
} from "./version.js";
} from "./version.js";
export {
preprocessRegexp,
} from "./query.js";
15 changes: 15 additions & 0 deletions packages/shared/src/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Wraps a value in quotes if it contains parentheses and isn't already quoted.
*
* This is needed because the query language does not allow values to include parenthesis unless they're quoted. This is
* due to the ParenExpr symbol which parses on these parenthesis. We instruct the agent to wrap the regexp in quotes
* but it's flaky, so we due it here as well as a backup plan to ensure the parser doesn't fail.
*/
export const preprocessRegexp = (value: string): string => {
const hasParentheses = value.includes('(') || value.includes(')');
const isAlreadyQuoted = value.startsWith('"') && value.endsWith('"');
if (hasParentheses && !isAlreadyQuoted) {
return `"${value}"`;
}
return value;
};
5 changes: 3 additions & 2 deletions packages/web/src/features/chat/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ export type ReadFilesToolUIPart = ToolUIPart<{ [toolNames.readFiles]: ReadFilesT

export const createCodeSearchTool = (selectedRepos: string[]) => tool({
description: `Fetches code that matches the provided regex pattern in \`query\`. This is NOT a semantic search.
Results are returned as an array of matching files, with the file's URL, repository, and language.`,
Results are returned as an array of matching files, with the file's URL, repository, and language.
If the request is asking to search for a file, or asking to only search for results within a specific filepath, **YOU MUST** use the fileNamesFilterRegexp to properly fulfil this request.`,
inputSchema: z.object({
queryRegexp: z
.string()
Expand Down Expand Up @@ -168,7 +169,7 @@ Multiple expressions can be or'd together with or, negated with -, or grouped wi
.optional(),
fileNamesFilterRegexp: z
.array(z.string())
.describe(`Filter results from filepaths that match the regex. When this option is not specified, all files are searched.`)
.describe(`Filter results from filepaths that match the regex. When this option is not specified, all files are searched. If the regex expresion includes a paranthesis **YOU MUST** wrap this value in quotes when passing it in.`)
.optional(),
limit: z.number().default(10).describe("Maximum number of matches to return (default: 100)"),
}),
Expand Down
7 changes: 5 additions & 2 deletions packages/web/src/features/chat/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CreateUIMessage, TextUIPart, UIMessagePart } from "ai";
import { Descendant, Editor, Point, Range, Transforms } from "slate";
import { preprocessRegexp } from "@sourcebot/shared/client";
import { ANSWER_TAG, FILE_REFERENCE_PREFIX, FILE_REFERENCE_REGEX } from "./constants";
import {
CustomEditor,
Expand Down Expand Up @@ -358,11 +359,13 @@ export const buildSearchQuery = (options: {
}

if (fileNamesFilterRegexp && fileNamesFilterRegexp.length > 0) {
query += ` ( file:${fileNamesFilterRegexp.join(' or file:')} )`;
const quotedFilters = fileNamesFilterRegexp.map(preprocessRegexp);
query += ` ( file:${quotedFilters.join(' or file:')} )`;
}

if (repoNamesFilterRegexp && repoNamesFilterRegexp.length > 0) {
query += ` ( repo:${repoNamesFilterRegexp.join(' or repo:')} )`;
const quotedFilters = repoNamesFilterRegexp.map(preprocessRegexp);
query += ` ( repo:${quotedFilters.join(' or repo:')} )`;
}

return query;
Expand Down