samagotchi 0.185.0 → 0.187.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 +37 -1
- data/bin/chi +2 -0
- data/docs/cli.md +1 -0
- 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/inline-widgets.md +18 -0
- data/docs/internals/pages.md +172 -0
- data/docs/internals/prompt-caching.md +14 -0
- data/docs/internals/remote-sessions.md +7 -0
- data/docs/internals/web-frontend.md +5 -4
- data/docs/memory.md +7 -0
- data/docs/pages.md +150 -0
- data/docs/thin-clients.md +67 -2
- data/lib/samagotchi/bridge.rb +1 -1
- data/lib/samagotchi/bundles/system/manifest.yml +5 -2
- data/lib/samagotchi/bundles/system/self_map.md +5 -1
- data/lib/samagotchi/bundles/system/skill_pages.md +93 -0
- 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/inline_widgets.rb +65 -0
- data/lib/samagotchi/llm/chat_loop.rb +16 -2
- data/lib/samagotchi/page_command.rb +234 -0
- data/lib/samagotchi/pages/builder.rb +187 -0
- data/lib/samagotchi/pages/launch.rb +187 -0
- data/lib/samagotchi/pages/runner.rb +154 -0
- data/lib/samagotchi/pages/store.rb +143 -0
- data/lib/samagotchi/pages.rb +119 -0
- data/lib/samagotchi/plain_reply.rb +75 -0
- data/lib/samagotchi/reply_for.rb +254 -0
- data/lib/samagotchi/run_options.rb +1 -0
- data/lib/samagotchi/session_access/ask.rb +5 -2
- data/lib/samagotchi/session_access/contract.rb +1 -1
- 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 +6 -6
- data/lib/samagotchi/web/ask_routes.rb +62 -17
- data/lib/samagotchi/web/page_routes.rb +116 -0
- data/lib/samagotchi/web/participant_access.rb +1 -1
- data/lib/samagotchi/web/public/app.js +24 -84
- data/lib/samagotchi/web/public/index.html +3 -0
- data/lib/samagotchi/web/public/page_blocks.js +58 -0
- data/lib/samagotchi/web/public/page_links.js +33 -0
- data/lib/samagotchi/web/public/page_model.js +68 -0
- data/lib/samagotchi/web/public/page_route.js +24 -0
- data/lib/samagotchi/web/public/page_table.js +74 -0
- data/lib/samagotchi/web/public/page_table_model.js +76 -0
- data/lib/samagotchi/web/public/page_view.js +218 -0
- data/lib/samagotchi/web/public/pages.css +77 -0
- data/lib/samagotchi/web/public/pages_group.js +65 -0
- data/lib/samagotchi/web/public/peer_text.js +22 -5
- data/lib/samagotchi/web/public/views.js +161 -0
- data/lib/samagotchi/web/session_access_routes.rb +30 -2
- data/lib/samagotchi/web/session_control.rb +21 -2
- data/lib/samagotchi/web/viewer.rb +1 -1
- data/lib/samagotchi/worker.rb +2 -1
- metadata +23 -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
|
@@ -84,6 +84,7 @@ require_relative "verify_fresh_routes"
|
|
|
84
84
|
require_relative "block_fix_routes"
|
|
85
85
|
require_relative "reload_check_route"
|
|
86
86
|
require_relative "working_set_routes"
|
|
87
|
+
require_relative "page_routes"
|
|
87
88
|
require_relative "push_routes"
|
|
88
89
|
require_relative "block_renderer_routes"
|
|
89
90
|
require_relative "update_check"
|
|
@@ -125,6 +126,7 @@ module Samagotchi
|
|
|
125
126
|
include BlockFixRoutes
|
|
126
127
|
include ReloadCheckRoute
|
|
127
128
|
include WorkingSetRoutes
|
|
129
|
+
include PageRoutes
|
|
128
130
|
include PushRoutes
|
|
129
131
|
include BlockRendererRoutes
|
|
130
132
|
include ProxyGate
|
|
@@ -297,7 +299,7 @@ module Samagotchi
|
|
|
297
299
|
["GET", %r{\A/api/sessions/summaries\z}, :handle_summaries, :owner],
|
|
298
300
|
["GET", %r{\A/api/info\z}, :handle_info, :participant],
|
|
299
301
|
["POST", %r{\A/api/token\z}, :handle_token, :participant],
|
|
300
|
-
["GET", %r{\A/api/models\z}, :handle_models, :owner],
|
|
302
|
+
["GET", %r{\A/api/models\z}, :handle_models, :owner], *PageRoutes::ROUTES,
|
|
301
303
|
["GET", %r{\A/api/history\z}, :handle_history, :owner],
|
|
302
304
|
["GET", %r{\A/api/events\z}, :handle_events, :owner],
|
|
303
305
|
["POST", %r{\A/api/update\z}, :handle_update, :owner],
|
|
@@ -905,8 +907,7 @@ module Samagotchi
|
|
|
905
907
|
end
|
|
906
908
|
|
|
907
909
|
# At most once a minute per peer: a phone with an old link retries.
|
|
908
|
-
# At most REFUSED_LOG_KEYS peers a minute (rotating headers can't
|
|
909
|
-
# fill the log).
|
|
910
|
+
# At most REFUSED_LOG_KEYS peers a minute (rotating headers can't fill the log).
|
|
910
911
|
def log_refused(peer, path, reason = nil)
|
|
911
912
|
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
912
913
|
due = @refused_mutex.synchronize do
|
|
@@ -1099,8 +1100,7 @@ module Samagotchi
|
|
|
1099
1100
|
# on loopback only; proxy: {identity_header} behind an auth proxy
|
|
1100
1101
|
# (web.public_url or a tunnel; the header nil when none is trusted),
|
|
1101
1102
|
# nil without one: the Invite dialog's email field and chi share's
|
|
1102
|
-
# warning. A participant gets app, version and features only
|
|
1103
|
-
# (#participant_info).
|
|
1103
|
+
# warning. A participant gets app, version and features only (#participant_info).
|
|
1104
1104
|
def handle_info(req)
|
|
1105
1105
|
return participant_info unless principal(req).owner?
|
|
1106
1106
|
|
|
@@ -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)
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
require_relative "../pages"
|
|
5
|
+
require_relative "../pages/launch"
|
|
6
|
+
|
|
7
|
+
module Samagotchi
|
|
8
|
+
module Web
|
|
9
|
+
# The pages routes, mixed into App (docs/internals/pages.md, chi web): the
|
|
10
|
+
# list for All sessions' Pages group, a page as stored (opening runs
|
|
11
|
+
# nothing), Refresh (one run through Pages::Launch, inside the request)
|
|
12
|
+
# and Delete. All the owner's: a participant never sees pages. The name
|
|
13
|
+
# is checked (Pages.valid_name?) before anything builds a path; one that
|
|
14
|
+
# breaks the rule is no such page.
|
|
15
|
+
module PageRoutes
|
|
16
|
+
ROUTES = [
|
|
17
|
+
["GET", %r{\A/api/pages\z}, :handle_pages_list, :owner],
|
|
18
|
+
["GET", %r{\A/api/pages/([^/]+)\z}, :handle_page_show, :owner],
|
|
19
|
+
["POST", %r{\A/api/pages/([^/]+)/run\z}, :handle_page_run, :owner],
|
|
20
|
+
["DELETE", %r{\A/api/pages/([^/]+)\z}, :handle_page_delete, :owner]
|
|
21
|
+
].freeze
|
|
22
|
+
|
|
23
|
+
# How much of a failed run's stderr the answer carries (its tail).
|
|
24
|
+
STDERR_BYTES = 2048
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
# GET /api/pages: {pages: [{name, title, state, ran_at, failed_at,
|
|
29
|
+
# changed, error_line}]}, by name.
|
|
30
|
+
def handle_pages_list(_req)
|
|
31
|
+
rows = Pages::Store.list.map do |page|
|
|
32
|
+
{ name: page.name, title: page.title, state: page.state.to_s, ran_at: page.ran_at, failed_at: page.error&.at,
|
|
33
|
+
changed: page.changed?, error_line: page.error&.line }
|
|
34
|
+
end
|
|
35
|
+
json_response(200, { pages: rows })
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# GET /api/pages/NAME: the page as stored (#page_json); 404 page_missing.
|
|
39
|
+
def handle_page_show(_req, name)
|
|
40
|
+
return page_missing(name) unless Pages.valid_name?(name) && File.file?(Pages.script_path(name))
|
|
41
|
+
|
|
42
|
+
json_response(200, page_json(Pages::Store.read(name)))
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# POST /api/pages/NAME/run: the page as after the run, with +stderr+
|
|
46
|
+
# (its last STDERR_BYTES) when it failed. 409 page_busy, 404
|
|
47
|
+
# page_missing, 503 server_stopping (chi web's stop ends the run:
|
|
48
|
+
# Launch's cancelled:, "the run was stopped").
|
|
49
|
+
def handle_page_run(_req, name)
|
|
50
|
+
return page_missing(name) unless Pages.valid_name?(name)
|
|
51
|
+
|
|
52
|
+
running = @server_running
|
|
53
|
+
result = Pages::Launch.run(name, timeout: Pages::TIMEOUT_SECONDS, cancelled: -> { !running.call })
|
|
54
|
+
return error_response(503, "server_stopping", "chi web is stopping; Refresh again") unless running.call
|
|
55
|
+
|
|
56
|
+
json = page_json(result.page)
|
|
57
|
+
json[:stderr] = tail(result.stderr) unless result.ok?
|
|
58
|
+
json_response(200, json)
|
|
59
|
+
rescue Pages::Missing => e
|
|
60
|
+
error_response(404, "page_missing", e.message)
|
|
61
|
+
rescue Pages::Busy => e
|
|
62
|
+
error_response(409, "page_busy", e.message)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# DELETE /api/pages/NAME: the script and its result. {removed: NAME};
|
|
66
|
+
# 409 page_busy, 404 page_missing.
|
|
67
|
+
def handle_page_delete(_req, name)
|
|
68
|
+
return page_missing(name) unless Pages.valid_name?(name)
|
|
69
|
+
|
|
70
|
+
Pages::Store.remove(name)
|
|
71
|
+
json_response(200, { removed: name })
|
|
72
|
+
rescue Pages::Missing => e
|
|
73
|
+
error_response(404, "page_missing", e.message)
|
|
74
|
+
rescue Pages::Busy => e
|
|
75
|
+
error_response(409, "page_busy", e.message)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def page_missing(name) = error_response(404, "page_missing", Pages::Missing.new(name).message)
|
|
79
|
+
|
|
80
|
+
# A page for the view. +changed+: the result came from an older script
|
|
81
|
+
# (Stored#changed?); +changed_after_failure+: the script differs from
|
|
82
|
+
# the one that failed, so the view needs no sha. +script_changed_at+:
|
|
83
|
+
# the script file's mtime. A text block carries +html+ (sanitised)
|
|
84
|
+
# when Markdown rendering is available.
|
|
85
|
+
def page_json(page)
|
|
86
|
+
error = page.error
|
|
87
|
+
{ name: page.name, title: page.title, state: page.state.to_s, ran_at: page.ran_at, duration_ms: page.duration_ms,
|
|
88
|
+
changed: page.changed?,
|
|
89
|
+
changed_after_failure: !error.nil? && !error.script_sha.nil? && !page.current_sha.nil? &&
|
|
90
|
+
error.script_sha != page.current_sha,
|
|
91
|
+
script_changed_at: script_mtime(page.name),
|
|
92
|
+
error: error && { message: error.message, line: error.line, at: error.at },
|
|
93
|
+
blocks: page.blocks.map { |block| with_html(block) } }
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def with_html(block)
|
|
97
|
+
return block unless block.is_a?(Hash) && block["type"] == "text" && block["text"].is_a?(String)
|
|
98
|
+
return block unless @markdown_renderer.available?
|
|
99
|
+
|
|
100
|
+
block.merge("html" => @markdown_renderer.render(block["text"]))
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def script_mtime(name)
|
|
104
|
+
File.mtime(Pages.script_path(name)).utc.iso8601
|
|
105
|
+
rescue SystemCallError
|
|
106
|
+
nil
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def tail(text)
|
|
110
|
+
return text if text.bytesize <= STDERR_BYTES
|
|
111
|
+
|
|
112
|
+
text.byteslice(-STDERR_BYTES, STDERR_BYTES).scrub("")
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -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
|