Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/backend/backend/server/v2/library/db.py (Python)

Code before:
    filename = f"agent_{agent_id}.jpeg"
    try:
        if not (image_url := await store_media.check_media_exists(user_id, filename)):
            # Generate agent image as JPEG
            image = await asyncio.to_thread(
                store_image_gen.generate_agent_image_v2, agent=agent
            )

            # Create UploadFile with the correct filename and content_type
            image_file = fastapi.UploadFile(file=image, filename=filename)

            image_url = await store_media.upload_media(
                user_id=user_id, file=image_file, use_file_name=True
            )
    except Exception as e:
        logger.warning(f"Error generating and uploading agent image: {e}")
        return None

    return await prisma.models.LibraryAgent.prisma().update(
        where={"id": library_agent_id},
        data={"imageUrl": image_url},
    )


async def create_library_agent(
    agent: backend.data.graph.GraphModel,
    user_id: str,
) -> prisma.models.LibraryAgent:
    """
    Adds an agent to the user's library (LibraryAgent table).

    Args:
        agent: The agent/Graph to add to the library.
        user_id: The user to whom the agent will be added.

    Returns:
        The newly created LibraryAgent record.

    Raises:
        AgentNotFoundError: If the specified agent does not exist.
        DatabaseError: If there's an error during creation or if image generation fails.
    """
    logger.info(
        f"Creating library agent for graph #{agent.id} v{agent.version}; "
        f"user #{user_id}"
    )

    try:
        return await prisma.models.LibraryAgent.prisma().create(
            data={
                "isCreatedByUser": (user_id == agent.user_id),

Review comment (suggestion):
this seems unnecessary? would require fetching the `GraphModel` to call this function even if the graph ID and version are already available from e.g. a `StoreListingVersion`

Code after:

    # Use .jpeg here since we are generating JPEG images
    filename = f"agent_{graph_id}.jpeg"
    try:
        if not (image_url := await store_media.check_media_exists(user_id, filename)):
            # Generate agent image as JPEG
            if config.use_agent_image_generation_v2:
                image = await asyncio.to_thread(
                    store_image_gen.generate_agent_image_v2, graph=graph
                )
            else:
                image = await store_image_gen.generate_agent_image(agent=graph)

            # Create UploadFile with the correct filename and content_type
            image_file = fastapi.UploadFile(file=image, filename=filename)

            image_url = await store_media.upload_media(
                user_id=user_id, file=image_file, use_file_name=True
            )
    except Exception as e:
        logger.warning(f"Error generating and uploading agent image: {e}")
        return None

    return await prisma.models.LibraryAgent.prisma().update(
        where={"id": library_agent_id},
        data={"imageUrl": image_url},
    )


async def create_library_agent(
    graph: backend.data.graph.GraphModel,
    user_id: str,
) -> prisma.models.LibraryAgent:
    """
    Adds an agent to the user's library (LibraryAgent table).

    Args:
        agent: The agent/Graph to add to the library.
        user_id: The user to whom the agent will be added.

    Returns:
        The newly created LibraryAgent record.

    Raises:
        AgentNotFoundError: If the specified agent does not exist.
        DatabaseError: If there's an error during creation or if image generation fails.
    """
    logger.info(
        f"Creating library agent for graph #{graph.id} v{graph.version}; "
        f"user #{user_id}"
    )