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

Code before:
                "id": id,
                "userId": user_id,
                "isDeleted": False,
            },
            include={
                "Agent": {
                    "include": {
                        **backend.data.includes.AGENT_GRAPH_INCLUDE,
                        "AgentGraphExecution": {"where": {"userId": user_id}},
                    }
                },
                "Creator": True,
            },
        )

        if not library_agent:
            raise store_exceptions.AgentNotFoundError(f"Library agent #{id} not found")

        return library_model.LibraryAgent.from_db(library_agent)

    except prisma.errors.PrismaError as e:
        logger.error(f"Database error fetching library agent: {e}")
        raise store_exceptions.DatabaseError("Failed to fetch library agent") from e


async def generate_agent_image(
    agent: backend.data.graph.GraphModel,
    library_agent_id: str,
) -> Optional[prisma.models.LibraryAgent]:
    """
    Generates an image for the specified LibraryAgent and updates its record.
    """
    user_id = agent.user_id
    agent_id = agent.id

    # Use .jpeg here since we are generating JPEG images
    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:

Review comment (suggestion):
nit:
```suggestion
async def add_generated_agent_image(
```

Code after:
        library_agent = await prisma.models.LibraryAgent.prisma().find_first(
            where={
                "id": id,
                "userId": user_id,
                "isDeleted": False,
            },
            include={
                "Agent": {
                    "include": {
                        **backend.data.includes.AGENT_GRAPH_INCLUDE,
                        "AgentGraphExecution": {"where": {"userId": user_id}},
                    }
                },
                "Creator": True,
            },
        )

        if not library_agent:
            raise store_exceptions.AgentNotFoundError(f"Library agent #{id} not found")

        return library_model.LibraryAgent.from_db(library_agent)

    except prisma.errors.PrismaError as e:
        logger.error(f"Database error fetching library agent: {e}")
        raise store_exceptions.DatabaseError("Failed to fetch library agent") from e


async def add_generated_agent_image(
    graph: backend.data.graph.GraphModel,
    library_agent_id: str,
) -> Optional[prisma.models.LibraryAgent]:
    """
    Generates an image for the specified LibraryAgent and updates its record.
    """
    user_id = graph.user_id
    graph_id = graph.id

    # 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)