feat: Add complete Agentic Compaction & Pipeline System

- Context Compaction System with token counting and summarization
- Deterministic State Machine for flow control (no LLM decisions)
- Parallel Execution Engine (up to 12 concurrent sessions)
- Event-Driven Coordination via Event Bus
- Agent Workspace Isolation (tools, memory, identity, files)
- YAML Workflow Integration (OpenClaw/Lobster compatible)
- Claude Code integration layer
- Complete demo UI with real-time visualization
- Comprehensive documentation and README

Components:
- agent-system/: Context management, token counting, subagent spawning
- pipeline-system/: State machine, parallel executor, event bus, workflows
- skills/: AI capabilities (LLM, ASR, TTS, VLM, image generation, etc.)
- src/app/: Next.js demo application

Total: ~100KB of production-ready TypeScript code
This commit is contained in:
Z User
2026-03-03 12:40:47 +00:00
Unverified
parent 63a8b123c9
commit 2380d33861
152 changed files with 51569 additions and 817 deletions

311
download/PIPELINE_README.md Normal file
View File

@@ -0,0 +1,311 @@
# Deterministic Multi-Agent Pipeline System
A comprehensive, open-source implementation of **Claude Code-level architecture** for building deterministic, parallel, event-driven multi-agent pipelines.
## 🎯 Key Features
| Feature | Description |
|---------|-------------|
| **Deterministic Orchestration** | State machine controls flow, not LLM decisions |
| **Parallel Execution** | 4 projects × 3 roles = 12 concurrent agent sessions |
| **Event-Driven Coordination** | Agents finish work → next step triggers automatically |
| **Full Agent Capabilities** | Each agent gets tools, memory, identity, workspace |
| **YAML Workflow Support** | OpenClaw/Lobster-compatible workflow definitions |
## 📦 Package Contents
```
pipeline-system/
├── core/
│ └── state-machine.ts # Deterministic state machine engine
├── engine/
│ └── parallel-executor.ts # Parallel execution with worker pools
├── events/
│ └── event-bus.ts # Event-driven coordination system
├── workspace/
│ └── agent-workspace.ts # Isolated agent workspaces
├── workflows/
│ └── yaml-workflow.ts # YAML workflow parser (Lobster-compatible)
├── integrations/
│ └── claude-code.ts # Claude Code integration layer
└── index.ts # Main exports
```
## 🚀 Quick Start
### Installation
```bash
bun add z-ai-web-dev-sdk
```
Copy `pipeline-system/` to your project.
### Basic Usage
```typescript
import { PipelineOrchestrator, runWorkflow } from './pipeline-system';
// Option 1: Create a code pipeline
const orchestrator = new PipelineOrchestrator();
await orchestrator.initialize();
const pipelineId = await orchestrator.createPipeline({
name: 'Code Pipeline',
projects: [
{
id: 'project-1',
name: 'Authentication Module',
tasks: [
{ type: 'implement', description: 'Create auth module', role: 'programmer' },
{ type: 'review', description: 'Review code', role: 'reviewer' },
{ type: 'test', description: 'Test implementation', role: 'tester' }
]
}
],
roles: ['programmer', 'reviewer', 'tester'],
maxConcurrency: 12
});
// Option 2: Run predefined workflow
const workflowId = await runWorkflow('code-pipeline', {
projectId: 'my-project',
requirements: 'Build REST API'
});
// Subscribe to events
orchestrator.onEvent('agent.completed', (event) => {
console.log('Agent completed:', event.payload);
});
```
## 📐 Architecture
```
┌─────────────────────────────────────────────────────────────────┐
│ Pipeline Orchestrator │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ State Machine│ │ Event Bus │ │Parallel Exec │ │
│ │(Deterministic)│ │(Coordination)│ │ (Concurrency)│ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │ │ │ │
│ ┌──────┴────────────────┴─────────────────┴──────┐ │
│ │ Agent Workspaces │ │
│ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │
│ │ │Programmer│ │Reviewer │ │ Tester │ │ │
│ │ │ • Tools │ │ • Tools │ │ • Tools │ │ │
│ │ │ • Memory │ │ • Memory│ │ • Memory│ │ │
│ │ │ • Files │ │ • Files │ │ • Files │ │ │
│ │ └─────────┘ └─────────┘ └─────────┘ │ │
│ └────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ LLM Provider (ZAI SDK) │
└─────────────────────────────────────────────────────────────────┘
```
## 🔄 State Machine
### Define States
```typescript
const definition: StateMachineDefinition = {
id: 'code-pipeline',
name: 'Code Pipeline',
initial: 'start',
states: {
start: {
id: 'start',
name: 'Start',
type: 'start',
onExit: [{ event: 'start', target: 'code' }]
},
code: {
id: 'code',
name: 'Code',
type: 'action',
agent: 'programmer',
timeout: 300000,
retry: { maxAttempts: 2, backoff: 'exponential' },
onExit: [
{ event: 'completed', target: 'review' },
{ event: 'failed', target: 'failed' }
]
},
review: {
id: 'review',
name: 'Review',
type: 'choice',
onExit: [
{ event: 'approved', target: 'test', condition: { type: 'equals', field: 'approved', value: true } },
{ event: 'rejected', target: 'code' }
]
},
test: {
id: 'test',
name: 'Test',
type: 'action',
agent: 'tester',
onExit: [
{ event: 'passed', target: 'end' },
{ event: 'failed', target: 'failed' }
]
},
end: { id: 'end', name: 'End', type: 'end' },
failed: { id: 'failed', name: 'Failed', type: 'end' }
}
};
```
## ⚡ Parallel Execution
```typescript
const executor = new ParallelExecutionEngine({
maxWorkers: 4,
maxConcurrentPerWorker: 3,
taskTimeout: 300000
});
executor.start();
// Submit parallel tasks
const tasks = executor.submitBatch([
{ projectId: 'p1', role: 'programmer', type: 'implement', description: 'Auth module', priority: 'high' },
{ projectId: 'p2', role: 'programmer', type: 'implement', description: 'Payment module', priority: 'high' },
{ projectId: 'p3', role: 'programmer', type: 'implement', description: 'Dashboard', priority: 'medium' },
{ projectId: 'p4', role: 'programmer', type: 'implement', description: 'API service', priority: 'medium' }
]);
```
## 📨 Event Bus
```typescript
const eventBus = new EventBus();
// Subscribe to events
eventBus.subscribe({
eventType: 'code.written',
handler: async (event) => {
console.log('Code written:', event.payload);
// Trigger review
eventBus.publish({
type: 'review.start',
source: 'coordinator',
payload: { projectId: event.payload.projectId }
});
}
});
// Publish events
eventBus.publish({
type: 'code.written',
source: 'programmer-1',
payload: { projectId: 'p1', files: ['auth.ts', 'auth.test.ts'] }
});
```
## 📁 YAML Workflows (Lobster-Compatible)
```yaml
id: code-pipeline
name: Code Pipeline
initial: start
states:
start:
type: start
on:
start: code
code:
type: action
role: programmer
timeout: 30m
retry:
maxAttempts: 2
backoff: exponential
on:
completed: review
failed: failed
review:
type: choice
on:
approved: test
rejected: code
test:
type: action
role: tester
on:
passed: end
failed: test_failed
test_failed:
type: choice
on:
retry: code
abort: failed
end:
type: end
failed:
type: end
```
## 🤝 Integration with Claude Code & OpenClaw
### Claude Code Integration
```typescript
import { PipelineOrchestrator } from './pipeline-system';
const orchestrator = new PipelineOrchestrator();
await orchestrator.initialize();
// Create pipeline for Claude Code project
const pipelineId = await orchestrator.createPipeline({
name: 'Claude Code Pipeline',
projects: [
{
id: 'claude-project',
name: 'Claude Integration',
tasks: [
{ type: 'implement', description: 'Add MCP server', role: 'programmer' },
{ type: 'review', description: 'Review changes', role: 'reviewer' },
{ type: 'test', description: 'Test integration', role: 'tester' }
]
}
]
});
```
### OpenClaw Integration
```typescript
import { runWorkflow } from './pipeline-system';
// Run Lobster-compatible workflow
const workflowId = await runWorkflow('parallel-projects', {
projects: ['project1', 'project2', 'project3', 'project4'],
roles: ['programmer', 'reviewer', 'tester']
});
```
## 📊 Predefined Workflows
| Workflow | Description |
|----------|-------------|
| `code-pipeline` | Code → Review → Test with max 3 review iterations |
| `parallel-projects` | Run multiple projects in parallel |
| `human-approval` | Workflow with human approval gates |
## 📄 License
MIT License - Free to use, modify, and distribute.
## 🤝 Contributing
Contributions welcome! This is designed for easy integration with Claude Code and OpenClaw.

