Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Fix GH webhooks
Tomas Valentadde7d1717050
diff --git a/api-client/AgentConnection.ts b/api-client/AgentConnection.tsindex af0a5bd96..17e5e8566 100644--- a/api-client/AgentConnection.ts+++ b/api-client/AgentConnection.ts@@ -59,7 +59,7 @@ export class AgentConnection {this.opts.onStateChange(AgentRunState.None)await this.disconnect()} else {- console.error('Unhandler interaction request', data)+ console.error('Unhandled interaction request', data)}breakdefault:diff --git a/api-service/agent/base.py b/api-service/agent/base.pyindex 43592d354..d1d8c6323 100644--- a/api-service/agent/base.py+++ b/api-service/agent/base.py@@ -5,14 +5,14 @@ from pydantic import BaseModelfrom session.env import EnvVar# The schema for agent config and interaction data is serializable and-# the format for interations with the agent is based on a modified JSON-RPC 2.0 spec (should we use less modified JSON-RPC?).+# the format for interactions with the agent is based on a modified JSON-RPC 2.0 spec (should we use less modified JSON-RPC?).# This way is to have a schema for communication and a tools that can for example take# the implementation of AgentBase class and generate REST server that implements the schema# that then be deployed on our platform.# For local testing there can be a CLI that takes the implementation of AgentBase class-# and just starts the server and exposes the functionality with a premade web UI where you can test the agent immediately.+# and just starts the server and exposes the functionality with a pre-made web UI where you can test the agent immediately.# Later we can start supporting other ways to generate the server, for example a gRPC or WS server.# This is orthogonal to the environment where the agent is deployed.diff --git a/api-service/agent/smol_agent.py b/api-service/agent/smol_agent.pyindex 240b83de8..3feeb4874 100644--- a/api-service/agent/smol_agent.py+++ b/api-service/agent/smol_agent.py@@ -272,10 +272,14 @@ class SmolAgent(AgentBase):if playground is not None:playground.close()+ # TODO: Save the prompt as "last_finished_prompt" column to the deployments table in the databaseawait self.on_interaction_request(AgentInteractionRequest(interaction_id=str(uuid.uuid4()),type="done",+ data={+ "prompt": user_prompt,+ },))diff --git a/api-service/deployment/stateless.py b/api-service/deployment/stateless.pydeleted file mode 100644index 1feaf8c46..000000000--- a/api-service/deployment/stateless.py+++ /dev/null@@ -1,59 +0,0 @@-from typing import Any, Dict--from database.base import db-from deployment.manager import AgentDeploymentManager, AgentDeployment-from agent.from_template import get_agent_factory_from_template--class StatelessDeploymentManager(AgentDeploymentManager):- def __init__(self):- self._deployments: Dict[str, AgentDeployment] = {}-- async def create_deployment(- self,- id: str,- project_id: str,- config: Any,- ):- deployment = await AgentDeployment.from_factory(- id,- get_agent_factory_from_template(config["templateID"]),- project_id,- config,- )- try:- await db.create_deployment(deployment.id, project_id, config)- except:- await deployment.agent.stop()- raise-- self._deployments[deployment.id] = deployment- return deployment-- async def update_deployment(- self,- id: str,- project_id: str,- config: Any,- ):- try:- await self.remove_deployment(id)- except:- print("Failed to remove deployment", id)- pass- return await self.create_deployment(- id,- project_id,- config,- )-- async def remove_deployment(self, id: str):- deployment = await self.get_deployment(id)- await deployment.agent.stop()- await db.update_deployment(deployment.id, enabled=False)- del self._deployments[deployment.id]-- async def get_deployment(self, id: str):- return self._deployments[id]-- async def list_deployments(self):- return list(self._deployments.values())diff --git a/api-service/start.sh b/api-service/start.shindex 0411d6565..300fcabad 100755--- a/api-service/start.sh+++ b/api-service/start.sh@@ -2,12 +2,12 @@# Start playground apicd /playground && exec node lib/server.js &-+# Start python appcd /app && exec poetry run gunicorn --bind 0.0.0.0:$PORT --workers 1 --threads 12 --timeout 0 app:app -k uvicorn.workers.UvicornWorker &-+# Wait for any process to exitwait -n-+# Exit with status of process that exited firstexit $?diff --git a/github/pullRequest.ts b/github/pullRequest.tsindex 0a9b25baf..c76371768 100644--- a/github/pullRequest.ts+++ b/github/pullRequest.ts@@ -100,42 +100,46 @@ export async function createPR({}export async function getPromptFromPR({- issueID,+ issueNumber,client,repo,owner,+ body,}: {+ body?: string,owner: string,repo: string,client: GitHubClient,- issueID: number,+ // Number identifying the issue in the repo (this is not issueID)+ issueNumber: number,}): Promise<string> {const [issueResult,commentsResult,] = await Promise.all([- client.issues.get({- issue_number: issueID,+ body === undefined ? client.issues.get({+ issue_number: issueNumber,owner,repo,- }),+ }) : Promise.resolve(undefined),client.issues.listComments({- issue_number: issueID,+ issue_number: issueNumber,owner,repo,+ // TODO: Handle pagination+ page: 1,per_page: 100,}),])- // TODO: Check if issue body and comments are returned as markdown- const body = issueResult.data.body_text+ const prBody = issueResult?.data.body || bodyconst comments = commentsResult.data.filter(c => !c.performed_via_github_app)- .map(c => c.body_text)+ .map(c => c.body).join('\n')- return `${body}\n\n${comments}`+ return `${prBody}\n\n${comments}`}export async function getDeploymentsForPR({@@ -149,6 +153,7 @@ export async function getDeploymentsForPR({}) {return await prisma.deployments.findMany({where: {+ enabled: true,AND: [{auth: {@@ -200,6 +205,8 @@ export async function triggerSmolDevAgentRun({prompt: string,accessToken: string,}) {+ console.log('Triggering smol dev agent run:', prompt)+await interactWithAgent({id: deployment.id,type: 'start',diff --git a/github/webhooks.ts b/github/webhooks.tsindex 453a5f412..23c93458f 100644--- a/github/webhooks.ts+++ b/github/webhooks.ts@@ -17,7 +17,14 @@ export async function getGitHubWebhooksMiddleware() {webhooks.on('pull_request.edited', pullRequestEditHandler)// TODO: Add handling for review comments- return createNodeMiddleware(webhooks, { path: '/api/github/webhook' })+ return createNodeMiddleware(webhooks, {+ path: '/api/github/webhook', log: {+ debug: console.log,+ info: console.log,+ warn: console.log,+ error: console.error,+ }+ })}const pullRequestEditHandler: HandlerFunction<'pull_request.edited', unknown> = async (event) => {@@ -29,7 +36,8 @@ const pullRequestEditHandler: HandlerFunction<'pull_request.edited', unknown> =// Every PR is also an issues - GH API endpoint for issues is used for the shared functionalityconst issueID = payload.pull_request.id- const prompt = payload.pull_request.body as string+ const issueNumber = payload.pull_request.number+ const body = payload.pull_request.body || ''if (!installationID) {throw new Error('InstallationID not found')@@ -37,11 +45,17 @@ const pullRequestEditHandler: HandlerFunction<'pull_request.edited', unknown> =const client = getGHInstallationClient({ installationID })- const [deployments] = await Promise.all([+ const [prompt, deployments] = await Promise.all([+ getPromptFromPR({ client, issueNumber, repo, owner, body }),getDeploymentsForPR({ issueID, installationID, repositoryID }),])- await Promise.allSettled(+ if (deployments.length === 0) {+ console.log('No deployments found')+ return+ }++ const res = await Promise.allSettled(deployments.map(async deployment => {const accessToken = await getGHAccessToken({client,@@ -57,8 +71,19 @@ const pullRequestEditHandler: HandlerFunction<'pull_request.edited', unknown> =repo,})}))++ console.log('Trigger results', res)}+function getPRNumber(pr_url?: string) {+ const prNumber = pr_url?.split('/').pop()++ if (prNumber) {+ return parseInt(prNumber, 10)+ }+}++const issueCommentHandler: HandlerFunction<'issue_comment', unknown> = async (event) => {const { payload } = event@@ -67,20 +92,37 @@ const issueCommentHandler: HandlerFunction<'issue_comment', unknown> = async (evconst { owner, repo } = parseRepoName(payload.repository.full_name)// Every PR is also an issues - GH API endpoint for issues is used for the shared functionality- const issueID = payload.issue.id-+ // This issueID is not the PR issue id though, it's the issue id of the commentif (!installationID) {throw new Error('InstallationID not found')}const client = getGHInstallationClient({ installationID })+ const pullNumber = getPRNumber(payload.issue.pull_request?.url)+ if (!pullNumber) {+ console.log('Not a PR comment')+ return+ }++ const pr = await client.pulls.get({+ owner,+ repo,+ pull_number: pullNumber,+ })++ const body = pr.data.body || ''const [prompt, deployments] = await Promise.all([- getPromptFromPR({ client, issueID, repo, owner }),- getDeploymentsForPR({ issueID, installationID, repositoryID }),+ getPromptFromPR({ client, issueNumber: pullNumber, repo, owner, body }),+ getDeploymentsForPR({ issueID: pr.data.id, installationID, repositoryID }),])- await Promise.allSettled(+ if (deployments.length === 0) {+ console.log('No deployments found')+ return+ }++ const res = await Promise.allSettled(deployments.map(async deployment => {const accessToken = await getGHAccessToken({client,@@ -96,4 +138,6 @@ const issueCommentHandler: HandlerFunction<'issue_comment', unknown> = async (evcommitMessage: 'Update based on PR comments',})}))++ console.log('Trigger results', res)}diff --git a/package-lock.json b/package-lock.jsonindex bbce62ac6..2ff521b26 100644--- a/package-lock.json+++ b/package-lock.json@@ -12,7 +12,7 @@"@devbookhq/splitter": "^1.4.0","@octokit/auth-app": "^4.0.13","@octokit/rest": "^19.0.11",- "@octokit/webhooks": "^11.0.0",+ "@octokit/webhooks": "^11.1.0","@prisma/client": "^4.13.0","@supabase/auth-helpers-nextjs": "^0.5.5","@supabase/auth-helpers-react": "^0.3.1",@@ -35,6 +35,7 @@"immer": "^9.0.19","jsonpath-plus": "^7.2.0","lucide-react": "^0.161.0",+ "micro-cors": "^0.1.1","mustache": "^4.2.0","nanoid": "^4.0.1","next": "13.2.3",@@ -57,6 +58,7 @@},"devDependencies": {"@tailwindcss/typography": "^0.5.9",+ "@types/micro-cors": "^0.1.3","@types/mustache": "^4.2.2","@types/node": "^18.14.6","@types/react": "^18.0.28",@@ -732,13 +734,13 @@}},"node_modules/@octokit/webhooks": {- "version": "11.0.0",- "resolved": "https://registry.npmjs.org/@octokit/webhooks/-/webhooks-11.0.0.tgz",- "integrity": "sha512-+cn1O4+ItqXwc9Yo0DVfcfxPgeIRjV2AGDOkG6fwNRwXHw2IIUE/HEaNaHLUTiVCvXbL/pv6swqd+bM/Eylu5Q==",+ "version": "11.1.0",+ "resolved": "https://registry.npmjs.org/@octokit/webhooks/-/webhooks-11.1.0.tgz",+ "integrity": "sha512-Yw/e8Ja1Ewc+fxfa80OfeEea+Q/WLOh3rB1XqCfExi/+NuD5Z4IqpGCD1t1qR94wl7BdmPtJxA2PbtBJSxL0Yw==","dependencies": {"@octokit/request-error": "^3.0.0","@octokit/webhooks-methods": "^3.0.0",- "@octokit/webhooks-types": "6.11.0",+ "@octokit/webhooks-types": "7.0.2","aggregate-error": "^3.1.0"},"engines": {@@ -754,9 +756,9 @@}},"node_modules/@octokit/webhooks-types": {- "version": "6.11.0",- "resolved": "https://registry.npmjs.org/@octokit/webhooks-types/-/webhooks-types-6.11.0.tgz",- "integrity": "sha512-AanzbulOHljrku1NGfafxdpTCfw2ENaWzH01N2vqQM+cUFbk868Cgh0xylz0JIM9BoKbfI++bdD6EYX0Q/UTEw=="+ "version": "7.0.2",+ "resolved": "https://registry.npmjs.org/@octokit/webhooks-types/-/webhooks-types-7.0.2.tgz",+ "integrity": "sha512-lmxpnApoRaj8AEtHXp6Dso4IMHTL+YOphjCYYbu2G6BbXiqaO6tPQF5tO14ItpLkiwbgBiTj2m4xWGRkjAcT2A=="},"node_modules/@pkgr/utils": {"version": "2.3.1",@@ -1411,6 +1413,24 @@"@types/unist": "*"}},+ "node_modules/@types/micro": {+ "version": "7.3.7",+ "resolved": "https://registry.npmjs.org/@types/micro/-/micro-7.3.7.tgz",+ "integrity": "sha512-MFsX7eCj0Tg3TtphOQvANNvNtFpya+s/rYOCdV6o+DFjOQPFi2EVRbBALjbbgZTXUaJP1Q281MJiJOD40d0UxQ==",+ "dev": true,+ "dependencies": {+ "@types/node": "*"+ }+ },+ "node_modules/@types/micro-cors": {+ "version": "0.1.3",+ "resolved": "https://registry.npmjs.org/@types/micro-cors/-/micro-cors-0.1.3.tgz",+ "integrity": "sha512-f4aMXqEw9YjfdKX87m1LecvZJ2Mhz5maIHXjIvm5K6OTPe9auaTQwaFk4OZYS9zY6zdzfxqs2cEmwJAF7C9Y8A==",+ "dev": true,+ "dependencies": {+ "@types/micro": "^7.3.7"+ }+ },"node_modules/@types/ms": {"version": "0.7.31","resolved": "https://registry.npmjs.org/@types/ms/-/ms-0.7.31.tgz",@@ -5122,6 +5142,14 @@"node": ">= 8"}},+ "node_modules/micro-cors": {+ "version": "0.1.1",+ "resolved": "https://registry.npmjs.org/micro-cors/-/micro-cors-0.1.1.tgz",+ "integrity": "sha512-6WqIahA5sbQR1Gjexp1VuWGFDKbZZleJb/gy1khNGk18a6iN1FdTcr3Q8twaxkV5H94RjxIBjirYbWCehpMBFw==",+ "engines": {+ "node": ">=6"+ }+ },"node_modules/micromark": {"version": "3.1.0","resolved": "https://registry.npmjs.org/micromark/-/micromark-3.1.0.tgz",diff --git a/package.json b/package.jsonindex 1d4a28b6d..369960e2b 100644--- a/package.json+++ b/package.json@@ -42,7 +42,7 @@"@devbookhq/splitter": "^1.4.0","@octokit/auth-app": "^4.0.13","@octokit/rest": "^19.0.11",- "@octokit/webhooks": "^11.0.0",+ "@octokit/webhooks": "^11.1.0","@prisma/client": "^4.13.0","@supabase/auth-helpers-nextjs": "^0.5.5","@supabase/auth-helpers-react": "^0.3.1",@@ -65,6 +65,7 @@"immer": "^9.0.19","jsonpath-plus": "^7.2.0","lucide-react": "^0.161.0",+ "micro-cors": "^0.1.1","mustache": "^4.2.0","nanoid": "^4.0.1","next": "13.2.3",@@ -87,6 +88,7 @@},"devDependencies": {"@tailwindcss/typography": "^0.5.9",+ "@types/micro-cors": "^0.1.3","@types/mustache": "^4.2.2","@types/node": "^18.14.6","@types/react": "^18.0.28",diff --git a/pages/api/github/webhook.ts b/pages/api/github/webhook.tsindex d6b02afbd..5642b4fd1 100644--- a/pages/api/github/webhook.ts+++ b/pages/api/github/webhook.ts@@ -1,9 +1,22 @@import { NextApiRequest, NextApiResponse } from 'next'+import Cors from 'micro-cors'import { getGitHubWebhooksMiddleware } from 'github/webhooks'const webhookHandler = getGitHubWebhooksMiddleware()-export default async function handler(req: NextApiRequest, res: NextApiResponse) {+export const config = {+ api: {+ bodyParser: false,+ },+}++const cors = Cors({+ allowMethods: ['POST', 'HEAD'],+})++async function handler(req: NextApiRequest, res: NextApiResponse) {await (await webhookHandler)(req, res)}++export default cors(handler as any)diff --git a/prisma/schema.prisma b/prisma/schema.prismaindex 93e93d0de..f6645360e 100644--- a/prisma/schema.prisma+++ b/prisma/schema.prisma@@ -252,18 +252,19 @@ model users_teams {}model deployments {- id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid- created_at DateTime @default(now()) @db.Timestamptz(6)- logs Json[]- project_id String- route_id String?- state deployment_state?- url String?- logs_raw String? @default("")- config Json?- enabled Boolean @default(false)- auth Json?- projects projects @relation(fields: [project_id], references: [id], onDelete: NoAction, onUpdate: NoAction)+ id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid+ created_at DateTime @default(now()) @db.Timestamptz(6)+ logs Json[]+ project_id String+ route_id String?+ state deployment_state?+ url String?+ logs_raw String? @default("")+ config Json?+ enabled Boolean @default(false)+ auth Json?+ last_finished_prompt String?+ projects projects @relation(fields: [project_id], references: [id], onDelete: NoAction, onUpdate: NoAction)@@schema("public")}