Skip to content

Commit 75d83bf

Browse files
committed
Walkthrough for creating an agent!
1 parent ac1097a commit 75d83bf

File tree

2 files changed

+339
-0
lines changed

2 files changed

+339
-0
lines changed

web/src/components/docs/doc-sidebar.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ export const sections = [
3737
})),
3838
external: false,
3939
},
40+
{
41+
title: 'Walkthroughs',
42+
href: '/docs/walkthroughs',
43+
subsections: getDocsByCategory('walkthroughs').map((doc) => ({
44+
title: doc.title,
45+
href: `/docs/walkthroughs/${doc.slug}`,
46+
})),
47+
external: false,
48+
},
4049
{
4150
title: 'Advanced',
4251
href: '/docs/advanced',
Lines changed: 330 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
---
2+
title: 'Creating Your First Agent'
3+
section: 'walkthroughs'
4+
tags: ['walkthrough', 'agents', 'getting-started', 'publishing']
5+
order: 1
6+
---
7+
8+
# Creating Your First Agent
9+
10+
This walkthrough will guide you through creating, testing, and publishing your first custom agent. We'll build a simple code reviewer agent from scratch.
11+
12+
## Step 1: Install and Initialize
13+
14+
First, install Codebuff globally:
15+
16+
```bash
17+
npm install -g codebuff
18+
```
19+
20+
Then navigate to your project and run:
21+
22+
```bash
23+
codebuff init-agents
24+
```
25+
26+
This creates a `.agents` directory with:
27+
28+
- Type definitions in `.agents/types/`
29+
- Example agents in `.agents/examples/`
30+
- A starter template: `.agents/my-custom-agent.ts`
31+
32+
You should see output like:
33+
34+
```
35+
📁 Creating agent files:
36+
✓ .agents/README.md - Documentation for your agents
37+
✓ .agents/types/agent-definition.ts - TypeScript type definitions for agents
38+
✓ .agents/my-custom-agent.ts - Your first custom agent example
39+
✓ .agents/examples/01-basic-diff-reviewer.ts - Basic diff reviewer agent example
40+
```
41+
42+
## Step 2: Create Your Agent
43+
44+
Create a new file `.agents/simple-code-reviewer.ts`:
45+
46+
```typescript
47+
import type { AgentDefinition } from './types/agent-definition'
48+
49+
const definition: AgentDefinition = {
50+
id: 'simple-code-reviewer',
51+
displayName: 'Simple Code Reviewer',
52+
model: 'anthropic/claude-4-sonnet-20250522',
53+
54+
// Tools this agent can use
55+
toolNames: [
56+
'read_files',
57+
'run_terminal_command',
58+
'code_search',
59+
'spawn_agents'
60+
],
61+
62+
// Other agents this agent can spawn
63+
spawnableAgents: ['codebuff/file-explorer@0.0.2'],
64+
65+
// When should other agents spawn this one?
66+
spawnerPrompt: 'Spawn when you need to review local code changes',
67+
68+
// System prompt defines the agent's identity
69+
systemPrompt: \`You are an expert software developer specializing in code review.
70+
Your job is to review code changes and provide helpful, constructive feedback.\`,
71+
72+
// Instructions for what the agent should do
73+
instructionsPrompt: \`Review code changes by following these steps:
74+
1. Use git diff to see what changed
75+
2. Read the modified files to understand the context
76+
3. Look for potential issues: bugs, security problems, style violations
77+
4. Suggest specific improvements with examples
78+
5. Highlight what was done well\`,
79+
}
80+
81+
export default definition
82+
```
83+
84+
## Step 3: Test Your Agent Locally
85+
86+
Now test your agent with Codebuff:
87+
88+
1. **Start Codebuff:**
89+
90+
```bash
91+
codebuff
92+
```
93+
94+
2. **Have Codebuff spawn your agent using the @ command:**
95+
96+
```
97+
@Simple Code Reviewer please review my recent changes
98+
```
99+
100+
3. **Watch it work:**
101+
- Your spawned agent will run `git diff` to see changes
102+
- Read the modified files
103+
- Provide a detailed code review
104+
105+
Make some changes to a file first to give it something to review!
106+
107+
## Step 4: Create a Publisher Profile
108+
109+
Before publishing, you need a publisher profile:
110+
111+
1. **Visit the publisher creation page:**
112+
113+
Go to [https://www.codebuff.com/publishers/new](https://www.codebuff.com/publishers/new)
114+
115+
2. **Choose ownership type:**
116+
- **Personal**: Publish under your own name
117+
- **Organization**: Publish under a team/company
118+
119+
3. **Fill in basic information:**
120+
- **Publisher Name**: Your display name (e.g., "John Smith")
121+
- **Publisher ID**: URL-friendly identifier (e.g., "john-smith")
122+
- **Email**: Contact email (optional)
123+
124+
4. **Add profile details:**
125+
- **Bio**: Brief description of yourself
126+
- **Avatar**: Profile picture (optional)
127+
128+
5. **Create your profile**
129+
130+
## Step 5: Prepare for Publishing
131+
132+
Add your publisher ID to your agent definition:
133+
134+
```typescript
135+
// Add this field to your agent definition
136+
const definition: AgentDefinition = {
137+
id: 'simple-code-reviewer',
138+
displayName: 'Simple Code Reviewer',
139+
publisher: 'your-publisher-id', // ← Add this line
140+
model: 'anthropic/claude-4-sonnet-20250522',
141+
// ... rest of your definition
142+
}
143+
```
144+
145+
Replace \`'your-publisher-id'\` with the ID you created in Step 4.
146+
147+
## Step 6: Publish Your Agent
148+
149+
Publish your agent to the Codebuff store:
150+
151+
```bash
152+
codebuff publish simple-code-reviewer
153+
```
154+
155+
You should see:
156+
157+
```
158+
Publishing:
159+
- Simple Code Reviewer (simple-code-reviewer)
160+
Successfully published:
161+
- Simple Code Reviewer (your-publisher-id/simple-code-reviewer@1.0.0)
162+
```
163+
164+
## Step 7: Test Your Published Agent
165+
166+
Now anyone can use your agent:
167+
168+
```bash
169+
codebuff --agent @your-publisher-id/simple-code-reviewer@0.0.1
170+
```
171+
172+
## Advanced Example: Deep Code Reviewer
173+
174+
Once you're comfortable with basic agents, try building something more sophisticated using **programmatic control**. Here's an advanced agent that demonstrates the power of the `handleSteps` generator function:
175+
176+
`.agents/deep-code-reviewer.ts`:
177+
178+
```typescript
179+
import type { AgentDefinition } from './types/agent-definition'
180+
181+
const definition: AgentDefinition = {
182+
id: 'deep-code-reviewer',
183+
displayName: 'Deep Code Reviewer',
184+
publisher: 'your-publisher-id',
185+
model: 'anthropic/claude-sonnet-4',
186+
187+
spawnerPrompt:
188+
'Spawn when you need to review code changes in the git diff or staged changes',
189+
190+
toolNames: [
191+
'read_files',
192+
'code_search',
193+
'run_terminal_command',
194+
'spawn_agents',
195+
],
196+
197+
// Use fully qualified agent names with publisher and version
198+
spawnableAgents: [
199+
'codebuff/file-explorer@0.0.1',
200+
'codebuff/deep-thinker@0.0.4',
201+
],
202+
203+
instructionsPrompt: `Review code changes comprehensively:
204+
1. Analyze git diff and untracked files
205+
2. Find related files using file explorer
206+
3. Use multiple perspectives for thorough review
207+
4. Look for simplification opportunities
208+
5. Check for logical errors and edge cases`,
209+
210+
// This is where the magic happens - programmatic control!
211+
handleSteps: function* () {
212+
// Step 1: Get changed files from git
213+
const { toolResult: gitDiffResult } = yield {
214+
toolName: 'run_terminal_command',
215+
input: { command: 'git diff HEAD --name-only' },
216+
}
217+
218+
// Step 2: Get untracked files
219+
const { toolResult: gitStatusResult } = yield {
220+
toolName: 'run_terminal_command',
221+
input: { command: 'git status --porcelain' },
222+
}
223+
224+
// Step 3: Show the actual diff
225+
yield {
226+
toolName: 'run_terminal_command',
227+
input: { command: 'git diff HEAD' },
228+
}
229+
230+
// Step 4: Parse file paths (with error handling)
231+
const changedFiles = (gitDiffResult || '')
232+
.split('\n')
233+
.map((line) => line.trim())
234+
.filter((line) => line && !line.includes('OSC'))
235+
236+
const untrackedFiles = (gitStatusResult || '')
237+
.split('\n')
238+
.filter((line) => line.startsWith('??'))
239+
.map((line) => line.substring(3).trim())
240+
.filter((file) => file)
241+
242+
const allFiles = [...changedFiles, ...untrackedFiles]
243+
244+
// Step 5: Read all the files
245+
if (allFiles.length > 0) {
246+
yield {
247+
toolName: 'read_files',
248+
input: { paths: allFiles },
249+
}
250+
}
251+
252+
// Step 6: Spawn file explorer to find related files
253+
yield {
254+
toolName: 'spawn_agents',
255+
input: {
256+
agents: [
257+
{
258+
agent_type: 'codebuff/file-explorer@0.0.1',
259+
prompt: 'Find files related to the changed code',
260+
},
261+
],
262+
},
263+
}
264+
265+
// Step 7: Let the LLM generate the final review
266+
yield 'STEP_ALL'
267+
},
268+
}
269+
270+
export default definition
271+
```
272+
273+
### Understanding `handleSteps`
274+
275+
The `handleSteps` generator function gives you **precise control** over your agent's execution:
276+
277+
**Key Concepts:**
278+
279+
1. **`yield` tool calls**: Execute tools and get results
280+
281+
```typescript
282+
const { toolResult, toolError } = yield {
283+
toolName: 'run_terminal_command',
284+
input: { command: 'git status' }
285+
}
286+
```
287+
288+
2. **`yield 'STEP_ALL'`**: Let the LLM take over and generate responses
289+
290+
```typescript
291+
yield 'STEP_ALL' // LLM runs until completion
292+
```
293+
294+
3. **`yield 'STEP'`**: Let the LLM take one step, then return control
295+
296+
```typescript
297+
yield 'STEP' // LLM takes one turn, then back to your code
298+
```
299+
300+
4. **`return`**: End the agent's execution immediately
301+
```typescript
302+
return // Agent stops here
303+
```
304+
305+
**When to Use Programmatic Control:**
306+
307+
- **Complex workflows** with multiple steps
308+
- **Conditional logic** based on file contents
309+
- **Error handling** for tool failures
310+
- **Dynamic agent spawning** based on analysis
311+
- **Data transformation** between steps
312+
313+
**When to Use Simple Prompts:**
314+
315+
- **Straightforward tasks** with clear instructions
316+
- **Creative tasks** that benefit from LLM flexibility
317+
- **Quick prototypes** that don't need complex logic
318+
319+
---
320+
321+
🎉 **Congratulations!** You've successfully:
322+
323+
- ✅ Created a custom agent from scratch
324+
- ✅ Tested it locally with the @ command
325+
- ✅ Set up a publisher profile
326+
- ✅ Published your agent to the store
327+
- ✅ Learned the basics of agent architecture
328+
- ✅ Explored advanced programmatic control with `handleSteps`
329+
330+
You're now ready to build sophisticated agents and contribute to the Codebuff ecosystem!

0 commit comments

Comments
 (0)