Skip to content

armgit / E2B

Public Imported from GitHub
0

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

add ability to rename log uploads

Vasek Mlejnskya036ed5f8ea4
diff --git a/components/AgentLogFilesList.tsx b/components/AgentLogFilesList.tsx
index 916c7c17c..5e1dee040 100644
--- a/components/AgentLogFilesList.tsx
+++ b/components/AgentLogFilesList.tsx
@@ -7,13 +7,15 @@ import {
} from 'react'
import {
ChevronRight,
+ Edit,
} from 'lucide-react'
import clsx from 'clsx'
import { useRouter } from 'next/router'
import Splitter, { SplitDirection } from '@devbookhq/splitter'
import { log_files } from 'db/prisma'
-import { useUploadLogs } from 'hooks/useUploadLogs'
+import { useUploadLogUpload } from 'hooks/useUploadLogUpload'
+import { useRenameLogUpload } from 'hooks/useRenameLogUpload'
import LogFolderUploadButton from 'components/LogFolderUploadButton'
import {
LiteLogUpload,
@@ -38,10 +40,17 @@ function AgentLogFilesList({
const [selectedLogFile, setSelectedLogFile] = useState<LiteLogFile>()
const router = useRouter()
const fileInput = useRef<HTMLInputElement>(null)
- const uploadFiles = useUploadLogs(defaultProjectID)
+ const uploadFiles = useUploadLogUpload(defaultProjectID)
+ const renameLogUpload = useRenameLogUpload()
const [splitterSizes, setSplitterSizes] = useLocalStorage('log-file-list-splitter', [40, 60])
- const setSizes = useCallback((pairIdx: number, sizes: number[]) => {
+ // ID of the log upload that is being renamed.
+ const [renamingLogUploadID, setIsRenamingLogUploadID] = useState('')
+
+ // Just a helpful object to keep track of the renamed log uploads in the UI.
+ const [renamedLogUploads, setRenamedLogUploads] = useState<{ [key: string]: string }>({})
+
+ const setSizes = useCallback((_: number, sizes: number[]) => {
setSplitterSizes(sizes)
}, [setSplitterSizes])
@@ -132,6 +141,27 @@ function AgentLogFilesList({
}
}
+ async function handleRenameLogUpload(logUploadID: string) {
+ if (renamingLogUploadID) return
+
+ const newName = prompt('Enter a new name for the logs')
+ if (!newName) return
+
+
+ setIsRenamingLogUploadID(logUploadID)
+ try {
+ await renameLogUpload({ logUploadID, displayName: newName })
+ setRenamedLogUploads(val => ({
+ ...val,
+ [logUploadID]: newName,
+ }))
+ } catch (err: any) {
+ console.error(`failed to rename log upload ${logUploadID}`)
+ console.error(err)
+ }
+ setIsRenamingLogUploadID('')
+ }
+
return (
<main className="overflow-hidden flex flex-col max-h-full flex-1">
<input
@@ -187,32 +217,57 @@ function AgentLogFilesList({
className="flex flex-col space-y-2 flex-1"
>
<div className="flex items-center space-x-2">
- <div
+ <button
className={clsx(
- 'p-1 cursor-pointer hover:bg-gray-700 transition-all rounded-md',
+ 'h-6 px-1 cursor-pointer hover:bg-gray-700 transition-all rounded-md',
openLogUploads[logUpload.id] && 'bg-gray-700',
!openLogUploads[logUpload.id] && 'bg-gray-800',
)}
onClick={() => toggleLogUpload(logUpload.id)}
>
- <ChevronRight size={15} className={clsx(
+ <ChevronRight size={14} className={clsx(
'text-gray-400',
'transition-all',
'select-none',
openLogUploads[logUpload.id] && 'rotate-90',
)} />
- </div>
+ </button>
+
+ <button
+ className={clsx(
+ 'h-6 w-20 flex items-center justify-center space-x-1 px-1 bg-gray-800 transition-all rounded-md text-gray-400 text-xs',
+ renamingLogUploadID === logUpload.id && 'hover:bg-gray-800 cursor-not-allowed',
+ renamingLogUploadID !== logUpload.id && 'cursor-pointer hover:bg-gray-700',
+ )}
+ onClick={() => handleRenameLogUpload(logUpload.id)}
+ >
+ {renamingLogUploadID === logUpload.id ? (
+ <Spinner
+ className="text-gray-400 transition-all select-none"
+ />
+ ) : (
+ <Edit
+ className="text-gray-400 transition-all select-none"
+ size={14}
+ />
+ )}
+ <span>Rename</span>
+ </button>
<span
className={clsx(
'text-sm',
'font-semibold',
- 'text-gray-500',
+ 'text-gray-200',
'whitespace-nowrap',
)}
// This prevents hydration warning for timestamps rendered via SSR
suppressHydrationWarning
>
- Upload from {logUpload.created_at.toLocaleString()}
+ {logUpload.display_name && !renamedLogUploads[logUpload.id] && logUpload.display_name}
+ {logUpload.display_name && renamedLogUploads[logUpload.id] && renamedLogUploads[logUpload.id]}
+ {!logUpload.display_name && (
+ `Upload from ${logUpload.created_at.toLocaleString()}`
+ )}
</span>
</div>
diff --git a/hooks/useRemoveLogs.ts b/hooks/useDeleteLogUpload.ts
similarity index 53%
rename from hooks/useRemoveLogs.ts
rename to hooks/useDeleteLogUpload.ts
index b5c851460..11be32989 100644
--- a/hooks/useRemoveLogs.ts
+++ b/hooks/useDeleteLogUpload.ts
@@ -1,13 +1,14 @@
import { useCallback } from 'react'
import useSWRMutation from 'swr/mutation'
-export interface DeleteLogs {
+export interface DeleteLogUpload {
+ // Log Upload ID
id: string
}
-export interface DeleteLogsResponse { }
+export interface DeleteLogUploadResponse { }
-async function handleDeleteLogs(url: string, { arg }: { arg: DeleteLogs }) {
+async function handleDeleteLogUpload(url: string, { arg }: { arg: DeleteLogUpload }) {
const response = await fetch(url, {
method: 'DELETE',
body: JSON.stringify(arg),
@@ -15,13 +16,13 @@ async function handleDeleteLogs(url: string, { arg }: { arg: DeleteLogs }) {
'Content-Type': 'application/json',
},
})
- return await response.json() as DeleteLogsResponse
+ return await response.json() as DeleteLogUploadResponse
}
-export function useDeleteLogs() {
+export function useDeleteLogUpload() {
const {
trigger: remove,
- } = useSWRMutation('/api/logs', handleDeleteLogs)
+ } = useSWRMutation('/api/log_upload', handleDeleteLogUpload)
return useCallback(async (logUploadID: string) => remove({
id: logUploadID,
diff --git a/hooks/useRenameLogUpload.ts b/hooks/useRenameLogUpload.ts
new file mode 100644
index 000000000..ac23e5fd3
--- /dev/null
+++ b/hooks/useRenameLogUpload.ts
@@ -0,0 +1,29 @@
+import { useCallback } from 'react'
+import useSWRMutation from 'swr/mutation'
+
+export interface PatchLogUpload {
+ logUploadID: string
+ displayName: string
+}
+
+export interface PatchLogUploadResponse { }
+
+async function handlePatchLogUpload(url: string, { arg }: { arg: PatchLogUpload }) {
+ const response = await fetch(url, {
+ method: 'PATCH',
+ body: JSON.stringify(arg),
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ })
+ return await response.json() as PatchLogUploadResponse
+}
+
+export function useRenameLogUpload() {
+ const {
+ trigger: rename,
+ } = useSWRMutation('/api/log_upload', handlePatchLogUpload)
+
+ return useCallback(async (args: PatchLogUpload) =>
+ rename(args), [rename])
+}
\ No newline at end of file
diff --git a/hooks/useUploadLogs.ts b/hooks/useUploadLogUpload.ts
similarity index 65%
rename from hooks/useUploadLogs.ts
rename to hooks/useUploadLogUpload.ts
index 295bea8d5..c8d94a3ac 100644
--- a/hooks/useUploadLogs.ts
+++ b/hooks/useUploadLogUpload.ts
@@ -4,16 +4,16 @@ import useSWRMutation from 'swr/mutation'
export interface FileUpload extends Pick<log_files, 'content' | 'filename' | 'relativePath' | 'size' | 'last_modified' | 'type'> { }
-export interface PostLogs {
+export interface PostLogUpload {
logFiles: FileUpload[]
projectID: string
}
-export interface PostLogsResponse {
+export interface PostLogUploadResponse {
id: string
}
-async function handlePostLogs(url: string, { arg }: { arg: PostLogs }) {
+async function handlePostLogUpload(url: string, { arg }: { arg: PostLogUpload }) {
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify(arg),
@@ -21,17 +21,17 @@ async function handlePostLogs(url: string, { arg }: { arg: PostLogs }) {
'Content-Type': 'application/json',
},
})
- return await response.json() as PostLogsResponse
+ return await response.json() as PostLogUploadResponse
}
-export function useUploadLogs(projectID: string) {
+export function useUploadLogUpload(projectID: string) {
const {
trigger: upload,
- } = useSWRMutation('/api/logs', handlePostLogs)
+ } = useSWRMutation('/api/log_upload', handlePostLogUpload)
return useCallback(async (logFiles: FileUpload[]) =>
upload({
logFiles,
projectID,
}), [projectID, upload])
-}
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/pages/api/logs/index.ts b/pages/api/log_upload/index.ts
similarity index 61%
rename from pages/api/logs/index.ts
rename to pages/api/log_upload/index.ts
index 7c07f48b3..dd592b0d3 100644
--- a/pages/api/logs/index.ts
+++ b/pages/api/log_upload/index.ts
@@ -3,14 +3,14 @@ import { NextApiRequest, NextApiResponse } from 'next'
import { prisma } from 'db/prisma'
import { serverCreds } from 'db/credentials'
-import { PostLogs } from 'hooks/useUploadLogs'
-import { DeleteLogs } from 'hooks/useRemoveLogs'
-// import { GetLogFile } from 'hooks/useLogFile'
+import { PostLogUpload } from 'hooks/useUploadLogUpload'
+import { DeleteLogUpload } from 'hooks/useDeleteLogUpload'
+import { PatchLogUpload } from 'hooks/useRenameLogUpload'
-async function deleteLogs(req: NextApiRequest, res: NextApiResponse) {
+async function deleteLogUpload(req: NextApiRequest, res: NextApiResponse) {
const {
id,
- } = req.body as DeleteLogs
+ } = req.body as DeleteLogUpload
try {
const supabase = createServerSupabaseClient({ req, res }, serverCreds)
@@ -78,11 +78,11 @@ async function deleteLogs(req: NextApiRequest, res: NextApiResponse) {
}
}
-async function postLogs(req: NextApiRequest, res: NextApiResponse) {
+async function postLogUpload(req: NextApiRequest, res: NextApiResponse) {
const {
logFiles,
projectID,
- } = req.body as PostLogs
+ } = req.body as PostLogUpload
try {
const supabase = createServerSupabaseClient({ req, res }, serverCreds)
@@ -159,21 +159,101 @@ async function postLogs(req: NextApiRequest, res: NextApiResponse) {
}
}
+
+async function patchLogUpload(req: NextApiRequest, res: NextApiResponse) {
+ const {
+ logUploadID,
+ displayName,
+ } = req.body as PatchLogUpload
+
+ 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: {
+ log_uploads: {
+ where: {
+ id: logUploadID,
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ })
+
+ if (!user) {
+ return res.status(401).json({
+ error: 'invalid_user',
+ })
+ }
+
+ const hasAccessToLogs = user
+ .users_teams
+ .flatMap(t => t.teams.projects)
+ .flatMap(p => p.log_uploads)
+ .some(l => l.id === logUploadID)
+
+ if (!hasAccessToLogs) {
+ return res.status(401).json({
+ error: 'logs_does_not_belong_to_user',
+ })
+ }
+
+ await prisma.log_uploads.update({
+ where: {
+ id: logUploadID,
+ },
+ data: {
+ display_name: displayName,
+ },
+ })
+ 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 postLogs(req, res)
+ await postLogUpload(req, res)
return
}
if (req.method === 'DELETE') {
- await deleteLogs(req, res)
+ await deleteLogUpload(req, res)
+ return
+ }
+
+ if (req.method === 'PATCH') {
+ await patchLogUpload(req, res)
return
}
- res.setHeader('Allow', ['GET', 'POST', 'DELETE'])
+ res.setHeader('Allow', ['POST', 'DELETE', 'PATCH'])
res.status(405).json({ statusCode: 405, message: 'Method Not Allowed' })
return
}
diff --git a/pages/index.tsx b/pages/index.tsx
index 3762f68c2..94e69789c 100644
--- a/pages/index.tsx
+++ b/pages/index.tsx
@@ -8,6 +8,12 @@ import { serverCreds } from 'db/credentials'
import DashboardHome from 'components/DashboardHome'
import { AgentNextActionLog, AgentPromptLogs, LiteLogUpload } from 'utils/agentLogs'
+export interface Props {
+ projects: (projects & { log_uploads: LiteLogUpload[], deployments: deployments[] })[]
+ defaultProjectID: string
+}
+
+
function getLastTwoDirsAndFile(fullPath: string): string {
const fileName = path.basename(fullPath)
const dirName = path.dirname(fullPath)
@@ -103,6 +109,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
include: {
log_uploads: {
select: {
+ display_name: true,
id: true,
created_at: true,
log_files: {
@@ -231,11 +238,6 @@ export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
}
}
-export interface Props {
- projects: (projects & { log_uploads: LiteLogUpload[], deployments: deployments[] })[]
- defaultProjectID: string
-}
-
function Home({ projects, defaultProjectID }: Props) {
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 4453658d4..640fb1257 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -340,11 +340,12 @@ model log_files {
/// This model contains row level security and requires additional setup for migrations. Visit https://pris.ly/d/row-level-security for more info.
model log_uploads {
- id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
- created_at DateTime @default(now()) @db.Timestamptz(6)
- project_id String?
- log_files log_files[]
- projects projects? @relation(fields: [project_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
+ id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
+ created_at DateTime @default(now()) @db.Timestamptz(6)
+ project_id String?
+ display_name String? @default("")
+ log_files log_files[]
+ projects projects? @relation(fields: [project_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
@@schema("public")
}