refactor: eliminate duplicate utility types, dead files, and shared parser runtime#977
Open
apoc wants to merge 3 commits intoanthropics:mainfrom
Open
refactor: eliminate duplicate utility types, dead files, and shared parser runtime#977apoc wants to merge 3 commits intoanthropics:mainfrom
apoc wants to merge 3 commits intoanthropics:mainfrom
Conversation
- 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.
a38687f to
d4be30d
Compare
DepkaCZ
approved these changes
Apr 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 ofshims.ts. Zero importers.src/lib/tools/ToolRunner.ts: orphaned predecessor ofBetaToolRunner.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 forSimplify<T>(4 file-private copies → 1) andPromisable<T>(re-exported fromBetaRunnableTool.tspreserving existing import paths)NoInfer<T>: remove local workaround from bothjson-schema.tsfiles — TS 5.4+ ships it natively; project is on 5.8.3type Bytesincore/streaming.ts: import frominternal/decoders/line.tswhere it was already the canonical exportisAbortError: re-export fromsrc/core/error.ts;packages/bedrock-sdkreplaces its byte-for-byte local copy withimport { 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 →AnthropicErroron failure) that was duplicated identically inparser.tsandbeta-parser.ts. Both files now import and delegate to it.output_formatshim,.parsedgetter) stays in each file — intentionally different between stable and beta paths.Tests
37/37 suites, 584 passed, 1 skipped (pre-existing).