Skip to content

Commit f18f570

Browse files
committed
temp
1 parent 778740b commit f18f570

File tree

7 files changed

+296
-26
lines changed

7 files changed

+296
-26
lines changed

app.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

codegen-examples/examples/codegen_app/app.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ async def handle_mention(event: SlackEvent):
3535
logger.info("[CODE_AGENT] Running code agent")
3636
response = agent.run(event.text)
3737

38-
slack_response = cg.slack.client.chat_postMessage(channel=event.channel, text=response, thread_ts=event.ts)
39-
40-
return {"message": "Mentioned", "received_text": event.text, "response": response, "slack_response": slack_response}
38+
cg.slack.client.chat_postMessage(channel=event.channel, text=response, thread_ts=event.ts)
39+
return {"message": "Mentioned", "received_text": event.text, "response": response}
4140

4241

4342
@cg.github.event("pull_request:labeled")

codegen-examples/examples/codegen_app/codegen_app_app.py

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import logging
2+
import os
3+
from typing import Literal
4+
5+
import modal
6+
from codegen import CodeAgent, CodegenApp
7+
from codegen.configs.models.secrets import SecretsConfig
8+
from codegen.extensions.github.types.events.pull_request import PullRequestLabeledEvent
9+
from codegen.extensions.linear.types import LinearEvent
10+
from codegen.extensions.slack.types import SlackEvent
11+
from codegen.extensions.tools.github.create_pr_comment import create_pr_comment
12+
from codegen.sdk.core.codebase import Codebase
13+
14+
# Set up logging
15+
logging.basicConfig(level=logging.INFO)
16+
logger = logging.getLogger(__name__)
17+
18+
19+
########################################################################################################################
20+
# MODAL DEPLOYMENT
21+
########################################################################################################################
22+
# This deploys the FastAPI app to Modal
23+
# TODO: link this up with memory snapshotting.
24+
25+
# For deploying local package
26+
REPO_URL = "https://github.com/codegen-sh/codegen-sdk.git"
27+
COMMIT_ID = "26dafad2c319968e14b90806d42c6c7aaa627bb0"
28+
29+
# Create the base image with dependencies
30+
base_image = (
31+
modal.Image.debian_slim(python_version="3.13")
32+
.apt_install("git")
33+
.pip_install(
34+
# =====[ Codegen ]=====
35+
# "codegen",
36+
f"git+{REPO_URL}@{COMMIT_ID}",
37+
# =====[ Rest ]=====
38+
"openai>=1.1.0",
39+
"fastapi[standard]",
40+
"slack_sdk",
41+
)
42+
.add_local_dir("../../../../codegen-sdk", remote_path="/root/codegen-sdk", ignore=[".venv", "**/.venv"], copy=True)
43+
.run_commands("pip install -e /root/codegen-sdk")
44+
)
45+
46+
class ModalCodegenApp:
47+
def __init__(self, repo_org: str, repo_name: str, commit: str = "latest"):
48+
self.repo_org = repo_org
49+
self.repo_name = repo_name
50+
self.commit = commit
51+
self.cg = CodegenApp(name="codegen-test", repos=[f"{self.repo_org}/{self.repo_name}"])
52+
53+
def setup(self):
54+
self.setup_handlers(self.cg)
55+
self.cg.parse_repos()
56+
57+
def setup_handlers(self, cg: CodegenApp):
58+
@cg.slack.event("app_mention")
59+
async def handle_mention(event: SlackEvent):
60+
logger.info("[APP_MENTION] Received cg_app_mention event")
61+
logger.info(event)
62+
63+
# Codebase
64+
logger.info("[CODEBASE] Initializing codebase")
65+
codebase = cg.get_codebase("codegen-sh/Kevin-s-Adventure-Game")
66+
67+
# Code Agent
68+
logger.info("[CODE_AGENT] Initializing code agent")
69+
agent = CodeAgent(codebase=codebase)
70+
71+
logger.info("[CODE_AGENT] Running code agent")
72+
response = agent.run(event.text)
73+
74+
cg.slack.client.chat_postMessage(channel=event.channel, text=response, thread_ts=event.ts)
75+
76+
return {"message": "Mentioned", "received_text": event.text, "response": response}
77+
78+
79+
@cg.github.event("pull_request:labeled")
80+
def handle_pr(event: PullRequestLabeledEvent):
81+
logger.info("PR labeled")
82+
logger.info(f"PR head sha: {event.pull_request.head.sha}")
83+
codebase = cg.get_codebase("codegen-sh/Kevin-s-Adventure-Game")
84+
85+
# =====[ Check out commit ]=====
86+
# Might require fetch?
87+
logger.info("> Checking out commit")
88+
codebase.checkout(commit=event.pull_request.head.sha)
89+
90+
logger.info("> Getting files")
91+
file = codebase.get_file("README.md")
92+
93+
# =====[ Create PR comment ]=====
94+
create_pr_comment(codebase, event.pull_request.number, f"File content:\n```python\n{file.content}\n```")
95+
96+
return {"message": "PR event handled", "num_files": len(codebase.files), "num_functions": len(codebase.functions)}
97+
98+
99+
@cg.linear.event("Issue")
100+
def handle_issue(event: LinearEvent):
101+
logger.info(f"Issue created: {event}")
102+
codebase = cg.get_codebase("codegen-sh/Kevin-s-Adventure-Game")
103+
return {"message": "Linear Issue event", "num_files": len(codebase.files), "num_functions": len(codebase.functions)}
104+
105+
app = modal.App("codegen-test")
106+
107+
108+
@app.cls(image=base_image, secrets=[modal.Secret.from_dotenv()], enable_memory_snapshot=True)
109+
class CodegenFastApi:
110+
repo_org: str = modal.parameter()
111+
repo_name: str = modal.parameter()
112+
commit: str = modal.parameter(default="latest")
113+
114+
@modal.enter(snap=True)
115+
def load(self):
116+
logger.info("Preparing codegen fastapi app")
117+
self.modal_app = ModalCodegenApp(repo_org=self.repo_org, repo_name=self.repo_name, commit=self.commit)
118+
self.modal_app.setup()
119+
120+
@modal.enter(snap=False)
121+
def setup(self):
122+
logger.info("Setting up codegen fastapi app")
123+
124+
@modal.exit()
125+
def exit(self):
126+
pass
127+
128+
@modal.asgi_app()
129+
def fastapi_endpoint(self):
130+
logger.info("Serving FastAPI app from class method")
131+
return self.modal_app.cg.app
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import modal"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 2,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"REPO_URL = \"https://github.com/codegen-sh/codegen-sdk.git\"\n",
19+
"COMMIT_ID = \"26dafad2c319968e14b90806d42c6c7aaa627bb0\"\n",
20+
"\n",
21+
"# Create the base image with dependencies\n",
22+
"base_image = (\n",
23+
" modal.Image.debian_slim(python_version=\"3.13\")\n",
24+
" .apt_install(\"git\")\n",
25+
" .pip_install(\n",
26+
" # =====[ Codegen ]=====\n",
27+
" # \"codegen\",\n",
28+
" f\"git+{REPO_URL}@{COMMIT_ID}\",\n",
29+
" # =====[ Rest ]=====\n",
30+
" \"openai>=1.1.0\",\n",
31+
" \"fastapi[standard]\",\n",
32+
" \"slack_sdk\",\n",
33+
" )\n",
34+
")"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": 3,
40+
"metadata": {},
41+
"outputs": [],
42+
"source": [
43+
"app = modal.App(\"codegen-test\")\n"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 47,
49+
"metadata": {},
50+
"outputs": [
51+
{
52+
"data": {
53+
"text/plain": [
54+
"'https://codegen-sh-develop-rpatel--codegen-test-codegenfastapi-f-5f8240.modal.run'"
55+
]
56+
},
57+
"execution_count": 47,
58+
"metadata": {},
59+
"output_type": "execute_result"
60+
}
61+
],
62+
"source": [
63+
"\n",
64+
"CodegenFastApi = modal.Cls.from_name(app_name=\"codegen-test\", name=\"CodegenFastApi\", environment_name=\"rpatel\")\n",
65+
"codegen_app = CodegenFastApi(repo_org=\"codegen-sh\", repo_name=\"Kevin-s-Adventure-Game\")\n",
66+
"codegen_app.fastapi_endpoint.web_url"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": 26,
72+
"metadata": {},
73+
"outputs": [
74+
{
75+
"data": {
76+
"text/plain": [
77+
"{'_sync_original_4358152144': <modal.cls._Obj at 0x105b089e0>,\n",
78+
" '_sync_synchronizer': <synchronicity.synchronizer.Synchronizer at 0x103c41fd0>}"
79+
]
80+
},
81+
"execution_count": 26,
82+
"metadata": {},
83+
"output_type": "execute_result"
84+
}
85+
],
86+
"source": [
87+
"vars(app)"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": 45,
93+
"metadata": {},
94+
"outputs": [
95+
{
96+
"ename": "ExecutionError",
97+
"evalue": "The app has not been assigned on the function at this point",
98+
"output_type": "error",
99+
"traceback": [
100+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
101+
"\u001b[0;31mExecutionError\u001b[0m Traceback (most recent call last)",
102+
"Cell \u001b[0;32mIn[45], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mcodegen_app\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfastapi_endpoint\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mapp\u001b[49m\n",
103+
"File \u001b[0;32m~/dev/codegen-sdk/.venv/lib/python3.13/site-packages/synchronicity/synchronizer.py:592\u001b[0m, in \u001b[0;36mSynchronizer._wrap_proxy_method.<locals>.proxy_method\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 590\u001b[0m instance \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__dict__\u001b[39m[synchronizer_self\u001b[38;5;241m.\u001b[39m_original_attr]\n\u001b[1;32m 591\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 592\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mwrapped_method\u001b[49m\u001b[43m(\u001b[49m\u001b[43minstance\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 593\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m UserCodeException \u001b[38;5;28;01mas\u001b[39;00m uc_exc:\n\u001b[1;32m 594\u001b[0m uc_exc\u001b[38;5;241m.\u001b[39mexc\u001b[38;5;241m.\u001b[39m__suppress_context__ \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n",
104+
"File \u001b[0;32m~/dev/codegen-sdk/.venv/lib/python3.13/site-packages/synchronicity/synchronizer.py:482\u001b[0m, in \u001b[0;36mSynchronizer._wrap_callable.<locals>.f_wrapped\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 479\u001b[0m kwargs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_translate_in(kwargs)\n\u001b[1;32m 481\u001b[0m \u001b[38;5;66;03m# Call the function\u001b[39;00m\n\u001b[0;32m--> 482\u001b[0m res \u001b[38;5;241m=\u001b[39m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 484\u001b[0m \u001b[38;5;66;03m# Figure out if this is a coroutine or something\u001b[39;00m\n\u001b[1;32m 485\u001b[0m is_coroutine \u001b[38;5;241m=\u001b[39m inspect\u001b[38;5;241m.\u001b[39miscoroutine(res)\n",
105+
"File \u001b[0;32m~/dev/codegen-sdk/.venv/lib/python3.13/site-packages/modal/_functions.py:1155\u001b[0m, in \u001b[0;36m_Function.app\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1153\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"mdmd:hidden\"\"\"\u001b[39;00m\n\u001b[1;32m 1154\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_app \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m-> 1155\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m ExecutionError(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe app has not been assigned on the function at this point\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 1157\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_app\n",
106+
"\u001b[0;31mExecutionError\u001b[0m: The app has not been assigned on the function at this point"
107+
]
108+
}
109+
],
110+
"source": []
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": null,
115+
"metadata": {},
116+
"outputs": [],
117+
"source": [
118+
"from codegen.sdk.core.codebase import Codebase\n",
119+
"\n",
120+
"\n",
121+
"c = Codebase.from_repo('vercel/next.js', language='typescript')"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": null,
127+
"metadata": {},
128+
"outputs": [],
129+
"source": []
130+
}
131+
],
132+
"metadata": {
133+
"kernelspec": {
134+
"display_name": ".venv",
135+
"language": "python",
136+
"name": "python3"
137+
},
138+
"language_info": {
139+
"codemirror_mode": {
140+
"name": "ipython",
141+
"version": 3
142+
},
143+
"file_extension": ".py",
144+
"mimetype": "text/x-python",
145+
"name": "python",
146+
"nbconvert_exporter": "python",
147+
"pygments_lexer": "ipython3",
148+
"version": "3.13.0"
149+
}
150+
},
151+
"nbformat": 4,
152+
"nbformat_minor": 2
153+
}

src/codegen/cli/commands/serve/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ def create_app_module(file_path: Path) -> str:
7979
# Create a module that imports and exposes the app
8080
module_name = f"codegen_app_{file_path.stem}"
8181
module_code = f"""
82-
from {file_path.stem} import cg
83-
app = cg.app
82+
from {file_path.stem} import app
83+
app = app
8484
"""
8585
module_path = file_path.parent / f"{module_name}.py"
8686
module_path.write_text(module_code)

src/codegen/extensions/events/codegen_app.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,22 @@ def __init__(self, name: str, repos: Optional[list[str]] = None, modal_api_key:
3232
self.linear = Linear(self)
3333
self.slack = Slack(self)
3434
self.github = GitHub(self)
35+
self.repos = repos
3536

3637
# Initialize codebase cache
3738
self.codebases: dict[str, Codebase] = {}
3839

39-
# Parse initial repos if provided
40-
if repos:
41-
for repo in repos:
42-
self._parse_repo(repo)
4340

4441
# Register routes
4542
self._setup_routes()
4643

44+
45+
def parse_repos(self) -> None:
46+
# Parse initial repos if provided
47+
if self.repos:
48+
for repo in self.repos:
49+
self._parse_repo(repo)
50+
4751
def _parse_repo(self, repo_name: str) -> None:
4852
"""Parse a GitHub repository and cache it.
4953

0 commit comments

Comments
 (0)