llama_bot_rails 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +249 -0
- data/Rakefile +8 -0
- data/app/assets/config/llama_bot_rails_manifest.js +1 -0
- data/app/assets/javascripts/llama_bot_rails/application.js +7 -0
- data/app/assets/javascripts/llama_bot_rails/chat.js +13 -0
- data/app/assets/stylesheets/llama_bot_rails/application.css +15 -0
- data/app/channels/llama_bot_rails/application_cable/channel.rb +8 -0
- data/app/channels/llama_bot_rails/application_cable/connection.rb +13 -0
- data/app/channels/llama_bot_rails/chat_channel.rb +306 -0
- data/app/controllers/llama_bot_rails/agent_controller.rb +72 -0
- data/app/controllers/llama_bot_rails/application_controller.rb +4 -0
- data/app/helpers/llama_bot_rails/application_helper.rb +4 -0
- data/app/javascript/channels/consumer.js +4 -0
- data/app/jobs/llama_bot_rails/application_job.rb +4 -0
- data/app/models/llama_bot_rails/application_record.rb +5 -0
- data/app/views/layouts/llama_bot_rails/application.html.erb +17 -0
- data/app/views/llama_bot_rails/agent/chat.html.erb +962 -0
- data/bin/rails +26 -0
- data/bin/rubocop +8 -0
- data/config/initializers/llama_bot_rails.rb +2 -0
- data/config/routes.rb +6 -0
- data/lib/llama_bot_rails/agent_state_builder.rb +17 -0
- data/lib/llama_bot_rails/engine.rb +23 -0
- data/lib/llama_bot_rails/llama_bot.rb +25 -0
- data/lib/llama_bot_rails/tools/rails_console_tool.rb +20 -0
- data/lib/llama_bot_rails/version.rb +3 -0
- data/lib/llama_bot_rails.rb +10 -0
- data/lib/tasks/llama_bot_rails_tasks.rake +4 -0
- metadata +128 -0
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'llama_bot_rails/llama_bot'
|
2
|
+
module LlamaBotRails
|
3
|
+
class AgentController < ActionController::Base
|
4
|
+
skip_before_action :verify_authenticity_token, only: [:command]
|
5
|
+
before_action :authenticate_agent!, only: [:command]
|
6
|
+
|
7
|
+
# POST /agent/command
|
8
|
+
def command
|
9
|
+
input = params[:command]
|
10
|
+
result = safety_eval(input)
|
11
|
+
render json: { result: result.inspect }
|
12
|
+
rescue => e
|
13
|
+
render json: { error: e.class.name, message: e.message }, status: :unprocessable_entity
|
14
|
+
end
|
15
|
+
|
16
|
+
def index
|
17
|
+
@llama_bot = LlamaBot.new
|
18
|
+
end
|
19
|
+
|
20
|
+
# GET /agent/chat
|
21
|
+
def chat
|
22
|
+
# Render chat.html.erb
|
23
|
+
end
|
24
|
+
|
25
|
+
def threads
|
26
|
+
begin
|
27
|
+
threads = LlamaBotRails::LlamaBot.get_threads
|
28
|
+
render json: threads
|
29
|
+
rescue => e
|
30
|
+
Rails.logger.error "Error in threads action: #{e.message}"
|
31
|
+
render json: { error: "Failed to fetch threads" }, status: :internal_server_error
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def chat_history
|
36
|
+
begin
|
37
|
+
thread_id = params[:thread_id]
|
38
|
+
Rails.logger.info "Fetching chat history for thread: #{thread_id}"
|
39
|
+
|
40
|
+
if thread_id == 'undefined' || thread_id.blank?
|
41
|
+
render json: []
|
42
|
+
return
|
43
|
+
end
|
44
|
+
|
45
|
+
history = LlamaBotRails::LlamaBot.get_chat_history(thread_id)
|
46
|
+
render json: history
|
47
|
+
rescue => e
|
48
|
+
Rails.logger.error "Error in chat_history action: #{e.message}"
|
49
|
+
render json: { error: "Failed to fetch chat history" }, status: :internal_server_error
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def safety_eval(input)
|
56
|
+
# VERY basic, sandbox for real use!
|
57
|
+
result = eval(input)
|
58
|
+
|
59
|
+
{ result: result.inspect }
|
60
|
+
rescue => e
|
61
|
+
{ error: e.class.name, message: e.message }
|
62
|
+
end
|
63
|
+
|
64
|
+
def authenticate_agent!
|
65
|
+
auth_header = request.headers["Authorization"]
|
66
|
+
token = auth_header&.split("Bearer ")&.last # Extract token after "Bearer "
|
67
|
+
@session_payload = Rails.application.message_verifier(:llamabot_ws).verify(token)
|
68
|
+
rescue ActiveSupport::MessageVerifier::InvalidSignature
|
69
|
+
head :unauthorized
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Llama bot rails</title>
|
5
|
+
<%= csrf_meta_tags %>
|
6
|
+
<%= csp_meta_tag %>
|
7
|
+
|
8
|
+
<%= yield :head %>
|
9
|
+
|
10
|
+
<%= stylesheet_link_tag "llama_bot_rails/application", media: "all" %>
|
11
|
+
</head>
|
12
|
+
<body>
|
13
|
+
|
14
|
+
<%= yield %>
|
15
|
+
|
16
|
+
</body>
|
17
|
+
</html>
|