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
|
@@ -64,7 +64,7 @@ module Samagotchi
|
|
|
64
64
|
# SessionMetrics keys its per-model segments on it. nil when it can't be
|
|
65
65
|
# named (the turn's record then has none).
|
|
66
66
|
def generation_model_ref(model)
|
|
67
|
-
model.
|
|
67
|
+
model.turn_ref
|
|
68
68
|
rescue StandardError => e
|
|
69
69
|
Log.warn(:turn, "model_ref_failed", error: e.class.name, msg: e.message.to_s[0, 200])
|
|
70
70
|
nil
|
data/lib/samagotchi/engine.rb
CHANGED
|
@@ -261,7 +261,7 @@ module Samagotchi
|
|
|
261
261
|
# The session's settings on its effective model (SessionModelSettings),
|
|
262
262
|
# read at use: /model moves the choice, #session= the session.
|
|
263
263
|
@model_settings = SessionModelSettings.new(registry: @host_registry, choice: -> { @model_binding.choice },
|
|
264
|
-
session: -> { @session })
|
|
264
|
+
session: -> { @session }, served_id: ->(choice) { @model_binding.served_id_for(choice) })
|
|
265
265
|
# An injected client (specs) stands in for every host's client.
|
|
266
266
|
@host_registry.client_override = client if client
|
|
267
267
|
@client = effective_model.client
|
|
@@ -590,7 +590,7 @@ module Samagotchi
|
|
|
590
590
|
|
|
591
591
|
# The turn endings' builders (see #initialize for when).
|
|
592
592
|
def build_turn_endings
|
|
593
|
-
TurnEndings.new(failure_summary: ->(error) { failure_summary(error) })
|
|
593
|
+
TurnEndings.new(failure_summary: ->(error) { failure_summary(error) }, served_line: ->(error) { served_failure_line(error) })
|
|
594
594
|
end
|
|
595
595
|
private :build_turn_endings
|
|
596
596
|
|
|
@@ -831,7 +831,7 @@ module Samagotchi
|
|
|
831
831
|
recap = RecapSetup.build(@recap_kwarg, turn_state: @turn_state, messages_json: -> { messages_json_for_recap },
|
|
832
832
|
emit_recap: ->(**fields) { emit_recap(**fields) },
|
|
833
833
|
on_usage: ->(usage) { note_side_usage(usage) }, host_registry: @host_registry,
|
|
834
|
-
session_target: -> { session_model_recap_target },
|
|
834
|
+
session_target: ->(**opts) { session_model_recap_target(**opts) },
|
|
835
835
|
session_id: -> { @session&.id }, state_dir: -> { session_state_dir },
|
|
836
836
|
session_config: -> { @session&.settings&.core })
|
|
837
837
|
recap.awaiting_continue = @recap_awaiting_continue if recap && @recap_awaiting_continue
|
|
@@ -1102,6 +1102,7 @@ module Samagotchi
|
|
|
1102
1102
|
image_refs, image_error = turn_image_refs(session, images)
|
|
1103
1103
|
# Emit turn_started event
|
|
1104
1104
|
bind_metrics(session)
|
|
1105
|
+
served_started = served_turn_start # a served model's host listed, a change followed: cut_after is the new model's
|
|
1105
1106
|
turn_started = { type: :turn_started, session_id: session.id, prompt: turn.prompt, turn_id: turn.id, at: turn.at }
|
|
1106
1107
|
turn_started[:continue] = true if turn.continue
|
|
1107
1108
|
turn_started[:resumed] = true if turn.resumed?
|
|
@@ -1114,6 +1115,8 @@ module Samagotchi
|
|
|
1114
1115
|
emit_event(turn.on_event, turn.tag(turn_started))
|
|
1115
1116
|
raise image_error if image_error
|
|
1116
1117
|
|
|
1118
|
+
served_started.call
|
|
1119
|
+
|
|
1117
1120
|
# On mode's ask starts here too; the turn joins it before
|
|
1118
1121
|
# apply_toolkit! below, so its latency hides behind the probes and
|
|
1119
1122
|
# the init wait.
|
|
@@ -1234,12 +1237,12 @@ module Samagotchi
|
|
|
1234
1237
|
# Ask the effective model's backend.
|
|
1235
1238
|
def generate(turn, max_iterations:, max_tool_output_chars:, pending_input:)
|
|
1236
1239
|
# One resolve for the request: nothing runs between these reads that
|
|
1237
|
-
# could change what a resolve gives (the hooks ran before).
|
|
1238
|
-
model =
|
|
1240
|
+
# could change what a resolve gives (the hooks ran before; a served model is pinned first).
|
|
1241
|
+
model = generate_model
|
|
1239
1242
|
sync_kernel_client!(model)
|
|
1240
|
-
#
|
|
1241
|
-
# host selection already done via active client.
|
|
1242
|
-
bare_for_backend =
|
|
1243
|
+
# The id the host is sent (HostRegistry#sent_model_id's, from this
|
|
1244
|
+
# resolve); host selection already done via active client.
|
|
1245
|
+
bare_for_backend = model.bare
|
|
1243
1246
|
# The turn's settings, set once here into its context (with the model
|
|
1244
1247
|
# key and name the gate and memory_read use: frozen now, after the
|
|
1245
1248
|
# hooks that may switch the model), which goes down the backend's
|
|
@@ -79,6 +79,9 @@ module Samagotchi
|
|
|
79
79
|
**setting_chips(SessionSetting::STATS, model),
|
|
80
80
|
prompt_notes: prompt_notes.map(&:to_h), prompt_files: prompt_builder.prompt_files,
|
|
81
81
|
price_host: model.host_name)
|
|
82
|
+
# A served model: its ref and the id it runs on now (/stats' model line).
|
|
83
|
+
served_id = served_id_of(model)
|
|
84
|
+
snapshot = snapshot.merge(served_ref: model.ref, model_served: served_id) if served_id
|
|
82
85
|
kit_prompt = kit_prompt_stats
|
|
83
86
|
snapshot = snapshot.merge(kit_prompt: kit_prompt) if kit_prompt
|
|
84
87
|
# The plugins' prompt sections the built prompt holds ({bundle:, name:}).
|
|
@@ -133,6 +136,8 @@ module Samagotchi
|
|
|
133
136
|
# without the probe)
|
|
134
137
|
# :served_expected_by [String, nil] the host whose models: entry names
|
|
135
138
|
# that served model under served: (#served_expected_by)
|
|
139
|
+
# :model_served [String, nil] the id a served model (main:served) runs
|
|
140
|
+
# on now (EffectiveModel#served_id), nil for any other model
|
|
136
141
|
# :recap_enabled [Boolean] whether an idle recap is configured, with
|
|
137
142
|
# :recap_min_user_turns and :recap_inactivity_seconds (nil when not)
|
|
138
143
|
# :participants [Array<Hash>] who is in the review now ({id:, name:,
|
|
@@ -162,6 +167,7 @@ module Samagotchi
|
|
|
162
167
|
served_model: served_pair[0],
|
|
163
168
|
served_model_for: served_pair[1],
|
|
164
169
|
served_expected_by: model&.served_expected_by(served_pair[0]),
|
|
170
|
+
model_served: served_id_of(model),
|
|
165
171
|
context_status: last_context_status&.dup || saved_context_status(metrics[:context], model: model),
|
|
166
172
|
**setting_chips(SessionSetting::ALL, model),
|
|
167
173
|
recap_enabled: !recap.nil?,
|
|
@@ -173,13 +179,14 @@ module Samagotchi
|
|
|
173
179
|
|
|
174
180
|
# See Engine#served_model. A report for another name (before a /model switch)
|
|
175
181
|
# doesn't count. Without a model (EffectiveModel), no probe. A remote
|
|
176
|
-
# chat host has no /props: nil until a turn. The name asked is
|
|
177
|
-
#
|
|
182
|
+
# chat host has no /props: nil until a turn. The name asked is the id
|
|
183
|
+
# sent: the model's own (EffectiveModel#bare) when given, else never
|
|
184
|
+
# resolved (HostRegistry#sent_model_id), so it holds on a broken resolve.
|
|
178
185
|
# Only bare_model_name and the snapshot lookup are left to fail here:
|
|
179
186
|
# the probe's failure is EffectiveModel#served_from_props's.
|
|
180
187
|
def served_model_for(snapshot, model: nil)
|
|
181
188
|
Degrade.call(:model, "served_model_unreadable", fallback: [nil, nil]) do
|
|
182
|
-
asked =
|
|
189
|
+
asked = model ? model.bare : bare_model_name(effective_model_name)
|
|
183
190
|
return [snapshot[:served_model], asked] if snapshot[:served_model] && snapshot[:served_model_for] == asked
|
|
184
191
|
return [nil, nil] unless model
|
|
185
192
|
|
|
@@ -187,6 +194,12 @@ module Samagotchi
|
|
|
187
194
|
end
|
|
188
195
|
end
|
|
189
196
|
|
|
197
|
+
# +model+'s served id (EffectiveModel#served_id), nil for none or when
|
|
198
|
+
# +model+ is nil or can't say (never raises: the session state).
|
|
199
|
+
def served_id_of(model)
|
|
200
|
+
Degrade.call(:model, "served_id_unreadable") { model&.served_id }
|
|
201
|
+
end
|
|
202
|
+
|
|
190
203
|
# The next turn's kit prompt text for /stats: its kit and its estimated
|
|
191
204
|
# tokens (chars / 4), nil when its toolkit has none.
|
|
192
205
|
def kit_prompt_stats
|
data/lib/samagotchi/events.rb
CHANGED
data/lib/samagotchi/hooks.rb
CHANGED
|
@@ -27,11 +27,13 @@ module Samagotchi
|
|
|
27
27
|
# call is about to open: event[:refs] attaches refs to the asking
|
|
28
28
|
# reply (QuestionAsked)
|
|
29
29
|
# :after_tool_call — after tool execution, before result injection
|
|
30
|
+
# :peer_wait_ended — between turns: a /<peer> pair wait ran out with no peer
|
|
31
|
+
# (PeerWaitExpiry): {peer:, session_id:, minutes:}
|
|
30
32
|
module Hooks
|
|
31
33
|
# Every event a hook can be registered for, in a turn's order
|
|
32
34
|
# (docs/hooks.md). Plugin::Api#on takes any symbol: `chi bundle check`
|
|
33
35
|
# warns on one not here.
|
|
34
36
|
EVENTS = %i[session_start before_turn before_generation generation_progress after_generation before_tool_call
|
|
35
|
-
question_asked after_tool_call after_turn session_end].freeze
|
|
37
|
+
question_asked after_tool_call after_turn session_end peer_wait_ended].freeze
|
|
36
38
|
end
|
|
37
39
|
end
|
|
@@ -6,6 +6,8 @@ require_relative "log"
|
|
|
6
6
|
require_relative "model_ref"
|
|
7
7
|
require_relative "client"
|
|
8
8
|
require_relative "model_list_store"
|
|
9
|
+
require_relative "served_lookup"
|
|
10
|
+
require_relative "served_ref"
|
|
9
11
|
require_relative "llm/adapters"
|
|
10
12
|
require_relative "llm/dialect"
|
|
11
13
|
|
|
@@ -216,6 +218,9 @@ module Samagotchi
|
|
|
216
218
|
@cache = nil
|
|
217
219
|
@cache_at = nil
|
|
218
220
|
@model_index = nil # downcased model_id => [host_name] in hosts: order
|
|
221
|
+
# What each host serves now, for a `served` ref (ServedLookup).
|
|
222
|
+
@served = ServedLookup.new(lister: ->(entry, **opts) { probe_models(entry, **opts) }, adapter: ->(entry) { listing_adapter(entry) },
|
|
223
|
+
env: env, clock: @clock)
|
|
219
224
|
end
|
|
220
225
|
|
|
221
226
|
attr_reader :entries
|
|
@@ -245,10 +250,14 @@ module Samagotchi
|
|
|
245
250
|
# Thinking, SamplingSettings, VisionSupport and ModelProfile all take these.
|
|
246
251
|
# @param target [ModelTarget, nil] the resolved model's target (resolved when not given)
|
|
247
252
|
# @return [Array<String>]
|
|
253
|
+
# A served ref's own names (`served`, `main:served`) are left out: the
|
|
254
|
+
# resolved id (the target's bare model) is what its models: entry is
|
|
255
|
+
# under. An alias of main:served stays (its entry applies to any model).
|
|
248
256
|
def lookup_names(typed, resolved: nil, target: nil)
|
|
249
257
|
target ||= resolve(resolved || typed)
|
|
250
258
|
[typed, model_ref(typed).ref, resolved, parse_qualified_model(typed).last, target.bare_model]
|
|
251
|
-
.map { |name| name.to_s.strip }.reject(&:empty?)
|
|
259
|
+
.map { |name| name.to_s.strip }.reject(&:empty?)
|
|
260
|
+
.reject { |name| ServedRef.literal?(name, hosts: @entries) }.uniq
|
|
252
261
|
end
|
|
253
262
|
|
|
254
263
|
# The host a model name, alias or host:model ref goes to and the model
|
|
@@ -260,8 +269,14 @@ module Samagotchi
|
|
|
260
269
|
# process's listing: a worker spawned with --model never lists) has
|
|
261
270
|
# it, the same way; else the default host (no discovery here, the
|
|
262
271
|
# latency budget). Never by a substring.
|
|
263
|
-
|
|
272
|
+
#
|
|
273
|
+
# A served ref (ServedRef: main:served; a bare one is the default
|
|
274
|
+
# host's) goes to its host with +served_id+ when given (a turn's pin),
|
|
275
|
+
# else the id ServedLookup#cached knows, else the word itself: never a
|
|
276
|
+
# request, never a raise.
|
|
277
|
+
def host_for_model(raw_model, served_id: nil)
|
|
264
278
|
ref = model_ref(raw_model)
|
|
279
|
+
return served_target(ref, served_id) if ServedRef.served?(ref)
|
|
265
280
|
# ModelRef names a host only when it is one of ours
|
|
266
281
|
return [find_entry(ref.host_name), ref.id] if ref.host_name
|
|
267
282
|
|
|
@@ -299,14 +314,7 @@ module Samagotchi
|
|
|
299
314
|
def list_models_for(entry)
|
|
300
315
|
return adapter_for(entry).list_models if entry.chat? && !@client_override
|
|
301
316
|
|
|
302
|
-
|
|
303
|
-
if raw.is_a?(Hash)
|
|
304
|
-
id = raw["id"] || raw[:id] || raw["model"] || raw["name"]
|
|
305
|
-
LLM::ModelInfo.new(id: id.to_s, context_window: nil, supports_tools: nil, raw: raw)
|
|
306
|
-
else
|
|
307
|
-
LLM::ModelInfo.new(id: raw.to_s, context_window: nil, supports_tools: nil, raw: {})
|
|
308
|
-
end
|
|
309
|
-
end
|
|
317
|
+
raw_infos(client_for(entry).list_models)
|
|
310
318
|
end
|
|
311
319
|
|
|
312
320
|
# List one host's models and save them to ModelListStore: what
|
|
@@ -338,11 +346,78 @@ module Samagotchi
|
|
|
338
346
|
# the name sent to the server (host_for_model).
|
|
339
347
|
# @param raw_model [String] a model name, alias or host:model ref
|
|
340
348
|
# @return [ModelTarget]
|
|
341
|
-
|
|
342
|
-
|
|
349
|
+
# @param served_id [String, nil] a served ref's id as the turn pinned it
|
|
350
|
+
# (ignored for any other ref)
|
|
351
|
+
def resolve(raw_model, served_id: nil)
|
|
352
|
+
entry, bare = host_for_model(raw_model, served_id: served_id)
|
|
343
353
|
ModelTarget.new(model: raw_model, entry: entry, bare_model: bare, client: client_for(entry))
|
|
344
354
|
end
|
|
345
355
|
|
|
356
|
+
# The host entry a served ref goes to: its own host, else the default
|
|
357
|
+
# one (a bare `served`, or +host+: a hosts: name a side setting gives).
|
|
358
|
+
# @return [HostEntry]
|
|
359
|
+
def served_entry(raw, host: nil)
|
|
360
|
+
ref = raw.is_a?(ModelRef) ? raw : model_ref(raw)
|
|
361
|
+
(ref.host_name && find_entry(ref.host_name)) || (host && find_entry(host)) || default_entry
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
# Whether +raw+ is a served ref (ServedRef.served?, the alias applied).
|
|
365
|
+
def served?(raw) = ServedRef.served?(model_ref(raw))
|
|
366
|
+
|
|
367
|
+
# A served ref's id with no request (ServedLookup#cached), nil when
|
|
368
|
+
# nothing is known or +raw+ isn't one.
|
|
369
|
+
# @return [String, nil]
|
|
370
|
+
def cached_served_id(raw, host: nil)
|
|
371
|
+
ref = model_ref(raw)
|
|
372
|
+
ServedRef.served?(ref) ? @served.cached(served_entry(ref, host: host)) : nil
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
# Lists a served ref's host unless it was listed less than +max_age+
|
|
376
|
+
# seconds ago (ServedLookup#refresh!; bounded, no retry).
|
|
377
|
+
# +timeout+ (seconds) caps the listing's timeouts (System 1's deadline);
|
|
378
|
+
# +cancel_controller+ cuts it (a judgment's Stop).
|
|
379
|
+
# @return [ServedLookup::Resolution, nil] nil when +raw+ isn't a served ref
|
|
380
|
+
def refresh_served!(raw, max_age: 0, host: nil, timeout: nil, cancel_controller: nil)
|
|
381
|
+
ref = model_ref(raw)
|
|
382
|
+
return nil unless ServedRef.served?(ref)
|
|
383
|
+
|
|
384
|
+
@served.refresh!(served_entry(ref, host: host), max_age: max_age, timeout: timeout, cancel_controller: cancel_controller)
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
# Why a served ref can't run, as this registry's last listing of its host
|
|
388
|
+
# said (none or several models; ServedLookup#cached_refusal), with no
|
|
389
|
+
# request; nil when it can, nothing was listed yet, or +raw+ isn't one.
|
|
390
|
+
# @return [String, nil]
|
|
391
|
+
def cached_served_refusal(raw, host: nil)
|
|
392
|
+
ref = model_ref(raw)
|
|
393
|
+
ServedRef.served?(ref) ? @served.cached_refusal(served_entry(ref, host: host)) : nil
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
# Forget a served ref's listing (the next turn's start lists again).
|
|
397
|
+
def invalidate_served!(raw, host: nil)
|
|
398
|
+
ref = model_ref(raw)
|
|
399
|
+
@served.invalidate!(served_entry(ref, host: host)) if ServedRef.served?(ref)
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
# The models +entry+'s host lists now, as one bounded request: a probe
|
|
403
|
+
# adapter (short timeouts, no retry, as SelfReport's) on a chat host, the
|
|
404
|
+
# Client's #list_models_probe on a native one. Never the turn adapter's
|
|
405
|
+
# list (its retries and turn timeouts). A Stop cuts it (the thread's
|
|
406
|
+
# Client.probe_cancel: a turn's controller). Raises on failure.
|
|
407
|
+
# +timeout+ (seconds) lowers the chat probe's timeouts; +cancel_controller+
|
|
408
|
+
# (a judgment's) cuts it instead of the thread's.
|
|
409
|
+
# @return [Array<LLM::ModelInfo>]
|
|
410
|
+
def probe_models(entry, timeout: nil, cancel_controller: nil)
|
|
411
|
+
if entry.chat? && !@client_override
|
|
412
|
+
return LLM::Adapters.for(entry, open_timeout: [Client::CONTEXT_WINDOW_PROBE_OPEN_TIMEOUT, timeout].compact.min,
|
|
413
|
+
timeout: [Client::CONTEXT_WINDOW_PROBE_READ_TIMEOUT, timeout].compact.min, retries: false,
|
|
414
|
+
env: @env).list_models(cancel_controller: cancel_controller || Client.probe_cancel)
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
client = client_for(entry)
|
|
418
|
+
raw_infos(client.respond_to?(:list_models_probe) ? client.list_models_probe : client.list_models)
|
|
419
|
+
end
|
|
420
|
+
|
|
346
421
|
# What the alias +raw_model+ became, for an error that names only the
|
|
347
422
|
# host ("splash is an alias: gemma-x on host main"), or nil when it
|
|
348
423
|
# isn't an alias. An alias without a host of its own goes where a bare
|
|
@@ -356,10 +431,41 @@ module Samagotchi
|
|
|
356
431
|
"#{ref.alias_name} is an alias: #{target.bare_model} on host #{target.entry.name}"
|
|
357
432
|
end
|
|
358
433
|
|
|
359
|
-
# The model id sent for +
|
|
360
|
-
#
|
|
361
|
-
|
|
362
|
-
|
|
434
|
+
# The model id a host is sent for +raw+ (and what readers that name the
|
|
435
|
+
# model compare or key by: the wire, the guardrails' models: rules, the
|
|
436
|
+
# memory overlay key, the served-model mismatch, a side target): its
|
|
437
|
+
# alias applied, a known host prefix stripped ("box:gemma" → "gemma").
|
|
438
|
+
# The one source of that id, read from the parsed ref only: no resolve,
|
|
439
|
+
# no model index or saved list, never a request, so it answers when a
|
|
440
|
+
# resolve breaks. Equal to resolve(raw).bare_model.
|
|
441
|
+
# @param raw [String] a model name, alias or host:model ref
|
|
442
|
+
# @return [String]
|
|
443
|
+
#
|
|
444
|
+
# A served ref's id is +served_id+ (a turn's pin) when given, else the
|
|
445
|
+
# one ServedLookup#cached knows (on its host, or +host+'s for a bare
|
|
446
|
+
# one), else the word `served`.
|
|
447
|
+
# @return [String]
|
|
448
|
+
def sent_model_id(raw, served_id: nil, host: nil)
|
|
449
|
+
ref = model_ref(raw)
|
|
450
|
+
return ref.id unless ServedRef.served?(ref)
|
|
451
|
+
|
|
452
|
+
served_id || @served.cached(served_entry(ref, host: host)) || ref.id
|
|
453
|
+
end
|
|
454
|
+
alias bare_name sent_model_id
|
|
455
|
+
|
|
456
|
+
# The ref a turn's generations are counted under (SessionMetrics'
|
|
457
|
+
# per-model segments, the switch check) for +raw+: the stored ref
|
|
458
|
+
# (ModelRef#ref), or for a served ref the host and the id it resolves to
|
|
459
|
+
# ("main:q8"; the stored ref while nothing is known). Like
|
|
460
|
+
# #sent_model_id, never resolving. See EffectiveModel#turn_ref, its value
|
|
461
|
+
# on a resolved model.
|
|
462
|
+
# @return [String]
|
|
463
|
+
def turn_ref(raw, served_id: nil)
|
|
464
|
+
ref = model_ref(raw)
|
|
465
|
+
return ref.ref unless ServedRef.served?(ref)
|
|
466
|
+
|
|
467
|
+
id = served_id || @served.cached(served_entry(ref))
|
|
468
|
+
id ? "#{served_entry(ref).name}:#{id}" : ref.ref
|
|
363
469
|
end
|
|
364
470
|
|
|
365
471
|
# +raw+ parsed against these hosts and config.yml's aliases.
|
|
@@ -475,6 +581,30 @@ module Samagotchi
|
|
|
475
581
|
# that shows it names the host itself.
|
|
476
582
|
def list_error(error) = error.is_a?(LLM::ProviderError) ? error.detail : error.message
|
|
477
583
|
|
|
584
|
+
# A raw server list (Client#list_models: the server's own shape) as ModelInfo.
|
|
585
|
+
def raw_infos(list)
|
|
586
|
+
Array(list).map do |raw|
|
|
587
|
+
if raw.is_a?(Hash)
|
|
588
|
+
id = raw["id"] || raw[:id] || raw["model"] || raw["name"]
|
|
589
|
+
LLM::ModelInfo.new(id: id.to_s, context_window: nil, supports_tools: nil, raw: raw)
|
|
590
|
+
else
|
|
591
|
+
LLM::ModelInfo.new(id: raw.to_s, context_window: nil, supports_tools: nil, raw: {})
|
|
592
|
+
end
|
|
593
|
+
end
|
|
594
|
+
end
|
|
595
|
+
|
|
596
|
+
# [entry, id] for a served +ref+ (#host_for_model).
|
|
597
|
+
def served_target(ref, served_id)
|
|
598
|
+
entry = served_entry(ref)
|
|
599
|
+
[entry, served_id || @served.cached(entry) || ref.id]
|
|
600
|
+
end
|
|
601
|
+
|
|
602
|
+
# A chat host's turn adapter, whose cached list the served lookup reads
|
|
603
|
+
# and primes; nil on a native host or under a client override.
|
|
604
|
+
def listing_adapter(entry)
|
|
605
|
+
entry.chat? && !@client_override ? adapter_for(entry) : nil
|
|
606
|
+
end
|
|
607
|
+
|
|
478
608
|
# Save a host's just-listed models on disk, for the processes that spawn
|
|
479
609
|
# a worker without listing (`chi send --new --model`, delegate):
|
|
480
610
|
# ModelListStore is what they check an id against.
|
|
@@ -25,6 +25,15 @@ module Samagotchi
|
|
|
25
25
|
# Raised when summarization fails (server down, timeout, malformed body…).
|
|
26
26
|
class SummarizeError < StandardError; end
|
|
27
27
|
|
|
28
|
+
# Whether +error+ (a SummarizeError) is the host refusing the model as
|
|
29
|
+
# not served now (its cause's LLM::BadRequest#model_not_served?): the
|
|
30
|
+
# host swapped models, and a side ask on a served ref re-lists and asks
|
|
31
|
+
# once more.
|
|
32
|
+
def self.not_served?(error)
|
|
33
|
+
cause = error&.cause
|
|
34
|
+
cause.respond_to?(:model_not_served?) && cause.model_not_served? ? true : false
|
|
35
|
+
end
|
|
36
|
+
|
|
28
37
|
# What #summarize and #ask return: the text and the model that
|
|
29
38
|
# answered (the server's name for it; nil when the reply names none).
|
|
30
39
|
# +top_logprobs+: the first token's alternatives (LLM::TokenLogprob)
|
|
@@ -53,19 +62,21 @@ module Samagotchi
|
|
|
53
62
|
# summarize thread can't outlive the recap attempt by minutes.
|
|
54
63
|
# @param purpose [String] what the requests are for, in the HTTP log
|
|
55
64
|
# lines and error messages ("recap", "broadcast")
|
|
65
|
+
# @param label [String] names the requests in error messages ("block-fix
|
|
66
|
+
# request failed …": an Engine side request's kind); default: +purpose+
|
|
56
67
|
# @param api [Symbol] the wire to talk (LLM::Adapters; IdleTarget#api)
|
|
57
68
|
# @param auth [Symbol, nil] the header the key goes in (IdleTarget#auth)
|
|
58
69
|
# @param dialect [String, nil] a Messages host's dialect (IdleTarget#dialect)
|
|
59
70
|
# @param price [ModelPrice, nil] the model's price on its host
|
|
60
71
|
# (IdleTarget#price), on each SideUsage the on_usage callbacks get
|
|
61
72
|
def initialize(model:, base_url: nil, api_key_env: nil, timeout: DEFAULT_TIMEOUT_SECONDS, env: ENV, purpose: "recap",
|
|
62
|
-
api: :openai, auth: nil, dialect: nil, price: nil)
|
|
73
|
+
label: purpose, api: :openai, auth: nil, dialect: nil, price: nil)
|
|
63
74
|
@model = model
|
|
64
75
|
@price = price
|
|
65
76
|
# A recap is best-effort: one short attempt, no retries. The idle job
|
|
66
77
|
# tries again after the next activity, never on its own. Only the
|
|
67
78
|
# Messages adapter takes a dialect.
|
|
68
|
-
@chat = LLM::Adapters.build(api: api, base_url: base_url.to_s, host_name:
|
|
79
|
+
@chat = LLM::Adapters.build(api: api, base_url: base_url.to_s, host_name: label, api_key_env: api_key_env,
|
|
69
80
|
auth: auth, stream: false, retries: false, timeout: timeout, env: env, purpose: purpose,
|
|
70
81
|
**(dialect ? { dialect: dialect } : {}))
|
|
71
82
|
end
|
|
@@ -34,6 +34,8 @@ module Samagotchi
|
|
|
34
34
|
DEFAULT_INACTIVITY_SECONDS = 180.0
|
|
35
35
|
DEFAULT_TIMEOUT_SECONDS = 30.0
|
|
36
36
|
DEFAULT_MIN_USER_TURNS = 2
|
|
37
|
+
# The least time left (seconds) a not-served retry is worth.
|
|
38
|
+
RETRY_MIN_SECONDS = 1
|
|
37
39
|
# Above this many tool calls we ask only for a goal/achievement summary
|
|
38
40
|
# (never enumerate calls).
|
|
39
41
|
LARGE_TOOL_THRESHOLD = 10
|
|
@@ -314,6 +316,9 @@ module Samagotchi
|
|
|
314
316
|
# string (Engine#messages_json_for_recap)
|
|
315
317
|
# @param emit_recap [#call] (recap:, generation:, covered:, created_at:)
|
|
316
318
|
# announces a written recap (Engine#emit_recap)
|
|
319
|
+
# @param retarget [#call, nil] → IdleTarget, called once when the host
|
|
320
|
+
# refuses the asked model as not served (IdleClient.not_served?): a
|
|
321
|
+
# target naming another model is asked once more
|
|
317
322
|
def initialize(turn_state:, messages_json:, emit_recap:, model: nil, base_url: nil, api_key_env: nil, target: nil,
|
|
318
323
|
inactivity: DEFAULT_INACTIVITY_SECONDS,
|
|
319
324
|
min_user_turns: DEFAULT_MIN_USER_TURNS,
|
|
@@ -323,6 +328,7 @@ module Samagotchi
|
|
|
323
328
|
store: nil,
|
|
324
329
|
session_id: -> {},
|
|
325
330
|
on_usage: nil,
|
|
331
|
+
retarget: nil,
|
|
326
332
|
clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
|
|
327
333
|
raise ArgumentError, "IdleRecap requires a turn state" unless turn_state
|
|
328
334
|
|
|
@@ -335,6 +341,9 @@ module Samagotchi
|
|
|
335
341
|
# a dropped recap's too: it was billed (RecapSetup: the Engine's
|
|
336
342
|
# side line).
|
|
337
343
|
@on_usage = on_usage
|
|
344
|
+
# → a fresh target after the host refused the asked model as not
|
|
345
|
+
# served (RecapSetup: a served model listed again); nil: no retry.
|
|
346
|
+
@retarget = retarget
|
|
338
347
|
@target = target || lambda {
|
|
339
348
|
IdleTarget.new(base_url: base_url, api_key_env: api_key_env, model: model, label: model)
|
|
340
349
|
}
|
|
@@ -500,7 +509,7 @@ module Samagotchi
|
|
|
500
509
|
return :nothing_new if prompt.nil?
|
|
501
510
|
|
|
502
511
|
asked = target
|
|
503
|
-
@in_flight = { thread: spawn_summarize(client_for(asked), prompt), generation: gen, deadline: @clock.call + @timeout,
|
|
512
|
+
@in_flight = { thread: spawn_summarize(client_for(asked), prompt, asked), generation: gen, deadline: @clock.call + @timeout,
|
|
504
513
|
covered: parsed.size, covered_digest: self.class.digest(parsed.last), model: asked.label,
|
|
505
514
|
previous_title: previous&.dig(:title) }
|
|
506
515
|
:started
|
|
@@ -600,9 +609,9 @@ module Samagotchi
|
|
|
600
609
|
# any kind (a LoadError, a stack overflow too) ends it quietly, logged:
|
|
601
610
|
# no recap this time, and no dead thread's trace on the worker's
|
|
602
611
|
# stderr. A real exit or interrupt goes on as raised.
|
|
603
|
-
def spawn_summarize(client, prompt)
|
|
612
|
+
def spawn_summarize(client, prompt, asked)
|
|
604
613
|
Thread.new do
|
|
605
|
-
|
|
614
|
+
summarize(client, prompt, asked)
|
|
606
615
|
rescue SystemExit, SignalException
|
|
607
616
|
raise
|
|
608
617
|
rescue Exception => e # rubocop:disable Lint/RescueException -- a recap's failure, whatever it is, is no recap
|
|
@@ -612,6 +621,27 @@ module Samagotchi
|
|
|
612
621
|
end
|
|
613
622
|
end
|
|
614
623
|
|
|
624
|
+
# One summarize, and once more on a retarget naming another model when
|
|
625
|
+
# the host refused +asked+'s as not served (it swapped models). On the
|
|
626
|
+
# summarize thread: the retry's client is its own (@client stays the
|
|
627
|
+
# scheduler thread's).
|
|
628
|
+
# The retry gets what is left of the timeout (the attempt's deadline).
|
|
629
|
+
def summarize(client, prompt, asked)
|
|
630
|
+
started = @clock.call
|
|
631
|
+
client.summarize(prompt, session_id: @session_id.call, on_usage: @on_usage)
|
|
632
|
+
rescue IdleClient::SummarizeError => e
|
|
633
|
+
raise unless @retarget && IdleClient.not_served?(e)
|
|
634
|
+
|
|
635
|
+
retried = @retarget.call
|
|
636
|
+
raise if retried.nil? || retried.model == asked.model
|
|
637
|
+
|
|
638
|
+
left = @timeout - (@clock.call - started)
|
|
639
|
+
raise if left < RETRY_MIN_SECONDS
|
|
640
|
+
|
|
641
|
+
Log.info(:recap, "recap_not_served", was: asked.model, now: retried.model)
|
|
642
|
+
(@client_override || IdleClient.for(retried, timeout: left)).summarize(prompt, session_id: @session_id.call, on_usage: @on_usage)
|
|
643
|
+
end
|
|
644
|
+
|
|
615
645
|
def safe_value(worker)
|
|
616
646
|
worker.value
|
|
617
647
|
rescue StandardError
|
|
@@ -73,7 +73,7 @@ module Samagotchi
|
|
|
73
73
|
|
|
74
74
|
label = model.to_s
|
|
75
75
|
parsed = model && host_registry.model_ref(model)
|
|
76
|
-
return of_model(host_registry, model) unless
|
|
76
|
+
return of_model(host_registry, model) unless fixes_host?(model: model, host_ref: host_ref, base_url: base_url,
|
|
77
77
|
host_registry: host_registry)
|
|
78
78
|
|
|
79
79
|
host_ref ||= parsed.host_name if parsed && base_url.nil?
|
|
@@ -96,9 +96,10 @@ module Samagotchi
|
|
|
96
96
|
raise self::Unresolved.new(:model_host_mismatch, model: model, host_ref: host_ref, named: named)
|
|
97
97
|
end
|
|
98
98
|
|
|
99
|
-
|
|
99
|
+
# A bare `served` is host_ref's own (ServedLookup on that host).
|
|
100
|
+
model = host_registry.sent_model_id(model, host: entry.name) if parsed
|
|
100
101
|
elsif parsed && !parsed.host_name
|
|
101
|
-
model =
|
|
102
|
+
model = host_registry.sent_model_id(model)
|
|
102
103
|
end
|
|
103
104
|
raise self::Unresolved.new(:incomplete, model: model, host_ref: host_ref) if blank(base_url).nil? || blank(model).nil?
|
|
104
105
|
|
|
@@ -106,16 +107,35 @@ module Samagotchi
|
|
|
106
107
|
dialect: dialect, price: entry && price_of(entry, model))
|
|
107
108
|
end
|
|
108
109
|
|
|
109
|
-
# Whether the settings
|
|
110
|
-
# ref naming one)
|
|
111
|
-
#
|
|
112
|
-
#
|
|
110
|
+
# Whether resolving the settings once holds: they fix a host
|
|
111
|
+
# (+base_url+, +host_ref+, or a model ref naming one) and the model isn't
|
|
112
|
+
# a served one (ServedRef: its id is whatever the host serves now, so it
|
|
113
|
+
# is resolved at each request, as a model alone that names no host is:
|
|
114
|
+
# that goes where a bare --model goes, which a host's model list decides).
|
|
113
115
|
def self.host_named?(model:, host_ref:, base_url:, host_registry:)
|
|
116
|
+
return false if blank(base_url).nil? && served_model?(model, host_registry)
|
|
117
|
+
|
|
118
|
+
fixes_host?(model: model, host_ref: host_ref, base_url: base_url, host_registry: host_registry)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Whether +model+ is a served ref (main:served, a bare served;
|
|
122
|
+
# HostRegistry#served?, so this file requires nothing: the testing kit
|
|
123
|
+
# loads it).
|
|
124
|
+
def self.served_model?(model, host_registry)
|
|
125
|
+
model = blank(model)
|
|
126
|
+
!model.nil? && host_registry.served?(model)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# Whether the settings fix a host (+base_url+, +host_ref+, or a model
|
|
130
|
+
# ref naming one): #resolve then goes by that host, else where a bare
|
|
131
|
+
# --model goes.
|
|
132
|
+
def self.fixes_host?(model:, host_ref:, base_url:, host_registry:)
|
|
114
133
|
return true if blank(base_url) || blank(host_ref)
|
|
115
134
|
|
|
116
135
|
model = blank(model)
|
|
117
136
|
!model.nil? && !host_registry.model_ref(model).host_name.nil?
|
|
118
137
|
end
|
|
138
|
+
private_class_method :fixes_host?
|
|
119
139
|
|
|
120
140
|
def self.blank(value)
|
|
121
141
|
text = value.to_s.strip
|
|
@@ -49,14 +49,16 @@ module Samagotchi
|
|
|
49
49
|
# Every page of the host's model list (MAX_MODEL_PAGES at most): the
|
|
50
50
|
# block gives a page's URI for the last page's last_id (nil first).
|
|
51
51
|
# @return [Array<ModelInfo>]
|
|
52
|
-
|
|
52
|
+
# @param cancel_controller [CancellationController, nil] a Stop cuts the listing
|
|
53
|
+
def list_model_pages(headers: {}, cancel_controller: nil)
|
|
53
54
|
models = []
|
|
54
55
|
after = nil
|
|
55
56
|
MAX_MODEL_PAGES.times do
|
|
56
57
|
uri = yield(after)
|
|
57
58
|
request = Net::HTTP::Get.new(uri)
|
|
58
59
|
headers.each { |name, value| request[name] = value }
|
|
59
|
-
body = parse_json(@http.fetch(uri, request, log_fields: { purpose: "models" }).body,
|
|
60
|
+
body = parse_json(@http.fetch(uri, request, cancel_controller: cancel_controller, log_fields: { purpose: "models" }).body,
|
|
61
|
+
"model list")
|
|
60
62
|
models.concat(model_entries(body).map { |raw| model_info(raw) })
|
|
61
63
|
break unless body.is_a?(Hash) && body["has_more"] && body["last_id"]
|
|
62
64
|
|
|
@@ -244,8 +244,9 @@ module Samagotchi
|
|
|
244
244
|
# (max_input_tokens, max_tokens, capabilities) and the OpenAI shape a
|
|
245
245
|
# gateway or Splash serves here are both read (ModelListing).
|
|
246
246
|
# @return [Array<ModelInfo>]
|
|
247
|
-
|
|
248
|
-
|
|
247
|
+
# @param cancel_controller [CancellationController, nil] a Stop cuts it
|
|
248
|
+
def list_models(cancel_controller: nil)
|
|
249
|
+
list_model_pages(headers: { "anthropic-version" => API_VERSION }, cancel_controller: cancel_controller) do |after|
|
|
249
250
|
URI("#{@base_url}/v1/models?#{URI.encode_www_form({ limit: 1000, after_id: after }.compact)}")
|
|
250
251
|
end
|
|
251
252
|
end
|