Skip to content

Commit 72e477f

Browse files
committed
fix: implement runtime support for widened type hints in Agent init
- Add string preset handling in _init_autonomy() for autonomy='full_auto' etc. - Add str/dict support in context_manager property for context='summarize' etc. - Add dict-to-ApprovalConfig conversion for approval={'backend': ..., 'all_tools': True} - Add string mode handling in learn parameter for learn='agentic' etc. - Fix AUTONOMY_PRESETS to use 'level' field (not 'mode') per AutonomyConfig Resolves type hint/implementation mismatches identified by code reviewers. All new type hints now have proper runtime support. Co-authored-by: Mervin Praison <MervinPraison@users.noreply.github.com>
1 parent 9c2f14e commit 72e477f

2 files changed

Lines changed: 98 additions & 3 deletions

File tree

src/praisonai-agents/praisonaiagents/agent/agent.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,18 @@ def __init__(
10761076
_learn_config = learn
10771077
elif isinstance(learn, dict):
10781078
_learn_config = LearnConfig(**learn)
1079+
elif isinstance(learn, str):
1080+
# String mode: "disabled", "agentic", "propose"
1081+
from ..memory.learn.protocols import LearnMode
1082+
if learn == "disabled":
1083+
_learn_config = None
1084+
elif learn == "agentic":
1085+
_learn_config = LearnConfig(mode=LearnMode.AGENTIC)
1086+
elif learn == "propose":
1087+
_learn_config = LearnConfig(mode=LearnMode.PROPOSE)
1088+
else:
1089+
# Unknown string mode, disable learning
1090+
_learn_config = None
10791091
else:
10801092
_learn_config = learn # Pass through
10811093
elif _memory_config is not None and isinstance(_memory_config, MemoryConfig):
@@ -1651,6 +1663,12 @@ def __init__(
16511663
self._approval_backend = approval.backend
16521664
self._approve_all_tools = approval.all_tools
16531665
self._approval_timeout = approval.timeout # None = indefinite, 0 = backend default
1666+
elif isinstance(approval, dict):
1667+
# Dict config: convert to ApprovalConfig
1668+
approval_config = ApprovalConfig(**approval)
1669+
self._approval_backend = approval_config.backend
1670+
self._approve_all_tools = approval_config.all_tools
1671+
self._approval_timeout = approval_config.timeout
16541672
else:
16551673
# Plain backend object — dangerous tools only, backend default timeout
16561674
self._approval_backend = approval
@@ -1958,6 +1976,64 @@ def context_manager(self) -> Optional[Any]:
19581976
elif hasattr(self._context_param, 'process'):
19591977
# Already a ContextManager instance
19601978
self._context_manager = self._context_param
1979+
elif isinstance(self._context_param, str):
1980+
# String preset: "sliding_window", "summarize", "truncate"
1981+
from ..config.presets import CONTEXT_PRESETS
1982+
preset_config = CONTEXT_PRESETS.get(self._context_param)
1983+
if preset_config is not None:
1984+
# Convert preset to ContextConfig, then to ManagerConfig
1985+
try:
1986+
from ..context.models import ContextConfig as _ContextConfig
1987+
context_config = _ContextConfig(**preset_config)
1988+
manager_config = ManagerConfig(
1989+
auto_compact=context_config.auto_compact,
1990+
compact_threshold=context_config.compact_threshold,
1991+
strategy=context_config.strategy,
1992+
output_reserve=context_config.output_reserve,
1993+
default_tool_output_max=context_config.tool_output_max,
1994+
protected_tools=list(context_config.protected_tools),
1995+
keep_recent_turns=context_config.keep_recent_turns,
1996+
monitor_enabled=context_config.monitor.enabled if context_config.monitor else False,
1997+
)
1998+
self._context_manager = ContextManager(
1999+
model=self.llm if isinstance(self.llm, str) else "gpt-4o-mini",
2000+
config=manager_config,
2001+
agent_name=self.name or "Agent",
2002+
session_cache=self._session_dedup_cache,
2003+
llm_summarize_fn=None,
2004+
)
2005+
except Exception as e:
2006+
logging.debug(f"Context preset conversion failed: {e}")
2007+
self._context_manager = None
2008+
else:
2009+
# Unknown string preset, disable
2010+
self._context_manager = None
2011+
elif isinstance(self._context_param, dict):
2012+
# Dict config: convert to ContextConfig, then to ManagerConfig
2013+
try:
2014+
from ..context.models import ContextConfig as _ContextConfig
2015+
context_config = _ContextConfig(**self._context_param)
2016+
manager_config = ManagerConfig(
2017+
auto_compact=context_config.auto_compact,
2018+
compact_threshold=context_config.compact_threshold,
2019+
strategy=context_config.strategy,
2020+
output_reserve=context_config.output_reserve,
2021+
default_tool_output_max=context_config.tool_output_max,
2022+
protected_tools=list(context_config.protected_tools),
2023+
keep_recent_turns=context_config.keep_recent_turns,
2024+
monitor_enabled=context_config.monitor.enabled if context_config.monitor else False,
2025+
)
2026+
llm_summarize_enabled = self._context_param.get('llm_summarize', False)
2027+
self._context_manager = ContextManager(
2028+
model=self.llm if isinstance(self.llm, str) else "gpt-4o-mini",
2029+
config=manager_config,
2030+
agent_name=self.name or "Agent",
2031+
session_cache=self._session_dedup_cache,
2032+
llm_summarize_fn=self._create_llm_summarize_fn() if llm_summarize_enabled else None,
2033+
)
2034+
except Exception as e:
2035+
logging.debug(f"Context dict conversion failed: {e}")
2036+
self._context_manager = None
19612037
else:
19622038
# Unknown type, disable
19632039
self._context_manager = None
@@ -2174,6 +2250,25 @@ def _init_autonomy(self, autonomy: Any, verification_hooks: Optional[List[Any]]
21742250
# Extract verification_hooks from AutonomyConfig if provided
21752251
if autonomy.verification_hooks and not verification_hooks:
21762252
self._verification_hooks = autonomy.verification_hooks
2253+
elif isinstance(autonomy, str):
2254+
# String preset: "suggest", "auto_edit", "full_auto"
2255+
from ..config.presets import AUTONOMY_PRESETS
2256+
preset_config = AUTONOMY_PRESETS.get(autonomy)
2257+
if preset_config is not None:
2258+
config = AutonomyConfig.from_dict(preset_config)
2259+
else:
2260+
# Unknown string preset — disable autonomy
2261+
self.autonomy_enabled = False
2262+
self.autonomy_config = {}
2263+
self._autonomy_trigger = None
2264+
self._doom_loop_tracker = None
2265+
self._file_snapshot = None
2266+
self._snapshot_stack = []
2267+
self._redo_stack = []
2268+
self._autonomy_turn_tool_count = 0
2269+
self._consecutive_no_tool_turns = 0
2270+
self._doom_recovery_active = False
2271+
return
21772272
else:
21782273
self.autonomy_enabled = False
21792274
self.autonomy_config = {}

src/praisonai-agents/praisonaiagents/config/presets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,9 @@
300300
# =============================================================================
301301

302302
AUTONOMY_PRESETS: Dict[str, Dict[str, Any]] = {
303-
"suggest": {"mode": "suggest"},
304-
"auto_edit": {"mode": "auto_edit"},
305-
"full_auto": {"mode": "full_auto"},
303+
"suggest": {"level": "suggest"},
304+
"auto_edit": {"level": "auto_edit"},
305+
"full_auto": {"level": "full_auto"},
306306
}
307307

308308

0 commit comments

Comments
 (0)