Skip to content

5.1b 配置进阶

💡 一句话总结:深入理解高级配置项,优化 OpenCode 的性能和功能。


学完你能做什么

  • 掌握高级配置项的用法
  • 能进行性能优化配置
  • 能配置多模型切换
  • 能自定义扩展功能

🎒 开始前的准备

确保你已经完成以下事项:


核心思路

高级 API 配置

多模型配置

json
{
  "api": {
    "defaultProvider": "zhipu",
    "providers": {
      "zhipu": {
        "model": "glm-4.7",
        "apiKey": "${ZHIPU_API_KEY}",
        "priority": 1
      },
      "deepseek": {
        "model": "deepseek-chat",
        "apiKey": "${DEEPSEEK_API_KEY}",
        "priority": 2
      },
      "claude": {
        "model": "claude-3-5-sonnet-20241022",
        "apiKey": "${CLAUDE_API_KEY}",
        "priority": 3
      }
    },
    "fallback": {
      "enabled": true,
      "maxRetries": 2,
      "delay": 1000
    }
  }
}

模型参数调优

json
{
  "api": {
    "parameters": {
      "temperature": 0.7,
      "topP": 0.9,
      "frequencyPenalty": 0.0,
      "presencePenalty": 0.0,
      "maxOutputTokens": 4096
    },
    "streaming": {
      "enabled": true,
      "chunkSize": 64
    }
  }
}

性能优化配置

json
{
  "performance": {
    "cache": {
      "enabled": true,
      "maxSize": "100MB",
      "ttl": 3600,
      "directory": "~/.opencode/cache"
    },
    "preload": {
      "enabled": true,
      "extensions": ["typescript", "python", "javascript"],
      "strategy": "on-demand"
    },
    "memory": {
      "maxHeap": "4GB",
      "garbageCollection": true,
      "interval": 60000
    },
    "parallel": {
      "enabled": true,
      "maxWorkers": 4,
      "taskTimeout": 30000
    }
  }
}

高级功能配置

会话管理

json
{
  "sessions": {
    "autoSave": true,
    "autoSaveInterval": 30000,
    "maxSessions": 50,
    "history": {
      "enabled": true,
      "maxEntries": 1000,
      "expireAfter": "30d"
    },
    "encryption": {
      "enabled": true,
      "algorithm": "aes-256-gcm"
    }
  }
}

网络和代理

json
{
  "network": {
    "proxy": {
      "enabled": false,
      "url": "http://proxy.example.com:8080",
      "bypass": ["localhost", "127.0.0.1"]
    },
    "ssl": {
      "verify": true,
      "caFile": "/path/to/ca.crt",
      "rejectUnauthorized": true
    },
    "rateLimit": {
      "enabled": true,
      "requestsPerMinute": 60,
      "burstSize": 10
    }
  }
}

开发工具配置

LSP 服务器配置

json
{
  "lsp": {
    "typescript": {
      "enabled": true,
      "path": "node_modules/typescript/lib",
      "diagnostics": {
        "enable": true,
        "trigger": "change"
      }
    },
    "python": {
      "enabled": true,
      "server": "pylance",
      "path": "/usr/local/lib/python3.11/site-packages"
    },
    "general": {
      "trace": {
        "server": "off",
        "communication": "off"
      }
    }
  }
}

格式化工具

json
{
  "formatters": {
    "prettier": {
      "enabled": true,
      "path": "node_modules/.bin/prettier",
      "config": {
        "semi": true,
        "singleQuote": false,
        "tabWidth": 2
      }
    },
    "black": {
      "enabled": true,
      "path": "~/.local/bin/black",
      "config": {
        "line-length": 88
      }
    },
    "onSave": true,
    "formatOnSaveTimeout": 5000
  }
}

跟我做

实战 1:配置多模型自动切换

目标:当主模型失败时自动切换到备用模型

  1. 编辑配置文件:
