ruby_llm-ag_ui 0.3.0 → 1.0.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 (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +70 -0
  3. data/CONTRIBUTING.md +4 -0
  4. data/README.md +2 -2
  5. data/docs/_advanced/client-example.md +7 -0
  6. data/docs/_advanced/provider-verification.md +70 -0
  7. data/docs/_advanced/rails-integration.md +9 -1
  8. data/docs/_advanced/security-and-deployment.md +2 -2
  9. data/docs/_core_features/approval-and-resume.md +9 -0
  10. data/docs/_core_features/reconnects.md +15 -0
  11. data/docs/_core_features/streaming-runs.md +9 -1
  12. data/docs/_getting_started/configuration.md +7 -2
  13. data/docs/_getting_started/getting-started.md +1 -1
  14. data/docs/_getting_started/overview.md +2 -1
  15. data/docs/_getting_started/whats-new-in-1-0.md +103 -0
  16. data/docs/_reference/api-reference.md +2 -1
  17. data/docs/_reference/compatibility.md +211 -8
  18. data/docs/_reference/protocol-coverage.md +1 -1
  19. data/docs/_reference/public-api.md +103 -0
  20. data/docs/_reference/release-history.md +3 -1
  21. data/docs/_reference/releasing.md +5 -0
  22. data/docs/_reference/upgrading.md +46 -27
  23. data/docs/{_getting_started → _reference}/whats-new-in-0-3.md +2 -1
  24. data/docs/_reference/whats-new-in-0-4.md +58 -0
  25. data/lib/ruby_llm/ag_ui/configuration.rb +16 -12
  26. data/lib/ruby_llm/ag_ui/emitter.rb +1 -1
  27. data/lib/ruby_llm/ag_ui/error_sanitizer.rb +1 -1
  28. data/lib/ruby_llm/ag_ui/immutable.rb +34 -0
  29. data/lib/ruby_llm/ag_ui/input.rb +12 -6
  30. data/lib/ruby_llm/ag_ui/interrupt.rb +10 -10
  31. data/lib/ruby_llm/ag_ui/mapper.rb +60 -15
  32. data/lib/ruby_llm/ag_ui/message_parser.rb +1 -1
  33. data/lib/ruby_llm/ag_ui/rails/active_record_store.rb +18 -9
  34. data/lib/ruby_llm/ag_ui/rails/streaming.rb +8 -2
  35. data/lib/ruby_llm/ag_ui/reconnect.rb +1 -1
  36. data/lib/ruby_llm/ag_ui/redaction.rb +3 -1
  37. data/lib/ruby_llm/ag_ui/resume.rb +1 -1
  38. data/lib/ruby_llm/ag_ui/resume_entry.rb +3 -3
  39. data/lib/ruby_llm/ag_ui/run.rb +18 -13
  40. data/lib/ruby_llm/ag_ui/run_record.rb +10 -10
  41. data/lib/ruby_llm/ag_ui/sse_writer.rb +20 -3
  42. data/lib/ruby_llm/ag_ui/store/memory_store.rb +3 -3
  43. data/lib/ruby_llm/ag_ui/validation.rb +1 -1
  44. data/lib/ruby_llm/ag_ui/version.rb +1 -1
  45. metadata +9 -4
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLLM
4
+ module AgUI
5
+ # Isolated JSON-shaped values for request and storage boundaries.
6
+ module Immutable # :nodoc:
7
+ module_function
8
+
9
+ def copy(value)
10
+ case value
11
+ when Hash
12
+ value.to_h { |key, item| [copy(key), copy(item)] }.freeze
13
+ when Array
14
+ value.map { |item| copy(item) }.freeze
15
+ when String, Time
16
+ value.dup.freeze
17
+ when ::AgUiProtocol::Core::Types::Model
18
+ copy_model(value)
19
+ else
20
+ value
21
+ end
22
+ end
23
+
24
+ def copy_model(value)
25
+ value.dup.tap do |model|
26
+ model.instance_variables.each do |name|
27
+ model.instance_variable_set(name, copy(model.instance_variable_get(name)))
28
+ end
29
+ end.freeze
30
+ end
31
+ private_class_method :copy_model
32
+ end
33
+ end
34
+ end
@@ -17,8 +17,8 @@ module RubyLLM
17
17
  #
18
18
  # Nothing here grants trust. Client messages, state, tools, context and
19
19
  # forwardedProps are accepted as data only; the host decides which tools
20
- # and model a run may use, and client history is reconciled by id against
21
- # host-stored history rather than appended as trusted system or tool turns.
20
+ # and model a run may use. This gem never reconciles or appends client
21
+ # history to the host chat. The host owns that policy.
22
22
  class Input
23
23
  # The protocol versions this gem can act on.
24
24
  SUPPORTED_PROTOCOL_VERSIONS = [Protocol::PROTOCOL_VERSION].freeze
@@ -71,6 +71,7 @@ module RubyLLM
71
71
  end
72
72
 
73
73
  def initialize(attributes)
74
+ attributes = Immutable.copy(attributes)
74
75
  @thread_id = attributes[:thread_id]
75
76
  @run_id = attributes[:run_id]
76
77
  @parent_run_id = attributes[:parent_run_id]
@@ -98,8 +99,8 @@ module RubyLLM
98
99
  @tools.filter_map { |tool| tool['name'] }
99
100
  end
100
101
 
101
- # Whether the client supplied tool definitions. The first release never
102
- # grants them; a run reports them and stops.
102
+ # Whether the client supplied tool definitions. Informational only: the
103
+ # gem never registers a client tool on the host's chat.
103
104
  def client_tools?
104
105
  !@tools.empty?
105
106
  end
@@ -118,7 +119,7 @@ module RubyLLM
118
119
  #
119
120
  # Kept separate from Input so the value object stays immutable and every
120
121
  # failure message stays in one place.
121
- class Parser
122
+ class Parser # :nodoc:
122
123
  include Validation
123
124
 
124
125
  MAX_NESTING = 32
@@ -132,10 +133,15 @@ module RubyLLM
132
133
  @parts = PartBuilder.new
133
134
  end
134
135
 
136
+ # The digest re-encodes the accepted values, so text that is not UTF-8
137
+ # or a number that overflowed to Infinity is refused here as malformed
138
+ # input rather than escaping as an encoder error.
135
139
  def parse
136
140
  document = strip_unknown_members(decode(read_body))
137
141
  validate_common(document)
138
142
  build(document)
143
+ rescue JSON::GeneratorError
144
+ raise MalformedInputError, 'The request body must be valid UTF-8 JSON.'
139
145
  end
140
146
 
141
147
  private
@@ -246,7 +252,7 @@ module RubyLLM
246
252
 
247
253
  # Builds the composite members of a RunAgentInput: the client tool
248
254
  # definitions, the context entries and the resume entries.
249
- class PartBuilder
255
+ class PartBuilder # :nodoc:
250
256
  include Validation
251
257
 
252
258
  def tools(values)
@@ -69,15 +69,15 @@ module RubyLLM
69
69
  # rubocop:disable Metrics/ParameterLists -- one keyword per field the value object owns
70
70
  def initialize(id:, tool_call_id:, arguments_digest:, expires_at:, reason: REASON_TOOL_CALL,
71
71
  message: nil, status: PENDING, decided_at: nil, created_at: nil)
72
- @id = id
73
- @tool_call_id = tool_call_id
74
- @arguments_digest = arguments_digest
75
- @expires_at = expires_at
76
- @reason = reason
77
- @message = message
72
+ @id = Immutable.copy(id)
73
+ @tool_call_id = Immutable.copy(tool_call_id)
74
+ @arguments_digest = Immutable.copy(arguments_digest)
75
+ @expires_at = Immutable.copy(expires_at)
76
+ @reason = Immutable.copy(reason)
77
+ @message = Immutable.copy(message)
78
78
  @status = assert_status!(status)
79
- @decided_at = decided_at
80
- @created_at = created_at
79
+ @decided_at = Immutable.copy(decided_at)
80
+ @created_at = Immutable.copy(created_at)
81
81
  freeze
82
82
  end
83
83
 
@@ -146,7 +146,7 @@ module RubyLLM
146
146
  reason: reason,
147
147
  message: message,
148
148
  tool_call_id: tool_call_id,
149
- expires_at: expires_at&.utc&.iso8601
149
+ expires_at: expires_at&.getutc&.iso8601
150
150
  )
