samagotchi 0.182.0 → 0.183.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 +68 -1
- data/bin/chi +3 -1
- data/docs/architecture.md +16 -6
- data/docs/bundle-authoring.md +1 -1
- data/docs/bundles.md +103 -4
- data/docs/cli.md +28 -6
- data/docs/configuration.md +2 -1
- data/docs/guardrails.md +16 -1
- data/docs/internals/block-renderers.md +58 -3
- data/docs/internals/bridge.md +13 -5
- data/docs/internals/bundles.md +21 -4
- data/docs/internals/engine.md +18 -8
- data/docs/internals/remote-sessions.md +24 -5
- data/docs/internals/web-frontend.md +88 -28
- data/docs/internals/working-set.md +1 -1
- data/docs/mcp-server.md +80 -0
- data/docs/plugins.md +308 -21
- data/docs/sessions.md +3 -3
- data/docs/sub-agent.md +9 -0
- data/docs/testing.md +3 -1
- data/docs/thin-clients.md +146 -0
- data/lib/samagotchi/bridge/pending_card.rb +4 -3
- data/lib/samagotchi/bridge/sse_writer.rb +16 -15
- data/lib/samagotchi/bridge.rb +59 -68
- data/lib/samagotchi/bundle_command/api_calls.rb +1 -1
- data/lib/samagotchi/bundle_command/check.rb +36 -19
- data/lib/samagotchi/bundle_command.rb +50 -21
- data/lib/samagotchi/bundle_manifest.rb +101 -4
- data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
- data/lib/samagotchi/bundles/mcp/client.rb +260 -0
- data/lib/samagotchi/bundles/mcp/content.rb +107 -0
- data/lib/samagotchi/bundles/mcp/http_client.rb +407 -0
- data/lib/samagotchi/bundles/mcp/manifest.yml +13 -5
- data/lib/samagotchi/bundles/mcp/oauth.rb +814 -0
- data/lib/samagotchi/bundles/mcp/plugin.rb +556 -574
- data/lib/samagotchi/bundles/mcp/tools.rb +232 -0
- data/lib/samagotchi/bundles/system/config_modification_protocol.md +10 -2
- data/lib/samagotchi/bundles/system/manifest.yml +2 -2
- data/lib/samagotchi/cli/parent_wait.rb +5 -3
- data/lib/samagotchi/command_intake.rb +7 -0
- data/lib/samagotchi/commands/registry.rb +8 -3
- data/lib/samagotchi/continue_offer.rb +24 -15
- data/lib/samagotchi/engine/block_fix.rb +4 -4
- data/lib/samagotchi/engine/cards.rb +14 -5
- data/lib/samagotchi/engine/events.rb +10 -15
- data/lib/samagotchi/engine/models.rb +1 -1
- data/lib/samagotchi/engine/plugins.rb +3 -0
- data/lib/samagotchi/engine/recap.rb +1 -1
- data/lib/samagotchi/engine/side_requests.rb +1 -1
- data/lib/samagotchi/engine/sign_in.rb +142 -0
- data/lib/samagotchi/engine/turn_end.rb +3 -3
- data/lib/samagotchi/engine.rb +43 -27
- data/lib/samagotchi/engine_snapshots.rb +1 -1
- data/lib/samagotchi/guardrail_wiring.rb +3 -1
- data/lib/samagotchi/guardrails/protected_paths.rb +51 -2
- data/lib/samagotchi/guardrails/verdict.rb +3 -2
- data/lib/samagotchi/hold_stop.rb +4 -4
- data/lib/samagotchi/idle_recap.rb +19 -11
- data/lib/samagotchi/idle_reminders.rb +7 -5
- data/lib/samagotchi/idle_scheduler.rb +7 -10
- data/lib/samagotchi/installed_bundles/dir.rb +37 -0
- data/lib/samagotchi/installed_bundles/manifest.rb +7 -1
- data/lib/samagotchi/installed_bundles/plugin_ref.rb +9 -5
- data/lib/samagotchi/installed_bundles/record.rb +3 -2
- data/lib/samagotchi/installed_bundles/standing.rb +3 -1
- data/lib/samagotchi/llm/http.rb +96 -5
- data/lib/samagotchi/mcp_command.rb +110 -0
- data/lib/samagotchi/mcp_server/http.rb +112 -0
- data/lib/samagotchi/mcp_server/protocol.rb +244 -0
- data/lib/samagotchi/mcp_server/stdio.rb +92 -0
- data/lib/samagotchi/mcp_server/text.rb +155 -0
- data/lib/samagotchi/mcp_server/tools.rb +245 -0
- data/lib/samagotchi/memory_bundle/asset_installer.rb +40 -12
- data/lib/samagotchi/memory_bundle/builder.rb +20 -2
- data/lib/samagotchi/memory_bundle/installed_copy.rb +1 -1
- data/lib/samagotchi/memory_bundle/installer.rb +16 -3
- data/lib/samagotchi/memory_bundle/provenance.rb +18 -1
- data/lib/samagotchi/memory_bundle/shas.rb +6 -4
- data/lib/samagotchi/memory_bundle/shipped_update.rb +7 -3
- data/lib/samagotchi/memory_bundle/status.rb +15 -6
- data/lib/samagotchi/memory_bundle/uninstaller.rb +9 -4
- data/lib/samagotchi/plugin/api.rb +6 -1
- data/lib/samagotchi/plugin/card.rb +36 -11
- data/lib/samagotchi/plugin/context.rb +63 -6
- data/lib/samagotchi/plugin/http.rb +40 -15
- data/lib/samagotchi/plugin/http_errors.rb +15 -0
- data/lib/samagotchi/plugin/http_stream.rb +86 -0
- data/lib/samagotchi/plugin/http_transport.rb +58 -16
- data/lib/samagotchi/plugin/loader.rb +18 -11
- data/lib/samagotchi/plugin/secrets.rb +266 -0
- data/lib/samagotchi/plugin/sign_in.rb +392 -0
- data/lib/samagotchi/plugin/sse_reader.rb +103 -0
- data/lib/samagotchi/queued_command.rb +28 -4
- data/lib/samagotchi/recap_setup.rb +13 -4
- data/lib/samagotchi/ref.rb +3 -2
- data/lib/samagotchi/relay_verifier.rb +8 -7
- data/lib/samagotchi/relay_watcher.rb +4 -4
- data/lib/samagotchi/run_options.rb +2 -1
- data/lib/samagotchi/send_command.rb +54 -41
- data/lib/samagotchi/session_access/ask.rb +244 -0
- data/lib/samagotchi/session_access/contract.rb +5 -0
- data/lib/samagotchi/session_access/exchange/token.rb +101 -0
- data/lib/samagotchi/session_access/exchange.rb +173 -0
- data/lib/samagotchi/session_access/local.rb +25 -6
- data/lib/samagotchi/session_access/remote.rb +4 -1
- data/lib/samagotchi/session_command_catalog.rb +10 -0
- data/lib/samagotchi/session_commands.rb +17 -0
- data/lib/samagotchi/session_config.rb +4 -4
- data/lib/samagotchi/session_observer.rb +27 -0
- data/lib/samagotchi/steer_cut.rb +4 -3
- data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +4 -1
- data/lib/samagotchi/terminal_ui/formatting.rb +15 -0
- data/lib/samagotchi/testing/boundary.rb +4 -0
- data/lib/samagotchi/testing/bundle.rb +48 -2
- data/lib/samagotchi/testing/fake_http.rb +60 -15
- data/lib/samagotchi/testing/host.rb +4 -1
- data/lib/samagotchi/testing/recorder.rb +45 -5
- data/lib/samagotchi/tools/tool_index.rb +14 -3
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +8 -6
- data/lib/samagotchi/web/ask_routes.rb +292 -0
- data/lib/samagotchi/web/mcp_route.rb +88 -0
- data/lib/samagotchi/web/public/app.js +2 -3
- data/lib/samagotchi/web/public/block_frame.js +72 -11
- data/lib/samagotchi/web/public/block_host.js +44 -8
- data/lib/samagotchi/web/public/block_regions.js +126 -0
- data/lib/samagotchi/web/public/card.js +22 -9
- data/lib/samagotchi/web/public/card_actions.js +21 -0
- data/lib/samagotchi/web/public/card_kinds.js +1 -1
- data/lib/samagotchi/web/public/card_registry.js +3 -2
- data/lib/samagotchi/web/public/data.js +15 -12
- data/lib/samagotchi/web/public/delete_sheet.js +222 -162
- data/lib/samagotchi/web/public/diagram_wiring.js +28 -0
- data/lib/samagotchi/web/public/format.js +3 -0
- data/lib/samagotchi/web/public/index.html +52 -7
- data/lib/samagotchi/web/public/more_sheet.js +6 -4
- data/lib/samagotchi/web/public/picture_regions.js +243 -0
- data/lib/samagotchi/web/public/picture_viewer.js +113 -12
- data/lib/samagotchi/web/public/refs.js +5 -3
- data/lib/samagotchi/web/public/region_hit.js +144 -0
- data/lib/samagotchi/web/public/removal_sheet_model.js +275 -0
- data/lib/samagotchi/web/public/running_tasks.js +6 -4
- data/lib/samagotchi/web/public/session_actions.js +105 -100
- data/lib/samagotchi/web/session_access_routes.rb +14 -11
- data/lib/samagotchi/web/session_control.rb +31 -9
- data/lib/samagotchi/web/session_create.rb +2 -2
- data/lib/samagotchi/worker/child_side.rb +3 -2
- data/lib/samagotchi/worker/command_queue.rb +13 -13
- data/lib/samagotchi/worker/leave.rb +3 -3
- data/lib/samagotchi/worker/scratch.rb +6 -6
- data/lib/samagotchi/worker/signal_grace.rb +6 -5
- data/lib/samagotchi/worker/startup.rb +3 -3
- data/lib/samagotchi/worker/wake_turns.rb +4 -4
- data/lib/samagotchi/worker.rb +32 -21
- data/lib/samagotchi/worker_exit_claim.rb +1 -1
- data/lib/samagotchi/worker_idle_exit.rb +18 -9
- data/lib/samagotchi/worker_inbound.rb +12 -7
- data/lib/samagotchi/worker_participant_gate.rb +9 -7
- data/lib/samagotchi/worker_task_wakes.rb +7 -5
- metadata +31 -1
|
@@ -66,8 +66,7 @@ module Samagotchi
|
|
|
66
66
|
# No file list, just remove provenance (rules, a plugin and scripts
|
|
67
67
|
# live there too)
|
|
68
68
|
save_edits(provenance, bundle)
|
|
69
|
-
|
|
70
|
-
@removed_files << "plugin/#{File.basename(plugin_path)}" if plugin_path && File.exist?(plugin_path)
|
|
69
|
+
note_removed_plugin(provenance, bundle)
|
|
71
70
|
FileUtils.rm_rf(provenance.bundle_dir)
|
|
72
71
|
return true
|
|
73
72
|
end
|
|
@@ -146,8 +145,7 @@ module Samagotchi
|
|
|
146
145
|
FileUtils.rm_rf(provenance.hooks_dir)
|
|
147
146
|
end
|
|
148
147
|
|
|
149
|
-
|
|
150
|
-
@removed_files << "plugin/#{File.basename(plugin_path)}" if plugin_path && File.exist?(plugin_path)
|
|
148
|
+
note_removed_plugin(provenance, bundle)
|
|
151
149
|
FileUtils.rm_rf(provenance.bundle_dir)
|
|
152
150
|
true
|
|
153
151
|
rescue SystemCallError => e
|
|
@@ -156,6 +154,13 @@ module Samagotchi
|
|
|
156
154
|
|
|
157
155
|
private
|
|
158
156
|
|
|
157
|
+
# The plugin's files on disk (its parts, then the plugin file) as
|
|
158
|
+
# plugin/F in removed_files.
|
|
159
|
+
def note_removed_plugin(provenance, bundle)
|
|
160
|
+
paths = provenance.plugin_part_paths(bundle) + Array(provenance.plugin_path(bundle))
|
|
161
|
+
paths.each { |path| @removed_files << "plugin/#{File.basename(path)}" if File.exist?(path) }
|
|
162
|
+
end
|
|
163
|
+
|
|
159
164
|
# Copies each edited file of the bundle's own folder to the trash: the
|
|
160
165
|
# rm_rf below takes them, --force or not. Rules, the plugin and
|
|
161
166
|
# scripts by their source-less AssetState and ASSET_UNINSTALL's
|
|
@@ -55,7 +55,12 @@ module Samagotchi
|
|
|
55
55
|
"ctx.session_dir" => "0.143.0", "ctx.session_dir(create:)" => "0.144.0",
|
|
56
56
|
"ctx.objectives" => "0.149.0", "ctx.turn_running?" => "0.149.0",
|
|
57
57
|
"ctx.last_answer" => "0.154.0",
|
|
58
|
-
"chi.on(on_error:)" => "0.157.0"
|
|
58
|
+
"chi.on(on_error:)" => "0.157.0",
|
|
59
|
+
"ctx.http.stream" => "0.183.0",
|
|
60
|
+
"ctx.cancel_controller" => "0.183.0",
|
|
61
|
+
"ctx.secrets" => "0.183.0",
|
|
62
|
+
"ctx.sign_in" => "0.183.0",
|
|
63
|
+
"ctx.card(between_turns:)" => "0.183.0"
|
|
59
64
|
}.freeze
|
|
60
65
|
|
|
61
66
|
# chi.on's on_error: values. :log (the default) logs a raise and goes
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "securerandom"
|
|
4
|
+
require "samagotchi/ref"
|
|
4
5
|
|
|
5
6
|
module Samagotchi
|
|
6
7
|
module Plugin
|
|
@@ -9,11 +10,15 @@ module Samagotchi
|
|
|
9
10
|
module Card
|
|
10
11
|
LEVELS = %i[info warn].freeze
|
|
11
12
|
MAX_ACTIONS = 6
|
|
13
|
+
# An authorize address can be longer than a ref's (RefAction::MAX_URL).
|
|
14
|
+
MAX_URL = 4_096
|
|
12
15
|
|
|
13
16
|
module_function
|
|
14
17
|
|
|
15
18
|
# @return [Hash] {type: :card, id:, source:, title:, body:, level:,
|
|
16
|
-
# actions: [{label:, command:}]}; a new id when none
|
|
19
|
+
# actions: [{label:, command:} | {label:, url:}]}; a new id when none
|
|
20
|
+
# is given. A +url+ action opens that http(s) address in a new tab
|
|
21
|
+
# (the web) or prints it (the TUI); it never sends to the session.
|
|
17
22
|
# @raise [ArgumentError] a card without a title, a bad level or action
|
|
18
23
|
def build(source:, title:, body:, actions:, level:, id:)
|
|
19
24
|
title = title.to_s.strip
|
|
@@ -25,20 +30,40 @@ module Samagotchi
|
|
|
25
30
|
actions = Array(actions)
|
|
26
31
|
raise ArgumentError, "a card has at most #{MAX_ACTIONS} actions" if actions.size > MAX_ACTIONS
|
|
27
32
|
|
|
28
|
-
actions = actions.map
|
|
29
|
-
raise ArgumentError, "a card action is a Hash {label:, command:}" unless action.is_a?(Hash)
|
|
30
|
-
|
|
31
|
-
action = action.transform_keys(&:to_sym)
|
|
32
|
-
command = action[:command].to_s.strip
|
|
33
|
-
raise ArgumentError, "a card action needs a command" if command.empty? || command.include?("\n")
|
|
34
|
-
|
|
35
|
-
label = action[:label].to_s.strip
|
|
36
|
-
{ label: label.empty? ? command : label, command: command }
|
|
37
|
-
end
|
|
33
|
+
actions = actions.map { |action| build_action(action) }
|
|
38
34
|
id = id.to_s.strip
|
|
39
35
|
{ type: :card, id: id.empty? ? SecureRandom.hex(4) : id, source: source.to_s, title: title, body: body.to_s,
|
|
40
36
|
level: level, actions: actions }
|
|
41
37
|
end
|
|
38
|
+
|
|
39
|
+
# @raise [ArgumentError] not a Hash, both or neither of command / url,
|
|
40
|
+
# a bad command, a url that isn't http(s) with a host (no userinfo,
|
|
41
|
+
# at most MAX_URL characters), a url action without a label
|
|
42
|
+
def build_action(action)
|
|
43
|
+
raise ArgumentError, "a card action is a Hash {label:, command:} or {label:, url:}" unless action.is_a?(Hash)
|
|
44
|
+
|
|
45
|
+
action = action.transform_keys(&:to_sym)
|
|
46
|
+
label = action[:label].to_s.strip
|
|
47
|
+
has_command = action.key?(:command) && !action[:command].nil?
|
|
48
|
+
has_url = action.key?(:url) && !action[:url].nil?
|
|
49
|
+
raise ArgumentError, "a card action has a command or a url, not both" if has_command && has_url
|
|
50
|
+
|
|
51
|
+
return url_action(label, action[:url].to_s.strip) if has_url
|
|
52
|
+
|
|
53
|
+
command = action[:command].to_s.strip
|
|
54
|
+
raise ArgumentError, "a card action needs a command" if command.empty? || command.include?("\n")
|
|
55
|
+
|
|
56
|
+
{ label: label.empty? ? command : label, command: command }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def url_action(label, url)
|
|
60
|
+
unless RefAction.http_url?(url, max: MAX_URL)
|
|
61
|
+
raise ArgumentError, "a card action's url must be http(s) with a host, at most #{MAX_URL} characters"
|
|
62
|
+
end
|
|
63
|
+
raise ArgumentError, "a card action with a url needs a label" if label.empty?
|
|
64
|
+
|
|
65
|
+
{ label: label, url: url }
|
|
66
|
+
end
|
|
42
67
|
end
|
|
43
68
|
end
|
|
44
69
|
end
|
|
@@ -33,9 +33,11 @@ module Samagotchi
|
|
|
33
33
|
# ctx.http's file needs only json and uri (net/http loads with the first
|
|
34
34
|
# real request); autoloaded so a bundle can name HttpError at load time.
|
|
35
35
|
autoload :Http, File.expand_path("http", __dir__)
|
|
36
|
-
autoload :HttpError, File.expand_path("
|
|
37
|
-
autoload :HttpCancelled, File.expand_path("
|
|
36
|
+
autoload :HttpError, File.expand_path("http_errors", __dir__)
|
|
37
|
+
autoload :HttpCancelled, File.expand_path("http_errors", __dir__)
|
|
38
38
|
autoload :Judgments, File.expand_path("judgments", __dir__)
|
|
39
|
+
autoload :Secrets, File.expand_path("secrets", __dir__)
|
|
40
|
+
autoload :SignIn, File.expand_path("sign_in", __dir__)
|
|
39
41
|
|
|
40
42
|
# The Engine's side of a Context: callables, so the Engine itself is
|
|
41
43
|
# never handed out. +messages+ returns the conversation, +notify+ takes
|
|
@@ -64,7 +66,9 @@ module Samagotchi
|
|
|
64
66
|
# +cancel_controller+ takes (handler:) and returns the controller a
|
|
65
67
|
# ctx.http call without cancel: uses, nil for none: an init task's own,
|
|
66
68
|
# else the running turn's for work done for it (the turn's thread, or a
|
|
67
|
-
# plugin handler: +handler+) until the turn has ended.
|
|
69
|
+
# plugin handler: +handler+) until the turn has ended. +current_cancel+
|
|
70
|
+
# returns the controller +cancelled+ reads on the calling thread (an init
|
|
71
|
+
# task's own, else the running turn's; nil for none): ctx.cancel_controller.
|
|
68
72
|
# +cost+ returns the session's spend now (SessionMetrics::Cost),
|
|
69
73
|
# +usage+ its work now (SessionMetrics::Usage).
|
|
70
74
|
# +cache_confirm+ takes (line, changed) and returns a
|
|
@@ -75,11 +79,17 @@ module Samagotchi
|
|
|
75
79
|
# prompt mentions, for the candidate judge (Engine#judge_late_candidates).
|
|
76
80
|
# +judgments+ returns the SystemOne::Judgments ctx.judge, ctx.judge_later
|
|
77
81
|
# and ctx.judgments ask and label through.
|
|
82
|
+
# +secrets_store+ takes (bundle) and returns the store ctx.secrets keeps
|
|
83
|
+
# that bundle's secrets in (Secrets::FileStore; the test kit's in memory).
|
|
84
|
+
# +sign_in+ takes (bundle, title:, button:, timeout:, port:, path:, cancel:) and
|
|
85
|
+
# the block that builds the address, and returns the SignIn flow
|
|
86
|
+
# (Engine#start_sign_in; the test kit's opens no port).
|
|
78
87
|
Host = Struct.new(:session_id, :cwd, :messages, :messages_partial, :notify, :ask_user, :cancelled, :card,
|
|
79
88
|
:ask_model, :model_name, :model_key, :state_dir, :scratch, :setup, :prompt_files,
|
|
80
89
|
:steer, :stop_turn, :stop_generation, :frontend, :toolkit, :declared_tools, :use_toolkit, :load_tools,
|
|
81
90
|
:cancel_controller, :cost, :usage, :turn_author, :cache_confirm, :model_names,
|
|
82
|
-
:candidate_proposed, :judgments, :turn_running,
|
|
91
|
+
:candidate_proposed, :judgments, :turn_running, :current_cancel, :secrets_store,
|
|
92
|
+
:sign_in, keyword_init: true)
|
|
83
93
|
|
|
84
94
|
# ctx.ask_model failed: the model couldn't be reached, timed out, or
|
|
85
95
|
# sent nothing usable. The message says why, for the user.
|
|
@@ -404,10 +414,14 @@ module Samagotchi
|
|
|
404
414
|
# @param actions [Array<Hash>] {label:, command:} ("/hello again")
|
|
405
415
|
# @param level [Symbol] :info or :warn
|
|
406
416
|
# @param id [String, nil] an earlier card's id to replace it
|
|
417
|
+
# @param between_turns [Boolean] never a running turn's: shown outside
|
|
418
|
+
# the turn's block while one runs (from a thread of the plugin's own)
|
|
407
419
|
# @return [String] the card's id
|
|
408
420
|
# @raise [ArgumentError] no title, a bad level or action
|
|
409
|
-
def card(title:, body: "", actions: [], level: :info, id: nil)
|
|
410
|
-
|
|
421
|
+
def card(title:, body: "", actions: [], level: :info, id: nil, between_turns: false)
|
|
422
|
+
shown = { source: @bundle, title: title, body: body, actions: actions, level: level, id: id }
|
|
423
|
+
shown[:between_turns] = true if between_turns
|
|
424
|
+
@host.card.call(**shown)
|
|
411
425
|
end
|
|
412
426
|
|
|
413
427
|
# The session's objectives (docs/internals/objectives.md), read now: the
|
|
@@ -510,6 +524,40 @@ module Samagotchi
|
|
|
510
524
|
@sessions ||= Sessions.new(@host)
|
|
511
525
|
end
|
|
512
526
|
|
|
527
|
+
# The bundle's secrets: tokens kept apart from its data files
|
|
528
|
+
# (Secrets, docs/plugins.md).
|
|
529
|
+
# @return [Secrets]
|
|
530
|
+
def secrets
|
|
531
|
+
@secrets ||= Secrets.new(@host.secrets_store.call(@bundle))
|
|
532
|
+
end
|
|
533
|
+
|
|
534
|
+
# Send the session's owner to an address and get back the address the
|
|
535
|
+
# browser was sent to (SignIn, docs/plugins.md, Sign-in): a card with
|
|
536
|
+
# +button+, a loopback listener, /callback for a browser elsewhere.
|
|
537
|
+
# Returns at once: wait on the flow (#wait) on a thread of your own.
|
|
538
|
+
# A one-shot run and a :generation_progress handler ask no one: the
|
|
539
|
+
# flow is :stopped already, with no card and no port. No Stop ends it
|
|
540
|
+
# unless +cancel+ is given: a controller it polls (ctx.cancel_controller
|
|
541
|
+
# read on the calling thread: a tool's is its turn's), cancelled → the
|
|
542
|
+
# flow ends :cancelled.
|
|
543
|
+
# @yieldparam redirect_uri [String] "http://127.0.0.1:<port><path>"
|
|
544
|
+
# @yieldreturn [String] the address to send the user to (http(s))
|
|
545
|
+
# @return [SignIn]
|
|
546
|
+
# @raise [ArgumentError] no block, a timeout outside 60..900, a taken
|
|
547
|
+
# port, a bad path or address (one without state), a cancel: that isn't a controller
|
|
548
|
+
def sign_in(title:, button: "Open sign-in page", timeout: SignIn::DEFAULT_TIMEOUT, port: nil,
|
|
549
|
+
path: SignIn::DEFAULT_PATH, cancel: nil, &build)
|
|
550
|
+
raise ArgumentError, "ctx.sign_in needs a block that builds the address" unless build
|
|
551
|
+
|
|
552
|
+
SignIn.check_options!(timeout: timeout, path: path, cancel: cancel)
|
|
553
|
+
event = Thread.current[CURRENT_EVENTS]&.[](self)
|
|
554
|
+
stream = event.is_a?(Hash) && event[:type] == :generation_progress
|
|
555
|
+
return SignIn.stopped(bundle: @bundle, title: title.to_s) if frontend == :one_shot || stream || @host.sign_in.nil?
|
|
556
|
+
|
|
557
|
+
@host.sign_in.call(@bundle, title: title.to_s, button: button.to_s, timeout: timeout, port: port, path: path,
|
|
558
|
+
cancel: cancel, &build)
|
|
559
|
+
end
|
|
560
|
+
|
|
513
561
|
# An HTTP client for calling services (Http, docs/plugins.md). A call
|
|
514
562
|
# made for the turn (a tool, a handler) stops when the turn is stopped;
|
|
515
563
|
# one from a command, a service or your own thread has only its
|
|
@@ -549,6 +597,15 @@ module Samagotchi
|
|
|
549
597
|
# Whether the running turn was cancelled (a long tool should stop).
|
|
550
598
|
def cancelled? = !!@host.cancelled.call
|
|
551
599
|
|
|
600
|
+
# The CancellationController ctx.cancelled? reads on this thread: inside
|
|
601
|
+
# an init task the task's own (chi shutting down), else the running
|
|
602
|
+
# turn's; nil when neither. Read it on the thread that does the work and
|
|
603
|
+
# hand it on (ctx.http's cancel:, a thread of yours that polls it): on
|
|
604
|
+
# another thread the answer is that thread's. Unlike ctx.http's default
|
|
605
|
+
# it is the turn's inside a service's start block too.
|
|
606
|
+
# @return [CancellationController, nil]
|
|
607
|
+
def cancel_controller = @host.current_cancel&.call
|
|
608
|
+
|
|
552
609
|
# Put +text+ into the running turn, as a user's steering does: at the
|
|
553
610
|
# loop's next boundary (after the tool calls in flight) it joins the
|
|
554
611
|
# conversation as its own user message, shown in every UI as a nudge
|
|
@@ -2,19 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
require "json"
|
|
4
4
|
require "uri"
|
|
5
|
+
require_relative "http_errors"
|
|
5
6
|
|
|
6
7
|
module Samagotchi
|
|
7
8
|
module Plugin
|
|
8
|
-
# ctx.http failed: the request didn't complete (refused, timed out, DNS,
|
|
9
|
-
# TLS, reset, a malformed response, a bad gzip body), or Response#json
|
|
10
|
-
# was called on a body that isn't JSON. The message says what and where,
|
|
11
|
-
# never headers or a body; the original error is the #cause.
|
|
12
|
-
class HttpError < StandardError; end
|
|
13
|
-
|
|
14
|
-
# A ctx.http request was cancelled (its cancel: controller, or the turn's
|
|
15
|
-
# Stop).
|
|
16
|
-
class HttpCancelled < HttpError; end
|
|
17
|
-
|
|
18
9
|
# An HTTP client for a plugin (ctx.http, docs/plugins.md): a few methods,
|
|
19
10
|
# a small Response, one error type (HttpError). It checks its arguments
|
|
20
11
|
# and builds the request; a transport (Http::NetTransport by default,
|
|
@@ -51,7 +42,8 @@ module Samagotchi
|
|
|
51
42
|
# @param cancel_lookup [#call] → the CancellationController a call
|
|
52
43
|
# without cancel: uses (nil: none), read at call time
|
|
53
44
|
# @param transport [#call, nil] call(method:, uri:, headers:, body:,
|
|
54
|
-
# timeout:, cancel:) → Response, or raises HttpError / HttpCancelled
|
|
45
|
+
# timeout:, cancel:) → Response, or raises HttpError / HttpCancelled;
|
|
46
|
+
# stream(same keywords) { |stream| … } → the block's value
|
|
55
47
|
def initialize(bundle:, cancel_lookup: -> {}, transport: nil)
|
|
56
48
|
@bundle = bundle.to_s
|
|
57
49
|
@cancel_lookup = cancel_lookup
|
|
@@ -90,16 +82,46 @@ module Samagotchi
|
|
|
90
82
|
# @param body [String, nil] sent as is (set your own Content-Type)
|
|
91
83
|
# @raise [ArgumentError] an unknown method, a body that isn't a String, and as #get
|
|
92
84
|
def request(method, url, body: nil, headers: {}, timeout: DEFAULT_TIMEOUT, cancel: nil)
|
|
85
|
+
transport.call(**wire_args(method, url, body, headers, timeout, cancel))
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# A streamed request: the block gets a Stream (status and headers
|
|
89
|
+
# first, then the body as it arrives: Stream#each_event,
|
|
90
|
+
# #each_chunk, #body). Leaving the block closes the connection,
|
|
91
|
+
# however it is left (its end, break, return, a raise; the raise goes
|
|
92
|
+
# on as it is). Arguments, checks, cancel, proxies, User-Agent and
|
|
93
|
+
# the log line as #request; no retries; +timeout+ is the connect, the
|
|
94
|
+
# write and each read, not a total deadline.
|
|
95
|
+
# @yield [Stream]
|
|
96
|
+
# @return [Object] the block's value
|
|
97
|
+
# @raise [ArgumentError] no block, and as #request
|
|
98
|
+
# @raise [HttpError, HttpCancelled] before the block, or from a
|
|
99
|
+
# Stream read inside it
|
|
100
|
+
def stream(method, url, body: nil, headers: {}, timeout: DEFAULT_TIMEOUT, cancel: nil, &block)
|
|
101
|
+
raise ArgumentError, "ctx.http.stream needs a block (it gets the Stream)" unless block
|
|
102
|
+
|
|
103
|
+
transport.stream(**wire_args(method, url, body, headers, timeout, cancel), &block)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# +bytes+ tagged UTF-8 when they are valid UTF-8, else binary.
|
|
107
|
+
def self.text(bytes)
|
|
108
|
+
text = bytes.to_s.dup
|
|
109
|
+
utf8 = text.dup.force_encoding(Encoding::UTF_8)
|
|
110
|
+
utf8.valid_encoding? ? utf8 : text.force_encoding(Encoding::BINARY)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
private
|
|
114
|
+
|
|
115
|
+
# The checked arguments a transport gets.
|
|
116
|
+
def wire_args(method, url, body, headers, timeout, cancel)
|
|
93
117
|
verb = method.to_s.upcase
|
|
94
118
|
raise ArgumentError, "unknown HTTP method #{method.inspect} (#{METHODS.join(", ")})" unless METHODS.include?(verb)
|
|
95
119
|
raise ArgumentError, "the body must be a String (or nil), got #{body.class}" unless body.nil? || body.is_a?(String)
|
|
96
120
|
|
|
97
|
-
|
|
98
|
-
|
|
121
|
+
{ method: verb, uri: parse_url(url), headers: header_hash(headers), body: body,
|
|
122
|
+
timeout: timeout_seconds(timeout), cancel: cancel || @cancel_lookup.call }
|
|
99
123
|
end
|
|
100
124
|
|
|
101
|
-
private
|
|
102
|
-
|
|
103
125
|
def transport
|
|
104
126
|
@transport ||= begin
|
|
105
127
|
require_relative "http_transport"
|
|
@@ -152,3 +174,6 @@ module Samagotchi
|
|
|
152
174
|
end
|
|
153
175
|
end
|
|
154
176
|
end
|
|
177
|
+
|
|
178
|
+
# After Http: its Stream and SseReader reopen the class.
|
|
179
|
+
require_relative "http_stream"
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Samagotchi
|
|
4
|
+
module Plugin
|
|
5
|
+
# ctx.http failed: the request didn't complete (refused, timed out, DNS,
|
|
6
|
+
# TLS, reset, a malformed response, a bad gzip body), or Response#json
|
|
7
|
+
# was called on a body that isn't JSON. The message says what and where,
|
|
8
|
+
# never headers or a body; the original error is the #cause.
|
|
9
|
+
class HttpError < StandardError; end
|
|
10
|
+
|
|
11
|
+
# A ctx.http request was cancelled (its cancel: controller, or the turn's
|
|
12
|
+
# Stop).
|
|
13
|
+
class HttpCancelled < HttpError; end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require_relative "http_errors"
|
|
5
|
+
require_relative "sse_reader"
|
|
6
|
+
|
|
7
|
+
module Samagotchi
|
|
8
|
+
module Plugin
|
|
9
|
+
class Http
|
|
10
|
+
# The answer ctx.http.stream's block gets (docs/plugins.md, "Calling
|
|
11
|
+
# services"): the status and headers before any of the body, then the
|
|
12
|
+
# body, read once, by one of #each_event, #each_chunk or #body. After
|
|
13
|
+
# one of them has read it, another read gives nothing (#body is "").
|
|
14
|
+
#
|
|
15
|
+
# A read that fails on the wire raises the HttpError (or
|
|
16
|
+
# HttpCancelled) the transport maps it to, from inside the read: the
|
|
17
|
+
# plugin's block never sees a raw IOError or Net::ReadTimeout. An
|
|
18
|
+
# error the plugin's own block raises while reading goes on as it is.
|
|
19
|
+
class Stream
|
|
20
|
+
attr_reader :status, :headers
|
|
21
|
+
|
|
22
|
+
# @param status [Integer]
|
|
23
|
+
# @param headers [Hash] frozen, lower-case names
|
|
24
|
+
# @param reader [#call] call { |bytes| … } reads the rest of the body
|
|
25
|
+
# @param failure [#call] (raw error) → the error to raise instead
|
|
26
|
+
def initialize(status:, headers:, reader:, failure: ->(error) { error })
|
|
27
|
+
@status = status
|
|
28
|
+
@headers = headers
|
|
29
|
+
@reader = reader
|
|
30
|
+
@failure = failure
|
|
31
|
+
@read = false
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# @return [Boolean] a 2xx status
|
|
35
|
+
def ok? = status.between?(200, 299)
|
|
36
|
+
|
|
37
|
+
# @yield [String] each piece of the body as it arrives (binary)
|
|
38
|
+
# @return [nil]
|
|
39
|
+
def each_chunk(&block)
|
|
40
|
+
raise ArgumentError, "each_chunk needs a block" unless block
|
|
41
|
+
return nil if @read
|
|
42
|
+
|
|
43
|
+
@read = true
|
|
44
|
+
inside = false
|
|
45
|
+
@reader.call do |bytes|
|
|
46
|
+
inside = true
|
|
47
|
+
yield(bytes)
|
|
48
|
+
inside = false
|
|
49
|
+
end
|
|
50
|
+
nil
|
|
51
|
+
rescue StandardError => e
|
|
52
|
+
raise if inside
|
|
53
|
+
|
|
54
|
+
raise @failure.call(e)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# The body as Server-Sent Events (SseReader's rules).
|
|
58
|
+
# @yield [SseReader::Event]
|
|
59
|
+
# @return [nil]
|
|
60
|
+
def each_event(&block)
|
|
61
|
+
raise ArgumentError, "each_event needs a block" unless block
|
|
62
|
+
|
|
63
|
+
reader = SseReader.new
|
|
64
|
+
each_chunk { |bytes| reader.feed(bytes, &block) }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# @return [String] the rest of the body, read to its end: UTF-8 when
|
|
68
|
+
# its bytes are, else binary; "" once the body was read
|
|
69
|
+
def body
|
|
70
|
+
bytes = +"".b
|
|
71
|
+
each_chunk { |chunk| bytes << chunk }
|
|
72
|
+
Http.text(bytes)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# #body parsed as JSON.
|
|
76
|
+
# @raise [HttpError] the body isn't JSON
|
|
77
|
+
def json
|
|
78
|
+
text = body
|
|
79
|
+
JSON.parse(text)
|
|
80
|
+
rescue JSON::ParserError, EncodingError
|
|
81
|
+
raise HttpError, "not JSON: status #{status}, #{text.to_s.bytesize} bytes"
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
@@ -51,30 +51,72 @@ module Samagotchi
|
|
|
51
51
|
response = client.fetch(uri, request, retries: false, check_status: false, write_timeout: timeout, cancel_controller: cancel,
|
|
52
52
|
log_fields: { purpose: "plugin", bundle: @bundle })
|
|
53
53
|
build_response(response)
|
|
54
|
-
rescue LLM::RequestCancelled
|
|
55
|
-
raise HttpCancelled, "#{method} #{where(uri)}: cancelled"
|
|
56
|
-
rescue HttpError
|
|
57
|
-
raise
|
|
58
|
-
rescue *WIRE_ERRORS => e
|
|
59
|
-
raise HttpError, "#{method} #{where(uri)}: #{reason(e)}"
|
|
60
54
|
rescue StandardError => e
|
|
61
|
-
|
|
62
|
-
|
|
55
|
+
raise mapped(e, method, uri)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# One streamed attempt (Plugin::Http#stream): the block gets a Stream
|
|
59
|
+
# whose reads map a wire error (or a cancel's closed socket) as
|
|
60
|
+
# #call does. Only chi's own code between the request and the block
|
|
61
|
+
# is under the catch-all: what the block raises goes on as it is.
|
|
62
|
+
# @return [Object] the block's value
|
|
63
|
+
# @raise [HttpError, HttpCancelled]
|
|
64
|
+
def stream(method:, uri:, headers:, body:, timeout:, cancel:)
|
|
65
|
+
request = Net::HTTP.const_get(method.capitalize).new(uri, headers)
|
|
66
|
+
request.body = body if body
|
|
67
|
+
client = LLM::HTTP.new(label: "plugin:#{@bundle}", open_timeout: timeout, read_timeout: timeout,
|
|
68
|
+
retry_policy: LLM::HTTP::RetryPolicy.none)
|
|
69
|
+
raised = nil
|
|
70
|
+
client.open_stream(uri, request, write_timeout: timeout, cancel_controller: cancel,
|
|
71
|
+
log_fields: { purpose: "plugin", bundle: @bundle }) do |response, marks|
|
|
72
|
+
stream = Stream.new(status: response.code.to_i, headers: headers_of(response),
|
|
73
|
+
reader: ->(&on_chunk) { response.read_body(&on_chunk) },
|
|
74
|
+
failure: ->(error) { read_failure(error, marks, cancel, method, uri) })
|
|
75
|
+
yield stream
|
|
76
|
+
rescue StandardError => e
|
|
77
|
+
raised = e
|
|
78
|
+
raise
|
|
79
|
+
end
|
|
80
|
+
rescue StandardError => e
|
|
81
|
+
raise if e.equal?(raised)
|
|
82
|
+
|
|
83
|
+
raise mapped(e, method, uri)
|
|
63
84
|
end
|
|
64
85
|
|
|
65
86
|
private
|
|
66
87
|
|
|
88
|
+
# A request's error as the plugin sees it: a cancel, a wire error
|
|
89
|
+
# (HttpError), or chi's own bug, logged and raised as it is.
|
|
90
|
+
def mapped(error, method, uri)
|
|
91
|
+
case error
|
|
92
|
+
when LLM::RequestCancelled then HttpCancelled.new("#{method} #{where(uri)}: cancelled")
|
|
93
|
+
when HttpError then error
|
|
94
|
+
when *WIRE_ERRORS then HttpError.new("#{method} #{where(uri)}: #{reason(error)}")
|
|
95
|
+
else
|
|
96
|
+
Log.exception(:plugins, "http_transport_failed", error, bundle: @bundle)
|
|
97
|
+
error
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# A Stream read that failed: the cancel's closed socket, else a wire
|
|
102
|
+
# error, marked for the log line (LLM::HTTP::StreamMarks).
|
|
103
|
+
def read_failure(error, marks, cancel, method, uri)
|
|
104
|
+
if cancel&.cancelled?
|
|
105
|
+
marks.cancelled(cancel.reason)
|
|
106
|
+
return HttpCancelled.new("#{method} #{where(uri)}: cancelled")
|
|
107
|
+
end
|
|
108
|
+
marks.failed(error)
|
|
109
|
+
mapped(error, method, uri)
|
|
110
|
+
end
|
|
111
|
+
|
|
67
112
|
def build_response(response)
|
|
68
|
-
headers
|
|
69
|
-
response.each_header { |name, value| headers[name.downcase] = text(value) }
|
|
70
|
-
Response.new(status: response.code.to_i, headers: headers.freeze, body: text(response.body))
|
|
113
|
+
Response.new(status: response.code.to_i, headers: headers_of(response), body: Http.text(response.body))
|
|
71
114
|
end
|
|
72
115
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
utf8.valid_encoding? ? utf8 : text.force_encoding(Encoding::BINARY)
|
|
116
|
+
def headers_of(response)
|
|
117
|
+
headers = {}
|
|
118
|
+
response.each_header { |name, value| headers[name.downcase] = Http.text(value) }
|
|
119
|
+
headers.freeze
|
|
78
120
|
end
|
|
79
121
|
|
|
80
122
|
def where(uri) = "#{uri.host}:#{uri.port}"
|
|
@@ -40,9 +40,11 @@ module Samagotchi
|
|
|
40
40
|
# Loads installed bundles' plugins (manifest plugin: {file:, sha256:})
|
|
41
41
|
# into an Engine's registries (docs/plugins.md).
|
|
42
42
|
#
|
|
43
|
-
# For each bundle, by name:
|
|
44
|
-
#
|
|
45
|
-
#
|
|
43
|
+
# For each bundle, by name: every file of the plugin (its parts, then
|
|
44
|
+
# the plugin file) must match the sha256 recorded at install
|
|
45
|
+
# (InstalledBundles::Dir#plugin_refusal) and chi must meet the bundle's
|
|
46
|
+
# requires_chi. The parts, in their manifest order, then the file are
|
|
47
|
+
# module_eval'd into one fresh module in the bundle's namespace (#namespace_for), the
|
|
46
48
|
# class named like the file (plugin.rb → Plugin) is built with the
|
|
47
49
|
# bundle's settings, and its #register gets a Plugin::Api. What it
|
|
48
50
|
# registered takes effect only when #register returns: a plugin that
|
|
@@ -85,7 +87,7 @@ module Samagotchi
|
|
|
85
87
|
end
|
|
86
88
|
|
|
87
89
|
# Whether it may load is the verdict's (InstalledBundles::Standing, then
|
|
88
|
-
# Dir#
|
|
90
|
+
# Dir#plugin_refusal); what a refusal does is the loader's: a
|
|
89
91
|
# failure, required for a guardrail plugin.
|
|
90
92
|
# @param standing [InstalledBundles::Standing] the bundle's (built from
|
|
91
93
|
# +bundle+ when not given)
|
|
@@ -100,10 +102,10 @@ module Samagotchi
|
|
|
100
102
|
# (its sha, requires_chi, a raise) denies every tool call.
|
|
101
103
|
required = standing.unreadable? ? unreadable_required?(bundle_name) : bundle.manifest.plugin_required
|
|
102
104
|
into = required ? guardrail_failures || failures : failures
|
|
103
|
-
refusal = standing.refusal || installed.
|
|
105
|
+
refusal = standing.refusal || installed.plugin_refusal(bundle)
|
|
104
106
|
return failed(bundle_name, basename, refusal.text, into, required: required) if refusal
|
|
105
107
|
|
|
106
|
-
api = register_file(bundle_name, file, registries, settings: settings)
|
|
108
|
+
api = register_file(bundle_name, file, registries, settings: settings, parts: installed.plugin_part_paths(bundle))
|
|
107
109
|
warn_unrequired(bundle_name, basename) if api.fail_closed? && !required
|
|
108
110
|
Log.info(TAG, "plugin_loaded", bundle: bundle_name, file: basename, **api.counts)
|
|
109
111
|
true
|
|
@@ -127,15 +129,17 @@ module Samagotchi
|
|
|
127
129
|
# makes them; the test kit loads a bundle's source this way).
|
|
128
130
|
# @param namespace [Module, nil] the module the file is evaluated in
|
|
129
131
|
# (default: a fresh one, #fresh_namespace)
|
|
132
|
+
# @param parts [Array<String>] the plugin's parts' paths, evaluated in
|
|
133
|
+
# this order before +file+, into the same module
|
|
130
134
|
# @param api_class [Class] what #register gets (Api; the test kit's
|
|
131
135
|
# recording subclass)
|
|
132
136
|
# @yieldparam plugin [Object] the built plugin, before #register
|
|
133
137
|
# @return [Api] the committed Api (#counts)
|
|
134
138
|
# @raise [Exception] whatever the file, #register or #commit! raised,
|
|
135
139
|
# after stopping the services it started; nothing is registered
|
|
136
|
-
def register_file(bundle_name, file, registries, settings: {}, namespace: nil, api_class: Api)
|
|
140
|
+
def register_file(bundle_name, file, registries, settings: {}, namespace: nil, api_class: Api, parts: [])
|
|
137
141
|
api = nil
|
|
138
|
-
plugin = instantiate(bundle_name, file, settings, namespace: namespace)
|
|
142
|
+
plugin = instantiate(bundle_name, file, settings, namespace: namespace, parts: parts)
|
|
139
143
|
yield plugin if block_given?
|
|
140
144
|
plugin_label = label(bundle_name, File.basename(file))
|
|
141
145
|
api = api_class.new(bundle: bundle_name, label: plugin_label, registries: registries,
|
|
@@ -181,14 +185,17 @@ module Samagotchi
|
|
|
181
185
|
end
|
|
182
186
|
end
|
|
183
187
|
|
|
184
|
-
# module_eval the file into a fresh
|
|
188
|
+
# module_eval each of +parts+, in order, then the file into a fresh
|
|
189
|
+
# module in the bundle's namespace
|
|
185
190
|
# (Samagotchi::Bundles::<bundle>::PluginLoad<n>: each load gets new
|
|
186
|
-
# classes, not the last load's reopened
|
|
191
|
+
# classes, not the last load's reopened; each eval names its own file,
|
|
192
|
+
# so a backtrace or a SyntaxError names the part) and build its class
|
|
187
193
|
# (plugin.rb → Plugin; my_plugin.rb → MyPlugin) as config hooks are
|
|
188
194
|
# built (Hooks.build_plugin): an initialize that takes an argument
|
|
189
195
|
# gets the settings.
|
|
190
|
-
def instantiate(bundle_name, file, settings, namespace: nil)
|
|
196
|
+
def instantiate(bundle_name, file, settings, namespace: nil, parts: [])
|
|
191
197
|
namespace ||= fresh_namespace(bundle_name)
|
|
198
|
+
parts.each { |part| namespace.module_eval(File.read(part), part, 1) }
|
|
192
199
|
namespace.module_eval(File.read(file), file, 1)
|
|
193
200
|
klass = Hooks.class_for(file, namespace)
|
|
194
201
|
plugin = Hooks.build_plugin(klass, settings)
|