@@ -29,6 +29,7 @@ import {
2929 type ResponsesResult ,
3030 type ResponseStreamEvent ,
3131} from "~/services/copilot/create-responses"
32+ import { getCopilotUsage } from "~/services/github/get-copilot-usage"
3233
3334import {
3435 type AnthropicMessagesPayload ,
@@ -46,10 +47,37 @@ interface OutLogOptions {
4647 model : string
4748 chunks : number
4849 done : boolean
50+ premium ?: { remaining : number ; total : number } | null
4951}
5052
51- const formatOutLog = ( { model, chunks, done } : OutLogOptions ) : string =>
52- `\x1b[2K\r↪ ${ model } ${ chunks } ${ done ? " ✓" : "" } `
53+ const formatOutLog = ( {
54+ model,
55+ chunks,
56+ done,
57+ premium,
58+ } : OutLogOptions ) : string => {
59+ const base = `\x1b[2K\r↪ ${ model } ${ chunks } ${ done ? " ✓" : "" } `
60+ if ( done && premium ) {
61+ return `${ base } [${ premium . remaining } left]`
62+ }
63+ return base
64+ }
65+
66+ const getPremiumInfo = async ( ) : Promise < {
67+ remaining : number
68+ total : number
69+ } | null > => {
70+ try {
71+ const usage = await getCopilotUsage ( )
72+ const pi = usage . quota_snapshots . premium_interactions
73+ if ( ! pi . unlimited ) {
74+ return { remaining : pi . remaining , total : pi . entitlement }
75+ }
76+ } catch {
77+ // Ignore errors, don't affect main flow
78+ }
79+ return null
80+ }
5381
5482export async function handleCompletion ( c : Context ) {
5583 await checkRateLimit ( state )
@@ -102,8 +130,9 @@ const handleWithChatCompletions = async (
102130 "Translated Anthropic response:" ,
103131 JSON . stringify ( anthropicResponse ) ,
104132 )
133+ const premium = await getPremiumInfo ( )
105134 process . stdout . write (
106- `${ formatOutLog ( { model : openAIPayload . model , chunks : 0 , done : true } ) } \n` ,
135+ `${ formatOutLog ( { model : openAIPayload . model , chunks : 0 , done : true , premium } ) } \n` ,
107136 )
108137 return c . json ( anthropicResponse )
109138 }
@@ -154,8 +183,9 @@ const handleWithChatCompletions = async (
154183 }
155184 } finally {
156185 clearInterval ( pingInterval )
186+ const premium = await getPremiumInfo ( )
157187 process . stdout . write (
158- `${ formatOutLog ( { model : openAIPayload . model , chunks : chunkCount , done : true } ) } \n` ,
188+ `${ formatOutLog ( { model : openAIPayload . model , chunks : chunkCount , done : true , premium } ) } \n` ,
159189 )
160190 }
161191 } )
@@ -245,8 +275,9 @@ const handleWithResponsesApi = async (
245275 }
246276 } finally {
247277 clearInterval ( pingInterval )
278+ const premium = await getPremiumInfo ( )
248279 process . stdout . write (
249- `${ formatOutLog ( { model : responsesPayload . model , chunks : chunkCount , done : true } ) } \n` ,
280+ `${ formatOutLog ( { model : responsesPayload . model , chunks : chunkCount , done : true , premium } ) } \n` ,
250281 )
251282 }
252283 } )
@@ -263,8 +294,9 @@ const handleWithResponsesApi = async (
263294 "Translated Anthropic response:" ,
264295 JSON . stringify ( anthropicResponse ) ,
265296 )
297+ const premium = await getPremiumInfo ( )
266298 process . stdout . write (
267- `${ formatOutLog ( { model : responsesPayload . model , chunks : 0 , done : true } ) } \n` ,
299+ `${ formatOutLog ( { model : responsesPayload . model , chunks : 0 , done : true , premium } ) } \n` ,
268300 )
269301 return c . json ( anthropicResponse )
270302}
0 commit comments