Skip to content

Commit 4cb25b3

Browse files
committed
refactor: extract telemetry into separate module with enable/disable toggle
- Create src/common/telemetry.ts — standalone reportNewPrompt() with configurable enabled flag - Add telemetryEnabled to settings types and resolution chain (DeepcodingSettings, ResolvedDeepcodingSettings, CreateOpenAIClient) - Resolution priority: system env > project env > project settings > user env > user settings > default true (opt-out) - Configurable via settings.json ("telemetryEnabled": false) or env (DEEPCODE_TELEMETRY_ENABLED=0) - Reduce session.ts by ~20 lines, delegating to shared module
1 parent d3c991c commit 4cb25b3

5 files changed

Lines changed: 54 additions & 21 deletions

File tree

src/common/openai-client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export function createOpenAIClient(projectRoot: string = process.cwd()): {
2626
thinkingEnabled: boolean;
2727
reasoningEffort: "high" | "max";
2828
debugLogEnabled: boolean;
29+
telemetryEnabled: boolean;
2930
notify?: string;
3031
webSearchTool?: string;
3132
env: Record<string, string>;
@@ -40,6 +41,7 @@ export function createOpenAIClient(projectRoot: string = process.cwd()): {
4041
thinkingEnabled: settings.thinkingEnabled,
4142
reasoningEffort: settings.reasoningEffort,
4243
debugLogEnabled: settings.debugLogEnabled,
44+
telemetryEnabled: settings.telemetryEnabled,
4345
notify: settings.notify,
4446
webSearchTool: settings.webSearchTool,
4547
env: settings.env,
@@ -56,6 +58,7 @@ export function createOpenAIClient(projectRoot: string = process.cwd()): {
5658
thinkingEnabled: settings.thinkingEnabled,
5759
reasoningEffort: settings.reasoningEffort,
5860
debugLogEnabled: settings.debugLogEnabled,
61+
telemetryEnabled: settings.telemetryEnabled,
5962
notify: settings.notify,
6063
webSearchTool: settings.webSearchTool,
6164
env: settings.env,
@@ -91,6 +94,7 @@ export function createOpenAIClient(projectRoot: string = process.cwd()): {
9194
thinkingEnabled: settings.thinkingEnabled,
9295
reasoningEffort: settings.reasoningEffort,
9396
debugLogEnabled: settings.debugLogEnabled,
97+
telemetryEnabled: settings.telemetryEnabled,
9498
notify: settings.notify,
9599
webSearchTool: settings.webSearchTool,
96100
env: settings.env,

src/common/telemetry.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const DEFAULT_NEW_PROMPT_API_URL = "https://deepcode.vegamo.cn/api/plugin/new";
2+
const DEFAULT_REPORT_TIMEOUT_MS = 3000;
3+
4+
export type NewPromptReportOptions = {
5+
enabled: boolean;
6+
machineId?: string;
7+
timeoutMs?: number;
8+
};
9+
10+
/**
11+
* Fire-and-forget report of a new prompt session.
12+
* Respects the `enabled` toggle: when disabled, the call is a no-op.
13+
*/
14+
export function reportNewPrompt(options: NewPromptReportOptions): void {
15+
if (!options.enabled || !options.machineId) {
16+
return;
17+
}
18+
19+
const timeoutMs = options.timeoutMs ?? DEFAULT_REPORT_TIMEOUT_MS;
20+
const controller = new AbortController();
21+
const timeout = setTimeout(() => controller.abort(), timeoutMs);
22+
23+
void fetch(DEFAULT_NEW_PROMPT_API_URL, {
24+
method: "POST",
25+
headers: {
26+
"Content-Type": "application/json",
27+
Token: options.machineId,
28+
},
29+
body: JSON.stringify({}),
30+
signal: controller.signal,
31+
})
32+
.catch(() => {})
33+
.finally(() => clearTimeout(timeout));
34+
}

src/session.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
type UserToolPermission,
4646
} from "./common/permissions";
4747
import { clearSessionWorkingDir } from "./tools/bash-handler";
48+
import { reportNewPrompt } from "./common/telemetry";
4849

4950
export type { PermissionScope } from "./settings";
5051
export type {
@@ -59,8 +60,6 @@ export type {
5960
const MAX_SESSION_ENTRIES = 50;
6061
const MAX_PROJECT_CODE_LENGTH = 64;
6162
const PROJECT_CODE_HASH_LENGTH = 16;
62-
const DEFAULT_NEW_PROMPT_API_URL = "https://deepcode.vegamo.cn/api/plugin/new";
63-
const NEW_PROMPT_REPORT_TIMEOUT_MS = 3000;
6463
const DEFAULT_COMPACT_PROMPT_TOKEN_THRESHOLD = 128 * 1024;
6564
const DEEPSEEK_V4_COMPACT_PROMPT_TOKEN_THRESHOLD = 512 * 1024;
6665

@@ -1504,25 +1503,8 @@ ${skillMd}
15041503
}
15051504

15061505
private reportNewPrompt(): void {
1507-
const { machineId } = this.createOpenAIClient();
1508-
if (!machineId) {
1509-
return;
1510-
}
1511-
1512-
const controller = new AbortController();
1513-
const timeout = setTimeout(() => controller.abort(), NEW_PROMPT_REPORT_TIMEOUT_MS);
1514-
1515-
void fetch(DEFAULT_NEW_PROMPT_API_URL, {
1516-
method: "POST",
1517-
headers: {
1518-
"Content-Type": "application/json",
1519-
Token: machineId,
1520-
},
1521-
body: JSON.stringify({}),
1522-
signal: controller.signal,
1523-
})
1524-
.catch(() => {})
1525-
.finally(() => clearTimeout(timeout));
1506+
const { machineId, telemetryEnabled } = this.createOpenAIClient();
1507+
reportNewPrompt({ enabled: telemetryEnabled ?? true, machineId });
15261508
}
15271509

15281510
interruptActiveSession(): void {

src/settings.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type DeepcodingEnv = Record<string, string | undefined> & {
1010
THINKING_ENABLED?: string;
1111
REASONING_EFFORT?: string;
1212
DEBUG_LOG_ENABLED?: string;
13+
TELEMETRY_ENABLED?: string;
1314
};
1415

1516
export type ReasoningEffort = "high" | "max";
@@ -47,6 +48,7 @@ export type DeepcodingSettings = {
4748
thinkingEnabled?: boolean;
4849
reasoningEffort?: ReasoningEffort;
4950
debugLogEnabled?: boolean;
51+
telemetryEnabled?: boolean;
5052
notify?: string;
5153
webSearchTool?: string;
5254
mcpServers?: Record<string, McpServerConfig>;
@@ -61,6 +63,7 @@ export type ResolvedDeepcodingSettings = {
6163
thinkingEnabled: boolean;
6264
reasoningEffort: ReasoningEffort;
6365
debugLogEnabled: boolean;
66+
telemetryEnabled: boolean;
6467
notify?: string;
6568
webSearchTool?: string;
6669
mcpServers?: Record<string, McpServerConfig>;
@@ -313,6 +316,14 @@ export function resolveSettingsSources(
313316
parseBoolean(userEnv.DEBUG_LOG_ENABLED) ??
314317
false;
315318

319+
const telemetryEnabled =
320+
parseBoolean(systemEnv.TELEMETRY_ENABLED) ??
321+
parseBoolean(projectSettings?.telemetryEnabled) ??
322+
parseBoolean(projectEnv.TELEMETRY_ENABLED) ??
323+
parseBoolean(userSettings?.telemetryEnabled) ??
324+
parseBoolean(userEnv.TELEMETRY_ENABLED) ??
325+
true;
326+
316327
const notify =
317328
trimString(systemEnv.NOTIFY) || trimString(projectSettings?.notify) || trimString(userSettings?.notify) || "";
318329
const webSearchTool =
@@ -329,6 +340,7 @@ export function resolveSettingsSources(
329340
thinkingEnabled,
330341
reasoningEffort,
331342
debugLogEnabled,
343+
telemetryEnabled,
332344
notify: notify || undefined,
333345
webSearchTool: webSearchTool || undefined,
334346
mcpServers: mergeMcpServers(userSettings, projectSettings, userEnv, projectEnv, systemEnv),

src/tools/executor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type CreateOpenAIClient = () => {
1616
thinkingEnabled: boolean;
1717
reasoningEffort?: ReasoningEffort;
1818
debugLogEnabled?: boolean;
19+
telemetryEnabled?: boolean;
1920
notify?: string;
2021
webSearchTool?: string;
2122
env?: Record<string, string>;

0 commit comments

Comments
 (0)