151
151
  end
152
152
 
@@ -194,7 +194,7 @@ module RubyLLM
194
194
  private
195
195
 
196
196
  def iso8601(time)
197
- time&.utc&.iso8601
197
+ time&.getutc&.iso8601
198
198
  end
199
199
 
200
200
  def assert_status!(value)
@@ -47,12 +47,12 @@ module RubyLLM
47
47
  @run_id = run_id
48
48
  @redact = redact || RubyLLM::AgUI.config.redact
49
49
  @logger = logger || RubyLLM::AgUI.config.logger
50
- @message_count = 0
51
- @step_count = 0
50
+ @message_count = @step_count = 0
52
51
  @open_text_id = nil
53
52
  @last_message_id = nil
54
53
  @text_emitted = false
55
54
  @calls = {}
55
+ end_stream_keys
56
56
  @completed_tools = {}
57
57
  @open_step = nil
58
58
  @step_kind = nil
@@ -60,6 +60,12 @@ module RubyLLM
60
60
  @closed = false
61
61
  end
62
62
 
63
+ # Internal resume bookkeeping: these calls were already reported by the
64
+ # interrupted run. Only their result and progress belong to this run.
65
+ def continue_tool_calls(ids) # :nodoc:
66
+ @continued_tool_call_ids = ids.dup.freeze
67
+ end
68
+
63
69
  # One streamed Chunk from Chat#complete.
