Skip to content

Commit 1fb34b8

Browse files
authored
Show requesty key balance on the settings screen (RooCodeInc#1995)
1 parent 0fd0800 commit 1fb34b8

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

.changeset/lazy-ducks-hang.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"roo-cline": patch
3+
---
4+
5+
Show requesty key balance on the settings screen

webview-ui/src/components/settings/ApiOptions.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
OPENROUTER_DEFAULT_PROVIDER_NAME,
4848
} from "@/components/ui/hooks/useOpenRouterModelProviders"
4949
import { useOpenRouterKeyInfo } from "@/components/ui/hooks/useOpenRouterKeyInfo"
50+
import { useRequestyKeyInfo } from "@/components/ui/hooks/useRequestyKeyInfo"
5051
import { MODELS_BY_PROVIDER, PROVIDERS, AWS_REGIONS, VERTEX_REGIONS } from "./constants"
5152
import { VSCodeButtonLink } from "../common/VSCodeButtonLink"
5253
import { ModelInfoView } from "./ModelInfoView"
@@ -74,6 +75,24 @@ const OpenRouterBalanceDisplay = ({ apiKey, baseUrl }: { apiKey: string; baseUrl
7475
)
7576
}
7677

78+
const RequestyBalanceDisplay = ({ apiKey }: { apiKey: string }) => {
79+
const { data: keyInfo } = useRequestyKeyInfo(apiKey)
80+
81+
if (!keyInfo) {
82+
return null
83+
}
84+
85+
// Parse the balance to a number and format it to 2 decimal places
86+
const balance = parseFloat(keyInfo.org_balance)
87+
const formattedBalance = balance.toFixed(2)
88+
89+
return (
90+
<VSCodeLink href="https://app.requesty.ai/settings" className="text-vscode-foreground hover:underline">
91+
${formattedBalance}
92+
</VSCodeLink>
93+
)
94+
}
95+
7796
interface ApiOptionsProps {
7897
uriScheme: string | undefined
7998
apiConfiguration: ApiConfiguration
@@ -427,7 +446,12 @@ const ApiOptions = ({
427446
onInput={handleInputChange("requestyApiKey")}
428447
placeholder={t("settings:providers.getRequestyApiKey")}
429448
className="w-full">
430-
<label className="block font-medium mb-1">{t("settings:providers.requestyApiKey")}</label>
449+
<div className="flex justify-between items-center mb-1">
450+
<label className="block font-medium">{t("settings:providers.requestyApiKey")}</label>
451+
{apiConfiguration?.requestyApiKey && (
452+
<RequestyBalanceDisplay apiKey={apiConfiguration.requestyApiKey} />
453+
)}
454+
</div>
431455
</VSCodeTextField>
432456
<div className="text-sm text-vscode-descriptionForeground -mt-2">
433457
{t("settings:providers.apiKeyStorageNotice")}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import axios from "axios"
2+
import { z } from "zod"
3+
import { useQuery, UseQueryOptions } from "@tanstack/react-query"
4+
5+
const requestyKeyInfoSchema = z.object({
6+
name: z.string(),
7+
monthly_limit: z.string(),
8+
monthly_spend: z.string(),
9+
org_balance: z.string(),
10+
config: z.object({
11+
aliases: z.record(z.string(), z.any()).optional(),
12+
}),
13+
})
14+
15+
export type RequestyKeyInfo = z.infer<typeof requestyKeyInfoSchema>
16+
17+
async function getRequestyKeyInfo(apiKey?: string) {
18+
if (!apiKey) return null
19+
20+
try {
21+
const response = await axios.get("https://api.requesty.ai/x/apikey", {
22+
headers: {
23+
Authorization: `Bearer ${apiKey}`,
24+
"Content-Type": "application/json",
25+
},
26+
})
27+
28+
const result = requestyKeyInfoSchema.safeParse(response.data)
29+
if (!result.success) {
30+
console.error("Requesty API key info validation failed:", result.error)
31+
return null
32+
}
33+
34+
return result.data
35+
} catch (error) {
36+
console.error("Error fetching Requesty key info:", error)
37+
return null
38+
}
39+
}
40+
41+
type UseRequestyKeyInfoOptions = Omit<UseQueryOptions<RequestyKeyInfo | null>, "queryKey" | "queryFn">
42+
export const useRequestyKeyInfo = (apiKey?: string, options?: UseRequestyKeyInfoOptions) => {
43+
return useQuery<RequestyKeyInfo | null>({
44+
queryKey: ["requesty-key-info", apiKey],
45+
queryFn: () => getRequestyKeyInfo(apiKey),
46+
staleTime: 30 * 1000, // 30 seconds
47+
enabled: !!apiKey,
48+
...options,
49+
})
50+
}

0 commit comments

Comments
 (0)