Class: ActiveAgent::ChatsController

Inherits:
ActionController::Base
  • Object
show all
Includes:
ActionController::Live
Defined in:
app/controllers/active_agent/chats_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



11
12
13
14
15
16
17
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
# File 'app/controllers/active_agent/chats_controller.rb', line 11

def create
  agent_name = params[:agent]
  conversation_id = params[:conversation_id]
  message = params[:message]

  if agent_name.blank? || conversation_id.blank? || message.blank?
    return render json: { error: "Missing required parameters: agent, conversation_id, message" }, status: :bad_request
  end

  agent_class = ActiveAgent.find_agent(agent_name)
  agent = agent_class.new(conversation_id: conversation_id)

  if request.headers["Accept"]&.include?("text/event-stream")
    response.headers["Content-Type"] = "text/event-stream"
    response.headers["Last-Modified"] = Time.now.httpdate
    response.headers["X-Accel-Buffering"] = "no"
    response.headers["Cache-Control"] = "no-cache, no-store"
    response.headers["Connection"] = "keep-alive"

    begin
      agent.chat(message) do |chunk|
        response.stream.write("data: #{ { chunk: chunk }.to_json }\n\n")
      end
    rescue StandardError => e
      ActiveAgent.logger.error("[ActiveAgent] Streaming controller error: #{e.message}\n#{e.backtrace.join("\n")}")
      response.stream.write("data: #{ { error: e.message }.to_json }\n\n")
    ensure
      response.stream.close
    end
  else
    begin
      reply = agent.chat(message)
      render json: { reply: reply }
    rescue StandardError => e
      ActiveAgent.logger.error("[ActiveAgent] Controller error: #{e.message}")
      render json: { error: e.message }, status: :unprocessable_entity
    end
  end
end

#destroyObject

Optional endpoint to clear history



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/active_agent/chats_controller.rb', line 69

def destroy
  agent_name = params[:agent]
  conversation_id = params[:conversation_id]

  if agent_name.blank? || conversation_id.blank?
    return render json: { error: "Missing agent or conversation_id" }, status: :bad_request
  end

  agent_class = ActiveAgent.find_agent(agent_name)
  agent = agent_class.new(conversation_id: conversation_id)
  agent.memory.clear

  render json: { success: true, message: "Conversation history cleared." }
end

#indexObject

Optional endpoint to fetch history



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/active_agent/chats_controller.rb', line 52

def index
  agent_name = params[:agent]
  conversation_id = params[:conversation_id]

  if agent_name.blank? || conversation_id.blank?
    return render json: { error: "Missing agent or conversation_id" }, status: :bad_request
  end

  agent_class = ActiveAgent.find_agent(agent_name)
  agent = agent_class.new(conversation_id: conversation_id)

  # Filter out system and tool messages for the client UI if desired
  user_facing_messages = agent.memory.messages.reject { |m| m[:role].to_s == "system" }
  render json: { messages: user_facing_messages }
end