Skip to content
Kward Search API index

Class: Kward::Workers::Worker

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#cancellationObject (readonly)

Returns the value of attribute cancellation.



33
34
35
# File 'lib/kward/workers/worker.rb', line 33

def cancellation
  @cancellation
end

#conversationObject

Returns the value of attribute conversation.



33
34
35
# File 'lib/kward/workers/worker.rb', line 33

def conversation
  @conversation
end

#created_atObject (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

#errorObject (readonly)

Returns the value of attribute error.



33
34
35
# File 'lib/kward/workers/worker.rb', line 33

def error
  @error
end

#event_historyObject (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_queueObject (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_atObject (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

#idObject (readonly)

Returns the value of attribute id.



33
34
35
# File 'lib/kward/workers/worker.rb', line 33

def id
  @id
end

#promptObject (readonly)

Returns the value of attribute prompt.



33
34
35
# File 'lib/kward/workers/worker.rb', line 33

def prompt
  @prompt
end

#reportObject (readonly)

Returns the value of attribute report.



33
34
35
# File 'lib/kward/workers/worker.rb', line 33

def report
  @report
end

#roleObject (readonly)

Returns the value of attribute role.



33
34
35
# File 'lib/kward/workers/worker.rb', line 33

def role
  @role
end

#sessionObject

Returns the value of attribute session.



33
34
35
# File 'lib/kward/workers/worker.rb', line 33

def session
  @session
end

#started_atObject (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

#threadObject

Returns the value of attribute thread.



33
34
35
# File 'lib/kward/workers/worker.rb', line 33

def thread
  @thread
end

#titleObject (readonly)

Returns the value of attribute title.



33
34
35
# File 'lib/kward/workers/worker.rb', line 33

def title
  @title
end

#updated_atObject (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_rootObject (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

#statusObject



36
37
38
# File 'lib/kward/workers/worker.rb', line 36

def status
  @status
end

#to_hObject



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" => timestamp(created_at),
    "updated_at" => timestamp(updated_at),
    "started_at" => timestamp(started_at),
    "finished_at" => timestamp(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