Skip to content

Commit 8cee2b1

Browse files
committed
Limit code_search to 30 results by default
1 parent 620f09f commit 8cee2b1

File tree

6 files changed

+73
-16
lines changed

6 files changed

+73
-16
lines changed

backend/src/tools/definitions/tool/code-search.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ ${getToolCallString(toolName, { pattern: 'foo\\.bar = 1\\.0' })}
4343
${getToolCallString(toolName, { pattern: 'import.*foo', cwd: 'src' })}
4444
${getToolCallString(toolName, { pattern: 'function.*authenticate', flags: '-i -t ts' })}
4545
${getToolCallString(toolName, { pattern: 'TODO', flags: '-n --type-not test' })}
46+
${getToolCallString(toolName, { pattern: 'getUserData', maxResults: 10 })}
4647
`.trim(),
4748
} satisfies ToolDescription

common/src/templates/initial-agents-dir/types/tools.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { Message } from './util-types'
2-
31
/**
42
* Union type of all available tool names
53
*/
@@ -59,6 +57,8 @@ export interface CodeSearchParams {
5957
flags?: string
6058
/** Optional working directory to search within, relative to the project root. Defaults to searching the entire project. */
6159
cwd?: string
60+
/** Maximum number of results to return. Defaults to 30. */
61+
maxResults?: number
6262
}
6363

6464
/**
@@ -78,11 +78,11 @@ export interface FindFilesParams {
7878
* Fetch up-to-date documentation for libraries and frameworks using Context7 API.
7979
*/
8080
export interface ReadDocsParams {
81-
/** The exact library or framework name (e.g., "Next.js", "MongoDB", "React"). Use the official name as it appears in documentation, not a search query. */
81+
/** The library or framework name (e.g., "Next.js", "MongoDB", "React"). Use the official name as it appears in documentation if possible. Only public libraries available in Context7's database are supported, so small or private libraries may not be available. */
8282
libraryTitle: string
83-
/** Optional specific topic to focus on (e.g., "routing", "hooks", "authentication") */
84-
topic?: string
85-
/** Optional maximum number of tokens to return. Defaults to 10000. Values less than 10000 are automatically increased to 10000. */
83+
/** Specific topic to focus on (e.g., "routing", "hooks", "authentication") */
84+
topic: string
85+
/** Optional maximum number of tokens to return. Defaults to 20000. Values less than 10000 are automatically increased to 10000. */
8686
max_tokens?: number
8787
}
8888

@@ -120,7 +120,7 @@ export interface RunTerminalCommandParams {
120120
* Set the conversation history to the provided messages.
121121
*/
122122
export interface SetMessagesParams {
123-
messages: Message[]
123+
messages: any
124124
}
125125

126126
/**
@@ -129,7 +129,7 @@ export interface SetMessagesParams {
129129
export interface SetOutputParams {}
130130

131131
/**
132-
* Spawn multiple agents and send a prompt to each of them.
132+
* Spawn multiple agents and send a prompt and/or parameters to each of them. These agents will run in parallel. Note that that means they will run independently. If you need to run agents sequentially, use spawn_agents with one agent at a time instead.
133133
*/
134134
export interface SpawnAgentsParams {
135135
agents: {

common/src/tools/params/tool/code-search.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ export const codeSearchParams = {
2525
.describe(
2626
`Optional working directory to search within, relative to the project root. Defaults to searching the entire project.`,
2727
),
28+
maxResults: z
29+
.number()
30+
.int()
31+
.positive()
32+
.optional()
33+
.default(30)
34+
.describe(`Maximum number of results to return. Defaults to 30.`),
2835
})
2936
.describe(
3037
`Search for string patterns in the project's files. This tool uses ripgrep (rg), a fast line-oriented search tool. Use this tool only when read_files is not sufficient to find the files you need.`,

