samagotchi 0.192.0 → 0.194.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.
Files changed (190) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +128 -1
  3. data/docs/architecture.md +3 -3
  4. data/docs/bundle-authoring.md +1 -1
  5. data/docs/bundles.md +82 -40
  6. data/docs/cli.md +47 -17
  7. data/docs/configuration.md +75 -8
  8. data/docs/context.md +8 -5
  9. data/docs/desktop.md +3 -0
  10. data/docs/guardrails.md +20 -5
  11. data/docs/hooks.md +1 -0
  12. data/docs/internals/bridge.md +3 -1
  13. data/docs/internals/bundles.md +2 -2
  14. data/docs/internals/engine.md +46 -3
  15. data/docs/internals/pages.md +4 -4
  16. data/docs/internals/panes.md +66 -0
  17. data/docs/internals/participants.md +5 -3
  18. data/docs/internals/peers.md +176 -27
  19. data/docs/internals/prompt-caching.md +6 -1
  20. data/docs/internals/remote-sessions.md +6 -0
  21. data/docs/internals/session-lifecycle.md +12 -4
  22. data/docs/internals/web-frontend.md +106 -22
  23. data/docs/internals/working-set.md +5 -1
  24. data/docs/peers.md +150 -27
  25. data/docs/plugins.md +48 -7
  26. data/docs/releasing.md +30 -2
  27. data/docs/scheduling.md +3 -2
  28. data/docs/sessions.md +16 -7
  29. data/docs/sub-agent.md +6 -4
  30. data/docs/system-one.md +4 -0
  31. data/docs/testing.md +38 -3
  32. data/docs/thin-clients.md +27 -11
  33. data/lib/samagotchi/bridge/card_store.rb +1 -1
  34. data/lib/samagotchi/broadcast/triage_model.rb +17 -0
  35. data/lib/samagotchi/bundles/claude-peer/manifest.yml +8 -5
  36. data/lib/samagotchi/bundles/claude-peer/plugin.rb +24 -5
  37. data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +61 -24
  38. data/lib/samagotchi/bundles/claude-peer/scripts/pair +31 -8
  39. data/lib/samagotchi/bundles/codex-peer/manifest.yml +8 -5
  40. data/lib/samagotchi/bundles/codex-peer/plugin.rb +62 -6
  41. data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +57 -19
  42. data/lib/samagotchi/bundles/codex-peer/scripts/pair +28 -7
  43. data/lib/samagotchi/bundles/coordinator/manifest.yml +2 -2
  44. data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +31 -0
  45. data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
  46. data/lib/samagotchi/bundles/mcp/manifest.yml +2 -2
  47. data/lib/samagotchi/bundles/mcp/plugin.rb +0 -7
  48. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  49. data/lib/samagotchi/chi_invocation.rb +7 -0
  50. data/lib/samagotchi/client.rb +18 -0
  51. data/lib/samagotchi/codex_rollout.rb +93 -0
  52. data/lib/samagotchi/compat.rb +8 -1
  53. data/lib/samagotchi/config.rb +19 -6
  54. data/lib/samagotchi/continue_offer.rb +13 -1
  55. data/lib/samagotchi/delivery.rb +2 -2
  56. data/lib/samagotchi/effective_model.rb +28 -2
  57. data/lib/samagotchi/engine/cards.rb +9 -4
  58. data/lib/samagotchi/engine/hooks.rb +8 -0
  59. data/lib/samagotchi/engine/models.rb +197 -7
  60. data/lib/samagotchi/engine/plugins.rb +2 -0
  61. data/lib/samagotchi/engine/side_requests.rb +72 -13
  62. data/lib/samagotchi/engine/toolkit.rb +1 -1
  63. data/lib/samagotchi/engine/turn_helpers.rb +1 -1
  64. data/lib/samagotchi/engine.rb +11 -8
  65. data/lib/samagotchi/engine_snapshots.rb +16 -3
  66. data/lib/samagotchi/events.rb +1 -0
  67. data/lib/samagotchi/guardrails/pair_boundary.rb +49 -18
  68. data/lib/samagotchi/hooks.rb +3 -1
  69. data/lib/samagotchi/host_registry.rb +146 -16
  70. data/lib/samagotchi/idle_client.rb +13 -2
  71. data/lib/samagotchi/idle_recap.rb +33 -3
  72. data/lib/samagotchi/idle_target.rb +27 -7
  73. data/lib/samagotchi/jsonl_store.rb +13 -2
  74. data/lib/samagotchi/llm/adapter_common.rb +4 -2
  75. data/lib/samagotchi/llm/anthropic_messages.rb +14 -2
  76. data/lib/samagotchi/llm/errors.rb +35 -4
  77. data/lib/samagotchi/llm/model_listing.rb +19 -0
  78. data/lib/samagotchi/llm/openai_chat.rb +8 -2
  79. data/lib/samagotchi/llm/prompt_cache.rb +5 -0
  80. data/lib/samagotchi/model_binding.rb +46 -4
  81. data/lib/samagotchi/model_catalog.rb +68 -8
  82. data/lib/samagotchi/model_profile.rb +35 -0
  83. data/lib/samagotchi/models_command.rb +8 -4
  84. data/lib/samagotchi/pages/launch.rb +1 -1
  85. data/lib/samagotchi/panes/kitty.rb +122 -0
  86. data/lib/samagotchi/panes.rb +185 -0
  87. data/lib/samagotchi/peer_announce.rb +245 -0
  88. data/lib/samagotchi/peer_cold.rb +92 -0
  89. data/lib/samagotchi/peer_command.rb +121 -20
  90. data/lib/samagotchi/peer_continue.rb +42 -16
  91. data/lib/samagotchi/peer_exchanges.rb +7 -0
  92. data/lib/samagotchi/peer_mailbox.rb +50 -10
  93. data/lib/samagotchi/peer_pair_here.rb +149 -0
  94. data/lib/samagotchi/peer_presence.rb +434 -0
  95. data/lib/samagotchi/peer_receipts.rb +33 -6
  96. data/lib/samagotchi/peer_say.rb +46 -21
  97. data/lib/samagotchi/peer_wait_expiry.rb +47 -0
  98. data/lib/samagotchi/plugin/api.rb +13 -1
  99. data/lib/samagotchi/plugin/card.rb +68 -11
  100. data/lib/samagotchi/plugin/context.rb +33 -2
  101. data/lib/samagotchi/plugin/panes.rb +54 -0
  102. data/lib/samagotchi/plugin/peer.rb +170 -63
  103. data/lib/samagotchi/plugin/peer_pairing.rb +290 -0
  104. data/lib/samagotchi/plugin/peer_start.rb +301 -0
  105. data/lib/samagotchi/plugin/peer_start_hook.rb +207 -0
  106. data/lib/samagotchi/plugin/peers.rb +89 -9
  107. data/lib/samagotchi/recap_setup.rb +40 -3
  108. data/lib/samagotchi/self_report.rb +25 -6
  109. data/lib/samagotchi/send_command.rb +6 -6
  110. data/lib/samagotchi/served_lookup.rb +225 -0
  111. data/lib/samagotchi/served_ref.rb +41 -0
  112. data/lib/samagotchi/session_access/contract.rb +6 -0
  113. data/lib/samagotchi/session_access/local.rb +149 -40
  114. data/lib/samagotchi/session_access/remote.rb +4 -4
  115. data/lib/samagotchi/session_access/remote_peers.rb +8 -5
  116. data/lib/samagotchi/session_chain/links.rb +8 -3
  117. data/lib/samagotchi/session_commands.rb +40 -8
  118. data/lib/samagotchi/session_continue.rb +11 -6
  119. data/lib/samagotchi/session_lifecycle/allowed.rb +4 -2
  120. data/lib/samagotchi/session_manager/delivery.rb +5 -0
  121. data/lib/samagotchi/session_manager/lifecycle.rb +58 -7
  122. data/lib/samagotchi/session_manager/removal.rb +3 -0
  123. data/lib/samagotchi/session_manager/spawn.rb +6 -1
  124. data/lib/samagotchi/session_model_settings.rb +7 -3
  125. data/lib/samagotchi/session_stores.rb +25 -11
  126. data/lib/samagotchi/sessions_command.rb +16 -3
  127. data/lib/samagotchi/system_one/http_backend.rb +111 -10
  128. data/lib/samagotchi/system_one/runs.rb +5 -2
  129. data/lib/samagotchi/system_prompt.rb +11 -4
  130. data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -1
  131. data/lib/samagotchi/terminal_ui/formatting.rb +26 -12
  132. data/lib/samagotchi/terminal_ui/status_row.rb +20 -3
  133. data/lib/samagotchi/terminal_ui.rb +1 -1
  134. data/lib/samagotchi/testing/boundary.rb +0 -1
  135. data/lib/samagotchi/testing/bundle.rb +6 -2
  136. data/lib/samagotchi/testing/host.rb +1 -0
  137. data/lib/samagotchi/testing/recorder.rb +3 -0
  138. data/lib/samagotchi/turn_endings.rb +6 -2
  139. data/lib/samagotchi/user_typed.rb +29 -0
  140. data/lib/samagotchi/version.rb +1 -1
  141. data/lib/samagotchi/web/app.rb +8 -20
  142. data/lib/samagotchi/web/participant_review.rb +1 -1
  143. data/lib/samagotchi/web/participant_view.rb +1 -1
  144. data/lib/samagotchi/web/peer_routes.rb +40 -15
  145. data/lib/samagotchi/web/public/agent_rows.js +206 -0
  146. data/lib/samagotchi/web/public/agent_runs.js +595 -0
  147. data/lib/samagotchi/web/public/answer_fold.js +41 -44
  148. data/lib/samagotchi/web/public/app.js +4 -4
  149. data/lib/samagotchi/web/public/block_fix.js +1 -1
  150. data/lib/samagotchi/web/public/card.js +26 -10
  151. data/lib/samagotchi/web/public/cards_place.js +6 -4
  152. data/lib/samagotchi/web/public/context_chips.js +24 -2
  153. data/lib/samagotchi/web/public/copy.js +8 -2
  154. data/lib/samagotchi/web/public/data.js +2 -1
  155. data/lib/samagotchi/web/public/format.js +9 -1
  156. data/lib/samagotchi/web/public/history_reveal.js +5 -2
  157. data/lib/samagotchi/web/public/index.html +56 -2
  158. data/lib/samagotchi/web/public/info_bar_model.js +1 -1
  159. data/lib/samagotchi/web/public/model_pick.js +18 -5
  160. data/lib/samagotchi/web/public/model_picker.js +1 -1
  161. data/lib/samagotchi/web/public/page_view.js +1 -1
  162. data/lib/samagotchi/web/public/pages.css +1 -7
  163. data/lib/samagotchi/web/public/peer_chip.js +15 -5
  164. data/lib/samagotchi/web/public/peer_data.js +4 -2
  165. data/lib/samagotchi/web/public/peer_mailbox_api.js +2 -1
  166. data/lib/samagotchi/web/public/peer_note.js +4 -2
  167. data/lib/samagotchi/web/public/peer_row.js +37 -0
  168. data/lib/samagotchi/web/public/peer_wait.js +36 -0
  169. data/lib/samagotchi/web/public/quiet_notes.js +12 -1
  170. data/lib/samagotchi/web/public/revise_stack.js +5 -2
  171. data/lib/samagotchi/web/public/send_turn.js +11 -3
  172. data/lib/samagotchi/web/public/served_ref.js +9 -0
  173. data/lib/samagotchi/web/public/session_fields.js +1 -0
  174. data/lib/samagotchi/web/public/session_state.js +4 -0
  175. data/lib/samagotchi/web/public/stage_missing.js +23 -6
  176. data/lib/samagotchi/web/public/stream_handlers.js +16 -0
  177. data/lib/samagotchi/web/public/turn_block.js +4 -0
  178. data/lib/samagotchi/web/public/version_notices.js +7 -1
  179. data/lib/samagotchi/web/session_control.rb +19 -10
  180. data/lib/samagotchi/web/session_show.rb +20 -0
  181. data/lib/samagotchi/web/update_ops.rb +2 -2
  182. data/lib/samagotchi/worker/command_queue.rb +3 -0
  183. data/lib/samagotchi/worker/mail.rb +24 -1
  184. data/lib/samagotchi/worker.rb +8 -5
  185. data/lib/samagotchi/worker_inbound/mail.rb +34 -6
  186. data/lib/samagotchi/worker_inbound.rb +11 -3
  187. data/lib/samagotchi/worker_link.rb +13 -1
  188. data/lib/samagotchi/worker_wakes.rb +33 -5
  189. metadata +22 -2
  190. data/lib/samagotchi/web/public/pages_group.js +0 -67
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "peer_mailbox"
4
+ require_relative "peer_presence"
5
+
6
+ module Samagotchi
7
+ # The worker's check for a wait that ran out (/claude pair's mark,
8
+ # PeerPresence; owner decision 7): at the worker's start and on each idle
9
+ # pass (between turns, Worker#idle_pass), this session's marks past their
10
+ # 10 minutes are removed and, once per mark, +on_ended+ gets the peer: the
11
+ # Engine fires :peer_wait_ended, and the peer's bundle says "No Claude
12
+ # Code session came in 10 min; /claude pair to wait again." (ctx.notify:
13
+ # a :hook_notice kept in cards.json; never a message, no turn). A restart
14
+ # within or after the 10 minutes is the same check. Only this worker
15
+ # removes a timed-out mark (its unpair and its delete too): every other
16
+ # reader treats it as absent, so the line isn't lost. A mark of a session
17
+ # paired meanwhile, or of a peer no loaded bundle declares, goes without
18
+ # a word.
19
+ class PeerWaitExpiry
20
+ # The folder is read at most this often (the pass runs on every wake).
21
+ EVERY_SECONDS = 5
22
+
23
+ # @param registry [PeerRegistry] the peers the loaded bundles declared
24
+ # @param on_ended [#call] gets the peer's name
25
+ def initialize(session_id:, state_dir:, registry:, on_ended:)
26
+ @session_id = session_id
27
+ @state_dir = state_dir
28
+ @registry = registry
29
+ @on_ended = on_ended
30
+ @looked_at = nil
31
+ end
32
+
33
+ def call(now: Time.now)
34
+ return if @looked_at && now - @looked_at < EVERY_SECONDS
35
+
36
+ @looked_at = now
37
+ PeerPresence.session_marks(@session_id, state_dir: @state_dir).each do |peer, mark|
38
+ next unless mark.stale?(now)
39
+ next unless PeerPresence.stop_waiting(peer, @session_id, state_dir: @state_dir)
40
+ next if PeerMailbox.link(@session_id, peer, state_dir: @state_dir)
41
+ next unless @registry&.declared?(peer)
42
+
43
+ @on_ended.call(peer)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -62,7 +62,19 @@ module Samagotchi
62
62
  "ctx.sign_in" => "0.183.0",
