Skip to content

feat: add safety settings threshold configuration#130

Merged
bbbugg merged 1 commit intomainfrom
feat/safe-setting
Apr 19, 2026
Merged

feat: add safety settings threshold configuration#130
bbbugg merged 1 commit intomainfrom
feat/safe-setting

Conversation

@bbbugg
Copy link
Copy Markdown
Member

@bbbugg bbbugg commented Apr 19, 2026

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_THRESHOLD environment variable.

此拉取请求为 Gemini API 请求引入了可配置的默认安全设置阈值,允许用户通过 SAFETY_SETTINGS_THRESHOLD 环境变量控制内容审核的严格程度。

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.safetySettingsThreshold and reads SAFETY_SETTINGS_THRESHOLD from environment with validation.
  • Centralizes default Gemini safetySettings construction in FormatConverter.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.

Comment thread src/utils/ConfigLoader.js
maxContexts: 1,
maxRetries: 3,
retryDelay: 2000,
safetySettingsThreshold: "OFF",
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
safetySettingsThreshold: "OFF",
safetySettingsThreshold: "BLOCK_NONE",

Copilot uses AI. Check for mistakes.
Comment thread src/utils/ConfigLoader.js
Comment on lines +87 to +90
if (process.env.SAFETY_SETTINGS_THRESHOLD) {
const rawThreshold = String(process.env.SAFETY_SETTINGS_THRESHOLD).trim().toUpperCase();
const allowedThresholds = new Set([
"HARM_BLOCK_THRESHOLD_UNSPECIFIED",
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +140 to +147
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 },
];
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@bbbugg bbbugg merged commit 3ffa926 into main Apr 19, 2026
4 checks passed
@bbbugg bbbugg deleted the feat/safe-setting branch April 22, 2026 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[反馈]: 强制安全设置等级

2 participants