samagotchi 0.192.0 → 0.193.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 +52 -1
- data/docs/architecture.md +3 -3
- data/docs/bundles.md +45 -28
- data/docs/cli.md +22 -8
- data/docs/configuration.md +4 -3
- data/docs/context.md +8 -5
- data/docs/guardrails.md +9 -4
- data/docs/internals/bridge.md +3 -1
- data/docs/internals/bundles.md +2 -2
- data/docs/internals/pages.md +4 -4
- data/docs/internals/participants.md +5 -3
- data/docs/internals/peers.md +75 -22
- data/docs/internals/prompt-caching.md +2 -1
- data/docs/internals/remote-sessions.md +6 -0
- data/docs/internals/session-lifecycle.md +3 -2
- data/docs/internals/web-frontend.md +91 -19
- data/docs/internals/working-set.md +5 -1
- data/docs/peers.md +71 -22
- data/docs/plugins.md +7 -4
- data/docs/releasing.md +30 -2
- data/docs/scheduling.md +3 -2
- data/docs/sessions.md +1 -1
- data/docs/testing.md +35 -3
- data/docs/thin-clients.md +27 -11
- data/lib/samagotchi/bundles/claude-peer/manifest.yml +2 -2
- data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +33 -17
- data/lib/samagotchi/bundles/codex-peer/manifest.yml +2 -2
- data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +34 -17
- data/lib/samagotchi/bundles/coordinator/manifest.yml +2 -2
- data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +31 -0
- data/lib/samagotchi/bundles/mcp/manifest.yml +2 -2
- data/lib/samagotchi/bundles/mcp/plugin.rb +0 -7
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/compat.rb +8 -1
- data/lib/samagotchi/config.rb +4 -0
- data/lib/samagotchi/continue_offer.rb +13 -1
- data/lib/samagotchi/delivery.rb +2 -2
- data/lib/samagotchi/engine/models.rb +17 -0
- data/lib/samagotchi/engine/plugins.rb +2 -0
- data/lib/samagotchi/guardrails/pair_boundary.rb +49 -18
- data/lib/samagotchi/jsonl_store.rb +13 -2
- data/lib/samagotchi/llm/anthropic_messages.rb +11 -0
- data/lib/samagotchi/llm/openai_chat.rb +5 -0
- data/lib/samagotchi/llm/prompt_cache.rb +5 -0
- data/lib/samagotchi/pages/launch.rb +1 -1
- data/lib/samagotchi/peer_cold.rb +92 -0
- data/lib/samagotchi/peer_command.rb +24 -9
- data/lib/samagotchi/peer_continue.rb +32 -16
- data/lib/samagotchi/peer_exchanges.rb +7 -0
- data/lib/samagotchi/peer_mailbox.rb +21 -4
- data/lib/samagotchi/peer_receipts.rb +33 -6
- data/lib/samagotchi/peer_say.rb +34 -16
- data/lib/samagotchi/plugin/api.rb +2 -1
- data/lib/samagotchi/plugin/context.rb +13 -1
- data/lib/samagotchi/plugin/peer.rb +118 -53
- data/lib/samagotchi/plugin/peers.rb +13 -6
- data/lib/samagotchi/session_access/local.rb +98 -37
- data/lib/samagotchi/session_access/remote_peers.rb +3 -3
- data/lib/samagotchi/session_chain/links.rb +8 -3
- data/lib/samagotchi/session_continue.rb +7 -6
- data/lib/samagotchi/session_manager/spawn.rb +6 -1
- data/lib/samagotchi/session_stores.rb +16 -11
- data/lib/samagotchi/system_one/runs.rb +5 -2
- data/lib/samagotchi/testing/boundary.rb +0 -1
- data/lib/samagotchi/testing/host.rb +1 -0
- data/lib/samagotchi/testing/recorder.rb +3 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/peer_routes.rb +10 -5
- data/lib/samagotchi/web/public/agent_rows.js +206 -0
- data/lib/samagotchi/web/public/agent_runs.js +595 -0
- data/lib/samagotchi/web/public/answer_fold.js +41 -44
- data/lib/samagotchi/web/public/app.js +3 -3
- data/lib/samagotchi/web/public/block_fix.js +1 -1
- data/lib/samagotchi/web/public/cards_place.js +6 -4
- data/lib/samagotchi/web/public/history_reveal.js +5 -2
- data/lib/samagotchi/web/public/index.html +38 -2
- data/lib/samagotchi/web/public/page_view.js +1 -1
- data/lib/samagotchi/web/public/pages.css +1 -7
- data/lib/samagotchi/web/public/peer_mailbox_api.js +2 -1
- data/lib/samagotchi/web/public/peer_note.js +4 -2
- data/lib/samagotchi/web/public/peer_row.js +37 -0
- data/lib/samagotchi/web/public/quiet_notes.js +12 -1
- data/lib/samagotchi/web/public/revise_stack.js +5 -2
- data/lib/samagotchi/web/public/send_turn.js +11 -3
- data/lib/samagotchi/web/public/turn_block.js +4 -0
- data/lib/samagotchi/worker/mail.rb +11 -1
- data/lib/samagotchi/worker.rb +7 -5
- data/lib/samagotchi/worker_inbound/mail.rb +34 -6
- data/lib/samagotchi/worker_inbound.rb +11 -3
- data/lib/samagotchi/worker_link.rb +13 -1
- data/lib/samagotchi/worker_wakes.rb +33 -5
- metadata +5 -2
- data/lib/samagotchi/web/public/pages_group.js +0 -67
|
@@ -8,6 +8,7 @@ require_relative "../client_id"
|
|
|
8
8
|
require_relative "../context_note"
|
|
9
9
|
require_relative "../log"
|
|
10
10
|
require_relative "../peer_mailbox"
|
|
11
|
+
require_relative "../peer_cold"
|
|
11
12
|
require_relative "../peer_note"
|
|
12
13
|
require_relative "../peer_receipts"
|
|
13
14
|
require_relative "../turn_origin"
|
|
@@ -37,7 +38,8 @@ module Samagotchi
|
|
|
37
38
|
# The fourth inbound kind: a peer's messages from the mailbox
|
|
38
39
|
# (PeerMailbox), one note each, between turns. Mixed into WorkerInbound;
|
|
39
40
|
# reads @session, @engine, @wakes, @awaiting_continue, @stopped,
|
|
40
|
-
# @peer_registry, @peer_receipts, @mail_cap
|
|
41
|
+
# @peer_registry, @peer_receipts, @mail_cap, @cache_ttl, @wall_clock.
|
|
42
|
+
# See docs/internals/peers.md.
|
|
41
43
|
module MailPart
|
|
42
44
|
# The holds a caller gives (a wake can't run now) and the receipt's
|
|
43
45
|
# reason for each, beside :off and WorkerWakes#wake_hold's.
|
|
@@ -70,7 +72,7 @@ module Samagotchi
|
|
|
70
72
|
taken = items.select(&:taken?)
|
|
71
73
|
asking = taken.select(&:asked).last
|
|
72
74
|
@wakes.pause! if replayed && asking
|
|
73
|
-
reason = (replayed && asking ? :paused : hold) || (mail_hold(asking) if asking)
|
|
75
|
+
reason = (replayed && asking ? :paused : hold) || (mail_hold(asking, taken) if asking)
|
|
74
76
|
if asking && reason.nil? && @wakes.in_grace?
|
|
75
77
|
@mail_wake_waiting = true
|
|
76
78
|
return nil
|
|
@@ -123,16 +125,32 @@ module Samagotchi
|
|
|
123
125
|
end
|
|
124
126
|
|
|
125
127
|
# Why the batch's newest asking message can't wake chi now: :off
|
|
126
|
-
# (peer.turns is 0), else WorkerWakes#wake_hold's
|
|
127
|
-
# message is held by session.max_wakes only); nil: it may.
|
|
128
|
-
def mail_hold(asking)
|
|
128
|
+
# (peer.turns is 0), :cold (#cold_hold?), else WorkerWakes#wake_hold's
|
|
129
|
+
# (an agreeing message is held by session.max_wakes only); nil: it may.
|
|
130
|
+
def mail_hold(asking, batch)
|
|
129
131
|
return :off if @wakes.peer_max.zero?
|
|
132
|
+
return :cold if cold_hold?(batch)
|
|
130
133
|
|
|
131
134
|
@wakes.wake_hold(awaiting_continue: @awaiting_continue.call, peer: !agreed?(asking))
|
|
132
135
|
end
|
|
133
136
|
|
|
134
137
|
def agreed?(item) = PeerNote.agrees?(item&.message&.summary)
|
|
135
138
|
|
|
139
|
+
# Whether the batch is held because chi's cache is cold (PeerCold):
|
|
140
|
+
# none of its asking messages is an answer chi asked for (a +re+
|
|
141
|
+
# naming one of its sends, PeerCold.asked_for?) that doesn't agree, and
|
|
142
|
+
# the cache is cold now. An answer chi asked for wakes it cold or warm.
|
|
143
|
+
def cold_hold?(batch)
|
|
144
|
+
asked = batch.select(&:asked)
|
|
145
|
+
return false if asked.any? { |item| !agreed?(item) && asked_for?(item) }
|
|
146
|
+
|
|
147
|
+
PeerCold.cold?(ttl: @cache_ttl&.call, last_turn: @session.last_turn, used_tokens: PeerCold.used_tokens(@session_dir),
|
|
148
|
+
now: @wall_clock.call)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def asked_for?(item) = PeerCold.asked_for?(item.message, item.peer, session_id: @session.id,
|
|
152
|
+
continues: @session.continues, state_dir: @state_dir)
|
|
153
|
+
|
|
136
154
|
# The notes, in order; the last one starts the wake turn (+waking+),
|
|
137
155
|
# with the wake line of the newest message that asked (+asking+).
|
|
138
156
|
# @return [Hash, nil] the turn start's plain note
|
|
@@ -204,11 +222,21 @@ module Samagotchi
|
|
|
204
222
|
|
|
205
223
|
# The UIs hear once that a peer's message waits for the user's next
|
|
206
224
|
# one when a wake budget held it (as #peer_held does for the context
|
|
207
|
-
# path).
|
|
225
|
+
# path), or a cold cache did (its own notice and once-flag).
|
|
208
226
|
def mail_held(asking, reason)
|
|
227
|
+
return cold_held(PeerMailbox.label(asking.peer)) if asking && reason == :cold
|
|
228
|
+
|
|
209
229
|
key = { max_wakes: ChildRing::MAX_WAKES_KEY, peer_turns: WorkerWakes::PEER_TURNS_KEY }[reason]
|
|
210
230
|
peer_held(PeerMailbox.label(asking.peer), key) if asking && key
|
|
211
231
|
end
|
|
232
|
+
|
|
233
|
+
def cold_held(label)
|
|
234
|
+
return unless @wakes.notice_peer_cold!
|
|
235
|
+
|
|
236
|
+
@event_log.announce(type: :hook_notice, hook: "context", level: :info, between_turns: true,
|
|
237
|
+
text: "#{label} spoke up while chi's cache is cold; it waits as a note for your next " \
|
|
238
|
+
"message (a turn now would read the whole conversation again)")
|
|
239
|
+
end
|
|
212
240
|
end
|
|
213
241
|
end
|
|
214
242
|
end
|
|
@@ -105,8 +105,13 @@ module Samagotchi
|
|
|
105
105
|
# @param peer_receipts [PeerReceipts, nil] the worker's receipts
|
|
106
106
|
# @param mail_cap [#call, nil] the most characters of a peer's text a
|
|
107
107
|
# wake turn's note holds (the tool output cap)
|
|
108
|
+
# @param cache_ttl [#call, nil] the host's prompt-cache TTL in seconds,
|
|
109
|
+
# nil when its requests carry no breakpoints (Engine#remote_cache_ttl:
|
|
110
|
+
# the cold hold)
|
|
111
|
+
# @param wall_clock [#call] the time now (the cold hold's)
|
|
108
112
|
def initialize(session:, state_dir:, session_dir:, engine:, event_log:, turn_state:, context_absorber:, wakes:,
|
|
109
|
-
awaiting_continue:, stopped:, queue_command:, peer_registry: nil, peer_receipts: nil, mail_cap: nil
|
|
113
|
+
awaiting_continue:, stopped:, queue_command:, peer_registry: nil, peer_receipts: nil, mail_cap: nil,
|
|
114
|
+
cache_ttl: nil, wall_clock: -> { Time.now })
|
|
110
115
|
@session = session
|
|
111
116
|
@state_dir = state_dir
|
|
112
117
|
@session_dir = session_dir
|
|
@@ -121,6 +126,8 @@ module Samagotchi
|
|
|
121
126
|
@peer_registry = peer_registry
|
|
122
127
|
@peer_receipts = peer_receipts
|
|
123
128
|
@mail_cap = mail_cap
|
|
129
|
+
@cache_ttl = cache_ttl
|
|
130
|
+
@wall_clock = wall_clock
|
|
124
131
|
@initial_prompt_taken = false
|
|
125
132
|
@context_wake_waiting = false
|
|
126
133
|
# The link this one continues, when its notes may still come here
|
|
@@ -258,8 +265,9 @@ module Samagotchi
|
|
|
258
265
|
# (Session#first_turn_client_id: a delegating parent, a schedule). A
|
|
259
266
|
# delegate child's task is its parent's even without the field (one
|
|
260
267
|
# spawned by an older chi, or by a caller that names no sender). Else
|
|
261
|
-
# the owner's (ClientId::FIRST_PROMPT):
|
|
262
|
-
#
|
|
268
|
+
# the owner's (ClientId::FIRST_PROMPT): chi send's and no sender's save
|
|
269
|
+
# none (a web tab's or a TUI's does, from this chi on), nor did one from
|
|
270
|
+
# before either field.
|
|
263
271
|
# @return [TurnOrigin]
|
|
264
272
|
def initial_origin
|
|
265
273
|
client_id = @session.first_turn_client_id
|
|
@@ -33,7 +33,19 @@ module Samagotchi
|
|
|
33
33
|
# Bridge).
|
|
34
34
|
module WorkerLink
|
|
35
35
|
# The sidecar protocol this chi speaks; a sidecar with a bigger one is a
|
|
36
|
-
# newer worker's (Skew).
|
|
36
|
+
# newer worker's (Skew). The policy (docs/releasing.md, "Bumping
|
|
37
|
+
# WorkerLink::PROTOCOL"):
|
|
38
|
+
# - an additive link change (a new sidecar key, Bridge route or FEATURES
|
|
39
|
+
# name) is no bump: older clients ignore what they don't know;
|
|
40
|
+
# - bump only when an older client would do damage driving a newer
|
|
41
|
+
# worker: a sidecar key, route or feature name removed, renamed or given
|
|
42
|
+
# a new meaning, or the secret rule changed;
|
|
43
|
+
# - every bump needs the owner's OK and a CHANGELOG line ("older chi web
|
|
44
|
+
# and terminals refuse workers of this release until restarted"), and
|
|
45
|
+
# starts a new list in spec/worker_link_contract_spec.rb (which fails
|
|
46
|
+
# when an entry of this protocol's list goes);
|
|
47
|
+
# - a worker on an older protocol is never refused: a restart is
|
|
48
|
+
# suggested, never gated.
|
|
37
49
|
PROTOCOL = 1
|
|
38
50
|
|
|
39
51
|
# A worker whose sidecar publishes this may be asked what it is busy with
|
|
@@ -17,7 +17,11 @@ module Samagotchi
|
|
|
17
17
|
# A peer agent's messages (its mailbox: WorkerInbound#absorb_mail) have a
|
|
18
18
|
# budget of their own on top: peer.turns wake turns in a row,
|
|
19
19
|
# reset only by input typed in the web or a TUI (ClientId.interactive?),
|
|
20
|
-
# not by chi send, which a peer can run (docs/peers.md).
|
|
20
|
+
# not by chi send, which a peer can run (docs/peers.md). That count
|
|
21
|
+
# outlives the worker: it starts from +peer_in_a_row+ (what the last
|
|
22
|
+
# worker saved) and each change goes to +on_peer_count+ (the Worker saves
|
|
23
|
+
# it in peer_receipts.json), so a peer that speaks once per idle exit is
|
|
24
|
+
# held after peer.turns wakes as one that speaks every minute is.
|
|
21
25
|
class WorkerWakes
|
|
22
26
|
PEER_TURNS_KEY = "peer.turns"
|
|
23
27
|
PEER_TURNS_DEFAULT = 3
|
|
@@ -26,18 +30,25 @@ module Samagotchi
|
|
|
26
30
|
# @param clock [#call] monotonic seconds
|
|
27
31
|
# @param max [#call, nil] the budget; nil reads session.max_wakes
|
|
28
32
|
# @param peer_max [#call, nil] the peer budget; nil reads peer.turns
|
|
29
|
-
|
|
33
|
+
# @param peer_in_a_row [Integer] the peer wake turns in a row a previous
|
|
34
|
+
# worker left (PeerReceipts#peer_wakes)
|
|
35
|
+
# @param on_peer_count [#call, nil] called with the peer count each time
|
|
36
|
+
# it changes (on the loop thread)
|
|
37
|
+
def initialize(grace:, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }, max: nil, peer_max: nil,
|
|
38
|
+
peer_in_a_row: 0, on_peer_count: nil)
|
|
30
39
|
@grace = grace
|
|
31
40
|
@clock = clock
|
|
32
41
|
@max = max
|
|
33
42
|
@peer_max = peer_max
|
|
34
43
|
@started_at = clock.call
|
|
35
44
|
@in_a_row = 0
|
|
36
|
-
@peer_in_a_row = 0
|
|
45
|
+
@peer_in_a_row = peer_in_a_row.is_a?(Integer) && peer_in_a_row.positive? ? peer_in_a_row : 0
|
|
46
|
+
@on_peer_count = on_peer_count
|
|
37
47
|
@paused = false
|
|
38
48
|
@budget_noticed = false
|
|
39
49
|
@visitor_noticed = false
|
|
40
50
|
@peer_noticed = false
|
|
51
|
+
@cold_noticed = false
|
|
41
52
|
@handed = nil
|
|
42
53
|
end
|
|
43
54
|
|
|
@@ -134,7 +145,10 @@ module Samagotchi
|
|
|
134
145
|
# A wake turn starts; +peer+: for a peer's answer, which counts toward
|
|
135
146
|
# both budgets. @return [Integer] the wake turns in a row now
|
|
136
147
|
def count!(peer: false)
|
|
137
|
-
|
|
148
|
+
if peer
|
|
149
|
+
@peer_in_a_row += 1
|
|
150
|
+
@on_peer_count&.call(@peer_in_a_row)
|
|
151
|
+
end
|
|
138
152
|
@in_a_row += 1
|
|
139
153
|
end
|
|
140
154
|
|
|
@@ -154,8 +168,12 @@ module Samagotchi
|
|
|
154
168
|
@visitor_noticed = false
|
|
155
169
|
return unless interactive
|
|
156
170
|
|
|
157
|
-
@peer_in_a_row = 0
|
|
158
171
|
@peer_noticed = false
|
|
172
|
+
@cold_noticed = false
|
|
173
|
+
return if @peer_in_a_row.zero?
|
|
174
|
+
|
|
175
|
+
@peer_in_a_row = 0
|
|
176
|
+
@on_peer_count&.call(0)
|
|
159
177
|
end
|
|
160
178
|
|
|
161
179
|
# @return [Boolean] true the first time the budget is spent since the
|
|
@@ -184,6 +202,16 @@ module Samagotchi
|
|
|
184
202
|
@peer_noticed = true
|
|
185
203
|
end
|
|
186
204
|
|
|
205
|
+
# @return [Boolean] true the first time a peer's message is held for a
|
|
206
|
+
# cold cache (WorkerInbound::MailPart, :cold) since the last
|
|
207
|
+
# interactive input: the UIs hear it once. Apart from
|
|
208
|
+
# #notice_peer_budget!, whose text is about turn counts.
|
|
209
|
+
def notice_peer_cold!
|
|
210
|
+
return false if @cold_noticed
|
|
211
|
+
|
|
212
|
+
@cold_noticed = true
|
|
213
|
+
end
|
|
214
|
+
|
|
187
215
|
# The idle loop's sleep: +poll+, or less while a wake (the block says
|
|
188
216
|
# one is due) waits out the start grace.
|
|
189
217
|
def idle_wait(poll)
|
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.193.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Dmitry Dedov
|
|
@@ -636,6 +636,7 @@ files:
|
|
|
636
636
|
- lib/samagotchi/participants.rb
|
|
637
637
|
- lib/samagotchi/participants_watch.rb
|
|
638
638
|
- lib/samagotchi/paths.rb
|
|
639
|
+
- lib/samagotchi/peer_cold.rb
|
|
639
640
|
- lib/samagotchi/peer_command.rb
|
|
640
641
|
- lib/samagotchi/peer_continue.rb
|
|
641
642
|
- lib/samagotchi/peer_exchanges.rb
|
|
@@ -1022,6 +1023,8 @@ files:
|
|
|
1022
1023
|
- lib/samagotchi/web/project_list.rb
|
|
1023
1024
|
- lib/samagotchi/web/proxy_gate.rb
|
|
1024
1025
|
- lib/samagotchi/web/public/activity.js
|
|
1026
|
+
- lib/samagotchi/web/public/agent_rows.js
|
|
1027
|
+
- lib/samagotchi/web/public/agent_runs.js
|
|
1025
1028
|
- lib/samagotchi/web/public/annotate_actions.js
|
|
1026
1029
|
- lib/samagotchi/web/public/annotate_bar.css
|
|
1027
1030
|
- lib/samagotchi/web/public/annotate_bar.js
|
|
@@ -1133,7 +1136,6 @@ files:
|
|
|
1133
1136
|
- lib/samagotchi/web/public/page_table_model.js
|
|
1134
1137
|
- lib/samagotchi/web/public/page_view.js
|
|
1135
1138
|
- lib/samagotchi/web/public/pages.css
|
|
1136
|
-
- lib/samagotchi/web/public/pages_group.js
|
|
1137
1139
|
- lib/samagotchi/web/public/pages_list.js
|
|
1138
1140
|
- lib/samagotchi/web/public/pages_model.js
|
|
1139
1141
|
- lib/samagotchi/web/public/pages_wiring.js
|
|
@@ -1151,6 +1153,7 @@ files:
|
|
|
1151
1153
|
- lib/samagotchi/web/public/peer_links.js
|
|
1152
1154
|
- lib/samagotchi/web/public/peer_mailbox_api.js
|
|
1153
1155
|
- lib/samagotchi/web/public/peer_note.js
|
|
1156
|
+
- lib/samagotchi/web/public/peer_row.js
|
|
1154
1157
|
- lib/samagotchi/web/public/peer_text.js
|
|
1155
1158
|
- lib/samagotchi/web/public/picture_regions.js
|
|
1156
1159
|
- lib/samagotchi/web/public/picture_viewer.js
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
// All sessions' former Pages group: KEPT for an older cached app.js until
|
|
2
|
-
// the next release (the mixed-cache rule, docs/internals/web-frontend.md):
|
|
3
|
-
// remove after 0.189.0, with index.html's inert #pagesGroup and pages.css's
|
|
4
|
-
// group rules. Pages have their own way in now: the top bar's pages pill
|
|
5
|
-
// and the Pages list (pages_list.js, pages_wiring.js); nothing new imports
|
|
6
|
-
// this. What it did: above the session cards, one row of page cards, read
|
|
7
|
-
// when All sessions opened (GET /api/pages) and filtered by the search
|
|
8
|
-
// field (#filter), under a "Pages" heading and over a "Sessions" one.
|
|
9
|
-
//
|
|
10
|
-
// +root+: #pagesGroup. +filterEl+: #filter. +api+: data.js api.
|
|
11
|
-
// +open(name)+: views.js openPage. +model+: pages_model.js's list, read
|
|
12
|
-
// through instead of its own GET when given.
|
|
13
|
-
import { html, nothing, render } from "./vendor/lit-html.js";
|
|
14
|
-
import { createLatest } from "./latest.js";
|
|
15
|
-
import { pageCardMeta } from "./pages_list.js";
|
|
16
|
-
|
|
17
|
-
// A card's second line (the list's).
|
|
18
|
-
export { pageCardMeta };
|
|
19
|
-
|
|
20
|
-
// The pages the search shows: +query+ in the name or the title, any case.
|
|
21
|
-
export function filterPages(pages, query = "") {
|
|
22
|
-
const q = query.trim().toLowerCase();
|
|
23
|
-
if (!q) return pages;
|
|
24
|
-
return pages.filter((page) => page.name.toLowerCase().includes(q) || String(page.title || "").toLowerCase().includes(q));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function pagesGroupTemplate(pages, { open, now = Date.now() }) {
|
|
28
|
-
if (!pages.length) return nothing;
|
|
29
|
-
return html`<div class="pg-group-label">Pages</div>
|
|
30
|
-
<div class="pg-cards" role="list">${pages.map((page) => html`<button type="button" class="pg-card" role="listitem"
|
|
31
|
-
data-page=${page.name} @click=${() => open(page.name)}><b>${page.title || page.name}</b><span class="pg-card-meta">${pageCardMeta(page, now)}</span></button>`)}</div>
|
|
32
|
-
<div class="pg-group-label">Sessions</div>`;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function createPagesGroup({ root, filterEl, api, open, model = null, now = () => Date.now() }) {
|
|
36
|
-
let pages = [];
|
|
37
|
-
const latest = createLatest();
|
|
38
|
-
|
|
39
|
-
function draw() {
|
|
40
|
-
const shown = filterPages(pages, filterEl?.value || "");
|
|
41
|
-
root.hidden = !shown.length;
|
|
42
|
-
render(pagesGroupTemplate(shown, { open, now: now() }), root);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async function load() {
|
|
46
|
-
if (model) return model.load();
|
|
47
|
-
const fresh = latest.begin();
|
|
48
|
-
try {
|
|
49
|
-
const answer = await api("/api/pages");
|
|
50
|
-
if (!fresh()) return;
|
|
51
|
-
pages = Array.isArray(answer?.pages) ? answer.pages : [];
|
|
52
|
-
} catch (_) {
|
|
53
|
-
if (!fresh()) return;
|
|
54
|
-
pages = []; // the group is a way in, not a place for errors: the page itself says them
|
|
55
|
-
}
|
|
56
|
-
draw();
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// A failed read empties the group, as its own read does.
|
|
60
|
-
model?.subscribe((state) => {
|
|
61
|
-
pages = state.failed ? [] : state.pages;
|
|
62
|
-
draw();
|
|
63
|
-
});
|
|
64
|
-
filterEl?.addEventListener("input", draw);
|
|
65
|
-
draw();
|
|
66
|
-
return { load, redraw: draw };
|
|
67
|
-
}
|