File size: 1,191 Bytes
0e759d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Request, Response } from "express";
import { RequestWithAuth } from "./types";
import { getACUC, getACUCTeam } from "../auth";
import { logger } from "../../lib/logger";
import { RateLimiterMode } from "../../types";

export async function tokenUsageController(
  req: RequestWithAuth,
  res: Response,
): Promise<void> {
  try {
    // If we already have the token usage info from auth, use it
    if (req.acuc) {
      res.json({
        success: true,
        data: {
          remaining_tokens: req.acuc.remaining_credits,
        },
      });
      return;
    }

    // Otherwise fetch fresh data
    const chunk = await getACUCTeam(req.auth.team_id, false, true, RateLimiterMode.Extract);
    if (!chunk) {
      res.status(404).json({
        success: false,
        error: "Could not find token usage information",
      });
      return;
    }

    res.json({
      success: true,
      data: {
        remaining_tokens: chunk.remaining_credits,
      },
    });
  } catch (error) {
    logger.error("Error in token usage controller:", error);
    res.status(500).json({
      success: false,
      error: "Internal server error while fetching token usage",
    });
  }
}