63
63
  "ctx.card(between_turns:)" => "0.183.0",
64
64
  "chi.peer" => "0.188.0",
65
- "ctx.peers" => "0.188.0"
65
+ "ctx.peers" => "0.188.0",
66
+ "ctx.turn_interactive?" => "0.193.0",
67
+ "ctx.card(rows:)" => "0.194.0",
68
+ "ctx.peers.pair" => "0.194.0",
69
+ "ctx.peers.unpair" => "0.194.0",
70
+ "ctx.peers.here" => "0.194.0",
71
+ "ctx.peers.wait" => "0.194.0",
72
+ "ctx.peers.waiting" => "0.194.0",
73
+ "ctx.peers.claim" => "0.194.0",
74
+ "ctx.peers.release" => "0.194.0",
75
+ "ctx.peers.stop_waiting" => "0.194.0",
76
+ "ctx.panes" => "0.194.0",
77
+ "ctx.user_typed" => "0.194.0"
66
78
  }.freeze
67
79
 
68
80
  # chi.on's on_error: values. :log (the default) logs a raise and goes
@@ -10,39 +10,96 @@ module Samagotchi
10
10
  module Card
11
11
  LEVELS = %i[info warn].freeze
12
12
  MAX_ACTIONS = 6
13
+ # Rows (a list to pick from: claude-peer's Claude Code sessions): each
14
+ # a title and a sub line, with up to ROW_ACTIONS buttons of its own,
15
+ # the actions that name it (row: its index). They count apart from
16
+ # the card's own MAX_ACTIONS.
17
+ MAX_ROWS = 8
18
+ ROW_ACTIONS = 3
19
+ ROW_TITLE_CHARS = 200
20
+ ROW_SUB_CHARS = 300
13
21
  # An authorize address can be longer than a ref's (RefAction::MAX_URL).
