samagotchi 0.195.0 → 0.196.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 (97) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +41 -1
  3. data/docs/architecture.md +2 -1
  4. data/docs/bundle-authoring.md +23 -23
  5. data/docs/bundles.md +11 -3
  6. data/docs/cli.md +4 -0
  7. data/docs/context.md +13 -3
  8. data/docs/guardrails.md +6 -4
  9. data/docs/hooks.md +3 -1
  10. data/docs/internals/block-renderers.md +4 -1
  11. data/docs/internals/engine.md +2 -1
  12. data/docs/internals/knowledge.md +15 -5
  13. data/docs/internals/participants.md +15 -3
  14. data/docs/internals/peers.md +12 -8
  15. data/docs/internals/session-lifecycle.md +26 -12
  16. data/docs/internals/web-frontend.md +64 -27
  17. data/docs/pages.md +1 -1
  18. data/docs/peers.md +1 -1
  19. data/docs/plugins.md +73 -3
  20. data/docs/sessions.md +1 -1
  21. data/docs/system-one.md +3 -1
  22. data/lib/samagotchi/bundle_manifest.rb +30 -5
  23. data/lib/samagotchi/bundles/core/manifest.yml +2 -2
  24. data/lib/samagotchi/bundles/cost-cap/manifest.yml +4 -4
  25. data/lib/samagotchi/bundles/cost-cap/plugin.rb +54 -3
  26. data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
  27. data/lib/samagotchi/bundles/github-pr/manifest.yml +4 -2
  28. data/lib/samagotchi/bundles/knowledge/chi_adapters.rb +43 -25
  29. data/lib/samagotchi/bundles/knowledge/manifest.yml +6 -4
  30. data/lib/samagotchi/bundles/knowledge/plugin.rb +37 -18
  31. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  32. data/lib/samagotchi/context_change.rb +44 -0
  33. data/lib/samagotchi/context_note.rb +1 -1
  34. data/lib/samagotchi/context_providers.rb +14 -0
  35. data/lib/samagotchi/context_sources.rb +3 -25
  36. data/lib/samagotchi/engine/plugins.rb +21 -0
  37. data/lib/samagotchi/events.rb +1 -0
  38. data/lib/samagotchi/guardrails/pair_boundary.rb +12 -4
  39. data/lib/samagotchi/guardrails/targets.rb +24 -2
  40. data/lib/samagotchi/installed_bundles/manifest.rb +7 -1
  41. data/lib/samagotchi/memory_bundle/builder.rb +2 -1
  42. data/lib/samagotchi/memory_bundle/installed_copy.rb +2 -1
  43. data/lib/samagotchi/participants.rb +17 -6
  44. data/lib/samagotchi/peer_exchanges.rb +1 -2
  45. data/lib/samagotchi/peer_note.rb +38 -5
  46. data/lib/samagotchi/peer_presence.rb +20 -7
  47. data/lib/samagotchi/plugin/api.rb +5 -1
  48. data/lib/samagotchi/plugin/chips.rb +125 -0
  49. data/lib/samagotchi/plugin/context.rb +19 -1
  50. data/lib/samagotchi/plugin/peer_pairing.rb +2 -2
  51. data/lib/samagotchi/plugin/peer_start.rb +1 -1
  52. data/lib/samagotchi/plugin/peers.rb +5 -2
  53. data/lib/samagotchi/plugin/tool_spec.rb +19 -9
  54. data/lib/samagotchi/schedules.rb +8 -2
  55. data/lib/samagotchi/session_access/local.rb +34 -33
  56. data/lib/samagotchi/session_chain/sharing.rb +79 -0
  57. data/lib/samagotchi/session_chain/successor.rb +4 -2
  58. data/lib/samagotchi/session_chips.rb +133 -0
  59. data/lib/samagotchi/session_inbox_delivery.rb +11 -11
  60. data/lib/samagotchi/session_manager/emptiness.rb +4 -1
  61. data/lib/samagotchi/session_observer.rb +1 -1
  62. data/lib/samagotchi/session_stats.rb +5 -4
  63. data/lib/samagotchi/session_stores.rb +3 -0
  64. data/lib/samagotchi/speaker.rb +228 -0
  65. data/lib/samagotchi/steer.rb +36 -132
  66. data/lib/samagotchi/testing/bundle.rb +5 -0
  67. data/lib/samagotchi/testing/host.rb +1 -0
  68. data/lib/samagotchi/testing/recorder.rb +10 -0
  69. data/lib/samagotchi/tools/registry.rb +8 -3
  70. data/lib/samagotchi/turn_excerpts.rb +8 -4
  71. data/lib/samagotchi/version.rb +1 -1
  72. data/lib/samagotchi/web/app.rb +21 -10
  73. data/lib/samagotchi/web/participant_view.rb +1 -1
  74. data/lib/samagotchi/web/peer_routes.rb +5 -2
  75. data/lib/samagotchi/web/plugin_chip_rows.rb +39 -0
  76. data/lib/samagotchi/web/public/agent_rows.js +1 -0
  77. data/lib/samagotchi/web/public/app.js +17 -25
  78. data/lib/samagotchi/web/public/chains.js +0 -26
  79. data/lib/samagotchi/web/public/command_list.js +2 -3
  80. data/lib/samagotchi/web/public/composer.js +2 -3
  81. data/lib/samagotchi/web/public/composer_mount.js +3 -6
  82. data/lib/samagotchi/web/public/context_chips.js +240 -93
  83. data/lib/samagotchi/web/public/context_groups.js +84 -0
  84. data/lib/samagotchi/web/public/draft_follow.js +33 -0
  85. data/lib/samagotchi/web/public/drafts.js +26 -22
  86. data/lib/samagotchi/web/public/index.html +32 -0
  87. data/lib/samagotchi/web/public/peer_chip_kind.js +49 -0
  88. data/lib/samagotchi/web/public/peer_note.js +2 -2
  89. data/lib/samagotchi/web/public/peer_row.js +10 -5
  90. data/lib/samagotchi/web/public/peer_wait.js +4 -2
  91. data/lib/samagotchi/web/public/plugin_chip.js +55 -0
  92. data/lib/samagotchi/web/public/plugin_chip_kind.js +48 -0
  93. data/lib/samagotchi/web/public/send_turn.js +8 -21
  94. data/lib/samagotchi/web/public/session_actions.js +10 -23
  95. data/lib/samagotchi/web/public/session_view.js +7 -3
  96. data/lib/samagotchi/web/public/stream_handlers.js +2 -0
  97. metadata +12 -1
