feat: introduce workflow management tools and commands

- Added new workflow commands: `/workflow` to open the workflow panel and `/workflow run <prompt>` to execute workflows.
- Implemented `spawn_agents` and `spawn_pipeline` tools for parallel and sequential task execution, respectively.
- Enhanced workflow engine to handle real-time agent status updates and display in the UI.
- Updated workflow panel to show agent statuses, findings count, and session counters.
- Refactored existing code to integrate new workflow functionalities and improve overall structure.
This commit is contained in:
asepharyana
2026-07-12 17:49:34 +07:00
parent 7bdfd9c4c4
commit 53b0cb271f
14 changed files with 668 additions and 173 deletions
+142 -86
View File
@@ -1,111 +1,167 @@
//! Workflow status panel rendering.
//!
//! Flow: `draw_workflow_panel` reads `state.session_runtime` and
//! `state.workflow_engine` and renders a compact `List` of counters
//! (messages, completed/pending tool calls, active bash jobs, agents,
//! findings) plus the current auto-run phase.
//! Flow: `draw_workflow_panel` reads `state.workflow_engine` and renders a
//! rich panel showing agent statuses, findings count, session counters,
//! and usage hints.
//!
//! Why: shows a placeholder panel when there is no active session
//! runtime, and only emits rows for counters that are nonzero, to keep
//! the panel compact during simple single-turn sessions.
//! Why: the panel is useful even without a running session (shows engine
//! state and instructions), and only shows non-zero counters to keep it
//! compact.
use ratatui::layout::Rect;
use ratatui::style::{Style, Modifier};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, List, ListItem};
use ratatui::widgets::{Block, Borders, List, ListItem, Paragraph, Wrap};
use ratatui::Frame;
use super::theme::Theme;
use crate::app::workflow::engine::AgentState;
/// Render the workflow status panel summarizing the active session runtime.
/// Render the workflow status panel.
///
/// Flow: bail out with a "No active session" placeholder if
/// `state.session_runtime` is None → otherwise build a list of status
/// lines (message count, completed/pending tool calls, active bash jobs,
/// agent/finding counts, auto-run phase) → render as a List widget.
///
/// Why: rows for pending tool queue, bash jobs, agents, and findings are
/// only shown when their count is nonzero, to keep the panel compact
/// during simple single-turn sessions.
/// Flow: build header lines (title + usage hints) → if agents exist,
/// list each with its lifecycle state colour-coded → show findings
/// count and session counters → fall back to an instruction paragraph
/// when no agents have been spawned yet.
///
/// Return: nothing; draws directly into `frame` at `area`.
pub fn draw_workflow_panel(frame: &mut Frame, area: Rect, state: &crate::app::state::rest::AppStateRest) {
use ratatui::layout::{Constraint, Direction, Layout};
let block = Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(Theme::BORDER))
.title(" Workflow ");
.border_style(Style::default().fg(Theme::PRIMARY))
.title(Span::styled(" Workflow ", Style::default().fg(Theme::PRIMARY).add_modifier(Modifier::BOLD)));
let session_runtime = match &state.session_runtime {
Some(r) => r,
None => {
let empty = Block::default().borders(Borders::ALL).title(" Workflow ");
let paragraph = ratatui::widgets::Paragraph::new(Line::from(Span::raw("No active session")))
.block(empty);
frame.render_widget(paragraph, area);
return;
}
};
let inner = block.inner(area);
frame.render_widget(block, area);
let tool_count = session_runtime.tool_call_results.len();
let pending_count = session_runtime.pending_tool_queue.len();
let bash_count = session_runtime.bash_jobs.len();
// Split inner into header (hints) and body (agent list / status)
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(3), // header / hints
Constraint::Min(4), // agent list or placeholder
])
.split(inner);
let agent_count = state.workflow_engine.agents.len();
let findings_count = state.workflow_engine.findings.len();
// ── Header: usage hints ─────────────────────────────────────────────────
let hint_lines = vec![
Line::from(vec![
Span::styled("/workflow run ", Style::default().fg(Theme::PRIMARY).add_modifier(Modifier::BOLD)),
Span::styled("<prompt>", Style::default().fg(Theme::DIM)),
Span::styled(" · Esc to close", Style::default().fg(Theme::DIM)),
]),
Line::from(vec![
Span::styled("Status: ", Style::default().fg(Theme::DIM)),
if state.turn_in_flight() {
Span::styled("● Running", Style::default().fg(Theme::WARNING).add_modifier(Modifier::BOLD))
} else {
Span::styled("● Idle", Style::default().fg(Theme::SUCCESS))
},
Span::raw(" "),
Span::styled(
format!("Agents: {} Findings: {}",
state.workflow_engine.agents.len(),
state.workflow_engine.findings.len(),
),
Style::default().fg(Theme::DIM),
),
]),
];
let header = Paragraph::new(hint_lines);
frame.render_widget(header, chunks[0]);
let mut items = Vec::new();
items.push(ListItem::new(Line::from(Span::styled(
format!(" Messages: {}", session_runtime.messages.len()),
Style::default().fg(Theme::TEXT),
))));
items.push(ListItem::new(Line::from(Span::styled(
format!(" Tool calls completed: {}", tool_count),
Style::default().fg(Theme::SUCCESS),
))));
if pending_count > 0 {
items.push(ListItem::new(Line::from(Span::styled(
format!(" Pending tool queue: {}", pending_count),
Style::default().fg(Theme::WARNING),
))));
}
if bash_count > 0 {
items.push(ListItem::new(Line::from(Span::styled(
format!(" Active bash jobs: {}", bash_count),
Style::default().fg(Theme::WARNING),
))));
}
if agent_count > 0 {
items.push(ListItem::new(Line::from(Span::styled(
format!(" Agents: {}", agent_count),
Style::default().fg(Theme::INFO),
))));
}
if findings_count > 0 {
items.push(ListItem::new(Line::from(Span::styled(
format!(" Findings: {}", findings_count),
Style::default().fg(Theme::WARNING),
))));
}
let phase_status = if state.turn_in_flight() {
"Auto-running"
// ── Body: agent list or placeholder ─────────────────────────────────────
if state.workflow_engine.agents.is_empty() {
// No agents yet — show session counters and a welcome message
let session_lines = build_session_lines(state);
let placeholder = Paragraph::new(session_lines).wrap(Wrap { trim: false });
frame.render_widget(placeholder, chunks[1]);
} else {
"Awaiting input"
};
// Build agent status list
let items: Vec<ListItem> = state.workflow_engine.agents.iter().map(|agent| {
let (state_str, state_color) = match agent.status.state {
AgentState::Idle => ("Idle", Theme::DIM),
AgentState::Running => ("Running…", Theme::WARNING),
AgentState::Completed => ("Done ✓", Theme::SUCCESS),
AgentState::Failed => ("Failed ✗", Theme::ERROR),
};
let duration_str = match (agent.status.started_at, agent.status.completed_at) {
(Some(s), Some(e)) => format!(" {}ms", e.saturating_sub(s)),
(Some(_), None) => " (running)".to_string(),
_ => String::new(),
};
ListItem::new(Line::from(vec![
Span::styled(
format!(" {:8} ", state_str),
Style::default().fg(state_color).add_modifier(Modifier::BOLD),
),
Span::styled(
format!("{}{}", agent.name, duration_str),
Style::default().fg(Theme::TEXT),
),
if let Some(ref err) = agent.status.error {
Span::styled(format!("{}", err), Style::default().fg(Theme::ERROR))
} else {
Span::raw("")
},
]))
}).collect();
items.push(ListItem::new(Line::from(Span::styled(
format!(" Phase: {}", phase_status),
Style::default().fg(Theme::PRIMARY),
))));
let list = List::new(items)
.highlight_style(Style::default().add_modifier(Modifier::BOLD));
frame.render_widget(list, chunks[1]);
}
}
let list = List::new(items)
.block(block)
.highlight_style(Style::default().add_modifier(Modifier::BOLD));
/// Build a compact list of session counters for the placeholder view.
fn build_session_lines(state: &crate::app::state::rest::AppStateRest) -> Vec<Line<'static>> {
let mut lines: Vec<Line<'static>> = Vec::new();
frame.render_widget(list, area);
lines.push(Line::from(Span::styled(
" No workflow running.",
Style::default().fg(Theme::DIM),
)));
lines.push(Line::from(Span::raw("")));
if let Some(ref rt) = state.session_runtime {
let tool_count = rt.tool_call_results.len();
let pending = rt.pending_tool_queue.len();
let bash_count = rt.bash_jobs.len();
let msg_count = rt.messages.len();
lines.push(Line::from(vec![
Span::styled(" Messages ", Style::default().fg(Theme::DIM)),
Span::styled(msg_count.to_string(), Style::default().fg(Theme::INFO).add_modifier(Modifier::BOLD)),
]));
lines.push(Line::from(vec![
Span::styled(" Tool calls", Style::default().fg(Theme::DIM)),
Span::styled(format!(" {}", tool_count), Style::default().fg(Theme::SUCCESS)),
]));
if pending > 0 {
lines.push(Line::from(vec![
Span::styled(" Pending ", Style::default().fg(Theme::DIM)),
Span::styled(format!(" {}", pending), Style::default().fg(Theme::WARNING)),
]));
}
if bash_count > 0 {
lines.push(Line::from(vec![
Span::styled(" Bash jobs ", Style::default().fg(Theme::DIM)),
Span::styled(format!(" {}", bash_count), Style::default().fg(Theme::WARNING)),
]));
}
} else {
lines.push(Line::from(Span::styled(
" (no active session)",
Style::default().fg(Theme::DIM),
)));
}
lines.push(Line::from(Span::raw("")));
lines.push(Line::from(Span::styled(
" Run a workflow to see agents here.",
Style::default().fg(Theme::DIM).add_modifier(Modifier::ITALIC),
)));
lines
}