Class: ActiveAgent::ChatsController
- Inherits:
-
ActionController::Base
- Object
- ActionController::Base
- ActiveAgent::ChatsController
- Includes:
- ActionController::Live
- Defined in:
- app/controllers/active_agent/chats_controller.rb
Instance Method Summary collapse
- #create ⇒ Object
-
#destroy ⇒ Object
Optional endpoint to clear history.
-
#index ⇒ Object
Optional endpoint to fetch history.
Instance Method Details
#create ⇒ Object
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] = params[:message] if agent_name.blank? || conversation_id.blank? || .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() do |chunk| response.stream.write("data: #{ { chunk: chunk }.to_json }\n\n") end rescue StandardError => e ActiveAgent.logger.error("[ActiveAgent] Streaming controller error: #{e.}\n#{e.backtrace.join("\n")}") response.stream.write("data: #{ { error: e. }.to_json }\n\n") ensure response.stream.close end else begin reply = agent.chat() render json: { reply: reply } rescue StandardError => e ActiveAgent.logger.error("[ActiveAgent] Controller error: #{e.}") render json: { error: e. }, status: :unprocessable_entity end end end |
#destroy ⇒ Object
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 |
#index ⇒ Object
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 = agent.memory..reject { |m| m[:role].to_s == "system" } render json: { messages: } end |