64
70
  def chunk(chunk)
65
71
  return [] if @closed
@@ -176,6 +182,7 @@ module RubyLLM
176
182
  events.concat(close_step)
177
183
  @last_message_id = nil
178
184
  @text_emitted = false
185
+ end_stream_keys
179
186
  events
180
187
  end
181
188
 
@@ -185,6 +192,8 @@ module RubyLLM
185
192
  return [] if tool_calls.nil? || tool_calls.empty?
186
193
 
187
194
  tool_calls.flat_map do |key, tool_call|
195
+ next [] if @continued_tool_call_ids&.include?(tool_call.id)
196
+
188
197
  record = track(key, tool_call, streamed: streamed)
189
198
  open_tool_call(record) + argument_events(record, tool_call, streamed: streamed)
190
199
  end
@@ -192,21 +201,47 @@ module RubyLLM
192
201
 
193
202
  # The record for one tool call, created on first sight.
194
203
  #
195
- # A streamed delta is keyed by index and carries the id only on its first
196
- # fragment; the complete message is keyed by id. Both keys point at the
197
- # same record, so the call is opened once.
204
+ # A streamed delta is keyed by its provider stream index and carries the
205
+ # id only on its first fragment; the complete message is keyed by id. Both
206
+ # keys point at the same record, so the call is opened once. Providers
207
+ # restart stream indexes in every response, so an index names a call only
208
+ # until its completion ends. An unkeyed fragment continues the latest
209
+ # streamed call, and a fragment with a different id starts a new call
210
+ # under the same key, as RubyLLM's own accumulator does.
198
211
  def track(key, tool_call, streamed:)
199
- record = @calls[key] || find_call(tool_call.id) || new_call
212
+ record = lookup_call(key, tool_call, streamed: streamed)
200
213
  record[:id] ||= tool_call.id
201
214
  record[:name] ||= tool_call.name
202
- record[:streamed] ||= streamed
203
- @calls[key] = record
215
+ remember_stream_call(key, record) if streamed
204
216
  @calls[record[:id]] = record if record[:id]
205
217
  record
206
218
  end
207
219
 
