From 61e0e63880f0b13c49001709317623f47a29590f Mon Sep 17 00:00:00 2001 From: Milan Cermak Date: Tue, 14 Jan 2025 13:51:38 +0100 Subject: [PATCH] fix: no apiKey in logs --- packages/core/src/core/llm-client.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/core/src/core/llm-client.ts b/packages/core/src/core/llm-client.ts index f93437b1..62393b1c 100644 --- a/packages/core/src/core/llm-client.ts +++ b/packages/core/src/core/llm-client.ts @@ -69,6 +69,32 @@ export class LLMClient { }); } + // obfuscate the API key when logging + toJSON() { + let maskedApiKey = this.config.apiKey.slice(0, 7) + "*".repeat(3); + + return { + ...this, + config: { + ...this.config, + apiKey: maskedApiKey, + }, + anthropic: this.anthropic && { + ...this.anthropic, + apiKey: maskedApiKey, + _options: { + // @ts-ignore + ...this.anthropic._options, + apiKey: maskedApiKey, + }, + }, + }; + } + + [Symbol.for("nodejs.util.inspect.custom")]() { + return this.toJSON(); + } + public async complete(prompt: string): Promise { let lastError: Error | null = null;