14
22
  MAX_URL = 4_096
15
23
 
16
24
  module_function
17
25
 
18
26
  # @return [Hash] {type: :card, id:, source:, title:, body:, level:,
19
- # actions: [{label:, command:} | {label:, url:}]}; a new id when none
20
- # is given. A +url+ action opens that http(s) address in a new tab
21
- # (the web) or prints it (the TUI); it never sends to the session.
22
- # @raise [ArgumentError] a card without a title, a bad level or action
23
- def build(source:, title:, body:, actions:, level:, id:)
27
+ # actions: [{label:, command:} | {label:, url:}], rows: (only when
28
+ # given) [{title:, sub:}]}; a new id when none is given. A +url+
29
+ # action opens that http(s) address in a new tab (the web) or prints
30
+ # it (the TUI); it never sends to the session. An action with
31
+ # +row:+ (an index into +rows+) is that row's button; +primary:+
32
+ # true draws it solid in the web.
33
+ # @raise [ArgumentError] a card without a title, a bad level, action or row
34
+ def build(source:, title:, body:, actions:, level:, id:, rows: nil)
24
35
  title = title.to_s.strip
25
36
  raise ArgumentError, "a card needs a title" if title.empty?
26
37
 
27
38
  level = level.to_s.to_sym
28
39
  raise ArgumentError, "card level must be one of #{LEVELS.join(", ")}" unless LEVELS.include?(level)
