From 2e0fd5506effbd0368112ab549a90fe744e4583e Mon Sep 17 00:00:00 2001 From: Robert Snedegar Date: Sat, 13 Jun 2026 11:11:52 -0700 Subject: [PATCH] feat: include agent name in User-Agent after login Append the logged-in player's name to the User-Agent header so the server can attribute requests to a specific agent, e.g. "SpaceMolt-Client/0.8.0 (SantaClaus)". The name is read from the persisted session (set on login/register), so it carries across CLI invocations. Pre-login requests (session bootstrap) fall back to the plain "SpaceMolt-Client/" string. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/client.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/client.ts b/src/client.ts index ff359a1..806e978 100644 --- a/src/client.ts +++ b/src/client.ts @@ -837,7 +837,7 @@ async function createSession(): Promise { if (DEBUG) console.log(`${c.dim}[DEBUG] Creating new session...${c.reset}`); const response = await fetch(`${API_BASE}/session`, { method: 'POST', - headers: { 'Content-Type': 'application/json', 'User-Agent': `SpaceMolt-Client/${VERSION}` }, + headers: { 'Content-Type': 'application/json', 'User-Agent': userAgent() }, }); const data = (await response.json()) as APIResponse; if (data.error) throw new Error(`Failed to create session: ${data.error.message}`); @@ -860,6 +860,14 @@ async function getSession(): Promise { return !session || isSessionExpired(session) ? createSession() : session; } +// Build the User-Agent header. Once a player has logged in, the agent name is +// appended (e.g. "SpaceMolt-Client/0.8.0 (SantaClaus)") so the server can +// attribute requests to a specific player. +function userAgent(session?: Session): string { + const base = `SpaceMolt-Client/${VERSION}`; + return session?.username ? `${base} (${session.username})` : base; +} + // ============================================================================= // HTTP API // ============================================================================= @@ -886,7 +894,7 @@ async function execute(command: string, payload?: Record): Prom headers: { 'Content-Type': 'application/json', 'X-Session-Id': session.id, - 'User-Agent': `SpaceMolt-Client/${VERSION}`, + 'User-Agent': userAgent(session), }, body: payload ? JSON.stringify(payload) : undefined, signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),