跳转到内容

搭建 monorepo

要创建一个新的 monorepo,请在您希望的目录中运行以下命令:

Terminal window
pnpm create @aws/nx-workspace dungeon-adventure --iac=cdk

这将在 dungeon-adventure 目录中创建一个 NX monorepo。在 VSCode 中打开该目录后,您将看到如下文件结构:

  • 文件夹.nx/
  • 文件夹.vscode/
  • 文件夹node_modules/
  • 文件夹packages/ 子项目将存放于此
  • .gitignore
  • biome.json 配置 Biome 的代码检查与格式化
  • nx.json 配置 Nx CLI 和 monorepo 默认值
  • package.json 所有 node 依赖均在此定义
  • pnpm-lock.yaml 或 bun.lock、yarn.lock、package-lock.json,取决于包管理器
  • pnpm-workspace.yaml 使用 pnpm 时存在
  • README.md
  • tsconfig.base.json 所有基于 node 的子项目均继承此配置
  • tsconfig.json
  • aws-nx-plugin.config.mts Nx Plugin for AWS 的配置文件

有了工作区后,我们来搭建游戏的各个子项目——Game API、Story Agent、Inventory MCP 服务器、游戏数据库和网站——以及将它们连接在一起的配置。有两种方式可以完成:

  • 快速方式 — 直接从下方的图表中复制命令并运行。这是到达相同起点的最快方式。
  • 逐步方式 — 展开下方的章节,逐个运行每个生成器,并仔细查看每个生成器的产出。

下方的图表_就是_地牢冒险工作区:您将在本模块中构建的每个项目、组件和连接。点击 Copy commands 获取完整命令序列,然后在任务 1 中创建的 dungeon-adventure 目录中运行它们。

Loading the diagram…

逐步方式:自行运行每个生成器并查看其产出

让我们更新 packages/infra/src/stacks/application-stack.ts 以实例化一些生成的构件:

import {
GameApi,
GameUI,
InventoryMcpServer,
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 mcpServer = new InventoryMcpServer(this, 'InventoryMcpServer');
// Use Cognito for user authentication with the agent
const storyAgent = new StoryAgent(this, 'StoryAgent', {
identity: userIdentity,
});
new CfnOutput(this, 'StoryAgentArn', {
value: storyAgent.agentCoreRuntime.agentRuntimeArn,
});
new CfnOutput(this, 'InventoryMcpArn', {
value: mcpServer.agentCoreRuntime.agentRuntimeArn,
});
// Grant the agent permissions to invoke our mcp server
mcpServer.grantInvokeAccess(storyAgent);
// Grant the authenticated role access to invoke the api
gameApi.grantInvokeAccess(userIdentity.identityPool.authenticatedRole);
new GameUI(this, 'GameUI');
}
}
现在是我们第一次构建代码的时候了

使用命令行,首先运行以下命令修复任何 lint 问题:

Terminal window
pnpm lint

然后,运行以下命令进行完整构建:

Terminal window
pnpm build

您将看到以下提示:

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

此消息表明 NX 检测到一些可以自动为您更新的文件。在这种情况下,它指的是没有在引用项目上设置 TypeScript 引用的 tsconfig.json 文件。

选择 Yes, sync the changes and run the tasks 选项继续。您应该会注意到所有与 IDE 相关的导入错误都会自动解决,因为同步生成器会自动添加缺失的 TypeScript 引用!

所有构建产物现在都可以在位于 monorepo 根目录的 dist/ 文件夹中找到。这是使用 @aws/nx-plugin 生成的项目时的标准做法,因为它不会用生成的文件污染您的文件树。如果您想清理文件,只需删除 dist/ 文件夹,无需担心构建产物散落在整个文件树中。

恭喜!您已经创建了开始实现 AI 地牢冒险游戏核心所需的所有子项目。🎉🎉🎉