config_parser.py
1 """ 2 Parses and validates configuration for the SamAgentComponent and its App. 3 """ 4 5 import logging 6 from typing import Any, Callable 7 from google.adk.agents.readonly_context import ReadonlyContext 8 9 log = logging.getLogger(__name__) 10 11 InstructionProvider = Callable[[ReadonlyContext], str] 12 13 14 def resolve_instruction_provider( 15 component, config_value: Any 16 ) -> str | InstructionProvider: 17 """ 18 Resolves instruction config which can be a string or an invoke block 19 handled by SAC's get_config. 20 Args: 21 component: The component instance (for context in get_config). 22 config_value: The configuration value for the instruction (e.g., the value 23 retrieved using component.get_config("instruction")). 24 Returns: 25 The resolved instruction string or provider function. 26 Raises: 27 ValueError: If the configuration is invalid or resolution fails. 28 """ 29 if isinstance(config_value, str): 30 return config_value 31 elif isinstance(config_value, dict) and "invoke" in config_value: 32 if callable(config_value): 33 log.info( 34 "%s Resolved instruction to a callable provider.", 35 component.log_identifier, 36 ) 37 return config_value 38 else: 39 raise ValueError( 40 f"Invoke block for instruction resolved to unexpected type: {type(config_value)}" 41 ) 42 elif not config_value: 43 return "" 44 else: 45 raise ValueError( 46 f"Invalid instruction configuration type: {type(config_value)}" 47 )