agents_control 0.4.1 → 0.5.0
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 +4 -4
- data/README.md +33 -0
- data/exe/agents_control +10 -0
- data/lib/agents_control/agents/claude_code.rb +2 -2
- data/lib/agents_control/auto_reloader.rb +94 -0
- data/lib/agents_control/auto_updater.rb +113 -0
- data/lib/agents_control/channels/telegram/api.rb +62 -0
- data/lib/agents_control/channels/telegram/channel.rb +46 -3
- data/lib/agents_control/channels/telegram/keyboards.rb +17 -0
- data/lib/agents_control/channels/telegram/router.rb +252 -49
- data/lib/agents_control/cli.rb +25 -14
- data/lib/agents_control/config.rb +14 -0
- data/lib/agents_control/daemon.rb +59 -2
- data/lib/agents_control/dispatcher.rb +13 -0
- data/lib/agents_control/git_root.rb +31 -0
- data/lib/agents_control/inbox.rb +8 -5
- data/lib/agents_control/secrets.rb +1 -1
- data/lib/agents_control/store.rb +1 -1
- data/lib/agents_control/transcript.rb +25 -0
- data/lib/agents_control/version.rb +1 -1
- data/lib/agents_control.rb +3 -0
- metadata +4 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ab757dd52a8fe4081964ef265f682d6ee79732b1279652089237180f98f019c6
|
|
4
|
+
data.tar.gz: 1fdb05972ff015837ac038d15bf36038e877dd6acbfc8370286eda4ee0f4e96f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3cf360899987e55be7abcad6fa992da70c38fe7785a25a0aaeda7ce54750550feb3494336d853bb4bc39e3e09f0fa0ea4eae8f98ed452b6aff46ce8e10e6332b
|
|
7
|
+
data.tar.gz: 353db9ffb397cc37fac78209d9dc4636387eeaf55cf61ea376cd093116e36c8da50023a6524f2b7f9b6a8863d8d8a6ad5c49f84af5b4980342e0bd828ac7b580
|
data/README.md
CHANGED
|
@@ -203,6 +203,14 @@ name through in passing — and swept after `telegram.inbox_keep_days`
|
|
|
203
203
|
directory would otherwise only grow. Telegram caps what a bot can
|
|
204
204
|
download at 20MB; anything larger is refused before the round trip.
|
|
205
205
|
|
|
206
|
+
### Sending files out
|
|
207
|
+
|
|
208
|
+
`/getfile PATH` sends a file from this machine to the chat — a log, a
|
|
209
|
+
build artifact, anything on disk. `/getfile N` lists the N'th tab's two
|
|
210
|
+
most recently touched files (`Read`/`Edit`/`Write`/`NotebookEdit` in its
|
|
211
|
+
transcript) as buttons; tapping one sends it. With no argument, it uses
|
|
212
|
+
the tab the current conversation is already with.
|
|
213
|
+
|
|
206
214
|
### Two modes
|
|
207
215
|
|
|
208
216
|
This whole section is about Claude Code specifically — its hooks are
|
|
@@ -324,8 +332,33 @@ continuation itself once the time comes (with a minute of headroom).
|
|
|
324
332
|
```sh
|
|
325
333
|
agents_control doctor # is everything in place
|
|
326
334
|
agents_control service install # autostart (launchd / systemd)
|
|
335
|
+
agents_control reload # restart into updated code, same tab if there is one
|
|
327
336
|
```
|
|
328
337
|
|
|
338
|
+
`reload` (also `/reload` from the chat) finds the process holding the
|
|
339
|
+
hooks port and sends it HUP; the daemon traps that and `exec`s itself —
|
|
340
|
+
same process, same terminal tab, freshly loaded from disk. Useful right
|
|
341
|
+
after a `git pull` or a hand-edited file, instead of stopping and
|
|
342
|
+
starting it by hand.
|
|
343
|
+
|
|
344
|
+
It can also notice and act on its own, in two separate ways:
|
|
345
|
+
|
|
346
|
+
- **`daemon.auto_reload`** (on by default): if the local copy of the
|
|
347
|
+
tracked upstream branch has moved — someone, or some agent, already
|
|
348
|
+
ran `git pull` — it reloads into that code within a minute. A plain
|
|
349
|
+
local commit doesn't trigger this: only the upstream ref moving does,
|
|
350
|
+
which matters if the daemon is running from the same checkout it's
|
|
351
|
+
being developed in.
|
|
352
|
+
- **`daemon.auto_update`** (off by default): fetches on its own, once a
|
|
353
|
+
week (`daemon.update_check_interval`), and pulls if the checkout is
|
|
354
|
+
clean and fast-forwardable and the upstream commit has sat there for
|
|
355
|
+
at least 2 days (`daemon.update_delay`) — long enough that it isn't
|
|
356
|
+
the thing someone pushed five minutes ago. Runs `bundle install` if
|
|
357
|
+
needed, then reloads. This is the one that decides, unattended, to
|
|
358
|
+
trust unreviewed code — opt in explicitly if that's wanted.
|
|
359
|
+
|
|
360
|
+
Either way, a diverged or dirty working tree, or no tracked remote, is left alone.
|
|
361
|
+
|
|
329
362
|
`doctor` checks **the environment the daemon will actually get**, not
|
|
330
363
|
the current one: an interactive shell can show a different Ruby and a
|
|
331
364
|
different PATH than the process a service manager launches separately
|
data/exe/agents_control
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
2
|
# frozen_string_literal: true
|
|
3
3
|
|
|
4
|
+
# launchd, systemd and a self-exec reload have no interactive shell's
|
|
5
|
+
# gem setup to inherit — pin to vendor/bundle ourselves. Skipped for a
|
|
6
|
+
# real gem install, which has no Gemfile here and already resolved its
|
|
7
|
+
# dependencies at install time.
|
|
8
|
+
gemfile = File.expand_path("../Gemfile", __dir__)
|
|
9
|
+
if File.exist?(gemfile)
|
|
10
|
+
ENV["BUNDLE_GEMFILE"] ||= gemfile
|
|
11
|
+
require "bundler/setup"
|
|
12
|
+
end
|
|
13
|
+
|
|
4
14
|
require_relative "../lib/agents_control"
|
|
5
15
|
|
|
6
16
|
AgentsControl::CLI.start(ARGV)
|
|
@@ -39,7 +39,7 @@ module AgentsControl
|
|
|
39
39
|
def own_hook(path = settings_path)
|
|
40
40
|
return unless File.exist?(path)
|
|
41
41
|
|
|
42
|
-
groups = JSON.parse(File.read(path)).dig("hooks", "Stop")
|
|
42
|
+
groups = JSON.parse(File.read(path, encoding: "UTF-8")).dig("hooks", "Stop")
|
|
43
43
|
Array(groups).flat_map { |group| Array(group["hooks"]) }
|
|
44
44
|
.find { |hook| hook["_agents_control"] }
|
|
45
45
|
rescue JSON::ParserError
|
|
@@ -230,7 +230,7 @@ module AgentsControl
|
|
|
230
230
|
def read_settings
|
|
231
231
|
return {} unless File.exist?(@settings_path)
|
|
232
232
|
|
|
233
|
-
parsed = JSON.parse(File.read(@settings_path))
|
|
233
|
+
parsed = JSON.parse(File.read(@settings_path, encoding: "UTF-8"))
|
|
234
234
|
parsed.is_a?(Hash) ? parsed : {}
|
|
235
235
|
rescue JSON::ParserError
|
|
236
236
|
# A broken file that isn't ours to fix, but we definitely have
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AgentsControl
|
|
4
|
+
# Restarts into code that's already on disk — never fetched, just
|
|
5
|
+
# noticed. Something else (the person at a keyboard, an agent, a cron
|
|
6
|
+
# job) already made the call to `git pull`; this exists so that
|
|
7
|
+
# choice doesn't also require remembering to restart the daemon by
|
|
8
|
+
# hand. See AutoUpdater for the other half — going and fetching that
|
|
9
|
+
# code in the first place — which is a separate, more cautious call
|
|
10
|
+
# to make, gated by its own config key and a multi-day delay.
|
|
11
|
+
#
|
|
12
|
+
# Watches the upstream branch moving, not HEAD: this daemon often
|
|
13
|
+
# runs from the very checkout it's being developed in, where HEAD
|
|
14
|
+
# moves on every local commit — restarting on each of those would
|
|
15
|
+
# make iterating on this project itself unworkable. A pull is what
|
|
16
|
+
# moves the local copy of the upstream ref; a local commit never does.
|
|
17
|
+
#
|
|
18
|
+
# No delay here on purpose, unlike AutoUpdater: whoever pulled the
|
|
19
|
+
# code already decided to trust it today, not after it's sat
|
|
20
|
+
# untested for two days — that waiting period belongs to
|
|
21
|
+
# AutoUpdater's own, unattended fetch-and-merge.
|
|
22
|
+
class AutoReloader
|
|
23
|
+
def self.default_root = File.expand_path("../..", __dir__)
|
|
24
|
+
|
|
25
|
+
def initialize(root: self.class.default_root, interval: 60, config: nil,
|
|
26
|
+
executor: Executor.new, logger: nil, &on_update)
|
|
27
|
+
@git = GitRoot.new(root: root, executor: executor)
|
|
28
|
+
@interval = interval
|
|
29
|
+
@config = config
|
|
30
|
+
@logger = logger
|
|
31
|
+
@on_update = on_update
|
|
32
|
+
@running = false
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def start
|
|
36
|
+
return self unless enabled? && @on_update && @git.checkout?
|
|
37
|
+
|
|
38
|
+
@last_upstream = upstream
|
|
39
|
+
@running = true
|
|
40
|
+
@thread = Thread.new { tick while @running }
|
|
41
|
+
self
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def stop
|
|
45
|
+
@running = false
|
|
46
|
+
@thread&.kill
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def tick
|
|
50
|
+
restart_if_pulled if enabled?
|
|
51
|
+
rescue StandardError => e
|
|
52
|
+
log("failure: #{e.class}: #{e.message}")
|
|
53
|
+
ensure
|
|
54
|
+
sleep(@interval) if @running
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def enabled? = @config.nil? || @config.get("daemon.auto_reload", true)
|
|
60
|
+
|
|
61
|
+
# A pull moves the local copy of the upstream branch, not just
|
|
62
|
+
# HEAD — a local commit moves HEAD alone, leaving the upstream ref
|
|
63
|
+
# exactly where it was. Watching the upstream ref, not HEAD, is
|
|
64
|
+
# what tells "someone pulled" apart from "someone's mid-edit,
|
|
65
|
+
# committing in the very checkout this daemon runs from" — which
|
|
66
|
+
# is this project's own daily workflow, and must never trigger a restart.
|
|
67
|
+
def restart_if_pulled
|
|
68
|
+
current = upstream
|
|
69
|
+
moved = current && current != @last_upstream
|
|
70
|
+
@last_upstream = current
|
|
71
|
+
return unless moved && current == head
|
|
72
|
+
|
|
73
|
+
log("pulled to #{current.slice(0, 7)} — restarting into it")
|
|
74
|
+
@running = false
|
|
75
|
+
@on_update.call
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def upstream
|
|
79
|
+
result = @git.run("rev-parse", "@{u}", timeout: 10)
|
|
80
|
+
result.success? ? result.stdout.strip : nil
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def head
|
|
84
|
+
result = @git.run("rev-parse", "HEAD", timeout: 10)
|
|
85
|
+
result.success? ? result.stdout.strip : nil
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def log(message)
|
|
89
|
+
@logger&.puts("[auto-reload] #{message}")
|
|
90
|
+
rescue StandardError
|
|
91
|
+
nil
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AgentsControl
|
|
4
|
+
# Fetches a tracked git remote on a schedule and pulls + restarts
|
|
5
|
+
# into it — only when a plain `git pull --ff-only` would be the
|
|
6
|
+
# honest answer anyway: clean working tree, no diverged local
|
|
7
|
+
# history, and the upstream commit old enough that it isn't the
|
|
8
|
+
# latest thing pushed a minute ago. git only, never RubyGems — a gem
|
|
9
|
+
# install has no working tree to check for local edits against.
|
|
10
|
+
#
|
|
11
|
+
# Opt-in, and deliberately cautious about it: this is the one of the
|
|
12
|
+
# two that reaches out on its own and decides to trust whatever's
|
|
13
|
+
# newest upstream, unattended. AutoReloader is the other half —
|
|
14
|
+
# restarting into code someone already chose to pull themselves —
|
|
15
|
+
# and defaults to on, since it isn't making that same call on its own.
|
|
16
|
+
class AutoUpdater
|
|
17
|
+
def self.default_root = File.expand_path("../..", __dir__)
|
|
18
|
+
|
|
19
|
+
def initialize(root: self.class.default_root, interval: 7 * 86_400, delay: 2 * 86_400,
|
|
20
|
+
config: nil, executor: Executor.new, logger: nil, &on_update)
|
|
21
|
+
@root = root
|
|
22
|
+
@git = GitRoot.new(root: root, executor: executor)
|
|
23
|
+
@interval = interval
|
|
24
|
+
@delay = delay
|
|
25
|
+
@config = config
|
|
26
|
+
@executor = executor
|
|
27
|
+
@logger = logger
|
|
28
|
+
@on_update = on_update
|
|
29
|
+
@running = false
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def start
|
|
33
|
+
return self unless auto_update_enabled? && @on_update && @git.checkout?
|
|
34
|
+
|
|
35
|
+
@running = true
|
|
36
|
+
@thread = Thread.new { tick while @running }
|
|
37
|
+
self
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def stop
|
|
41
|
+
@running = false
|
|
42
|
+
@thread&.kill
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def tick
|
|
46
|
+
check_for_update if auto_update_enabled?
|
|
47
|
+
rescue StandardError => e
|
|
48
|
+
log("failure: #{e.class}: #{e.message}")
|
|
49
|
+
ensure
|
|
50
|
+
sleep(@interval) if @running
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def auto_update_enabled? = @config.nil? || @config.get("daemon.auto_update", false)
|
|
56
|
+
|
|
57
|
+
def check_for_update
|
|
58
|
+
return unless tracks_upstream? && working_tree_clean?
|
|
59
|
+
|
|
60
|
+
fetch = @git.run("fetch", "--quiet")
|
|
61
|
+
return log("couldn't fetch: #{fetch.stderr.strip}") unless fetch.success?
|
|
62
|
+
return unless fast_forwardable? && upstream_old_enough?
|
|
63
|
+
|
|
64
|
+
apply_update
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def tracks_upstream?
|
|
68
|
+
@git.run("rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}").success?
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def working_tree_clean? = @git.run("status", "--porcelain").stdout.strip.empty?
|
|
72
|
+
|
|
73
|
+
def fast_forwardable?
|
|
74
|
+
local = @git.run("rev-parse", "HEAD").stdout.strip
|
|
75
|
+
remote = @git.run("rev-parse", "@{u}").stdout.strip
|
|
76
|
+
return false if local.empty? || remote.empty? || local == remote
|
|
77
|
+
|
|
78
|
+
@git.run("merge-base", "--is-ancestor", "HEAD", "@{u}").success?
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def upstream_old_enough?
|
|
82
|
+
pushed_at = @git.run("log", "-1", "--format=%ct", "@{u}").stdout.strip
|
|
83
|
+
return false if pushed_at.empty?
|
|
84
|
+
|
|
85
|
+
(Time.now.to_i - pushed_at.to_i) >= @delay
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def apply_update
|
|
89
|
+
merge = @git.run("merge", "--ff-only", "@{u}")
|
|
90
|
+
return log("found an update but couldn't fast-forward: #{merge.stderr.strip}") unless merge.success?
|
|
91
|
+
return log("pulled an update but `bundle install` failed — not restarting into it") unless bundle_install
|
|
92
|
+
|
|
93
|
+
log("pulled an update — restarting into it")
|
|
94
|
+
@running = false
|
|
95
|
+
@on_update.call
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Best-effort: no bundler and no Gemfile both just mean there's
|
|
99
|
+
# nothing to install, same as succeeding.
|
|
100
|
+
def bundle_install
|
|
101
|
+
bundle = Which.find("bundle")
|
|
102
|
+
return true unless bundle && File.exist?(File.join(@root, "Gemfile"))
|
|
103
|
+
|
|
104
|
+
@executor.run(bundle, "install", "--gemfile", File.join(@root, "Gemfile"), timeout: 300).success?
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def log(message)
|
|
108
|
+
@logger&.puts("[auto-updater] #{message}")
|
|
109
|
+
rescue StandardError
|
|
110
|
+
nil
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -5,6 +5,7 @@ require "openssl"
|
|
|
5
5
|
require "timeout"
|
|
6
6
|
require "json"
|
|
7
7
|
require "uri"
|
|
8
|
+
require "securerandom"
|
|
8
9
|
|
|
9
10
|
module AgentsControl
|
|
10
11
|
module Channels
|
|
@@ -97,6 +98,23 @@ module AgentsControl
|
|
|
97
98
|
})
|
|
98
99
|
end
|
|
99
100
|
|
|
101
|
+
# Telegram's own cap on a bot's multipart upload.
|
|
102
|
+
MAX_UPLOAD_BYTES = 50 * 1024 * 1024
|
|
103
|
+
|
|
104
|
+
def send_document(chat_id:, path:, caption: nil)
|
|
105
|
+
size = File.size(path)
|
|
106
|
+
if size > MAX_UPLOAD_BYTES
|
|
107
|
+
raise Error, "sendDocument: #{File.basename(path)} is larger than Telegram allows a bot to upload"
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
response = @http.post_file(url_for("sendDocument"), compact({ chat_id: chat_id, caption: caption }),
|
|
111
|
+
field: "document", path: path, filename: File.basename(path),
|
|
112
|
+
read_timeout: DOWNLOAD_TIMEOUT)
|
|
113
|
+
parse(response, "sendDocument")
|
|
114
|
+
rescue Http::NetworkError => e
|
|
115
|
+
raise Unavailable, redact(e.message)
|
|
116
|
+
end
|
|
117
|
+
|
|
100
118
|
def edit_message_text(chat_id:, message_id:, text:, reply_markup: nil)
|
|
101
119
|
call("editMessageText", {
|
|
102
120
|
chat_id: chat_id, message_id: message_id, text: text,
|
|
@@ -242,6 +260,23 @@ module AgentsControl
|
|
|
242
260
|
raise NetworkError, e.message
|
|
243
261
|
end
|
|
244
262
|
|
|
263
|
+
# multipart/form-data, for endpoints that take an attached file
|
|
264
|
+
# rather than a URL or a file_id (sendDocument).
|
|
265
|
+
def post_file(uri, fields, field:, path:, filename:, read_timeout:)
|
|
266
|
+
# Random per call: a fixed boundary could appear inside the file's own bytes.
|
|
267
|
+
boundary = SecureRandom.hex(16)
|
|
268
|
+
request = Net::HTTP::Post.new(uri)
|
|
269
|
+
request["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
|
|
270
|
+
request.body = multipart_body(boundary, fields, field, path, filename)
|
|
271
|
+
|
|
272
|
+
Net::HTTP.start(uri.host, uri.port,
|
|
273
|
+
use_ssl: true, open_timeout: 10, read_timeout: read_timeout) do |http|
|
|
274
|
+
http.request(request)
|
|
275
|
+
end
|
|
276
|
+
rescue *NETWORK_ERRORS => e
|
|
277
|
+
raise NetworkError, e.message
|
|
278
|
+
end
|
|
279
|
+
|
|
245
280
|
# SystemCallError as a family rather than a list of Errno
|
|
246
281
|
# constants. The list was a bet on knowing every errno a network
|
|
247
282
|
# stack can raise, and it lost: Errno::EADDRNOTAVAIL — "can't
|
|
@@ -253,6 +288,33 @@ module AgentsControl
|
|
|
253
288
|
NETWORK_ERRORS = [
|
|
254
289
|
SystemCallError, SocketError, Timeout::Error, OpenSSL::SSL::SSLError, IOError
|
|
255
290
|
].freeze
|
|
291
|
+
|
|
292
|
+
private
|
|
293
|
+
|
|
294
|
+
CRLF = "\r\n"
|
|
295
|
+
|
|
296
|
+
# `.b` relabels each piece as binary without touching its bytes
|
|
297
|
+
# — appending a UTF-8 caption straight onto binary file bytes
|
|
298
|
+
# otherwise raises Encoding::CompatibilityError.
|
|
299
|
+
def multipart_body(boundary, fields, field, path, filename)
|
|
300
|
+
body = "".b
|
|
301
|
+
fields.each { |name, value| body << text_part(boundary, name, value) }
|
|
302
|
+
body << file_part(boundary, field, filename)
|
|
303
|
+
body << File.binread(path)
|
|
304
|
+
body << "#{CRLF}--#{boundary}--#{CRLF}".b
|
|
305
|
+
body
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
def text_part(boundary, name, value)
|
|
309
|
+
"--#{boundary}#{CRLF}Content-Disposition: form-data; name=\"#{name}\"#{CRLF}#{CRLF}#{value}#{CRLF}".b
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
def file_part(boundary, field, filename)
|
|
313
|
+
"--#{boundary}#{CRLF}Content-Disposition: form-data; name=\"#{field}\"; " \
|
|
314
|
+
"filename=\"#{escape_header_value(filename)}\"#{CRLF}Content-Type: application/octet-stream#{CRLF}#{CRLF}".b
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
def escape_header_value(filename) = filename.to_s.delete("\r\n").gsub('"', "'")
|
|
256
318
|
end
|
|
257
319
|
end
|
|
258
320
|
end
|
|
@@ -47,6 +47,24 @@ module AgentsControl
|
|
|
47
47
|
end
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
+
# Ties Claude's hook session_id (unrelated to any terminal) to
|
|
51
|
+
# the registry session actually running it, resolved once while
|
|
52
|
+
# unambiguous and trusted afterward — Router's exact-match
|
|
53
|
+
# lookup depends on this. Called for every event, including
|
|
54
|
+
# SessionStart, which Dispatcher otherwise keeps silent.
|
|
55
|
+
def remember_session(event)
|
|
56
|
+
return unless event.session_id && event.cwd
|
|
57
|
+
|
|
58
|
+
key = session_map_key(event.session_id)
|
|
59
|
+
return if @store.get(key)
|
|
60
|
+
|
|
61
|
+
session = resolve_session(event)
|
|
62
|
+
return unless session
|
|
63
|
+
|
|
64
|
+
@store.put(session.id, ttl: REPLY_TARGET_TTL, key: key)
|
|
65
|
+
remember_as_last(event, session) if event.kind == :started
|
|
66
|
+
end
|
|
67
|
+
|
|
50
68
|
private
|
|
51
69
|
|
|
52
70
|
def registry = @registry ||= Registry.new
|
|
@@ -135,13 +153,38 @@ module AgentsControl
|
|
|
135
153
|
id = sent.is_a?(Hash) ? sent["message_id"] : nil
|
|
136
154
|
return unless id
|
|
137
155
|
|
|
138
|
-
|
|
139
|
-
|
|
156
|
+
resolved_id = mapped_session_id(event)
|
|
157
|
+
target = { "session_id" => resolved_id || event.session_id,
|
|
158
|
+
"question_id" => question_id, "cwd" => event.cwd, "label" => event.label,
|
|
159
|
+
"from_agent" => true, "resolved" => !resolved_id.nil? }
|
|
140
160
|
@store.put(target, ttl: REPLY_TARGET_TTL, key: "reply:#{chat_id}:#{id}")
|
|
141
161
|
# Where a message sent without a reply goes — see the Router.
|
|
142
|
-
@store.put(target, ttl: REPLY_TARGET_TTL, key: "last:#{chat_id}")
|
|
162
|
+
@store.put(target, ttl: REPLY_TARGET_TTL, key: "last:#{chat_id}") unless downgrades_a_resolved_last?(chat_id, target)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# An unresolved event mustn't bump a "last" that already points
|
|
166
|
+
# at a real tab — an unrelated agent's own notification would
|
|
167
|
+
# otherwise knock the live conversation off course.
|
|
168
|
+
def downgrades_a_resolved_last?(chat_id, target)
|
|
169
|
+
return false if target["resolved"]
|
|
170
|
+
|
|
171
|
+
current = @store.get("last:#{chat_id}")
|
|
172
|
+
current && current["resolved"] != false
|
|
143
173
|
end
|
|
144
174
|
|
|
175
|
+
# Makes a freshly started session the target for Router's
|
|
176
|
+
# reply-less messages, with no notification needed to set it.
|
|
177
|
+
def remember_as_last(event, session)
|
|
178
|
+
target = { "session_id" => session.id, "question_id" => nil, "cwd" => event.cwd,
|
|
179
|
+
"label" => event.label, "from_agent" => true }
|
|
180
|
+
|
|
181
|
+
chats.each { |chat_id| @store.put(target, ttl: REPLY_TARGET_TTL, key: "last:#{chat_id}") }
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def mapped_session_id(event) = @store.get(session_map_key(event.session_id))
|
|
185
|
+
|
|
186
|
+
def session_map_key(session_id) = "agent_session:#{session_id}"
|
|
187
|
+
|
|
145
188
|
# A short session tag in the header. Needed so that in a chat
|
|
146
189
|
# with several agents it's clear which one a message belongs to
|
|
147
190
|
# — especially when they work in the same directory and the
|
|
@@ -62,6 +62,16 @@ module AgentsControl
|
|
|
62
62
|
]] }
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
+
# How many number buttons fit on one row before Telegram wraps it
|
|
66
|
+
# wider than a phone screen.
|
|
67
|
+
NUMBERS_PER_ROW = 5
|
|
68
|
+
|
|
69
|
+
# The same numbered list as render_list, but with the numbers
|
|
70
|
+
# also tappable — each one does what /focus N would.
|
|
71
|
+
def tabs_markup(sessions, universe: sessions)
|
|
72
|
+
{ inline_keyboard: focus_buttons(sessions, universe: universe) + list_actions.fetch(:inline_keyboard) }
|
|
73
|
+
end
|
|
74
|
+
|
|
65
75
|
# Buttons under /start and /help — the full command list isn't
|
|
66
76
|
# always visible in the Telegram UI (the menu button is easy to
|
|
67
77
|
# miss among everything else), so this keeps it available at any
|
|
@@ -104,6 +114,13 @@ module AgentsControl
|
|
|
104
114
|
|
|
105
115
|
private
|
|
106
116
|
|
|
117
|
+
def focus_buttons(sessions, universe: sessions)
|
|
118
|
+
numbers = numbered(universe)
|
|
119
|
+
sessions.each_slice(NUMBERS_PER_ROW).map do |group|
|
|
120
|
+
group.map { |session| button(numbers.fetch(session.id), action: "focus", session: session) }
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
107
124
|
def index_key(chat_id) = "index:#{chat_id}"
|
|
108
125
|
|
|
109
126
|
# session.id => "1".."N", in the order of the full session list.
|