samagotchi 0.193.0 → 0.194.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 +77 -1
- data/docs/bundle-authoring.md +1 -1
- data/docs/bundles.md +37 -12
- data/docs/cli.md +26 -10
- data/docs/configuration.md +71 -5
- data/docs/desktop.md +3 -0
- data/docs/guardrails.md +11 -1
- data/docs/hooks.md +1 -0
- data/docs/internals/engine.md +46 -3
- data/docs/internals/panes.md +66 -0
- data/docs/internals/peers.md +101 -5
- data/docs/internals/prompt-caching.md +4 -0
- data/docs/internals/session-lifecycle.md +9 -2
- data/docs/internals/web-frontend.md +16 -4
- data/docs/peers.md +79 -5
- data/docs/plugins.md +42 -4
- data/docs/sessions.md +15 -6
- data/docs/sub-agent.md +6 -4
- data/docs/system-one.md +4 -0
- data/docs/testing.md +3 -0
- data/lib/samagotchi/bridge/card_store.rb +1 -1
- data/lib/samagotchi/broadcast/triage_model.rb +17 -0
- data/lib/samagotchi/bundles/claude-peer/manifest.yml +8 -5
- data/lib/samagotchi/bundles/claude-peer/plugin.rb +24 -5
- data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +28 -7
- data/lib/samagotchi/bundles/claude-peer/scripts/pair +31 -8
- data/lib/samagotchi/bundles/codex-peer/manifest.yml +8 -5
- data/lib/samagotchi/bundles/codex-peer/plugin.rb +62 -6
- data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +23 -2
- data/lib/samagotchi/bundles/codex-peer/scripts/pair +28 -7
- data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/chi_invocation.rb +7 -0
- data/lib/samagotchi/client.rb +18 -0
- data/lib/samagotchi/codex_rollout.rb +93 -0
- data/lib/samagotchi/config.rb +15 -6
- data/lib/samagotchi/effective_model.rb +28 -2
- data/lib/samagotchi/engine/cards.rb +9 -4
- data/lib/samagotchi/engine/hooks.rb +8 -0
- data/lib/samagotchi/engine/models.rb +180 -7
- data/lib/samagotchi/engine/side_requests.rb +72 -13
- data/lib/samagotchi/engine/toolkit.rb +1 -1
- data/lib/samagotchi/engine/turn_helpers.rb +1 -1
- data/lib/samagotchi/engine.rb +11 -8
- data/lib/samagotchi/engine_snapshots.rb +16 -3
- data/lib/samagotchi/events.rb +1 -0
- data/lib/samagotchi/hooks.rb +3 -1
- data/lib/samagotchi/host_registry.rb +146 -16
- data/lib/samagotchi/idle_client.rb +13 -2
- data/lib/samagotchi/idle_recap.rb +33 -3
- data/lib/samagotchi/idle_target.rb +27 -7
- data/lib/samagotchi/llm/adapter_common.rb +4 -2
- data/lib/samagotchi/llm/anthropic_messages.rb +3 -2
- data/lib/samagotchi/llm/errors.rb +35 -4
- data/lib/samagotchi/llm/model_listing.rb +19 -0
- data/lib/samagotchi/llm/openai_chat.rb +3 -2
- data/lib/samagotchi/model_binding.rb +46 -4
- data/lib/samagotchi/model_catalog.rb +68 -8
- data/lib/samagotchi/model_profile.rb +35 -0
- data/lib/samagotchi/models_command.rb +8 -4
- data/lib/samagotchi/panes/kitty.rb +122 -0
- data/lib/samagotchi/panes.rb +185 -0
- data/lib/samagotchi/peer_announce.rb +245 -0
- data/lib/samagotchi/peer_command.rb +97 -11
- data/lib/samagotchi/peer_continue.rb +10 -0
- data/lib/samagotchi/peer_mailbox.rb +30 -7
- data/lib/samagotchi/peer_pair_here.rb +149 -0
- data/lib/samagotchi/peer_presence.rb +434 -0
- data/lib/samagotchi/peer_say.rb +17 -10
- data/lib/samagotchi/peer_wait_expiry.rb +47 -0
- data/lib/samagotchi/plugin/api.rb +12 -1
- data/lib/samagotchi/plugin/card.rb +68 -11
- data/lib/samagotchi/plugin/context.rb +20 -1
- data/lib/samagotchi/plugin/panes.rb +54 -0
- data/lib/samagotchi/plugin/peer.rb +52 -10
- data/lib/samagotchi/plugin/peer_pairing.rb +290 -0
- data/lib/samagotchi/plugin/peer_start.rb +301 -0
- data/lib/samagotchi/plugin/peer_start_hook.rb +207 -0
- data/lib/samagotchi/plugin/peers.rb +76 -3
- data/lib/samagotchi/recap_setup.rb +40 -3
- data/lib/samagotchi/self_report.rb +25 -6
- data/lib/samagotchi/send_command.rb +6 -6
- data/lib/samagotchi/served_lookup.rb +225 -0
- data/lib/samagotchi/served_ref.rb +41 -0
- data/lib/samagotchi/session_access/contract.rb +6 -0
- data/lib/samagotchi/session_access/local.rb +54 -6
- data/lib/samagotchi/session_access/remote.rb +4 -4
- data/lib/samagotchi/session_access/remote_peers.rb +7 -4
- data/lib/samagotchi/session_commands.rb +40 -8
- data/lib/samagotchi/session_continue.rb +6 -2
- data/lib/samagotchi/session_lifecycle/allowed.rb +4 -2
- data/lib/samagotchi/session_manager/delivery.rb +5 -0
- data/lib/samagotchi/session_manager/lifecycle.rb +58 -7
- data/lib/samagotchi/session_manager/removal.rb +3 -0
- data/lib/samagotchi/session_model_settings.rb +7 -3
- data/lib/samagotchi/session_stores.rb +9 -0
- data/lib/samagotchi/sessions_command.rb +16 -3
- data/lib/samagotchi/system_one/http_backend.rb +111 -10
- data/lib/samagotchi/system_prompt.rb +11 -4
- data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -1
- data/lib/samagotchi/terminal_ui/formatting.rb +26 -12
- data/lib/samagotchi/terminal_ui/status_row.rb +20 -3
- data/lib/samagotchi/terminal_ui.rb +1 -1
- data/lib/samagotchi/testing/bundle.rb +6 -2
- data/lib/samagotchi/turn_endings.rb +6 -2
- data/lib/samagotchi/user_typed.rb +29 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +8 -20
- data/lib/samagotchi/web/participant_review.rb +1 -1
- data/lib/samagotchi/web/participant_view.rb +1 -1
- data/lib/samagotchi/web/peer_routes.rb +33 -13
- data/lib/samagotchi/web/public/app.js +1 -1
- data/lib/samagotchi/web/public/card.js +26 -10
- data/lib/samagotchi/web/public/context_chips.js +24 -2
- data/lib/samagotchi/web/public/copy.js +8 -2
- data/lib/samagotchi/web/public/data.js +2 -1
- data/lib/samagotchi/web/public/format.js +9 -1
- data/lib/samagotchi/web/public/index.html +18 -0
- data/lib/samagotchi/web/public/info_bar_model.js +1 -1
- data/lib/samagotchi/web/public/model_pick.js +18 -5
- data/lib/samagotchi/web/public/model_picker.js +1 -1
- data/lib/samagotchi/web/public/peer_chip.js +15 -5
- data/lib/samagotchi/web/public/peer_data.js +4 -2
- data/lib/samagotchi/web/public/peer_wait.js +36 -0
- data/lib/samagotchi/web/public/served_ref.js +9 -0
- data/lib/samagotchi/web/public/session_fields.js +1 -0
- data/lib/samagotchi/web/public/session_state.js +4 -0
- data/lib/samagotchi/web/public/stage_missing.js +23 -6
- data/lib/samagotchi/web/public/stream_handlers.js +16 -0
- data/lib/samagotchi/web/public/version_notices.js +7 -1
- data/lib/samagotchi/web/session_control.rb +19 -10
- data/lib/samagotchi/web/session_show.rb +20 -0
- data/lib/samagotchi/web/update_ops.rb +2 -2
- data/lib/samagotchi/worker/command_queue.rb +3 -0
- data/lib/samagotchi/worker/mail.rb +14 -1
- data/lib/samagotchi/worker.rb +1 -0
- metadata +18 -1
|
@@ -141,8 +141,10 @@ module Samagotchi
|
|
|
141
141
|
|
|
142
142
|
# @param hint [String, nil] what to try, added to the summary
|
|
143
143
|
def initialize(message = nil, context_overflow: false, tools_unsupported: false, reasoning_refused: false,
|
|
144
|
-
thinking_signature: false, thinking_binding_unsupported: false, sampling_refused: false, hint: nil,
|
|
144
|
+
thinking_signature: false, thinking_binding_unsupported: false, sampling_refused: false, hint: nil,
|
|
145
|
+
model_not_served: false, **)
|
|
145
146
|
@sampling_refused = sampling_refused
|
|
147
|
+
@model_not_served = model_not_served
|
|
146
148
|
@context_overflow = context_overflow
|
|
147
149
|
@tools_unsupported = tools_unsupported
|
|
148
150
|
@reasoning_refused = reasoning_refused
|
|
@@ -183,6 +185,11 @@ module Samagotchi
|
|
|
183
185
|
# without those fields instead.
|
|
184
186
|
def sampling_refused? = @sampling_refused
|
|
185
187
|
|
|
188
|
+
# The host doesn't serve the model asked for now (ProviderErrors.not_served?):
|
|
189
|
+
# Splash's chat 404 code model_not_found, its System 1's 422 "not served
|
|
190
|
+
# by this endpoint". A served ref's turn names what it serves instead.
|
|
191
|
+
def model_not_served? = @model_not_served
|
|
192
|
+
|
|
186
193
|
def kind = :bad_request
|
|
187
194
|
|
|
188
195
|
def summary
|
|
@@ -453,6 +460,12 @@ module Samagotchi
|
|
|
453
460
|
# does not exist", "model X-1 not found" (unquoted, an id-like word,
|
|
454
461
|
# checked after the match). Bounded quantifiers: a server's long token
|
|
455
462
|
# can't make the match quadratic (Ruby 3.3 has no regex memoization).
|
|
463
|
+
# A host's answer for a model it doesn't serve now (Splash's System 1:
|
|
464
|
+
# 422 "model X is not served by this endpoint").
|
|
465
|
+
NOT_SERVED_RE = /not served by this endpoint/i
|
|
466
|
+
# The error.code a chat endpoint gives for it (Splash's
|
|
467
|
+
# /v1/chat/completions: 404 {"error":{"code":"model_not_found"}}).
|
|
468
|
+
NOT_SERVED_CODE = "model_not_found"
|
|
456
469
|
MODEL_NOT_FOUND_RE = /model\s+(?:["'`]([^"'`\s]{1,#{HINT_ID_MAX}})["'`]|([^"'`\s]{1,#{HINT_ID_MAX}}))\s+(?:was\s+)?(?:not found|does not exist)/i
|
|
457
470
|
# The ids a 404 lists after "Available models:".
|
|
458
471
|
AVAILABLE_MODELS_RE = /available models?:\s*(.+)\z/im
|
|
@@ -500,8 +513,10 @@ module Samagotchi
|
|
|
500
513
|
else
|
|
501
514
|
OutOfCredits.new(text, **options)
|
|
502
515
|
end
|
|
503
|
-
when 404
|
|
504
|
-
|
|
516
|
+
when 404
|
|
517
|
+
bad_request(text, status, message, options.merge(hint: model_hint(error_message(body, limit: nil)),
|
|
518
|
+
model_not_served: not_served?(message, body)))
|
|
519
|
+
when 400..499 then bad_request(text, status, message, options.merge(model_not_served: not_served?(message, body)))
|
|
505
520
|
when 500..599
|
|
506
521
|
ServerError.new(text, retryable: RETRYABLE_SERVER_STATUSES.include?(status),
|
|
507
522
|
retry_after: parse_retry_after(retry_after), **options)
|
|
@@ -535,7 +550,23 @@ module Samagotchi
|
|
|
535
550
|
from_error_json(body.to_s, host: host)
|
|
536
551
|
end
|
|
537
552
|
|
|
538
|
-
#
|
|
553
|
+
# Whether a 4xx says the model isn't served now: its message matches
|
|
554
|
+
# NOT_SERVED_RE, its error.code is NOT_SERVED_CODE, or it is Splash's
|
|
555
|
+
# Messages answer (error.type not_found_error, "model X not found").
|
|
556
|
+
# Never a bare 404 (a wrong path, a proxy's page).
|
|
557
|
+
def not_served?(message, body)
|
|
558
|
+
return true if NOT_SERVED_RE.match?(message.to_s)
|
|
559
|
+
|
|
560
|
+
parsed = parse_json(body.to_s)
|
|
561
|
+
error = parsed["error"] if parsed.is_a?(Hash)
|
|
562
|
+
return false unless error.is_a?(Hash)
|
|
563
|
+
|
|
564
|
+
error["code"] == NOT_SERVED_CODE ||
|
|
565
|
+
(error["type"] == "not_found_error" && MODEL_NOT_FOUND_RE.match?(message.to_s))
|
|
566
|
+
end
|
|
567
|
+
|
|
568
|
+
# A BadRequest for a 4xx, with its predicates from +message+ (and
|
|
569
|
+
# +options+' model_not_served:, from the body).
|
|
539
570
|
def bad_request(text, status, message, options)
|
|
540
571
|
tools = TOOLS_UNSUPPORTED_RE.match?(message)
|
|
541
572
|
signature = THINKING_SIGNATURE_RE.match?(message)
|
|
@@ -49,6 +49,25 @@ module Samagotchi
|
|
|
49
49
|
nil
|
|
50
50
|
end
|
|
51
51
|
|
|
52
|
+
# The cached list while it is fresh (models_ttl), else nil: never a
|
|
53
|
+
# GET (ServedLookup#cached, a Bridge read).
|
|
54
|
+
# @return [Array<ModelInfo>, nil]
|
|
55
|
+
def models_if_fresh
|
|
56
|
+
now = monotonic
|
|
57
|
+
@models_mutex.synchronize { @models if @models_at && (now - @models_at) < @models_ttl }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Takes +models+ as the cached list, listed just now elsewhere (the
|
|
61
|
+
# served lookup's probe listing): the window and vision lookups read
|
|
62
|
+
# it without a GET of their own.
|
|
63
|
+
def store_models!(models)
|
|
64
|
+
@models_mutex.synchronize do
|
|
65
|
+
@models = Array(models)
|
|
66
|
+
@models_at = monotonic
|
|
67
|
+
@models_failure_at = nil
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
52
71
|
private
|
|
53
72
|
|
|
54
73
|
def init_model_listing(models_ttl)
|
|
@@ -226,9 +226,10 @@ module Samagotchi
|
|
|
226
226
|
# @return [Hash] frozen
|
|
227
227
|
def thinking_off_refused_fields = {}.freeze
|
|
228
228
|
|
|
229
|
+
# @param cancel_controller [CancellationController, nil] a Stop cuts it
|
|
229
230
|
# @return [Array<ModelInfo>] the host's models (every page)
|
|
230
|
-
def list_models
|
|
231
|
-
list_model_pages { |after| URI("#{@base_url}/models#{"?after=#{URI.encode_www_form_component(after)}" if after}") }
|
|
231
|
+
def list_models(cancel_controller: nil)
|
|
232
|
+
list_model_pages(cancel_controller: cancel_controller) { |after| URI("#{@base_url}/models#{"?after=#{URI.encode_www_form_component(after)}" if after}") }
|
|
232
233
|
end
|
|
233
234
|
|
|
234
235
|
def self.served_model(payload)
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require_relative "effective_model"
|
|
4
4
|
require_relative "model_overlay"
|
|
5
5
|
require_relative "model_profile"
|
|
6
|
+
require_relative "served_ref"
|
|
6
7
|
|
|
7
8
|
module Samagotchi
|
|
8
9
|
# An Engine's model choice (ModelChoice: the model as given and as typed)
|
|
@@ -38,6 +39,9 @@ module Samagotchi
|
|
|
38
39
|
@resolution = nil
|
|
39
40
|
@model_key = nil
|
|
40
41
|
@fallback_model_key = nil
|
|
42
|
+
# A served choice starts on the id known now (no request): what the
|
|
43
|
+
# overlay key and notes key with until the first turn start lists.
|
|
44
|
+
@pin = [choice, registry.cached_served_id(choice.name)]
|
|
41
45
|
end
|
|
42
46
|
|
|
43
47
|
# @return [ModelChoice] the model turns run on now
|
|
@@ -51,8 +55,17 @@ module Samagotchi
|
|
|
51
55
|
# @return [String, nil]
|
|
52
56
|
attr_reader :model_key, :fallback_model_key
|
|
53
57
|
|
|
54
|
-
#
|
|
55
|
-
#
|
|
58
|
+
# The id a served choice (main:served) runs on: the one the last turn
|
|
59
|
+
# start listed (#follow!), seeded at start and by a switch with the id
|
|
60
|
+
# known then (#pin_served!); nil for any other model, or while nothing
|
|
61
|
+
# is known. Every read of the effective model passes it
|
|
62
|
+
# (EffectiveModel.of's served_id:), so one turn sees one id even when a
|
|
63
|
+
# side ask re-lists the host meanwhile.
|
|
64
|
+
# @return [String, nil]
|
|
65
|
+
def served_id = served_id_for(@choice)
|
|
66
|
+
|
|
67
|
+
# Moves to +choice+ (Engine#switch_model!): the given profile, the kept
|
|
68
|
+
# resolution and the served pin go with the old one.
|
|
56
69
|
def switch!(choice)
|
|
57
70
|
@mutex.synchronize do
|
|
58
71
|
# One assignment: a reader on another thread sees the old pair or the new one.
|
|
@@ -60,9 +73,36 @@ module Samagotchi
|
|
|
60
73
|
# A profile given to .new was for the starting model.
|
|
61
74
|
@given_profile = nil
|
|
62
75
|
@resolution = nil
|
|
76
|
+
@pin = nil
|
|
63
77
|
end
|
|
64
78
|
end
|
|
65
79
|
|
|
80
|
+
# A served choice's host now serves +id+ (Engine#follow_served!): a new
|
|
81
|
+
# choice object of the same name and typed name, so the profile kept by
|
|
82
|
+
# identity is dropped, pinned to +id+. Unlike #switch!, a given profile
|
|
83
|
+
# (--profile) stays: nobody typed /model.
|
|
84
|
+
def follow!(id)
|
|
85
|
+
@mutex.synchronize do
|
|
86
|
+
@choice = ModelChoice.new(name: @choice.name, typed: @choice.typed)
|
|
87
|
+
@resolution = nil
|
|
88
|
+
@pin = [@choice, id]
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Pins the served choice to +id+ without the resets (the id a start or a
|
|
93
|
+
# switch keyed with; nil while nothing was known).
|
|
94
|
+
def pin_served!(id)
|
|
95
|
+
@mutex.synchronize { @pin = [@choice, id] }
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# The pin for +choice+: the binding's when it was set for that choice.
|
|
99
|
+
# The pin and its choice are one value (one read, no lock: the profile's
|
|
100
|
+
# apply runs under the mutex and may read the model).
|
|
101
|
+
def served_id_for(choice)
|
|
102
|
+
choice_pinned, id = @pin
|
|
103
|
+
choice_pinned.equal?(choice) ? id : nil
|
|
104
|
+
end
|
|
105
|
+
|
|
66
106
|
# Which profile the effective model gets and where that came from
|
|
67
107
|
# (ModelProfile.resolve: --profile/env, models:, hosts.<name>.profile,
|
|
68
108
|
# the server's chat template, the name, qwen36). Resolved on first need
|
|
@@ -86,7 +126,7 @@ module Samagotchi
|
|
|
86
126
|
given = @given_profile
|
|
87
127
|
return ModelProfile::Resolution.new(profile: given, source: :given, detail: nil, retry: false) if given
|
|
88
128
|
|
|
89
|
-
EffectiveModel.of(@registry, choice).profile_resolution
|
|
129
|
+
EffectiveModel.of(@registry, choice, served_id: served_id_for(choice)).profile_resolution
|
|
90
130
|
end
|
|
91
131
|
|
|
92
132
|
# Keeps +resolution+ as +choice+'s and applies it, unless the model
|
|
@@ -131,7 +171,9 @@ module Samagotchi
|
|
|
131
171
|
def sync_model_key!
|
|
132
172
|
choice = @choice
|
|
133
173
|
@model_key = ModelOverlay.key_for(@bare_name.call(choice.name))
|
|
134
|
-
|
|
174
|
+
typed = @registry.parse_qualified_model(choice.typed).last
|
|
175
|
+
# A typed `served` keyed nothing: the id it resolves to is the key.
|
|
176
|
+
typed_key = ServedRef.word?(typed) ? nil : ModelOverlay.key_for(typed)
|
|
135
177
|
@fallback_model_key = typed_key == @model_key ? nil : typed_key
|
|
136
178
|
end
|
|
137
179
|
end
|
|
@@ -10,13 +10,16 @@ module Samagotchi
|
|
|
10
10
|
# host: the host that listed (or declares) the id; ref: the name
|
|
11
11
|
# `--model` takes for it (see .spell); configured: declared under
|
|
12
12
|
# hosts.<name>.models (HostModel.rows); unavailable: a declared id of a
|
|
13
|
-
# host that failed to list its models (still pickable)
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
# host that failed to list its models (still pickable); served: the
|
|
14
|
+
# host's `served` row (ref "host:served", id the one id it lists now).
|
|
15
|
+
Row = Data.define(:host, :id, :ref, :configured, :unavailable, :served) do
|
|
16
|
+
def initialize(host:, id:, ref:, configured: false, unavailable: false, served: false) = super
|
|
16
17
|
end
|
|
17
18
|
# rows: the default host's first, then the others by name, each host's
|
|
18
|
-
#
|
|
19
|
-
#
|
|
19
|
+
# served row first (a host whose own listing names exactly one id:
|
|
20
|
+
# ServedLookup.one_id, never its declared ids), then its declared ids
|
|
21
|
+
# (HostModel.rows; a host that failed keeps only those); warnings:
|
|
22
|
+
# "host: error" per host that failed.
|
|
20
23
|
Listing = Data.define(:rows, :warnings)
|
|
21
24
|
|
|
22
25
|
module_function
|
|
@@ -32,6 +35,7 @@ module Samagotchi
|
|
|
32
35
|
results.keys.sort_by { |name| [name == default_host ? 0 : 1, name] }.each do |host|
|
|
33
36
|
error = results[host].error
|
|
34
37
|
warnings << "#{host}: #{error}" if error
|
|
38
|
+
rows << served_row(host, results[host]) if served_id(results[host])
|
|
35
39
|
host_rows.fetch(host, []).each do |row|
|
|
36
40
|
id = row.id
|
|
37
41
|
next if id.strip.empty? || (!row.configured && id.end_with?(":batch"))
|
|
@@ -43,6 +47,16 @@ module Samagotchi
|
|
|
43
47
|
Listing.new(rows: rows, warnings: warnings)
|
|
44
48
|
end
|
|
45
49
|
|
|
50
|
+
# The one id +data+ (a HostListing) lists, nil when it failed or lists
|
|
51
|
+
# none or several: a `served` ref on that host would run on it.
|
|
52
|
+
def served_id(data) = data.error ? nil : ServedLookup.one_id(data.models)
|
|
53
|
+
|
|
54
|
+
# +host+'s served row: "host:served" (any host, the default too: one
|
|
55
|
+
# spelling everywhere), running on the id it lists now.
|
|
56
|
+
def served_row(host, data)
|
|
57
|
+
Row.new(host: host, id: served_id(data), ref: "#{host}:#{ServedRef::WORD}", served: true)
|
|
58
|
+
end
|
|
59
|
+
|
|
46
60
|
# A listed id as a ref that routes back to its host: bare on the
|
|
47
61
|
# default host (a bare id goes there first), "host:id" elsewhere and for
|
|
48
62
|
# a default-host id with a ':' (bare, "qwen3:8b" would name a host qwen3).
|
|
@@ -65,6 +79,7 @@ module Samagotchi
|
|
|
65
79
|
hash = { name: row.ref, host: row.host, id: row.id }
|
|
66
80
|
hash[:configured] = true if row.configured
|
|
67
81
|
hash[:unavailable] = true if row.unavailable
|
|
82
|
+
hash[:served] = true if row.served
|
|
68
83
|
shadow = shadowed_by(row, registry)
|
|
69
84
|
shadow ? hash.merge(shadowed_by: shadow) : hash
|
|
70
85
|
end
|
|
@@ -73,8 +88,11 @@ module Samagotchi
|
|
|
73
88
|
aliases: aliases(registry), warnings: listing.warnings }
|
|
74
89
|
end
|
|
75
90
|
|
|
76
|
-
# The alias (or the ref) that takes +row+'s name elsewhere, or nil
|
|
91
|
+
# The alias (or the ref) that takes +row+'s name elsewhere, or nil
|
|
92
|
+
# (never for a served row: `served` is reserved, no alias takes it).
|
|
77
93
|
def shadowed_by(row, registry)
|
|
94
|
+
return nil if row.served
|
|
95
|
+
|
|
78
96
|
target = registry.resolve(row.ref)
|
|
79
97
|
return nil if target.entry.name == row.host && target.bare_model == row.id
|
|
80
98
|
|
|
@@ -85,11 +103,53 @@ module Samagotchi
|
|
|
85
103
|
return [nil, nil] if name.to_s.strip.empty?
|
|
86
104
|
|
|
87
105
|
ref = registry.model_ref(name)
|
|
88
|
-
|
|
89
|
-
|
|
106
|
+
row = served_default_row(name, rows, registry)
|
|
107
|
+
unless ServedRef.served?(ref)
|
|
108
|
+
target = registry.resolve(name)
|
|
109
|
+
row = rows.find { |r| !r.served && r.host == target.entry.name && r.id.casecmp?(target.bare_model) }
|
|
110
|
+
end
|
|
90
111
|
[row ? row.ref : ref.ref, ref.alias_name ? name.to_s.strip : nil]
|
|
91
112
|
end
|
|
92
113
|
|
|
114
|
+
# The served row a served default (`served`, `main:served`, an alias of
|
|
115
|
+
# one) names: its host's, nil when it has none or +name+ isn't served.
|
|
116
|
+
def served_default_row(name, rows, registry)
|
|
117
|
+
return nil unless registry.served?(name)
|
|
118
|
+
|
|
119
|
+
host = registry.served_entry(name).name
|
|
120
|
+
rows.find { |r| r.served && r.host == host }
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# GET /api/models' models and default (chi web's picker): the web's own
|
|
124
|
+
# spelling, kept as it was (bare on the default host, "host:id"
|
|
125
|
+
# elsewhere), a served row as "host:served" with served: true and its id.
|
|
126
|
+
# The default the payload names: a served default is its row's name;
|
|
127
|
+
# one no row matches is unshifted ({name:, host: nil, id: name}).
|
|
128
|
+
# @param listing [Listing, nil] nil: the hosts are still listing
|
|
129
|
+
# @return [Array(Array<Hash>, String, nil)] the models and the default
|
|
130
|
+
def web_models(listing, registry:, default:)
|
|
131
|
+
default_host = registry.default_entry&.name
|
|
132
|
+
models = Array(listing&.rows).map do |row|
|
|
133
|
+
model = { name: web_name(row, default_host), host: row.host, id: row.id }
|
|
134
|
+
model[:configured] = true if row.configured
|
|
135
|
+
model[:unavailable] = true if row.unavailable
|
|
136
|
+
model[:served] = true if row.served
|
|
137
|
+
model
|
|
138
|
+
end
|
|
139
|
+
served = default && listing ? served_default_row(default, listing.rows, registry) : nil
|
|
140
|
+
default = served.ref if served
|
|
141
|
+
models.unshift({ name: default, host: nil, id: default }) if default && models.none? { |m| m[:name].casecmp?(default) }
|
|
142
|
+
[models, default]
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# A row's name on chi web: a served row's ref, a default-host id bare,
|
|
146
|
+
# another host's "host:id".
|
|
147
|
+
def web_name(row, default_host)
|
|
148
|
+
return row.ref if row.served
|
|
149
|
+
|
|
150
|
+
row.host == default_host ? row.id : "#{row.host}:#{row.id}"
|
|
151
|
+
end
|
|
152
|
+
|
|
93
153
|
# config.yml's aliases, each under the host its target goes to: the
|
|
94
154
|
# default host's first, then by host and name.
|
|
95
155
|
def aliases(registry)
|
|
@@ -241,6 +241,8 @@ module Samagotchi
|
|
|
241
241
|
hosts ||= Samagotchi::HostConfig.all(env: env)
|
|
242
242
|
lists ||= Samagotchi::ModelListStore
|
|
243
243
|
parsed = Samagotchi::HostConfig.model_ref(model_name, env: env, hosts: hosts)
|
|
244
|
+
return served_warning(parsed, hosts, env, lists, relist, now) if Samagotchi::ServedRef.served?(parsed)
|
|
245
|
+
|
|
244
246
|
host = parsed.host_name
|
|
245
247
|
return nil unless host
|
|
246
248
|
# Declared under hosts.<name>.models: served whatever the host lists.
|
|
@@ -270,6 +272,39 @@ module Samagotchi
|
|
|
270
272
|
unknown_model_message(parsed.id, host, list.ids)
|
|
271
273
|
end
|
|
272
274
|
|
|
275
|
+
# A served ref's spawn-time check (main:served; a bare one is the default
|
|
276
|
+
# host's): never "doesn't list model served". Its host's list, saved less
|
|
277
|
+
# than SERVED_LIST_MAX_AGE ago or re-listed once (bounded as a miss's
|
|
278
|
+
# re-list), warns only when it isn't exactly one id
|
|
279
|
+
# (ServedLookup.refusal); no answer warns nothing: the worker's turn
|
|
280
|
+
# says so itself.
|
|
281
|
+
# @return [String, nil]
|
|
282
|
+
def self.served_warning(parsed, hosts, env, lists, relist, now)
|
|
283
|
+
require_relative "served_lookup"
|
|
284
|
+
host = parsed.host_name || default_host_name(hosts)
|
|
285
|
+
return nil unless host
|
|
286
|
+
|
|
287
|
+
saved = lists.read(env: env)[host]
|
|
288
|
+
ids = saved.ids if saved && saved.age(now: now) < SERVED_LIST_MAX_AGE
|
|
289
|
+
ids ||= relist_or_nil(relist || default_relist(hosts), host, env)
|
|
290
|
+
return nil unless ids
|
|
291
|
+
|
|
292
|
+
refusal = Samagotchi::ServedLookup.refusal(host, ids)
|
|
293
|
+
refusal && "#{refusal}; started it anyway"
|
|
294
|
+
end
|
|
295
|
+
private_class_method :served_warning
|
|
296
|
+
|
|
297
|
+
# Seconds a saved list stands for a served ref's spawn check.
|
|
298
|
+
SERVED_LIST_MAX_AGE = 60
|
|
299
|
+
|
|
300
|
+
# The host a bare ref goes to when none lists it: "default", else the
|
|
301
|
+
# first configured (HostRegistry#default_entry).
|
|
302
|
+
def self.default_host_name(hosts)
|
|
303
|
+
names = hosts.keys.map { |key| key.to_s.strip.downcase }
|
|
304
|
+
names.include?("default") ? "default" : names.first
|
|
305
|
+
end
|
|
306
|
+
private_class_method :default_host_name
|
|
307
|
+
|
|
273
308
|
# How long a miss's re-list may take before it is treated as failed: a
|
|
274
309
|
# spawn (`chi send --new --model`, delegate) warns from the saved list
|
|
275
310
|
# then, rather than wait on a slow host.
|
|
@@ -22,7 +22,9 @@ module Samagotchi
|
|
|
22
22
|
Usage: chi models [--format text|json] [--timeout S] [TEXT]
|
|
23
23
|
Lists the models every configured host offers, as the names
|
|
24
24
|
--model takes: the default first, a default-host id bare, another
|
|
25
|
-
host's as host:id,
|
|
25
|
+
host's as host:id, a host listing one model also as
|
|
26
|
+
"host:served → id" (it follows what the host serves), then the
|
|
27
|
+
aliases as "name -> ref". A host that
|
|
26
28
|
fails or doesn't answer in time is noted on stderr; the ids
|
|
27
29
|
declared under its hosts.<name>.models are still listed.
|
|
28
30
|
TEXT only the names containing it (any case)
|
|
@@ -105,10 +107,12 @@ module Samagotchi
|
|
|
105
107
|
# @param down_hosts [Array<String>] hosts that failed but declare ids
|
|
106
108
|
def print_text(payload, down_hosts)
|
|
107
109
|
default = payload[:default]
|
|
108
|
-
|
|
110
|
+
rows = payload[:models].reject { |m| m[:shadowed_by] }
|
|
111
|
+
# A served row names the id it runs on now: "main:served → q8".
|
|
112
|
+
shown = ->(name) { (row = rows.find { |m| m[:served] && m[:name] == name }) ? "#{name} → #{row[:id]}" : name }
|
|
109
113
|
shown_default = default && (payload[:filter].nil? || default.downcase.include?(payload[:filter].downcase))
|
|
110
|
-
@stdout.puts(default) if shown_default
|
|
111
|
-
(
|
|
114
|
+
@stdout.puts(shown.call(default)) if shown_default
|
|
115
|
+
(rows.map { |m| m[:name] } - [default]).each { |name| @stdout.puts(shown.call(name)) }
|
|
112
116
|
payload[:aliases].each { |a| @stdout.puts("#{a[:name]} -> #{a[:ref]}") }
|
|
113
117
|
payload[:warnings].each do |w|
|
|
114
118
|
kept = down_hosts.any? { |host| w.start_with?("#{host}: ") }
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Samagotchi
|
|
6
|
+
class Panes
|
|
7
|
+
# panes.rb requires this file before it defines Pane: an autoload, not a
|
|
8
|
+
# circular require, for a load of this file alone.
|
|
9
|
+
autoload :Pane, File.expand_path("../panes", __dir__)
|
|
10
|
+
|
|
11
|
+
# kitty's pure parts, ported from the desktop helper's Kitty.swift as
|
|
12
|
+
# they are (socketPath, socketName, the window list) and
|
|
13
|
+
# AgentText.cleanTitle: which files are sockets of a listen_on, and a
|
|
14
|
+
# `kitty @ ls` as panes. Panes runs kitty.
|
|
15
|
+
module Kitty
|
|
16
|
+
module_function
|
|
17
|
+
|
|
18
|
+
# listen_on's path with `~`, `$VAR` and `${VAR}` expanded from +env+
|
|
19
|
+
# (an unknown variable stays as written: kitty.conf's `${KITTY_PID}`
|
|
20
|
+
# is not expanded by kitty either). nil for anything but `unix:`.
|
|
21
|
+
def socket_path(listen_on, env)
|
|
22
|
+
text = listen_on.to_s
|
|
23
|
+
return nil unless text.start_with?("unix:")
|
|
24
|
+
|
|
25
|
+
path = text.delete_prefix("unix:")
|
|
26
|
+
home = env["HOME"] || Dir.home
|
|
27
|
+
path = home + path[1..] if path == "~" || path.start_with?("~/")
|
|
28
|
+
path.gsub(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}|\$([A-Za-z_][A-Za-z0-9_]*)/) { |whole| env[Regexp.last_match(1) || Regexp.last_match(2)] || whole }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Whether +name+ is a socket name kitty makes from +base+: base
|
|
32
|
+
# itself, base with `-<pid>` appended (listen_on without {kitty_pid}),
|
|
33
|
+
# or base with {kitty_pid} filled in.
|
|
34
|
+
def socket_name?(name, base)
|
|
35
|
+
parts = base.split("{kitty_pid}", -1)
|
|
36
|
+
pattern = parts.map { |part| Regexp.escape(part) }.join("\\d+")
|
|
37
|
+
full = parts.size > 1 ? "\\A#{pattern}\\z" : "\\A#{pattern}(-\\d+)?\\z"
|
|
38
|
+
name.match?(Regexp.new(full))
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# The sockets listen_on names: the files in its folder whose names
|
|
42
|
+
# match, sockets only (a directory listing, never a glob: the path
|
|
43
|
+
# may hold `${ }` literally, which a glob's braces would read as
|
|
44
|
+
# alternation). Sorted.
|
|
45
|
+
def sockets(listen_on, env)
|
|
46
|
+
path = socket_path(listen_on, env) or return []
|
|
47
|
+
dir = File.dirname(path)
|
|
48
|
+
base = File.basename(path)
|
|
49
|
+
Dir.children(dir).select { |name| socket_name?(name, base) }.sort.map { |name| File.join(dir, name) }
|
|
50
|
+
.select { |file| File.socket?(file) }
|
|
51
|
+
rescue SystemCallError
|
|
52
|
+
[]
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# A `kitty @ ls` on +socket+ as panes: every window, its key
|
|
56
|
+
# (`<socket>#<window id>`), cleaned title, cwd and the pids of its
|
|
57
|
+
# foreground processes.
|
|
58
|
+
# @return [Array<Pane>]
|
|
59
|
+
def panes(ls, socket)
|
|
60
|
+
data = JSON.parse(ls.to_s)
|
|
61
|
+
return [] unless data.is_a?(Array)
|
|
62
|
+
|
|
63
|
+
data.flat_map do |os_window|
|
|
64
|
+
Array(os_window.is_a?(Hash) ? os_window["tabs"] : nil).flat_map do |tab|
|
|
65
|
+
Array(tab.is_a?(Hash) ? tab["windows"] : nil).filter_map { |window| pane(window, socket) }
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
rescue JSON::ParserError
|
|
69
|
+
[]
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def pane(window, socket)
|
|
73
|
+
return nil unless window.is_a?(Hash) && window["id"].is_a?(Integer)
|
|
74
|
+
|
|
75
|
+
pids = Array(window["foreground_processes"]).filter_map { |process| process["pid"] if process.is_a?(Hash) && process["pid"].is_a?(Integer) }
|
|
76
|
+
Pane.new(driver: "kitty", key: "#{socket}##{window["id"]}", title: clean_title(window["title"].to_s),
|
|
77
|
+
cwd: window["cwd"].to_s, pids: pids)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# A title without the status glyph agents put in front ("✳ Claude Code").
|
|
81
|
+
def clean_title(title)
|
|
82
|
+
first = title[0]
|
|
83
|
+
return title unless first && first.ord > 127 && !first.match?(/\p{Alpha}/) && title[1] == " "
|
|
84
|
+
|
|
85
|
+
title[2..]
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# The socket #open launches on, of +lists+ ([socket, its `ls`] for
|
|
89
|
+
# each kitty that answered): the one with a focused OS window, else
|
|
90
|
+
# the newest socket file (the kitty started last); nil for none.
|
|
91
|
+
def target(lists)
|
|
92
|
+
focused = lists.find { |_, ls| focused?(ls) }
|
|
93
|
+
return focused.first if focused
|
|
94
|
+
|
|
95
|
+
lists.map(&:first).max_by { |socket| File.mtime(socket) }
|
|
96
|
+
rescue SystemCallError
|
|
97
|
+
lists.map(&:first).last
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Whether a `kitty @ ls` has an OS window with the focus.
|
|
101
|
+
def focused?(ls)
|
|
102
|
+
data = JSON.parse(ls.to_s)
|
|
103
|
+
data.is_a?(Array) && data.any? { |os_window| os_window.is_a?(Hash) && os_window["is_focused"] == true }
|
|
104
|
+
rescue JSON::ParserError
|
|
105
|
+
false
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# +text+ as one bracketed paste, its own paste markers taken out (as
|
|
109
|
+
# the helper's AgentText.text).
|
|
110
|
+
def bracketed(text)
|
|
111
|
+
"\e[200~#{text.to_s.gsub("\e[200~", "").gsub("\e[201~", "")}\e[201~"
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# A `<socket>#<window id>` key back into [socket, id]; nil for another.
|
|
115
|
+
def parse(key)
|
|
116
|
+
socket, id = key.to_s.rpartition("#").values_at(0, 2)
|
|
117
|
+
number = Integer(id, 10, exception: false)
|
|
118
|
+
socket.empty? || number.nil? ? nil : [socket, number]
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|