Bỏ qua để đến nội dung

Triển khai và cấu hình 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.

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_errors
from strands import Agent
from strands.hooks import HookCallback, HookProvider
AGENT_HOOKS: list[HookProvider | HookCallback] = [log_model_errors, log_tool_errors]
@contextmanager
def 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 story
begins, 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 the
scene in the chosen genre, and populate their inventory with a few starting
items. On subsequent turns, advance the story in response to the player's
actions 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,
)

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:

Terminal window
pnpm build

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:

Terminal window
pnpm nx agent-dev story

Sau đó, trong terminal thứ hai, khởi động chat:

Terminal window
pnpm nx agent-chat story

Tin 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ộ! 🎉🎉🎉