Skip to content

Commit a2e3866

Browse files
committed
migrating to path
1 parent 1e3ceec commit a2e3866

File tree

10 files changed

+104
-64
lines changed

10 files changed

+104
-64
lines changed

.env.example

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ PORT=3048
33

44
RUN_DANGEROUSLY_AS_SUDO=false
55
RVM=false
6+
RERUN=false
67

78
FORCE_SANDBOXED=true
9+
ALLOW_CARTRIDGES_PATH_HEADER=true
810

911
NANO_BOTS_ENCRYPTION_PASSWORD=UNSAFE
1012
NANO_BOTS_END_USER=your-user
1113

12-
# NANO_BOTS_CARTRIDGES_DIRECTORY=/home/user/.local/share/nano-bots/cartridges
13-
# NANO_BOTS_STATE_DIRECTORY=/home/user/.local/state/nano-bots
14+
# NANO_BOTS_CARTRIDGES_PATH=/home/user/.local/share/nano-bots/cartridges
15+
# NANO_BOTS_STATE_PATH=/home/user/.local/state/nano-bots
1416
# NANO_BOTS_RACK_ATTACK=false
1517
# NANO_BOTS_NEW_RELIC=false
1618

Gemfile

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ gem 'rack', '~> 3.0', '>= 3.0.8'
1010
gem 'rack-cors', '~> 2.0', '>= 2.0.1'
1111
gem 'rackup', '~> 2.1'
1212

13-
gem 'puma', '~> 6.4', '>= 6.4.1'
13+
gem 'puma', '~> 6.4', '>= 6.4.2'
1414
gem 'rack-attack', '~> 6.7'
15-
gem 'roda', '~> 3.75'
15+
gem 'roda', '~> 3.76'
1616

1717
gem 'concurrent-ruby', '~> 1.2', '>= 1.2.2'
1818

19-
gem 'newrelic_rpm', '~> 9.6'
19+
gem 'newrelic_rpm', '~> 9.7'
2020

21-
gem 'nano-bots', '~> 2.5', '>= 2.5.1'
21+
gem 'nano-bots', '~> 3.0', '>= 3.0.1'
2222

2323
group :test, :development do
2424
gem 'pry-byebug', '~> 3.10', '>= 3.10.1'

Gemfile.lock

