|
6 | 6 | import json
|
7 | 7 | from pathlib import Path
|
8 | 8 |
|
| 9 | +import jsonschema |
| 10 | +from jsonschema import ValidationError |
| 11 | + |
9 | 12 | from ..agent import Agent
|
10 | 13 |
|
| 14 | +# JSON Schema for agent configuration |
| 15 | +AGENT_CONFIG_SCHEMA = { |
| 16 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 17 | + "title": "Agent Configuration", |
| 18 | + "description": "Configuration schema for creating agents", |
| 19 | + "type": "object", |
| 20 | + "properties": { |
| 21 | + "name": { |
| 22 | + "description": "Name of the agent", |
| 23 | + "type": ["string", "null"], |
| 24 | + "default": None |
| 25 | + }, |
| 26 | + "model": { |
| 27 | + "description": "The model ID to use for this agent. If not specified, uses the default model.", |
| 28 | + "type": ["string", "null"], |
| 29 | + "default": None |
| 30 | + }, |
| 31 | + "prompt": { |
| 32 | + "description": "The system prompt for the agent. Provides high level context to the agent.", |
| 33 | + "type": ["string", "null"], |
| 34 | + "default": None |
| 35 | + }, |
| 36 | + "tools": { |
| 37 | + "description": "List of tools the agent can use. Can be module paths, file paths, or tool names.", |
| 38 | + "type": "array", |
| 39 | + "items": { |
| 40 | + "type": "string" |
| 41 | + }, |
| 42 | + "default": [] |
| 43 | + } |
| 44 | + }, |
| 45 | + "additionalProperties": False |
| 46 | +} |
| 47 | + |
11 | 48 |
|
12 | 49 | def config_to_agent(config: str | dict[str, any], **kwargs) -> Agent:
|
13 | 50 | """Create an Agent from a configuration file or dictionary.
|
@@ -56,6 +93,14 @@ def config_to_agent(config: str | dict[str, any], **kwargs) -> Agent:
|
56 | 93 | else:
|
57 | 94 | raise ValueError("Config must be a file path string or dictionary")
|
58 | 95 |
|
| 96 | + # Validate configuration against schema |
| 97 | + try: |
| 98 | + jsonschema.validate(config_dict, AGENT_CONFIG_SCHEMA) |
| 99 | + except ValidationError as e: |
| 100 | + # Provide more detailed error message |
| 101 | + error_path = " -> ".join(str(p) for p in e.absolute_path) if e.absolute_path else "root" |
| 102 | + raise ValueError(f"Configuration validation error at {error_path}: {e.message}") from e |
| 103 | + |
59 | 104 | # Prepare Agent constructor arguments
|
60 | 105 | agent_kwargs = {}
|
61 | 106 |
|
|
0 commit comments