refactor: improve context economics display logic and logging

- Updated logging category from 'CONTEXT' to 'HOOK' for context settings loading failure.
- Simplified the display logic for the Context Economics section to show only when relevant settings are enabled.
- Enhanced readability by consolidating repeated code for displaying token savings.
- Adjusted footer logic to conditionally display token savings message based on visibility of context economics.
This commit is contained in:
Alex Newman
2025-12-01 17:43:04 -05:00
parent d1876cb6e0
commit b116681529
2 changed files with 63 additions and 53 deletions
File diff suppressed because one or more lines are too long
+28 -18
View File
@@ -98,7 +98,7 @@ function loadContextConfig(): ContextConfig {
showLastMessage: env.CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE === 'true', showLastMessage: env.CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE === 'true',
}; };
} catch (error) { } catch (error) {
logger.warn('CONTEXT', 'Failed to load context settings, using defaults', {}, error as Error); logger.warn('HOOK', 'Failed to load context settings, using defaults', {}, error as Error);
return defaults; return defaults;
} }
} }
@@ -370,15 +370,15 @@ async function contextHook(input?: SessionStartInput, useColors: boolean = false
? Math.round((savings / totalDiscoveryTokens) * 100) ? Math.round((savings / totalDiscoveryTokens) * 100)
: 0; : 0;
// Display Context Economics section // Display Context Economics section only if at least one token setting is enabled
const showContextEconomics = config.showReadTokens || config.showWorkTokens ||
config.showSavingsAmount || config.showSavingsPercent;
if (showContextEconomics) {
if (useColors) { if (useColors) {
output.push(`${colors.bright}${colors.cyan}📊 Context Economics${colors.reset}`); output.push(`${colors.bright}${colors.cyan}📊 Context Economics${colors.reset}`);
if (config.showReadTokens) {
output.push(`${colors.dim} Loading: ${totalObservations} observations (${totalReadTokens.toLocaleString()} tokens to read)${colors.reset}`); output.push(`${colors.dim} Loading: ${totalObservations} observations (${totalReadTokens.toLocaleString()} tokens to read)${colors.reset}`);
}
if (config.showWorkTokens) {
output.push(`${colors.dim} Work investment: ${totalDiscoveryTokens.toLocaleString()} tokens spent on research, building, and decisions${colors.reset}`); output.push(`${colors.dim} Work investment: ${totalDiscoveryTokens.toLocaleString()} tokens spent on research, building, and decisions${colors.reset}`);
}
if (totalDiscoveryTokens > 0 && (config.showSavingsAmount || config.showSavingsPercent)) { if (totalDiscoveryTokens > 0 && (config.showSavingsAmount || config.showSavingsPercent)) {
let savingsLine = ' Your savings: '; let savingsLine = ' Your savings: ';
if (config.showSavingsAmount && config.showSavingsPercent) { if (config.showSavingsAmount && config.showSavingsPercent) {
@@ -393,12 +393,8 @@ async function contextHook(input?: SessionStartInput, useColors: boolean = false
output.push(''); output.push('');
} else { } else {
output.push(`📊 **Context Economics**:`); output.push(`📊 **Context Economics**:`);
if (config.showReadTokens) {
output.push(`- Loading: ${totalObservations} observations (${totalReadTokens.toLocaleString()} tokens to read)`); output.push(`- Loading: ${totalObservations} observations (${totalReadTokens.toLocaleString()} tokens to read)`);
}
if (config.showWorkTokens) {
output.push(`- Work investment: ${totalDiscoveryTokens.toLocaleString()} tokens spent on research, building, and decisions`); output.push(`- Work investment: ${totalDiscoveryTokens.toLocaleString()} tokens spent on research, building, and decisions`);
}
if (totalDiscoveryTokens > 0 && (config.showSavingsAmount || config.showSavingsPercent)) { if (totalDiscoveryTokens > 0 && (config.showSavingsAmount || config.showSavingsPercent)) {
let savingsLine = '- Your savings: '; let savingsLine = '- Your savings: ';
if (config.showSavingsAmount && config.showSavingsPercent) { if (config.showSavingsAmount && config.showSavingsPercent) {
@@ -412,6 +408,7 @@ async function contextHook(input?: SessionStartInput, useColors: boolean = false
} }
output.push(''); output.push('');
} }
}
// Prepare summaries for timeline display // Prepare summaries for timeline display
// The most recent summary shows full details (investigated, learned, etc.) // The most recent summary shows full details (investigated, learned, etc.)
@@ -584,14 +581,16 @@ async function contextHook(input?: SessionStartInput, useColors: boolean = false
if (useColors) { if (useColors) {
const timePart = showTime ? `${colors.dim}${time}${colors.reset}` : ' '.repeat(time.length); const timePart = showTime ? `${colors.dim}${time}${colors.reset}` : ' '.repeat(time.length);
const readPart = readTokens > 0 ? `${colors.dim}(~${readTokens}t)${colors.reset}` : ''; const readPart = (config.showReadTokens && readTokens > 0) ? `${colors.dim}(~${readTokens}t)${colors.reset}` : '';
const discoveryPart = discoveryTokens > 0 ? `${colors.dim}(${workEmoji} ${discoveryTokens.toLocaleString()}t)${colors.reset}` : ''; const discoveryPart = (config.showWorkTokens && discoveryTokens > 0) ? `${colors.dim}(${workEmoji} ${discoveryTokens.toLocaleString()}t)${colors.reset}` : '';
output.push(` ${colors.dim}#${obs.id}${colors.reset} ${timePart} ${icon} ${colors.bright}${title}${colors.reset}`); output.push(` ${colors.dim}#${obs.id}${colors.reset} ${timePart} ${icon} ${colors.bright}${title}${colors.reset}`);
if (detailField) { if (detailField) {
output.push(` ${colors.dim}${detailField}${colors.reset}`); output.push(` ${colors.dim}${detailField}${colors.reset}`);
} }
if (readPart || discoveryPart) {
output.push(` ${readPart} ${discoveryPart}`); output.push(` ${readPart} ${discoveryPart}`);
}
output.push(''); output.push('');
} else { } else {
// Close table for full observation // Close table for full observation
@@ -606,7 +605,16 @@ async function contextHook(input?: SessionStartInput, useColors: boolean = false
output.push(detailField); output.push(detailField);
output.push(''); output.push('');
} }
output.push(`Read: ~${readTokens}, Work: ${discoveryDisplay}`); const tokenParts: string[] = [];
if (config.showReadTokens) {
tokenParts.push(`Read: ~${readTokens}`);
}
if (config.showWorkTokens) {
tokenParts.push(`Work: ${discoveryDisplay}`);
}
if (tokenParts.length > 0) {
output.push(tokenParts.join(', '));
}
output.push(''); output.push('');
// Reopen table for next items if in same file // Reopen table for next items if in same file
@@ -616,11 +624,13 @@ async function contextHook(input?: SessionStartInput, useColors: boolean = false
// Compact index rendering (existing code) // Compact index rendering (existing code)
if (useColors) { if (useColors) {
const timePart = showTime ? `${colors.dim}${time}${colors.reset}` : ' '.repeat(time.length); const timePart = showTime ? `${colors.dim}${time}${colors.reset}` : ' '.repeat(time.length);
const readPart = readTokens > 0 ? `${colors.dim}(~${readTokens}t)${colors.reset}` : ''; const readPart = (config.showReadTokens && readTokens > 0) ? `${colors.dim}(~${readTokens}t)${colors.reset}` : '';
const discoveryPart = discoveryTokens > 0 ? `${colors.dim}(${workEmoji} ${discoveryTokens.toLocaleString()}t)${colors.reset}` : ''; const discoveryPart = (config.showWorkTokens && discoveryTokens > 0) ? `${colors.dim}(${workEmoji} ${discoveryTokens.toLocaleString()}t)${colors.reset}` : '';
output.push(` ${colors.dim}#${obs.id}${colors.reset} ${timePart} ${icon} ${title} ${readPart} ${discoveryPart}`); output.push(` ${colors.dim}#${obs.id}${colors.reset} ${timePart} ${icon} ${title} ${readPart} ${discoveryPart}`);
} else { } else {
output.push(`| #${obs.id} | ${timeDisplay || ''} | ${icon} | ${title} | ~${readTokens} | ${discoveryDisplay} |`); const readCol = config.showReadTokens ? `~${readTokens}` : '';
const workCol = config.showWorkTokens ? discoveryDisplay : '';
output.push(`| #${obs.id} | ${timeDisplay || '″'} | ${icon} | ${title} | ${readCol} | ${workCol} |`);
} }
} }
} }
@@ -670,8 +680,8 @@ async function contextHook(input?: SessionStartInput, useColors: boolean = false
} }
} }
// Footer with token savings message // Footer with token savings message (only show if token economics is visible)
if (totalDiscoveryTokens > 0 && savings > 0) { if (showContextEconomics && totalDiscoveryTokens > 0 && savings > 0) {
const workTokensK = Math.round(totalDiscoveryTokens / 1000); const workTokensK = Math.round(totalDiscoveryTokens / 1000);
output.push(''); output.push('');
if (useColors) { if (useColors) {