Files
ruflo/v2/bin/stream-chain.js
T
rUvandClaude Opus 4.5 1b6a1971f7 feat(v3): add comprehensive v3 implementation plans and swarm architecture
- Reorganize v3/ into implementation/ subfolders:
  - planning/, architecture/, migration/, integration/
  - security/, adrs/, research/, swarm-plans/

- Add 15-agent concurrent swarm implementation plans:
  - SWARM-OVERVIEW.md: Hierarchical mesh topology, 4 phases
  - AGENT-SPECIFICATIONS.md: 15 agent configs with capabilities
  - TDD-LONDON-SCHOOL-PLAN.md: Mock-first TDD methodology
  - BENCHMARK-OPTIMIZATION.md: Performance targets (2.49x-7.47x)
  - DEPLOYMENT-PLAN.md: Release strategy, CI/CD, rollback
  - GITHUB-ISSUE-TRACKING.md: Issue workflow with agent replies

- Update .claude/settings.json with proper hooks format
- Add bash aliases for claude --dangerously-skip-permissions

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-04 15:40:51 +00:00

495 lines
14 KiB
JavaScript

#!/usr/bin/env node
/**
* Stream Chain Command - Working implementation for Claude Code
* Uses stream-json output but regular prompt input for compatibility
*/
import { spawn, execSync } from 'child_process';
/**
* Check if claude command is available
*/
function checkClaudeAvailable() {
try {
execSync('which claude', { stdio: 'ignore' });
return true;
} catch {
return false;
}
}
/**
* Extract content from stream-json output
*/
function extractContentFromStream(streamOutput) {
const lines = streamOutput.split('\n').filter(line => line.trim());
let content = '';
for (const line of lines) {
try {
const json = JSON.parse(line);
if (json.type === 'assistant' && json.message && json.message.content) {
for (const item of json.message.content) {
if (item.type === 'text' && item.text) {
content += item.text;
}
}
}
} catch (e) {
// Skip non-JSON lines
}
}
return content.trim();
}
/**
* Execute a single step with context from previous step
*/
async function executeStep(prompt, previousContent, stepNum, totalSteps, flags) {
return new Promise((resolve) => {
const startTime = Date.now();
const timeout = (flags.timeout || 30) * 1000;
// Build the full prompt with context
let fullPrompt = prompt;
if (previousContent) {
fullPrompt = `Previous step output:\n${previousContent}\n\nNext step: ${prompt}`;
}
// Build command args
const args = ['-p'];
// Always use stream-json output for parsing
args.push('--output-format', 'stream-json', '--verbose');
// Add the prompt
args.push(fullPrompt);
if (flags.verbose) {
console.log(` Command: claude ${args[0]} ${args[1]} ${args[2]} "${args[4].slice(0, 50)}..."`);
}
// Spawn Claude process
const claudeProcess = spawn('claude', args, {
stdio: ['pipe', 'pipe', 'pipe'],
env: process.env
});
let output = '';
let stderr = '';
let completed = false;
// Close stdin since we're not piping input
claudeProcess.stdin.end();
// Capture output
claudeProcess.stdout.on('data', (chunk) => {
output += chunk.toString();
});
// Capture errors
claudeProcess.stderr.on('data', (chunk) => {
stderr += chunk.toString();
});
// Handle completion
claudeProcess.on('close', (code) => {
if (completed) return;
completed = true;
const duration = Date.now() - startTime;
if (code !== 0) {
console.error(` Process exited with code ${code}`);
if (stderr && flags.verbose) {
console.error(` stderr: ${stderr.slice(0, 200)}`);
}
resolve({
success: false,
duration,
content: null,
rawOutput: output
});
return;
}
// Extract content from stream-json output
const content = extractContentFromStream(output);
resolve({
success: true,
duration,
content,
rawOutput: output
});
});
// Handle errors
claudeProcess.on('error', (error) => {
if (completed) return;
completed = true;
console.error(' Process error:', error.message);
resolve({
success: false,
duration: Date.now() - startTime,
content: null,
rawOutput: null
});
});
// Timeout
setTimeout(() => {
if (!completed) {
completed = true;
claudeProcess.kill('SIGTERM');
console.log(' ⏱️ Step timed out');
resolve({
success: false,
duration: timeout,
content: null,
rawOutput: null
});
}
}, timeout);
});
}
/**
* Execute the chain
*/
async function executeChain(prompts, flags) {
const results = [];
let previousContent = null;
for (let i = 0; i < prompts.length; i++) {
const prompt = prompts[i];
console.log(`\n🔄 Step ${i + 1}/${prompts.length}: ${prompt.slice(0, 50)}...`);
const result = await executeStep(
prompt,
previousContent,
i + 1,
prompts.length,
flags
);
results.push({
step: i + 1,
prompt: prompt.slice(0, 50),
success: result.success,
duration: result.duration,
content: result.content
});
if (!result.success) {
console.error(`❌ Step ${i + 1} failed`);
break;
}
console.log(`✅ Step ${i + 1} completed (${result.duration}ms)`);
if (result.content) {
previousContent = result.content;
if (flags.verbose) {
console.log(` Content: ${result.content.slice(0, 100)}...`);
}
}
}
return results;
}
/**
* Main command
*/
export async function streamChainCommand(args, flags) {
const subcommand = args[0] || 'help';
if (subcommand === 'help') {
showHelp();
return;
}
if (!checkClaudeAvailable()) {
console.error('❌ Claude Code not found');
console.log('Please ensure Claude Code is installed and available');
return;
}
switch (subcommand) {
case 'demo':
await runDemo(flags);
break;
case 'run':
await runCustom(args.slice(1), flags);
break;
case 'test':
await runTest(flags);
break;
case 'pipeline':
await runPipeline(args.slice(1), flags);
break;
default:
console.error(`❌ Unknown subcommand: ${subcommand}`);
showHelp();
}
}
function showHelp() {
console.log(`
🔗 NAME
claude-flow stream-chain - Connect multiple Claude instances via stream-json for chained workflows
📋 SYNOPSIS
claude-flow stream-chain <subcommand> [options]
📝 DESCRIPTION
Stream chaining enables multi-step Claude workflows where each step receives the full
output from the previous step, creating powerful agent pipelines with context preservation.
Uses Claude Code's --output-format stream-json to capture structured responses and
chains them together by injecting previous outputs into subsequent prompts.
📚 SUBCOMMANDS
run <prompt1> <prompt2> [...]
Execute a custom stream chain with your own prompts
Minimum 2 prompts required for chaining
Each prompt receives the output from the previous step
demo
Run a 3-step demonstration chain:
1. Write a Python function to reverse a string
2. Add type hints to the function
3. Add a docstring to the function
pipeline <type>
Execute predefined pipelines for common workflows:
analysis - Code analysis and improvement pipeline
Analyze → Identify issues → Generate report
refactor - Automated refactoring workflow
Find refactoring opportunities → Create plan → Apply changes
test - Comprehensive test generation
Analyze coverage → Design test cases → Generate tests
optimize - Performance optimization pipeline
Profile code → Identify bottlenecks → Apply optimizations
test
Test stream chain connection and context preservation
Verifies that output from step 1 is received by step 2
help
Display this comprehensive help message
⚙️ OPTIONS
--verbose
Show detailed execution information including:
- Full command lines being executed
- Content preview from each step
- Stream-json parsing details
--timeout <seconds>
Maximum time per step in seconds (default: 30)
Prevents hanging on long-running operations
--debug
Enable debug mode with full stream-json output
Shows raw JSON messages between steps
💡 EXAMPLES
# Run a custom 3-step code improvement chain
claude-flow stream-chain run "analyze this code" "suggest improvements" "implement the top 3"
# Execute the demo chain to see stream chaining in action
claude-flow stream-chain demo
# Run the analysis pipeline on your codebase
claude-flow stream-chain pipeline analysis
# Test that stream chaining is working correctly
claude-flow stream-chain test --verbose
# Custom refactoring workflow with extended timeout
claude-flow stream-chain run "find code smells" "prioritize fixes" "refactor" --timeout 60
# Debug mode to see raw stream-json messages
claude-flow stream-chain demo --debug --verbose
🔧 HOW IT WORKS
1. Step 1 executes with --output-format stream-json to capture structured output
2. The assistant's response is extracted from the stream-json format
3. Step 2 receives the previous output as context in its prompt
4. This continues for all steps in the chain
5. Each step has full context of all previous outputs
📊 STREAM-JSON FORMAT
Each step outputs newline-delimited JSON with message types:
{"type":"system","subtype":"init",...} - Session initialization
{"type":"assistant","message":{...}} - Claude's response
{"type":"tool_use","name":"...",...} - Tool invocations
{"type":"result","status":"success",...} - Completion status
⚡ PERFORMANCE
- Latency: ~10-30s per step depending on complexity
- Context: 100% preservation between steps
- Parallel: Steps run sequentially to maintain context
- Timeout: Default 30s per step, configurable
📋 REQUIREMENTS
- Claude Code must be installed and available in PATH
- Valid Claude API configuration
- Sufficient API credits for multiple Claude calls
🔍 TROUBLESHOOTING
"Command not found"
→ Ensure Claude Code is installed: npm install -g @anthropic-ai/claude-code
"Step timed out"
→ Increase timeout with --timeout flag
→ Check network connectivity
"Context not preserved"
→ Verify stream-json output with --verbose
→ Check that all steps completed successfully
"Invalid JSON"
→ Use --debug to see raw output
→ Report issue if stream format has changed
🔗 SEE ALSO
claude-flow swarm - Multi-agent coordination
claude-flow hive-mind - Collective intelligence mode
claude-flow sparc - SPARC development methodology
📖 DOCUMENTATION
Full docs: ./claude-flow-wiki/Stream-Chain-Command.md
Stream spec: ./docs/stream-chaining.md
GitHub: https://github.com/ruvnet/claude-flow
🏷️ VERSION
Claude Flow Alpha 89 - Stream Chain v2.0.0
`);
}
async function runDemo(flags) {
console.log('🎭 Running Stream Chain Demo');
console.log('━'.repeat(50));
const prompts = [
"Write a simple Python function to reverse a string",
"Add type hints to the function",
"Add a docstring to the function"
];
const results = await executeChain(prompts, flags);
showSummary(results);
}
async function runCustom(prompts, flags) {
if (prompts.length < 2) {
console.error('❌ Need at least 2 prompts');
return;
}
console.log('🔗 Starting Custom Chain');
console.log('━'.repeat(50));
const results = await executeChain(prompts, flags);
showSummary(results);
}
async function runTest(flags) {
console.log('🧪 Testing Stream Chain');
console.log('━'.repeat(50));
const prompts = [
"Say exactly: 'Step 1 complete'",
"If the previous step said 'Step 1 complete', say 'Chain working!'"
];
const results = await executeChain(prompts, { ...flags, verbose: true });
const success = results.every(r => r.success);
console.log('\n' + (success ? '✅ Test passed!' : '❌ Test failed'));
}
async function runPipeline(args, flags) {
const pipelineType = args[0];
const pipelines = {
analysis: {
name: 'Code Analysis Pipeline',
prompts: [
"Analyze the current directory structure and identify the main components",
"Based on the analysis, identify potential improvements and issues",
"Generate a detailed report with actionable recommendations"
]
},
refactor: {
name: 'Refactoring Workflow',
prompts: [
"Identify code that could benefit from refactoring in the current project",
"Create a prioritized refactoring plan with specific changes",
"Provide refactored code examples for the top 3 priorities"
]
},
test: {
name: 'Test Generation Pipeline',
prompts: [
"Analyze the codebase and identify areas lacking test coverage",
"Design comprehensive test cases for the critical functions",
"Generate unit test implementations with assertions"
]
},
optimize: {
name: 'Performance Optimization Pipeline',
prompts: [
"Profile the codebase and identify performance bottlenecks",
"Analyze the bottlenecks and suggest optimization strategies",
"Provide optimized implementations for the main issues"
]
}
};
if (!pipelineType || !pipelines[pipelineType]) {
console.error('❌ Invalid or missing pipeline type');
console.log('Available pipelines: ' + Object.keys(pipelines).join(', '));
console.log('Usage: claude-flow stream-chain pipeline <type>');
return;
}
const pipeline = pipelines[pipelineType];
console.log(`🚀 Running ${pipeline.name}`);
console.log('━'.repeat(50));
const results = await executeChain(pipeline.prompts, flags);
showSummary(results);
}
function showSummary(results) {
console.log('\n' + '═'.repeat(50));
console.log('📊 Summary');
console.log('═'.repeat(50));
for (const r of results) {
const status = r.success ? '✅' : '❌';
console.log(`${status} Step ${r.step}: ${r.prompt}... (${r.duration}ms)`);
}
const total = results.reduce((sum, r) => sum + r.duration, 0);
const success = results.filter(r => r.success).length;
console.log(`\n⏱️ Total: ${total}ms`);
console.log(`📈 Success: ${success}/${results.length}`);
}
export default streamChainCommand;