Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
majdyz committed Oct 19, 2024
1 parent 6ae31ac commit 3ac6ff5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
23 changes: 13 additions & 10 deletions autogpt_platform/backend/backend/blocks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import re
from pathlib import Path
from typing import Type
from typing import Type, TypeVar

from backend.data.block import Block

Expand All @@ -28,25 +28,28 @@
AVAILABLE_BLOCKS: dict[str, Type[Block]] = {}


def all_subclasses(clz):
subclasses = clz.__subclasses__()
T = TypeVar("T")


def all_subclasses(cls: Type[T]) -> list[Type[T]]:
subclasses = cls.__subclasses__()
for subclass in subclasses:
subclasses += all_subclasses(subclass)
return subclasses


for cls in all_subclasses(Block):
name = cls.__name__
for block_cls in all_subclasses(Block):
name = block_cls.__name__

if cls.__name__.endswith("Base"):
if block_cls.__name__.endswith("Base"):
continue

if not cls.__name__.endswith("Block"):
if not block_cls.__name__.endswith("Block"):
raise ValueError(
f"Block class {cls.__name__} does not end with 'Block', If you are creating an abstract class, please name the class with 'Base' at the end"
f"Block class {block_cls.__name__} does not end with 'Block', If you are creating an abstract class, please name the class with 'Base' at the end"
)

block = cls()
block = block_cls.create()

if not isinstance(block.id, str) or len(block.id) != 36:
raise ValueError(f"Block ID {block.name} error: {block.id} is not a valid UUID")
Expand Down Expand Up @@ -88,6 +91,6 @@ def all_subclasses(clz):
if block.disabled:
continue

AVAILABLE_BLOCKS[block.id] = cls
AVAILABLE_BLOCKS[block.id] = block_cls

__all__ = ["AVAILABLE_MODULES", "AVAILABLE_BLOCKS"]
4 changes: 4 additions & 0 deletions autogpt_platform/backend/backend/data/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ def __init__(
self.block_type = block_type
self.execution_stats = {}

@classmethod
def create(cls: Type["Block"]) -> "Block":
return cls()

@abstractmethod
def run(self, input_data: BlockSchemaInputType, **kwargs) -> BlockOutput:
"""
Expand Down

0 comments on commit 3ac6ff5

Please sign in to comment.