feat(api): introduce RequestConfigBuilder for SDK-agnostic abort signal support#2
Open
easonLiangWorldedtech wants to merge 5 commits into
Open
feat(api): introduce RequestConfigBuilder for SDK-agnostic abort signal support#2easonLiangWorldedtech wants to merge 5 commits into
easonLiangWorldedtech wants to merge 5 commits into
Conversation
cac1a1c to
c2dd146
Compare
…nfiguration Body: Implement generic request configuration builder with chainable methods (addAbortSignal, addHeaders, setOption), static factory methods (fromMetadata, mergeAbortSignals), and 40 unit tests.
… and multi-SDK examples
…ls early-abort - Fix README TOC: change #how-mergesignals-works to #how-mergeabortsignals-works to match the actual heading anchor - Simplify mergeAbortSignals: return primarySignal directly when it's already aborted instead of creating a new AbortController
…onfigBuilder (Zoo-Code-Org#615) - Add default empty object parameter to addHeaders() so calling with undefined no longer throws TypeError from Object.keys(undefined) - Reorder mergeAbortSignals to check primarySignal.aborted before allocating AbortController, preventing unnecessary controller creation
c2dd146 to
ba54c38
Compare
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.
Summary
This PR introduces
RequestConfigBuilder— a generic, SDK-agnostic request configuration builder that provides a fluent API for building type-safe request configurations. It serves as the second foundation for abort signal support in Zoo-Code, complementing #674 which wired theAbortSignalthrough the core metadata layer.Why this builder?
#674 successfully added
abortSignal?: AbortSignaltoApiHandlerCreateMessageMetadataand wired it fromTask.ts. However, each provider still manually constructs its request config by spreading{ signal: metadata?.abortSignal, headers: ... }— a repetitive pattern that:queryParams, Bedrock usesbody, Anthropic usesapiVersionThis builder solves all of that with a unified, chainable API:
Changes
src/api/providers/config-builder/request-config-builder.tssrc/api/providers/__tests__/request-config-builder.spec.tssrc/api/providers/config-builder/README.mdsrc/api/providers/index.tsRequestConfigBuilderfrom the providers barrelAPI Overview
Instance Methods (all chainable)
addAbortSignal(metadata?)— Extracts abort signal from metadata and adds it to configaddHeaders(headers)— Merges custom headers (empty objects skipped)setOption(key, value)— Type-safe single option settergetOption(key)— Get an option by keybuild()— Returns shallow copy for immutability;undefinedif no options setStatic Methods
fromMetadata(metadata?, extraOptions?)— Quick factory for simple signal + options scenariosmergeAbortSignals(primary, secondary?)— Combines two signals (abort when either fires)Design Principles
build()returns a shallow copy; constructor copies defaultOptionsTOptions extends Record<string, any>ensures compile-time correctness per SDKPros & Cons
✅ Pros
TOptions)build()returns shallow copyRelationship to other PRs
abortSignalto metadata interface and wired it through Task.ts. This PR builds on top of that foundation.Next Steps (after merge)
OpenAiRequestConfigBuilder,BedrockRequestConfigBuilder, etc.) with provider-specific methods likeaddPath(),addQueryParams(),setApiVersion()Related
Closes #434 (supersedes the manual approach with a reusable pattern)