flare 0.5.1 → 0.5.2

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: d9a717e37a1b09780b8430531cb36d7ed8881562522e69e9f7a770710f41ada6
4
- data.tar.gz: a5becacc7ae0e85bd7b29e50f650ffc0c2105c425a190befad58098b85b83bc4
3
+ metadata.gz: 5fc174023f479f7d8310b929433f258fdf4a5acb2e958d5c5480b8869cc22be5
4
+ data.tar.gz: 945dae2872f5d693da04fd7481fd6b74f7cae5987f1cde40ae42e439ea3fb77f
5
5
  SHA512:
6
- metadata.gz: 02c2ffb1e25b8b447173ed8e1eba61c80051e91420ffccfa0f002f45e62cf8af832bb35b1776ff9026da6e665eca27992e13f418936d57faa02b36edc85b4292
7
- data.tar.gz: 5579cf57806f815da0c5dd97a45a3397aaa7bd603a695353cd89772704767fea34bbcab4c21a3db75eb292797df56e8bc0272e8fba6aef84c9945f75ba37e383
6
+ metadata.gz: a40e785ca8110a4c4efd9fd0b4443c421758811b8dd4f8939a6410fb01d5a9e4cc0a6c3d41226241ea31bd872d7a14c579cfeba07a8b85543871df0ab8f66515
7
+ data.tar.gz: 21df6659516866de21ac319e555407da98a760668336c2d771426cc4cdd57b914b46c85c2fc274b66a308ca5b448ed5012dbea0a75598e5657185b334667e931
data/README.md CHANGED
@@ -45,6 +45,7 @@ The setup command will:
45
45
  1. Authenticate with flare.am to configure metrics
46
46
  2. Create a config initializer with sensible defaults
47
47
  3. Update .gitignore
48
+ 4. Offer to connect Claude Code, Codex and Cursor to Flare's MCP server, so you can ask your AI assistant about your app's performance. Each one signs in to Flare the first time it connects.
48
49
 
49
50
  Start your Rails server and visit `/flare` to see the dashboard.
50
51
 
