Skip to content

Commit 5545c6f

Browse files
authored
feat: agent cli (#520)
1 parent 7d89af4 commit 5545c6f

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

src/codegen/cli/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import rich_click as click
22
from rich.traceback import install
33

4+
from codegen.cli.commands.agent.main import agent_command
45
from codegen.cli.commands.config.main import config_command
56
from codegen.cli.commands.create.main import create_command
67
from codegen.cli.commands.deploy.main import deploy_command
@@ -29,6 +30,7 @@ def main():
2930

3031

3132
# Wrap commands with error handler
33+
main.add_command(agent_command)
3234
main.add_command(init_command)
3335
main.add_command(logout_command)
3436
main.add_command(login_command)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import uuid
2+
import warnings
3+
4+
import rich_click as click
5+
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
6+
from rich.console import Console
7+
from rich.markdown import Markdown
8+
from rich.prompt import Prompt
9+
10+
from codegen import Codebase
11+
from codegen.extensions.langchain.agent import create_agent_with_tools
12+
from codegen.extensions.langchain.tools import (
13+
CreateFileTool,
14+
DeleteFileTool,
15+
EditFileTool,
16+
ListDirectoryTool,
17+
MoveSymbolTool,
18+
RenameFileTool,
19+
RevealSymbolTool,
20+
SearchTool,
21+
ViewFileTool,
22+
)
23+
24+
# Suppress specific warnings
25+
warnings.filterwarnings("ignore", message=".*Helicone.*")
26+
warnings.filterwarnings("ignore", message=".*LangSmith.*")
27+
warnings.filterwarnings("ignore", category=DeprecationWarning)
28+
29+
console = Console()
30+
31+
WELCOME_ART = r"""[bold blue]
32+
____ _
33+
/ ___|___ __| | ___ __ _ ___ _ __
34+
| | / _ \ / _` |/ _ \/ _` |/ _ \ '_ \
35+
| |__| (_) | (_| | __/ (_| | __/ | | |
36+
\____\___/ \__,_|\___|\__, |\___|_| |_|
37+
|___/
38+
39+
[/bold blue]
40+
"""
41+
42+
43+
@click.command(name="agent")
44+
@click.option("--query", "-q", default=None, help="Initial query for the agent.")
45+
def agent_command(query: str):
46+
"""Start an interactive chat session with the Codegen AI agent."""
47+
# Show welcome message
48+
console.print(WELCOME_ART)
49+
50+
# Initialize codebase from current directory
51+
with console.status("[bold green]Initializing codebase...[/bold green]"):
52+
codebase = Codebase("./")
53+
54+
# Helper function for agent to print messages
55+
def say(message: str):
56+
console.print() # Add blank line before message
57+
markdown = Markdown(message)
58+
console.print(markdown)
59+
console.print() # Add blank line after message
60+
61+
# Initialize tools
62+
tools = [
63+
ViewFileTool(codebase),
64+
ListDirectoryTool(codebase),
65+
SearchTool(codebase),
66+
CreateFileTool(codebase),
67+
DeleteFileTool(codebase),
68+
RenameFileTool(codebase),
69+
MoveSymbolTool(codebase),
70+
RevealSymbolTool(codebase),
71+
EditFileTool(codebase),
72+
# RunBashCommandTool(codebase),
73+
]
74+
75+
# Initialize chat history with system message
76+
chat_history = [
77+
SystemMessage(
78+
content="""You are a helpful AI assistant with access to the local codebase.
79+
You can help with code exploration, editing, and general programming tasks.
80+
Always explain what you're planning to do before taking actions."""
81+
)
82+
]
83+
84+
# Get initial query if not provided via command line
85+
if not query:
86+
console.print("[bold]Welcome to the Codegen CLI Agent![/bold]")
87+
console.print("I'm an AI assistant that can help you explore and modify code in this repository.")
88+
console.print("I can help with tasks like viewing files, searching code, making edits, and more.")
89+
console.print()
90+
console.print("What would you like help with today?")
91+
console.print()
92+
query = Prompt.ask("[bold]>[/bold]") # Simple arrow prompt
93+
94+
# Create the agent
95+
agent = create_agent_with_tools(codebase, tools, chat_history=chat_history)
96+
97+
# Main chat loop
98+
while True:
99+
if not query: # Only prompt for subsequent messages
100+
user_input = Prompt.ask("\n[bold]>[/bold]") # Simple arrow prompt
101+
else:
102+
user_input = query
103+
query = None # Clear the initial query so we enter the prompt flow
104+
105+
if user_input.lower() in ["exit", "quit"]:
106+
break
107+
108+
# Add user message to chat history
109+
chat_history.append(HumanMessage(content=user_input))
110+
111+
# Invoke the agent
112+
with console.status("[bold green]Agent is thinking...") as status:
113+
try:
114+
session_id = str(uuid.uuid4())
115+
result = agent.invoke(
116+
{"input": user_input},
117+
config={"configurable": {"session_id": session_id}},
118+
)
119+
# Update chat history with AI's response
120+
if result.get("output"):
121+
say(result["output"])
122+
chat_history.append(AIMessage(content=result["output"]))
123+
except Exception as e:
124+
console.print(f"[bold red]Error during agent execution:[/bold red] {e}")
125+
break

0 commit comments

Comments
 (0)