220
+ def lookup_call(key, tool_call, streamed:)
221
+ record = stream_call(key) if streamed
222
+ return record if record && continues?(record, tool_call)
223
+
224
+ find_call(tool_call.id) || new_call
225
+ end
226
+
227
+ def continues?(record, tool_call)
228
+ tool_call.id.nil? || record[:id].nil? || record[:id] == tool_call.id
229
+ end
230
+
231
+ def stream_call(key) = key.nil? ? @latest_stream_call : @stream_calls[key]
232
+
233
+ def remember_stream_call(key, record)
234
+ @stream_calls[key] = record unless key.nil?
235
+ @latest_stream_call = record
236
+ end
237
+
238
+ def end_stream_keys
239
+ @stream_calls = {}
240
+ @latest_stream_call = nil
241
+ end
242
+
208
243
  def new_call
209
- { id: nil, name: nil, streamed: false, args_emitted: false, started: false, closed: false, buffered: nil }
244
+ { id: nil, name: nil, args_emitted: false, started: false, closed: false, buffered: nil }
210
245
  end
211
246
 
212
247
  def find_call(id)
@@ -252,19 +287,29 @@ module RubyLLM
252
287
  # A streamed chunk carries a fragment of the provider's own argument bytes
253
288
  # and goes out unchanged. A complete message carries the Hash RubyLLM
254
289
  # reconstructed from those bytes, so it is serialized once and only when
255
- # the stream did not already deliver the fragments.
290
+ # the stream delivered no argument bytes for the call.
256
291
  def argument_delta(record, tool_call, streamed:)
257
- return streamed_delta(tool_call) if streamed
258
- return nil if record[:streamed] || record[:args_emitted]
292
+ delta = streamed ? streamed_delta(tool_call) : complete_delta(record, tool_call)
293
+ record[:args_emitted] = true unless delta.nil? || delta.empty?
294
+ delta
295
+ end
259
296
 
260
- record[:args_emitted] = true
297
+ # Like RubyLLM's stream accumulator, an empty value carries no bytes:
298
+ # Anthropic and Bedrock open a call with an empty input object before
299
+ # streaming its partial JSON. Gemini streams a whole call as a parsed
300
+ # object, which is serialized as JSON.
301
+ def streamed_delta(tool_call)
261
302
  arguments = tool_call.arguments
303
+ return nil if arguments.nil? || (arguments.respond_to?(:empty?) && arguments.empty?)
304
+
262
305
  arguments.is_a?(String) ? arguments : Canonical.dump(arguments)
263
306
  end
264
307
 
265
- def streamed_delta(tool_call)
308
+ def complete_delta(record, tool_call)
309
+ return nil if record[:args_emitted]
310
+
266
311
  arguments = tool_call.arguments
267
- arguments.is_a?(String) ? arguments : arguments.to_s
312
+ arguments.is_a?(String) ? arguments : Canonical.dump(arguments)
268
313
  end
269
314
 
270
315
  def close_tool_calls
@@ -9,7 +9,7 @@ module RubyLLM
9
9
  # gem never trusts a client message as a system instruction or a tool
10
10
  # result. This parser only guarantees the shapes the pinned schema defines,
11
11
  # so a mismatch is a clear InputError instead of a downstream surprise.
12
- class MessageParser
12
+ class MessageParser # :nodoc:
13
13
  include Validation
14
14
 
15
15
  # The roles the pinned schema defines.
@@ -1,10 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'record'
4
-
5
3
  module RubyLLM
6
4
  module AgUI
7
5
  module Rails
6
+ # Defining the row class loads ActiveRecord::Base. Defer it to first use,
7
+ # so requiring the gem during a Rails boot does not load Active Record
8
+ # before the host's configuration.
9
+ autoload :Record, File.expand_path('record', __dir__)
10
+
8
11
  # The durable run store: run identity in a table instead of a process.
9
12
  #
10
13
  # The in-process store is correct for one process only. A host that runs
@@ -43,8 +46,7 @@ module RubyLLM
43
46
  end
44
47
 
45
48
  def latest_interrupted(scope:, thread_id:)
46
- interrupted = RunRecord::INTERRUPTED.to_s
47
- thread_rows(scope, thread_id).reverse.find { |row| row.status == interrupted }&.as_run_record
49
+ interrupted_rows(scope, thread_id).last&.as_run_record
48
50
  end
49
51
 
50
52
  def save(record)
@@ -116,12 +118,19 @@ module RubyLLM
116
118
  Record.where(scope: scope, thread_id: thread_id).order(:id).to_a
117
119
  end
118
120
 
119
- # An interrupt id is unique among a thread's runs, so the thread is
120
- # searched newest first and the first row that knows the id wins. A
121
- # thread holds few runs, and the alternative is a JSON query that would
122
- # bind this store to one database.
121
+ # Only an interrupted run carries interrupts, and it keeps that status
122
+ # after they are answered. The status index finds those rows without
123
+ # loading every run on the thread.
124
+ def interrupted_rows(scope, thread_id)
125
+ Record.where(scope: scope, thread_id: thread_id, status: RunRecord::INTERRUPTED.to_s).order(:id).to_a
126
+ end
127
+
128
+ # An interrupt id is unique among a thread's runs, so the interrupted
129
+ # runs are searched newest first and the first row that knows the id
130
+ # wins. The alternative is a JSON query that would bind this store to
131
+ # one database.
123
132
  def row_for_interrupt(scope, thread_id, interrupt_id)
124
- thread_rows(scope, thread_id).reverse.find { |row| row.as_run_record.interrupt(interrupt_id) }
133
+ interrupted_rows(scope, thread_id).reverse.find { |row| row.as_run_record.interrupt(interrupt_id) }
125
134
  end
126
135
  end
127
136
  end
@@ -66,10 +66,10 @@ module RubyLLM
66
66
  # identity claimed with different input or a resume that answers
67
67
  # nothing, 413 for a body past the configured bound.
68
68
  reject_ag_ui_request(e.http_status, e.message)
69
- rescue ::ActiveRecord::RecordNotFound
70
- reject_ag_ui_request(NOT_FOUND, 'There is no such thread.')
71
69
  rescue ::RubyLLM::AgUI::StreamError => e
72
70
  reject_ag_ui_request(CONFLICT, e.message)
71
+ rescue StandardError => e
72
+ reject_ag_ui_host_error(e)
73
73
  end
74
74
 
75
75
  # The tenant/actor key every reservation is bounded by.
@@ -89,6 +89,12 @@ module RubyLLM
89
89
 
90
90
  private
91
91
 
92
+ def reject_ag_ui_host_error(error)
93
+ raise error unless defined?(::ActiveRecord::RecordNotFound) && error.is_a?(::ActiveRecord::RecordNotFound)
94
+
95
+ reject_ag_ui_request(NOT_FOUND, 'There is no such thread.')
96
+ end
97
+
92
98
  # Whether this client can read an event stream. An AG-UI client asks for
93
99
  # `text/event-stream`; a request that asks for something else is told so
94
100
  # instead of being streamed at.
@@ -12,7 +12,7 @@ module RubyLLM
12
12
  # What cannot be served this way is a run that has not ended: attaching a
13
13
  # second stream to a live run needs a relay the gem does not own, and a
14
14
  # silent second stream would duplicate the work. Those are refused.
15
- class Reconnect
15
+ class Reconnect # :nodoc:
16
16
  # The failure reported for a run that failed before its sanitized error
17
17
  # was recorded — a record from an older release, or one whose write was
18
18
  # interrupted.
@@ -51,8 +51,10 @@ module RubyLLM
51
51
 
52
52
  # Calls +callable+ with the arguments its arity takes, so a host can write
53
53
  # either `->(kind, payload) { ... }` or the terse `->(payload) { ... }`.
54
+ # A policy object that only defines #call is read the same way.
54
55
  def apply(callable, kind, payload)
55
- callable.arity == 1 ? callable.call(payload) : callable.call(kind, payload)
56
+ arity = callable.respond_to?(:arity) ? callable.arity : callable.method(:call).arity
57
+ arity == 1 ? callable.call(payload) : callable.call(kind, payload)
56
58
  end
