samagotchi 0.184.0 → 0.186.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/CHANGELOG.md +68 -1
- data/docs/architecture.md +1 -1
- data/docs/bundles.md +62 -35
- data/docs/cli.md +10 -1
- data/docs/configuration.md +19 -1
- data/docs/context.md +76 -10
- data/docs/guardrails.md +20 -2
- data/docs/hooks.md +1 -0
- data/docs/internals/block-renderers.md +4 -2
- data/docs/internals/bridge.md +7 -2
- data/docs/internals/engine.md +15 -1
- data/docs/internals/llm-context-forget.md +6 -0
- data/docs/internals/participants.md +4 -1
- data/docs/internals/prompt-caching.md +14 -0
- data/docs/internals/remote-sessions.md +10 -1
- data/docs/internals/session-lifecycle.md +1 -1
- data/docs/internals/web-frontend.md +23 -6
- data/docs/mcp-server.md +6 -2
- data/docs/scheduling.md +5 -0
- data/docs/sessions.md +12 -2
- data/docs/sub-agent.md +48 -3
- data/docs/thin-clients.md +76 -4
- data/lib/samagotchi/agent_caller.rb +97 -0
- data/lib/samagotchi/bridge.rb +7 -7
- data/lib/samagotchi/bundles/claude-peer/manifest.yml +14 -8
- data/lib/samagotchi/bundles/claude-peer/plugin.rb +3 -3
- data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +49 -30
- data/lib/samagotchi/bundles/claude-peer/scripts/pair +2 -2
- data/lib/samagotchi/bundles/claude-peer/scripts/pair-remote +123 -0
- data/lib/samagotchi/bundles/claude-peer/skill_claude_peer.md +11 -3
- data/lib/samagotchi/bundles/codex-peer/manifest.yml +10 -8
- data/lib/samagotchi/bundles/codex-peer/plugin.rb +3 -3
- data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +29 -8
- data/lib/samagotchi/bundles/codex-peer/scripts/pair +1 -1
- data/lib/samagotchi/bundles/codex-peer/skill_codex_peer.md +11 -3
- data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
- data/lib/samagotchi/bundles/mcp/http_client.rb +19 -2
- data/lib/samagotchi/bundles/mcp/manifest.yml +2 -2
- data/lib/samagotchi/bundles/system/manifest.yml +2 -2
- data/lib/samagotchi/bundles/system/self_map.md +2 -0
- data/lib/samagotchi/client_id.rb +61 -1
- data/lib/samagotchi/command_intake.rb +16 -8
- data/lib/samagotchi/config.rb +3 -1
- data/lib/samagotchi/context_absorber.rb +215 -46
- data/lib/samagotchi/delivery.rb +15 -0
- data/lib/samagotchi/driver_command.rb +68 -0
- data/lib/samagotchi/engine/block_fix.rb +2 -2
- data/lib/samagotchi/engine.rb +20 -5
- data/lib/samagotchi/engine_peers.rb +6 -0
- data/lib/samagotchi/guardrails/pair_boundary.rb +1 -1
- data/lib/samagotchi/llm/chat_loop.rb +16 -2
- data/lib/samagotchi/mcp_command.rb +7 -2
- data/lib/samagotchi/mcp_server/tools.rb +9 -3
- data/lib/samagotchi/objectives_track.rb +2 -1
- data/lib/samagotchi/owner_prompt.rb +5 -1
- data/lib/samagotchi/parent_report.rb +9 -5
- data/lib/samagotchi/plain_reply.rb +75 -0
- data/lib/samagotchi/plugin/peer.rb +10 -4
- data/lib/samagotchi/reply_for.rb +254 -0
- data/lib/samagotchi/reply_wait.rb +12 -3
- data/lib/samagotchi/schedules.rb +10 -7
- data/lib/samagotchi/send_command/parent_gate.rb +3 -1
- data/lib/samagotchi/send_command.rb +45 -8
- data/lib/samagotchi/session.rb +9 -1
- data/lib/samagotchi/session_access/ask.rb +10 -4
- data/lib/samagotchi/session_access/contract.rb +2 -0
- data/lib/samagotchi/session_access/exchange/token.rb +42 -7
- data/lib/samagotchi/session_access/exchange.rb +5 -2
- data/lib/samagotchi/session_access/local.rb +6 -3
- data/lib/samagotchi/session_access/remote.rb +23 -3
- data/lib/samagotchi/session_access.rb +4 -2
- data/lib/samagotchi/session_command_catalog.rb +8 -0
- data/lib/samagotchi/session_commands.rb +9 -0
- data/lib/samagotchi/session_continue.rb +4 -1
- data/lib/samagotchi/session_inbox.rb +2 -0
- data/lib/samagotchi/session_inbox_delivery.rb +4 -2
- data/lib/samagotchi/session_manager/delivery.rb +80 -5
- data/lib/samagotchi/session_manager/spawn.rb +7 -1
- data/lib/samagotchi/steer.rb +119 -22
- data/lib/samagotchi/terminal_ui/formatting.rb +12 -2
- data/lib/samagotchi/tool_activity.rb +4 -0
- data/lib/samagotchi/tool_declarations.rb +22 -1
- data/lib/samagotchi/tool_names.rb +4 -0
- data/lib/samagotchi/toolkit.rb +3 -2
- data/lib/samagotchi/tools/ask_session.rb +208 -0
- data/lib/samagotchi/tools/builtin_calls.rb +1 -0
- data/lib/samagotchi/tools/builtins.rb +5 -0
- data/lib/samagotchi/turn_anchors.rb +65 -17
- data/lib/samagotchi/turn_checkpoint.rb +21 -2
- data/lib/samagotchi/turn_stages/context_judge.rb +3 -1
- data/lib/samagotchi/turn_stages/objectives.rb +4 -2
- data/lib/samagotchi/turn_stages/toolkit_suggestion.rb +3 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/visitor.rb +37 -0
- data/lib/samagotchi/web/app.rb +1 -1
- data/lib/samagotchi/web/ask_routes.rb +68 -21
- data/lib/samagotchi/web/message_display.rb +15 -2
- data/lib/samagotchi/web/participant_access.rb +1 -1
- data/lib/samagotchi/web/public/app.js +1 -1
- data/lib/samagotchi/web/public/block_fix.js +11 -3
- data/lib/samagotchi/web/public/history_shape.js +3 -1
- data/lib/samagotchi/web/public/index.html +8 -0
- data/lib/samagotchi/web/public/peer_links.js +27 -4
- data/lib/samagotchi/web/public/peer_note.js +19 -3
- data/lib/samagotchi/web/public/peer_text.js +62 -0
- data/lib/samagotchi/web/public/picture_regions.js +2 -2
- data/lib/samagotchi/web/public/turn_events.js +19 -0
- data/lib/samagotchi/web/public/working_set_view.js +11 -2
- data/lib/samagotchi/web/session_access_routes.rb +30 -2
- data/lib/samagotchi/web/session_control.rb +22 -3
- data/lib/samagotchi/web/session_create.rb +11 -7
- data/lib/samagotchi/worker/command_queue.rb +5 -2
- data/lib/samagotchi/worker/senders.rb +79 -0
- data/lib/samagotchi/worker.rb +22 -18
- data/lib/samagotchi/worker_inbound.rb +12 -4
- data/lib/samagotchi/worker_participant_gate.rb +1 -3
- data/lib/samagotchi/worker_wakes.rb +12 -0
- metadata +10 -1
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "config"
|
|
4
|
+
require_relative "steer"
|
|
5
|
+
|
|
6
|
+
module Samagotchi
|
|
7
|
+
# What the surface a message was sent from can show, declared by a thin
|
|
8
|
+
# client on POST …/ask as "reply_for" (docs/thin-clients.md): a name
|
|
9
|
+
# (config.yml's surfaces:, `voice` by default) or the values themselves.
|
|
10
|
+
# chi knows values, never surfaces:
|
|
11
|
+
# - formatting: "markdown" or "plain";
|
|
12
|
+
# - room: "long", "short" (at most SHORT_WORDS words) or "one_line" (at
|
|
13
|
+
# most ONE_LINE_CHARS characters);
|
|
14
|
+
# - choices: "many" or how many options a question may offer (0..9);
|
|
15
|
+
# - detail: true, or false (no commands, code or diffs; no approvals,
|
|
16
|
+
# spending questions or user-only options either: #answers?).
|
|
17
|
+
# A missing value is the full one, and a value that is full everywhere is
|
|
18
|
+
# no ReplyFor at all (nil): full is the absence of a value, never a
|
|
19
|
+
# stored "full", so a full ask sends and saves what no ask does.
|
|
20
|
+
#
|
|
21
|
+
# One member of a turn's TurnAnchors: it rides from chi web to the Engine
|
|
22
|
+
# as verify and revises do, and is saved (#to_file, the values with
|
|
23
|
+
# string keys) on the turn's opening user message beside the baked
|
|
24
|
+
# wording the model reads (#line), so a later release that rewords a
|
|
25
|
+
# value never changes an old turn's bytes (.opener_fields).
|
|
26
|
+
class ReplyFor < Data.define(:formatting, :room, :choices, :detail)
|
|
27
|
+
# A refused name, key or value (POST …/ask's 400 invalid_ask): the
|
|
28
|
+
# message says which and what it may be.
|
|
29
|
+
class Invalid < ArgumentError; end
|
|
30
|
+
|
|
31
|
+
# The worker's Bridge feature (Bridge::FEATURES): it saves the field and
|
|
32
|
+
# bakes its line. chi web sends the field to no worker without it.
|
|
33
|
+
FEATURE = "reply_for"
|
|
34
|
+
# A worker without FEATURE (SessionManager::Delivery). The session
|
|
35
|
+
# restarts on the chi home that made it, so a restart alone may not be
|
|
36
|
+
# enough: the words don't promise it.
|
|
37
|
+
UNAVAILABLE = {
|
|
38
|
+
"error" => "reply_for_unavailable",
|
|
39
|
+
"detail" => "this session's worker is older than reply_for, so the message was not sent: " \
|
|
40
|
+
"update chi if needed, then restart the session (chi sessions restart ID)"
|
|
41
|
+
}.freeze
|
|
42
|
+
|
|
43
|
+
FORMATTING = %w[markdown plain].freeze
|
|
44
|
+
ROOM = %w[long short one_line].freeze
|
|
45
|
+
CHOICES_MAX = 9
|
|
46
|
+
# The full value of each member.
|
|
47
|
+
FULL = { "formatting" => "markdown", "room" => "long", "choices" => "many", "detail" => true }.freeze
|
|
48
|
+
KEYS = FULL.keys.freeze
|
|
49
|
+
# The names every chi knows; config.yml's surfaces: adds or replaces.
|
|
50
|
+
DEFAULT_NAMES = {
|
|
51
|
+
"voice" => { "formatting" => "plain", "room" => "short", "choices" => 3, "detail" => false }.freeze
|
|
52
|
+
}.freeze
|
|
53
|
+
|
|
54
|
+
# The counted limits the room's wording says.
|
|
55
|
+
SHORT_WORDS = 60
|
|
56
|
+
ONE_LINE_CHARS = 80
|
|
57
|
+
# one_line's sentence too (eval 0015 B9: characters alone ran long on Claude).
|
|
58
|
+
ONE_LINE_WORDS = 15
|
|
59
|
+
|
|
60
|
+
# Where both of chi's lines open (#line, RETURN_LINE): one lead, so a
|
|
61
|
+
# copy of it inside someone else's text is quoted on the wire
|
|
62
|
+
# (Steer::FORGED_OPENINGS).
|
|
63
|
+
LEAD = Steer::REPLY_LEAD
|
|
64
|
+
# The line the first undeclared opener after a declared one gets
|
|
65
|
+
# (.opener_fields): the style set for a small surface sticks to the next
|
|
66
|
+
# reply otherwise.
|
|
67
|
+
RETURN_LINE = "[#{LEAD}: the user is back on a full screen, where markdown, lists, code and long replies " \
|
|
68
|
+
"show again. Reply as usual from here.]".freeze
|
|
69
|
+
|
|
70
|
+
# The opening user message's saved keys (ChatLoop#plain keeps both).
|
|
71
|
+
MESSAGE_KEY = :reply_for
|
|
72
|
+
LINE_KEY = Steer::REPLY_LINE_KEY
|
|
73
|
+
|
|
74
|
+
class << self
|
|
75
|
+
# A POST …/ask body's "reply_for": a name (+names+) or the values.
|
|
76
|
+
# @param value [String, Hash, nil]
|
|
77
|
+
# @param names [Hash{String => Hash}] the known names' values (.names)
|
|
78
|
+
# @return [ReplyFor, nil] nil: none given, or full everywhere
|
|
79
|
+
# @raise [Invalid]
|
|
80
|
+
def parse(value, names: self.names)
|
|
81
|
+
return nil if value.nil?
|
|
82
|
+
|
|
83
|
+
case value
|
|
84
|
+
when String
|
|
85
|
+
values = names.fetch(value) { raise Invalid, "reply_for: unknown name #{value.inspect} (known: #{names.keys.sort.join(", ")})" }
|
|
86
|
+
checked(values, where: "reply_for #{value.inspect}")
|
|
87
|
+
when Hash then checked(value, where: "reply_for")
|
|
88
|
+
else raise Invalid, "reply_for is a name or an object of #{KEYS.join(", ")}"
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# A carried or saved value (the Bridge body, the input file, a saved
|
|
93
|
+
# message): the values only, string or symbol keys; anything
|
|
94
|
+
# malformed is nil, as the other anchors read theirs.
|
|
95
|
+
# @return [ReplyFor, nil]
|
|
96
|
+
def from(value)
|
|
97
|
+
return value if value.is_a?(self)
|
|
98
|
+
return nil unless value.is_a?(Hash)
|
|
99
|
+
|
|
100
|
+
checked(value, where: "reply_for")
|
|
101
|
+
rescue Invalid
|
|
102
|
+
nil
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# The names an ask may give: DEFAULT_NAMES, then config.yml's
|
|
106
|
+
# surfaces: (a name there replaces a default). An entry is checked
|
|
107
|
+
# when an ask names it (.parse), so a bad one refuses only that name.
|
|
108
|
+
# @return [Hash{String => Hash}]
|
|
109
|
+
def names(env: ENV)
|
|
110
|
+
configured = ConfigFile.read_yaml(env: env)&.dig("surfaces")
|
|
111
|
+
configured = {} unless configured.is_a?(Hash)
|
|
112
|
+
DEFAULT_NAMES.merge(configured.to_h { |name, values| [name.to_s, values] })
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# The fields a turn's opening user message is saved with
|
|
116
|
+
# (Engine#turn_messages, and the worker's checkpoint copy of it):
|
|
117
|
+
# +declared+'s values and line; else, when the latest opener in
|
|
118
|
+
# +history+ declared one (.latest_opener), the return line alone; else
|
|
119
|
+
# none, so a session that never declares keeps its bytes. The return
|
|
120
|
+
# line ends the run: the opener carrying it declares nothing. Read
|
|
121
|
+
# from the history alone (no session state), so a one-shot --resume,
|
|
122
|
+
# a schedule's or an agent's opener get it too.
|
|
123
|
+
# @param history [Array<Hash>] the session's messages before this one
|
|
124
|
+
# @param declared [ReplyFor, Hash, nil]
|
|
125
|
+
# @return [Hash{Symbol => Object}]
|
|
126
|
+
def opener_fields(history, declared)
|
|
127
|
+
declared = from(declared)
|
|
128
|
+
return { MESSAGE_KEY => declared.to_file, LINE_KEY => declared.line } if declared
|
|
129
|
+
|
|
130
|
+
last = latest_opener(history)
|
|
131
|
+
last && from(last[MESSAGE_KEY] || last[MESSAGE_KEY.to_s]) ? { LINE_KEY => RETURN_LINE } : {}
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# The latest message in +history+ that opened a turn from a sender
|
|
135
|
+
# (Engine#turn_messages' user message, and the worker's checkpoint
|
|
136
|
+
# copy of it): a user message with a turn_id and no kind that is not
|
|
137
|
+
# a !shell line. A wake's note or report, a steer and merged input
|
|
138
|
+
# have a kind (or are a system message), and a step-limit Stop's
|
|
139
|
+
# reason (TurnFlow#abort_continue!) has an opening_id but no turn_id,
|
|
140
|
+
# so none of them hides the turn before them.
|
|
141
|
+
# @return [Hash, nil]
|
|
142
|
+
def latest_opener(history)
|
|
143
|
+
Array(history).reverse_each.find do |message|
|
|
144
|
+
next false unless message.is_a?(Hash) && (message[:role] || message["role"]).to_s == "user"
|
|
145
|
+
next false unless (message[:kind] || message["kind"]).to_s.empty?
|
|
146
|
+
next false if (message[:turn_id] || message["turn_id"]).to_s.empty?
|
|
147
|
+
|
|
148
|
+
!Steer.shell_line?(message)
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# The ReplyFor the latest turn in +messages+ was opened with: its
|
|
153
|
+
# opening (Steer.turn_prompt?) is a wake's too, which declares none,
|
|
154
|
+
# so a wake turn's question never takes an earlier ask's surface.
|
|
155
|
+
# @return [ReplyFor, nil]
|
|
156
|
+
def of_latest_turn(messages)
|
|
157
|
+
opening = Array(messages).reverse_each.find { |message| Steer.turn_prompt?(message) }
|
|
158
|
+
opening && from(opening[MESSAGE_KEY] || opening[MESSAGE_KEY.to_s])
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
private
|
|
162
|
+
|
|
163
|
+
def checked(value, where:)
|
|
164
|
+
unless value.is_a?(Hash)
|
|
165
|
+
raise Invalid, "#{where} is a mapping of #{KEYS.join(", ")}"
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
fields = value.to_h { |key, entry| [key.to_s, entry] }
|
|
169
|
+
unknown = fields.keys - KEYS
|
|
170
|
+
raise Invalid, "#{where}: unknown key #{unknown.first.inspect} (known: #{KEYS.join(", ")})" unless unknown.empty?
|
|
171
|
+
|
|
172
|
+
values = FULL.merge(fields)
|
|
173
|
+
raise Invalid, "#{where}: formatting is #{FORMATTING.join(" or ")}" unless FORMATTING.include?(values["formatting"])
|
|
174
|
+
raise Invalid, "#{where}: room is #{ROOM.join(", ")}" unless ROOM.include?(values["room"])
|
|
175
|
+
unless values["choices"] == "many" || (values["choices"].is_a?(Integer) && values["choices"].between?(0, CHOICES_MAX))
|
|
176
|
+
raise Invalid, "#{where}: choices is \"many\" or 0..#{CHOICES_MAX}"
|
|
177
|
+
end
|
|
178
|
+
raise Invalid, "#{where}: detail is true or false" unless [true, false].include?(values["detail"])
|
|
179
|
+
return nil if values == FULL
|
|
180
|
+
|
|
181
|
+
new(formatting: values["formatting"], room: values["room"], choices: values["choices"], detail: values["detail"])
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# The values, string keys (the saved form, the body's and the input
|
|
186
|
+
# file's).
|
|
187
|
+
def to_file = { "formatting" => formatting, "room" => room, "choices" => choices, "detail" => detail }
|
|
188
|
+
|
|
189
|
+
# A :prompt_restored event's field.
|
|
190
|
+
def to_h = to_file
|
|
191
|
+
|
|
192
|
+
def plain? = formatting == "plain"
|
|
193
|
+
|
|
194
|
+
def detail? = detail
|
|
195
|
+
|
|
196
|
+
# The question kinds a surface without detail may not answer
|
|
197
|
+
# (PendingQuestion#kind): an approval (a delegate's relayed one too),
|
|
198
|
+
# a hook's (a spending question: cost-cap, work-cap). The model's own
|
|
199
|
+
# (nil) and the step-limit question ("continue") it may.
|
|
200
|
+
FULL_SCREEN_KINDS = %w[approval hook].freeze
|
|
201
|
+
# What such a surface is told instead of the question (spoken by a
|
|
202
|
+
# voice client), per kind.
|
|
203
|
+
NEEDS_DETAIL_WORDS = {
|
|
204
|
+
"approval" => "chi needs an approval: open it on a full screen.",
|
|
205
|
+
nil => "chi has a question only a full screen can answer: open it there."
|
|
206
|
+
}.freeze
|
|
207
|
+
NEEDS_DETAIL = {
|
|
208
|
+
"error" => "needs_detail",
|
|
209
|
+
"detail" => "this question is answered on a full screen (an approval, a spending question or a user-only option)"
|
|
210
|
+
}.freeze
|
|
211
|
+
|
|
212
|
+
# Whether this surface may answer +question+ (a PendingQuestion): any
|
|
213
|
+
# with detail; without it, not a FULL_SCREEN_KINDS one (told by kind,
|
|
214
|
+
# never by approval:, which a relayed one may have empty) nor one whose
|
|
215
|
+
# options are the user's only (user_only). Like client_id, a
|
|
216
|
+
# convention for an honest client: the token can still answer anything.
|
|
217
|
+
def answers?(question)
|
|
218
|
+
return true if detail? || question.nil?
|
|
219
|
+
|
|
220
|
+
!FULL_SCREEN_KINDS.include?(question.kind.to_s) && question.user_only != true
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# The words for a question this surface may not answer (#answers?).
|
|
224
|
+
def needs_detail_text(question) = NEEDS_DETAIL_WORDS.fetch(question.kind.to_s == "approval" ? "approval" : nil)
|
|
225
|
+
|
|
226
|
+
# One phrase per restricted value, in a fixed order; a full value says
|
|
227
|
+
# nothing.
|
|
228
|
+
FORMATTING_WORDS = {
|
|
229
|
+
"plain" => "shows plain text only, so write plain spoken sentences, not lists: no markdown (no headings, bullets, " \
|
|
230
|
+
"numbered lists, tables, bold or code), no emoji, no links"
|
|
231
|
+
}.freeze
|
|
232
|
+
ROOM_WORDS = {
|
|
233
|
+
"short" => "has room for a short reply of at most #{SHORT_WORDS} words",
|
|
234
|
+
"one_line" => "has room for one line of at most #{ONE_LINE_CHARS} characters: one short sentence of " \
|
|
235
|
+
"#{ONE_LINE_WORDS} words or fewer, the answer only"
|
|
236
|
+
}.freeze
|
|
237
|
+
DETAIL_WORDS = { false => "can't show commands, code or diffs: describe them in words" }.freeze
|
|
238
|
+
|
|
239
|
+
def choice_words
|
|
240
|
+
case choices
|
|
241
|
+
when "many" then nil
|
|
242
|
+
when 0, 1 then "can't offer a list of choices: if you must ask, ask one yes-or-no question"
|
|
243
|
+
else "can offer at most #{choices} choices: if you must ask, offer no more than #{choices} options"
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# The line the model reads above the user's text (Steer.wire_text),
|
|
248
|
+
# baked on the message when it is made.
|
|
249
|
+
def line
|
|
250
|
+
clauses = [FORMATTING_WORDS[formatting], ROOM_WORDS[room], choice_words, DETAIL_WORDS[detail]].compact
|
|
251
|
+
"[#{LEAD}: the user sent it from a surface that #{clauses.join("; ")}. Write this one reply to fit it.]"
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
end
|
|
@@ -206,9 +206,18 @@ module Samagotchi
|
|
|
206
206
|
|
|
207
207
|
running = session.status == Session::STATUS_RUNNING && session.interrupted_turn&.enqueued_id == id
|
|
208
208
|
queued = SessionInbox.waiting_input(session_dir, id) == :turn_end
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
209
|
+
running || queued || ended_turn?(session, session_dir: session_dir, turn: id) ||
|
|
210
|
+
!SessionInbox.merged_into(session_dir, id).nil?
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# Whether +turn+ has ended: the last one to have ended, one with its
|
|
214
|
+
# reply file, or one settled (refused, canceled, killed).
|
|
215
|
+
def ended_turn?(session, session_dir:, turn:)
|
|
216
|
+
id = turn.to_s
|
|
217
|
+
return false if id.empty?
|
|
218
|
+
|
|
219
|
+
session.last_turn&.enqueued_id == id || !SessionInbox.reply_for(session_dir, id).nil? ||
|
|
220
|
+
!TurnCheckpoint.settled_turn(session_dir, id).nil?
|
|
212
221
|
end
|
|
213
222
|
|
|
214
223
|
# Whether the session's live worker names TURN_IDS_FEATURE.
|
data/lib/samagotchi/schedules.rb
CHANGED
|
@@ -507,7 +507,8 @@ module Samagotchi
|
|
|
507
507
|
if verdict
|
|
508
508
|
kind, reason = verdict
|
|
509
509
|
status = kind == :skip ? "skipped" : "failed"
|
|
510
|
-
updated = status == "skipped" ? skipped(current, reason) : failed(current, reason)
|
|
510
|
+
updated = status == "skipped" ? skipped(current, reason) : failed(current, reason) if current.enabled
|
|
511
|
+
updated ||= current.with(last_result: reason)
|
|
511
512
|
file.entries[position] = updated
|
|
512
513
|
outcome = Outcome.new(id: current.id, title: current.title, status: status, detail: reason, entry: updated)
|
|
513
514
|
next
|
|
@@ -697,7 +698,7 @@ module Samagotchi
|
|
|
697
698
|
file.entries[position] = nil
|
|
698
699
|
file.entries.compact!
|
|
699
700
|
else
|
|
700
|
-
changed =
|
|
701
|
+
changed = file.entries[position].with(enabled: enabled, last_result: enabled ? "enabled" : "disabled")
|
|
701
702
|
# Enabled again with a fresh budget: its next skip must not end it at once.
|
|
702
703
|
file.entries[position] = enabled ? changed.with(skips: 0, failures: 0) : changed
|
|
703
704
|
end
|
|
@@ -826,15 +827,17 @@ module Samagotchi
|
|
|
826
827
|
|
|
827
828
|
def skipped(entry, reason)
|
|
828
829
|
skips = entry.skips + 1
|
|
829
|
-
|
|
830
|
+
within_budget = skips < entry.max_skips
|
|
830
831
|
entry.with(skips: skips, firing_since: nil, firing_token: nil, pending_session_id: nil, pending_turn_id: nil,
|
|
831
|
-
last_result:
|
|
832
|
+
last_result: within_budget ? "skipped" : "disabled: #{reason}",
|
|
833
|
+
enabled: within_budget && (entry.enabled || entry.recurrence[:kind] == "date"))
|
|
832
834
|
end
|
|
833
835
|
|
|
834
836
|
def failed(entry, reason)
|
|
835
837
|
failures = entry.failures + 1
|
|
838
|
+
within_budget = failures < entry.max_failures
|
|
836
839
|
entry.with(failures: failures, firing_since: nil, firing_token: nil, pending_session_id: nil, pending_turn_id: nil,
|
|
837
|
-
last_result: reason, enabled:
|
|
840
|
+
last_result: reason, enabled: within_budget && (entry.enabled || entry.recurrence[:kind] == "date"))
|
|
838
841
|
end
|
|
839
842
|
|
|
840
843
|
def pending_detail(entry)
|
|
@@ -1192,8 +1195,8 @@ module Samagotchi
|
|
|
1192
1195
|
Outcome.new(id: entry.id, title: entry.title, status: status, detail: message, entry: entry)
|
|
1193
1196
|
end
|
|
1194
1197
|
|
|
1195
|
-
# A run-now refused before anything was tried:
|
|
1196
|
-
#
|
|
1198
|
+
# A run-now refused before anything was tried: an enabled schedule moves
|
|
1199
|
+
# the same counters a tick does. `due_was` is untouched — run-now is not a
|
|
1197
1200
|
# scheduled occurrence, so there is no occurrence to put the clock back to.
|
|
1198
1201
|
# The result goes back under the lock before any pruning, so a prune that
|
|
1199
1202
|
# raises midway cannot lose the new head.
|
|
@@ -34,7 +34,9 @@ module Samagotchi
|
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
# A /continue yes (or bare /continue) from a parent agent's process.
|
|
37
|
-
def self.parent_continue?(prompt
|
|
37
|
+
def self.parent_continue?(prompt, env: ENV)
|
|
38
|
+
TurnFlow.continue_yes?(prompt) && Guardrails::ParentApprovals.parent_process?(env: env)
|
|
39
|
+
end
|
|
38
40
|
|
|
39
41
|
# The first refusal of a parent's /continue yes among targets whose
|
|
40
42
|
# own parent_answers are +values+, held to this process's config.yml
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "agent_caller"
|
|
3
4
|
require_relative "client_id"
|
|
4
5
|
require_relative "delivery"
|
|
5
6
|
require_relative "terminal_ui/formatting"
|
|
@@ -42,7 +43,6 @@ module Samagotchi
|
|
|
42
43
|
include CLI::Command
|
|
43
44
|
include CLI::ParentWait
|
|
44
45
|
|
|
45
|
-
CLIENT_ID = ClientId::CLI_SEND
|
|
46
46
|
# The cap on one turn's images the Bridge checks.
|
|
47
47
|
MAX_IMAGES = ImageStore::MAX_TURN_REFS
|
|
48
48
|
# A cut's outcomes in the Bridge's ack (SteerCut#cut_for_steer).
|
|
@@ -57,8 +57,8 @@ module Samagotchi
|
|
|
57
57
|
def self.pause(seconds) = sleep(seconds)
|
|
58
58
|
|
|
59
59
|
USAGE = <<~TEXT
|
|
60
|
-
Usage: chi send [-m TEXT] [--image PATH]... [--cut | --queue] (ID|PREFIX)...
|
|
61
|
-
chi send --new [--dir DIR] [--model M] [--llm-context LAYERS] [--thinking LEVEL] [--toolkit NAME] [--set KEY=VALUE]... [--parent-answers LEVEL] [-m TEXT] [--image PATH]...
|
|
60
|
+
Usage: chi send [-m TEXT] [--image PATH]... [--cut | --queue] [--as NAME] (ID|PREFIX)...
|
|
61
|
+
chi send --new [--dir DIR] [--model M] [--llm-context LAYERS] [--thinking LEVEL] [--toolkit NAME] [--set KEY=VALUE]... [--parent-answers LEVEL] [--driver NAME] [-m TEXT] [--image PATH]...
|
|
62
62
|
chi send --new --continues (ID|PREFIX|last:ID) [--model M] [-m TEXT] [--image PATH]...
|
|
63
63
|
chi send --wait [--timeout S] [--format json] [-m TEXT] [--image PATH]... (--new | ID)
|
|
64
64
|
chi send --queue --wait [--timeout S] [--format json] [-m TEXT] ID
|
|
@@ -123,6 +123,17 @@ module Samagotchi
|
|
|
123
123
|
questions: hard, standard or soft, only stricter than
|
|
124
124
|
config.yml's guardrails.parent_answers (a looser one
|
|
125
125
|
warns; config.yml's applies)
|
|
126
|
+
--driver NAME
|
|
127
|
+
(--new) name the agent NAME its driver (its messages go
|
|
128
|
+
in uncounted, with no header); an agent that creates
|
|
129
|
+
one drives it
|
|
130
|
+
--as NAME send as the agent NAME (agent:NAME; lowercase letters,
|
|
131
|
+
digits, - and _, up to 32), not as you. Without it, an
|
|
132
|
+
agent's marker names the sender (CLAUDECODE: claude,
|
|
133
|
+
CODEX_THREAD_ID: codex, AI_AGENT: its value; inside a
|
|
134
|
+
chi session its own, chi-ID8, and --as is ignored).
|
|
135
|
+
An agent's message restarts no stopped session, brings
|
|
136
|
+
back no archived one and runs no ! line
|
|
126
137
|
--wait wait for the answer and print it (one session); the
|
|
127
138
|
other lines go to stderr. Exit 3: it waits for an
|
|
128
139
|
answer from you: the question, its options and the
|
|
@@ -191,6 +202,8 @@ module Samagotchi
|
|
|
191
202
|
f.value "--timeout"
|
|
192
203
|
f.value "--format"
|
|
193
204
|
f.value "--turn"
|
|
205
|
+
f.value "--as", key: :as
|
|
206
|
+
f.value "--driver", key: :driver
|
|
194
207
|
# Starting a turn in every live session at once is too easy to do
|
|
195
208
|
# by accident.
|
|
196
209
|
f.refuse "--all", "there is no --all: name the sessions"
|
|
@@ -258,8 +271,10 @@ module Samagotchi
|
|
|
258
271
|
# @param argv [Array<String>] the arguments after "send"
|
|
259
272
|
# @param access [SessionAccess::Local, nil] where the sessions are;
|
|
260
273
|
# default this machine's, in +state_dir+
|
|
261
|
-
|
|
274
|
+
# @param env [Hash] where the agent markers are read (ENV; specs pass their own)
|
|
275
|
+
def initialize(argv, stdin: $stdin, stdout: $stdout, stderr: $stderr, state_dir: nil, access: nil, env: ENV)
|
|
262
276
|
@argv = argv.dup
|
|
277
|
+
@env = env
|
|
263
278
|
@stdin = stdin
|
|
264
279
|
@stdout = stdout
|
|
265
280
|
@stderr = stderr
|
|
@@ -279,6 +294,9 @@ module Samagotchi
|
|
|
279
294
|
@waiting = options[:wait] == true
|
|
280
295
|
@cut = options[:cut] == true
|
|
281
296
|
@queue = options[:queue] == true
|
|
297
|
+
return CLI::Exit::USAGE unless (@agent = agent_name(options))
|
|
298
|
+
|
|
299
|
+
@agent = nil if @agent == :owner
|
|
282
300
|
prompt = compose(utf8(read_stdin), utf8(options[:message]))
|
|
283
301
|
# Before the wait-only case, which would drop the images.
|
|
284
302
|
return usage_error("--image needs a message: pass -m TEXT or pipe it in") if prompt.nil? && !options[:images].empty?
|
|
@@ -377,7 +395,7 @@ module Samagotchi
|
|
|
377
395
|
return usage_error("at most #{MAX_IMAGES} images") if options[:images].size > MAX_IMAGES
|
|
378
396
|
return new_options(options) if options[:new]
|
|
379
397
|
|
|
380
|
-
%i[dir model continues thinking toolkit].each { |key| return usage_error("--#{key} needs --new") if options[key] }
|
|
398
|
+
%i[dir model continues thinking toolkit driver].each { |key| return usage_error("--#{key} needs --new") if options[key] }
|
|
381
399
|
TOOLKIT_FLAGS.each { |key, flag| return usage_error("#{flag} needs --new") unless Array(options[key]).empty? }
|
|
382
400
|
LLM_CONTEXT_FLAGS.each { |key, flag| return usage_error("#{flag} needs --new") if options[key] }
|
|
383
401
|
return usage_error("--set needs --new") unless Array(options[:sets]).empty?
|
|
@@ -396,6 +414,8 @@ module Samagotchi
|
|
|
396
414
|
|
|
397
415
|
def new_options(options)
|
|
398
416
|
return usage_error("--new takes no session ids: it starts one session") unless options[:ids].empty?
|
|
417
|
+
return usage_error("--driver takes an agent's name: lowercase letters, digits, - and _") if options[:driver] && !AgentCaller.clean(options[:driver])
|
|
418
|
+
return usage_error("--continues keeps the previous session's driver; leave out --driver") if options[:driver] && options[:continues]
|
|
399
419
|
|
|
400
420
|
if options[:continues]
|
|
401
421
|
taken = [("--dir" if options[:dir])] +
|
|
@@ -564,6 +584,8 @@ module Samagotchi
|
|
|
564
584
|
setup = options[:setup_words] || SessionSetup.from_request(llm_context: options[:llm_context], thinking: options[:thinking],
|
|
565
585
|
toolkit: options[:toolkit_value], settings: options[:settings],
|
|
566
586
|
parent_answers: options[:parent_answers])
|
|
587
|
+
# --driver: the agent the owner names as its driver (Session#driver).
|
|
588
|
+
start[:driver] = ClientId.agent(AgentCaller.clean(options[:driver])) if options[:driver]
|
|
567
589
|
SessionAccess::NewSession.new(**start, working_directory: options[:dir], model_name: options[:model], setup: setup)
|
|
568
590
|
end
|
|
569
591
|
|
|
@@ -727,9 +749,24 @@ module Samagotchi
|
|
|
727
749
|
|
|
728
750
|
SENT_COMMAND = "sent as a session command"
|
|
729
751
|
|
|
730
|
-
# chi send's client id
|
|
731
|
-
#
|
|
732
|
-
|
|
752
|
+
# chi send's client id (an agent's, agent:<name>, when one sends:
|
|
753
|
+
# #agent_name), or chi answer's for a parent agent's /continue yes (the
|
|
754
|
+
# worker holds that to its guardrails.parent_answers): an agent's
|
|
755
|
+
# always is one, a marker-less process's when ParentGate says so.
|
|
756
|
+
def client_id_for(prompt)
|
|
757
|
+
continue = @agent ? TurnFlow.continue_yes?(prompt) : ParentGate.parent_continue?(prompt, env: @env)
|
|
758
|
+
continue ? ClientId::CLI_ANSWER : AgentCaller.send_id(name: @agent)
|
|
759
|
+
end
|
|
760
|
+
|
|
761
|
+
# Who sends (AgentCaller.send_name): the agent's name, :owner for none,
|
|
762
|
+
# or nil after a usage error (an --as that is not a name).
|
|
763
|
+
def agent_name(options)
|
|
764
|
+
name, error, note = AgentCaller.send_name(as: options[:as], env: @env)
|
|
765
|
+
return usage_error(error) && nil if error
|
|
766
|
+
|
|
767
|
+
@stderr.puts("chi send: #{note}") if note
|
|
768
|
+
name || :owner
|
|
769
|
+
end
|
|
733
770
|
|
|
734
771
|
# Whether +text+ may be a session command, which only its worker knows
|
|
735
772
|
# (a bundle's too).
|
data/lib/samagotchi/session.rb
CHANGED
|
@@ -290,6 +290,14 @@ module Samagotchi
|
|
|
290
290
|
# continue) and an idle session. Set once, by spawn_session; a
|
|
291
291
|
# continue and a fork don't carry it.
|
|
292
292
|
SessionField.plain("first_turn_client_id", nil, optional: true),
|
|
293
|
+
# The agent that drives this session (agent:<name>, a ClientId): the
|
|
294
|
+
# one that created it (spawn_session, a prompt or none), or the one
|
|
295
|
+
# the owner named (chi send --new --driver, /driver). Its messages are
|
|
296
|
+
# as chi send's were (no header, never counted: Visitor); any other
|
|
297
|
+
# agent's are a visitor's. A continue keeps it
|
|
298
|
+
# (SessionContinue.spawn_continuation); a fork and a delegate child
|
|
299
|
+
# have none. nil: no agent drives it.
|
|
300
|
+
SessionField.plain("driver", nil, optional: true),
|
|
293
301
|
# A Verify fresh child's requester, rules and claim (VerifyFreshOrigin),
|
|
294
302
|
# nil for any other session. Set once, at spawn (VerifyFresh.start).
|
|
295
303
|
# Autoloaded on the first one (the bundle test kit's load budget).
|
|
@@ -309,7 +317,7 @@ module Samagotchi
|
|
|
309
317
|
attr_accessor :id, :metadata_version, :mode, :model_name, :model_typed, :working_directory, :messages,
|
|
310
318
|
:created_at, :updated_at, :status, :last_prompt, :first_preview, :title, :test_run,
|
|
311
319
|
:used_memory_names, :preloaded_memory_names, :muted_memory_names, :parent_id, :scratch,
|
|
312
|
-
:continues, :first_turn_id, :first_turn_client_id, :project_id
|
|
320
|
+
:continues, :first_turn_id, :first_turn_client_id, :driver, :project_id
|
|
313
321
|
attr_writer :project_root, :delegate
|
|
314
322
|
attr_reader :appended_prompt_files, :written_by
|
|
315
323
|
|
|
@@ -4,6 +4,7 @@ require_relative "exchange"
|
|
|
4
4
|
require_relative "exchange/token"
|
|
5
5
|
require_relative "../bridge_client"
|
|
6
6
|
require_relative "../bridge_secret"
|
|
7
|
+
require_relative "../agent_caller"
|
|
7
8
|
require_relative "../client_id"
|
|
8
9
|
require_relative "../parent_answers_command"
|
|
9
10
|
require_relative "../reply_wait/target"
|
|
@@ -95,9 +96,12 @@ module Samagotchi
|
|
|
95
96
|
# waited on again, nothing sent (Reply rebound), unless it only waits
|
|
96
97
|
# as an input file with no worker running (#waiting_turn?); else the
|
|
97
98
|
# delivery carries the id, which the Bridge dedupes
|
|
99
|
+
# @param anchors [TurnAnchors, Hash, nil] what the turn points at
|
|
100
|
+
# (TurnAnchors), on every delivery (each relocation after a
|
|
101
|
+
# continue's too); nil: none sent, as before
|
|
98
102
|
# @return [Reply, Ran, Refused]
|
|
99
103
|
def call(given, prompt:, client_id:, queue:, timeout:, cap:, continuing_wait: Exchange::CONTINUING_WAIT,
|
|
100
|
-
cancelled: nil, request_id: nil)
|
|
104
|
+
cancelled: nil, request_id: nil, anchors: nil)
|
|
101
105
|
enqueued_id = SessionInbox.enqueued_id_for("ask:#{request_id}") if request_id
|
|
102
106
|
deadline = @clock.call + cap
|
|
103
107
|
# Everything before the wait ends by here, so its MIN_SLICE fits.
|
|
@@ -126,7 +130,7 @@ module Samagotchi
|
|
|
126
130
|
return Refused.new(kind: :busy, session_id: at.id, busy: busy) if busy
|
|
127
131
|
|
|
128
132
|
start ||= @access.prepare_wait(at.id, session: session)
|
|
129
|
-
more =
|
|
133
|
+
more = { enqueued_id: enqueued_id, anchors: anchors }.compact
|
|
130
134
|
result = @exchange.attempt(at, prompt, client_id: client_id, queue: queue, **more).result
|
|
131
135
|
if result.continued?
|
|
132
136
|
follow_until ||= [@clock.call + continuing_wait, ready_by].min
|
|
@@ -164,7 +168,9 @@ module Samagotchi
|
|
|
164
168
|
# session asked and its chain's newest link); none: config.yml's
|
|
165
169
|
# value alone (fail closed)
|
|
166
170
|
# @return [String, Refused] the client_id to send as, or why not
|
|
167
|
-
|
|
171
|
+
# @param name [String, nil] the agent's name (AgentCaller): its line
|
|
172
|
+
# goes as agent:<name>; nil: chi send's
|
|
173
|
+
def agent_line(prompt, session:, name: nil)
|
|
168
174
|
sessions = Array(session).compact
|
|
169
175
|
values = sessions.empty? ? [nil] : sessions.map(&:parent_answers)
|
|
170
176
|
continue = TurnFlow.continue_yes?(prompt)
|
|
@@ -172,7 +178,7 @@ module Samagotchi
|
|
|
172
178
|
refusal ||= ParentAnswersCommand::REFUSED if SendCommand::ParentGate.loosens_any?(prompt, values)
|
|
173
179
|
return Refused.new(kind: :parent_refused, session_id: sessions.first&.id, message: refusal) if refusal
|
|
174
180
|
|
|
175
|
-
continue ? ClientId::CLI_ANSWER :
|
|
181
|
+
continue ? ClientId::CLI_ANSWER : AgentCaller.send_id(name: name)
|
|
176
182
|
end
|
|
177
183
|
|
|
178
184
|
# The answer to the question session +id+ waits on, then one slice of
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "json"
|
|
4
4
|
require_relative "../../session"
|
|
5
|
+
require_relative "../../reply_for"
|
|
5
6
|
require_relative "../../reply_wait/target"
|
|
6
7
|
|
|
7
8
|
module Samagotchi
|
|
@@ -11,7 +12,10 @@ module Samagotchi
|
|
|
11
12
|
# POST …/wait calls: "w1." and the url-safe base64 (no padding) of
|
|
12
13
|
# {s: session_id, t: turn, r: report_turn_id, q: queued, e:
|
|
13
14
|
# ended_before, c: cursor, b: baseline ({messages, question_id,
|
|
14
|
-
# last_turn}) or nil, g: owner_grace
|
|
15
|
+
# last_turn}) or nil, g: owner_grace, f: the asking surface's
|
|
16
|
+
# ReplyFor values (an ask's reply_for; left out without one, so such a
|
|
17
|
+
# token's bytes are as before, and a token made before it strips
|
|
18
|
+
# nothing)}.
|
|
15
19
|
#
|
|
16
20
|
# Unsigned on purpose: every field is one the owner may already put
|
|
17
21
|
# in a POST …/wait body (only the owner's routes take a token);
|
|
@@ -20,7 +24,7 @@ module Samagotchi
|
|
|
20
24
|
# helpers (SessionInbox). A token says nothing a body couldn't.
|
|
21
25
|
module Token
|
|
22
26
|
PREFIX = "w1."
|
|
23
|
-
KEYS = %w[s t r q e c b g].freeze
|
|
27
|
+
KEYS = %w[s t r q e c b g f].freeze
|
|
24
28
|
STRINGS = %w[t r c].freeze
|
|
25
29
|
FLAGS = %w[q e].freeze
|
|
26
30
|
BASELINE_FIELDS = { "messages" => Integer, "question_id" => String, "last_turn" => String }.freeze
|
|
@@ -32,24 +36,55 @@ module Samagotchi
|
|
|
32
36
|
module_function
|
|
33
37
|
|
|
34
38
|
# @param target [ReplyWait::Target]
|
|
39
|
+
# @param reply_for [ReplyFor, nil] the asking surface's (its answers
|
|
40
|
+
# shaped for it: Web::SessionAccessRoutes#wait_result_json)
|
|
35
41
|
# @return [String]
|
|
36
|
-
def dump(session_id, target)
|
|
42
|
+
def dump(session_id, target, reply_for: nil)
|
|
37
43
|
fields = { s: session_id, t: target.turn, r: target.report_turn_id, q: target.queued == true,
|
|
38
44
|
e: target.ended_before == true, c: target.cursor, b: target.baseline&.to_h, g: target.owner_grace }
|
|
39
|
-
|
|
45
|
+
fields[:f] = reply_for.to_file if reply_for
|
|
46
|
+
encode(fields)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# +string+ (a token, or nil) carrying +reply_for+ too; as it is
|
|
50
|
+
# without one.
|
|
51
|
+
# @raise [Invalid]
|
|
52
|
+
def carrying(string, reply_for)
|
|
53
|
+
return string if string.nil? || reply_for.nil?
|
|
54
|
+
|
|
55
|
+
encode(decode(checked(string)).merge("f" => reply_for.to_file))
|
|
40
56
|
end
|
|
41
57
|
|
|
42
58
|
# @return [Array(String, ReplyWait::Target)] the session id and the wait
|
|
43
59
|
# @raise [Invalid]
|
|
44
60
|
def load(string)
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
fields = decode(string.delete_prefix(PREFIX))
|
|
61
|
+
fields = decode(checked(string))
|
|
48
62
|
raise Invalid, "a wait token names unknown fields" unless (fields.keys - KEYS).empty?
|
|
49
63
|
|
|
64
|
+
reply_for_of(fields["f"])
|
|
50
65
|
[session_of(fields["s"]), target_of(fields)]
|
|
51
66
|
end
|
|
52
67
|
|
|
68
|
+
# The asking surface a token carries (#dump's reply_for:): nil for
|
|
69
|
+
# none, or a token made before it.
|
|
70
|
+
# @return [ReplyFor, nil]
|
|
71
|
+
# @raise [Invalid]
|
|
72
|
+
def reply_for(string) = reply_for_of(decode(checked(string))["f"])
|
|
73
|
+
|
|
74
|
+
def checked(string)
|
|
75
|
+
raise Invalid, "not a wait token" unless string.is_a?(String) && string.start_with?(PREFIX)
|
|
76
|
+
|
|
77
|
+
string.delete_prefix(PREFIX)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def encode(fields) = PREFIX + [JSON.generate(fields)].pack("m0").tr("+/", "-_").delete("=")
|
|
81
|
+
|
|
82
|
+
def reply_for_of(raw)
|
|
83
|
+
return nil if raw.nil?
|
|
84
|
+
|
|
85
|
+
ReplyFor.from(raw) || raise(Invalid, "a wait token's f is reply_for's values")
|
|
86
|
+
end
|
|
87
|
+
|
|
53
88
|
# A wait body's or a token's baseline: nil, or a Baseline from
|
|
54
89
|
# exactly its fields, each of its type or null.
|
|
55
90
|
# @return [ReplyWait::Baseline, nil]
|