@@ -0,0 +1,228 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "client_id"
4
+
5
+ module Samagotchi
6
+ # Who wrote a saved message, and what kind of line it is: the reads Steer's
7
+ # wire code (headers, the forged-frame quoting) and every reader of a
8
+ # history (a recap, the page's turns, the knowledge index) share. Pure
9
+ # reads of one message hash, symbol or string keys alike; it writes
10
+ # nothing and adds no bytes to the wire (Steer does).
11
+ #
12
+ # .of answers with one symbol: :owner (the session's owner typed it),
13
+ # :participant (an invited participant), :chi (a line chi wrote in the
14
+ # user's place: a note, a reminder, a `!cmd` line, KernelLoop's recovery),
15
+ # :sender (another agent, a parent, chi send, a client chi doesn't know),
16
+ # :plugin (a plugin's steer or send), :model or :tool; nil for a non-Hash.
17
+ # Bundles read it through ctx.speaker; the message's keys stay chi's own.
18
+ #
19
+ # A message someone other than the session's owner wrote (a participant:
20
+ # author "p:<id>", Web::Principal#author) carries author: and
21
+ # author_name: (the name when it was written); its opening message also
22
+ # carries participant_mode: (the rules when it was delivered). The
23
+ # owner's messages carry none of these keys.
24
+ module Speaker
25
+ KIND = "steer"
26
+ INPUT_KIND = "input"
27
+ # KernelLoop's line asking a Qwen model to finish the tool call it left
28
+ # open: a user line inside the running turn, the model's alone. No turn
29
+ # opens with it (#turn_prompt?), and the page draws nothing for it, as it
30
+ # showed nothing live. Its kind adds no header (Steer.header): its bytes go
31
+ # out as before it had one.
32
+ RECOVERY_KIND = "recovery"
33
+
34
+ DELEGATE_REPORT = "delegate_report"
35
+ # The source of a line from a client that is not a human's
36
+ # (ClientId.human?) and not one chi names: automatic:<client id>.
37
+ AUTOMATIC_SOURCE_PREFIX = "automatic:"
38
+
39
+ # A participant's line's saved source (participant:<id>): never the user's,
40
+ # and never one that cuts (Steer::CUT_SOURCES). Its header is the review
41
+ # one, by its author.
42
+ PARTICIPANT_SOURCE = "participant"
43
+ # The owner's author, when one is written out; absent means the same.
44
+ OWNER_AUTHOR = "owner"
45
+ # A participant's author: this prefix, then its id (Grants'
46
+ # grant id), "p:<id>" (#author_for, #participant_id).
47
+ PARTICIPANT_PREFIX = "p:"
48
+ # A participant's message's keys, which every message copy keeps.
49
+ AUTHOR_KEYS = %i[author author_name participant_mode].freeze
50
+
51
+ # The sources of a steer a person sent (a continue card's text, from the
52
+ # user or a parent agent's chi answer), not a plugin's prod.
53
+ PERSON_SOURCES = %w[user parent_agent].freeze
54
+
55
+ # A steer's sources that are the owner's (Steer.header reads "" as the user).
56
+ OWNER_STEER_SOURCES = ["", "user"].freeze
57
+ # A steer's or an input's sources that are another sender's: a parent agent
58
+ # and chi send by name, an agent, another session and an unknown client by
59
+ # prefix.
60
+ SENDER_SOURCES = %w[parent_agent chi_send].freeze
61
+ SENDER_PREFIXES = [ClientId::AGENT_PREFIX, ClientId::SESSION_PREFIX, AUTOMATIC_SOURCE_PREFIX].freeze
62
+ # A plugin's sends (ClientId's plugin row): an input's or an opener's source.
63
+ PLUGIN_SEND = "plugin_send"
64
+
65
+ module_function
66
+
67
+ # Who wrote +message+ (the module comment's symbols); nil for a non-Hash.
68
+ # A user message is a participant's by its author (any kind), or by a
69
+ # participant source; else by its kind and source. A shell line and
70
+ # /continue no's reason line are chi's only when they have no kind.
71
+ # @return [Symbol, nil]
72
+ def of(message)
73
+ return nil unless message.is_a?(Hash)
74
+
75
+ case field(message, :role).to_s
76
+ when "user" then user_speaker(message)
77
+ when "model", "assistant" then :model
78
+ when "tool_response", "tool" then :tool
79
+ else :chi
80
+ end
81
+ end
82
+
83
+ def user_speaker(message)
84
+ source = field(message, :source).to_s
85
+ return :participant if participant_author?(field(message, :author))
86
+ return :participant if source == PARTICIPANT_SOURCE || source.start_with?(ClientId::PARTICIPANT_PREFIX)
87
+
88
+ case field(message, :kind).to_s
89
+ when "" then chi_line?(message) ? :chi : sent_by(source)
90
+ when INPUT_KIND then sent_by(source)
91
+ when KIND then steered_by(source)
92
+ else :chi
93
+ end
94
+ end
95
+
96
+ # An input's or opener's speaker by its +source+: none is the owner's.
97
+ def sent_by(source)
98
+ return :owner if source.empty?
99
+ return :plugin if source == PLUGIN_SEND
100
+
101
+ :sender
102
+ end
103
+
104
+ # A steer's speaker by its +source+: an unknown one is a plugin's (its
105
+ # bundle or hook label), as Steer.header reads it.
106
+ def steered_by(source)
107
+ return :owner if OWNER_STEER_SOURCES.include?(source)
108
+ return :sender if SENDER_SOURCES.include?(source) || SENDER_PREFIXES.any? { |prefix| source.start_with?(prefix) }
109
+
110
+ :plugin
111
+ end
112
+
113
+ # A kind-less user line chi wrote: a `!cmd` line, or /continue no's
114
+ # reason line (an opening_id, no turn_id: TurnFlow#abort_continue!).
115
+ def chi_line?(message)
116
+ shell_line?(message) || (field(message, :opening_id).is_a?(String) && field(message, :turn_id).nil?)
117
+ end
118
+
119
+ # Whether +author+ (an origin's or a message's) is someone other than
120
+ # the owner. Any author but none or "owner" is: an author only narrows.
121
+ def participant_author?(author)
122
+ !author.nil? && !author.to_s.empty? && author.to_s != OWNER_AUTHOR
123
+ end
124
+
125
+ # A user message a participant wrote.
126
+ def participant_message?(message)
127
+ message.is_a?(Hash) && (message[:role] || message["role"]).to_s == "user" &&
128
+ participant_author?(message[:author] || message["author"])
129
+ end
130
+
131
+ # Who wrote the turn +messages+[+index+] belongs to: a user message's
132
+ # own author; an assistant, tool or system message's is its turn's, the
133
+ # nearest turn-opening message before it (#turn_prompt?). nil for the
134
+ # owner (and before any turn).
135
+ def author_at(messages, index)
136
+ list = Array(messages)
137
+ message = list[index]
138
+ return nil unless message.is_a?(Hash)
139
+
140
+ own = message[:author] || message["author"]
141
+ return sender_author(own) if (message[:role] || message["role"]).to_s == "user"
142
+
143
+ opener = list[0...index].rindex { |m| turn_prompt?(m) }
144
+ opener ? sender_author(list[opener][:author] || list[opener]["author"]) : nil
145
+ end
146
+
147
+ # +author+ when it is a participant's, else nil (the owner).
148
+ def sender_author(author) = participant_author?(author) ? author.to_s : nil
149
+
150
+ # The author a participant's turns and messages carry: "p:<id>".
151
+ def author_for(participant_id) = "#{PARTICIPANT_PREFIX}#{participant_id}"
152
+
153
+ # The participant id in +author+ ("p:<id>" → "<id>"); a string without
154
+ # the prefix as it is ("" for nil).
155
+ def participant_id(author) = author.to_s.delete_prefix(PARTICIPANT_PREFIX)
156
+
157
+ def steer?(message)
158
+ (message[:kind] || message["kind"]).to_s == KIND
159
+ end
160
+
161
+ # A steer a person sent: what a recap and a side question keep.
162
+ def person?(message)
163
+ steer?(message) && PERSON_SOURCES.include?((message[:source] || message["source"]).to_s)
164
+ end
165
+
166
+ # A user message that is a prompt or the user's steering, not a steer:
167
+ # what "the last prompt" and "user turns" count.
168
+ def prompt?(message)
169
+ message.is_a?(Hash) && (message[:role] || message["role"]).to_s == "user" && !steer?(message)
170
+ end
171
+
172
+ # KernelLoop's recovery line (RECOVERY_KIND).
173
+ def recovery?(message)
174
+ message.is_a?(Hash) && (message[:kind] || message["kind"]).to_s == RECOVERY_KIND
175
+ end
176
+
177
+ # The user's lines merged into a running turn (Steer::Merge#messages).
178
+ def input?(message)
179
+ message.is_a?(Hash) && (message[:kind] || message["kind"]).to_s == INPUT_KIND
180
+ end
181
+
182
+ # A prompt that started a turn: not a steer, not input merged into a
183
+ # running turn, except the input a turn with no prompt starts with (a
184
+ # wake turn's delegate report, marked turn_start); also the context note
185
+ # a context wake turn starts with (role system, marked turn_start). What
186
+ # the turns a page shows count.
187
+ def turn_prompt?(message)
188
+ return false unless message.is_a?(Hash)
189
+
190
+ start = (message[:turn_start] || message["turn_start"]) == true
191
+ return start if (message[:role] || message["role"]).to_s == "system"
192
+
193
+ prompt?(message) && !recovery?(message) && (!input?(message) || start)
194
+ end
195
+
196
+ # A stored `!shell` line: "!(cmd)\n<output>", no turn id. A user message,
197
+ # so #turn_prompt? takes it; no turn ran.
198
+ def shell_line?(message)
199
+ return false unless message.is_a?(Hash)
200
+
201
+ content = message[:content] || message["content"]
202
+ (message[:turn_id] || message["turn_id"]).nil? && content.is_a?(String) && content.start_with?("!(")
203
+ end
204
+
205
+ # A turn's opening as the page draws it (TURN_PROMPT, bubbles.js): a prompt
206
+ # or a wake turn's note or report, not a `!shell` line. What the recap, the
207
+ # cards' count and "a later turn" go by. #turn_prompt? stays what bounds a
208
+ # turn's slice, where a `!shell` line ends one.
209
+ def turn_opening?(message)
210
+ turn_prompt?(message) && !shell_line?(message)
211
+ end
212
+
213
+ # An opening's stamp: its turn_id (a prompt's, a wake note's), else its
214
+ # own opening_id (/continue no's reason line); nil for neither (a line
215
+ # an older chi wrote). Where a card (Bridge::CardPlaces) and the recap
216
+ # (RecapPlace) sit.
217
+ # @return [String, nil]
218
+ def opening_stamp(message)
219
+ id = message[:turn_id] || message["turn_id"] || message[:opening_id] || message["opening_id"]
220
+ id.is_a?(String) && !id.empty? ? id : nil
221
+ end
222
+
223
+ # +message+'s +key+, a symbol or a string key.
224
+ def field(message, key) = message[key] || message[key.to_s]
225
+
226
+ private_class_method :user_speaker, :sent_by, :steered_by, :chi_line?, :field
227
+ end
228
+ end
@@ -1,10 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "client_id"
4
- require_relative "participant_name"
4
+ require_relative "speaker"
5
5
  require_relative "message_time"