57
59
  end
58
60
  end
@@ -15,7 +15,7 @@ module RubyLLM
15
15
  #
16
16
  # An approval answers an interrupt. The host still re-authorizes the actor
17
17
  # before the tool executes.
18
- class Resume
18
+ class Resume # :nodoc:
19
19
  # One entry's effect: the interrupt it answers and the decision to record.
20
20
  Decision = Struct.new(:interrupt, :decision, keyword_init: true)
21
21
 
@@ -25,9 +25,9 @@ module RubyLLM
25
25
  attr_reader :payload
26
26
 
27
27
  def initialize(interrupt_id:, status:, payload: nil)
28
- @interrupt_id = interrupt_id
29
- @status = status
30
- @payload = payload
28
+ @interrupt_id = Immutable.copy(interrupt_id)
29
+ @status = Immutable.copy(status)
30
+ @payload = Immutable.copy(payload)
31
31
  freeze
32
32
  end
33
33
 
@@ -20,7 +20,8 @@ module RubyLLM
20
20
  # input, because a scope derived from anything the client sent would let one
21
21
  # client attach to another's run.
22
22
  class Run
23
- # The run's authoritative record, once it has been reserved.
23
+ # The reserved record or final outcome observed by this run, including a
24
+ # reconnect. Nil before the run obtains its reservation.
24
25
  attr_reader :record
25
26
 
26
27
  # rubocop:disable-next Metrics/ParameterLists -- one keyword per host-supplied or overridable value
@@ -52,16 +53,15 @@ module RubyLLM
52
53
  def stream(to:)
53
54
  resume.plan(now: @clock.call) if resume.needed?
54
55
  reservation = reserve
55
- return execute(reservation.record, to) if reservation.created?
56
+ @record = reservation.record
57
+ return execute(@record, to) if reservation.created?
56
58
 
57
- reconnect(reservation.record, to)
59
+ reconnect(@record, to)
58
60
  end
59
61
 
60
62
  private
61
63
 
62
- def resume
63
- @resume ||= Resume.new(input: @input, store: @store, scope: @scope, chat: @chat)
64
- end
64
+ def resume = @resume ||= Resume.new(input: @input, store: @store, scope: @scope, chat: @chat)
65
65
 
66
66
  def reserve
67
67
  Persistence.write do
@@ -97,11 +97,12 @@ module RubyLLM
97
97
  rescue StandardError => e
98
98
  @record = finalize { @emitter.fail(e) }
99
99
  ensure
100
- finalize { @mapper&.close }
100
+ finalize { release }
101
101
  end
102
102
 
103
103
  def start_execution(record, to)
104
104
  @mapper = build_mapper(record)
105
+ @mapper.continue_tool_calls(@chat.pending_approvals.map(&:id)) if resume.needed?
105
106
  @emitter = build_emitter(record, to, mapper: @mapper)
106
107
  @emitter.start(run_started_event(record), state: @state)
107
108
  end
@@ -131,8 +132,15 @@ module RubyLLM
131
132
 
132
133
  def finalize(&) = @mutex.synchronize(&)
133
134
 
134
- def project
135
- @mutex.synchronize { @emitter.write(yield) }
135
+ # After release, a stale callback maps and writes nothing.
136
+ def project = @mutex.synchronize { @emitter&.write(yield) }
137
+
138
+ # The callbacks this run registered stay on the chat after it ends, and
139
+ # RubyLLM cannot remove them. The closed mapper maps nothing further, and
140
+ # dropping the emitter lets the response stream it writes to be freed.
141
+ def release
142
+ @mapper&.close
143
+ @emitter = nil
136
144
  end
137
145
 
138
146
  # A run that stopped for a human is an interrupt, never a success. Any
@@ -177,10 +185,7 @@ module RubyLLM
177
185
  raise ConfigurationError, 'chat must be a RubyLLM::Chat or act like one (it does not respond to #complete)'
178
186
  end
179
187
  raise ConfigurationError, 'input must be a RubyLLM::AgUI::Input' unless @input.is_a?(Input)
