Skip to content

refactor: eliminate duplicate utility types, dead files, and shared parser runtime#977

Open
apoc wants to merge 3 commits intoanthropics:mainfrom
apoc:refactor/dedup-trivial-to-medium
Open

refactor: eliminate duplicate utility types, dead files, and shared parser runtime#977
apoc wants to merge 3 commits intoanthropics:mainfrom
apoc:refactor/dedup-trivial-to-medium

Conversation

@apoc
Copy link
Copy Markdown

@apoc apoc commented Apr 6, 2026

What

Structural cleanup to eliminate duplicate code identified by static analysis (nucleus find_duplicate_code). All changes are behaviour-preserving — verified by 37/37 test suites, 584 tests.

Changes

Commit 1 — Delete dead files with zero importers (96a499b)

  • src/internal/stream-utils.ts: single export was a verbatim copy of shims.ts. Zero importers.
  • src/lib/tools/ToolRunner.ts: orphaned predecessor of BetaToolRunner.ts, missing all features added since. Zero importers.

Commit 2 — Consolidate duplicate utility types and functions (cad969a)

  • src/lib/type-utils.ts (new): canonical home for Simplify<T> (4 file-private copies → 1) and Promisable<T> (re-exported from BetaRunnableTool.ts preserving existing import paths)
  • NoInfer<T>: remove local workaround from both json-schema.ts files — TS 5.4+ ships it natively; project is on 5.8.3
  • type Bytes in core/streaming.ts: import from internal/decoders/line.ts where it was already the canonical export
  • isAbortError: re-export from src/core/error.ts; packages/bedrock-sdk replaces its byte-for-byte local copy with import { isAbortError } from '@anthropic-ai/sdk/error'

Commit 3 — Extract shared parser runtime (a38687f)

  • src/lib/core-parser.ts (new): parseAccumulatedFormat(format, content) — the 18-line parse step (json_schema check → format.parse()JSON.parse() fallback → AnthropicError on failure) that was duplicated identically in parser.ts and beta-parser.ts. Both files now import and delegate to it.
  • Type-level logic (conditional types, deprecated output_format shim, .parsed getter) stays in each file — intentionally different between stable and beta paths.

Tests

37/37 suites, 584 passed, 1 skipped (pre-existing).

apoc added 3 commits April 6, 2026 11:50
- src/internal/stream-utils.ts: single export (ReadableStreamToAsyncIterable)
  was a verbatim duplicate of the same symbol in shims.ts. Zero importers
  confirmed via nucleus dependency graph.
- src/lib/tools/ToolRunner.ts: orphaned predecessor of BetaToolRunner.ts,
  exported BetaToolRunner under the same name but was missing all features
  added since (request options, x-stainless-helper header, compactionControl).
  Zero live importers confirmed.
Tier 2 — type deduplication:
- Add src/lib/type-utils.ts as the canonical home for Simplify<T> and
  Promisable<T>; remove four file-private Simplify copies from parser.ts,
  beta-parser.ts, BetaToolRunner.ts; BetaRunnableTool.ts re-exports
  Promisable from type-utils preserving the existing import path for its
  three consumers (helpers/beta/{zod,json-schema,memory}.ts)
- Remove local NoInfer<T> workaround from helpers/json-schema.ts and
  helpers/beta/json-schema.ts — native NoInfer<T> ships in TS 5.4+;
  project uses 5.8.3
- Import type Bytes from internal/decoders/line.ts in core/streaming.ts
  instead of redefining it; the canonical export was already there

Tier 3 — cross-package consolidation:
- Re-export isAbortError from src/core/error.ts (via ../internal/errors)
  so platform packages can import it from the stable public surface
- packages/bedrock-sdk: replace local isAbortError definition (byte-for-
  byte copy of the canonical one) with import from @anthropic-ai/sdk/error;
  merge the two separate @anthropic-ai/sdk/error import statements
Both parser.ts and beta-parser.ts contained an identical parseOutputFormat
implementation (18 lines each): resolve the format object, check for
json_schema type, call format.parse() or JSON.parse(), throw AnthropicError
on failure.

Extract that shared runtime to src/lib/core-parser.ts as
parseAccumulatedFormat(format, content). Both parsers now import and
delegate to it, removing the AnthropicError import from each.

The type-level logic (conditional types, getOutputFormat, deprecated
output_format shim, .parsed getter) stays file-local — it is intentionally
different between the stable and beta paths and has zero runtime cost.

Behaviour preserved exactly: same error message, same fallback to
JSON.parse, same null return when format is absent or non-json_schema.
@apoc apoc requested a review from a team as a code owner April 6, 2026 09:59
@apoc apoc force-pushed the refactor/dedup-trivial-to-medium branch from a38687f to d4be30d Compare April 6, 2026 10:01
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