6
6
 
7
7
  module Samagotchi
8
+ # Read when a participant's header or author fields are made, not at load
9
+ # (the bundle test kit loads Steer; it keeps the kit's file count).
10
+ autoload :ParticipantName, File.expand_path("participant_name", __dir__)
11
+
8
12
  # A plugin's steer: text a plugin puts into the running turn (Engine#steer),
9
13
  # the way a UI's steering does. The drain a loop gets returns items: a
10
14
  # String (a user line, as ever) or {text:, source:} (a steer). The user
@@ -20,20 +24,14 @@ module Samagotchi
20
24
  # that source:. The user's own lines carry none, so their saved bytes are
21
25
  # as they always were.
22
26
  #
23
- # A message someone other than the session's owner wrote (a participant:
24
- # author "p:<id>", Web::Principal#author) carries author: and
25
- # author_name: (the name when it was written); its opening message also
26
- # carries participant_mode: (the rules when it was delivered). The
27
- # owner's messages carry none of these keys.
27
+ # Who wrote a message (an owner's, a participant's, chi's own line) and
28
+ # what kind of line it is are Speaker's reads; the predicates and
29
+ # constants here are Speaker's, kept under these names. Steer keeps the
30
+ # wire: headers, wire text, the forged-frame quoting, Merge and #inject!.
28
31
  module Steer
