feat: Display open router key usage (#27)

This commit is contained in:
Aidan Bleser 2025-06-18 17:40:37 -05:00 committed by GitHub
parent 625cca98c5
commit afef01f0cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 78 additions and 7 deletions

View file

@ -0,0 +1,34 @@
import { Result, ResultAsync } from 'neverthrow';
export type OpenRouterApiKeyData = {
label: string;
usage: number;
is_free_tier: boolean;
is_provisioning_key: boolean;
limit: number;
limit_remaining: number;
};
export const OpenRouter = {
getApiKey: async (key: string): Promise<Result<OpenRouterApiKeyData, string>> => {
return await ResultAsync.fromPromise(
(async () => {
const res = await fetch('https://openrouter.ai/api/v1/key', {
headers: {
Authorization: `Bearer ${key}`,
'Content-Type': 'application/json',
},
});
if (!res.ok) throw new Error('Failed to get API key');
const { data } = await res.json();
if (!data) throw new Error('No info returned for api key');
return data as OpenRouterApiKeyData;
})(),
(e) => `Failed to get API key ${e}`
);
},
};

View file

@ -52,6 +52,10 @@
name: 'New Chat', name: 'New Chat',
keys: [cmdOrCtrl, 'Shift', 'O'], keys: [cmdOrCtrl, 'Shift', 'O'],
}, },
{
name: 'Search Messages',
keys: [cmdOrCtrl, 'K'],
},
]; ];
async function signOut() { async function signOut() {

View file

@ -7,10 +7,12 @@
import { Input } from '$lib/components/ui/input'; import { Input } from '$lib/components/ui/input';
import { Link } from '$lib/components/ui/link'; import { Link } from '$lib/components/ui/link';
import { session } from '$lib/state/session.svelte.js'; import { session } from '$lib/state/session.svelte.js';
import type { Provider, ProviderMeta } from '$lib/types'; import { Provider, type ProviderMeta } from '$lib/types';
import KeyIcon from '~icons/lucide/key'; import KeyIcon from '~icons/lucide/key';
import { useConvexClient } from 'convex-svelte'; import { useConvexClient } from 'convex-svelte';
import { ResultAsync } from 'neverthrow'; import { ResultAsync } from 'neverthrow';
import { resource } from 'runed';
import * as providers from '$lib/utils/providers';
type Props = { type Props = {
provider: Provider; provider: Provider;
@ -57,6 +59,19 @@
loading = false; loading = false;
} }
const apiKeyInfoResource = resource(
() => keyQuery.data,
async (key) => {
if (!key) return null;
if (provider === Provider.OpenRouter) {
return (await providers.OpenRouter.getApiKey(key)).unwrapOr(null);
}
return null;
}
);
</script> </script>
<Card.Root> <Card.Root>
@ -80,12 +95,30 @@
value={keyQuery.data!} value={keyQuery.data!}
/> />
{/if} {/if}
<span class="text-muted-foreground text-xs"> {#if keyQuery.data}
Get your API key from {#if apiKeyInfoResource.loading}
<Link href={meta.link} target="_blank" class="text-blue-500"> <div class="bg-input h-6 w-[200px] animate-pulse rounded-md"></div>
{meta.title} {:else if apiKeyInfoResource.current}
</Link> <span class="text-muted-foreground flex h-6 place-items-center text-xs">
</span> ${apiKeyInfoResource.current?.usage.toFixed(3)} used ・ ${apiKeyInfoResource.current?.limit_remaining.toFixed(
3
)} remaining
</span>
{:else}
<span
class="flex h-6 w-fit place-items-center rounded-lg bg-red-500/50 px-2 text-xs text-red-500"
>
We encountered an error while trying to check your usage. Please try again later.
</span>
{/if}
{:else}
<span class="text-muted-foreground text-xs">
Get your API key from
<Link href={meta.link} target="_blank" class="text-blue-500">
{meta.title}
</Link>
</span>
{/if}
</div> </div>
<div class="flex justify-end"> <div class="flex justify-end">
<Button {loading} type="submit" {...toasts.trigger}>Save</Button> <Button {loading} type="submit" {...toasts.trigger}>Save</Button>