惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
有赞技术团队
有赞技术团队
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Check Point Blog
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
The Cloudflare Blog
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
Vercel News
Vercel News
IT之家
IT之家
MyScale Blog
MyScale Blog
博客园_首页
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
罗磊的独立博客

Wayne的博客

OpenWorker:当 AI 不再只是回答问题,而是交付成品 | Wayne的博客 OpenWorker:当 AI 不再只是回答问题,而是交付成品 | Wayne的博客 从 AI Coding 到 Agent Loop:2026H1 研发工具演进趋势 从 AI Coding 到 Agent Loop:2026 H1 研发工具演进趋势 Awesome AI Prompts:提示词收集手册 | Wayne的博客 Awesome AI Prompts:提示词收集手册 | Wayne的博客 大模型缓存技术工程指南(下):面向缓存命中的 Prompt 与 Agent 工程实践 大模型缓存技术工程指南(下):面向缓存命中的 Prompt 与 Agent 工程实践 大模型缓存技术工程指南(上):从价格信号到推理缓存机制 | Wayne的博客 大模型缓存技术工程指南(上):从价格信号到推理缓存机制 | Wayne的博客 面向 AI / Agent 友好的 CLI 开发建议 面向 AI / Agent 友好的 CLI 开发建议 【笔记】Harness Engineering(驾驭工程) | Wayne的博客 【笔记】Harness Engineering(驾驭工程) | Wayne的博客 【笔记】location.href 和 location.assign 进行页面 URL 跳转 【笔记】location.href 和 location.assign 进行页面 URL 跳转 【工具】ZeroClaw配置项(config.toml)说明 | Wayne的博客 【工具】ZeroClaw 配置项(config.toml)说明 | Wayne的博客 【工具】ZeroClaw配置项(config.toml)说明 | Wayne的博客 Skills 技术深度解析:AI Agent 能力扩展的新范式 | Wayne的博客 Skills 技术深度解析:AI Agent 能力扩展的新范式 | Wayne的博客 【笔记】浏览器 PNA/LNA 策略——记一次 iframe 中 CORS 报错的问题排查经历 【笔记】浏览器PNA/LNA策略——记一次iframe中CORS报错的问题排查经历 | Wayne的博客 【工具】Spec 驱动开发解析 | Wayne的博客 【工具】spec驱动开发解析 | Wayne的博客 Agent 设计模式 | Wayne的博客 Agent设计模式 | Wayne的博客 【工具】2025 年 AI Coding 部分产品技术分析——Claude Code 和 Codex CLI 【工具】2025年AI Coding部分产品技术分析——ClaudeCode和CodexCLI | Wayne的博客 【工具】设计-开发-测试的 AIGC 产品清单梳理 | Wayne的博客
【调研】AI 编程工具WindSurf使用技巧——WindsurfRules配置 | ...
2025-01-19 · via Wayne的博客

AI 编程工具 WindSurf 使用技巧——WindsurfRules 配置

在现代软件开发中,AI 工具正逐渐成为开发者不可或缺的助手,而 Windsurf 便是当前最热门的 AI IDE 之一。

如何利用和使用好 Windsurf 也有很多技巧,本文介绍的 WindsurfRules 规则配置就是实用的一个技巧。

介绍

WindsurfRules 是为 Windsurf 提供的一个配置文件,允许开发者定义项目中 AI 的行为规则。它分为 全局规则global_rules.md) 和 工作区规则.windsurfrules)。全局规则应用于所有项目,而工作区规则则针对特定项目进行定制。通过合理配置这些规则,开发者可以确保生成的代码符合团队的编码标准、技术栈要求以及项目的最佳实践。通过设置这些规则,我们能够指导 AI 在项目开发过程中如何生成代码,确保生成的代码符合团队的编码标准、技术栈要求及项目的最佳实践。

配置和使用

1. 规则设置

前文有提到,Windsurf 提供两种规则文件:

  • 全局规则(global_rules.md):适用于所有项目,存储在 Windsurf 的全局配置目录中。
    • 在 Linux 和 macOS 系统中,全局规则文件一般默认位于 ~/.codeium/windsurf/memories/global_rules.md
    • 在 Windows 系统中,全局规则文件位于 %USERPROFILE%\.codeium\windsurf\memories\global_rules.md
  • 工作区规则(.windsurfrules):仅适用于当前工作区或项目,存储在项目的根目录下。
    • 例如,如果你的项目目录为 C:\Projects\MyProject,则 .windsurfrules 文件应位于 C:\Projects\MyProject\.windsurfrules

另外,目前 global_rules.md.windsurfrules 文件各可以包含最多 6,000 个字符,因此总共可以有 12,000 个字符的规则。

2. 规则内容

以下场景我们都可以考虑通过 WindsurfRules 设置:

2.1. 项目初始化与配置

在项目启动阶段,开发者可以通过 .windsurfrules 文件为 AI 提供项目背景、技术栈、开发目标等信息。例如,指定项目使用的技术栈(如 TypeScript、React、Node.js 等),并要求 AI 遵循最新的最佳实践。这有助于 AI 在后续的代码生成中快速适应项目需求。

2.2. 编码规范与代码风格

团队通常会有一套统一的编码规范,通过 .windsurfrules 文件,开发者可以将这些规范转化为 AI 的行为准则。例如,规定代码的缩进方式、命名规则、注释风格等。这不仅提高了代码的可读性,还减少了团队成员之间因风格差异导致的冲突。

2.3. 性能优化与安全性

在性能优化方面,开发者可以在规则文件中定义代码优化的策略,如避免不必要的计算、优化数据库查询等。同时,安全性也是开发中不可忽视的方面。通过规则文件,开发者可以要求 AI 在生成代码时遵循安全最佳实践,例如防止 SQL 注入、确保数据验证等。

2.4. API 设计与集成

对于涉及 API 开发的项目,.windsurfrules 文件可以定义 REST、GraphQL 或 SQL 的设计规范。例如,规定 API 的命名规则、数据结构、错误处理机制等。这有助于确保生成的 API 代码符合行业标准,易于维护和扩展。

2.5. 团队协作与知识共享

在一个团队中,不同成员可能对项目的理解和经验有所不同。通过 .windsurfrules 文件,团队可以将项目的核心要求和最佳实践共享给每一位成员,确保团队协作的高效性和一致性。

3.配置技巧

3.1. 明确项目背景与目标

.windsurfrules 文件的开头,提供清晰的项目背景和目标描述,帮助 AI 理解开发环境和技术栈。例如:

1
2
3
4
5
6
7
8
## AI Guidelines

You are an expert programming assistant focusing on:

- TypeScript, React, Node.js, Next.js, and Prisma
- Shadcn UI, Ant Design, RICH Design principle, and Tailwind CSS useations
- Latest features and best practices
- Clear, readable, and maintainable code

这样的描述可以帮助 AI 更好地理解项目的上下文。

3.2. 细化编码规范

在规则文件中,开发者可以详细定义代码的格式和风格。例如定义性能优化和安全规则:

1
2
3
4
5
6
7
8
9
10
11
### Performance

- Code splitting, image/bundle optimization
- Caching, lazy loading, key props
- Database query optimization

### Security

- Input: sanitize data, validate types, escape properly, secure uploads
- Auth: JWT handling, secure sessions, token refresh, RBAC
- Protection: CSP headers, prevent XSS/CSRF, secure APIs, follow OWASP

这些规则可以指导 AI 在生成代码时考虑性能和安全性。

3.3. 设置性能与安全规则

性能和安全是现代开发中的关键。开发者可以在规则文件中定义以下内容:

1
2
3
4
5
6
7
8
9
10
11
### Performance

- Code splitting, image/bundle optimization
- Caching, lazy loading, key props
- Database query optimization

### Security

- Input: sanitize data, validate types, escape properly, secure uploads
- Auth: JWT handling, secure sessions, token refresh, RBAC
- Protection: CSP headers, prevent XSS/CSRF, secure APIs, follow OWASP

这些规则可以指导 AI 在生成代码时考虑性能和安全性。

3.4 部署与监控

设定构建、部署和监控的流程,帮助 AI 自动化处理部署工作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
## Build and Deployment

### Build Process

- **Linting**: Run ESLint and Prettier during the build process to ensure consistent code style.
- **Testing**: Execute unit tests and integration tests to ensure all tests pass before deployment.
- **Type Checking**: Ensure TypeScript type coverage meets project requirements.
- **Bundle Optimization**: Optimize the build using Webpack or Rollup to reduce the final bundle size.

### Deployment Strategy

- **Semantic Versioning**: Use semantic versioning for releases to clearly define version changes.
- **Blue-Green Deployment**: Implement blue-green deployment to minimize downtime during deployment.
- **Rollback Mechanism**: Provide rollback capabilities to quickly revert to the previous version if the deployment fails.
- **Health Checks**: Automatically run health checks after deployment to ensure the application is running smoothly.

## Monitoring

### Application Monitoring

- **Real-time Monitoring**: Use Prometheus or Grafana to monitor application performance in real-time.
- **Log Management**: Integrate with ELK Stack or another log management tool to centralize application logs.
- **Error Tracking**: Use Sentry or a similar tool to capture and track runtime errors.
- **Performance Metrics**: Monitor key performance metrics such as response time, throughput, and resource utilization.

