mirror of
https://github.com/MervinPraison/PraisonAI.git
synced 2026-11-07 04:35:58 -10:00
* fix(examples): resolve syntax errors and align with consolidated-params API * fix: correct TypeError in 3 example files by using proper parameter names - Fix MemoryConfig(memory=...) → user_id=... in agent_centric_api.py - Fix Session(memory=...) → user_id=... in session-example.py - Fix build_context_for_task(memory=...) → user_id=... in memory-quality-example.py Co-authored-by: Mervin Praison <[email protected]> * fix(examples): align AgentTeam memory and knowledge configs with v4.6.52 API * fix(examples): phase-3 API alignment for consolidated params on v4.6.52 * fix(examples): phase-4 API alignment for 40 validation failures on v4.6.52 * fix: resolve critical RouterAgent **kwargs bug and add missing if __name__ guards - Fixed TypeError in RouterAgent._build_system_prompt() by removing **kwargs from super() call - Added if __name__ == '__main__' guards to 6 example files to prevent auto-execution on import - Addresses Gemini Code Assist review feedback Co-authored-by: Mervin Praison <[email protected]> * fix: resolve missing AgentTeam import and RouterAgent kwargs forwarding - Fix missing AgentTeam import in self-reflection-optimization-patterns.py - Forward kwargs properly in RouterAgent._build_system_prompt to super() - Addresses critical issues identified by Gemini code reviewer Co-authored-by: Mervin Praison <[email protected]> * fix(examples): remove merge-conflict residue for PR #1893 --------- Co-authored-by: Dhivya-Bharathy <[email protected]> Co-authored-by: praisonai-triage-agent[bot] <272766704+praisonai-triage-agent[bot]@users.noreply.github.com> Co-authored-by: Mervin Praison <[email protected]> Co-authored-by: Mervin Praison <[email protected]>
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
"""
|
|
Injected State Example - PraisonAI Agents
|
|
|
|
Demonstrates injecting agent state into tools without exposing it in the schema.
|
|
"""
|
|
|
|
from praisonaiagents import Agent, tool
|
|
from praisonaiagents.tools import Injected
|
|
from praisonaiagents.tools.injected import AgentState, with_injection_context
|
|
|
|
# Tool with injected state - state param is NOT in the public schema
|
|
@tool
|
|
def show_context(query: str, state: Injected[dict]) -> str:
|
|
"""Show the current agent context."""
|
|
session_id = state.get('session_id', 'unknown')
|
|
agent_id = state.get('agent_id', 'unknown')
|
|
return f"Query: {query}, Session: {session_id}, Agent: {agent_id}"
|
|
|
|
# Create agent with the tool
|
|
agent = Agent(
|
|
name="ContextBot",
|
|
instructions="You help show context information.",
|
|
tools=[show_context],
|
|
memory={"session_id": "my-session-123"},
|
|
)
|
|
|
|
if __name__ == "__main__":
|
|
# Verify injected param is not in schema
|
|
schema = show_context.get_schema()
|
|
params = schema['function']['parameters']['properties']
|
|
print(f"Schema params: {list(params.keys())}")
|
|
assert 'state' not in params, "state should NOT be in schema"
|
|
print("✓ 'state' correctly excluded from schema")
|
|
|
|
# Test with manual injection context
|
|
mock_state = AgentState(
|
|
agent_id="test-agent",
|
|
run_id="run-1",
|
|
session_id="session-abc"
|
|
)
|
|
|
|
with with_injection_context(mock_state):
|
|
result = show_context(query="hello")
|
|
print(f"Result: {result}")
|
|
|
|
# Test via agent.execute_tool
|
|
result = agent.execute_tool("show_context", {"query": "test"})
|
|
print(f"Agent result: {result}")
|
|
|
|
print("\n✓ Injected state example complete")
|