决策矩阵——项目检测与规则选择
检测顺序
按此顺序运行检测。每个步骤读取文件并设置后续步骤使用的标志。
1. 包管理器
检查锁文件:bun.lock→ bun,pnpm-lock.yaml→ pnpm,package-lock.json→ npm,yarn.lock→ yarn。
2. 项目类型
根package.json中的workspaces→ 单一代码库。否则 → 单一应用。
3. 框架
| Dep found | Framework | Router detection |
|---|
next | Next.js | app/haspage.tsxorlayout.tsx→ App Router;pages/has.tsx→ Pages Router; both → hybrid (treat as App Router primary) |
@nestjs/core | NestJS | N/A |
vite | Vite | Checkreactfor React,vuefor Vue,sveltefor Svelte |
@angular/core | Angular | N/A |
express | Express | N/A |
fastify | Fastify | N/A |
remix/@remix-run/* | Remix | N/A |
astro | Astro | N/A |
4. 单仓库工具(如果是单仓库)
| File found | Tool |
|---|
turbo.json | Turborepo |
nx.json | Nx |
lerna.json | Lerna |
pnpm-workspace.yamlonly | pnpm workspaces (no orchestrator) |
5. 语言
tsconfig.json存在 → TypeScript。检查compilerOptions.strict: true→ 严格模式。
6. CSS 方法
| Signal | Approach |
|---|
tailwindcssin deps | Tailwind CSS |
components.jsonexists | shadcn/ui (implies Tailwind) |
styled-componentsin deps | styled-components |
@emotion/*in deps | Emotion |
.module.cssor.module.scssfiles found | CSS Modules |
sassornode-sassin deps | Sass/SCSS |
| None of the above | Plain CSS / CSS imports |
7. 测试
| Dep found | Runner | Extra |
|---|
vitest | Vitest | Checkvitest.config.*for env (node/jsdom/happy-dom) |
jest | Jest | Checkjest.config.*for env |
playwrightor@playwright/test | Playwright | E2E tests present |
cypress | Cypress | E2E tests present |
| None | — | Skip testing rules |
8. 验证
| Dep found | Library |
|---|
zod | Zod |
yup | Yup |
class-validator | class-validator |
valibot | Valibot |
| None | Plain TypeScript (type guards, manual validation) |
9. ORM / 数据库
| Dep found | ORM | Detect provider |
|---|
prisma | Prisma | Readprisma/schema.prisma→datasource db { provider = "..." } |
drizzle-orm | Drizzle | Checkdrizzle.config.*fordialect |
knex | Knex | Checkknexfile.*for client |
typeorm | TypeORM | Check config fortype |
mongoose | MongoDB/Mongoose | N/A |
提供程序影响ID类型约定(PostgreSQL使用UUID,SQLite/MySQL使用自增,MongoDB使用ObjectId)。
10. 状态管理
| Dep found | Library |
|---|
zustand | Zustand |
@reduxjs/toolkit | Redux Toolkit |
jotai | Jotai |
valtio | Valtio |
recoil | Recoil |
mobx | MobX |
xstate | XState |
@tanstack/react-query | React Query (server state) |
swr | SWR (server state) |
| None | useState / useReducer only |
服务器状态库(React Query、SWR)需要与客户端状态(Zustand、Redux)不同的规则。
11. API 客户端模式
| Signal | Pattern |
|---|
@trpc/*in deps | tRPC |
graphql+@apollo/client | GraphQL (Apollo) |
graphql+relay-runtime | GraphQL (Relay) |
@tanstack/react-query+fetch | REST with React Query |
swr+fetch | REST with SWR |
axiosin deps | REST with Axios |
Server action files ("use server") found | Server Actions |
app/api/withroute.tsfiles | Next.js API Routes |
src/with NestJS controllers | NestJS REST |
| None of the above | fetch() directly |
12. 表单库
| Dep found | Library |
|---|
react-hook-form | react-hook-form |
formik | Formik |
@tanstack/react-form | TanStack Form |
@hookform/resolvers | react-hook-form + Zod/Yup resolver |
| None | Controlled/uncontrolled inputs manually |
13. 认证库
| Dep found | Library | Extra detection |
|---|
next-auth | NextAuth v5 (Auth.js) | Check forauth.ts,middleware.tsroute protection |
next-authv4 | NextAuth v4 | Check for[...nextauth].ts |
@clerk/nextjs | Clerk | Check formiddleware.ts |
lucia/lucia-auth | Lucia | Check forauth.ts |
@supabase/supabase-js+@supabase/ssr | Supabase Auth | Check for middleware |
firebase+firebase/auth | Firebase Auth | Check forfirebase.tsconfig |
@auth0/* | Auth0 | Check forauth0.ts |
| None | No auth or custom | — |
14. 国际化库
| Dep found | Library | Extra detection |
|---|
next-intl | next-intl | Checki18n.ts,messages/, middleware config |
react-i18next+i18next | react-i18next | Checki18n.ts, locale JSON files |
next-i18next | next-i18next | Checknext-i18next.config.js |
lingui/* | Lingui | Checklingui.config.* |
| None | No i18n | Checklang=in<html>for language hint |
15. 后端模式
| Signal | Pattern |
|---|
Files containing"use server" | Server Actions |
app/api/withroute.ts | Next.js API Routes |
| NestJS controllers | NestJS REST |
| Express/Fastify route files | REST API |
| tRPC routers | tRPC API |
| GraphQL resolvers | GraphQL API |
16. 代码检查与质量
| File/Dep found | Tool |
|---|
eslintin deps | ESLint |
eslint.config.* | ESLint flat config |
.eslintrc.* | ESLint legacy config |
prettierin deps | Prettier |
.prettierrc* | Prettier configured |
react-doctorin deps | react-doctor |
doctor.config.* | react-doctor configured |
biome.json | Biome |
.oxlintrc.* | oxlint |
规则文件选择
将检测到的标志映射到规则文件以生成:
| Rule File | Required When |
|---|
architecture.md | Always |
frontend-patterns.md | Has React/Next.js/Vite/Angular/Vue/Svelte frontend |
backend-patterns.md | Has NestJS backend |
server-actions.md | Uses server actions OR has API routes OR has NestJS/Express/Fastify backend |
styling.md | Has Tailwind, CSS Modules, or styled-components (not plain CSS) |
forms.md | Has react-hook-form, formik, or TanStack Form |
database.md | Has Prisma, Drizzle, Knex, TypeORM, or Mongoose |
i18n.md | Has next-intl, react-i18next, or similar |
testing.md | Has test runner (vitest/jest/playwright/cypress) in devDeps |
git-workflow.md | Always |
sdd-workflow.md | Always |
AGENTS.md 部分选择
根据检测生成的 AGENTS.md 中包含的部分:
| Section | Include When |
|---|
| Monorepo Structure | workspacesin package.json |
| Monorepo Tool Commands | Turborepo/Nx/Lerna detected |
| Next.js Router Convention | Next.js detected → App Router vs Pages Router rules |
| Zod Validation Convention | zodoryuporvalibotin deps |
| Plain TS Validation Convention | No validation library, but TypeScript strict |
| Prisma Convention | prismain deps |
| Drizzle Convention | drizzle-ormin deps |
| Server Actions Convention | "use server"files detected |
| tRPC Convention | tRPC detected |
| GraphQL Convention | GraphQL detected |
| Tailwind Convention | tailwindcssin deps |
| shadcn/ui Convention | components.jsonexists |
| CSS Modules Convention | .module.cssfiles found |
| Styled Components Convention | styled-componentsin deps |
| State Management Section | Zustand/Redux/Jotai/MobX/XState in deps |
| Server State Section | React Query/SWR in deps |
| Form Library Section | react-hook-form/formik/TanStack Form in deps |
| i18n Section | i18n library detected |
| Auth Section | Auth library detected |
| react-doctor Convention | doctor.config.*orreact-doctorin deps |
| UI Language Convention | Checklang=attribute or i18n config |
命令生成
从package.json的scripts生成命令表。直接映射scripts键:
{"scripts":{"dev":"...","build":"...","lint":"...","test":"..."}}
变为:
| Comando | Qué hace |
|---|
{pm} dev | Servidor de desarrollo |
{pm} run build | Build de producción |
{pm} run lint | Linting |
{pm} run test | Tests (watch) |
为以下内容添加额外行:test:run(单次运行)、test:coverage、doctor、format、typecheck。
其中{pm}= 对于 bun(不带run的脚本)为bun,pnpm 为pnpm,npm 为npm run,yarn 为yarn。
Monorepo 命令生成
如果使用 Turborepo 的 monorepo:生成turbo run {cmd}变体。
如果使用 pnpm 的 monorepo:生成pnpm --filter {pkg} {cmd}示例。
如果使用 Nx 的单仓库:生成nx {cmd} {pkg}变体。
验证周期
从检测到的工具组装:
{typecheck} → {lint} → {test} → {doctor}
{typecheck}:如果是 TypeScript,则使用tsc --noEmit,如果是 bun,则使用bunx tsc --noEmit,如果没有 TS 则跳过。{lint}:{pm} run lint或检测到的 lint 命令。{test}:{pm} run test:run或{pm} run test(取单次运行的那个)。{doctor}:{pm} doctor或{pm}x react-doctor@latest如果检测到 react-doctor。如果没有检测到则跳过。
单仓库重建说明
如果是带共享包的单一代码库:添加关于在循环之前重建包的说明:
Si se modificaron packages compartidos ({shared_pkg_list}), ejecutar su rebuild primero: {rebuild_commands}
特定框架的约定
Next.js 应用路由器
- 默认使用服务器组件。只有在使用钩子时才使用
"use client"。 - 服务器操作在
lib/actions.ts或src/actions/。 error.tsx、loading.tsx、not-found.tsx按段落。generateMetadata()用于 SEO。next/image用于图片,next/font用于字体。- 路径别名
@/*→./*或./src/*。
Next.js 页面路由器
getServerSideProps、getStaticProps、getStaticPaths。pages/api/用于 API 路由。pages/_app.tsx,pages/_document.tsx。- 没有服务器组件。所有都是客户端或 API。
NestJS
- 模块像目录:
src/{feature}/{feature}.module.ts。 - 带装饰器的 DTO。使用
class-validator或ZodValidationPipe进行验证。 - 守卫用于身份验证:
@UseGuards(AuthGuard)。 PrismaService作为全局提供者。
边缘情况
| Situation | Action |
|---|
| No test runner | Skip testing.md, skip test from verification cycle |
| No lint tool | Skip lint from verification cycle |
| No TypeScript | Skiptsc --noEmit, skip no-any rule, use JSDoc conventions instead |
| No config files at all | Minimal defaults, note that project needs setup |
| Multiple frameworks | Pick primary based on root package.json, note secondary |
| Both App Router and Pages Router | Treat as App Router primary, note Pages Router legacy routes |
| No package.json scripts | Generate commands from direct tool invocations (npx next dev) |
Bun withoutbun runscripts | Usebunxprefix for npx equivalents |
| Monorepo without workspaces | Treat eachapps/*orpackages/*as independent |
| CSS: multiple approaches | Pick primary (Tailwind wins over CSS Modules over plain CSS) |
| Auth: multiple libraries | Pick primary, note secondary (e.g., NextAuth for web, Clerk for admin) |
| i18n with Pages Router | Different routing rules than App Router i18n |
| Database with multiple providers | Document each, note which is primary |