Class: A2A::Task
- Inherits:
-
Object
- Object
- A2A::Task
- Defined in:
- lib/a2a/task.rb,
lib/a2a/task/state.rb,
lib/a2a/task/status.rb
Defined Under Namespace
Modules: State Classes: Status
Instance Attribute Summary collapse
-
#artifacts ⇒ Object
readonly
Returns the value of attribute artifacts.
-
#context_id ⇒ Object
readonly
Returns the value of attribute context_id.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(id:, status:, **kwargs) ⇒ Task
constructor
A new instance of Task.
- #terminal? ⇒ Boolean
- #to_h ⇒ Object
-
#transition_to(state, message: nil, timestamp: nil) ⇒ Object
Returns a new Task with the status transitioned to ‘state`.
Constructor Details
#initialize(id:, status:, **kwargs) ⇒ Task
Returns a new instance of Task.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/a2a/task.rb', line 10 def initialize(id:, status:, **kwargs) raise TypeError, "status must be a Task::Status" unless status.is_a?(Task::Status) @id = id @status = status @context_id = kwargs[:context_id] @artifacts = kwargs[:artifacts] @history = kwargs[:history] @metadata = kwargs[:metadata] end |
Instance Attribute Details
#artifacts ⇒ Object (readonly)
Returns the value of attribute artifacts.
8 9 10 |
# File 'lib/a2a/task.rb', line 8 def artifacts @artifacts end |
#context_id ⇒ Object (readonly)
Returns the value of attribute context_id.
8 9 10 |
# File 'lib/a2a/task.rb', line 8 def context_id @context_id end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
8 9 10 |
# File 'lib/a2a/task.rb', line 8 def history @history end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/a2a/task.rb', line 8 def id @id end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
8 9 10 |
# File 'lib/a2a/task.rb', line 8 def @metadata end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
8 9 10 |
# File 'lib/a2a/task.rb', line 8 def status @status end |
Class Method Details
.from_h(hash) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/a2a/task.rb', line 21 def self.from_h(hash) new( id: hash.fetch("id"), context_id: hash["contextId"], status: Task::Status.from_h(hash.fetch("status")), artifacts: Array(hash["artifacts"]).map { Artifact.from_h(_1) }, history: Array(hash["history"]).map { Message.from_h(_1) }, metadata: hash["metadata"] ) end |
Instance Method Details
#terminal? ⇒ Boolean
32 33 34 |
# File 'lib/a2a/task.rb', line 32 def terminal? status.terminal? end |
#to_h ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/a2a/task.rb', line 55 def to_h { "id" => id, "contextId" => context_id, "status" => status.to_h, "artifacts" => artifacts&.map(&:to_h), "history" => history&.map(&:to_h), "metadata" => }.compact end |
#transition_to(state, message: nil, timestamp: nil) ⇒ Object
Returns a new Task with the status transitioned to ‘state`. Optionally attaches a status message and ISO-8601 timestamp. Raises ArgumentError for unknown states; raises TaskNotCancelableError when trying to transition away from a terminal state.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/a2a/task.rb', line 40 def transition_to(state, message: nil, timestamp: nil) raise ArgumentError, "unknown state: #{state.inspect}" unless Task::State.valid?(state) raise TaskNotCancelableError, "task #{id} is already in terminal state #{status.state}" if terminal? new_status = Task::Status.new(state: state, message: , timestamp: ) Task.new( id: id, context_id: context_id, status: new_status, artifacts: artifacts, history: history, metadata: ) end |