Class: Harnex::Adapters::Claude

Inherits:
Base
  • Object
show all
Defined in:
lib/harnex/adapters/claude.rb

Constant Summary collapse

SUBMIT_DELAY_MS =
75
SUBMIT_DELAY_PER_KB_MS =
50

Constants inherited from Base

Base::PROMPT_PREFIXES

Instance Attribute Summary

Attributes inherited from Base

#key

Instance Method Summary collapse

Methods inherited from Base

#build_command, #infer_repo_path, #send_wait_seconds, #wait_for_sendable, #wait_for_sendable_state?

Constructor Details

#initialize(extra_args = []) ⇒ Claude

Returns a new instance of Claude.



7
8
9
# File 'lib/harnex/adapters/claude.rb', line 7

def initialize(extra_args = [])
  super("claude", extra_args)
end

Instance Method Details

#base_commandObject



11
12
13
14
15
16
# File 'lib/harnex/adapters/claude.rb', line 11

def base_command
  [
    "claude",
    "--dangerously-skip-permissions"
  ]
end

#build_send_payload(text:, submit:, enter_only:, screen_text:, force: false) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/harnex/adapters/claude.rb', line 54

def build_send_payload(text:, submit:, enter_only:, screen_text:, force: false)
  state = input_state(screen_text)
  if !force && blocked_state?(state, enter_only: enter_only)
    raise ArgumentError, blocked_message(state, enter_only: enter_only)
  end

  steps = []
  unless enter_only
    body = text.to_s
    steps << { text: body, newline: false } unless body.empty?
  end

  if submit || enter_only
    step = { text: submit_bytes, newline: false }
    step[:delay_ms] = submit_delay_ms(text) if steps.any?
    steps << step
  end

  {
    steps: steps,
    input_state: state,
    force: force
  }
end

#inject_exit(writer) ⇒ Object



79
80
81
# File 'lib/harnex/adapters/claude.rb', line 79

def inject_exit(writer)
  super(writer, delay_ms: SUBMIT_DELAY_MS)
end

#input_state(screen_text) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/harnex/adapters/claude.rb', line 18

def input_state(screen_text)
  lines = recent_lines(screen_text, limit: 20)
  text = lines.join
  compact = text.gsub(/\s+/, "")

  if compact.include?("Quicksafetycheck:") && compact.include?("Yes,Itrustthisfolder")
    {
      state: "workspace-trust-prompt",
      input_ready: false,
      action: "press-enter-to-confirm"
    }
  elsif compact.include?("Entertoconfirm") && compact.include?("Esctocancel")
    {
      state: "confirmation",
      input_ready: false
    }
  elsif compact.include?("--INSERT--") || compact.include?("bypasspermissionson")
    {
      state: "prompt",
      input_ready: true
    }
  elsif compact.include?("NORMAL") || compact.include?("--NORMAL--")
    {
      state: "vim-normal",
      input_ready: true
    }
  elsif lines.any? { |line| prompt_line?(line) }
    {
      state: "prompt",
      input_ready: true
    }
  else
    super
  end
end