11import consola from "consola"
2- import { FetchError } from "ofetch"
32
43import { ensurePaths } from "~/lib/paths"
5- import { tokenService } from "~/lib/token"
4+ import { readGithubToken , tokenService , writeGithubToken } from "~/lib/token"
5+ import { getDeviceCode } from "~/services/github/get-device-code"
66import { getGitHubUser } from "~/services/github/get-user/service"
7+ import { pollAccessToken } from "~/services/github/poll-access-token"
78
89import { getModels } from "../services/copilot/get-models/service"
9- import { getGitHubToken } from "../services/github/get-token/service "
10+ import { state } from "./state "
1011
11- async function initializeGithubAuthentication ( ) : Promise < void > {
12- const githubToken = await tokenService . getGithubToken ( )
12+ async function logUser ( ) {
13+ const user = await getGitHubUser ( )
14+ consola . info ( `Logged in as ${ JSON . stringify ( user . login ) } \n` )
15+ }
1316
14- try {
15- if ( githubToken ) {
16- // Set token in the service so github fetcher can use it
17- await tokenService . setGithubToken ( githubToken )
18- await logUser ( )
19- } else {
20- throw new Error ( "No GitHub token available" )
21- }
22- } catch ( error ) {
23- if ( error instanceof FetchError && error . statusCode !== 401 ) {
24- consola . error ( "Authentication error:" , {
25- error,
26- request : error . request ,
27- options : error . options ,
28- response : error . response ,
29- data : error . response ?. _data as Record < string , unknown > ,
30- } )
31- throw error
32- }
17+ async function setupGitHubToken ( ) : Promise < void > {
18+ const githubToken = await readGithubToken ( )
3319
34- consola . info ( "Not logged in, getting new access token" )
35- const newToken = await initializeGithubToken ( )
36- await tokenService . setGithubToken ( newToken )
20+ if ( githubToken ) {
21+ state . githubToken = githubToken
3722 await logUser ( )
23+
24+ return
3825 }
26+
27+ consola . info ( "Not logged in, getting new access token" )
28+ const response = await getDeviceCode ( )
29+
30+ consola . info (
31+ `Please enter the code "${ response . user_code } " in ${ response . verification_uri } ` ,
32+ )
33+
34+ const token = await pollAccessToken ( response )
35+ await writeGithubToken ( token )
36+ state . githubToken = token
37+
38+ await logUser ( )
3939}
4040
4141async function initializeCopilotToken ( ) : Promise < void > {
@@ -49,19 +49,9 @@ async function logModelInformation(): Promise<void> {
4949 )
5050}
5151
52- async function initializeGithubToken ( ) {
53- consola . start ( "Getting GitHub device code" )
54- return await getGitHubToken ( )
55- }
56-
57- async function logUser ( ) {
58- const user = await getGitHubUser ( )
59- consola . info ( `Logged in as ${ JSON . stringify ( user . login ) } \n` )
60- }
61-
6252export async function initializeApp ( ) {
6353 await ensurePaths ( )
64- await initializeGithubAuthentication ( )
54+ await setupGitHubToken ( )
6555 await initializeCopilotToken ( )
6656 await logModelInformation ( )
6757}
0 commit comments