开发设置
本指南帮助您设置用于为飞码扣做出贡献的开发环境。
- Node.js >= 18.x
- npm >= 9.x
- Git
- VS Code >= 1.85.0
VS Code 扩展
Section titled “VS Code 扩展”安装这些 VS Code 扩展用于开发:
- ESLint
- TypeScript
- Prettier(可选)
# 克隆仓库git clone https://github.com/feimacode/feima-copilot-llms-extension.gitcd feima-copilot-llms-extension# 检查当前分支(应该是 main)git branch
# 对于功能开发,创建新分支git checkout -b feature/your-feature-name# 安装所有依赖npm install
# 验证安装npm list --depth=0feima-copilot-llms-extension/├── src/ # 源代码│ ├── extension.ts # 扩展入口点│ ├── auth/ # 认证服务│ ├── models/ # 语言模型提供器│ ├── services/ # API 客户端│ └── commands/ # VS Code 命令├── docs/ # 文档(Astro Starlight)├── build/ # 构建脚本├── test/ # 测试文件├── dist/ # 编译输出(不在 git 中)├── docs-dist/ # 文档构建(不在 git 中)├── package.json # 扩展清单├── tsconfig.json # TypeScript 配置├── .eslintrc.js # ESLint 配置└── .vscode/ # VS Code 设置编译 TypeScript
Section titled “编译 TypeScript”# 编译 TypeScriptnpm run compile
# 或使用监视模式进行开发npm run watch编译输出将在 dist/ 目录中。
# 检查 dist/ 目录是否存在ls -la dist/
# 应该看到:# extension.js# extension.js.map# 运行所有测试npm test
# 运行测试并生成覆盖率报告npm run test:coverage
# 运行特定测试文件npm test -- src/auth/__tests__/oauthService.test.ts在开发中运行扩展
Section titled “在开发中运行扩展”启动扩展开发主机
Section titled “启动扩展开发主机”- 在 VS Code 中打开项目
- 按
F5或转到 运行 → 开始调试 - 将打开一个新的 VS Code 窗口(扩展开发主机)
扩展可以在扩展开发主机中调试:
- 在 TypeScript 代码中设置断点
- 按
F5开始调试 - 在新窗口中调试扩展
- 在扩展开发主机中,转到 查看 → 输出
- 从下拉列表中选择 “飞码”
- 查看扩展日志
代码风格和检查
Section titled “代码风格和检查”ESLint
Section titled “ESLint”# 运行 ESLintnpm run lint
# 自动修复检查问题npm run lint:fixPrettier
Section titled “Prettier”# 格式化所有文件npm run format
# 检查格式npm run format:check项目使用提交前钩子:
# 安装 husky(如果尚未安装)npm install --save-dev huskynpx husky install
# 添加提交前钩子npx husky add .husky/pre-commit "npm run lint && npm run test"创建 .env 文件用于本地开发(不提交):
# Feima API 配置FEIMA_API_ENDPOINT=https://api.feimacode.cnFEIMA_IDP_ENDPOINT=https://idp.feimacode.cn
# OAuth 配置OAUTH_CLIENT_ID=vscode-feima-clientOAUTH_REDIRECT_URI=vscode://feima.cn-model-for-copilot/oauth/callback
# 调试设置DEBUG=feima:*LOG_LEVEL=debugTypeScript 配置
Section titled “TypeScript 配置”tsconfig.json 配置了:
- 严格模式已启用
- 目标:ES2022
- 模块:CommonJS
- 源映射已启用
常见开发任务
Section titled “常见开发任务”- 在
src/commands/中创建命令:
import * as vscode from 'vscode';
export function registerMyCommand(context: vscode.ExtensionContext) { const command = vscode.commands.registerCommand('feima.myCommand', async () => { // 您的命令逻辑 vscode.window.showInformationMessage('来自我的命令的问候!'); });
context.subscriptions.push(command);}- 在
src/extension.ts中注册命令:
import { registerMyCommand } from './commands/myCommand';
export function activate(context: vscode.ExtensionContext) { // ... 其他代码 registerMyCommand(context);}- 添加到
package.json:
{ "contributes": { "commands": [ { "command": "feima.myCommand", "title": "我的命令", "category": "Feima" } ] }}添加新语言模型
Section titled “添加新语言模型”- 将模型添加到
src/models/:
import { LanguageModel } from './types';
export const newModel: LanguageModel = { id: 'new-model-id', name: '新模型', description: '新模型的描述', provider: '提供商名称', contextLength: 32000, maxOutputTokens: 4000, capabilities: { codeGeneration: true, codeReview: true, reasoning: true }};- 在
src/models/languageModelProvider.ts中注册模型:
import { newModel } from './newModel';
// 添加到模型列表private models: LanguageModel[] = [ // ... 现有模型 newModel];- 在
src/**/__tests__/中创建测试文件:
import { myFunction } from '../myFeature';
describe('myFunction', () => { it('应该返回预期值', () => { const result = myFunction('input'); expect(result).toBe('预期输出'); });});- 运行测试:
npm test文档使用 Astro Starlight 构建:
安装文档依赖
Section titled “安装文档依赖”cd docsnpm install运行文档开发服务器
Section titled “运行文档开发服务器”cd docsnpm run dev访问 http://localhost:4321 查看文档。
cd docsnpm run build输出将在 docs-dist/ 中。
Git 工作流程
Section titled “Git 工作流程”feature/feature-name- 新功能fix/bug-description- 错误修复docs/documentation-update- 文档更新refactor/refactor-description- 重构
使用约定式提交:
feat: 添加新功能fix: 解决认证错误docs: 更新安装指南test: 为认证服务添加测试refactor: 简化令牌刷新逻辑拉取请求流程
Section titled “拉取请求流程”- Fork 仓库
- 创建功能分支
- 进行更改
- 运行测试和检查
- 提交您的更改
- 推送到您的 fork
- 创建拉取请求
问题:TypeScript 编译失败
解决方案:
# 清理构建产物npm run cleanrm -rf dist/
# 重新安装依赖rm -rf node_modules package-lock.jsonnpm install
# 重新构建npm run compile问题:扩展在扩展开发主机中未激活
解决方案:
- 检查输出面板 → “扩展主机” 中的错误
- 验证
package.jsonactivationEvents正确 - 检查
src/extension.ts是否有正确的activate()函数
问题:无法在开发中认证
解决方案:
- 验证
.env文件已配置 - 检查 feima-idp 是否可访问
- 查看输出面板 → “飞码” 获取详细日志