Skip to content

feat: create-messages-native.ts — Anthropic pass-through service client #39

@HXYerror

Description

@HXYerror

Part of #38.

Background

api.githubcopilot.com/v1/messages natively accepts Anthropic Messages API requests for Claude 4.5+ models. We need a new service module that forwards requests directly, bypassing the OpenAI translation layer.

Goal

src/services/copilot/create-messages-native.ts — sends an Anthropic Messages payload straight to ${copilotBaseUrl(state)}/v1/messages and returns the raw Anthropic response.

Tasks

  • Create src/services/copilot/create-messages-native.ts:
    • Accept AnthropicMessagesPayload (from anthropic-types.ts) as input — no transformation
    • Build Copilot auth headers the same way other service modules do (Authorization Bearer, copilot-integration-id, editor-version, user-agent, x-github-api-version, x-request-id, x-vscode-user-agent-library-version)
    • Forward client's anthropic-version header if present (default: "2023-06-01")
    • POST to ${copilotBaseUrl(state)}/v1/messages
    • Non-streaming: return raw parsed AnthropicMessagesResponse JSON
    • Streaming: return the raw SSE byte stream to be piped directly to the client (no re-parsing needed — Copilot SSE events are already in Anthropic format)
  • Add NATIVE_ANTHROPIC_MODELS constant (initial hardcoded set; replaced by dynamic detection in feat: Dynamic NATIVE_ANTHROPIC_MODELS detection from /models endpoint (vendor-based) #42):
    export const NATIVE_ANTHROPIC_MODELS = new Set([
      "claude-sonnet-4.5", "claude-sonnet-4.6",
      "claude-opus-4.5",   "claude-opus-4.6", "claude-opus-4.6-1m",
      "claude-haiku-4.5",
    ])
  • Error handling: surface upstream errors using existing HTTPError class (src/lib/error.ts)
  • Honor state.showToken for debug logging (same pattern as other service modules)

Upstream SSE events returned (verified by live test)

event: message_start
event: content_block_start
event: content_block_delta   (type: text_delta | thinking_delta | signature_delta)
event: content_block_stop
event: message_delta         (includes copilot_usage + amazon-bedrock-invocationMetrics)
event: message_stop
data: [DONE]

Note: upstream returns data: [DONE] at the end (same as OpenAI). Pass it through — Anthropic SDK clients ignore it.

Acceptance criteria

  • createMessagesNative({model: "claude-sonnet-4.5", ...}) returns a valid AnthropicMessagesResponse with content array
  • When stream: true, the client receives SSE events with correct event: prefix lines
  • Thinking blocks with signature are preserved byte-exact in pass-through
  • No upstream call made for non-native models (checked by caller via NATIVE_ANTHROPIC_MODELS)

File pointers

  • New: src/services/copilot/create-messages-native.ts
  • Reference: src/services/copilot/create-chat-completions.ts (header pattern)
  • Touch: src/routes/messages/anthropic-types.ts (may need signature field added to AnthropicThinkingBlock)

Metadata

Metadata

Assignees

No one assigned

    Labels

    anthropicAnthropic Messages API compatibility

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions