Skip to content

refactor: give anthropic hosts an anthropic config - #394

Merged
eric8810 merged 2 commits into
masterfrom
refactor/give-anthropic-hosts-an-anthropic-config
Oct 28, 2026
Merged

eric8810 merged 2 commits into
masterfrom
refactor/give-anthropic-hosts-an-anthropic-config

Conversation

@cunninghamcard-bit

Copy link
Copy Markdown
Contributor

Before:Anthropic 模型的配置叫 AnthropicModelConfig,各宿主的差异装在一个子结构 AnthropicModelHooks 里:URL 用 MessagesUrl,请求体改写是函数指针 prepare_body: Option<fn(Value) -> Value>,Vertex 的 rawPredict 信封函数和 anthropic_version 常量写在 Vertex 目录里。

After:配置改名 AnthropicConfig(上游的类型名),上游的几个成员直接成为它的字段:build_request_url: AnthropicRequestUrl、body_rewrite: Option<AnthropicBodyRewrite>、supports_native_structured_output、supports_strict_tools,旁边是用户回调 transform_request_body 和 aimux 的错误处理器。URL 和请求体改写是协议目录里的封闭枚举(anthropic/request_rewrite.rs),Vertex 只选变体。线上的 URL 和请求体不变。

这是 RFC-0046 R3(托管变体改用 AnthropicConfig,§3.4、§3.5)的第一个 PR,跟踪在 #328。

一、体系现状(概览)

上游(@ai-sdk/anthropic 4.0.71、@ai-sdk/google-vertex 5.0.100):

AnthropicMessagesLanguageModel(modelId, config)          anthropic-language-model.ts
  config.buildRequestUrl?(baseURL, isStreaming)          :251,用于 :1144
  config.transformRequestBody?(args, betas)              :252,用于 :1153
  config.supportsNativeStructuredOutput / supportsStrictTools   :258-259
createVertexAnthropic                                    google-vertex-anthropic-provider.ts
  buildRequestUrl: …/{model}:rawPredict 或 :streamRawPredict     :223
  transformRequestBody: 去掉 model,加 anthropic_version        :227-232
  supportsNativeStructuredOutput: false, supportsStrictTools: false   :238-240
createAnthropicAws:不传 buildRequestUrl,baseURL 就是区域端点

aimux(本 PR 之前):

AnthropicMessagesModel::with_config(model_id, AnthropicModelConfig { …, hooks: AnthropicModelHooks { messages_url, prepare_body, failed_response_handler, supports_* } })
  anthropic/provider.rs        hooks: AnthropicModelHooks::default()
  anthropic_aws/provider.rs    hooks: AnthropicModelHooks::default()
  vertex/anthropic/anthropic_provider.rs
      hooks: { MessagesUrl::RawPredict, prepare_body: Some(raw_predict_envelope), google 错误, false, false }
      raw_predict_envelope、ANTHROPIC_VERTEX_VERSION 在 Vertex 目录
language_model.rs:226  config.request_profile() → build_request_body_for
                 :227  config.transform_body(body)   先 hooks.prepare_body,再 transform_request_body
                 :232  config.messages_url(model_id, stream)

二、本 PR 改动(细节)

anthropic/request_rewrite.rs(新,aimux 扩展)
  :13  VERTEX_ANTHROPIC_VERSION = "vertex-2023-10-16"
  :17  enum AnthropicRequestUrl { Messages, VertexRawPredict }
  :30    fn build(base_url, model_id, stream)    原 messages_url 的两条分支
  :49  enum AnthropicBodyRewrite { VertexRawPredict }
  :57    fn apply(body)                           原 raw_predict_envelope

anthropic/config.rs
  MessagesUrl、AnthropicModelHooks 删除
  :57  struct AnthropicConfig(原 AnthropicModelConfig)
  :68    build_request_url    :71 body_rewrite    supports_native_structured_output、supports_strict_tools
  :79    failed_response_handler(aimux:宿主的错误格式)
  :120 fn messages_url  → self.build_request_url.build(..)
  :213 fn transform_body → 先 body_rewrite.apply,再 transform_request_body(顺序不变)

anthropic/provider.rs:217、anthropic_aws/provider.rs:201   Messages、无改写、true、true、Anthropic 错误
vertex/anthropic/anthropic_provider.rs:164                VertexRawPredict、Some(VertexRawPredict)、false、false、Google 错误
anthropic/mod.rs     pub(crate) mod request_rewrite;导出 anthropic_failed_response_handler 给 AWS
files.rs、language_model.rs、skills/skills.rs             只改类型名
scripts/check_provider_boundaries.sh 规则 6               不再禁止 `AnthropicConfig` 这个名字(原来禁的是 builder 时代的同名类型),继续禁 body_overrides、api_key_source、from_env

与 RFC §3.5 草图的两处出入,记在这里:

  • AnthropicRequestUrl 没有 AwsRegional:Claude Platform on AWS 的区域主机是它的 ResolveEndpoint 解析出的 base URL(refactor: resolve anthropic host endpoints outside anthropic #390),URL 本身就是 {base_url}/messages,与上游 createAnthropicAws 不传 buildRequestUrl 一致。
  • AnthropicBodyRewrite 没有 Bedrock:Bedrock Converse 是独立协议,不经过 Anthropic 模型。

测试

范围 结果
aimux-providers lib 4379 通过,0 失败(用例数与 master 相同,本 PR 不改测试)
Vertex 上的 Claude(vertex/tests/anthropic/anthropic_provider.rs) URL 以 :rawPredict 结尾、请求体带 anthropic_version、没有 model 的断言照旧通过
Anthropic、Anthropic AWS 全部通过

体积

文件 增 删
anthropic/request_rewrite.rs 72 0
anthropic/config.rs 27 67
vertex/anthropic/anthropic_provider.rs 8 29
anthropic/provider.rs 10 5
anthropic_aws/provider.rs 9 4
其余 4 个 Rust 文件 13 11
scripts/check_provider_boundaries.sh 5 4
合计 10 个文件 +144 -120

公开 API 与行为

  • 公开 API 无变化:AnthropicConfig、两个枚举都是 crate 内部的;公开的 TransformRequestBody 不动。
  • 行为无变化。

检查

基于 master e76ffeee。cargo +1.99 fmt --check、scripts/check_provider_boundaries.sh、cargo +1.99 clippy -p aimux-providers --lib --tests -D warnings、cargo check --workspace --tests、cargo doc -D warnings、aimux-providers 全部测试,均通过。


Generated by Claude Code

AnthropicModelConfig becomes AnthropicConfig with the upstream members as
its own fields: build_request_url (AnthropicRequestUrl), body_rewrite
(AnthropicBodyRewrite), supports_native_structured_output and
supports_strict_tools, beside the user's transform_request_body and the
host's error handler. AnthropicModelHooks and its fn-pointer body hook go.

The Vertex rawPredict URL and envelope move into anthropic/request_rewrite.rs
as enum variants, so Vertex only picks them (RFC-0046 §3.5). Claude Platform
on AWS keeps the Messages URL: its regional host is the base URL its
endpoint resolves. URLs and bodies on the wire are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0116HPMoqaLfFr1TyqDq1hrW
Boundary rule 6 banned the builder-era AnthropicConfig. The name now
belongs to the RFC-0046 §3.5 model config, so the rule keeps checking the
builder-era fields (body_overrides, api_key_source, from_env) only.

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.

AnthropicConfig + AnthropicRequestUrl/body rewrite 逐项等价,三路 host 的 flags/handler/header 保持,cycle 断开;CI 通过。Approve。

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