|
1 | 1 | import type { Serve } from "bun" |
2 | 2 |
|
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" |
9 | 4 | 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 | | -} |
72 | 5 |
|
73 | 6 | // Initialize the application |
74 | 7 | await initialize() |
|
0 commit comments