前言
本技术文档系统阐述如何通过Cursor IDE集成Gemini(gemini-exp-1206、gemini-2.0-flash-exp)与Claude(3.5 sonnet及3 opus)进行高效软件开发,确保代码完整性与项目可维护性。
1. 项目初始化:为AI协作奠定基石
将您的项目想象成一个井然有序的工作坊,在这里,您与AI协作者(Gemini和Claude)将共同打造非凡成果。首要任务就是为这个"工作坊"做好妥善搭建。
1.1 定义协作规则(.cursorrules)
就像任何研讨会都需要规则一样,您的 AI 合作者也需要指导方针。在项目的根目录下创建一个. cursorrules规则文件。该文件充当宪法,定义 AI 应该如何与您的代码交互。
为什么:这确保了每个人(您和 AI)在优先级、编码标准以及如何处理任务方面都在意见一致。
示例.cursorrules
{"rules": {"context_initialization": {"description": "Starting point for each interaction","steps": ["ALWAYS read `.notes/project_overview.md` and `.notes/task_list.md`"]},"operational_protocol": {"description": "How to approach tasks","before_action": ["Create a MECE task breakdown"],"code_changes": ["Read relevant code sections before editing","Preserve existing functionality","Maintain type safety"]},"safety_requirements": ["NEVER break type safety","ALWAYS maintain proper error handling","ALWAYS document new code"],"priorities": [{"source": ".notes/","weight": 1.0}],"modes": {"base": {"description": "For routine tasks"},"enhanced": {"description": "For complex problems"}},"project_directives": {"name": "my_project","ai_first": true}}}
1.2. 使用 .cursorignore控制文件哪些文件可以忽略
使用.cursorignore告诉您的 AI 助手忽略什么,类似于您使用.gitignore 进行版本控制的方式。
为什么:这使 AI 专注于项目的基本部分,减少噪音并提高效率。
示例.cursorignore:
/node_modules/build/temp.DS_Store
1.3 使用 .notes 构建项目知识中枢
每个项目都需要一个信息中心。创建一个。笔记目录来存储所有与项目相关的留档、会议笔记、架构图和人工智能交互日志。把它想象成你项目的 “大脑” 或 “知识库” 为什么:这确保了你和你的人工智能合作者对项目的目标、状态和历史有共同的理解
关键的.notes:
|
文件 |
功能描述 |
|---|---|
|
project_overview.md |
对您的项目、其目标和架构的高级描述 |
|
task_list.md |
项目、目标和架构的高级描述。task_list.md:任务的详细列表、它们的状态(例如,“待办”、“进行中”、“完成”)、优先级和任何相关注释 |
|
directory_structure.md |
项目目录结构的自动更新概述。这有助于人工智能了解不同的代码组件位于何处。 |
|
meeting_notes.md |
你与人工智能的交互日志,包括提出的问题。 |
2.管理. note 和共享上下文
想象一下,你正在和一个团队一起工作。为了有效协作,你需要确保每个人都对项目有共同的理解,比如 Scrum/kanban/ 项目方案。这就是使用降价文档作为共享记事本的地方。
2.1 project_overview.md
这个文档就像你项目的"电梯演讲",要在最短时间内说清楚
-
项目是干啥的
-
要实现什么目标
-
整体架构长啥样
(电梯演讲是一种在30秒到2分钟内精准传达核心价值的简短陈述,用于快速吸引注意并促成行动,常见于商业推销、求职等场景)
例子:
# Project Overview## GoalBuild a web application that allows users to create and manage to-do lists.## Architecture- **Frontend:** React application using TypeScript.- **Backend:** Serverless API using Node.js and a cloud database.- **State Management:** Using a custom state management solution based on React hooks.## Key Features- User authentication- Create, edit, and delete to-do lists- Mark tasks as complete- Real-time synchronization
2.2 Create task_list.md
该文件是您项目的 “待办事项列表”。它应该跟踪所有任务、它们的状态、优先级和任何相关的注释。
例子:
# Task List## High Priority- [ ] Implement user authentication flow. (**Status:** In Progress, **Assigned To:** Gemini, **Notes:** Currently working on the login component.)- [ ] Design database schema. (**Status:** To Do, **Assigned To:** Claude, **Notes:** Need to consider scalability.)## Medium Priority- [ ] Create basic UI for to-do list creation. (**Status:** To Do, **Assigned To:** Gemini, **Notes:** Waiting for authentication to be completed.)## Low Priority- [ ] Add support for task categorization. (**Status:** To Do, **Assigned To:** None, **Notes:** Can be implemented later.)## Completed- [x] Set up project structure.- [x] Create basic React components for header and footer.
2.3 directory_structure.md
此文件提供项目布局的地图。您可以手动创建它或使用脚本自动生成它。
-
Example (Manual):
# Directory Structure- **components/**: Reusable UI components (e.g., Button, Input, List)- **hooks/**: Custom React hooks (e.g., useAuth, useData)- **lib/**: Utility functions and API clients- **pages/**: Top-level application pages (e.g., Home, Login, Register)- **styles/**: Global styles and theme definitions
-
Example Script (PowerShell):
# Save as update_directory.ps1$projectRoot = "."$outputFile = "./.notes/directory_structure.md"# Generate directory listingfunction Get-FormattedDirectory {param ([string]$path,[int]$indent = 0)$indentString = " " * $indent$content = ""foreach ($item in Get-ChildItem -Path $path -Force) {if ($item.PSIsContainer) {$content += "$indentString- **$($item.Name)/**`n"$content += Get-FormattedDirectory -path $item.FullName -indent ($indent + 1)} else {$content += "$indentString- $($item.Name)`n"}}return $content}# Generate content for markdown file$markdownContent = @"# Current Directory Structure## Core Components
$(Get-FormattedDirectory -path $projectRoot)
"@# Output to file$markdownContent | Out-File -FilePath $outputFile -Encoding UTF8Write-Host "Directory structure updated in $($outputFile)"
3.提示词精通与对话聚焦
现在您已通过上述创建的文档,与Claude和Gemini建立起三方同步的数字"工作间",是时候以标准模式(Normal mode)与Gemini开始使用Composer进行创作了。这里正是展现 “提示词”艺术的关键时刻。
-
Focus(聚焦导向性):上下文如同聚光灯,帮助AI聚焦代码库相关部分,自动过滤无关细节
-
Accuracy(精准锚定性): 对项目目标、当前状态和架构的共享认知,确保响应精准契合开发意图
-
Consistency(一致性维护):通过上下文约束维护项目架构完整性,避免破坏现有功能或引发逻辑矛盾
3.2 使用 @来聚焦AI
@符号是Cursor中为Gemini和Claude提供上下文的核⼼⼯具,其功能等同于对话时直接指向特定⽂档或代码区块
⽰例:
@components/Button.tsx: “Hey Gemini, let’s focus on this specific component.”@.notes/task_list.md: “Claude, check out the current task list and priorities.”@.notes/project_overview.md: “Gemini, remember the overall goals and architecture we discussed?”
3.3. 用准确性和一致性来支持AI答案
-
具体化:提出直接、具体的问题。避免模糊或含糊不清的语言。 不要问:“我如何改进这段代码?” 尝试问:“Gemini,在@components/LoginForm.tsx中,我怎样才能使handleSubmit函数更高效?”
-
使用MECE:MECE(相互排斥,集体穷举)是一种强大的解决问题的技巧。将复杂的任务分解为较小的、不重叠的子任务。 例如:“Claude,让我们使用MECE来分解用户身份验证流程的实现: 用户输入:处理用户名和密码输入。 API调用:向认证API发送凭据。 响应处理:处理API响应(成功或错误)。 状态更新:根据认证结果更新应用程序状态。你对这个分解有什么看法吗?有任何建议吗?” 迭代和优化:
-
不要期望第一次就能得到完美的结果。与AI进行迭代对话。
对其建议提供反馈,提出澄清问题,并根据其回应调整你的提示。 例如:“Gemini,你对优化handleSubmit函数的建议很好,但它没有解决潜在的竞争条件问题。我们该如何修改它以应对这种情况?”
-
询问“为什么”:鼓励AI解释它的推理过程。这有助于你理解它的思考方式并识别潜在的误解。 例如:“Claude,你为什么推荐使用这种特定的状态管理方法而不是在@.notes/project_overview.md中概述的那种方法?”
-
探索反事实情况:考虑替代方案并与AI讨论它们的优缺点。 例如:“Gemini,如果使用不同于目前在@lib/api.ts中使用的API客户端库,会有什么弊端呢?”
-
假设情景:用假设的情况挑战AI,以评估其理解和解决问题的能力。 例如:“Claude,如果认证API暂时不可用,应用程序应该如何处理这种情况以提供良好的用户体验?”
-
利用“记住当时”的提示:
回顾之前的互动或决策,以保持连续性并建立在已建立的知识基础上。
例如:“Gemini,还记得我们在讨论待办事项列表创建时需要乐观更新的需求吗?我们如何将在@components/TaskItem.tsx中的任务编辑功能应用这一概念?”
5.结论
通过将你的AI合作者视为有价值的贡献者,建立清晰的合作规则,提供专注的上下文,并有效地提出问题,你可以在大型代码库中取得显著的成功。
在过去6个月里,我已经在超过70个应用程序/项目中使用了这种方法,并且有几种情况没有出现任何差错。记住要用AI来帮助AI自己。这就是Cursor的魔力,以及将IDE作为AI扩展(即数字身体)的价值所在。

945

被折叠的 条评论
为什么被折叠?



