Spaces:
Sleeping
Sleeping
File size: 2,087 Bytes
097eaf0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | import { Response } from "express";
import { RequestWithAuth } from "./types";
import { billTeam } from "../../services/billing/credit_billing";
import { getACUCTeam } from "../auth";
import { RateLimiterMode } from "../../types";
import { logger } from "../../lib/logger";
const FIRECLAW_COST_PER_PLAY = 100;
const MAX_PLAYS = 10;
interface FireclawResponse {
success: true;
credits_billed: number;
plays: number;
remaining_credits: number;
}
interface FireclawErrorResponse {
success: false;
error: string;
}
export async function fireclawController(
req: RequestWithAuth<{}, undefined, { plays?: number }>,
res: Response<FireclawResponse | FireclawErrorResponse>,
): Promise<void> {
const plays = Math.max(1, Math.min(MAX_PLAYS, Math.floor(Number(req.body?.plays) || 1)));
const totalCredits = plays * FIRECLAW_COST_PER_PLAY;
const chunk =
req.acuc ??
(await getACUCTeam(req.auth.team_id, false, false, RateLimiterMode.Scrape));
if (!chunk) {
res.status(404).json({ success: false, error: "Could not find team billing information." });
return;
}
if (chunk.remaining_credits < totalCredits) {
res.status(402).json({
success: false,
error: `Not enough credits. You need ${totalCredits} credits (${plays} play${plays > 1 ? "s" : ""} x ${FIRECLAW_COST_PER_PLAY}) but only have ${chunk.remaining_credits}.`,
});
return;
}
try {
await billTeam(
req.auth.team_id,
req.acuc?.sub_id ?? undefined,
totalCredits,
req.acuc?.api_key_id ?? null,
{ endpoint: "fireclaw" },
);
} catch (error) {
logger.error(`Fireclaw billing failed for team ${req.auth.team_id}`, { error });
res.status(500).json({ success: false, error: "Failed to process billing. Please try again." });
return;
}
const updatedChunk = await getACUCTeam(req.auth.team_id, false, false, RateLimiterMode.Scrape);
res.json({
success: true,
credits_billed: totalCredits,
plays,
remaining_credits: updatedChunk?.remaining_credits ?? chunk.remaining_credits - totalCredits,
});
}
|