Class: Binocs::TUI::RequestDetail

Inherits:
Window
  • Object
show all
Defined in:
lib/binocs/tui/request_detail.rb

Constant Summary collapse

TABS =
%w[Overview Params Headers Body Response Logs Exception Swagger Agent].freeze

Instance Attribute Summary collapse

Attributes inherited from Window

#height, #left, #top, #width, #win

Instance Method Summary collapse

Methods inherited from Window

#clear, #close, #draw_box, #noutrefresh, #refresh, #resize, #write, #write_centered

Constructor Details

#initialize(height:, width:, top:, left:) ⇒ RequestDetail

Returns a new instance of RequestDetail.



13
14
15
16
17
18
19
20
21
# File 'lib/binocs/tui/request_detail.rb', line 13

def initialize(height:, width:, top:, left:)
  super
  @request = nil
  @current_tab = 0
  @scroll_offset = 0
  @content_lines = []
  @swagger_operation = nil
  reset_agent_state
end

Instance Attribute Details

#agent_inputObject

Returns the value of attribute agent_input.



8
9
10
# File 'lib/binocs/tui/request_detail.rb', line 8

def agent_input
  @agent_input
end

#agent_input_activeObject

Returns the value of attribute agent_input_active.



8
9
10
# File 'lib/binocs/tui/request_detail.rb', line 8

def agent_input_active
  @agent_input_active
end

#agent_input_cursorObject

Returns the value of attribute agent_input_cursor.



8
9
10
# File 'lib/binocs/tui/request_detail.rb', line 8

def agent_input_cursor
  @agent_input_cursor
end

#agent_toolObject

Returns the value of attribute agent_tool.



8
9
10
# File 'lib/binocs/tui/request_detail.rb', line 8

def agent_tool
  @agent_tool
end

#agent_use_worktreeObject

Returns the value of attribute agent_use_worktree.



8
9
10
# File 'lib/binocs/tui/request_detail.rb', line 8

def agent_use_worktree
  @agent_use_worktree
end

#agent_worktree_input_activeObject

Returns the value of attribute agent_worktree_input_active.



8
9
10
# File 'lib/binocs/tui/request_detail.rb', line 8

def agent_worktree_input_active
  @agent_worktree_input_active
end

#agent_worktree_nameObject

Returns the value of attribute agent_worktree_name.



8
9
10
# File 'lib/binocs/tui/request_detail.rb', line 8

def agent_worktree_name
  @agent_worktree_name
end

#agent_worktree_name_cursorObject

Returns the value of attribute agent_worktree_name_cursor.



8
9
10
# File 'lib/binocs/tui/request_detail.rb', line 8

def agent_worktree_name_cursor
  @agent_worktree_name_cursor
end

#current_tabObject

Returns the value of attribute current_tab.



8
9
10
# File 'lib/binocs/tui/request_detail.rb', line 8

def current_tab
  @current_tab
end

#requestObject

Returns the value of attribute request.



8
9
10
# File 'lib/binocs/tui/request_detail.rb', line 8

def request
  @request
end

#scroll_offsetObject

Returns the value of attribute scroll_offset.



8
9
10
# File 'lib/binocs/tui/request_detail.rb', line 8

def scroll_offset
  @scroll_offset
end

#swagger_operationObject

Returns the value of attribute swagger_operation.



8
9
10
# File 'lib/binocs/tui/request_detail.rb', line 8

def swagger_operation
  @swagger_operation
end

Instance Method Details

#agent_tab?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/binocs/tui/request_detail.rb', line 190

def agent_tab?
  TABS[@current_tab] == 'Agent'
end

#agent_tool_labelObject



199
200
201
202
203
204
205
# File 'lib/binocs/tui/request_detail.rb', line 199

def agent_tool_label
  case @agent_tool
  when :claude_code then 'Claude Code'
  when :opencode then 'OpenCode'
  else @agent_tool.to_s
  end
end

#build_agentObject



602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# File 'lib/binocs/tui/request_detail.rb', line 602

