Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Fix PR comment handling

Tomas Valenta69861e277966
diff --git a/pages/api/agent/run.ts b/pages/api/agent/run.ts
index 151d29684..0dde6faa6 100644
--- a/pages/api/agent/run.ts
+++ b/pages/api/agent/run.ts
@@ -5,79 +5,93 @@ import { getGHInstallationClient } from 'github/installationClient'
import { addCommentToPR } from 'github/pullRequest'
import { DeploymentAuthData } from '.'
-
// Indicate that the agent state changed (run finished)
async function postRun(req: NextApiRequest, res: NextApiResponse) {
- const {
- deployment_id,
- prompt,
-
- } = req.body as { prompt: string, deployment_id: string }
-
- const header = req.headers.authorization
- // Extract the token from the header
- const token = header?.split(' ')[1]
- if (process.env.SECRET_TOKEN && token !== process.env.SECRET_TOKEN) {
- console.log('Invalid token', token, process.env.SECRET_TOKEN)
- throw new Error('Invalid token')
+ try {
+ const {
+ deployment_id,
+ prompt,
+ } = req.body as {
+ prompt: string,
+ deployment_id: string,
+ }
+
+ const header = req.headers.authorization
+ // Extract the token from the header
+ const token = header?.split(' ')[1]
+ if (process.env.SECRET_TOKEN && token !== process.env.SECRET_TOKEN) {
+ console.log('Invalid token', token, process.env.SECRET_TOKEN)
+ throw new Error('Invalid token')
+ }
+
+ const deployment = await prisma.deployments.update({
+ where: {
+ id: deployment_id,
+ },
+ data: {
+ last_finished_prompt: prompt,
+ },
+ })
+
+ const authData = deployment.auth as unknown as DeploymentAuthData
+ const client = getGHInstallationClient({ installationID: authData.github.installation_id })
+
+
+ // TODO: Should we maybe just edit the previous comment?
+ // Or should we do that only if the previous run was cancelled
+ await addCommentToPR({
+ body: 'Finished running the agent',
+ client,
+ owner: authData.github.owner,
+ repo: authData.github.repo,
+ pullNumber: authData.github.pull_number,
+ })
+
+ res.status(200).json({})
+ } catch (err: any) {
+ console.error(err)
+ res.status(500).json({ statusCode: 500, message: err.message })
}
-
- const deployment = await prisma.deployments.update({
- where: {
- id: deployment_id,
- },
- data: {
- last_finished_prompt: prompt,
- },
- })
-
- const authData = deployment.auth as unknown as DeploymentAuthData
- const client = getGHInstallationClient({ installationID: authData.github.installation_id })
-
-
- // TODO: Should we maybe just edit the previous comment?
- // Or should we do that only if the previous run was cancelled
- await addCommentToPR({
- body: 'Finished running the agent',
- client,
- owner: authData.github.owner,
- repo: authData.github.repo,
- pullNumber: authData.github.pull_number,
- })
}
// Indicate that the agent state changed (run cancelled)
async function deleteRun(req: NextApiRequest, res: NextApiResponse) {
- const {
- deployment_id,
- } = req.body as { deployment_id: string }
-
- const header = req.headers.authorization
- // Extract the token from the header
- const token = header?.split(' ')[1]
- if (process.env.SECRET_TOKEN && token !== process.env.SECRET_TOKEN) {
- console.log('Invalid token', token, process.env.SECRET_TOKEN)
- throw new Error('Invalid token')
+ try {
+ const {
+ deployment_id,
+ } = req.body as { deployment_id: string }
+
+ const header = req.headers.authorization
+ // Extract the token from the header
+ const token = header?.split(' ')[1]
+ if (process.env.SECRET_TOKEN && token !== process.env.SECRET_TOKEN) {
+ console.log('Invalid token', token, process.env.SECRET_TOKEN)
+ throw new Error('Invalid token')
+ }
+
+ const deployment = await prisma.deployments.findUniqueOrThrow({
+ where: {
+ id: deployment_id,
+ },
+ })
+
+ const authData = deployment.auth as unknown as DeploymentAuthData
+ const client = getGHInstallationClient({ installationID: authData.github.installation_id })
+
+ // Add comment about the run being cancelled
+ // TODO: Maybe just edit the previous comment?
+ await addCommentToPR({
+ body: 'Run cancelled',
+ client,
+ owner: authData.github.owner,
+ repo: authData.github.repo,
+ pullNumber: authData.github.pull_number,
+ })
+ res.status(200).json({})
+ } catch (err: any) {
+ console.error(err)
+ res.status(500).json({ statusCode: 500, message: err.message })
}
-
- const deployment = await prisma.deployments.findUniqueOrThrow({
- where: {
- id: deployment_id,
- },
- })
-
- const authData = deployment.auth as unknown as DeploymentAuthData
- const client = getGHInstallationClient({ installationID: authData.github.installation_id })
-
- // Add comment about the run being cancelled
- // TODO: Maybe just edit the previous comment?
- await addCommentToPR({
- body: 'Run cancelled',
- client,
- owner: authData.github.owner,
- repo: authData.github.repo,
- pullNumber: authData.github.pull_number,
- })
}
async function handler(