Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Add managing tags in DB; Fix tag filtering; Improve tag modal spacing

Tomas Valentaa884afdb328c
diff --git a/components/AgentChallengeTagModal.tsx b/components/AgentChallengeTagModal.tsx
index 954fc9200..58064cc26 100644
--- a/components/AgentChallengeTagModal.tsx
+++ b/components/AgentChallengeTagModal.tsx
@@ -34,7 +34,6 @@ function AgentChallengeTagModal({
left-1/2
top-1/2
w-[200px]
- py-2
border
border-[#6366F1]/50
shadow-lg
@@ -46,8 +45,7 @@ function AgentChallengeTagModal({
className="
w-full
bg-transparent
- px-2
- pb-4
+ p-4
text-sm
outline-none
border-b
diff --git a/components/AgentLogFilesList.tsx b/components/AgentLogFilesList.tsx
index 5927c3c50..6ac54f592 100644
--- a/components/AgentLogFilesList.tsx
+++ b/components/AgentLogFilesList.tsx
@@ -145,7 +145,6 @@ function AgentLogFilesList({
const newName = prompt('Enter a new name for the logs')
if (!newName) return
-
setIsRenamingLogUploadID(logUploadID)
try {
await renameLogUpload({ logUploadID, displayName: newName })
diff --git a/components/AgentPromptLogDetail.tsx b/components/AgentPromptLogDetail.tsx
index 6690c087d..654770508 100644
--- a/components/AgentPromptLogDetail.tsx
+++ b/components/AgentPromptLogDetail.tsx
@@ -10,15 +10,10 @@ const ReactJson = dynamic(import('react-json-view'), { ssr: false })
function handleJSONQuotes(content: string) {
try {
return JSON.parse(content)
- } catch (err) {
- console.log('JSON parse error', err, content)
- }
-
+ } catch (err) { }
try {
return JSON.parse(content.replace(/"/g, '\\"').replace(/'/g, '"'))
- } catch (err) {
- console.log('JSON parse error', err, content)
- }
+ } catch (err) { }
}
export interface Props {
diff --git a/components/Filesystem/Dir.tsx b/components/Filesystem/Dir.tsx
index 0b09b44c6..a9fb10f29 100644
--- a/components/Filesystem/Dir.tsx
+++ b/components/Filesystem/Dir.tsx
@@ -19,6 +19,8 @@ import {
DirProps,
NodeType,
} from 'filesystem'
+import { useAddLogUploadTag } from 'hooks/useAddLogUploadTag'
+import { useDeleteLogUploadTag } from 'hooks/useDeleteLogUploadTag'
@@ -53,6 +55,8 @@ function Dir({
// }
// }, [path])
+ const addTag = useAddLogUploadTag()
+ const deleteTag = useDeleteLogUploadTag()
function handleOnClick(e: any) {
fs.setIsDirExpanded(path, !isExpanded)
@@ -73,23 +77,31 @@ function Dir({
})
}
- function addNewTag(s: Severity) {
+ async function addNewTag(s: Severity) {
if (!newTagName) return
const newTag: AgentChallengeTagType = {
path,
text: newTagName,
severity: s,
}
- // TODO: Add to DB
- // TODO: If success add to metadata
setNewTagName('')
- setMetadata([newTag])
+ setMetadata([...tags, newTag])
+ try {
+ await addTag(metadata['logUploadID'], newTag)
+ } catch (e) {
+ setMetadata([])
+ console.error(e)
+ }
}
- function removeTag() {
- // TODO: Remove from DB
- // TODO: IF success remove from metadata
- setMetadata([])
+ async function removeTag(i: number) {
+ setMetadata(tags.filter((t: any) => t !== tags[i]))
+ try {
+ await deleteTag(metadata['logUploadID'], tags[i])
+ } catch (e) {
+ setMetadata(tags)
+ console.error(e)
+ }
}
const icon = isExpanded ? (
@@ -114,7 +126,7 @@ function Dir({
/>
<button
className="text-gray-400 hover:text-gray-400"
- onClick={removeTag}
+ onClick={() => removeTag(i)}
>
<X size={12} />
</button>
diff --git a/components/Filesystem/index.tsx b/components/Filesystem/index.tsx
index d6487aa5a..9063c341c 100644
--- a/components/Filesystem/index.tsx
+++ b/components/Filesystem/index.tsx
@@ -29,7 +29,7 @@ export interface Props {
rootPath: string
ignore?: string[]
onFiletreeClick?: (path: string, type: NodeType) => void
- fetchContent: (dirpath: string) => (FileInfo & { id?: string })[]
+ fetchContent: (dirpath: string) => (FileInfo & { id?: string, logUploadID: string })[]
expandedPath?: string
}
@@ -45,7 +45,7 @@ function Filesystem({
const fetchDirContent = useCallback(async (dirpath: string) => {
const files = fetchContent(dirpath)
const ns = files.map(f => (f.isDir
- ? new DirNode({ name: f.name })
+ ? new DirNode({ name: f.name, metadata: { logUploadID: f.logUploadID } })
: new FileNode({
name: f.name, metadata: {
href: {
diff --git a/components/UploadFiletree.tsx b/components/UploadFiletree.tsx
index b280e71da..6c0be3803 100644
--- a/components/UploadFiletree.tsx
+++ b/components/UploadFiletree.tsx
@@ -2,11 +2,11 @@ import { useCallback } from 'react'
import path from 'path-browserify'
import Filesystem, { FileInfo } from 'components/Filesystem'
-import { AgentChallengeTag, LiteDeployment, LiteLogFile, LiteLogUpload } from 'utils/agentLogs'
+import { LiteLogFile, LiteLogUpload } from 'utils/agentLogs'
import { FiletreeProvider } from 'hooks/useFiletree'
export interface Props {
- log: LiteLogUpload | LiteDeployment
+ log: LiteLogUpload
selectedLogFile?: LiteLogFile
}
@@ -14,12 +14,7 @@ function UploadTree({
log: logUpload,
selectedLogFile,
}: Props) {
- const fetchLogDirContent = useCallback<(dirpath: string) => (FileInfo & { id?: string })[]>(dirpath => {
- let tags: AgentChallengeTag[] = []
- if ((logUpload as any)['tags']) {
- tags = (logUpload as LiteLogUpload)['tags']
- }
-
+ const fetchLogDirContent = useCallback<(dirpath: string) => (FileInfo & { id?: string, logUploadID: string })[]>(dirpath => {
const currentDir = dirpath.slice(1)
const childFiles = logUpload
.log_files
@@ -47,18 +42,22 @@ function UploadTree({
return acc.add(childDir)
}, new Set<string>())
+ console.log('tags', logUpload.tags)
+
const content = [
...childFiles.map(f => ({
name: f.filename,
isDir: false,
id: f.id,
tags: [],
+ logUploadID: logUpload.id,
})),
...Array.from(childDirs).map(dir => ({
name: dir,
isDir: true,
// Filter out tags that don't belong to this dir.
- tags: tags.filter(t => t.path === dir),
+ tags: logUpload.tags.filter(t => t.path === `/${dir}`),
+ logUploadID: logUpload.id,
})),
]
return content
diff --git a/hooks/useAddLogUploadTag.ts b/hooks/useAddLogUploadTag.ts
new file mode 100644
index 000000000..d2da92581
--- /dev/null
+++ b/hooks/useAddLogUploadTag.ts
@@ -0,0 +1,33 @@
+import { useCallback } from 'react'
+import useSWRMutation from 'swr/mutation'
+import { AgentChallengeTag } from 'utils/agentLogs'
+
+export interface PostLogUploadTag {
+ id: string
+ tag: AgentChallengeTag
+}
+
+export interface PostLogUploadTagResponse { }
+
+async function handlePostLogUploadTag(url: string, { arg }: { arg: PostLogUploadTag }) {
+ const response = await fetch(url, {
+ method: 'POST',
+ body: JSON.stringify(arg),
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ })
+ return await response.json() as PostLogUploadTagResponse
+}
+
+export function useAddLogUploadTag() {
+ const {
+ trigger: upload,
+ } = useSWRMutation('/api/log_upload/tag', handlePostLogUploadTag)
+
+ return useCallback(async (logUploadID: string, tag: AgentChallengeTag) =>
+ upload({
+ id: logUploadID,
+ tag,
+ }), [upload])
+}
diff --git a/hooks/useDeleteLogUploadTag.ts b/hooks/useDeleteLogUploadTag.ts
new file mode 100644
index 000000000..e84ce6e3f
--- /dev/null
+++ b/hooks/useDeleteLogUploadTag.ts
@@ -0,0 +1,33 @@
+import { useCallback } from 'react'
+import useSWRMutation from 'swr/mutation'
+import { AgentChallengeTag } from 'utils/agentLogs'
+
+export interface DeleteLogUploadTag {
+ // Log Upload ID
+ id: string
+ tag: AgentChallengeTag
+}
+
+export interface DeleteLogUploadTagResponse { }
+
+async function handleDeleteLogUploadTag(url: string, { arg }: { arg: DeleteLogUploadTag }) {
+ const response = await fetch(url, {
+ method: 'DELETE',
+ body: JSON.stringify(arg),
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ })
+ return await response.json() as DeleteLogUploadTagResponse
+}
+
+export function useDeleteLogUploadTag() {
+ const {
+ trigger: remove,
+ } = useSWRMutation('/api/log_upload/tag', handleDeleteLogUploadTag)
+
+ return useCallback(async (logUploadID: string, tag: AgentChallengeTag) => remove({
+ id: logUploadID,
+ tag,
+ }), [remove])
+}
diff --git a/pages/api/log_upload/tag.ts b/pages/api/log_upload/tag.ts
new file mode 100644
index 000000000..df5dced79
--- /dev/null
+++ b/pages/api/log_upload/tag.ts
@@ -0,0 +1,122 @@
+import { createServerSupabaseClient } from '@supabase/auth-helpers-nextjs'
+import { NextApiRequest, NextApiResponse } from 'next'
+
+import { prisma } from 'db/prisma'
+import { serverCreds } from 'db/credentials'
+import { DeleteLogUploadTag } from 'hooks/useDeleteLogUploadTag'
+import { PostLogUploadTag } from 'hooks/useAddLogUploadTag'
+
+async function deleteLogUploadTag(req: NextApiRequest, res: NextApiResponse) {
+ const {
+ id,
+ tag,
+ } = req.body as DeleteLogUploadTag
+
+ 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 log = await prisma.log_uploads.findFirstOrThrow({
+ where: {
+ id,
+ projects: {
+ teams: {
+ users_teams: {
+ some: {
+ user_id: session.user.id,
+ },
+ },
+ },
+ },
+ },
+ })
+
+ await prisma.log_uploads.update({
+ where: {
+ id,
+ },
+ data: {
+ tags: (log.tags as any[]).filter(t => t.path !== tag.path || t.text !== tag.text),
+ },
+ })
+
+ res.status(200).json({})
+ } catch (err: any) {
+ console.error(err)
+ res.status(500).json({ statusCode: 500, message: err.message })
+ }
+}
+
+async function postLogUploadTag(req: NextApiRequest, res: NextApiResponse) {
+ const {
+ id,
+ tag,
+ } = req.body as PostLogUploadTag
+
+ 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 log = await prisma.log_uploads.findFirstOrThrow({
+ where: {
+ id,
+ projects: {
+ teams: {
+ users_teams: {
+ some: {
+ user_id: session.user.id,
+ },
+ },
+ },
+ },
+ },
+ })
+
+ await prisma.log_uploads.update({
+ where: {
+ id,
+ },
+ data: {
+ tags: [...log.tags as any[], tag],
+ },
+ })
+
+ res.status(200).json({})
+ } catch (err: any) {
+ console.error(err)
+ res.status(500).json({ statusCode: 500, message: err.message })
+ }
+}
+
+async function handler(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ if (req.method === 'POST') {
+ await postLogUploadTag(req, res)
+ return
+ }
+
+ if (req.method === 'DELETE') {
+ await deleteLogUploadTag(req, res)
+ return
+ }
+
+ res.setHeader('Allow', ['POST', 'DELETE'])
+ res.status(405).json({ statusCode: 405, message: 'Method Not Allowed' })
+ return
+}
+
+export default handler
diff --git a/pages/index.tsx b/pages/index.tsx
index 73d803ae5..6d2906dfe 100644
--- a/pages/index.tsx
+++ b/pages/index.tsx
@@ -6,7 +6,7 @@ import path from 'path'
import { prisma, projects, log_files } from 'db/prisma'
import { serverCreds } from 'db/credentials'
import DashboardHome from 'components/DashboardHome'
-import { AgentNextActionLog, AgentPromptLogs, LiteDeployment, LiteLogUpload } from 'utils/agentLogs'
+import { AgentChallengeTag, AgentNextActionLog, AgentPromptLogs, LiteDeployment, LiteLogUpload } from 'utils/agentLogs'
export interface Props {
projects: (projects & { log_uploads: LiteLogUpload[], deployments: LiteDeployment[] })[]
@@ -298,6 +298,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
.map<LiteLogUpload>(u => ({
...u,
log_files: u.log_files.map(formatLogFileContent),
+ tags: u.tags as unknown as AgentChallengeTag[],
}))
}))
},