180
-
181
- return if @scope.is_a?(String) && !@scope.empty?
182
-
183
- raise ConfigurationError, Store::Base::SCOPE_MESSAGE
188
+ raise ConfigurationError, Store::Base::SCOPE_MESSAGE unless @scope.is_a?(String) && !@scope.empty?
184
189
  end
185
190
  end
186
191
  end
@@ -72,18 +72,18 @@ module RubyLLM
72
72
  # rubocop:disable-next Metrics/ParameterLists -- one keyword per field the record owns
73
73
  def initialize(scope:, thread_id:, run_id:, digest:, created_at:, status: RESERVED, sequence: 0,
74
74
  snapshot: nil, interrupts: [], failure: nil, updated_at: nil, revision: 0)
75
- @scope = scope
76
- @thread_id = thread_id
77
- @run_id = run_id
78
- @digest = digest
75
+ @scope = Immutable.copy(scope)
76
+ @thread_id = Immutable.copy(thread_id)
77
+ @run_id = Immutable.copy(run_id)
78
+ @digest = Immutable.copy(digest)
79
79
  @status = assert_status!(status)
80
80
  @sequence = sequence
81
81
  @revision = revision
82
- @snapshot = snapshot
83
- @interrupts = interrupts.freeze
84
- @failure = failure
85
- @created_at = created_at
86
- @updated_at = updated_at || created_at
82
+ @snapshot = Immutable.copy(snapshot)
83
+ @interrupts = Immutable.copy(interrupts)
84
+ @failure = Immutable.copy(failure)
85
+ @created_at = Immutable.copy(created_at)
86
+ @updated_at = Immutable.copy(updated_at || created_at)
87
87
  freeze
88
88
  end
89
89
 
@@ -229,7 +229,7 @@ module RubyLLM
229
229
  end
230
230
 
231
231
  def iso8601(time)
232
- time&.utc&.iso8601
232
+ time&.getutc&.iso8601
233
233
  end
234
234
 
235
235
  def assert_status!(value)
@@ -48,14 +48,31 @@ module RubyLLM
48
48
  #
49
49
  # A host calls this before opening the stream. An explicit header that
50
50
  # cannot accept `text/event-stream` is a client that will not understand
51
- # the response, so the host should answer 406 instead of streaming.
51
+ # the response, so the host should answer 406 instead of streaming. A zero
52
+ # quality refuses that range; a more specific range overrides a wildcard.
52
53
  def self.accepts?(accept)
53
54
  return true if accept.nil? || accept.strip.empty?
54
55
 
