Class: Anima::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/anima/installer.rb

Constant Summary collapse

DIRECTORIES =
%w[
  agents
  skills
  db
  config/credentials
  log
  tmp
  tmp/pids
  tmp/cache
].freeze
ANIMA_HOME =
Pathname.new(File.expand_path("~/.anima")).freeze
TEMPLATE_DIR =
File.expand_path("../../templates", __dir__).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(anima_home: ANIMA_HOME) ⇒ Installer

Returns a new instance of Installer.



25
26
27
# File 'lib/anima/installer.rb', line 25

def initialize(anima_home: ANIMA_HOME)
  @anima_home = anima_home
end

Instance Attribute Details

#anima_homeObject (readonly)

Returns the value of attribute anima_home.



23
24
25
# File 'lib/anima/installer.rb', line 23

def anima_home
  @anima_home
end

Instance Method Details

#create_directoriesObject



40
41
42
43
44
45
46
47
48
# File 'lib/anima/installer.rb', line 40

def create_directories
  DIRECTORIES.each do |dir|
    path = anima_home.join(dir)
    next if path.directory?

    FileUtils.mkdir_p(path)
    say "  created #{path}"
  end
end

#create_mcp_configObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/anima/installer.rb', line 71

def create_mcp_config
  config_path = anima_home.join("mcp.toml")
  return if config_path.exist?

  config_path.write(<<~TOML)
    # MCP server configuration
    # Declare MCP servers here. Anima connects on startup and
    # registers their tools alongside built-in ones.
    #
    # HTTP transport:
    # [servers.example]
    # transport = "http"
    # url = "http://localhost:3000/mcp/v2"
    # headers = { Authorization = "Bearer ${API_KEY}" }
    #
    # Stdio transport:
    # [servers.example]
    # transport = "stdio"
    # command = "my-mcp-server"
    # args = ["--verbose"]
    # env = { API_KEY = "${API_KEY}" }
  TOML
  say "  created #{config_path}"
end

#create_settings_configObject



62
63
64
65
66
67
68
69
# File 'lib/anima/installer.rb', line 62

def create_settings_config
  config_path = anima_home.join("config.toml")
  return if config_path.exist?

  template = File.read(File.join(TEMPLATE_DIR, "config.toml"))
  config_path.write(template.gsub("{{ANIMA_HOME}}") { anima_home.to_s })
  say "  created #{config_path}"
end

#create_soul_fileObject

Copies the soul template to ~/.anima/soul.md — the agent’s self-authored identity file. Skips if the file already exists so agent-written content is never overwritten.



53
54
55
56
57
58
59
60
# File 'lib/anima/installer.rb', line 53

def create_soul_file
  soul_path = anima_home.join("soul.md")
  return if soul_path.exist?

  template = File.join(TEMPLATE_DIR, "soul.md")
  soul_path.write(File.read(template))
  say "  created #{soul_path}"
end

#create_systemd_serviceObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/anima/installer.rb', line 132

def create_systemd_service
  service_dir = Pathname.new(File.expand_path("~/.config/systemd/user"))
  service_path = service_dir.join("anima.service")
  FileUtils.mkdir_p(service_dir)

  anima_bin = File.join(Gem.bindir, "anima")
  unit_content = <<~UNIT
    [Unit]
    Description=Anima - Personal AI Agent
    After=network.target

    [Service]
    Type=simple
    ExecStart=#{anima_bin} start -e production
    Restart=on-failure
    RestartSec=5

    [Install]
    WantedBy=default.target
  UNIT

  already_exists = service_path.exist?
  if already_exists && service_path.read == unit_content
    say "  anima.service unchanged"
  else
    service_path.write(unit_content)
    say "  #{already_exists ? "updated" : "created"} #{service_path}"
  end

  system("systemctl", "--user", "daemon-reload", err: File::NULL, out: File::NULL)
  system("systemctl", "--user", "enable", "--now", "anima.service", err: File::NULL, out: File::NULL)
  say "  enabled and started anima.service"
end

#generate_credentialsObject



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
# File 'lib/anima/installer.rb', line 96

def generate_credentials
  require "active_support"
  require "active_support/encrypted_configuration"

  %w[production development test].each do |env|
    content_path = anima_home.join("config", "credentials", "#{env}.yml.enc")
    key_path = anima_home.join("config", "credentials", "#{env}.key")

    next if key_path.exist? && content_path.exist?

    content_str = content_path.to_s
    key_str = key_path.to_s

    key = ActiveSupport::EncryptedFile.generate_key
    key_path.write(key)
    File.chmod(0o600, key_str)

    config = ActiveSupport::EncryptedConfiguration.new(
      config_path: content_str,
      key_path: key_str,
      env_key: "RAILS_MASTER_KEY",
      raise_if_missing_key: true
    )

    config.write(<<~YAML)
      secret_key_base: #{SecureRandom.hex(64)}
      active_record_encryption:
        primary_key: #{SecureRandom.base64(32)}
        deterministic_key: #{SecureRandom.base64(32)}
        key_derivation_salt: #{SecureRandom.base64(32)}
    YAML
    File.chmod(0o600, content_str)
    say "  created credentials for #{env}"
  end
end

#runObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/anima/installer.rb', line 29

def run
  say "Installing Anima to #{anima_home}..."
  create_directories
  create_soul_file
  create_settings_config
  create_mcp_config
  generate_credentials
  create_systemd_service
  say "Installation complete. Brain is running. Connect with 'anima tui'."
end