理解配置
ByteBuddy 的配置系统是整个工具的核心,理解配置文件的结构和选项对于充分利用 ByteBuddy 的功能至关重要。
配置文件概述
配置文件位置
项目级别:
.bytebuddy/
config.json # 主配置文件
rules.json # 规则配置
prompts.json # 提示词配置
models.json # 模型配置
用户级别:
~/.bytebuddy/
config.json # 用户默认配置
history.json # 使用历史
cache/ # 缓存目录
系统级别:
/etc/bytebuddy/
default.json # 系统默认配置
policies.json # 策略配置配置文件层次结构
系统默认 < 用户配置 < 项目配置 < 环境变量 < 命令行参数主配置文件结构
基础配置
json
{
"version": "2.0.0",
"name": "my-project",
"description": "项目配置描述",
"models": {
"default": "gpt-4",
"providers": {
"openai": {
"apiKey": "${OPENAI_API_KEY}",
"baseURL": "https://api.openai.com/v1",
"models": {
"chat": ["gpt-4", "gpt-3.5-turbo"],
"completion": ["text-davinci-003"]
}
}
}
},
"context": {
"include": ["src/**/*", "docs/**/*"],
"exclude": ["node_modules/**", "dist/**"],
"maxFiles": 100,
"maxFileSize": "1MB"
},
"rules": ["react-best-practices", "typescript-strict"],
"tools": ["file-manager", "code-generator"],
"plugins": ["@bytebuddy/plugin-react"]
}完整配置示例
json
{
"version": "2.0.0",
"name": "advanced-project",
"description": "高级项目配置示例",
"models": {
"default": "gpt-4",
"fallback": "gpt-3.5-turbo",
"providers": {
"openai": {
"apiKey": "${OPENAI_API_KEY}",
"baseURL": "https://api.openai.com/v1",
"organization": "org-your-org",
"timeout": 30000,
"maxRetries": 3,
"models": {
"chat": ["gpt-4", "gpt-3.5-turbo"],
"completion": ["text-davinci-003"],
"embedding": ["text-embedding-ada-002"],
"image": ["dall-e-3"]
},
"rateLimit": {
"requestsPerMinute": 60,
"tokensPerMinute": 90000
}
},
"anthropic": {
"apiKey": "${ANTHROPIC_API_KEY}",
"baseURL": "https://api.anthropic.com",
"models": {
"chat": ["claude-3-opus", "claude-3-sonnet"]
}
},
"local": {
"type": "ollama",
"baseURL": "http://localhost:11434",
"models": {
"chat": ["llama2", "mistral", "codellama"]
}
}
},
"selection": {
"strategy": "cost-optimized",
"rules": [
{
"condition": "task.type === 'code-generation'",
"model": "gpt-4"
},
{
"condition": "task.type === 'simple-question'",
"model": "gpt-3.5-turbo"
},
{
"condition": "task.privacy === 'high'",
"model": "local"
}
]
}
},
"context": {
"scope": "project",
"include": ["src/**/*", "docs/**/*", "tests/**/*", "*.md", "*.json"],
"exclude": [
"node_modules/**/*",
"dist/**/*",
".git/**/*",
"*.log",
".DS_Store"
],
"maxFiles": 500,
"maxFileSize": "2MB",
"respectGitignore": true,
"followSymlinks": false,
"indexing": {
"enabled": true,
"interval": "daily",
"deep": true
}
},
"rules": {
"enabled": [
"react-best-practices",
"typescript-strict",
"accessibility-a11y",
"security-owasp"
],
"custom": [
{
"name": "company-naming-convention",
"type": "style",
"pattern": "^[A-Z][a-zA-Z0-9]*$",
"message": "必须使用 PascalCase 命名",
"severity": "warning",
"fileTypes": ["tsx", "ts"]
},
{
"name": "no-hardcoded-secrets",
"type": "security",
"pattern": "(password|secret|key|token)\\s*[:=]\\s*['\"][^'\"]+['\"]",
"message": "检测到硬编码的敏感信息",
"severity": "error",
"autoFix": true
}
],
"severity": {
"error": "block",
"warning": "warn",
"info": "suggest"
}
},
"tools": {
"fileManager": {
"enabled": true,
"maxFileSize": "10MB",
"allowedExtensions": [".js", ".ts", ".jsx", ".tsx", ".json", ".md"],
"operations": ["read", "write", "delete", "move", "copy"]
},
"codeGenerator": {
"enabled": true,
"templates": "./templates",
"outputDirectory": "./src/generated",
"variables": {
"author": "Your Name",
"license": "MIT",
"year": 2024
},
"generators": {
"component": {
"template": "react-component.tsx.hbs",
"testTemplate": "react-component.test.tsx.hbs",
"storyTemplate": "react-component.stories.tsx.hbs"
}
}
},
"database": {
"enabled": false,
"connection": {
"type": "postgresql",
"host": "localhost",
"port": 5432,
"database": "myapp",
"username": "${DB_USER}",
"password": "${DB_PASSWORD}"
}
}
},
"plugins": {
"enabled": [
"@bytebuddy/plugin-react",
"@bytebuddy/plugin-typescript",
"@bytebuddy/plugin-eslint"
],
"config": {
"@bytebuddy/plugin-react": {
"componentTemplate": "functional",
"styledComponents": true,
"propTypes": true
},
"@bytebuddy/plugin-typescript": {
"strictMode": true,
"target": "ES2020",
"module": "ESNext"
}
}
},
"ui": {
"theme": "dark",
"fontSize": 14,
"fontFamily": "Fira Code",
"sidebar": {
"width": 300,
"collapsible": true
},
"editor": {
"tabSize": 2,
"wordWrap": true,
"lineNumbers": true,
"minimap": false
}
},
"performance": {
"cache": {
"enabled": true,
"maxSize": "1GB",
"ttl": 86400,
"directory": "./.bytebuddy/cache"
},
"concurrency": {
"maxConcurrentRequests": 5,
"maxConcurrentGenerations": 2,
"queueSize": 100
},
"optimization": {
"lazyLoading": true,
"codeSplitting": true,
"compression": true
}
},
"security": {
"encryption": {
"enabled": false,
"algorithm": "AES-256-GCM",
"keyDerivation": "PBKDF2"
},
"privacy": {
"anonymousUsage": false,
"shareCode": true,
"shareErrors": false,
"dataRetention": "30d"
},
"accessControl": {
"enabled": false,
"roles": {
"admin": ["read", "write", "delete", "configure"],
"user": ["read", "write"],
"guest": ["read"]
}
}
},
"logging": {
"level": "info",
"format": "json",
"outputs": [
{
"type": "console",
"colorize": true
},
{
"type": "file",
"path": "./logs/bytebuddy.log",
"rotation": "daily",
"maxSize": "100MB"
}
],
"categories": {
"http": "debug",
"ai": "info",
"plugin": "warn"
}
},
"workflows": {
"pre-commit": ["lint", "test", "security-scan"],
"pre-push": ["build", "integration-test", "coverage-check"],
"on-pr": ["code-review", "vulnerability-scan"]
},
"hooks": {
"beforeGenerate": "npm run lint",
"afterGenerate": "npm run format",
"beforeSave": "validate-syntax",
"onError": "send-notification"
}
}配置选项详解
Models 配置
json
{
"models": {
"default": "gpt-4", // 默认模型
"fallback": "gpt-3.5-turbo", // 备用模型
"providers": {
// 模型提供商配置
"openai": {
"apiKey": "${OPENAI_API_KEY}", // 环境变量
"baseURL": "https://api.openai.com/v1",
"timeout": 30000, // 超时时间(毫秒)
"maxRetries": 3, // 重试次数
"rateLimit": {
// 速率限制
"requestsPerMinute": 60,
"tokensPerMinute": 90000
}
}
},
"selection": {
// 模型选择策略
"strategy": "cost-optimized", // 策略类型
"rules": [
// 选择规则
{
"condition": "task.type === 'code-generation'",
"model": "gpt-4"
}
]
}
}
}Context 配置
json
{
"context": {
"scope": "project", // 上下文范围
"include": [
// 包含的文件模式
"src/**/*",
"docs/**/*"
],
"exclude": [
// 排除的文件模式
"node_modules/**/*",
"dist/**/*"
],
"maxFiles": 100, // 最大文件数
"maxFileSize": "1MB", // 最大文件大小
"respectGitignore": true, // 遵守 .gitignore
"followSymlinks": false, // 跟随符号链接
"indexing": {
// 索引配置
"enabled": true,
"interval": "daily", // 索引间隔
"deep": true // 深度索引
}
}
}Rules 配置
json
{
"rules": {
"enabled": [
// 启用的规则
"react-best-practices",
"typescript-strict"
],
"custom": [
// 自定义规则
{
"name": "company-naming-convention",
"type": "style",
"pattern": "^[A-Z][a-zA-Z0-9]*$",
"message": "必须使用 PascalCase 命名",
"severity": "warning",
"fileTypes": ["tsx", "ts"]
}
],
"severity": {
// 严重程度配置
"error": "block", // 阻止执行
"warning": "warn", // 警告
"info": "suggest" // 建议
}
}
}配置验证
JSON Schema 验证
json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"version": {
"type": "string",
"pattern": "^\\d+\\.\\d+\\.\\d+$"
},
"models": {
"type": "object",
"properties": {
"default": { "type": "string" },
"providers": { "type": "object" }
},
"required": ["default"]
}
},
"required": ["version", "models"]
}配置检查命令
bash
# 验证配置文件
bytebuddy config validate
# 检查特定配置
bytebuddy config check --section=models
# 显示当前配置
bytebuddy config show
# 测试配置
bytebuddy config test环境变量
支持的环境变量
bash
# API 密钥
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
# 模型配置
BYTEBUDDY_DEFAULT_MODEL=gpt-4
BYTEBUDDY_MAX_TOKENS=4096
BYTEBUDDY_TEMPERATURE=0.7
# 网络配置
HTTP_PROXY=http://proxy.example.com:8080
HTTPS_PROXY=http://proxy.example.com:8080
NO_PROXY=localhost,127.0.0.1
# 文件路径
BYTEBUDDY_CONFIG_PATH=/path/to/config.json
BYTEBUDDY_CACHE_PATH=/path/to/cache
BYTEBUDDY_LOG_PATH=/path/to/logs
# 性能配置
BYTEBUDDY_MAX_CONCURRENT=5
BYTEBUDDY_TIMEOUT=30000
BYTEBUDDY_MEMORY_LIMIT=4GB环境特定配置
json
{
"environments": {
"development": {
"models": {
"default": "gpt-3.5-turbo",
"temperature": 0.8
},
"logging": {
"level": "debug"
}
},
"production": {
"models": {
"default": "gpt-4",
"temperature": 0.3
},
"logging": {
"level": "warn"
},
"security": {
"encryption": {
"enabled": true
}
}
},
"testing": {
"models": {
"default": "mock-model"
},
"cache": {
"enabled": false
}
}
}
}配置最佳实践
结构化配置
json
{
"metadata": {
"name": "project-config",
"version": "1.0.0",
"description": "项目配置描述",
"author": "Your Name",
"created": "2024-01-01T00:00:00Z",
"updated": "2024-01-02T00:00:00Z"
},
"bytebuddy": {
// 实际配置内容
}
}模块化配置
bash
# 主配置文件
.bytebuddy/
├── config.json # 主配置
├── models.json # 模型配置
├── rules.json # 规则配置
├── prompts.json # 提示词配置
├── workflows.json # 工作流配置
└── environments/ # 环境配置
├── development.json
├── staging.json
└── production.json版本控制策略
json
{
"configManagement": {
"versioning": {
"strategy": "semantic",
"autoIncrement": true
},
"backup": {
"enabled": true,
"retention": 30,
"compress": true
},
"rollback": {
"enabled": true,
"maxVersions": 10
}
}
}配置迁移
从 YAML 迁移到 JSON
yaml
# 旧格式 (YAML)
models:
default: gpt-4
providers:
openai:
apiKey: "${OPENAI_API_KEY}"json
// 新格式 (JSON)
{
"models": {
"default": "gpt-4",
"providers": {
"openai": {
"apiKey": "${OPENAI_API_KEY}"
}
}
}
}自动迁移工具
bash
# 检查配置版本
bytebuddy config check-version
# 迁移配置
bytebuddy config migrate --from-version=1.0 --to-version=2.0
# 验证迁移结果
bytebuddy config validate故障排除
常见配置错误
json
{
"errors": [
{
"type": "validation_error",
"message": "缺少必需的 'models.default' 字段",
"path": "config.json",
"line": 5,
"suggestion": "添加 models.default 字段"
},
{
"type": "syntax_error",
"message": "JSON 语法错误:意外的标记",
"path": "rules.json",
"line": 12,
"suggestion": "检查 JSON 语法"
}
]
}调试配置
bash
# 启用调试模式
BYTEBUDDY_DEBUG=1 bytebuddy --config=./config.json
# 查看配置解析过程
bytebuddy config debug --show-steps
# 测试特定配置
bytebuddy config test --section=models高级配置技巧
条件配置
json
{
"conditionalConfig": {
"if": {
"env": "NODE_ENV",
"equals": "production"
},
"then": {
"models": {
"default": "gpt-4"
}
},
"else": {
"models": {
"default": "gpt-3.5-turbo"
}
}
}
}动态配置
javascript
// dynamic-config.js
module.exports = {
generateConfig: function (env, context) {
return {
models: {
default: env.NODE_ENV === "production" ? "gpt-4" : "gpt-3.5-turbo",
},
context: {
include: context.isFileBased ? ["src/**/*"] : [],
},
};
},
};通过深入理解 ByteBuddy 的配置系统,您可以充分发挥工具的潜力,根据项目需求定制最适合的 AI 辅助开发环境。