🌊 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
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
feat(G3): real workflow runtime — task/wait/condition step execution
Reuven6206b93dab77
diff --git a/v3/@claude-flow/cli/src/mcp-tools/agent-execute-core.ts b/v3/@claude-flow/cli/src/mcp-tools/agent-execute-core.tsnew file mode 100644index 0000000000..3cbfbf1b1e--- /dev/null+++ b/v3/@claude-flow/cli/src/mcp-tools/agent-execute-core.ts@@ -0,0 +1,194 @@+/**+ * Shared agent-execution core.+ *+ * Both the agent_execute MCP tool and the workflow runtime (G3) need+ * to dispatch a prompt to an agent's configured Anthropic model. This+ * module factors that path out so it's testable and reusable, and+ * keeps the wire from agent_spawn → ProviderManager (real) in one+ * place rather than duplicated.+ */++import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';+import { join } from 'node:path';+import { getProjectCwd } from './types.js';++const STORAGE_DIR = '.claude-flow';+const AGENT_DIR = 'agents';+const AGENT_FILE = 'store.json';++type ClaudeModel = 'haiku' | 'sonnet' | 'opus' | 'inherit';++export interface AgentRecord {+ agentId: string;+ agentType: string;+ status: 'idle' | 'busy' | 'terminated';+ health: number;+ taskCount: number;+ config: Record<string, unknown>;+ createdAt: string;+ domain?: string;+ model?: ClaudeModel;+ modelRoutedBy?: 'explicit' | 'router' | 'agent-booster' | 'default';+ lastResult?: Record<string, unknown>;+}++interface AgentStore {+ agents: Record<string, AgentRecord>;+ version: string;+}++function getAgentDir(): string { return join(getProjectCwd(), STORAGE_DIR, AGENT_DIR); }+function getAgentPath(): string { return join(getAgentDir(), AGENT_FILE); }+function ensureAgentDir(): void {+ const dir = getAgentDir();+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true });+}+function loadAgentStore(): AgentStore {+ try {+ if (existsSync(getAgentPath())) return JSON.parse(readFileSync(getAgentPath(), 'utf-8'));+ } catch { /* fall through */ }+ return { agents: {}, version: '3.0.0' };+}+function saveAgentStore(store: AgentStore): void {+ ensureAgentDir();+ writeFileSync(getAgentPath(), JSON.stringify(store, null, 2), 'utf-8');+}++const MODEL_MAP: Record<string, string> = {+ haiku: 'claude-3-5-haiku-latest',+ sonnet: 'claude-3-5-sonnet-latest',+ opus: 'claude-3-opus-latest',+ inherit: 'claude-3-5-sonnet-latest',+};++export interface AgentExecuteInput {+ agentId: string;+ prompt: string;+ systemPrompt?: string;+ maxTokens?: number;+ temperature?: number;+ timeoutMs?: number;+}++export interface AgentExecuteResult {+ success: boolean;+ agentId: string;+ model?: string;+ messageId?: string;+ stopReason?: string;+ output?: string;+ usage?: { inputTokens: number; outputTokens: number; totalTokens: number };+ durationMs?: number;+ error?: string;+ remediation?: string;+}++export async function executeAgentTask(input: AgentExecuteInput): Promise<AgentExecuteResult> {+ const apiKey = process.env.ANTHROPIC_API_KEY;+ if (!apiKey) {+ return {+ success: false,+ agentId: input.agentId,+ error: 'ANTHROPIC_API_KEY not set in environment',+ remediation: 'Set the env var and re-run. The key is read at call time.',+ };+ }++ const store = loadAgentStore();+ const agent = store.agents[input.agentId];+ if (!agent) return { success: false, agentId: input.agentId, error: 'Agent not found' };+ if (agent.status === 'terminated') return { success: false, agentId: input.agentId, error: 'Agent has been terminated' };++ const anthropicModel = MODEL_MAP[agent.model || 'sonnet'] || 'claude-3-5-sonnet-latest';+ const systemPrompt = input.systemPrompt ||+ `You are a ${agent.agentType} agent operating as part of a Ruflo swarm. ` ++ `Agent ID: ${input.agentId}. Domain: ${agent.domain ?? 'general'}. ` ++ `Respond directly and stay focused on the task. If you need information you don't have, state that explicitly.`;++ agent.status = 'busy';+ agent.taskCount = (agent.taskCount || 0) + 1;+ saveAgentStore(store);++ const startedAt = Date.now();++ try {+ const controller = new AbortController();+ const timeoutMs = input.timeoutMs || 60000;+ const timer = setTimeout(() => controller.abort(), timeoutMs);++ const res = await fetch('https://api.anthropic.com/v1/messages', {+ method: 'POST',+ headers: {+ 'x-api-key': apiKey,+ 'anthropic-version': '2023-06-01',+ 'content-type': 'application/json',+ },+ body: JSON.stringify({+ model: anthropicModel,+ max_tokens: input.maxTokens || 1024,+ temperature: typeof input.temperature === 'number' ? input.temperature : 0.7,+ system: systemPrompt,+ messages: [{ role: 'user', content: input.prompt }],+ }),+ signal: controller.signal,+ });+ clearTimeout(timer);++ if (!res.ok) {+ const errText = await res.text().catch(() => '<unreadable error body>');+ agent.status = 'idle';+ saveAgentStore(store);+ return {+ success: false,+ agentId: input.agentId,+ model: anthropicModel,+ error: `Anthropic API error ${res.status}: ${errText.slice(0, 400)}`,+ };+ }++ const data = await res.json() as {+ id: string;+ model: string;+ content: Array<{ type: string; text?: string }>;+ stop_reason: string;+ usage: { input_tokens: number; output_tokens: number };+ };++ const textOut = data.content+ .filter(c => c.type === 'text' && typeof c.text === 'string')+ .map(c => c.text as string)+ .join('');++ const result: AgentExecuteResult = {+ success: true,+ agentId: input.agentId,+ messageId: data.id,+ model: data.model,+ stopReason: data.stop_reason,+ output: textOut,+ usage: {+ inputTokens: data.usage.input_tokens,+ outputTokens: data.usage.output_tokens,+ totalTokens: data.usage.input_tokens + data.usage.output_tokens,+ },+ durationMs: Date.now() - startedAt,+ };++ agent.status = 'idle';+ agent.lastResult = result as unknown as Record<string, unknown>;+ saveAgentStore(store);++ return result;+ } catch (err) {+ agent.status = 'idle';+ saveAgentStore(store);+ const msg = err instanceof Error ? err.message : String(err);+ return {+ success: false,+ agentId: input.agentId,+ model: anthropicModel,+ error: `agent_execute failed: ${msg}`,+ durationMs: Date.now() - startedAt,+ };+ }+}diff --git a/v3/@claude-flow/cli/src/mcp-tools/agent-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/agent-tools.tsindex 6309f9ca05..c16d0d45dc 100644--- a/v3/@claude-flow/cli/src/mcp-tools/agent-tools.ts+++ b/v3/@claude-flow/cli/src/mcp-tools/agent-tools.ts@@ -9,6 +9,7 @@ import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs';import { join } from 'node:path';import { type MCPTool, getProjectCwd } from './types.js';import { validateIdentifier, validateText, validateAgentSpawn } from './validate-input.js';+import { executeAgentTask } from './agent-execute-core.js';// Storage pathsconst STORAGE_DIR = '.claude-flow';@@ -303,128 +304,15 @@ export const agentTools: MCPTool[] = [const vP = validateText(input.prompt as string, 'prompt');if (!vP.valid) return { success: false, error: `Input validation failed: ${vP.error}` };- const apiKey = process.env.ANTHROPIC_API_KEY;- if (!apiKey) {- return {- success: false,- error: 'ANTHROPIC_API_KEY not set in environment',- remediation: 'Set the env var with a valid Anthropic API key, then re-run. The key is read at call time, not at spawn time.',- };- }-- const store = loadAgentStore();- const agentId = input.agentId as string;- const agent = store.agents[agentId];- if (!agent) {- return { success: false, error: 'Agent not found', agentId };- }- if (agent.status === 'terminated') {- return { success: false, error: 'Agent has been terminated', agentId };- }-- // Map the agent's logical model name to the Anthropic model ID.- const modelMap: Record<string, string> = {- haiku: 'claude-3-5-haiku-latest',- sonnet: 'claude-3-5-sonnet-latest',- opus: 'claude-3-opus-latest',- inherit: 'claude-3-5-sonnet-latest',- };- const anthropicModel = modelMap[agent.model || 'sonnet'] || 'claude-3-5-sonnet-latest';-- // Default system prompt reflects the agent's role.- const systemPrompt = (input.systemPrompt as string) ||- `You are a ${agent.agentType} agent operating as part of a Ruflo swarm. ` +- `Agent ID: ${agentId}. Domain: ${agent.domain ?? 'general'}. ` +- `Respond directly and stay focused on the task. If you need information you don\'t have, state that explicitly.`;-- // Mark agent busy + bump task count.- agent.status = 'busy';- agent.taskCount = (agent.taskCount || 0) + 1;- saveAgentStore(store);-- const startedAt = Date.now();- let result: Record<string, unknown>;-- try {- const controller = new AbortController();- const timeoutMs = (input.timeoutMs as number) || 60000;- const timer = setTimeout(() => controller.abort(), timeoutMs);-- const res = await fetch('https://api.anthropic.com/v1/messages', {- method: 'POST',- headers: {- 'x-api-key': apiKey,- 'anthropic-version': '2023-06-01',- 'content-type': 'application/json',- },- body: JSON.stringify({- model: anthropicModel,- max_tokens: (input.maxTokens as number) || 1024,- temperature: typeof input.temperature === 'number' ? input.temperature : 0.7,- system: systemPrompt,- messages: [{ role: 'user', content: input.prompt as string }],- }),- signal: controller.signal,- });- clearTimeout(timer);-- if (!res.ok) {- const errText = await res.text().catch(() => '<unreadable error body>');- // Restore agent state on failure- agent.status = 'idle';- saveAgentStore(store);- return {- success: false,- error: `Anthropic API error ${res.status}: ${errText.slice(0, 400)}`,- agentId,- model: anthropicModel,- };- }-- const data = await res.json() as {- id: string;- model: string;- content: Array<{ type: string; text?: string }>;- stop_reason: string;- usage: { input_tokens: number; output_tokens: number };- };-- const textOut = data.content- .filter(c => c.type === 'text' && typeof c.text === 'string')- .map(c => c.text as string)- .join('');-- result = {- messageId: data.id,- model: data.model,- stopReason: data.stop_reason,- output: textOut,- usage: {- inputTokens: data.usage.input_tokens,- outputTokens: data.usage.output_tokens,- totalTokens: data.usage.input_tokens + data.usage.output_tokens,- },- durationMs: Date.now() - startedAt,- };-- // Persist on the agent record.- agent.status = 'idle';- agent.lastResult = result;- saveAgentStore(store);-- return { success: true, agentId, ...result };- } catch (err) {- agent.status = 'idle';- saveAgentStore(store);- const msg = err instanceof Error ? err.message : String(err);- return {- success: false,- error: `agent_execute failed: ${msg}`,- agentId,- model: anthropicModel,- durationMs: Date.now() - startedAt,- };- }+ // Delegate to the shared core (also used by the workflow runtime).+ return executeAgentTask({+ agentId: input.agentId as string,+ prompt: input.prompt as string,+ systemPrompt: input.systemPrompt as string | undefined,+ maxTokens: input.maxTokens as number | undefined,+ temperature: input.temperature as number | undefined,+ timeoutMs: input.timeoutMs as number | undefined,+ });},},{diff --git a/v3/@claude-flow/cli/src/mcp-tools/workflow-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/workflow-tools.tsindex 04ca006496..3b2011efad 100644--- a/v3/@claude-flow/cli/src/mcp-tools/workflow-tools.ts+++ b/v3/@claude-flow/cli/src/mcp-tools/workflow-tools.ts@@ -8,6 +8,7 @@ import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs';import { join } from 'node:path';import { type MCPTool, getProjectCwd } from './types.js';import { validateIdentifier, validatePath, validateText } from './validate-input.js';+import { executeAgentTask } from './agent-execute-core.js';// Storage pathsconst STORAGE_DIR = '.claude-flow';@@ -284,7 +285,6 @@ export const workflowTools: MCPTool[] = [if (!workflow) {return { workflowId, error: 'Workflow not found' };}-if (workflow.status === 'running') {return { workflowId, error: 'Workflow already running' };}@@ -297,29 +297,152 @@ export const workflowTools: MCPTool[] = [workflow.status = 'running';workflow.startedAt = new Date().toISOString();workflow.currentStep = (input.startFromStep as number) || 0;+ saveWorkflowStore(store);++ // ADR-095 G3: real workflow runtime. Walk the steps in order;+ // dispatch each by type. Persist progress after each step so a+ // crash or pause can resume cleanly. No mock — task steps make+ // real LLM calls via agent_execute (G1's wire).+ const stepResults: Array<{ stepId: string; type: string; status: string; durationMs?: number; output?: string; error?: string }> = [];++ // Variable substitution: {{name}} → workflow.variables[name] OR steps[stepId].output+ const interp = (text: string): string => {+ return text.replace(/\{\{\s*([a-zA-Z_][\w.]*)\s*\}\}/g, (_, key) => {+ // Direct variable+ if (key in workflow.variables) return String(workflow.variables[key]);+ // Step-output reference: stepId.output+ const dot = key.indexOf('.');+ if (dot > 0) {+ const stepId = key.slice(0, dot);+ const field = key.slice(dot + 1);+ const prior = stepResults.find(s => s.stepId === stepId);+ if (prior && field === 'output' && typeof prior.output === 'string') return prior.output;+ }+ return '';+ });+ };++ const startedAt = Date.now();+ let i = workflow.currentStep;+ while (i < workflow.steps.length) {+ // Honor pause/cancel signals between steps.+ const live = loadWorkflowStore().workflows[workflowId];+ if (!live || live.status === 'paused') {+ workflow.status = 'paused';+ workflow.currentStep = i;+ saveWorkflowStore(store);+ break;+ }+ if (live.status === 'failed') {+ workflow.status = 'failed';+ saveWorkflowStore(store);+ break;+ }- // Set steps to pending — actual execution requires agent assignment via task tools- const results: Array<{ stepId: string; status: string; _note: string }> = [];- for (let i = workflow.currentStep; i < workflow.steps.length; i++) {const step = workflow.steps[i];- step.status = 'pending';+ step.status = 'running';+ step.startedAt = new Date().toISOString();+ const stepStart = Date.now();+ saveWorkflowStore(store);- results.push({- stepId: step.stepId,- status: step.status,- _note: 'Workflow execution tracks state. Actual step execution requires agent assignment via task tools.',- });+ let stepEntry: typeof stepResults[number] = { stepId: step.stepId, type: step.type, status: 'running' };++ try {+ if (step.type === 'task') {+ const cfg = step.config as Record<string, unknown>;+ const agentId = (cfg.agentId as string) || (workflow.variables.defaultAgentId as string);+ const promptTpl = (cfg.prompt as string) || step.name;+ if (!agentId) throw new Error(`task step ${step.stepId} requires config.agentId or workflow.variables.defaultAgentId`);+ const prompt = interp(promptTpl);+ const result = await executeAgentTask({+ agentId,+ prompt,+ systemPrompt: cfg.systemPrompt ? interp(String(cfg.systemPrompt)) : undefined,+ maxTokens: cfg.maxTokens as number | undefined,+ temperature: cfg.temperature as number | undefined,+ timeoutMs: cfg.timeoutMs as number | undefined,+ });+ if (!result.success) throw new Error(result.error || 'agent_execute failed');+ step.result = result;+ workflow.variables[`${step.stepId}.output`] = result.output;+ workflow.variables.lastStepOutput = result.output;+ stepEntry = { stepId: step.stepId, type: 'task', status: 'completed', durationMs: result.durationMs, output: result.output };+ } else if (step.type === 'wait') {+ const cfg = step.config as Record<string, unknown>;+ const ms = Math.min(Math.max(0, (cfg.ms as number) || 0), 60000);+ await new Promise(r => setTimeout(r, ms));+ step.result = { waitedMs: ms };+ stepEntry = { stepId: step.stepId, type: 'wait', status: 'completed', durationMs: ms };+ } else if (step.type === 'condition') {+ // Simple condition: config.when is a JS expression evaluated against workflow.variables.+ // For safety, we only support `var === 'value'` or `var === number`.+ const cfg = step.config as Record<string, unknown>;+ const expr = String(cfg.when || 'true').trim();+ const m = expr.match(/^([a-zA-Z_][\w]*)\s*===?\s*(['\"])?([^'\"]*)\2?$/);+ let truthy = false;+ if (m) {+ const v = workflow.variables[m[1]];+ const expected = m[2] ? m[3] : Number(m[3]);+ truthy = v === expected;+ } else if (expr === 'true') truthy = true;+ step.result = { conditionExpr: expr, truthy };+ // condition can declare a target step index to jump to via cfg.thenStep / cfg.elseStep+ if (typeof cfg.thenStep === 'number' && truthy) i = (cfg.thenStep as number) - 1;+ if (typeof cfg.elseStep === 'number' && !truthy) i = (cfg.elseStep as number) - 1;+ stepEntry = { stepId: step.stepId, type: 'condition', status: 'completed' };+ } else {+ // parallel/loop are deferred — mark skipped honestly rather than mock-completing.+ step.result = { _note: `step type '${step.type}' not yet implemented in runtime` };+ stepEntry = { stepId: step.stepId, type: step.type, status: 'skipped' };+ }+ step.status = stepEntry.status === 'skipped' ? 'skipped' : 'completed';+ step.completedAt = new Date().toISOString();+ } catch (err) {+ const msg = err instanceof Error ? err.message : String(err);+ step.status = 'failed';+ step.result = { error: msg };+ step.completedAt = new Date().toISOString();+ stepEntry = { stepId: step.stepId, type: step.type, status: 'failed', durationMs: Date.now() - stepStart, error: msg };+ stepResults.push(stepEntry);+ workflow.status = 'failed';+ workflow.error = msg;+ workflow.completedAt = new Date().toISOString();+ saveWorkflowStore(store);+ return {+ workflowId,+ status: 'failed',+ error: msg,+ failedStep: step.stepId,+ stepsCompleted: stepResults.filter(s => s.status === 'completed').length,+ results: stepResults,+ durationMs: Date.now() - startedAt,+ };+ }++ if (typeof stepEntry.durationMs !== 'number') stepEntry.durationMs = Date.now() - stepStart;+ stepResults.push(stepEntry);+ workflow.currentStep = i + 1;+ saveWorkflowStore(store);+ i++;}- saveWorkflowStore(store);+ if (workflow.status === 'running') {+ workflow.status = 'completed';+ workflow.completedAt = new Date().toISOString();+ saveWorkflowStore(store);+ }return {workflowId,status: workflow.status,- totalSteps: results.length,- results,+ totalSteps: workflow.steps.length,+ stepsCompleted: stepResults.filter(s => s.status === 'completed').length,+ stepsSkipped: stepResults.filter(s => s.status === 'skipped').length,+ stepsFailed: stepResults.filter(s => s.status === 'failed').length,+ results: stepResults,startedAt: workflow.startedAt,- _note: 'Workflow is now running. Steps are in pending state and must be executed via task tools.',+ completedAt: workflow.completedAt,+ durationMs: Date.now() - startedAt,};},},