Skip to content

Commit

Permalink
feat(backend): Working deploy route
Browse files Browse the repository at this point in the history
  • Loading branch information
RezaRahemtola committed Oct 31, 2024
1 parent 1221a03 commit c3c4285
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
5 changes: 2 additions & 3 deletions backend/src/interfaces/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ class SetupAgentBody(DeleteAgentBody):
account: SubscriptionAccount


class UpdateAgentPutBody(BaseModel):
id: str
secret: str
class UpdateAgentResponse(BaseModel):
vm_hash: str


class Agent(BaseModel):
Expand Down
20 changes: 13 additions & 7 deletions backend/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
from aleph.sdk.chains.ethereum import ETHAccount
from aleph_message.models.execution import Encoding
from ecies import encrypt, decrypt
from fastapi import FastAPI, HTTPException, UploadFile
from fastapi import FastAPI, HTTPException, UploadFile, File, Form
from starlette.middleware.cors import CORSMiddleware

from src.config import config
from src.interfaces.agent import (
Agent,
UpdateAgentPutBody,
SetupAgentBody,
DeleteAgentBody,
UpdateAgentResponse,
)
from src.interfaces.aleph import AlephVolume
from src.utils.agent import fetch_agents, fetch_agent_program_message
Expand Down Expand Up @@ -67,13 +67,18 @@ async def setup(body: SetupAgentBody) -> None:


@app.put("/agent", description="Deploy an agent or update it")
async def update(body: UpdateAgentPutBody, code: UploadFile, packages: UploadFile):
agents = await fetch_agents([body.id])
async def update(
agent_id: str = Form(),
secret: str = Form(),
code: UploadFile = File(...),
packages: UploadFile = File(...),
) -> UpdateAgentResponse:
agents = await fetch_agents([agent_id])

if len(agents) != 1:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail=f"Agent with ID {body.id} not found.",
detail=f"Agent with ID {agent_id} not found.",
)
agent = agents[0]
agent_program = (
Expand All @@ -86,7 +91,7 @@ async def update(body: UpdateAgentPutBody, code: UploadFile, packages: UploadFil
encrypted_secret = base64.b64decode(agent.encrypted_secret)

decrypted_secret = decrypt(config.ALEPH_SENDER_SK, encrypted_secret).decode()
if body.secret != decrypted_secret:
if secret != decrypted_secret:
raise HTTPException(
status_code=HTTPStatus.UNAUTHORIZED,
detail="The secret provided doesn't match the one of this agent.",
Expand All @@ -105,7 +110,7 @@ async def update(body: UpdateAgentPutBody, code: UploadFile, packages: UploadFil

if agent_program is not None:
# Program is already deployed and we updated the volumes, exiting here
return
return UpdateAgentResponse(vm_hash=agent_program.item_hash)

# Register the program
aleph_account = ETHAccount(config.ALEPH_SENDER_SK)
Expand Down Expand Up @@ -140,6 +145,7 @@ async def update(body: UpdateAgentPutBody, code: UploadFile, packages: UploadFil
ref=agent.post_hash,
channel=config.ALEPH_CHANNEL,
)
return UpdateAgentResponse(vm_hash=message.item_hash)


@app.delete("/agent", description="Remove an agent on subscription end")
Expand Down

0 comments on commit c3c4285

Please sign in to comment.