Skip to content

Commit

Permalink
initial setup, no CLI/URL
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-agenta committed Dec 20, 2024
1 parent 171fc7c commit 4800b5b
Show file tree
Hide file tree
Showing 5 changed files with 408 additions and 70 deletions.
7 changes: 7 additions & 0 deletions agenta-backend/agenta_backend/models/api/api_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ class AddVariantFromImagePayload(BaseModel):
config_name: Optional[str]


class AddVariantFromURLPayload(BaseModel):
variant_name: str
url: str
base_name: Optional[str]
config_name: Optional[str]


class ImageExtended(Image):
# includes the mongodb image id
id: str
Expand Down
88 changes: 83 additions & 5 deletions agenta-backend/agenta_backend/routers/app_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
UpdateAppOutput,
CreateAppOutput,
AddVariantFromImagePayload,
AddVariantFromURLPayload,
)

if isCloudEE():
Expand Down Expand Up @@ -342,7 +343,6 @@ async def add_variant_from_image(
Args:
app_id (str): The ID of the app to add the variant to.
payload (AddVariantFromImagePayload): The payload containing information about the variant to add.
stoken_session (SessionContainer, optional): The session container. Defaults to Depends(verify_session()).
Raises:
HTTPException: If the feature flag is set to "demo" or if the image does not have a tag starting with the registry name (agenta-server) or if the image is not found or if the user does not have access to the app.
Expand All @@ -367,6 +367,7 @@ async def add_variant_from_image(

try:
app = await db_manager.fetch_app_by_id(app_id)

if isCloudEE():
has_permission = await check_action_access(
user_uid=request.state.user_id,
Expand Down Expand Up @@ -394,14 +395,91 @@ async def add_variant_from_image(
is_template_image=False,
user_uid=request.state.user_id,
)
app_variant_db = await db_manager.fetch_app_variant_by_id(str(variant_db.id))

logger.debug("Step 8: We create ready-to use evaluators")
app_variant_db = await db_manager.fetch_app_variant_by_id(
str(variant_db.id),
)

await evaluator_manager.create_ready_to_use_evaluators(
app_name=app.app_name, project_id=str(app.project_id)
app_name=app.app_name,
project_id=str(app.project_id),
)

return await converters.app_variant_db_to_output(app_variant_db)
app_variant_dto = await converters.app_variant_db_to_output(
app_variant_db,
)

return app_variant_dto

except Exception as e:
logger.exception(f"An error occurred: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))


@router.post("/{app_id}/variant/from-url/", operation_id="add_variant_from_url")
async def add_variant_from_url(
app_id: str,
payload: AddVariantFromURLPayload,
request: Request,
):
"""
Add a new variant to an app based on a URL.
Args:
app_id (str): The ID of the app to add the variant to.
payload (AddVariantFromURLPayload): The payload containing information about the variant to add.
Raises:
HTTPException: If the user does not have access to the app or if there is an error adding the variant.
Returns:
dict: The newly added variant.
"""

try:
app = await db_manager.fetch_app_by_id(app_id)

if isCloudEE():
has_permission = await check_action_access(
user_uid=request.state.user_id,
object=app,
permission=Permission.CREATE_APPLICATION,
)
logger.debug(
f"User has Permission to create app from url: {has_permission}"
)
if not has_permission:
error_msg = f"You do not have access to perform this action. Please contact your organization admin."
return JSONResponse(
{"detail": error_msg},
status_code=403,
)

variant_db = await app_manager.add_variant_based_on_url(
app=app,
project_id=str(app.project_id),
variant_name=payload.variant_name,
url=payload.url,
base_name=payload.base_name,
config_name=payload.config_name,
user_uid=request.state.user_id,
)

app_variant_db = await db_manager.fetch_app_variant_by_id(
str(variant_db.id),
)

await evaluator_manager.create_ready_to_use_evaluators(
app_name=app.app_name,
project_id=str(app.project_id),
)

app_variant_dto = await converters.app_variant_db_to_output(
app_variant_db,
)

return app_variant_dto

except Exception as e:
logger.exception(f"An error occurred: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
Expand Down
94 changes: 91 additions & 3 deletions agenta-backend/agenta_backend/routers/variants_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,88 @@ async def update_variant_image(
)

await app_manager.update_variant_image(
db_app_variant, str(db_app_variant.project_id), image, request.state.user_id
db_app_variant,
str(db_app_variant.project_id),
image,
request.state.user_id,
)

# Update last_modified_by app information
await app_manager.update_last_modified_by(
user_uid=request.state.user_id,
object_id=str(db_app_variant.app_id),
object_type="app",
project_id=str(db_app_variant.project_id),
)
logger.debug("Successfully updated last_modified_by app information")

except ValueError as e:
import traceback

traceback.print_exc()
detail = f"Error while trying to update the app variant: {str(e)}"
raise HTTPException(status_code=500, detail=detail)
except DockerException as e:
import traceback

traceback.print_exc()
detail = f"Docker error while trying to update the app variant: {str(e)}"
raise HTTPException(status_code=500, detail=detail)
except Exception as e:
import traceback

traceback.print_exc()
detail = f"Unexpected error while trying to update the app variant: {str(e)}"
raise HTTPException(status_code=500, detail=detail)


@router.put("/{variant_id}/url/", operation_id="update_variant_url")
async def update_variant_url(
variant_id: str,
url: str,
request: Request,
):
"""
Updates the URL used in an app variant.
Args:
variant_id (str): The ID of the app variant to update.
url (str): The URL to update.
Raises:
HTTPException: If an error occurs while trying to update the app variant.
Returns:
JSONResponse: A JSON response indicating whether the update was successful or not.
"""

try:
db_app_variant = await db_manager.fetch_app_variant_by_id(
app_variant_id=variant_id
)

if isCloudEE():
has_permission = await check_action_access(
user_uid=request.state.user_id,
project_id=str(db_app_variant.project_id),
permission=Permission.CREATE_APPLICATION,
)
logger.debug(
f"User has Permission to update variant image: {has_permission}"
)
if not has_permission:
error_msg = f"You do not have permission to perform this action. Please contact your organization admin."
logger.error(error_msg)
return JSONResponse(
{"detail": error_msg},
status_code=403,
)

await app_manager.update_variant_url(
db_app_variant,
str(db_app_variant.project_id),
url,
request.state.user_id,
)

# Update last_modified_by app information
Expand All @@ -290,6 +371,7 @@ async def update_variant_image(
project_id=str(db_app_variant.project_id),
)
logger.debug("Successfully updated last_modified_by app information")

except ValueError as e:
import traceback

Expand Down Expand Up @@ -382,8 +464,14 @@ async def retrieve_variant_logs(
try:
app_variant = await db_manager.fetch_app_variant_by_id(variant_id)
deployment = await db_manager.get_deployment_by_appid(str(app_variant.app.id))
logs_result = await logs_manager.retrieve_logs(deployment.container_id)
return logs_result
if deployment.container_id is not None:
logs_result = await logs_manager.retrieve_logs(deployment.container_id)
return logs_result
else:
raise HTTPException(
404,
detail="No logs available for this variant.",
)
except Exception as exc:
logger.exception(f"An error occurred: {str(exc)}")
raise HTTPException(500, {"message": str(exc)})
Expand Down
Loading

0 comments on commit 4800b5b

Please sign in to comment.