29
40
 
30
- actions = Array(actions)
31
- raise ArgumentError, "a card has at most #{MAX_ACTIONS} actions" if actions.size > MAX_ACTIONS
41
+ rows = rows.nil? ? nil : build_rows(rows)
42
+ actions = Array(actions).map { |action| build_action(action, rows: rows || []) }
43
+ own = actions.count { |action| !action.key?(:row) }
44
+ raise ArgumentError, "a card has at most #{MAX_ACTIONS} actions" if own > MAX_ACTIONS
45
+ if actions.filter_map { |action| action[:row] }.tally.values.any? { |count| count > ROW_ACTIONS }
46
+ raise ArgumentError, "a card has at most #{ROW_ACTIONS} actions in a row"
47
+ end
32
48
 
33
- actions = actions.map { |action| build_action(action) }
34
49
  id = id.to_s.strip
35
- { type: :card, id: id.empty? ? SecureRandom.hex(4) : id, source: source.to_s, title: title, body: body.to_s,
36
- level: level, actions: actions }
50
+ card = { type: :card, id: id.empty? ? SecureRandom.hex(4) : id, source: source.to_s, title: title, body: body.to_s,
51
+ level: level, actions: actions }
52
+ rows ? card.merge(rows: rows) : card
53
+ end
54
+
55
+ # @raise [ArgumentError] not an Array of {title:, sub:} Hashes, more
56
+ # than MAX_ROWS, a row with neither
57
+ def build_rows(rows)
58
+ raise ArgumentError, "a card's rows are an Array" unless rows.is_a?(Array)
59
+ raise ArgumentError, "a card has at most #{MAX_ROWS} rows" if rows.size > MAX_ROWS
60
+
61
+ rows.map do |row|
62
+ raise ArgumentError, "a card row is a Hash {title:, sub:}" unless row.is_a?(Hash)
63
+
64
+ row = row.transform_keys(&:to_sym)
65
+ title = one_line(row[:title], ROW_TITLE_CHARS)
66
+ sub = one_line(row[:sub], ROW_SUB_CHARS)
67
+ raise ArgumentError, "a row needs a title or a sub line" if title.empty? && sub.empty?
68
+
69
+ { title: title, sub: sub }
70
+ end
37
71
  end
