feat(rnd): Add Agent block UUID validation for Agent Server (#7501)

Co-authored-by: Nicholas Tindle <nicholas.tindle@agpt.co>
pull/7499/head^2
Zamil Majdy 2024-07-20 06:14:18 +04:00 committed by GitHub
parent 21084c5817
commit fa1b486c64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 10 deletions

View File

@ -19,6 +19,16 @@ for module in modules:
AVAILABLE_MODULES.append(module)
# Load all Block instances from the available modules
AVAILABLE_BLOCKS = {block.id: block for block in [v() for v in Block.__subclasses__()]}
AVAILABLE_BLOCKS = {}
for cls in Block.__subclasses__():
block = cls()
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")
if block.id in AVAILABLE_BLOCKS:
raise ValueError(f"Block ID {block.name} error: {block.id} is already in use")
AVAILABLE_BLOCKS[block.id] = block
__all__ = ["AVAILABLE_MODULES", "AVAILABLE_BLOCKS"]

View File

@ -193,17 +193,33 @@ def get_blocks() -> dict[str, Block]:
async def initialize_blocks() -> None:
for block in get_blocks().values():
if await AgentBlock.prisma().find_unique(where={"id": block.id}):
existing_block = await AgentBlock.prisma().find_unique(where={"id": block.id})
if not existing_block:
await AgentBlock.prisma().create(
data={
"id": block.id,
"name": block.name,
"inputSchema": json.dumps(block.input_schema.jsonschema()),
"outputSchema": json.dumps(block.output_schema.jsonschema()),
}
)
continue
await AgentBlock.prisma().create(
data={
"id": block.id,
"name": block.name,
"inputSchema": json.dumps(block.input_schema.jsonschema()),
"outputSchema": json.dumps(block.output_schema.jsonschema()),
}
)
input_schema = json.dumps(block.input_schema.jsonschema())
output_schema = json.dumps(block.output_schema.jsonschema())
if (
block.name != existing_block.name
or input_schema != existing_block.inputSchema
or output_schema != existing_block.outputSchema
):
await AgentBlock.prisma().update(
where={"id": block.id},
data={
"name": block.name,
"inputSchema": input_schema,
"outputSchema": output_schema,
},
)
def get_block(block_id: str) -> Block | None: