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

Thiết lập monorepo

Để tạo một monorepo mới, từ thư mục mong muốn của bạn, chạy lệnh sau:

Terminal window
npx create-nx-workspace@22.0.2 dungeon-adventure --pm=pnpm --preset=@aws/nx-plugin --iacProvider=CDK --ci=skip --aiAgents

Điều này sẽ thiết lập một NX monorepo trong thư mục dungeon-adventure. Khi bạn mở thư mục trong VSCode, bạn sẽ thấy cấu trúc tệp này:

  • Thư mục.nx/
  • Thư mục.vscode/
  • Thư mụcnode_modules/
  • Thư mụcpackages/ đây là nơi các dự án con của bạn sẽ nằm
  • .gitignore
  • .npmrc
  • .prettierignore
  • .prettierrc
  • nx.json cấu hình Nx CLI và các giá trị mặc định của monorepo
  • package.json tất cả các phụ thuộc node được định nghĩa ở đây
  • pnpm-lock.yaml hoặc bun.lock, yarn.lock, package-lock.json tùy thuộc vào trình quản lý gói
  • pnpm-workspace.yaml nếu sử dụng pnpm
  • README.md
  • tsconfig.base.json tất cả các dự án con dựa trên node đều mở rộng từ tệp này
  • tsconfig.json
  • aws-nx-plugin.config.mts cấu hình cho Nx Plugin cho AWS

Bây giờ chúng ta có thể bắt đầu tạo các dự án con khác nhau bằng cách sử dụng @aws/nx-plugin.

Đầu tiên, hãy tạo Game API của chúng ta. Để làm điều này, tạo một tRPC API có tên GameApi bằng các bước sau:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - ts#trpc-api
  5. Fill in the required parameters
    • name: GameApi
  6. Click Generate

Bạn sẽ thấy một số tệp mới xuất hiện trong cây tệp của mình.

Nhấp vào đây để xem xét các tệp này chi tiết hơn.

Bây giờ hãy tạo các Story Agent của chúng ta.

Để tạo một dự án Python:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - py#project
  5. Fill in the required parameters
    • name: story
  6. Click Generate

Bạn sẽ thấy một số tệp mới xuất hiện trong cây tệp của mình.

Nhấp vào đây để xem xét các tệp này chi tiết hơn.

Để thêm một Strands agent vào dự án với trình tạo py#strands-agent:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - py#strands-agent
  5. Fill in the required parameters
    • project: story
  6. Click Generate

Bạn sẽ thấy một số tệp mới xuất hiện trong cây tệp của mình.

Nhấp vào đây để xem xét các tệp này chi tiết hơn.

Nhiệm vụ 4: Thiết lập công cụ inventory

Phần tiêu đề “Nhiệm vụ 4: Thiết lập công cụ inventory”

Hãy tạo một máy chủ MCP để cung cấp công cụ cho Story Agent của chúng ta để quản lý kho đồ của người chơi.

Đầu tiên, chúng ta tạo một dự án TypeScript:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - ts#project
  5. Fill in the required parameters
    • name: inventory
  6. Click Generate

Điều này sẽ tạo một dự án TypeScript trống.

Nhấp vào đây để xem xét các tệp này chi tiết hơn.

Tiếp theo, chúng ta sẽ thêm một máy chủ MCP vào dự án TypeScript của mình:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - ts#mcp-server
  5. Fill in the required parameters
    • project: inventory
  6. Click Generate

Điều này sẽ thêm một máy chủ MCP.

Nhấp vào đây để xem xét các tệp này chi tiết hơn.

Nhiệm vụ 5: Tạo Giao diện Người dùng (UI)

Phần tiêu đề “Nhiệm vụ 5: Tạo Giao diện Người dùng (UI)”

Trong nhiệm vụ này, chúng ta sẽ tạo UI cho phép bạn tương tác với trò chơi.

Để tạo UI, tạo một website có tên GameUI bằng các bước sau:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - ts#react-website
  5. Fill in the required parameters
    • name: GameUI
  6. Click Generate

Bạn sẽ thấy một số tệp mới xuất hiện trong cây tệp của mình.

Nhấp vào đây để xem xét các tệp này chi tiết hơn.

Hãy cấu hình Game UI của chúng ta để yêu cầu truy cập được xác thực qua Amazon Cognito bằng các bước sau:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - ts#react-website#auth
  5. Fill in the required parameters
    • cognitoDomain: game-ui
    • project: @dungeon-adventure/game-ui
    • allowSignup: true
  6. Click Generate

Bạn sẽ thấy một số tệp mới xuất hiện/thay đổi trong cây tệp của mình.

Nhấp vào đây để xem xét các tệp này chi tiết hơn.

Hãy cấu hình Game UI của chúng ta để kết nối với Game API đã tạo trước đó.

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - api-connection
  5. Fill in the required parameters
    • sourceProject: @dungeon-adventure/game-ui
    • targetProject: @dungeon-adventure/game-api
  6. Click Generate

Bạn sẽ thấy một số tệp mới xuất hiện/thay đổi trong cây tệp của mình.

Nhấp vào đây để xem xét các tệp này chi tiết hơn.

Hãy tạo dự án con cuối cùng cho cơ sở hạ tầng CDK.

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - ts#infra
  5. Fill in the required parameters
    • name: infra
  6. Click Generate

Bạn sẽ thấy một số tệp mới xuất hiện/thay đổi trong cây tệp của mình.

Nhấp vào đây để xem xét các tệp này chi tiết hơn.

Nhiệm vụ 6: Cập nhật cơ sở hạ tầng của chúng ta

Phần tiêu đề “Nhiệm vụ 6: Cập nhật cơ sở hạ tầng của chúng ta”

Hãy cập nhật packages/infra/src/stacks/application-stack.ts để khởi tạo một số construct đã tạo của chúng ta:

import {
GameApi,
GameUI,
InventoryMcpServer,
RuntimeConfig,
StoryAgent,
UserIdentity,
} from ':dungeon-adventure/common-constructs';
import { Stack, StackProps, CfnOutput } from 'aws-cdk-lib';
import { Construct } from 'constructs';
export class ApplicationStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const userIdentity = new UserIdentity(this, 'UserIdentity');
const gameApi = new GameApi(this, 'GameApi', {
integrations: GameApi.defaultIntegrations(this).build(),
});
const { userPool, userPoolClient } = userIdentity;
const mcpServer = new InventoryMcpServer(this, 'InventoryMcpServer');
// Use Cognito for user authentication with the agent
const storyAgent = new StoryAgent(this, 'StoryAgent', {
authorizerConfiguration: {
customJwtAuthorizer: {
discoveryUrl: `https://cognito-idp.${Stack.of(userPool).region}.amazonaws.com/${userPool.userPoolId}/.well-known/openid-configuration`,
allowedAudience: [userPoolClient.userPoolClientId],
},
},
environment: {
INVENTORY_MCP_ARN: mcpServer.agentCoreRuntime.arn,
},
});
// Add the Story Agent ARN to runtime-config.json so it can be referenced by the website
RuntimeConfig.ensure(this).config.agentArn =
storyAgent.agentCoreRuntime.arn;
new CfnOutput(this, 'StoryAgentArn', {
value: storyAgent.agentCoreRuntime.arn,
});
new CfnOutput(this, 'InventoryMcpArn', {
value: mcpServer.agentCoreRuntime.arn,
});
// Grant the agent permissions to invoke our mcp server
mcpServer.agentCoreRuntime.grantInvoke(storyAgent.agentCoreRuntime);
// Grant the authenticated role access to invoke the api
gameApi.grantInvokeAccess(userIdentity.identityPool.authenticatedRole);
// Ensure this is instantiated last so our runtime-config.json can be automatically configured
new GameUI(this, 'GameUI');
}
}
Bây giờ đã đến lúc chúng ta build mã lần đầu tiên

Sử dụng dòng lệnh, chạy lệnh sau để sửa các lỗi lint trước:

Terminal window
pnpm nx run-many --target lint --configuration=fix --all

Sau đó, chạy lệnh sau để thực hiện build đầy đủ:

Terminal window
pnpm nx run-many --target build --all

Bạn sẽ được nhắc với thông báo sau:

Terminal window
NX The workspace is out of sync
[@nx/js:typescript-sync]: Some TypeScript configuration files are missing project references to the projects they depend on or contain outdated project references.
This will result in an error in CI.
? Would you like to sync the identified changes to get your workspace up to date?
Yes, sync the changes and run the tasks
No, run the tasks without syncing the changes

Thông báo này cho biết rằng NX đã phát hiện một số tệp có thể được cập nhật tự động cho bạn. Trong trường hợp này, nó đề cập đến các tệp tsconfig.json không có tham chiếu Typescript được thiết lập trên các dự án tham chiếu.

Chọn tùy chọn Yes, sync the changes and run the tasks để tiếp tục. Bạn sẽ nhận thấy tất cả các lỗi import liên quan đến IDE của bạn được tự động giải quyết vì trình tạo sync sẽ tự động thêm các tham chiếu typescript còn thiếu!

Tất cả các artifact đã build hiện có sẵn trong thư mục dist/ nằm ở gốc của monorepo. Đây là một thực hành tiêu chuẩn khi sử dụng các dự án được tạo bởi @aws/nx-plugin vì nó không làm ô nhiễm cây tệp của bạn với các tệp được tạo. Trong trường hợp bạn muốn dọn dẹp các tệp của mình, hãy xóa thư mục dist/ mà không phải lo lắng về các artifact build bị rải rác khắp cây tệp.

Chúc mừng! Bạn đã tạo tất cả các dự án con cần thiết để bắt đầu triển khai cốt lõi của trò chơi AI Dungeon Adventure của chúng ta. 🎉🎉🎉