View File

@@ -1 +1,235 @@
Here are all the generated files.
# Agent System - Complete Implementation
A comprehensive, open-source implementation of context compaction, agent orchestration, and subagent spawning.
## 📦 Package Contents
```
agent-system/
├── core/
│ ├── token-counter.ts # Token counting & management
│ ├── summarizer.ts # Conversation summarization (uses LLM)
│ ├── context-manager.ts # Context compaction logic
│ ├── orchestrator.ts # Agent orchestration system
│ └── subagent-spawner.ts # Subagent spawning mechanism
├── agents/
│ ├── base-agent.ts # Base agent class
│ └── task-agent.ts # Task-specific agent
├── storage/
│ └── memory-store.ts # Persistent file storage
├── utils/
│ └── helpers.ts # Utility functions
└── index.ts # Main exports
```
## 🚀 Quick Start
### Installation
1. Copy the `agent-system` folder to your project
2. Install dependencies:
```bash
bun add z-ai-web-dev-sdk
```
### Basic Usage
```typescript
import {
TokenCounter,
ContextCompactor,
AgentOrchestrator,
SubagentSpawner,
createAgent
} from './agent-system';
// Token Counting
const counter = new TokenCounter(128000);
const result = counter.countText("Hello world");
console.log(result.tokens); // ~3 tokens
// Context Compaction
const compactor = new ContextCompactor({
maxTokens: 100000,
strategy: 'hybrid'
});
if (compactor.needsCompaction(messages)) {
const compacted = await compactor.compact(messages);
console.log(`Saved ${compacted.tokensSaved} tokens`);
}
// Agent Orchestration
const orchestrator = new AgentOrchestrator();
orchestrator.registerAgent({
id: 'worker-1',
name: 'Worker',
type: 'worker',
capabilities: ['process'],
maxConcurrentTasks: 3,
timeout: 60000
});
orchestrator.start();
// Subagent Spawning
const spawner = new SubagentSpawner();
const result = await spawner.executeWithSubagent(
'researcher',
'Research AI agents'
);
// Create Custom Agent
const agent = createAgent(
'MyAgent',
'You are a helpful assistant.'
);
await agent.initialize();
const response = await agent.act('Hello!');
```
## 📚 Components
### 1. Token Counter
Estimates token counts using character-based approximation (~4 chars/token).
```typescript
const counter = new TokenCounter(128000); // max tokens
// Count text
counter.countText("text"); // { tokens, characters, words }
// Count conversation
counter.countConversation(messages); // { total, breakdown[] }
// Check budget
counter.getBudget(usedTokens); // { used, remaining, total, percentageUsed }
```
### 2. Conversation Summarizer
Creates intelligent summaries using LLM.
```typescript
const summarizer = new ConversationSummarizer();
const result = await summarizer.summarize(messages, {
maxSummaryTokens: 1000,
extractKeyPoints: true,
extractDecisions: true,
extractActionItems: true
});
// result.summary, result.keyPoints[], result.decisions[], result.actionItems[]
```
### 3. Context Compactor
4 compaction strategies:
- `sliding-window` - Keep recent messages only
- `summarize-old` - Summarize older messages
- `priority-retention` - Keep important messages
- `hybrid` - Combine all strategies
```typescript
const compactor = new ContextCompactor({
maxTokens: 120000,
targetTokens: 80000,
strategy: 'hybrid',
preserveRecentCount: 6,
triggerThreshold: 80 // % of maxTokens
});
const result = await compactor.compact(messages);
// result.messages, result.tokensSaved, result.compressionRatio
```
### 4. Agent Orchestrator
Manages agent lifecycle and task routing.
```typescript
const orchestrator = new AgentOrchestrator();
// Register agent
orchestrator.registerAgent({
id: 'agent-1',
name: 'Worker',
type: 'worker',
capabilities: ['process', 'execute'],
maxConcurrentTasks: 3,
timeout: 60000
});
// Create task
orchestrator.createTask('process', 'Process data', { data: [...] });
// Events
orchestrator.on('task_completed', (event) => { ... });
// Start processing
orchestrator.start();
```
### 5. Subagent Spawner
Spawn and manage child agents for parallel execution.
```typescript
const spawner = new SubagentSpawner();
// Single execution
const result = await spawner.executeWithSubagent('explorer', 'Find files');
// Parallel execution
const results = await spawner.executeParallel([
{ type: 'explorer', input: 'Task 1' },
{ type: 'coder', input: 'Task 2' }
]);
// Pipeline execution (context passes between steps)
const pipeline = await spawner.executePipeline([
{ type: 'planner', input: 'Plan task' },
{ type: 'coder', input: (prev) => `Implement: ${prev}` },
{ type: 'reviewer', input: (prev) => `Review: ${prev}` }
]);
```
### 6. Predefined Subagent Types
- `explorer` - Code exploration
- `researcher` - Information gathering
- `coder` - Code generation
- `reviewer` - Code review
- `planner` - Task planning
- `executor` - Task execution
- `custom` - Your custom type
## 🔧 Configuration Options
### CompactionConfig
```typescript
{
maxTokens: 120000, // Maximum context tokens
targetTokens: 80000, // Target after compaction
strategy: 'hybrid', // Compaction strategy
preserveRecentCount: 6, // Keep last N messages
preserveSystemMessage: true, // Keep system message
priorityKeywords: ['important', 'critical'],
summaryMaxTokens: 2000, // Max tokens for summaries
triggerThreshold: 80 // % to trigger compaction
}
```
### AgentConfig
```typescript
{
id: 'agent-id',
name: 'Agent Name',
type: 'agent-type',
capabilities: ['task1', 'task2'],
maxConcurrentTasks: 3,
timeout: 60000
}
```
## 📄 License
MIT License - Free to use, modify, and distribute.
## 🤝 Contributing
Feel free to extend and customize for your needs!

BIN
download/agent-system.zip Normal file

Binary file not shown.

BIN
download/all-systems.zip Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.