feat(ai-proxy): add mergeConsecutiveMessages option to merge consecutive same-role messages (#3598)

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
澄潭
2026-03-16 14:28:17 +08:00
committed by GitHub
parent 68d6090e36
commit f1e305844e
4 changed files with 191 additions and 3 deletions

View File

@@ -462,6 +462,9 @@ type ProviderConfig struct {
// @Title zh-CN 智谱AI Code Plan 模式
// @Description zh-CN 仅适用于智谱AI服务。启用后将使用 /api/coding/paas/v4/chat/completions 接口
zhipuCodePlanMode bool `required:"false" yaml:"zhipuCodePlanMode" json:"zhipuCodePlanMode"`
// @Title zh-CN 合并连续同角色消息
// @Description zh-CN 开启后,若请求的 messages 中存在连续的同角色消息(如连续两条 user 消息将其内容合并为一条以满足要求严格轮流交替user→assistant→user→...)的模型服务商的要求。
mergeConsecutiveMessages bool `required:"false" yaml:"mergeConsecutiveMessages" json:"mergeConsecutiveMessages"`
}
func (c *ProviderConfig) GetId() string {
@@ -681,6 +684,7 @@ func (c *ProviderConfig) FromJson(json gjson.Result) {
c.contextCleanupCommands = append(c.contextCleanupCommands, cmd.String())
}
}
c.mergeConsecutiveMessages = json.Get("mergeConsecutiveMessages").Bool()
}
func (c *ProviderConfig) Validate() error {
@@ -1120,6 +1124,17 @@ func (c *ProviderConfig) handleRequestBody(
}
}
// merge consecutive same-role messages for providers that require strict role alternation
if apiName == ApiNameChatCompletion && c.mergeConsecutiveMessages {
body, err = mergeConsecutiveMessages(body)
if err != nil {
log.Warnf("[mergeConsecutiveMessages] failed to merge messages: %v", err)
err = nil
} else {
log.Debugf("[mergeConsecutiveMessages] merged consecutive messages for provider: %s", c.typ)
}
}
// convert developer role to system role for providers that don't support it
if apiName == ApiNameChatCompletion && !isDeveloperRoleSupported(c.typ) {
body, err = convertDeveloperRoleToSystem(body)