From bbafb1ec2ddfcf45f19cc2e70c32b9042db167a7 Mon Sep 17 00:00:00 2001 From: RugerMc <550279039@qq.com> Date: Wed, 29 Mar 2023 22:09:52 +0800 Subject: [PATCH] feat: display the balance of current key --- app/api/credit-summary/route.ts | 22 ++++++++++++++++++++++ app/components/settings.tsx | 18 ++++++++++++++++++ app/locales/cn.ts | 5 +++++ app/locales/en.ts | 5 +++++ app/locales/tw.ts | 5 +++++ app/requests.ts | 18 +++++++++++++++++- 6 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 app/api/credit-summary/route.ts diff --git a/app/api/credit-summary/route.ts b/app/api/credit-summary/route.ts new file mode 100644 index 000000000..6c7c3c399 --- /dev/null +++ b/app/api/credit-summary/route.ts @@ -0,0 +1,22 @@ +import { NextRequest } from "next/server"; +export async function POST(req: NextRequest) { + try { + let apiKey = process.env.OPENAI_API_KEY; + const userApiKey = req.headers.get("token"); + if (userApiKey) { + apiKey = userApiKey; + } + + const res = await fetch("https://api.openai.com/dashboard/billing/credit_grants", { + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${apiKey}`, + }, + method: "GET" + }); + console.log(res.body) + return new Response(res.body); + } catch (error) { + console.error("[TOKEN]", error); + } + } \ No newline at end of file diff --git a/app/components/settings.tsx b/app/components/settings.tsx index 241cebae6..baf3c8fdc 100644 --- a/app/components/settings.tsx +++ b/app/components/settings.tsx @@ -27,6 +27,7 @@ import { getCurrentCommitId } from "../utils"; import Link from "next/link"; import { UPDATE_URL } from "../constant"; import { SearchService, usePromptStore } from "../store/prompt"; +import { requestAccountBalance, requestCreditSummary, requestWithPrompt } from "../requests"; function SettingItem(props: { title: string; @@ -72,6 +73,7 @@ export function Settings(props: { closeSettings: () => void }) { useEffect(() => { checkUpdate(); + requestAccountBalance().then((res)=> setAccountBalance(res)) }, []); const accessStore = useAccessStore(); @@ -84,6 +86,8 @@ export function Settings(props: { closeSettings: () => void }) { const builtinCount = SearchService.count.builtin; const customCount = promptStore.prompts.size ?? 0; + const [accountBalance, setAccountBalance] = useState(-1); + return ( <>