29
- KIND = "steer"
30
- INPUT_KIND = "input"
31
- # KernelLoop's line asking a Qwen model to finish the tool call it left
32
- # open: a user line inside the running turn, the model's alone. No turn
33
- # opens with it (#turn_prompt?), and the page draws nothing for it, as it
34
- # showed nothing live. Its kind adds no header (#header): its bytes go
35
- # out as before it had one.
36
- RECOVERY_KIND = "recovery"
32
+ KIND = Speaker::KIND
33
+ INPUT_KIND = Speaker::INPUT_KIND
34
+ RECOVERY_KIND = Speaker::RECOVERY_KIND
37
35
 
38
36
  # A drain item for an input line with its sender (source nil = the user).
39
37
  # A plain String is still a valid item: the user's line. +mark+: fields
@@ -45,10 +43,8 @@ module Samagotchi
45
43
  def initialize(text:, source:, mark: nil, author: nil, author_name: nil) = super
46
44
  end
47
45
 
48
- DELEGATE_REPORT = "delegate_report"
49
- # The source of a line from a client that is not a human's
50
- # (ClientId.human?) and not one chi names: automatic:<client id>.
51
- AUTOMATIC_SOURCE_PREFIX = "automatic:"
46
+ DELEGATE_REPORT = Speaker::DELEGATE_REPORT
47
+ AUTOMATIC_SOURCE_PREFIX = Speaker::AUTOMATIC_SOURCE_PREFIX
52
48
 
