refactor(log): unify request lifecycle logging with session-colored tags

Collapse scattered per-request console lines (request/routing/auth/pending/
usage/stream-usage/stream) into 3 correlated lines: request, transform,
done. Add stable per-session color tag so concurrent request lines are
easy to follow, surface thinking intent, always-on full error logging
for debug, re-enable warn level, and uppercase keyword labels. Also fix
usage overview cards wrapping (5 cards -> grid-cols-5).

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
decolua
2026-07-10 18:01:20 +07:00
co-authored by Cursor
parent b61c50cbb7
commit a625ea9fd8
11 changed files with 176 additions and 69 deletions
+4 -17
View File
@@ -48,15 +48,9 @@ export async function handleChat(request, clientRawRequest = null) {
}
cacheClaudeHeaders(clientRawRequest.headers);
// Log request endpoint and model
const url = new URL(request.url);
const modelStr = body.model;
// Count messages (support both messages[] and input[] formats)
const msgCount = body.messages?.length || body.input?.length || 0;
const toolCount = body.tools?.length || 0;
const effort = body.reasoning_effort || body.reasoning?.effort || null;
log.request("POST", `${url.pathname} | ${modelStr} | ${msgCount} msgs${toolCount ? ` | ${toolCount} tools` : ""}${effort ? ` | effort=${effort}` : ""}`);
// Request summary is emitted as the unified "▶" line in chatCore (has fmt/thinking/account)
// Log API key (masked)
const authHeader = request.headers.get("Authorization");
@@ -191,12 +185,7 @@ async function handleSingleModelChat(body, modelStr, clientRawRequest = null, re
const { provider, model } = modelInfo;
// Log model routing (alias → actual model)
if (modelStr !== `${provider}/${model}`) {
log.info("ROUTING", `${modelStr}${provider}/${model}`);
} else {
log.info("ROUTING", `Provider: ${provider}, Model: ${model}`);
}
// Routing shown in the unified "▶" line (client model → provider/model)
// Extract userAgent from request
const userAgent = request?.headers?.get("user-agent") || "";
@@ -225,9 +214,7 @@ async function handleSingleModelChat(body, modelStr, clientRawRequest = null, re
return errorResponse(lastStatus || HTTP_STATUS.SERVICE_UNAVAILABLE, lastError || "All accounts unavailable");
}
// Log account selection
log.info("AUTH", `\x1b[32mUsing ${provider} account: ${credentials.connectionName}\x1b[0m`);
// Account selection shown in the unified "▶" line (acc:...)
const refreshedCredentials = await checkAndRefreshToken(provider, credentials);
// Ensure real project ID is available for providers that need it (P0 fix: cold miss)
@@ -288,7 +275,7 @@ async function handleSingleModelChat(body, modelStr, clientRawRequest = null, re
const { shouldFallback } = await markAccountUnavailable(credentials.connectionId, result.status, result.error, provider, model, result.resetsAtMs);
if (shouldFallback) {
log.warn("AUTH", `Account ${credentials.connectionName} unavailable (${result.status}), trying fallback`);
log.warn("FALLBACK", `⇄ ACC:${credentials.connectionName} UNAVAILABLE (${result.status}) → NEXT ACCOUNT`);
excludeConnectionIds.add(credentials.connectionId);
lastError = result.error;
lastStatus = result.status;
+44 -1
View File
@@ -13,6 +13,49 @@ function formatTime() {
return new Date().toLocaleTimeString("en-US", { hour12: false });
}
// Colored-dot tags to correlate request lines by session (same session → same color)
const REQ_TAGS = ["🟢", "🔵", "🟣", "🟡", "🟠", "🔴", "⚪", "🟤"];
let tagCursor = 0;
// Allocate next rotating tag (fallback when no session seed available)
export function nextTag() {
const tag = REQ_TAGS[tagCursor % REQ_TAGS.length];
tagCursor++;
return tag;
}
// Stable tag derived from a session/connection seed: same seed always maps to the same color
export function tagForSession(seed) {
if (!seed) return nextTag();
let h = 0;
for (let i = 0; i < seed.length; i++) h = (h * 31 + seed.charCodeAt(i)) | 0;
return REQ_TAGS[Math.abs(h) % REQ_TAGS.length];
}
// Print one correlated line: [time] tag symbol message
export function line(tag, symbol, message) {
if (LEVEL > LOG_LEVELS.INFO) return;
console.log(`[${formatTime()}] ${tag} ${symbol} ${message}`);
}
// Like line() but always printed regardless of LOG_LEVEL (errors must never be hidden)
export function errorLine(tag, symbol, message) {
console.log(`[${formatTime()}] ${tag} ${symbol} ${message}`);
}
// Format thinking intent for the request line ("high(10k)" / "off" / "auto")
export function fmtThink(intent) {
if (!intent || !intent.mode) return null;
if (intent.mode === "none") return "off";
if (intent.mode === "auto") return "auto";
if (intent.mode === "budget") {
const k = intent.budget >= 1000 ? `${Math.round(intent.budget / 1000)}k` : `${intent.budget}`;
return k;
}
if (intent.mode === "level") return intent.level;
return null;
}
function formatData(data) {
if (!data) return "";
if (typeof data === "string") return data;
@@ -40,7 +83,7 @@ export function info(tag, message, data) {
export function warn(tag, message, data) {
if (LEVEL <= LOG_LEVELS.WARN) {
const dataStr = data ? ` ${formatData(data)}` : "";
// console.warn(`[${formatTime()}] ⚠️ [${tag}] ${message}${dataStr}`);
console.warn(`[${formatTime()}] ⚠️ [${tag}] ${message}${dataStr}`);
}
}