Skip to content
This repository was archived by the owner on May 13, 2026. It is now read-only.

fix(claude): 修复工具调用兼容与解析回退 - #55

Merged
CJackHwang merged 2 commits into
CJackHwang:mainfrom
BigUncle:fix/claude-toolcall
Feb 27, 2026
Merged

CJackHwang merged 2 commits into
CJackHwang:mainfrom
BigUncle:fix/claude-toolcall

Conversation

@BigUncle

Copy link
Copy Markdown
Contributor
  • Claude 工具定义兼容 input_schema 与 function.parameters

  • tool_calls 解析增加 thinking 回退与大小写无关工具名匹配

  • 补充 claude/util 相关回归测试

fix(claude): improve tool-call compatibility and parsing fallback for Anthropic messages

PR 内容

背景

在 POST /anthropic/v1/messages 场景下,存在“返回普通文本而非 tool_use”的问题,主要表现为:

  1. OpenAI 风格 tools[].function.parameters 请求兼容性不足。
  2. 模型将 {"tool_calls":...} 输出在 thinking 通道时,工具调用可能漏检。
  3. 工具名大小写不一致(如 Bash / bash)会导致策略过滤误拒绝。

本次改动

  1. Claude 工具定义提取兼容增强
    支持从以下路径提取工具元信息(name/description/schema):
  • tools[].input_schema(Claude 原生)
  • tools[].parameters(扁平)
  • tools[].function.parameters(OpenAI 风格)
  1. 工具调用解析增加 fallback
    在未从 finalText 命中 tool_calls 时,回退解析 finalThinking:
  • 非流式:internal/format/claude/render.go
  • 流式:internal/adapter/claude/stream_runtime_finalize.go
  1. 工具名匹配策略改进
    tool_calls 过滤改为大小写不敏感匹配,并回写为声明时 canonical 工具名,减少 Bash/bash 场景下的误拒绝。

影响范围

  • internal/adapter/claude/handler_utils.go
  • internal/adapter/claude/stream_runtime_finalize.go
  • internal/format/claude/render.go
  • internal/util/toolcalls_parse.go
  • 对应测试文件:
    • internal/adapter/claude/handler_util_test.go
    • internal/adapter/claude/handler_stream_test.go
    • internal/format/claude/render_test.go(新增)
    • internal/util/toolcalls_test.go

验证

已完成以下回归并通过:

  1. Claude 原生 tools[].input_schema:返回 tool_use,stop_reason=tool_use
  2. OpenAI 风格 tools[].function.parameters:返回 tool_use,stop_reason=tool_use
  3. Bash/bash 大小写场景:返回 tool_use,无原始 {"tool_calls":...} 文本泄漏

测试命令:

GOCACHE=/tmp/go-build go test ./internal/util ./internal/adapter/claude ./internal/format/claude

风险与兼容性

  1. 扩大了工具 schema 输入兼容面,但仍受声明工具白名单约束。
  2. thinking 回退可能提升边缘识别概率;策略过滤仍保留,未放宽未声明工具执行边界。

- Claude 工具定义兼容 input_schema 与 function.parameters

- tool_calls 解析增加 thinking 回退与大小写无关工具名匹配

- 补充 claude/util 相关回归测试
@vercel

vercel Bot commented Feb 25, 2026

Copy link
Copy Markdown

@BigUncle is attempting to deploy a commit to the cjack's projects Team on Vercel.

A member of the Team first needs to authorize it.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 255feb2e65

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread internal/format/claude/render.go Resolved
- only parse tool_calls from thinking when finalText is empty

- apply the same guard in stream runtime finalizer

- add regression tests for non-stream and stream paths
@vercel

vercel Bot commented Feb 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
ds2api Ready Ready Preview, Comment Feb 27, 2026 3:08pm

@CJackHwang
CJackHwang merged commit 3f106ac into CJackHwang:main Feb 27, 2026
3 checks passed
@BigUncle
BigUncle deleted the fix/claude-toolcall branch March 8, 2026 14:38

This branch was successfully deployed

1 active deployment
Preview — d3b5493d Deployed Feb 27, 2026 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants