Class: Ask::Rails::ChatController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/ask/rails/chat_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /ask/sessions — create a new session



21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/ask/rails/chat_controller.rb', line 21

def create
  session_id = SecureRandom.uuid

  agent = build_agent_session
  agent.run("Hello") # warm up
  agent.save

  # Store minimal session metadata
  (session_id, agent)

  redirect_to ask_rails.root_path
end

#destroyObject

DELETE /ask/sessions — destroy all sessions



102
103
104
105
106
107
108
# File 'app/controllers/ask/rails/chat_controller.rb', line 102

def destroy
  persistence = Ask::Rails.configuration.persistence_adapter
  if persistence
    persistence.list.each { |id| persistence.delete(id) }
  end
  redirect_to ask_rails.root_path
end

#historyObject

GET /ask/sessions/:session_id/messages — get message history as JSON



96
97
98
99
# File 'app/controllers/ask/rails/chat_controller.rb', line 96

def history
  messages = load_session_messages(@session_id)
  render json: messages
end

#indexObject

GET /ask — main chat interface



14
15
16
17
18
# File 'app/controllers/ask/rails/chat_controller.rb', line 14

def index
  @sessions = load_sessions
  @active_session = @sessions.first
  render :index
end

#index_sessionsObject

GET /ask/sessions — list all sessions



41
42
43
44
# File 'app/controllers/ask/rails/chat_controller.rb', line 41

def index_sessions
  @sessions = load_sessions
  render json: @sessions
end

#messageObject

POST /ask/sessions/:session_id/messages — send a message, get SSE stream



47
48
49
50
51
52
53
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 'app/controllers/ask/rails/chat_controller.rb', line 47

def message
  prompt = params[:message].to_s.strip
  return head :bad_request if prompt.empty?

  response.headers["Content-Type"] = "text/event-stream"
  response.headers["Cache-Control"] = "no-cache"
  response.headers["X-Accel-Buffering"] = "no"

  # Disable buffering for streaming
  response.headers["Last-Modified"] = Time.now.httpdate

  self.response_body = Enumerator.new do |yielder|
    agent = resume_or_create_session(@session_id)

    yielder << "data: #{JSON.generate(type: 'start', session_id: @session_id)}\n\n"

    agent.run(prompt) do |chunk|
      if chunk.content&.length&.> 0
        yielder << "data: #{JSON.generate(type: 'delta', content: chunk.content)}\n\n"
      end
      if chunk.thinking&.length&.> 0
        yielder << "data: #{JSON.generate(type: 'thinking', content: chunk.thinking)}\n\n"
      end
    end

    agent.save
    @session = agent

    yielder << "data: #{JSON.generate(type: 'done', session_id: @session_id)}\n\n"
  end
end

#showObject

GET /ask/sessions/:id — show a specific session



35
36
37
38
# File 'app/controllers/ask/rails/chat_controller.rb', line 35

def show
  @messages = load_session_messages(@session_id)
  render json: { id: @session_id, messages: @messages }
end

#streamObject

GET /ask/sessions/:session_id/stream — SSE stream for an existing session



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/ask/rails/chat_controller.rb', line 80

def stream
  response.headers["Content-Type"] = "text/event-stream"
  response.headers["Cache-Control"] = "no-cache"
  response.headers["X-Accel-Buffering"] = "no"

  self.response_body = Enumerator.new do |yielder|
    messages = load_session_messages(@session_id)
    yielder << "data: #{JSON.generate(type: 'history', messages: messages)}\n\n"

    # Keep connection open briefly for potential updates
    sleep 30
    yielder << "data: #{JSON.generate(type: 'keepalive')}\n\n"
  end
end