def build_agent
  agent = current_agent
  agents_for_request = Binocs::Agent.for_request(@request.id)

  # Settings section
  add_section('Settings')
  add_field('Tool', agent_tool_label)
  if @agent_use_worktree && @agent_worktree_name.present?
    add_field('Mode', "Worktree: agent/#{@agent_worktree_name}")
  else
    add_field('Mode', 'Current Branch')
  end
  add_blank
  add_line("Press 't' to change tool, 'w' to #{@agent_use_worktree ? 'disable' : 'enable'} worktree mode", Colors::KEY_HINT)
  add_blank

  if agent
    # Status section
    add_section('Agent Status')
    status_color = case agent.status
                   when :running then Colors::STATUS_SUCCESS
                   when :completed then Colors::HEADER
                   when :failed, :stopped then Colors::ERROR
                   else Colors::MUTED
                   end
    add_field('Status', agent.status.to_s.upcase, status_color)
    add_field('Tool', agent.tool_command)
    add_field('Duration', agent.duration)
    add_field('Branch', agent.branch_name) if agent.branch_name
    add_field('Worktree', agent.worktree_path) if agent.worktree_path
    add_blank

    if agent.running?
      add_line("Press 's' to stop the agent", Colors::KEY_HINT)
      add_blank
    end

    # Show agent prompt
    if agent.prompt.present?
      add_section('Prompt')
      agent.prompt.each_line do |line|
        add_line(line.chomp, Colors::NORMAL)
      end
      add_blank
    end

    # Show recent output
    add_section('Output (last 30 lines)')
    output = agent.output_tail(30)
    if output.present?
      output.each_line do |line|
        add_line(line.chomp, Colors::MUTED)
      end
    else
      add_line('No output yet...', Colors::MUTED)
    end
  else
    # No agent yet - show context preview
    add_section('Context (will be sent to agent)')
    method_str = @request.respond_to?(:read_attribute) ? @request.read_attribute(:method) : @request.method
    add_line("#{method_str} #{@request.path} -> #{@request.status_code}", Colors::NORMAL)
    if @request.controller_name
      add_line("#{@request.controller_name}##{@request.action_name}", Colors::MUTED)
    end
    if @request.has_exception?
      add_line("Exception: #{@request.exception&.dig('class')}", Colors::ERROR)
    end
    add_blank
    add_line("Press 'i' or Enter to compose a prompt for the AI agent", Colors::KEY_HINT)
  end

  # Show history of agents if more than current
  if agents_for_request.length > 1
    add_blank
    add_section("Agent History (#{agents_for_request.length} total)")
    agents_for_request.each do |a|
      status_indicator = case a.status
                         when :running then ''
                         when :completed then ''
                         when :failed then ''
                         else ''
                         end
      add_line("#{status_indicator} #{a.short_prompt(40)} (#{a.duration})", Colors::MUTED)
    end
  end
end

#build_bodyObject



454
455
456
457
458
459
460
461
462
# File 'lib/binocs/tui/request_detail.rb', line 454

def build_body
  body = @request.request_body
  if body.present?
    add_section('Request Body')
    format_body(body)
  else
    add_line('No request body', Colors::MUTED)
  end
end

#build_contentObject

Agent tab methods - need to be public for app.rb to call



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/binocs/tui/request_detail.rb', line 173

def build_content
  @content_lines = []
  return unless @request

  case TABS[@current_tab]
  when 'Overview' then build_overview
  when 'Params' then build_params
  when 'Headers' then build_headers
  when 'Body' then build_body
  when 'Response' then build_response
  when 'Logs' then build_logs
  when 'Exception' then build_exception
  when 'Swagger' then build_swagger
  when 'Agent' then build_agent
  end
end

#build_exceptionObject



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/binocs/tui/request_detail.rb', line 504

def build_exception
  exc = @request.exception
  if exc.present?
    add_section('Exception Details')
    add_field('Class', exc['class'], Colors::ERROR)
    add_blank
    add_field('Message', exc['message'], Colors::ERROR)
    add_blank

    if exc['backtrace'].present?
      add_section('Backtrace')
      exc['backtrace'].each do |line|
        add_line(line, Colors::MUTED)
      end
    end
  else
    add_line('No exception', Colors::STATUS_SUCCESS)
  end
