Skip to content

refactor: adopt ai-kit's tryChain + createHealthTracker - #164

Merged
catomean merged 1 commit into
mainfrom
feat/ai-kit-chain-health
Aug 29, 2026
Merged

refactor: adopt ai-kit's tryChain + createHealthTracker#164
catomean merged 1 commit into
mainfrom
feat/ai-kit-chain-health

Conversation

@catomean

Copy link
Copy Markdown
Collaborator

Summary

  • generateWithBestProvider now builds one flat provider+model chain via ai-kit's usableChain/freeChain and walks it with tryChain, replacing two separate hand-rolled retry 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 doesn't fit ai-kit's Provider shape.
  • generateWithGroq/generateWithOpenRouter are behaviorally unchanged, refactored into thin loops over new callGroqModel/callOpenRouterModel single-shot primitives — the same primitives the chain walk calls directly, so there's one fetch implementation per vendor, not two.
  • llm-health.ts keeps its exact five-function API (every route/test still imports recordLLMSuccess/recordLLMFailure/getLLMHealth/resetLLMHealth unchanged) but now wraps ai-kit's createHealthTracker instead of hand-rolled module state.
  • Bumps ai-kit to v0.5.0 — the package this session extracted tryChain/createHealthTracker into, generalized from this exact code.

This closes a gap a fleet-wide AI-tooling audit flagged: botsmann had ai-kit installed 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 verify green locally: format, lint, typecheck, 272 tests (38 in the affected files), production build
  • One test's error-wording assertion updated (provider(s)link(s)) to match ai-kit's more precise per-link failure report — no other test needed changes
  • CI green on this PR

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>
@catomean
catomean merged commit 0d87be1 into main Aug 29, 2026
1 check passed
@catomean
catomean deleted the feat/ai-kit-chain-health branch August 29, 2026 08:05
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.

1 participant