scout-ai 2.0.0 → 2.2.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/.vimproject +135 -55
- data/README.md +2 -1
- data/VERSION +1 -1
- data/doc/Improvements.md +490 -148
- data/doc/Model.md +53 -10
- data/doc/StartHere.md +44 -19
- data/doc/developer/Architecture.md +25 -6
- data/doc/developer/Backends.md +207 -48
- data/doc/developer/ChatLifecycle.md +121 -20
- data/doc/developer/DelegationInternals.md +320 -34
- data/doc/developer/DesignPrinciples.md +205 -16
- data/doc/developer/PathEngine.md +628 -0
- data/doc/developer/PromptProcessing.md +332 -82
- data/doc/developer/Provenance.md +402 -44
- data/doc/developer/Sandbox.md +213 -0
- data/doc/user/BuildingAgents.md +130 -18
- data/doc/user/Cookbook.md +29 -8
- data/doc/user/CoreConcepts.md +7 -6
- data/doc/user/Delegation.md +166 -15
- data/doc/user/GettingStarted.md +20 -16
- data/doc/user/ManagingContext.md +155 -17
- data/doc/user/MultiAgentWorkflows.md +105 -15
- data/doc/user/Python.md +11 -1
- data/doc/user/RunningInference.md +90 -47
- data/doc/user/ToolCalling.md +134 -29
- data/doc/user/WritingChats.md +56 -27
- data/lib/scout/llm/agent/attach.rb +64 -0
- data/lib/scout/llm/agent/chat.rb +32 -0
- data/lib/scout/llm/agent/construction.rb +64 -0
- data/lib/scout/llm/agent/conversation.rb +304 -0
- data/lib/scout/llm/agent/delegate.rb +56 -238
- data/lib/scout/llm/agent/path/edit.rb +230 -0
- data/lib/scout/llm/agent/path/patch.rb +252 -0
- data/lib/scout/llm/agent/path/workflow_kinds.rb +175 -0
- data/lib/scout/llm/agent/path.rb +1777 -0
- data/lib/scout/llm/agent/save.rb +11 -9
- data/lib/scout/llm/agent/workflow.rb +79 -17
- data/lib/scout/llm/agent.rb +151 -27
- data/lib/scout/llm/ask.rb +37 -27
- data/lib/scout/llm/backends/anthropic.rb +1 -1
- data/lib/scout/llm/backends/bedrock.rb +3 -2
- data/lib/scout/llm/backends/default.rb +131 -32
- data/lib/scout/llm/backends/glm.rb +28 -1
- data/lib/scout/llm/backends/huggingface.rb +1 -1
- data/lib/scout/llm/backends/ollama.rb +1 -1
- data/lib/scout/llm/backends/openai.rb +7 -6
- data/lib/scout/llm/backends/openwebui.rb +14 -7
- data/lib/scout/llm/backends/relay.rb +8 -2
- data/lib/scout/llm/chat/agent_meta.rb +24 -60
- data/lib/scout/llm/chat/parse.rb +1 -1
- data/lib/scout/llm/chat/process/files.rb +10 -0
- data/lib/scout/llm/chat/process/meta.rb +5 -7
- data/lib/scout/llm/chat/process/tools.rb +104 -52
- data/lib/scout/llm/chat/prompt/epoch_tool_compactation.rb +355 -0
- data/lib/scout/llm/chat/prompt/inbox.rb +188 -0
- data/lib/scout/llm/chat/prompt/shorten_tools.rb +797 -13
- data/lib/scout/llm/chat/prompt/shorten_tools_epoch.rb +1 -365
- data/lib/scout/llm/chat/prompt/shorten_tools_epoch_increment.rb +1 -0
- data/lib/scout/llm/chat/prompt.rb +24 -10
- data/lib/scout/llm/chat/provenance/live.rb +1080 -0
- data/lib/scout/llm/chat/provenance/tokens.rb +386 -0
- data/lib/scout/llm/chat/provenance.rb +352 -626
- data/lib/scout/llm/chat.rb +8 -2
- data/lib/scout/llm/embed.rb +15 -3
- data/lib/scout/llm/image.rb +4 -1
- data/lib/scout/llm/mcp.rb +4 -2
- data/lib/scout/llm/request_context.rb +234 -0
- data/lib/scout/llm/sandbox.rb +263 -0
- data/lib/scout/llm/tools/call.rb +163 -42
- data/lib/scout/llm/tools/definition.rb +48 -0
- data/lib/scout/llm/tools/knowledge_base.rb +1 -0
- data/lib/scout/llm/tools/mcp.rb +50 -43
- data/lib/scout/llm/tools/workflow.rb +90 -20
- data/lib/scout/llm/tools.rb +14 -41
- data/lib/scout/model/decision/base.rb +43 -0
- data/lib/scout/model/decision/jev.rb +161 -0
- data/lib/scout/model/python/torch.rb +1 -1
- data/research/clear-tools-inherit.md +171 -0
- data/research/path-engine-dc1-dc2-dc3-fixes.md +279 -0
- data/research/path-kind-chat-role.md +96 -0
- data/research/path-kind-contract-fixes-engine.md +307 -0
- data/research/scout-ai-runtime-availability.md +65 -0
- data/research/subsys/README.md +127 -0
- data/research/subsys/agent-delegation.md +201 -0
- data/research/subsys/agent-society.md +275 -0
- data/research/subsys/agentworkflow.md +233 -0
- data/research/subsys/backends.md +221 -0
- data/research/subsys/chat.md +314 -0
- data/research/subsys/embed-image.md +244 -0
- data/research/subsys/entry-cli.md +253 -0
- data/research/subsys/model-ml.md +326 -0
- data/research/subsys/provenance.md +290 -0
- data/research/subsys/tools.md +334 -0
- data/scout-ai.gemspec +72 -26
- data/scout_commands/agent/ask +8 -7
- data/scout_commands/llm/abridged +40 -0
- data/scout_commands/llm/ask +9 -9
- data/scout_commands/llm/md +2 -0
- data/scout_commands/llm/prov +845 -80
- data/scout_commands/llm/word +4 -0
- data/share/word/reference.docx +0 -0
- data/test/integration/scout/model/decision/test_jev.rb +144 -0
- data/test/scout/llm/agent/test_attach.rb +117 -0
- data/test/scout/llm/agent/test_conversation.rb +407 -0
- data/test/scout/llm/agent/test_delegate.rb +209 -0
- data/test/scout/llm/agent/test_path.rb +1812 -0
- data/test/scout/llm/agent/test_path_edit.rb +908 -0
- data/test/scout/llm/agent/test_path_errors.rb +390 -0
- data/test/scout/llm/agent/test_path_filesystem.rb +532 -0
- data/test/scout/llm/agent/test_path_patch.rb +233 -0
- data/test/scout/llm/agent/test_path_promotion.rb +386 -0
- data/test/scout/llm/agent/test_path_workflow_integration.rb +665 -0
- data/test/scout/llm/agent/test_save.rb +70 -15
- data/test/scout/llm/agent/test_workflow.rb +130 -4
- data/test/scout/llm/backends/test_anthropic.rb +4 -4
- data/test/scout/llm/backends/test_bedrock.rb +4 -4
- data/test/scout/llm/backends/test_format_other.rb +43 -0
- data/test/scout/llm/backends/test_ollama.rb +2 -2
- data/test/scout/llm/backends/test_openwebui.rb +12 -8
- data/test/scout/llm/backends/test_reasoning.rb +114 -0
- data/test/scout/llm/chat/agent_meta_fixtures.rb +109 -22
- data/test/scout/llm/chat/prompt/test_epoch_tool_compactation.rb +612 -0
- data/test/scout/llm/chat/prov_plot_fixtures.rb +131 -0
- data/test/scout/llm/chat/test_agent_meta.rb +40 -80
- data/test/scout/llm/chat/test_agent_meta_provenance.rb +200 -28
- data/test/scout/llm/chat/test_agent_meta_tokens.rb +112 -7
- data/test/scout/llm/chat/test_agent_view.rb +178 -0
- data/test/scout/llm/chat/test_live_report.rb +282 -0
- data/test/scout/llm/chat/test_live_workload.rb +193 -0
- data/test/scout/llm/chat/test_parse.rb +32 -2
- data/test/scout/llm/chat/test_prompt.rb +411 -0
- data/test/scout/llm/chat/test_prov_cli.rb +708 -13
- data/test/scout/llm/chat/test_prov_cli_live.rb +152 -0
- data/test/scout/llm/chat/test_prov_plot.rb +213 -0
- data/test/scout/llm/chat/test_prov_timeline.rb +355 -0
- data/test/scout/llm/chat/test_provenance.rb +67 -22
- data/test/scout/llm/chat/test_provenance_index.rb +479 -0
- data/test/scout/llm/chat/test_society_view.rb +159 -0
- data/test/scout/llm/test_agent.rb +1 -1
- data/test/scout/llm/test_embed.rb +103 -1
- data/test/scout/llm/test_request_context.rb +299 -0
- data/test/scout/llm/test_sandbox.rb +249 -0
- data/test/scout/llm/tools/test_agent_pairing.rb +150 -0
- data/test/scout/llm/tools/test_call_delegation_conflict.rb +316 -0
- data/test/scout/llm/tools/test_definition.rb +332 -0
- data/test/scout/llm/tools/test_jobs_file.rb +211 -0
- data/test/scout/llm/tools/test_mcp.rb +37 -8
- data/test/scout/llm/tools/test_workflow.rb +45 -1
- data/test/scout/model/decision/test_jev.rb +453 -0
- data/test/scout/model/python/huggingface/causal/test_next_token.rb +7 -10
- data/test/scout/model/python/huggingface/test_causal.rb +11 -2
- data/test/scout/model/python/huggingface/test_classification.rb +5 -3
- data/test/scout/model/python/test_availability.rb +116 -0
- data/test/scout/model/python/test_torch.rb +54 -2
- data/test/support/availability.rb +72 -174
- data/test/support/fake_clients.rb +18 -9
- data/test/support/fixtures.rb +75 -6
- data/test/support/mock_backend.rb +9 -1
- data/test/support/test_fixtures.rb +156 -0
- data/test/test_helper.rb +14 -4
- metadata +71 -25
- data/research/ChatAnalyst-required-changes.md +0 -167
- data/research/agent-delegation-analysis.md +0 -810
- data/research/agent-meta-provenance-integration-plan.md +0 -622
- data/research/agent-workflow-analysis.md +0 -1120
- data/research/backends-analysis.md +0 -836
- data/research/chat-core-analysis.md +0 -946
- data/research/chatanalyst-provenance/00-baseline.md +0 -30
- data/research/chatanalyst-provenance/01-repo-map.md +0 -60
- data/research/chatanalyst-provenance/02-event-reconstruction.md +0 -55
- data/research/chatanalyst-provenance/03-duplication-evidence.md +0 -45
- data/research/chatanalyst-provenance/04-tooling-root-cause.md +0 -57
- data/research/chatanalyst-provenance/05-fix-plan.md +0 -46
- data/research/chatanalyst-provenance/07-critic-review.md +0 -25
- data/research/chatanalyst-provenance/final-report.md +0 -45
- data/research/chatanalyst-provenance/resumption.md +0 -37
- data/research/coding-philosophy-analysis.md +0 -928
- data/research/commands-analysis.md +0 -947
- data/research/multi-agent-patterns-analysis.md +0 -853
- data/research/prompt-strategies-analysis.md +0 -630
- data/research/prov-verbosity-fix-notes.md +0 -77
- data/research/provenance-analysis.md +0 -469
- data/research/synthesis-report.md +0 -487
- data/research/tools-system-analysis.md +0 -779
data/lib/scout/llm/tools/call.rb
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
require_relative '../request_context'
|
|
1
2
|
# LLM::Agent is referenced below in content dispatch, but the require chain
|
|
2
3
|
# chat -> tools -> tools/call does not pull in scout/llm/agent (agent.rb
|
|
3
4
|
# requires ask.rb, which requires chat.rb, so loading agent from chat would
|
|
@@ -20,7 +21,11 @@ module LLM
|
|
|
20
21
|
function_arguments = tool_call.dig("arguments")
|
|
21
22
|
end
|
|
22
23
|
|
|
23
|
-
function_arguments =
|
|
24
|
+
function_arguments = begin
|
|
25
|
+
JSON.parse(function_arguments, { symbolize_names: true })
|
|
26
|
+
rescue JSON::ParserError
|
|
27
|
+
{error: "Error parsing function arguments - #{$!.message}"}
|
|
28
|
+
end if String === function_arguments
|
|
24
29
|
|
|
25
30
|
[tool_call_id, function_name, function_arguments]
|
|
26
31
|
end
|
|
@@ -45,7 +50,7 @@ module LLM
|
|
|
45
50
|
end.compact
|
|
46
51
|
end
|
|
47
52
|
|
|
48
|
-
def self.process_calls(tools, calls, &block)
|
|
53
|
+
def self.process_calls(tools, calls, save_file: nil, request_context: nil, &block)
|
|
49
54
|
max_content_length = LLM.max_content_length
|
|
50
55
|
IndiferentHash.setup tools
|
|
51
56
|
|
|
@@ -75,14 +80,14 @@ module LLM
|
|
|
75
80
|
else
|
|
76
81
|
wf = Workflow.require_workflow obj
|
|
77
82
|
end
|
|
78
|
-
call_workflow(wf, function_name, function_arguments)
|
|
83
|
+
call_workflow(wf, function_name, function_arguments, request_context: request_context)
|
|
79
84
|
when Workflow
|
|
80
|
-
call_workflow(obj, function_name, function_arguments)
|
|
85
|
+
call_workflow(obj, function_name, function_arguments, request_context: request_context)
|
|
81
86
|
when KnowledgeBase
|
|
82
87
|
call_knowledge_base(obj, function_name, function_arguments.dup)
|
|
83
88
|
else
|
|
84
89
|
if block_given?
|
|
85
|
-
block
|
|
90
|
+
LLM.call_tool_callback(block, function_name, function_arguments, request_context)
|
|
86
91
|
else
|
|
87
92
|
ParameterException.new "Tool or function not found '#{function_name}'. Called with parameters #{Log.fingerprint function_arguments}" if obj.nil? && definition.nil?
|
|
88
93
|
end
|
|
@@ -134,40 +139,150 @@ module LLM
|
|
|
134
139
|
end
|
|
135
140
|
|
|
136
141
|
jobs = tool_call_content.collect{|p| p.last }.select{|c| Step === c }
|
|
137
|
-
|
|
138
|
-
|
|
142
|
+
jobs.each { |job| job.extend(RequestContext::StepExtension) if request_context }
|
|
143
|
+
workload = jobs.reject{|job| job.done? }
|
|
144
|
+
if workload.any?
|
|
145
|
+
# Live-workload sidecar to help provenance traversal: a SIBLING of the
|
|
146
|
+
# chat save_file holding the short paths of every workflow job still in
|
|
147
|
+
# flight, rewritten per round (it is a snapshot of the CURRENT set, not
|
|
148
|
+
# an append log) and removed in the ensure once Workflow.produce returns
|
|
149
|
+
# -- on success AND on error. Path derivation goes through
|
|
150
|
+
# Chat.jobs_file, the one trailing-strip sibling rule shared with
|
|
151
|
+
# .inbox/.inbox_removed.
|
|
152
|
+
jobs_file = Chat.jobs_file(save_file) if save_file
|
|
153
|
+
workload.each{|job| job.rec_dependencies.each{|job| job.init_info }; job.init_info }
|
|
154
|
+
Open.write(jobs_file, workload.collect{|j| j.short_path } * "\n") if jobs_file
|
|
139
155
|
begin
|
|
140
156
|
Workflow.produce jobs
|
|
141
157
|
rescue
|
|
158
|
+
ensure
|
|
159
|
+
Open.rm jobs_file if jobs_file && Open.exists?(jobs_file)
|
|
142
160
|
end
|
|
143
161
|
end
|
|
144
162
|
|
|
145
163
|
agents = tool_call_content.collect{|p| p.last }.select{|c| LLM::Agent === c }
|
|
146
164
|
|
|
165
|
+
# ScoutCoder: the delegations of one tool round are executed in parallel
|
|
166
|
+
# by the Open.traverse below, and a specialist conversation is persisted
|
|
167
|
+
# by exactly ONE save file: two pending delegations onto the same save
|
|
168
|
+
# file would drive (and auto-save) that conversation twice concurrently,
|
|
169
|
+
# which used to block forever on the persistence lock. Validate the
|
|
170
|
+
# batch here, before any specialist is started, and report the faulty
|
|
171
|
+
# plan as tool errors instead of raising out of the round: every
|
|
172
|
+
# delegation of the round is replaced with one ScoutException (rendered
|
|
173
|
+
# by the Exception branch below through the same channel as any other
|
|
174
|
+
# tool error), while the round's OTHER tool calls keep their normal,
|
|
175
|
+
# already-computed results. Agent objects are not compared because two
|
|
176
|
+
# delegation entry points can mint distinct objects for the same
|
|
177
|
+
# persisted conversation; save_file is the actual persistence target,
|
|
178
|
+
# which is the identity this invariant protects.
|
|
179
|
+
conflicted = agents.collect(&:save_file).compact.tally.select{|_f, count| count > 1 }.keys
|
|
180
|
+
if conflicted.any?
|
|
181
|
+
exception = ScoutException.new(
|
|
182
|
+
"Conflicting delegations: multiple agent executions of this round " \
|
|
183
|
+
"target the same saved conversation #{conflicted.join(", ")}. A " \
|
|
184
|
+
"conversation can be advanced by only one delegation at a time. " \
|
|
185
|
+
"Use distinct conversation names, or delegate sequentially " \
|
|
186
|
+
"(across rounds) so each conversation runs one turn at a time. No " \
|
|
187
|
+
"specialist of this round was started, and every delegation of " \
|
|
188
|
+
"this round was rejected together with it; the round's other tool " \
|
|
189
|
+
"calls were not affected."
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
# Un-stage what this round appended so a retried plan finds each
|
|
193
|
+
# conversation exactly as before the faulty round. Delegation staging
|
|
194
|
+
# appended one user prompt per delegation (preceded, for a
|
|
195
|
+
# clear_tools continuation, by one context-only marker), and no save
|
|
196
|
+
# has happened yet -- saving starts inside Agent#chat, which none of
|
|
197
|
+
# these delegations reached -- so the chats are still memory-only.
|
|
198
|
+
# Walk the calls newest first and pop the matching tail; a marker
|
|
199
|
+
# directly before a staged prompt can only be the marker staged by
|
|
200
|
+
# that same delegation (an earlier round's marker is followed by its
|
|
201
|
+
# own prompt and answer).
|
|
202
|
+
# One delegation in the rejected batch may have asked for a RESTART of
|
|
203
|
+
# an existing conversation (hand_off's new_conversation, or
|
|
204
|
+
# ask_conversation's restart): Agent#start then saved a snapshot of the
|
|
205
|
+
# prior chat to <save_file>.files/resets/ and re-branched in place
|
|
206
|
+
# (keeping start_chat, dropping everything after it) BEFORE this
|
|
207
|
+
# validation ran. Popping the staged prompt is not enough there: the
|
|
208
|
+
# turns the conversation had accumulated are gone from the live chat.
|
|
209
|
+
# Un-restart them: pop the staged prompt off the restart branch and
|
|
210
|
+
# swap back in the exact pre-restart chat object that Agent#start
|
|
211
|
+
# displaced. The snapshot file under resets/ is only an append-only
|
|
212
|
+
# recovery record of what was displaced, never read back by the live
|
|
213
|
+
# conversation, so it needs no compensating action. This runs BEFORE
|
|
214
|
+
# the plain prompt pops below: the restored chat does not end with the
|
|
215
|
+
# staged prompt (it ends where the old conversation ended), so the
|
|
216
|
+
# plain pop loop leaves it alone.
|
|
217
|
+
tool_call_content.reverse_each do |_function_name, function_arguments, _tool_call_id, _tool_call, content|
|
|
218
|
+
next unless LLM::Agent === content
|
|
219
|
+
next unless function_arguments[:new_conversation] || function_arguments[:restart]
|
|
220
|
+
next unless (prior = content.restart_undo_stash)
|
|
221
|
+
content.restart_undo_stash = nil
|
|
222
|
+
chat = content.current_chat
|
|
223
|
+
prompt = function_arguments[:prompt] || function_arguments[:message]
|
|
224
|
+
if chat.last && chat.last[:role].to_s == 'user' && chat.last[:content] == prompt
|
|
225
|
+
chat.pop
|
|
226
|
+
content.restore_restarted_chat(prior)
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
tool_call_content.reverse_each do |function_name, function_arguments, _tool_call_id, _tool_call, content|
|
|
231
|
+
next unless LLM::Agent === content
|
|
232
|
+
chat = content.current_chat
|
|
233
|
+
prompt = function_arguments[:prompt] || function_arguments[:message]
|
|
234
|
+
next unless chat.last && chat.last[:role].to_s == 'user' && chat.last[:content] == prompt
|
|
235
|
+
chat.pop
|
|
236
|
+
if function_arguments[:inherit].to_s == 'clear_tools' &&
|
|
237
|
+
chat.last && chat.last[:role].to_s == 'clear_tools'
|
|
238
|
+
chat.pop
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
tool_call_content.each{|entry| entry[-1] = exception if LLM::Agent === entry.last }
|
|
243
|
+
agents = []
|
|
244
|
+
end
|
|
245
|
+
|
|
147
246
|
agent_answers = TSV.setup({}, key_field: 'Pos', fields: ['Content', 'Job path'], type: :list)
|
|
148
247
|
|
|
149
248
|
if agents.any?
|
|
150
249
|
cpus = Scout::Config.get(:cpus, :agent_ask, :agents, env: 'ASK_AGENTS', default: 3)
|
|
151
250
|
Open.traverse (0..agents.length-1).to_a, cpus: cpus, bar: 'Asking agents', type: :list, into: agent_answers do |i|
|
|
152
251
|
agent = agents[i]
|
|
153
|
-
res = agent.chat return_messages: true
|
|
252
|
+
res = agent.chat return_messages: true, request_context: request_context
|
|
154
253
|
path = Step === agent.job ? agent.job.path : nil
|
|
155
254
|
[i, [res, path]]
|
|
156
255
|
end
|
|
157
256
|
end
|
|
158
257
|
|
|
159
|
-
|
|
258
|
+
# ScoutCoder: each agent-returning tool call must be paired with the
|
|
259
|
+
# answer produced by ITS OWN round. `agents.index(content)` returns the
|
|
260
|
+
# position of the FIRST occurrence, so two calls in one round returning
|
|
261
|
+
# the SAME Agent (two `ask` calls to one registered conversation, or two
|
|
262
|
+
# delegate calls to the same specialist slot) both embedded the FIRST
|
|
263
|
+
# answer in every `function_call_output` message and followed the wrong
|
|
264
|
+
# chat. Pair by tool-call position instead: the n-th agent-returning
|
|
265
|
+
# call consumes the n-th collected answer.
|
|
266
|
+
agent_call_positions = {}
|
|
267
|
+
tool_call_content.each_with_index do |entry, position|
|
|
268
|
+
agent_call_positions[position] = agent_call_positions.length if LLM::Agent === entry.last
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
tool_call_content.each_with_index.collect do |(function_name,function_arguments,tool_call_id,tool_call,content), call_position|
|
|
160
272
|
error = false
|
|
161
273
|
stack = nil
|
|
162
274
|
meta = []
|
|
163
275
|
if Step === content
|
|
164
276
|
step = content
|
|
277
|
+
Chat.allow_read_job step
|
|
165
278
|
if content.done?
|
|
166
279
|
content = content.load
|
|
167
280
|
elsif content.error? && content.exception
|
|
168
281
|
error = :error
|
|
169
282
|
content = if String === content.exception
|
|
170
283
|
{exception: content.exception}.to_json
|
|
284
|
+
elsif Hash === content.exception
|
|
285
|
+
content = content.exception
|
|
171
286
|
else
|
|
172
287
|
content = {exception: content.exception.message, exception_line: content.exception.backtrace&.first}.to_json
|
|
173
288
|
end
|
|
@@ -181,7 +296,7 @@ module LLM
|
|
|
181
296
|
end
|
|
182
297
|
end
|
|
183
298
|
elsif LLM::Agent === content
|
|
184
|
-
res, path = agent_answers[
|
|
299
|
+
res, path = agent_answers[agent_call_positions[call_position]]
|
|
185
300
|
|
|
186
301
|
begin
|
|
187
302
|
Chat.allow_read_job Step.load(path)
|
|
@@ -204,44 +319,50 @@ module LLM
|
|
|
204
319
|
step = nil
|
|
205
320
|
end
|
|
206
321
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
322
|
+
|
|
323
|
+
begin
|
|
324
|
+
content = case content
|
|
325
|
+
when Hash
|
|
326
|
+
# ScoutCoder: When the response of the function contains a
|
|
327
|
+
# Hash with exactly two keys, content and meta, treat it as
|
|
328
|
+
# content with inference meta. Extract accordingly.
|
|
329
|
+
content = IndiferentHash.setup(content)
|
|
330
|
+
keys = content.keys.collect{|k| k.to_s }
|
|
331
|
+
if keys.sort == %w(content meta)
|
|
332
|
+
# `meta` is the deserialized receipt array; pass it
|
|
333
|
+
# through verbatim.
|
|
334
|
+
meta, content = content.values_at :meta, :content
|
|
335
|
+
content
|
|
336
|
+
else
|
|
337
|
+
content.to_json
|
|
338
|
+
end
|
|
339
|
+
when TSV
|
|
340
|
+
content.to_s
|
|
341
|
+
when String
|
|
219
342
|
content
|
|
220
|
-
elsif keys.sort == %w(agent_meta content)
|
|
221
|
-
# Legacy inbound shape: serialized meta messages;
|
|
222
|
-
# normalize them into the new deserialized form.
|
|
223
|
-
meta = LLM.meta_receipt_from_messages(content[:agent_meta])
|
|
224
|
-
content = content[:content]
|
|
225
343
|
else
|
|
226
344
|
content.to_json
|
|
227
345
|
end
|
|
228
|
-
when TSV
|
|
229
|
-
content.to_s
|
|
230
|
-
when String
|
|
231
|
-
content
|
|
232
|
-
else
|
|
233
|
-
content.to_json
|
|
234
|
-
end
|
|
235
346
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
347
|
+
if (String === content) && content.length > max_content_length
|
|
348
|
+
exception_msg = "Function #{function_name} #{tool_call_id} (#{Log.fingerprint function_arguments}) was executed successfully, but it returned #{content.length} characters, which is more than the maximum of #{max_content_length}. To protect the model context window this result was not returned. Here is a fingerprint of the content #{Log.fingerprint(content)}."
|
|
349
|
+
exception_msg += " The results was persisted at '#{step.path}'." if step
|
|
350
|
+
Log.high exception_msg
|
|
351
|
+
content = {exception: exception_msg, stack: caller}.to_json
|
|
352
|
+
error = :truncated
|
|
353
|
+
end
|
|
243
354
|
|
|
244
|
-
|
|
355
|
+
Log.high "Called #{function_name} #{tool_call_id} (#{Log.fingerprint function_arguments}): " + Log.fingerprint(content)
|
|
356
|
+
rescue ScoutException => e
|
|
357
|
+
error = :scout
|
|
358
|
+
stack = e.backtrace
|
|
359
|
+
content = {exception: e.message, exception_line: e.backtrace&.first}.to_json
|
|
360
|
+
rescue => e
|
|
361
|
+
Log.exception e
|
|
362
|
+
error = :harness
|
|
363
|
+
stack = e.backtrace
|
|
364
|
+
content = {exception: e.message, exception_line: e.backtrace&.first}.to_json
|
|
365
|
+
end
|
|
245
366
|
|
|
246
367
|
response_message = {
|
|
247
368
|
name: function_name,
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
module LLM
|
|
2
|
+
# Build one tool definition from its parts, shared by the agent layer
|
|
3
|
+
# (`socialize`, `delegate`, `attachments`) and the workflow task tools
|
|
4
|
+
# (`task_tool_definition`).
|
|
5
|
+
#
|
|
6
|
+
# `name` is the task name exposed to the model (String or Symbol),
|
|
7
|
+
# `description` its description, and `properties` a Hash of
|
|
8
|
+
# `property name => schema fragment`. Fragments are copied verbatim, so an
|
|
9
|
+
# optional property keeps its `default:` key: providers show it to the model
|
|
10
|
+
# and it is preserved on replay.
|
|
11
|
+
#
|
|
12
|
+
# `required` lists the property names that must be supplied (`nil` means
|
|
13
|
+
# none). `defaults` is the replay side-channel consumed by
|
|
14
|
+
# `LLM.process_calls` (tools/call.rb), which merges it into the arguments of
|
|
15
|
+
# a tool call before execution; it is emitted as `parameters[:defaults]`
|
|
16
|
+
# only when truthy, so a nil `defaults` leaves the key absent while an empty
|
|
17
|
+
# Hash is kept as-is.
|
|
18
|
+
#
|
|
19
|
+
# `strict` (default true) sets `additionalProperties: false` so the schema
|
|
20
|
+
# rejects unknown parameters. `envelope` (default true) wraps the payload in
|
|
21
|
+
# the `{type: 'function', function: ...}` envelope used by the agent layer;
|
|
22
|
+
# workflow task tools are emitted bare, as their consumers (backend
|
|
23
|
+
# `format_tool_definitions` and `mcp.rb`) expect.
|
|
24
|
+
def self.tool_definition(name, description, properties,
|
|
25
|
+
required: nil, defaults: nil, strict: true, envelope: true)
|
|
26
|
+
required = [] if required.nil?
|
|
27
|
+
required = [required] unless Array === required
|
|
28
|
+
|
|
29
|
+
parameters = {
|
|
30
|
+
type: 'object',
|
|
31
|
+
properties: properties,
|
|
32
|
+
required: required
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
parameters[:defaults] = defaults if defaults
|
|
36
|
+
parameters[:additionalProperties] = false if strict
|
|
37
|
+
|
|
38
|
+
function = {
|
|
39
|
+
name: name,
|
|
40
|
+
description: description,
|
|
41
|
+
parameters: parameters
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return IndiferentHash.setup(function) unless envelope
|
|
45
|
+
|
|
46
|
+
IndiferentHash.setup(function.merge(type: 'function', function: function))
|
|
47
|
+
end
|
|
48
|
+
end
|
data/lib/scout/llm/tools/mcp.rb
CHANGED
|
@@ -2,62 +2,69 @@ require_relative '../utils'
|
|
|
2
2
|
require 'mcp_client'
|
|
3
3
|
|
|
4
4
|
module LLM
|
|
5
|
+
MCP_SERVERS = {}
|
|
5
6
|
def self.mcp_tools(url, options = {})
|
|
6
|
-
|
|
7
|
+
key = Misc.digest([url, options])
|
|
8
|
+
MCP_SERVERS[key] ||=
|
|
9
|
+
begin
|
|
10
|
+
timeout = Scout::Config.get :timeout, :mcp, :tools, default: 600
|
|
7
11
|
|
|
8
|
-
|
|
12
|
+
options = IndiferentHash.add_defaults options, read_timeout: timeout.to_i if timeout && timeout != ""
|
|
9
13
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
if url == 'stdio'
|
|
15
|
+
client = MCPClient.create_client(mcp_server_configs: [options.merge(type: 'stdio')])
|
|
16
|
+
else
|
|
17
|
+
type = IndiferentHash.process_options options, :type,
|
|
18
|
+
type: (Open.remote?(url) ? :https : :stdio)
|
|
15
19
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
if url && Open.remote?(url)
|
|
21
|
+
token ||= LLM.get_url_config(:key, url, :mcp)
|
|
22
|
+
options[:headers] = { 'Authorization' => "Bearer #{token}" }
|
|
23
|
+
end
|
|
20
24
|
|
|
21
|
-
|
|
22
|
-
|
|
25
|
+
mcp_server_options = options.merge(type: 'http', transport: :streamable_http)
|
|
26
|
+
#client = MCPClient.create_client(mcp_server_configs: [mcp_server_options])
|
|
27
|
+
client = MCPClient.connect(url, **mcp_server_options)
|
|
28
|
+
end
|
|
23
29
|
|
|
24
|
-
|
|
30
|
+
tools = client.list_tools
|
|
25
31
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
tool_definitions = IndiferentHash.setup({})
|
|
33
|
+
tools.each do |tool|
|
|
34
|
+
name = tool.name
|
|
35
|
+
description = tool.description
|
|
36
|
+
schema = tool.schema
|
|
31
37
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
38
|
+
function = {
|
|
39
|
+
name: name,
|
|
40
|
+
description: description,
|
|
41
|
+
parameters: schema
|
|
42
|
+
}
|
|
37
43
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
definition = IndiferentHash.setup function.merge(type: 'function', function: function)
|
|
45
|
+
block = Proc.new do |name,params|
|
|
46
|
+
res = tool.server.call_tool(name, params)
|
|
47
|
+
if Hash === res && res['content']
|
|
48
|
+
res = res['content']
|
|
49
|
+
end
|
|
44
50
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
51
|
+
if Array === res and res.length == 1
|
|
52
|
+
res = res.first
|
|
53
|
+
end
|
|
48
54
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
55
|
+
if Hash === res && res['content']
|
|
56
|
+
res = res['content']
|
|
57
|
+
end
|
|
52
58
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
59
|
+
if Hash === res && res['text']
|
|
60
|
+
res = res['text']
|
|
61
|
+
end
|
|
56
62
|
|
|
57
|
-
|
|
63
|
+
res
|
|
64
|
+
end
|
|
65
|
+
tool_definitions[name] = [block, definition]
|
|
66
|
+
end
|
|
67
|
+
tool_definitions
|
|
58
68
|
end
|
|
59
|
-
tool_definitions[name] = [block, definition]
|
|
60
|
-
end
|
|
61
|
-
tool_definitions
|
|
62
69
|
end
|
|
63
70
|
end
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
require 'scout/workflow'
|
|
2
|
+
require_relative '../request_context'
|
|
2
3
|
module LLM
|
|
3
4
|
def self.scout_to_tool_input_type(type)
|
|
5
|
+
type = type.to_sym
|
|
4
6
|
type = :text if type == :chat
|
|
7
|
+
type = :text if type == :json
|
|
5
8
|
type = :string if type == :text
|
|
6
9
|
type = :string if type == :select
|
|
7
10
|
type = :string if type == :path
|
|
11
|
+
type = :string if type == :file
|
|
8
12
|
type = :number if type == :float
|
|
9
13
|
type = :array if type.to_s.end_with?('_array')
|
|
10
14
|
type
|
|
@@ -30,10 +34,16 @@ module LLM
|
|
|
30
34
|
end
|
|
31
35
|
|
|
32
36
|
|
|
33
|
-
|
|
37
|
+
intent = Scout::Config.get(:intent, :tool_call, :tool, :workflow, workflow.to_s, task_name.to_s, default: true)
|
|
38
|
+
inputs = task_info[:inputs]
|
|
39
|
+
properties = inputs.inject({}) do |acc,input|
|
|
34
40
|
next acc if names and not names.include?(input)
|
|
35
41
|
type = task_info[:input_types][input]
|
|
36
42
|
description = task_info[:input_descriptions][input]
|
|
43
|
+
options = task_info[:input_options][input] || {}
|
|
44
|
+
next acc if ! (names && names.include?(input)) && options && options.include?(:hidden) && options[:hidden].to_s.downcase == 'true'
|
|
45
|
+
required = options[:required]
|
|
46
|
+
default = task_info[:input_defaults][input]
|
|
37
47
|
|
|
38
48
|
type = scout_to_tool_input_type(type)
|
|
39
49
|
type = :array if type.to_s.end_with?('_array')
|
|
@@ -43,6 +53,8 @@ module LLM
|
|
|
43
53
|
description: description || ''
|
|
44
54
|
}
|
|
45
55
|
|
|
56
|
+
acc[input][:default] = default unless default.nil?
|
|
57
|
+
|
|
46
58
|
if type == :array
|
|
47
59
|
acc[input]['items'] = {type: :string}
|
|
48
60
|
end
|
|
@@ -54,35 +66,57 @@ module LLM
|
|
|
54
66
|
end
|
|
55
67
|
end
|
|
56
68
|
|
|
69
|
+
if options && options[:file].to_s == 'true'
|
|
70
|
+
acc[input] = {
|
|
71
|
+
oneOf: [
|
|
72
|
+
acc[input],
|
|
73
|
+
{
|
|
74
|
+
type: :string,
|
|
75
|
+
description: 'Path to a file containing the input values'
|
|
76
|
+
}
|
|
77
|
+
],
|
|
78
|
+
description: description || ''
|
|
79
|
+
}
|
|
80
|
+
end
|
|
81
|
+
|
|
57
82
|
acc
|
|
58
83
|
end
|
|
59
84
|
|
|
60
85
|
if not workflow.exec_exports.include?(task_name.to_sym)
|
|
61
86
|
properties[:return_path] = {
|
|
62
87
|
type: 'boolean',
|
|
63
|
-
description: 'Instead of the result
|
|
88
|
+
description: 'Instead of returning the result, return the path where the result is persisted. Use this when you want to pass the result to another tool or script, move it, or process it as a file without loading its contents into the conversation.',
|
|
89
|
+
default: false
|
|
90
|
+
}
|
|
91
|
+
properties[:refresh] = {
|
|
92
|
+
type: 'string',
|
|
93
|
+
enum: ['false', 'refresh', 'deep_refresh'],
|
|
94
|
+
description: 'Control whether cached results may be reused. Use "refresh" to recompute this task; use "deep_refresh" when results used by this task may also be stale and should be refreshed.',
|
|
95
|
+
default: 'false'
|
|
96
|
+
}
|
|
97
|
+
else
|
|
98
|
+
properties[:return_path] = {
|
|
99
|
+
type: 'boolean',
|
|
100
|
+
description: 'Instead of returning the result, write it to a temporary file and return its path. Use this when you want to pass the result to another tool or script, move it, or process it as a file without loading its contents into the conversation.',
|
|
101
|
+
default: false
|
|
64
102
|
}
|
|
65
103
|
end
|
|
66
104
|
|
|
67
105
|
required_inputs = task_info[:inputs].select do |input|
|
|
68
|
-
next if names and not names.include?(input.to_sym)
|
|
106
|
+
next false if names and not names.include?(input.to_sym)
|
|
69
107
|
task_info[:input_options].include?(input) && task_info[:input_options][input][:required]
|
|
70
108
|
end
|
|
71
109
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
type: "object",
|
|
77
|
-
properties: properties,
|
|
78
|
-
required: required_inputs,
|
|
110
|
+
if intent.to_s == 'true'
|
|
111
|
+
properties[:intent] = {
|
|
112
|
+
type: 'string',
|
|
113
|
+
description: 'Short sentence describing why you are running this command',
|
|
79
114
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
function[:parameters][:defaults] = defaults if defaults
|
|
115
|
+
required_inputs << :intent
|
|
116
|
+
end
|
|
83
117
|
|
|
84
|
-
|
|
85
|
-
|
|
118
|
+
LLM.tool_definition(task_name, task_info[:description] || '', properties,
|
|
119
|
+
required: required_inputs, defaults: defaults, envelope: false)
|
|
86
120
|
end
|
|
87
121
|
|
|
88
122
|
def self.workflow_tools(workflow, tasks = nil)
|
|
@@ -102,21 +136,57 @@ module LLM
|
|
|
102
136
|
end
|
|
103
137
|
end
|
|
104
138
|
|
|
105
|
-
def self.call_workflow(workflow, task_name, parameters={})
|
|
106
|
-
parameters = {}
|
|
107
|
-
|
|
139
|
+
def self.call_workflow(workflow, task_name, parameters={}, request_context: nil, **keyword_parameters)
|
|
140
|
+
parameters = IndiferentHash.setup((parameters || {}).merge(keyword_parameters))
|
|
141
|
+
|
|
142
|
+
# Some models use '' for optional parameters
|
|
143
|
+
parameters = parameters.reject{|k,v| v == ''}
|
|
144
|
+
|
|
145
|
+
jobname, return_path, exec_type, allow_recursive, refresh = IndiferentHash.process_options parameters, :jobname, :return_path, :exec_type, :allow_recursive, :refresh
|
|
108
146
|
begin
|
|
109
147
|
job = workflow.job(task_name.to_sym, jobname, parameters)
|
|
110
148
|
if workflow.exec_exports.include?(task_name.to_sym) || exec_type.to_s == 'exec'
|
|
111
|
-
|
|
149
|
+
if request_context
|
|
150
|
+
job.extend(RequestContext::StepExtension)
|
|
151
|
+
job.register_request_context(request_context)
|
|
152
|
+
end
|
|
153
|
+
if return_path
|
|
154
|
+
result = job.exec
|
|
155
|
+
file = TmpFile.tmp_file
|
|
156
|
+
case result
|
|
157
|
+
when Chat
|
|
158
|
+
Open.write(file, Chat.print(result))
|
|
159
|
+
else
|
|
160
|
+
Open.write(file, result.to_s)
|
|
161
|
+
end
|
|
162
|
+
file
|
|
163
|
+
else
|
|
164
|
+
job.exec
|
|
165
|
+
end
|
|
112
166
|
else
|
|
167
|
+
case refresh
|
|
168
|
+
when 'refresh'
|
|
169
|
+
job.clean
|
|
170
|
+
when 'deep_refresh'
|
|
171
|
+
job.recursive_clean
|
|
172
|
+
end
|
|
113
173
|
if return_path
|
|
114
|
-
|
|
174
|
+
if request_context
|
|
175
|
+
job.extend(RequestContext::StepExtension)
|
|
176
|
+
job.register_request_context(request_context)
|
|
177
|
+
job.run
|
|
178
|
+
else
|
|
179
|
+
job.run(true)
|
|
180
|
+
end
|
|
115
181
|
Chat.allow_read_job job
|
|
116
182
|
job.path
|
|
117
183
|
else
|
|
118
184
|
raise ScoutException, 'Potential recursive call' if allow_recursive != 'true' &&
|
|
119
185
|
(job.running? and job.info[:pid] == Process.pid)
|
|
186
|
+
if request_context
|
|
187
|
+
job.extend(RequestContext::StepExtension)
|
|
188
|
+
job.register_request_context(request_context)
|
|
189
|
+
end
|
|
120
190
|
job
|
|
121
191
|
end
|
|
122
192
|
end
|