From 1735d97a9e00e523202f073294f59cf60d46b9e7 Mon Sep 17 00:00:00 2001 From: redshift <213178690+1ftredsh@users.noreply.github.com> Date: Sat, 20 Jun 2026 10:10:21 +0800 Subject: [PATCH] feat: configure raw request response logging --- src/daemon/http/index.ts | 5 +++++ src/daemon/index.ts | 17 ++++++++++++++++- src/utils/config.ts | 8 ++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/daemon/http/index.ts b/src/daemon/http/index.ts index 28f3974..2cfc503 100644 --- a/src/daemon/http/index.ts +++ b/src/daemon/http/index.ts @@ -48,6 +48,7 @@ type DaemonDeps = { routstrPubkey?: string; providerManager: ProviderManager; refundClient: any; + requestResponseLogDir?: string; }; /** @@ -303,6 +304,7 @@ export function createDaemonRequestHandler(deps: { usageTrackingDriver: UsageTrackingDriver; providerManager: ProviderManager; refundClient: any; + requestResponseLogDir?: string; }) { return async function handler(req: IncomingMessage, res: ServerResponse) { const host = req.headers.host || "localhost"; @@ -1274,6 +1276,9 @@ export function createDaemonRequestHandler(deps: { sdkStore: deps.store, providerManager: deps.providerManager, logger: reqLogger, + ...(deps.requestResponseLogDir + ? { requestResponseLogDir: deps.requestResponseLogDir } + : {}), ...(deps.routstrPubkey ? { routstrPubkey: deps.routstrPubkey } : {}), }); diff --git a/src/daemon/index.ts b/src/daemon/index.ts index bf26043..f7538a7 100644 --- a/src/daemon/index.ts +++ b/src/daemon/index.ts @@ -11,7 +11,13 @@ import { // (SDK 0.3.7+ browser-safe entrypoint split). import { ModelManager } from "@routstr/sdk/bun"; import type { SdkLogger } from "@routstr/sdk"; -import { CONFIG_DIR, DB_PATH, SOCKET_PATH, PID_FILE } from "../utils/config"; +import { + CONFIG_DIR, + DB_PATH, + SOCKET_PATH, + PID_FILE, + REQUEST_RESPONSE_LOGS_DIR, +} from "../utils/config"; import { logger } from "../utils/logger"; @@ -49,6 +55,11 @@ async function main(): Promise { const port = args.port; const provider = args.provider || config.provider; + const requestResponseLogDir = + process.env.ROUTSTRD_REQUEST_RESPONSE_LOG_DIR || + (config.requestResponseLogging?.enabled + ? config.requestResponseLogging.dir || REQUEST_RESPONSE_LOGS_DIR + : undefined); await ensureDirs(); @@ -133,6 +144,7 @@ async function main(): Promise { usageTrackingDriver, providerManager, refundClient, + requestResponseLogDir, }), ); @@ -242,6 +254,9 @@ async function main(): Promise { server.listen(port, async () => { logger.log(`Routstr daemon listening on http://localhost:${port}/v1`); + if (requestResponseLogDir) { + logger.log(`Raw request/response logs: ${requestResponseLogDir}`); + } // Start the recurring model refresh job after initial bootstrap void ensureProvidersBootstrapped() diff --git a/src/utils/config.ts b/src/utils/config.ts index 0b19c75..202b2ca 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -6,6 +6,7 @@ export const PID_FILE = process.env.ROUTSTRD_PID || `${CONFIG_DIR}/routstrd.pid` export const DB_PATH = `${CONFIG_DIR}/routstr.db`; export const CONFIG_FILE = `${CONFIG_DIR}/config.json`; export const LOGS_DIR = `${CONFIG_DIR}/logs`; +export const REQUEST_RESPONSE_LOGS_DIR = `${CONFIG_DIR}/request-response-logs`; /** NWC auto-refill configuration */ export interface NwcAutoRefillConfig { @@ -34,6 +35,13 @@ export interface RoutstrdConfig { provider: string | null; cocodPath: string | null; mode?: "xcashu" | "apikeys"; + /** Raw upstream request/response logging. Disabled by default because logs can contain sensitive prompts, outputs, and auth/payment headers. */ + requestResponseLogging?: { + /** Enable raw request/response file logging. */ + enabled?: boolean; + /** Root log directory. SDK writes requests/*.json and responses/*.jsonl below this directory. Defaults to ~/.routstrd/request-response-logs. */ + dir?: string; + }; daemonUrl?: string; /** URL of the auth proxy (routstrd-auth) for management endpoints (npubs, clients, usage). * Defaults to daemonUrl or localhost:{port} if not set. */