Skip to content

Commit

Permalink
chore(agents-api): misc fixes + refators
Browse files Browse the repository at this point in the history
  • Loading branch information
Vedantsahai18 committed Jan 4, 2025
1 parent 9c12ad3 commit 7412ca2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 37 deletions.
6 changes: 1 addition & 5 deletions agents-api/agents_api/activities/execute_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,10 @@ async def execute_system(
# Handle special cases for doc operations
if system.operation == "create" and system.subresource == "doc":
arguments["x_developer_id"] = arguments.pop("developer_id")
bg_runner = BackgroundTasks()
res = await handler(
return await handler(
data=CreateDocRequest(**arguments.pop("data")),
background_tasks=bg_runner,
**arguments,
)
await bg_runner()
return res

# Handle search operations
if system.operation == "search" and system.subresource == "doc":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _recursive_evaluate(expr, evaluator: SimpleEval):
}

activity.logger.error(
f"Error in base_evaluate: {evaluate_error}\nVariables accessed: {variables_accessed}"
f"Error in base_evaluate: {evaluate_error}\n"
)
raise evaluate_error from e
elif isinstance(expr, list):
Expand Down
21 changes: 14 additions & 7 deletions agents-api/agents_api/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
from contextlib import asynccontextmanager
from typing import Any, Protocol
from typing import Protocol

from aiobotocore.client import AioBaseClient
from aiobotocore.session import get_session
from asyncpg.pool import Pool
from fastapi import APIRouter, FastAPI
from prometheus_fastapi_instrumentator import Instrumentator
from scalar_fastapi import get_scalar_api_reference
Expand All @@ -11,17 +13,18 @@
from .env import api_prefix, hostname, protocol, public_port


class Assignable(Protocol):
def __setattr__(self, name: str, value: Any) -> None: ...
class State(Protocol):
postgres_pool: Pool | None
s3_client: AioBaseClient | None


class ObjectWithState(Protocol):
state: Assignable
state: State


# TODO: This currently doesn't use env.py, we should move to using them
@asynccontextmanager
async def lifespan(*containers: list[FastAPI | ObjectWithState]):
async def lifespan(*containers: FastAPI | ObjectWithState):
# INIT POSTGRES #
pg_dsn = os.environ.get("PG_DSN")

Expand Down Expand Up @@ -50,13 +53,17 @@ async def lifespan(*containers: list[FastAPI | ObjectWithState]):
# CLOSE POSTGRES #
for container in containers:
if hasattr(container, "state") and getattr(container.state, "postgres_pool", None):
await container.state.postgres_pool.close()
pool = getattr(container.state, "postgres_pool", None)
if pool:
await pool.close()
container.state.postgres_pool = None

# CLOSE S3 #
for container in containers:
if hasattr(container, "state") and getattr(container.state, "s3_client", None):
await container.state.s3_client.close()
s3_client = getattr(container.state, "s3_client", None)
if s3_client:
await s3_client.close()
container.state.s3_client = None


Expand Down
23 changes: 0 additions & 23 deletions agents-api/agents_api/common/utils/debug.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _transform(d):
Transition,
transform=_transform,
)
@pg_query(debug=True)
@pg_query
@beartype
async def list_execution_transitions(
*,
Expand Down

0 comments on commit 7412ca2

Please sign in to comment.