- 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.
165 lines
4.8 KiB
TypeScript
165 lines
4.8 KiB
TypeScript
/**
|
||
* Example: OpenClaw Integration
|
||
*
|
||
* Demonstrates how to use OpenClaw integration for
|
||
* deterministic multi-agent pipeline orchestration.
|
||
*/
|
||
|
||
import { OpenClawIntegration } from '../agent-system/integrations/openclaw';
|
||
|
||
async function openclawExample() {
|
||
// Initialize OpenClaw integration
|
||
const openclaw = new OpenClawIntegration({
|
||
maxContextTokens: 200000,
|
||
compactionStrategy: 'hybrid',
|
||
workspaceIsolation: true,
|
||
enableLobsterWorkflows: true,
|
||
enableParallelExecution: true,
|
||
maxParallelAgents: 12, // 4 projects × 3 roles
|
||
hooks: {
|
||
onCompactionStart: (ctx) => {
|
||
console.log(`[Hook] Compaction started for context ${ctx.id}`);
|
||
},
|
||
onCompactionEnd: (result) => {
|
||
console.log(`[Hook] Compaction complete: saved ${result.tokensSaved} tokens`);
|
||
},
|
||
onStateTransition: (from, to, ctx) => {
|
||
console.log(`[Hook] State transition: ${from} → ${to}`);
|
||
}
|
||
}
|
||
});
|
||
|
||
console.log('Context ID:', openclaw.getContext().id);
|
||
|
||
// Add message with OpenClaw context
|
||
openclaw.addMessage({
|
||
role: 'user',
|
||
content: 'Implement user authentication with JWT tokens',
|
||
tags: ['feature', 'auth', 'security'],
|
||
references: {
|
||
files: ['src/auth.ts', 'src/middleware.ts'],
|
||
functions: ['login', 'verifyToken', 'refreshToken']
|
||
}
|
||
});
|
||
|
||
// Create a deterministic pipeline
|
||
console.log('\n--- Creating Pipeline ---');
|
||
const pipeline = openclaw.createPipeline({
|
||
name: 'feature-development',
|
||
description: 'Complete feature development workflow with deterministic state machine',
|
||
states: [
|
||
{
|
||
name: 'analyze',
|
||
type: 'parallel',
|
||
agents: ['explorer', 'researcher'],
|
||
transitions: [
|
||
{ target: 'design', event: 'analysis_complete' }
|
||
]
|
||
},
|
||
{
|
||
name: 'design',
|
||
type: 'sequential',
|
||
agents: ['planner'],
|
||
onEnter: 'agent:planner',
|
||
transitions: [
|
||
{ target: 'implement', event: 'design_approved' }
|
||
]
|
||
},
|
||
{
|
||
name: 'implement',
|
||
type: 'parallel',
|
||
agents: ['coder'],
|
||
transitions: [
|
||
{ target: 'review', event: 'implementation_complete' }
|
||
]
|
||
},
|
||
{
|
||
name: 'review',
|
||
type: 'sequential',
|
||
agents: ['reviewer'],
|
||
timeout: 3600000, // 1 hour
|
||
transitions: [
|
||
{ target: 'complete', event: 'approved' },
|
||
{ target: 'implement', event: 'rejected' }
|
||
]
|
||
},
|
||
{
|
||
name: 'complete',
|
||
type: 'sequential',
|
||
transitions: []
|
||
}
|
||
]
|
||
});
|
||
|
||
console.log('Pipeline created:', pipeline.id);
|
||
console.log('Initial state:', pipeline.currentState);
|
||
|
||
// Start the pipeline
|
||
await openclaw.startPipeline(pipeline.id);
|
||
console.log('Pipeline started');
|
||
|
||
// Execute parallel agents (4 projects × 3 roles pattern)
|
||
console.log('\n--- Parallel Agent Execution (4×3 Pattern) ---');
|
||
const projects = ['frontend', 'backend', 'database', 'tests'];
|
||
const roles = ['security', 'performance', 'quality'] as const;
|
||
|
||
const tasks = projects.flatMap(project =>
|
||
roles.map(role => ({
|
||
type: 'explorer' as const,
|
||
prompt: `Analyze ${project} for ${role} considerations`,
|
||
context: { project, role }
|
||
}))
|
||
);
|
||
|
||
console.log(`Executing ${tasks.length} agents in parallel...`);
|
||
const results = await openclaw.executeParallelAgents(tasks);
|
||
|
||
for (const [agentId, result] of results) {
|
||
console.log(`Agent ${agentId}: ${result.success ? '✓' : '✗'}`);
|
||
}
|
||
|
||
// Transition pipeline states
|
||
console.log('\n--- Pipeline State Transitions ---');
|
||
await openclaw.transitionPipeline(pipeline.id, 'analysis_complete');
|
||
console.log('Current state:', openclaw.getPipelineStatus(pipeline.id)?.currentState);
|
||
|
||
await openclaw.transitionPipeline(pipeline.id, 'design_approved');
|
||
console.log('Current state:', openclaw.getPipelineStatus(pipeline.id)?.currentState);
|
||
|
||
// Create isolated workspace
|
||
console.log('\n--- Workspace Isolation ---');
|
||
const workspace = await openclaw.createWorkspace({
|
||
permissions: ['read', 'write'],
|
||
quota: {
|
||
maxFiles: 1000,
|
||
maxSize: 100 * 1024 * 1024 // 100MB
|
||
}
|
||
});
|
||
console.log('Workspace created:', workspace.id);
|
||
|
||
// Use persistent memory
|
||
await openclaw.remember('pipelineState', {
|
||
pipelineId: pipeline.id,
|
||
currentState: openclaw.getPipelineStatus(pipeline.id)?.currentState
|
||
});
|
||
|
||
// Save context
|
||
await openclaw.saveContext('feature-auth');
|
||
console.log('Context saved');
|
||
|
||
// Get token stats
|
||
const tokenStats = openclaw.getTokenStats();
|
||
console.log('\nToken Stats:', tokenStats);
|
||
|
||
// Export full state
|
||
const state = openclaw.exportState();
|
||
console.log('\nExported state:', {
|
||
messages: state.context.messages.length,
|
||
agents: state.agents.length,
|
||
pipelines: state.pipelines.length
|
||
});
|
||
}
|
||
|
||
// Run the example
|
||
openclawExample().catch(console.error);
|