samagotchi 0.185.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 +24 -1
- data/docs/configuration.md +18 -0
- data/docs/guardrails.md +13 -0
- data/docs/internals/bridge.md +1 -0
- data/docs/internals/engine.md +15 -1
- data/docs/internals/prompt-caching.md +14 -0
- data/docs/internals/remote-sessions.md +7 -0
- data/docs/thin-clients.md +67 -2
- data/lib/samagotchi/bridge.rb +1 -1
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/config.rb +3 -1
- data/lib/samagotchi/context_absorber.rb +5 -2
- data/lib/samagotchi/engine.rb +9 -2
- data/lib/samagotchi/llm/chat_loop.rb +16 -2
- data/lib/samagotchi/plain_reply.rb +75 -0
- data/lib/samagotchi/reply_for.rb +254 -0
- data/lib/samagotchi/session_access/ask.rb +5 -2
- 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 +4 -2
- data/lib/samagotchi/session_access/remote.rb +5 -1
- data/lib/samagotchi/session_manager/delivery.rb +36 -6
- data/lib/samagotchi/steer.rb +30 -8
- data/lib/samagotchi/turn_anchors.rb +65 -17
- data/lib/samagotchi/turn_checkpoint.rb +5 -3
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +1 -1
- data/lib/samagotchi/web/ask_routes.rb +62 -17
- data/lib/samagotchi/web/participant_access.rb +1 -1
- data/lib/samagotchi/web/public/peer_text.js +22 -5
- data/lib/samagotchi/web/session_access_routes.rb +30 -2
- data/lib/samagotchi/web/session_control.rb +21 -2
- data/lib/samagotchi/worker.rb +2 -1
- metadata +3 -1
|
@@ -1,51 +1,99 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "reply_for"
|
|
3
4
|
require_relative "revise_anchor"
|
|
4
5
|
require_relative "verify_anchor"
|
|
5
6
|
|
|
6
7
|
module Samagotchi
|
|
7
|
-
# What a turn points at
|
|
8
|
-
#
|
|
8
|
+
# What a turn points at: a Verify turn's claim (VerifyAnchor), a Revise
|
|
9
|
+
# turn's draft (ReviseAnchor) and the surface its reply is for (ReplyFor,
|
|
10
|
+
# an ask's reply_for). One carrier
|
|
9
11
|
# through every layer a turn's input travels (chi web's POST /turn, the
|
|
10
12
|
# Bridge, the input file, SessionInbox::Input, WorkerInbound::Prompt, a
|
|
11
13
|
# failed turn's restore), so a new anchor is one field here. On the wire
|
|
12
14
|
# and in the input file each is its own key ("verify", "revises"); a
|
|
13
15
|
# missing or malformed one is nil. The Engine takes them as run_turn's
|
|
14
|
-
# keywords (#kwargs): what each means is its own.
|
|
15
|
-
|
|
16
|
+
# keywords (#kwargs): what each means is its own. What a layer does
|
|
17
|
+
# with a member by kind (held for the turn's end, shown to a
|
|
18
|
+
# participant) is that member's rule (TurnAnchors::RULES), never a
|
|
19
|
+
# list of names.
|
|
20
|
+
TurnAnchors = Data.define(:verify, :revises, :reply_for) do
|
|
16
21
|
def self.from(value)
|
|
17
22
|
return value if value.is_a?(self)
|
|
18
23
|
|
|
19
24
|
fields = value.is_a?(Hash) ? value.to_h { |key, entry| [key.to_s, entry] } : {}
|
|
20
|
-
new(verify: fields["verify"], revises: fields["revises"])
|
|
25
|
+
new(verify: fields["verify"], revises: fields["revises"], reply_for: fields["reply_for"])
|
|
21
26
|
end
|
|
22
27
|
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
-
|
|
28
|
+
# A chi web POST …/turn body's anchors (the owner's composer, a
|
|
29
|
+
# participant's): only the members whose rule takes them from there
|
|
30
|
+
# (+turn_route+); the others are set by their own route alone.
|
|
31
|
+
def self.from_turn_route(body)
|
|
32
|
+
anchors = from(body)
|
|
33
|
+
anchors.with(**self::RULES.reject { |_, rule| rule.turn_route }.to_h { |member, _| [member, nil] })
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Whether a parsed input file names an anchor whose rule holds it for
|
|
37
|
+
# the turn's end (a Hash under its key, well-formed or not):
|
|
38
|
+
# SessionInbox.turn_end_data? holds it.
|
|
39
|
+
def self.named_in?(data) = self::RULES.any? { |member, rule| rule.holds && data[member.to_s].is_a?(Hash) }
|
|
26
40
|
|
|
27
|
-
def initialize(verify: nil, revises: nil)
|
|
28
|
-
super(verify: VerifyAnchor.from(verify), revises: ReviseAnchor.from(revises))
|
|
41
|
+
def initialize(verify: nil, revises: nil, reply_for: nil)
|
|
42
|
+
super(verify: VerifyAnchor.from(verify), revises: ReviseAnchor.from(revises), reply_for: ReplyFor.from(reply_for))
|
|
29
43
|
end
|
|
30
44
|
|
|
31
|
-
def empty? =
|
|
45
|
+
def empty? = member_values.none?
|
|
32
46
|
|
|
33
47
|
# The anchors given, each answering FEATURE (the worker's Bridge
|
|
34
48
|
# feature it needs) and UNAVAILABLE (the refusal without it).
|
|
35
|
-
def present =
|
|
49
|
+
def present = member_values.compact
|
|
50
|
+
|
|
51
|
+
# The anchors given whose rule has an input file written for them only
|
|
52
|
+
# when the reading worker's sidecar lists their FEATURE (+vouched+: the
|
|
53
|
+
# no-Bridge fallback, SessionManager::Delivery).
|
|
54
|
+
def vouched = self.class::RULES.filter_map { |member, rule| public_send(member) if rule.vouched }.compact
|
|
36
55
|
|
|
37
56
|
# The body's and the input file's keys.
|
|
38
|
-
def to_file = {
|
|
57
|
+
def to_file = self.class.members.to_h { |member| [member.to_s, public_send(member)&.to_file] }.compact
|
|
39
58
|
|
|
40
59
|
# A :prompt_restored event's fields.
|
|
41
|
-
def to_h = {
|
|
60
|
+
def to_h = self.class.members.to_h { |member| [member, public_send(member)&.to_h] }.compact
|
|
42
61
|
|
|
43
|
-
# What a participant sees of each anchor
|
|
62
|
+
# What a participant sees of each anchor whose rule shows it to them
|
|
63
|
+
# (its participant_view); a member without one is left out.
|
|
44
64
|
def participant_view
|
|
45
|
-
self.class.members.
|
|
65
|
+
self.class.members.each_with_object({}) do |member, view|
|
|
66
|
+
next unless self.class::RULES.fetch(member).participant
|
|
67
|
+
|
|
68
|
+
shown = public_send(member)&.participant_view
|
|
69
|
+
view[member.to_s] = shown unless shown.nil?
|
|
70
|
+
end
|
|
46
71
|
end
|
|
47
72
|
|
|
48
73
|
# Engine#run_turn's keywords.
|
|
49
|
-
def kwargs = {
|
|
74
|
+
def kwargs = self.class.members.to_h { |member| [member, public_send(member)] }.compact
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
def member_values = self.class.members.map { |member| public_send(member) }
|
|
50
79
|
end
|
|
80
|
+
|
|
81
|
+
# A member's rules: +holds+, an input file naming it waits for the
|
|
82
|
+
# running turn's end (#named_in?: merged into that turn as a steer, the
|
|
83
|
+
# anchor would be lost); +participant+, a participant sees its
|
|
84
|
+
# participant_view (#participant_view; false: left out); +turn_route+,
|
|
85
|
+
# chi web's POST …/turn takes it from a body (.from_turn_route; false:
|
|
86
|
+
# its own route alone sets it); +vouched+, the no-Bridge fallback writes
|
|
87
|
+
# its input file only for a worker whose sidecar lists its FEATURE
|
|
88
|
+
# (#vouched; false: the file goes as it is).
|
|
89
|
+
TurnAnchors::Rule = Data.define(:holds, :participant, :turn_route, :vouched)
|
|
90
|
+
# One per member, in the members' order.
|
|
91
|
+
TurnAnchors::RULES = {
|
|
92
|
+
verify: TurnAnchors::Rule.new(holds: true, participant: true, turn_route: true, vouched: false),
|
|
93
|
+
revises: TurnAnchors::Rule.new(holds: true, participant: true, turn_route: true, vouched: false),
|
|
94
|
+
# An ask's (POST …/ask): its own turn, never shown to a participant.
|
|
95
|
+
# A worker older than it would drop it from the file, so the file goes
|
|
96
|
+
# only to one that vouches for it.
|
|
97
|
+
reply_for: TurnAnchors::Rule.new(holds: true, participant: false, turn_route: false, vouched: true)
|
|
98
|
+
}.freeze
|
|
51
99
|
end
|
|
@@ -200,8 +200,10 @@ module Samagotchi
|
|
|
200
200
|
# it theirs; {} for the owner's
|
|
201
201
|
# +source+: a visitor's opener's sender (Steer::OPENER_HEADERS), saved
|
|
202
202
|
# on it as the Engine saves it (a turn killed before its first boundary
|
|
203
|
-
# keeps its header); nil for the owner's and the driver's.
|
|
204
|
-
|
|
203
|
+
# keeps its header); nil for the owner's and the driver's. +reply+:
|
|
204
|
+
# the opener's ReplyFor.opener_fields, saved on it as the Engine saves
|
|
205
|
+
# them (its line is on the wire after a kill too); {} for none.
|
|
206
|
+
def begin_turn(prompt: nil, turn_id: nil, enqueued_id: nil, author: {}, source: nil, reply: {})
|
|
205
207
|
@boundaries = 0
|
|
206
208
|
@enqueued_id = enqueued_id.to_s.empty? ? nil : enqueued_id.to_s
|
|
207
209
|
@author = author[:author]
|
|
@@ -212,7 +214,7 @@ module Samagotchi
|
|
|
212
214
|
# stamp (Engine's Turn#wall), which the turn's first boundary saves.
|
|
213
215
|
unless prompt.to_s.empty?
|
|
214
216
|
messages << { role: "user", content: prompt, **(turn_id ? { turn_id: turn_id } : {}), **author,
|
|
215
|
-
**(source ? { source: source } : {}), MessageTime::KEY => MessageTime.iso(MessageTime.now) }
|
|
217
|
+
**(source ? { source: source } : {}), MessageTime::KEY => MessageTime.iso(MessageTime.now), **reply }
|
|
216
218
|
end
|
|
217
219
|
@session.turn_checkpoint = { messages: messages, marker: InterruptedTurn.new(started_at: @started_at, at: @started_at, iteration: 0,
|
|
218
220
|
enqueued_id: @enqueued_id, author: @author) }
|
data/lib/samagotchi/version.rb
CHANGED
data/lib/samagotchi/web/app.rb
CHANGED
|
@@ -1126,7 +1126,7 @@ module Samagotchi
|
|
|
1126
1126
|
# delegates: chi --remote sessions delete --dry-run asks for this first.
|
|
1127
1127
|
DELETE_PLAN_FEATURE = "delete_plan_all"
|
|
1128
1128
|
INFO_FEATURES = ["dir", "newest_workers", SessionAccessRoutes::FEATURE, SessionAccessRoutes::ATTACH_FEATURE,
|
|
1129
|
-
BOUND_FEATURE, CHAIN_DELETE_FEATURE, STOP_TASKS_FEATURE, DELETE_PLAN_FEATURE, ASK_FEATURE, McpRoute::FEATURE, Delivery::AGENT_FEATURE].freeze
|
|
1129
|
+
BOUND_FEATURE, CHAIN_DELETE_FEATURE, STOP_TASKS_FEATURE, DELETE_PLAN_FEATURE, ASK_FEATURE, McpRoute::FEATURE, Delivery::AGENT_FEATURE, ReplyFor::FEATURE].freeze
|
|
1130
1130
|
|
|
1131
1131
|
# The models a new session can start on, spelled as chi spells them
|
|
1132
1132
|
# elsewhere: bare for the default host (what default.model holds), and
|
|
@@ -4,6 +4,7 @@ require "delegate"
|
|
|
4
4
|
require_relative "../bridge_client"
|
|
5
5
|
require_relative "../agent_caller"
|
|
6
6
|
require_relative "../client_id"
|
|
7
|
+
require_relative "../command_intake"
|
|
7
8
|
require_relative "../reply_wait"
|
|
8
9
|
require_relative "../session"
|
|
9
10
|
require_relative "../session_command_catalog"
|
|
@@ -12,6 +13,8 @@ require_relative "../turn_flow"
|
|
|
12
13
|
require_relative "../session_access/ask"
|
|
13
14
|
require_relative "../session_access/exchange"
|
|
14
15
|
require_relative "../session_access/exchange/token"
|
|
16
|
+
require_relative "../reply_for"
|
|
17
|
+
require_relative "../turn_anchors"
|
|
15
18
|
require_relative "request_body"
|
|
16
19
|
|
|
17
20
|
module Samagotchi
|
|
@@ -82,12 +85,14 @@ module Samagotchi
|
|
|
82
85
|
private
|
|
83
86
|
|
|
84
87
|
# POST /api/sessions/:id/ask {prompt, timeout, queue, client_id,
|
|
85
|
-
# request_id}: the message delivered (to a chain's newest link; a
|
|
88
|
+
# request_id, reply_for}: the message delivered (to a chain's newest link; a
|
|
86
89
|
# running session queues it as its own turn unless queue is false) and
|
|
87
90
|
# its answer waited for, all within ASK_MAX_SECONDS. A request_id (a
|
|
88
91
|
# message's, not a command's) names its turn: a retry whose turn the
|
|
89
92
|
# session knows waits on it again, nothing sent and no second history
|
|
90
|
-
# line (SessionAccess::Ask#call).
|
|
93
|
+
# line (SessionAccess::Ask#call). A reply_for (ReplyFor.parse: a name
|
|
94
|
+
# or the values) rides a message's turn as its anchor (a line the
|
|
95
|
+
# Bridge runs as a command, #runs_as_command?: kept in hand only). 200 wait_result_json's fields +
|
|
91
96
|
# session_id (+ asked, the id given, when another link ran it; +
|
|
92
97
|
# token when the status is timeout: POST …/wait {token} goes on; +
|
|
93
98
|
# request_id when one was given); 200 {status: command} for a session
|
|
@@ -103,6 +108,11 @@ module Samagotchi
|
|
|
103
108
|
prompt, client_id, options, error = ask_request(id, body)
|
|
104
109
|
return error if error
|
|
105
110
|
|
|
111
|
+
reply_for, error = ask_reply_for(body)
|
|
112
|
+
return error if error
|
|
113
|
+
|
|
114
|
+
options[:anchors] = TurnAnchors.new(reply_for: reply_for) if reply_for && !runs_as_command?(prompt, client_id)
|
|
115
|
+
|
|
106
116
|
gated = ask_gate(id, prompt, client_id)
|
|
107
117
|
return gated if gated
|
|
108
118
|
|
|
@@ -116,7 +126,7 @@ module Samagotchi
|
|
|
116
126
|
cancelled: stopping, **options)
|
|
117
127
|
rebound = outcome.is_a?(SessionAccess::Ask::Reply) && outcome.rebound
|
|
118
128
|
record_history(body, prompt) unless outcome.is_a?(SessionAccess::Ask::Refused) || rebound
|
|
119
|
-
ask_response(id, outcome, request_id: options[:request_id])
|
|
129
|
+
ask_response(id, outcome, request_id: options[:request_id], reply_for: reply_for)
|
|
120
130
|
rescue SessionManager::OwnedByTUI => e
|
|
121
131
|
owned_by_tui(e)
|
|
122
132
|
end
|
|
@@ -153,6 +163,34 @@ module Samagotchi
|
|
|
153
163
|
[nil, nil, nil, error_response(400, "invalid_prompt", e.message)]
|
|
154
164
|
end
|
|
155
165
|
|
|
166
|
+
# The body's reply_for: [nil or a ReplyFor, nil] or [nil, a 400
|
|
167
|
+
# invalid_ask]. A name is resolved here (config.yml's surfaces:, read
|
|
168
|
+
# now), so the session saves values: a later config edit leaves old
|
|
169
|
+
# turns as they were.
|
|
170
|
+
def ask_reply_for(body)
|
|
171
|
+
[ReplyFor.parse(body["reply_for"]), nil]
|
|
172
|
+
rescue ReplyFor::Invalid => e
|
|
173
|
+
[nil, error_response(400, "invalid_ask", e.message)]
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Whether the Bridge runs +prompt+ from +sender+ as a session command
|
|
177
|
+
# or a shell line (Bridge#handle_post_turn's rule: the registry's
|
|
178
|
+
# command? and CommandIntake.allowed?), so a turn's anchor would be
|
|
179
|
+
# dropped: an unknown /word ("/tmp is full, what now?") is a message
|
|
180
|
+
# and keeps it. chi web knows the built-ins only, so a bundle's command
|
|
181
|
+
# carries the anchor too, which the worker ignores as it runs it.
|
|
182
|
+
def runs_as_command?(prompt, sender)
|
|
183
|
+
SessionCommandCatalog.builtin_registry.command?(prompt) && CommandIntake.allowed?(sender)
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# A line a worker may run as a session command or a shell line, by its
|
|
187
|
+
# shape (COMMAND_SHAPE): a request_id is refused for it (#ask_request_id),
|
|
188
|
+
# a bundle's command included.
|
|
189
|
+
def command_line?(prompt)
|
|
190
|
+
line = prompt.strip
|
|
191
|
+
line.start_with?("!") || COMMAND_SHAPE.match?(line)
|
|
192
|
+
end
|
|
193
|
+
|
|
156
194
|
# The body's request_id: [nil or it, nil] or [nil, a 400]. A session
|
|
157
195
|
# command never runs as a turn of that id (the Bridge's command path
|
|
158
196
|
# takes none), so a retry would run it again: refused. Any line the
|
|
@@ -165,8 +203,7 @@ module Samagotchi
|
|
|
165
203
|
return [nil, error_response(400, "invalid_ask", "request_id is 1 to 128 printable ASCII characters")]
|
|
166
204
|
end
|
|
167
205
|
|
|
168
|
-
|
|
169
|
-
if line.start_with?("!") || COMMAND_SHAPE.match?(line)
|
|
206
|
+
if command_line?(prompt)
|
|
170
207
|
return [nil, error_response(400, "invalid_ask", "request_id is for messages, not session commands")]
|
|
171
208
|
end
|
|
172
209
|
|
|
@@ -203,18 +240,21 @@ module Samagotchi
|
|
|
203
240
|
end
|
|
204
241
|
|
|
205
242
|
# The ask's answer for an Ask value (+request_id+ echoed when given:
|
|
206
|
-
# the client sees this chi web used it)
|
|
207
|
-
|
|
243
|
+
# the client sees this chi web used it), shaped for +reply_for+ (the
|
|
244
|
+
# request's: a /continue yes's too, which opens no message), its token
|
|
245
|
+
# carrying it for the next wait.
|
|
246
|
+
def ask_response(id, outcome, request_id: nil, reply_for: nil)
|
|
208
247
|
case outcome
|
|
209
248
|
when SessionAccess::Ask::Ran then json_response(200, { status: "command", session_id: outcome.session_id })
|
|
210
|
-
when SessionAccess::Ask::Refused then ask_refused(id, outcome)
|
|
249
|
+
when SessionAccess::Ask::Refused then ask_refused(id, outcome, reply_for: reply_for)
|
|
211
250
|
else
|
|
212
251
|
result = outcome.result
|
|
213
252
|
return error_response(503, "server_stopping", "chi web is stopping; wait again") if result.status == :canceled
|
|
214
253
|
|
|
215
|
-
json = wait_result_json(result, session_access.parent_strategy(outcome.session_id)
|
|
254
|
+
json = wait_result_json(result, session_access.parent_strategy(outcome.session_id), reply_for: reply_for)
|
|
255
|
+
.merge(session_id: outcome.session_id)
|
|
216
256
|
json[:asked] = outcome.asked if outcome.asked && outcome.asked != outcome.session_id
|
|
217
|
-
json[:token] = outcome.token if outcome.token
|
|
257
|
+
json[:token] = SessionAccess::Exchange::Token.carrying(outcome.token, reply_for) if outcome.token
|
|
218
258
|
json[:request_id] = request_id if request_id
|
|
219
259
|
json_response(200, json)
|
|
220
260
|
end
|
|
@@ -224,7 +264,7 @@ module Samagotchi
|
|
|
224
264
|
ASK_BUSY_WORDS = { "busy" => "a turn is running: ask with queue, or wait for it",
|
|
225
265
|
"waiting" => "a question waits for an answer" }.freeze
|
|
226
266
|
|
|
227
|
-
def ask_refused(id, refused)
|
|
267
|
+
def ask_refused(id, refused, reply_for: nil)
|
|
228
268
|
case refused.kind
|
|
229
269
|
when :canceled then error_response(503, "server_stopping", "chi web is stopping; ask again")
|
|
230
270
|
when :unknown
|
|
@@ -235,7 +275,9 @@ module Samagotchi
|
|
|
235
275
|
when :continuing then refusal(409, "continuing", refused.message, session_id: refused.session_id)
|
|
236
276
|
when :busy
|
|
237
277
|
words = ASK_BUSY_WORDS.fetch(refused.busy.error)
|
|
238
|
-
|
|
278
|
+
json = busy_json(refused.busy).merge(detail: words, message: words)
|
|
279
|
+
json[:question] = question_json(refused.busy.question, reply_for) if refused.busy.question
|
|
280
|
+
json_response(409, json.merge(needs_detail_fields(reply_for, refused.busy.question)))
|
|
239
281
|
else refused_delivery(refused.result)
|
|
240
282
|
end
|
|
241
283
|
end
|
|
@@ -258,6 +300,7 @@ module Samagotchi
|
|
|
258
300
|
end
|
|
259
301
|
|
|
260
302
|
session_id, target = SessionAccess::Exchange::Token.load(body["token"])
|
|
303
|
+
reply_for = SessionAccess::Exchange::Token.reply_for(body["token"])
|
|
261
304
|
# The URL's id may be a prefix (as the ask's): resolved first (no
|
|
262
305
|
# such session, or several: session_route's 404).
|
|
263
306
|
full = session_access.resolve(id)
|
|
@@ -267,7 +310,7 @@ module Samagotchi
|
|
|
267
310
|
**wait_extras)
|
|
268
311
|
return error_response(503, "server_stopping", "chi web is stopping; wait again") if result.status == :canceled
|
|
269
312
|
|
|
270
|
-
json = wait_result_json(result, session_access.parent_strategy(full)).merge(session_id: full)
|
|
313
|
+
json = wait_result_json(result, session_access.parent_strategy(full), reply_for: reply_for).merge(session_id: full)
|
|
271
314
|
json[:token] = body["token"] if result.status == :timeout
|
|
272
315
|
json_response(200, json)
|
|
273
316
|
rescue SessionAccess::Exchange::Token::Invalid => e
|
|
@@ -301,8 +344,10 @@ module Samagotchi
|
|
|
301
344
|
# went in before the wait, so a wait that fails (chi web stopping, the
|
|
302
345
|
# session deleted, its files unreadable) is still a 200: wait_error
|
|
303
346
|
# {error: server_stopping | not_found | wait_failed, detail} in place
|
|
304
|
-
# of reply (an answer retried would be 409).
|
|
305
|
-
|
|
347
|
+
# of reply (an answer retried would be 409). +reply_for+: the pending
|
|
348
|
+
# turn's (its saved opener's, read before the answer went in), which
|
|
349
|
+
# shapes the reply and rides its token.
|
|
350
|
+
def answered_wait(id, question_id, start, seconds, answered:, reply_for: nil)
|
|
306
351
|
head = answered ? { status: "answered" } : { status: "not_pending", answered_here: false }
|
|
307
352
|
head = { **head, session_id: id, id: question_id }
|
|
308
353
|
target = ReplyWait::Target.answer(start)
|
|
@@ -311,8 +356,8 @@ module Samagotchi
|
|
|
311
356
|
return answer_wait_error(head, "server_stopping", "chi web is stopping; the answer went in")
|
|
312
357
|
end
|
|
313
358
|
|
|
314
|
-
reply = wait_result_json(result, session_access.parent_strategy(id))
|
|
315
|
-
reply[:token] = SessionAccess::Exchange::Token.dump(id, target) if result.status == :timeout
|
|
359
|
+
reply = wait_result_json(result, session_access.parent_strategy(id), reply_for: reply_for)
|
|
360
|
+
reply[:token] = SessionAccess::Exchange::Token.dump(id, target, reply_for: reply_for) if result.status == :timeout
|
|
316
361
|
json_response(200, { **head, reply: reply })
|
|
317
362
|
rescue ArgumentError => e # deleted while waiting
|
|
318
363
|
answer_wait_error(head, "not_found", e.message)
|
|
@@ -326,7 +326,7 @@ module Samagotchi
|
|
|
326
326
|
prompt = body["prompt"].to_s
|
|
327
327
|
return error_response(400, "missing_fields", "prompt is required") if prompt.strip.empty?
|
|
328
328
|
|
|
329
|
-
anchors = TurnAnchors.
|
|
329
|
+
anchors = TurnAnchors.from_turn_route(body)
|
|
330
330
|
revises = anchors.revises&.turn_id
|
|
331
331
|
reason = participant_turn_refusal(grant, prompt, body) || participant_revise_refusal(grant, id, anchors)
|
|
332
332
|
return participant_turn_refused(grant, prompt, *reason, revises: revises) if reason
|
|
@@ -16,17 +16,34 @@ export const PEER_FORGED_OPENINGS = Object.freeze([
|
|
|
16
16
|
"tool results", "images from tool results",
|
|
17
17
|
]);
|
|
18
18
|
// ContextAbsorber::PEER_FORGED_LINES: chi's unbracketed lines it quotes.
|
|
19
|
-
export const PEER_FORGED_LINES = Object.freeze(["chi started this turn"]);
|
|
19
|
+
export const PEER_FORGED_LINES = Object.freeze(["chi started this turn", "Updated:", "Attached:"]);
|
|
20
20
|
|
|
21
21
|
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
22
22
|
// An opening that ends in a word character must end there (\b).
|
|
23
|
-
const opening = (o) =>
|
|
23
|
+
const opening = (o) => {
|
|
24
|
+
let pattern = "";
|
|
25
|
+
for (let i = 0; i < o.length;) {
|
|
26
|
+
const pair = o.slice(i, i + 2).toLowerCase();
|
|
27
|
+
if (pair === "ss") {
|
|
28
|
+
pattern += "(?:ss|ß)";
|
|
29
|
+
i += 2;
|
|
30
|
+
} else if (pair === "st") {
|
|
31
|
+
pattern += "(?:st|st|ſt)";
|
|
32
|
+
i += 2;
|
|
33
|
+
} else {
|
|
34
|
+
pattern += escape(o[i]);
|
|
35
|
+
i += 1;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return `${pattern}${/\w$/.test(o) ? "\\b" : ""}`;
|
|
39
|
+
};
|
|
24
40
|
// Steer::FORGED_BREAKS and FORGED_LEAD: a line starts after any of these
|
|
25
41
|
// breaks; spaces, format characters and markdown leads may come first.
|
|
26
42
|
const BREAKS = "\\n\\r\\u2028\\u2029\\u0085\\v\\f";
|
|
27
|
-
const LEAD =
|
|
28
|
-
const
|
|
29
|
-
|
|
43
|
+
const LEAD = "(?:(?![" + BREAKS + "])(?:[\\p{White_Space}\\p{Cf}\\p{Default_Ignorable_Code_Point}\\p{Mn}\\u{3164}\\u{2800}>*_`~]|-|\\+|#|\\||[0-9]))*";
|
|
44
|
+
const bracketFrames = `\\[(?:${PEER_FORGED_OPENINGS.map(opening).join("|")})`;
|
|
45
|
+
const lineFrames = `(?:${PEER_FORGED_LINES.map(opening).join("|")})`;
|
|
46
|
+
const QUOTED = new RegExp(`(^|[${BREAKS}])(${LEAD})\\(quoted\\) (?=(?:\\(quoted\\) )?(?:${bracketFrames}|${lineFrames}))`, "giu");
|
|
30
47
|
const OPEN = /^\[PEER MESSAGE from [^\]\n]*\]$/;
|
|
31
48
|
const CLOSE = "[END PEER MESSAGE]";
|
|
32
49
|
|
|
@@ -5,6 +5,8 @@ require_relative "../session_access/local"
|
|
|
5
5
|
require_relative "../session_inbox"
|
|
6
6
|
require_relative "../reply_wait"
|
|
7
7
|
require_relative "../pending_question"
|
|
8
|
+
require_relative "../plain_reply"
|
|
9
|
+
require_relative "../reply_for"
|
|
8
10
|
require_relative "../context_sources"
|
|
9
11
|
require_relative "../peer_exchanges"
|
|
10
12
|
require_relative "../context_command"
|
|
@@ -183,6 +185,25 @@ module Samagotchi
|
|
|
183
185
|
question: busy.question&.to_file, parent_strategy: (strategy_json(busy.strategy) if busy.strategy) }.compact
|
|
184
186
|
end
|
|
185
187
|
|
|
188
|
+
# A question +reply_for+'s surface may not answer (ReplyFor#answers?):
|
|
189
|
+
# needs: "detail", the words to say in its text, the question's id and
|
|
190
|
+
# kind only (no options, no approval's facts); {} for any other.
|
|
191
|
+
def needs_detail_fields(reply_for, question)
|
|
192
|
+
return {} if reply_for.nil? || question.nil? || reply_for.answers?(question)
|
|
193
|
+
|
|
194
|
+
{ needs: "detail", text: reply_for.needs_detail_text(question), question: question.to_file.slice("id", "kind") }
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# The question as +reply_for+'s surface gets it: a plain one's text and
|
|
198
|
+
# header made plain (PlainReply), its options byte-exact (they are
|
|
199
|
+
# answered by label); the saved question is never touched.
|
|
200
|
+
def question_json(question, reply_for)
|
|
201
|
+
json = question&.to_file
|
|
202
|
+
return json unless json && reply_for&.plain?
|
|
203
|
+
|
|
204
|
+
json.merge(%w[question header].to_h { |key| [key, PlainReply.strip(json[key])] }.compact)
|
|
205
|
+
end
|
|
206
|
+
|
|
186
207
|
def strategy_json(strategy) = { name: strategy.name, allowed: strategy.allowed, origin: strategy.origin.to_s }
|
|
187
208
|
|
|
188
209
|
def wait_start_json(id, start)
|
|
@@ -249,11 +270,18 @@ module Samagotchi
|
|
|
249
270
|
|
|
250
271
|
RESULT_FIELDS = %i[text file outcome limit error_kind retryable cancel_reason stopped_by kept_steps turn_id origin].freeze
|
|
251
272
|
|
|
252
|
-
|
|
273
|
+
# +reply_for+: the asking surface's (ReplyFor), passed in by the route
|
|
274
|
+
# (an ask's from its request, a token's from the token, an answer's
|
|
275
|
+
# from the pending turn's saved opener), never looked up after the
|
|
276
|
+
# turn: a plain one gets the reply and its question's words made plain
|
|
277
|
+
# (PlainReply, #question_json), here only.
|
|
278
|
+
def wait_result_json(result, strategy, reply_for: nil)
|
|
253
279
|
json = { status: result.status.to_s }
|
|
254
280
|
RESULT_FIELDS.each { |key| json[key] = result[key] }
|
|
255
281
|
json[:text] = json[:text]&.dup&.force_encoding(Encoding::UTF_8)&.scrub
|
|
256
|
-
json[:
|
|
282
|
+
json[:text] = PlainReply.strip(json[:text]) if reply_for&.plain?
|
|
283
|
+
json[:question] = question_json(result.question, reply_for)
|
|
284
|
+
json.merge!(needs_detail_fields(reply_for, result.question)) if result.status == :waiting_for_answer
|
|
257
285
|
json[:parent_strategy] = strategy_json(strategy)
|
|
258
286
|
json
|
|
259
287
|
end
|
|
@@ -5,6 +5,7 @@ require_relative "../client_id"
|
|
|
5
5
|
require_relative "../delivery"
|
|
6
6
|
require_relative "../image_store"
|
|
7
7
|
require_relative "../prompt_history"
|
|
8
|
+
require_relative "../reply_for"
|
|
8
9
|
require_relative "../session"
|
|
9
10
|
require_relative "../session_command_catalog"
|
|
10
11
|
require_relative "../session_manager"
|
|
@@ -57,6 +58,19 @@ module Samagotchi
|
|
|
57
58
|
qid = body["id"]
|
|
58
59
|
return error_response(400, "missing_fields", "id is required") if qid.to_s.strip.empty?
|
|
59
60
|
|
|
61
|
+
# reply_for: the answering surface's (a thin client's, ReplyFor). One
|
|
62
|
+
# without detail may not answer an approval, a spending question or
|
|
63
|
+
# a user-only option (ReplyFor#answers?): 403, nothing relayed.
|
|
64
|
+
begin
|
|
65
|
+
answering = ReplyFor.parse(body["reply_for"])
|
|
66
|
+
rescue ReplyFor::Invalid => e
|
|
67
|
+
return error_response(400, "invalid_answer", e.message)
|
|
68
|
+
end
|
|
69
|
+
pending = session.pending_question
|
|
70
|
+
if answering && pending && pending.id.to_s == qid.to_s && !answering.answers?(pending)
|
|
71
|
+
return json_response(403, ReplyFor::NEEDS_DETAIL)
|
|
72
|
+
end
|
|
73
|
+
|
|
60
74
|
# wait: a thin client's answer that also waits for what comes next
|
|
61
75
|
# (AskRoutes#answered_wait), its start read before the answer goes in.
|
|
62
76
|
wait, error = answer_wait_option(body)
|
|
@@ -65,6 +79,11 @@ module Samagotchi
|
|
|
65
79
|
start = answer_wait_start(id, session, qid) if wait
|
|
66
80
|
return session_not_found(id) if wait && !start
|
|
67
81
|
|
|
82
|
+
# The pending turn's surface, from its saved opener (the running
|
|
83
|
+
# turn's checkpoint copy until its first boundary), read before the
|
|
84
|
+
# answer goes in: its reply is shaped for it (#answered_wait).
|
|
85
|
+
reply_for = ReplyFor.of_latest_turn(session.messages) if wait
|
|
86
|
+
|
|
68
87
|
# client_id: only chi answer's (a parent agent's answer, which the
|
|
69
88
|
# worker holds to its guardrails.parent_answers) is passed on; any
|
|
70
89
|
# other is dropped, so a forward can only tighten (an answer never
|
|
@@ -93,7 +112,7 @@ module Samagotchi
|
|
|
93
112
|
detail: reply_detail(reply, "the worker refused that option") })
|
|
94
113
|
end
|
|
95
114
|
end
|
|
96
|
-
relayed.is_a?(Symbol) ? answered_wait(id, qid, start, wait, answered: relayed == :answered) : relayed
|
|
115
|
+
relayed.is_a?(Symbol) ? answered_wait(id, qid, start, wait, answered: relayed == :answered, reply_for: reply_for) : relayed
|
|
97
116
|
end
|
|
98
117
|
|
|
99
118
|
# A request to the worker sent once more when the worker dropped it as
|
|
@@ -235,7 +254,7 @@ module Samagotchi
|
|
|
235
254
|
result = SessionManager.deliver_turn(id, prompt: prompt.to_s, client_id: client_id, images: images,
|
|
236
255
|
delivery: delivery, no_interrupt: body["no_interrupt"] == true,
|
|
237
256
|
author: principal(req).author,
|
|
238
|
-
anchors: TurnAnchors.
|
|
257
|
+
anchors: TurnAnchors.from_turn_route(body),
|
|
239
258
|
follow: body["follow"] == true,
|
|
240
259
|
state_dir: @state_dir, manager: @manager,
|
|
241
260
|
bridge: -> { live_bridge_client(id) })
|
data/lib/samagotchi/worker.rb
CHANGED
|
@@ -26,6 +26,7 @@ require_relative "reminder_carry"
|
|
|
26
26
|
require_relative "log"
|
|
27
27
|
require_relative "turn_flow"
|
|
28
28
|
require_relative "turn_checkpoint"
|
|
29
|
+
require_relative "reply_for"
|
|
29
30
|
require_relative "continue_offer"
|
|
30
31
|
require_relative "iteration_limit"
|
|
31
32
|
require_relative "session_commands"
|
|
@@ -763,7 +764,7 @@ module Samagotchi
|
|
|
763
764
|
restores = @turn_flow.restores
|
|
764
765
|
begin
|
|
765
766
|
@checkpoint.begin_turn(prompt: prompt, turn_id: turn_args[:id], enqueued_id: origin.enqueued_id, author: turn_author_fields(origin),
|
|
766
|
-
source: turn_args[:opener_source])
|
|
767
|
+
source: turn_args[:opener_source], reply: ReplyFor.opener_fields(@session.messages, turn_args[:reply_for]))
|
|
767
768
|
@session.status = Session::STATUS_RUNNING
|
|
768
769
|
# A continue that moved this session to the chain's new link wrote
|
|
769
770
|
# the new parent beside it (Session::PARENT_FILE): this turn's
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: samagotchi
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.186.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Dmitry Dedov
|
|
@@ -621,6 +621,7 @@ files:
|
|
|
621
621
|
- lib/samagotchi/peer_exchanges.rb
|
|
622
622
|
- lib/samagotchi/peer_sources.rb
|
|
623
623
|
- lib/samagotchi/pending_question.rb
|
|
624
|
+
- lib/samagotchi/plain_reply.rb
|
|
624
625
|
- lib/samagotchi/plugin/api.rb
|
|
625
626
|
- lib/samagotchi/plugin/attached_context.rb
|
|
626
627
|
- lib/samagotchi/plugin/card.rb
|
|
@@ -686,6 +687,7 @@ files:
|
|
|
686
687
|
- lib/samagotchi/remote_attach_command.rb
|
|
687
688
|
- lib/samagotchi/remote_command.rb
|
|
688
689
|
- lib/samagotchi/remotes.rb
|
|
690
|
+
- lib/samagotchi/reply_for.rb
|
|
689
691
|
- lib/samagotchi/reply_wait.rb
|
|
690
692
|
- lib/samagotchi/reply_wait/liveness.rb
|
|
691
693
|
- lib/samagotchi/reply_wait/poll.rb
|