samagotchi 0.199.0 → 0.200.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 +31 -1
- data/docs/bundles.md +3 -2
- data/docs/cli.md +18 -3
- data/docs/configuration.md +12 -7
- data/docs/internals/bridge.md +9 -4
- data/docs/internals/knowledge.md +8 -5
- data/docs/internals/participants.md +2 -1
- data/docs/internals/prompt-caching.md +9 -4
- data/docs/internals/web-frontend.md +27 -2
- data/docs/plugins.md +4 -2
- data/docs/sessions.md +8 -1
- data/docs/testing.md +1 -1
- data/lib/samagotchi/bridge/card_store.rb +19 -10
- data/lib/samagotchi/bridge.rb +1 -1
- data/lib/samagotchi/bundles/knowledge/github.rb +28 -10
- data/lib/samagotchi/bundles/knowledge/manifest.yml +4 -4
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/cache_confirm.rb +54 -18
- data/lib/samagotchi/engine/models.rb +10 -0
- data/lib/samagotchi/guardrail_wiring.rb +1 -1
- data/lib/samagotchi/inline_widgets.rb +14 -2
- data/lib/samagotchi/level_setting_command.rb +2 -3
- data/lib/samagotchi/llm_context_command.rb +2 -2
- data/lib/samagotchi/model_switch_mark.rb +49 -0
- data/lib/samagotchi/paths.rb +8 -1
- data/lib/samagotchi/plugin/secrets.rb +1 -1
- data/lib/samagotchi/plugin_session_state.rb +1 -1
- data/lib/samagotchi/session_commands.rb +11 -4
- data/lib/samagotchi/session_stats.rb +11 -6
- data/lib/samagotchi/toolkit_command.rb +1 -1
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/public/app.js +3 -1
- data/lib/samagotchi/web/public/bar_fold.js +1 -1
- data/lib/samagotchi/web/public/bubbles.js +7 -2
- data/lib/samagotchi/web/public/cache_cost_text.js +15 -0
- data/lib/samagotchi/web/public/commands.js +11 -2
- data/lib/samagotchi/web/public/confirm_block.js +59 -0
- data/lib/samagotchi/web/public/index.html +26 -1
- data/lib/samagotchi/web/public/info_bar.js +12 -1
- data/lib/samagotchi/web/public/info_bar_model.js +11 -1
- data/lib/samagotchi/web/public/model_mark.js +18 -0
- data/lib/samagotchi/web/public/model_picker.js +23 -6
- data/lib/samagotchi/web/public/model_switch.js +101 -0
- data/lib/samagotchi/web/public/more_sheet.js +11 -4
- data/lib/samagotchi/web/public/quiet_notes.js +1 -1
- data/lib/samagotchi/web/public/reasoning_history_chip.js +2 -1
- data/lib/samagotchi/web/public/settings_chip.js +2 -1
- data/lib/samagotchi/web/public/start_page.js +41 -8
- data/lib/samagotchi/web/public/stream_handlers.js +13 -2
- data/lib/samagotchi/web/public/thinking_chip.js +2 -1
- data/lib/samagotchi/web/public/tokens.css +2 -2
- metadata +6 -1
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
# GitHub repository (github.com only) as a source, through a shallow clone
|
|
5
5
|
# of the bundle's own under the catalog's clones/ folder, pulled at refresh
|
|
6
6
|
# (never in a search's own budget: Refresh runs it in the background).
|
|
7
|
-
# Auth is gh's own login (
|
|
8
|
-
# fetch's credential helper): no token here
|
|
7
|
+
# Auth is gh's own login (`gh auth git-credential` as the clone's and the
|
|
8
|
+
# fetch's credential helper): no token here, and no gh subcommand whose
|
|
9
|
+
# flags change between gh versions (gh repo clone --no-upstream is 2.90+).
|
|
9
10
|
#
|
|
10
11
|
# The location is model-supplied on a tool that runs unasked, so it is
|
|
11
12
|
# parsed strictly (GithubAdapter.parse): OWNER/REPO[@REF][:SUBPATH] or
|
|
@@ -39,9 +40,9 @@ class Plugin
|
|
|
39
40
|
SAFE = ["core.hooksPath=/dev/null", "core.fsmonitor=false", "protocol.allow=never", "protocol.https.allow=always",
|
|
40
41
|
"protocol.ssh.allow=always", "submodule.recurse=false"].freeze
|
|
41
42
|
GIT = ["git", *SAFE.flat_map { |setting| ["-c", setting] }].freeze
|
|
42
|
-
# The fetch's credentials: gh's login, no other helper.
|
|
43
|
+
# The clone's and the fetch's credentials: gh's login, no other helper.
|
|
43
44
|
CREDENTIAL = ["-c", "credential.helper=", "-c", "credential.helper=!gh auth git-credential"].freeze
|
|
44
|
-
CLONE_FLAGS = ["--depth", "1", "--single-branch", "--no-tags"
|
|
45
|
+
CLONE_FLAGS = ["--depth", "1", "--single-branch", "--no-tags"].freeze
|
|
45
46
|
# ls-tree modes that aren't a plain file: a symlink, a submodule.
|
|
46
47
|
LINK_MODES = %w[120000 160000].freeze
|
|
47
48
|
|
|
@@ -143,16 +144,18 @@ class Plugin
|
|
|
143
144
|
clone!
|
|
144
145
|
end
|
|
145
146
|
|
|
146
|
-
#
|
|
147
|
-
#
|
|
148
|
-
#
|
|
147
|
+
# Plain git with gh's credential helper (the pull's auth), the SAFE
|
|
148
|
+
# -c's on the clone's own process (they reach its checkout; every later
|
|
149
|
+
# spawn passes them again), origin the https URL the pull fetches. The
|
|
150
|
+
# validated OWNER/REPO and the bundle's own folder after "--".
|
|
149
151
|
def clone!
|
|
150
152
|
FileUtils.mkdir_p(File.dirname(@dir), mode: 0o700)
|
|
151
153
|
partial = "#{@dir}.partial"
|
|
152
154
|
FileUtils.rm_rf(partial)
|
|
155
|
+
name = "#{@location.owner}/#{@location.repo}"
|
|
153
156
|
flags = CLONE_FLAGS + (@location.ref ? ["--branch", @location.ref] : [])
|
|
154
|
-
ok, out = run([
|
|
155
|
-
raise Error, "
|
|
157
|
+
ok, out = run([*GIT, *CREDENTIAL, "clone", *flags, "--", "https://github.com/#{name}.git", partial])
|
|
158
|
+
raise Error, "git clone #{name}: #{fatal_line(out)}" unless ok
|
|
156
159
|
|
|
157
160
|
File.rename(partial, @dir)
|
|
158
161
|
ensure
|
|
@@ -233,7 +236,7 @@ class Plugin
|
|
|
233
236
|
# gone in between
|
|
234
237
|
end
|
|
235
238
|
wait.join
|
|
236
|
-
raise Error, "#{argv
|
|
239
|
+
raise Error, "#{command_name(argv)} took over #{@timeout} s"
|
|
237
240
|
end
|
|
238
241
|
[wait.value.success?, reader.value.to_s]
|
|
239
242
|
end
|
|
@@ -241,6 +244,21 @@ class Plugin
|
|
|
241
244
|
raise Error, "#{argv.first}: #{e.message}"
|
|
242
245
|
end
|
|
243
246
|
|
|
247
|
+
# "git clone", "gh api": the command and its subcommand, skipping the
|
|
248
|
+
# global -c/-C flags and their values (a gh call has only its subcommand).
|
|
249
|
+
def command_name(argv)
|
|
250
|
+
command, *tokens = argv
|
|
251
|
+
while (token = tokens.first) && %w[-c -C].include?(token)
|
|
252
|
+
tokens.shift(2)
|
|
253
|
+
end
|
|
254
|
+
subcommand = tokens.first
|
|
255
|
+
subcommand ? "#{command} #{subcommand}" : command
|
|
256
|
+
end
|
|
257
|
+
|
|
244
258
|
def last_line(out) = out.to_s.lines.map(&:strip).reject(&:empty?).last.to_s[0, 300]
|
|
259
|
+
|
|
260
|
+
# git clone's first "fatal:" line (its last is often the generic "and
|
|
261
|
+
# the repository exists."), else its last line.
|
|
262
|
+
def fatal_line(out) = out.to_s.lines.map(&:strip).find { |line| line.start_with?("fatal:") }&.slice(0, 300) || last_line(out)
|
|
245
263
|
end
|
|
246
264
|
end
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: knowledge
|
|
3
|
-
version: 0.3.
|
|
3
|
+
version: 0.3.1
|
|
4
4
|
scope: system
|
|
5
5
|
description: "knowledge_search: search a project's docs, memories and the folders, GitHub repos, past sessions and attached context added to its catalog (or a named catalog) in one local SQLite index, each answer saying how old each source is; knowledge_add/list/remove for a kit that adds them (the coordinator kit); /knowledge lists the catalog. And a Knowledge page in chi web (#/p/knowledge, chi page run knowledge): how searches went on this machine (rankings, what the agent did next, speed by week, the last searches) and each catalog's health; read-only, no model"
|
|
6
6
|
trust_level: reviewed
|
|
@@ -14,7 +14,7 @@ plugin:
|
|
|
14
14
|
chunker.rb: sha256:169c638ed109ce28e2530868e36b60df7ccffdf64ad64ef027403ba998d7c79e
|
|
15
15
|
adapters.rb: sha256:11fee1ca0473d9b65a1b66ff50e49b6daf8fdc63d1651e6edc31c02d794ccc1a
|
|
16
16
|
chi_adapters.rb: sha256:897ef55c077df1fdab371753f6c0a9fabb5e477a3646ffc96a756b2364266ebb
|
|
17
|
-
github.rb: sha256:
|
|
17
|
+
github.rb: sha256:bd9906738f32b1e33d66f94ccd04ac97ed1eb6fb30ef0c669c24a4db2a5d1ffb
|
|
18
18
|
catalog.rb: sha256:ffa07afcfb8c3358ac3500bc80f5a028ece79e4adb7e0be5153ef50f95359251
|
|
19
19
|
refresh.rb: sha256:085545276488772f830757f9f1fe2da9807fb0fdc3a6f1c8851737412ffd3938
|
|
20
20
|
query_log.rb: sha256:57841260a62451862ba75e3a5d8f5326cafaf89189ca1032ef7493f964d24f37
|
|
@@ -22,9 +22,9 @@ plugin:
|
|
|
22
22
|
embedder.rb: sha256:22af7c9401aa9da0e122c0815066e3ebf8c65732b14eb17f14ec97667602b50b
|
|
23
23
|
# chi.tool(declare:) and ctx.project are new: release:bump raises requires_chi.
|
|
24
24
|
requires_chi: ">= 0.199.0"
|
|
25
|
-
# runtime.data_dir (the page's data folder)
|
|
25
|
+
# 0.199.0 brings what the bundle needs: runtime.data_dir (the page's data folder),
|
|
26
26
|
# ctx.speaker (a participant's turns left out of the sessions source) and
|
|
27
|
-
# chi.tool(participant:) (the catalog tools kept for the owner)
|
|
27
|
+
# chi.tool(participant:) (the catalog tools kept for the owner).
|
|
28
28
|
needs: [sqlite3, gh, git]
|
|
29
29
|
scripts:
|
|
30
30
|
knowledge.rb: sha256:5851abff555fa01948c771d0bcac4e438364b11d29b2b7cfeb6483d1dde29df0
|
|
@@ -16,11 +16,30 @@ module Samagotchi
|
|
|
16
16
|
# thread may ask (#asking).
|
|
17
17
|
ASKING = :samagotchi_cache_confirm_asking
|
|
18
18
|
|
|
19
|
-
#
|
|
20
|
-
|
|
19
|
+
# What a confirm asks, per kind of change: its title (a format with the
|
|
20
|
+
# change's subject, %<subject>s) and the button that applies it. A
|
|
21
|
+
# plugin's (ctx.cache_confirm) is generic.
|
|
22
|
+
KINDS = {
|
|
23
|
+
model: { title: "Switch to %<subject>s?", button: "Switch model" },
|
|
24
|
+
thinking: { title: "Switch thinking to %<subject>s?", button: "Switch thinking" },
|
|
25
|
+
reasoning_history: { title: "Switch reasoning history to %<subject>s?", button: "Switch reasoning history" },
|
|
26
|
+
llm_context: { title: "Change the LLM context?", button: "Change LLM context" },
|
|
27
|
+
toolkit: { title: "Change the toolkit?", button: "Change toolkit" },
|
|
28
|
+
plugin: { title: "Apply change?", button: "Apply" }
|
|
29
|
+
}.freeze
|
|
30
|
+
# The body's last sentence: nothing is applied before the button.
|
|
31
|
+
NOTHING_YET = "Nothing has changed yet."
|
|
32
|
+
|
|
33
|
+
# The line that applies a change, and what the reply asks: +text+ the
|
|
34
|
+
# whole question as a terminal prints it (it ends with the line to send),
|
|
35
|
+
# and for a page's warning block +title+, +body+ (when the cost lands and
|
|
36
|
+
# how much, then NOTHING_YET) and +button+ (what applies it).
|
|
37
|
+
Confirm = Data.define(:line, :text, :title, :body, :button) do
|
|
38
|
+
def initialize(line:, text:, title: nil, body: nil, button: nil) = super
|
|
39
|
+
|
|
21
40
|
# The plain form the worker's command_ran carries.
|
|
22
41
|
# @return [Hash] symbol keys
|
|
23
|
-
def to_h = { line: line, text: text }
|
|
42
|
+
def to_h = { line: line, text: text, title: title, body: body, button: button }.compact
|
|
24
43
|
end
|
|
25
44
|
|
|
26
45
|
module_function
|
|
@@ -66,27 +85,44 @@ module Samagotchi
|
|
|
66
85
|
# @param cost [Symbol, nil] the change's cache cost
|
|
67
86
|
# @param changed [Boolean] whether the effective setting changes
|
|
68
87
|
# @param engine [Engine] supplies history, used tokens and model host
|
|
88
|
+
# @param kind [Symbol] a KINDS key: the title and the button
|
|
89
|
+
# @param subject [String, nil] what the title names (the model, the level)
|
|
69
90
|
# @return [Confirm, nil]
|
|
70
|
-
def ask(line:, cost:, changed:, engine:)
|
|
91
|
+
def ask(line:, cost:, changed:, engine:, kind: :plugin, subject: nil)
|
|
71
92
|
return nil unless needed?(cost: cost, history: engine.history?, changed: changed)
|
|
72
93
|
|
|
73
94
|
apply_line = "#{line.to_s.strip} --yes"
|
|
95
|
+
words = KINDS.fetch(kind, KINDS[:plugin])
|
|
96
|
+
notes = cost_notes(cost, engine, reader: kind == :model && subject ? subject.to_s : "the model")
|
|
97
|
+
Confirm.new(line: apply_line, text: [*notes, "#{NOTHING_YET.delete_suffix(".")}: send `#{apply_line}` to apply."].join("\n"),
|
|
98
|
+
title: format(words[:title], subject: subject.to_s), body: [*notes, NOTHING_YET].join(" "),
|
|
99
|
+
button: words[:button])
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# When the cost lands (the next turn: the change itself costs nothing)
|
|
103
|
+
# and about how much; +reader+ is who reads the conversation again.
|
|
104
|
+
def cost_notes(cost, engine, reader:)
|
|
105
|
+
size = size_text(engine.cache_restart_tokens)
|
|
74
106
|
notes = []
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
107
|
+
case cost
|
|
108
|
+
when :full, :provider
|
|
109
|
+
notes << "The next turn starts without a prompt cache: #{reader} reads the whole conversation again" \
|
|
110
|
+
"#{" (about #{size} tokens)" if size}."
|
|
111
|
+
if engine.cache_restart_anthropic?
|
|
112
|
+
notes << "On an `api: anthropic` host, preserved thinking in replies is dropped from then on."
|
|
113
|
+
end
|
|
114
|
+
when :history
|
|
115
|
+
notes << "The next turn reads the conversation again from the first changed reply" \
|
|
116
|
+
"#{" (up to about #{size} tokens)" if size}."
|
|
84
117
|
end
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
118
|
+
notes
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# "104k", "8.2k", "950"; nil without a count.
|
|
122
|
+
def size_text(tokens)
|
|
123
|
+
return nil unless tokens
|
|
124
|
+
|
|
125
|
+
tokens >= 1000 ? "#{format("%.1f", tokens / 1000.0).delete_suffix(".0")}k" : tokens.to_s
|
|
90
126
|
end
|
|
91
127
|
end
|
|
92
128
|
end
|
|
@@ -10,6 +10,7 @@ require_relative "../llm/prompt_cache"
|
|
|
10
10
|
require_relative "../log"
|
|
11
11
|
require_relative "../model_notes"
|
|
12
12
|
require_relative "../model_profile"
|
|
13
|
+
require_relative "../model_switch_mark"
|
|
13
14
|
require_relative "../prompt_note"
|
|
14
15
|
require_relative "../served_lookup"
|
|
15
16
|
require_relative "../system_prompt"
|
|
@@ -370,6 +371,15 @@ module Samagotchi
|
|
|
370
371
|
end
|
|
371
372
|
private :generate_model
|
|
372
373
|
|
|
374
|
+
# The line a /model switch leaves where it happened (ModelSwitchMark): a
|
|
375
|
+
# between-turns notice, hook "model", in the CardStore and every UI but a
|
|
376
|
+
# participant's; never a message. SessionCommands#switch_model decides.
|
|
377
|
+
# @param from [String] the model's name before (as the bar shows it)
|
|
378
|
+
# @param to [String] its name now
|
|
379
|
+
def announce_model_switch(from:, to:)
|
|
380
|
+
hook_notify(ModelSwitchMark.text(from: from, to: to), :info, ModelSwitchMark::HOOK)
|
|
381
|
+
end
|
|
382
|
+
|
|
373
383
|
# After /model: how full the context is on the new model's window,
|
|
374
384
|
# before its first turn: the last turn's count (the old model's
|
|
375
385
|
# tokenizer's, replaced at the next generation) against the new model's
|
|
@@ -284,7 +284,7 @@ module Samagotchi
|
|
|
284
284
|
runs_dir: File.join(File.dirname(@approvals.path, 2), "system_one"),
|
|
285
285
|
home_path: ChiHome.path,
|
|
286
286
|
shim_path: ChiShim.path,
|
|
287
|
-
plugins_dir:
|
|
287
|
+
plugins_dir: Paths.plugins_dir,
|
|
288
288
|
schedules_path: Guardrails::ChiDirs.schedules_file
|
|
289
289
|
)
|
|
290
290
|
end
|
|
@@ -122,20 +122,32 @@ module Samagotchi
|
|
|
122
122
|
end
|
|
123
123
|
|
|
124
124
|
# [Tree, lenient?]: strict first; only a block that isn't YAML gets the
|
|
125
|
-
# lenient pass. A refused feature (an alias) is final either way.
|
|
125
|
+
# lenient pass. A refused feature (an alias) is final either way. libyaml's
|
|
126
|
+
# own nesting limit ("exceeded maximum nesting depth", a newer runner image's
|
|
127
|
+
# libyaml) is the same refusal as ours, whichever pass it surfaces in.
|
|
126
128
|
def load(source)
|
|
127
129
|
[Tree.new(source, Psych.parse_stream(source)), false]
|
|
128
|
-
rescue Psych::Exception
|
|
130
|
+
rescue Psych::Exception => e
|
|
131
|
+
raise Refused, "the block nests too deep" if nesting_limit?(e)
|
|
132
|
+
|
|
129
133
|
begin
|
|
130
134
|
quoted = Lenient.quote(source)
|
|
131
135
|
[Tree.new(quoted, Psych.parse_stream(quoted)), true]
|
|
132
136
|
rescue Psych::SyntaxError => e
|
|
137
|
+
raise Refused, "the block nests too deep" if nesting_limit?(e)
|
|
138
|
+
|
|
133
139
|
raise Refused, "not YAML: #{e.problem} at line #{e.line}"
|
|
134
140
|
rescue Psych::Exception => e
|
|
135
141
|
raise Refused, "not YAML: #{e.message.lines.first.to_s.strip}"
|
|
136
142
|
end
|
|
137
143
|
end
|
|
138
144
|
|
|
145
|
+
# True for libyaml's own nesting-depth refusal (a SyntaxError, so only it
|
|
146
|
+
# carries a problem); our own MAX_DEPTH covers the rest.
|
|
147
|
+
def nesting_limit?(exception)
|
|
148
|
+
exception.is_a?(Psych::SyntaxError) && exception.problem.to_s.include?("maximum nesting depth")
|
|
149
|
+
end
|
|
150
|
+
|
|
139
151
|
# Psych's node tree to Hashes, Arrays and Strings (nil for a plain empty
|
|
140
152
|
# or null value), the scalars' own text. folds: each one-pair mapping of
|
|
141
153
|
# scalars (`fix now: drop it`), by identity, to the text it was written as,
|
|
@@ -83,11 +83,10 @@ module Samagotchi
|
|
|
83
83
|
|
|
84
84
|
cost = spec.cache_cost.call(@engine, before.level, would.level)
|
|
85
85
|
step = CacheConfirm.ask(line: "#{spec.name} #{word}", cost: cost, changed: would.level != before.level,
|
|
86
|
-
engine: @engine)
|
|
86
|
+
engine: @engine, kind: spec.label.tr(" ", "_").to_sym, subject: would.level)
|
|
87
87
|
return nil unless step
|
|
88
88
|
|
|
89
|
-
[[listing(before),
|
|
90
|
-
false, step]
|
|
89
|
+
[[listing(before), step.title, spec.cost_notes[cost], step.text].compact.join("\n"), false, step]
|
|
91
90
|
end
|
|
92
91
|
|
|
93
92
|
def notes(before, after)
|
|
@@ -106,11 +106,11 @@ module Samagotchi
|
|
|
106
106
|
was = before&.resolved
|
|
107
107
|
now = would&.resolved
|
|
108
108
|
cost = cache_cost(session, was, now)
|
|
109
|
-
step = CacheConfirm.ask(line: "#{NAME} #{args}", cost: cost, changed: !cost.nil?, engine: @engine)
|
|
109
|
+
step = CacheConfirm.ask(line: "#{NAME} #{args}", cost: cost, changed: !cost.nil?, engine: @engine, kind: :llm_context)
|
|
110
110
|
return nil unless step
|
|
111
111
|
|
|
112
112
|
costs = notes(session, was, now).drop(1).reject { |note| note.start_with?("stale:") }
|
|
113
|
-
[[listing(before),
|
|
113
|
+
[[listing(before), step.title, *costs, step.text].join("\n"), false, step]
|
|
114
114
|
end
|
|
115
115
|
|
|
116
116
|
# "strategy stale stale forget apply turn_end budget 64k" as fields
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "message_time"
|
|
4
|
+
|
|
5
|
+
module Samagotchi
|
|
6
|
+
# The line a /model switch leaves in the conversation where it happened:
|
|
7
|
+
# "model: gemma-4 → qwen-3.5 · Dec 3, 09:31". A between-turns hook notice
|
|
8
|
+
# (hook "model"), so it lives in the Bridge's CardStore (cards.json, its own
|
|
9
|
+
# cap class there), never in session.messages: the model never reads it,
|
|
10
|
+
# and a participant never gets it (Web::ParticipantView drops hook notices).
|
|
11
|
+
# The web draws it as a quiet divider, a terminal prints "model> …".
|
|
12
|
+
#
|
|
13
|
+
# Emitted by SessionCommands#switch_model only (a typed or picked /model,
|
|
14
|
+
# its --yes, chi --model X --attach, a schedule's /model line): a resume and
|
|
15
|
+
# the one-shot also switch, and are not switches the user made. Only when
|
|
16
|
+
# the resolved ref changed and the session has a model reply, the same
|
|
17
|
+
# condition that makes /model ask first (CacheConfirm).
|
|
18
|
+
module ModelSwitchMark
|
|
19
|
+
# The notice's hook (the in-turn "model:" rows of a served follow share
|
|
20
|
+
# it; a mark is never in a turn).
|
|
21
|
+
HOOK = "model"
|
|
22
|
+
# The worker draws the mark (Bridge::FEATURES): the page leaves a picked
|
|
23
|
+
# switch's command lines out only then.
|
|
24
|
+
FEATURE = "model_mark"
|
|
25
|
+
|
|
26
|
+
module_function
|
|
27
|
+
|
|
28
|
+
# @param from [String] the model the bar showed before (its name: an
|
|
29
|
+
# alias as typed, else host:id)
|
|
30
|
+
# @param to [String] the one it shows now
|
|
31
|
+
# @param now [Time] the worker's clock
|
|
32
|
+
# @return [String] "gemma-4 → qwen-3.5 · Dec 3, 09:31"
|
|
33
|
+
def text(from:, to:, now: MessageTime.now)
|
|
34
|
+
"#{from} → #{to} · #{now.localtime.strftime("%b %-d, %H:%M")}"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Whether a CardStore entry (symbol or string keys and type, as the
|
|
38
|
+
# store or its saved file holds it) is a mark: a hook notice of HOOK
|
|
39
|
+
# outside any turn.
|
|
40
|
+
def mark?(entry)
|
|
41
|
+
return false unless entry.is_a?(Hash)
|
|
42
|
+
|
|
43
|
+
type = entry[:type] || entry["type"]
|
|
44
|
+
hook = entry.key?(:hook) ? entry[:hook] : entry["hook"]
|
|
45
|
+
in_turn = entry.key?(:in_turn) ? entry[:in_turn] : entry["in_turn"]
|
|
46
|
+
type.to_s == "hook_notice" && hook.to_s == HOOK && in_turn == false
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
data/lib/samagotchi/paths.rb
CHANGED
|
@@ -25,13 +25,20 @@ module Samagotchi
|
|
|
25
25
|
File.join(state_home(env: env), APP_DIR)
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
+
# The bundles' data folder: <state dir>/plugins. Only the path:
|
|
29
|
+
# nothing is made.
|
|
30
|
+
# @return [String]
|
|
31
|
+
def plugins_dir(env: ENV)
|
|
32
|
+
File.join(state_dir(env: env), "plugins")
|
|
33
|
+
end
|
|
34
|
+
|
|
28
35
|
# A bundle's plugin data folder: <state dir>/plugins/<bundle>. The one
|
|
29
36
|
# rule for ctx.data_dir (which creates it) and a bundle page's
|
|
30
37
|
# runtime.data_dir (which doesn't). Only the path: nothing is made.
|
|
31
38
|
# @param bundle [String] the bundle's name
|
|
32
39
|
# @return [String]
|
|
33
40
|
def plugin_data_dir(bundle, env: ENV)
|
|
34
|
-
File.join(
|
|
41
|
+
File.join(plugins_dir(env: env), bundle.to_s)
|
|
35
42
|
end
|
|
36
43
|
|
|
37
44
|
# @return [String] $XDG_CONFIG_HOME (absolute), else ~/.config
|
|
@@ -154,7 +154,7 @@ module Samagotchi
|
|
|
154
154
|
|
|
155
155
|
# @param bundle [String]
|
|
156
156
|
def self.for_bundle(bundle, env: ENV)
|
|
157
|
-
new(File.join(Paths.
|
|
157
|
+
new(File.join(Paths.plugin_data_dir(bundle, env: env), "secrets"))
|
|
158
158
|
end
|
|
159
159
|
|
|
160
160
|
def backend = :file
|
|
@@ -49,7 +49,7 @@ module Samagotchi
|
|
|
49
49
|
# Only a plain id: anything else could name a path outside the dir.
|
|
50
50
|
return [] unless Session.valid_id?(id)
|
|
51
51
|
|
|
52
|
-
Dir.glob(File.join(Paths.
|
|
52
|
+
Dir.glob(File.join(Paths.plugins_dir(env: env), "*", "sessions")).flat_map do |dir|
|
|
53
53
|
[File.join(dir, "#{id}.json"), File.join(dir, id)].select { |path| File.exist?(path) }
|
|
54
54
|
.each { |path| FileUtils.rm_rf(path) }
|
|
55
55
|
end
|
|
@@ -721,17 +721,24 @@ module Samagotchi
|
|
|
721
721
|
# @param line [String] the /model line as typed (its flags too), --yes taken out
|
|
722
722
|
def model_ask(name, line)
|
|
723
723
|
ref = @engine.model_switch_ref(name)
|
|
724
|
-
step = CacheConfirm.ask(line: line, cost: :full, changed: ref != model_name, engine: @engine
|
|
724
|
+
step = CacheConfirm.ask(line: line, cost: :full, changed: ref != model_name, engine: @engine,
|
|
725
|
+
kind: :model, subject: name)
|
|
725
726
|
return nil unless step
|
|
726
727
|
|
|
727
|
-
["runtime model: #{model_name}\
|
|
728
|
-
"this conversation.\n#{step.text}", false, step]
|
|
728
|
+
["runtime model: #{model_name}\n#{step.title}\n#{step.text}", false, step]
|
|
729
729
|
end
|
|
730
730
|
|
|
731
731
|
# Engine owns the switch (alias resolution, profile, kernel, client and
|
|
732
|
-
# the optional default persist); the session keeps the new model.
|
|
732
|
+
# the optional default persist); the session keeps the new model. A
|
|
733
|
+
# switch to another ref in a session with a model reply leaves its mark
|
|
734
|
+
# (ModelSwitchMark: the condition model_ask asks on).
|
|
733
735
|
def switch_model(name, persist_default: false)
|
|
736
|
+
was_ref = model_name
|
|
737
|
+
was_name = @engine.effective_model_name
|
|
734
738
|
@engine.switch_model!(name, persist_default: persist_default)
|
|
739
|
+
if model_name != was_ref && @engine.history?
|
|
740
|
+
@engine.announce_model_switch(from: was_name, to: @engine.effective_model_name)
|
|
741
|
+
end
|
|
735
742
|
@default_model = @engine.default_model_name if persist_default
|
|
736
743
|
persist_session_model
|
|
737
744
|
# The ctx % on the new model's window, before its first turn; saved
|
|
@@ -44,6 +44,9 @@ module Samagotchi
|
|
|
44
44
|
DAYS = 14
|
|
45
45
|
MAX_SESSIONS = 500
|
|
46
46
|
DAY_SECONDS = 86_400
|
|
47
|
+
# chi's own bare-word notice hooks (ModelSwitchMark::HOOK, ThinkingNotices):
|
|
48
|
+
# not a plugin's, never counted as one.
|
|
49
|
+
CHI_OWN_HOOKS = %w[model thinking].freeze
|
|
47
50
|
|
|
48
51
|
# A failed turn's kind, from its failure line (summary, else message,
|
|
49
52
|
# else error class) by the first rule that matches; "none" when it has
|
|
@@ -349,12 +352,10 @@ module Samagotchi
|
|
|
349
352
|
|
|
350
353
|
entries = data[:entries].grep(Hash)
|
|
351
354
|
in_window = entries.select { |entry| entry_in_window?(entry, turn_ids, since) }
|
|
352
|
-
|
|
353
|
-
llm_types = Bridge::CardStore::LLM_CONTEXT_TYPES.map(&:to_s)
|
|
354
|
-
types = entries.map { |entry| entry[:type].to_s }
|
|
355
|
+
caps = entries.map { |entry| Bridge::CardStore.cap_class(entry) }
|
|
355
356
|
[{ approvals_by_rule: approvals(in_window), hook_notices_by_bundle: hook_notices(in_window),
|
|
356
|
-
cards_at_cap:
|
|
357
|
-
notices_at_cap:
|
|
357
|
+
cards_at_cap: caps.count(:card) >= Bridge::CardStore::CAPACITY,
|
|
358
|
+
notices_at_cap: caps.count(:notice) >= Bridge::CardStore::CAPACITY }, 0]
|
|
358
359
|
rescue JSON::ParserError, SystemCallError
|
|
359
360
|
[empty_cards, 1]
|
|
360
361
|
end
|
|
@@ -394,13 +395,17 @@ module Samagotchi
|
|
|
394
395
|
end
|
|
395
396
|
|
|
396
397
|
# Hook notices by bundle (HookLabel.source). A label that is neither a
|
|
397
|
-
# bundle hook's nor one word (a hook's failure line) is skipped
|
|
398
|
+
# bundle hook's nor one word (a hook's failure line) is skipped, and so
|
|
399
|
+
# are chi's own (CHI_OWN_HOOKS: a model switch's mark, a served follow,
|
|
400
|
+
# a thinking note), which no plugin wrote.
|
|
398
401
|
def hook_notices(entries)
|
|
399
402
|
counts = Hash.new(0)
|
|
400
403
|
entries.each do |entry|
|
|
401
404
|
next unless entry[:type] == "hook_notice"
|
|
402
405
|
|
|
403
406
|
label = entry[:hook].to_s
|
|
407
|
+
next if CHI_OWN_HOOKS.include?(label)
|
|
408
|
+
|
|
404
409
|
source = HookLabel.source(label)
|
|
405
410
|
next if source == label && (label.empty? || label.match?(/\s/))
|
|
406
411
|
|
|
@@ -52,7 +52,7 @@ module Samagotchi
|
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
cost = Toolkit.cache_cost(before.prompt_key, after.prompt_key)
|
|
55
|
-
step = CacheConfirm.ask(line: "#{NAME} #{words}", cost: cost, changed: true, engine: @engine) if confirm && !yes
|
|
55
|
+
step = CacheConfirm.ask(line: "#{NAME} #{words}", cost: cost, changed: true, engine: @engine, kind: :toolkit) if confirm && !yes
|
|
56
56
|
if step
|
|
57
57
|
return [[change_line(before, after), reread_line(before, after), step.text].join("\n"), false, step]
|
|
58
58
|
end
|
data/lib/samagotchi/version.rb
CHANGED
|
@@ -38,6 +38,7 @@ import { renderInfoBar } from "./info_bar.js";
|
|
|
38
38
|
import { createMoreSheet } from "./more_sheet.js";
|
|
39
39
|
import { createBarFold } from "./bar_fold.js";
|
|
40
40
|
import { barItemKeys, sessionBar } from "./info_bar_model.js";
|
|
41
|
+
import { wireModelSwitch } from "./model_switch.js";
|
|
41
42
|
import { createShownTexts } from "./shown_texts.js";
|
|
42
43
|
import { emptySessionState } from "./session_state.js";
|
|
43
44
|
import { sessionFieldsFrom } from "./session_payload.js";
|
|
@@ -939,6 +940,7 @@ infoTextEl.addEventListener("click", (e) => {
|
|
|
939
940
|
openAllView();
|
|
940
941
|
}
|
|
941
942
|
});
|
|
943
|
+
wireModelSwitch({ el: infoTextEl, open: (from) => startPage.openForSession?.(from) }); // the model's name opens its list (model_switch.js)
|
|
942
944
|
// The info bar's id: a click copies the full id, which the bar shows only short.
|
|
943
945
|
infoTextEl.addEventListener("click", async (e) => {
|
|
944
946
|
if (!e.target.closest(".id") || !store.selected) return;
|
|
@@ -964,7 +966,7 @@ function updateInfoBar() {
|
|
|
964
966
|
const { buttons, bar } = sessionBar({
|
|
965
967
|
id: store.selected, state: store.current, sessions: store.sessions, versions: versionNotices.versions(), cardTitle: (s) => lists.cardTitle(s),
|
|
966
968
|
continuing: sessionActions.continuing, restarting: versionNotices.restarting, streaming: sessionSync.hasStream(), running: liveTurn.running, waiting: sessionWaits(),
|
|
967
|
-
workingSet: workingSet ? workingSet.chip() : undefined, participants: participantsControl?.chip() ?? null,
|
|
969
|
+
workingSet: workingSet ? workingSet.chip() : undefined, participants: participantsControl?.chip() ?? null, modelSwitch: !!settingsControl && !!startPage.modelsLoaded?.() && typeof startPage.openForSession === "function",
|
|
968
970
|
});
|
|
969
971
|
if (continueBtn) {
|
|
970
972
|
continueBtn.style.display = buttons.continue.shown ? "" : "none";
|
|
@@ -98,7 +98,7 @@ export function foldSignature(bar, buttons) {
|
|
|
98
98
|
if (!bar) return "";
|
|
99
99
|
const gear = bar.settings ? settingsChipModel(bar.settings) : null;
|
|
100
100
|
return JSON.stringify([
|
|
101
|
-
bar.id, bar.status, bar.statusText, bar.model?.text, bar.model?.mismatch, gear?.text, gear?.own,
|
|
101
|
+
bar.id, bar.status, bar.statusText, bar.model?.text, bar.model?.mismatch, bar.model?.switchable, gear?.text, gear?.own,
|
|
102
102
|
bar.toolkit?.summary?.name, bar.toolkit?.summary?.label, bar.toolkit?.defaultKit, bar.notes?.text,
|
|
103
103
|
bar.workingSet ? [bar.workingSet.chip?.n] : (bar.memories || []).join(","),
|
|
104
104
|
bar.participants?.text, bar.participants?.count,
|
|
@@ -14,6 +14,7 @@ import { linkedLine } from "./notice_links.js";
|
|
|
14
14
|
import { hookNoticeLabel, isReportClient, promptOps } from "./turn_events.js";
|
|
15
15
|
import { drawCommandBubble } from "./commands.js";
|
|
16
16
|
import { TIME_GAP_CLASS, timeGapEl } from "./time_gaps.js";
|
|
17
|
+
import { isModelMark, MODEL_MARK_CLASS } from "./model_mark.js";
|
|
17
18
|
|
|
18
19
|
// What a session's load places at a turn's prompt from the snapshot
|
|
19
20
|
// (cards_place.js placeSnapshotCards): its cards, answered questions,
|
|
@@ -78,8 +79,12 @@ export function guardrailWarningTemplate(text, label = "guardrails") {
|
|
|
78
79
|
// A hook's line to the user (event[:notify]), in the warning colour for
|
|
79
80
|
// level warn; +cls+ adds a kind's class (llm_context_edited's). +bubble+:
|
|
80
81
|
// one of the history's own, else a row of a turn's step. Its http(s) URLs
|
|
81
|
-
// are links (notice_links.js).
|
|
82
|
-
|
|
82
|
+
// are links (notice_links.js). A /model switch's mark (model_mark.js, the
|
|
83
|
+
// event's or entry's other fields) is a quiet divider with its line.
|
|
84
|
+
export function hookNoticeTemplate({ line, level = null, cls = null, title = null, ...data }, { bubble = true } = {}) {
|
|
85
|
+
if (bubble && isModelMark(data)) {
|
|
86
|
+
return html`<div class=${`bubble hook-notice ${MODEL_MARK_CLASS}`}><span class="mm-text">${line}</span></div>`;
|
|
87
|
+
}
|
|
83
88
|
const klass = (bubble ? "bubble hook-notice" : "hook-notice") + (level === "warn" ? " warn" : "") + (cls ? ` ${cls}` : "");
|
|
84
89
|
return html`<div class=${klass} title=${title || nothing}>${linkedLine(line)}</div>`;
|
|
85
90
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// What a ⚙ panel change costs the prompt cache, in the words of the
|
|
2
|
+
// confirm a typed change gets (CacheConfirm, lib/samagotchi/cache_confirm.rb):
|
|
3
|
+
// the change itself costs nothing, the next turn pays. The panel sends its
|
|
4
|
+
// line with --yes, so it says the cost here instead of asking
|
|
5
|
+
// (docs/internals/prompt-caching.md, rule 6). One sentence per setting.
|
|
6
|
+
//
|
|
7
|
+
// A new module: the page's modules are cached at unversioned URLs
|
|
8
|
+
// (docs/internals/web-frontend.md); thinking_chip.js and
|
|
9
|
+
// reasoning_history_chip.js keep their CONFIG.cost, read from here.
|
|
10
|
+
|
|
11
|
+
export const CACHE_COST = Object.freeze({
|
|
12
|
+
thinking: "A thinking change on Gemma or a hosted API: the next turn starts without a prompt cache and reads the whole conversation again.",
|
|
13
|
+
reasoningHistory: "A reasoning history change to or from all: the next turn reads the conversation again from the first changed reply.",
|
|
14
|
+
llmContext: "Turning forget on or off: the next turn reads the whole prompt again.",
|
|
15
|
+
});
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import { clientLabel, isOwn } from "./turn_events.js";
|
|
8
8
|
import { anchorInsertIndex, commandAnchor, isShellCommandLine, keepsCommandBubble } from "./kept_commands.js";
|
|
9
|
+
import { confirmBlockOf, drawConfirmBlock } from "./confirm_block.js";
|
|
9
10
|
|
|
10
11
|
// Whether a composer line in an open session is one of its commands
|
|
11
12
|
// (+commands+: the session GET's listing), as the terminals decide
|
|
@@ -159,7 +160,9 @@ export function continueLine(choice, reason = "") {
|
|
|
159
160
|
// (waits: "turn_end") draws it `waiting`, saying so, until it ran or was
|
|
160
161
|
// dropped (styled as failed). A change that restarts the prompt cache asks
|
|
161
162
|
// first (CacheConfirm): `confirm` is the line that applies it, and this
|
|
162
|
-
// tab's own bubble (`own`) gets an Apply button that sends it
|
|
163
|
+
// tab's own bubble (`own`) gets an Apply button that sends it; with the
|
|
164
|
+
// worker's words for it (`confirmBlock`: title, body, button) that bubble
|
|
165
|
+
// is the warning block instead (confirm_block.js).
|
|
163
166
|
const QUEUED_TEXT = "queued: runs after this turn";
|
|
164
167
|
const APPLY_TEXT = "Apply (restarts the cache)";
|
|
165
168
|
|
|
@@ -182,6 +185,7 @@ export function commandView(event, myId) {
|
|
|
182
185
|
waiting,
|
|
183
186
|
commandId: event.command_id || null,
|
|
184
187
|
confirm: typeof event.confirm?.line === "string" ? event.confirm.line : null,
|
|
188
|
+
confirmBlock: typeof event.confirm?.line === "string" ? confirmBlockOf(event.confirm) : null,
|
|
185
189
|
// A card's action (Nudge…): no bubble unless it says something back.
|
|
186
190
|
// A tap's /objectives edit: the strip and the entry are its answer.
|
|
187
191
|
hidden: event.card === true && event.status !== "error" && (!event.output || OBJECTIVES_LINE.test(event.line || "")),
|
|
@@ -205,14 +209,19 @@ export function historyHasCommandBubble(historyBubbles, line) {
|
|
|
205
209
|
// Command bubbles are page-only: after a reload the button is gone, and
|
|
206
210
|
// the change is typed again.
|
|
207
211
|
export function drawCommandBubble(div, { label, line, text, busy = false, failed = false, waiting = false, commandId = null,
|
|
208
|
-
own = false, confirm = null }) {
|
|
212
|
+
own = false, confirm = null, confirmBlock = null }) {
|
|
209
213
|
div.className = "bubble command" + (busy ? " busy" : "") + (failed ? " failed" : "") + (waiting ? " queued" : "");
|
|
214
|
+
div.removeAttribute("role");
|
|
215
|
+
div.removeAttribute("aria-label");
|
|
210
216
|
if (commandId) div.dataset.commandId = commandId;
|
|
211
217
|
div.replaceChildren();
|
|
212
218
|
const head = document.createElement("div");
|
|
213
219
|
head.className = "command-line";
|
|
214
220
|
head.textContent = label ? `${label}> ${line}` : line;
|
|
215
221
|
div.appendChild(head);
|
|
222
|
+
// This tab's own question with the worker's words: the warning block (its
|
|
223
|
+
// body says what the output's text says, without the line to send).
|
|
224
|
+
if (own && confirm && confirmBlock) return drawConfirmBlock(div, { block: confirmBlock, confirm });
|
|
216
225
|
if (text) {
|
|
217
226
|
const body = document.createElement("pre");
|
|
218
227
|
body.className = "command-output";
|