end

#build_headersObject



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/binocs/tui/request_detail.rb', line 435

def build_headers
  add_section('Request Headers')
  req_headers = @request.request_headers
  if req_headers.present? && req_headers.any?
    format_hash(req_headers)
  else
    add_line('No request headers', Colors::MUTED)
  end

  add_blank
  add_section('Response Headers')
  res_headers = @request.response_headers
  if res_headers.present? && res_headers.any?
    format_hash(res_headers)
  else
    add_line('No response headers', Colors::MUTED)
  end
end

#build_logsObject



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/binocs/tui/request_detail.rb', line 474

def build_logs
  logs = @request.logs
  if logs.present? && logs.any?
    logs.each_with_index do |log, i|
      add_section("Log Entry #{i + 1} - #{log['type']&.upcase}")
      add_field('Timestamp', log['timestamp'])

      case log['type']
      when 'controller'
        add_field('Controller', "#{log['controller']}##{log['action']}")
        add_field('Format', log['format'])
        add_field('View Runtime', "#{log['view_runtime']}ms") if log['view_runtime']
        add_field('DB Runtime', "#{log['db_runtime']}ms") if log['db_runtime']
        add_field('Duration', "#{log['duration']}ms")
      when 'redirect'
        add_field('Location', log['location'])
        add_field('Status', log['status'])
      else
        log.each do |key, value|
          next if key == 'timestamp' || key == 'type'
          add_field(key.to_s.titleize, value.to_s)
        end
      end
      add_blank
    end
  else
    add_line('No logs captured', Colors::MUTED)
  end
end

#build_overviewObject



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/binocs/tui/request_detail.rb', line 396

def build_overview
  add_section('Request Information')
  add_field('Method', @request.method)
  add_field('Path', @request.path)
  add_field('Full URL', @request.full_url)
  add_field('Controller', @request.controller_name || 'N/A')
  add_field('Action', @request.action_name || 'N/A')
  add_blank

  add_section('Response')
  add_field('Status', @request.status_code.to_s)
  add_field('Duration', @request.formatted_duration)
  add_field('Memory Delta', @request.formatted_memory_delta)
  add_blank

  add_section('Client')
  add_field('IP Address', @request.ip_address || 'N/A')
  add_field('Session ID', @request.session_id || 'N/A')
  add_blank

  add_section('Timing')
  add_field('Created At', @request.created_at&.strftime('%Y-%m-%d %H:%M:%S.%L'))

  if @request.has_exception?
    add_blank
    add_line('!! HAS EXCEPTION - See Exception tab', Colors::ERROR)
  end
end

#build_paramsObject



425
426
427
428
429
430
431
432
433
# File 'lib/binocs/tui/request_detail.rb', line 425

def build_params
  params = @request.params
  if params.present? && params.any?
    add_section('Request Parameters')
    format_hash(params)
  else
    add_line('No parameters', Colors::MUTED)
  end
end

#build_responseObject



464
465
466
467
468
469
470
471
472
# File 'lib/binocs/tui/request_detail.rb', line 464

def build_response
  body = @request.response_body
  if body.present?
    add_section('Response Body')
    format_body(body)
  else
    add_line('No response body captured', Colors::MUTED)
  end
end

#build_swaggerObject



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# File 'lib/binocs/tui/request_detail.rb', line 524

