From 5da74802dd16f59f5bfa82e3a927ad8d3656b501 Mon Sep 17 00:00:00 2001 From: ecokayiza Date: Wed, 16 Sep 2026 15:13:42 +0800 Subject: [PATCH 1/7] Add custom API request body setting Expose a JSON textarea under Advanced > API Params that merges user-provided fields into the OpenAI-compatible, Azure OpenAI and Anthropic request bodies, so parameters the extension does not surface (thinking, reasoning_effort) can be sent. The stream key stays under extension control to keep SSE replies intact. --- src/_locales/en/main.json | 3 + src/_locales/zh-hans/main.json | 3 + src/_locales/zh-hant/main.json | 3 + src/config/index.mjs | 1 + src/popup/sections/AdvancedPart.jsx | 18 ++++ src/services/apis/azure-openai-api.mjs | 2 + src/services/apis/claude-api.mjs | 3 + src/services/apis/extra-body-params.mjs | 29 ++++++ src/services/apis/openai-compatible-core.mjs | 3 + .../unit/services/extra-body-params.test.mjs | 32 +++++++ .../unit/services/extra-body-request.test.mjs | 92 +++++++++++++++++++ 11 files changed, 189 insertions(+) create mode 100644 src/services/apis/extra-body-params.mjs create mode 100644 tests/unit/services/extra-body-params.test.mjs create mode 100644 tests/unit/services/extra-body-request.test.mjs diff --git a/src/_locales/en/main.json b/src/_locales/en/main.json index 445148a56..0ec302b3c 100644 --- a/src/_locales/en/main.json +++ b/src/_locales/en/main.json @@ -124,6 +124,9 @@ "Override provider temperature": "Override provider temperature", "The temperature parameter is not sent. The provider or model default is used.": "The temperature parameter is not sent. The provider or model default is used.", "The current model does not accept a custom temperature. The parameter will not be sent.": "The current model does not accept a custom temperature. The parameter will not be sent.", + "Extra Request Body (JSON)": "Extra Request Body (JSON)", + "Merged into the API request body. Must be a JSON object, other values are ignored.": "Merged into the API request body. Must be a JSON object, other values are ignored.", + "Invalid JSON object, this value is ignored.": "Invalid JSON object, this value is ignored.", "API Url": "API Url", "Provider": "Provider", "Others": "Others", diff --git a/src/_locales/zh-hans/main.json b/src/_locales/zh-hans/main.json index cfad6d2d1..3748a96ba 100644 --- a/src/_locales/zh-hans/main.json +++ b/src/_locales/zh-hans/main.json @@ -118,6 +118,9 @@ "Override provider temperature": "覆盖提供商的温度参数", "The temperature parameter is not sent. The provider or model default is used.": "不会发送温度参数,将使用提供商或模型的默认值。", "The current model does not accept a custom temperature. The parameter will not be sent.": "当前模型不接受自定义温度参数,因此不会发送该参数。", + "Extra Request Body (JSON)": "额外请求参数 (JSON)", + "Merged into the API request body. Must be a JSON object, other values are ignored.": "会合并进 API 请求体,必须是 JSON 对象,其他类型的值会被忽略。", + "Invalid JSON object, this value is ignored.": "不是合法的 JSON 对象,该值会被忽略。", "API Url": "API地址", "Provider": "提供商", "Others": "其他", diff --git a/src/_locales/zh-hant/main.json b/src/_locales/zh-hant/main.json index 3dd20a229..832f8792f 100644 --- a/src/_locales/zh-hant/main.json +++ b/src/_locales/zh-hant/main.json @@ -118,6 +118,9 @@ "Override provider temperature": "覆寫供應商的溫度參數", "The temperature parameter is not sent. The provider or model default is used.": "不會傳送溫度參數,將使用供應商或模型的預設值。", "The current model does not accept a custom temperature. The parameter will not be sent.": "目前的模型不接受自訂溫度參數,因此不會傳送這個參數。", + "Extra Request Body (JSON)": "額外請求參數 (JSON)", + "Merged into the API request body. Must be a JSON object, other values are ignored.": "會合併進 API 請求主體,必須是 JSON 物件,其他類型的值會被忽略。", + "Invalid JSON object, this value is ignored.": "不是合法的 JSON 物件,這個值會被忽略。", "API Url": "API 網址", "Provider": "供應商", "Others": "其他", diff --git a/src/config/index.mjs b/src/config/index.mjs index 6267ef67c..8c849343c 100644 --- a/src/config/index.mjs +++ b/src/config/index.mjs @@ -839,6 +839,7 @@ export const defaultConfig = { maxConversationContextLength: 9, temperatureOverrideEnabled: false, temperature: 1, + extraBody: '', customChatGptWebApiUrl: 'https://chatgpt.com', customChatGptWebApiPath: '/backend-api/conversation', customOpenAiApiUrl: 'https://api.openai.com', diff --git a/src/popup/sections/AdvancedPart.jsx b/src/popup/sections/AdvancedPart.jsx index 017b9191f..134535198 100644 --- a/src/popup/sections/AdvancedPart.jsx +++ b/src/popup/sections/AdvancedPart.jsx @@ -3,6 +3,7 @@ import { parseFloatWithClamp, parseIntWithClamp } from '../../utils/index.mjs' import { getModelValue } from '../../utils/model-name-convert.mjs' import { isUsingAzureOpenAiApiModel } from '../../config/index.mjs' import { canApplyTemperatureOverride } from '../../services/apis/temperature-params.mjs' +import { parseExtraBody } from '../../services/apis/extra-body-params.mjs' import PropTypes from 'prop-types' import { Tab, TabList, TabPanel, Tabs } from 'react-tabs' import Browser from 'webextension-polyfill' @@ -22,6 +23,8 @@ function ApiParams({ config, updateConfig }) { ? config.customModelName : getModelValue(config) const temperatureOverrideAvailable = canApplyTemperatureOverride(selectedModel) + const extraBodyValue = typeof config.extraBody === 'string' ? config.extraBody : '' + const extraBodyInvalid = extraBodyValue.trim() !== '' && !parseExtraBody(extraBodyValue) return ( <> @@ -89,6 +92,21 @@ function ApiParams({ config, updateConfig }) { /> )} +