feat(ai): resilient shared OpenRouter client with retry and fallback#99
Merged
Conversation
Extract src/lib/ai/openrouter.ts as the single OpenRouter transport for enhance-notes, suggestions, and ask-series. Adds timeout, one retry, and a configurable model fallback (AI_MODEL_FALLBACK) so a slow or unavailable primary model no longer surfaces as a 502. Routes now record the model that actually answered and resolve the key via getOpenRouterApiKey. Wire the AI notes contract verifier and a new OpenRouter client test into CI; both were previously unenforced.
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
PR A of the AI 80:20 program. Extracts a single OpenRouter transport (
src/lib/ai/openrouter.ts) and routes all three AI features (enhance-notes, suggestions, ask-series) through it, adding cross-cutting reliability.Why
The three routes each carried a byte-identical
getOpenRouterDatawith no timeout, no retry, no fallback. A slow or unavailable model surfaced as a hard 502 (the class of bug we just patched). This is the highest-leverage reliability win across every AI feature.Changes
src/lib/ai/openrouter.ts(new):callOpenRouter({ apiKey, system, prompt })with a 30s timeout (AbortController), one retry, and ordered model fallback;getOpenRouterApiKey(). Returns the model that actually answered.model.ts:getAiModels()returns[AI_MODEL, AI_MODEL_FALLBACK](fallback defaults to the cheap model). Default stays cheap for testing; self-host already shipsclaude-sonnet-4-6.ai_model/ response.test:ai-client(node:test, 6 cases: success, model fallback, retry, total failure, key precedence, default models). Wired bothtest:ai-contractsandtest:ai-clientintoci.ymllint-and-typecheck job. The AI contract verifier was previously orphaned (not in CI); now enforced.AI_MODEL_FALLBACKin.env.exampleand the self-host env generator.Verification (local)
test:ai-client6/6,test:ai-contracts,test:query-contracts,test:runtime-config7/7,test:ci-workflows: passpnpm build(full typecheck): pass; eslint on changed files: cleanTDD
Client written test-first: failing test (module missing) → minimal implementation → green. Route refactor is behavior-preserving, locked by the contract verifier.
Next in program
PR B carry-over intelligence, PR C transcript paste, then Edge Functions infra PR. STT last.