smith-agents 0.14.0 → 0.16.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.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +147 -0
  3. data/README.md +6 -4
  4. data/docs/AGENTS.md +64 -8
  5. data/docs/BUDGETS.md +14 -1
  6. data/docs/CONFIGURATION.md +9 -3
  7. data/docs/ERRORS_AND_RETRIES.md +13 -0
  8. data/docs/PATTERNS.md +98 -8
  9. data/docs/PERSISTENCE.md +21 -7
  10. data/docs/TOOLS_AND_GUARDRAILS.md +7 -0
  11. data/lib/smith/agent/content_parts.rb +18 -8
  12. data/lib/smith/agent/image_request.rb +38 -0
  13. data/lib/smith/agent/invocation_preparation.rb +2 -2
  14. data/lib/smith/agent/lifecycle.rb +2 -0
  15. data/lib/smith/agent/output_integrity.rb +1 -1
  16. data/lib/smith/agent/output_recovery.rb +10 -1
  17. data/lib/smith/agent/paint_input.rb +42 -0
  18. data/lib/smith/agent/painted_answer.rb +70 -0
  19. data/lib/smith/agent/prompt_cache.rb +31 -0
  20. data/lib/smith/agent/provider_call_timing.rb +2 -1
  21. data/lib/smith/agent/provider_completion.rb +4 -3
  22. data/lib/smith/agent/provider_failure_handling.rb +27 -5
  23. data/lib/smith/agent/provider_paint.rb +128 -0
  24. data/lib/smith/agent/provider_request.rb +29 -4
  25. data/lib/smith/agent/provider_usage.rb +27 -2
  26. data/lib/smith/agent/usage_tracking.rb +3 -1
  27. data/lib/smith/agent.rb +6 -1
  28. data/lib/smith/budget/host_ledger.rb +3 -1
  29. data/lib/smith/budget/request_hold.rb +49 -3
  30. data/lib/smith/errors.rb +8 -0
  31. data/lib/smith/pricing/catalog_validation.rb +20 -1
  32. data/lib/smith/pricing/images.rb +29 -0
  33. data/lib/smith/pricing.rb +37 -16
  34. data/lib/smith/providers/output_limit.rb +22 -1
  35. data/lib/smith/providers/request_holds.rb +29 -4
  36. data/lib/smith/providers/request_outcome.rb +39 -0
  37. data/lib/smith/providers/stop_reason.rb +4 -2
  38. data/lib/smith/streaming/session.rb +2 -0
  39. data/lib/smith/streaming.rb +3 -1
  40. data/lib/smith/version.rb +1 -1
  41. data/lib/smith/workflow/agent_result.rb +5 -1
  42. data/lib/smith/workflow/composite/effects.rb +3 -1
  43. data/lib/smith/workflow/evaluator_optimizer.rb +93 -17
  44. data/lib/smith/workflow/execution.rb +1 -0
  45. data/lib/smith/workflow/failure_reconstructor.rb +1 -0
  46. data/lib/smith/workflow/failure_record.rb +7 -3
  47. data/lib/smith/workflow/failure_record_validator.rb +7 -7
  48. data/lib/smith/workflow/graph/optimization_contract.rb +1 -0
  49. data/lib/smith/workflow/graph/stream_diagnostic.rb +9 -4
  50. data/lib/smith/workflow/graph/transition_optimization_configuration.rb +1 -0
  51. data/lib/smith/workflow/guarded_step_execution.rb +1 -1
  52. data/lib/smith/workflow/nested_execution.rb +18 -6
  53. data/lib/smith/workflow/nested_items_execution.rb +122 -0
  54. data/lib/smith/workflow/optimization_state.rb +7 -2
  55. data/lib/smith/workflow/optimizer_batch.rb +21 -9
  56. data/lib/smith/workflow/parallel/nested_execution.rb +4 -1
  57. data/lib/smith/workflow/parallel/root_execution.rb +2 -1
  58. data/lib/smith/workflow/parallel.rb +13 -5
  59. data/lib/smith/workflow/step_io.rb +10 -4
  60. data/lib/smith/workflow/step_stream.rb +9 -0
  61. data/lib/smith/workflow/transition.rb +54 -19
  62. data/lib/smith/workflow/usage_entry.rb +5 -0
  63. data/lib/smith.rb +2 -0
  64. metadata +9 -1
@@ -28,17 +28,23 @@ module Smith
28
28
  count
29
29
  end
30
30
 
31
- def self.execute(branches:)
31
+ # `concurrency` caps how many of these branches run at once, below the
32
+ # configured parallel_concurrency.
33
+ def self.execute(branches:, concurrency: nil)
32
34
  validate_branch_count!(branches.length)
33
35
  context = ExecutionContext.current
34
- return NestedExecution.new(branches:, context:).call if context
36
+ return NestedExecution.new(branches:, context:, concurrency:).call if context
35
37
 
36
- RootExecution.new(branches:).call
38
+ RootExecution.new(branches:, concurrency:).call
37
39
  end
38
40
 
41
+ # A request that may have been billed outranks an interruption, which
42
+ # takes the step again: a branch's uncertain request is never sent
43
+ # twice because another branch was cut short.
39
44
  def self.preferred_error(reasons)
40
45
  errors = Array(reasons).compact
41
46
  process_fatal_error(errors) ||
47
+ uncertain_request(errors) ||
42
48
  interruption(errors) ||
43
49
  notification_failure(errors) ||
44
50
  terminal_execution_error(errors) ||
@@ -48,6 +54,8 @@ module Smith
48
54
 
49
55
  def self.process_fatal_error(errors) = errors.find { !_1.is_a?(StandardError) }
50
56
 
57
+ def self.uncertain_request(errors) = errors.find { _1.is_a?(RequestOutcomeUncertain) }
58
+
51
59
  def self.interruption(errors) = errors.find { Interruptions.find(_1) }
52
60
 
53
61
  def self.notification_failure(errors) = errors.find { _1.is_a?(ToolFailureNotificationFailed) }
@@ -56,8 +64,8 @@ module Smith
56
64
 
57
65
  def self.ordinary_error(errors) = errors.find { !_1.is_a?(Cancellation) }
58
66
 
59
- private_class_method :process_fatal_error, :interruption, :notification_failure, :terminal_execution_error,
60
- :ordinary_error
67
+ private_class_method :process_fatal_error, :uncertain_request, :interruption, :notification_failure,
68
+ :terminal_execution_error, :ordinary_error
61
69
  end
62
70
  end
63
71
  end
@@ -74,14 +74,20 @@ module Smith
74
74
  "agent in an isolated session and must declare input:"
75
75
  end
76
76
 
77
- # A non-empty Array of at most MessageBatch::MAX_MESSAGES user and
78
- # assistant messages, bounded and JSON-shaped like session messages.
79
77
  # The callable gets a copy of the context, so it cannot change the
80
78
  # run's.
81
79
  def step_input_messages(transition)
82
- messages = transition.step_input.call(snapshot_value(@context))
80
+ checked_input(transition.step_input.call(snapshot_value(@context)), "input:")
81
+ end
82
+
83
+ # A non-empty Array of at most MessageBatch::MAX_MESSAGES user and
84
+ # assistant messages, bounded and JSON-shaped like session messages, as
85
+ # a step's `input:` or an optimizer's callable `evaluator_context:`
86
+ # (`option`) returns them.
87
+ def checked_input(messages, option)
83
88
  unless messages.is_a?(Array) && !messages.empty? && messages.length <= MessageBatch::MAX_MESSAGES
84
- raise WorkflowError, "input: must return a non-empty Array of at most #{MessageBatch::MAX_MESSAGES} messages"
89
+ raise WorkflowError,
90
+ "#{option} must return a non-empty Array of at most #{MessageBatch::MAX_MESSAGES} messages"
85
91
  end
86
92
 
87
93
  MessageValueNormalizer.new(messages, label: "step input").call.map { |message| step_input_message(message) }
@@ -30,6 +30,15 @@ module Smith
30
30
  @step_stream = nil
31
31
  end
32
32
 
33
+ # Calls the block's agent unheard by the step's sink.
34
+ def unstreamed
35
+ stream = @step_stream
36
+ @step_stream = nil
37
+ yield
38
+ ensure
39
+ @step_stream = stream
40
+ end
41
+
33
42
  def accept_streamed(session, output)
34
43
  return session.finish(output) unless @persistence_key
35
44
 
@@ -9,7 +9,7 @@ module Smith
9
9
  :router_config, :workflow_class, :optimization_config, :orchestrator_config,
10
10
  :fanout_config, :retry_config, :deterministic_block, :deterministic_kind,
11
11
  :deterministic_routes, :deterministic_fallthrough, :await_config, :timeout_transition,
12
- :step_input, :output_to, :stream, :batch_mode, :on_request_change
12
+ :step_input, :output_to, :stream, :batch_mode, :on_request_change, :nested_items
13
13
 