json
{
  "api": {
    "defaultProvider": "zhipu",
    "providers": {
      "zhipu": {
        "model": "glm-4.7",
        "apiKey": "${ZHIPU_API_KEY}",
        "priority": 1
      },
      "deepseek": {
        "model": "deepseek-chat",
        "apiKey": "${DEEPSEEK_API_KEY}",
        "priority": 2
      }
    },
    "fallback": {
      "enabled": true,
      "maxRetries": 2,
      "delay": 1000
    }
  }
}
  1. 测试故障切换:
bash
# 临时修改 API Key 使其失效
export ZHIPU_API_KEY="invalid-key"

# 发送请求,观察是否自动切换

你应该看到:主模型失败后自动切换到备用模型


实战 2:配置性能优化

目标:提升 OpenCode 的响应速度和性能

  1. 编辑配置文件:
json
{
  "performance": {
    "cache": {
      "enabled": true,
      "maxSize": "200MB",
      "ttl": 7200
    },
    "preload": {
      "enabled": true,
      "strategy": "aggressive"
    },
    "parallel": {
      "enabled": true,
      "maxWorkers": 8
    }
  }
}
  1. 监控性能改进

你应该看到

  • 重复请求响应更快
  • 代码补全更及时
  • 整体操作更流畅

📋 完整高级配置示例

json
{
  "$schema": "https://opencode.ai/schema.json",
  "version": "1.0",

  "api": {
    "defaultProvider": "zhipu",
    "providers": {
      "zhipu": {
        "model": "glm-4.7",
        "apiKey": "${ZHIPU_API_KEY}",
        "priority": 1,
        "weight": 0.6
      },
      "deepseek": {
        "model": "deepseek-chat",
        "apiKey": "${DEEPSEEK_API_KEY}",
        "priority": 2,
        "weight": 0.4
      }
    },
    "parameters": {
      "temperature": 0.7,
      "topP": 0.9,
      "maxOutputTokens": 4096
    },
    "fallback": {
      "enabled": true,
      "maxRetries": 2,
      "delay": 1000,
      "circuitBreaker": {
        "failureThreshold": 5,
        "resetTimeout": 60000
      }
    },
    "streaming": {
      "enabled": true,
      "chunkSize": 64
    }
  },

  "performance": {
    "cache": {
      "enabled": true,
      "maxSize": "200MB",
      "ttl": 7200,
      "directory": "~/.opencode/cache"
    },
    "preload": {
      "enabled": true,
      "extensions": ["typescript", "python", "javascript", "rust"],
      "strategy": "aggressive"
    },
    "parallel": {
      "enabled": true,
      "maxWorkers": 8,
      "taskTimeout": 30000
    },
    "memory": {
      "maxHeap": "4GB",
      "garbageCollection": true,
      "interval": 60000
    }
  },

  "sessions": {
    "autoSave": true,
    "autoSaveInterval": 30000,
    "maxSessions": 100,
    "history": {
      "enabled": true,
      "maxEntries": 5000,
      "expireAfter": "90d"
    },
    "encryption": {
      "enabled": true,
      "algorithm": "aes-256-gcm"
    }
  },

  "network": {
    "proxy": {
      "enabled": false,
      "url": "http://proxy.example.com:8080"
    },
    "rateLimit": {
      "enabled": true,
      "requestsPerMinute": 120,
      "burstSize": 20
    }
  },

  "lsp": {
    "enabled": true,
    "trace": {
      "server": "off"
    },
    "autoDownload": true
  },

  "formatters": {
    "enabled": true,
    "onSave": true,
    "default": "prettier"
  },

  "logging": {
    "level": "info",
    "directory": "~/.opencode/logs",
    "maxFiles": 10,
    "maxSize": "10MB"
  },

  "telemetry": {
    "enabled": true,
    "anonymous": true,
    "errors": true,
    "performance": true
  }
}

检查点 ✅

全部通过才能继续

  • [ ] 理解多模型配置
  • [ ] 能进行性能优化配置
  • [ ] 了解高级功能配置

本课小结

你学会了:

  1. 多模型自动切换配置
  2. 性能优化配置
  3. 会话管理高级配置
  4. 网络和代理配置
  5. LSP 和格式化工具配置

下一课预告

下一课我们将学习 Agent 快速入门,了解如何创建自定义 Agent。


📚 更多完整模板Prompt 模板库