Repository navigation
fix(anthropic): 兼容 messages 里的 system role,转为顶层 system 参数 - #13
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
OpenAI 风格客户端会把系统提示词放在
messages: [{ role: "system", content: ... }]里。Anthropic Messages API 不接受 messages 中的 system role,会直接报错并提示改用顶层system参数(use the top-level 'system' parameter for the initial system prompt),请求到不了模型。改动
在 anthropic provider 的
prepareRequest转发前归一化(src/providers/anthropic.ts新增hoistSystemRoleMessages):role: "system"的消息摘出(不只是首条,中间位置的同样会让上游报错),messages 里不再留 system rolesystem: "...":多条按顺序以空行连接;已有顶层 system 为字符串时拼在其后,为 block 数组时以 text block 追加cache_control保留(此时 system 以 block 数组形式发出)systemPrompt注入和claudeCodeCompat搬运之前:一处归一,两处受益——开启 compat 时摘出的 system 也会被搬进首条 user 消息OpenAI 上游不受影响(system role 是其原生格式,未改 openai provider)。
测试
新增
test/anthropic-system-role-compat.test.ts,9 个用例覆盖:首条/多条/中间位置的 system role、与已有顶层 system(字符串/数组)的合并、block content 与 cache_control、空内容、无 system role 时不动、claudeCodeCompat 联动。本地验证:新用例 9/9 通过;连同已有的 anthropic system 搬运测试共 16/16 通过。全量
bun test117 pass / 7 fail,7 个 fail 为改动前就存在的TEST_DATABASE_URL is required环境问题(基线同样 7 fail)。