38
72
 
73
+ # Control characters out (a row's text may come from a file any local
74
+ # process writes, and the TUI prints it), whitespace collapsed.
75
+ def one_line(text, max) = text.to_s.gsub(/[[:cntrl:]]/, " ").gsub(/\s+/, " ").strip[0, max]
76
+
39
77
  # @raise [ArgumentError] not a Hash, both or neither of command / url,
40
78
  # a bad command, a url that isn't http(s) with a host (no userinfo,
41
79
  # at most MAX_URL characters), a url action without a label
42
- def build_action(action)
80
+ def build_action(action, rows: [])
43
81
  raise ArgumentError, "a card action is a Hash {label:, command:} or {label:, url:}" unless action.is_a?(Hash)
44
82
 
45
83
  action = action.transform_keys(&:to_sym)
84
+ marks = action_marks(action, rows)
85
+ plain_action(action).merge(marks)
86
+ end
87
+
88
+ # An action's +row+ (checked against +rows+, ROW_ACTIONS a row) and
89
+ # +primary+, as given.
90
+ def action_marks(action, rows)
91
+ marks = {}
92
+ if action.key?(:row) && !action[:row].nil?
93
+ row = action[:row]
94
+ raise ArgumentError, "a card action's row #{row.inspect} isn't one of the card's rows" unless row.is_a?(Integer) && row.between?(0, rows.size - 1)
95
+
96
+ marks[:row] = row
97
+ end
98
+ marks[:primary] = true if action[:primary] == true
99
+ marks
100
+ end
101
+
102
+ def plain_action(action)
46
103
  label = action[:label].to_s.strip
47
104
  has_command = action.key?(:command) && !action[:command].nil?
48
105
  has_url = action.key?(:url) && !action[:url].nil?
@@ -10,6 +10,7 @@ require_relative "../hook_question"
10
10
  require_relative "../idle_client"
11
11
  require_relative "../tool_ids"
12
12
  require_relative "../cache_confirm"
13
+ require_relative "../user_typed"
13
14
  require_relative "service"
14
15
  require_relative "side_question"
15
16
 
@@ -31,6 +32,7 @@ module Samagotchi
31
32
  autoload :Sessions, File.expand_path("sessions", __dir__)
32
33
  autoload :AttachedContext, File.expand_path("attached_context", __dir__)
33
34
  autoload :Peers, File.expand_path("peers", __dir__)
35
+ autoload :Panes, File.expand_path("panes", __dir__)
34
36
  # ctx.http's file needs only json and uri (net/http loads with the first
35
37
  # real request); autoloaded so a bundle can name HttpError at load time.
36
38
  autoload :Http, File.expand_path("http", __dir__)
@@ -85,12 +87,14 @@ module Samagotchi
85
87
  # +sign_in+ takes (bundle, title:, button:, timeout:, port:, path:, cancel:) and
86
88
  # the block that builds the address, and returns the SignIn flow
87
89
  # (Engine#start_sign_in; the test kit's opens no port).
90
+ # +turn_interactive+ returns whether the running turn came from a screen
91
+ # that can answer a question (ClientId.interactive?: a web tab, a TUI).
88
92
  Host = Struct.new(:session_id, :cwd, :messages, :messages_partial, :notify, :ask_user, :cancelled, :card,
89
93
  :ask_model, :model_name, :model_key, :state_dir, :scratch, :setup, :prompt_files,
90
94
  :steer, :stop_turn, :stop_generation, :frontend, :toolkit, :declared_tools, :use_toolkit, :load_tools,
91
95
  :cancel_controller, :cost, :usage, :turn_author, :cache_confirm, :model_names,
92
96
  :candidate_proposed, :judgments, :turn_running, :current_cancel, :secrets_store,
93
- :sign_in, keyword_init: true)
97
+ :sign_in, :turn_interactive, keyword_init: true)
94
98
 
95
99
  # ctx.ask_model failed: the model couldn't be reached, timed out, or
96
100
  # sent nothing usable. The message says why, for the user.
@@ -230,6 +234,16 @@ module Samagotchi
230
234
  # power whatever the author (docs/hooks.md).
231
235
  def turn_author = @host.turn_author&.call
232
236
 
237
+ # @return [Boolean] whether the running turn was sent from a screen
238
+ # that shows a question and can answer it now: the owner's web tab or
239
+ # TUI (ClientId.interactive?; their first prompt and a Continue tap on
240
+ # such a turn too), and a thin client's POST …/ask (web:unnamed),
241
+ # which answers through …/answer. False between turns and for every other
242
+ # turn: chi send's, a schedule's, a delegate's, a wake turn (a peer's,
243
+ # a task's, attached context's), a participant's, a one-shot run. A
244
+ # ctx.ask_user in such a turn would wait with nobody there to answer.
245
+ def turn_interactive? = @host.turn_interactive&.call == true
246
+
233
247
  # @return [String, nil] the name of the session's toolkit ("full" for
234
248
  # one from before toolkits; nil with no session)
235
249
  def toolkit = @host.toolkit&.call
@@ -283,6 +297,13 @@ module Samagotchi
283
297
  @host.cache_confirm&.call(line.to_s, changed ? true : false)
284
298
  end
285
299
 
300
+ # Whether the command running on this thread was typed by a person at
301
+ # a UI (the web, a card's button there, an attached TUI): never chi
302
+ # send (the model's own shell can run it), a tool call, a delegate or
303
+ # a participant. What acts as the user on their screen, or writes
304
+ # another agent's settings, runs only then (UserTyped).
305
+ def user_typed? = UserTyped.typed?
306
+
286
307
  # Whether the session is a `chi scratch` one (deleted when it ends).
