class RailsAiAdmin::Assistant::Coordinator::DraftStream
Saves the reply text as it streams, so pages can show it before the message is complete. Only the lease holder writes; the persisted message replaces the draft when the step ends.
Public Class Methods
Source
# File lib/rails_ai_admin/assistant/coordinator.rb, line 212 def initialize(coordinator, claim) @coordinator, @claim = coordinator, claim @text = +"" @saved_at = nil end
Public Instance Methods
Source
# File lib/rails_ai_admin/assistant/coordinator.rb, line 218 def <<(piece) return self unless piece.is_a?(String) && !piece.empty? && @text.length < DRAFT_LIMIT @text << piece now = Process.clock_gettime(Process::CLOCK_MONOTONIC) return self if @saved_at && now - @saved_at < DRAFT_INTERVAL @saved_at = now @coordinator.save_draft(@claim, @text.first(DRAFT_LIMIT)) self end
Source
# File lib/rails_ai_admin/assistant/coordinator.rb, line 228 def clear = @coordinator.save_draft(@claim, nil) end
Source
# File lib/rails_ai_admin/assistant/coordinator.rb, line 238 def renew!(claim) updated = Turn.where(id: claim.turn_id, status: "running", lease_owner: claim.owner, generation: claim.generation) .where("lease_expires_at > ?", Time.current).update_all(lease_expires_at: Time.current + LEASE, updated_at: Time.current) raise Fenced unless updated == 1 end
Extends a live lease or stops the worker. Called before each provider step and each tool call, so a fenced worker cannot start either.
Source
# File lib/rails_ai_admin/assistant/coordinator.rb, line 231 def save_draft(claim, text) Turn.where(id: claim.turn_id, status: "running", lease_owner: claim.owner, generation: claim.generation) .update_all(draft: text, updated_at: Time.current) end