Triển khai và cấu hình Story agent
Nhiệm vụ 1: Triển khai Story Agent
Phần tiêu đề “Nhiệm vụ 1: Triển khai Story Agent”Story Agent là một agent Strands được tạo ra với --protocol=ag-ui trong Mô-đun 1, vì vậy giao diện người dùng có thể stream từ nó qua giao thức Agent-User Interaction thông qua CopilotKit. Nó sử dụng Inventory MCP Server để quản lý các vật phẩm của người chơi, và đã lưu trữ lịch sử hội thoại thông qua session.py mà generator đã cung cấp trong Mô-đun 1 — khi được triển khai, đó là S3SessionManager tích hợp sẵn của Strands ghi vào cùng một S3 bucket mà queryActions của Game API đọc từ đó, để trình duyệt có thể xây dựng lại bản ghi khi truy cập lại.
Triển khai Agent
Phần tiêu đề “Triển khai Agent”Cập nhật agent.py trong packages/story/dungeon_adventure_story/agent:
from contextlib import contextmanager
from dungeon_adventure_agent_connection import InventoryMcpServerClientStrands, log_model_errors, log_tool_errorsfrom strands import Agentfrom strands.hooks import HookCallback, HookProvider
AGENT_HOOKS: list[HookProvider | HookCallback] = [log_model_errors, log_tool_errors]
@contextmanagerdef get_agent(): inventory_mcp_server = InventoryMcpServerClientStrands.create() with ( inventory_mcp_server, ): yield Agent( system_prompt="""You are running a text adventure game for a lone adventurer. When a new storybegins, the first user message will tell you the player's name and the genre(one of 'medieval', 'zombie', 'superhero'). Greet the player by name, set thescene in the chosen genre, and populate their inventory with a few startingitems. On subsequent turns, advance the story in response to the player'sactions and keep item state in sync with the narrative.Use the tools to manage the player's inventory as items are obtained or lost.Item names in the inventory must be Title Case — match them exactly.Only use list-inventory-items if you are unsure of the exact item name before modifying it.IMPORTANT: Only call ONE tool per response turn. Never batch multiple tool calls in a single turn.Ensure you specify a suitable emoji when adding items if available.Items should be a key part of the narrative.Keep responses under 100 words.""", tools=[*inventory_mcp_server.list_tools_sync()], hooks=AGENT_HOOKS, )from contextlib import contextmanager
from dungeon_adventure_agent_connection import InventoryMcpServerClientStrands, log_model_errors, log_tool_errorsfrom strands import Agent, toolfrom strands import Agentfrom strands.hooks import HookCallback, HookProviderfrom strands_tools import current_time
@tooldef subtract(a: int, b: int) -> int: return a - b
AGENT_HOOKS: list[HookProvider | HookCallback] = [log_model_errors, log_tool_errors]
@contextmanagerdef get_agent(): inventory_mcp_server = InventoryMcpServerClientStrands.create() with ( inventory_mcp_server, ): yield Agent( name="StoryAgent", description="StoryAgent Strands Agent", system_prompt="""You are a mathematical wizard.Use your tools for mathematical tasks.Refer to tools as your 'spellbook'.You are running a text adventure game for a lone adventurer. When a new storybegins, the first user message will tell you the player's name and the genre(one of 'medieval', 'zombie', 'superhero'). Greet the player by name, set thescene in the chosen genre, and populate their inventory with a few startingitems. On subsequent turns, advance the story in response to the player'sactions and keep item state in sync with the narrative.Use the tools to manage the player's inventory as items are obtained or lost.Item names in the inventory must be Title Case — match them exactly.Only use list-inventory-items if you are unsure of the exact item name before modifying it.IMPORTANT: Only call ONE tool per response turn. Never batch multiple tool calls in a single turn.Ensure you specify a suitable emoji when adding items if available.Items should be a key part of the narrative.Keep responses under 100 words.""", tools=[subtract, current_time, *inventory_mcp_server.list_tools_sync()], tools=[*inventory_mcp_server.list_tools_sync()], hooks=AGENT_HOOKS, )agent.py loại bỏ công cụ mẫu subtract và thay thế system prompt bằng một prompt dungeon-master mời tin nhắn đầu tiên của người dùng nêu tên và thể loại của người chơi, đồng thời sử dụng các công cụ của Inventory MCP Server. main.py không cần thay đổi gì ở đây — quản lý phiên đã được kết nối khi agent được tạo ra.
Nhiệm vụ 2: Kiểm tra Agent của bạn cục bộ
Phần tiêu đề “Nhiệm vụ 2: Kiểm tra Agent của bạn cục bộ”Build code
Phần tiêu đề “Build code”Để build code:
pnpm buildyarn buildnpm run buildbun buildTrò chuyện với Agent của bạn
Phần tiêu đề “Trò chuyện với Agent của bạn”Target agent-chat được tạo ra sẽ mở một REPL tương tác với agent của bạn. Nó chạy độc lập và kết nối với agent đang chạy cục bộ của bạn, vì vậy trước tiên hãy khởi động máy chủ cục bộ của agent trong một terminal:
pnpm nx agent-dev storyyarn nx agent-dev storynpx nx agent-dev storybunx nx agent-dev storySau đó, trong terminal thứ hai, khởi động chat:
pnpm nx agent-chat storyyarn nx agent-chat storynpx nx agent-chat storybunx nx agent-chat storyTin nhắn đầu tiên của bạn nên cho agent biết tên anh hùng của bạn và thể loại (ví dụ: My name is Alice. Start my zombie adventure.) và câu chuyện sẽ được stream trở lại.
Chúc mừng. Bạn đã xây dựng và kiểm tra Strands Agent đầu tiên của mình cục bộ! 🎉🎉🎉