+33-32
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,23 @@ GEM
55
public_suffix (>= 2.0.2, < 6.0)
66
ast (2.4.2)
77
babosa (2.0.0)
8-
base64 (0.2.0)
98
byebug (11.1.3)
109
coderay (1.1.3)
11-
cohere-ai (1.0.1)
12-
faraday (~> 2.8, >= 2.8.1)
10+
cohere-ai (1.0.2)
11+
faraday (~> 2.9)
1312
concurrent-ruby (1.2.2)
1413
dotenv (2.8.1)
1514
event_stream_parser (1.0.0)
16-
faraday (2.8.1)
17-
base64
18-
faraday-net_http (>= 2.0, < 3.1)
19-
ruby2_keywords (>= 0.0.4)
15+
faraday (2.9.0)
16+
faraday-net_http (>= 2.0, < 3.2)
2017
faraday-multipart (1.0.4)
2118
multipart-post (~> 2)
22-
faraday-net_http (3.0.2)
19+
faraday-net_http (3.1.0)
20+
net-http
2321
ffi (1.16.3)
24-
gemini-ai (3.1.2)
22+
gemini-ai (3.1.3)
2523
event_stream_parser (~> 1.0)
26-
faraday (~> 2.8, >= 2.8.1)
24+
faraday (~> 2.9)
2725
googleauth (~> 1.8)
2826
google-cloud-env (2.1.0)
2927
faraday (>= 1.0, < 3.a)
@@ -40,36 +38,38 @@ GEM
4038
listen (3.8.0)
4139
rb-fsevent (~> 0.10, >= 0.10.3)
4240
rb-inotify (~> 0.9, >= 0.9.10)
43-
maritaca-ai (1.0.0)
44-
faraday (~> 2.8, >= 2.8.1)
41+
maritaca-ai (1.0.1)
42+
faraday (~> 2.9)
4543
method_source (1.0.0)
46-
mistral-ai (1.1.1)
44+
mistral-ai (1.1.2)
4745
event_stream_parser (~> 1.0)
48-
faraday (~> 2.8, >= 2.8.1)
46+
faraday (~> 2.9)
4947
multi_json (1.15.0)
5048
multipart-post (2.3.0)
51-
nano-bots (2.5.1)
49+
nano-bots (3.0.1)
5250
babosa (~> 2.0)
53-
cohere-ai (~> 1.0, >= 1.0.1)
51+
cohere-ai (~> 1.0, >= 1.0.2)
5452
concurrent-ruby (~> 1.2, >= 1.2.2)
5553
dotenv (~> 2.8, >= 2.8.1)
56-
gemini-ai (~> 3.1, >= 3.1.2)
57-
maritaca-ai (~> 1.0)
58-
mistral-ai (~> 1.1, >= 1.1.1)
59-
ollama-ai (~> 1.0)
54+
gemini-ai (~> 3.1, >= 3.1.3)
55+
maritaca-ai (~> 1.0, >= 1.0.1)
56+
mistral-ai (~> 1.1, >= 1.1.2)
57+
ollama-ai (~> 1.0, >= 1.0.1)
6058
pry (~> 0.14.2)
6159
rainbow (~> 3.1, >= 3.1.1)
6260
rbnacl (~> 7.1, >= 7.1.1)
61+
redcarpet (~> 3.6)
6362
ruby-openai (~> 6.3, >= 6.3.1)
6463
sweet-moon (~> 0.0.7)
65-
newrelic_rpm (9.6.0)
66-
base64
64+
net-http (0.4.1)
65+
uri
66+
newrelic_rpm (9.7.0)
6767
nio4r (2.7.0)
68-
ollama-ai (1.0.0)
69-
faraday (~> 2.8)
68+
ollama-ai (1.0.1)
69+
faraday (~> 2.9)
7070
os (1.1.4)
7171
parallel (1.24.0)
72-
parser (3.3.0.1)
72+
parser (3.3.0.2)
7373
ast (~> 2.4.1)
7474
racc
7575
pry (0.14.2)
@@ -79,7 +79,7 @@ GEM
7979
byebug (~> 11.0)
8080
pry (>= 0.13, < 0.15)
8181
public_suffix (5.0.4)
82-
puma (6.4.1)
82+
puma (6.4.2)
8383
nio4r (~> 2.0)
8484
racc (1.7.3)
8585
rack (3.0.8)
@@ -96,11 +96,12 @@ GEM
9696
ffi (~> 1.0)
9797
rbnacl (7.1.1)
9898
ffi
99+
redcarpet (3.6.0)
99100
regexp_parser (2.9.0)
100101
rerun (0.14.0)
101102
listen (~> 3.0)
102103
rexml (3.2.6)
103-
roda (3.75.0)
104+
roda (3.76.0)
104105
rack
105106
rubocop (1.59.0)
106107
json (~> 2.3)
@@ -120,7 +121,6 @@ GEM
120121
faraday (>= 1)
121122
faraday-multipart (>= 1)
122123
ruby-progressbar (1.13.0)
123-
ruby2_keywords (0.0.5)
124124
signet (0.18.0)
125125
addressable (~> 2.8)
126126
faraday (>= 0.17.5, < 3.a)
@@ -129,6 +129,7 @@ GEM
129129
sweet-moon (0.0.7)
130130
ffi (~> 1.15, >= 1.15.5)
131131
unicode-display_width (2.5.0)
132+
uri (0.13.0)
132133
webrick (1.8.1)
133134

134135
PLATFORMS
@@ -137,16 +138,16 @@ PLATFORMS
137138
DEPENDENCIES
138139
concurrent-ruby (~> 1.2, >= 1.2.2)
139140
dotenv (~> 2.8, >= 2.8.1)
140-
nano-bots (~> 2.5, >= 2.5.1)
141-
newrelic_rpm (~> 9.6)
141+
nano-bots (~> 3.0, >= 3.0.1)
142+
newrelic_rpm (~> 9.7)
142143
pry-byebug (~> 3.10, >= 3.10.1)
143-
puma (~> 6.4, >= 6.4.1)
144+
puma (~> 6.4, >= 6.4.2)
144145
rack (~> 3.0, >= 3.0.8)
145146
rack-attack (~> 6.7)
146147
rack-cors (~> 2.0, >= 2.0.1)
147148
rackup (~> 2.1)
148149
rerun (~> 0.14.0)
149-
roda (~> 3.75)
150+
roda (~> 3.76)
150151
rubocop (~> 1.59)
151152

152153
RUBY VERSION

controllers/cartridges.rb

+26-9
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88
require './logic/safety'
99

1010
module CartridgesController
11-
def self.index
12-
cartridges = NanoBot.cartridges
11+
def self.index(environment)
12+
components = {}
13+
14+
if environment[:NANO_BOTS_CARTRIDGES_PATH]
15+
components[:ENV] = { 'NANO_BOTS_CARTRIDGES_PATH' => environment[:NANO_BOTS_CARTRIDGES_PATH] }
16+
end
17+
18+
cartridges = NanoBot.cartridges.all(components:)
1319

1420
cartridges = cartridges.map do |cartridge|
1521
{
@@ -27,22 +33,28 @@ def self.index
2733
end
2834

2935
default_override = cartridges.find do |cartridge|
30-
cartridge[:system][:id].to_s.downcase == 'default'
36+
cartridge[:system][:id].to_s.downcase.strip == 'default'
3137
end
3238

3339
if default_override
3440
cartridges = cartridges.filter do |cartridge|
35-
cartridge[:system][:id] != '-' && cartridge[:system][:id].to_s.downcase != 'default'
41+
cartridge[:system][:id] != '-' && cartridge[:system][:id].to_s.downcase.strip != 'default'
3642
end.prepend(default_override)
3743
end
3844

3945
{ body: cartridges, status: 200 }
4046
end
4147

42-
def self.source(params)
48+
def self.source(params, environment)
4349
params = JSON.parse(params)
4450

45-
cartridge = NanoBot.cartridges.find do |cartridge|
51+
components = {}
52+
53+
if environment[:NANO_BOTS_CARTRIDGES_PATH]
54+
components[:ENV] = { 'NANO_BOTS_CARTRIDGES_PATH' => environment[:NANO_BOTS_CARTRIDGES_PATH] }
55+
end
56+
57+
cartridge = NanoBot.cartridges.all(components:).find do |cartridge|
4658
cartridge[:system][:id] == params['id']
4759
end
4860

@@ -73,12 +85,14 @@ def self.get_stream(id)
7385
end
7486

7587
def self.create_stream(params, environment)
88+
status = 200
89+
7690
params = JSON.parse(params)
7791

7892
stream = Stream.instance.create
7993

8094
nbot = NanoBot.new(
81-
cartridge: SafetyLogic.ensure_cartridge_is_safe_to_run(params['cartridge']),
95+
cartridge: SafetyLogic.ensure_cartridge_is_safe_to_run(params['cartridge'], environment),
8296
state: params['state'],
8397
environment:
8498
)
@@ -116,17 +130,18 @@ def self.create_stream(params, environment)
116130
end
117131
rescue StandardError => e
118132
Stream.instance.get(stream[:id])[:finished_at] = Time.now
133+
status = 500
119134
Stream.instance.get(stream[:id])[:state] = 'failed'
120135
Stream.instance.get(stream[:id])[:error] = { message: e.message, backtrace: e.backtrace }
121136
end
122137

123-
{ body: stream, status: 200 }
138+
{ body: stream, status: }
124139
end
125140

126141
def self.run(params, environment)
127142
params = JSON.parse(params)
128143
nbot = NanoBot.new(
129-
cartridge: SafetyLogic.ensure_cartridge_is_safe_to_run(params['cartridge']),
144+
cartridge: SafetyLogic.ensure_cartridge_is_safe_to_run(params['cartridge'], environment),
130145
state: params['state'],
131146
environment:
132147
)
@@ -150,5 +165,7 @@ def self.run(params, environment)
150165
state[:finished_at] = Time.now
151166

152167
{ body: state, status: 200 }
168+
rescue StandardError => e
169+
{ body: { error: { message: e.message, backtrace: e.backtrace } }, status: 500 }
153170
end
154171
end

docker-compose.example.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ services:
99
RVM: "false"
1010

1111
FORCE_SANDBOXED: "true"
12+
ALLOW_CARTRIDGES_PATH_HEADER: "true"
1213

1314
NANO_BOTS_ENCRYPTION_PASSWORD: UNSAFE
1415
NANO_BOTS_END_USER: your-user
15-
NANO_BOTS_CARTRIDGES_DIRECTORY: /cartridges
16-
# NANO_BOTS_STATE_DIRECTORY=/state
16+
NANO_BOTS_CARTRIDGES_PATH: /cartridges
17+
# NANO_BOTS_STATE_PATH=/state
1718
NANO_BOTS_RACK_ATTACK: "false"
1819
NANO_BOTS_NEW_RELIC: "false"
1920

init.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export $(grep -v '^#' .env | xargs)
22

33
command="bundle exec rackup --server puma -o 0.0.0.0 -p $PORT"
44

5-
if [ "$ENVIRONMENT" = 'development' ]; then
5+
if [ "$RERUN" = 'true' ]; then
66
command="bundle exec rerun -- $command"
77
fi
88

logic/safety.rb

+12-5
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,23 @@ def self.symbolize_keys(object)
1616
end
1717
end
1818

19-
def self.ensure_cartridge_is_safe_to_run(cartridge_input)
19+
def self.ensure_cartridge_is_safe_to_run(cartridge_input, environment)
20+
components = {}
21+
22+
if environment[:NANO_BOTS_CARTRIDGES_PATH]
23+
components[:ENV] = { 'NANO_BOTS_CARTRIDGES_PATH' => environment[:NANO_BOTS_CARTRIDGES_PATH] }
24+
end
25+
2026
cartridge = if cartridge_input.is_a?(Hash)
2127
cartridge_input
2228
else
23-
found = NanoBot.cartridges.find do |cartridge|
29+
found = NanoBot.cartridges.all(components:).find do |cartridge|
2430
cartridge[:system][:id].to_s == cartridge_input.to_s
2531
end
2632

27-
if found.nil? && cartridge_input == 'default'
33+
if found.nil? && cartridge_input.to_s.downcase.strip == 'default'
2834
cartridge_input = '-'
29-
found = NanoBot.cartridges.find do |cartridge|
35+
found = NanoBot.cartridges.all(components:).find do |cartridge|
3036
cartridge[:system][:id].to_s == cartridge_input.to_s
3137
end
3238
end
@@ -37,8 +43,9 @@ def self.ensure_cartridge_is_safe_to_run(cartridge_input)
3743
cartridge = SafetyLogic.symbolize_keys(cartridge)
3844

3945
unless cartridge[:provider]
40-
cartridge[:provider] =
46+
cartridge[:provider] = SafetyLogic.symbolize_keys(
4147
YAML.safe_load_file('static/cartridges/default.yml', permitted_classes: [Symbol])['provider']
48+
)
4249
end
4350

4451
cartridge[:safety] = {} if cartridge[:safety].nil?

ports/http.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@
99
module HTTP
1010
def self.routes(route, request, response)
1111
new_relic = ENV.fetch('NANO_BOTS_NEW_RELIC', nil).to_s == 'true'
12+
1213
ip = request.env['HTTP_X_FORWARDED_FOR']&.split(',')&.first || request.ip
1314

1415
environment = {
1516
NANO_BOTS_END_USER: "#{ip}-#{request.get_header('HTTP_NANO_BOTS_END_USER')}"
1617
}
1718

19+
cartridges_path = request.get_header('HTTP_NANO_BOTS_CARTRIDGES_PATH')
20+
21+
if ENV.fetch('ALLOW_CARTRIDGES_PATH_HEADER', nil).to_s == 'true' && !cartridges_path.to_s.strip.empty?
22+
environment[:NANO_BOTS_CARTRIDGES_PATH] = cartridges_path
23+
end
24+
1825
route.root do
1926
NewRelic::Agent.set_transaction_name('GET/') if new_relic
2027
IndexController.handler
@@ -27,7 +34,7 @@ def self.routes(route, request, response)
2734

2835
route.get 'cartridges' do
2936
NewRelic::Agent.set_transaction_name('GET/cartridges') if new_relic
30-
result = CartridgesController.index
37+
result = CartridgesController.index(environment)
3138
response.status = result[:status]
3239
result[:body]
3340
end
@@ -41,7 +48,7 @@ def self.routes(route, request, response)
4148

4249
route.post 'cartridges/source' do
4350
NewRelic::Agent.set_transaction_name('POST/cartridges/source') if new_relic
44-
result = CartridgesController.source(request.body.read)
51+
result = CartridgesController.source(request.body.read, environment)
4552
response.status = result[:status]
4653
result[:body]
4754
end

0 commit comments

Comments
 (0)