Files
g-butandClaude Opus 4.6 372fe46261 refactor(chat): extract shared utilities from 3 chat API routes
Create lib/chat/ module with shared utilities:
- embedding.ts: generateEmbeddingWithTimeout (embedding + timeout guard)
- settings.ts: getUserLLMSettings (provider/apiKey/ollamaUrl lookup)
- context.ts: truncateChunks + joinContext (context building)
- llm-errors.ts: handleLLMError (API key fallback, Ollama, AI unavailable)
- parse.ts: parseResponseWithSuggestions (extract >>> suggestions)

Update all 3 chat routes to use shared utilities, removing ~220 lines of
duplicated code. Also fix module-scope getServerEnv() call in llm-client
that caused SSG build failure.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-02 17:23:14 +01:00

51 lines
1.3 KiB
TypeScript

/**
* Shared LLM error handling for chat routes
* @module lib/chat/llm-errors
*/
import { jsonError, jsonSuccess, HTTP_STATUS } from '@/lib/api';
import { DOMAIN_ERRORS } from '@/lib/constants';
interface FallbackSource {
label: string;
content: string;
}
/**
* Handle common LLM call errors.
* Returns a Response or null if the error isn't recognized.
*/
export function handleLLMError(
error: unknown,
fallbackSources?: FallbackSource[],
): Response | null {
if (!(error instanceof Error)) return null;
// API key errors — return context-only response if we have sources
if (error.message.includes('API key') && fallbackSources && fallbackSources.length > 0) {
const parts = fallbackSources.map((s) => `${s.label}\n${s.content}`);
return jsonSuccess({
response: `Here's what I found:\n\n${parts.join('\n\n---\n\n')}`,
sources: fallbackSources,
provider: 'none',
model: 'context-only',
});
}
// Ollama connection errors
if (error.message.includes('Cannot connect to Ollama')) {
return jsonError(
DOMAIN_ERRORS.OLLAMA_NOT_RUNNING,
'SERVICE_UNAVAILABLE',
HTTP_STATUS.SERVICE_UNAVAILABLE,
);
}
// Generic AI unavailable
return jsonError(
DOMAIN_ERRORS.AI_UNAVAILABLE,
'SERVICE_UNAVAILABLE',
HTTP_STATUS.SERVICE_UNAVAILABLE,
);
}