Skip to content

refactor: move vertex partner body rewrites into the compatible config - #395

Merged
eric8810 merged 3 commits into
masterfrom
refactor/move-vertex-partner-body-rewrites-into-the-compatible-config
Oct 29, 2026
Merged

eric8810 merged 3 commits into
masterfrom
refactor/move-vertex-partner-body-rewrites-into-the-compatible-config

Conversation

@cunninghamcard-bit

Copy link
Copy Markdown
Contributor

RFC-0046 R5(清单见 #328):Vertex 上的 MaaS、xAI 两个托管变体对 Chat Completions 请求体的改写,从公开的 transform_request_body 回调改成 openai_compatible 目录里的封闭枚举 CompatibleBodyRewrite,与 #394 给 Vertex 上的 Claude 用的 AnthropicBodyRewrite 同一做法(RFC-0046 §3.5)。

Before:create_google_vertex_maas、create_google_vertex_xai 各写一个私有函数(maas_request_body、xai_request_body),包成 Arc<dyn Fn> 塞进 OpenAICompatibleProviderSettings.transform_request_body。托管变体的改写占用了用户回调的位置,配置不能打印也不能比较。

After:两个函数搬进 openai_compatible/request_rewrite.rs,成为 CompatibleBodyRewrite::{VertexMaas, VertexXai} 的 apply。托管变体在 build(..) 里选一个变体;协议包先跑这个改写,再跑用户的 transform_request_body(只对 chat)。发出的请求不变:MaaS 上两个 Llama 4 模型没设 max_tokens 时补 8192,Vertex 上的 Grok 不发 reasoning_effort。

一、体系现状(概览)

上游:

@ai-sdk/google-vertex 5.0.100
  maas/google-vertex-maas-provider.ts:129   createOpenAICompatible({ ..., transformRequestBody: transformGoogleVertexMaasRequestBody })
  xai/google-vertex-xai-provider.ts:182     createOpenAICompatible({ ..., transformRequestBody: transformGoogleVertexXaiRequestBody })
@ai-sdk/openai-compatible 3.0.62
  openai-compatible-provider.ts:185         chat 模型的 config.transformRequestBody = options.transformRequestBody
  chat/openai-compatible-chat-language-model.ts
    :165  transformRequestBody(args) = config.transformRequestBody?.(args) ?? args
    :340  doGenerate → transformRequestBody(args)
    :448  doStream   → transformRequestBody({ ...args, stream: true, ... })

aimux(master d30f834):

vertex/maas/maas_provider.rs     create_google_vertex_maas
  └─ openai_compatible::build(settings{ transform_request_body: Some(Arc::new(maas_request_body)) }, ...)
vertex/xai/xai_provider.rs       create_google_vertex_xai
  └─ openai_compatible::build(settings{ transform_request_body: Some(Arc::new(xai_request_body)) }, ...)
openai_compatible/provider.rs
  build → Assembly → OpenAICompatibleProvider → CompatModelConfig.transform_request_body(chat)
  CompatModelConfig::transform_body(body) = 用户回调(body)
openai_compatible/chat/chat_language_model.rs:492 / :619   generate / stream 调 transform_body

Claude on Vertex 在 #394 已改成 anthropic/request_rewrite.rs 的 AnthropicRequestUrl、AnthropicBodyRewrite。

二、本 PR 改动(细节)

openai_compatible/request_rewrite.rs(新)
  :14  pub(crate) enum CompatibleBodyRewrite { VertexMaas, VertexXai }
  :27  apply(self, body) -> Value        (原 maas_request_body、xai_request_body 的逻辑,原样搬过来)
openai_compatible/mod.rs:34,54           mod request_rewrite;pub(crate) use CompatibleBodyRewrite
openai_compatible/provider.rs
  :152  build(settings, base_url, headers, fetch_headers, body_rewrite, package)   新增 body_rewrite 参数
  :201  Assembly.body_rewrite
  :219  OpenAICompatibleProvider.body_rewrite
  :261  建模型时只给 chat:body_rewrite.filter(|_| method == "chat")
  :414  CompatModelConfig.body_rewrite
  :541  transform_body:先 body_rewrite.apply,再用户的 transform_request_body
vertex/maas/maas_provider.rs:38          Some(CompatibleBodyRewrite::VertexMaas);删除 maas_request_body 和 transform_request_body 设置
vertex/xai/xai_provider.rs:87            Some(CompatibleBodyRewrite::VertexXai);删除 xai_request_body 和 transform_request_body 设置
preset.rs:479                            Assembly { body_rewrite: None, .. }
docs/aisdk-architecture-alignment.md     §5 加一行“托管变体对协议包请求的改写”(同时补记 #394 的两个 Anthropic 枚举)

测试:

文件 用例 说明
openai_compatible/tests/request_rewrite.rs 3 aimux 用例(上游的回调只经由 Vertex provider 测,没有单独的测试文件):MaaS 只给两个 Llama 4 模型补 max_tokens,已设时不覆盖;xAI 删除 reasoning_effort;托管改写先于用户回调运行
vertex 下 MaaS、xAI 的上游移植用例 0 不改,照旧通过(请求体断言不变)
openai_compatible/tests 里构造 CompatModelConfig 的三处 0 只补 body_rewrite: None

改动规模:

范围 文件 增 删
aimux-providers 11 161 35
文档与 CHANGELOG 2 9 0
合计 13 170 35

公开 API 与行为

  • 公开 API 不变:CompatibleBodyRewrite 是 pub(crate),build 本来就是 pub(crate),OpenAICompatibleProviderSettings.transform_request_body 保留。
  • 行为不变:两个 Vertex 托管变体发出的请求体与 master 相同。
  • CHANGELOG 的 Changed 一条。

检查

基于 master d30f8343。cargo fmt --check、scripts/check_provider_boundaries.sh、cargo +1.99 clippy(providers 与 provider-utils,-D warnings)、cargo check --workspace、cargo doc、全部测试均通过。


Generated by Claude Code

The compatible package's Assembly, provider and model config carry
body_rewrite: Option<CompatibleBodyRewrite> next to the user's
transform_request_body; transform_body runs the rewrite first. The MaaS
and xAI providers on Vertex pick VertexMaas and VertexXai instead of
setting the public callback, so their rewrites live with the protocol.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0116HPMoqaLfFr1TyqDq1hrW
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0116HPMoqaLfFr1TyqDq1hrW

@eric8810 eric8810 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Vertex partner body rewrite 是逐行搬移,host rewrite 先于 user callback;其他 compatible 路径不受影响,现有 MaaS/xAI e2e 覆盖完整,16/16 CI 通过。Approve。

@eric8810
eric8810 merged commit 853a512 into master Oct 29, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants