11import consola from "consola"
22import fs from "node:fs"
3+ import { FetchError } from "ofetch"
34
45import { ENV } from "~/config/env"
6+ import { getGitHubUser } from "~/services/github/get-user/service"
57
68import { PATHS } from "../config/paths"
79import { TOKENS } from "../config/tokens"
810import { getModels } from "../services/copilot/get-models/service"
911import { getCopilotToken } from "../services/copilot/get-token/copilot-token"
10- import { getGitHubToken } from "../services/copilot /get-token/github-token "
12+ import { getGitHubToken } from "../services/github /get-token/service "
1113import { CACHE } from "./cache"
1214
15+ async function getCachedGithubToken ( ) {
16+ const cachedToken = await CACHE . get ( "github-token" )
17+ return cachedToken ?. value
18+ }
19+
1320async function initializeGithubToken ( ) {
14- const EIGHT_HOURS = 8 * 60 * 60 * 1000
15- const cachedGithubToken = await CACHE . get ( "github-token" )
16-
17- // If exists and at most 8 hours old
18- if (
19- cachedGithubToken &&
20- Date . now ( ) - cachedGithubToken . createdAt < EIGHT_HOURS
21- ) {
22- return cachedGithubToken . value
23- }
21+ consola . start ( "Getting GitHub device code" )
22+ const token = await getGitHubToken ( )
2423
25- const newToken = await getGitHubToken ( )
26- await CACHE . set ( "github-token" , newToken )
27- return newToken
24+ await CACHE . set ( "github-token" , token . access_token )
25+ return token . access_token
2826}
2927
3028async function initializeCopilotToken ( ) {
3129 const { token, refresh_in } = await getCopilotToken ( )
3230 TOKENS . COPILOT_TOKEN = token
3331
3432 // refresh_in is in seconds
35- // we're refreshing 10 minutes (600 seconds) early
36- const refreshInterval = ( refresh_in - 600 ) * 1000
33+ // we're refreshing 1 minute (60 seconds) early
34+ const refreshInterval = ( refresh_in - 60 ) * 1000
3735
3836 setInterval ( async ( ) => {
3937 consola . start ( "Refreshing copilot token" )
@@ -58,13 +56,36 @@ async function logAvailableModels() {
5856 )
5957}
6058
59+ async function logUser ( ) {
60+ const user = await getGitHubUser ( )
61+ consola . info ( `Logged in as ${ JSON . stringify ( user . login ) } ` )
62+ }
63+
6164export async function initialize ( ) {
6265 if ( ENV . EMULATE_STREAMING ) consola . box ( "Streaming emulation is enabled." )
6366
6467 await initializeCache ( )
6568
66- // Initialize tokens
67- TOKENS . GITHUB_TOKEN = await initializeGithubToken ( )
69+ TOKENS . GITHUB_TOKEN = await getCachedGithubToken ( )
70+
71+ try {
72+ await logUser ( )
73+ } catch ( error ) {
74+ if ( ! ( error instanceof FetchError ) ) throw error
75+ consola . log (
76+ error ,
77+ error . request ,
78+ error . options ,
79+ error . response ,
80+ error . response ?. _data ,
81+ )
82+ if ( error . statusCode !== 401 ) throw error
83+
84+ consola . info ( "Not logged in, getting new access token" )
85+ TOKENS . GITHUB_TOKEN = await initializeGithubToken ( )
86+ await logUser ( )
87+ }
88+
6889 await initializeCopilotToken ( )
6990
7091 // Log available models
0 commit comments