Salta ai contenuti

Implementare e configurare l'agente Story

Lo Story Agent è un agente Strands generato con --protocol=AG-UI nel Modulo 1, in modo che l’interfaccia utente possa ricevere lo streaming da esso tramite il protocollo Agent-User Interaction via CopilotKit. Utilizza il server MCP dell’inventario per gestire gli oggetti del giocatore e l’S3SessionManager integrato di Strands per persistere la cronologia delle conversazioni nel bucket delle sessioni che abbiamo fornito nel Modulo 2.

Aggiorna i seguenti file in packages/story/dungeon_adventure_story/agent:

import logging
import os
import uuid
from functools import cache
from typing import Any, cast
from ag_ui.core import RunAgentInput
from ag_ui_strands import StrandsAgent, StrandsAgentConfig, create_strands_app
from aws_lambda_powertools.utilities import parameters
from dungeon_adventure_agent_connection import session_id_context
from fastapi import Request
from starlette.middleware.base import BaseHTTPMiddleware
from strands.session import FileSessionManager, S3SessionManager, SessionManager
from .agent import get_agent
logging.basicConfig(level=logging.INFO)
SESSION_ID_HEADER = "x-amzn-bedrock-agentcore-runtime-session-id"
@cache
def _resolve_sessions_bucket() -> str:
"""Read the conversation-history bucket name from runtime config.
Resolved lazily (and memoised) so `fastapi dev` can import this module
before ``RUNTIME_CONFIG_APP_ID`` is in the environment.
"""
application = os.environ.get("RUNTIME_CONFIG_APP_ID")
if not application:
raise RuntimeError("RUNTIME_CONFIG_APP_ID is not set — cannot resolve the StorySessions bucket.")
provider = parameters.AppConfigProvider(environment="default", application=application)
buckets = cast(dict[str, Any], provider.get("buckets", transform="json"))
return buckets["StorySessions"]["bucketName"]
def _session_manager_provider(input_data: RunAgentInput) -> SessionManager:
"""Create a session manager keyed by the AG-UI thread_id.
- In AgentCore (and `agent-serve`), persist to the shared ``StorySessions``
S3 bucket — the same bucket the Game API reads from to rebuild
conversation history on revisit.
- In `agent-dev` (`LOCAL_DEV=true`), persist to a temp
directory so the agent can run fully offline against the local MCP
server without any AWS calls.
"""
session_id = input_data.thread_id or "default"
if os.environ.get("LOCAL_DEV") == "true":
return FileSessionManager(session_id=session_id, storage_dir="/tmp/strands-sessions")
return S3SessionManager(session_id=session_id, bucket=_resolve_sessions_bucket())
# The template Agent is cloned per thread_id by ``StrandsAgent`` — we plug in
# a ``session_manager_provider`` so each thread gets its own session manager
# and conversation history is replayed on subsequent turns and survives agent
# restarts.
_agent_ctx = get_agent()
_agent = _agent_ctx.__enter__()
agui_agent = StrandsAgent(
agent=_agent,
name="StoryAgent",
description="A Strands Agent exposed via the AG-UI protocol.",
config=StrandsAgentConfig(session_manager_provider=_session_manager_provider),
)
class _SessionIdMiddleware(BaseHTTPMiddleware):
"""Bind the session ID for this request so downstream MCP / A2A clients forward it on outbound calls."""
async def dispatch(self, request: Request, call_next):
session_id = request.headers.get(SESSION_ID_HEADER) or str(uuid.uuid4())
with session_id_context(session_id):
return await call_next(request)
app = create_strands_app(agui_agent, path="/invocations")
app.add_middleware(_SessionIdMiddleware)

Le modifiche sono:

  • main.py aggiunge un session_manager_provider che crea un S3SessionManager per ogni thread_id quando deployato, e ricade su un FileSessionManager su disco sotto /tmp/strands-sessions quando eseguito con agent-dev (LOCAL_DEV=true). In deployment, il bucket S3 è lo stesso da cui legge il queryActions della Game API, così il browser può ricostruire le trascrizioni alla rivisitazione; localmente l’agente persiste le sessioni su disco e comunica con il server MCP locale, senza un deployment.
  • agent.py rimuove lo strumento di esempio subtract e sostituisce il prompt di sistema con uno da dungeon-master che invita il primo messaggio dell’utente a dichiarare il nome del giocatore e il genere, e utilizza gli strumenti del server MCP dell’inventario.

Per compilare il codice:

Terminal window
pnpm build

Il target generato agent-chat apre un REPL interattivo contro il tuo agente. Viene eseguito in modo autonomo e si connette al tuo agente in esecuzione locale, quindi prima avvia il server locale dell’agente in un terminale:

Terminal window
pnpm nx agent-dev story

Poi, in un secondo terminale, avvia la chat:

Terminal window
pnpm nx agent-chat story

Il tuo primo messaggio dovrebbe dire all’agente il nome del tuo eroe e il genere (per esempio: My name is Alice. Start my zombie adventure.) e la storia verrà trasmessa in streaming.

Complimenti. Hai creato e testato il tuo primo Strands Agent localmente! 🎉🎉🎉