fix(rnd): Cleanup block names and add explicit block name check (#7709)

pull/7710/head
Zamil Majdy 2024-08-05 21:58:03 +04:00 committed by GitHub
parent 951abf6d5b
commit fe5c1968bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 20 additions and 8 deletions

View File

@ -1,6 +1,7 @@
import glob
import importlib
import os
import re
from pathlib import Path
from autogpt_server.data.block import Block
@ -15,6 +16,11 @@ modules = [
if os.path.isfile(f) and f.endswith(".py") and not f.endswith("__init__.py")
]
for module in modules:
if not re.match("^[a-z_]+$", module):
raise ValueError(
f"Block module {module} error: module name must be lowercase, separated by underscores, and contain only alphabet characters"
)
importlib.import_module(f".{module}", package=__name__)
AVAILABLE_MODULES.append(module)
@ -30,9 +36,16 @@ def all_subclasses(clz):
for cls in all_subclasses(Block):
if not cls.__name__.endswith("Block"):
name = cls.__name__
if cls.__name__.endswith("Base"):
continue
if not 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"
)
block = cls()
if not isinstance(block.id, str) or len(block.id) != 36:

View File

@ -131,7 +131,7 @@ class WebScraperBlock(Block, GetRequest):
yield "error", f"Request to Jina-ai Reader failed: {e}"
class GetOpenWeatherMapWeather(Block, GetRequest):
class GetOpenWeatherMapBlock(Block, GetRequest):
class Input(BlockSchema):
location: str
api_key: BlockSecret = SecretField(key="openweathermap_api_key")
@ -146,8 +146,8 @@ class GetOpenWeatherMapWeather(Block, GetRequest):
def __init__(self):
super().__init__(
id="f7a8b2c3-6d4e-5f8b-9e7f-6d4e5f8b9e7f",
input_schema=GetOpenWeatherMapWeather.Input,
output_schema=GetOpenWeatherMapWeather.Output,
input_schema=GetOpenWeatherMapBlock.Input,
output_schema=GetOpenWeatherMapBlock.Output,
test_input={
"location": "New York",
"api_key": "YOUR_API_KEY",

View File

@ -7,7 +7,7 @@ from autogpt_server.data.block import Block, BlockOutput, BlockSchema
from autogpt_server.data.model import SchemaField
class YouTubeTranscriber(Block):
class YouTubeTranscriberBlock(Block):
class Input(BlockSchema):
youtube_url: str = SchemaField(
description="The URL of the YouTube video to transcribe",
@ -24,8 +24,8 @@ class YouTubeTranscriber(Block):
def __init__(self):
super().__init__(
id="f3a8f7e1-4b1d-4e5f-9f2a-7c3d5a2e6b4c",
input_schema=YouTubeTranscriber.Input,
output_schema=YouTubeTranscriber.Output,
input_schema=YouTubeTranscriberBlock.Input,
output_schema=YouTubeTranscriberBlock.Output,
test_input={"youtube_url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ"},
test_output=[
("video_id", "dQw4w9WgXcQ"),

View File

@ -1,5 +1,4 @@
import asyncio
import uuid
from collections import defaultdict
from contextlib import asynccontextmanager
from typing import Annotated, Any, Dict