Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend: support environment variables #3

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/src/interfaces/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
29 changes: 20 additions & 9 deletions backend/src/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import time
from http import HTTPStatus
from uuid import uuid4
Expand Down Expand Up @@ -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,
Expand All @@ -83,7 +84,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,
)


Expand All @@ -100,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:
Expand Down Expand Up @@ -138,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:
Expand Down Expand Up @@ -179,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

Expand All @@ -189,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,
Expand All @@ -204,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,
Expand Down Expand Up @@ -233,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",
)
2 changes: 1 addition & 1 deletion backend/src/utils/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
)
Expand Down