diff --git a/packages/cli/src/cli.tsx b/packages/cli/src/cli.tsx index c595916b..d7d008a4 100644 --- a/packages/cli/src/cli.tsx +++ b/packages/cli/src/cli.tsx @@ -2,13 +2,14 @@ import React from "react"; import { render } from "ink"; import { setShellIfWindows } from "@vegamo/deepcode-core"; import { checkForNpmUpdate, promptForPendingUpdate, type PackageInfo } from "./common/update-check"; +import { APP_NAME, APP_VERSION } from "./common/package-info"; import { AppContainer } from "./ui"; const args = process.argv.slice(2); -const packageInfo = readPackageInfo(); +const packageInfo: PackageInfo = { name: APP_NAME, version: APP_VERSION }; if (args.includes("--version") || args.includes("-v")) { - process.stdout.write(`${packageInfo.version || "unknown"}\n`); + process.stdout.write(`${APP_VERSION}\n`); process.exit(0); } @@ -129,15 +130,3 @@ function configureWindowsShell(): void { process.exit(1); } } - -function readPackageInfo(): PackageInfo { - try { - const pkg = require("../package.json") as { name?: unknown; version?: unknown }; - return { - name: typeof pkg.name === "string" ? pkg.name : "@vegamo/deepcode-cli", - version: typeof pkg.version === "string" ? pkg.version : "", - }; - } catch { - return { name: "@vegamo/deepcode-cli", version: "" }; - } -} diff --git a/packages/core/src/common/openai-client.ts b/packages/core/src/common/openai-client.ts index d3b56c08..d73c8a51 100644 --- a/packages/core/src/common/openai-client.ts +++ b/packages/core/src/common/openai-client.ts @@ -4,6 +4,7 @@ import * as path from "path"; import OpenAI from "openai"; import { Agent, fetch as undiciFetch } from "undici"; import { resolveCurrentSettings } from "../settings"; +import { APP_VERSION } from "./package-info"; // Custom undici Agent with a 180-second keepAlive timeout. The default // global fetch (undici) only keeps connections alive for 4 seconds, which @@ -11,6 +12,7 @@ import { resolveCurrentSettings } from "../settings"; // output between prompts. By passing a dedicated Agent to undiciFetch we // keep connections reusable for three minutes after the last request. const keepAliveAgent = new Agent({ keepAliveTimeout: 180_000 }); +const USER_AGENT = `deepcode-cli/${APP_VERSION}`; // Module-level cache for the OpenAI client instance. The client itself is // a stateless fetch wrapper, so it is safe to share across calls as long as @@ -73,7 +75,11 @@ export function createOpenAIClient(projectRoot: string = process.cwd()): { apiKey: settings.apiKey, baseURL: settings.baseURL || undefined, // eslint-disable-next-line @typescript-eslint/no-explicit-any - fetch: (url: any, init: any) => undiciFetch(url, { ...init, dispatcher: keepAliveAgent }), + fetch: (url: any, init: any) => { + const headers = new Headers(init?.headers); + headers.set("User-Agent", USER_AGENT); + return undiciFetch(url, { ...init, headers, dispatcher: keepAliveAgent }); + }, }); cachedOpenAIKey = cacheKey; diff --git a/src/common/package-info.ts b/src/common/package-info.ts new file mode 100644 index 00000000..6cb3f205 --- /dev/null +++ b/src/common/package-info.ts @@ -0,0 +1,3 @@ +import pkg from "../../package.json" with { type: "json" }; +export const APP_NAME: string = pkg.name || "@vegamo/deepcode-cli"; +export const APP_VERSION: string = pkg.version || "unknown";