Skip to content

Extract AnthropicClient from AnthropicAgent#233

Merged
shellicar merged 1 commit intomainfrom
feature/sdk-refactor-1-extract-client
Apr 9, 2026
Merged

Extract AnthropicClient from AnthropicAgent#233
shellicar merged 1 commit intomainfrom
feature/sdk-refactor-1-extract-client

Conversation

@shellicar
Copy link
Copy Markdown
Owner

First step of the SDK refactor series in #232. Splitting the refactor into small PRs so each one can land or be reverted on its own.

Why

AnthropicAgent's constructor was doing four things: resolving auth, wiring the token-refreshing HTTP client, wrapping that client in an AnthropicMessageStreamer, and holding the Conversation. Only the last is an agent concern. Auth, transport, and the stream protocol are transport concerns, and they belong in a dedicated client.

What

  • New packages/claude-sdk/src/private/AnthropicClient.ts that extends IMessageStreamer directly and owns auth + token refresh + HTTP transport.
  • AnthropicAgent now composes an IMessageStreamer (abstract type, not the concrete client). The constructor builds an AnthropicClient internally and stores it as the streamer.
  • The previous AnthropicMessageStreamer class was a one-method passthrough, so it is removed rather than kept as a second layer. IMessageStreamer (the abstract class) remains unchanged.
  • IMessageStreamer stays in MessageStreamer.ts for this PR. Moving it into interfaces.ts is a separate filename churn I did not want mixed in here.

Decisions

  • Composition over inheritance for the agent/streamer relationship. The agent holds an IMessageStreamer, not an AnthropicClient. This keeps the FakeMessageStreamer contract used by AgentRun.spec.ts working without touching any test imports.
  • No public API change. The agent's external shape is identical; all construction paths go through the same code.
  • No runtime behaviour change. Same headers, same auth flow, same streaming call.

Verification

  • pnpm --filter @shellicar/claude-sdk type-check: clean
  • pnpm test: 981/981 passing (core 5, sdk 78, sdk-tools 233, sdk-cli 426, cli 239)
  • pnpm build: 5/5 packages
  • pnpm biome check --diagnostic-level=error: 319 files, no fixes needed
  • Changelog entry validated

Step 1 of 6 for #232.

First step of the SDK refactor series tracked in #232. Splitting the
refactor into small, independently reviewable PRs so each step can land
(or be reverted) on its own.

Before this change, AnthropicAgent's constructor did four things: it
resolved auth, wired up the token-refreshing HTTP client, wrapped that
client in an AnthropicMessageStreamer, and held the Conversation. Only
the last one is actually an agent concern. Auth, transport, and stream
protocol are a transport concern.

This extracts the first three into a new private AnthropicClient that
extends IMessageStreamer directly. The previous AnthropicMessageStreamer
wrapper class was a one-method passthrough, so it is removed rather
than kept around as a second layer.

AnthropicAgent now holds an IMessageStreamer (the abstract type), not
the concrete client. Composition over inheritance. The constructor
builds an AnthropicClient internally and stores it as the streamer.
This preserves the FakeMessageStreamer contract used by AgentRun.spec.ts
without touching any test imports.

IMessageStreamer stays in MessageStreamer.ts for this PR. Moving it
into interfaces.ts is a separate filename churn I did not want mixed
into this commit.

No public API or runtime behaviour changes. The agent's external shape
is identical; all construction paths go through the same code.

Verification:
- pnpm --filter @shellicar/claude-sdk type-check: clean
- pnpm test: 981/981 passing (core 5, sdk 78, sdk-tools 233, sdk-cli 426, cli 239)
- pnpm build: 5/5 packages
- pnpm biome check --diagnostic-level=error: 319 files, no fixes needed
- changelog entry validated

Step 1 of 6 for #232.
@shellicar shellicar added this to the 1.0 milestone Apr 9, 2026
@shellicar shellicar added enhancement New feature or request pkg: claude-sdk The agent SDK labels Apr 9, 2026
@shellicar shellicar self-assigned this Apr 9, 2026
@shellicar shellicar added enhancement New feature or request pkg: claude-sdk The agent SDK labels Apr 9, 2026
@shellicar shellicar requested a review from bananabot9000 April 9, 2026 12:40
@shellicar shellicar enabled auto-merge (squash) April 9, 2026 12:40
Copy link
Copy Markdown
Collaborator

@bananabot9000 bananabot9000 left a comment

Choose a reason for hiding this comment

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

Clean extraction. Composition over inheritance preserved -- agent depends on IMessageStreamer, not the concrete AnthropicClient. Test contract untouched. Scope discipline excellent (step 1 of 6, nothing beyond auth/transport/streaming moved). Session log is a solid resumable artifact for future steps.

LGTM 🍌

@shellicar shellicar merged commit 45b9a39 into main Apr 9, 2026
4 checks passed
@shellicar shellicar deleted the feature/sdk-refactor-1-extract-client branch April 9, 2026 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request pkg: claude-sdk The agent SDK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants