Class: RubynCode::IDE::Handlers::PromptHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyn_code/ide/handlers/prompt_handler.rb

Overview

Handles the “prompt” JSON-RPC request — the main chat entry point.

Returns immediately with { “accepted” => true } and spawns a background thread that runs the agent loop. As the agent works, it emits stream/text, tool/use, tool/result, and agent/status notifications over the JSON-RPC transport.

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ PromptHandler

Returns a new instance of PromptHandler.



15
16
17
18
19
20
# File 'lib/rubyn_code/ide/handlers/prompt_handler.rb', line 15

def initialize(server)
  @server = server
  @sessions = {}       # sessionId => Thread
  @conversations = {}  # sessionId => Agent::Conversation (persists across prompts)
  @started_sessions = Set.new # tracks which sessions have fired session_start
end

Instance Method Details

#call(params) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rubyn_code/ide/handlers/prompt_handler.rb', line 38

def call(params)
  text       = params['text'] || ''
  context    = params['context'] || {}
  session_id = params['sessionId'] || SecureRandom.uuid

  # Cancel any existing agent thread for this session
  cancel_session(session_id)

  # Spawn the agent loop in a background thread
  @sessions[session_id] = Thread.new do
    run_agent(session_id, text, context)
  end

  { 'accepted' => true, 'sessionId' => session_id }
end

#cancel_session(session_id) ⇒ Object

Called by CancelHandler to stop a running session.



55
56
57
58
59
60
61
# File 'lib/rubyn_code/ide/handlers/prompt_handler.rb', line 55

def cancel_session(session_id)
  thread = @sessions.delete(session_id)
  return unless thread&.alive?

  thread.raise(Interrupt)
  thread.join(2) # give it a moment to clean up
end

#inject_conversation(session_id, conversation) ⇒ Object

Called by SessionResumeHandler to inject a restored conversation into the cache so the next prompt continues from the loaded history.



24
25
26
27
# File 'lib/rubyn_code/ide/handlers/prompt_handler.rb', line 24

def inject_conversation(session_id, conversation)
  cancel_session(session_id)
  @conversations[session_id] = conversation
end

#reset_session(session_id) ⇒ Object

Called by SessionResetHandler when the user clicks “New Session” in the chat UI. Drops the cached conversation for this session so the next prompt starts fresh — parity with the CLI’s ‘/new`.



32
33
34
35
36
# File 'lib/rubyn_code/ide/handlers/prompt_handler.rb', line 32

def reset_session(session_id)
  cancel_session(session_id)
  @conversations.delete(session_id)
  @started_sessions.delete(session_id)
end