Skip to content

Commit 31319d5

Browse files
committed
refactor: Simplify GitHub token initialization and logging process
1 parent e935d0d commit 31319d5

2 files changed

Lines changed: 39 additions & 77 deletions

File tree

src/lib/initialization.ts

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
11
import consola from "consola"
22
import fs from "node:fs"
3+
import { FetchError } from "ofetch"
34

45
import { ENV } from "~/config/env"
6+
import { getGitHubUser } from "~/services/github/get-user/service"
57

68
import { PATHS } from "../config/paths"
79
import { TOKENS } from "../config/tokens"
810
import { getModels } from "../services/copilot/get-models/service"
911
import { 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"
1113
import { CACHE } from "./cache"
1214

15+
async function getCachedGithubToken() {
16+
const cachedToken = await CACHE.get("github-token")
17+
return cachedToken?.value
18+
}
19+
1320
async 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

3028
async 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+
6164
export 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

src/services/copilot/get-token/github-token.ts

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)