Class: Kward::Workers::Worker
- Inherits:
-
Object
- Object
- Kward::Workers::Worker
- Defined in:
- lib/kward/workers/worker.rb
Overview
Runtime record for one independent unit of agent work.
Constant Summary collapse
- STATUSES =
%w[idle queued running ready failed cancelled archived].freeze
Instance Attribute Summary collapse
-
#cancellation ⇒ Object
readonly
Returns the value of attribute cancellation.
-
#conversation ⇒ Object
Returns the value of attribute conversation.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#event_history ⇒ Object
readonly
Returns the value of attribute event_history.
-
#event_queue ⇒ Object
readonly
Returns the value of attribute event_queue.
-
#finished_at ⇒ Object
readonly
Returns the value of attribute finished_at.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#prompt ⇒ Object
readonly
Returns the value of attribute prompt.
-
#report ⇒ Object
readonly
Returns the value of attribute report.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#session ⇒ Object
Returns the value of attribute session.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#thread ⇒ Object
Returns the value of attribute thread.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
-
#workspace_root ⇒ Object
readonly
Returns the value of attribute workspace_root.
Instance Method Summary collapse
-
#initialize(id: SecureRandom.hex(4), title:, role:, workspace_root: Dir.pwd, status: "idle", prompt: nil, conversation: nil, session: nil, cancellation: Cancellation.new, created_at: Time.now.utc) ⇒ Worker
constructor
A new instance of Worker.
- #record_event(event) ⇒ Object
- #status ⇒ Object
- #to_h ⇒ Object
- #update_status(status, error: nil, report: nil) ⇒ Object
Constructor Details
#initialize(id: SecureRandom.hex(4), title:, role:, workspace_root: Dir.pwd, status: "idle", prompt: nil, conversation: nil, session: nil, cancellation: Cancellation.new, created_at: Time.now.utc) ⇒ Worker
Returns a new instance of Worker.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/kward/workers/worker.rb', line 12 def initialize(id: SecureRandom.hex(4), title:, role:, workspace_root: Dir.pwd, status: "idle", prompt: nil, conversation: nil, session: nil, cancellation: Cancellation.new, created_at: Time.now.utc) @id = id @title = title.to_s @role = role.to_s @workspace_root = ConfigFiles.canonical_workspace_root(workspace_root) @status = status.to_s @prompt = prompt.to_s @conversation = conversation @session = session @cancellation = cancellation @created_at = created_at @updated_at = created_at @started_at = nil @finished_at = nil @report = nil @error = nil @thread = nil @event_history = [] @event_queue = Queue.new end |
Instance Attribute Details
#cancellation ⇒ Object (readonly)
Returns the value of attribute cancellation.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def cancellation @cancellation end |
#conversation ⇒ Object
Returns the value of attribute conversation.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def conversation @conversation end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def created_at @created_at end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def error @error end |
#event_history ⇒ Object (readonly)
Returns the value of attribute event_history.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def event_history @event_history end |
#event_queue ⇒ Object (readonly)
Returns the value of attribute event_queue.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def event_queue @event_queue end |
#finished_at ⇒ Object (readonly)
Returns the value of attribute finished_at.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def finished_at @finished_at end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def id @id end |
#prompt ⇒ Object (readonly)
Returns the value of attribute prompt.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def prompt @prompt end |
#report ⇒ Object (readonly)
Returns the value of attribute report.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def report @report end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def role @role end |
#session ⇒ Object
Returns the value of attribute session.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def session @session end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def started_at @started_at end |
#thread ⇒ Object
Returns the value of attribute thread.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def thread @thread end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def title @title end |
#updated_at ⇒ Object (readonly)
Returns the value of attribute updated_at.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def updated_at @updated_at end |
#workspace_root ⇒ Object (readonly)
Returns the value of attribute workspace_root.
33 34 35 |
# File 'lib/kward/workers/worker.rb', line 33 def workspace_root @workspace_root end |
Instance Method Details
#record_event(event) ⇒ Object
51 52 53 54 |
# File 'lib/kward/workers/worker.rb', line 51 def record_event(event) @event_history << event @event_queue << event end |
#status ⇒ Object
36 37 38 |
# File 'lib/kward/workers/worker.rb', line 36 def status @status end |
#to_h ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/kward/workers/worker.rb', line 56 def to_h { "id" => id, "title" => title, "role" => role, "status" => status, "prompt" => prompt, "workspace_root" => workspace_root, "session_id" => session&.id, "session_path" => session&.path, "created_at" => (created_at), "updated_at" => (updated_at), "started_at" => (started_at), "finished_at" => (finished_at), "report" => report, "error" => error } end |
#update_status(status, error: nil, report: nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/kward/workers/worker.rb', line 40 def update_status(status, error: nil, report: nil) @status = status.to_s @error = error unless error.nil? @report = report unless report.nil? now = Time.now.utc @updated_at = now @started_at ||= now if @status == "running" @finished_at = now if %w[ready failed cancelled archived].include?(@status) self end |