Skip to content

refactor: share provider credentials and user agent - #388

Open
cunninghamcard-bit wants to merge 9 commits into
masterfrom
refactor/share-provider-credentials-and-user-agent
Open

cunninghamcard-bit wants to merge 9 commits into
masterfrom
refactor/share-provider-credentials-and-user-agent

Conversation

@cunninghamcard-bit

Copy link
Copy Markdown
Contributor

RFC-0046 R1 的第 10 个小 PR,也是 R1 清单的最后一项(见 #328):把凭据与请求头组装(Credential、AuthScheme、ProviderHeaders)和 UA 改写(UserAgentFetch、transport_user_agent)从 aimux-providers/src/shared/mod.rs 挪进 aimux-provider-utils。

Before:这五个是 providers 里 shared 模块的 pub(crate) 项,约 60 个厂商文件从 crate::shared 引入。它们只依赖 provider-utils 里已有的 load_api_key、Resolvable、combine_headers、normalize_headers、with_user_agent_suffix、Fetch。

After:五个都在 aimux_provider_utils 公开导出,厂商改从这里引入。shared/mod.rs 只剩 package_fetch(它读 providers 自己的 aisdk_pins,留在原处)和 TransformRequestBody。请求头逐字节不变。

一、体系现状(概览)

上游没有共享的请求头类型,每个包在工厂里写一个 getHeaders:

@ai-sdk/openai 4.0.83   openai-provider.ts:219
  getHeaders = () => withUserAgentSuffix(
      { Authorization: `Bearer ${loadApiKey(...)}`, 'OpenAI-Organization', 'OpenAI-Project', ...headers },
      `ai-sdk/openai/${VERSION}`)
@ai-sdk/anthropic、groq、mistral、…   同样的写法,凭据头和固定头各不相同
@ai-sdk/amazon-bedrock 5.0.100  amazon-bedrock-sigv4-fetch.ts:42-46、:126-130   传输层追加 UA 后缀
@ai-sdk/anthropic-aws 2.0.63    anthropic-aws-fetch.ts:42-46、:126-130          同上
@ai-sdk/provider-utils 5.0.55   load-api-key.ts、combine-headers.ts、with-user-agent-suffix.ts

aimux(master 9f9f5ab):

aimux-providers/src/shared/mod.rs
  :81   pub(crate) enum Credential { None, Explicit, Env, OptionalEnv }   secret()
  :128  pub(crate) enum AuthScheme { Bearer, Header, Scheme }
  :143  pub(crate) struct ProviderHeaders { credential, scheme, fixed, user, user_agent }   resolve()
  :241  pub(crate) struct UserAgentFetch           // package_fetch、preset 用
  :270  pub(crate) fn transport_user_agent         // anthropic_aws/fetch.rs、bedrock/sigv4_fetch.rs 用
  └─ 约 60 个厂商文件 use crate::shared::{Credential, ProviderHeaders, AuthScheme, …}

RFC-0046 §3.2 把“凭据、UA 组装”定为 provider-utils 的通用函数。

二、本 PR 改动(细节)

aimux-provider-utils/src/provider_headers.rs
  :16   pub enum Credential               :35 explicit_or_env   :57 secret
  :69   pub enum AuthScheme
  :84   pub struct ProviderHeaders        :97 new   :114 bearer   :125 with_user_agent   :135 resolve
aimux-provider-utils/src/user_agent_fetch.rs
  :13   pub struct UserAgentFetch
  :39   pub fn transport_user_agent
aimux-provider-utils/src/lib.rs           pub mod + pub use

aimux-providers/src/
  shared/mod.rs              删掉五项;package_fetch 改用 aimux_provider_utils::UserAgentFetch
  shared/exchange.rs、decision_support.rs、preset.rs、anthropic_aws/fetch.rs、bedrock/sigv4_fetch.rs、
  各厂商的 provider/config 与测试(共 61 个文件)     use crate::shared::… → use aimux_provider_utils::…

搬过去的代码只多了 clippy pedantic 对公开函数要求的 #[must_use] 和 # Errors 说明,逻辑一字未改。上游没有同名类型,名字沿用。

测试:

文件 用例 说明
provider-utils src/tests/provider_headers.rs 4 上游没有对应的 .test.ts;4 条非上游:三种凭据方案与 Credential::None、用户头覆盖或删除(不分大小写)且 UA 后缀最后追加、Env 与 OptionalEnv 读不到环境变量时的区别、transport_user_agent 改写 ai-sdk-provider-utils/<v> 并追加后缀

各厂商已有的请求头、UA 用例全部原样通过。工作区测试数不变(4380),provider-utils 另加 4 个。

改动规模:

范围 文件 增 删
aimux-provider-utils 5 353 0
aimux-providers 61 116 294
合计(含 CHANGELOG) 67 475 294

providers 那 61 个文件里绝大多数只改一行 use。

公开 API 与行为

  • Added:aimux_provider_utils::{Credential, AuthScheme, ProviderHeaders, UserAgentFetch, transport_user_agent}。
  • 被删的是 pub(crate) 项,没有 Breaking;行为不变。CHANGELOG 的 Added 一条。

检查

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

Credential, AuthScheme and ProviderHeaders describe a provider's request
headers as data and resolve them per request, the way each AI SDK
package's getHeaders combines loadApiKey, its fixed headers, the user's
headers and withUserAgentSuffix. UserAgentFetch and transport_user_agent
make the user-agent edits of the AI SDK AWS transports. They move here
from aimux-providers' shared module (RFC-0046 R1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0116HPMoqaLfFr1TyqDq1hrW
Every provider imports Credential, AuthScheme, ProviderHeaders,
UserAgentFetch and transport_user_agent from provider-utils; the shared
copies are gone. Headers are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0116HPMoqaLfFr1TyqDq1hrW
…ider-credentials-and-user-agent

# Conflicts:
#	aimux-provider-utils/src/tests/mod.rs

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

credential/header/UA 的优先级和 redaction 都核对通过,61 处只是路径改动,4 个新测试覆盖完整。只差 rebase:冲突只在 provider-utils/src/tests/mod.rs,保留 master 的三个模块 + mod provider_headers;,CI 重跑。

…ider-credentials-and-user-agent

# Conflicts:
#	aimux-providers/src/azure/openai_provider.rs
#	aimux-providers/src/openai/config.rs
#	aimux-providers/src/vertex/anthropic/anthropic_provider.rs
…ider-credentials-and-user-agent

# Conflicts:
#	CHANGELOG.md
#	aimux-provider-utils/src/tests/mod.rs

Copy link
Copy Markdown
Contributor Author
eric 的意见 处理
合上新 master,CI 重跑 已合上 master e76ffee(含 #380、#382、#389–#391),提交 740675e。冲突在 Azure、OpenAI 配置、Vertex Claude 和 Anthropic AWS 的 import(#389、#390 动过这几处):Credential、ProviderHeaders、AuthScheme 改从 aimux_provider_utils 引入,Endpoint、ResolveEndpoint、TransformRequestBody 仍从 crate::shared 引入;CHANGELOG 和 tests/mod.rs 两边都保留。本地 fmt、边界检查、clippy、doc、providers 与 provider-utils 全部测试通过,CI 重跑中

Generated by Claude Code

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

仅 rebase + union;credential/header/UA 优先级和脱敏未变,16/16 CI 通过。Approve。

…ider-credentials-and-user-agent

# Conflicts:
#	CHANGELOG.md
#	aimux-providers/src/anthropic_aws/provider.rs
#	aimux-providers/src/decision_support.rs
#	aimux-providers/src/vertex/anthropic/anthropic_provider.rs
…ider-credentials-and-user-agent

# Conflicts:
#	aimux-provider-utils/src/tests/mod.rs

Copy link
Copy Markdown
Contributor Author

合上 master 到 d30f8343(#383 合入后),head ef9050ea。

提交 内容
bb285350 合上 master 0bc182a9(#389–#394)。冲突在 R2/R3 改过 import 的文件,按本 PR 的做法改成从 aimux_provider_utils 引入;#393 带进来的 openai/tests/evaluation.rs 还在用 crate::shared::{Credential, ProviderHeaders},同样改成 aimux_provider_utils::{…}
ef9050ea 合上 master d30f8343(#383)。只有 provider-utils src/tests/mod.rs 的模块列表冲突,两边都保留

本地 cargo fmt --check、scripts/check_provider_boundaries.sh、cargo +1.99 clippy -D warnings、cargo check --workspace、cargo doc、全部测试通过。没有其他改动。


Generated by Claude Code

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

Rebase verified content-preserving; CI 16/16; re-approve current head.

@cunninghamcard-bit

Copy link
Copy Markdown
Contributor Author

合上 master 853a512a(#384、#395)。冲突只在 CHANGELOG.md 的 Added:本 PR 的 Credential / ProviderHeaders / transport_user_agent 和 #384 的 create_provider_stream_error 都留。没有新标题。代码是自动合并,本 PR 的改动没动。head 3c1f9f26。CI 绿了再请审。

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

Rebase 内容没丢,但和 #395 语义冲突:aimux-providers/src/openai_compatible/tests/request_rewrite.rs:11 仍 import crate::shared::{AuthScheme, Credential, ProviderHeaders},这些已搬进 aimux-provider-utils,Rust core clippy 直接 E0432。把该 import 改成 aimux_provider_utils::{...}(或对应 re-export),重跑 CI 即可。

This branch has not been deployed

No deployments
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