Skip to content

Commit 5cf3602

Browse files
committed
Add glob & list_directory tools!
1 parent 9710a50 commit 5cf3602

File tree

21 files changed

+539
-5
lines changed

21 files changed

+539
-5
lines changed

.agents/types/tools.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export type ToolName =
66
| 'code_search'
77
| 'end_turn'
88
| 'find_files'
9+
| 'glob'
10+
| 'list_directory'
911
| 'lookup_agent_info'
1012
| 'read_docs'
1113
| 'read_files'
@@ -27,6 +29,8 @@ export interface ToolParamsMap {
2729
code_search: CodeSearchParams
2830
end_turn: EndTurnParams
2931
find_files: FindFilesParams
32+
glob: GlobParams
33+
list_directory: ListDirectoryParams
3034
lookup_agent_info: LookupAgentInfoParams
3135
read_docs: ReadDocsParams
3236
read_files: ReadFilesParams
@@ -59,7 +63,7 @@ export interface CodeSearchParams {
5963
flags?: string
6064
/** Optional working directory to search within, relative to the project root. Defaults to searching the entire project. */
6165
cwd?: string
62-
/** Maximum number of results to return. Defaults to 30. */
66+
/** Maximum number of results to return per file. Defaults to 15. There is also a global limit of 250 results across all files. */
6367
maxResults?: number
6468
}
6569

@@ -76,6 +80,24 @@ export interface FindFilesParams {
7680
prompt: string
7781
}
7882

83+
/**
84+
* Search for files matching a glob pattern. Returns matching file paths sorted by modification time.
85+
*/
86+
export interface GlobParams {
87+
/** Glob pattern to match files against (e.g., *.js, src/glob/*.ts, glob/test/glob/*.go). */
88+
pattern: string
89+
/** Optional working directory to search within, relative to project root. If not provided, searches from project root. */
90+
cwd?: string
91+
}
92+
93+
/**
94+
* List files and directories in the specified path. Returns information about each entry including name, type, size, and modification time.
95+
*/
96+
export interface ListDirectoryParams {
97+
/** Directory path to list, relative to the project root. */
98+
path: string
99+
}
100+
79101
/**
80102
* Retrieve information about an agent by ID
81103
*/

backend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"gpt-tokenizer": "2.8.1",
4343
"ignore": "5.3.2",
4444
"lodash": "*",
45+
"micromatch": "^4.0.8",
4546
"openai": "^4.78.1",
4647
"pino": "9.4.0",
4748
"postgres": "3.4.4",
@@ -56,6 +57,7 @@
5657
"@types/cors": "^2.8.19",
5758
"@types/diff": "^5.0.3",
5859
"@types/express": "^4.17.13",
60+
"@types/micromatch": "^4.0.9",
5961
"@types/ws": "^8.5.5"
6062
}
6163
}

backend/src/tools/definitions/list.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ import { codeSearchTool } from './tool/code-search'
77
import { createPlanTool } from './tool/create-plan'
88
import { endTurnTool } from './tool/end-turn'
99
import { findFilesTool } from './tool/find-files'
10+
import { globTool } from './tool/glob'
11+
import { listDirectoryTool } from './tool/list-directory'
1012
import { lookupAgentInfoTool } from './tool/lookup-agent-info'
1113
import { readDocsTool } from './tool/read-docs'
1214
import { readFilesTool } from './tool/read-files'
1315
import { runFileChangeHooksTool } from './tool/run-file-change-hooks'
1416
import { runTerminalCommandTool } from './tool/run-terminal-command'
1517
import { setMessagesTool } from './tool/set-messages'
1618
import { setOutputTool } from './tool/set-output'
17-
import { spawnAgentsTool } from './tool/spawn-agents'
1819
import { spawnAgentInlineTool } from './tool/spawn-agent-inline'
20+
import { spawnAgentsTool } from './tool/spawn-agents'
1921
import { strReplaceTool } from './tool/str-replace'
2022
import { thinkDeeplyTool } from './tool/think-deeply'
2123
import { updateSubgoalTool } from './tool/update-subgoal'
@@ -34,6 +36,8 @@ const toolDescriptions = {
3436
create_plan: createPlanTool,
3537
end_turn: endTurnTool,
3638
find_files: findFilesTool,
39+
glob: globTool,
40+
list_directory: listDirectoryTool,
3741
lookup_agent_info: lookupAgentInfoTool,
3842
read_docs: readDocsTool,
3943
read_files: readFilesTool,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The pattern supports regular expressions and will search recursively through all
2929
- Automatically ignores binary files, hidden files, and files in .gitignore
3030
3131
32-
ADVANCED RIPGREP FLAGS (use the flags parameter):
32+
Advanced ripgrep flags (use the flags parameter):
3333
3434
- Case sensitivity: "-i" for case-insensitive search
3535
- File type filtering: "-t ts" (TypeScript), "-t js" (JavaScript), "-t py" (Python), etc.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { getToolCallString } from '@codebuff/common/tools/utils'
2+
3+
import type { ToolDescription } from '../tool-def-type'
4+
5+
const toolName = 'glob'
6+
export const globTool = {
7+
toolName,
8+
description: `
9+
Example:
10+
${getToolCallString(toolName, {
11+
pattern: '**/*.test.ts',
12+
})}
13+
14+
Purpose: Search for files matching a glob pattern to discover files by name patterns rather than content.
15+
Use cases:
16+
- Find all files with a specific extension (e.g., "*.js", "*.test.ts")
17+
- Locate files in specific directories (e.g., "src/**/*.ts")
18+
- Find files with specific naming patterns (e.g., "**/test_*.go", "**/*-config.json")
19+
- Discover test files, configuration files, or other files with predictable naming
20+
21+
Glob patterns support:
22+
- * matches any characters except /
23+
- ** matches any characters including /
24+
- ? matches a single character
25+
- [abc] matches one of the characters in brackets
26+
- {a,b} matches one of the comma-separated patterns
27+
28+
This tool is fast and works well for discovering files by name patterns.
29+
`.trim(),
30+
} satisfies ToolDescription
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { getToolCallString } from '@codebuff/common/tools/utils'
2+
3+
import type { ToolDescription } from '../tool-def-type'
4+
5+
const toolName = 'list_directory'
6+
export const listDirectoryTool = {
7+
toolName,
8+
description: `
9+
Lists all files and directories in the specified path. Useful for exploring directory structure and finding files.
10+
11+
Example:
12+
${getToolCallString(toolName, {
13+
path: 'src/components',
14+
})}
15+
16+
${getToolCallString(toolName, {
17+
path: '.',
18+
})}
19+
`.trim(),
20+
} satisfies ToolDescription

backend/src/tools/handlers/list.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import { handleCodeSearch } from './tool/code-search'
55
import { handleCreatePlan } from './tool/create-plan'
66
import { handleEndTurn } from './tool/end-turn'
77
import { handleFindFiles } from './tool/find-files'
8+
import { handleGlob } from './tool/glob'
9+
import { handleListDirectory } from './tool/list-directory'
810
import { handleLookupAgentInfo } from './tool/lookup-agent-info'
911
import { handleReadDocs } from './tool/read-docs'
1012
import { handleReadFiles } from './tool/read-files'
1113
import { handleRunFileChangeHooks } from './tool/run-file-change-hooks'
1214
import { handleRunTerminalCommand } from './tool/run-terminal-command'
1315
import { handleSetMessages } from './tool/set-messages'
1416
import { handleSetOutput } from './tool/set-output'
15-
import { handleSpawnAgents } from './tool/spawn-agents'
1617
import { handleSpawnAgentInline } from './tool/spawn-agent-inline'
18+
import { handleSpawnAgents } from './tool/spawn-agents'
1719
import { handleStrReplace } from './tool/str-replace'
1820
import { handleThinkDeeply } from './tool/think-deeply'
1921
import { handleUpdateSubgoal } from './tool/update-subgoal'
@@ -40,6 +42,8 @@ export const codebuffToolHandlers = {
4042
create_plan: handleCreatePlan,
4143
end_turn: handleEndTurn,
4244
find_files: handleFindFiles,
45+
glob: handleGlob,
46+
list_directory: handleListDirectory,
4347
lookup_agent_info: handleLookupAgentInfo,
4448
read_docs: handleReadDocs,
4549
read_files: handleReadFiles,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { CodebuffToolHandlerFunction } from '../handler-function-type'
2+
import type {
3+
ClientToolCall,
4+
CodebuffToolCall,
5+
CodebuffToolOutput,
6+
} from '@codebuff/common/tools/list'
7+
8+
type ToolName = 'glob'
9+
export const handleGlob = ((params: {
10+
previousToolCallFinished: Promise<void>
11+
toolCall: CodebuffToolCall<ToolName>
12+
requestClientToolCall: (
13+
toolCall: ClientToolCall<ToolName>,
14+
) => Promise<CodebuffToolOutput<ToolName>>
15+
}): {
16+
result: Promise<CodebuffToolOutput<ToolName>>
17+
state: {}
18+
} => {
19+
const { previousToolCallFinished, toolCall, requestClientToolCall } = params
20+
21+
return {
22+
result: (async () => {
23+
await previousToolCallFinished
24+
return await requestClientToolCall(toolCall)
25+
})(),
26+
state: {},
27+
}
28+
}) satisfies CodebuffToolHandlerFunction<ToolName>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { CodebuffToolHandlerFunction } from '../handler-function-type'
2+
import type {
3+
ClientToolCall,
4+
CodebuffToolCall,
5+
CodebuffToolOutput,
6+
} from '@codebuff/common/tools/list'
7+
8+
type ToolName = 'list_directory'
9+
export const handleListDirectory = ((params: {
10+
previousToolCallFinished: Promise<void>
11+
toolCall: CodebuffToolCall<ToolName>
12+
requestClientToolCall: (
13+
toolCall: ClientToolCall<ToolName>,
14+
) => Promise<CodebuffToolOutput<ToolName>>
15+
}): {
16+
result: Promise<CodebuffToolOutput<ToolName>>
17+
state: {}
18+
} => {
19+
const { previousToolCallFinished, toolCall, requestClientToolCall } = params
20+
21+
return {
22+
result: (async () => {
23+
await previousToolCallFinished
24+
return await requestClientToolCall(toolCall)
25+
})(),
26+
state: {},
27+
}
28+
}) satisfies CodebuffToolHandlerFunction<ToolName>

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export type ToolName =
66
| 'code_search'
77
| 'end_turn'
88
| 'find_files'
9+
| 'glob'
10+
| 'list_directory'
911
| 'lookup_agent_info'
1012
| 'read_docs'
1113
| 'read_files'
@@ -27,6 +29,8 @@ export interface ToolParamsMap {
2729
code_search: CodeSearchParams
2830
end_turn: EndTurnParams
2931
find_files: FindFilesParams
32+
glob: GlobParams
33+
list_directory: ListDirectoryParams
3034
lookup_agent_info: LookupAgentInfoParams
3135
read_docs: ReadDocsParams
3236
read_files: ReadFilesParams
@@ -59,7 +63,7 @@ export interface CodeSearchParams {
5963
flags?: string
6064
/** Optional working directory to search within, relative to the project root. Defaults to searching the entire project. */
6165
cwd?: string
62-
/** Maximum number of results to return. Defaults to 30. */
66+
/** Maximum number of results to return per file. Defaults to 15. There is also a global limit of 250 results across all files. */
6367
maxResults?: number
6468
}
6569

@@ -76,6 +80,24 @@ export interface FindFilesParams {
7680
prompt: string
7781
}
7882

83+
/**
84+
* Search for files matching a glob pattern. Returns matching file paths sorted by modification time.
85+
*/
86+
export interface GlobParams {
87+
/** Glob pattern to match files against (e.g., *.js, src/glob/*.ts, glob/test/glob/*.go). */
88+
pattern: string
89+
/** Optional working directory to search within, relative to project root. If not provided, searches from project root. */
90+
cwd?: string
91+
}
92+
93+
/**
94+
* List files and directories in the specified path. Returns information about each entry including name, type, size, and modification time.
95+
*/
96+
export interface ListDirectoryParams {
97+
/** Directory path to list, relative to the project root. */
98+
path: string
99+
}
100+
79101
/**
80102
* Retrieve information about an agent by ID
81103
*/

0 commit comments

Comments
 (0)