-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathdebug-logger.ts
More file actions
82 lines (74 loc) · 1.98 KB
/
debug-logger.ts
File metadata and controls
82 lines (74 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
const DEBUG_LOG_FILE = "debug.log";
export type OpenAIChatCompletionDebugEntry = {
timestamp: string;
location: string;
requestId?: string;
sessionId?: string;
model?: string;
baseURL?: string;
durationMs?: number;
params?: Record<string, unknown>;
request: Record<string, unknown>;
response?: unknown;
responseChunks?: unknown[];
error?: {
name: string;
message: string;
stack?: string;
};
};
export function logOpenAIChatCompletionDebug(entry: OpenAIChatCompletionDebugEntry): void {
try {
const logPath = getDebugLogPath();
fs.mkdirSync(path.dirname(logPath), { recursive: true });
fs.appendFileSync(logPath, `${JSON.stringify(toSerializable(entry))}\n`, "utf8");
} catch {
// Debug logging must never affect CLI behavior.
}
}
export function getDebugLogPath(): string {
return path.join(os.homedir(), ".deepcode", "logs", DEBUG_LOG_FILE);
}
export function normalizeDebugError(error: unknown): { name: string; message: string; stack?: string } {
if (error instanceof Error) {
return {
name: error.name,
message: error.message,
stack: error.stack,
};
}
return {
name: "UnknownError",
message: String(error),
};
}
function toSerializable(value: unknown): unknown {
const seen = new WeakSet<object>();
function walk(current: unknown): unknown {
if (typeof current === "bigint") {
return current.toString();
}
if (current instanceof Error) {
return normalizeDebugError(current);
}
if (!current || typeof current !== "object") {
return current;
}
if (seen.has(current)) {
return "[Circular]";
}
seen.add(current);
if (Array.isArray(current)) {
return current.map(walk);
}
const result: Record<string, unknown> = {};
for (const [key, val] of Object.entries(current)) {
result[key] = walk(val);
}
return result;
}
return walk(value);
}