Skip to content

feat: add transport ABC + AnthropicTransport wired to all paths#13366

Merged
teknium1 merged 1 commit into
mainfrom
feat/anthropic-transport
Apr 21, 2026
Merged

feat: add transport ABC + AnthropicTransport wired to all paths#13366
teknium1 merged 1 commit into
mainfrom
feat/anthropic-transport

Conversation

@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Summary

PR 3 of the provider transport refactor (PR 1: #12975, PR 2: #13073).

Adds the ProviderTransport ABC and the first concrete transport (AnthropicTransport), wired to all Anthropic code paths in run_agent.py. When this merges, every Anthropic format-conversion and normalization call goes through the transport — no dead code, no dormant abstraction.

What ships

agent/transports/base.py — ProviderTransport ABC

  • 4 abstract methods: convert_messages, convert_tools, build_kwargs, normalize_response
  • 3 optional hooks with defaults: validate_response, extract_cache_stats, map_finish_reason
  • Does NOT own: client lifecycle, streaming, credentials, prompt caching, interrupts

agent/transports/__init__.py — Transport registry

  • register_transport(api_mode, cls) / get_transport(api_mode)
  • Lazy discovery — _discover_transports() auto-imports transport modules on first call
  • Returns None for unregistered api_modes (graceful fallback for gradual migration)

agent/transports/anthropic.py — AnthropicTransport

  • Delegates to existing anthropic_adapter.py functions — zero duplicated logic
  • Auto-registers on import via register_transport('anthropic_messages', ...)

run_agent.py — All Anthropic paths wired (9 sites):

Site Method Lines
Main normalize loop normalize_response() 10775
Main kwargs builder build_kwargs() 6673
Response validation validate_response() 9366
Finish reason mapping map_finish_reason() 9534
Cache stats extraction extract_cache_stats() 9827
Truncation normalize normalize_response() 9565
Memory flush kwargs + normalize build_kwargs() + normalize_response() 7363, 7395
Iteration summary + retry build_kwargs() + normalize_response() 8465, 8498

Zero direct adapter imports remain for transport methods. Client lifecycle, streaming, auth stay on AIAgent.

What stays on agent (explicitly NOT in the transport)

  • Client construction (build_anthropic_client) — 9 sites
  • Credential refresh/swap — 3 sites
  • API call dispatch (_anthropic_messages_create) — 3 sites
  • Streaming (_call_anthropic) — 1 site
  • Runtime snapshots, auth 401 retry, policy decisions

Test plan

  • 20 new tests (ABC contract, registry, AnthropicTransport methods)
  • 359 anthropic-related tests pass (0 failures)
  • 13,995 total tests pass (79 pre-existing failures, fewer than main's 86)

Add ProviderTransport ABC (4 abstract methods: convert_messages,
convert_tools, build_kwargs, normalize_response) plus optional hooks
(validate_response, extract_cache_stats, map_finish_reason).

Add transport registry with lazy discovery — get_transport() auto-imports
transport modules on first call.

Add AnthropicTransport — delegates to existing anthropic_adapter.py
functions, wired to ALL Anthropic code paths in run_agent.py:
- Main normalize loop (L10775)
- Main build_kwargs (L6673)
- Response validation (L9366)
- Finish reason mapping (L9534)
- Cache stats extraction (L9827)
- Truncation normalize (L9565)
- Memory flush build_kwargs + normalize (L7363, L7395)
- Iteration-limit summary + retry (L8465, L8498)

Zero direct adapter imports remain for transport methods. Client lifecycle,
streaming, auth, and credential management stay on AIAgent.

20 new tests (ABC contract, registry, AnthropicTransport methods).
359 anthropic-related tests pass (0 failures).

PR 3 of the provider transport refactor.
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.

2 participants