Skip to content

Commit 8b98837

Browse files
committed
add specific user agent for deepcode
1 parent c8d8fa8 commit 8b98837

3 files changed

Lines changed: 13 additions & 15 deletions

File tree

packages/cli/src/cli.tsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import React from "react";
22
import { render } from "ink";
33
import { setShellIfWindows } from "@vegamo/deepcode-core";
44
import { checkForNpmUpdate, promptForPendingUpdate, type PackageInfo } from "./common/update-check";
5+
import { APP_NAME, APP_VERSION } from "./common/package-info";
56
import { AppContainer } from "./ui";
67

78
const args = process.argv.slice(2);
8-
const packageInfo = readPackageInfo();
9+
const packageInfo: PackageInfo = { name: APP_NAME, version: APP_VERSION };
910

1011
if (args.includes("--version") || args.includes("-v")) {
11-
process.stdout.write(`${packageInfo.version || "unknown"}\n`);
12+
process.stdout.write(`${APP_VERSION}\n`);
1213
process.exit(0);
1314
}
1415

@@ -129,15 +130,3 @@ function configureWindowsShell(): void {
129130
process.exit(1);
130131
}
131132
}
132-
133-
function readPackageInfo(): PackageInfo {
134-
try {
135-
const pkg = require("../package.json") as { name?: unknown; version?: unknown };
136-
return {
137-
name: typeof pkg.name === "string" ? pkg.name : "@vegamo/deepcode-cli",
138-
version: typeof pkg.version === "string" ? pkg.version : "",
139-
};
140-
} catch {
141-
return { name: "@vegamo/deepcode-cli", version: "" };
142-
}
143-
}

packages/core/src/common/openai-client.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import * as path from "path";
44
import OpenAI from "openai";
55
import { Agent, fetch as undiciFetch } from "undici";
66
import { resolveCurrentSettings } from "../settings";
7+
import { APP_VERSION } from "./package-info";
78

89
// Custom undici Agent with a 180-second keepAlive timeout. The default
910
// global fetch (undici) only keeps connections alive for 4 seconds, which
1011
// is too short for a CLI where the user may spend 10–30 seconds reading
1112
// output between prompts. By passing a dedicated Agent to undiciFetch we
1213
// keep connections reusable for three minutes after the last request.
1314
const keepAliveAgent = new Agent({ keepAliveTimeout: 180_000 });
15+
const USER_AGENT = `deepcode-cli/${APP_VERSION}`;
1416

1517
// Module-level cache for the OpenAI client instance. The client itself is
1618
// 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()): {
7375
apiKey: settings.apiKey,
7476
baseURL: settings.baseURL || undefined,
7577
// eslint-disable-next-line @typescript-eslint/no-explicit-any
76-
fetch: (url: any, init: any) => undiciFetch(url, { ...init, dispatcher: keepAliveAgent }),
78+
fetch: (url: any, init: any) => {
79+
const headers = new Headers(init?.headers);
80+
headers.set("User-Agent", USER_AGENT);
81+
return undiciFetch(url, { ...init, headers, dispatcher: keepAliveAgent });
82+
},
7783
});
7884
cachedOpenAIKey = cacheKey;
7985

src/common/package-info.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import pkg from "../../package.json" with { type: "json" };
2+
export const APP_NAME: string = pkg.name || "@vegamo/deepcode-cli";
3+
export const APP_VERSION: string = pkg.version || "unknown";

0 commit comments

Comments
 (0)