Files
raucao 4f3c4334bd Use only normal API keys for OpenRouter spend
OpenRouter now rejects management keys from the browser on both
/api/v1/keys and /api/v1/analytics/query, so the management-key flow can
no longer work in the extension. Read the configured key's own usage from
GET /api/v1/key instead, which returns UTC daily, weekly, and monthly
buckets and works from the browser.

Drop the key-type selection, key listing/selection, pagination, and the
management/analytics endpoints. Validate the key against /key before
persisting, and write a fresh record so legacy keyType/keyIds fields are
dropped. Keep the browser-block hint for anyone still storing a
management key.
2026-11-16 22:16:35 +04:00

58 lines
2.5 KiB
JavaScript

export const OPENROUTER_API_URL = 'https://openrouter.ai/api/v1';
export const OPENROUTER_KEY_URL = `${OPENROUTER_API_URL}/key`;
export const OPENROUTER_API_KEYS_URL = 'https://openrouter.ai/settings/keys';
export const OPENROUTER_DEFAULT_INTERVAL = 300000;
export const OPENROUTER_INTERVAL_OPTIONS = [
{ value: 300000, label: 'Every 5 minutes' },
{ value: 600000, label: 'Every 10 minutes' },
{ value: 1800000, label: 'Every 30 minutes' },
];
export const OPENROUTER_DEFAULT_GRANULARITY = 'today';
export const OPENROUTER_GRANULARITY_OPTIONS = [
{ value: 'today', label: 'Today' },
{ value: 'this-week', label: 'This week' },
{ value: 'this-month', label: 'This month' },
];
// The OpenRouter glyph is a single path in a 1024x1024 viewBox. Scaling it to
// the 24x24 viewBox keeps the same shape in the dashboard's small icon.
export const OPENROUTER_ICON_PATH =
'm 795.89139,147.2 c 119.883,0 217.05701,97.9963 217.05701,218.88 0,120.884 -97.17401,218.88 -217.05701,218.88 l 215.30701,217.109 c 27.35,27.578 7.98,74.731 -30.69601,74.731 h -618.734 c -199.8,0 -361.76999913,-163.327 -361.76999913,-364.8 0,-201.473 161.96999913,-364.8 361.76999913,-364.8 z m -434.123,145.92 c -119.88,0 -217.062,97.996 -217.062,218.88 0,120.884 97.182,218.88 217.062,218.88 119.879,0 217.061,-97.996 217.061,-218.88 0,-120.884 -97.182,-218.88 -217.061,-218.88 z';
export const OPENROUTER_ICON_TRANSFORM = 'scale(0.0234375)';
// Maps the dashboard granularity onto the UTC usage buckets returned by
// GET /api/v1/key. OpenRouter only reports UTC-windowed figures, so "today"
// rolls over at 00:00 UTC regardless of the browser's timezone.
const USAGE_FIELD_BY_GRANULARITY = {
today: 'usage_daily',
'this-week': 'usage_weekly',
'this-month': 'usage_monthly',
};
export function openrouterAuthorizationHeader(apiKey) {
return `Bearer ${apiKey}`;
}
// Falls back to "today" for values the app no longer supports, so a stored
// value written during development cannot render an empty select.
export function openrouterNormalizeGranularity(granularity) {
return OPENROUTER_GRANULARITY_OPTIONS.some(
(option) => option.value === granularity
)
? granularity
: OPENROUTER_DEFAULT_GRANULARITY;
}
// Reads the usage bucket for the chosen window from a GET /api/v1/key response.
export function openrouterUsageForGranularity(
keyData,
granularity = OPENROUTER_DEFAULT_GRANULARITY
) {
const field =
USAGE_FIELD_BY_GRANULARITY[openrouterNormalizeGranularity(granularity)];
return Number(keyData?.[field] ?? 0);
}