@@ -2,12 +2,45 @@ import { Hono } from "hono"
22import { describeRoute , validator , resolver } from "hono-openapi"
33import z from "zod"
44import { Config } from "../../config/config"
5+ import { Auth } from "../../auth"
56import { Provider } from "../../provider/provider"
67import { ModelsDev } from "../../provider/models"
78import { ProviderAuth } from "../../provider/auth"
89import { mapValues } from "remeda"
910import { errors } from "../error"
1011import { lazy } from "../../util/lazy"
12+ import { Installation } from "../../installation"
13+
14+ const CopilotQuota = z
15+ . object ( {
16+ completions : z . number ( ) . nullable ( ) ,
17+ chat : z . number ( ) . nullable ( ) ,
18+ premium : z . number ( ) . nullable ( ) ,
19+ reset : z . string ( ) . optional ( ) ,
20+ updated : z . string ( ) . optional ( ) ,
21+ } )
22+ . meta ( { ref : "CopilotQuota" } )
23+
24+ const CopilotSnapshot = z
25+ . object ( {
26+ percent_remaining : z . number ( ) . optional ( ) ,
27+ timestamp_utc : z . string ( ) . optional ( ) ,
28+ } )
29+ . partial ( )
30+
31+ const CopilotApi = z
32+ . object ( {
33+ quota_snapshots : z
34+ . object ( {
35+ chat : CopilotSnapshot . optional ( ) ,
36+ completions : CopilotSnapshot . optional ( ) ,
37+ premium_interactions : CopilotSnapshot . optional ( ) ,
38+ } )
39+ . optional ( ) ,
40+ quota_reset_date_utc : z . string ( ) . optional ( ) ,
41+ quota_reset_date : z . string ( ) . optional ( ) ,
42+ } )
43+ . passthrough ( )
1144
1245export const ProviderRoutes = lazy ( ( ) =>
1346 new Hono ( )
@@ -161,5 +194,59 @@ export const ProviderRoutes = lazy(() =>
161194 } )
162195 return c . json ( true )
163196 } ,
197+ )
198+ . get (
199+ "/copilot/quota" ,
200+ describeRoute ( {
201+ summary : "Get GitHub Copilot quota" ,
202+ description : "Get GitHub Copilot remaining quota percentages for chat, completions, and premium requests." ,
203+ operationId : "provider.copilot.quota" ,
204+ responses : {
205+ 200 : {
206+ description : "Copilot quota" ,
207+ content : {
208+ "application/json" : {
209+ schema : resolver ( CopilotQuota . nullable ( ) ) ,
210+ } ,
211+ } ,
212+ } ,
213+ } ,
214+ } ) ,
215+ async ( c ) => {
216+ const auth = ( await Auth . get ( "github-copilot" ) ) ?? ( await Auth . get ( "github-copilot-enterprise" ) )
217+ if ( ! auth || auth . type !== "oauth" ) return c . json ( null )
218+
219+ const domain = auth . enterpriseUrl
220+ ? auth . enterpriseUrl . replace ( / ^ h t t p s ? : \/ \/ / , "" ) . replace ( / \/ $ / , "" )
221+ : undefined
222+ const url = domain
223+ ? `https://${ domain } /api/v3/copilot_internal/user`
224+ : "https://api.github.com/copilot_internal/user"
225+ const response = await fetch ( url , {
226+ headers : {
227+ Authorization : `Bearer ${ auth . refresh } ` ,
228+ "User-Agent" : `opencode/${ Installation . VERSION } ` ,
229+ Accept : "application/json" ,
230+ } ,
231+ } )
232+ if ( ! response . ok ) return c . json ( null )
233+
234+ const data = await response . json ( )
235+ const parsed = CopilotApi . safeParse ( data )
236+ if ( ! parsed . success ) return c . json ( null )
237+
238+ const snapshots = parsed . data . quota_snapshots
239+ const quota = {
240+ completions : snapshots ?. completions ?. percent_remaining ?? null ,
241+ chat : snapshots ?. chat ?. percent_remaining ?? null ,
242+ premium : snapshots ?. premium_interactions ?. percent_remaining ?? null ,
243+ reset : parsed . data . quota_reset_date_utc ?? parsed . data . quota_reset_date ,
244+ updated :
245+ snapshots ?. premium_interactions ?. timestamp_utc ??
246+ snapshots ?. chat ?. timestamp_utc ??
247+ snapshots ?. completions ?. timestamp_utc ,
248+ }
249+ return c . json ( quota )
250+ } ,
164251 ) ,
165252)
0 commit comments