Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

πŸ”– Checkpoint: Edit /workspaces/claude-code-flow/src/cli/simple-commands/stream-chain-working.js

rUvc0194f1657e5
diff --git a/src/cli/simple-commands/stream-chain-working.js b/src/cli/simple-commands/stream-chain-working.js
new file mode 100644
index 0000000000..4cb09fa980
--- /dev/null
+++ b/src/cli/simple-commands/stream-chain-working.js
@@ -0,0 +1,327 @@
+#!/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;
+
+ default:
+ console.error(`❌ Unknown subcommand: ${subcommand}`);
+ showHelp();
+ }
+}
+
+function showHelp() {
+ console.log(`
+πŸ”— Stream Chain Command - Claude Code Context Chaining
+
+DESCRIPTION
+ Chain multiple Claude Code prompts with context preservation.
+ Each step receives the output from the previous step.
+
+USAGE
+ stream-chain <subcommand> [options]
+
+SUBCOMMANDS
+ run <p1> <p2> [...] Execute custom chain
+ demo Run demo chain
+ test Test chaining
+ help Show this help
+
+OPTIONS
+ --verbose Show detailed output
+ --timeout <seconds> Timeout per step (default: 30)
+
+EXAMPLES
+ stream-chain demo
+ stream-chain run "Write code" "Review it" "Improve it"
+ stream-chain test --verbose
+ `);
+}
+
+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'));
+}
+
+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;
\ No newline at end of file