|
14 | 14 | @tool |
15 | 15 | def tool_with_context(message: str, strands_context: StrandsContext) -> dict: |
16 | 16 | """Tool that uses StrandsContext to access tool_use_id.""" |
17 | | - try: |
18 | | - print(f"DEBUG: tool_with_context called with message='{message}'") |
19 | | - print(f"DEBUG: strands_context type: {type(strands_context)}") |
20 | | - print(f"DEBUG: strands_context contents: {strands_context}") |
21 | | - |
22 | | - tool_use_id = strands_context["tool_use"]["toolUseId"] |
23 | | - print(f"DEBUG: Successfully extracted tool_use_id: {tool_use_id}") |
24 | | - |
25 | | - result = { |
26 | | - "status": "success", |
27 | | - "content": [{"text": f"Context tool processed '{message}' with ID: {tool_use_id}"}], |
28 | | - } |
29 | | - print(f"DEBUG: Returning result: {result}") |
30 | | - return result |
31 | | - |
32 | | - except Exception as e: |
33 | | - print(f"ERROR in tool_with_context: {type(e).__name__}: {e}") |
34 | | - print(f"ERROR: strands_context = {strands_context}") |
35 | | - import traceback |
36 | | - |
37 | | - traceback.print_exc() |
38 | | - return {"status": "error", "content": [{"text": f"Error: {str(e)}"}]} |
| 17 | + tool_use_id = strands_context["tool_use"]["toolUseId"] |
| 18 | + return {"status": "success", "content": [{"text": f"Context tool processed '{message}' with ID: {tool_use_id}"}]} |
39 | 19 |
|
40 | 20 |
|
41 | 21 | @tool |
42 | 22 | def tool_with_agent_and_context(message: str, agent: Agent, strands_context: StrandsContext) -> dict: |
43 | 23 | """Tool that uses both agent and StrandsContext.""" |
44 | | - try: |
45 | | - print(f"DEBUG: tool_with_agent_and_context called with message='{message}'") |
46 | | - print(f"DEBUG: agent type: {type(agent)}") |
47 | | - print(f"DEBUG: strands_context type: {type(strands_context)}") |
48 | | - print(f"DEBUG: strands_context contents: {strands_context}") |
49 | | - |
50 | | - tool_use_id = strands_context["tool_use"]["toolUseId"] |
51 | | - print(f"DEBUG: Successfully extracted tool_use_id: {tool_use_id}") |
52 | | - |
53 | | - agent_name = getattr(agent, "name", "unknown-agent") |
54 | | - print(f"DEBUG: Agent name: {agent_name}") |
55 | | - |
56 | | - result = { |
57 | | - "status": "success", |
58 | | - "content": [{"text": f"Agent '{agent_name}' processed '{message}' with ID: {tool_use_id}"}], |
59 | | - } |
60 | | - print(f"DEBUG: Returning result: {result}") |
61 | | - return result |
62 | | - |
63 | | - except Exception as e: |
64 | | - print(f"ERROR in tool_with_agent_and_context: {type(e).__name__}: {e}") |
65 | | - print(f"ERROR: agent = {agent}") |
66 | | - print(f"ERROR: strands_context = {strands_context}") |
67 | | - import traceback |
68 | | - |
69 | | - traceback.print_exc() |
70 | | - return {"status": "error", "content": [{"text": f"Error: {str(e)}"}]} |
| 24 | + tool_use_id = strands_context["tool_use"]["toolUseId"] |
| 25 | + agent_name = getattr(agent, "name", "unknown-agent") |
| 26 | + return { |
| 27 | + "status": "success", |
| 28 | + "content": [{"text": f"Agent '{agent_name}' processed '{message}' with ID: {tool_use_id}"}], |
| 29 | + } |
71 | 30 |
|
72 | 31 |
|
73 | 32 | def test_strands_context_integration(): |
74 | 33 | """Test StrandsContext functionality with real agent interactions.""" |
75 | | - try: |
76 | | - print("DEBUG: Starting test_strands_context_integration") |
77 | | - |
78 | | - # Initialize agent with tools |
79 | | - print("DEBUG: Initializing agent with tools") |
80 | | - agent = Agent(tools=[tool_with_context, tool_with_agent_and_context]) |
81 | | - print(f"DEBUG: Agent created: {agent}") |
82 | | - |
83 | | - # Test tool with StrandsContext |
84 | | - print("DEBUG: Testing tool_with_context") |
85 | | - result1 = agent.tool.tool_with_context(message="hello world") |
86 | | - print(f"DEBUG: tool_with_context result: {result1}") |
87 | | - assert result1.get("status") == "success" |
88 | | - print("DEBUG: tool_with_context assertion passed") |
89 | | - |
90 | | - # Test tool with both agent and StrandsContext |
91 | | - print("DEBUG: Testing tool_with_agent_and_context") |
92 | | - result = agent.tool.tool_with_agent_and_context(message="hello agent") |
93 | | - print(f"DEBUG: tool_with_agent_and_context result: {result}") |
94 | | - assert result.get("status") == "success" |
95 | | - print("DEBUG: tool_with_agent_and_context assertion passed") |
96 | 34 |
|
97 | | - print("DEBUG: All tests passed successfully") |
| 35 | + # Initialize agent with tools |
| 36 | + agent = Agent(tools=[tool_with_context, tool_with_agent_and_context]) |
98 | 37 |
|
99 | | - except Exception as e: |
100 | | - print(f"ERROR in test_strands_context_integration: {type(e).__name__}: {e}") |
101 | | - import traceback |
| 38 | + # Test tool with StrandsContext |
| 39 | + result1 = agent.tool.tool_with_context(message="hello world") |
| 40 | + assert result1.get("status") == "success" |
102 | 41 |
|
103 | | - traceback.print_exc() |
104 | | - raise |
| 42 | + # Test tool with both agent and StrandsContext |
| 43 | + result = agent.tool.tool_with_agent_and_context(message="hello agent") |
| 44 | + assert result.get("status") == "success" |
0 commit comments