Files
b5b2f967fe fix(ai): a total LLM outage was reporting itself as success (#151)
Found by driving the Lex demo in production: the chat answered "I'm sorry,
I encountered an error processing your request." Behind it, every AI feature
on the site is down and has been invisible.

The box has an invalid Groq key, no other provider key, and no Ollama:

  Groq API error: 401 (model openai/gpt-oss-120b)  {"code":"invalid_api_key"}
  Groq API error: 401 (model openai/gpt-oss-20b)   {"code":"invalid_api_key"}

Every endpoint answered HTTP 200 anyway:

  /api/demo/chat          {"success":true,"data":{"response":""}}
  /api/quick-chat         {"success":true,"data":{"response":"I'm having a moment..."}}
  /api/professional-chat  same
  /api/health             {"success":true,"data":{"status":"healthy"}}

Green everywhere, product dead. demo/chat was the worst of the three: its
catch returns `content: context`, and a bot demo has no context, so a failed
generation became a successful empty string.

  - demo/chat throws LLMUnavailableError when there is nothing to fall back
    on; the knowledge-base demo still serves its retrieved passage
  - quick-chat and professional-chat return 503 LLM_UNAVAILABLE instead of
    apologising with 200
  - lib/llm-health.ts records what generations actually did; /api/health
    reports it and returns 503 once the chain is consistently failing, so
    "healthy" stops meaning "the database answered"

tests/__tests__/api/professional-chat.test.ts asserted the bug -- 200, the
word "moment", provider 'fallback'. It now asserts 503 LLM_UNAVAILABLE. A
test that pins broken behaviour is worse than no test.

Never-twice: tests/__tests__/lib/llm-health.test.ts covers the tracker's
state machine and asserts at source level that no chat route answers an LLM
failure with success, and that health consults the LLM chain.

This makes the outage visible. It does NOT fix it -- that needs a valid
GROQ_API_KEY (or OPENROUTER_API_KEY) in /opt/botsmann/shared/.env on the box.
Until then these endpoints will correctly return 503.

verify: format, lint, typecheck, 268 tests, build -- all green.


Claude-Session: https://claude.ai/code/session_01HVwg8DKHQktxJuHeLM3xpG

Co-authored-by: Mao Nakamoto <41178744+maonakamoto@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-28 11:57:09 +00:00

34 lines
598 B
TypeScript

/**
* API Utilities Module
* @module lib/api
*
* Centralized exports for API-related utilities.
*/
export {
// Response creators
jsonSuccess,
jsonMessage,
jsonError,
jsonValidationError,
jsonRateLimitError,
jsonUnauthorized,
jsonNotFound,
jsonServiceUnavailable,
jsonLLMUnavailable,
// Validation helpers
validateBody,
hasValidationError,
formatZodErrors,
handleError,
// Constants
HTTP_STATUS,
CACHE_CONTROL,
// Types
type ApiResponse,
type ErrorCode,
type ValidationError,
type CacheControlPreset,
type ResponseOptions,
} from './responses';