53
49
  # What one drain brought: the merged user text (nil when none) and how
54
50
  # many lines made it, the input messages' text grouped by sender
@@ -159,17 +155,10 @@ module Samagotchi
159
155
  # By the message's participant_mode stamp; a missing or unknown one is
160
156
  # a review (every message saved before the stamp).
161
157
  PARTICIPANT_HEADERS = { "review" => REVIEW_HEADER, "pair" => PAIR_HEADER }.freeze
162
- # A participant's line's saved source (participant:<id>): never the user's,
163
- # and never one that cuts (CUT_SOURCES). Its header is the review one,
164
- # by its author.
165
- PARTICIPANT_SOURCE = "participant"
166
- # The owner's author, when one is written out; absent means the same.
167
- OWNER_AUTHOR = "owner"
168
- # A participant's author: this prefix, then its id (Grants'
169
- # grant id), "p:<id>" (#author_for, #participant_id).
170
- PARTICIPANT_PREFIX = "p:"
171
- # A participant's message's keys, which every message copy keeps.
172
- AUTHOR_KEYS = %i[author author_name participant_mode].freeze
158
+ PARTICIPANT_SOURCE = Speaker::PARTICIPANT_SOURCE
159
+ OWNER_AUTHOR = Speaker::OWNER_AUTHOR
160
+ PARTICIPANT_PREFIX = Speaker::PARTICIPANT_PREFIX
161
+ AUTHOR_KEYS = Speaker::AUTHOR_KEYS
173
162
  # An opener's baked reply line (ReplyFor#line, ReplyFor::LINE_KEY):
174
163
  # the model reads it after the opener's header, before the user's text
175
164
  # (#wire_text). Where every such line opens (ReplyFor::LEAD), so a copy
@@ -232,17 +221,9 @@ module Samagotchi
232
221
  format(AUTOMATIC_HEADER, client_id.empty? ? "an unnamed client" : client_id)
233
222
  end
234
223
 
235
- # Whether +author+ (an origin's or a message's) is someone other than
236
- # the owner. Any author but none or "owner" is: an author only narrows.
237
- def participant_author?(author)
238
- !author.nil? && !author.to_s.empty? && author.to_s != OWNER_AUTHOR
239
- end
240
-
241
- # A user message a participant wrote.
242
- def participant_message?(message)
243
- message.is_a?(Hash) && (message[:role] || message["role"]).to_s == "user" &&
244
- participant_author?(message[:author] || message["author"])
245
- end
224
+ # Speaker's author reads (Speaker.participant_author? and the rest).
225
+ def participant_author?(author) = Speaker.participant_author?(author)
226
+ def participant_message?(message) = Speaker.participant_message?(message)
246
227
 
