Skip to content

Commit 0a8aa5c

Browse files
authored
docs: code agent fixes (#593)
1 parent 2a19d6f commit 0a8aa5c

File tree

1 file changed

+90
-153
lines changed

1 file changed

+90
-153
lines changed

docs/tutorials/build-code-agent.mdx

Lines changed: 90 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,205 +1,146 @@
11
---
2-
title: "Building a Code Agent with LangChain"
2+
title: "Building Code Agents"
33
sidebarTitle: "Code Agent"
44
icon: "robot"
55
iconType: "solid"
66
---
77

8-
This guide demonstrates how to build an intelligent code agent that can analyze and manipulate codebases using Codegen's LangChain integration.
8+
This guide demonstrates how to build an intelligent code agent that can analyze and manipulate codebases.
99

10-
This agent access to powerful code viewing and manipulation tools powered by Codegen, including:
11-
- `RevealSymbolTool`: reveal all N-th degree dependencies and usages of a function
12-
- `MoveSymbolTool`: move a symbol between files, updating all imports etc. (guaranteed correctness)
13-
- `SemanticEditTool`: implementation of Cursor-style smart file editing
14-
- `SemanticSearchTool`: search over an index of vector embeddings for files
10+
```python
11+
from codegen import CodeAgent, Codebase
12+
13+
# Grab a repo from Github
14+
codebase = Codebase.from_repo('fastapi/fastapi')
1515

16-
<Info>View the full code for the default tools and agent implementation in our [examples repository](https://github.com/codegen-sh/codegen-sdk/tree/develop/src/codegen/extensions/langchain)</Info>
16+
# Create a code agent with read/write codebase access
17+
agent = CodeAgent(codebase)
18+
19+
# Run the agent with a prompt
20+
agent.run("Tell me about this repo")
21+
```
1722

18-
# Agent Starter Code
23+
The agent has access to powerful code viewing and manipulation tools powered by Codegen, including:
24+
- `ViewFileTool`: View contents and metadata of files
25+
- `SemanticSearchTool`: Search over code using natural language queries
26+
- `SemanticEditTool`: Make intelligent edits to files
27+
- `RevealSymbolTool`: Analyze symbol dependencies and usages
28+
- `MoveSymbolTool`: Move symbols between files with import handling
29+
- And many more...
1930

20-
Below is a code snippet you can run as-is to solve SWE bench issues.
31+
<Info>View the full code for the default tools and agent implementation in our [examples repository](https://github.com/codegen-sh/codegen-sdk/tree/develop/src/codegen/extensions/langchain/tools)</Info>
32+
33+
# Basic Usage
34+
35+
The following example shows how to create and run a `CodeAgent`:
2136

2237
```python
23-
from codegen import Codebase
24-
from codegen.extensions.langchain.agent import create_agent_with_tools
25-
from codegen.extensions.langchain.tools import (
26-
CreateFileTool,
27-
DeleteFileTool,
28-
EditFileTool,
29-
ListDirectoryTool,
30-
MoveSymbolTool,
31-
RenameFileTool,
32-
RevealSymbolTool,
33-
SearchTool,
34-
SemanticEditTool,
35-
ViewFileTool,
36-
)
38+
from codegen import CodeAgent, Codebase
3739

38-
# Parse Codebase
39-
# Using a repo + commit from SWE-Bench
40-
codebase = Codebase.from_repo('sqlfluff/sqlfluff', commit='a820c139ccbe6d1865d73c4a459945cd69899f8f', language='python')
41-
42-
# Define tools
43-
tools = [
44-
ViewFileTool(codebase), # View file contents
45-
ListDirectoryTool(codebase), # List directory contents
46-
SearchTool(codebase), # Search code
47-
EditFileTool(codebase), # Edit files
48-
CreateFileTool(codebase), # Create new files
49-
DeleteFileTool(codebase), # Delete files
50-
RenameFileTool(codebase), # Rename files
51-
MoveSymbolTool(codebase), # Move functions/classes
52-
RevealSymbolTool(codebase), # Analyze symbol relationships
53-
SemanticEditTool(codebase), # Make semantic edits
54-
]
55-
56-
57-
agent = create_agent_with_tools(codebase, tools)
58-
59-
# Using a prompt from SWE Bench
60-
issue = """Enable quiet mode/no-verbose in CLI for use in pre-commit hook There seems to be only an option to increase the level of verbosity when using SQLFluff [CLI](https://docs.sqlfluff.com/en/stable/cli.html), not to limit it further. It would be great to have an option to further limit the amount of prints when running `sqlfluff fix`, especially in combination with deployment using a pre-commit hook. For example, only print the return status and the number of fixes applied, similar to how it is when using `black` in a pre-commit hook: ![image](https://user-images.githubusercontent.com/10177212/140480676-dc98d00b-4383-44f2-bb90-3301a6eedec2.png) This hides the potentially long list of fixes that are being applied to the SQL files, which can get quite verbose."""
61-
prompt = f"""
62-
Hey CodegenBot!
63-
64-
Here's a SWE task for you. Please solve this task diligently.
65-
66-
>>>>> ISSUE TEXT <<<<<
67-
{issue}
68-
>>>>> ISSUE TEXT <<<<<
69-
70-
Godspeed, CodegenBot! 🚀
71-
"""
72-
73-
# Run the agent
74-
result = agent.invoke(
75-
{'input': prompt},
76-
config={"configurable": {"session_id": "demo"}}
77-
)
40+
# Grab a repo from Github
41+
codebase = Codebase.from_repo('fastapi/fastapi')
42+
43+
# Create a code agent with read/write codebase access
44+
agent = CodeAgent(codebase)
45+
46+
# Run the agent with a prompt
47+
agent.run("Tell me about this repo")
7848
```
49+
<Note>Your `ANTHROPIC_API_KEY` must be set in your env.</Note>
7950

80-
## Step 1: Tool Configuration
51+
# Available Tools
8152

82-
The agent comes with several built-in tools for code operations:
53+
The agent comes with a comprehensive set of tools for code analysis and manipulation. Here are some key tools:
8354

8455
```python
8556
from codegen.extensions.langchain.tools import (
57+
ViewFileTool,
58+
SemanticSearchTool,
59+
SemanticEditTool,
60+
RevealSymbolTool,
61+
MoveSymbolTool,
62+
EditFileTool,
8663
CreateFileTool,
8764
DeleteFileTool,
88-
EditFileTool,
8965
ListDirectoryTool,
90-
MoveSymbolTool,
91-
RenameFileTool,
92-
RevealSymbolTool,
9366
SearchTool,
94-
SemanticEditTool,
95-
ViewFileTool,
9667
)
97-
98-
tools = [
99-
ViewFileTool(codebase), # View file contents
100-
ListDirectoryTool(codebase), # List directory contents
101-
SearchTool(codebase), # Search code
102-
EditFileTool(codebase), # Edit files
103-
CreateFileTool(codebase), # Create new files
104-
DeleteFileTool(codebase), # Delete files
105-
RenameFileTool(codebase), # Rename files
106-
MoveSymbolTool(codebase), # Move functions/classes
107-
RevealSymbolTool(codebase), # Analyze symbol relationships
108-
SemanticEditTool(codebase), # Make semantic edits
109-
]
11068
```
11169

112-
## Step 2: Setting Up the Agent
70+
<Note>View the full set of [tools on Github](https://github.com/codegen-sh/codegen-sdk/blob/develop/src/codegen/extensions/langchain/tools.py)</Note>
11371

114-
You can create an agent using `create_agent_with_tools`:
72+
Each tool provides specific capabilities:
11573

116-
```python
117-
from langchain_openai import ChatOpenAI
118-
from codegen import Codebase
119-
from codegen.extensions.langchain.agent import create_agent_with_tools
120-
121-
# Create the agent with GPT-4
122-
agent = create_agent_with_tools(codebase=codebase, tools=tools)
123-
```
124-
125-
The agent is initialized with:
126-
- A Codebase instance to operate on
127-
- An LLM (GPT-4o in this case)
128-
- Tools for code manipulation
129-
- A conversation memory to maintain context
74+
# Extensions
13075

76+
## GitHub Integration
13177

78+
The agent includes tools for GitHub operations like PR management. Set up GitHub access with:
13279

133-
Each tool provides specific capabilities to the agent, allowing it to perform complex code operations.
134-
135-
## Step 3: Interacting with the Agent
80+
```bash
81+
CODEGEN_SECRETS__GITHUB_TOKEN="..."
82+
```
13683

137-
Let's see some examples of how to interact with the agent:
84+
Import the GitHub tools:
13885

13986
```python
140-
# Analyze dependencies
141-
result = agent.invoke(
142-
{"input": "What are the dependencies of the FastAPI class?"},
143-
config={"configurable": {"session_id": "demo"}}
87+
from codegen.extensions.langchain.tools import (
88+
GithubCreatePRTool,
89+
GithubViewPRTool,
90+
GithubCreatePRCommentTool,
91+
GithubCreatePRReviewCommentTool
14492
)
145-
print(result["output"])
93+
```
14694

147-
# Find usage patterns
148-
result = agent.invoke(
149-
{"input": "Show me examples of dependency injection in the codebase"},
150-
config={"configurable": {"session_id": "demo"}}
151-
)
152-
print(result["output"])
95+
These tools enable:
96+
- Creating pull requests
97+
- Viewing PR contents and diffs
98+
- Adding general PR comments
99+
- Adding inline review comments
153100

154-
# Perform code analysis
155-
result = agent.invoke(
156-
{"input": "What's the most complex function in terms of dependencies?"},
157-
config={"configurable": {"session_id": "demo"}}
158-
)
159-
print(result["output"])
160-
```
101+
<Note>View all Github tools on [Github](https://github.com/codegen-sh/codegen-sdk/blob/develop/src/codegen/extensions/langchain/tools.py)</Note>
161102

162-
The agent maintains conversation history, so it can reference previous queries and build context over time.
163103

164-
## Step 4: Code Manipulation
104+
## Linear Integration
165105

166-
The agent can also perform code changes:
106+
The agent can interact with Linear for issue tracking and project management. To use Linear tools, set the following environment variables:
167107

168-
```python
169-
# Move a function to a new file
170-
result = agent.invoke(
171-
{"input": "Move the validate_email function to validation_utils.py"},
172-
config={"configurable": {"session_id": "demo"}}
173-
)
108+
```bash
109+
LINEAR_ACCESS_TOKEN="..."
110+
LINEAR_TEAM_ID="..."
111+
LINEAR_SIGNING_SECRET="..."
112+
```
174113

175-
# Rename a class and update all references
176-
result = agent.invoke(
177-
{"input": "Rename the UserModel class to User and update all imports"},
178-
config={"configurable": {"session_id": "demo"}}
179-
)
114+
Import and use the Linear tools:
180115

181-
# Add error handling
182-
result = agent.invoke(
183-
{"input": "Add proper error handling to the process_data function"},
184-
config={"configurable": {"session_id": "demo"}}
116+
```python
117+
from codegen.extensions.langchain.tools import (
118+
LinearGetIssueTool,
119+
LinearGetIssueCommentsTool,
120+
LinearCommentOnIssueTool,
121+
LinearSearchIssuesTool,
122+
LinearCreateIssueTool,
123+
LinearGetTeamsTool
185124
)
186125
```
187126

188-
The agent will:
189-
1. Analyze the current code state
190-
2. Plan the necessary changes
191-
3. Execute the changes while maintaining code correctness
192-
4. Update all related imports and references
127+
These tools allow the agent to:
128+
- Create and search issues
129+
- Get issue details and comments
130+
- Add comments to issues
131+
- View team information
132+
133+
<Note>View all Linear tools on [Github](https://github.com/codegen-sh/codegen-sdk/blob/develop/src/codegen/extensions/langchain/tools.py)</Note>
193134

194-
## Advanced Usage
195135

196-
### Adding Custom Tools
136+
## Adding Custom Tools
197137

198138
You can extend the agent with custom tools:
199139

200140
```python
201141
from langchain.tools import BaseTool
202142
from pydantic import BaseModel, Field
143+
from codegen import CodeAgent
203144

204145
class CustomToolInput(BaseModel):
205146
"""Input schema for custom tool."""
@@ -217,9 +158,5 @@ class CustomCodeTool(BaseTool):
217158

218159
# Add custom tool to agent
219160
tools.append(CustomCodeTool())
220-
agent = create_codebase_agent(
221-
codebase=codebase,
222-
tools=tools,
223-
model_name="gpt-4o"
224-
)
161+
agent = CodebaseAgent(codebase, tools=tools, model_name="gpt-4o")
225162
```

0 commit comments

Comments
 (0)