Class: Aidp::Harness::UI::EnhancedTUI

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/harness/ui/enhanced_tui.rb

Overview

Enhanced TUI system using TTY libraries, inspired by Claude Code and modern LLM agents

Defined Under Namespace

Classes: DisplayError, InputError, TUIError

Instance Method Summary collapse

Constructor Details

#initializeEnhancedTUI

Returns a new instance of EnhancedTUI.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/aidp/harness/ui/enhanced_tui.rb', line 21

def initialize
  @cursor = TTY::Cursor
  @screen = TTY::Screen
  @pastel = Pastel.new
  @prompt = TTY::Prompt.new
  # Headless (non-interactive) detection for test/CI environments:
  # - RSpec defined or RSPEC_RUNNING env set
  # - STDIN not a TTY (captured by PTY/tmux harness)
  @headless = !!(defined?(RSpec) || ENV["RSPEC_RUNNING"] || $stdin.nil? || !$stdin.tty?)
  @current_mode = nil
  @workflow_active = false
  @current_step = nil

  @jobs = {}
  @jobs_visible = false

  setup_signal_handlers
end

Instance Method Details

#add_job(job_id, job_data) ⇒ Object

Job monitoring methods



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/aidp/harness/ui/enhanced_tui.rb', line 51

def add_job(job_id, job_data)
  @jobs[job_id] = {
    id: job_id,
    name: job_data[:name] || job_id,
    status: job_data[:status] || :pending,
    progress: job_data[:progress] || 0,
    started_at: Time.now,
    message: job_data[:message] || "",
    provider: job_data[:provider] || "unknown"
  }
  @jobs_visible = true
end

#announce_mode(mode) ⇒ Object

Called by CLI after mode selection in interactive flow (added helper)



112
113
114
115
116
117
118
119
# File 'lib/aidp/harness/ui/enhanced_tui.rb', line 112

def announce_mode(mode)
  @current_mode = mode
  if @headless
    header = (mode == :analyze) ? "Analyze Mode" : "Execute Mode"
    @prompt.say(header)
    @prompt.say("Select workflow")
  end
end

#get_confirmation(message, default: true) ⇒ Object



81
82
83
# File 'lib/aidp/harness/ui/enhanced_tui.rb', line 81

def get_confirmation(message, default: true)
  @prompt.yes?(message)
end

#get_user_input(prompt = "💬 You: ") ⇒ Object

Input methods using TTY::Prompt only - no background threads



77
78
79
# File 'lib/aidp/harness/ui/enhanced_tui.rb', line 77

def get_user_input(prompt = "💬 You: ")
  @prompt.ask(prompt)
end

#multiselect(title, items, selected: []) ⇒ Object

Multiselect interface using TTY::Prompt



91
92
93
# File 'lib/aidp/harness/ui/enhanced_tui.rb', line 91

def multiselect(title, items, selected: [])
  @prompt.multi_select(title, items, default: selected)
end

#remove_job(job_id) ⇒ Object



71
72
73
74
# File 'lib/aidp/harness/ui/enhanced_tui.rb', line 71

def remove_job(job_id)
  @jobs.delete(job_id)
  @jobs_visible = @jobs.any?
end

#show_message(message, type = :info) ⇒ Object

Display methods using TTY::Prompt



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/aidp/harness/ui/enhanced_tui.rb', line 96

def show_message(message, type = :info)
  case type
  when :info
    @prompt.say("#{message}", color: :blue)
  when :success
    @prompt.say("#{message}", color: :green)
  when :warning
    @prompt.say("#{message}", color: :yellow)
  when :error
    @prompt.say("#{message}", color: :red)
  else
    @prompt.say(message)
  end
end

#show_step_execution(step_name, status, details = {}) ⇒ Object

Enhanced step execution display



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/aidp/harness/ui/enhanced_tui.rb', line 160