@@ -58,6 +59,7 @@ To enable metrics, set `FLARE_KEY` in your environment (get one at [flare.am](ht
58
59
 
59
60
  ```bash
60
61
  flare setup # Authenticate and configure Flare
62
+ flare connect # Connect your AI assistants to Flare
61
63
  flare doctor # Check your setup for issues
62
64
  flare status # Show current configuration
63
65
  flare requests # List recent requests from local development spans
@@ -0,0 +1,249 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "open3"
5
+ require "fileutils"
6
+ require "securerandom"
7
+ require_relative "output"
8
+
9
+ module Flare
10
+ # Points the AI clients on this machine at Flare's hosted MCP server, as
11
+ # `flare-cli setup` does. Each client signs in to Flare itself the first
12
+ # time it connects, so no token is written anywhere. A client that already
13
+ # has a different `flare` server is left alone.
14
+ class AIClientsSetup
15
+ include CLI::Output
16
+
17
+ DEFAULT_HOST = "https://flare.am"
18
+ SERVER_NAME = "flare"
19
+ FIRST_PROMPT = "Using Flare, what's slowest in production over the last 24 hours?"
20
+ CLIENTS = {
21
+ claude: "Claude Code",
22
+ codex: "Codex",
23
+ cursor: "Cursor",
24
+ claude_desktop: "Claude Desktop",
25
+ }.freeze
26
+
27
+ # Runs the clients' own CLIs. `capture` returns [output, success];
28
+ # `attached` shares the terminal, because Codex may start its sign-in
29
+ # right away and print a URL to open.
30
+ class Runner
31
+ def capture(*command)
32
+ output, status = Open3.capture2e(*command)
33
+ [output, status.success?]
34
+ rescue SystemCallError => e
35
+ [e.message, false]
36
+ end
37
+
38
+ def attached(*command)
39
+ system(*command) == true
40
+ end
41
+ end
42
+
43
+ def initialize(host: ENV.fetch("FLARE_HOST", DEFAULT_HOST), input: $stdin, env: ENV, home: Dir.home, platform: RUBY_PLATFORM, runner: Runner.new)
44
+ @host = host.chomp("/")
45
+ @url = "#{@host}/mcp"
46
+ @input = input
47
+ @env = env
48
+ @home = home
49
+ @platform = platform
50
+ @runner = runner
51
+ end
52
+
53
+ # `flare setup` asks first, since this changes other apps' settings.
54
+ # `flare connect` is the asking. Returns false if any client failed to
55
+ # connect, so `flare connect` can exit non-zero.
56
+ def run(ask: true)
57
+ @failed = false
58
+ puts
59
+ puts bold("Connect your AI assistant")
60
+
61
+ clients = CLIENTS.keys.select { |client| installed?(client) }
62
+ if clients.empty?
63
+ puts " No AI clients found. To connect one later, add #{url} to it as a remote MCP server."
64
+ puts " See #{@host}/docs/mcp"
65
+ elsif !ask || confirmed?(clients)
66
+ connect(clients)
67
+ end
68
+ !@failed
69
+ end
70
+
71
+ private
72
+
73
+ attr_reader :url
74
+
75
+ # A closed stdin (a script, an agent) counts as no.
76
+ def confirmed?(clients)
77
+ print " Found #{clients.map { |client| CLIENTS[client] }.join(", ")}. Connect #{clients.one? ? "it" : "them"} to Flare? (Y/n): "
78
+ answer = @input.gets
79
+ reply = answer.to_s.strip.downcase
80
+ return true if answer && (reply.empty? || reply.start_with?("y"))
81
+
82
+ puts if answer.nil?
83
+ puts " Skipped. Run #{bold("flare connect")} #{answer.nil? ? "in a terminal" : "any time"} to connect them."
84
+ false
85
+ end
86
+
87
+ # Suggests a first question only when some client is, or is about to be,
88
+ # pointed at Flare.
89
+ def connect(clients)
90
+ @ready = false
91
+ next_steps = clients.filter_map do |client|
92
+ hint = send("connect_#{client}")
93
+ "#{CLIENTS[client]}: #{hint}" if hint
94
+ end
95
+
96
+ puts
97
+ if next_steps.any?
98
+ puts " Next:"
99
+ next_steps.each { |line| puts " #{line}" }
100
+ puts
101
+ puts " Then ask: #{FIRST_PROMPT.inspect}"
102
+ elsif @ready
103
+ puts " Ask: #{FIRST_PROMPT.inspect}"
104
+ end
105
+ end
106
+
107
+ def installed?(client)
108
+ case client
109
+ when :claude then executable?("claude")
110
+ when :codex then executable?("codex")
111
+ when :cursor then executable?("cursor") || File.directory?(File.join(@home, ".cursor"))
112
+ when :claude_desktop then File.directory?(claude_desktop_directory)
113
+ end
114
+ end
115
+
116
+ def connect_claude
117
+ current = claude_config.dig("mcpServers", SERVER_NAME)
118
+ if current
119
+ return already(:claude) if current["type"] == "http" && current["url"] == url
120
+ return different(:claude, "run #{bold("claude mcp remove #{SERVER_NAME} -s user")}")
121
+ end
122
+
123
+ command = ["claude", "mcp", "add", "--transport", "http", "-s", "user", SERVER_NAME, url]
124
+ output, success = @runner.capture(*command)
125
+ return failed(:claude, command: command, output: output) unless success
126
+
127
+ connected(:claude, "run /mcp, choose #{SERVER_NAME}, and sign in")
128
+ end
129
+
130
+ def connect_codex
131
+ output, success = @runner.capture("codex", "mcp", "get", SERVER_NAME, "--json")
132
+ if success
133
+ current = parse_json(output)
134
+ transport = current["transport"] || {}
135
+ matching = transport["type"] == "streamable_http" && transport["url"] == url
136
+ return different(:codex, "run #{bold("codex mcp remove #{SERVER_NAME}")}") unless matching
137
+ return already(:codex) unless current["enabled"] == false
138
+ elsif !output.downcase.match?(/not found|no mcp server/)
139
+ return failed(:codex, command: ["codex", "mcp", "get", SERVER_NAME], output: output)
140
+ end
141
+
142
+ command = ["codex", "mcp", "add", SERVER_NAME, "--url", url]
143
+ return failed(:codex, command: command) unless @runner.attached(*command)
144
+
145
+ connected(:codex, "run `codex mcp login #{SERVER_NAME}` if it hasn't asked you to sign in")
146
+ end
147
+
148
+ # Sets mcpServers.flare in ~/.cursor/mcp.json, keeping everything else in it.
149
+ def connect_cursor
150
+ path = File.join(@home, ".cursor", "mcp.json")
151
+ document = File.exist?(path) ? JSON.parse(File.read(path)) : {}
152
+ servers = document["mcpServers"] ||= {}
153
+ entry = { "url" => url }
154
+
155
+ if servers.key?(SERVER_NAME)
156
+ return already(:cursor) if servers[SERVER_NAME] == entry
157
+ return different(:cursor, "remove #{SERVER_NAME} from ~/.cursor/mcp.json")
158
+ end
159
+
160
+ servers[SERVER_NAME] = entry
161
+ write_atomically(path, JSON.pretty_generate(document) + "\n")
162
+ connected(:cursor, "open Cursor Settings → MCP and sign in to #{SERVER_NAME}", where: "~/.cursor/mcp.json")
163
+ rescue JSON::ParserError, SystemCallError => e
164
+ failed(:cursor, output: e.message)
165
+ end
166
+
167
+ # Claude Desktop keeps remote servers in your account, not a file.
168
+ def connect_claude_desktop
169
+ "in Settings, add a custom connector with the URL #{url}"
170
+ end
171
+
172
+ # Each outcome prints one line and returns what's left to do in the client.
173
+ def connected(client, hint, where: nil)
174
+ @ready = true
175
+ puts " #{checkmark} Connected #{CLIENTS[client]}#{" in #{where}" if where}"
176
+ hint
177
+ end
178
+
179
+ def already(client)
180
+ @ready = true
181
+ puts " #{checkmark} #{CLIENTS[client]} is already connected to Flare"
182
+ nil
183
+ end
184
+
185
+ # Nothing broke, but this client isn't pointed at Flare, so it counts as
186
+ # a failure for `flare connect`'s exit status.
187
+ def different(client, how)
188
+ @failed = true
189
+ puts " #{warn_mark} #{CLIENTS[client]} already has a different #{SERVER_NAME} server, so it was left alone."
190
+ puts " To replace it, #{how}, then run #{bold("flare connect")}."
191
+ nil
192
+ end
193
+
194
+ def failed(client, command: nil, output: nil)
195
+ @failed = true
196
+ reason = output.to_s.lines.map(&:strip).reject(&:empty?).first
197
+ puts " #{xmark} Couldn't connect #{CLIENTS[client]}#{": #{reason}" if reason}"
198
+ puts " Run it yourself: #{command.join(" ")}" if command
199
+ nil
200
+ end
201
+
202
+ def claude_config
203
+ directory = @env.fetch("CLAUDE_CONFIG_DIR", "").empty? ? @home : @env["CLAUDE_CONFIG_DIR"]
204
+ path = File.join(directory, ".claude.json")
205
+ File.exist?(path) ? parse_json(File.read(path)) : {}
206
+ end
207
+
208
+ def parse_json(text)
209
+ value = JSON.parse(text)
210
+ value.is_a?(Hash) ? value : {}
211
+ rescue JSON::ParserError
212
+ {}
213
+ end
214
+
215
+ def claude_desktop_directory
216
+ case @platform
217
+ when /darwin/ then File.join(@home, "Library", "Application Support", "Claude")
218
+ when /mingw|mswin/ then File.join(@env.fetch("APPDATA", File.join(@home, "AppData", "Roaming")), "Claude")
219
+ else File.join(@env.fetch("XDG_CONFIG_HOME", File.join(@home, ".config")), "Claude")
220
+ end
221
+ end
222
+
223
+ def executable?(name)
224
+ @env.fetch("PATH", "").split(File::PATH_SEPARATOR).any? do |directory|
225
+ path = File.join(directory, name)
226
+ File.file?(path) && File.executable?(path)
227
+ end
228
+ end
229
+
230
+ # Writes to a temporary file and renames it, so a crash never leaves the
231
+ # client with half a config. The temporary file is created with the
232
+ # original's permissions, so a private config is never readable by others.
233
+ def write_atomically(path, contents)
234
+ FileUtils.mkdir_p(File.dirname(path))
235
+ mode = File.exist?(path) ? File.stat(path).mode & 0o777 : 0o644
236
+ temporary = "#{path}.flare-#{SecureRandom.hex(8)}"
237
+ created = false
238
+ File.open(temporary, File::WRONLY | File::CREAT | File::EXCL, mode) do |file|
239
+ created = true
240
+ file.write(contents)
241
+ end
242
+ File.chmod(mode, temporary)
243
+ File.rename(temporary, path)
244
+ created = false
245
+ ensure
246
+ File.delete(temporary) if created && File.exist?(temporary)
247
+ end
248
+ end
249
+ end
@@ -11,13 +11,14 @@ require "fileutils"
11
11
  require "timeout"
12
12
  require_relative "output"
13
13
  require_relative "project"
14
+ require_relative "ai_clients_setup"
14
15
 
15
16
  module Flare
16
17
  class SetupCommand
17
18
  include CLI::Output
18
19
  include CLI::Project
19
20
 
20
- DEFAULT_HOST = "https://flare.am"
21
+ DEFAULT_HOST = AIClientsSetup::DEFAULT_HOST
21
22
  TIMEOUT_SECONDS = 300 # 5 minutes
22
23
  CLIENT_READ_TIMEOUT = 1
23
24
 
@@ -85,14 +86,16 @@ module Flare
85
86
  # end
86
87
  RUBY
87
88
 
88
- def initialize(force: false)
89
+ def initialize(force: false, ai_clients: AIClientsSetup.new)
89
90
  @force = force
91
+ @ai_clients = ai_clients
90
92
  end
91
93
 
92
94
  def run
93
95
  authenticate
94
96
  create_initializer
95
97
  add_gitignore_entries
98
+ @ai_clients.run
96
99
 
97
100
  puts
98
101
  puts "#{green("Setup complete!")}"
data/lib/flare/cli.rb CHANGED
@@ -6,6 +6,7 @@ module Flare
6
6
  module CLI
7
7
  COMMANDS = {
8
8
  "setup" => "Authenticate and configure Flare for this project",
9
+ "connect" => "Connect your AI assistants to Flare",
9
10
  "doctor" => "Check your Flare setup for issues",
10
11
  "status" => "Show current Flare configuration",
11
12
  "requests" => "List recent requests from local development spans",
@@ -23,6 +24,9 @@ module Flare
23
24
  require_relative "cli/setup_command"
24
25
  force = argv.include?("--force")
25
26
  SetupCommand.new(force: force).run
27
+ when "connect"
28
+ require_relative "cli/ai_clients_setup"
29
+ exit 1 unless AIClientsSetup.new.run(ask: false)
26
30
  when "doctor"
27
31
  require_relative "cli/doctor_command"
28
32
  DoctorCommand.new.run
@@ -276,8 +276,8 @@ module Flare
276
276
 
277
277
  # List spans that have exception events
278
278
  def list_exception_spans(name: nil, sort: nil, limit: 50, offset: 0)
279
- conditions = []
280
- values = []
279
+ conditions = ["e.name = ?"]
280
+ values = ["exception"]
281
281
 
282
282
  if name
283
283
  conditions << "e.name LIKE ?"
@@ -315,8 +315,8 @@ module Flare
315
315
  end
316
316
 
317
317
  def count_exception_spans(name: nil)
318
- conditions = []
319
- values = []
318
+ conditions = ["e.name = ?"]
319
+ values = ["exception"]
320
320
 
321
321
  if name
322
322
  conditions << "e.name LIKE ?"
data/lib/flare/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Flare
4
- VERSION = "0.5.1"
4
+ VERSION = "0.5.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flare
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -192,6 +192,7 @@ files:
192
192
  - lib/flare.rb
193
193
  - lib/flare/backoff_policy.rb
194
194
  - lib/flare/cli.rb
195
+ - lib/flare/cli/ai_clients_setup.rb
195
196
  - lib/flare/cli/doctor_command.rb
196
197
  - lib/flare/cli/local_spans.rb
197
198
  - lib/flare/cli/nplus1_command.rb