Skip to content

Commit 99ed2d4

Browse files
committed
fix(agui-strands): restore durable AgentCore Memory recall
The agui-strands pattern lost same-session conversation recall: ag-ui-strands 0.1.1 discards the agent's session_manager and keeps history only in an in-process dict that doesn't survive AgentCore Runtime scaling. - Bump ag-ui-strands 0.1.1 -> 0.1.9 (adds session_manager_provider) and other deps to latest. - Supply the AgentCore Memory session manager per-thread via StrandsAgentConfig.session_manager_provider (keyed by actor_id + thread_id). - Set replay_history_into_strands=False: 0.1.9 checks agent.session_manager but Strands stores it as _session_manager, so its replay path otherwise overwrites the rehydrated history with only the latest message. Verified with a local repro against a real AgentCore Memory resource: direct agent recalls, adapter without the flag fails, adapter with the flag recalls.
1 parent 7ca2c03 commit 99ed2d4

2 files changed

Lines changed: 32 additions & 20 deletions

File tree

patterns/agui-strands-agent/agent.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77

88
from ag_ui.core import RunAgentInput, RunErrorEvent
9-
from ag_ui_strands import StrandsAgent
9+
from ag_ui_strands import StrandsAgent, StrandsAgentConfig
1010
from bedrock_agentcore.memory.integrations.strands.config import AgentCoreMemoryConfig
1111
from bedrock_agentcore.memory.integrations.strands.session_manager import (
1212
AgentCoreMemorySessionManager,
@@ -37,36 +37,48 @@
3737
CODE_INTERPRETER = StrandsCodeInterpreterTools(REGION).execute_python_securely
3838

3939

40-
def get_memory_session_manager(
41-
actor_id: str, session_id: str
42-
) -> AgentCoreMemorySessionManager | None:
43-
"""Return an AgentCore Memory session manager, or None when MEMORY_ID is unset."""
44-
if not MEMORY_ID:
45-
return None
46-
return AgentCoreMemorySessionManager(
47-
AgentCoreMemoryConfig(
48-
memory_id=MEMORY_ID, session_id=session_id, actor_id=actor_id
49-
),
50-
region_name=REGION,
51-
)
40+
def _make_session_manager_provider(actor_id: str):
41+
"""Per-thread AgentCore Memory session-manager factory for the AG-UI adapter.
42+
43+
ag-ui-strands attaches the returned manager to the agent it runs (keyed by
44+
actor_id + thread_id). A session_manager on the template Agent is ignored.
45+
Returns None when MEMORY_ID is unset.
46+
"""
47+
48+
def provider(run_input: RunAgentInput) -> AgentCoreMemorySessionManager | None:
49+
if not MEMORY_ID:
50+
return None
51+
session_id = run_input.thread_id or actor_id
52+
return AgentCoreMemorySessionManager(
53+
AgentCoreMemoryConfig(
54+
memory_id=MEMORY_ID, session_id=session_id, actor_id=actor_id
55+
),
56+
region_name=REGION,
57+
)
58+
59+
return provider
5260

5361

5462
@app.entrypoint
5563
async def invocations(payload: dict, context: RequestContext):
5664
input_data = RunAgentInput.model_validate(payload)
5765
actor_id = extract_user_id_from_context(context)
58-
session_id = input_data.thread_id or actor_id
5966

67+
# session_manager is supplied per-thread via the provider below, not here.
6068
agent = Agent(
6169
model=MODEL,
6270
system_prompt=SYSTEM_PROMPT,
6371
tools=[create_gateway_mcp_client(actor_id), CODE_INTERPRETER],
64-
session_manager=get_memory_session_manager(actor_id, session_id),
6572
)
6673
agui_agent = StrandsAgent(
6774
agent=agent,
6875
name="agui_strands_agent",
6976
description="AG-UI Strands agent with Gateway MCP tools and Code Interpreter",
77+
config=StrandsAgentConfig(
78+
session_manager_provider=_make_session_manager_provider(actor_id),
79+
# Disable client-side replay so the session manager owns history.
80+
replay_history_into_strands=False,
81+
),
7082
)
7183

7284
try:
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Strands AG-UI agent dependencies
2-
strands-agents==1.32.0
3-
ag-ui-strands==0.1.1
4-
bedrock-agentcore==1.4.7
5-
mcp==1.26.0
6-
PyJWT[crypto]==2.12.1
2+
strands-agents==1.42.0
3+
ag-ui-strands==0.1.9
4+
bedrock-agentcore==1.14.0
5+
mcp==1.27.2
6+
PyJWT[crypto]==2.13.0

0 commit comments

Comments
 (0)