From 7f06fd4b39fc5bbd498e53c8cb32212557602f7f Mon Sep 17 00:00:00 2001 From: rUv Date: Tue, 12 Aug 2025 17:37:00 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=96=20Checkpoint:=20Edit=20/workspaces?= =?UTF-8?q?/claude-code-flow/src/cli/simple-commands/stream-chain.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .claude/checkpoints/task-1755020170.json | 1 + src/cli/simple-commands/stream-chain.js | 165 +++++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 .claude/checkpoints/task-1755020170.json diff --git a/.claude/checkpoints/task-1755020170.json b/.claude/checkpoints/task-1755020170.json new file mode 100644 index 0000000000..2e5ba653ca --- /dev/null +++ b/.claude/checkpoints/task-1755020170.json @@ -0,0 +1 @@ +{"checkpoint": "task-20250812-173609", "task": "can i use the @docs/wiki/background-commands.md as part of the stream-chain command ", "timestamp": "2025-08-12T17:36:10Z", "commit": "35d59840c347ffd5ebf4faa3c90bb9c566cd59ba", "github_release": "true"} diff --git a/src/cli/simple-commands/stream-chain.js b/src/cli/simple-commands/stream-chain.js index eb3c7b91c6..2554f0f1d5 100644 --- a/src/cli/simple-commands/stream-chain.js +++ b/src/cli/simple-commands/stream-chain.js @@ -14,6 +14,12 @@ import path from 'path'; export async function streamChainCommand(args, flags) { const subcommand = args[0] || 'help'; + // Check if background flag is set + if (flags.background || flags.bg) { + console.log('šŸ”„ Running stream chain in background...'); + return runBackgroundStreamChain(args, flags); + } + switch (subcommand) { case 'run': return runStreamChain(args.slice(1), flags); @@ -27,12 +33,171 @@ export async function streamChainCommand(args, flags) { case 'test': return testStreamConnection(flags); + case 'monitor': + return monitorBackgroundChains(flags); + + case 'kill': + return killBackgroundChain(args.slice(1), flags); + case 'help': default: return showStreamChainHelp(); } } +/** + * Run stream chain in background + */ +async function runBackgroundStreamChain(args, flags) { + const { spawn } = await import('child_process'); + const subcommand = args[0] || 'run'; + + // Build command for background execution + const command = `npx claude-flow stream-chain ${args.join(' ')}`; + const flagString = Object.entries(flags) + .filter(([key]) => key !== 'background' && key !== 'bg') + .map(([key, value]) => `--${key}${value === true ? '' : ` ${value}`}`) + .join(' '); + + const fullCommand = `${command} ${flagString}`.trim(); + + console.log(`šŸ“‹ Command: ${fullCommand}`); + + // Spawn in background + const child = spawn('sh', ['-c', fullCommand], { + detached: true, + stdio: ['ignore', 'pipe', 'pipe'] + }); + + // Store background process info + const processId = `stream_${Date.now()}`; + await storeBackgroundProcess(processId, fullCommand, child.pid); + + console.log(`āœ… Stream chain started in background`); + console.log(` Process ID: ${processId}`); + console.log(` PID: ${child.pid}`); + console.log(''); + console.log('šŸ“Š Monitor with: stream-chain monitor'); + console.log(`šŸ›‘ Stop with: stream-chain kill ${processId}`); + + // Detach from child process + child.unref(); + + return { processId, pid: child.pid }; +} + +/** + * Store background process information + */ +async function storeBackgroundProcess(processId, command, pid) { + const fs = await import('fs/promises'); + const processFile = '.claude-flow/stream-chains.json'; + + let processes = {}; + try { + const data = await fs.readFile(processFile, 'utf8'); + processes = JSON.parse(data); + } catch { + // File doesn't exist yet + } + + processes[processId] = { + command, + pid, + startTime: new Date().toISOString(), + status: 'running' + }; + + await fs.mkdir('.claude-flow', { recursive: true }); + await fs.writeFile(processFile, JSON.stringify(processes, null, 2)); +} + +/** + * Monitor background stream chains + */ +async function monitorBackgroundChains(flags) { + const fs = await import('fs/promises'); + const processFile = '.claude-flow/stream-chains.json'; + + try { + const data = await fs.readFile(processFile, 'utf8'); + const processes = JSON.parse(data); + + console.log('šŸ“Š Background Stream Chains'); + console.log('━'.repeat(50)); + + for (const [id, info] of Object.entries(processes)) { + const status = await checkProcessStatus(info.pid); + console.log(`\nšŸ”— ${id}`); + console.log(` Command: ${info.command}`); + console.log(` PID: ${info.pid}`); + console.log(` Started: ${info.startTime}`); + console.log(` Status: ${status ? '🟢 Running' : 'šŸ”“ Stopped'}`); + } + + if (Object.keys(processes).length === 0) { + console.log('No background stream chains running'); + } + } catch (error) { + console.log('No background stream chains found'); + } +} + +/** + * Kill a background stream chain + */ +async function killBackgroundChain(args, flags) { + const processId = args[0]; + + if (!processId) { + console.error('āŒ Error: Please specify a process ID'); + console.log('Usage: stream-chain kill '); + return; + } + + const fs = await import('fs/promises'); + const processFile = '.claude-flow/stream-chains.json'; + + try { + const data = await fs.readFile(processFile, 'utf8'); + const processes = JSON.parse(data); + + if (!processes[processId]) { + console.error(`āŒ Process ${processId} not found`); + return; + } + + const info = processes[processId]; + + // Kill the process + try { + process.kill(info.pid, 'SIGTERM'); + console.log(`āœ… Killed stream chain ${processId} (PID: ${info.pid})`); + + // Update status + processes[processId].status = 'killed'; + processes[processId].endTime = new Date().toISOString(); + await fs.writeFile(processFile, JSON.stringify(processes, null, 2)); + } catch (error) { + console.error(`āŒ Failed to kill process: ${error.message}`); + } + } catch (error) { + console.error('āŒ Error reading process file:', error.message); + } +} + +/** + * Check if a process is still running + */ +async function checkProcessStatus(pid) { + try { + process.kill(pid, 0); + return true; + } catch { + return false; + } +} + /** * Run a custom stream chain */