def show_step_execution(step_name, status, details = {})
  case status
  when :starting
    content = []
    content << @pastel.blue("Starting execution...")
    if details[:provider]
      content << @pastel.dim("Provider: #{details[:provider]}")
    end

    box = TTY::Box.frame(
      content.join("\n"),
      title: {top_left: "🚀 Executing Step: #{step_name}"},
      border: :thick,
      padding: [1, 2],
      style: {border: {fg: :blue}}
    )
    puts box

  when :running
    content = []
    content << @pastel.yellow("Step is running...")
    if details[:message]
      content << @pastel.dim(details[:message])
    end

    box = TTY::Box.frame(
      content.join("\n"),
      title: {top_left: "⏳ Running Step: #{step_name}"},
      border: :thick,
      padding: [1, 2],
      style: {border: {fg: :yellow}}
    )
    puts box

  when :completed
    content = []
    content << @pastel.green("Step completed successfully")
    if details[:duration]
      content << @pastel.dim("Duration: #{details[:duration].round(2)}s")
    end

    box = TTY::Box.frame(
      content.join("\n"),
      title: {top_left: "✅ Completed Step: #{step_name}"},
      border: :thick,
      padding: [1, 2],
      style: {border: {fg: :green}}
    )
    puts box

  when :failed
    content = []
    content << @pastel.red("Step failed")
    if details[:error]
      # Extract the most relevant error information
      error_msg = details[:error]

      # Look for key error patterns and extract them
      if error_msg.include?("ConnectError:")
        # Extract ConnectError and what comes after it
        connect_error_match = error_msg.match(/ConnectError: ([^\\n]+)/)
        if connect_error_match
          error_msg = "ConnectError: #{connect_error_match[1]}"
        end
      elsif error_msg.include?("exit status:")
        # Extract exit status and stderr using string operations to avoid ReDoS
        exit_status_match = error_msg.match(/exit status: (\d+)/)
        stderr_match = error_msg.match(/stderr: ([^\n\r]+)/)
        if exit_status_match && stderr_match
          error_msg = "Exit status: #{exit_status_match[1]}, Error: #{stderr_match[1]}"
        end
      elsif error_msg.length > 200
        # For other long errors, truncate but keep the beginning
        error_msg = error_msg[0..200] + "..."
      end

      # Wrap long lines
      wrapped_error = error_msg.gsub(/.{80}/, "\\&\n")
      content << @pastel.red("Error: #{wrapped_error}")
    end

    box = TTY::Box.frame(
      content.join("\n"),
      title: {top_left: "❌ Failed Step: #{step_name}"},
      border: :thick,
      padding: [1, 2],
      style: {border: {fg: :red}},
      width: 80
    )
    puts box
  end
end

#show_workflow_status(workflow_data) ⇒ Object

Enhanced workflow display



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/aidp/harness/ui/enhanced_tui.rb', line 133

def show_workflow_status(workflow_data)
  content = []
  content << "#{@pastel.bold("Type:")} #{workflow_data[:workflow_type]}"
  content << "#{@pastel.bold("Steps:")} #{workflow_data[:steps]&.length || 0} total"
  content << "#{@pastel.bold("Completed:")} #{workflow_data[:completed_steps] || 0}"
  content << "#{@pastel.bold("Current:")} #{workflow_data[:current_step] || "None"}"

  if workflow_data[:progress_percentage]
    progress_bar = TTY::ProgressBar.new(
      "#{@pastel.bold("Progress:")} [:bar] :percent%",
      total: 100,
      width: 30
    )
    progress_bar.current = workflow_data[:progress_percentage]
    content << progress_bar.render
  end

  box = TTY::Box.frame(
    content.join("\n"),
    title: {top_left: "📋 Workflow Status"},
    border: :thick,
    padding: [1, 2]
  )
  puts box
end

#simulate_step_execution(step_name) ⇒ Object

Simulate selecting a workflow step in test mode



122
123
124
125
126
127
128
129
130
# File 'lib/aidp/harness/ui/enhanced_tui.rb', line 122

def simulate_step_execution(step_name)
  return unless @headless
  @workflow_active = true
  @current_step = step_name
  questions = extract_questions_for_step(step_name)
  questions.each { |q| @prompt.say(q) }
  # Simulate quick completion
  @prompt.say("#{step_name.split("_").first} completed") if step_name.start_with?("00_PRD")
end

#single_select(title, items, default: 0) ⇒ Object

Single-select interface using TTY::Prompt



86
87
88
# File 'lib/aidp/harness/ui/enhanced_tui.rb', line 86

def single_select(title, items, default: 0)
  @prompt.select(title, items, default: default, cycle: true)
end

#start_display_loopObject

Simple display initialization - no background threads



41
42
43
# File 'lib/aidp/harness/ui/enhanced_tui.rb', line 41

def start_display_loop
  # Display loop is now just a no-op for compatibility
end

#stop_display_loopObject



45
46
47
48
# File 'lib/aidp/harness/ui/enhanced_tui.rb', line 45

def stop_display_loop
  # Simple cleanup - no background threads to stop
  restore_screen
end

#update_job(job_id, updates) ⇒ Object



64
65
66
67
68
69
# File 'lib/aidp/harness/ui/enhanced_tui.rb', line 64

def update_job(job_id, updates)
  return unless @jobs[job_id]

  @jobs[job_id].merge!(updates)
  @jobs[job_id][:updated_at] = Time.now
end