Skip to content

Refactor api-proxy request pipeline into focused modules#3876

Open
Copilot wants to merge 3 commits into
mainfrom
copilot/refactor-split-api-proxy-request
Open

Refactor api-proxy request pipeline into focused modules#3876
Copilot wants to merge 3 commits into
mainfrom
copilot/refactor-split-api-proxy-request

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 26, 2026

containers/api-proxy/proxy-request.js had grown into a multi-responsibility module (~764 LOC) combining deprecated-header learning, billing parsing, upstream response orchestration, and core proxy transport. This PR extracts those cohesive concerns into dedicated modules while preserving call sites and runtime behavior.

  • Module split: deprecated header learning

    • Added containers/api-proxy/deprecated-header-tracker.js.
    • Moved the in-memory deprecated-header cache + learning/strip cycle:
      • getDeprecatedValuesForHeader
      • maybeStripLearnedHeaderValues
      • parseDeprecatedHeaderFromBody
      • learnAndStripDeprecatedHeaderValue
    • Exposed resetDeprecatedHeaderValuesForTests and wired existing test reset export to it.
  • Module split: billing header parsing

    • Added containers/api-proxy/billing-headers.js.
    • Moved extractBillingHeaders into a dedicated utility module.
    • Kept export surface unchanged via proxy-request.js/server.js usage.
  • Module split: upstream response handling

    • Added containers/api-proxy/upstream-response.js.
    • Extracted:
      • handleUpstreamResponse
      • logUpstreamAuthError
      • logRequestCompletion
    • Implemented via createUpstreamResponseHandlers(...) to inject existing dependencies without behavior drift.
  • proxy-request.js trimmed to transport orchestration

    • Replaced inlined implementations with imports from the three new modules.
    • Kept proxyRequest, sendUpstreamRequest, buildRequestHeaders, and existing external exports/call paths intact.
// proxy-request.js
const {
  maybeStripLearnedHeaderValues,
  resetDeprecatedHeaderValuesForTests,
  parseDeprecatedHeaderFromBody,
  learnAndStripDeprecatedHeaderValue,
} = require('./deprecated-header-tracker');
const { extractBillingHeaders } = require('./billing-headers');
const { createUpstreamResponseHandlers } = require('./upstream-response');

const { handleUpstreamResponse } = createUpstreamResponseHandlers({ /* existing deps */ });

Copilot AI changed the title [WIP] Refactor proxy-request.js into focused modules Refactor api-proxy request pipeline into focused modules May 26, 2026
Copilot finished work on behalf of lpcox May 26, 2026 22:22
Copilot AI requested a review from lpcox May 26, 2026 22:22
@lpcox lpcox marked this pull request as ready for review May 26, 2026 22:40
Copilot AI review requested due to automatic review settings May 26, 2026 22:40
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 26, 2026

✅ Coverage Check Passed

Overall Coverage

Metric Base PR Delta
Lines 96.57% 96.62% 📈 +0.05%
Statements 96.44% 96.48% 📈 +0.04%
Functions 98.22% 98.22% ➡️ +0.00%
Branches 90.66% 90.70% 📈 +0.04%
📁 Per-file Coverage Changes (1 files)
File Lines (Before → After) Statements (Before → After)
src/config-writer.ts 89.3% → 90.9% (+1.65%) 89.3% → 90.9% (+1.65%)

Coverage comparison generated by scripts/ci/compare-coverage.ts

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the API proxy request pipeline by extracting billing parsing, deprecated header tracking, and upstream response handling into focused modules while preserving the existing proxy request surface.

Changes:

  • Added focused modules for billing headers, deprecated-header learning/stripping, and upstream response orchestration.
  • Rewired proxy-request.js to import those modules and keep existing exports intact.
  • Preserved the existing test-facing reset and billing helper re-export paths.
Show a summary per file
File Description
containers/api-proxy/proxy-request.js Replaces inlined helper implementations with module imports and wires the response handler factory.
containers/api-proxy/upstream-response.js Adds extracted upstream response logging, retry, streaming, token tracking, and completion handling.
containers/api-proxy/deprecated-header-tracker.js Adds extracted module-level deprecated header cache and learn/strip helpers.
containers/api-proxy/billing-headers.js Adds extracted billing/quota header parsing utility.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 4/4 changed files
  • Comments generated: 0

The api-proxy container crashes with 'Cannot find module ./deprecated-header-tracker'
because the file was not included in the Dockerfile COPY instruction.

Co-authored-by: Copilot <223556219+Copilot@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.

[Refactoring] Split containers/api-proxy/proxy-request.js into focused modules (follow-up to #3154)

3 participants