Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(backend): Apply lint on autogpt_lib folder on backend/linter.py #8751

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from typing import NamedTuple
import secrets
import hashlib
import secrets
from typing import NamedTuple


class APIKeyContainer(NamedTuple):
"""Container for API key parts."""

raw: str
prefix: str
postfix: str
hash: str


class APIKeyManager:
PREFIX: str = "agpt_"
PREFIX_LENGTH: int = 8
Expand All @@ -19,9 +22,9 @@ def generate_api_key(self) -> APIKeyContainer:
raw_key = f"{self.PREFIX}{secrets.token_urlsafe(32)}"
return APIKeyContainer(
raw=raw_key,
prefix=raw_key[:self.PREFIX_LENGTH],
postfix=raw_key[-self.POSTFIX_LENGTH:],
hash=hashlib.sha256(raw_key.encode()).hexdigest()
prefix=raw_key[: self.PREFIX_LENGTH],
postfix=raw_key[-self.POSTFIX_LENGTH :],
hash=hashlib.sha256(raw_key.encode()).hexdigest(),
)

def verify_api_key(self, provided_key: str, stored_hash: str) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions autogpt_platform/autogpt_libs/autogpt_libs/auth/depends.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fastapi

from .middleware import auth_middleware
from .models import User, DEFAULT_USER_ID, DEFAULT_EMAIL
from .config import Settings
from .middleware import auth_middleware
from .models import DEFAULT_USER_ID, User


def requires_user(payload: dict = fastapi.Depends(auth_middleware)) -> User:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from pydantic import Field, field_validator
from pydantic_settings import BaseSettings, SettingsConfigDict

from .filters import BelowLevelFilter
from .formatters import AGPTFormatter, StructuredLoggingFormatter

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .store import SupabaseIntegrationCredentialsStore
from .types import Credentials, APIKeyCredentials, OAuth2Credentials
from .types import APIKeyCredentials, Credentials, OAuth2Credentials

__all__ = [
"SupabaseIntegrationCredentialsStore",
Expand Down
majdyz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
from pydantic import SecretStr

if TYPE_CHECKING:
from redis import Redis
from backend.executor.database import DatabaseManager
from redis import Redis

from autogpt_libs.utils.cache import thread_cached
from autogpt_libs.utils.settings import Settings
from autogpt_libs.utils.synchronize import RedisKeyedMutex

from .types import (
Expand All @@ -19,8 +20,6 @@
UserIntegrations,
)

from backend.util.settings import Settings

settings = Settings()

revid_credentials = APIKeyCredentials(
Expand Down Expand Up @@ -305,5 +304,5 @@ def _get_user_integrations(self, user_id: str) -> UserIntegrations:
return integrations

def locked_user_integrations(self, user_id: str):
key = (self.db_manager, f"user:{user_id}", "integrations")
key = (f"user:{user_id}", "integrations")
majdyz marked this conversation as resolved.
Show resolved Hide resolved
return self.locks.locked(key)
Empty file.
2 changes: 1 addition & 1 deletion autogpt_platform/autogpt_libs/autogpt_libs/utils/cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Callable, TypeVar, ParamSpec
import threading
from typing import Callable, ParamSpec, TypeVar

P = ParamSpec("P")
R = TypeVar("R")
Expand Down
2 changes: 1 addition & 1 deletion autogpt_platform/backend/backend/blocks/github/_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
APIKeyCredentials,
OAuth2Credentials,
)
from autogpt_libs.utils.settings import Secrets
from pydantic import SecretStr

from backend.data.model import CredentialsField, CredentialsMetaInput
from backend.util.settings import Secrets

secrets = Secrets()
GITHUB_OAUTH_IS_CONFIGURED = bool(
Expand Down
2 changes: 1 addition & 1 deletion autogpt_platform/backend/backend/blocks/google/_auth.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Literal

from autogpt_libs.supabase_integration_credentials_store.types import OAuth2Credentials
from autogpt_libs.utils.settings import Secrets
from pydantic import SecretStr

from backend.data.model import CredentialsField, CredentialsMetaInput
from backend.util.settings import Secrets

# --8<-- [start:GoogleOAuthIsConfigured]
secrets = Secrets()
Expand Down
2 changes: 1 addition & 1 deletion autogpt_platform/backend/backend/blocks/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
import anthropic
import ollama
import openai
from autogpt_libs.utils.settings import BehaveAs, Settings
from groq import Groq

from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchema
from backend.data.model import CredentialsField, CredentialsMetaInput, SchemaField
from backend.util import json
from backend.util.settings import BehaveAs, Settings

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion autogpt_platform/backend/backend/data/credit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from abc import ABC, abstractmethod
from datetime import datetime, timezone

from autogpt_libs.utils.settings import Config
from prisma import Json
from prisma.enums import UserBlockCreditType
from prisma.errors import UniqueViolationError
Expand All @@ -9,7 +10,6 @@
from backend.data.block import Block, BlockInput, get_block
from backend.data.block_cost_config import BLOCK_COSTS
from backend.data.cost import BlockCost, BlockCostType
from backend.util.settings import Config

config = Config()

Expand Down
3 changes: 1 addition & 2 deletions autogpt_platform/backend/backend/data/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any, Callable, ClassVar, Generic, Optional, TypeVar

from autogpt_libs.supabase_integration_credentials_store.types import CredentialsType
from autogpt_libs.utils.settings import Secrets
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic_core import (
CoreSchema,
Expand All @@ -12,8 +13,6 @@
core_schema,
)

from backend.util.settings import Secrets

T = TypeVar("T")
logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion autogpt_platform/backend/backend/data/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from datetime import datetime
from typing import Any, AsyncGenerator, Generator, Generic, TypeVar

from autogpt_libs.utils.settings import Config
from pydantic import BaseModel
from redis.asyncio.client import PubSub as AsyncPubSub
from redis.client import PubSub

from backend.data import redis
from backend.data.execution import ExecutionResult
from backend.util.settings import Config

logger = logging.getLogger(__name__)
config = Config()
Expand Down
3 changes: 2 additions & 1 deletion autogpt_platform/backend/backend/executor/database.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from functools import wraps
from typing import Any, Callable, Concatenate, Coroutine, ParamSpec, TypeVar, cast

from autogpt_libs.utils.settings import Config

from backend.data.credit import get_user_credit_model
from backend.data.execution import (
ExecutionResult,
Expand All @@ -23,7 +25,6 @@
update_user_metadata,
)
from backend.util.service import AppService, expose
from backend.util.settings import Config

P = ParamSpec("P")
R = TypeVar("R")
Expand Down
2 changes: 1 addition & 1 deletion autogpt_platform/backend/backend/executor/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from backend.executor import DatabaseManager

from autogpt_libs.utils.cache import thread_cached
from autogpt_libs.utils.settings import Settings

from backend.blocks.agent import AgentExecutorBlock
from backend.data import redis
Expand All @@ -43,7 +44,6 @@
expose,
get_service_client,
)
from backend.util.settings import Settings
from backend.util.type import convert

logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion autogpt_platform/backend/backend/executor/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
from autogpt_libs.utils.cache import thread_cached
from autogpt_libs.utils.settings import Config
from dotenv import load_dotenv
from pydantic import BaseModel
from sqlalchemy import MetaData, create_engine

from backend.data.block import BlockInput
from backend.executor.manager import ExecutionManager
from backend.util.service import AppService, expose, get_service_client
from backend.util.settings import Config


def _extract_schema_from_url(database_url) -> tuple[str, str]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
Credentials,
SupabaseIntegrationCredentialsStore,
)
from autogpt_libs.utils.settings import Settings
from autogpt_libs.utils.synchronize import RedisKeyedMutex
from redis.lock import Lock as RedisLock

from backend.data import redis
from backend.integrations.oauth import HANDLERS_BY_NAME, BaseOAuthHandler
from backend.util.settings import Settings

logger = logging.getLogger(__name__)
settings = Settings()
Expand Down Expand Up @@ -129,7 +129,6 @@ def delete(self, user_id: str, credentials_id: str) -> None:

