Class: LlamaBotRails::AgentController

Inherits:
ActionController::Base
  • Object
show all
Includes:
ActionController::Live, AgentAuth, ControllerExtensions
Defined in:
app/controllers/llama_bot_rails/agent_controller.rb

Constant Summary

Constants included from AgentAuth

LlamaBotRails::AgentAuth::AUTH_SCHEME

Instance Method Summary collapse

Methods included from AgentAuth

#llama_bot_request?, #should_check_agent_auth?

Instance Method Details

#chatObject

GET /agent/chat



49
50
51
# File 'app/controllers/llama_bot_rails/agent_controller.rb', line 49

def chat
    # Render chat.html.erb
end

#chat_historyObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/llama_bot_rails/agent_controller.rb', line 68

def chat_history
    begin
        thread_id = params[:thread_id]
        Rails.logger.info "Fetching chat history for thread: #{thread_id}"
        
        if thread_id == 'undefined' || thread_id.blank?
            render json: []
            return
        end
        
        history = LlamaBotRails::LlamaBot.get_chat_history(thread_id)
        render json: history
    rescue => e
        Rails.logger.error "Error in chat_history action: #{e.message}"
        render json: { error: "Failed to fetch chat history" }, status: :internal_server_error
    end
end

#chat_wsObject

GET /agent/chat_ws



54
55
56
# File 'app/controllers/llama_bot_rails/agent_controller.rb', line 54

def chat_ws
    # render chat_ws.html.erb
end

#commandObject

POST /agent/command



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
# File 'app/controllers/llama_bot_rails/agent_controller.rb', line 16

def command

    unless LlamaBotRails.config.enable_console_tool
        Rails.logger.warn("[[LlamaBot Debug]] Console tool is disabled")
        render json: { error: "Console tool is disabled" }, status: :forbidden
        return
    end

    input = params[:command]
    
    # Capture both stdout and return value
    output = StringIO.new
    result = nil
    
    $stdout = output
    result = safety_eval(input)
    $stdout = STDOUT

    # Return both output and result
    render json: { 
        output: output.string.strip, 
        result: result 
    }
rescue => e
    $stdout = STDOUT  # Reset stdout on error
    render json: { error: "#{e.class.name}: #{e.message}" }
end

#indexObject



44
45
46
# File 'app/controllers/llama_bot_rails/agent_controller.rb', line 44

def index
    @llama_bot = LlamaBot.new
end

#send_messageObject

POST /agent/send-message



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/llama_bot_rails/agent_controller.rb', line 87

def send_message
    response.headers['Content-Type']  = 'text/event-stream'
    response.headers['Cache-Control'] = 'no-cache'
    response.headers['Connection']    = 'keep-alive'

    # Get the currently logged in user from the environment.
    current_user = LlamaBotRails.current_user_resolver.call(request.env)

    @api_token = Rails.application.message_verifier(:llamabot_ws).generate(
        { session_id: SecureRandom.uuid, user_id: current_user&.id},
        expires_in: 30.minutes
    )

    # 1. Instantiate the builder
    builder = state_builder_class.new(
        params: params,
        context: { api_token: @api_token }.with_indifferent_access
    )
    
    # 2. Construct the LangGraph-ready state
    state_payload = builder.build
    # sse = SSE.new(response.stream)

    begin
        LlamaBotRails::LlamaBot.send_agent_message(state_payload) do |chunk|
            Rails.logger.info "[[LlamaBot]] Received chunk in agent_controller.rb: #{chunk}"
            # sse.write(chunk)
            response.stream.write "data: #{chunk.to_json}\n\n"

        end
    rescue => e
        Rails.logger.error "Error in send_message action: #{e.message}"
        response.stream.write "data: #{ { type: 'error', content: e.message }.to_json }\n\n"

        # sse.write({ type: 'error', content: e.message })
    ensure
        response.stream.close

        # sse.close
    end
end

#test_streamingObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/controllers/llama_bot_rails/agent_controller.rb', line 129

def test_streaming
    response.headers['Content-Type']  = 'text/event-stream'
    response.headers['Cache-Control'] = 'no-cache'
    response.headers['Connection']    = 'keep-alive'
    sse = SSE.new(response.stream)
    sse.write({ type: 'start', content: 'Starting streaming' })
    sleep 1
    sse.write({ type: 'ai', content: 'This is an AI message' })
    sleep 1
    sse.write({ type: 'ai', content: 'This is an AI message' })
    sleep 1
    sse.write({ type: 'ai', content: 'This is an AI message' })
    sleep 1
    sse.write({ type: 'ai', content: 'This is an AI message' })
end

#threadsObject



58
59
60
61
62
63
64
65
66
# File 'app/controllers/llama_bot_rails/agent_controller.rb', line 58

def threads
    begin
        threads = LlamaBotRails::LlamaBot.get_threads
        render json: threads
    rescue => e
        Rails.logger.error "Error in threads action: #{e.message}"
        render json: { error: "Failed to fetch threads" }, status: :internal_server_error
    end
end