def build_swagger
  unless @swagger_operation
    add_line('No matching Swagger operation found', Colors::MUTED)
    add_blank
    add_line("Request: #{@request.method} #{@request.path}", Colors::MUTED)
    add_blank
    add_line('Ensure swagger_spec_url is configured correctly.', Colors::MUTED)
    return
  end

  op = @swagger_operation

  add_section('Operation')
  add_field('Operation ID', op[:operation_id] || 'N/A')
  add_field('Spec Path', op[:spec_path])
  add_field('Method', op[:method].upcase)
  add_field('Tags', op[:tags].join(', ')) if op[:tags].any?
  add_field('Deprecated', 'Yes', Colors::ERROR) if op[:deprecated]
  add_blank

  if op[:summary].present?
    add_section('Summary')
    add_line(op[:summary], Colors::NORMAL)
    add_blank
  end

  if op[:description].present?
    add_section('Description')
    op[:description].each_line do |line|
      add_line(line.chomp, Colors::NORMAL)
    end
    add_blank
  end

  if op[:parameters].any?
    add_section('Parameters')
    op[:parameters].each do |param|
      location = param['in']
      name = param['name']
      required = param['required'] ? '*' : ''
      param_type = param.dig('schema', 'type') || 'any'
      add_field("#{location}:#{name}#{required}", param_type)
      if param['description'].present?
        add_line("  #{param['description']}", Colors::MUTED)
      end
    end
    add_blank
  end

  if op[:request_body].present?
    add_section('Request Body')
    content = op[:request_body]['content']
    if content
      content.each do |media_type, schema_info|
        add_field('Content-Type', media_type)
        if schema_info['schema']
          format_schema(schema_info['schema'], 1)
        end
      end
    end
    add_blank
  end

  if op[:responses].any?
    add_section('Responses')
    op[:responses].each do |status, response_info|
      description = response_info['description'] || ''
      color = status.to_s.start_with?('2') ? Colors::STATUS_SUCCESS :
              status.to_s.start_with?('4') ? Colors::STATUS_CLIENT_ERROR :
              status.to_s.start_with?('5') ? Colors::STATUS_SERVER_ERROR : Colors::NORMAL
      add_field(status.to_s, description, color)
    end
    add_blank
  end

  add_line("Press 'o' to open in browser", Colors::KEY_HINT)
end

#cancel_worktree_inputObject



239
240
241
242
243
# File 'lib/binocs/tui/request_detail.rb', line 239

def cancel_worktree_input
  @agent_use_worktree = false
  @agent_worktree_name = ''
  @agent_worktree_input_active = false
end

#confirm_worktree_nameObject



229
230
231
232
233
234
235
236
237
# File 'lib/binocs/tui/request_detail.rb', line 229

def confirm_worktree_name
  if @agent_worktree_name.strip.empty?
    # Cancelled - go back to current branch mode
    @agent_use_worktree = false
  else
    @agent_use_worktree = true
  end
  @agent_worktree_input_active = false
end

#content_as_textObject



689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
# File 'lib/binocs/tui/request_detail.rb', line 689

def content_as_text
  # Convert content_lines to plain text for copying
  lines = []
  @content_lines.each do |line|
    case line[:type]
    when :section
      lines << ""
      lines << "── #{line[:text]} ──"
    when :field
      lines << "#{line[:label]}: #{line[:value]}"
    when :line
      lines << line[:text]
    when :blank
      lines << ""
    end
  end
  lines.join("\n")
end

#copy_to_clipboard(text) ⇒ Object



708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
# File 'lib/binocs/tui/request_detail.rb', line 708

def copy_to_clipboard(text)
  # Try different clipboard commands based on platform
  if RbConfig::CONFIG['host_os'] =~ /darwin/
    IO.popen('pbcopy', 'w') { |io| io.write(text) }
    true
  elsif system('which xclip > /dev/null 2>&1')
    IO.popen('xclip -selection clipboard', 'w') { |io| io.write(text) }
    true
  elsif system('which xsel > /dev/null 2>&1')
    IO.popen('xsel --clipboard --input', 'w') { |io| io.write(text) }
    true
  else
    false
  end
rescue
  false
end

#current_agentObject



194
195
196
197
# File 'lib/binocs/tui/request_detail.rb', line 194

def current_agent
  return nil unless @request
  Binocs::Agent.for_request(@request.id).first
end

#cycle_agent_toolObject



207
208
209
210
211
# File 'lib/binocs/tui/request_detail.rb', line 207

def cycle_agent_tool
  tools = [:claude_code, :opencode]
  current_index = tools.find_index(@agent_tool) || 0
  @agent_tool = tools[(current_index + 1) % tools.length]
