fix: improve apiKeyPolicy type safety and logging + fix model ID mismatch in usage.ts

- Added ApiKeyMetadata interface to replace 'any' types in apiKeyPolicy.ts
- Added error logging in catch blocks for getApiKeyMetadata() and checkBudget()
- Fixed claude-sonnet-4-6-thinking → claude-sonnet-4-6 mismatch in usage.ts importantModels

Follow-up fixes for merged PRs #131 and #128
This commit is contained in:
diegosouzapw
2026-02-25 06:06:37 -03:00
parent f670a1e451
commit 05d8d3d71d
2 changed files with 20 additions and 7 deletions
+1 -1
View File
@@ -318,7 +318,7 @@ async function getAntigravityUsage(accessToken, providerSpecificData) {
// Filter only recommended/important models (must match PROVIDER_MODELS ag ids)
const importantModels = [
"claude-opus-4-6-thinking",
"claude-sonnet-4-6-thinking",
"claude-sonnet-4-6",
"gemini-3.1-pro-high",
"gemini-3.1-pro-low",
"gemini-3-flash",
+19 -6
View File
@@ -13,12 +13,23 @@ import { getApiKeyMetadata, isModelAllowedForKey } from "@/lib/localDb";
import { checkBudget } from "@/domain/costRules";
import { errorResponse } from "@omniroute/open-sse/utils/error.ts";
import { HTTP_STATUS } from "@omniroute/open-sse/config/constants.ts";
import * as log from "@/sse/utils/logger";
/** Metadata stored for an API key in the local database. */
export interface ApiKeyMetadata {
id: string;
name?: string;
allowedModels?: string[];
budget?: number;
usedBudget?: number;
[key: string]: unknown;
}
export interface ApiKeyPolicyResult {
/** API key string (null if no key provided) */
apiKey: string | null;
/** Metadata from DB (null if no key or key not found) */
apiKeyInfo: any | null;
apiKeyInfo: ApiKeyMetadata | null;
/** If set, the request should be rejected with this Response */
rejection: Response | null;
}
@@ -53,11 +64,12 @@ export async function enforceApiKeyPolicy(
}
// Fetch key metadata (includes allowedModels)
let apiKeyInfo: any = null;
let apiKeyInfo: ApiKeyMetadata | null = null;
try {
apiKeyInfo = await getApiKeyMetadata(apiKey);
} catch {
// If metadata fetch fails, don't block — degrade gracefully
} catch (error) {
// If metadata fetch fails, don't block — degrade gracefully, but log for debugging
log.warn("API_POLICY", "Failed to fetch API key metadata. Request will be allowed.", { error });
return { apiKey, apiKeyInfo: null, rejection: null };
}
@@ -95,8 +107,9 @@ export async function enforceApiKeyPolicy(
),
};
}
} catch {
// Budget check is best-effort — don't block on errors
} catch (error) {
// Budget check is best-effort — don't block on errors, but log them
log.warn("API_POLICY", "Budget check failed. Request will be allowed.", { error });
}
}