Skip to content
91 changes: 50 additions & 41 deletions src/commands/compact-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,47 +30,56 @@ export const compactCommand: Command = {

// /compact debug - Show detailed context debugging info
if (subcommand === 'debug') {
const info = context.agent.getContextInfo();
const messages = context.agent.getMessages();
try {
const info = context.agent.getContextInfo();
const messages = context.agent.getMessages();

// Build detailed debug output
const debugInfo = {
context: {
tokens: info.tokens,
messageTokens: info.messageTokens,
systemPromptTokens: info.systemPromptTokens,
toolDefinitionTokens: info.toolDefinitionTokens,
messages: info.messages,
userMessages: info.userMessages,
assistantMessages: info.assistantMessages,
toolResultMessages: info.toolResultMessages,
},
limits: {
maxTokens: info.maxTokens,
contextWindow: info.contextWindow,
effectiveLimit: info.effectiveLimit,
outputReserve: info.outputReserve,
safetyBuffer: info.safetyBuffer,
tierName: info.tierName,
},
state: {
hasSummary: info.hasSummary,
compressionEnabled: info.compressionEnabled,
compressionStats: info.compression,
workingSetFiles: info.workingSetFiles,
},
messages: messages.map((msg, index) => ({
index,
role: msg.role,
contentPreview: typeof msg.content === 'string'
? msg.content.slice(0, 200) + (msg.content.length > 200 ? '...' : '')
: `[complex content: ${msg.content.length} blocks]`,
contentLength: typeof msg.content === 'string' ? msg.content.length : msg.content.reduce((acc, block) => acc + (typeof block.text === 'string' ? block.text.length : 0), 0),
})),
conversationSummary: info.hasSummary ? '[summary exists - use /compact status for token info]' : null,
};
// Validate we have a messages array
if (!messages || !Array.isArray(messages)) {
return 'CONTEXT_DEBUG_ERROR:Invalid messages data';
}

return `CONTEXT_DEBUG:${JSON.stringify(debugInfo, null, 2)}`;
// Build detailed debug output
const debugInfo = {
context: {
tokens: info.tokens,
messageTokens: info.messageTokens,
systemPromptTokens: info.systemPromptTokens,
toolDefinitionTokens: info.toolDefinitionTokens,
messages: info.messages,
userMessages: info.userMessages,
assistantMessages: info.assistantMessages,
toolResultMessages: info.toolResultMessages,
},
limits: {
maxTokens: info.maxTokens,
contextWindow: info.contextWindow,
effectiveLimit: info.effectiveLimit,
outputReserve: info.outputReserve,
safetyBuffer: info.safetyBuffer,
tierName: info.tierName,
},
state: {
hasSummary: info.hasSummary,
compressionEnabled: info.compressionEnabled,
compressionStats: info.compression,
workingSetFiles: info.workingSetFiles,
},
messagePreviews: messages.map((msg, index) => ({
index,
role: msg.role,
contentPreview: typeof msg.content === 'string'
? msg.content.slice(0, 200) + (msg.content.length > 200 ? '...' : '')
: `[complex content: ${msg.content.length} blocks]`,
contentLength: typeof msg.content === 'string' ? msg.content.length : (msg.content?.length || 0),
})),
conversationSummary: info.hasSummary ? '[summary exists - use /compact status for token info]' : null,
};

return `CONTEXT_DEBUG:${JSON.stringify(debugInfo, null, 2)}`;
} catch (error) {
return `CONTEXT_DEBUG_ERROR:${error instanceof Error ? error.message : 'Unknown error occurred'}`;
}
}

// /compact memory - Show memory usage
Expand Down Expand Up @@ -191,10 +200,10 @@ export const compactCommand: Command = {
return `COMPRESS_STATS:${JSON.stringify(output)}`;
}

return `COMPACT_ERROR:Unknown subcommand "${subcommand}". Use: status, summarize, compress, debug`;
return `COMPACT_ERROR:Unknown subcommand "${subcommand}". Use: status, summarize, compress, memory, debug`;
},
};

export function registerCompactCommands(): void {
registerCommand(compactCommand);
}
}