feat: add safety settings threshold configuration#130
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a configurable default safety settings threshold for Gemini requests, allowing deployments to control content moderation strictness via an environment variable.
Changes:
- Introduces
config.safetySettingsThresholdand readsSAFETY_SETTINGS_THRESHOLDfrom environment with validation. - Centralizes default Gemini
safetySettingsconstruction inFormatConverter.getDefaultSafetySettings(). - Updates docs (
README.md,README_EN.md,.env.example) to describe the new env var.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/utils/ConfigLoader.js | Adds config default + env var parsing/validation + config logging for safety threshold. |
| src/core/RequestHandler.js | Uses FormatConverter.getDefaultSafetySettings() for native Google requests when safetySettings is absent. |
| src/core/FormatConverter.js | Adds getDefaultSafetySettings() and uses it when finalizing adapter-generated Gemini requests. |
| README_EN.md | Documents SAFETY_SETTINGS_THRESHOLD in the env var table. |
| README.md | Documents SAFETY_SETTINGS_THRESHOLD in the env var table (Chinese). |
| .env.example | Adds example + supported values for SAFETY_SETTINGS_THRESHOLD. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| maxContexts: 1, | ||
| maxRetries: 3, | ||
| retryDelay: 2000, | ||
| safetySettingsThreshold: "OFF", |
There was a problem hiding this comment.
The default safety threshold is now "OFF" (via config default + docs), whereas the previous behavior hard-coded "BLOCK_NONE" for all categories. This is a backward-incompatible default change and may alter moderation behavior (or even cause request failures if "OFF" isn't accepted by the upstream API). Consider keeping the default at the prior value ("BLOCK_NONE") and making the env var override optional; if you want an 'OFF' mode, handle it explicitly (e.g., by omitting safetySettings injection entirely).
| safetySettingsThreshold: "OFF", | |
| safetySettingsThreshold: "BLOCK_NONE", |
| if (process.env.SAFETY_SETTINGS_THRESHOLD) { | ||
| const rawThreshold = String(process.env.SAFETY_SETTINGS_THRESHOLD).trim().toUpperCase(); | ||
| const allowedThresholds = new Set([ | ||
| "HARM_BLOCK_THRESHOLD_UNSPECIFIED", |
There was a problem hiding this comment.
This PR is marked as fixing #100, but the issue request specifically asks for a new config option named FORCE_BLOCK_LEVEL. Right now only SAFETY_SETTINGS_THRESHOLD is supported. To avoid leaving the issue partially addressed (and to preserve backward/forward compatibility with users following the issue text), consider supporting FORCE_BLOCK_LEVEL as an alias or updating the issue/PR description to reflect the chosen final name.
| getDefaultSafetySettings() { | ||
| const threshold = this.serverSystem?.config?.safetySettingsThreshold || "OFF"; | ||
| return [ | ||
| { category: "HARM_CATEGORY_HARASSMENT", threshold }, | ||
| { category: "HARM_CATEGORY_HATE_SPEECH", threshold }, | ||
| { category: "HARM_CATEGORY_SEXUALLY_EXPLICIT", threshold }, | ||
| { category: "HARM_CATEGORY_DANGEROUS_CONTENT", threshold }, | ||
| ]; |
There was a problem hiding this comment.
getDefaultSafetySettings() treats "OFF" as a literal threshold value and will send it to Gemini for each harm category. If the intent of "OFF" is to disable safety settings (i.e., let the API default apply), this should instead return null / undefined (and callers should skip setting safetySettings) or map "OFF" to a documented threshold value. Otherwise, clarify in docs that "OFF" is forwarded verbatim.
This pull request introduces a configurable default safety settings threshold for Gemini API requests, allowing users to control the strictness of content moderation via the
SAFETY_SETTINGS_THRESHOLDenvironment variable.此拉取请求为 Gemini API 请求引入了可配置的默认安全设置阈值,允许用户通过
SAFETY_SETTINGS_THRESHOLD环境变量控制内容审核的严格程度。