Skip to content

fix(daemon): forward original request path and headers to upstream#191

Open
sh1ftred wants to merge 1 commit into
sdk-integrationfrom
fix-forward-path-and-headers
Open

fix(daemon): forward original request path and headers to upstream#191
sh1ftred wants to merge 1 commit into
sdk-integrationfrom
fix-forward-path-and-headers

Conversation

@sh1ftred
Copy link
Copy Markdown
Contributor

Problem

Claude Code sends requests to POST /v1/messages (Anthropic protocol) but the
routstr daemon was silently rewriting the upstream path to /v1/chat/completions
because routeRequests() was called without passing the incoming url.pathname.
The SDK defaults to /v1/chat/completions when path is not provided.

This caused Claude Code to receive OpenAI-style SSE (chat.completion.chunk)
instead of Anthropic SSE (message_start, content_block_delta, etc.) and
fail with:

Stream completed without receiving message_start event - triggering non-streaming fallback

Fix

  1. sdk/routeRequests.ts — add optional headers?: Record<string, string>
    to RouteRequestOptions and thread it through resolveRouteRequestContext()
    into client.routeRequest().

  2. scripts/routstr-daemon.ts — add toForwardHeaders() helper that strips
    hop-by-hop headers (host, connection, content-length, transfer-encoding, etc.)
    and passes both path: url.pathname and the sanitized headers into
    routeRequests().

Result

The daemon now forwards the exact same path and headers the client sent, so:

  • /v1/messages stays /v1/messages upstream
  • anthropic-version and other Anthropic-specific headers are preserved
  • Streaming uses correct Anthropic SSE event sequence

This makes port 8009 behave identically to a transparent pass-through proxy
from Claude Code's perspective.

Before this change, routeRequests() was called without the incoming request
path, causing the SDK to default to /v1/chat/completions even when Claude Code
was calling /v1/messages. This protocol mismatch made Claude Code fail with:

    Stream completed without receiving message_start event

The fix threads the original url.pathname and sanitized request headers
(hop-by-hop headers stripped) through resolveRouteRequestContext() and into
client.routeRequest(), so:

- /v1/messages is forwarded upstream as /v1/messages (not rewritten)
- Anthropic-specific headers like anthropic-version are forwarded upstream
- Streaming responses use the correct SSE event format (message_start, etc.)

This makes the routstr daemon on port 8009 behave identically to a transparent
pass-through proxy from Claude Code's perspective.
@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying routstr-chat with  Cloudflare Pages  Cloudflare Pages

Latest commit: fb48c08
Status:🚫  Build failed.

View logs

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