mirror of
https://github.com/Significant-Gravitas/AutoGPT.git
synced 2026-04-08 03:00:28 -04:00
code quality improvements
This commit is contained in:
@@ -44,7 +44,7 @@ def launch_darkly_context():
|
||||
|
||||
|
||||
@contextlib.asynccontextmanager
|
||||
async def lifespan_context(app: fastapi.FastAPI):
|
||||
async def lifespan_context(lifespan_app: fastapi.FastAPI):
|
||||
await backend.data.db.connect()
|
||||
await backend.data.block.initialize_blocks()
|
||||
await backend.data.user.migrate_and_encrypt_user_integrations()
|
||||
|
||||
@@ -385,14 +385,25 @@ async def update_graph(
|
||||
latest_version_number = max(g.version for g in existing_versions)
|
||||
graph.version = latest_version_number + 1
|
||||
|
||||
latest_version_graph = next(
|
||||
v for v in existing_versions if v.version == latest_version_number
|
||||
)
|
||||
current_active_version = next((v for v in existing_versions if v.is_active), None)
|
||||
try:
|
||||
latest_version_graph = next(
|
||||
v for v in existing_versions if v.version == latest_version_number
|
||||
)
|
||||
except StopIteration:
|
||||
raise HTTPException(404, detail=f"Graph #{graph_id} not found")
|
||||
|
||||
try:
|
||||
current_active_version = next(
|
||||
(v for v in existing_versions if v.is_active), None
|
||||
)
|
||||
except StopIteration:
|
||||
raise HTTPException(404, detail=f"Graph #{graph_id} not found")
|
||||
|
||||
if latest_version_graph.is_template != graph.is_template:
|
||||
raise HTTPException(
|
||||
400, detail="Changing is_template on an existing graph is forbidden"
|
||||
)
|
||||
|
||||
graph.is_active = not graph.is_template
|
||||
graph = graph_db.make_graph_model(graph, user_id)
|
||||
graph.reassign_ids(user_id=user_id)
|
||||
@@ -487,7 +498,7 @@ def execute_graph(
|
||||
graph_version: Optional[int] = None,
|
||||
) -> dict[str, Any]: # FIXME: add proper return type
|
||||
try:
|
||||
logger.info(f"Node input: {node_input}")
|
||||
logger.info("Node input: %s", node_input)
|
||||
graph_exec = execution_manager_client().add_execution(
|
||||
graph_id, node_input, user_id=user_id, graph_version=graph_version
|
||||
)
|
||||
@@ -633,7 +644,7 @@ async def create_api_key(
|
||||
)
|
||||
return CreateAPIKeyResponse(api_key=api_key, plain_text_key=plain_text)
|
||||
except APIKeyError as e:
|
||||
logger.error(f"Failed to create API key: {str(e)}")
|
||||
logger.error("Failed to create API key: %s", e)
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@@ -650,7 +661,7 @@ async def get_api_keys(
|
||||
try:
|
||||
return await list_user_api_keys(user_id)
|
||||
except APIKeyError as e:
|
||||
logger.error(f"Failed to list API keys: {str(e)}")
|
||||
logger.error("Failed to list API keys: %s", e)
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@@ -670,7 +681,7 @@ async def get_api_key(
|
||||
raise HTTPException(status_code=404, detail="API key not found")
|
||||
return api_key
|
||||
except APIKeyError as e:
|
||||
logger.error(f"Failed to get API key: {str(e)}")
|
||||
logger.error("Failed to get API key: %s", e)
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@@ -692,7 +703,7 @@ async def delete_api_key(
|
||||
except APIKeyPermissionError:
|
||||
raise HTTPException(status_code=403, detail="Permission denied")
|
||||
except APIKeyError as e:
|
||||
logger.error(f"Failed to revoke API key: {str(e)}")
|
||||
logger.error("Failed to revoke API key: %s", e)
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@@ -714,7 +725,7 @@ async def suspend_key(
|
||||
except APIKeyPermissionError:
|
||||
raise HTTPException(status_code=403, detail="Permission denied")
|
||||
except APIKeyError as e:
|
||||
logger.error(f"Failed to suspend API key: {str(e)}")
|
||||
logger.error("Failed to suspend API key: %s", e)
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@@ -738,5 +749,5 @@ async def update_permissions(
|
||||
except APIKeyPermissionError:
|
||||
raise HTTPException(status_code=403, detail="Permission denied")
|
||||
except APIKeyError as e:
|
||||
logger.error(f"Failed to update API key permissions: {str(e)}")
|
||||
logger.error("Failed to update API key permissions: %s", e)
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
@@ -5,7 +5,6 @@ import prisma.errors
|
||||
import prisma.models
|
||||
import prisma.types
|
||||
|
||||
import backend.data.graph
|
||||
import backend.data.includes
|
||||
import backend.server.model
|
||||
import backend.server.v2.library.model
|
||||
@@ -18,11 +17,11 @@ async def get_library_agents(
|
||||
user_id: str, search_query: str | None = None
|
||||
) -> list[backend.server.v2.library.model.LibraryAgent]:
|
||||
logger.debug(
|
||||
f"Fetching library agents for user_id={user_id} search_query={search_query}"
|
||||
"Fetching library agents for user_id=%s search_query=%s", user_id, search_query
|
||||
)
|
||||
|
||||
if search_query and len(search_query.strip()) > 100:
|
||||
logger.warning(f"Search query too long: {search_query}")
|
||||
logger.warning("Search query too long: %s", search_query)
|
||||
raise backend.server.v2.store.exceptions.DatabaseError(
|
||||
"Search query is too long."
|
||||
)
|
||||
@@ -61,13 +60,15 @@ async def get_library_agents(
|
||||
},
|
||||
order=[{"updatedAt": "desc"}],
|
||||
)
|
||||
logger.debug(f"Retrieved {len(library_agents)} agents for user_id={user_id}.")
|
||||
logger.debug(
|
||||
"Retrieved %s agents for user_id=%s.", len(library_agents), user_id
|
||||
)
|
||||
return [
|
||||
backend.server.v2.library.model.LibraryAgent.from_db(agent)
|
||||
for agent in library_agents
|
||||
]
|
||||
except prisma.errors.PrismaError as e:
|
||||
logger.error(f"Database error fetching library agents: {e}")
|
||||
logger.error("Database error fetching library agents: %s", e)
|
||||
raise backend.server.v2.store.exceptions.DatabaseError(
|
||||
"Unable to fetch library agents."
|
||||
)
|
||||
@@ -93,7 +94,7 @@ async def create_library_agent(
|
||||
)
|
||||
return library_agent
|
||||
except prisma.errors.PrismaError as e:
|
||||
logger.error(f"Database error creating agent to library: {str(e)}")
|
||||
logger.error("Database error creating agent to library: %s", e)
|
||||
raise backend.server.v2.store.exceptions.DatabaseError(
|
||||
"Failed to create agent to library"
|
||||
) from e
|
||||
@@ -122,7 +123,7 @@ async def update_agent_version_in_library(
|
||||
),
|
||||
)
|
||||
except prisma.errors.PrismaError as e:
|
||||
logger.error(f"Database error updating agent version in library: {str(e)}")
|
||||
logger.error("Database error updating agent version in library: %s", e)
|
||||
raise backend.server.v2.store.exceptions.DatabaseError(
|
||||
"Failed to update agent version in library"
|
||||
) from e
|
||||
@@ -150,7 +151,7 @@ async def update_library_agent(
|
||||
),
|
||||
)
|
||||
except prisma.errors.PrismaError as e:
|
||||
logger.error(f"Database error updating library agent: {str(e)}")
|
||||
logger.error("Database error updating library agent: %s", e)
|
||||
raise backend.server.v2.store.exceptions.DatabaseError(
|
||||
"Failed to update library agent"
|
||||
) from e
|
||||
@@ -164,7 +165,9 @@ async def add_store_agent_to_library(
|
||||
if they don't already have it
|
||||
"""
|
||||
logger.debug(
|
||||
f"Adding agent from store listing version {store_listing_version_id} to library for user {user_id}"
|
||||
"Adding agent from store listing version %s to library for user %s",
|
||||
store_listing_version_id,
|
||||
user_id,
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -177,17 +180,17 @@ async def add_store_agent_to_library(
|
||||
|
||||
if not store_listing_version or not store_listing_version.Agent:
|
||||
logger.warning(
|
||||
f"Store listing version not found: {store_listing_version_id}"
|
||||
"Store listing version not found: %s", store_listing_version_id
|
||||
)
|
||||
raise backend.server.v2.store.exceptions.AgentNotFoundError(
|
||||
f"Store listing version {store_listing_version_id} not found"
|
||||
"Store listing version %s not found", store_listing_version_id
|
||||
)
|
||||
|
||||
agent = store_listing_version.Agent
|
||||
|
||||
if agent.userId == user_id:
|
||||
logger.warning(
|
||||
f"User {user_id} cannot add their own agent to their library"
|
||||
"User %s cannot add their own agent to their library", user_id
|
||||
)
|
||||
raise backend.server.v2.store.exceptions.DatabaseError(
|
||||
"Cannot add own agent to library"
|
||||
@@ -204,7 +207,7 @@ async def add_store_agent_to_library(
|
||||
|
||||
if existing_user_agent:
|
||||
logger.debug(
|
||||
f"User {user_id} already has agent {agent.id} in their library"
|
||||
"User %s already has agent %s in their library", user_id, agent.id
|
||||
)
|
||||
return
|
||||
|
||||
@@ -217,12 +220,12 @@ async def add_store_agent_to_library(
|
||||
isCreatedByUser=False,
|
||||
)
|
||||
)
|
||||
logger.debug(f"Added agent {agent.id} to library for user {user_id}")
|
||||
logger.debug("Added agent %s to library for user %s", agent.id, user_id)
|
||||
|
||||
except backend.server.v2.store.exceptions.AgentNotFoundError:
|
||||
raise
|
||||
except prisma.errors.PrismaError as e:
|
||||
logger.error(f"Database error adding agent to library: {str(e)}")
|
||||
logger.error("Database error adding agent to library: %s", e)
|
||||
raise backend.server.v2.store.exceptions.DatabaseError(
|
||||
"Failed to add agent to library"
|
||||
) from e
|
||||
|
||||
@@ -5,7 +5,6 @@ import prisma.models
|
||||
import pytest
|
||||
from prisma import Prisma
|
||||
|
||||
import backend.data.includes
|
||||
import backend.server.v2.library.db as db
|
||||
import backend.server.v2.store.exceptions
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ import typing
|
||||
|
||||
import autogpt_libs.auth.depends
|
||||
import autogpt_libs.auth.middleware
|
||||
import autogpt_libs.utils.cache
|
||||
import fastapi
|
||||
|
||||
import backend.server.v2.library.db
|
||||
@@ -32,7 +31,7 @@ async def get_library_agents(
|
||||
agents = await backend.server.v2.library.db.get_library_agents(user_id)
|
||||
return agents
|
||||
except Exception as e:
|
||||
logger.exception(f"Exception occurred whilst getting library agents: {e}")
|
||||
logger.exception("Exception occurred whilst getting library agents: %s", e)
|
||||
raise fastapi.HTTPException(
|
||||
status_code=500, detail="Failed to get library agents"
|
||||
)
|
||||
@@ -76,13 +75,15 @@ async def add_agent_to_library(
|
||||
detail=f"Store listing version {store_listing_version_id} not found",
|
||||
)
|
||||
except backend.server.v2.store.exceptions.DatabaseError as e:
|
||||
logger.exception(f"Database error occurred whilst adding agent to library: {e}")
|
||||
logger.exception(
|
||||
"Database error occurred whilst adding agent to library: %s", e
|
||||
)
|
||||
raise fastapi.HTTPException(
|
||||
status_code=500, detail="Failed to add agent to library"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception(
|
||||
f"Unexpected exception occurred whilst adding agent to library: {e}"
|
||||
"Unexpected exception occurred whilst adding agent to library: %s", e
|
||||
)
|
||||
raise fastapi.HTTPException(
|
||||
status_code=500, detail="Failed to add agent to library"
|
||||
@@ -135,13 +136,13 @@ async def update_library_agent(
|
||||
return fastapi.Response(status_code=204)
|
||||
|
||||
except backend.server.v2.store.exceptions.DatabaseError as e:
|
||||
logger.exception(f"Database error occurred whilst updating library agent: {e}")
|
||||
logger.exception("Database error occurred whilst updating library agent: %s", e)
|
||||
raise fastapi.HTTPException(
|
||||
status_code=500, detail="Failed to update library agent"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception(
|
||||
f"Unexpected exception occurred whilst updating library agent: {e}"
|
||||
"Unexpected exception occurred whilst updating library agent: %s", e
|
||||
)
|
||||
raise fastapi.HTTPException(
|
||||
status_code=500, detail="Failed to update library agent"
|
||||
|
||||
@@ -6,10 +6,8 @@ import autogpt_libs.auth.middleware
|
||||
import autogpt_libs.utils.cache
|
||||
import fastapi
|
||||
|
||||
import backend.data.graph
|
||||
import backend.executor
|
||||
import backend.integrations.creds_manager
|
||||
import backend.integrations.webhooks.graph_lifecycle_hooks
|
||||
import backend.server.v2.library.db
|
||||
import backend.server.v2.library.model
|
||||
import backend.util.service
|
||||
@@ -98,7 +96,7 @@ async def update_preset(
|
||||
user_id, preset, preset_id
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception(f"Exception occurred whilst updating preset: {e}")
|
||||
logger.exception("Exception occurred whilst updating preset: %s", e)
|
||||
raise fastapi.HTTPException(status_code=500, detail="Failed to update preset")
|
||||
|
||||
|
||||
@@ -113,7 +111,7 @@ async def delete_preset(
|
||||
await backend.server.v2.library.db.delete_preset(user_id, preset_id)
|
||||
return fastapi.Response(status_code=204)
|
||||
except Exception as e:
|
||||
logger.exception(f"Exception occurred whilst deleting preset: {e}")
|
||||
logger.exception("Exception occurred whilst deleting preset: %s", e)
|
||||
raise fastapi.HTTPException(status_code=500, detail="Failed to delete preset")
|
||||
|
||||
|
||||
@@ -136,8 +134,8 @@ async def execute_preset(
|
||||
if not preset:
|
||||
raise fastapi.HTTPException(status_code=404, detail="Preset not found")
|
||||
|
||||
logger.info(f"Preset inputs: {preset.inputs}")
|
||||
logger.info(f"Node input: {node_input}")
|
||||
logger.info("Preset inputs: %s", preset.inputs)
|
||||
logger.info("Node input: %s", node_input)
|
||||
|
||||
updated_node_input = node_input.copy()
|
||||
if "node_input" not in updated_node_input:
|
||||
@@ -150,7 +148,7 @@ async def execute_preset(
|
||||
if key not in updated_node_input["node_input"]:
|
||||
updated_node_input["node_input"][key] = value
|
||||
|
||||
logger.info(f"Updated node input: {updated_node_input}")
|
||||
logger.info("Updated node input: %s", updated_node_input)
|
||||
|
||||
execution = execution_manager_client().add_execution(
|
||||
graph_id=graph_id,
|
||||
@@ -160,7 +158,7 @@ async def execute_preset(
|
||||
preset_id=preset_id,
|
||||
)
|
||||
|
||||
logger.info(f"Execution added: {execution} with input: {updated_node_input}")
|
||||
logger.info("Execution added: %s with input: %s", execution, updated_node_input)
|
||||
|
||||
return {"id": execution.graph_exec_id}
|
||||
except Exception as e:
|
||||
|
||||
@@ -92,17 +92,23 @@ async def test_block_credit_reset(server: SpinTestServer):
|
||||
month2 = 2
|
||||
|
||||
# set the calendar to month 2 but use current time from now
|
||||
user_credit.time_now = lambda: datetime.now(timezone.utc).replace(month=month2)
|
||||
user_credit.time_now = lambda: datetime.now(timezone.utc).replace(
|
||||
month=month2, day=12
|
||||
)
|
||||
month2credit = await user_credit.get_credits(DEFAULT_USER_ID)
|
||||
|
||||
# Month 1 result should only affect month 1
|
||||
user_credit.time_now = lambda: datetime.now(timezone.utc).replace(month=month1)
|
||||
user_credit.time_now = lambda: datetime.now(timezone.utc).replace(
|
||||
month=month1, day=12
|
||||
)
|
||||
month1credit = await user_credit.get_credits(DEFAULT_USER_ID)
|
||||
await top_up(100)
|
||||
assert await user_credit.get_credits(DEFAULT_USER_ID) == month1credit + 100
|
||||
|
||||
# Month 2 balance is unaffected
|
||||
user_credit.time_now = lambda: datetime.now(timezone.utc).replace(month=month2)
|
||||
user_credit.time_now = lambda: datetime.now(timezone.utc).replace(
|
||||
month=month2, day=12
|
||||
)
|
||||
assert await user_credit.get_credits(DEFAULT_USER_ID) == month2credit
|
||||
|
||||
|
||||
|
||||
@@ -480,7 +480,6 @@ async def test_execute_preset_with_clash(server: SpinTestServer):
|
||||
|
||||
# Wait for execution to complete
|
||||
executions = await wait_execution(test_user.id, test_graph.id, graph_exec_id)
|
||||
# assert executions == [], f"Executions: {executions}"
|
||||
assert len(executions) == 4
|
||||
|
||||
# FindInDictionaryBlock should wait for the input pin to be provided,
|
||||
|
||||
Reference in New Issue
Block a user