@@ -9,47 +9,66 @@ import { getModels } from "./services/copilot/get-models/service"
99import { getCopilotToken } from "./services/copilot/get-token/copilot-token"
1010import { getGitHubToken } from "./services/copilot/get-token/github-token"
1111
12- if ( ! fs . existsSync ( PATHS . PATH_CACHE_FILE ) ) {
13- fs . mkdirSync ( PATHS . DIR_CACHE , { recursive : true } )
14- await CACHE . _write ( { } )
12+ async function initializeGithubToken ( ) {
13+ const EIGHT_HOURS = 8 * 60 * 60 * 1000
14+ const cachedGithubToken = await CACHE . get ( "github-token" )
15+
16+ // If exists and at most 8 hours old
17+ if (
18+ cachedGithubToken &&
19+ Date . now ( ) - cachedGithubToken . createdAt < EIGHT_HOURS
20+ ) {
21+ return cachedGithubToken . value
22+ }
23+
24+ const newToken = await getGitHubToken ( )
25+ await CACHE . set ( "github-token" , newToken )
26+ return newToken
1527}
1628
17- let githubToken : string
29+ async function initializeCopilotToken ( ) {
30+ const { token, refresh_in } = await getCopilotToken ( )
31+ TOKENS . COPILOT_TOKEN = token
1832
19- const cachedGithubToken = await CACHE . get ( "github-token" )
33+ // refresh_in is in minutes
34+ // we're refreshing 100 minutes early
35+ const refreshInterval = ( refresh_in - 100 ) * 60 * 1000
2036
21- const EIGHT_HOURS = 8 * 60 * 60 * 1000
37+ setInterval ( async ( ) => {
38+ consola . start ( "Refreshing copilot token" )
39+ const { token : newToken } = await getCopilotToken ( )
40+ TOKENS . COPILOT_TOKEN = newToken
41+ } , refreshInterval )
2242
23- // If exists and at most 8 hours old
24- if (
25- cachedGithubToken &&
26- Date . now ( ) - cachedGithubToken . createdAt < EIGHT_HOURS
27- ) {
28- githubToken = cachedGithubToken . value
29- } else {
30- githubToken = await getGitHubToken ( )
31- await CACHE . set ( "github-token" , githubToken )
43+ return token
3244}
3345
34- TOKENS . GITHUB_TOKEN = githubToken
46+ async function initializeCache ( ) {
47+ if ( ! fs . existsSync ( PATHS . PATH_CACHE_FILE ) ) {
48+ fs . mkdirSync ( PATHS . DIR_CACHE , { recursive : true } )
49+ await CACHE . _write ( { } )
50+ }
51+ }
3552
36- const { token : copilotToken , refresh_in } = await getCopilotToken ( )
37- TOKENS . COPILOT_TOKEN = copilotToken
53+ async function logAvailableModels ( ) {
54+ const models = await getModels ( )
55+ consola . info (
56+ `Available models: \n${ models . data . map ( ( model ) => `- ${ model . id } ` ) . join ( "\n" ) } ` ,
57+ )
58+ }
3859
39- // refresh_in is in minutes
40- // we're refreshing 100 minutes early
41- const refreshInterval = ( refresh_in - 100 ) * 60 * 1000
60+ async function initialize ( ) {
61+ await initializeCache ( )
4262
43- setInterval ( async ( ) => {
44- consola . start ( "Refreshing copilot token" )
45- const { token : copilotToken } = await getCopilotToken ( )
46- TOKENS . COPILOT_TOKEN = copilotToken
47- } , refreshInterval )
63+ // Initialize tokens
64+ TOKENS . GITHUB_TOKEN = await initializeGithubToken ( )
65+ await initializeCopilotToken ( )
4866
49- const models = await getModels ( )
67+ // Log available models
68+ await logAvailableModels ( )
69+ }
5070
51- consola . info (
52- `Available models: \n${ models . data . map ( ( model ) => `- ${ model . id } ` ) . join ( "\n" ) } ` ,
53- )
71+ // Initialize the application
72+ await initialize ( )
5473
5574export default server
0 commit comments