Skip to content

Commit da49444

Browse files
committed
feat: Add configuration and logging functionality to the app
1 parent e158012 commit da49444

7 files changed

Lines changed: 41 additions & 7 deletions

File tree

src/config/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const CONFIG = {
2+
EMULATE_STREAMING: false,
3+
LOGGING_ENABLED: false,
4+
}

src/config/env.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ const GITHUB_OAUTH_SCOPES = [
1111
export const ENV = {
1212
GITHUB_CLIENT_ID,
1313
GITHUB_OAUTH_SCOPES,
14-
EMULATE_STREAMING: false,
1514
}

src/config/paths.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import path from "pathe"
44
const APP_DIR = path.join(os.homedir(), ".local", "share", "copilot-api")
55

66
const CACHE_PATH = path.join(APP_DIR, "cache.json")
7+
const LOG_PATH = path.join(APP_DIR, "logs")
8+
const LOG_FILE = path.join(LOG_PATH, "app.log")
79

810
export const PATHS = {
911
APP_DIR,
1012
CACHE_PATH,
13+
LOG_PATH,
14+
LOG_FILE,
1115
}

src/lib/cli.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ const args = {
1818
default: "4141",
1919
description: "Port to listen on",
2020
},
21+
logs: {
22+
type: "boolean",
23+
default: false,
24+
description: "Write logs to the app directory",
25+
},
2126
} satisfies ArgsDef
2227

2328
export async function parseCli() {

src/lib/initialization.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import consola from "consola"
22
import fs from "node:fs"
33
import { FetchError } from "ofetch"
44

5-
import { ENV } from "~/config/env"
5+
import { CONFIG } from "~/config/config"
6+
import { PATHS } from "~/config/paths"
67
import { getGitHubUser } from "~/services/github/get-user/service"
78

89
import type { parseCli } from "./cli"
910

10-
import { PATHS } from "../config/paths"
1111
import { TOKENS } from "../config/tokens"
1212
import { getModels } from "../services/copilot/get-models/service"
1313
import { getCopilotToken } from "../services/copilot/get-token/copilot-token"
1414
import { getGitHubToken } from "../services/github/get-token/service"
1515
import { CACHE } from "./cache"
16+
import { initializeLogger } from "./logger"
1617

1718
interface InitStep {
1819
name: string
@@ -23,7 +24,7 @@ const initSteps: Array<InitStep> = [
2324
{
2425
name: "Emulation check",
2526
run: () => {
26-
if (ENV.EMULATE_STREAMING) {
27+
if (CONFIG.EMULATE_STREAMING) {
2728
consola.box("Streaming emulation is enabled.")
2829
}
2930
},
@@ -108,7 +109,11 @@ async function logUser() {
108109
export async function initializeApp(
109110
options: Awaited<ReturnType<typeof parseCli>>,
110111
) {
111-
ENV.EMULATE_STREAMING = options["emulate-streaming"]
112+
CONFIG.EMULATE_STREAMING = options["emulate-streaming"]
113+
CONFIG.LOGGING_ENABLED = options.logs
114+
115+
// Initialize logger if enabled
116+
initializeLogger()
112117

113118
await initialize()
114119

src/lib/logger.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import fs from "node:fs"
2+
3+
import { CONFIG } from "~/config/config"
4+
import { PATHS } from "~/config/paths"
5+
6+
export function initializeLogger() {
7+
if (!CONFIG.LOGGING_ENABLED) return
8+
9+
fs.mkdirSync(PATHS.LOG_PATH, { recursive: true })
10+
}
11+
12+
export function logToFile(type: string, message: string) {
13+
if (!CONFIG.LOGGING_ENABLED) return
14+
15+
const timestamp = new Date().toISOString()
16+
fs.appendFileSync(PATHS.LOG_FILE, `${timestamp} ${type}: ${message}\n`)
17+
}

src/routes/chat-completions/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import consola from "consola"
22
import { Hono } from "hono"
33
import { FetchError } from "ofetch"
44

5-
import { ENV } from "~/config/env"
5+
import { CONFIG } from "~/config/config"
66

77
import { handler } from "./handler"
88
import { handlerStreaming } from "./handler-streaming"
@@ -11,7 +11,7 @@ export const completionRoutes = new Hono()
1111

1212
completionRoutes.post("/chat/completions", async (c) => {
1313
try {
14-
if (ENV.EMULATE_STREAMING) {
14+
if (CONFIG.EMULATE_STREAMING) {
1515
return await handler(c)
1616
}
1717

0 commit comments

Comments
 (0)