Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Call nextjs app api from python api on agent state changes

Tomas Valentad69033e6b0c0
diff --git a/api-service/deployment/manager.py b/api-service/deployment/manager.py
index b1ca769e3..99c4f68ae 100644
--- a/api-service/deployment/manager.py
+++ b/api-service/deployment/manager.py
@@ -1,3 +1,7 @@
+import os
+import aiohttp
+import json
+
from typing import Any, Callable, Coroutine, List
from abc import abstractmethod, ABC
@@ -15,6 +19,44 @@ AgentFactory = Callable[
[Any, GetEnvs, OnLogs, OnInteractionRequest], Coroutine[None, None, AgentBase]
]
+app_url = os.environ.get("APP_URL", "http://localhost:3000")
+secret_token = os.environ.get("SECRET_TOKEN")
+
+
+async def agent_run_done(deployment_id: str, prompt: str):
+ async with aiohttp.ClientSession() as client:
+ async with client.post(
+ f"{app_url}/api/agent/run",
+ data=json.dumps(
+ {
+ "deployment_id": deployment_id,
+ "prompt": prompt,
+ }
+ ),
+ headers={
+ "Content-Type": "application/json",
+ "Authorization": f"Bearer {secret_token}",
+ },
+ ) as response:
+ pass
+
+
+async def agent_run_cancelled(deployment_id: str):
+ async with aiohttp.ClientSession() as client:
+ async with client.delete(
+ f"{app_url}/api/agent/run",
+ data=json.dumps(
+ {
+ "deployment_id": deployment_id,
+ }
+ ),
+ headers={
+ "Content-Type": "application/json",
+ "Authorization": f"Bearer {secret_token}",
+ },
+ ) as response:
+ pass
+
class AgentEvents:
def __init__(self, id: str) -> None:
@@ -30,12 +72,11 @@ class AgentEvents:
self, interaction_request: AgentInteractionRequest
):
self.interaction_requests.append(interaction_request)
- if interaction_request.type == "done":
- # TODO: Send the interaction request to nexjs backend + GH App
- pass
- elif interaction_request.type == "cancelled":
- # TODO: Send the interaction request to nexjs backend + GH App
- pass
+ match interaction_request.type:
+ case "done":
+ await agent_run_done(self.id, interaction_request.data["prompt"])
+ case "cancelled":
+ await agent_run_cancelled(self.id)
def remove_interaction_request(self, interaction_id: str):
self.interaction_requests = [
diff --git a/api-service/poetry.lock b/api-service/poetry.lock
index 5b509a1b4..59994969d 100644
--- a/api-service/poetry.lock
+++ b/api-service/poetry.lock
@@ -2020,4 +2020,4 @@ multidict = ">=4.0"
[metadata]
lock-version = "2.0"
python-versions = ">=3.10,<4.0"
-content-hash = "04b1b35933266342e43524c2a3aceea0d906fede78afeb36318042a76166bd2a"
+content-hash = "25338b5e2404c09c1bd5374a2879f27aef6992a36b18fee517c133860f06e1eb"
diff --git a/api-service/pyproject.toml b/api-service/pyproject.toml
index 94ae06513..1f101cc3c 100644
--- a/api-service/pyproject.toml
+++ b/api-service/pyproject.toml
@@ -23,6 +23,7 @@ langchain = "0.0.141"
chevron = "^0.14.0"
openai = "^0.27.6"
uuid = "^1.30"
+aiohttp = "^3.8.4"
[tool.poetry.group.dev]
optional = true