Skip to content

refactor: share tool name mapping and strip file extension - #382

Merged
eric8810 merged 9 commits into
masterfrom
refactor/share-tool-name-mapping-and-strip-file-extension
Oct 28, 2026
Merged

eric8810 merged 9 commits into
masterfrom
refactor/share-tool-name-mapping-and-strip-file-extension

Conversation

@cunninghamcard-bit

Copy link
Copy Markdown
Contributor

RFC-0046 R1 的第 4 个小 PR(清单见 #328):把 createToolNameMapping 和 stripFileExtension 放进 aimux-provider-utils。Anthropic、OpenAI Responses、Google 用前者,Bedrock 用后者。

Before:ToolNameMapping 是 Anthropic 的公开模块,里面还塞了 Anthropic 专有的 toolset 名和“代码执行标为 dynamic”的判断。OpenAI Responses 自己维护一张 HashMap,Google 自己找 google.code_execution 的名字。Bedrock 的去扩展名从最后一个点切,report.v2.pdf 会发成 reportv2。

After:三家都按上游 providerToolNames 表调用同一个 create_tool_name_mapping;Anthropic 的 toolset 名和 dynamic 判断拆成上游同名的两个函数;Bedrock 从第一个点切,report.v2.pdf 发成 report,与上游一致。

一、体系现状(概览)

上游:

@ai-sdk/provider-utils 5.0.55
  create-tool-name-mapping.ts  createToolNameMapping({ tools, providerToolNames }) → { toProviderToolName, toCustomToolName }
  strip-file-extension.ts      stripFileExtension(filename)          // 第一个点之前
调用方
  anthropic-language-model.ts:526   createToolNameMapping(23 个 id)
                              :203   getAnthropicToolsetNames(tools)
                              :1221  hasDynamicFilteringWebToolWithoutCodeExecution(tools)
    └─ convert-to-anthropic-prompt.ts:80  convertToAnthropicPrompt({ toolNameMapping, toolsetNames })
  openai-responses-language-model.ts:378  createToolNameMapping(12 个 id)
                                     :567  webSearchToolName
  google-language-model.ts:344            createToolNameMapping({ 'google.code_execution' })
  convert-to-amazon-bedrock-chat-messages.ts:78  sanitizeDocumentName → stripFileExtension

aimux(master b0450d4):

aimux-providers
  anthropic/tool_name_mapping.rs   pub struct ToolNameMapping { 两张表, mark_code_execution_dynamic, toolset_names }
    ├─ language_model.rs           ToolNameMapping::new(tools) 三处;names.mark_code_execution_dynamic() 两处
    └─ convert_to_prompt.rs        tool_names.toolset_name(..)
  openai/responses/responses_language_model.rs  fn tool_name_mapping(options) -> HashMap<String, String>
  google/language_model.rs         code_execution_tool_name(tools) 手找 google.code_execution
  bedrock/convert_to_chat_messages.rs  fn strip_file_extension: name.rfind('.')

二、本 PR 改动(细节)

aimux-provider-utils/src/create_tool_name_mapping.rs
  :17  pub struct ToolNameMapping
  :26  to_provider_tool_name / :35 to_custom_tool_name     // 没有映射时原样返回
  :48  create_tool_name_mapping(tools: Option<&[Tool]>, provider_tool_names: &[(&str, &str)])
aimux-provider-utils/src/strip_file_extension.rs:11  strip_file_extension(&str) -> &str   // split_once('.')

aimux-providers/src/anthropic/
  tool_name_mapping.rs                    删除(连同 tests/tool_name_mapping.rs)
  language_model.rs:54   anthropic_tool_name_mapping(tools)   // 上游 23 个 id 的表
                   :100  get_anthropic_toolset_names(tools)
                   :115  has_dynamic_filtering_web_tool_without_code_execution(tools)
                   :1353 / :1503 / :1748   dynamic 标志改为单独参数和流状态字段
  convert_to_prompt.rs:102,120,138  入口多一个 toolset_names: &HashMap<String, String>
                      :684,737     convert_tool_result / convert_assistant_part 带上它
                      :1025        toolset_name(toolset_names, ..)   // 对应 getAnthropicToolsetName
  tests/convert_to_prompt.rs       toolset 用例按上游写法:toolsetToolNameMapping + toolsetNames
aimux-providers/src/openai/responses/responses_language_model.rs
  :384  ResponsesToolNames { tool_name_mapping, web_search_tool_name }   // 上游 12 个 id 的表
  :433  to_custom_tool_name:web_search 取 webSearchToolName ?? 'web_search'
aimux-providers/src/google/language_model.rs:1710  code_execution_tool_name 改由映射得出
aimux-providers/src/bedrock/convert_to_chat_messages.rs:535  改用 strip_file_extension

测试:

文件 用例 说明
provider-utils src/tests/create_tool_name_mapping.rs 6 上游全部
provider-utils src/tests/strip_file_extension.rs 4 上游全部
bedrock tests/convert_to_chat_messages.rs 1 新增:多个点的文件名只留第一个点之前;在 master 上失败
anthropic tests/tool_name_mapping.rs −2 删除,两个场景由上游的 createToolNameMapping 用例覆盖

工作区测试总数 4380 → 4379(provider-utils 的 10 个另计)。

改动规模:

范围 文件 增 删
aimux-provider-utils 6 261 0
aimux-providers 11 278 299
合计(含 CHANGELOG) 18 554 299

公开 API 与行为

  • Breaking:aimux_providers::anthropic::tool_name_mapping 删除,ToolNameMapping 改为 aimux_provider_utils::ToolNameMapping,由 create_tool_name_mapping 构造;mark_code_execution_dynamic 不再公开。
  • Fixed:Bedrock 文档名从第一个点切(与上游 stripFileExtension 一致)。
  • CHANGELOG 的 Breaking、Added、Fixed 各有一条。

检查

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

…ension

RFC-0046 R1 (§4.5 item 4): both follow @ai-sdk/provider-utils 5.0.55.
create_tool_name_mapping maps the caller's names of provider tools to the
provider's wire names and back, from a table of provider tool ids;
strip_file_extension drops everything from the first dot. Their upstream
tests are ported under src/tests/.

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

The anthropic::tool_name_mapping module is removed. The language model
builds the shared ToolNameMapping from upstream's providerToolNames table,
the toolset names with get_anthropic_toolset_names and the dynamic code
execution flag with has_dynamic_filtering_web_tool_without_code_execution,
and passes the toolset names to the prompt converter as upstream's
convertToAnthropicPrompt takes them. The converter tests build the
mapping and toolset names as the upstream tests do.

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

OpenAI Responses replaces its name HashMap with the shared mapping plus
webSearchToolName, as openai-responses-language-model.ts does; Google's
code execution tool name comes from the mapping with the
google.code_execution entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0116HPMoqaLfFr1TyqDq1hrW
sanitizeDocumentName uses provider-utils stripFileExtension, which cuts at
the first dot; the Rust copy cut at the last, so report.v2.pdf was sent
as reportv2. The new test fails on master.

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

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.

tool name mapping 与上游 createToolNameMapping 一致;strip_file_extension 用第一个点,Bedrock 多点的行为变化与 upstream sanitizeDocumentName 对齐并有回归测试。Approve。

…-name-mapping-and-strip-file-extension

# Conflicts:
#	CHANGELOG.md
#	aimux-provider-utils/src/lib.rs
#	aimux-provider-utils/src/tests/mod.rs
…-name-mapping-and-strip-file-extension

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

Copy link
Copy Markdown
Contributor Author
项 处理
合上新 master 已合上 master 5cc14b1(含 #380、#389–#391),提交 7129644;本 PR 的改动不变。本地 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 仅合并 master,tests/mod.rs 为无重复 union,mapping/strip 逻辑与批准时逐字节一致,16/16 CI 通过。Re-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