Skip to content

refactor(adapters): typed v3 → v2.5 registry + central dispatch - #1143

Merged
bokelley merged 2 commits into
mainfrom
bokelley/v2.5-adapter-registry
May 1, 2026
Merged

bokelley merged 2 commits into
mainfrom
bokelley/v2.5-adapter-registry

Conversation

@bokelley

@bokelley bokelley commented May 1, 2026

Copy link
Copy Markdown
Contributor

Stacked on #1134 (TypeScript codegen for v2.5). Will retarget to main after #1134 merges.

Consolidates the v3 → v2.5 adapter dispatch into a per-tool registry pattern. Each tool that needs version translation has its own AdapterPair<V3Req, V25Req, V25Res, V3Res> module under src/lib/adapters/v3-to-v2-5/. SingleAgentClient.adaptRequestForServerVersion and normalizeResponseToV3 dispatch through getV3ToV25Adapter(taskName) instead of carrying tool-specific switch arms.

Why

Per the user's "kludgey code" framing, the four request adapters and four response normalizers were scattered across three files (pricing-adapter.ts, creative-adapter.ts, sync-creatives-adapter.ts) with two separate switch statements in SingleAgentClient dispatching to them. Adding v2.6 or v3.1 would mean editing all three files plus both switch arms.

The registry collapses that to "drop a per-tool file with a typed AdapterPair, register it once" — a clean (from, to, tool) triple that matches the compatibility-matrix mental model. The TypeScript types (#1134) make adapter signatures compile-time-checked: (req: V3.GetProductsRequest) => V25.GetProductsRequest.

What's in this PR

src/lib/adapters/v3-to-v2-5/
├── types.ts                    # AdapterPair interface
├── index.ts                    # registry + getV3ToV25Adapter()
├── get_products.ts             # 6 per-tool modules,
├── create_media_buy.ts         # each wrapping the existing
├── update_media_buy.ts         # utils/*-adapter.ts helpers
├── sync_creatives.ts           # unchanged
├── list_creative_formats.ts
└── preview_creative.ts

SingleAgentClient.adaptRequestForServerVersion (lines 1216–1226) and normalizeResponseToV3 (lines 1307–1325) lose their switch statements; both now dispatch through getV3ToV25Adapter(taskType).

What's NOT in this PR (deliberately)

  • The underlying helpers stay in utils/*-adapter.ts — same code, no rename. The registry is a wrapper layer for centralized dispatch, not a migration of the helper logic. Per-tool refactors land independently (e.g. #1116 rewrites adaptSyncCreativesRequestForV2 as a manifest flattener under the new pattern).
  • Public API surface unchanged — adaptGetProductsRequestForV2 etc. are still exported from utils/*-adapter.ts. Downstream callers using them directly are unaffected.

Test plan

  • Regression suite (test/lib/v3-to-v2-5-registry.test.js, 13 tests): every registered pair's adaptRequest produces the same output as the underlying utils/* helper for the same input. Same for normalizeResponse. Wire-level behavior is identical.
  • Coverage: listV3ToV25AdapterTools() returns all 6 tools. getV3ToV25Adapter('not_a_real_tool') returns undefined.
  • npm run test:lib — 5488 pass / 7 pre-existing skipped / 0 fail.

Compat-matrix playbook (preview)

Adding v2.6 in the future:

  1. npm run sync-schemas:v2.6 → schemas/cache/v2.6/
  2. npm run generate-types:v2.6 → src/lib/types/v2-6/tools.generated.ts
  3. New directory src/lib/adapters/v3-to-v2-6/ mirroring this PR's shape
  4. Dispatch table extended to (serverVersion, toolName) lookup
  5. New conformance fixtures in adapter-v2-6-conformance.test.js

The whole point of this refactor is making step 3 a copy-paste-and-rename of the v2-5 directory rather than a rewrite of three files plus two switch statements.

🤖 Generated with Claude Code

bokelley and others added 2 commits May 1, 2026 05:12
Consolidates the v3 → v2.5 adapter dispatch into a per-tool registry
pattern. Each tool that needs version translation now has its own
`AdapterPair<V3Req, V25Req, V25Res, V3Res>` module under
`src/lib/adapters/v3-to-v2-5/`. `SingleAgentClient.adaptRequestForServerVersion`
and `normalizeResponseToV3` dispatch through `getV3ToV25Adapter(taskName)`
instead of carrying tool-specific switch arms.

Six pairs: get_products, create_media_buy, update_media_buy,
sync_creatives, list_creative_formats, preview_creative. The pairs wrap
the existing scattered helpers unchanged — wire-level behavior is
identical and pinned by a regression suite that diffs registry output
vs direct-helper output for every pair. The underlying logic stays in
utils/*-adapter.ts files until each pair gets a focused per-tool
refactor (e.g. #1116 for sync_creatives manifest flatten).

Adding v2.6 / v3.1 in the future follows the same shape: a sibling
v3-to-v2-6/ directory with its own per-tool modules, then a dispatch
table keyed by (serverVersion, toolName). Today's single-pair surface
is the smallest version of that pattern that lets v3 buyers talk to
v2.5 sellers without breaking, plus the type endpoints from
@adcp/sdk/types/v2-5 so adapter signatures are compile-time-checked.

No public API change. utils/*-adapter.ts exports stay (downstream
callers using them directly are unchanged). The registry is a
centralized dispatch wrapper, not a migration of the helper code.

5488 lib tests pass / 7 pre-existing skipped / 0 fail.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Two review-driven refinements to the adapter registry:

1. Rename `src/lib/adapters/v3-to-v2-5/` → `src/lib/adapters/legacy/v2-5/`.
   Per ad-tech-product review: real ad-tech compat layers carry N=1
   active legacy shim with a deprecation runway (OpenRTB, Prebid, GAM
   all behave this way). Encoding `<sdk-version>-to-<seller-version>/`
   commits the codebase to a matrix nobody will staff. `legacy/v2-5/`
   reflects "exceptional, time-boxed compat" — when the SDK pin moves
   from v3 to v4, this directory continues to hold the v2.5 shim and a
   sibling `legacy/v3/` joins for v3 sellers.

   getV3ToV25Adapter → getV25Adapter (sdk-version implicit)
   listV3ToV25AdapterTools → listV25AdapterTools

2. Drop `SingleAgentClient.ts` imports that became unused after the
   dispatch refactor (adaptCreateMediaBuyRequestForV2,
   adaptUpdateMediaBuyRequestForV2, normalizeMediaBuyResponse,
   adaptSyncCreativesRequestForV2, normalizeFormatsResponse,
   normalizePreviewCreativeResponse, normalizeGetProductsResponse,
   adaptGetProductsRequestForV2). Code-reviewer flagged as a
   noUnusedLocals failure waiting to happen. The helpers are still
   exported from utils/*-adapter.ts for downstream callers using them
   directly — only the dispatch-site imports go away.

5565 lib tests pass / 7 pre-existing skipped / 0 fail.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@bokelley
bokelley force-pushed the bokelley/v2.5-adapter-registry branch from e7c9ae3 to eefbacb Compare May 1, 2026 09:12
@bokelley
bokelley changed the base branch from bokelley/v2.5-typescript-codegen to main May 1, 2026 09:13
@bokelley
bokelley merged commit 73e722a into main May 1, 2026
4 checks passed
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