### Infrastructure Monitoring

- **Server Health**: Use Nagios or Zabbix to monitor server health, including CPU, memory, and disk usage.
- \*\*Network Monitori

通过在 .windsurfrules 文件中定义这些规则,Windsurf 的 AI 助手可以更好地理解项目的部署和监控需求,从而生成符合最佳实践的代码和自动化脚本。

3.5. 灵活使用全局规则与工作区规则

全局规则适用于所有项目,而工作区规则则针对特定项目。开发者可以根据项目的不同需求,灵活配置这两类规则。例如,全局规则可以定义通用的编码规范和安全标准,而工作区规则则可以针对特定项目的框架或技术栈进行定制。

3.6. 持续优化规则文件

随着项目的进展和技术栈的更新,规则文件也需要不断优化。开发者可以根据实际需求,随时修改规则文件,并在下一次使用时生效。规则文件可以随时修改,修改后在下一次使用时生效。

对于特定于工作区的规则,建议将 .windsurfrules 文件添加到项目的 .gitignore 文件中,以避免不必要的版本控制。


示例

以一份前端 React + TailwindCSS + TypeScript 技术栈为例,以下是一个典型的 React 项目结构,包含 .windsurfrules 文件:

1
2
3
4
5
6
7
8
9
10
/my-react-project
├── .windsurfrules
├── package.json
├── tsconfig.json
├── next.config.js
├── pages/
├── components/
├── styles/
├── public/
└── ...

全局规则(global_rules.md)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
## AI Guidelines

You are an expert React developer with a focus on:

- React, TypeScript, Next.js, and Tailwind CSS
- Modern JavaScript (ES6+), including async/await and modern syntax
- Component-based architecture and design patterns
- Performance optimization and best practices
- Security best practices, including data validation and sanitization

## General Rules

- Always use functional components and hooks.
- Prefer TypeScript for type safety.
- Use Tailwind CSS for styling.
- Follow the latest React best practices.
- Ensure code is readable, maintainable, and well-documented.

## Code Formatting

- Indentation: 2 spaces
- Line length: 80 characters (soft limit)
- Use template literals and arrow functions.
- Use trailing commas and same-line braces.
- Destructure props and use path aliases for TypeScript imports.

## Performance

- Use React.memo and useCallback for optimizing component re-renders.
- Implement code splitting with React.lazy and Suspense.
- Optimize images and assets with Next.js Image component.
- Use caching strategies for API calls.

## Security

- Sanitize all user inputs.
- Validate data types and escape content where necessary.
- Follow OWASP guidelines for preventing XSS and CSRF attacks.
- Use secure authentication practices (e.g., JWT, OAuth).

工作区规则(.windsurfrules)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
## Project-Specific Guidelines

You are working on a React project with the following details:

- Framework: Next.js
- Styling: Tailwind CSS
- State Management: Redux Toolkit
- Routing: Next.js built-in routing
- API Integration: Axios for API calls

## Project Rules

- Use Next.js pages and components structure.
- Use Tailwind CSS for all styling needs.
- Use Redux Toolkit for state management.
- Use Axios for all API requests.
- Follow the project's specific coding conventions and naming conventions.

## Code Formatting

- Use 2 spaces for indentation.
- Use single quotes for strings unless double quotes are necessary.
- Use arrow functions for all functional components.
- Destructure props and use path aliases for TypeScript imports.
- Use consistent naming conventions for components and files (e.g., PascalCase for components).

## Performance

- Implement Next.js Image component for all images.
- Use React.memo and useCallback for optimizing component re-renders.
- Implement code splitting with React.lazy and Suspense.
- Optimize API calls with caching and debouncing.

## Security

- Sanitize all user inputs using a library like DOMPurify.
- Validate data types and escape content where necessary.
- Use secure authentication practices (e.g., JWT, OAuth).
- Follow OWASP guidelines for preventing XSS and CSRF attacks.
- Ensure all API calls are secure and follow best practices.

其他

社区支持:GitHub 上有社区维护的 Awesome WindsurfRules 项目,提供了一系列优秀的规则文件示例,供开发者参考和使用。(WindsurfRules 本质和 CursorRules 作用其实差不多)

在 2025 年 1 月 的更新中,Windsurf 推出了 Windsurf Wave 2,对 Cascade 辅助系统进行了多项升级,包括实时网络搜索、自动记忆系统等。这些更新可能对规则文件的使用方式和功能产生了进一步的优化,但具体规则文件的变更内容未明确提及。


相关链接