Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Add smol dev webhook handling [WIP]

Tomas Valentaf2f9d6e3bb84
diff --git a/SMOL_DEPLOYMENT.md b/SMOL_DEPLOYMENT.md
index a3d3c34ce..16159acae 100644
--- a/SMOL_DEPLOYMENT.md
+++ b/SMOL_DEPLOYMENT.md
@@ -9,7 +9,7 @@
- Then we create a PR throught the GH API
- We then create agent deployment for the specific repo
- The necessary GH data are in the `auth` field in the deployments table
- - The OpenAI key is included in the `config` field
+ - The OpenAI key is included in the `config` field alongside the model configuration
- We also add informations about the PR that was created so we can update the branch connected to the deployment via git pushes in the agent dev environemnt
- During each agent run we authenticate with GH App token, pull the repo, make the changes necessary and then push the changes to the branch connected to the deployment
- On each comment to the PR we trigger a webhook that compiles the prompt from the PR description and comments and then triggers a new run of the smol agent
diff --git a/github/pullRequest.ts b/github/pullRequest.ts
new file mode 100644
index 000000000..bc1491f7f
--- /dev/null
+++ b/github/pullRequest.ts
@@ -0,0 +1,27 @@
+import getInstallationClient from './installationClient'
+
+export async function createPR({ installationID, repositoryID, title, body, owner, repo }: {
+ installationID: number,
+ repositoryID: number,
+ title: string,
+ body: string,
+ owner: string,
+ repo: string,
+}) {
+ const client = getInstallationClient({ installationID })
+
+ // TODO: Get default branch
+
+ // TODO: Create branch from default branch
+
+ // TODO: Do we need to create an empty commit for the PR?
+
+ client.pulls.create({
+ owner,
+ repo,
+ title,
+ body,
+ head: 'smol-dev:new-feature',
+ base: 'master',
+ })
+}
diff --git a/github/webhooks.ts b/github/webhooks.ts
index 868343338..e35d98c36 100644
--- a/github/webhooks.ts
+++ b/github/webhooks.ts
@@ -3,102 +3,83 @@ import {
Webhooks,
createNodeMiddleware,
} from '@octokit/webhooks'
-import { apps_content, apps } from 'database'
+import api from 'api-client/api'
-import { getAppContentFromRepo, getRepo } from './repoProcessing'
-import { prisma } from 'queries/prisma'
-import getClient from './installationClient'
-
-export const branchRefPrefix = 'refs/heads/'
+import { prisma } from 'db/prisma'
export async function getGitHubWebhooksMiddleware() {
const webhooks = new Webhooks({
secret: process.env.GITHUB_APP_WEBHOOK_SECRET!,
})
- webhooks.on('push', pushHandler)
- return createNodeMiddleware(webhooks, { path: '/api/github/webhook' })
-}
-export async function deployLatestRepoState({
- repositoryFullName,
- installationID,
- apps,
- branch,
-}: {
- repositoryFullName: string,
- installationID: number,
- branch: string,
- apps: Pick<apps, 'repository_path' | 'id'>[],
-}) {
- const [repositoryOwnerName, repositoryName] = repositoryFullName.split('/')
-
- if (!installationID) {
- throw new Error('InstallationID not found')
- }
+ webhooks.on('issue_comment', issueCommentHandler)
- const github = getClient({ installationID })
- const repo = await getRepo({
- github,
- repo: repositoryName,
- owner: repositoryOwnerName,
- ref: `${branchRefPrefix}${branch}`,
+ webhooks.on('pull_request', async (event) => {
+ event.payload.
})
- await Promise.all(apps.map(async app => {
- try {
- const content = await getAppContentFromRepo({
- dir: app.repository_path,
- repo,
- })
+ // TODO: Add handling for review comments
+ // webhooks.on('pull_request_review_comment', async (event) => {
+ // event.payload.
+ // })
- if (!content) throw new Error('No content found in the repo')
+ return createNodeMiddleware(webhooks, { path: '/api/github/webhook' })
+}
+
+async function getPromptFromIssues(issue: Issue): Promise<string> {
- await prisma.apps_content.upsert({
- create: {
- content: content as unknown as NonNullable<apps_content['content']>,
- app_id: app.id,
- },
- update: {
- content: content as unknown as NonNullable<apps_content['content']>,
- updated_at: new Date(),
- },
- where: {
- app_id: app.id,
- }
- })
- } catch (err) {
- console.error(`Error deploying app "${app.id}" content from repository "${repositoryFullName}"`, err)
- }
- }))
}
-const pushHandler: HandlerFunction<'push', unknown> = async (event) => {
+const interactWithDeployedSmolAgent = api.path('/deployments/{id}/interactions').method('post').create()
+
+const issueCommentHandler: HandlerFunction<'issue_comment', unknown> = async (event) => {
const { payload } = event
+
const installationID = payload.installation?.id
const repositoryID = payload.repository.id
- const branch = payload.ref.slice(branchRefPrefix.length)
+ // Every PR is also an issues - GH API endpoint for issues is used for the shared functionality
+ const issueID = payload.issue.id
if (!installationID) {
throw new Error('InstallationID not found')
}
- const apps = await prisma.apps.findMany({
+ const prompt = await getPromptFromIssues(payload.issue)
+
+ // TODO: Ignore GH comments made by Agent
+ const deployments = await prisma.deployments.findMany({
where: {
- repository_branch: {
- equals: branch,
- },
- github_repositories: {
- repository_id: repositoryID,
- },
+ AND: [
+ {
+ auth: {
+ path: ['github', 'installation_id'],
+ equals: installationID,
+ },
+ },
+ {
+ auth: {
+ path: ['github', 'repository_id'],
+ equals: repositoryID,
+ },
+ }
+ ],
},
})
- if (apps.length === 0) return
-
- await deployLatestRepoState({
- apps,
- branch,
- installationID,
- repositoryFullName: payload.repository.full_name,
- })
+ await Promise.allSettled(
+ deployments.map(
+ async deployment => interactWithDeployedSmolAgent({
+ id: deployment.id,
+ type: 'start',
+ data: {
+ instructions: {
+ Prompt: prompt,
+ AccessToken: '',
+ RepositoryURL: '',
+ Branch: '',
+ IssueID: issueID,
+ },
+ } as any,
+ })
+ ))
}