247
228
  # The header above a participant's message: its saved name, cleaned
248
229
  # again (ParticipantName), or the participant's id when it has none.
@@ -262,31 +243,10 @@ module Samagotchi
262
243
  clean.empty? ? { author: author.to_s } : { author: author.to_s, author_name: clean }
263
244
  end
264
245
 
265
- # Who wrote the turn +messages+[+index+] belongs to: a user message's
266
- # own author; an assistant, tool or system message's is its turn's, the
267
- # nearest turn-opening message before it (#turn_prompt?). nil for the
268
- # owner (and before any turn).
269
- def author_at(messages, index)
270
- list = Array(messages)
271
- message = list[index]
272
- return nil unless message.is_a?(Hash)
273
-
274
- own = message[:author] || message["author"]
275
- return sender_author(own) if (message[:role] || message["role"]).to_s == "user"
276
-
277
- opener = list[0...index].rindex { |m| turn_prompt?(m) }
278
- opener ? sender_author(list[opener][:author] || list[opener]["author"]) : nil
279
- end
280
-
281
- # +author+ when it is a participant's, else nil (the owner).
282
- def sender_author(author) = participant_author?(author) ? author.to_s : nil
283
-
284
- # The author a participant's turns and messages carry: "p:<id>".
285
- def author_for(participant_id) = "#{PARTICIPANT_PREFIX}#{participant_id}"
286
-
287
- # The participant id in +author+ ("p:<id>" → "<id>"); a string without
288
- # the prefix as it is ("" for nil).
289
- def participant_id(author) = author.to_s.delete_prefix(PARTICIPANT_PREFIX)
246
+ def author_at(messages, index) = Speaker.author_at(messages, index)
247
+ def sender_author(author) = Speaker.sender_author(author)
248
+ def author_for(participant_id) = Speaker.author_for(participant_id)
249
+ def participant_id(author) = Speaker.participant_id(author)
290
250
 
291
251
  # The text a steer or merged input goes out as: its header, a newline,
292
252
  # then +text+ (the message's content by default); an opener's reply
@@ -385,13 +345,9 @@ module Samagotchi
385
345
  { role: "user", kind: KIND, source: source.to_s, content: text.to_s, **author_fields(author, author_name) }
386
346
  end
387
347
 
388
- def steer?(message)
389
- (message[:kind] || message["kind"]).to_s == KIND
390
- end
348
+ def steer?(message) = Speaker.steer?(message)
391
349
 
392
- # The sources of a steer a person sent (a continue card's text, from the
393
- # user or a parent agent's chi answer), not a plugin's prod.
394
- PERSON_SOURCES = %w[user parent_agent].freeze
350
+ PERSON_SOURCES = Speaker::PERSON_SOURCES
395
351
 
396
352
  # The senders whose message may cut a generation that has only been
397
353
  # thinking (Engine#cut_for_steer): the user (no source), chi send, a
@@ -403,67 +359,15 @@ module Samagotchi
403
359
  CUT_SOURCES.include?(source.to_s) || ClientId.agent?(source)
404
360
  end
405
361
 