14
14
  def initialize(name, from:, to:, &)
15
15
  @name = own_identifier(name)
@@ -115,11 +115,22 @@ module Smith
115
115
  commit_execution_primitive!("route")
116
116
  end
117
117
 
118
- def workflow(klass)
118
+ # Runs `klass` in-process as one step. With `over:` (a callable of a
119
+ # copy of the context returning an Array of items) it is a fan-out
120
+ # over items: one nested run an item, each with its item under the
121
+ # context key `item_to:`, at most `concurrency:` at once (the
122
+ # configured parallel_concurrency when not given). Each item's outcome
123
+ # is its own: `{ "output" => ... }`, or `{ "refused" => message }` when
124
+ # its run ended on a refused response (OutputRefused), in the items'
125
+ # order; any other failure fails the step. No items is no runs and an
126
+ # empty Array. `output_to:` names the context key the outcomes go to.
127
+ def workflow(klass, over: nil, item_to: nil, concurrency: nil, output_to: nil)
119
128
  raise WorkflowError, "workflow binding must be a Class" unless klass.is_a?(Class)
120
129
  raise WorkflowError, "workflow binding must be a Smith::Workflow subclass" unless klass < Workflow
121
130
 
122
131
  validate_execution_primitive_conflict!("workflow")
132
+ @nested_items = nested_items!(over, item_to, concurrency)
133
+ assign_step_io!(nil, output_to) if output_to
123
134
 
124
135
  @workflow_class = klass
125
136
  commit_execution_primitive!("workflow")
@@ -127,9 +138,15 @@ module Smith
127
138
 
128
139
  # `mode:` and `on_request_change:` as on `execute`: in batch mode each
129
140
  # of the loop's calls is a batch request of its own (OptimizerBatch).
141
+ # `stream:` as on `execute`, for the generator's replies alone, round
142
+ # after round: a round the evaluator or `before_eval` rejects is
143
+ # withdrawn (`reset(:rejected)`), and the evaluator never streams.
144
+ # `before_generate:` runs before each round's generator is asked, so a
145
+ # host can set aside what an earlier round or attempt left.
130
146
  def optimize(generator:, evaluator:, max_rounds:, evaluator_schema:,
131
147
  improvement_threshold: nil,
132
148
  evaluator_context: nil,
149
+ before_generate: nil,
133
150
  before_eval: nil,
134
151
  on_exhaustion: :raise,
135
152
  on_converged: :raise,
@@ -137,15 +154,16 @@ module Smith
137
154
  input: nil,
138
155
  output_to: nil,
139
156
  mode: nil,
140
- on_request_change: nil)
157
+ on_request_change: nil,
158
+ stream: nil)
141
159
  validate_execution_primitive_conflict!("optimize")
142
160
  assign_step_io!(input, output_to)
143
- assign_batch_mode!(mode, on_request_change)
161
+ assign_batch_mode!(mode, on_request_change, stream: stream_name!(stream, {}, primitive: "optimize"))
144
162
  validate_optimize_controls!(generator, evaluator, max_rounds, evaluator_schema)
145
163
  validate_optimize_exit_modes!(on_exhaustion: on_exhaustion, on_converged: on_converged,
146
164
  on_threshold: on_threshold)
147
165
  validate_optimize_evaluator_context!(evaluator_context)
148
- validate_optimize_before_eval!(before_eval)
166
+ validate_optimize_callbacks!(before_generate:, before_eval:)
149
167
 
150
168
  @optimization_config = {
151
169
  generator: normalize_agent_reference!(generator, "optimizer generator"),
@@ -153,7 +171,7 @@ module Smith
153
171
  max_rounds: max_rounds,
154
172
  evaluator_schema: evaluator_schema, improvement_threshold: improvement_threshold,
155
173
  evaluator_context: evaluator_context,
156
- before_eval: before_eval,
174
+ before_generate: before_generate, before_eval: before_eval,
157
175
  on_exhaustion: on_exhaustion,
158
176
  on_converged: on_converged,
159
177
  on_threshold: on_threshold
@@ -325,10 +343,12 @@ module Smith
325
343
  mode
326
344
  end
327
345
 
328
- # `mode:` and `on_request_change:` of a step that has no stream and no
329
- # parallel copies (`fan_out`, `optimize`).
330
- def assign_batch_mode!(mode, on_request_change)
331
- @batch_mode = batch_mode!(mode, stream: nil, parallel: false)
346
+ # `mode:` and `on_request_change:` of a step that has no parallel copies
347
+ # (`fan_out`, `optimize`), and the sink it streams to (`optimize`'s,
348
+ # named through stream_name!).
349
+ def assign_batch_mode!(mode, on_request_change, stream: nil)
350
+ @stream = stream
351
+ @batch_mode = batch_mode!(mode, stream:, parallel: false)
332
352
  @on_request_change = request_change!(on_request_change)
333
353
  end
334
354
 
@@ -346,10 +366,10 @@ module Smith
346
366
  end
347
367
 
348
368
  # One reply streams to one sink: a parallel step has several.
349
- def stream_name!(stream, opts)
369
+ def stream_name!(stream, opts, primitive: "execute")
350
370
  return if stream.nil?
351
- raise WorkflowError, "execute stream: must name a sink with a Symbol" unless stream.is_a?(Symbol)
352
- raise WorkflowError, "execute stream: is not available on a parallel step" if opts[:parallel] == true
371
+ raise WorkflowError, "#{primitive} stream: must name a sink with a Symbol" unless stream.is_a?(Symbol)
372
+ raise WorkflowError, "#{primitive} stream: is not available on a parallel step" if opts[:parallel] == true
353
373
 
354
374
  stream
355
375
  end
@@ -465,6 +485,20 @@ module Smith
465
485
  normalized.freeze
466
486
  end
467
487
 
488
+ def nested_items!(over, item_to, concurrency)
489
+ return if [over, item_to, concurrency].all?(&:nil?)
490
+ raise WorkflowError, "workflow over: must respond to call" unless over.respond_to?(:call)
491
+ raise WorkflowError, "workflow item_to: must be a Symbol context key" unless item_to.is_a?(Symbol)
492
+
493
+ { over:, item_to:, concurrency: nested_concurrency!(concurrency) }.freeze
494
+ end
495
+
496
+ def nested_concurrency!(concurrency)
497
+ return concurrency if concurrency.nil? || (concurrency.is_a?(Integer) && concurrency.positive?)
498
+
499
+ raise WorkflowError, "workflow concurrency: must be a positive Integer"
500
+ end
501
+
468
502
  def validate_fanout_size!(branches)
469
503
  limit = Smith.config.parallel_branch_limit
470
504
  return if branches.length <= limit
@@ -705,17 +739,18 @@ module Smith
705
739
  end
706
740
 
707
741
  def validate_optimize_evaluator_context!(evaluator_context)
708
- return if evaluator_context.nil? || evaluator_context == :inject_state
742
+ return if evaluator_context.nil? || evaluator_context == :inject_state || evaluator_context.respond_to?(:call)
709
743
 
710
744
  raise WorkflowError,
711
- "optimize evaluator_context must be nil or :inject_state; got #{evaluator_context.inspect}"
745
+ "optimize evaluator_context must be nil, :inject_state or a callable; got #{evaluator_context.inspect}"
712
746
  end
713
747
 
714
- def validate_optimize_before_eval!(before_eval)
715
- return if before_eval.nil?
716
- return if before_eval.respond_to?(:call)
748
+ def validate_optimize_callbacks!(**callbacks)
749
+ callbacks.each do |name, callback|
750
+ next if callback.nil? || callback.respond_to?(:call)
717
751
 
718
- raise WorkflowError, "optimize before_eval must respond to :call; got #{before_eval.inspect}"
752
+ raise WorkflowError, "optimize #{name} must respond to :call; got #{callback.inspect}"
753
+ end
719
754
  end
720
755
  end
721
756
  end
@@ -26,6 +26,8 @@ module Smith
26
26
  # `thinking_tokens` are already inside `output_tokens`. `stop_reason` and
27
27
  # `provider_message_id` come from the provider response, and `mode` is
28
28
  # how the call was made (`:sync`, `:stream`, `:batch`).
29
+ # `image_input_tokens` and `image_output_tokens` are the image parts of
30
+ # `input_tokens` and `output_tokens`.
29
31
  # rubocop:disable Style/RedundantStructKeywordInit
30
32
  UsageEntry = Struct.new(
31
33
  :usage_id,
@@ -48,6 +50,8 @@ module Smith
48
50
  :stop_reason,
49
51
  :provider_message_id,
50
52
  :mode,
53
+ :image_input_tokens,
54
+ :image_output_tokens,
51
55
  keyword_init: true
52
56
  ) do
53
57
  def initialize(**attributes)
@@ -83,6 +87,7 @@ module Smith
83
87
  UsageEntry::OPTIONAL_MEMBERS = %i[
84
88
  transition branch_key round attempt_id workflow
85
89
  cache_read_tokens cache_write_tokens thinking_tokens stop_reason provider_message_id mode
90
+ image_input_tokens image_output_tokens
86
91
  ].freeze
87
92
  # rubocop:enable Style/RedundantStructKeywordInit
88
93
  end
data/lib/smith.rb CHANGED
@@ -206,6 +206,7 @@ require_relative "smith/errors"
206
206
  require_relative "smith/output_failure"
207
207
  require_relative "smith/child_runs_failed"
208
208
  require_relative "smith/providers/stop_reason"
209
+ require_relative "smith/providers/request_outcome"
209
210
  require_relative "smith/providers/output_limit"
210
211
  require_relative "smith/providers/schema_constraints"
211
212
  require_relative "smith/tool_failure_notification_failed"
@@ -351,6 +352,7 @@ require_relative "smith/workflow/artifact_integration"
351
352
  require_relative "smith/workflow/data_volume_policy"
352
353
  require_relative "smith/workflow/deadline_enforcement"
353
354
  require_relative "smith/workflow/nested_execution"
355
+ require_relative "smith/workflow/nested_items_execution"
354
356
  require_relative "smith/workflow/evaluator_optimizer"
355
357
  require_relative "smith/workflow/orchestrator_worker"
356
358
  require_relative "smith/workflow/parallel_execution"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smith-agents
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0
4
+ version: 0.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Ralak
@@ -170,6 +170,7 @@ files:
170
170
  - lib/smith/agent/content_parts.rb
171
171
  - lib/smith/agent/dynamic_configuration.rb
172
172
  - lib/smith/agent/fallback_configuration.rb
173
+ - lib/smith/agent/image_request.rb
173
174
  - lib/smith/agent/invocation_preparation.rb
174
175
  - lib/smith/agent/lifecycle.rb
175
176
  - lib/smith/agent/model_reference.rb
@@ -178,11 +179,15 @@ files:
178
179
  - lib/smith/agent/output_integrity.rb
179
180
  - lib/smith/agent/output_policy.rb
180
181
  - lib/smith/agent/output_recovery.rb
182
+ - lib/smith/agent/paint_input.rb
183
+ - lib/smith/agent/painted_answer.rb
184
+ - lib/smith/agent/prompt_cache.rb
181
185
  - lib/smith/agent/provider_attempt.rb
182
186
  - lib/smith/agent/provider_call_timing.rb
183
187
  - lib/smith/agent/provider_candidate_sequence.rb
184
188
  - lib/smith/agent/provider_completion.rb
185
189
  - lib/smith/agent/provider_failure_handling.rb
190
+ - lib/smith/agent/provider_paint.rb
186
191
  - lib/smith/agent/provider_request.rb
187
192
  - lib/smith/agent/provider_streaming.rb
188
193
  - lib/smith/agent/provider_usage.rb
@@ -298,6 +303,7 @@ files:
298
303
  - lib/smith/persistence_payload_conflict.rb
299
304
  - lib/smith/pricing.rb
300
305
  - lib/smith/pricing/catalog_validation.rb
306
+ - lib/smith/pricing/images.rb
301
307
  - lib/smith/pricing_configuration_error.rb
302
308
  - lib/smith/provider_errors.rb
303
309
  - lib/smith/provider_permanent_failure.rb
@@ -308,6 +314,7 @@ files:
308
314
  - lib/smith/providers/openai/tools_extensions.rb
309
315
  - lib/smith/providers/output_limit.rb
310
316
  - lib/smith/providers/request_holds.rb
317
+ - lib/smith/providers/request_outcome.rb
311
318
  - lib/smith/providers/response_facts.rb
312
319
  - lib/smith/providers/schema_constraint_check.rb
313
320
  - lib/smith/providers/schema_constraints.rb
@@ -521,6 +528,7 @@ files:
521
528
  - lib/smith/workflow/message_roles.rb
522
529
  - lib/smith/workflow/message_value_normalizer.rb
523
530
  - lib/smith/workflow/nested_execution.rb
531
+ - lib/smith/workflow/nested_items_execution.rb
524
532
  - lib/smith/workflow/optimization_state.rb
525
533
  - lib/smith/workflow/optimizer_batch.rb
526
534
  - lib/smith/workflow/orchestration_state.rb