active_harness 0.3.9 → 0.3.11
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/active_harness/agent.rb +106 -13
- data/lib/active_harness/pipeline.rb +8 -1
- data/lib/active_harness/providers/audio/openai.rb +10 -1
- data/lib/active_harness/providers/images/openai.rb +6 -1
- data/lib/active_harness/providers/images/openrouter.rb +42 -10
- data/lib/active_harness/request/conversation.rb +104 -0
- data/lib/active_harness/request/custom_llm_backend.rb +1 -0
- data/lib/active_harness/request/hooks.rb +2 -0
- data/lib/active_harness/request/models.rb +6 -2
- data/lib/active_harness/request/prompt.rb +6 -2
- data/lib/active_harness/request/providers.rb +8 -1
- data/lib/active_harness/request.rb +24 -29
- data/lib/active_harness/result.rb +5 -1
- data/lib/active_harness/tool/path_policy.rb +3 -1
- data/lib/active_harness/tool.rb +16 -19
- data/lib/active_harness.rb +1 -1
- data/lib/generators/active_harness/agent/templates/agent.rb.tt +4 -4
- data/lib/generators/active_harness/install/templates/agents/files_agent.rb +9 -9
- data/lib/generators/active_harness/install/templates/concerns/agent_tracing.rb +1 -1
- data/lib/generators/active_harness/install/templates/tools/ls_tool.rb +4 -3
- data/lib/generators/active_harness/install/templates/tools/read_file_tool.rb +3 -1
- data/lib/generators/active_harness/tool/templates/tool.rb.tt +4 -2
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e94d379516ee916b1eb8f5c6bfc8ca2330564f1040ad2f4445c3512a352ac86c
|
|
4
|
+
data.tar.gz: 555da0e27b5e6a7118668332a21168d7a5703ca451ea28933707e0595467c0e0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 95ddc9596fa34a30a66bd4a90b4f1fcabc151929694a763c01aae46a8c7f6067db4108bb9d1f2014ab67b900da29af42d34d250ac132d0b0f3cfc6ecb8027d1e
|
|
7
|
+
data.tar.gz: 8bf25081f3c956858a13006f01bde32c3b76ff9acb72d863d67becc232bcdeddbec8d563d80484a0c966b4fe09ab04b6f2aa2851f74112f65c242c69d8560cab
|
data/lib/active_harness/agent.rb
CHANGED
|
@@ -12,10 +12,11 @@ module ActiveHarness
|
|
|
12
12
|
# everything model-related stays inside Requests.
|
|
13
13
|
#
|
|
14
14
|
# class SearchAgent < ActiveHarness::Agent
|
|
15
|
-
# request SearchRequest
|
|
16
|
-
# tools WebSearchTool, FetchPageTool
|
|
17
15
|
# max_turns 10
|
|
18
16
|
# max_cost 0.50
|
|
17
|
+
#
|
|
18
|
+
# request SearchRequest
|
|
19
|
+
# tools WebSearchTool, FetchPageTool
|
|
19
20
|
# end
|
|
20
21
|
#
|
|
21
22
|
# agent = SearchAgent.call(input: "Find 5 Ruby frameworks")
|
|
@@ -34,7 +35,7 @@ module ActiveHarness
|
|
|
34
35
|
before_llm after_llm
|
|
35
36
|
before_tool after_tool tool_denied
|
|
36
37
|
replan stopped
|
|
37
|
-
before_finalize complete
|
|
38
|
+
before_finalize finalize_error complete
|
|
38
39
|
].freeze
|
|
39
40
|
STEP_HOOKS = %i[before_step after_step].freeze
|
|
40
41
|
TOOL_HOOKS = %i[before_tool after_tool tool_denied].freeze
|
|
@@ -171,7 +172,7 @@ module ActiveHarness
|
|
|
171
172
|
# Instance API
|
|
172
173
|
# -------------------------------------------------------------------------
|
|
173
174
|
attr_accessor :input, :context, :params, :memory
|
|
174
|
-
attr_reader :state, :turns, :error, :execution_time
|
|
175
|
+
attr_reader :state, :turns, :error, :finalize_error, :execution_time
|
|
175
176
|
|
|
176
177
|
def initialize(input: nil, context: {}, params: {}, memory: nil, token: nil, stream: nil)
|
|
177
178
|
@input = input
|
|
@@ -239,7 +240,9 @@ module ActiveHarness
|
|
|
239
240
|
model: last&.model,
|
|
240
241
|
attempts: [],
|
|
241
242
|
execution_time: @execution_time,
|
|
242
|
-
usage: @state&.usage
|
|
243
|
+
usage: @state&.usage,
|
|
244
|
+
warnings: [],
|
|
245
|
+
meta: { status: status, stop_reason: stop_reason, run_id: run_id }
|
|
243
246
|
)
|
|
244
247
|
end
|
|
245
248
|
|
|
@@ -263,7 +266,7 @@ module ActiveHarness
|
|
|
263
266
|
body << " answer: #{res.output.to_s[0, 80].inspect}" if turn.tool_calls.empty? && res
|
|
264
267
|
([head] + body).join("\n")
|
|
265
268
|
end
|
|
266
|
-
lines << format("stop %s %d turns, %d tool calls (%d
|
|
269
|
+
lines << format("stop %s %d turns, %d tool calls (%d refused or failed), $%.4f",
|
|
267
270
|
@state.stop_reason, @turns.size, tool_calls.size, denied_calls.size,
|
|
268
271
|
@state.usage&.cost&.total.to_f)
|
|
269
272
|
lines.join("\n")
|
|
@@ -278,6 +281,7 @@ module ActiveHarness
|
|
|
278
281
|
def reset_run
|
|
279
282
|
@turns = []
|
|
280
283
|
@error = nil
|
|
284
|
+
@finalize_error = nil
|
|
281
285
|
@output = nil
|
|
282
286
|
@last_text = nil
|
|
283
287
|
@final_response = nil
|
|
@@ -299,13 +303,14 @@ module ActiveHarness
|
|
|
299
303
|
end
|
|
300
304
|
|
|
301
305
|
def run_setup
|
|
302
|
-
pipeline_class = @config[:setup]
|
|
306
|
+
pipeline_class = @config[:setup] && as_pipeline(@config[:setup])
|
|
303
307
|
fire(:before_setup, @state)
|
|
304
308
|
|
|
305
309
|
goal = nil
|
|
306
310
|
if pipeline_class
|
|
307
311
|
pipeline = pipeline_class.new(input: @input, context: @context.dup, params: @params, memory: @memory, stream: @stream)
|
|
308
312
|
pipeline.call
|
|
313
|
+
charge(:setup, pipeline)
|
|
309
314
|
journal(:setup, stopped: pipeline.stopped?, stopped_at: pipeline.stopped_at)
|
|
310
315
|
|
|
311
316
|
if pipeline.stopped?
|
|
@@ -372,19 +377,22 @@ module ActiveHarness
|
|
|
372
377
|
end
|
|
373
378
|
|
|
374
379
|
def run_finalize
|
|
375
|
-
|
|
376
|
-
return unless pipeline_class
|
|
380
|
+
return unless @config[:finalize]
|
|
377
381
|
|
|
382
|
+
pipeline_class = as_pipeline(@config[:finalize])
|
|
378
383
|
fire(:before_finalize, @state)
|
|
379
|
-
ctx = @context.merge(state: @state, status: @state.status, stop_reason: @state.stop_reason
|
|
384
|
+
ctx = @context.merge(state: @state, status: @state.status, stop_reason: @state.stop_reason,
|
|
385
|
+
processed: @final_response&.processed)
|
|
380
386
|
pipeline = pipeline_class.new(input: @output.to_s, context: ctx, params: @params, memory: @memory, stream: @stream)
|
|
381
387
|
pipeline.call
|
|
388
|
+
charge(:finalize, pipeline)
|
|
382
389
|
@output = pipeline.output unless pipeline.stopped? || pipeline.output.nil?
|
|
383
390
|
journal(:finalize, stopped: pipeline.stopped?)
|
|
384
391
|
rescue StandardError => e
|
|
385
392
|
# A failure in finalization never erases the primary stop reason.
|
|
386
393
|
@finalize_error = e
|
|
387
394
|
journal(:finalize_error, error: "#{e.class}: #{e.message}")
|
|
395
|
+
fire(:finalize_error, e)
|
|
388
396
|
end
|
|
389
397
|
|
|
390
398
|
def stop!(reason)
|
|
@@ -442,7 +450,7 @@ module ActiveHarness
|
|
|
442
450
|
memory: @memory,
|
|
443
451
|
token: @token,
|
|
444
452
|
stream: @stream,
|
|
445
|
-
|
|
453
|
+
input: messages,
|
|
446
454
|
tools: schemas.empty? ? nil : schemas
|
|
447
455
|
)
|
|
448
456
|
turn.request_name = name
|
|
@@ -604,11 +612,62 @@ module ActiveHarness
|
|
|
604
612
|
goal = @state.goal
|
|
605
613
|
case spec
|
|
606
614
|
when Proc then spec.call(candidate, goal, @state)
|
|
607
|
-
when Class
|
|
608
|
-
|
|
615
|
+
when Class
|
|
616
|
+
if spec <= Tribunal then judge_with_tribunal(spec, candidate)
|
|
617
|
+
elsif spec <= Request then judge_with_request(spec, candidate)
|
|
618
|
+
else spec.new.call(candidate: candidate, goal: goal, state: @state)
|
|
619
|
+
end
|
|
620
|
+
else spec.call(candidate: candidate, goal: goal, state: @state)
|
|
621
|
+
end
|
|
622
|
+
end
|
|
623
|
+
|
|
624
|
+
# A Tribunal as the judge: its verdict becomes a GoalCheckResult.
|
|
625
|
+
# true → :achieved
|
|
626
|
+
# false / nil → :continue, feedback = the reasons of the answers that did not approve
|
|
627
|
+
# GoalCheckResult / Hash with :status → used as is
|
|
628
|
+
def judge_with_tribunal(klass, candidate)
|
|
629
|
+
tribunal = klass.new(input: candidate, context: judge_context, params: @params, stream: @stream)
|
|
630
|
+
tribunal.call
|
|
631
|
+
charge(:goal, tribunal)
|
|
632
|
+
|
|
633
|
+
verdict = tribunal.verdict
|
|
634
|
+
return verdict if verdict.is_a?(GoalCheckResult) || verdict.is_a?(Hash)
|
|
635
|
+
return GoalCheckResult.new(status: :achieved, evidence: tribunal.results.map(&:output)) if verdict
|
|
636
|
+
|
|
637
|
+
GoalCheckResult.new(status: :continue, feedback: dissent_feedback(tribunal.results), evidence: tribunal.results.map(&:output))
|
|
638
|
+
end
|
|
639
|
+
|
|
640
|
+
# A Request as the judge: it answers with a Hash ({"status" => "achieved", "feedback" => ...}),
|
|
641
|
+
# or with text starting with yes / true / achieved / done.
|
|
642
|
+
def judge_with_request(klass, candidate)
|
|
643
|
+
request = klass.new(input: candidate, context: judge_context, params: @params, stream: @stream)
|
|
644
|
+
request.call
|
|
645
|
+
charge(:goal, request)
|
|
646
|
+
|
|
647
|
+
processed = request.result.processed
|
|
648
|
+
return GoalCheckResult.from(processed) if processed.is_a?(Hash)
|
|
649
|
+
|
|
650
|
+
text = request.result.output.to_s
|
|
651
|
+
if text.strip.downcase.start_with?("yes", "true", "achieved", "done")
|
|
652
|
+
GoalCheckResult.new(status: :achieved, evidence: text)
|
|
653
|
+
else
|
|
654
|
+
GoalCheckResult.new(status: :continue, feedback: text[0, 500], evidence: text)
|
|
609
655
|
end
|
|
610
656
|
end
|
|
611
657
|
|
|
658
|
+
def judge_context
|
|
659
|
+
@context.merge(goal: @state.goal, agent_state: @state)
|
|
660
|
+
end
|
|
661
|
+
|
|
662
|
+
def dissent_feedback(results)
|
|
663
|
+
reasons = results.map do |r|
|
|
664
|
+
processed = r.processed
|
|
665
|
+
reason = processed.is_a?(Hash) ? (processed["reason"] || processed[:reason] || processed["feedback"] || processed[:feedback]) : nil
|
|
666
|
+
(reason || r.output).to_s[0, 200]
|
|
667
|
+
end.reject(&:empty?)
|
|
668
|
+
reasons.empty? ? "The independent review did not approve the answer." : reasons.uniq.join("; ")
|
|
669
|
+
end
|
|
670
|
+
|
|
612
671
|
# -------------------------------------------------------------------------
|
|
613
672
|
# Limits
|
|
614
673
|
# -------------------------------------------------------------------------
|
|
@@ -652,6 +711,40 @@ module ActiveHarness
|
|
|
652
711
|
stop!(reason) if reason
|
|
653
712
|
end
|
|
654
713
|
|
|
714
|
+
# Wraps a bare Request / Tribunal in an implicit one-step Pipeline, so `setup` and
|
|
715
|
+
# `finalize` accept any executor. A Pipeline class is used as is.
|
|
716
|
+
def as_pipeline(executor)
|
|
717
|
+
return executor if executor <= Pipeline
|
|
718
|
+
|
|
719
|
+
@wrapped_pipelines ||= {}
|
|
720
|
+
@wrapped_pipelines[executor] ||= Class.new(Pipeline) { step :run, executor }
|
|
721
|
+
end
|
|
722
|
+
|
|
723
|
+
# Adds the usage of a nested executor (Pipeline, Tribunal, Request, Agent) to the run total,
|
|
724
|
+
# so that max_cost and agent.usage count every model call made on behalf of this run.
|
|
725
|
+
def charge(source, executor)
|
|
726
|
+
usages = usages_of(executor)
|
|
727
|
+
usages.each { |u| add_usage(u) }
|
|
728
|
+
return if usages.empty?
|
|
729
|
+
|
|
730
|
+
journal(:nested_usage, source: source, tokens: usages.sum { |u| u.tokens.total.to_i },
|
|
731
|
+
cost: usages.sum { |u| u.cost&.total.to_f }.round(8))
|
|
732
|
+
end
|
|
733
|
+
|
|
734
|
+
def usages_of(executor, result = nil)
|
|
735
|
+
list =
|
|
736
|
+
if executor.nil?
|
|
737
|
+
[result&.usage]
|
|
738
|
+
elsif executor.respond_to?(:stopped?) && executor.respond_to?(:steps) # Pipeline (also nested)
|
|
739
|
+
executor.steps.to_a.flat_map { |_name, ex, res| usages_of(ex, res) }
|
|
740
|
+
elsif executor.respond_to?(:results) # Tribunal: every member request
|
|
741
|
+
executor.results.map(&:usage)
|
|
742
|
+
else # Request / Agent
|
|
743
|
+
[executor.result&.usage]
|
|
744
|
+
end
|
|
745
|
+
list.compact
|
|
746
|
+
end
|
|
747
|
+
|
|
655
748
|
def add_usage(usage)
|
|
656
749
|
return unless usage
|
|
657
750
|
|
|
@@ -90,6 +90,11 @@ module ActiveHarness
|
|
|
90
90
|
def on_pipeline_event(&block)
|
|
91
91
|
(pipeline_config[:streams][:pipeline] ||= []) << block
|
|
92
92
|
end
|
|
93
|
+
|
|
94
|
+
# Events of an Agent used as a step (turns, tool calls, stop reason).
|
|
95
|
+
def on_agent_event(&block)
|
|
96
|
+
(pipeline_config[:streams][:agent] ||= []) << block
|
|
97
|
+
end
|
|
93
98
|
end
|
|
94
99
|
|
|
95
100
|
# -------------------------------------------------------------------------
|
|
@@ -255,8 +260,9 @@ module ActiveHarness
|
|
|
255
260
|
request_handlers = Array(class_handlers[:request]).compact
|
|
256
261
|
tribunal_handlers = Array(class_handlers[:tribunal]).compact
|
|
257
262
|
pipeline_handlers = Array(class_handlers[:pipeline]).compact
|
|
263
|
+
agent_handlers = Array(class_handlers[:agent]).compact
|
|
258
264
|
|
|
259
|
-
has_class_handlers = request_handlers.any? || tribunal_handlers.any? || pipeline_handlers.any?
|
|
265
|
+
has_class_handlers = request_handlers.any? || tribunal_handlers.any? || pipeline_handlers.any? || agent_handlers.any?
|
|
260
266
|
return passed_in unless has_class_handlers
|
|
261
267
|
|
|
262
268
|
pipeline_instance = self
|
|
@@ -265,6 +271,7 @@ module ActiveHarness
|
|
|
265
271
|
when :request then request_handlers
|
|
266
272
|
when :tribunal then tribunal_handlers
|
|
267
273
|
when :pipeline then pipeline_handlers
|
|
274
|
+
when :agent then agent_handlers
|
|
268
275
|
else []
|
|
269
276
|
end
|
|
270
277
|
handlers.each { |h| pipeline_instance.instance_exec(event, *args, &h) }
|
|
@@ -30,6 +30,12 @@ module ActiveHarness
|
|
|
30
30
|
# @param response_format [String] "json" (default, plain text) or,
|
|
31
31
|
# for gpt-4o-transcribe-diarize, "diarized_json" — returns per-segment
|
|
32
32
|
# speaker/text/start/end instead of just plain text.
|
|
33
|
+
# @param prompt [String] optional context for the model — spellings of
|
|
34
|
+
# names/terms and the topic. whisper-1 treats it as preceding text
|
|
35
|
+
# (only the last ~224 tokens count); the gpt-4o-*-transcribe models
|
|
36
|
+
# treat it as free-form instructions. Not supported by
|
|
37
|
+
# gpt-4o-transcribe-diarize.
|
|
38
|
+
# @param temperature [Float] 0..1 sampling temperature (optional).
|
|
33
39
|
# @param chunking_strategy [String, Hash] required by
|
|
34
40
|
# gpt-4o-transcribe-diarize for any audio over 30 seconds; "auto" is
|
|
35
41
|
# the simple/common value, or a voice-activity-detection config hash.
|
|
@@ -38,7 +44,8 @@ module ActiveHarness
|
|
|
38
44
|
# @param known_speaker_references [Array<String>] up to 4 short (2-10s)
|
|
39
45
|
# reference clips as base64 data URLs, e.g. "data:audio/wav;base64,...".
|
|
40
46
|
def call(model:, audio_data:, audio_format:, language: nil, response_format: nil,
|
|
41
|
-
chunking_strategy: nil, known_speaker_names: nil, known_speaker_references: nil,
|
|
47
|
+
chunking_strategy: nil, known_speaker_names: nil, known_speaker_references: nil,
|
|
48
|
+
prompt: nil, temperature: nil, **_)
|
|
42
49
|
content_type = CONTENT_TYPES[audio_format]
|
|
43
50
|
unless content_type
|
|
44
51
|
raise Errors::InvalidRequestError,
|
|
@@ -50,6 +57,8 @@ module ActiveHarness
|
|
|
50
57
|
fields["language"] = language if language
|
|
51
58
|
fields["response_format"] = response_format if response_format
|
|
52
59
|
fields["chunking_strategy"] = chunking_strategy if chunking_strategy
|
|
60
|
+
fields["prompt"] = prompt if prompt
|
|
61
|
+
fields["temperature"] = temperature if temperature
|
|
53
62
|
|
|
54
63
|
array_fields = {
|
|
55
64
|
"known_speaker_names[]" => known_speaker_names,
|
|
@@ -10,7 +10,12 @@ module ActiveHarness
|
|
|
10
10
|
# @param prompt [String] image description
|
|
11
11
|
# @param size [String] e.g. "1024x1024"
|
|
12
12
|
# @param quality [String] "standard"/"hd" (dall-e-3), "low"/"medium"/"high"/"auto" (gpt-image-1)
|
|
13
|
-
def call(model:, prompt:, size: "1024x1024", quality: nil, **_)
|
|
13
|
+
def call(model:, prompt:, size: "1024x1024", quality: nil, images: [], **_)
|
|
14
|
+
unless Array(images).empty?
|
|
15
|
+
raise Errors::InvalidRequestError, "input images are not supported by the :openai image provider yet " \
|
|
16
|
+
"(it only calls /images/generations) — use :openrouter"
|
|
17
|
+
end
|
|
18
|
+
|
|
14
19
|
headers = {
|
|
15
20
|
"Content-Type" => "application/json",
|
|
16
21
|
"Authorization" => "Bearer #{api_key}"
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
require "base64"
|
|
1
2
|
require "uri"
|
|
2
3
|
|
|
3
4
|
module ActiveHarness
|
|
@@ -9,7 +10,14 @@ module ActiveHarness
|
|
|
9
10
|
# Image-only models use OpenRouter's dedicated Images API. Models that
|
|
10
11
|
# also output text retain the legacy chat-completions request path.
|
|
11
12
|
# @param size [String] ignored by OpenRouter (passed through for future support)
|
|
12
|
-
|
|
13
|
+
# @param images [Array<String>] input images for image-to-image: file paths,
|
|
14
|
+
# "data:" URIs or http(s) URLs. Sent as `input_references` (Images API) or
|
|
15
|
+
# as `image_url` content parts (chat path) next to the prompt.
|
|
16
|
+
# @param aspect_ratio [String] e.g. "4:3", "1:1" — Images API only
|
|
17
|
+
# @param resolution [String] tier "512", "1K", "2K", ... — Images API only;
|
|
18
|
+
# when either is set, `size` is not sent (they describe the same thing).
|
|
19
|
+
def call(model:, prompt:, size: nil, quality: nil, images: [], aspect_ratio: nil, resolution: nil, **_)
|
|
20
|
+
images = Array(images).map { |img| image_url_for(img) }
|
|
13
21
|
headers = {
|
|
14
22
|
"Content-Type" => "application/json",
|
|
15
23
|
"Authorization" => "Bearer #{api_key}"
|
|
@@ -17,23 +25,42 @@ module ActiveHarness
|
|
|
17
25
|
referer = config.openrouter_http_referer.to_s
|
|
18
26
|
headers["HTTP-Referer"] = referer unless referer.empty?
|
|
19
27
|
|
|
20
|
-
|
|
28
|
+
extra = { images: images, aspect_ratio: aspect_ratio, resolution: resolution }
|
|
29
|
+
return call_images_api(model, prompt, size, quality, headers, **extra) if image_only_model?(model)
|
|
21
30
|
|
|
22
|
-
call_chat_api_with_fallback(model, prompt, size, quality, headers)
|
|
31
|
+
call_chat_api_with_fallback(model, prompt, size, quality, headers, **extra)
|
|
23
32
|
end
|
|
24
33
|
|
|
25
34
|
private
|
|
26
35
|
|
|
27
|
-
def call_chat_api_with_fallback(model, prompt, size, quality, headers)
|
|
28
|
-
call_chat_api(model, prompt, size, quality, headers)
|
|
36
|
+
def call_chat_api_with_fallback(model, prompt, size, quality, headers, **extra)
|
|
37
|
+
call_chat_api(model, prompt, size, quality, headers, images: extra[:images])
|
|
29
38
|
rescue Errors::InvalidRequestError => error
|
|
30
39
|
raise unless unsupported_output_modalities?(error)
|
|
31
40
|
|
|
32
|
-
call_images_api(model, prompt, size, quality, headers)
|
|
41
|
+
call_images_api(model, prompt, size, quality, headers, **extra)
|
|
33
42
|
end
|
|
34
43
|
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
# A file path becomes a base64 data URI; "data:" URIs and http(s) URLs pass through.
|
|
45
|
+
IMAGE_MIME_TYPES = { "jpg" => "image/jpeg", "jpeg" => "image/jpeg", "png" => "image/png",
|
|
46
|
+
"webp" => "image/webp", "gif" => "image/gif" }.freeze
|
|
47
|
+
|
|
48
|
+
def image_url_for(image)
|
|
49
|
+
image = image.to_s
|
|
50
|
+
return image if image.start_with?("data:", "http://", "https://")
|
|
51
|
+
|
|
52
|
+
raise Errors::InvalidRequestError, "input image not found: #{image}" unless File.file?(image)
|
|
53
|
+
|
|
54
|
+
mime = IMAGE_MIME_TYPES[File.extname(image).delete_prefix(".").downcase] || "image/png"
|
|
55
|
+
"data:#{mime};base64,#{Base64.strict_encode64(File.binread(image))}"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def call_chat_api(model, prompt, size, quality, headers, images: [])
|
|
59
|
+
content = prompt
|
|
60
|
+
unless images.empty?
|
|
61
|
+
content = [{ type: "text", text: prompt }] + images.map { |url| { type: "image_url", image_url: { url: url } } }
|
|
62
|
+
end
|
|
63
|
+
messages = [{ role: "user", content: content }]
|
|
37
64
|
body = { model: model, messages: messages, modalities: ["image", "text"] }
|
|
38
65
|
body[:size] = size if size
|
|
39
66
|
body[:quality] = quality if quality
|
|
@@ -48,10 +75,15 @@ module ActiveHarness
|
|
|
48
75
|
{ content: content, provider: :openrouter, model: model, usage: extract_usage_openai(data) }
|
|
49
76
|
end
|
|
50
77
|
|
|
51
|
-
def call_images_api(model, prompt, size, quality, headers)
|
|
78
|
+
def call_images_api(model, prompt, size, quality, headers, images: [], aspect_ratio: nil, resolution: nil)
|
|
52
79
|
body = { model: model, prompt: prompt }
|
|
53
|
-
body[:size] = size if size
|
|
80
|
+
body[:size] = size if size && !aspect_ratio && !resolution
|
|
81
|
+
body[:aspect_ratio] = aspect_ratio if aspect_ratio
|
|
82
|
+
body[:resolution] = resolution if resolution
|
|
54
83
|
body[:quality] = quality if quality
|
|
84
|
+
unless images.empty?
|
|
85
|
+
body[:input_references] = images.map { |url| { type: "image_url", image_url: { url: url } } }
|
|
86
|
+
end
|
|
55
87
|
body[:output_format] = "png"
|
|
56
88
|
|
|
57
89
|
raw = post_json(URI(config.openrouter_images_api_url), headers: headers, body: body, timeout: 120)
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
module ActiveHarness
|
|
2
|
+
class Request
|
|
3
|
+
# `input` is a String (one user turn) or an Array of messages (a conversation):
|
|
4
|
+
#
|
|
5
|
+
# Request.call(input: "Find Ruby frameworks")
|
|
6
|
+
# Request.call(input: [{ role: :user, content: "Find Ruby frameworks" }, ...])
|
|
7
|
+
#
|
|
8
|
+
# A String is shorthand for [{ role: :user, content: "..." }]. For an Array the
|
|
9
|
+
# request keeps the messages in @messages and @input holds the text of the last
|
|
10
|
+
# user message, so everything that reads @input as a String (prompt classes,
|
|
11
|
+
# lambdas, hooks) keeps working. There is no separate `messages:` keyword.
|
|
12
|
+
MULTI_MESSAGE_WARNING =
|
|
13
|
+
"only the last user message was taken from a multi-message input — " \
|
|
14
|
+
"make sure that is what this prompt needs (use `transcript` for the full history)".freeze
|
|
15
|
+
|
|
16
|
+
# What was passed in: the Array of messages, or the String.
|
|
17
|
+
def input
|
|
18
|
+
@messages || @input
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def input=(value)
|
|
22
|
+
assign_input(value)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Non-fatal remarks collected during the last call (also streamed as [:request, :warning, text]).
|
|
26
|
+
def warnings
|
|
27
|
+
@warnings ||= []
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Text of the input for prompts and hooks that need a String.
|
|
31
|
+
#
|
|
32
|
+
# input_text # => text of the last user message (the String itself for a String input)
|
|
33
|
+
# input_text(roles: [:user]) # => all user messages, joined with a blank line
|
|
34
|
+
def input_text(roles: nil)
|
|
35
|
+
return @input.to_s unless @messages
|
|
36
|
+
return @input.to_s if roles.nil?
|
|
37
|
+
|
|
38
|
+
wanted = Array(roles).map(&:to_s)
|
|
39
|
+
@messages.select { |m| wanted.include?(message_role(m)) }
|
|
40
|
+
.map { |m| message_text(message_content(m)) }
|
|
41
|
+
.join("\n\n")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Role-labelled history: "user: ...", "assistant: ...", "tool(os__ls): ...".
|
|
45
|
+
def transcript
|
|
46
|
+
return "user: #{@input}" unless @messages
|
|
47
|
+
|
|
48
|
+
@messages.map do |m|
|
|
49
|
+
role = message_role(m)
|
|
50
|
+
label = role == "tool" && (name = m[:name] || m["name"]) ? "tool(#{name})" : role
|
|
51
|
+
"#{label}: #{message_text(message_content(m))}"
|
|
52
|
+
end.join("\n")
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def assign_input(value)
|
|
58
|
+
if value.is_a?(Array)
|
|
59
|
+
@messages = value
|
|
60
|
+
@input = last_user_content(value)
|
|
61
|
+
else
|
|
62
|
+
@messages = nil
|
|
63
|
+
@input = value
|
|
64
|
+
end
|
|
65
|
+
normalize_input!
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def message_role(message)
|
|
69
|
+
(message[:role] || message["role"]).to_s
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def message_content(message)
|
|
73
|
+
message[:content] || message["content"]
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Content may be a String or a list of parts ([{ type: "text", text: "..." }, ...]).
|
|
77
|
+
def message_text(content)
|
|
78
|
+
return content.to_s unless content.is_a?(Array)
|
|
79
|
+
|
|
80
|
+
content.map { |part| part.is_a?(Hash) ? (part[:text] || part["text"]).to_s : part.to_s }.join
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def multi_message_input?
|
|
84
|
+
@messages && @messages.size > 1
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Called where a prompt consumes the text of a multi-message input.
|
|
88
|
+
def warn_multi_message_input
|
|
89
|
+
emit_warning(MULTI_MESSAGE_WARNING) if multi_message_input?
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def emit_warning(message)
|
|
93
|
+
return if warnings.include?(message)
|
|
94
|
+
|
|
95
|
+
warnings << message
|
|
96
|
+
fire(:warning, message)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def last_user_content(messages)
|
|
100
|
+
last = Array(messages).reverse.find { |m| message_role(m) == "user" }
|
|
101
|
+
last && message_text(message_content(last))
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
@@ -11,6 +11,7 @@ module ActiveHarness
|
|
|
11
11
|
parse_error
|
|
12
12
|
retry
|
|
13
13
|
failure
|
|
14
|
+
warning
|
|
14
15
|
].freeze
|
|
15
16
|
|
|
16
17
|
class << self
|
|
@@ -26,6 +27,7 @@ module ActiveHarness
|
|
|
26
27
|
# on :parse_error do |raw, error| ... end
|
|
27
28
|
# on :retry do |entry, error| ... end
|
|
28
29
|
# on :failure do |attempts| ... end
|
|
30
|
+
# on :warning do |message| ... end
|
|
29
31
|
def on(event, &block)
|
|
30
32
|
unless VALID_HOOKS.include?(event)
|
|
31
33
|
raise ArgumentError, "Unknown hook :#{event}. Valid hooks: #{VALID_HOOKS.map { |h| ":#{h}" }.join(", ")}"
|
|
@@ -136,7 +136,8 @@ module ActiveHarness
|
|
|
136
136
|
|
|
137
137
|
def use(provider:, model:, temperature: nil, name: nil, size: nil, quality: nil, retry_attempts: nil, retry_delay: nil,
|
|
138
138
|
questions: nil, response_format: nil, timestamp_granularities: nil, provider_options: nil,
|
|
139
|
-
chunking_strategy: nil, known_speaker_names: nil, known_speaker_references: nil
|
|
139
|
+
chunking_strategy: nil, known_speaker_names: nil, known_speaker_references: nil, prompt: nil,
|
|
140
|
+
aspect_ratio: nil, resolution: nil)
|
|
140
141
|
@models << {
|
|
141
142
|
provider: provider,
|
|
142
143
|
model: model,
|
|
@@ -152,7 +153,10 @@ module ActiveHarness
|
|
|
152
153
|
provider_options: provider_options,
|
|
153
154
|
chunking_strategy: chunking_strategy,
|
|
154
155
|
known_speaker_names: known_speaker_names,
|
|
155
|
-
known_speaker_references: known_speaker_references
|
|
156
|
+
known_speaker_references: known_speaker_references,
|
|
157
|
+
prompt: prompt,
|
|
158
|
+
aspect_ratio: aspect_ratio,
|
|
159
|
+
resolution: resolution
|
|
156
160
|
}.compact
|
|
157
161
|
end
|
|
158
162
|
|
|
@@ -29,8 +29,9 @@ module ActiveHarness
|
|
|
29
29
|
prompt = case sp
|
|
30
30
|
when String then sp
|
|
31
31
|
when NilClass then nil
|
|
32
|
-
when Proc then instance_exec(&sp)
|
|
32
|
+
when Proc then warn_multi_message_input; instance_exec(&sp)
|
|
33
33
|
when Class
|
|
34
|
+
warn_multi_message_input
|
|
34
35
|
instance = sp.new
|
|
35
36
|
inject_request_state(instance)
|
|
36
37
|
if instance.respond_to?(:call) then instance.call
|
|
@@ -38,6 +39,7 @@ module ActiveHarness
|
|
|
38
39
|
else instance.to_s
|
|
39
40
|
end
|
|
40
41
|
else
|
|
42
|
+
warn_multi_message_input
|
|
41
43
|
inject_request_state(sp)
|
|
42
44
|
if sp.respond_to?(:call) then sp.call
|
|
43
45
|
elsif sp.respond_to?(:text) then sp.text
|
|
@@ -50,9 +52,11 @@ module ActiveHarness
|
|
|
50
52
|
end
|
|
51
53
|
|
|
52
54
|
# Injects request state into a prompt class instance before #call.
|
|
53
|
-
# Available in prompt classes: @input
|
|
55
|
+
# Available in prompt classes: @input (text; for a message Array — the last user message),
|
|
56
|
+
# @transcript (role-labelled history), @context, @params, @memory, @context_window, @config
|
|
54
57
|
def inject_request_state(obj)
|
|
55
58
|
obj.instance_variable_set(:@input, @input)
|
|
59
|
+
obj.instance_variable_set(:@transcript, transcript)
|
|
56
60
|
obj.instance_variable_set(:@context, @context)
|
|
57
61
|
obj.instance_variable_set(:@params, @params)
|
|
58
62
|
obj.instance_variable_set(:@config, @config)
|
|
@@ -79,13 +79,17 @@ module ActiveHarness
|
|
|
79
79
|
end
|
|
80
80
|
|
|
81
81
|
def attempt_image_model(entry, system_prompt)
|
|
82
|
+
raise ArgumentError, "#{self.class.name}: image generation takes a text prompt, not a message Array" if @messages
|
|
82
83
|
factory = IMAGE_PROVIDERS[entry[:provider].to_sym]
|
|
83
84
|
raise ArgumentError, "Provider #{entry[:provider].inspect} does not support image generation. " \
|
|
84
85
|
"Supported image providers: #{IMAGE_PROVIDERS.keys.join(', ')}" unless factory
|
|
85
86
|
|
|
86
87
|
size = entry[:size] || @config[:image_size] || "1024x1024"
|
|
87
88
|
opts = { model: entry[:model], prompt: build_image_prompt(system_prompt, @input.to_s), size: size }
|
|
88
|
-
opts[:quality]
|
|
89
|
+
opts[:quality] = entry[:quality] if entry[:quality]
|
|
90
|
+
opts[:aspect_ratio] = entry[:aspect_ratio] if entry[:aspect_ratio]
|
|
91
|
+
opts[:resolution] = entry[:resolution] if entry[:resolution]
|
|
92
|
+
opts[:images] = @images if @images && !@images.empty?
|
|
89
93
|
factory.call.call(**opts)
|
|
90
94
|
end
|
|
91
95
|
|
|
@@ -99,6 +103,7 @@ module ActiveHarness
|
|
|
99
103
|
end
|
|
100
104
|
|
|
101
105
|
def attempt_transcription_model(entry)
|
|
106
|
+
raise ArgumentError, "#{self.class.name}: audio transcription takes a file path, not a message Array" if @messages
|
|
102
107
|
factory = TRANSCRIPTION_PROVIDERS[entry[:provider].to_sym]
|
|
103
108
|
raise ArgumentError, "Provider #{entry[:provider].inspect} does not support audio transcription. " \
|
|
104
109
|
"Supported transcription providers: #{TRANSCRIPTION_PROVIDERS.keys.join(', ')}" unless factory
|
|
@@ -117,6 +122,8 @@ module ActiveHarness
|
|
|
117
122
|
opts[:timestamp_granularities] = entry[:timestamp_granularities] if entry[:timestamp_granularities]
|
|
118
123
|
opts[:provider_options] = entry[:provider_options] if entry[:provider_options]
|
|
119
124
|
opts[:chunking_strategy] = entry[:chunking_strategy] if entry[:chunking_strategy]
|
|
125
|
+
opts[:prompt] = entry[:prompt] if entry[:prompt]
|
|
126
|
+
opts[:temperature] = entry[:temperature] if entry[:temperature]
|
|
120
127
|
opts[:known_speaker_names] = entry[:known_speaker_names] if entry[:known_speaker_names]
|
|
121
128
|
opts[:known_speaker_references] = entry[:known_speaker_references] if entry[:known_speaker_references]
|
|
122
129
|
factory.call.call(**opts)
|
|
@@ -18,10 +18,11 @@ module ActiveHarness
|
|
|
18
18
|
models: nil,
|
|
19
19
|
token: nil,
|
|
20
20
|
stream: nil,
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
tools: nil,
|
|
22
|
+
images: nil
|
|
23
23
|
)
|
|
24
24
|
new(
|
|
25
|
+
images: images,
|
|
25
26
|
input: input,
|
|
26
27
|
context: context,
|
|
27
28
|
params: params,
|
|
@@ -29,8 +30,7 @@ module ActiveHarness
|
|
|
29
30
|
models: models,
|
|
30
31
|
token: token,
|
|
31
32
|
stream: stream,
|
|
32
|
-
|
|
33
|
-
tools: tools
|
|
33
|
+
tools: tools
|
|
34
34
|
).call
|
|
35
35
|
end
|
|
36
36
|
|
|
@@ -55,12 +55,14 @@ module ActiveHarness
|
|
|
55
55
|
# -------------------------------------------------------------------------
|
|
56
56
|
# Instance API
|
|
57
57
|
# -------------------------------------------------------------------------
|
|
58
|
-
attr_accessor :
|
|
59
|
-
:context,
|
|
58
|
+
attr_accessor :context,
|
|
60
59
|
:params,
|
|
61
60
|
:memory,
|
|
62
|
-
:messages,
|
|
63
61
|
:tools
|
|
62
|
+
# Input images for an image request (image-to-image / restyling): file
|
|
63
|
+
# paths, "data:" URIs or http(s) URLs, sent along with the prompt.
|
|
64
|
+
attr_accessor :images
|
|
65
|
+
|
|
64
66
|
attr_reader :result,
|
|
65
67
|
:token,
|
|
66
68
|
:stream
|
|
@@ -78,14 +80,14 @@ module ActiveHarness
|
|
|
78
80
|
models: nil,
|
|
79
81
|
token: nil,
|
|
80
82
|
stream: nil,
|
|
81
|
-
|
|
82
|
-
|
|
83
|
+
tools: nil,
|
|
84
|
+
images: nil
|
|
83
85
|
)
|
|
84
|
-
@input = input
|
|
85
|
-
@messages = messages
|
|
86
86
|
@tools = tools
|
|
87
|
+
@images = Array(images)
|
|
87
88
|
@config = self.class.request_config
|
|
88
|
-
|
|
89
|
+
@warnings = []
|
|
90
|
+
assign_input(input)
|
|
89
91
|
@context = context
|
|
90
92
|
@params = params
|
|
91
93
|
@memory = memory
|
|
@@ -102,17 +104,13 @@ module ActiveHarness
|
|
|
102
104
|
# request.call("What is the capital of Japan?")
|
|
103
105
|
# request.call("...", stream: ->(token) { print token })
|
|
104
106
|
#
|
|
105
|
-
# Agent extension:
|
|
106
|
-
#
|
|
107
|
+
# Agent extension: +input+ may be an Array of messages (a conversation) and
|
|
108
|
+
# +tools:+ takes neutral tool schemas. The model may then answer with
|
|
107
109
|
# +result.tool_calls+; nothing is executed here — running tools is the Agent's job.
|
|
108
|
-
def call(input = nil, token: nil, stream: nil,
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
end
|
|
113
|
-
@messages = messages if messages
|
|
114
|
-
@tools = tools if tools
|
|
115
|
-
@input = last_user_content(@messages) if @input.nil? && @messages
|
|
110
|
+
def call(input = nil, token: nil, stream: nil, tools: nil)
|
|
111
|
+
@warnings = []
|
|
112
|
+
assign_input(input) if input
|
|
113
|
+
@tools = tools if tools
|
|
116
114
|
@token = token if token
|
|
117
115
|
@stream = stream if stream
|
|
118
116
|
fire(:before_call)
|
|
@@ -171,7 +169,7 @@ module ActiveHarness
|
|
|
171
169
|
model_cost = lookup_model_cost(entry)
|
|
172
170
|
|
|
173
171
|
Result.new(
|
|
174
|
-
input:
|
|
172
|
+
input: input,
|
|
175
173
|
output: raw,
|
|
176
174
|
processed: processed,
|
|
177
175
|
system_prompt: @system_prompt,
|
|
@@ -181,7 +179,8 @@ module ActiveHarness
|
|
|
181
179
|
execution_time: elapsed,
|
|
182
180
|
usage: build_usage(raw_usage, model_cost),
|
|
183
181
|
tool_calls: tool_calls,
|
|
184
|
-
message: tool_calls || @messages ? assistant_message(raw, tool_calls) : nil
|
|
182
|
+
message: tool_calls || @messages ? assistant_message(raw, tool_calls) : nil,
|
|
183
|
+
warnings: warnings.dup
|
|
185
184
|
)
|
|
186
185
|
end
|
|
187
186
|
|
|
@@ -191,11 +190,6 @@ module ActiveHarness
|
|
|
191
190
|
msg
|
|
192
191
|
end
|
|
193
192
|
|
|
194
|
-
def last_user_content(messages)
|
|
195
|
-
last = Array(messages).reverse.find { |m| (m[:role] || m["role"]).to_s == "user" }
|
|
196
|
-
last && (last[:content] || last["content"])
|
|
197
|
-
end
|
|
198
|
-
|
|
199
193
|
def build_model_info(entry, model_cost)
|
|
200
194
|
pricing = if model_cost&.input_per_million && model_cost&.output_per_million
|
|
201
195
|
ModelPricing.new(
|
|
@@ -238,6 +232,7 @@ module ActiveHarness
|
|
|
238
232
|
end
|
|
239
233
|
end
|
|
240
234
|
|
|
235
|
+
require_relative "request/conversation"
|
|
241
236
|
require_relative "request/prompt"
|
|
242
237
|
require_relative "request/hooks"
|
|
243
238
|
require_relative "request/models"
|
|
@@ -40,7 +40,7 @@ module ActiveHarness
|
|
|
40
40
|
|
|
41
41
|
# Result returned by Request#call (accessible via request.result).
|
|
42
42
|
#
|
|
43
|
-
# result.input —
|
|
43
|
+
# result.input — what was passed: a String, or an Array of messages
|
|
44
44
|
# result.output — raw string from the provider
|
|
45
45
|
# result.processed — parsed Hash/Array for :json requests, raw string for :text
|
|
46
46
|
# result.system_prompt — resolved system prompt string
|
|
@@ -51,6 +51,8 @@ module ActiveHarness
|
|
|
51
51
|
# result.usage — UsageInfo (tokens + cost), nil for streaming without usage
|
|
52
52
|
# result.tool_calls — Array of { id:, name:, arguments: } the model asked for (nil when none)
|
|
53
53
|
# result.message — the assistant message in neutral shape, ready to append to a messages list
|
|
54
|
+
# result.warnings — Array of non-fatal remarks, e.g. a multi-message input read as plain text
|
|
55
|
+
# result.meta — extra facts (Agent: status, stop_reason, run_id)
|
|
54
56
|
Result = Struct.new(
|
|
55
57
|
:input,
|
|
56
58
|
:output,
|
|
@@ -63,6 +65,8 @@ module ActiveHarness
|
|
|
63
65
|
:usage, # UsageInfo or nil
|
|
64
66
|
:tool_calls, # Array<Hash> or nil
|
|
65
67
|
:message, # { role: :assistant, content:, tool_calls: } or nil
|
|
68
|
+
:warnings, # Array<String> of non-fatal remarks (nil for results not built by a Request)
|
|
69
|
+
:meta, # Hash of extra facts (an Agent puts status / stop_reason / run_id here) or nil
|
|
66
70
|
keyword_init: true
|
|
67
71
|
)
|
|
68
72
|
end
|
|
@@ -8,7 +8,9 @@ module ActiveHarness
|
|
|
8
8
|
# include ActiveHarness::Tool::PathPolicy
|
|
9
9
|
# ALLOWED_PATHS = %w[/srv/app/workspace].freeze
|
|
10
10
|
#
|
|
11
|
-
# step
|
|
11
|
+
# step :resolve_path, transform: true do |input|
|
|
12
|
+
# input.merge(path: safe_path!(input[:path]))
|
|
13
|
+
# end
|
|
12
14
|
# end
|
|
13
15
|
module PathPolicy
|
|
14
16
|
# Returns a canonical absolute path inside ALLOWED_PATHS, or raises Denied.
|
data/lib/active_harness/tool.rb
CHANGED
|
@@ -43,11 +43,15 @@ module ActiveHarness
|
|
|
43
43
|
# call passes through your checks first.
|
|
44
44
|
#
|
|
45
45
|
# class LsTool < ActiveHarness::Tool
|
|
46
|
-
#
|
|
46
|
+
# tool_scope :os
|
|
47
|
+
# tool_name :ls
|
|
48
|
+
#
|
|
47
49
|
# description "List files in a directory"
|
|
48
50
|
# param :path, "Directory to list"
|
|
49
51
|
#
|
|
50
|
-
# step
|
|
52
|
+
# step :no_hidden do |input|
|
|
53
|
+
# deny!("hidden dirs are off limits") if input[:path].include?("/.")
|
|
54
|
+
# end
|
|
51
55
|
#
|
|
52
56
|
# def perform(input)
|
|
53
57
|
# Dir.children(input[:path]).first(200)
|
|
@@ -94,20 +98,22 @@ module ActiveHarness
|
|
|
94
98
|
class << self
|
|
95
99
|
# --- what the model sees ---
|
|
96
100
|
|
|
97
|
-
# Required namespace: :os, :web, :ruby ... Full name is "
|
|
98
|
-
|
|
99
|
-
|
|
101
|
+
# Required namespace: :os, :web, :ruby ... Full name is "tool_scope::tool_name".
|
|
102
|
+
# Both are explicit on purpose: the name the model sees must not change when a class is renamed.
|
|
103
|
+
def tool_scope(value = nil)
|
|
104
|
+
value ? @tool_scope = value.to_sym : @tool_scope
|
|
100
105
|
end
|
|
101
106
|
|
|
102
|
-
#
|
|
107
|
+
# Required: :ls, :fetch_url ... (a String works too)
|
|
103
108
|
def tool_name(value = nil)
|
|
104
|
-
value ? @tool_name = value.to_s :
|
|
109
|
+
value ? @tool_name = value.to_s : @tool_name
|
|
105
110
|
end
|
|
106
111
|
|
|
107
112
|
def full_name
|
|
108
|
-
raise ArgumentError, "#{name || 'Tool'} must declare a
|
|
113
|
+
raise ArgumentError, "#{name || 'Tool'} must declare a tool_scope (e.g. `tool_scope :os`)" unless tool_scope
|
|
114
|
+
raise ArgumentError, "#{name || 'Tool'} must declare a tool_name (e.g. `tool_name :ls`)" unless tool_name
|
|
109
115
|
|
|
110
|
-
"#{
|
|
116
|
+
"#{tool_scope}::#{tool_name}"
|
|
111
117
|
end
|
|
112
118
|
|
|
113
119
|
# Name on the wire: providers allow only [a-zA-Z0-9_-] in function names.
|
|
@@ -227,7 +233,7 @@ module ActiveHarness
|
|
|
227
233
|
|
|
228
234
|
def inherited(subclass)
|
|
229
235
|
super
|
|
230
|
-
subclass.instance_variable_set(:@
|
|
236
|
+
subclass.instance_variable_set(:@tool_scope, @tool_scope)
|
|
231
237
|
subclass.instance_variable_set(:@steps, steps.dup)
|
|
232
238
|
subclass.instance_variable_set(:@params, params.map(&:dup))
|
|
233
239
|
subclass.instance_variable_set(:@hooks, hooks.each_with_object({}) { |(k, v), h| h[k] = v.dup })
|
|
@@ -238,15 +244,6 @@ module ActiveHarness
|
|
|
238
244
|
def call(input:, context: {})
|
|
239
245
|
new(context: context).call(input)
|
|
240
246
|
end
|
|
241
|
-
|
|
242
|
-
private
|
|
243
|
-
|
|
244
|
-
def default_name
|
|
245
|
-
return nil unless name
|
|
246
|
-
|
|
247
|
-
base = name.split("::").last.sub(/Tool\z/, "")
|
|
248
|
-
base.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
|
|
249
|
-
end
|
|
250
247
|
end
|
|
251
248
|
|
|
252
249
|
include Core::HookRunner
|
data/lib/active_harness.rb
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
class <%= class_name %>Agent < ActiveHarness::Agent
|
|
2
|
+
# Hard limits so a looping model cannot run or spend forever.
|
|
3
|
+
max_turns 10
|
|
4
|
+
max_cost 0.50
|
|
5
|
+
|
|
2
6
|
# The Request that talks to the model each turn (owns model chain, retries, fallback).
|
|
3
7
|
request <%= class_name %>Request
|
|
4
8
|
|
|
@@ -7,8 +11,4 @@ class <%= class_name %>Agent < ActiveHarness::Agent
|
|
|
7
11
|
|
|
8
12
|
# Optional: judge the final answer; without it the first answer without tool calls ends the run.
|
|
9
13
|
# goal <%= class_name %>Goal
|
|
10
|
-
|
|
11
|
-
# Hard limits so a looping model cannot run or spend forever.
|
|
12
|
-
max_turns 10
|
|
13
|
-
max_cost 0.50
|
|
14
14
|
end
|
|
@@ -12,6 +12,15 @@ require_relative "../concerns/agent_tracing"
|
|
|
12
12
|
# agent.stop_reason # :goal_reached | :max_turns | ...
|
|
13
13
|
# puts agent.trace
|
|
14
14
|
class FilesAgent < ActiveHarness::Agent
|
|
15
|
+
# Hard limits: a model that loops or wanders must not run (or spend) forever.
|
|
16
|
+
# Whichever trips first ends the run with a matching stop_reason.
|
|
17
|
+
max_turns 8 # at most 8 Request calls
|
|
18
|
+
max_cost 0.50 # USD summed over all turns; stops NEW steps, a running call may overshoot
|
|
19
|
+
|
|
20
|
+
# Stop early when the model repeats the same tool call with the same input
|
|
21
|
+
# several turns in a row (stop_reason: :repeating).
|
|
22
|
+
loop_guard
|
|
23
|
+
|
|
15
24
|
include AgentTracing
|
|
16
25
|
|
|
17
26
|
# Which Request talks to the model each turn. It owns the model chain, retries and
|
|
@@ -27,13 +36,4 @@ class FilesAgent < ActiveHarness::Agent
|
|
|
27
36
|
# judges the final answer and can send the agent back for another turn.
|
|
28
37
|
# Without `goal`, the first answer without tool calls ends the run.
|
|
29
38
|
goal AnswerPresentGoal
|
|
30
|
-
|
|
31
|
-
# Hard limits: a model that loops or wanders must not run (or spend) forever.
|
|
32
|
-
# Whichever trips first ends the run with a matching stop_reason.
|
|
33
|
-
max_turns 8 # at most 8 Request calls
|
|
34
|
-
max_cost 0.50 # USD summed over all turns; stops NEW steps, a running call may overshoot
|
|
35
|
-
|
|
36
|
-
# Stop early when the model repeats the same tool call with the same input
|
|
37
|
-
# several turns in a row (stop_reason: :repeating).
|
|
38
|
-
loop_guard
|
|
39
39
|
end
|
|
@@ -29,7 +29,7 @@ module AgentTracing
|
|
|
29
29
|
|
|
30
30
|
base.on(:complete) do |result|
|
|
31
31
|
usage = result.usage
|
|
32
|
-
trace_log "#{status}/#{stop_reason} — #{turns.size} turns, #{tool_calls.size} tool calls (#{denied_calls.size} refused), " \
|
|
32
|
+
trace_log "#{status}/#{stop_reason} — #{turns.size} turns, #{tool_calls.size} tool calls (#{denied_calls.size} refused or failed), " \
|
|
33
33
|
"#{usage&.tokens&.total.to_i} tokens#{format(", $%.6f", usage.cost.total) if usage&.cost}, #{result.execution_time}s"
|
|
34
34
|
end
|
|
35
35
|
end
|
|
@@ -5,9 +5,10 @@ class LsTool < ActiveHarness::Tool
|
|
|
5
5
|
include ActiveHarness::Tool::PathPolicy # safe_path!: resolves symlinks and ".." and checks ALLOWED_PATHS
|
|
6
6
|
include ToolTracing # logs every step; see app/ai/concerns/
|
|
7
7
|
|
|
8
|
-
# `
|
|
9
|
-
#
|
|
10
|
-
|
|
8
|
+
# `tool_scope` namespaces the tool (os::ls vs web::ls) and lets you allow or log a whole group;
|
|
9
|
+
# `tool_name` is the name the model sees. Both are explicit: renaming the class changes nothing for the model.
|
|
10
|
+
tool_scope :os
|
|
11
|
+
tool_name :ls
|
|
11
12
|
|
|
12
13
|
# What the model reads when it decides whether to call this tool — write it for the model.
|
|
13
14
|
description "List files in a directory inside the workspace"
|
|
@@ -6,7 +6,9 @@ class ReadFileTool < ActiveHarness::Tool
|
|
|
6
6
|
include ActiveHarness::Tool::PathPolicy
|
|
7
7
|
include ToolTracing
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
tool_scope :os # full name: os::read_file
|
|
10
|
+
tool_name :read_file
|
|
11
|
+
|
|
10
12
|
description "Read the first lines of a text file inside the workspace"
|
|
11
13
|
|
|
12
14
|
param :path, "Absolute path of the file"
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
class <%= class_name %>Tool < ActiveHarness::Tool
|
|
2
|
-
#
|
|
3
|
-
|
|
2
|
+
# Namespace + name the model sees: <%= file_name %> becomes app::<%= file_name %> (wire name app__<%= file_name %>).
|
|
3
|
+
# Both are explicit, so renaming this class never changes what the model calls. Use :os, :web, :ruby ... per area.
|
|
4
|
+
tool_scope :app
|
|
5
|
+
tool_name :<%= file_name %>
|
|
4
6
|
|
|
5
7
|
# Written for the model: when to call this tool.
|
|
6
8
|
description "Describe what this tool does for the model"
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: active_harness
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.3.
|
|
4
|
+
version: 0.3.11
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- the-teacher
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-24 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: concurrent-ruby
|
|
@@ -95,6 +95,7 @@ files:
|
|
|
95
95
|
- lib/active_harness/providers/xai.rb
|
|
96
96
|
- lib/active_harness/railtie.rb
|
|
97
97
|
- lib/active_harness/request.rb
|
|
98
|
+
- lib/active_harness/request/conversation.rb
|
|
98
99
|
- lib/active_harness/request/cost.rb
|
|
99
100
|
- lib/active_harness/request/custom_llm_backend.rb
|
|
100
101
|
- lib/active_harness/request/hooks.rb
|