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

249 lines
6.7 KiB
Plaintext

import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { on } from '@ember/modifier';
import { service } from '@ember/service';
import { modifier } from 'ember-modifier';
import SettingsSelect from './select.gjs';
import {
OPENROUTER_API_KEYS_URL,
OPENROUTER_DEFAULT_GRANULARITY,
OPENROUTER_DEFAULT_INTERVAL,
OPENROUTER_GRANULARITY_OPTIONS,
OPENROUTER_INTERVAL_OPTIONS,
} from '../../../config/openrouter';
import { isCancellation } from '../../../services/http';
export default class SettingsIntegrationsOpenRouterForm extends Component {
@service openrouter;
@service pageLifecycle;
@tracked apiKeyDraft = null;
@tracked intervalDraft = null;
@tracked granularityDraft = null;
@tracked error = null;
@tracked submitting = false;
get apiKeyDraftValue() {
return this.apiKeyDraft ?? '';
}
get integration() {
return this.args.props?.integration;
}
get isEdit() {
return this.args.props?.mode === 'edit';
}
get title() {
return this.isEdit ? 'Edit OpenRouter Spend' : 'Add OpenRouter Spend';
}
get submitLabel() {
return this.isEdit ? 'Save' : 'Enable';
}
// A blank draft falls back to the stored key so users can change only the
// interval or granularity without re-entering the password.
get apiKey() {
return (
this.apiKeyDraft ?? this.integration?.apiKey ?? this.openrouter.apiKey
);
}
get interval() {
return (
this.intervalDraft ??
this.integration?.interval ??
this.openrouter.interval ??
OPENROUTER_DEFAULT_INTERVAL
);
}
get granularity() {
return (
this.granularityDraft ??
this.integration?.granularity ??
this.openrouter.granularity ??
OPENROUTER_DEFAULT_GRANULARITY
);
}
get intervalOptions() {
return OPENROUTER_INTERVAL_OPTIONS;
}
get granularityOptions() {
return OPENROUTER_GRANULARITY_OPTIONS;
}
get apiKeysUrl() {
return OPENROUTER_API_KEYS_URL;
}
resetOnClose = modifier((element) => {
const dialog = element.closest('dialog');
if (!dialog) {
return;
}
const handleClose = () => {
this.apiKeyDraft = null;
this.intervalDraft = null;
this.granularityDraft = null;
this.error = null;
};
dialog.addEventListener('close', handleClose);
return () => dialog.removeEventListener('close', handleClose);
});
@action
updateApiKey(event) {
const value = event.target.value;
this.apiKeyDraft = value || null;
this.error = null;
}
@action
updateInterval(value) {
this.intervalDraft = Number(value);
}
@action
updateGranularity(value) {
this.granularityDraft = value;
}
@action
async handleSubmit(event) {
event.preventDefault();
this.error = null;
if (!this.apiKey) {
this.error = 'Please enter an API key';
return;
}
this.submitting = true;
try {
if (this.isEdit) {
await this.openrouter.update(
this.apiKey,
this.interval,
this.granularity
);
} else {
await this.openrouter.enable(
this.apiKey,
this.interval,
this.granularity
);
}
this.args.close?.();
} catch (err) {
if (isCancellation(err) || this.pageLifecycle.leaving) {
return;
}
this.error = this.openrouter.classifyError(err);
} finally {
this.submitting = false;
}
}
<template>
<div class="p-6" {{this.resetOnClose}}>
<h3
id={{if @titleId @titleId "modal-title"}}
class="text-base font-semibold leading-6 text-gray-900"
>{{this.title}}</h3>
<div class="mt-2">
<p class="text-sm text-gray-500" data-test-openrouter-description>Show
your OpenRouter spend on the dashboard.</p>
</div>
<form {{on "submit" this.handleSubmit}} class="mt-5">
<div class="space-y-5">
<div>
<label
for="openrouter-key"
class="block text-sm font-medium leading-6 text-gray-900"
>
API key
</label>
<div class="mt-2">
<input
type="password"
id="openrouter-key"
name="apiKey"
autocomplete="off"
value={{this.apiKeyDraftValue}}
{{on "input" this.updateApiKey}}
placeholder={{if this.isEdit "Leave blank to keep current" ""}}
class="block w-full rounded-md border-0 px-3 py-2 text-sm text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600"
/>
</div>
<p class="mt-2 text-sm text-gray-500">
Create one at
<a
href={{this.apiKeysUrl}}
target="_blank"
rel="noopener noreferrer"
class="font-medium text-indigo-600 hover:text-indigo-500"
>OpenRouter API keys</a>.
</p>
<p class="mt-1 text-xs text-gray-500">
Shows this key's usage for the selected window (UTC).
</p>
</div>
<SettingsSelect
@id="openrouter-interval"
@name="interval"
@label="Update interval"
@options={{this.intervalOptions}}
@value={{this.interval}}
@onChange={{this.updateInterval}}
/>
<SettingsSelect
@id="openrouter-granularity"
@name="granularity"
@label="Show"
@options={{this.granularityOptions}}
@value={{this.granularity}}
@onChange={{this.updateGranularity}}
/>
{{#if this.error}}
<p class="text-sm text-red-600">{{this.error}}</p>
{{/if}}
</div>
<div class="mt-5 sm:mt-6 flex gap-3">
<button
type="submit"
disabled={{this.submitting}}
class="inline-flex w-full justify-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 disabled:opacity-50"
>
{{this.submitLabel}}
</button>
<button
type="button"
{{on "click" @close}}
class="inline-flex w-full justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
>
Cancel
</button>
</div>
</form>
</div>
</template>
}