feat: 支持自定义 JSON 请求体#218
Open
ZRHann wants to merge 1 commit into
Open
Conversation
新增按服务保存的「自定义请求体」配置,以顶层浅合并(用户字段优先)的方式追加到各 AI 服务的请求体中,可用于补充服务商需要的额外参数(如 thinking 等控制字段)。 - model: Config 新增 customBody 字段 - template: 新增 mergeCustomBody 辅助函数,接入全部消息模板(覆盖所有 AI 服务);空/非法 JSON 自动忽略,不影响翻译 - ui: 设置页「自定义模型」下方新增「自定义请求体」输入框,含 JSON 校验与导出清理 - test: 引入 Vitest,覆盖 mergeCustomBody 与 commonMsgTemplate 相关 issue: Bistutu#213 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Reviewer's GuideAdds per-service configurable custom JSON request bodies that are shallow-merged into all AI service payloads, wires this into the UI and config model, and introduces Vitest-based tests (plus tooling tweaks) to verify the merge logic and templates. Sequence diagram for applying custom JSON body to AI requestsequenceDiagram
actor User
participant MainVue as Main_vue
participant Config as Config
participant Template as commonMsgTemplate
participant Merge as mergeCustomBody
participant Service as AI_service
User->>MainVue: Edit config.customBody[config.service]
MainVue->>Config: Persist customBody per service
User->>Template: commonMsgTemplate(origin)
Template->>Config: Read config.service
Template->>Config: Read config.customBody[config.service]
Template->>Merge: mergeCustomBody(payload, currentCustomBody())
Merge-->>Template: merged payload
Template->>Service: JSON.stringify(merged payload)
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- In
mergeCustomBody, consider avoiding mutation of thepayloadargument (e.g., return a new merged object) so callers don’t need to reason about in-place side effects and it’s easier to reuse in different contexts. - The JSON-object validation logic for customBody is duplicated between
isValidCustomBodyandmergeCustomBody; you could extract a shared helper (e.g.,parseCustomBody) to keep the rules consistent and reduce maintenance overhead. - In
Main.vue, thev-model="config.customBody[config.service]"will throw ifconfig.customBodyis undefined; it might be safer to ensurecustomBodyis always initialized in the reactive config or to guard this access in the template/computed.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `mergeCustomBody`, consider avoiding mutation of the `payload` argument (e.g., return a new merged object) so callers don’t need to reason about in-place side effects and it’s easier to reuse in different contexts.
- The JSON-object validation logic for customBody is duplicated between `isValidCustomBody` and `mergeCustomBody`; you could extract a shared helper (e.g., `parseCustomBody`) to keep the rules consistent and reduce maintenance overhead.
- In `Main.vue`, the `v-model="config.customBody[config.service]"` will throw if `config.customBody` is undefined; it might be safer to ensure `customBody` is always initialized in the reactive config or to guard this access in the template/computed.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
实现 #213:当前各 AI 服务的请求体是写死的(如
commonMsgTemplate固定输出{model, temperature, messages}),无法追加服务商特定的字段。典型场景是 Kimi(K2.5 / K2.6)的「思考开关」——思考与非思考模式共用同一个模型名,靠请求体顶层的thinking字段控制({"type":"enabled"}/{"type":"disabled"}),现有自定义接口无法设置它。改动
新增按服务保存的「自定义请求体」配置,会以顶层浅合并、用户字段优先的方式合并进请求体:
Config.customBody(entrypoints/utils/model.ts):按服务存储的 JSON 字符串。mergeCustomBody()(entrypoints/utils/template.ts):纯函数,解析用户 JSON 并合并进 payload;空字符串 / 非法 JSON 自动忽略,绝不影响正常翻译。已接入全部消息模板(common / deepseek / gemini / claude / tongyi / yiyan / minimax / coze),因此对所有 AI 服务生效(含「自定义接口」)。components/Main.vue):在「自定义模型」下方新增「自定义请求体」输入框,带 JSON 合法性校验(非法时红框提示并被忽略),并在配置导出时清理空值。用法示例(Kimi 关闭思考)
「自定义请求体」填:
{"thinking": {"type": "disabled"}}最终发出的请求体:
{"model":"kimi-k2.6","temperature":1,"messages":[...],"thinking":{"type":"disabled"}}测试
引入 Vitest(项目此前无测试框架),新增
tests/template.test.ts,共 18 个用例,覆盖:mergeCustomBody的合并 / 覆盖 / 嵌套对象 / 空值 / 非法 JSON / 数组与基本类型拒绝等边界;commonMsgTemplate在有 / 无自定义请求体下的输出,以及thinking字段注入。说明
vue-tsc在wxt.config.ts上出现类型冲突;故在package.json加了pnpm.overrides.vite,固定为项目原本的5.4.11做去重,对运行时构建无影响。Closes #213
Summary by Sourcery
Add per-service configurable JSON request body that is shallow-merged into all AI service payloads, expose it in the UI, and introduce a Vitest test setup to cover the new merging logic and templates.
New Features:
Enhancements:
Build:
Tests: