summaryrefslogtreecommitdiff
path: root/components/storage.rb
blob: 10ea335f58305bd35612081644ca929047c165f9 (about) (plain)
1
2
3
4
5
6
7
8
9
10
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# frozen_string_literal: true

require 'babosa'

require_relative '../logic/helpers/hash'
require_relative 'crypto'

module NanoBot
  module Components
    class Storage
      EXTENSIONS = %w[yml yaml markdown mdown mkdn md].freeze

      def self.end_user(cartridge, environment)
        user = ENV.fetch('NANO_BOTS_END_USER', nil)

        if cartridge[:provider][:id] == 'openai' &&
           !cartridge[:provider][:settings][:user].nil? &&
           !cartridge[:provider][:settings][:user].to_s.strip.empty?
          user = cartridge[:provider][:settings][:user]
        end

        candidate = environment && (
          environment['NANO_BOTS_END_USER'] ||
          environment[:NANO_BOTS_END_USER]
        )

        user = candidate if !candidate.nil? && !candidate.to_s.strip.empty?

        user = if user.nil? || user.to_s.strip.empty?
                 'unknown'
               else
                 user.to_s.strip
               end

        Crypto.encrypt(user, soft: true)
      end

      def self.build_path_and_ensure_state_file!(key, cartridge, environment: {})
        path = [
          Logic::Helpers::Hash.fetch(cartridge, %i[state path]),
          Logic::Helpers::Hash.fetch(cartridge, %i[state directory]),
          ENV.fetch('NANO_BOTS_STATE_PATH', nil),
          ENV.fetch('NANO_BOTS_STATE_DIRECTORY', nil)
        ].find do |candidate|
          !candidate.nil? && !candidate.empty?
        end

        path = "#{user_home!.sub(%r{/$}, '')}/.local/state/nano-bots" if path.nil?

        path = "#{path.sub(%r{/$}, '')}/ruby-nano-bots"

        path = "#{path}/#{cartridge[:meta][:author].to_slug.normalize}"
        path = "#{path}/#{cartridge[:meta][:name].to_slug.normalize}"
        path = "#{path}/#{cartridge[:meta][:version].to_s.gsub('.', '-').to_slug.normalize}"
        path = "#{path}/#{end_user(cartridge, environment)}"
        path = "#{path}/#{Crypto.encrypt(key, soft: true)}"
        path = "#{path}/state.json"

        FileUtils.mkdir_p(File.dirname(path))

        unless File.exist?(path)
          File.write(
            path,
            Crypto.encrypt(JSON.generate({ key:, history: [] }))
          )
        end

        path
      end

      def self.cartridges_path(components: {})
        components[:directory?] = ->(path) { File.directory?(path) } unless components.key?(:directory?)
        components[:ENV] = ENV unless components.key?(:ENV)

        default = "#{user_home!(components:).sub(%r{/$}, '')}/.local/share/nano-bots/cartridges"

        from_environment = [
          components[:ENV].fetch('NANO_BOTS_CARTRIDGES_PATH', nil),
          components[:ENV].fetch('NANO_BOTS_CARTRIDGES_DIRECTORY', nil)
        ].compact

        elected = [
          from_environment.empty? ? nil : from_environment.join(':'),
          default
        ].compact.uniq.filter do |path|
          path.split(':').any? { |candidate| components[:directory?].call(candidate) }
        end.compact.first

        return default unless elected

        elected = elected.split(':').filter do |path|
          components[:directory?].call(path)
        end.compact

        elected.size.positive? ? elected.join(':') : default
      end

      def self.cartridge_path(path)
        partial = File.join(File.dirname(path), File.basename(path, File.extname(path)))

        candidates = [path]

        EXTENSIONS.each do |extension|
          candidates << "#{partial}.#{extension}"
        end

        directories = [
          ENV.fetch('NANO_BOTS_CARTRIDGES_PATH', nil),
          ENV.fetch('NANO_BOTS_CARTRIDGES_DIRECTORY', nil)
        ].compact.map do |directory|
          directory.split(':')
        end.flatten.map { |directory| directory.sub(%r{/$}, '') }

        directories.each do |directory|
          partial = File.join(File.dirname(partial), File.basename(partial, File.extname(partial)))

          partial = partial.sub(%r{^\.?/}, '')

          candidates << "#{directory}/#{partial}"

          EXTENSIONS.each do |extension|
            candidates << "#{directory}/#{partial}.#{extension}"
          end
        end

        directory = "#{user_home!.sub(%r{/$}, '')}/.local/share/nano-bots/cartridges"

        partial = File.join(File.dirname(partial), File.basename(partial, File.extname(partial)))

        partial = partial.sub(%r{^\.?/}, '')

        candidates << "#{directory}/#{partial}"

        EXTENSIONS.each do |extension|
          candidates << "#{directory}/#{partial}.#{extension}"
        end

        candidates = candidates.uniq

        candidates.find do |candidate|
          File.exist?(candidate) && File.file?(candidate)
        end
      end

      def self.user_home!(components: {})
        return components[:home] if components[:home]

        [Dir.home, `echo ~`.strip, '~'].find do |candidate|
          !candidate.nil? && !candidate.empty?
        end
      end
    end
  end
end