Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Add deployment management [WIP]; Add logs [WIP]

Tomas Valenta9088991ed226
diff --git a/components/Editor/Logs.tsx b/components/Editor/Logs.tsx
new file mode 100644
index 000000000..f1bc1ef3b
--- /dev/null
+++ b/components/Editor/Logs.tsx
@@ -0,0 +1,9 @@
+import { useDeployment } from 'hooks/useDeployment';
+
+export interface Logs {
+
+}
+
+function Logs() {
+
+}
\ No newline at end of file
diff --git a/components/Editor/index.tsx b/components/Editor/index.tsx
index df8558bfb..ce83af5bf 100644
--- a/components/Editor/index.tsx
+++ b/components/Editor/index.tsx
@@ -13,6 +13,7 @@ import Button from '../Button'
import ConnectionLine from './ConnectionLine'
import AddBlockButton from './AddBlockButton'
import { nanoid } from 'nanoid'
+import { api_deployments } from '@prisma/client'
const selector = (state: State) => ({
blocks: state.blocks,
@@ -23,7 +24,9 @@ const selector = (state: State) => ({
changeMethod: state.changeMethod,
})
-export interface Props { }
+export interface Props {
+ deployment: api_deployments
+}
async function handlePostGenerate(url: string, { arg }: {
arg: {
@@ -44,9 +47,12 @@ async function handlePostGenerate(url: string, { arg }: {
}).then(r => r.json())
}
-export default function Editor({ }: Props) {
+export default function Editor({ deployment }: Props) {
const useStore = getStoreContext()
+ const realtimeDeployment = useDeployment()
+
+
const {
addBlock,
blocks,
diff --git a/components/ItemList/Item.tsx b/components/ItemList/Item.tsx
new file mode 100644
index 000000000..f12a4ece3
--- /dev/null
+++ b/components/ItemList/Item.tsx
@@ -0,0 +1,150 @@
+import clsx from 'clsx'
+import Link from 'next/link'
+import { MouseEvent, useEffect, useState } from 'react'
+
+import SpinnerIcon from 'components/icons/Spinner'
+import Text from 'components/typography/Text'
+
+export interface ItemSetup {
+ id: string
+ title: string
+ created_at: Date
+ icon?: React.ReactNode
+ path: string
+ type: string
+}
+
+export interface Props {
+ item: ItemSetup
+ deleteItem?: () => Promise<void>
+}
+
+function Item({ item, deleteItem }: Props) {
+ const [confirmDelete, setConfirmDelete] = useState(false)
+ const [isDeleting, setIsDeleting] = useState(false)
+
+ useEffect(
+ function expireConfirm() {
+ if (confirmDelete) {
+ const cleanup = setTimeout(() => setConfirmDelete(false), 4000)
+ return () => {
+ clearTimeout(cleanup)
+ }
+ }
+ },
+ [confirmDelete],
+ )
+
+ async function handleDelete(
+ e: MouseEvent<HTMLButtonElement, globalThis.MouseEvent>,
+ ) {
+ if (!deleteItem) return
+
+ e.stopPropagation()
+ e.preventDefault()
+
+ if (confirmDelete && !isDeleting) {
+ setIsDeleting(true)
+ try {
+ await deleteItem()
+ } catch (err) {
+ const msg = err instanceof Error ? err.message : String(err)
+ console.error(`Error deleting item: ${msg}`)
+ } finally {
+ setIsDeleting(false)
+ }
+ } else {
+ setConfirmDelete(true)
+ }
+ }
+
+ return (
+ <Link
+ className="group flex items-center justify-between space-x-4 rounded"
+ href={{
+ pathname: item.path,
+ query: {
+ id: item.id,
+ },
+ }}
+ >
+ <div className="flex items-center space-x-4 truncate">
+ <div className="
+ m-1
+ flex
+ h-12
+ w-12
+ shrink-0
+ items-center
+ justify-center
+ rounded-lg border
+ border-slate-200
+ group-hover:border-green-800
+ text-slate-300
+ transition-all
+ group-hover:bg-green-400
+ group-hover:text-green-800
+ "
+ >
+ {item.icon}
+ </div>
+ <div
+ className="flex flex-col"
+ >
+ <Text
+ className="text-slate-600 transition-all group-hover:text-green-800"
+ size={Text.size.S2}
+ text={item.title}
+ />
+ <div className="flex space-x-1 text-slate-300 transition-all group-hover:text-slate-400">
+ <Text
+ size={Text.size.S3}
+ text={item.type}
+ />
+ <Text
+ size={Text.size.S3}
+ text="-"
+ />
+ <Text
+ size={Text.size.S3}
+ text={item.created_at.toLocaleString()}
+ suppressHydrationWarning
+ />
+ </div>
+ </div>
+ </div>
+ {deleteItem &&
+ <button
+ className={clsx(
+ 'flex items-center justify-center rounded border border-transparent px-3 py-1.5',
+ {
+ 'border-red-600 hover:bg-red-600/10': confirmDelete,
+ },
+ )}
+ onClick={handleDelete}
+ >
+ {isDeleting && (
+ <Text
+ className="whitespace-nowrap text-red-600"
+ icon={<SpinnerIcon className="text-red-600" />}
+ size={Text.size.S3}
+ text="Deleting..."
+ />
+ )}
+ {!isDeleting && (
+ <Text
+ size={Text.size.S3}
+ text={confirmDelete ? 'Confirm delete' : 'Delete'}
+ className={clsx('whitespace-nowrap hover:text-red-600', {
+ 'text-red-600': confirmDelete,
+ 'text-slate-300': !confirmDelete,
+ })}
+ />
+ )}
+ </button>
+ }
+ </Link>
+ )
+}
+
+export default Item
\ No newline at end of file
diff --git a/components/ItemList/index.tsx b/components/ItemList/index.tsx
new file mode 100644
index 000000000..8aceddb28
--- /dev/null
+++ b/components/ItemList/index.tsx
@@ -0,0 +1,45 @@
+import React, { useMemo } from 'react'
+
+import Item, { ItemSetup } from './Item'
+
+export interface Props {
+ items: ItemSetup[]
+ deleteItem?: (id: string) => Promise<void>
+}
+
+function AppList({ items, deleteItem }: Props) {
+ const sorted = useMemo(
+ () =>
+ items.sort(
+ (a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime(),
+ ),
+ [items],
+ )
+
+ return (
+ <div
+ className="
+ scroller
+ flex
+ max-w-[800px]
+ flex-1
+ flex-col
+ space-y-2
+ overflow-auto
+ pr-2
+ "
+ >
+ {sorted.map(i => (
+ <div
+ className="flex flex-col space-y-2"
+ key={i.id}
+ >
+ <Item item={i} deleteItem={deleteItem ? () => deleteItem(i.id) : undefined} />
+ <div className="border-b border-slate-200" />
+ </div>
+ ))}
+ </div>
+ )
+}
+
+export default AppList
\ No newline at end of file
diff --git a/db/supabase.ts b/db/supabase.ts
index ccfeb5e64..226d99f0e 100644
--- a/db/supabase.ts
+++ b/db/supabase.ts
@@ -11,21 +11,27 @@ export interface Database {
Tables: {
api_deployments: {
Row: {
+ code: Json | null
created_at: string | null
data: Json
id: number
+ logs: Json | null
team_id: string | null
}
Insert: {
+ code?: Json | null
created_at?: string | null
data: Json
id?: number
+ logs?: Json | null
team_id?: string | null
}
Update: {
+ code?: Json | null
created_at?: string | null
data?: Json
id?: number
+ logs?: Json | null
team_id?: string | null
}
}
diff --git a/hooks/useDeployment.tsx b/hooks/useDeployment.tsx
new file mode 100644
index 000000000..852dad776
--- /dev/null
+++ b/hooks/useDeployment.tsx
@@ -0,0 +1,29 @@
+import { api_deployments } from '@prisma/client'
+import { useSupabaseClient } from '@supabase/auth-helpers-react'
+import { useState, useEffect } from 'react'
+
+export function useDeployment(deployment: api_deployments) {
+ const [latest, setLatest] = useState<api_deployments>(deployment)
+ const client = useSupabaseClient()
+
+ useEffect(function subscribe() {
+ const sub = client.channel('any')
+ .on('postgres_changes',
+ {
+ event: 'UPDATE',
+ schema: 'public',
+ table: 'api_deployments',
+ filter: `id=eq.${deployment.id}`,
+ }, payload => {
+ console.log('Change received!', payload)
+ setLatest(l => ({ ...l, code: payload.new.code, logs: payload.new.logs }))
+ })
+ .subscribe()
+
+ return () => {
+ sub.unsubscribe()
+ }
+ }, [deployment, client])
+
+ return latest
+}
diff --git a/pages/[deploymentID].tsx b/pages/[deploymentID].tsx
new file mode 100644
index 000000000..9802107fe
--- /dev/null
+++ b/pages/[deploymentID].tsx
@@ -0,0 +1,91 @@
+import type { GetServerSideProps } from 'next'
+import { createServerSupabaseClient } from '@supabase/auth-helpers-nextjs'
+import { useSupabaseClient } from '@supabase/auth-helpers-react'
+import type { ParsedUrlQuery } from 'querystring'
+
+import { prisma, api_deployments } from 'db/prisma'
+import DeploymentEditor from 'components/Editor'
+import { StoreProvider } from 'state/StoreProvider'
+
+interface PathProps extends ParsedUrlQuery {
+ deploymentID: string
+}
+
+export const getServerSideProps: GetServerSideProps<Props, PathProps> = async (ctx) => {
+ const deploymentID = parseInt(ctx.params?.deploymentID || '')
+
+ if (!deploymentID) {
+ return {
+ notFound: true,
+ }
+ }
+
+ const supabase = createServerSupabaseClient(ctx)
+ const {
+ data: { session },
+ } = await supabase.auth.getSession()
+
+ if (!session) {
+ return {
+ redirect: {
+ destination: '/sign',
+ permanent: false,
+ },
+ }
+ }
+
+ const user = await prisma.auth_users.findUnique({
+ where: {
+ id: session.user.id,
+ },
+ select: {
+ users_teams: {
+ select: {
+ teams: {
+ include: {
+ api_deployments: {
+ where: {
+ id: {
+ equals: deploymentID,
+ },
+ }
+ }
+ },
+ },
+ },
+ },
+ },
+ })
+
+ const deployment = user?.users_teams.flatMap(t => t.teams.api_deployments).find(d => d.id === BigInt(deploymentID))
+ if (!deployment) {
+ return {
+ notFound: true,
+ }
+ }
+
+ return {
+ props: {
+ deployment,
+ }
+ }
+}
+
+interface Props {
+ deployment: api_deployments
+}
+
+function EditorPage({ deployment }: Props) {
+ const client = useSupabaseClient()
+
+ return (
+ <StoreProvider
+ client={client}
+ deployment={deployment}
+ >
+ <DeploymentEditor />
+ </StoreProvider>
+ )
+}
+
+export default EditorPage
diff --git a/pages/index.tsx b/pages/index.tsx
index 287258f8a..539e844c1 100644
--- a/pages/index.tsx
+++ b/pages/index.tsx
@@ -1,11 +1,13 @@
import type { GetServerSideProps } from 'next'
+import { LayoutGrid, Plus } from 'lucide-react'
import { createServerSupabaseClient } from '@supabase/auth-helpers-nextjs'
-import { useSupabaseClient } from '@supabase/auth-helpers-react'
+import useSWRMutation from 'swr/mutation'
+import { useRouter } from 'next/router'
-import { prisma, api_deployments } from 'db/prisma'
-import { notEmpty } from 'utils/notEmpty'
-import DeploymentEditor from 'components/Editor'
-import { StoreProvider } from 'state/StoreProvider'
+import ItemList from 'components/ItemList'
+import Text from 'components/typography/Text'
+import { api_deployments, prisma } from 'db/prisma'
+import Button from 'components/Button'
export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
const supabase = createServerSupabaseClient(ctx)
@@ -26,19 +28,11 @@ export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
where: {
id: session.user.id,
},
- select: {
+ include: {
users_teams: {
- where: {
+ include: {
teams: {
- is_default: {
- equals: true,
- },
- },
- },
- select: {
- teams: {
- select: {
- id: true,
+ include: {
api_deployments: true,
},
}
@@ -47,18 +41,13 @@ export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
},
})
- const hasDefaultTeam = user?.users_teams.some(t => t.teams)
+ const hasDefaultTeam = user?.users_teams.some(t => t.teams.is_default)
if (!hasDefaultTeam) {
// User is one of the old users without default team - create default team.
const team = await prisma.teams.create({
data: {
name: session.user.email || session.user.id,
is_default: true,
- api_deployments: {
- create: {
- data: {},
- },
- },
users_teams: {
create: {
users: {
@@ -74,62 +63,111 @@ export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
},
})
- if (!team.api_deployments) {
- return {
- notFound: true
- }
- }
-
return {
props: {
- deployment: team.api_deployments,
+ deployments: team.api_deployments,
}
}
}
-
- const apiDeploymentsFromDefaultTeam = user
- .users_teams
- .flatMap(t => t.teams.api_deployments)
- .filter(notEmpty)
-
- if (apiDeploymentsFromDefaultTeam.length > 0) {
- return {
- props: {
- deployment: apiDeploymentsFromDefaultTeam[0],
- }
- }
- }
-
- const defaultDeployment = await prisma.api_deployments.create({
- data: {
- data: {},
- team_id: user.users_teams[0].teams.id,
- },
- })
-
+ // Show deployments from all teams for now.
+ const deployments = user.users_teams.flatMap(t => t.teams.api_deployments)
return {
props: {
- deployment: defaultDeployment,
+ deployments,
}
}
}
+// interface DeleteProjectBody {
+// id: string
+// }
+
+// async function handleDeleteProject(url: string, { arg }: { arg: DeleteProjectBody }) {
+// return await fetch(url, {
+// method: 'DELETE',
+// body: JSON.stringify(arg),
+
+// headers: {
+// 'Content-Type': 'application/json',
+// },
+// }).then(r => r.json())
+// }
+
+
interface Props {
- deployment: api_deployments
+ deployments: api_deployments[]
}
-function ProjectPage({ deployment }: Props) {
- const client = useSupabaseClient()
+function Home({ deployments }: Props) {
+ const router = useRouter()
+
+ const {
+ trigger: deleteProject,
+ } = useSWRMutation('/api/project', handleDeleteProject)
+
+ async function handleDelete(id: string) {
+ await deleteProject({ id })
+ router.replace(router.asPath)
+ }
return (
- <StoreProvider
- client={client}
- deployment={deployment}
+ <div
+ className="
+ flex
+ flex-1
+ flex-col
+ space-x-0
+ space-y-4
+ overflow-hidden
+ p-8
+ lg:flex-row
+ lg:space-y-0
+ lg:space-x-4
+ lg:p-12
+ "
>
- <DeploymentEditor />
- </StoreProvider>
+ <div className="flex items-start space-x-4 lg:justify-start justify-between">
+ <div className="items-center flex space-x-2">
+ <LayoutGrid size="30px" strokeWidth="1.5" />
+ <Text
+ size={Text.size.S1}
+ text="Projects"
+ />
+ </div>
+
+ <Button
+ icon={<Plus size="16px" />}
+ text="New"
+ variant={Button.variant.Full}
+ onClick={() => router.push('/new/project')}
+ />
+ </div>
+
+ <div
+ className="
+ flex
+ flex-1
+ flex-col
+ items-stretch
+ overflow-hidden
+ "
+ >
+ <div className="flex flex-1 justify-center overflow-hidden">
+ <ItemList
+ deleteItem={handleDelete}
+ items={deployments.map(i => ({
+ ...i,
+ title: i.title || i.id,
+ path: '/[id]',
+ type: 'Project',
+ icon: <LayoutGrid size="22px" strokeWidth="1.7" />,
+ }))}
+ />
+ </div>
+ </div>
+ </div>
)
}
-export default ProjectPage
+export default Home
\ No newline at end of file
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index afde3e011..7c8fda030 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -327,11 +327,11 @@ model slack_installations {
}
model teams {
- id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
- created_at DateTime @default(now()) @db.Timestamptz(6)
+ id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
+ created_at DateTime @default(now()) @db.Timestamptz(6)
name String?
- is_default Boolean @default(false)
- api_deployments api_deployments?
+ is_default Boolean @default(false)
+ api_deployments api_deployments[]
apps apps[]
users_teams users_teams[]
@@ -383,8 +383,10 @@ model api_deployments {
id BigInt @id(map: "deployments_pkey") @default(autoincrement())
created_at DateTime? @default(now()) @db.Timestamptz(6)
data Json
- team_id String @unique @db.Uuid
- teams teams @relation(fields: [team_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
+ team_id String? @db.Uuid
+ code Json?
+ logs Json?
+ teams teams? @relation(fields: [team_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
@@schema("public")
}