Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Create deployment before inserting logs; Handle url and deploy state on frontend [WIP]

Tomas Valentaac8e2b037885
diff --git a/api-service/app.py b/api-service/app.py
index 1bfd2dee6..269347c31 100644
--- a/api-service/app.py
+++ b/api-service/app.py
@@ -44,16 +44,15 @@ def generate():
method = body["method"]
route = body["route"]
+ db.create_deployment(run_id=run_id, project_id=project_id, route_id=route_id)
+
final_prompt, js_code = generate_req_handler(
run_id=run_id,
db=db,
- project_id=project_id,
- route_id=route_id,
blocks=blocks,
method=method,
)
- # TODO: Change deployments.state field in db to "deploying"
db.update_state(run_id=run_id, state=DeploymentState.Deploying)
cf_worker_dir_path = (
diff --git a/api-service/codegen/base.py b/api-service/codegen/base.py
index 3a91223dc..66c9c240c 100644
--- a/api-service/codegen/base.py
+++ b/api-service/codegen/base.py
@@ -11,16 +11,12 @@ from codegen.db.base import Database
def generate_req_handler(
db: Database,
run_id: str,
- project_id: str,
- route_id: str,
blocks: List[str],
method: str,
) -> str:
executor = create_js_agent(
db=db,
run_id=run_id,
- project_id=project_id,
- route_id=route_id,
llm=OpenAI(temperature=0, max_tokens=1000),
# llm=OpenAI(temperature=0, model_name='code-davinci-002', max_tokens=1000),
# llm=OpenAIChat(temperature=0, max_tokens=1000),
diff --git a/api-service/codegen/db/base.py b/api-service/codegen/db/base.py
index 579041eb0..ec6baf0a9 100644
--- a/api-service/codegen/db/base.py
+++ b/api-service/codegen/db/base.py
@@ -15,19 +15,27 @@ class Database:
def __init__(self, url: str, key: str):
self.client = create_client(url, key)
+ def create_deployment(
+ self, run_id: str, project_id: str, route_id: str
+ ) -> None:
+ self.client.table(deploymentsTable).insert(
+ {
+ "id": run_id,
+ "project_id": project_id,
+ "route_id": route_id,
+ "state": DeploymentState.Generating.value,
+ },
+ ).execute()
+
def push_logs(
- self, run_id: str, project_id: str, route_id: str, logs: List[str]
+ self, run_id: str, logs: List[str]
) -> None:
if len(logs) > 0:
- self.client.table(deploymentsTable).upsert(
- json={
- "id": run_id,
+ self.client.table(deploymentsTable).update(
+ {
"logs": logs,
- "project_id": project_id,
- "route_id": route_id,
- "state": DeploymentState.Generating.value,
- },
- ).execute()
+ }
+ ).eq("id", run_id).execute()
def update_state(self, run_id: str, state: DeploymentState) -> None:
self.client.table(deploymentsTable).update(
diff --git a/api-service/codegen/js_agent/base.py b/api-service/codegen/js_agent/base.py
index 492f7ac74..a906456c0 100644
--- a/api-service/codegen/js_agent/base.py
+++ b/api-service/codegen/js_agent/base.py
@@ -21,8 +21,6 @@ from codegen.db.base import Database
def create_js_agent(
db: Database,
run_id: str,
- project_id: str,
- route_id: str,
llm: BaseLLM,
tools: List[BaseTool],
callback_manager: Optional[BaseCallbackManager] = None,
@@ -43,7 +41,7 @@ def create_js_agent(
cb_manager = SharedCallbackManager()
cb_manager.set_handler(
LoggerCallbackHandler(
- db=db, run_id=run_id, project_id=project_id, route_id=route_id
+ db=db, run_id=run_id
)
)
return AgentExecutor.from_agent_and_tools(
diff --git a/api-service/codegen/js_agent/callbacks/log.py b/api-service/codegen/js_agent/callbacks/log.py
index b281e29b7..b879977c4 100644
--- a/api-service/codegen/js_agent/callbacks/log.py
+++ b/api-service/codegen/js_agent/callbacks/log.py
@@ -15,8 +15,6 @@ class LoggerCallbackHandler(BaseCallbackHandler):
self,
db: Database,
run_id: str,
- project_id: str,
- route_id: str,
color: Optional[str] = None,
) -> None:
"""Initialize callback handler."""
@@ -24,8 +22,6 @@ class LoggerCallbackHandler(BaseCallbackHandler):
self.db = db
self.run_id = run_id
- self.route_id = route_id
- self.project_id = project_id
self.color = color
self.logs = []
@@ -34,8 +30,6 @@ class LoggerCallbackHandler(BaseCallbackHandler):
self.logs.append(log)
self.db.push_logs(
run_id=self.run_id,
- project_id=self.project_id,
- route_id=self.route_id,
logs=self.logs,
)
diff --git a/components/Editor/Logs/DeployButton.tsx b/components/Editor/Logs/DeployButton.tsx
index e5554c5a6..23f14e2f9 100644
--- a/components/Editor/Logs/DeployButton.tsx
+++ b/components/Editor/Logs/DeployButton.tsx
@@ -1,33 +1,78 @@
import { deployment_state } from '@prisma/client'
+import { ReactNode } from 'react'
+
import Button from 'components/Button'
import Spinner from 'components/Spinner'
-import { capitalize } from 'utils/capitalize'
+import { Check } from 'lucide-react'
export interface Props {
deploy: () => void
deployStatus?: deployment_state | null
+ isInitializingDeploy?: boolean
+}
+
+interface DeploymentRepresentation {
+ text: string
+ icon: ReactNode | null
}
-function isInProgress(deployStaus?: deployment_state | null) {
- switch (deployStaus) {
- case null:
+function getDeploymentRepresentation(deployStatus: deployment_state | null | undefined, isInitializingDeploy: boolean | undefined): DeploymentRepresentation {
+ switch (deployStatus) {
case deployment_state.generating:
+ return {
+ text: 'Generating...',
+ icon: <Spinner />,
+ }
case deployment_state.deploying:
- return true
+ return {
+ text: 'Deploying...',
+ icon: <Spinner />,
+ }
+ case deployment_state.error:
+ return {
+ text: 'Failed',
+ icon: null,
+ }
+ case deployment_state.finished:
+ if (isInitializingDeploy) {
+ return {
+ text: 'Deployed',
+ icon: <Check size="16px" />
+ }
+ } else {
+ return {
+ text: 'Deploy',
+ icon: null,
+ }
+ }
default:
- return false
+ if (isInitializingDeploy) {
+ return {
+ text: 'Initializing...',
+ icon: <Spinner />,
+ }
+ } else {
+ return {
+ text: 'Deploy',
+ icon: null,
+ }
+ }
}
}
-function DeployButton({ deploy, deployStatus }: Props) {
- const inProgress = isInProgress()
+function DeployButton({
+ deploy,
+ deployStatus,
+ isInitializingDeploy,
+}: Props) {
+ const representation = getDeploymentRepresentation(deployStatus, isInitializingDeploy)
return (
<Button
- text={deployStatus ? capitalize(deployStatus) : 'Deploy'}
+ text={representation.text}
onClick={deploy}
variant={Button.variant.Full}
- icon={inProgress ? <Spinner /> : null}
+ icon={representation.icon}
/>
)
}
diff --git a/components/Editor/Logs/index.tsx b/components/Editor/Logs/index.tsx
index 79e59aaa0..d29381b55 100644
--- a/components/Editor/Logs/index.tsx
+++ b/components/Editor/Logs/index.tsx
@@ -12,8 +12,9 @@ import DeployButton from './DeployButton'
export interface Props {
logs?: Log[]
deploy: () => void
- deployedURL: string
+ deployedURL?: string | null
deployStatus?: deployment_state | null
+ isInitializingDeploy?: boolean
}
function Logs({
@@ -21,6 +22,7 @@ function Logs({
deploy,
deployedURL,
deployStatus,
+ isInitializingDeploy,
}: Props) {
useEffect(function highlightCode() {
@@ -61,6 +63,7 @@ function Logs({
/>
<DeployButton
deploy={deploy}
+ isInitializingDeploy={isInitializingDeploy}
deployStatus={deployStatus}
/>
</div>
diff --git a/components/Editor/index.tsx b/components/Editor/index.tsx
index 7df47833c..8c5b46c9e 100644
--- a/components/Editor/index.tsx
+++ b/components/Editor/index.tsx
@@ -57,7 +57,7 @@ function Editor({ project }: Props) {
const changeRoute = store.use.changeRoute()
const addRoute = store.use.addRoute()
- const [deployedURL, setDeployedURL] = useState('')
+ const [isInitializingDeploy, setIsInitializingDeploy] = useState(false)
const [selectedRouteID, setSelectedRouteID] = useState(() => routes.length > 0 ? routes[0].id : undefined)
const selectedRoute = routes.find(s => s.id === selectedRouteID)
@@ -83,13 +83,11 @@ function Editor({ project }: Props) {
async function deploy() {
if (!selectedRoute) return
-
- const response = await generate({
+ setIsInitializingDeploy(true)
+ await generate({
projectID: project.id,
route: selectedRoute,
})
-
- setDeployedURL(response.url)
}
return (
@@ -190,24 +188,27 @@ function Editor({ project }: Props) {
setTimeout(() => setFocusedBlock({ index: selectedRoute.blocks.length }), 0)
}}
/>
- <a
- href={deployedURL}
- className="
+ {deployment?.url &&
+ <a
+ href={deployment.url}
+ className="
mt-6
underline
"
- >
- <Text
- size={Text.size.S3}
- text={deployedURL.substring('https://'.length)}
- />
- </a>
+ >
+ <Text
+ text={deployment.url.substring('https://'.length)}
+ size={Text.size.S3}
+ />
+ </a>
+ }
</div>
<Logs
deployStatus={deployment?.state}
+ isInitializingDeploy={isInitializingDeploy}
logs={logs}
deploy={deploy}
- deployedURL={deployedURL}
+ deployedURL={deployment?.url}
/>
</>
}
diff --git a/db/supabase.ts b/db/supabase.ts
index 251881621..e7e39d062 100644
--- a/db/supabase.ts
+++ b/db/supabase.ts
@@ -17,6 +17,7 @@ export interface Database {
project_id: string
route_id: string
state: Database["public"]["Enums"]["deployment_state"] | null
+ url: string | null
}
Insert: {
created_at?: string
@@ -25,6 +26,7 @@ export interface Database {
project_id: string
route_id: string
state?: Database["public"]["Enums"]["deployment_state"] | null
+ url?: string | null
}
Update: {
created_at?: string
@@ -33,6 +35,7 @@ export interface Database {
project_id?: string
route_id?: string
state?: Database["public"]["Enums"]["deployment_state"] | null
+ url?: string | null
}
}
projects: {
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 588fa5ace..9180a759b 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -258,6 +258,7 @@ model deployments {
project_id String
route_id String
state deployment_state?
+ url String?
projects projects @relation(fields: [project_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
@@schema("public")