samagotchi 0.190.1 → 0.192.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 +61 -1
- data/docs/architecture.md +13 -11
- data/docs/bundles.md +1 -1
- data/docs/cli.md +17 -6
- data/docs/configuration.md +6 -1
- data/docs/context.md +8 -1
- data/docs/internals/attached-context.md +3 -1
- data/docs/internals/inline-widgets.md +2 -1
- data/docs/internals/participants.md +1 -5
- data/docs/internals/peers.md +15 -3
- data/docs/internals/remote-sessions.md +5 -2
- data/docs/internals/session-lifecycle.md +233 -18
- data/docs/internals/web-frontend.md +107 -9
- data/docs/peers.md +3 -1
- data/docs/plugins.md +2 -1
- data/docs/scheduling.md +31 -7
- data/docs/sessions.md +12 -12
- data/docs/sub-agent.md +1 -1
- data/docs/testing.md +1 -2
- data/docs/thin-clients.md +98 -12
- data/lib/samagotchi/bridge.rb +3 -4
- data/lib/samagotchi/bundles/mcp/manifest.yml +4 -2
- data/lib/samagotchi/bundles/mcp/plugin.rb +7 -398
- data/lib/samagotchi/bundles/mcp/server_config.rb +158 -0
- data/lib/samagotchi/bundles/mcp/sign_in.rb +261 -0
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/client_id.rb +2 -19
- data/lib/samagotchi/compat.rb +1 -7
- data/lib/samagotchi/config.rb +3 -0
- data/lib/samagotchi/context_command.rb +5 -1
- data/lib/samagotchi/context_sources.rb +28 -0
- data/lib/samagotchi/continue_intent.rb +23 -43
- data/lib/samagotchi/delivery.rb +1 -5
- data/lib/samagotchi/hold_stop.rb +5 -2
- data/lib/samagotchi/participants.rb +13 -5
- data/lib/samagotchi/peer_say.rb +29 -14
- data/lib/samagotchi/plugin/sessions.rb +9 -5
- data/lib/samagotchi/schedule_command.rb +6 -1
- data/lib/samagotchi/schedules/prune_part.rb +162 -41
- data/lib/samagotchi/schedules.rb +32 -15
- data/lib/samagotchi/send_command.rb +6 -6
- data/lib/samagotchi/session.rb +7 -0
- data/lib/samagotchi/session_access/ask.rb +9 -3
- data/lib/samagotchi/session_access/exchange.rb +6 -2
- data/lib/samagotchi/session_access/local.rb +103 -32
- data/lib/samagotchi/session_access/remote.rb +10 -9
- data/lib/samagotchi/session_access/remote_peers.rb +2 -2
- data/lib/samagotchi/session_access.rb +17 -7
- data/lib/samagotchi/session_chain/delete.rb +100 -47
- data/lib/samagotchi/session_chain/delete_preview.rb +9 -5
- data/lib/samagotchi/session_chain/successor.rb +78 -0
- data/lib/samagotchi/session_continue.rb +112 -42
- data/lib/samagotchi/session_inbox.rb +1 -1
- data/lib/samagotchi/session_inbox_delivery.rb +111 -25
- data/lib/samagotchi/session_lifecycle/allowed.rb +20 -1
- data/lib/samagotchi/session_lifecycle/reading.rb +9 -3
- data/lib/samagotchi/session_lifecycle.rb +17 -2
- data/lib/samagotchi/session_manager/delivery.rb +61 -29
- data/lib/samagotchi/session_manager/emptiness.rb +5 -2
- data/lib/samagotchi/session_manager/lifecycle.rb +68 -19
- data/lib/samagotchi/session_manager/listing.rb +2 -1
- data/lib/samagotchi/session_manager/removal.rb +67 -13
- data/lib/samagotchi/session_manager/spawn.rb +16 -9
- data/lib/samagotchi/session_manager.rb +4 -0
- data/lib/samagotchi/session_retention.rb +28 -7
- data/lib/samagotchi/session_row.rb +2 -0
- data/lib/samagotchi/session_transition.rb +361 -0
- data/lib/samagotchi/sessions_command.rb +1 -1
- data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -0
- data/lib/samagotchi/tools/cancel_schedule.rb +10 -6
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +13 -3
- data/lib/samagotchi/web/ask_create.rb +138 -0
- data/lib/samagotchi/web/ask_routes.rb +56 -9
- data/lib/samagotchi/web/last_ask.rb +78 -0
- data/lib/samagotchi/web/peer_routes.rb +5 -2
- data/lib/samagotchi/web/public/answer_refs.js +3 -9
- data/lib/samagotchi/web/public/answer_write.js +28 -0
- data/lib/samagotchi/web/public/app.js +72 -96
- data/lib/samagotchi/web/public/block_fix.js +30 -12
- data/lib/samagotchi/web/public/chains.js +3 -1
- data/lib/samagotchi/web/public/command_list.js +5 -3
- data/lib/samagotchi/web/public/composer.js +9 -6
- data/lib/samagotchi/web/public/composer_mount.js +18 -11
- data/lib/samagotchi/web/public/draft_keys.js +10 -0
- data/lib/samagotchi/web/public/drafts.js +278 -0
- data/lib/samagotchi/web/public/owner_claim.js +7 -12
- data/lib/samagotchi/web/public/prompt_history.js +6 -0
- data/lib/samagotchi/web/public/send_turn.js +51 -20
- data/lib/samagotchi/web/public/session_actions.js +80 -16
- data/lib/samagotchi/web/public/session_prefs.js +9 -3
- data/lib/samagotchi/web/public/session_view.js +7 -2
- data/lib/samagotchi/web/public/turn_events.js +0 -2
- data/lib/samagotchi/web/public/unqueue.js +22 -9
- data/lib/samagotchi/web/public/viewer_self.js +1 -6
- data/lib/samagotchi/web/public/views.js +12 -1
- data/lib/samagotchi/web/session_access_routes.rb +11 -5
- data/lib/samagotchi/web/session_control.rb +8 -1
- data/lib/samagotchi/web/session_create.rb +51 -34
- data/lib/samagotchi/web/session_hub.rb +52 -36
- data/lib/samagotchi/web/session_summary.rb +45 -7
- data/lib/samagotchi/web/start_dir.rb +6 -4
- data/lib/samagotchi/worker_boot.rb +28 -6
- data/lib/samagotchi/worker_presence.rb +22 -6
- metadata +10 -1
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../client_id"
|
|
4
|
+
require_relative "../config"
|
|
5
|
+
require_relative "../guardrails/parent_policy"
|
|
6
|
+
require_relative "../model_profile"
|
|
7
|
+
require_relative "../session"
|
|
8
|
+
require_relative "../session_inbox"
|
|
9
|
+
require_relative "../session_manager"
|
|
10
|
+
require_relative "last_ask"
|
|
11
|
+
require_relative "start_dir"
|
|
12
|
+
|
|
13
|
+
module Samagotchi
|
|
14
|
+
module Web
|
|
15
|
+
# POST /api/sessions answering its first turn (docs/thin-clients.md "A
|
|
16
|
+
# new session in one call"): a create with any of reply_for, timeout or
|
|
17
|
+
# request_id (ASK_FIELDS) spawns the session idle, its prompt as the
|
|
18
|
+
# preview, then runs the same SessionAccess::Ask an /ask runs
|
|
19
|
+
# (AskRoutes#run_ask), all within one ASK_MAX_SECONDS deadline. Included
|
|
20
|
+
# in App beside SessionCreate (which hands such a body here) and
|
|
21
|
+
# AskRoutes, whose readers and response builders it uses.
|
|
22
|
+
module AskCreate
|
|
23
|
+
# The body fields that make a create answer its first turn.
|
|
24
|
+
ASK_FIELDS = %w[reply_for timeout request_id].freeze
|
|
25
|
+
# GET /api/info's feature: this chi web's create answers its first turn.
|
|
26
|
+
FEATURE = "ask_create"
|
|
27
|
+
INLINE_ONLY = "an answered create starts a new session with a prompt: leave out idle and continues"
|
|
28
|
+
|
|
29
|
+
# One lock per derived session id, held over the check, the spawn and
|
|
30
|
+
# the Ask: two same-request_id creates at once make one session and
|
|
31
|
+
# one turn. chi web is the only creator of these ids.
|
|
32
|
+
LOCKS = {}
|
|
33
|
+
LOCKS_GUARD = Mutex.new
|
|
34
|
+
|
|
35
|
+
def self.inline?(body) = ASK_FIELDS.any? { |key| body.key?(key) }
|
|
36
|
+
|
|
37
|
+
# The block under +id+'s lock (none for nil), the entry dropped once
|
|
38
|
+
# nobody holds or waits for it.
|
|
39
|
+
def self.locked(id, &)
|
|
40
|
+
return yield if id.nil?
|
|
41
|
+
|
|
42
|
+
entry = LOCKS_GUARD.synchronize { (LOCKS[id] ||= [Mutex.new, 0]).tap { |e| e[1] += 1 } }
|
|
43
|
+
begin
|
|
44
|
+
entry[0].synchronize(&)
|
|
45
|
+
ensure
|
|
46
|
+
LOCKS_GUARD.synchronize { LOCKS.delete(id) if (entry[1] -= 1).zero? }
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
# The inline create: the ask's fields read first (a bad timeout,
|
|
53
|
+
# request_id or reply_for, a UI-only command, an oversize prompt spawn
|
|
54
|
+
# nothing), then the folder (the body's dir, else web.ask_dir, else
|
|
55
|
+
# chi web's start folder) and every check a create makes, then the
|
|
56
|
+
# idle spawn (with a request_id, at the id it names: a retry finds it
|
|
57
|
+
# and spawns nothing) and the Ask. 201 with the ask's answer
|
|
58
|
+
# (AskRoutes#ask_response's fields; session_id, created, model_warning,
|
|
59
|
+
# parent_answers_warning); a refusal after the spawn keeps its code
|
|
60
|
+
# and carries session_id and created. The owner's own create is their
|
|
61
|
+
# `last` (LastAsk), refused or not.
|
|
62
|
+
def handle_ask_create(body, deadline)
|
|
63
|
+
if body["idle"] == true || body.key?("continues")
|
|
64
|
+
return error_response(400, "invalid_ask", INLINE_ONLY)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
prompt, client_id, options, reply_for, error = ask_fields(body, id: nil)
|
|
68
|
+
return error if error
|
|
69
|
+
|
|
70
|
+
dir, error = ask_create_dir(body)
|
|
71
|
+
return error if error
|
|
72
|
+
|
|
73
|
+
folder, parent_answers, error = create_fields(body, prompt, dir: dir)
|
|
74
|
+
return error if error
|
|
75
|
+
|
|
76
|
+
request_id = options[:request_id]
|
|
77
|
+
session_id = request_id && SessionInbox.enqueued_id_for("create:#{request_id}")
|
|
78
|
+
AskCreate.locked(session_id) do
|
|
79
|
+
session, error = ask_create_spawn(body, prompt, folder, session_id)
|
|
80
|
+
return error if error
|
|
81
|
+
|
|
82
|
+
sid = session&.id || session_id
|
|
83
|
+
@hub&.touch(sid)
|
|
84
|
+
extra = { session_id: sid, created: !session.nil?, model_warning: session&.model_warning,
|
|
85
|
+
parent_answers_warning: Guardrails::ParentPolicy.capped_note(parent_answers) }
|
|
86
|
+
response = ask_create_run(sid, prompt, client_id, options, reply_for, deadline, body, extra)
|
|
87
|
+
LastAsk.record(sid, state_dir: default_state_dir) unless ClientId.parent_line?(client_id)
|
|
88
|
+
response
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# The inline create's folder: [dir, nil] or [nil, a 400 invalid_dir].
|
|
93
|
+
# web.ask_dir (read now, StartDir's rules, from home) when the body
|
|
94
|
+
# names none; unset, chi web's start folder.
|
|
95
|
+
def ask_create_dir(body)
|
|
96
|
+
dir, error = scope_dir(body["dir"])
|
|
97
|
+
return [nil, error] if error
|
|
98
|
+
return [dir, nil] if dir
|
|
99
|
+
|
|
100
|
+
configured = Config.get("web.ask_dir")
|
|
101
|
+
return [start_dir, nil] if configured.to_s.strip.empty?
|
|
102
|
+
|
|
103
|
+
[StartDir.resolve(configured, from: :ask_config), nil]
|
|
104
|
+
rescue StartDir::Invalid => e
|
|
105
|
+
[nil, error_response(400, "invalid_dir", e.message)]
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# The idle spawn, prompt as its preview: [session, nil]; [nil, nil]
|
|
109
|
+
# when +session_id+ (a request_id's) is a session already (a retry:
|
|
110
|
+
# nothing spawns); [nil, a 400 invalid_model].
|
|
111
|
+
def ask_create_spawn(body, prompt, folder, session_id)
|
|
112
|
+
return [nil, nil] if session_id && Session.exist?(session_id, state_dir: default_state_dir)
|
|
113
|
+
|
|
114
|
+
ids = session_id ? { session_id: session_id } : {}
|
|
115
|
+
session = @manager.spawn_session(prompt: nil, preview: prompt.strip, client_id: body["client_id"] || ClientId::WEB_CREATE,
|
|
116
|
+
state_dir: @state_dir, **folder, **ids)
|
|
117
|
+
[session, nil]
|
|
118
|
+
rescue ModelProfile::MissingModel => e
|
|
119
|
+
[nil, error_response(400, "invalid_model", e.message)]
|
|
120
|
+
rescue SessionManager::IdTaken
|
|
121
|
+
[nil, nil]
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# The Ask on the new (or retried) session, its answer a 201 with
|
|
125
|
+
# +extra+ (a refusal keeps its code). Not a :session route: a session
|
|
126
|
+
# deleted meanwhile is 404 here, and a one-turn chi run's 409 too.
|
|
127
|
+
def ask_create_run(sid, prompt, client_id, options, reply_for, deadline, body, extra)
|
|
128
|
+
outcome = run_ask(sid, prompt: prompt, client_id: client_id, options: options, deadline: deadline, body: body)
|
|
129
|
+
response = ask_response(sid, outcome, request_id: options[:request_id], reply_for: reply_for)
|
|
130
|
+
with_fields(response, status: (201 if response.first == 200), **extra)
|
|
131
|
+
rescue SessionManager::OwnedByTUI => e
|
|
132
|
+
with_fields(owned_by_tui(e), **extra)
|
|
133
|
+
rescue ArgumentError => e
|
|
134
|
+
with_fields(error_response(404, "not_found", e.message), **extra)
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "delegate"
|
|
4
|
+
require "json"
|
|
4
5
|
require_relative "../bridge_client"
|
|
5
6
|
require_relative "../agent_caller"
|
|
6
7
|
require_relative "../client_id"
|
|
@@ -15,6 +16,7 @@ require_relative "../session_access/exchange"
|
|
|
15
16
|
require_relative "../session_access/exchange/token"
|
|
16
17
|
require_relative "../reply_for"
|
|
17
18
|
require_relative "../turn_anchors"
|
|
19
|
+
require_relative "last_ask"
|
|
18
20
|
require_relative "request_body"
|
|
19
21
|
|
|
20
22
|
module Samagotchi
|
|
@@ -49,6 +51,8 @@ module Samagotchi
|
|
|
49
51
|
# GET /api/info's feature: this chi web has the ask route, wait
|
|
50
52
|
# tokens, the answer's wait and the create's token.
|
|
51
53
|
ASK_FEATURE = "ask"
|
|
54
|
+
# GET /api/info's feature: POST /api/sessions/last/ask (LastAsk).
|
|
55
|
+
ASK_LAST_FEATURE = "ask_last"
|
|
52
56
|
# A line a worker may run as a session command (#ask_request_id): a
|
|
53
57
|
# / name (Registry::UNKNOWN_COMMAND_WORD's shape, which every
|
|
54
58
|
# built-in a worker runs and a bundle's COMMAND_NAME fits) followed by
|
|
@@ -105,30 +109,73 @@ module Samagotchi
|
|
|
105
109
|
# waits, the Bridge wait and the slice all draw on it.
|
|
106
110
|
deadline = ask_clock + ASK_MAX_SECONDS
|
|
107
111
|
body = body_hash(req) or return error_response(400, "invalid_json", "invalid JSON body")
|
|
108
|
-
|
|
109
|
-
|
|
112
|
+
# `last` (LastAsk): the session the owner last asked, resolved here
|
|
113
|
+
# (the route's gates saw the address: a participant's is a 404).
|
|
114
|
+
if id == LastAsk::ADDRESS
|
|
115
|
+
id = LastAsk.target(state_dir: default_state_dir) or return error_response(404, "not_found", LastAsk::NOT_FOUND)
|
|
116
|
+
end
|
|
110
117
|
|
|
111
|
-
reply_for, error =
|
|
118
|
+
prompt, client_id, options, reply_for, error = ask_fields(body, id: id)
|
|
112
119
|
return error if error
|
|
113
120
|
|
|
114
|
-
options[:anchors] = TurnAnchors.new(reply_for: reply_for) if reply_for && !runs_as_command?(prompt, client_id)
|
|
115
|
-
|
|
116
121
|
gated = ask_gate(id, prompt, client_id)
|
|
117
122
|
return gated if gated
|
|
118
123
|
|
|
124
|
+
outcome = run_ask(id, prompt: prompt, client_id: client_id, options: options, deadline: deadline, body: body)
|
|
125
|
+
record_last(outcome, client_id)
|
|
126
|
+
ask_response(id, outcome, request_id: options[:request_id], reply_for: reply_for)
|
|
127
|
+
rescue SessionManager::OwnedByTUI => e
|
|
128
|
+
owned_by_tui(e)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# An ask's body read whole (#ask_request, #ask_reply_for, the turn's
|
|
132
|
+
# anchor): [prompt, client_id, options, reply_for, nil] or [nil, nil,
|
|
133
|
+
# nil, nil, a 400 response]. +id+: the session asked, which a
|
|
134
|
+
# UI-only command's refusal names (nil: none yet, "ID").
|
|
135
|
+
def ask_fields(body, id:)
|
|
136
|
+
prompt, client_id, options, error = ask_request(id, body)
|
|
137
|
+
return [nil, nil, nil, nil, error] if error
|
|
138
|
+
|
|
139
|
+
reply_for, error = ask_reply_for(body)
|
|
140
|
+
return [nil, nil, nil, nil, error] if error
|
|
141
|
+
|
|
142
|
+
options[:anchors] = TurnAnchors.new(reply_for: reply_for) if reply_for && !runs_as_command?(prompt, client_id)
|
|
143
|
+
[prompt, client_id, options, reply_for, nil]
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# The ask itself (SessionAccess::Ask through chi web's manager and
|
|
147
|
+
# Bridge, all within +deadline+), its history line recorded unless it
|
|
148
|
+
# was refused or rebound to a turn already known.
|
|
149
|
+
# @return [SessionAccess::Ask::Reply, Ran, Refused] the outcome
|
|
150
|
+
def run_ask(id, prompt:, client_id:, options:, deadline:, body:)
|
|
119
151
|
bridge = ->(sid) { ask_bridge(sid, deadline) }
|
|
120
152
|
access = AskAccess.new(session_access, manager: @manager, bridge: bridge, poll_interval: @wait_poll_interval)
|
|
121
153
|
stopping = -> { !@server_running.call }
|
|
122
154
|
exchange = SessionAccess::Exchange.new(access, continuing_wait: [SessionAccess::Exchange::CONTINUING_WAIT,
|
|
123
155
|
ASK_MAX_SECONDS].min, cancelled: stopping)
|
|
124
156
|
outcome = SessionAccess::Ask.new(access, exchange: exchange)
|
|
125
|
-
.call(id, prompt: prompt, client_id: client_id, cap: deadline - ask_clock,
|
|
157
|
+
.call(id, prompt: prompt, client_id: client_id, cap: (deadline - ask_clock).clamp(0.0..),
|
|
126
158
|
cancelled: stopping, **options)
|
|
127
159
|
rebound = outcome.is_a?(SessionAccess::Ask::Reply) && outcome.rebound
|
|
128
160
|
record_history(body, prompt) unless outcome.is_a?(SessionAccess::Ask::Refused) || rebound
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
161
|
+
outcome
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# The session an answered ask ran in as the owner's `last`
|
|
165
|
+
# (LastAsk.record): not a refused ask's, nor a line a parent agent
|
|
166
|
+
# or chi send sent (ClientId.parent_line?).
|
|
167
|
+
def record_last(outcome, client_id)
|
|
168
|
+
return if outcome.is_a?(SessionAccess::Ask::Refused) || ClientId.parent_line?(client_id)
|
|
169
|
+
|
|
170
|
+
LastAsk.record(outcome.session_id, state_dir: default_state_dir)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# A json_response triple re-rendered with +extra+ keys added (its own
|
|
174
|
+
# keys win: an ask's session_id is where it ran) and +status+ in place
|
|
175
|
+
# of its own when given.
|
|
176
|
+
def with_fields(response, status: nil, **extra)
|
|
177
|
+
code, _headers, chunks = response
|
|
178
|
+
json_response(status || code, JSON.parse(chunks.join).merge(extra.transform_keys(&:to_s)) { |_key, own, _new| own })
|
|
132
179
|
end
|
|
133
180
|
|
|
134
181
|
# The ask's body read: [prompt, client_id, {queue:, timeout:,
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "time"
|
|
6
|
+
require_relative "../archive_store"
|
|
7
|
+
require_relative "../atomic_file"
|
|
8
|
+
require_relative "../log"
|
|
9
|
+
require_relative "../session"
|
|
10
|
+
require_relative "../session_inbox_delivery"
|
|
11
|
+
|
|
12
|
+
module Samagotchi
|
|
13
|
+
module Web
|
|
14
|
+
# A thin client's `last` (POST /api/sessions/last/ask,
|
|
15
|
+
# docs/thin-clients.md "Your last session"): the session that most
|
|
16
|
+
# recently got an owner's thin-client ask (an /ask, or a create that
|
|
17
|
+
# answers its first turn), kept as one id in FILE beside the sessions
|
|
18
|
+
# folder (as Schedules keeps its file), and the fallback when it is gone.
|
|
19
|
+
# Only AskRoutes records it, and only for the owner's own lines (an
|
|
20
|
+
# agent's or chi send's leave it): it is the owner's conversation.
|
|
21
|
+
module LastAsk
|
|
22
|
+
module_function
|
|
23
|
+
|
|
24
|
+
# The address a thin client asks in place of an id.
|
|
25
|
+
ADDRESS = "last"
|
|
26
|
+
FILE = "last_ask.json"
|
|
27
|
+
NOT_FOUND = "no session to ask as last: start one with POST /api/sessions"
|
|
28
|
+
|
|
29
|
+
def path(state_dir:) = File.join(File.dirname(File.expand_path(state_dir)), FILE)
|
|
30
|
+
|
|
31
|
+
# +session_id+ as the last one asked. Never raises: a write that
|
|
32
|
+
# can't happen is logged, and the ask still answers.
|
|
33
|
+
def record(session_id, state_dir:, at: Time.now)
|
|
34
|
+
file = path(state_dir: state_dir)
|
|
35
|
+
FileUtils.mkdir_p(File.dirname(file))
|
|
36
|
+
AtomicFile.write(file, JSON.generate({ "session_id" => session_id, "at" => at.utc.iso8601(3) }), perm: 0o600)
|
|
37
|
+
rescue StandardError => e
|
|
38
|
+
Log.warn(:web, "last_ask_write_failed", session_id: session_id, msg: "#{e.class}: #{e.message}")
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# @return [String, nil] the recorded id; nil when none, or the file
|
|
42
|
+
# is unreadable or holds no session id
|
|
43
|
+
def recorded(state_dir:)
|
|
44
|
+
data = JSON.parse(File.read(path(state_dir: state_dir)))
|
|
45
|
+
id = data["session_id"] if data.is_a?(Hash)
|
|
46
|
+
Session.valid_id?(id) ? id : nil
|
|
47
|
+
rescue SystemCallError, IOError, JSON::ParserError
|
|
48
|
+
nil
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# The session `last` names: the recorded one while its file is there
|
|
52
|
+
# and it is not archived with nothing continuing it (a continued one
|
|
53
|
+
# keeps its id: the ask follows the chain to its newest link, a
|
|
54
|
+
# continue in flight included); else #recent.
|
|
55
|
+
# @param head [#call] id → SessionInbox::Resolved (a send's chain walk)
|
|
56
|
+
# @return [String, nil] nil: no session to ask
|
|
57
|
+
def target(state_dir:, head: ->(id) { SessionInbox::Head.resolve(:send, id, state_dir: state_dir) })
|
|
58
|
+
id = recorded(state_dir: state_dir)
|
|
59
|
+
return recent(state_dir: state_dir) unless id && Session.exist?(id, state_dir: state_dir)
|
|
60
|
+
return id if head.call(id).id != id
|
|
61
|
+
return id unless ArchiveStore.archived?(Session.session_dir(id, state_dir: state_dir))
|
|
62
|
+
|
|
63
|
+
recent(state_dir: state_dir)
|
|
64
|
+
rescue ArgumentError # degrade-ok: deleted while resolving: the fallback
|
|
65
|
+
recent(state_dir: state_dir)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# The newest of the owner's own sessions (Session.list: archived left
|
|
69
|
+
# out, updated_at desc): not a chi scratch one, a delegate, one an
|
|
70
|
+
# agent drives, or a test run.
|
|
71
|
+
# @return [String, nil]
|
|
72
|
+
def recent(state_dir:)
|
|
73
|
+
Session.list(state_dir: state_dir)
|
|
74
|
+
.find { |s| !s.scratch && !s.delegate? && s.driver.nil? && !s.test_run }&.id
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -101,7 +101,8 @@ module Samagotchi
|
|
|
101
101
|
paired = session_access.peer_pair(id, name, address: address, from: from)
|
|
102
102
|
return json_response(200, { status: "paired", link: paired.link.to_h }) if paired.status == :paired
|
|
103
103
|
|
|
104
|
-
json_response(409, { status: paired.status.to_s, next_id: paired.next_id,
|
|
104
|
+
json_response(409, { status: paired.status.to_s, next_id: paired.next_id, in_flight: (true if paired.in_flight),
|
|
105
|
+
detail: paired.refusal }.compact)
|
|
105
106
|
end
|
|
106
107
|
|
|
107
108
|
# POST /api/sessions/:id/peers/:name/say {text, summary, wake, re,
|
|
@@ -120,7 +121,9 @@ module Samagotchi
|
|
|
120
121
|
|
|
121
122
|
said = session_access.peer_say(id, name, text: body["text"], summary: string_or_nil(body["summary"]),
|
|
122
123
|
wake: body["wake"] == true, re: string_or_nil(body["re"]), wait: wait)
|
|
123
|
-
|
|
124
|
+
line = said.line(name)
|
|
125
|
+
# An older chi --remote knows no busy status: it prints the detail.
|
|
126
|
+
json_response(said.received? ? 200 : 409, said.to_h.merge("line" => line, **(said.status == :busy ? { "detail" => line } : {})))
|
|
124
127
|
rescue PeerMailbox::Invalid => e
|
|
125
128
|
error_response(400, "invalid_text", e.message)
|
|
126
129
|
end
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
// The match and label are a plugin's text: they only ever go through
|
|
19
19
|
// textContent, setAttribute and bindRef, never markup or a RegExp.
|
|
20
20
|
|
|
21
|
-
import {
|
|
21
|
+
import { showsAnswer } from "./copy.js";
|
|
22
|
+
import { writeAnswer } from "./answer_write.js";
|
|
22
23
|
import { bindRef, boundRef, httpUrl, refKindOk } from "./refs.js";
|
|
23
24
|
|
|
24
25
|
// bubble → {refs, paired}. A WeakMap: a dropped bubble takes its refs with
|
|
@@ -64,14 +65,7 @@ export function applyFinalAnswer(messages, bubbles, normalize) {
|
|
|
64
65
|
);
|
|
65
66
|
const bubble = message && [...bubbles].reverse().find((item) => showsAnswer(item, message, normalize));
|
|
66
67
|
if (!bubble) return null;
|
|
67
|
-
|
|
68
|
-
bubble.classList.add("markdown");
|
|
69
|
-
bubble.setAttribute(COPY_SOURCE_ATTR, copySource(message));
|
|
70
|
-
// A hook's display in the answer's place (answer_fold.js's label).
|
|
71
|
-
bubble.toggleAttribute("data-answer-display", typeof message.display === "string");
|
|
72
|
-
// The server's rendered markdown, sanitized there (Web::MarkdownRenderer#sanitize): set as it is, not templated.
|
|
73
|
-
bubble.innerHTML = message.html;
|
|
74
|
-
}
|
|
68
|
+
writeAnswer(bubble, message);
|
|
75
69
|
setAnswerRefs(bubble, message.display_refs);
|
|
76
70
|
return bubble;
|
|
77
71
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// The answer's live write onto its bubble: the markdown class, copy source,
|
|
2
|
+
// display flag, html and hover time (the same four writes as a reload's
|
|
3
|
+
// answerBubbleTemplate, minus the at/turn/reply-key the live bubble already
|
|
4
|
+
// has). A new module for the one reason it exists: static JS is cached at
|
|
5
|
+
// unversioned URLs, so a tab or proxy can hold an OLD answer_refs.js while
|
|
6
|
+
// loading the NEW caller (markdown_place.js, block_fix.js); a new export on
|
|
7
|
+
// an old module would blank the page, an import of a new module would not.
|
|
8
|
+
// Not the refs: answer_refs.js sets them (a module of its own: it would
|
|
9
|
+
// import the other).
|
|
10
|
+
|
|
11
|
+
import { COPY_SOURCE_ATTR, copySource } from "./copy.js";
|
|
12
|
+
import { fullTime } from "./format.js";
|
|
13
|
+
|
|
14
|
+
// The answer written onto +bubble+ (the live bubble, which has its data-
|
|
15
|
+
// answer-at, data-answer-turn and data-reply-key already and keeps them).
|
|
16
|
+
// Only the html branch: with web.markdown off or no renderer the text stays.
|
|
17
|
+
export function writeAnswer(bubble, message) {
|
|
18
|
+
if (typeof message.html !== "string") return;
|
|
19
|
+
bubble.classList.add("markdown");
|
|
20
|
+
bubble.setAttribute(COPY_SOURCE_ATTR, copySource(message));
|
|
21
|
+
// A hook's display in the answer's place (answer_fold.js's label).
|
|
22
|
+
bubble.toggleAttribute("data-answer-display", typeof message.display === "string");
|
|
23
|
+
// The server's rendered markdown, sanitized there (Web::MarkdownRenderer#sanitize): set as it is, not templated.
|
|
24
|
+
bubble.innerHTML = message.html;
|
|
25
|
+
// The full hover time (turn_html.js's timeTitle is lit's: a string here).
|
|
26
|
+
const title = fullTime(message.at);
|
|
27
|
+
if (title) bubble.setAttribute("title", title);
|
|
28
|
+
}
|