Files
Agentic-Compaction-and-Pipl…/examples/01-basic-compaction.ts
admin e6981ee8f8 Add project files, examples, and documentation
- Added package.json for npm/bun compatibility
- Added tsconfig.json for TypeScript configuration
- Added MIT LICENSE
- Added CHANGELOG.md for version tracking
- Added 4 example files demonstrating usage:
  - 01-basic-compaction.ts - Context compaction basics
  - 02-claude-code-integration.ts - Claude Code integration
  - 03-openclaw-integration.ts - OpenClaw integration
  - 04-state-machine-pipeline.ts - Deterministic pipeline

All extracted source files are included in the repository.
2026-03-03 13:18:04 +00:00

70 lines
2.4 KiB
TypeScript

/**
* Example: Basic Context Compaction
*
* Demonstrates how to use context compaction to manage
* conversation history within token limits.
*/
import {
ContextManager,
TokenCounter,
Summarizer
} from '../agent-system';
async function basicCompaction() {
// Initialize components with a 128k token budget
const tokenCounter = new TokenCounter(128000);
const summarizer = new Summarizer();
const contextManager = new ContextManager(tokenCounter, summarizer, {
maxTokens: 100000, // Max tokens before compaction triggers
compactionStrategy: 'hybrid', // Use all strategies combined
slidingWindowSize: 50, // Keep last 50 messages
preserveRecentCount: 10, // Keep last 10 messages verbatim
priorityKeywords: [ // Always retain messages with these keywords
'error', 'important', 'decision', 'critical', 'remember'
],
reserveTokens: 20000 // Reserve for response generation
});
// Simulate a long conversation
const messages = [
{ role: 'user' as const, content: 'I need to build a REST API for my application.' },
{ role: 'assistant' as const, content: 'I can help you design and implement a REST API. What features do you need?' },
{ role: 'user' as const, content: 'I need CRUD operations for users, products, and orders.' },
{ role: 'assistant' as const, content: 'Great! Let\'s design the API endpoints...' },
// ... many more messages ...
];
// Add messages to context
for (const msg of messages) {
contextManager.addMessage(msg);
}
// Check if compaction is needed
if (contextManager.needsCompaction()) {
console.log('Context is full, compacting...');
const result = await contextManager.compact();
console.log('Compaction result:');
console.log(` - Tokens saved: ${result.tokensSaved}`);
console.log(` - Messages removed: ${result.messagesRemoved}`);
if (result.summary) {
console.log(` - Summary: ${result.summary.substring(0, 100)}...`);
}
}
// Get active context for API call
const activeContext = contextManager.getActiveContext();
console.log(`Active context: ${activeContext.messages.length} messages`);
console.log(`Token usage: ${tokenCounter.getUsagePercentage() * 100}%`);
// Get token stats
const stats = tokenCounter.getStats();
console.log(`Token stats: ${stats.used}/${stats.total} (${stats.percentage}%)`);
}
// Run the example
basicCompaction().catch(console.error);