Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Add delete logs handler

Tomas Valenta60ea5ba05241
diff --git a/hooks/useRemoveLogs.ts b/hooks/useRemoveLogs.ts
new file mode 100644
index 000000000..fd3c42183
--- /dev/null
+++ b/hooks/useRemoveLogs.ts
@@ -0,0 +1,31 @@
+import { useCallback } from 'react'
+import useSWRMutation from 'swr/mutation'
+
+export interface DeleteLogs {
+ id: string
+}
+
+export interface DeleteLogsResponse { }
+
+async function handleDeleteLogs(url: string, { arg }: { arg: DeleteLogs }) {
+ const response = await fetch(url, {
+ method: 'DELETE',
+ body: JSON.stringify(arg),
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ })
+ return await response.json() as DeleteLogsResponse
+}
+
+export function useRemoveLog() {
+ const {
+ trigger: remove,
+ } = useSWRMutation('/api/logs', handleDeleteLogs)
+
+ const deleteLogs = useCallback(async (logID: string) => remove({
+ id: logID,
+ }), [remove])
+
+ return deleteLogs
+}
diff --git a/hooks/useUploadLogs.ts b/hooks/useUploadLogs.ts
index 9322b9ef1..6db7cd2f0 100644
--- a/hooks/useUploadLogs.ts
+++ b/hooks/useUploadLogs.ts
@@ -1,7 +1,16 @@
import { useCallback } from 'react'
import useSWRMutation from 'swr/mutation'
-import { PostLogs, PostLogsResponse, RawFileLog } from 'utils/agentLogs'
+import { RawFileLog } from 'utils/agentLogs'
+
+export interface PostLogs {
+ logFiles: RawFileLog[]
+ projectID: string
+}
+
+export interface PostLogsResponse {
+ id: string
+}
async function handlePostLogs(url: string, { arg }: { arg: PostLogs }) {
const response = await fetch(url, {
diff --git a/pages/api/logs/index.ts b/pages/api/logs/index.ts
index 2242f630a..6263afe93 100644
--- a/pages/api/logs/index.ts
+++ b/pages/api/logs/index.ts
@@ -3,9 +3,84 @@ import { NextApiRequest, NextApiResponse } from 'next'
import { prisma } from 'db/prisma'
import { serverCreds } from 'db/credentials'
-import { PostLogs } from 'utils/agentLogs'
+import { PostLogs } from 'hooks/useUploadLogs'
+import { DeleteLogs } from 'hooks/useRemoveLogs'
-async function postAgent(req: NextApiRequest, res: NextApiResponse) {
+async function deleteLogs(req: NextApiRequest, res: NextApiResponse) {
+ const {
+ id,
+ } = req.body as DeleteLogs
+
+ try {
+ const supabase = createServerSupabaseClient({ req, res }, serverCreds)
+ const { data: { session } } = await supabase.auth.getSession()
+ if (!session) {
+ return res.status(401).json({
+ error: 'not_authenticated',
+ description: 'The user does not have an active session or is not authenticated',
+ })
+ }
+
+ const user = await prisma.auth_users.findUnique({
+ where: {
+ id: session.user.id,
+ },
+ include: {
+ users_teams: {
+ include: {
+ teams: {
+ include: {
+ projects: {
+ include: {
+ logs: {
+ where: {
+ id,
+ },
+ select: {
+ id: true,
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ })
+
+ if (!user) {
+ return res.status(401).json({
+ error: 'invalid_user',
+ })
+ }
+
+ const hasAccessToLogs = user
+ .users_teams
+ .flatMap(t => t.teams.projects)
+ .flatMap(p => p.logs)
+ .some(l => l.id === id)
+
+ if (!hasAccessToLogs) {
+ return res.status(401).json({
+ error: 'logs_does_not_belong_to_user',
+ })
+ }
+
+ await prisma.logs.delete({
+ where: {
+ id,
+ },
+ })
+
+ res.status(200).json({})
+ } catch (err: any) {
+ console.error(err)
+ res.status(500).json({ statusCode: 500, message: err.message })
+ }
+}
+
+async function postLogs(req: NextApiRequest, res: NextApiResponse) {
const {
logFiles,
projectID,
@@ -84,11 +159,16 @@ async function handler(
res: NextApiResponse,
) {
if (req.method === 'POST') {
- await postAgent(req, res)
+ await postLogs(req, res)
+ return
+ }
+
+ if (req.method === 'POST') {
+ await deleteLogs(req, res)
return
}
- res.setHeader('Allow', 'POST')
+ res.setHeader('Allow', ['POST', 'DELETE'])
res.status(405).json({ statusCode: 405, message: 'Method Not Allowed' })
return
}
diff --git a/utils/agentLogs.ts b/utils/agentLogs.ts
index 95dbc4763..377667095 100644
--- a/utils/agentLogs.ts
+++ b/utils/agentLogs.ts
@@ -42,12 +42,3 @@ export interface AssistantContext {
arguments: { [argName: string]: string }
}
}
-
-export interface PostLogs {
- logFiles: RawFileLog[]
- projectID: string
-}
-
-export interface PostLogsResponse {
- id: string
-}