Skip to content

Commit 5557911

Browse files
committed
feat: Add initialization module and refactor main.ts
1 parent 4061936 commit 5557911

2 files changed

Lines changed: 69 additions & 68 deletions

File tree

src/lib/initialization/index.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import consola from "consola"
2+
import fs from "node:fs"
3+
4+
import { PATHS } from "../../config/paths"
5+
import { TOKENS } from "../../config/tokens"
6+
import { getModels } from "../../services/copilot/get-models/service"
7+
import { getCopilotToken } from "../../services/copilot/get-token/copilot-token"
8+
import { getGitHubToken } from "../../services/copilot/get-token/github-token"
9+
import { CACHE } from "../cache"
10+
11+
async function initializeGithubToken() {
12+
const EIGHT_HOURS = 8 * 60 * 60 * 1000
13+
const cachedGithubToken = await CACHE.get("github-token")
14+
15+
// If exists and at most 8 hours old
16+
if (
17+
cachedGithubToken &&
18+
Date.now() - cachedGithubToken.createdAt < EIGHT_HOURS
19+
) {
20+
return cachedGithubToken.value
21+
}
22+
23+
const newToken = await getGitHubToken()
24+
await CACHE.set("github-token", newToken)
25+
return newToken
26+
}
27+
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 10 minutes (600 seconds) early
34+
const refreshInterval = (refresh_in - 600) * 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")}`,
56+
)
57+
}
58+
59+
export async function initialize() {
60+
await initializeCache()
61+
62+
// Initialize tokens
63+
TOKENS.GITHUB_TOKEN = await initializeGithubToken()
64+
await initializeCopilotToken()
65+
66+
// Log available models
67+
await logAvailableModels()
68+
}

src/main.ts

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,7 @@
11
import type { Serve } from "bun"
22

3-
import consola from "consola"
4-
import fs from "node:fs"
5-
6-
import { PATHS } from "./config/paths"
7-
import { TOKENS } from "./config/tokens"
8-
import { CACHE } from "./lib/cache"
3+
import { initialize } from "./lib/initialization"
94
import { server } from "./server"
10-
import { getModels } from "./services/copilot/get-models/service"
11-
import { getCopilotToken } from "./services/copilot/get-token/copilot-token"
12-
import { getGitHubToken } from "./services/copilot/get-token/github-token"
13-
14-
async function initializeGithubToken() {
15-
const EIGHT_HOURS = 8 * 60 * 60 * 1000
16-
const cachedGithubToken = await CACHE.get("github-token")
17-
18-
// If exists and at most 8 hours old
19-
if (
20-
cachedGithubToken &&
21-
Date.now() - cachedGithubToken.createdAt < EIGHT_HOURS
22-
) {
23-
return cachedGithubToken.value
24-
}
25-
26-
const newToken = await getGitHubToken()
27-
await CACHE.set("github-token", newToken)
28-
return newToken
29-
}
30-
31-
async function initializeCopilotToken() {
32-
const { token, refresh_in } = await getCopilotToken()
33-
TOKENS.COPILOT_TOKEN = token
34-
35-
// refresh_in is in seconds
36-
// we're refreshing 10 minutes (600 seconds) early
37-
const refreshInterval = (refresh_in - 600) * 1000
38-
39-
setInterval(async () => {
40-
consola.start("Refreshing copilot token")
41-
const { token: newToken } = await getCopilotToken()
42-
TOKENS.COPILOT_TOKEN = newToken
43-
}, refreshInterval)
44-
45-
return token
46-
}
47-
48-
async function initializeCache() {
49-
if (!fs.existsSync(PATHS.PATH_CACHE_FILE)) {
50-
fs.mkdirSync(PATHS.DIR_CACHE, { recursive: true })
51-
await CACHE._write({})
52-
}
53-
}
54-
55-
async function logAvailableModels() {
56-
const models = await getModels()
57-
consola.info(
58-
`Available models: \n${models.data.map((model) => `- ${model.id}`).join("\n")}`,
59-
)
60-
}
61-
62-
async function initialize() {
63-
await initializeCache()
64-
65-
// Initialize tokens
66-
TOKENS.GITHUB_TOKEN = await initializeGithubToken()
67-
await initializeCopilotToken()
68-
69-
// Log available models
70-
await logAvailableModels()
71-
}
725

736
// Initialize the application
747
await initialize()

0 commit comments

Comments
 (0)