Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/backend/backend/server/routers/v1.py (Python)

Code before:

@v1_router.get(path="/credits", dependencies=[Depends(auth_middleware)])
async def get_user_credits(
    user_id: Annotated[str, Depends(get_user_id)]
) -> dict[str, int]:
    # Credits can go negative, so ensure it's at least 0 for user to see.
    return {"credits": max(await _user_credit_model.get_or_refill_credit(user_id), 0)}


########################################################
##################### Graphs ###########################
########################################################


class DeleteGraphResponse(TypedDict):
    version_counts: int


@v1_router.get(path="/graphs", tags=["graphs"], dependencies=[Depends(auth_middleware)])
async def get_graphs(
    user_id: Annotated[str, Depends(get_user_id)],
    with_runs: bool = False,
    filter_by: Literal["all", "active", "template"] | None = "active",
) -> list[graph_db.Graph]:
    return await graph_db.get_graphs(
        include_executions=with_runs, filter_by=filter_by, user_id=user_id
    )


@v1_router.get(
    path="/graphs/{graph_id}", tags=["graphs"], dependencies=[Depends(auth_middleware)]
)
@v1_router.get(
    path="/graphs/{graph_id}/versions/{version}",
    tags=["graphs"],
    dependencies=[Depends(auth_middleware)],
)
async def get_graph(
    graph_id: str,
    user_id: Annotated[str, Depends(get_user_id)],
    version: int | None = None,
    hide_credentials: bool = False,
) -> graph_db.Graph:
    graph = await graph_db.get_graph(
        graph_id, version, user_id=user_id, hide_credentials=hide_credentials
    )
    if not graph:
        raise HTTPException(status_code=404, detail=f"Graph #{graph_id} not found.")
    return graph



Review comment (suggestion):
```suggestion
) -> list[graph_db.Graph]:
    return await graph_db.get_graphs(
        include_executions=with_runs, filter_by="active" if not with_runs else None, user_id=user_id

Code after:
    output = defaultdict(list)
    for name, data in obj.execute(data):
        output[name].append(data)
    return output


########################################################
##################### Credits ##########################
########################################################


@v1_router.get(path="/credits", dependencies=[Depends(auth_middleware)])
async def get_user_credits(
    user_id: Annotated[str, Depends(get_user_id)]
) -> dict[str, int]:
    # Credits can go negative, so ensure it's at least 0 for user to see.
    return {"credits": max(await _user_credit_model.get_or_refill_credit(user_id), 0)}


########################################################
##################### Graphs ###########################
########################################################


class DeleteGraphResponse(TypedDict):
    version_counts: int


@v1_router.get(path="/graphs", tags=["graphs"], dependencies=[Depends(auth_middleware)])
async def get_graphs(
    user_id: Annotated[str, Depends(get_user_id)],
    with_runs: bool = False,
) -> Sequence[graph_db.Graph]:
    return await graph_db.get_graphs(
        include_executions=with_runs, filter_by="active", user_id=user_id
    )


@v1_router.get(
    path="/graphs/{graph_id}", tags=["graphs"], dependencies=[Depends(auth_middleware)]
)
@v1_router.get(
    path="/graphs/{graph_id}/versions/{version}",
    tags=["graphs"],
    dependencies=[Depends(auth_middleware)],
)
async def get_graph(
    graph_id: str,
    user_id: Annotated[str, Depends(get_user_id)],
    version: int | None = None,
    hide_credentials: bool = False,