Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Fix method enum; Add logs streaming; Add logs UI [WIP]

Tomas Valenta41213796e97e
diff --git a/components/Editor/Code.tsx b/components/Editor/Code.tsx
new file mode 100644
index 000000000..adf435716
--- /dev/null
+++ b/components/Editor/Code.tsx
@@ -0,0 +1,25 @@
+export interface Props {
+ code: string
+}
+
+function Code({ code }: Props) {
+ return (
+ <div className="
+ flex
+ flex-col
+ bg-white
+ overflow-y-auto
+ text-xs
+ leading-4
+ break-words
+ w-[250px]
+ whitespace-normal
+ space-y-2
+ p-4
+ ">
+ {code}
+ </div>
+ )
+}
+
+export default Code
\ No newline at end of file
diff --git a/components/Editor/Logs.tsx b/components/Editor/Logs.tsx
index fa50aa1c6..b17ec35f3 100644
--- a/components/Editor/Logs.tsx
+++ b/components/Editor/Logs.tsx
@@ -1,13 +1,36 @@
+import Ansi from "ansi-to-react"
+
import { Log } from '.'
+import { Fragment } from 'react'
export interface Props {
logs: Log[]
}
function Logs({ logs }: Props) {
-
return (
- <div></div>
+ <div className="
+ flex
+ flex-col
+ bg-white
+ overflow-y-auto
+ text-xs
+ leading-4
+ break-words
+ w-[250px]
+ whitespace-normal
+ space-y-2
+ p-4
+ ">
+ {logs.map((l, i) =>
+ <Fragment key={i}>
+ <Ansi>
+ {l}
+ </Ansi>
+ <hr />
+ </Fragment>
+ )}
+ </div>
)
}
diff --git a/components/Editor/index.tsx b/components/Editor/index.tsx
index 2683f2a84..57f77535e 100644
--- a/components/Editor/index.tsx
+++ b/components/Editor/index.tsx
@@ -3,11 +3,11 @@ import useSWRMutation from 'swr/mutation'
import { Fragment, useState } from 'react'
import Hotkeys from 'react-hot-keys'
import { projects } from '@prisma/client'
+import { nanoid } from 'nanoid'
import { State, Block, methods, Method } from 'state/store'
import { getStoreContext } from 'state/StoreProvider'
import Select from 'components/Select'
-import { nanoid } from 'nanoid'
import Button from 'components/Button'
import Text from 'components/Text'
import { useLatestDeployment } from 'hooks/useLatestDeployment'
@@ -15,6 +15,7 @@ import { useLatestDeployment } from 'hooks/useLatestDeployment'
import BlockEditor from './BlockEditor'
import ConnectionLine from './ConnectionLine'
import AddBlockButton from './AddBlockButton'
+import Logs from './Logs'
// TODO: Prod api host
const apiHost = process.env.NODE_ENV === 'development' ?
@@ -30,9 +31,7 @@ const selector = (state: State) => ({
changeMethod: state.changeMethod,
})
-export interface Log {
-
-}
+export type Log = string
export interface Props {
project: projects
@@ -64,6 +63,7 @@ function Editor({ project }: Props) {
const deployment = useLatestDeployment(project)
const logs = deployment?.logs as Log[] | undefined
+ const code = deployment?.code
const {
addBlock,
@@ -106,7 +106,9 @@ function Editor({ project }: Props) {
}}
allowRepeat
>
- <div className="
+ <div className="flex flex-row flex-1 overflow-hidden">
+
+ <div className="
flex
flex-1
p-8
@@ -116,65 +118,67 @@ function Editor({ project }: Props) {
scroller
relative
">
- <div className="flex items-center space-x-2">
- <Text
- text="Incoming"
- className='font-bold'
- />
- <Select
- direction="left"
- selectedValue={{ key: method, title: method }}
- values={methods.map(m => ({ key: m, title: m }))}
- onChange={m => changeMethod(m.title as Method)}
- />
- <Text
- text="Request"
- className='font-bold'
- />
- </div>
- <div className="
+ <div className="flex items-center space-x-2">
+ <Text
+ text="Incoming"
+ className='font-bold'
+ />
+ <Select
+ direction="left"
+ selectedValue={{ key: method, title: method.toUpperCase() }}
+ values={methods.map(m => ({ key: m, title: m.toUpperCase() }))}
+ onChange={m => changeMethod(m.key as Method)}
+ />
+ <Text
+ text="Request"
+ className='font-bold'
+ />
+ </div>
+ <div className="
flex
flex-col
items-center
transition-all
">
- {blocks.map((b, i) =>
- <Fragment
- key={b.id}
- >
- <ConnectionLine className='h-4' />
- <BlockEditor
- block={b}
- onDelete={() => {
- removeBlock(i)
- setTimeout(() => {
- if (i <= focusedBlock.index) {
- setFocusedBlock(b => ({ index: b.index - 1 }))
- } else {
- setFocusedBlock(b => ({ index: b.index }))
- }
- }, 0)
- }}
- onChange={(b) => changeBlock(i, b)}
- index={i}
- focus={focusedBlock}
- onFocus={() => setFocusedBlock({ index: i })}
- />
- </Fragment>
- )}
- </div>
- <ConnectionLine className='min-h-[16px]' />
- <AddBlockButton addBlock={(block) => {
- addBlock(block)
- setTimeout(() => setFocusedBlock({ index: blocks.length }), 0)
- }} />
- <div className="fixed right-3 top-14">
- <Button
- text="Deploy"
- onClick={deploy}
- variant={Button.variant.Full}
- />
+ {blocks.map((b, i) =>
+ <Fragment
+ key={b.id}
+ >
+ <ConnectionLine className='h-4' />
+ <BlockEditor
+ block={b}
+ onDelete={() => {
+ removeBlock(i)
+ setTimeout(() => {
+ if (i <= focusedBlock.index) {
+ setFocusedBlock(b => ({ index: b.index - 1 }))
+ } else {
+ setFocusedBlock(b => ({ index: b.index }))
+ }
+ }, 0)
+ }}
+ onChange={(b) => changeBlock(i, b)}
+ index={i}
+ focus={focusedBlock}
+ onFocus={() => setFocusedBlock({ index: i })}
+ />
+ </Fragment>
+ )}
+ </div>
+ <ConnectionLine className='min-h-[16px]' />
+ <AddBlockButton addBlock={(block) => {
+ addBlock(block)
+ setTimeout(() => setFocusedBlock({ index: blocks.length }), 0)
+ }} />
+ <div className="fixed right-3 top-14">
+ <Button
+ text="Deploy"
+ onClick={deploy}
+ variant={Button.variant.Full}
+ />
+ </div>
</div>
+ {logs && <Logs logs={logs} />}
</div>
</Hotkeys>
)
diff --git a/hooks/useLatestDeployment.tsx b/hooks/useLatestDeployment.tsx
index ff5f3295b..49fd123d3 100644
--- a/hooks/useLatestDeployment.tsx
+++ b/hooks/useLatestDeployment.tsx
@@ -6,27 +6,59 @@ import { deploymentsTable } from 'db/tables'
import { Database } from 'db/supabase'
export function useLatestDeployment(project: projects) {
+ const [initDeployment, setInitDeployment] = useState<deployments>()
+
const [deployment, setDeployment] = useState<deployments>()
const client = useSupabaseClient<Database>()
- // useEffect(function subscribe() {
- // const sub = client.channel('any')
- // .on('postgres_changes',
- // {
- // event: 'UPDATE',
- // schema: 'public',
- // table: deploymentsTable
- // filter: `project_id=eq.${project.id}`,
- // }, payload => {
- // console.log('Change received!', payload)
- // setDeployment(payload.new)
- // })
- // .subscribe()
-
- // return () => {
- // sub.unsubscribe()
- // }
- // }, [client])
-
- return deployment
+ useEffect(function init() {
+ (async function () {
+ const deployment = await client
+ .from(deploymentsTable)
+ .select('*')
+ .eq('project_id', project.id)
+ .order('created_at')
+ .limit(1)
+ .single()
+
+ if (deployment.error) return
+
+ console.log(deployment.data)
+ setInitDeployment(deployment.data as unknown as deployments)
+ }())
+ }, [client, project.id])
+
+ useEffect(function subscribe() {
+ const insertSub = client.channel('any')
+ .on('postgres_changes',
+ {
+ event: 'INSERT',
+ schema: 'public',
+ table: deploymentsTable,
+ filter: `project_id=eq.${project.id}`,
+ }, payload => {
+ console.log('Change received!', payload)
+ setDeployment(payload.new as deployments)
+ })
+ .subscribe()
+ const updateSub = client.channel('any')
+ .on('postgres_changes',
+ {
+ event: 'UPDATE',
+ schema: 'public',
+ table: deploymentsTable,
+ filter: `project_id=eq.${project.id}`,
+ }, payload => {
+ console.log('Change received!', payload)
+ setDeployment(payload.new as deployments)
+ })
+ .subscribe()
+
+ return () => {
+ insertSub.unsubscribe()
+ updateSub.unsubscribe()
+ }
+ }, [client, project.id])
+
+ return deployment || initDeployment
}
diff --git a/package.json b/package.json
index 4de3de549..fda0fa3b1 100644
--- a/package.json
+++ b/package.json
@@ -22,6 +22,7 @@
"@supabase/auth-helpers-react": "^0.3.1",
"@supabase/supabase-js": "^2.10.0",
"@types/randomcolor": "^0.5.7",
+ "ansi-to-react": "^6.1.6",
"clsx": "^1.2.1",
"express": "^4.18.2",
"human-id": "^4.0.0",
@@ -61,4 +62,4 @@
"typescript": "^4.9.5",
"vercel": "^28.16.12"
}
-}
\ No newline at end of file
+}
diff --git a/state/store.ts b/state/store.ts
index dc8801f23..369585e1e 100644
--- a/state/store.ts
+++ b/state/store.ts
@@ -26,9 +26,10 @@ export enum Method {
PATCH = 'patch',
}
-export const methods = Object.keys(Method).filter((item) => {
- return isNaN(Number(item))
-})
+export const methods = Object
+ .keys(Method)
+ .filter((item) => isNaN(Number(item)))
+ .map(v => v.toLowerCase())
export interface State {
blocks: Block[]
diff --git a/yarn.lock b/yarn.lock
index 7414ee7c8..b1658eada 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2168,6 +2168,11 @@ ajv@^6.0.0, ajv@^6.10.0, ajv@^6.12.4:
json-schema-traverse "^0.4.1"
uri-js "^4.2.2"
+anser@^1.4.1:
+ version "1.4.10"
+ resolved "https://registry.yarnpkg.com/anser/-/anser-1.4.10.tgz#befa3eddf282684bd03b63dcda3927aef8c2e35b"
+ integrity sha512-hCv9AqTQ8ycjpSd3upOJd7vFwW1JaoYQ7tpham03GJ1ca8/65rqn0RpaWpItOAd6ylW9wAw6luXYPJIyPFVOww==
+
ansi-escapes@^4.2.1:
version "4.3.2"
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-4.3.2.tgz#6b2291d1db7d98b6521d5f1efa42d0f3a9feb65e"
@@ -2194,6 +2199,14 @@ ansi-styles@^4.0.0, ansi-styles@^4.1.0:
dependencies:
color-convert "^2.0.1"
+ansi-to-react@^6.1.6:
+ version "6.1.6"
+ resolved "https://registry.yarnpkg.com/ansi-to-react/-/ansi-to-react-6.1.6.tgz#d6fe15ecd4351df626a08121b1646adfe6c02ccb"
+ integrity sha512-+HWn72GKydtupxX9TORBedqOMsJRiKTqaLUKW8txSBZw9iBpzPKLI8KOu4WzwD4R7hSv1zEspobY6LwlWvwZ6Q==
+ dependencies:
+ anser "^1.4.1"
+ escape-carriage "^1.3.0"
+
anymatch@~3.1.2:
version "3.1.3"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.3.tgz#790c58b19ba1720a84205b57c618d5ad8524973e"
@@ -3589,6 +3602,11 @@ escalade@^3.1.1:
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40"
integrity sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==
+escape-carriage@^1.3.0:
+ version "1.3.0"
+ resolved "https://registry.yarnpkg.com/escape-carriage/-/escape-carriage-1.3.0.tgz#71006b2d4da8cb6828686addafcb094239c742f3"
+ integrity sha512-ATWi5MD8QlAGQOeMgI8zTp671BG8aKvAC0M7yenlxU4CRLGO/sKthxVUyjiOFKjHdIo+6dZZUNFgHFeVEaKfGQ==
+
escape-html@~1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/escape-html/-/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988"