swarm_sdk 2.7.13 → 2.7.15
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/lib/swarm_sdk/agent/builder.rb +25 -0
- data/lib/swarm_sdk/agent/chat.rb +47 -40
- data/lib/swarm_sdk/agent/chat_helpers/llm_configuration.rb +4 -0
- data/lib/swarm_sdk/agent/definition.rb +8 -1
- data/lib/swarm_sdk/agent/system_prompt_builder.rb +15 -3
- data/lib/swarm_sdk/builders/base_builder.rb +5 -0
- data/lib/swarm_sdk/concerns/cleanupable.rb +3 -0
- data/lib/swarm_sdk/config.rb +2 -1
- data/lib/swarm_sdk/configuration/translator.rb +2 -0
- data/lib/swarm_sdk/observer/manager.rb +12 -0
- data/lib/swarm_sdk/prompts/base_system_prompt.md.erb +2 -0
- data/lib/swarm_sdk/result.rb +29 -0
- data/lib/swarm_sdk/ruby_llm_patches/chat_callbacks_patch.rb +27 -22
- data/lib/swarm_sdk/ruby_llm_patches/init.rb +9 -0
- data/lib/swarm_sdk/ruby_llm_patches/mcp_ssl_patch.rb +144 -0
- data/lib/swarm_sdk/ruby_llm_patches/openai_thought_signature_patch.rb +98 -0
- data/lib/swarm_sdk/ruby_llm_patches/streaming_error_patch.rb +50 -0
- data/lib/swarm_sdk/ruby_llm_patches/tool_concurrency_patch.rb +3 -4
- data/lib/swarm_sdk/swarm/all_agents_builder.rb +9 -0
- data/lib/swarm_sdk/swarm/executor.rb +176 -20
- data/lib/swarm_sdk/swarm/hook_triggers.rb +11 -0
- data/lib/swarm_sdk/swarm/logging_callbacks.rb +1 -0
- data/lib/swarm_sdk/swarm/mcp_configurator.rb +20 -0
- data/lib/swarm_sdk/swarm.rb +132 -2
- data/lib/swarm_sdk/version.rb +1 -1
- data/lib/swarm_sdk.rb +3 -0
- metadata +4 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e774808cdf4817c1cd483b3b09972cc1bf8b99d9226308baac8df60315c85ea2
|
|
4
|
+
data.tar.gz: 13a214280887d48dafdfe8b36da95caca7b7788120d92efc71ed3e9cb6da0293
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b0c7654413b29e405fec0fa66ef6e992c2f84a5fdeaf6be6ce52412ca2f3f00cca0d24865141c97d922a7cd73b12ed00fbd3c0118fa48bee78a5846e0bb89fb7
|
|
7
|
+
data.tar.gz: fa812ed602e16954a23f72dec7bd9c1783afe4df852a93aa29db0358db1b329ede8303d7f59b5bdfe0d7847074ed515c534d0e32d5c2e523d3cef67886f61f56
|
|
@@ -63,6 +63,7 @@ module SwarmSDK
|
|
|
63
63
|
@shared_across_delegations = nil # nil = not set (will default to false in Definition)
|
|
64
64
|
@streaming = nil # nil = not set (will use global config default)
|
|
65
65
|
@thinking = nil # nil = not set (extended thinking disabled)
|
|
66
|
+
@disable_environment_info = nil # nil = not set (will default to false in Definition)
|
|
66
67
|
@context_management_config = nil # Context management DSL hooks
|
|
67
68
|
end
|
|
68
69
|
|
|
@@ -529,6 +530,29 @@ module SwarmSDK
|
|
|
529
530
|
!@coding_agent.nil?
|
|
530
531
|
end
|
|
531
532
|
|
|
533
|
+
# Disable environment info (date, platform, OS, working directory) in system prompt
|
|
534
|
+
#
|
|
535
|
+
# When true, omits the environment information section from the agent's system prompt.
|
|
536
|
+
# Defaults to false if not set.
|
|
537
|
+
#
|
|
538
|
+
# @param enabled [Boolean] Whether to disable environment info
|
|
539
|
+
# @return [void]
|
|
540
|
+
#
|
|
541
|
+
# @example
|
|
542
|
+
# disable_environment_info true # Omit environment info from prompt
|
|
543
|
+
def disable_environment_info(enabled)
|
|
544
|
+
@disable_environment_info = enabled
|
|
545
|
+
end
|
|
546
|
+
|
|
547
|
+
# Check if disable_environment_info has been explicitly set
|
|
548
|
+
#
|
|
549
|
+
# Used by Swarm::Builder to determine if all_agents disable_environment_info should apply.
|
|
550
|
+
#
|
|
551
|
+
# @return [Boolean] true if disable_environment_info was explicitly set
|
|
552
|
+
def disable_environment_info_set?
|
|
553
|
+
!@disable_environment_info.nil?
|
|
554
|
+
end
|
|
555
|
+
|
|
532
556
|
# Check if parameters have been set
|
|
533
557
|
#
|
|
534
558
|
# Used by Swarm::Builder for merging all_agents parameters.
|
|
@@ -586,6 +610,7 @@ module SwarmSDK
|
|
|
586
610
|
agent_config[:shared_across_delegations] = @shared_across_delegations unless @shared_across_delegations.nil?
|
|
587
611
|
agent_config[:streaming] = @streaming unless @streaming.nil?
|
|
588
612
|
agent_config[:thinking] = @thinking if @thinking
|
|
613
|
+
agent_config[:disable_environment_info] = @disable_environment_info unless @disable_environment_info.nil?
|
|
589
614
|
|
|
590
615
|
# Convert DSL hooks to HookDefinition format
|
|
591
616
|
agent_config[:hooks] = convert_hooks_to_definitions if @hooks.any?
|
data/lib/swarm_sdk/agent/chat.rb
CHANGED
|
@@ -658,57 +658,63 @@ module SwarmSDK
|
|
|
658
658
|
|
|
659
659
|
# Execute ask without timeout (original ask implementation)
|
|
660
660
|
def execute_ask(prompt, options)
|
|
661
|
-
|
|
661
|
+
@hook_swarm&.mark_agent_active(@agent_name, self)
|
|
662
662
|
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
663
|
+
begin
|
|
664
|
+
is_first = first_message?
|
|
665
|
+
|
|
666
|
+
# Collect system reminders to inject as ephemeral content
|
|
667
|
+
reminders = collect_system_reminders(prompt, is_first)
|
|
668
|
+
|
|
669
|
+
# Trigger user_prompt hook (with clean prompt, not reminders)
|
|
670
|
+
source = options.delete(:source) || "user"
|
|
671
|
+
final_prompt = prompt
|
|
672
|
+
if @hook_executor
|
|
673
|
+
hook_result = trigger_user_prompt(prompt, source: source)
|
|
674
|
+
|
|
675
|
+
if hook_result[:halted]
|
|
676
|
+
return RubyLLM::Message.new(
|
|
677
|
+
role: :assistant,
|
|
678
|
+
content: hook_result[:halt_message],
|
|
679
|
+
model_id: model_id,
|
|
680
|
+
)
|
|
681
|
+
end
|
|
671
682
|
|
|
672
|
-
|
|
673
|
-
return RubyLLM::Message.new(
|
|
674
|
-
role: :assistant,
|
|
675
|
-
content: hook_result[:halt_message],
|
|
676
|
-
model_id: model_id,
|
|
677
|
-
)
|
|
683
|
+
final_prompt = hook_result[:modified_prompt] if hook_result[:modified_prompt]
|
|
678
684
|
end
|
|
679
685
|
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
# Add CLEAN user message to history (no reminders embedded)
|
|
684
|
-
@llm_chat.add_message(role: :user, content: final_prompt)
|
|
686
|
+
# Add CLEAN user message to history (no reminders embedded)
|
|
687
|
+
@llm_chat.add_message(role: :user, content: final_prompt)
|
|
685
688
|
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
# Execute complete() which handles tool loop and ephemeral injection
|
|
693
|
-
response = execute_with_global_semaphore do
|
|
694
|
-
catch(:finish_agent) do
|
|
695
|
-
catch(:finish_swarm) do
|
|
696
|
-
if @streaming_enabled
|
|
697
|
-
# Reset chunk type tracking for new streaming request
|
|
698
|
-
@last_chunk_type = nil
|
|
689
|
+
# Track reminders as ephemeral content for this LLM call only
|
|
690
|
+
# They'll be injected by around_llm_request hook but not stored
|
|
691
|
+
reminders.each do |reminder|
|
|
692
|
+
@context_manager.add_ephemeral_reminder(reminder, messages_array: @llm_chat.messages)
|
|
693
|
+
end
|
|
699
694
|
|
|
700
|
-
|
|
701
|
-
|
|
695
|
+
# Execute complete() which handles tool loop and ephemeral injection
|
|
696
|
+
response = execute_with_global_semaphore do
|
|
697
|
+
catch(:finish_agent) do
|
|
698
|
+
catch(:finish_swarm) do
|
|
699
|
+
if @streaming_enabled
|
|
700
|
+
# Reset chunk type tracking for new streaming request
|
|
701
|
+
@last_chunk_type = nil
|
|
702
|
+
|
|
703
|
+
@llm_chat.complete(**options) do |chunk|
|
|
704
|
+
emit_content_chunk(chunk)
|
|
705
|
+
end
|
|
706
|
+
else
|
|
707
|
+
@llm_chat.complete(**options)
|
|
702
708
|
end
|
|
703
|
-
else
|
|
704
|
-
@llm_chat.complete(**options)
|
|
705
709
|
end
|
|
706
710
|
end
|
|
707
711
|
end
|
|
708
|
-
end
|
|
709
712
|
|
|
710
|
-
|
|
711
|
-
|
|
713
|
+
# Handle finish markers from hooks
|
|
714
|
+
handle_finish_marker(response)
|
|
715
|
+
ensure
|
|
716
|
+
@hook_swarm&.mark_agent_inactive(@agent_name)
|
|
717
|
+
end
|
|
712
718
|
end
|
|
713
719
|
|
|
714
720
|
# --- Tool Execution Hook ---
|
|
@@ -1134,6 +1140,7 @@ module SwarmSDK
|
|
|
1134
1140
|
error_message: error.message,
|
|
1135
1141
|
status_code: error.respond_to?(:response) ? error.response&.status : nil,
|
|
1136
1142
|
retryable: false,
|
|
1143
|
+
backtrace: error.backtrace&.first(20),
|
|
1137
1144
|
)
|
|
1138
1145
|
end
|
|
1139
1146
|
|
|
@@ -155,6 +155,10 @@ module SwarmSDK
|
|
|
155
155
|
when "ollama"
|
|
156
156
|
config.ollama_api_base = base_url
|
|
157
157
|
# Ollama doesn't need an API key
|
|
158
|
+
when "gemini"
|
|
159
|
+
config.gemini_api_base = base_url
|
|
160
|
+
api_key = SwarmSDK.config.gemini_api_key
|
|
161
|
+
config.gemini_api_key = api_key if api_key
|
|
158
162
|
when "gpustack"
|
|
159
163
|
config.gpustack_api_base = base_url
|
|
160
164
|
api_key = SwarmSDK.config.gpustack_api_key
|
|
@@ -43,7 +43,8 @@ module SwarmSDK
|
|
|
43
43
|
:plugin_configs,
|
|
44
44
|
:shared_across_delegations,
|
|
45
45
|
:streaming,
|
|
46
|
-
:thinking
|
|
46
|
+
:thinking,
|
|
47
|
+
:disable_environment_info
|
|
47
48
|
|
|
48
49
|
attr_accessor :bypass_permissions, :max_concurrent_tools
|
|
49
50
|
|
|
@@ -118,6 +119,9 @@ module SwarmSDK
|
|
|
118
119
|
# Extended thinking configuration (nil = disabled)
|
|
119
120
|
@thinking = config[:thinking]
|
|
120
121
|
|
|
122
|
+
# When true, omits date/platform/OS/working directory from system prompts
|
|
123
|
+
@disable_environment_info = config.fetch(:disable_environment_info, false)
|
|
124
|
+
|
|
121
125
|
# Build system prompt after directory and memory are set
|
|
122
126
|
@system_prompt = build_full_system_prompt(config[:system_prompt])
|
|
123
127
|
|
|
@@ -201,6 +205,7 @@ module SwarmSDK
|
|
|
201
205
|
hooks: @hooks,
|
|
202
206
|
shared_across_delegations: @shared_across_delegations,
|
|
203
207
|
streaming: @streaming,
|
|
208
|
+
disable_environment_info: @disable_environment_info,
|
|
204
209
|
# Permissions are core SDK functionality (not plugin-specific)
|
|
205
210
|
default_permissions: @default_permissions,
|
|
206
211
|
permissions: @agent_permissions,
|
|
@@ -301,6 +306,7 @@ module SwarmSDK
|
|
|
301
306
|
disable_default_tools: @disable_default_tools,
|
|
302
307
|
directory: @directory,
|
|
303
308
|
definition: self,
|
|
309
|
+
disable_environment_info: @disable_environment_info,
|
|
304
310
|
)
|
|
305
311
|
end
|
|
306
312
|
|
|
@@ -394,6 +400,7 @@ module SwarmSDK
|
|
|
394
400
|
:shared_across_delegations,
|
|
395
401
|
:streaming,
|
|
396
402
|
:directories,
|
|
403
|
+
:disable_environment_info,
|
|
397
404
|
]
|
|
398
405
|
|
|
399
406
|
config.reject { |k, _| standard_keys.include?(k.to_sym) }
|
|
@@ -28,24 +28,29 @@ module SwarmSDK
|
|
|
28
28
|
# @param disable_default_tools [Boolean, Array, nil] Default tools disable configuration
|
|
29
29
|
# @param directory [String] Agent's working directory
|
|
30
30
|
# @param definition [Definition] Full definition for plugin contributions
|
|
31
|
+
# @param disable_environment_info [Boolean] Whether to omit environment info from prompt
|
|
31
32
|
# @return [String] Complete system prompt
|
|
32
|
-
def build(custom_prompt:, coding_agent:, disable_default_tools:, directory:, definition
|
|
33
|
+
def build(custom_prompt:, coding_agent:, disable_default_tools:, directory:, definition:,
|
|
34
|
+
disable_environment_info: false)
|
|
33
35
|
new(
|
|
34
36
|
custom_prompt: custom_prompt,
|
|
35
37
|
coding_agent: coding_agent,
|
|
36
38
|
disable_default_tools: disable_default_tools,
|
|
37
39
|
directory: directory,
|
|
38
40
|
definition: definition,
|
|
41
|
+
disable_environment_info: disable_environment_info,
|
|
39
42
|
).build
|
|
40
43
|
end
|
|
41
44
|
end
|
|
42
45
|
|
|
43
|
-
def initialize(custom_prompt:, coding_agent:, disable_default_tools:, directory:, definition
|
|
46
|
+
def initialize(custom_prompt:, coding_agent:, disable_default_tools:, directory:, definition:,
|
|
47
|
+
disable_environment_info: false)
|
|
44
48
|
@custom_prompt = custom_prompt
|
|
45
49
|
@coding_agent = coding_agent
|
|
46
50
|
@disable_default_tools = disable_default_tools
|
|
47
51
|
@directory = directory
|
|
48
52
|
@definition = definition
|
|
53
|
+
@disable_environment_info = disable_environment_info
|
|
49
54
|
end
|
|
50
55
|
|
|
51
56
|
def build
|
|
@@ -80,7 +85,11 @@ module SwarmSDK
|
|
|
80
85
|
non_coding_base = render_non_coding_base_prompt
|
|
81
86
|
|
|
82
87
|
if @custom_prompt && !@custom_prompt.strip.empty?
|
|
83
|
-
|
|
88
|
+
if non_coding_base.empty?
|
|
89
|
+
@custom_prompt.to_s
|
|
90
|
+
else
|
|
91
|
+
"#{non_coding_base}\n\n#{@custom_prompt}"
|
|
92
|
+
end
|
|
84
93
|
else
|
|
85
94
|
non_coding_base
|
|
86
95
|
end
|
|
@@ -91,6 +100,7 @@ module SwarmSDK
|
|
|
91
100
|
end
|
|
92
101
|
|
|
93
102
|
def render_base_system_prompt
|
|
103
|
+
disable_environment_info = @disable_environment_info
|
|
94
104
|
cwd = @directory || Dir.pwd
|
|
95
105
|
platform = RUBY_PLATFORM
|
|
96
106
|
os_version = begin
|
|
@@ -105,6 +115,8 @@ module SwarmSDK
|
|
|
105
115
|
end
|
|
106
116
|
|
|
107
117
|
def render_non_coding_base_prompt
|
|
118
|
+
return "" if @disable_environment_info
|
|
119
|
+
|
|
108
120
|
cwd = @directory || Dir.pwd
|
|
109
121
|
platform = RUBY_PLATFORM
|
|
110
122
|
os_version = begin
|
|
@@ -265,6 +265,7 @@ module SwarmSDK
|
|
|
265
265
|
builder.parameters(config[:parameters]) if config[:parameters]
|
|
266
266
|
builder.headers(config[:headers]) if config[:headers]
|
|
267
267
|
builder.coding_agent(config[:coding_agent]) unless config[:coding_agent].nil?
|
|
268
|
+
builder.disable_environment_info(config[:disable_environment_info]) unless config[:disable_environment_info].nil?
|
|
268
269
|
builder.bypass_permissions(config[:bypass_permissions]) if config[:bypass_permissions]
|
|
269
270
|
builder.disable_default_tools(config[:disable_default_tools]) unless config[:disable_default_tools].nil?
|
|
270
271
|
|
|
@@ -451,6 +452,10 @@ module SwarmSDK
|
|
|
451
452
|
agent_builder.streaming(all_agents_hash[:streaming])
|
|
452
453
|
end
|
|
453
454
|
|
|
455
|
+
if !all_agents_hash[:disable_environment_info].nil? && !agent_builder.disable_environment_info_set?
|
|
456
|
+
agent_builder.disable_environment_info(all_agents_hash[:disable_environment_info])
|
|
457
|
+
end
|
|
458
|
+
|
|
454
459
|
if all_agents_hash[:thinking] && !agent_builder.thinking_set?
|
|
455
460
|
agent_builder.thinking(**all_agents_hash[:thinking])
|
|
456
461
|
end
|
data/lib/swarm_sdk/config.rb
CHANGED
|
@@ -105,6 +105,7 @@ module SwarmSDK
|
|
|
105
105
|
allow_filesystem_tools: ["SWARM_SDK_ALLOW_FILESYSTEM_TOOLS", true],
|
|
106
106
|
env_interpolation: ["SWARM_SDK_ENV_INTERPOLATION", true],
|
|
107
107
|
streaming: ["SWARM_SDK_STREAMING", true],
|
|
108
|
+
mcp_ssl_verify: ["SWARM_SDK_MCP_SSL_VERIFY", true],
|
|
108
109
|
}.freeze
|
|
109
110
|
|
|
110
111
|
class << self
|
|
@@ -345,7 +346,7 @@ module SwarmSDK
|
|
|
345
346
|
# @return [Integer, Float, Boolean, String] The parsed value
|
|
346
347
|
def parse_env_value(value, key)
|
|
347
348
|
case key
|
|
348
|
-
when :allow_filesystem_tools, :env_interpolation, :streaming
|
|
349
|
+
when :allow_filesystem_tools, :env_interpolation, :streaming, :mcp_ssl_verify
|
|
349
350
|
# Convert string to boolean
|
|
350
351
|
case value.to_s.downcase
|
|
351
352
|
when "true", "yes", "1", "on", "enabled"
|
|
@@ -100,6 +100,7 @@ module SwarmSDK
|
|
|
100
100
|
coding_agent(all_agents_cfg[:coding_agent]) unless all_agents_cfg[:coding_agent].nil?
|
|
101
101
|
disable_default_tools(all_agents_cfg[:disable_default_tools]) unless all_agents_cfg[:disable_default_tools].nil?
|
|
102
102
|
streaming(all_agents_cfg[:streaming]) unless all_agents_cfg[:streaming].nil?
|
|
103
|
+
disable_environment_info(all_agents_cfg[:disable_environment_info]) unless all_agents_cfg[:disable_environment_info].nil?
|
|
103
104
|
thinking(**all_agents_cfg[:thinking]) if all_agents_cfg[:thinking]
|
|
104
105
|
|
|
105
106
|
if all_agents_hks.any?
|
|
@@ -165,6 +166,7 @@ module SwarmSDK
|
|
|
165
166
|
disable_default_tools(config[:disable_default_tools]) unless config[:disable_default_tools].nil?
|
|
166
167
|
shared_across_delegations(config[:shared_across_delegations]) unless config[:shared_across_delegations].nil?
|
|
167
168
|
streaming(config[:streaming]) unless config[:streaming].nil?
|
|
169
|
+
disable_environment_info(config[:disable_environment_info]) unless config[:disable_environment_info].nil?
|
|
168
170
|
thinking(**config[:thinking]) if config[:thinking]
|
|
169
171
|
|
|
170
172
|
if config[:tools]&.any?
|
|
@@ -75,6 +75,18 @@ module SwarmSDK
|
|
|
75
75
|
end
|
|
76
76
|
end
|
|
77
77
|
|
|
78
|
+
# Stop all observer tasks immediately
|
|
79
|
+
#
|
|
80
|
+
# Interrupts in-flight observer LLM calls by stopping the barrier.
|
|
81
|
+
# Called during swarm interruption instead of wait_for_completion.
|
|
82
|
+
#
|
|
83
|
+
# @return [void]
|
|
84
|
+
def stop
|
|
85
|
+
@barrier&.stop
|
|
86
|
+
rescue StandardError => e
|
|
87
|
+
RubyLLM.logger.debug("SwarmSDK: Error stopping observer barrier: #{e.message}")
|
|
88
|
+
end
|
|
89
|
+
|
|
78
90
|
# Cleanup all subscriptions
|
|
79
91
|
#
|
|
80
92
|
# Unsubscribes from LogCollector to prevent memory leaks.
|
|
@@ -90,6 +90,7 @@ NEVER commit changes unless the user explicitly asks you to. It is VERY IMPORTAN
|
|
|
90
90
|
You MUST answer concisely with fewer than 4 lines of text (not including tool use or code generation), unless user asks for detail.
|
|
91
91
|
|
|
92
92
|
|
|
93
|
+
<% unless disable_environment_info %>
|
|
93
94
|
# Today's date
|
|
94
95
|
|
|
95
96
|
<today-date>
|
|
@@ -104,6 +105,7 @@ Working directory: <%= cwd %>
|
|
|
104
105
|
Platform: <%= platform %>
|
|
105
106
|
OS Version: <%= os_version %>
|
|
106
107
|
</env>
|
|
108
|
+
<% end %>
|
|
107
109
|
|
|
108
110
|
IMPORTANT: Assist with defensive security tasks only. Refuse to create, modify, or improve code that may be used maliciously. Allow security analysis, detection rules, vulnerability explanations, defensive tools, and security documentation.
|
|
109
111
|
|
data/lib/swarm_sdk/result.rb
CHANGED
|
@@ -23,6 +23,35 @@ module SwarmSDK
|
|
|
23
23
|
!success?
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
+
# Check if execution was interrupted via swarm.stop
|
|
27
|
+
#
|
|
28
|
+
# @return [Boolean] true if execution was interrupted
|
|
29
|
+
#
|
|
30
|
+
# @example
|
|
31
|
+
# result = swarm.execute("Build auth")
|
|
32
|
+
# result.interrupted? # => false
|
|
33
|
+
#
|
|
34
|
+
# # After calling swarm.stop during execution:
|
|
35
|
+
# result.interrupted? # => true
|
|
36
|
+
def interrupted?
|
|
37
|
+
@metadata[:interrupted] == true
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Get the reason execution finished
|
|
41
|
+
#
|
|
42
|
+
# Possible values:
|
|
43
|
+
# - "finished" - Normal completion
|
|
44
|
+
# - "interrupted" - Stopped via swarm.stop
|
|
45
|
+
# - "error" - Execution failed with an error
|
|
46
|
+
#
|
|
47
|
+
# @return [String] The finish reason
|
|
48
|
+
#
|
|
49
|
+
# @example
|
|
50
|
+
# result.finish_reason # => "finished"
|
|
51
|
+
def finish_reason
|
|
52
|
+
@metadata[:finish_reason] || (success? ? "finished" : "error")
|
|
53
|
+
end
|
|
54
|
+
|
|
26
55
|
# Calculate total cost from logs
|
|
27
56
|
#
|
|
28
57
|
# Delegates to total_cost for consistency. This attribute is calculated
|
|
@@ -153,30 +153,34 @@ module RubyLLM
|
|
|
153
153
|
end
|
|
154
154
|
end
|
|
155
155
|
|
|
156
|
-
# Override complete to use emit() and support around_llm_request hook
|
|
157
|
-
#
|
|
156
|
+
# Override complete to use emit() and support around_llm_request hook.
|
|
157
|
+
# Uses a trampoline loop instead of mutual recursion with handle_tool_calls
|
|
158
|
+
# to avoid stack growth during multi-round tool-call conversations.
|
|
158
159
|
def complete(&block)
|
|
159
|
-
|
|
160
|
-
|
|
160
|
+
loop do
|
|
161
|
+
response = execute_llm_request(&block)
|
|
161
162
|
|
|
162
|
-
|
|
163
|
+
emit(:new_message) unless block_given?
|
|
163
164
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
165
|
+
if @schema && response.content.is_a?(String)
|
|
166
|
+
begin
|
|
167
|
+
response.content = JSON.parse(response.content)
|
|
168
|
+
rescue JSON::ParserError
|
|
169
|
+
# If parsing fails, keep content as string
|
|
170
|
+
end
|
|
169
171
|
end
|
|
170
|
-
end
|
|
171
172
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
173
|
+
add_message(response)
|
|
174
|
+
emit(:end_message, response)
|
|
175
|
+
|
|
176
|
+
if response.tool_call?
|
|
177
|
+
halt_result = handle_tool_calls(response, &block)
|
|
178
|
+
return halt_result if halt_result
|
|
179
|
+
|
|
180
|
+
# Loop continues: next LLM call with zero stack growth
|
|
181
|
+
else
|
|
182
|
+
return response
|
|
183
|
+
end
|
|
180
184
|
end
|
|
181
185
|
end
|
|
182
186
|
|
|
@@ -238,8 +242,9 @@ module RubyLLM
|
|
|
238
242
|
end
|
|
239
243
|
end
|
|
240
244
|
|
|
241
|
-
#
|
|
242
|
-
|
|
245
|
+
# Execute tool calls and return halt result (or nil to continue the loop).
|
|
246
|
+
# Does NOT recurse back into complete() — the trampoline loop handles that.
|
|
247
|
+
def handle_tool_calls(response, &_block)
|
|
243
248
|
halt_result = nil
|
|
244
249
|
|
|
245
250
|
response.tool_calls.each_value do |tool_call|
|
|
@@ -259,7 +264,7 @@ module RubyLLM
|
|
|
259
264
|
halt_result = result if result.is_a?(Tool::Halt)
|
|
260
265
|
end
|
|
261
266
|
|
|
262
|
-
halt_result
|
|
267
|
+
halt_result
|
|
263
268
|
end
|
|
264
269
|
|
|
265
270
|
# Execute tool with around_tool_execution hook if set
|
|
@@ -39,3 +39,12 @@ require_relative "message_management_patch"
|
|
|
39
39
|
|
|
40
40
|
# 7. Responses API patch (depends on configuration, uses error classes)
|
|
41
41
|
require_relative "responses_api_patch"
|
|
42
|
+
|
|
43
|
+
# 8. Streaming error patch (hardens error parsing for non-standard proxy responses)
|
|
44
|
+
require_relative "streaming_error_patch"
|
|
45
|
+
|
|
46
|
+
# 9. OpenAI thought_signature patch (preserves Gemini thought signatures through OpenAI provider)
|
|
47
|
+
require_relative "openai_thought_signature_patch"
|
|
48
|
+
|
|
49
|
+
# 10. MCP SSL patch (configures SSL for HTTPX connections in ruby_llm-mcp)
|
|
50
|
+
require_relative "mcp_ssl_patch"
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Patches ruby_llm-mcp HTTPX connections to configure SSL verification
|
|
4
|
+
#
|
|
5
|
+
# OpenSSL 3.6 enforces CRL (Certificate Revocation List) checking by default.
|
|
6
|
+
# Most certificates don't provide accessible CRL endpoints (industry moved to OCSP),
|
|
7
|
+
# which breaks HTTPS MCP connections with:
|
|
8
|
+
# "certificate verify failed (unable to get certificate CRL)"
|
|
9
|
+
#
|
|
10
|
+
# This patch injects SSL options into all 5 HTTPX connection creation paths:
|
|
11
|
+
#
|
|
12
|
+
# Via HTTPClient.build_connection (paths 1-3):
|
|
13
|
+
# 1. StreamableHTTP#create_connection
|
|
14
|
+
# 2. StreamableHTTP#create_connection_with_streaming_callbacks
|
|
15
|
+
# 3. SSE#send_request
|
|
16
|
+
#
|
|
17
|
+
# Direct HTTPX.plugin calls (paths 4-5):
|
|
18
|
+
# 4. StreamableHTTP#create_connection_with_sse_callbacks
|
|
19
|
+
# 5. SSE#create_sse_client
|
|
20
|
+
#
|
|
21
|
+
# Default: VERIFY_PEER (validates cert chain without CRL checking)
|
|
22
|
+
# Configurable: VERIFY_NONE for local development via SwarmSDK.config.mcp_ssl_verify
|
|
23
|
+
|
|
24
|
+
require "openssl"
|
|
25
|
+
|
|
26
|
+
module SwarmSDK
|
|
27
|
+
# Module-level SSL configuration for MCP HTTPX connections
|
|
28
|
+
#
|
|
29
|
+
# Set ssl_options before creating MCP clients. The patched HTTPX methods
|
|
30
|
+
# read from this accessor to configure SSL on every connection.
|
|
31
|
+
#
|
|
32
|
+
# @example Default (validates cert chain, skips CRL)
|
|
33
|
+
# McpSslPatch.ssl_options #=> { verify_mode: OpenSSL::SSL::VERIFY_PEER }
|
|
34
|
+
#
|
|
35
|
+
# @example Disable SSL verification (local dev only)
|
|
36
|
+
# McpSslPatch.ssl_options = { verify_mode: OpenSSL::SSL::VERIFY_NONE }
|
|
37
|
+
module McpSslPatch
|
|
38
|
+
@ssl_options = { verify_mode: OpenSSL::SSL::VERIFY_PEER }
|
|
39
|
+
|
|
40
|
+
class << self
|
|
41
|
+
# @return [Hash] SSL options hash passed to HTTPX .with(ssl: ...)
|
|
42
|
+
attr_accessor :ssl_options
|
|
43
|
+
|
|
44
|
+
# Clear the thread-local HTTPX connection cache
|
|
45
|
+
#
|
|
46
|
+
# Must be called after changing ssl_options so that HTTPClient.build_connection
|
|
47
|
+
# runs again with the updated options.
|
|
48
|
+
#
|
|
49
|
+
# @return [void]
|
|
50
|
+
def reset_connection!
|
|
51
|
+
Thread.current[:ruby_llm_mcp_client_connection] = nil
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Patch 1: HTTPClient.build_connection
|
|
58
|
+
#
|
|
59
|
+
# Covers paths 1-3: StreamableHTTP#create_connection,
|
|
60
|
+
# StreamableHTTP#create_connection_with_streaming_callbacks, SSE#send_request
|
|
61
|
+
#
|
|
62
|
+
# These all chain from HTTPClient.connection which calls build_connection.
|
|
63
|
+
module RubyLLM
|
|
64
|
+
module MCP
|
|
65
|
+
module Transports
|
|
66
|
+
module Support
|
|
67
|
+
class HTTPClient
|
|
68
|
+
class << self
|
|
69
|
+
def build_connection
|
|
70
|
+
HTTPX.with(
|
|
71
|
+
pool_options: {
|
|
72
|
+
max_connections: RubyLLM::MCP.config.max_connections,
|
|
73
|
+
pool_timeout: RubyLLM::MCP.config.pool_timeout,
|
|
74
|
+
},
|
|
75
|
+
ssl: SwarmSDK::McpSslPatch.ssl_options,
|
|
76
|
+
)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Patch 2: StreamableHTTP#create_connection_with_sse_callbacks
|
|
86
|
+
#
|
|
87
|
+
# This method calls HTTPX.plugin(:callbacks) directly, bypassing HTTPClient.
|
|
88
|
+
# Merges SSL options with ALPN protocol when version is :http1.
|
|
89
|
+
module SwarmSDK
|
|
90
|
+
module McpSslPatch
|
|
91
|
+
module StreamableHttpSslPatch
|
|
92
|
+
private
|
|
93
|
+
|
|
94
|
+
def create_connection_with_sse_callbacks(options, headers)
|
|
95
|
+
client = HTTPX.plugin(:callbacks)
|
|
96
|
+
client = add_on_response_body_chunk_callback(client, options)
|
|
97
|
+
|
|
98
|
+
ssl = SwarmSDK::McpSslPatch.ssl_options.dup
|
|
99
|
+
ssl[:alpn_protocols] = ["http/1.1"] if @version == :http1
|
|
100
|
+
|
|
101
|
+
client = client.with(
|
|
102
|
+
timeout: {
|
|
103
|
+
connect_timeout: 10,
|
|
104
|
+
read_timeout: @request_timeout / 1000,
|
|
105
|
+
write_timeout: @request_timeout / 1000,
|
|
106
|
+
operation_timeout: @request_timeout / 1000,
|
|
107
|
+
},
|
|
108
|
+
headers: headers,
|
|
109
|
+
ssl: ssl,
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
register_client(client)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Patch 3: SSE#create_sse_client
|
|
119
|
+
#
|
|
120
|
+
# This method calls HTTPX.plugin(:stream) directly, bypassing HTTPClient.
|
|
121
|
+
# Merges SSL options with ALPN protocol when version is :http1.
|
|
122
|
+
module SwarmSDK
|
|
123
|
+
module McpSslPatch
|
|
124
|
+
module SseSslPatch
|
|
125
|
+
private
|
|
126
|
+
|
|
127
|
+
def create_sse_client
|
|
128
|
+
stream_headers = build_request_headers
|
|
129
|
+
|
|
130
|
+
ssl = SwarmSDK::McpSslPatch.ssl_options.dup
|
|
131
|
+
ssl[:alpn_protocols] = ["http/1.1"] if @version == :http1
|
|
132
|
+
|
|
133
|
+
HTTPX.plugin(:stream).with(
|
|
134
|
+
headers: stream_headers,
|
|
135
|
+
ssl: ssl,
|
|
136
|
+
)
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Apply prepend patches for direct HTTPX.plugin calls
|
|
143
|
+
RubyLLM::MCP::Transports::StreamableHTTP.prepend(SwarmSDK::McpSslPatch::StreamableHttpSslPatch)
|
|
144
|
+
RubyLLM::MCP::Transports::SSE.prepend(SwarmSDK::McpSslPatch::SseSslPatch)
|