11import consola from "consola"
2+ import fs from "node:fs"
23
4+ import { CACHE } from "./lib/cache"
5+ import { PATHS } from "./lib/paths"
36import { TOKENS } from "./lib/tokens"
4- import { chatCompletions } from "./services/copilot-vscode/chat-completions/service"
7+ import { server } from "./server"
8+ import { getModels } from "./services/copilot-vscode/get-models/service"
59import { getCopilotToken } from "./services/copilot-vscode/get-token/copilot-token"
610import { getGitHubToken } from "./services/copilot-vscode/get-token/github-token"
711
8- const githubToken = await getGitHubToken ( )
12+ if ( ! fs . existsSync ( PATHS . PATH_CACHE_FILE ) ) {
13+ fs . mkdirSync ( PATHS . DIR_CACHE , { recursive : true } )
14+ await CACHE . _write ( { } )
15+ }
16+
17+ let githubToken : string
18+
19+ const cachedGithubToken = await CACHE . get ( "github-token" )
20+
21+ const FOUR_HOURS = 4 * 60 * 60 * 1000
22+
23+ // If exists and at most 4 hours old
24+ if (
25+ cachedGithubToken &&
26+ Date . now ( ) - cachedGithubToken . createdAt < FOUR_HOURS
27+ ) {
28+ githubToken = cachedGithubToken . value
29+ } else {
30+ githubToken = await getGitHubToken ( )
31+ await CACHE . set ( "github-token" , githubToken )
32+ }
33+
934TOKENS . GITHUB_TOKEN = githubToken
1035
1136const { token : copilotToken , refresh_in } = await getCopilotToken ( )
@@ -21,17 +46,10 @@ setInterval(async () => {
2146 TOKENS . COPILOT_TOKEN = copilotToken
2247} , refreshInterval )
2348
24- const response = await chatCompletions ( {
25- messages : [
26- {
27- role : "user" ,
28- content : "Write a function that returns the sum of two numbers" ,
29- } ,
30- ] ,
31- model : "gpt-4o-mini" ,
32- stream : false ,
33- } )
49+ const models = await getModels ( )
3450
35- console . log ( response )
51+ consola . info (
52+ `Available models: ${ models . data . map ( ( model ) => model . id ) . join ( "\n" ) } ` ,
53+ )
3654
37- await Bun . write ( "response.json" , JSON . stringify ( response , null , 2 ) )
55+ export default server
0 commit comments