def _acquire_lock(self, user_id: str, credentials_id: str, *args: str) -> RedisLock:
key = (
self.store.db_manager,
majdyz marked this conversation as resolved.
Show resolved Hide resolved
f"user:{user_id}",
f"credentials:{credentials_id}",
*args,
Expand Down
3 changes: 2 additions & 1 deletion autogpt_platform/backend/backend/rest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from backend.app import run_processes
from backend.executor import ExecutionScheduler
from backend.executor import DatabaseManager, ExecutionScheduler
from backend.server.rest_api import AgentServer


Expand All @@ -8,6 +8,7 @@ def main():
Run all the processes required for the AutoGPT-server REST API.
"""
run_processes(
DatabaseManager(),
majdyz marked this conversation as resolved.
Show resolved Hide resolved
ExecutionScheduler(),
AgentServer(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
CredentialsType,
OAuth2Credentials,
)
from autogpt_libs.utils.settings import Settings
from fastapi import APIRouter, Body, Depends, HTTPException, Path, Query, Request
from pydantic import BaseModel, Field, SecretStr

from backend.integrations.creds_manager import IntegrationCredentialsManager
from backend.integrations.oauth import HANDLERS_BY_NAME, BaseOAuthHandler
from backend.util.settings import Settings

from ..utils import get_user_id

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from autogpt_libs.utils.settings import Settings
from supabase import Client, create_client

from backend.util.settings import Settings

settings = Settings()


Expand Down
10 changes: 5 additions & 5 deletions autogpt_platform/backend/backend/server/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import typing

import autogpt_libs.utils.settings
import fastapi
import fastapi.responses
import starlette.middleware.cors
Expand All @@ -13,9 +14,8 @@
import backend.data.user
import backend.server.routers.v1
import backend.util.service
import backend.util.settings

settings = backend.util.settings.Settings()
settings = autogpt_libs.utils.settings.Settings()
logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -46,7 +46,7 @@ def handler(request: fastapi.Request, exc: Exception):

docs_url = (
"/docs"
if settings.config.app_env == backend.util.settings.AppEnvironment.LOCAL
if settings.config.app_env == autogpt_libs.utils.settings.AppEnvironment.LOCAL
else None
)

Expand Down Expand Up @@ -83,8 +83,8 @@ def run(self):
)
uvicorn.run(
server_app,
host=backend.util.settings.Config().agent_api_host,
port=backend.util.settings.Config().agent_api_port,
host=autogpt_libs.utils.settings.Config().agent_api_host,
port=autogpt_libs.utils.settings.Config().agent_api_port,
)

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion autogpt_platform/backend/backend/server/routers/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pydantic
from autogpt_libs.auth.middleware import auth_middleware
from autogpt_libs.utils.cache import thread_cached
from autogpt_libs.utils.settings import Settings
from fastapi import APIRouter, Depends, HTTPException
from typing_extensions import Optional, TypedDict

Expand Down Expand Up @@ -39,7 +40,6 @@
)
from backend.server.utils import get_user_id
from backend.util.service import get_service_client
from backend.util.settings import Settings


@thread_cached
Expand Down
3 changes: 1 addition & 2 deletions autogpt_platform/backend/backend/server/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from autogpt_libs.auth.depends import requires_user
from autogpt_libs.auth.models import User
from autogpt_libs.utils.settings import Settings
from fastapi import Depends

from backend.util.settings import Settings

settings = Settings()


Expand Down
2 changes: 1 addition & 1 deletion autogpt_platform/backend/backend/server/ws_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import uvicorn
from autogpt_libs.auth import parse_jwt_token
from autogpt_libs.utils.settings import AppEnvironment, Config, Settings
from fastapi import Depends, FastAPI, WebSocket, WebSocketDisconnect
from starlette.middleware.cors import CORSMiddleware

Expand All @@ -13,7 +14,6 @@
from backend.server.conn_manager import ConnectionManager
from backend.server.model import ExecutionSubscription, Methods, WsMessage
from backend.util.service import AppProcess
from backend.util.settings import AppEnvironment, Config, Settings

logger = logging.getLogger(__name__)
settings = Settings()
Expand Down
3 changes: 1 addition & 2 deletions autogpt_platform/backend/backend/util/encryption.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import json
from typing import Optional

from autogpt_libs.utils.settings import Settings
from cryptography.fernet import Fernet

from backend.util.settings import Settings

ENCRYPTION_KEY = Settings().secrets.encryption_key


Expand Down
2 changes: 1 addition & 1 deletion autogpt_platform/backend/backend/util/logging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from backend.util.settings import AppEnvironment, BehaveAs, Settings
from autogpt_libs.utils.settings import AppEnvironment, BehaveAs, Settings

settings = Settings()

Expand Down
3 changes: 1 addition & 2 deletions autogpt_platform/backend/backend/util/metrics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sentry_sdk

from backend.util.settings import Settings
from autogpt_libs.utils.settings import Settings


def sentry_init():
Expand Down
Loading
Loading