From 2807f8fb8d3613b1b570e5989a0ddd57bd32cfa7 Mon Sep 17 00:00:00 2001 From: Reza Rahemtola Date: Wed, 13 Nov 2024 02:51:06 +0700 Subject: [PATCH 1/3] fix(backend): Fetch posts for owner --- backend/src/utils/agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/utils/agent.py b/backend/src/utils/agent.py index 67c8ed2..d530ee9 100644 --- a/backend/src/utils/agent.py +++ b/backend/src/utils/agent.py @@ -11,7 +11,7 @@ async def fetch_agents(ids: list[str] | None = None) -> list[FetchedAgent]: result = await client.get_posts( post_filter=PostFilter( types=[config.ALEPH_AGENT_POST_TYPE], - addresses=[config.ALEPH_SENDER], + addresses=[config.ALEPH_OWNER], tags=ids, channels=[config.ALEPH_CHANNEL], ) From c14c36690dfeca9273096f05bea3bb0e092a1eac Mon Sep 17 00:00:00 2001 From: Reza Rahemtola Date: Wed, 13 Nov 2024 09:58:42 +0700 Subject: [PATCH 2/3] feat(backend): Add subscription_id in public GET route --- backend/src/interfaces/agent.py | 2 +- backend/src/main.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/backend/src/interfaces/agent.py b/backend/src/interfaces/agent.py index 454d43f..916cc51 100644 --- a/backend/src/interfaces/agent.py +++ b/backend/src/interfaces/agent.py @@ -25,12 +25,12 @@ class UpdateAgentResponse(BaseModel): class PublicAgentData(BaseModel): id: str + subscription_id: str vm_hash: str | None last_update: int class Agent(PublicAgentData): - subscription_id: str encrypted_secret: str tags: list[str] diff --git a/backend/src/main.py b/backend/src/main.py index 606db5e..abe47d3 100644 --- a/backend/src/main.py +++ b/backend/src/main.py @@ -83,7 +83,10 @@ async def get_agent_public_info(agent_id: str) -> GetAgentResponse: agent = agents[0] return GetAgentResponse( - id=agent.id, vm_hash=agent.vm_hash, last_update=agent.last_update + id=agent.id, + vm_hash=agent.vm_hash, + last_update=agent.last_update, + subscription_id=agent.subscription_id, ) From bd857a194d82aaa07c115f99a4e24be3010ee483 Mon Sep 17 00:00:00 2001 From: Reza Rahemtola Date: Fri, 22 Nov 2024 23:07:22 +0900 Subject: [PATCH 3/3] feat(backend): Support environment variables --- backend/src/main.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/backend/src/main.py b/backend/src/main.py index abe47d3..b7e08a6 100644 --- a/backend/src/main.py +++ b/backend/src/main.py @@ -1,3 +1,4 @@ +import json import time from http import HTTPStatus from uuid import uuid4 @@ -60,7 +61,7 @@ async def setup(body: SetupAgentBody) -> None: aleph_account = ETHAccount(config.ALEPH_SENDER_SK) async with AuthenticatedAlephHttpClient( - account=aleph_account, api_server=config.ALEPH_API_URL + account=aleph_account, api_server=config.ALEPH_API_URL ) as client: post_message, _ = await client.create_post( address=config.ALEPH_OWNER, @@ -103,7 +104,7 @@ async def get_agent_secret(agent_id: str, signature: str) -> GetAgentSecretRespo async with aiohttp.ClientSession() as session: async with session.get( - url=f"{config.SUBSCRIPTION_BACKEND_URL}/subscriptions/{agent.subscription_id}" + url=f"{config.SUBSCRIPTION_BACKEND_URL}/subscriptions/{agent.subscription_id}" ) as response: data = await response.json() if response.status != HTTPStatus.OK: @@ -141,11 +142,13 @@ def get_agent_secret_message(agent_id: str) -> GetAgentSecretMessage: @app.put("/agent/{agent_id}", description="Deploy an agent or update it") async def update( - agent_id: str, - secret: str = Form(), - code: UploadFile = File(...), - packages: UploadFile = File(...), + agent_id: str, + secret: str = Form(), + env_variables: str = Form(), # actually dict[str, str] but the typing doesn't work well with forms + code: UploadFile = File(...), + packages: UploadFile = File(...), ) -> UpdateAgentResponse: + env_variables = json.loads(env_variables) agents = await fetch_agents([agent_id]) if len(agents) != 1: @@ -182,7 +185,7 @@ async def update( # Register the program aleph_account = ETHAccount(config.ALEPH_SENDER_SK) async with AuthenticatedAlephHttpClient( - account=aleph_account, api_server=config.ALEPH_API_URL + account=aleph_account, api_server=config.ALEPH_API_URL ) as client: vm_hash = agent.vm_hash @@ -192,6 +195,7 @@ async def update( program_ref=code_ref, entrypoint="run", runtime="63f07193e6ee9d207b7d1fcf8286f9aee34e6f12f101d2ec77c1229f92964696", + environment_variables=env_variables, channel=config.ALEPH_CHANNEL, encoding=Encoding.squashfs, persistent=False, @@ -207,6 +211,8 @@ async def update( ) vm_hash = message.item_hash + # TODO: update env_vars also if vm already created + # Updating the related POST message await client.create_post( address=config.ALEPH_OWNER, @@ -236,10 +242,12 @@ async def delete(body: DeleteAgentBody): aleph_account = ETHAccount(config.ALEPH_SENDER_SK) async with AuthenticatedAlephHttpClient( - account=aleph_account, api_server=config.ALEPH_API_URL + account=aleph_account, api_server=config.ALEPH_API_URL ) as client: + # TODO: should we delete STORE messages of the code / deps too ? await client.forget( address=config.ALEPH_OWNER, hashes=[agent.vm_hash], + channel=config.ALEPH_CHANNEL, reason="LibertAI Agent subscription ended", )