pro: show remaining credits (#329)

Fixes #265
This commit is contained in:
Will Chen
2025-06-03 23:46:35 -07:00
committed by GitHub
parent 0f4e532206
commit 9d1a0f7ad7
8 changed files with 182 additions and 21 deletions

View File

@@ -0,0 +1,61 @@
import fetch from "node-fetch"; // Electron main process might need node-fetch
import log from "electron-log";
import { createLoggedHandler } from "./safe_handle";
import { readSettings } from "../../main/settings"; // Assuming settings are read this way
import { UserBudgetInfoSchema } from "../ipc_types";
const logger = log.scope("pro_handlers");
const handle = createLoggedHandler(logger);
const CONVERSION_RATIO = (10 * 3) / 2;
export function registerProHandlers() {
// This method should try to avoid throwing errors because this is auxiliary
// information and isn't critical to using the app
handle("get-user-budget", async (): Promise<UserBudgetInfo | null> => {
logger.info("Attempting to fetch user budget information.");
const settings = readSettings();
const apiKey = settings.providerSettings?.auto?.apiKey?.value;
if (!apiKey) {
logger.error("LLM Gateway API key (Dyad Pro) is not configured.");
return null;
}
const url = "https://llm-gateway.dyad.sh/user/info";
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
};
try {
// Use native fetch if available, otherwise node-fetch will be used via import
const response = await fetch(url, {
method: "GET",
headers: headers,
});
if (!response.ok) {
const errorBody = await response.text();
logger.error(
`Failed to fetch user budget. Status: ${response.status}. Body: ${errorBody}`,
);
return null;
}
const data = await response.json();
const userInfoData = data["user_info"];
logger.info("Successfully fetched user budget information.");
return UserBudgetInfoSchema.parse({
usedCredits: userInfoData["spend"] * CONVERSION_RATIO,
totalCredits: userInfoData["max_budget"] * CONVERSION_RATIO,
budgetResetDate: new Date(userInfoData["budget_reset_at"]),
});
} catch (error: any) {
logger.error(`Error fetching user budget: ${error.message}`, error);
return null;
}
});
}

View File

@@ -30,6 +30,7 @@ import type {
ImportAppResult,
ImportAppParams,
RenameBranchParams,
UserBudgetInfo,
} from "./ipc_types";
import type { ProposalResult } from "@/lib/schemas";
import { showError } from "@/lib/toast";
@@ -825,4 +826,9 @@ export class IpcClient {
async clearSessionData(): Promise<void> {
return this.ipcRenderer.invoke("clear-session-data");
}
// Method to get user budget information
public async getUserBudget(): Promise<UserBudgetInfo | null> {
return this.ipcRenderer.invoke("get-user-budget");
}
}

View File

@@ -18,6 +18,7 @@ import { registerLanguageModelHandlers } from "./handlers/language_model_handler
import { registerReleaseNoteHandlers } from "./handlers/release_note_handlers";
import { registerImportHandlers } from "./handlers/import_handlers";
import { registerSessionHandlers } from "./handlers/session_handlers";
import { registerProHandlers } from "./handlers/pro_handlers";
export function registerIpcHandlers() {
// Register all IPC handlers by category
@@ -41,4 +42,5 @@ export function registerIpcHandlers() {
registerReleaseNoteHandlers();
registerImportHandlers();
registerSessionHandlers();
registerProHandlers();
}

View File

@@ -1,3 +1,5 @@
import { z } from "zod";
export interface AppOutput {
type: "stdout" | "stderr" | "info" | "client-error";
message: string;
@@ -207,3 +209,10 @@ export interface RenameBranchParams {
oldBranchName: string;
newBranchName: string;
}
export const UserBudgetInfoSchema = z.object({
usedCredits: z.number(),
totalCredits: z.number(),
budgetResetDate: z.date(),
});
export type UserBudgetInfo = z.infer<typeof UserBudgetInfoSchema>;