end

#drawObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/binocs/tui/request_detail.rb', line 80

def draw
  return unless @request

  clear
  draw_box("Request Detail")
  draw_header
  draw_tabs
  draw_content
  draw_agent_input if agent_tab?
  draw_footer
  draw_agent_cursor if agent_tab? && @agent_input_active
  refresh
end

#go_to_tab(index) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/binocs/tui/request_detail.rb', line 54

def go_to_tab(index)
  return if index < 0 || index >= TABS.length

  @current_tab = index
  @scroll_offset = 0
  build_content
end

#handle_agent_key(key) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/binocs/tui/request_detail.rb', line 245

def handle_agent_key(key)
  return false unless agent_tab?

  # If worktree name input is active
  if @agent_worktree_input_active
    case key
    when 27 # Esc - cancel
      cancel_worktree_input
      return true
    when Curses::KEY_ENTER, 10, 13
      confirm_worktree_name
      return true
    when Curses::KEY_BACKSPACE, 127, 8
      if @agent_worktree_name_cursor > 0
        @agent_worktree_name = @agent_worktree_name[0, @agent_worktree_name_cursor - 1] + @agent_worktree_name[@agent_worktree_name_cursor..]
        @agent_worktree_name_cursor -= 1
      end
      return true
    when Curses::KEY_LEFT
      @agent_worktree_name_cursor = [@agent_worktree_name_cursor - 1, 0].max
      return true
    when Curses::KEY_RIGHT
      @agent_worktree_name_cursor = [@agent_worktree_name_cursor + 1, @agent_worktree_name.length].min
      return true
    when String
      # Only allow valid branch name characters
      if key.length == 1 && key =~ /[a-zA-Z0-9\-_]/
        @agent_worktree_name = @agent_worktree_name[0, @agent_worktree_name_cursor] + key + (@agent_worktree_name[@agent_worktree_name_cursor..] || '')
        @agent_worktree_name_cursor += 1
      end
      return true
    when Integer
      if key >= 32 && key < 127
        char = key.chr
        if char =~ /[a-zA-Z0-9\-_]/
          @agent_worktree_name = @agent_worktree_name[0, @agent_worktree_name_cursor] + char + (@agent_worktree_name[@agent_worktree_name_cursor..] || '')
          @agent_worktree_name_cursor += 1
        end
      end
      return true
    end
    return true
  end

  # If input is active, handle text input first
  if @agent_input_active
    case key
    when 27 # Esc - cancel input
      @agent_input_active = false
      return true
    when Curses::KEY_ENTER, 10, 13
      # Enter submits if we have input
      if @agent_input.strip.length > 0
        submit_agent_prompt
      end
      return true
    when Curses::KEY_BACKSPACE, 127, 8
      if @agent_input_cursor > 0
        @agent_input = @agent_input[0, @agent_input_cursor - 1] + @agent_input[@agent_input_cursor..]
        @agent_input_cursor -= 1
      end
      return true
    when Curses::KEY_LEFT
      @agent_input_cursor = [@agent_input_cursor - 1, 0].max
      return true
    when Curses::KEY_RIGHT
      @agent_input_cursor = [@agent_input_cursor + 1, @agent_input.length].min
      return true
    when Curses::KEY_HOME
      @agent_input_cursor = 0
      return true
    when Curses::KEY_END
      @agent_input_cursor = @agent_input.length
      return true
    when String
      if key.length == 1 && key.ord >= 32
        @agent_input = @agent_input[0, @agent_input_cursor] + key + (@agent_input[@agent_input_cursor..] || '')
        @agent_input_cursor += 1
      end
      return true
    when Integer
      if key >= 32 && key < 127
        char = key.chr
        @agent_input = @agent_input[0, @agent_input_cursor] + char + (@agent_input[@agent_input_cursor..] || '')
        @agent_input_cursor += 1
      end
      return true
    end
    return true # Consume all keys when input is active
  end

  # Not in input mode - handle commands
  case key
  when 'i', Curses::KEY_ENTER, 10, 13
    @agent_input_active = true
    return true
  when 't', 'T'
    cycle_agent_tool
    return true
  when 'w', 'W'
    toggle_worktree_mode
    return true
  when 's', 'S'
    stop_current_agent
    return true
  end

  false
