Building AI agents, atomically
fix: isolate default component configurations (#307)
ningyixidd73523120ba
diff --git a/atomic-agents/atomic_agents/base/base_prompt.py b/atomic-agents/atomic_agents/base/base_prompt.pyindex 75416d9..f9857fa 100644--- a/atomic-agents/atomic_agents/base/base_prompt.py+++ b/atomic-agents/atomic_agents/base/base_prompt.py@@ -35,17 +35,19 @@ class BasePrompt[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema](ABC):input_schema (Type[InputSchema]): Schema class defining the input data (derived from generic type parameter).output_schema (Type[OutputSchema]): Schema class defining the output data (derived from generic type parameter).prompt_name (str): The name of the prompt, derived from the input schema's title or overridden by the config.- prompt_description (str): Description of the prompt, derived from the input schema's description or overridden by the config.+ prompt_description (str): Description of the prompt, derived from the input schema's description+ or overridden by the config."""- def __init__(self, config: BasePromptConfig = BasePromptConfig()):+ def __init__(self, config: Optional[BasePromptConfig] = None):"""Initializes the BasePrompt with an optional configuration override.Args:- config (BasePromptConfig, optional): Configuration for the prompt, including optional title and description overrides.+ config (BasePromptConfig, optional): Configuration for the prompt, including optional title+ and description overrides. If omitted, a new configuration is created for this instance."""- self.config = config+ self.config = config if config is not None else BasePromptConfig()def __init_subclass__(cls, **kwargs):"""diff --git a/atomic-agents/atomic_agents/base/base_resource.py b/atomic-agents/atomic_agents/base/base_resource.pyindex 913e23d..ce25f61 100644--- a/atomic-agents/atomic_agents/base/base_resource.py+++ b/atomic-agents/atomic_agents/base/base_resource.py@@ -35,17 +35,19 @@ class BaseResource[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema](ABC):input_schema (Type[InputSchema]): Schema class defining the input data (derived from generic type parameter).output_schema (Type[OutputSchema]): Schema class defining the output data (derived from generic type parameter).resource_name (str): The name of the resource, derived from the input schema's title or overridden by the config.- resource_description (str): Description of the resource, derived from the input schema's description or overridden by the config.+ resource_description (str): Description of the resource, derived from the input schema's description+ or overridden by the config."""- def __init__(self, config: BaseResourceConfig = BaseResourceConfig()):+ def __init__(self, config: Optional[BaseResourceConfig] = None):"""Initializes the BaseResource with an optional configuration override.Args:- config (BaseResourceConfig, optional): Configuration for the resource, including optional title and description overrides.+ config (BaseResourceConfig, optional): Configuration for the resource, including optional title+ and description overrides. If omitted, a new configuration is created for this instance."""- self.config = config+ self.config = config if config is not None else BaseResourceConfig()def __init_subclass__(cls, **kwargs):"""diff --git a/atomic-agents/atomic_agents/base/base_tool.py b/atomic-agents/atomic_agents/base/base_tool.pyindex 9566bae..da9a51d 100644--- a/atomic-agents/atomic_agents/base/base_tool.py+++ b/atomic-agents/atomic_agents/base/base_tool.py@@ -35,17 +35,18 @@ class BaseTool[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema](ABC):input_schema (Type[InputSchema]): Schema class defining the input data (derived from generic type parameter).output_schema (Type[OutputSchema]): Schema class defining the output data (derived from generic type parameter).tool_name (str): The name of the tool, derived from the input schema's title or overridden by the config.- tool_description (str): Description of the tool, derived from the input schema's description or overridden by the config.+ tool_description (str): Description of the tool, derived from the input schema's description+ or overridden by the config."""- def __init__(self, config: BaseToolConfig = BaseToolConfig()):+ def __init__(self, config: Optional[BaseToolConfig] = None):"""Initializes the BaseTool with an optional configuration override.Args:config (BaseToolConfig, optional): Configuration for the tool, including optional title and description overrides."""- self.config = config+ self.config = config if config is not None else BaseToolConfig()def __init_subclass__(cls, **kwargs):"""diff --git a/atomic-agents/tests/base/test_default_config_isolation.py b/atomic-agents/tests/base/test_default_config_isolation.pynew file mode 100644index 0000000..9d72edd--- /dev/null+++ b/atomic-agents/tests/base/test_default_config_isolation.py@@ -0,0 +1,62 @@+import pytest++from atomic_agents.base import BaseIOSchema, BasePrompt, BaseResource, BaseTool+++class ValueSchema(BaseIOSchema):+ """A value passed through a component."""++ value: str+++class EchoTool(BaseTool[ValueSchema, ValueSchema]):+ def run(self, params: ValueSchema) -> ValueSchema:+ return params+++class EchoResource(BaseResource[ValueSchema, ValueSchema]):+ def read(self, params: ValueSchema) -> ValueSchema:+ return params+++class EchoPrompt(BasePrompt[ValueSchema, ValueSchema]):+ def generate(self, params: ValueSchema) -> ValueSchema:+ return params+++ "component_class, name_attribute, description_attribute",+ [+ (EchoTool, "tool_name", "tool_description"),+ (EchoResource, "resource_name", "resource_description"),+ (EchoPrompt, "prompt_name", "prompt_description"),+ ],+)+def test_default_config_changes_do_not_affect_other_instances(+ component_class: type[EchoTool] | type[EchoResource] | type[EchoPrompt],+ name_attribute: str,+ description_attribute: str,+) -> None:+ first = component_class()+ second = component_class()+ original_title, original_description = first.config.title, first.config.description++ try:+ first.config.title = "Customized instance"+ first.config.description = "Only this instance should change."++ assert getattr(first, name_attribute) == "Customized instance"+ assert getattr(first, description_attribute) == "Only this instance should change."+ for other in (second, component_class()):+ assert getattr(other, name_attribute) == "ValueSchema"+ assert getattr(other, description_attribute) == ValueSchema.__doc__+ finally:+ first.config.title, first.config.description = original_title, original_description++[email protected]("component_class", [EchoTool, EchoResource, EchoPrompt])+def test_explicit_config_is_retained(component_class: type[EchoTool] | type[EchoResource] | type[EchoPrompt]) -> None:+ config = component_class().config+ component = component_class(config=config)++ assert component.config is config