AI Agent Skills 是连接团队规范与 AI 协作的桥梁。本文详解其作为开放标准的结构(SKILL.md),涵盖元数据定义、指令正文及资源组织。通过十个核心原则,如只写私有约定、按风险调管控力度、内容分层加载等,指导开发者编写高效技能。结合实战案例与避坑指南,帮助前端团队利用 Cursor、Copilot 等工具实现自动化任务执行,提升开发效率与代码一致性。
芝士奶盖1 浏览
背景
用 Cursor 写代码时,明明团队有自己的组件规范,但 AI 生成的代码风格却对不上号,每次都要手动改半天。这不是 AI 不够聪明,而是你没'教'过它。
---
name: react-component-creator
description: Create React components with TypeScript and hooks. Use when the user asks to create, generate, or scaffold a React component.
---# React Component Creator## Instructions1. Create component file at `src/components/[ComponentName]/index.tsx`2. Use function component with TypeScript interface for props
3. Export as named export (not default export)
## Component Template```tsx
interface [Name]Props {
// props here
}
export function [Name]({ ...props }: [Name]Props) {
return <section>...</section>
}
Conventions
File naming: PascalCase for components, camelCase for hooks
One component per file
Co-locate styles: [ComponentName].module.css
Co-locate tests: [ComponentName].test.tsx
## 写好 Skill 的 10 个核心原则
知道了怎么写一个 SKILL.md 之后,怎么把它写好才是关键。基于大量优秀案例和实践试错,总结出这 10 个核心原则。它们各自解决的问题完全不同,缺哪个都会踩坑。
### 原则一:只写 Agent 不知道的事
Agent 已经是一个训练有素的高级开发了——它知道 React 怎么写、TypeScript 怎么用、怎么发起 HTTP 请求。你不需要教它这些。
你真正需要写的,是那些只存在于团队脑子里的知识:我们的状态管理用 zustand 不用 redux、接口请求统一走 `src/api/request.ts` 里封装的方法、日期用 dayjs 不用 moment...这些潜规则 Agent 没法从公开知识里推断出来。
举个例子,下面两种写法对比:
```typescript
// ❌ 浪费:Agent 自己就知道怎么用 fetch
When making HTTP requests, you can use the fetch API. The fetch function takes a URL and an options object containing method, headers, and body...
// ✅ 有价值:这是你们团队的私有约定
All API calls MUST use the project's request wrapper:
```ts
import { request } from'@/api/request'// GETconst users = await request.get<User[]>('/api/users')
// POSTawait request.post('/api/users', { name: 'Winty' })
## Design Thinking
Before coding, understand the context and commit to a BOLD aesthetic direction:
-**Purpose**: What problem does this interface solve? Who uses it?
-**Tone**: Pick an extreme: brutally minimal, maximalist chaos, retro-futuristic, luxury/refined, playful/toy-like...
-**Constraints**: Technical requirements (framework, performance, accessibility)
-**Differentiation**: What makes this UNFORGETTABLE?
这个案例好在哪里?
description 写得精准:既描述了能力(create frontend interfaces),又明确了触发时机(when the user asks to build web components, pages...)
## Decision Tree: Choosing Your Approach
User task → Is it static HTML?
├─ Yes → Read HTML file directly to identify selectors
│ └─ Write Playwright script using selectors
│ └─ No (dynamic webapp) → Is the server already running?
│ ├─ No → Run: python scripts/with_server.py --help
│ └─ Yes → Reconnaissance-then-action:
│ 1. Navigate and wait for networkidle
│ 2. Take screenshot or inspect DOM
│ 3. Identify selectors from rendered state
│ 4. Execute actions with discovered selectors
这个写法的亮点在于:
决策树清晰:Agent 能快速判断走哪条路径
脚本当黑盒用:不需要把脚本内容加载到上下文,用 --help 了解用法后直接调用
有明确的反模式提示:用 ❌ 和 ✅ 标记常见陷阱
案例三:GitHub Copilot docs-agent —— 角色定位模式
来自 GitHub 官方博客对 2500+ 个 agents.md 的最佳实践总结:
---name:docs_agentdescription:Experttechnicalwriterforthisproject---Youareanexperttechnicalwriterforthisproject.## Your role-YouarefluentinMarkdownandcanreadTypeScriptcode-Youwriteforadeveloperaudience-Your task:readcodefrom`src/`andgeneratedocumentationin`docs/`## Commands you can useBuild docs:`npmrundocs:build`Lint markdown:`npxmarkdownlintdocs/`## Boundaries-✅Always do:Writenewfilesto`docs/`,followstyleexamples-⚠️Ask first:Beforemodifyingexistingdocumentsinamajorway-🚫Never do:Modifycodein`src/`,editconfigfiles,commitsecrets
---
name: react-component
description: Scaffold React components with TypeScript following team conventions. Use when the user asks to create, generate, or scaffold a React component, or mentions creating a new UI element.
---# React Component Generator## File Structure
For a component named `UserCard`:
这个 Skill 只有约 60 行正文,但信息密度很高:有文件结构、代码模板、规则清单、测试模板和明确边界。Agent 拿到后能直接执行,不需要额外猜测。
## 避坑指南
有了实战经验,再来看看常见的坑。这些都是我踩过或者看别人踩过的,帮大家提前避开:
### 1. description 写得太模糊
```markdown
# ❌ Agent 完全不知道什么时候该用你
description: Helps with frontend stuff.
# ✅ 明确能力 + 触发场景
description: Create and style React components with TypeScript and CSS Modules. Use when building UI components, pages, or layouts.
2. 正文太啰嗦
AI Agent 已经很聪明了,不需要解释什么是 React、什么是 TypeScript。只写它不知道的信息:你们团队的规范、项目的特殊约定、偏好的技术方案。
3. 给太多选择
# ❌ Agent 会困惑到底该用哪个
You can use styled-components, or CSS Modules, or Tailwind, or Emotion...
# ✅ 给一个默认方案,必要时提供备选
Use CSS Modules for component styling. For utility-based layouts, use Tailwind CSS classes instead.