55
- accept.split(',').any? do |range|
56
- %w[text/event-stream text/* */*].include?(range.split(';').first.to_s.strip.downcase)
56
+ matches = accept.split(',').filter_map do |range|
57
+ media_type, *parameters = range.downcase.split(';').map(&:strip)
58
+ specificity = %w[*/* text/* text/event-stream].index(media_type)
59
+ next if specificity.nil?
60
+
61
+ [specificity, accept_quality(parameters)]
57
62
  end
63
+ return false if matches.empty?
64
+
65
+ matches.max.last.positive?
66
+ end
67
+
68
+ def self.accept_quality(parameters)
69
+ quality = parameters.find { |parameter| parameter.split('=', 2).first.to_s.strip == 'q' }
70
+ return 1.0 unless quality
71
+
72
+ value = quality.split('=', 2).last.to_s.strip
73
+ value.match?(/\A(?:0(?:\.\d{0,3})?|1(?:\.0{0,3})?)\z/) ? value.to_f : 0.0
58
74
  end
75
+ private_class_method :accept_quality
59
76
 
60
77
  # The Content-Type for the response the host is about to open.
61
78
  def content_type
@@ -34,7 +34,7 @@ module RubyLLM
34
34
  )
35
35
  @records[key] = record
36
36
  thread = thread_key(scope, thread_id)
37
- @threads[thread] = (@threads[thread] || []) + [run_id]
37
+ @threads[thread] = (@threads[thread] || []) + [record.run_id]
38
38
  Reservation.new(record: record, status: Reservation::CREATED)
39
39
  end
40
40
  end
@@ -107,11 +107,11 @@ module RubyLLM
107
107
  end
108
108
 
109
109
  def key_for(scope, thread_id, run_id)
110
- [scope_key(scope), thread_id, run_id]
110
+ Immutable.copy([scope_key(scope), thread_id, run_id])
111
111
  end
112
112
 
113
113
  def thread_key(scope, thread_id)
114
- [scope_key(scope), thread_id]
114
+ Immutable.copy([scope_key(scope), thread_id])
115
115
  end
116
116
  end
117
117
  end
@@ -9,7 +9,7 @@ module RubyLLM
9
9
  # the client without echoing request bytes back.
10
10
  #
11
11
  # Included by RubyLLM::AgUI::Input::Parser and RubyLLM::AgUI::MessageParser.
12
- module Validation
12
+ module Validation # :nodoc:
13
13
  # The longest accepted id (threadId, runId, message id, interrupt id).
14
14
  MAX_ID_LENGTH = 255
15
15
 
@@ -3,6 +3,6 @@
3
3
  module RubyLLM
4
4
  module AgUI
5
5
  # The version of the ruby_llm-ag_ui gem, as a string.
6
- VERSION = '0.3.0'
6
+ VERSION = '1.0.0'
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_llm-ag_ui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Wang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-10-20 00:00:00.000000000 Z
11
+ date: 2026-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ag-ui-protocol
@@ -163,6 +163,7 @@ files:
163
163
  - README.md
164
164
  - SECURITY.md
165
165
  - docs/_advanced/client-example.md
166
+ - docs/_advanced/provider-verification.md
166
167
  - docs/_advanced/rails-integration.md
167
168
  - docs/_advanced/security-and-deployment.md
168
169
  - docs/_advanced/worker-recovery.md
@@ -175,16 +176,19 @@ files:
175
176
  - docs/_getting_started/configuration.md
176
177
  - docs/_getting_started/getting-started.md
177
178
  - docs/_getting_started/overview.md
178
- - docs/_getting_started/whats-new-in-0-3.md
179
+ - docs/_getting_started/whats-new-in-1-0.md
179
180
  - docs/_reference/api-reference.md
180
181
  - docs/_reference/compatibility.md
181
182
  - docs/_reference/protocol-coverage.md
183
+ - docs/_reference/public-api.md
182
184
  - docs/_reference/release-history.md
183
185
  - docs/_reference/releasing.md
184
186
  - docs/_reference/third-party-notices.md
185
187
  - docs/_reference/upgrading.md
186
188
  - docs/_reference/whats-new-in-0-1.md
187
189
  - docs/_reference/whats-new-in-0-2.md
190
+ - docs/_reference/whats-new-in-0-3.md
191
+ - docs/_reference/whats-new-in-0-4.md
188
192
  - docs/index.md
189
193
  - lib/generators/ruby_llm/ag_ui/install_generator.rb
190
194
  - lib/generators/ruby_llm/ag_ui/templates/create_ruby_llm_ag_ui_runs.rb.tt
@@ -196,6 +200,7 @@ files:
196
200
  - lib/ruby_llm/ag_ui/emitter.rb
197
201
  - lib/ruby_llm/ag_ui/error.rb
198
202
  - lib/ruby_llm/ag_ui/error_sanitizer.rb
203
+ - lib/ruby_llm/ag_ui/immutable.rb
199
204
  - lib/ruby_llm/ag_ui/input.rb
200
205
  - lib/ruby_llm/ag_ui/interrupt.rb
201
206
  - lib/ruby_llm/ag_ui/mapper.rb
@@ -236,7 +241,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
236
241
  requirements:
237
242
  - - ">="
238
243
  - !ruby/object:Gem::Version
239
- version: 3.2.0
244
+ version: 3.3.0
240
245
  required_rubygems_version: !ruby/object:Gem::Requirement
241
246
  requirements:
242
247
  - - ">="