@@ -4,127 +4,72 @@ import { FetchError } from "ofetch"
44
55import { PATHS } from "~/lib/paths"
66import { getGitHubUser } from "~/services/github/get-user/service"
7-
8- import type { getOptions } from "./cli"
7+ import { tokenService } from "~/services/token-service"
98
109import { getModels } from "../services/copilot/get-models/service"
11- import { getCopilotToken } from "../services/copilot/get-token/copilot-token"
1210import { getGitHubToken } from "../services/github/get-token/service"
13- import { initializeLogger } from "./logger"
14- import { getGithubToken , saveGithubToken , TOKENS } from "./tokens"
1511
16- interface InitStep {
17- name : string
18- run : ( ) => Promise < void > | void
12+ // Extract to individual functions for each initialization step
13+ async function initializeAppDirectory ( ) : Promise < void > {
14+ await fs . mkdir ( PATHS . APP_DIR , { recursive : true } )
15+ await fs . writeFile ( PATHS . GITHUB_TOKEN_PATH , "" , { flag : "a" } )
1916}
2017
21- const initSteps : Array < InitStep > = [
22- {
23- name : "App directory" ,
24- run : async ( ) => {
25- await fs . mkdir ( PATHS . APP_DIR , { recursive : true } )
26- await fs . writeFile ( PATHS . GITHUB_TOKEN_PATH , "" , { flag : "a" } )
27- } ,
28- } ,
29- {
30- name : "GitHub authentication" ,
31- run : async ( ) => {
32- TOKENS . GITHUB_TOKEN = await getGithubToken ( )
18+ async function initializeGithubAuthentication ( ) : Promise < void > {
19+ const githubToken = await tokenService . getGithubToken ( )
3320
34- try {
35- await logUser ( )
36- } catch ( error ) {
37- if ( ! ( error instanceof FetchError ) ) throw error
38- if ( error . statusCode !== 401 ) {
39- consola . error ( "Authentication error:" , {
40- error,
41- request : error . request ,
42- options : error . options ,
43- response : error . response ,
44- data : error . response ?. _data as Record < string , unknown > ,
45- } )
46- throw error
47- }
21+ try {
22+ if ( githubToken ) {
23+ // Set token in the service so github fetcher can use it
24+ await tokenService . setGithubToken ( githubToken )
25+ await logUser ( )
26+ } else {
27+ throw new Error ( "No GitHub token available" )
28+ }
29+ } catch ( error ) {
30+ if ( error instanceof FetchError && error . statusCode !== 401 ) {
31+ consola . error ( "Authentication error:" , {
32+ error,
33+ request : error . request ,
34+ options : error . options ,
35+ response : error . response ,
36+ data : error . response ?. _data as Record < string , unknown > ,
37+ } )
38+ throw error
39+ }
4840
49- consola . info ( "Not logged in, getting new access token" )
50- TOKENS . GITHUB_TOKEN = await initializeGithubToken ( )
51- await logUser ( )
52- }
53- } ,
54- } ,
55- {
56- name : "Copilot token" ,
57- run : async ( ) => {
58- const { token, refresh_in } = await getCopilotToken ( )
59- TOKENS . COPILOT_TOKEN = token
41+ consola . info ( "Not logged in, getting new access token" )
42+ const newToken = await initializeGithubToken ( )
43+ await tokenService . setGithubToken ( newToken )
44+ await logUser ( )
45+ }
46+ }
6047
61- const refreshInterval = ( refresh_in - 60 ) * 1000
62- setInterval ( async ( ) => {
63- consola . start ( "Refreshing copilot token" )
64- const { token : newToken } = await getCopilotToken ( )
65- TOKENS . COPILOT_TOKEN = newToken
66- } , refreshInterval )
67- } ,
68- } ,
69- {
70- name : "Model information" ,
71- run : async ( ) => {
72- const models = await getModels ( )
73- consola . info (
74- `Available models: \n${ models . data . map ( ( model ) => `- ${ model . id } ` ) . join ( "\n" ) } ` ,
75- )
76- } ,
77- } ,
78- ]
48+ async function initializeCopilotToken ( ) : Promise < void > {
49+ await tokenService . initCopilotToken ( )
50+ }
51+
52+ async function initializeModelInformation ( ) : Promise < void > {
53+ const models = await getModels ( )
54+ consola . info (
55+ `Available models: \n${ models . data . map ( ( model ) => `- ${ model . id } ` ) . join ( "\n" ) } ` ,
56+ )
57+ }
7958
8059async function initializeGithubToken ( ) {
8160 consola . start ( "Getting GitHub device code" )
82- const token = await getGitHubToken ( )
83- await saveGithubToken ( token . access_token )
84- return token . access_token
61+ const tokenResponse = await getGitHubToken ( )
62+ return tokenResponse . access_token
8563}
8664
8765async function logUser ( ) {
8866 const user = await getGitHubUser ( )
8967 consola . info ( `Logged in as ${ JSON . stringify ( user . login ) } ` )
9068}
9169
92- import { configManager } from "./config"
93- import { initializePort } from "./port"
94-
95- export async function initializeApp (
96- options : Awaited < ReturnType < typeof getOptions > > ,
97- ) {
98- configManager . setConfig ( {
99- EMULATE_STREAMING : options [ "emulate-streaming" ] ,
100- LOGGING_ENABLED : options . logs ,
101- } )
102-
103- // Get available port, trying the CLI option first
104- const port = await initializePort ( )
105-
106- // Initialize logger if enabled
107- await initializeLogger ( )
108-
109- await initialize ( )
110-
111- const serverUrl = `http://localhost:${ port } `
112- consola . success ( `Server started at ${ serverUrl } ` )
113-
114- return {
115- port,
116- }
117- }
118-
119- async function initialize ( ) {
120- for ( const step of initSteps ) {
121- try {
122- consola . start ( `Initializing ${ step . name } ...` )
123- await step . run ( )
124- consola . success ( `${ step . name } initialized` )
125- } catch ( error ) {
126- consola . error ( `Failed to initialize ${ step . name } :` , error )
127- throw error
128- }
129- }
70+ export async function initializeApp ( ) {
71+ await initializeAppDirectory ( )
72+ await initializeGithubAuthentication ( )
73+ await initializeCopilotToken ( )
74+ await initializeModelInformation ( )
13075}
0 commit comments