Address CodeRabbit review: Improve openrouterService robustness

1. Lazy initialization of baseUrl via getBaseUrl() method
   - Allows API URL to be updated at runtime without stale URL issues

2. Runtime validation of API response structure
   - Validates embedding_models array exists before caching
   - Prevents invalid responses from being cached

Addresses CodeRabbit nitpick comments on PR #852

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
leex279
2025-11-08 23:38:01 +01:00
parent 71fbae575b
commit 4d2851cb2f

View File

@@ -23,7 +23,7 @@ export interface OpenRouterModelListResponse {
}
class OpenRouterService {
private baseUrl = getApiUrl();
private getBaseUrl = () => getApiUrl();
private cacheKey = "openrouter_models_cache";
private cacheTTL = 5 * 60 * 1000; // 5 minutes
@@ -101,7 +101,7 @@ class OpenRouterService {
return cached;
}
const response = await fetch(`${this.baseUrl}/api/openrouter/models`, {
const response = await fetch(`${this.getBaseUrl()}/api/openrouter/models`, {
method: "GET",
headers: {
"Content-Type": "application/json",
@@ -115,6 +115,11 @@ class OpenRouterService {
const data = await response.json();
// Validate response structure
if (!data.embedding_models || !Array.isArray(data.embedding_models)) {
throw new Error("Invalid response structure from OpenRouter API");
}
// Cache the successful response
this.cacheModels(data);