Skip to content

feat(gateway): add merge-consecutive-same-role-messages upstream flag - #804

Open
yyyr-p wants to merge 5 commits into
Menci:mainfrom
yyyr-p:feat/merge-consecutive-same-role-messages
Open

yyyr-p wants to merge 5 commits into
Menci:mainfrom
yyyr-p:feat/merge-consecutive-same-role-messages

Conversation

@yyyr-p

@yyyr-p yyyr-p commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds an opt-in upstream feature flag merge-consecutive-same-role-messages (default false across all upstreams) for upstreams that require strict role alternation and reject adjacent messages with the same role.

Behavior

Runs inside withRoleCompatibilityApplied after role-rewrite flags (such as rewrite-all-system-to-user, rewrite-system-to-developer, rewrite-developer-to-system, and rewrite-mid-conv-system-to-user) so newly adjacent messages produced by rewrites are also merged:

  • Anthropic Messages (packages/gateway/src/data-plane/chat/anthropic-messages/interceptors/apply-role-compatibility.ts): merges consecutive user and content-bearing system messages (\n\n for string + string; concatenating blocks when either side is an array). Leaves assistant messages and directive-only system messages carrying output_config separate.
  • OpenAI Chat Completions (packages/gateway/src/data-plane/chat/openai-chat-completions/interceptors/apply-role-compatibility.ts): merges consecutive user, developer, and system messages sharing the same name. Leaves assistant messages and distinct name boundaries separate.
  • OpenAI Responses (packages/gateway/src/data-plane/chat/openai-responses/interceptors/apply-role-compatibility.ts): merges consecutive message items with role user, developer, or system and matching phase. Leaves assistant messages and non-message items (e.g. function_call) separate.
  • Gemini GenerateContent (packages/gateway/src/data-plane/chat/gemini-generate-content/interceptors/apply-role-compatibility.ts): merges consecutive user (including omitted role), developer, and system contents entries by concatenating parts. Leaves model and assistant turns separate.

Verification

  • Typechecked all 10 affected workspace packages (@floway-dev/upstream, all 7 upstream provider packages, @floway-dev/gateway, and @floway-dev/web).
  • Ran targeted ESLint across all 19 changed files and Vitest across 6 test files (41 tests passing), covering each protocol's merge semantics, non-merge boundaries (assistant / model / output_config / name / non-message items), and composition with role-rewrite flags.

yyyr-p and others added 5 commits December 1, 2026 19:19
* Add upstream system-to-user role compatibility

Gate the rewrite behind rewrite-all-system-to-user. Preserve the original system and developer roles in <system_instruction> and <developer_instruction> tags, and declare those tags in the leading system instruction when needed.

* Send empty leading system instructions without role tags

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…reams (Menci#802)

Third-party relays and Claude Code pool resellers commonly enforce
Claude Code client checks: User-Agent, X-Stainless-*, x-app,
anthropic-beta, the billing header, metadata.user_id, and the identity
system blocks. The wire mimicry that satisfies those checks lived only
inside @floway-dev/upstream-claude-code, so a Custom upstream layered on
top of such a relay had no way to reuse it.

Move the OAuth-independent Claude Code wire profile into
@floway-dev/upstream/client-profiles: request detection, the pinned
header surface, the inbound header allowlist, the Anthropic Messages
mimicry chain (backfill required fields, synthesize metadata.user_id,
hoist caller system text, inject the billing, identity, and cached
template blocks), and the per-profile default flags. The Claude Code
subscription provider delegates to the shared layer and keeps OAuth,
quota, and account state local.

Expose the profile as clientProfile: 'claude-code' on Custom upstreams,
validated in provider config and the control-plane schema, preserved on
serialization, and clearable through PATCH. Genuine Claude Code-shaped
Messages requests pass through with their system blocks, anthropic-beta
opt-ins, and metadata intact. Other requests run the mimicry chain,
merge caller-requested beta tokens without duplicates, keep
ingressHeadersRules override precedence, and do not leak
profile-admitted headers into non-Messages protocols. The profile layers
strip-billing-attribution: false and
rewrite-disallowed-mid-conv-system-to-user: true beneath operator and
model overrides; enabling strip-billing-attribution removes the billing
block and claude-code-20250219 token. Cached auto-model flags are
recomputed when the profile changes.

The dashboard gains a Client profile selector with en and zh-Hans
strings, a warning when the profile is set without the Anthropic
Messages endpoint, and profile-aware defaults in both flag editors.
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…erve Responses status boundary

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…mpatibility.ts

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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.

1 participant