406
- # A steer a person sent: what a recap and a side question keep.
407
- def person?(message)
408
- steer?(message) && PERSON_SOURCES.include?((message[:source] || message["source"]).to_s)
409
- end
410
-
411
- # A user message that is a prompt or the user's steering, not a steer:
412
- # what "the last prompt" and "user turns" count.
413
- def prompt?(message)
414
- message.is_a?(Hash) && (message[:role] || message["role"]).to_s == "user" && !steer?(message)
415
- end
416
-
417
- # KernelLoop's recovery line (RECOVERY_KIND).
418
- def recovery?(message)
419
- message.is_a?(Hash) && (message[:kind] || message["kind"]).to_s == RECOVERY_KIND
420
- end
421
-
422
- # The user's lines merged into a running turn (Merge#messages).
423
- def input?(message)
424
- message.is_a?(Hash) && (message[:kind] || message["kind"]).to_s == INPUT_KIND
425
- end
426
-
427
- # A prompt that started a turn: not a steer, not input merged into a
428
- # running turn, except the input a turn with no prompt starts with (a
429
- # wake turn's delegate report, marked turn_start); also the context note
430
- # a context wake turn starts with (role system, marked turn_start). What
431
- # the turns a page shows count.
432
- def turn_prompt?(message)
433
- return false unless message.is_a?(Hash)
434
-
435
- start = (message[:turn_start] || message["turn_start"]) == true
436
- return start if (message[:role] || message["role"]).to_s == "system"
437
-
438
- prompt?(message) && !recovery?(message) && (!input?(message) || start)
439
- end
440
-
441
- # A stored `!shell` line: "!(cmd)\n<output>", no turn id. A user message,
442
- # so #turn_prompt? takes it; no turn ran.
443
- def shell_line?(message)
444
- return false unless message.is_a?(Hash)
445
-
446
- content = message[:content] || message["content"]
447
- (message[:turn_id] || message["turn_id"]).nil? && content.is_a?(String) && content.start_with?("!(")
448
- end
449
-
450
- # A turn's opening as the page draws it (TURN_PROMPT, bubbles.js): a prompt
451
- # or a wake turn's note or report, not a `!shell` line. What the recap, the
452
- # cards' count and "a later turn" go by. #turn_prompt? stays what bounds a
453
- # turn's slice, where a `!shell` line ends one.
454
- def turn_opening?(message)
455
- turn_prompt?(message) && !shell_line?(message)
456
- end
457
-
458
- # An opening's stamp: its turn_id (a prompt's, a wake note's), else its
459
- # own opening_id (/continue no's reason line); nil for neither (a line
460
- # an older chi wrote). Where a card (Bridge::CardPlaces) and the recap
461
- # (RecapPlace) sit.
462
- # @return [String, nil]
463
- def opening_stamp(message)
464
- id = message[:turn_id] || message["turn_id"] || message[:opening_id] || message["opening_id"]
465
- id.is_a?(String) && !id.empty? ? id : nil
466
- end
362
+ # Speaker's message reads, kept under Steer's names.
363
+ def person?(message) = Speaker.person?(message)
364
+ def prompt?(message) = Speaker.prompt?(message)
365
+ def recovery?(message) = Speaker.recovery?(message)
366
+ def input?(message) = Speaker.input?(message)
367
+ def turn_prompt?(message) = Speaker.turn_prompt?(message)
368
+ def shell_line?(message) = Speaker.shell_line?(message)
369
+ def turn_opening?(message) = Speaker.turn_opening?(message)
370
+ def opening_stamp(message) = Speaker.opening_stamp(message)
467
371
 
468
372
  # Call a loop's drain: +at_answer+ goes only to a drain that takes it
469
373
  # (the Engine's); a caller's own drain (a queue's #drain) is called bare.
@@ -381,6 +381,11 @@ module Samagotchi
381
381
  # What ctx.secrets holds now ({"name" => "value"}); seed it with
382
382
  # bundle.set(secrets: {...}).
383
383
  def secrets = @recorder.secrets.to_h
384
+ # The bundle's chips now (ctx.chips), by name, as stored: {"label",
385
+ # "state", "dot", "title", "at", "body", "actions", "set_at"}.
386
+ def chips = @recorder.chips.all[@name] || {}
387
+ # How many chip changes were written (the same chip again writes none).
388
+ def chip_writes = @recorder.chips.writes
384
389
 
385
390
  # Every ctx.sign_in flow, oldest first (Plugin::SignIn: #title,
386
391
  # #address the block built, #redirect_uri, #card_id, #outcome). The kit
@@ -75,6 +75,7 @@ module Samagotchi
75
75
  candidate_proposed: ->(_candidate) {},
76
76
  judgments: -> { recorder.judgments.layer },
77
77
  secrets_store: ->(_bundle) { recorder.secrets },
78
+ chips: -> { recorder[:session_id] ? recorder.chips : nil },
78
79
  sign_in: ->(bundle, **options, &build) { recorder.sign_in(bundle, **options, &build) }
79
80
  )
80
81
  end
@@ -7,6 +7,10 @@ require_relative "../hook_question"
7
7
  require_relative "judgments"
8
8
 
9
9
  module Samagotchi
10
+ # ctx.chips' store, loaded with the first Recorder: the kit's load stays under its file budget
11
+ # (spec/samagotchi/testing/sandbox_spec.rb).
12
+ autoload :SessionChips, File.expand_path("../session_chips", __dir__)
13
+
10
14
  module Testing
11
15
  # A line to the user (ctx.notify, event[:notify]); +hook+ is the
12
16
  # plugin's label ("plugin.rb (bundle x)").
@@ -68,6 +72,7 @@ module Samagotchi
68
72
  @values = DEFAULTS.merge(values)
69
73
  @secrets = Plugin::Secrets::MemoryStore.new
70
74
  @secrets.replace(@values[:secrets] || {})
75
+ @chips = SessionChips::Memory.new
71
76
  @notices = []
72
77
  @sign_ins = []
73
78
  @cards = []
@@ -96,6 +101,11 @@ module Samagotchi
96
101
  # @return [Plugin::Secrets::MemoryStore]
97
102
  attr_reader :secrets
