cursor-ai-module
v1.0.0
Published
Cursor AI 模块初始化代码,将 Agent Prompt 2025-09-03.txt 作为 system prompt 传递给所有模型
Maintainers
Readme
Cursor AI Module
一个强大的 Node.js 模块,用于在 Cursor 中集成多个 AI 模型,自动加载系统提示词,并提供统一的 API 接口。
✨ 特性
- 🤖 多模型支持: GPT-5、Claude Sonnet 4、Gemini 2.0 Pro 等主流模型
- 📝 智能提示词管理: 自动加载
Agent Prompt 2025-09-03.txt作为系统提示词 - ⚡ 并行初始化: 同时初始化多个 AI 模型,提高效率
- 🔧 灵活配置: 支持模型特定配置和自定义指令
- 📊 使用统计: 提供详细的模型使用统计和监控
- 🔄 热重载: 支持系统提示词和配置的动态重新加载
- 🛡️ 错误处理: 完善的错误处理和重试机制
🚀 快速开始
安装
npm install cursor-ai-module基础使用
const { initializeCursorAI, cursorAI } = require('cursor-ai-module');
async function main() {
// 初始化 AI 模块
await initializeCursorAI();
// 获取可用模型
const models = cursorAI.getAvailableModels();
console.log('可用模型:', models);
// 使用模型生成响应
const messages = [
{ role: 'user', content: '请用中文解释什么是递归函数' }
];
const response = await cursorAI.generateResponse('GPT5', messages);
console.log('AI 响应:', response);
}
main().catch(console.error);🔧 环境配置
创建 .env 文件并添加必要的 API 密钥:
# OpenAI API 配置
OPENAI_API_KEY=your_openai_api_key_here
OPENAI_BASE_URL=https://api.openai.com/v1
# Anthropic API 配置
ANTHROPIC_API_KEY=your_anthropic_api_key_here
# Google API 配置
GOOGLE_API_KEY=your_google_api_key_here
# 可选配置
LOG_LEVEL=info
MAX_RETRIES=3
TIMEOUT=30000
CURSOR_AI_PROMPT_PATH=/path/to/custom/prompts📖 API 文档
初始化
const { initializeCursorAI, cursorAI } = require('cursor-ai-module');
// 初始化所有模型
await initializeCursorAI();
// 获取可用模型列表
const models = cursorAI.getAvailableModels();生成响应
// 基础使用
const response = await cursorAI.generateResponse('GPT5', messages);
// 高级选项
const response = await cursorAI.generateResponse('CLAUDE_SONNET', messages, {
maxTokens: 1000,
temperature: 0.7
});配置管理
const { cursorAIConfig } = require('cursor-ai-module');
// 获取模型配置
const config = cursorAIConfig.getModelConfig('GPT5');
// 更新配置
cursorAIConfig.updateModelConfig('GPT5', {
temperature: 0.2,
customInstructions: ['始终使用中文简体回复']
});
// 验证环境变量
const validation = cursorAIConfig.validateEnvironment();统计和监控
// 获取使用统计
const stats = cursorAI.getModelStats();
// 重新加载系统提示词
const reloaded = cursorAI.reloadSystemPrompt();🎯 支持的模型
| 模型 | 提供商 | 模型ID | 最大Token | 特点 | |------|--------|--------|-----------|------| | GPT-5 | OpenAI | gpt-5 | 128,000 | 最新最强模型 | | Claude Sonnet 4 | Anthropic | claude-3-5-sonnet-20241022 | 200,000 | 代码理解能力强 | | Gemini 2.0 Pro | Google | gemini-2.0-pro-exp | 1,000,000 | 上下文窗口最大 | | GPT-4o | OpenAI | gpt-4o | 128,000 | 平衡性能和成本 |
🔄 系统提示词管理
系统会自动按以下优先级加载提示词:
- 包内提示词: npm 包中的
Agent Prompt 2025-09-03.txt - 项目提示词: 项目根目录的提示词文件
- 自定义路径: 通过
CURSOR_AI_PROMPT_PATH环境变量指定 - 用户配置:
~/.cursor-ai/目录下的提示词文件 - 默认模板: 内置的默认提示词模板
自定义提示词
// 通过环境变量指定自定义路径
process.env.CURSOR_AI_PROMPT_PATH = '/path/to/your/prompts';
// 或者在用户主目录创建配置
mkdir ~/.cursor-ai
cp your-prompt.txt ~/.cursor-ai/Agent\ Prompt\ 2025-09-03.txt📊 高级功能
并行处理
// 同时使用多个模型
const promises = [
cursorAI.generateResponse('GPT5', messages),
cursorAI.generateResponse('CLAUDE_SONNET', messages),
cursorAI.generateResponse('GEMINI_PRO', messages)
];
const responses = await Promise.all(promises);错误处理
try {
const response = await cursorAI.generateResponse('GPT5', messages);
} catch (error) {
if (error.message.includes('API key')) {
console.error('API 密钥配置错误');
} else if (error.message.includes('rate limit')) {
console.error('API 速率限制,请稍后重试');
} else {
console.error('未知错误:', error.message);
}
}🛠️ 开发指南
添加新模型
- 在配置中添加模型信息
- 实现对应的初始化逻辑
- 更新 API 接口
自定义配置
// 添加新模型配置
cursorAIConfig.addModelConfig('NEW_MODEL', {
name: 'New Model',
provider: 'custom',
modelId: 'new-model-id',
maxTokens: 50000,
temperature: 0.1
});📝 注意事项
- API 密钥安全: 确保 API 密钥安全存储,不要提交到版本控制
- 速率限制: 注意各提供商的 API 速率限制
- 成本控制: 监控 API 使用量,避免意外费用
- 错误处理: 在生产环境中添加适当的错误处理和重试机制
🤝 贡献
欢迎提交 Issue 和 Pull Request 来改进这个项目!
- Fork 这个仓库
- 创建你的特性分支 (
git checkout -b feature/AmazingFeature) - 提交你的更改 (
git commit -m 'Add some AmazingFeature') - 推送到分支 (
git push origin feature/AmazingFeature) - 打开一个 Pull Request
📄 许可证
MIT License - 详见 LICENSE 文件
