Class: A2A::Task

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, status:, **kwargs) ⇒ Task

Returns a new instance of Task.

Raises:

  • (TypeError)


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

#artifactsObject (readonly)

Returns the value of attribute artifacts.



8
9
10
# File 'lib/a2a/task.rb', line 8

def artifacts
  @artifacts
end

#context_idObject (readonly)

Returns the value of attribute context_id.



8
9
10
# File 'lib/a2a/task.rb', line 8

def context_id
  @context_id
end

#historyObject (readonly)

Returns the value of attribute history.



8
9
10
# File 'lib/a2a/task.rb', line 8

def history
  @history
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/a2a/task.rb', line 8

def id
  @id
end

#metadataObject (readonly)

Returns the value of attribute metadata.



8
9
10
# File 'lib/a2a/task.rb', line 8

def 
  @metadata
end

#statusObject (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(it) },
    history: Array(hash["history"]).map { Message.from_h(it) },
    metadata: hash["metadata"]
  )
end

Instance Method Details

#terminal?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/a2a/task.rb', line 32

def terminal?
  status.terminal?
end

#to_hObject



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.

Raises:

  • (ArgumentError)


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: message, timestamp: timestamp)
  Task.new(
    id: id,
    context_id: context_id,
    status: new_status,
    artifacts: artifacts,
    history: history,
    metadata: 
  )
end