Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Fix agent loop; Cleanup; Add deploy UI [WIP]

Tomas Valentafe91c7c4af58
diff --git a/.github/workflows/api.yml b/.github/workflows/api.yml
index 736c79256..b329ced42 100644
--- a/.github/workflows/api.yml
+++ b/.github/workflows/api.yml
@@ -1,11 +1,11 @@
name: Build and Deploy to Google Compute Engine
-# on:
-# push:
-# paths:
-# - 'api-service/**'
-# branches:
-# - main
+on:
+ push:
+ paths:
+ - 'api-service/**'
+ branches:
+ - main
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
diff --git a/api-service/agent/basic_agent.py b/api-service/agent/basic_agent.py
index 0b79e7cf0..b801898a8 100644
--- a/api-service/agent/basic_agent.py
+++ b/api-service/agent/basic_agent.py
@@ -39,6 +39,8 @@ class RewriteStepsException(Exception):
class BasicAgent(AgentBase):
+ max_run_time = 60 * 60 * 24 # in seconds
+
def __init__(
self,
config: Any,
@@ -47,6 +49,7 @@ class BasicAgent(AgentBase):
on_interaction_request: OnInteractionRequest,
):
super().__init__()
+ self._start_loop: asyncio.Task | None = None
self.get_envs = get_envs
self.config = ModelConfig(**config)
self.on_interaction_request = on_interaction_request
@@ -303,10 +306,23 @@ class BasicAgent(AgentBase):
async def _start(self, instructions: Any):
print("Start agent run")
+
+ async def start_with_timeout():
+ try:
+ self._start_loop = asyncio.create_task(
+ self._run(instructions=instructions),
+ )
+
+ await asyncio.wait_for(
+ self._start_loop,
+ timeout=self.max_run_time,
+ )
+ except asyncio.TimeoutError:
+ await self.stop()
+ print("Timeout")
+
asyncio.create_task(
- self._run(
- instructions=instructions,
- )
+ start_with_timeout(),
)
async def _pause(self):
@@ -344,7 +360,7 @@ class BasicAgent(AgentBase):
await self._start(interaction.data["instructions"])
case "rewrite_steps":
await self._rewrite_steps(interaction.data["steps"])
- case default:
+ case _:
raise Exception(f"Unknown interaction action: {interaction.type}")
async def stop(self):
@@ -352,3 +368,5 @@ class BasicAgent(AgentBase):
self.canceled = True
if self.llm_generation:
self.llm_generation.cancel()
+ if self._start_loop:
+ self._start_loop.cancel()
diff --git a/api-service/app.py b/api-service/app.py
index ba58623cf..dbbc223fe 100644
--- a/api-service/app.py
+++ b/api-service/app.py
@@ -8,7 +8,7 @@ from pydantic import BaseModel
from agent.base import AgentInteraction
from deployment.in_memory import InMemoryDeploymentManager
-from agent.json_rpc import JsonRpcAgent
+from json_rpc import JsonRpcAgentConnection
from agent.basic_agent import BasicAgent
from database.base import db
@@ -44,16 +44,16 @@ app.add_middleware(
@app.websocket("/dev/agent")
async def ws_agent_run(websocket: WebSocket, project_id: str):
await websocket.accept()
- connector = JsonRpcAgent(
+ connection = JsonRpcAgentConnection(
project_id=project_id,
iter_json=websocket.iter_json,
send_json=websocket.send_json,
agent_factory=BasicAgent.create,
)
try:
- await connector.handle()
+ await connection.handle()
except:
- await connector.close()
+ await connection.close()
finally:
print("Closing websocket")
@@ -72,7 +72,6 @@ async def list_deployments():
async def create_agent_deployment(body: CreateDeploymentBody, project_id: str):
db_deployment = await db.get_deployment(project_id)
- print("test", db_deployment)
if db_deployment:
deployment = await deployment_manager.update_deployment(
db_deployment["id"],
diff --git a/api-service/agent/json_rpc.py b/api-service/json_rpc.py
similarity index 99%
rename from api-service/agent/json_rpc.py
rename to api-service/json_rpc.py
index f66a0d523..11f251b25 100644
--- a/api-service/agent/json_rpc.py
+++ b/api-service/json_rpc.py
@@ -7,7 +7,7 @@ from deployment.manager import AgentFactory
from database.base import db
-class JsonRpcAgent:
+class JsonRpcAgentConnection:
jsonrpc_version = "2.0"
def __init__(
diff --git a/api-service/playground/package-lock.json b/api-service/playground/package-lock.json
index 0546462a7..7849e7807 100644
--- a/api-service/playground/package-lock.json
+++ b/api-service/playground/package-lock.json
@@ -7,7 +7,6 @@
"": {
"name": "playground",
"version": "1.0.0",
- "license": "MIT",
"dependencies": {
"@devbookhq/sdk": "^2.6.74",
"express": "^4.18.2",
diff --git a/components/Editor/Sidebar/Deploy.tsx b/components/Editor/Sidebar/Deploy.tsx
index f595bbe63..f496ea923 100644
--- a/components/Editor/Sidebar/Deploy.tsx
+++ b/components/Editor/Sidebar/Deploy.tsx
@@ -1,4 +1,5 @@
-import { memo } from 'react'
+import { memo, useState } from 'react'
+import Link from 'next/link'
import api from 'api-client/api'
import Text from 'components/Text'
@@ -7,6 +8,9 @@ import { useStateStore } from 'state/StoreProvider'
import { evaluatePrompt } from 'state/prompt'
import useModelProviderArgs from 'hooks/useModelProviderArgs'
import { getModelArgs } from 'state/model'
+import Button from 'components/Button'
+import Spinner from 'components/Spinner'
+import useDeployment from 'hooks/useDeployment'
const createDeployment = api.path('/deployments').method('put').create({
@@ -20,31 +24,39 @@ export interface Props {
function Deploy({ project }: Props) {
const [selectors] = useStateStore()
const modelConfig = selectors.use.getSelectedModelConfig()()
+ const [isDeploying, setIsDeploying] = useState(false)
const [creds] = useModelProviderArgs()
- async function deployAgent() {
- if (!modelConfig) {
- console.error('Cannot get model config')
- return
- }
- const {
- references: promptReferences,
- prompt: evaluatedPrompt,
- } = evaluatePrompt(modelConfig.prompt)
+ const deployment = useDeployment(project)
- createDeployment({
- // TODO: Add current envs vars and save them on backend
- project_id: project.id,
- config: {
- name: modelConfig.name,
- provider: modelConfig.provider,
- args: getModelArgs(modelConfig, creds) as any,
+ async function deployAgent() {
+ try {
+ setIsDeploying(true)
+ if (!modelConfig) {
+ console.error('Cannot get model config')
+ return
+ }
+ const {
+ references: promptReferences,
prompt: evaluatedPrompt,
- // TODO: Handle prompt references
- prompt_references: promptReferences,
- } as any,
- })
+ } = evaluatePrompt(modelConfig.prompt)
+
+ await createDeployment({
+ // TODO: Add current envs vars and save them on backend
+ project_id: project.id,
+ config: {
+ name: modelConfig.name,
+ provider: modelConfig.provider,
+ args: getModelArgs(modelConfig, creds) as any,
+ prompt: evaluatedPrompt,
+ // TODO: Handle prompt references
+ // prompt_references: promptReferences,
+ } as any,
+ })
+ } finally {
+ setIsDeploying(false)
+ }
}
return (
@@ -79,7 +91,42 @@ function Deploy({ project }: Props) {
space-y-4
"
>
-
+ <div
+ className=""
+ >
+ <Button
+ text={isDeploying ? 'Deploying' : 'Deploy'}
+ isDisabled={isDeploying}
+ icon={isDeploying ? <Spinner /> : ''}
+ onClick={deployAgent}
+ variant={Button.variant.Full}
+ />
+ </div>
+ <div
+ className="
+ "
+ >
+ {deployment && deployment['config'] &&
+ <div
+ className="
+ flex
+ flex-col
+ "
+ >
+ <Text
+ text={`Deployment ID: ${deployment.id}`}
+ />
+ <Link
+ href={`${process.env.NEXT_PUBLIC_API_URL}/deployment/${deployment.id}`}
+ target="_blank"
+ rel="noopener noreferrers"
+ className=""
+ >
+ {`${process.env.NEXT_PUBLIC_API_URL}/deployment/${deployment.id}`}
+ </Link>
+ </div>
+ }
+ </div>
</div>
</div>
)
diff --git a/db/tables.ts b/db/tables.ts
index 8469f8eb2..67cc0d5ef 100644
--- a/db/tables.ts
+++ b/db/tables.ts
@@ -1 +1,2 @@
export const projectsTable = 'projects'
+export const deploymentsTable = 'deployments'
diff --git a/hooks/useDeployment.ts b/hooks/useDeployment.ts
new file mode 100644
index 000000000..56295c9f0
--- /dev/null
+++ b/hooks/useDeployment.ts
@@ -0,0 +1,60 @@
+import { deployments, projects } from '@prisma/client'
+import { useSupabaseClient } from '@supabase/auth-helpers-react'
+import { useState, useEffect } from 'react'
+
+import { deploymentsTable } from 'db/tables'
+import { Database } from 'db/supabase'
+
+function useDeployment(project: projects) {
+ const [initDeployment, setInitDeployment] = useState<deployments>()
+
+ const [deployment, setDeployment] = useState<deployments>()
+ const client = useSupabaseClient<Database>()
+
+ useEffect(function init() {
+ (async function () {
+ // TODO: SECURITY - Enable row security for all tables and secure access to deployments.
+ const deployment = await client
+ .from(deploymentsTable)
+ .select('*')
+ .eq('project_id', project.id)
+ .order('created_at', { ascending: false })
+ .limit(1)
+ .maybeSingle()
+
+ if (!deployment) return
+ if (deployment.error) return
+ setInitDeployment(deployment.data as unknown as deployments)
+ }())
+ }, [client, project.id])
+
+ // Sometimes a large field from realtime server can be missing because of the internal POSTGRES/TOAST workings.
+ // We changed the table replication to full with `ALTER TABLE events REPLICA IDENTITY FULL;` to fix this.
+ // https://github.com/supabase/realtime/issues/223 mentioned that we may need to check the `old_record` field of the payload for the actual value,
+ // but so far it seems we don't have to.
+ useEffect(function subscribe() {
+ // TODO: SECURITY - Enable row security for all tables and configure access to deployments.
+ const insertSub = client.channel('any')
+ .on('postgres_changes',
+ {
+ event: 'INSERT',
+ schema: 'public',
+ table: deploymentsTable,
+ filter: `project_id=eq.${project.id}`,
+ }, payload => {
+ setDeployment(payload.new as deployments)
+ })
+ .subscribe()
+
+ return () => {
+ insertSub.unsubscribe()
+ }
+ }, [
+ client,
+ project.id,
+ ])
+
+ return deployment || initDeployment
+}
+
+export default useDeployment
diff --git a/pages/api/service/[...all].ts b/pages/api/service/[...all].ts
index ee6d8d5b6..22dd8e2ac 100644
--- a/pages/api/service/[...all].ts
+++ b/pages/api/service/[...all].ts
@@ -23,7 +23,7 @@ const proxy = createProxyMiddleware<NextApiRequest, NextApiResponse>({
ws: true,
secure: !isSecure,
changeOrigin: true,
- // pathRewrite: { [`^${pathPrefix}`]: '' }, // remove prefix
+ pathRewrite: { [`^${pathPrefix}`]: '' }, // remove prefix
})
export default function handler(req: NextApiRequest, res: NextApiResponse) {
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index b7b413100..d82f6d01e 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -1,6 +1,7 @@
generator js {
provider = "prisma-client-js"
previewFeatures = ["multiSchema", "views"]
+ engineType = "binary"
}
datasource db {