end

#next_tabObject



42
43
44
45
46
# File 'lib/binocs/tui/request_detail.rb', line 42

def next_tab
  @current_tab = (@current_tab + 1) % TABS.length
  @scroll_offset = 0
  build_content
end

#page_downObject



75
76
77
78
# File 'lib/binocs/tui/request_detail.rb', line 75

def page_down
  max_scroll = [@content_lines.length - content_height, 0].max
  @scroll_offset = [@scroll_offset + content_height, max_scroll].min
end

#page_upObject



71
72
73
# File 'lib/binocs/tui/request_detail.rb', line 71

def page_up
  @scroll_offset = [@scroll_offset - content_height, 0].max
end

#prev_tabObject



48
49
50
51
52
# File 'lib/binocs/tui/request_detail.rb', line 48

def prev_tab
  @current_tab = (@current_tab - 1) % TABS.length
  @scroll_offset = 0
  build_content
end

#reset_agent_stateObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/binocs/tui/request_detail.rb', line 23

def reset_agent_state
  @agent_input = ''
  @agent_input_cursor = 0
  @agent_input_active = false
  @agent_use_worktree = false
  @agent_worktree_name = ''
  @agent_worktree_name_cursor = 0
  @agent_worktree_input_active = false
  @agent_tool = Binocs.configuration.agent_tool
end

#scroll_downObject



66
67
68
69
# File 'lib/binocs/tui/request_detail.rb', line 66

def scroll_down
  max_scroll = [@content_lines.length - content_height, 0].max
  @scroll_offset = [@scroll_offset + 1, max_scroll].min
end

#scroll_upObject



62
63
64
# File 'lib/binocs/tui/request_detail.rb', line 62

def scroll_up
  @scroll_offset = [@scroll_offset - 1, 0].max
end

#set_request(request, reset_tab: true) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/binocs/tui/request_detail.rb', line 34

def set_request(request, reset_tab: true)
  @request = request
  @current_tab = 0 if reset_tab
  @scroll_offset = 0
  @swagger_operation = Binocs::Swagger::PathMatcher.find_operation(request) if request
  build_content
end

#stop_current_agentObject



388
389
390
391
392
393
394
# File 'lib/binocs/tui/request_detail.rb', line 388

def stop_current_agent
  agent = current_agent
  return unless agent&.running?

  agent.stop!
  build_content
end

#submit_agent_promptObject



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/binocs/tui/request_detail.rb', line 355

def submit_agent_prompt
  return if @agent_input.strip.empty?
  return unless @request

  prompt = @agent_input.strip
  @agent_input = ''
  @agent_input_cursor = 0
  @agent_input_active = false

  # Check if there's an existing agent for this request
  existing_agent = current_agent

  if existing_agent && !existing_agent.running?
    # Continue in same directory with new prompt
    Binocs::AgentManager.continue_session(
      agent: existing_agent,
      prompt: prompt,
      tool: @agent_tool
    )
  else
    # Launch new agent
    Binocs::AgentManager.launch(
      request: @request,
      prompt: prompt,
      tool: @agent_tool,
      use_worktree: @agent_use_worktree,
      branch_name: @agent_use_worktree ? @agent_worktree_name : nil
    )
  end

  build_content
end

#toggle_worktree_modeObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/binocs/tui/request_detail.rb', line 213

def toggle_worktree_mode
  if @agent_use_worktree
    # Turning off worktree mode
    @agent_use_worktree = false
    @agent_worktree_name = ''
    @agent_worktree_input_active = false
  else
    # Turning on worktree mode - prompt for name
    timestamp = Time.now.strftime('%m%d-%H%M')
    @agent_worktree_name = "#{timestamp}-fix"
    @agent_worktree_name_cursor = @agent_worktree_name.length
    @agent_worktree_input_active = true
    @agent_input_active = false
  end
end