@@ -12,6 +12,80 @@ import { getCopilotToken } from "../services/copilot/get-token/copilot-token"
1212import { getGitHubToken } from "../services/github/get-token/service"
1313import { CACHE } from "./cache"
1414
15+ interface InitStep {
16+ name : string
17+ run : ( ) => Promise < void > | void
18+ }
19+
20+ const initSteps : Array < InitStep > = [
21+ {
22+ name : "Emulation check" ,
23+ run : ( ) => {
24+ if ( ENV . EMULATE_STREAMING ) {
25+ consola . box ( "Streaming emulation is enabled." )
26+ }
27+ } ,
28+ } ,
29+ {
30+ name : "Cache" ,
31+ run : async ( ) => {
32+ if ( ! fs . existsSync ( PATHS . PATH_CACHE_FILE ) ) {
33+ fs . mkdirSync ( PATHS . DIR_CACHE , { recursive : true } )
34+ await CACHE . _write ( { } )
35+ }
36+ } ,
37+ } ,
38+ {
39+ name : "GitHub authentication" ,
40+ run : async ( ) => {
41+ TOKENS . GITHUB_TOKEN = await getCachedGithubToken ( )
42+
43+ try {
44+ await logUser ( )
45+ } catch ( error ) {
46+ if ( ! ( error instanceof FetchError ) ) throw error
47+ if ( error . statusCode !== 401 ) {
48+ consola . error ( "Authentication error:" , {
49+ error,
50+ request : error . request ,
51+ options : error . options ,
52+ response : error . response ,
53+ data : error . response ?. _data as Record < string , unknown > ,
54+ } )
55+ throw error
56+ }
57+
58+ consola . info ( "Not logged in, getting new access token" )
59+ TOKENS . GITHUB_TOKEN = await initializeGithubToken ( )
60+ await logUser ( )
61+ }
62+ } ,
63+ } ,
64+ {
65+ name : "Copilot token" ,
66+ run : async ( ) => {
67+ const { token, refresh_in } = await getCopilotToken ( )
68+ TOKENS . COPILOT_TOKEN = token
69+
70+ const refreshInterval = ( refresh_in - 60 ) * 1000
71+ setInterval ( async ( ) => {
72+ consola . start ( "Refreshing copilot token" )
73+ const { token : newToken } = await getCopilotToken ( )
74+ TOKENS . COPILOT_TOKEN = newToken
75+ } , refreshInterval )
76+ } ,
77+ } ,
78+ {
79+ name : "Model information" ,
80+ run : async ( ) => {
81+ const models = await getModels ( )
82+ consola . info (
83+ `Available models: \n${ models . data . map ( ( model ) => `- ${ model . id } ` ) . join ( "\n" ) } \n` ,
84+ )
85+ } ,
86+ } ,
87+ ]
88+
1589async function getCachedGithubToken ( ) {
1690 const cachedToken = await CACHE . get ( "github-token" )
1791 return cachedToken ?. value
@@ -20,74 +94,24 @@ async function getCachedGithubToken() {
2094async function initializeGithubToken ( ) {
2195 consola . start ( "Getting GitHub device code" )
2296 const token = await getGitHubToken ( )
23-
2497 await CACHE . set ( "github-token" , token . access_token )
2598 return token . access_token
2699}
27100
28- async function initializeCopilotToken ( ) {
29- const { token, refresh_in } = await getCopilotToken ( )
30- TOKENS . COPILOT_TOKEN = token
31-
32- // refresh_in is in seconds
33- // we're refreshing 1 minute (60 seconds) early
34- const refreshInterval = ( refresh_in - 60 ) * 1000
35-
36- setInterval ( async ( ) => {
37- consola . start ( "Refreshing copilot token" )
38- const { token : newToken } = await getCopilotToken ( )
39- TOKENS . COPILOT_TOKEN = newToken
40- } , refreshInterval )
41-
42- return token
43- }
44-
45- async function initializeCache ( ) {
46- if ( ! fs . existsSync ( PATHS . PATH_CACHE_FILE ) ) {
47- fs . mkdirSync ( PATHS . DIR_CACHE , { recursive : true } )
48- await CACHE . _write ( { } )
49- }
50- }
51-
52- async function logAvailableModels ( ) {
53- const models = await getModels ( )
54- consola . info (
55- `Available models: \n${ models . data . map ( ( model ) => `- ${ model . id } ` ) . join ( "\n" ) } \n` ,
56- )
57- }
58-
59101async function logUser ( ) {
60102 const user = await getGitHubUser ( )
61103 consola . info ( `Logged in as ${ JSON . stringify ( user . login ) } ` )
62104}
63105
64106export async function initialize ( ) {
65- if ( ENV . EMULATE_STREAMING ) consola . box ( "Streaming emulation is enabled." )
66-
67- await initializeCache ( )
68-
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 ( )
107+ for ( const step of initSteps ) {
108+ try {
109+ consola . start ( `Initializing ${ step . name } ...` )
110+ await step . run ( )
111+ consola . success ( `${ step . name } initialized` )
112+ } catch ( error ) {
113+ consola . error ( `Failed to initialize ${ step . name } :` , error )
114+ throw error
115+ }
87116 }
88-
89- await initializeCopilotToken ( )
90-
91- // Log available models
92- await logAvailableModels ( )
93117}
0 commit comments