npm-app/src/__tests__/tool-handlers.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export interface TestInterface {
8181
const parameters = {
8282
pattern: 'testFunction',
8383
cwd: '__tests__/data',
84+
maxResults: 30,
8485
}
8586

8687
await handleCodeSearch(parameters, 'test-id')
@@ -91,6 +92,7 @@ export interface TestInterface {
9192
test('handles basic search without cwd', async () => {
9293
const parameters = {
9394
pattern: 'export',
95+
maxResults: 30,
9496
}
9597

9698
const result = await handleCodeSearch(parameters, 'test-id')
@@ -102,6 +104,7 @@ export interface TestInterface {
102104
const parameters = {
103105
pattern: 'UNIQUE_SEARCH_STRING_12345',
104106
cwd: 'src/__tests__/data',
107+
maxResults: 30,
105108
}
106109

107110
const result = await handleCodeSearch(parameters, 'test-id')
@@ -118,10 +121,27 @@ export interface TestInterface {
118121
pattern: 'findme_xyz789',
119122
flags: '-i',
120123
cwd: 'src/__tests__/data',
124+
maxResults: 30,
121125
}
122126

123127
const result = await handleCodeSearch(parameters, 'test-id')
124128

125129
expect((result[0].value as any).stdout).toContain('findme_xyz789')
126130
})
131+
132+
test('limits results when maxResults is specified', async () => {
133+
const parameters = {
134+
pattern: 'export',
135+
maxResults: 1,
136+
cwd: 'src/__tests__/data',
137+
}
138+
139+
const result = await handleCodeSearch(parameters, 'test-id')
140+
141+
// Should contain results limited message when there are more results than maxResults
142+
const stdout = (result[0].value as any).stdout
143+
if (stdout.includes('Results limited to 1 of')) {
144+
expect(stdout).toContain('Results limited to 1 of')
145+
}
146+
})
127147
})

npm-app/src/tool-handlers.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ export const handleCodeSearch: ToolHandler<'code_search'> = async (
178178
) => {
179179
const projectPath = getProjectRoot()
180180
const rgPath = await getRgPath()
181+
const maxResults = parameters.maxResults ?? 30
181182

182183
return new Promise((resolve) => {
183184
let stdout = ''
@@ -228,18 +229,32 @@ export const handleCodeSearch: ToolHandler<'code_search'> = async (
228229

229230
childProcess.on('close', (code) => {
230231
const lines = stdout.split('\n').filter((line) => line.trim())
231-
const maxResults = 3
232-
const previewResults = lines.slice(0, maxResults)
232+
const limitedLines = lines.slice(0, maxResults)
233+
const previewResults = limitedLines.slice(0, 3)
233234
if (previewResults.length > 0) {
234235
console.log(previewResults.join('\n'))
235-
if (lines.length > maxResults) {
236+
if (limitedLines.length > 3) {
236237
console.log('...')
237238
}
238239
}
239-
console.log(green(`Found ${lines.length} results`))
240+
console.log(
241+
green(
242+
`Found ${limitedLines.length} results${lines.length > maxResults ? ` (limited from ${lines.length})` : ''}`,
243+
),
244+
)
245+
246+
// Limit results to maxResults
247+
const limitedStdout = limitedLines.join('\n')
248+
249+
// Add truncation message if results were limited
250+
const finalStdout =
251+
lines.length > maxResults
252+
? limitedStdout +
253+
`\n\n[Results limited to ${maxResults} of ${lines.length} total matches]`
254+
: limitedStdout
240255

241256
const truncatedStdout = truncateStringWithMessage({
242-
str: stdout,
257+
str: finalStdout,
243258
maxLength: 10000,
244259
})
245260
const truncatedStderr = truncateStringWithMessage({

sdk/src/tools/code-search.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ export function codeSearch({
1010
pattern,
1111
flags,
1212
cwd,
13+
maxResults = 30,
1314
}: {
1415
projectPath: string
1516
pattern: string
1617
flags?: string
1718
cwd?: string
19+
maxResults?: number
1820
}): Promise<CodebuffToolOutput<'code_search'>> {
1921
return new Promise((resolve) => {
2022
let stdout = ''
@@ -55,12 +57,24 @@ export function codeSearch({
5557
})
5658

5759
childProcess.on('close', (code) => {
60+
// Limit results to maxResults
61+
const lines = stdout.split('\n')
62+
const limitedLines = lines.slice(0, maxResults)
63+
const limitedStdout = limitedLines.join('\n')
64+
65+
// Add truncation message if results were limited
66+
const finalStdout =
67+
lines.length > maxResults
68+
? limitedStdout +
69+
`\n\n[Results limited to ${maxResults} of ${lines.length} total matches]`
70+
: limitedStdout
71+
5872
// Truncate output to prevent memory issues
5973
const maxLength = 10000
6074
const truncatedStdout =
61-
stdout.length > maxLength
62-
? stdout.substring(0, maxLength) + '\n\n[Output truncated]'
63-
: stdout
75+
finalStdout.length > maxLength
76+
? finalStdout.substring(0, maxLength) + '\n\n[Output truncated]'
77+
: finalStdout
6478

6579
const maxErrorLength = 1000
6680
const truncatedStderr =

0 commit comments

Comments
 (0)