98
103
 
104
+ # ctx.chips' store: in memory, the session's (#all by bundle; #writes
105
+ # counts the changes written). Nothing on disk.
106
+ # @return [SessionChips::Memory]
107
+ attr_reader :chips
108
+
99
109
  def [](key) = @mutex.synchronize { @values.fetch(key) }
100
110
 
101
111
  # ctx.judge and ctx.judge_later's System 1 (Testing::Judgments).
@@ -48,8 +48,12 @@ module Samagotchi
48
48
  # :kit (declared only by a kit that adds it, never by full's nil),
49
49
  # :always (by every kit that doesn't remove it), nil (the built-ins'
50
50
  # rule: Toolkit::Resolved#declares?)
51
+ # @!attribute participant [Symbol, nil] a plugin's
52
+ # chi.tool(participant:): :deny (a participant's pair turn is
53
+ # refused it: Guardrails::PairBoundary), :allow; nil (chi's own
54
+ # tools) allows as :allow does
51
55
  Entry = Struct.new(:name, :schema, :handler, :label, :preview, :targets, :refs, :source, :layer, :unwrap,
52
- :declare, keyword_init: true) do
56
+ :declare, :participant, keyword_init: true) do
53
57
  def core? = source == "core"
54
58
 
55
59
  # Declared only by a kit that adds it (chi.tool(declare: :kit)).
@@ -120,12 +124,13 @@ module Samagotchi
120
124
 
121
125
  # @return [Entry]
122
126
  def register(name, schema:, handler:, label: nil, preview: nil, targets: nil, refs: nil, source: "core", layer: nil,
123
- unwrap: nil, declare: nil)
127
+ unwrap: nil, declare: nil, participant: nil)
124
128
  raise ArgumentError, "tool #{name} is already registered" if @entries.key?(name)
125
129
 
126
130
  @entries[name] = Entry.new(name: name, schema: schema, handler: handler, label: label,
127
131
  preview: Entry.per_call(preview), targets: Entry.per_call(targets), refs: refs,
128
- source: source, layer: layer, unwrap: unwrap, declare: declare)
132
+ source: source, layer: layer, unwrap: unwrap, declare: declare,
133
+ participant: participant)
129
134
  end
130
135
 
131
136
  # Remove a tool (a plugin's tool set changed after load).
@@ -4,6 +4,7 @@ require "json"
4
4
  require_relative "session_paths"
5
5
  require_relative "analytics_file"
6
6
  require_relative "redact"
7
+ require_relative "speaker"
7
8
 
8
9
  module Samagotchi
9
10
  # The failing tool calls of given turns, with their error results, cut:
@@ -15,7 +16,8 @@ module Samagotchi
15
16
  # or blocked, at most MAX_CALLS_PER_TURN, each paired with its call's
16
17
  # arguments and its result (the session file's messages), and a failed
17
18
  # turn's failure line. The user's prompt and the last assistant text come
18
- # only with include_text. Every field goes through Redact.text (key
19
+ # only with include_text. A participant's turn (Speaker.of) has neither,
20
+ # and no calls: the owner's words only. Every field goes through Redact.text (key
19
21
  # shapes, the home as ~) before it is cut, and all of them together stay
20
22
  # within max_chars.
21
23
  #
@@ -147,13 +149,15 @@ module Samagotchi
147
149
  slice = slice_of(files.messages, turn_id)
148
150
  records = files.records_of(turn_id)
149
151
  pairs = slice && pair(records, slice)
150
- failing = records.select { |r| FAILING.include?(r["status"]) }.first(MAX_CALLS_PER_TURN)
152
+ opening = slice&.find { |m| m["role"] == "user" && m["turn_id"] == turn_id }
153
+ # A participant's turn: no text of theirs (a call's args can carry it).
154
+ participant = Speaker.of(opening) == :participant
155
+ failing = participant ? [] : records.select { |r| FAILING.include?(r["status"]) }.first(MAX_CALLS_PER_TURN)
151
156
 
152
157
  failure = failure_line(record)
153
158
  failure = nil unless budget.take?(failure)
154
159
  prompt = answer = nil
155
- if include_text && slice
156
- opening = slice.find { |m| m["role"] == "user" && m["turn_id"] == turn_id }
160
+ if include_text && slice && !participant
157
161
  prompt = take(budget, cut(opening && opening["content"], LINE_CHARS))
158
162
  answer = take(budget, cut(last_answer(slice), LINE_CHARS))
159
163
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Samagotchi
4
- VERSION = "0.195.0"
4
+ VERSION = "0.196.0"
5
5
  # Sent as the User-Agent of every HTTP request chi makes.
6
6
  USER_AGENT = "chi/#{VERSION}"
7
7
  end