Class: Ask::Terminal::Repl

Inherits:
Object
  • Object
show all
Defined in:
lib/ask/terminal/repl.rb

Overview

The interactive terminal coding agent: a thin client of the session protocol. Owns one session with the host and drives it entirely through canonical requests and events:

prompt → session/send → stream events → turn ends → prompt again

Human-in-the-loop events are handled inline: approvals ask y/n, plan proposals ask approve/reject, and both resolve through the canonical interaction/plan methods — the same requests any other client (web console, bot) would make.

Instance Method Summary collapse

Constructor Details

#initialize(client:, input: $stdin, output: $stdout, workspace: Dir.pwd, model: nil, approval: "on_request") ⇒ Repl

Returns a new instance of Repl.

Parameters:

  • client (Client) —

    protocol client (spawned host or socket)

  • input (IO) (defaults to: $stdin) —

    user input (readline when a tty, else gets)

  • output (IO) (defaults to: $stdout) —

    rendered output

  • workspace (String) (defaults to: Dir.pwd) —

    workspace path for session/create

  • model (String, nil) (defaults to: nil) —

    model override

  • approval (String) (defaults to: "on_request") —

    approval mode: on_request | off | auto



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ask/terminal/repl.rb', line 22

def initialize(client:, input: $stdin, output: $stdout, workspace: Dir.pwd,
               model: nil, approval: "on_request")
  @client = client
  @input = input
  @output = output
  @workspace = workspace
  @model = model
  @approval = approval
  @session_id = nil
  @turn_active = false
end

Instance Method Details

#run ⇒ Object

Connect to the host, create a session, and run the interactive loop. Returns when the user exits (/exit or EOF).



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ask/terminal/repl.rb', line 36

def run
  handshake = @client.initialize!
  @server_info = handshake["server"] || {}

  created = @client.create_session(
    workspace_path: @workspace, mode: @approval, model: @model
  )
  @session_id = created.dig("session", "sessionId")
  @client.subscribe(@session_id, after_seq: 0)

  banner
  main_loop
rescue Client::ConnectionClosed
  @output.puts "\n#{Renderer::RED}host closed the connection#{Renderer::RESET}"
end