refactor: adopt ai-kit's tryChain + createHealthTracker - #164
Merged
Conversation
botsmann's own generateWithBestProvider/getProviderChain and llm-health.ts
were the source this session extracted ai-kit v0.5.0's tryChain and
createHealthTracker from. This adopts that package for real, closing the
gap flagged by a fleet-wide audit: botsmann had ai-kit installed for model
lists only, while the actual retry/health logic stayed hand-rolled here.
- generateWithBestProvider now builds one flat provider+model chain via
ai-kit's usableChain/freeChain and walks it with tryChain, instead of
two separate hand-rolled loops (provider here, model inside
generateWithGroq/generateWithOpenRouter). Ollama stays a special-cased
first check -- its availability is a live ping, not an API key, so it
does not fit ai-kit's Provider shape.
- generateWithGroq/generateWithOpenRouter are unchanged in behavior,
refactored into thin loops over new callGroqModel/callOpenRouterModel
single-shot primitives -- the same primitives generateWithBestProvider's
chain walk now calls directly, so there is exactly one fetch
implementation per vendor, not two.
- llm-health.ts keeps its exact five-function API (every route and test
still imports recordLLMSuccess/recordLLMFailure/getLLMHealth/
resetLLMHealth unchanged) but now wraps ai-kit's createHealthTracker
instead of hand-rolled module state.
One test's assertion on the aggregate-failure error wording updated
("provider(s)" -> "link(s)") to match ai-kit's more precise per-link
failure report; no other test needed changes. Full verify green: format,
lint, typecheck, 272 tests, production build.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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
generateWithBestProvidernow builds one flat provider+model chain viaai-kit'susableChain/freeChainand walks it withtryChain, replacing two separate hand-rolled retry loops (provider here, model insidegenerateWithGroq/generateWithOpenRouter). Ollama stays a special-cased first check — its availability is a live ping, not an API key, so it doesn't fitai-kit'sProvidershape.generateWithGroq/generateWithOpenRouterare behaviorally unchanged, refactored into thin loops over newcallGroqModel/callOpenRouterModelsingle-shot primitives — the same primitives the chain walk calls directly, so there's one fetch implementation per vendor, not two.llm-health.tskeeps its exact five-function API (every route/test still importsrecordLLMSuccess/recordLLMFailure/getLLMHealth/resetLLMHealthunchanged) but now wrapsai-kit'screateHealthTrackerinstead of hand-rolled module state.ai-kitto v0.5.0 — the package this session extractedtryChain/createHealthTrackerinto, generalized from this exact code.This closes a gap a fleet-wide AI-tooling audit flagged: botsmann had
ai-kitinstalled for model lists only, while the retry/health logic that matters (the thing that actually caused a production outage on 2026-08-28) stayed hand-rolled here.Test plan
npm run verifygreen locally: format, lint, typecheck, 272 tests (38 in the affected files), production buildprovider(s)→link(s)) to matchai-kit's more precise per-link failure report — no other test needed changes