Skip to content

Commit 5e011f1

Browse files
committed
Generate tools.d.ts! Create the file when using agent builder
1 parent a00476c commit 5e011f1

File tree

9 files changed

+728
-48
lines changed

9 files changed

+728
-48
lines changed

.agents/agent-config.d.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
* }
1313
*/
1414

15+
import type * as Tools from './tools'
16+
export type { Tools }
17+
18+
type ToolName = Tools.ToolName
19+
1520
// ============================================================================
1621
// Core Agent Configuration Types
1722
// ============================================================================
@@ -181,9 +186,9 @@ export interface AgentStepContext {
181186
/**
182187
* Tool call object for handleSteps generator
183188
*/
184-
export interface ToolCall {
185-
toolName: ToolName
186-
args?: Record<string, any>
189+
export interface ToolCall<T extends ToolName = ToolName> {
190+
toolName: T
191+
args?: Tools.GetToolParams<T>
187192
}
188193

189194
/**
@@ -249,16 +254,11 @@ export type PlanningTools =
249254
export type OutputTools = 'set_output' | 'end_turn'
250255

251256
/**
252-
* All available tools that agents can use
257+
* Common tool combinations for convenience
253258
*/
254-
export type ToolName =
255-
| FileTools
256-
| CodeAnalysisTools
257-
| TerminalTools
258-
| WebTools
259-
| AgentTools
260-
| PlanningTools
261-
| OutputTools
259+
export type FileEditingTools = FileTools | 'end_turn'
260+
export type ResearchTools = WebTools | 'write_file' | 'end_turn'
261+
export type CodeAnalysisToolSet = FileTools | CodeAnalysisTools | 'end_turn'
262262

263263
// ============================================================================
264264
// Available Models (see: https://openrouter.ai/models)
@@ -299,14 +299,3 @@ export type SubagentName =
299299
| 'thinker'
300300
| 'reviewer'
301301
| (string & {})
302-
303-
// ============================================================================
304-
// Utility Types
305-
// ============================================================================
306-
307-
/**
308-
* Common tool combinations for convenience
309-
*/
310-
export type FileEditingTools = FileTools | 'end_turn'
311-
export type ResearchTools = WebTools | 'write_file' | 'end_turn'
312-
export type CodeAnalysisToolSet = FileTools | CodeAnalysisTools | 'end_turn'

.agents/tools.d.ts

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
/**
2+
* Add a new message to the conversation history. To be used for complex requests that can't be solved in a single step, as you may forget what happened!
3+
*/
4+
export interface AddMessageParams {
5+
"role": "user" | "assistant"
6+
"content": string
7+
}
8+
9+
/**
10+
* Add a new subgoal for tracking progress. To be used for complex requests that can't be solved in a single step, as you may forget what happened!
11+
*/
12+
export interface AddSubgoalParams {
13+
// A unique identifier for the subgoal. Try to choose the next sequential integer that is not already in use.
14+
"id": string
15+
// The objective of the subgoal, concisely and clearly stated.
16+
"objective": string
17+
// The status of the subgoal.
18+
"status": "NOT_STARTED" | "IN_PROGRESS" | "COMPLETE" | "ABORTED"
19+
// A plan for the subgoal.
20+
"plan"?: string
21+
// A log message for the subgoal progress.
22+
"log"?: string
23+
}
24+
25+
/**
26+
* Parameters for browser_logs tool
27+
*/
28+
export interface BrowserLogsParams {
29+
// The type of browser action to perform (e.g., "navigate").
30+
"type": string
31+
// The URL to navigate to.
32+
"url": string
33+
// When to consider navigation successful. Defaults to 'load'.
34+
"waitUntil"?: "load" | "domcontentloaded" | "networkidle0"
35+
}
36+
37+
/**
38+
* 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.
39+
*/
40+
export interface CodeSearchParams {
41+
// The pattern to search for.
42+
"pattern": string
43+
// Optional ripgrep flags to customize the search (e.g., "-i" for case-insensitive, "-t ts" for TypeScript files only, "-A 3" for 3 lines after match, "-B 2" for 2 lines before match, "--type-not test" to exclude test files).
44+
"flags"?: string
45+
// Optional working directory to search within, relative to the project root. Defaults to searching the entire project.
46+
"cwd"?: string
47+
}
48+
49+
/**
50+
* Generate a detailed markdown plan for complex tasks.
51+
*/
52+
export interface CreatePlanParams {
53+
// The path including the filename of a markdown file that will be overwritten with the plan.
54+
"path": string
55+
// A detailed plan to solve the user's request.
56+
"plan": string
57+
}
58+
59+
/**
60+
* End your turn, regardless of any new tool results that might be coming. This will allow the user to type another prompt.
61+
*/
62+
export interface EndTurnParams {
63+
64+
}
65+
66+
/**
67+
* Find several files related to a brief natural language description of the files or the name of a function or class you are looking for.
68+
*/
69+
export interface FindFilesParams {
70+
// A brief natural language description of the files or the name of a function or class you are looking for. It's also helpful to mention a directory or two to look within.
71+
"prompt": string
72+
}
73+
74+
/**
75+
* Fetch up-to-date documentation for libraries and frameworks using Context7 API.
76+
*/
77+
export interface ReadDocsParams {
78+
// 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.
79+
"libraryTitle": string
80+
// Optional specific topic to focus on (e.g., "routing", "hooks", "authentication")
81+
"topic"?: string
82+
// Optional maximum number of tokens to return. Defaults to 10000. Values less than 10000 are automatically increased to 10000.
83+
"max_tokens"?: number
84+
}
85+
86+
/**
87+
* Read the multiple files from disk and return their contents. Use this tool to read as many files as would be helpful to answer the user's request.
88+
*/
89+
export interface ReadFilesParams {
90+
// List of file paths to read.
91+
"paths": string[]
92+
}
93+
94+
/**
95+
* Parameters for run_file_change_hooks tool
96+
*/
97+
export interface RunFileChangeHooksParams {
98+
// List of file paths that were changed and should trigger file change hooks
99+
"files": string[]
100+
}
101+
102+
/**
103+
* Execute a CLI command from the **project root** (different from the user's cwd).
104+
*/
105+
export interface RunTerminalCommandParams {
106+
// CLI command valid for user's OS.
107+
"command": string
108+
// Either SYNC (waits, returns output) or BACKGROUND (runs in background). Default SYNC
109+
"process_type": "SYNC" | "BACKGROUND"
110+
// The working directory to run the command in. Default is the project root.
111+
"cwd"?: string
112+
// Set to -1 for no timeout. Does not apply for BACKGROUND commands. Default 30
113+
"timeout_seconds": number
114+
}
115+
116+
/**
117+
* Send a message to another agent (parent or child) for communication and data exchange.
118+
*/
119+
export interface SendAgentMessageParams {
120+
// ID of the target agent to send message to. Use "PARENT_ID" to send to parent agent.
121+
"target_agent_id": string
122+
// Message prompt to send to the target agent
123+
"prompt": string
124+
// Optional parameters object to send with the message
125+
"params"?: Record<string, any>
126+
}
127+
128+
/**
129+
* Set the conversation history to the provided messages.
130+
*/
131+
export interface SetMessagesParams {
132+
"messages": {
133+
"role": "user" | "assistant"
134+
"content": string
135+
}[]
136+
}
137+
138+
/**
139+
* JSON object to set as the agent output. This completely replaces any previous output. If the agent was spawned, this value will be passed back to its parent. If the agent has an outputSchema defined, the output will be validated against it.
140+
*/
141+
export interface SetOutputParams {
142+
143+
}
144+
145+
/**
146+
* Spawn multiple agents and send a prompt to each of them.
147+
*/
148+
export interface SpawnAgentsParams {
149+
"agents": {
150+
// Agent to spawn
151+
"agent_type": string
152+
// Prompt to send to the agent
153+
"prompt"?: string
154+
// Parameters object for the agent (if any)
155+
"params"?: Record<string, any>
156+
}[]
157+
}
158+
159+
/**
160+
* Parameters for spawn_agents_async tool
161+
*/
162+
export interface SpawnAgentsAsyncParams {
163+
"agents": {
164+
// Agent to spawn
165+
"agent_type": string
166+
// Prompt to send to the agent
167+
"prompt"?: string
168+
// Parameters object for the agent (if any)
169+
"params"?: Record<string, any>
170+
}[]
171+
}
172+
173+
/**
174+
* Replace strings in a file with new strings.
175+
*/
176+
export interface StrReplaceParams {
177+
// The path to the file to edit.
178+
"path": string
179+
// Array of replacements to make.
180+
"replacements": {
181+
// The string to replace. This must be an *exact match* of the string you want to replace, including whitespace and punctuation.
182+
"old": string
183+
// The string to replace the corresponding old string with. Can be empty to delete.
184+
"new": string
185+
}[]
186+
}
187+
188+
/**
189+
* Deeply consider complex tasks by brainstorming approaches and tradeoffs step-by-step.
190+
*/
191+
export interface ThinkDeeplyParams {
192+
// Detailed step-by-step analysis. Initially keep each step concise (max ~5-7 words per step).
193+
"thought": string
194+
}
195+
196+
/**
197+
* Update a subgoal in the context given the id, and optionally the status or plan, or a new log to append. Feel free to update any combination of the status, plan, or log in one invocation.
198+
*/
199+
export interface UpdateSubgoalParams {
200+
// The id of the subgoal to update.
201+
"id": string
202+
// Change the status of the subgoal.
203+
"status"?: "NOT_STARTED" | "IN_PROGRESS" | "COMPLETE" | "ABORTED"
204+
// Change the plan for the subgoal.
205+
"plan"?: string
206+
// Add a log message to the subgoal. This will create a new log entry and append it to the existing logs. Use this to record your progress and any new information you learned as you go.
207+
"log"?: string
208+
}
209+
210+
/**
211+
* Search the web for current information using Linkup API.
212+
*/
213+
export interface WebSearchParams {
214+
// The search query to find relevant web content
215+
"query": string
216+
// Search depth - 'standard' for quick results, 'deep' for more comprehensive search. Default is 'standard'.
217+
"depth": "standard" | "deep"
218+
}
219+
220+
/**
221+
* Create or edit a file with the given content.
222+
*/
223+
export interface WriteFileParams {
224+
// Path to the file relative to the **project root**
225+
"path": string
226+
// What the change is intended to do in only one sentence.
227+
"instructions": string
228+
// Edit snippet to apply to the file.
229+
"content": string
230+
}
231+
232+
/**
233+
* Union type of all available tool names
234+
*/
235+
export type ToolName = 'add_message' | 'add_subgoal' | 'browser_logs' | 'code_search' | 'create_plan' | 'end_turn' | 'find_files' | 'read_docs' | 'read_files' | 'run_file_change_hooks' | 'run_terminal_command' | 'send_agent_message' | 'set_messages' | 'set_output' | 'spawn_agents' | 'spawn_agents_async' | 'str_replace' | 'think_deeply' | 'update_subgoal' | 'web_search' | 'write_file'
236+
237+
/**
238+
* Map of tool names to their parameter types
239+
*/
240+
export interface ToolParamsMap {
241+
'add_message': AddMessageParams
242+
'add_subgoal': AddSubgoalParams
243+
'browser_logs': BrowserLogsParams
244+
'code_search': CodeSearchParams
245+
'create_plan': CreatePlanParams
246+
'end_turn': EndTurnParams
247+
'find_files': FindFilesParams
248+
'read_docs': ReadDocsParams
249+
'read_files': ReadFilesParams
250+
'run_file_change_hooks': RunFileChangeHooksParams
251+
'run_terminal_command': RunTerminalCommandParams
252+
'send_agent_message': SendAgentMessageParams
253+
'set_messages': SetMessagesParams
254+
'set_output': SetOutputParams
255+
'spawn_agents': SpawnAgentsParams
256+
'spawn_agents_async': SpawnAgentsAsyncParams
257+
'str_replace': StrReplaceParams
258+
'think_deeply': ThinkDeeplyParams
259+
'update_subgoal': UpdateSubgoalParams
260+
'web_search': WebSearchParams
261+
'write_file': WriteFileParams
262+
}
263+
264+
/**
265+
* Get parameters type for a specific tool
266+
*/
267+
export type GetToolParams<T extends ToolName> = ToolParamsMap[T]

backend/src/templates/agents/agent-builder.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
AGENT_CONFIG_FILE,
1212
} from '@codebuff/common/constants'
1313
import { AgentTemplateTypes } from '@codebuff/common/types/session-state'
14+
import { compileToolDefinitions } from '@codebuff/common/tools/compile-tool-definitions'
1415
import z from 'zod/v4'
1516

1617
const TEMPLATE_RELATIVE_PATH =
@@ -22,6 +23,11 @@ import(TEMPLATE_RELATIVE_PATH)
2223
const TEMPLATE_PATH = path.join(__dirname, TEMPLATE_RELATIVE_PATH)
2324
const DEFAULT_MODEL = openrouterModels.openrouter_claude_sonnet_4
2425
const TEMPLATE_TYPES_PATH = path.join(AGENT_TEMPLATES_DIR, AGENT_CONFIG_FILE)
26+
const TOOL_DEFINITIONS_FILE = 'tools.d.ts'
27+
const TOOL_DEFINITIONS_PATH = path.join(
28+
AGENT_TEMPLATES_DIR,
29+
TOOL_DEFINITIONS_FILE
30+
)
2531

2632
export const agentBuilder = (model: Model): Omit<AgentTemplate, 'id'> => {
2733
// Read the AGENT_CONFIG_FILE content dynamically
@@ -169,7 +175,18 @@ IMPORTANT: Always end your response with the end_turn tool when you have complet
169175
},
170176
}
171177

172-
// Step 3: Add user message with requirements for agent creation or editing
178+
// Step 3: Write the tool definitions file
179+
const toolDefinitionsContent = compileToolDefinitions()
180+
yield {
181+
toolName: 'write_file',
182+
args: {
183+
path: TOOL_DEFINITIONS_PATH,
184+
instructions: 'Create tools type file',
185+
content: toolDefinitionsContent,
186+
},
187+
}
188+
189+
// Step 4: Add user message with requirements for agent creation or editing
173190
const isEditMode = params?.editMode === true
174191

175192
if (isEditMode) {

codebuff.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@
5959
"name": "prettier-format",
6060
"command": "git diff --name-only --diff-filter=ACMR | grep -E '\\.(ts|tsx|json|md)$' | xargs -r npx prettier --write",
6161
"filePattern": "**/*.{ts,tsx,json,md}"
62+
},
63+
{
64+
"name": "regenerate-tool-definitions",
65+
"command": "bun run generate-tool-definitions",
66+
"filePattern": "common/src/tools/{params/tool/*.ts,list.ts}"
6267
}
6368
],
6469
"maxAgentSteps": 20

0 commit comments

Comments
 (0)