File size: 3,904 Bytes
98c9143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import consola from "consola"

const copilotRateLimitTypes = ["session", "weekly"] as const

export type CopilotRateLimitType = (typeof copilotRateLimitTypes)[number]
type HeadersMap = Record<string, string | undefined>
type HeadersLike = Headers | HeadersMap
type QuotaSnapshotMap = Record<string, unknown>

export interface CopilotRateLimitUsage {
  type: CopilotRateLimitType
  remaining: string
  resetAt: string
}

export interface CopilotQuotaSnapshot {
  entitlement: string
  percent_remaining: number
  overage_permitted: boolean
  overage_count: number
  reset_date: string
}

const copilotRateLimitHeaders: Record<CopilotRateLimitType, string> = {
  session: "x-usage-ratelimit-session",
  weekly: "x-usage-ratelimit-weekly",
}

const copilotQuotaSnapshotKeys: Record<CopilotRateLimitType, string> = {
  session: "5Hour-Session-RateLimits",
  weekly: "Weekly-Session-RateLimits",
}

const hasGetMethod = (headers: HeadersLike): headers is Headers => {
  return "get" in headers && typeof headers.get === "function"
}

const getHeaderValue = (
  headers: HeadersLike,
  headerName: string,
): string | null => {
  if (hasGetMethod(headers)) {
    return headers.get(headerName)
  }

  const normalizedHeaderName = headerName.toLowerCase()
  const matchedEntry = Object.entries(headers).find(
    ([key]) => key.toLowerCase() === normalizedHeaderName,
  )

  return matchedEntry?.[1] ?? null
}

export const parseCopilotRateLimitHeader = (
  headerValue: string,
): Omit<CopilotRateLimitUsage, "type"> | null => {
  const params = new URLSearchParams(headerValue)
  const remaining = params.get("rem")
  const resetAt = params.get("rst")

  if (!remaining || !resetAt) {
    return null
  }

  return {
    remaining,
    resetAt,
  }
}

export const getCopilotRateLimitUsage = (
  headers: HeadersLike,
  type: CopilotRateLimitType,
): CopilotRateLimitUsage | null => {
  const headerName = copilotRateLimitHeaders[type]
  const headerValue = getHeaderValue(headers, headerName)

  if (!headerValue) {
    return null
  }

  const parsed = parseCopilotRateLimitHeader(headerValue)

  if (!parsed) {
    return null
  }

  return {
    type,
    ...parsed,
  }
}

export const getCopilotRateLimitUsageFromSnapshots = (
  snapshots: QuotaSnapshotMap | undefined,
  type: CopilotRateLimitType,
): CopilotRateLimitUsage | null => {
  const snapshot = snapshots?.[copilotQuotaSnapshotKeys[type]]
  if (!isCopilotQuotaSnapshot(snapshot)) {
    return null
  }

  return {
    remaining: String(snapshot.percent_remaining),
    resetAt: snapshot.reset_date,
    type,
  }
}

export const logCopilotRateLimits = (headers: HeadersLike): void => {
  for (const type of copilotRateLimitTypes) {
    const usage = getCopilotRateLimitUsage(headers, type)

    if (!usage) {
      continue
    }

    logCopilotRateLimitUsage(usage)
  }
}

export const logCopilotQuotaSnapshots = (
  snapshots: QuotaSnapshotMap | undefined,
): void => {
  for (const type of copilotRateLimitTypes) {
    const usage = getCopilotRateLimitUsageFromSnapshots(snapshots, type)

    if (!usage) {
      continue
    }

    logCopilotRateLimitUsage(usage)
  }
}

const logCopilotRateLimitUsage = (usage: CopilotRateLimitUsage): void => {
  const d = new Date(usage.resetAt)
  const dateStr = Number.isNaN(d.getTime()) ? usage.resetAt : d.toLocaleString()
  consola.info(
    `Copilot ${usage.type} quota remaining: ${usage.remaining}, resets at: ${dateStr}`,
  )
}

const isCopilotQuotaSnapshot = (
  value: unknown,
): value is CopilotQuotaSnapshot => {
  if (!value || typeof value !== "object") {
    return false
  }

  const record = value as Record<string, unknown>
  return (
    typeof record.entitlement === "string"
    && typeof record.percent_remaining === "number"
    && typeof record.overage_permitted === "boolean"
    && typeof record.overage_count === "number"
    && typeof record.reset_date === "string"
  )
}