61
src/ipc/handlers/pro_handlers.ts
Normal file
61
src/ipc/handlers/pro_handlers.ts
Normal 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;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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>;
|
||||
|
||||
Reference in New Issue
Block a user