287
308
  def scratch? = !!@host.scratch&.call
288
309
 
@@ -417,11 +438,15 @@ module Samagotchi
417
438
  # @param id [String, nil] an earlier card's id to replace it
418
439
  # @param between_turns [Boolean] never a running turn's: shown outside
419
440
  # the turn's block while one runs (from a thread of the plugin's own)
441
+ # @param rows [Array<Hash>, nil] {title:, sub:}: a list to pick from,
442
+ # each row's buttons the actions with its index as +row:+ (up to 3
443
+ # a row, apart from the card's own 6)
420
444
  # @return [String] the card's id
421
445
  # @raise [ArgumentError] no title, a bad level or action
422
- def card(title:, body: "", actions: [], level: :info, id: nil, between_turns: false)
446
+ def card(title:, body: "", actions: [], level: :info, id: nil, between_turns: false, rows: nil)
423
447
  shown = { source: @bundle, title: title, body: body, actions: actions, level: level, id: id }
424
448
  shown[:between_turns] = true if between_turns
449
+ shown[:rows] = rows unless rows.nil?
425
450
  @host.card.call(**shown)
426
451
  end
427
452
 
@@ -576,6 +601,12 @@ module Samagotchi
576
601
  @context ||= AttachedContext.new(@host, bundle: @bundle)
577
602
  end
578
603
 
604
+ # The panes where agents' own UIs run (Samagotchi::Panes: kitty
605
+ # windows), read now from the config. #show moves the user's screen,
606
+ # so it runs only in a command they typed (#user_typed?).
607
+ # @return [Panes]
608
+ def panes = Panes.new
609
+
579
610
  # This session's peers: a pairing and a peer's messages (Peers).
580
611
  # @return [Peers]
581
612
  def peers
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../panes"
4
+ require_relative "../user_typed"
5
+
6
+ module Samagotchi
7
+ module Plugin
8
+ # ctx.panes (docs/plugins.md, Panes): the kitty windows agents run in
9
+ # (Samagotchi::Panes, its config read at each call). #find a pid's
10
+ # window and its title; #alive? whether a window #open made is still
11
+ # there. #show brings one to the front, #open opens a background tab
12
+ # and #paste types into one: they act as the user on their screen, so
13
+ # only in a command a person typed at a UI (UserTyped: never a tool
14
+ # call or chi send).
15
+ class Panes
16
+ # Show, open or paste from anything but a command the user typed.
17
+ class NotTyped < StandardError; end
18
+
19
+ # @return [Samagotchi::Panes::Pane, nil] the window +pid+ runs in
20
+ def find(pid:) = Samagotchi::Panes.from_config.find(pid: pid)
21
+
22
+ # @return [Boolean] whether kitty showed it (false: the window is gone)
23
+ # @raise [NotTyped] not in a command the user typed
24
+ def show(pane)
25
+ raise NotTyped, "Show brings a window to the front, so it runs only when you tap it or type it" unless UserTyped.typed?
26
+
27
+ Samagotchi::Panes.from_config.show(pane)
28
+ end
29
+
30
+ # A background kitty tab running +command+ (argv) in +cwd+, titled
31
+ # +title+.
32
+ # @return [Samagotchi::Panes::PaneRef]
33
+ # @raise [NotTyped] not in a command the user typed
34
+ # @raise [Samagotchi::Panes::Error] no kitty answers (Unavailable), or it didn't open it
35
+ def open(command:, cwd:, title:)
36
+ raise NotTyped, "it opens a kitty tab on your screen, so it runs only when you tap it or type it" unless UserTyped.typed?
37
+
38
+ Samagotchi::Panes.from_config.open(command: command, cwd: cwd, title: title)
39
+ end
40
+
41
+ # Whether +ref+'s window is still there.
42
+ def alive?(ref) = Samagotchi::Panes.from_config.alive?(ref)
43
+
44
+ # Pastes +text+ into +ref+'s window, then Enter when +enter+.
45
+ # @return [Boolean] whether kitty took it (false: the window is gone)
46
+ # @raise [NotTyped] not in a command the user typed
47
+ def paste(ref, text, enter: true)
48
+ raise NotTyped, "it types into a window on your screen, so it runs only when you tap it or type it" unless UserTyped.typed?
49
+
50
+ Samagotchi::Panes.from_config.paste(ref, text, enter: enter)
51
+ end
52
+ end
53
+ end
54
+ end