- 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.
101 lines
3.2 KiB
TypeScript
101 lines
3.2 KiB
TypeScript
/**
|
||
* Example: Claude Code Integration
|
||
*
|
||
* Demonstrates how to use the Claude Code integration
|
||
* for context-aware AI applications.
|
||
*/
|
||
|
||
import { ClaudeCodeIntegration } from '../agent-system/integrations/claude-code';
|
||
|
||
async function claudeCodeExample() {
|
||
// Initialize Claude Code integration
|
||
const claude = new ClaudeCodeIntegration({
|
||
maxContextTokens: 200000, // Claude's 200k context window
|
||
reserveTokens: 40000, // Reserve for response
|
||
compactionStrategy: 'hybrid',
|
||
autoCompact: true, // Auto-compact when threshold reached
|
||
compactionThreshold: 0.75, // Compact at 75% capacity
|
||
enableSubagents: true, // Enable subagent spawning
|
||
maxSubagents: 6,
|
||
persistentMemory: true,
|
||
memoryStorePath: '.claude-code/memory'
|
||
});
|
||
|
||
console.log('Session ID:', claude.getSessionId());
|
||
|
||
// Add user message
|
||
claude.addMessage({
|
||
role: 'user',
|
||
content: 'Analyze this codebase and identify potential security issues.',
|
||
metadata: {
|
||
priority: 1,
|
||
fileReferences: ['src/auth.ts', 'src/middleware.ts']
|
||
}
|
||
});
|
||
|
||
// Check token stats
|
||
const stats = claude.getTokenStats();
|
||
console.log(`Token usage: ${stats.percentage}% (${stats.used}/${stats.total})`);
|
||
|
||
// Get context for Claude API
|
||
const { messages, systemPrompt } = claude.getContextForAPI();
|
||
console.log(`Prepared ${messages.length} messages for API`);
|
||
if (systemPrompt) {
|
||
console.log('System prompt includes context summary');
|
||
}
|
||
|
||
// Spawn a researcher subagent
|
||
console.log('\n--- Spawning Researcher Subagent ---');
|
||
const researcherResult = await claude.spawnSubagent({
|
||
type: 'researcher',
|
||
prompt: 'Research best practices for JWT authentication in TypeScript',
|
||
priority: 'high'
|
||
});
|
||
|
||
console.log('Researcher result:', {
|
||
success: researcherResult.success,
|
||
tokens: researcherResult.tokens,
|
||
duration: `${researcherResult.duration}ms`
|
||
});
|
||
|
||
// Spawn multiple subagents in parallel (OpenClaw pattern: 4×3)
|
||
console.log('\n--- Parallel Subagent Execution ---');
|
||
const parallelResults = await claude.executeParallelSubagents([
|
||
{ type: 'explorer', prompt: 'Find all authentication endpoints' },
|
||
{ type: 'explorer', prompt: 'Find all database queries' },
|
||
{ type: 'reviewer', prompt: 'Review input validation logic' }
|
||
]);
|
||
|
||
console.log(`Completed ${parallelResults.length} subagent tasks`);
|
||
|
||
// Use persistent memory
|
||
await claude.remember('projectContext', {
|
||
name: 'MyApp',
|
||
type: 'REST API',
|
||
securityScan: 'in-progress'
|
||
});
|
||
|
||
const savedContext = await claude.recall('projectContext');
|
||
console.log('Recalled context:', savedContext);
|
||
|
||
// Save context for later restoration
|
||
await claude.saveContext('security-audit-milestone');
|
||
console.log('Context saved for future sessions');
|
||
|
||
// Get session info
|
||
const session = claude.getSessionInfo();
|
||
console.log('\nSession Info:', {
|
||
id: session.id,
|
||
messageCount: session.messageCount,
|
||
tokenUsage: session.tokenUsage,
|
||
status: session.status
|
||
});
|
||
|
||
// Get registered tools
|
||
const tools = claude.getTools();
|
||
console.log(`\nRegistered tools: ${tools.map(t => t.name).join(', ')}`);
|
||
}
|
||
|
||
// Run the example
|
||
claudeCodeExample().catch(console.error);
|