brainiac-fizzy 0.0.44 → 0.0.45

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c290f711243211d67abdd35bf34d3abd9676461f81bfd104b03f6b53e243328
4
- data.tar.gz: 72e650801901d0c6150f231ac16104124ceff67df87ffc39cbd3abf09b915e30
3
+ metadata.gz: b17e1dace17022716de710400df7e96a81e6162e3f24ef6de18d29e8b4f44708
4
+ data.tar.gz: 1797a9113aeb6bf305f7405f7b8f0e29c7a51ac2f777e7f1e72019f09a228ea4
5
5
  SHA512:
6
- metadata.gz: 5e03a9c0438cbb058848b27bc9f66af30392eca388212740a6eb1a7f0c82f7c257c2b8d09dfde839d9a37feee4549f484546f2e4da77af417c1b4202f686bf6c
7
- data.tar.gz: 185febcad0410ecb6a75020887d1d04e00ef753e6dd3d9193b8cfecb21eaf24d6df226ba3a7b608e85673ef655cb4cde4baad62a7bb1e582db27e6453a23c010
6
+ metadata.gz: 54fe307c208de599fcc2cfdd63d13d8490521c18d78e21b4d7ca3203ca998e9707cdc332d26fed23f8d624b44d5b5e54fe5192a1aab460b0fa71af645301d2af
7
+ data.tar.gz: 389eb3f9420e9cc34a2154f082fe99218b03b8dd78c860e642cf2aefcf3bbbc6e5c9ecd0fe59a7a3b1bc70bf98ee838c4e065987f262d971210e4e30f7f49b4e
@@ -0,0 +1,147 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "json"
5
+ require "open3"
6
+
7
+ module Brainiac
8
+ module Plugins
9
+ module Fizzy
10
+ # Provisions a Fizzy user for a new brainiac agent (`brainiac agent create`
11
+ # / `brainiac agent setup`).
12
+ #
13
+ # Fizzy has no "create user" API and only issues access tokens in the web
14
+ # UI, so the manual part is: open the account's join link, join as the
15
+ # agent's email, and copy a personal access token. Given that token, the
16
+ # rest is automated: user id lookup, name + avatar, authorized_users, and
17
+ # FIZZY_TOKEN in agents.json.
18
+ module AgentSetup
19
+ class << self
20
+ def run(agent_key, entry)
21
+ display_name = entry["display_name"] || agent_key.capitalize
22
+ config = fizzy_config
23
+ return unless config
24
+
25
+ token = registry.dig(agent_key, "env", "FIZZY_TOKEN")
26
+ if token && authorized?(config, display_name)
27
+ puts " [Fizzy] ✓ #{display_name} already has a Fizzy user (skipping)"
28
+ return
29
+ end
30
+
31
+ token ||= prompt_for_token(display_name, entry["email"], config)
32
+ return unless token
33
+
34
+ user = identity_user(token)
35
+ unless user
36
+ puts " [Fizzy] ✗ Fizzy rejected that token — re-run: brainiac agent setup #{agent_key}"
37
+ return
38
+ end
39
+
40
+ update_profile(user["id"], display_name, entry["avatar_path"], token, config["admin_token"])
41
+ save_token(agent_key, token)
42
+ authorize(display_name, user["id"])
43
+ puts " [Fizzy] ✓ Saved FIZZY_TOKEN and added #{display_name} (#{user["id"]}) to authorized_users"
44
+ end
45
+
46
+ private
47
+
48
+ def prompt_for_token(display_name, email, config)
49
+ return nil unless $stdin.tty?
50
+
51
+ join = fizzy_json("account", "join-code-show", token: config["admin_token"])
52
+ puts ""
53
+ puts " [Fizzy] Create a Fizzy user for #{display_name}:"
54
+ puts " 1. Open in a private window: #{join&.dig("url") || "(couldn't fetch — Account settings → Invite link)"}"
55
+ puts " 2. Join as #{email || "the agent's email"}, then click the magic link from that inbox"
56
+ puts " 3. Profile → Personal access tokens → new token with write access → copy it"
57
+ puts " Name, avatar and brainiac config are set automatically."
58
+ print " Access token (blank to skip): "
59
+ token = read_secret
60
+ puts ""
61
+ token.nil? || token.empty? ? nil : token
62
+ end
63
+
64
+ def read_secret
65
+ require "io/console"
66
+ $stdin.noecho(&:gets)&.strip
67
+ rescue Errno::ENOTTY, NoMethodError
68
+ $stdin.gets&.strip
69
+ end
70
+
71
+ # The token owner's user record in the configured account.
72
+ def identity_user(token)
73
+ accounts = fizzy_json("identity", "show", token: token)&.dig("accounts") || []
74
+ account = fizzy_json("config", "show")&.dig("account")
75
+ match = accounts.find { |a| a["slug"].to_s.delete_prefix("/") == account.to_s } || accounts.first
76
+ match&.dig("user")
77
+ end
78
+
79
+ # Users can't always edit themselves (members vs admins), so fall back
80
+ # to the admin token from fizzy.json.
81
+ def update_profile(user_id, display_name, avatar_path, token, admin_token)
82
+ args = ["user", "update", user_id, "--name", display_name]
83
+ args += ["--avatar", avatar_path] if avatar_path && File.file?(avatar_path)
84
+ what = args.include?("--avatar") ? "name + avatar" : "name"
85
+
86
+ if run_fizzy(*args, token: token) || (admin_token && run_fizzy(*args, token: admin_token))
87
+ puts " [Fizzy] ✓ Set #{what}"
88
+ else
89
+ puts " [Fizzy] ⚠ Couldn't set #{what} — update the profile in Fizzy"
90
+ end
91
+ end
92
+
93
+ def fizzy_json(*, token: nil)
94
+ output = run_fizzy(*, "--quiet", token: token)
95
+ output && JSON.parse(output)
96
+ rescue JSON::ParserError
97
+ nil
98
+ end
99
+
100
+ def run_fizzy(*args, token: nil)
101
+ args += ["--token", token] if token
102
+ output, status = Open3.capture2("fizzy", *args, err: File::NULL)
103
+ status.success? ? output : nil
104
+ rescue Errno::ENOENT
105
+ nil
106
+ end
107
+
108
+ def brainiac_dir
109
+ ENV.fetch("BRAINIAC_DIR", File.join(Dir.home, ".brainiac"))
110
+ end
111
+
112
+ def registry_file = File.join(brainiac_dir, "agents.json")
113
+ def config_file = File.join(brainiac_dir, "fizzy.json")
114
+
115
+ def registry
116
+ File.exist?(registry_file) ? JSON.parse(File.read(registry_file)) : {}
117
+ end
118
+
119
+ def fizzy_config
120
+ File.exist?(config_file) ? JSON.parse(File.read(config_file)) : nil
121
+ end
122
+
123
+ def authorized?(config, display_name)
124
+ (config["authorized_users"] || []).any? { |u| u["name"]&.downcase == display_name.downcase }
125
+ end
126
+
127
+ def save_token(agent_key, token)
128
+ reg = registry
129
+ reg[agent_key] ||= {}
130
+ reg[agent_key]["env"] ||= {}
131
+ reg[agent_key]["env"]["FIZZY_TOKEN"] = token
132
+ FileUtils.mkdir_p(brainiac_dir)
133
+ File.write(registry_file, JSON.pretty_generate(reg))
134
+ end
135
+
136
+ def authorize(display_name, user_id)
137
+ config = fizzy_config
138
+ config["authorized_users"] ||= []
139
+ config["authorized_users"].reject! { |u| u["name"]&.downcase == display_name.downcase }
140
+ config["authorized_users"] << { "id" => user_id, "name" => display_name, "human" => false }
141
+ File.write(config_file, JSON.pretty_generate(config))
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "json"
4
+ require_relative "agent_setup"
4
5
 
5
6
  module Brainiac
6
7
  module Plugins
@@ -792,32 +793,10 @@ module Brainiac
792
793
  []
793
794
  end
794
795
 
795
- # Called by brainiac CLI after `agent create` — prompts for Fizzy user ID.
796
+ # Called by brainiac CLI after `agent create` / `agent setup` — provisions
797
+ # the agent's Fizzy user (see AgentSetup). Safe to re-run.
796
798
  def self.on_agent_created(agent_key, entry)
797
- return unless $stdin.tty?
798
-
799
- config_file = File.join(ENV.fetch("BRAINIAC_DIR", File.join(Dir.home, ".brainiac")), "fizzy.json")
800
- return unless File.exist?(config_file)
801
-
802
- display_name = entry["display_name"] || agent_key.capitalize
803
- puts ""
804
- puts " [Fizzy] Configure Fizzy for #{display_name}?"
805
- print " Fizzy user ID (or blank to skip): "
806
- user_id = $stdin.gets&.chomp
807
- return if user_id.nil? || user_id.empty?
808
-
809
- config = JSON.parse(File.read(config_file))
810
- config["authorized_users"] ||= []
811
-
812
- existing = config["authorized_users"].find { |u| u["id"] == user_id || u["name"]&.downcase == display_name.downcase }
813
- if existing
814
- puts " ✓ #{display_name} already in authorized_users (id: #{existing["id"]})"
815
- return
816
- end
817
-
818
- config["authorized_users"] << { "id" => user_id, "name" => display_name, "human" => false }
819
- File.write(config_file, JSON.pretty_generate(config))
820
- puts " ✓ Added #{display_name} (#{user_id}) to fizzy.json authorized_users"
799
+ AgentSetup.run(agent_key, entry)
821
800
  rescue JSON::ParserError => e
822
801
  puts " ⚠ Could not update fizzy.json: #{e.message}"
823
802
  end
@@ -3,7 +3,7 @@
3
3
  module Brainiac
4
4
  module Plugins
5
5
  module Fizzy
6
- VERSION = "0.0.44"
6
+ VERSION = "0.0.45"
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brainiac-fizzy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.44
4
+ version: 0.0.45
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Davis
@@ -89,6 +89,7 @@ extra_rdoc_files: []
89
89
  files:
90
90
  - README.md
91
91
  - lib/brainiac/plugins/fizzy.rb
92
+ - lib/brainiac/plugins/fizzy/agent_setup.rb
92
93
  - lib/brainiac/plugins/fizzy/cli.rb
93
94
  - lib/brainiac/plugins/fizzy/config.rb
94
95
  - lib/brainiac/plugins/fizzy/delegators.rb