Class: A2A::Types::Task

Inherits:
BaseModel show all
Defined in:
lib/a2a/types/task.rb

Overview

Represents a task in the A2A protocol

A task represents a unit of work that can be executed by an agent. It includes status information, artifacts, message history, and metadata.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

#==, #camelize, from_h, from_json, #hash, #to_h, #to_json, underscore, #valid?, #validate_array_type, #validate_inclusion, #validate_required, #validate_type

Constructor Details

#initialize(id:, context_id:, status:, kind: KIND_TASK, artifacts: nil, history: nil, metadata: nil) ⇒ Task

Initialize a new task

Parameters:

  • Unique task identifier

  • Context identifier for grouping related tasks

  • Current task status

  • (defaults to: KIND_TASK)

    Task kind (always "task")

  • (defaults to: nil)

    Task artifacts

  • (defaults to: nil)

    Message history

  • (defaults to: nil)

    Additional metadata



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/a2a/types/task.rb', line 24

def initialize(id:, context_id:, status:, kind: KIND_TASK, artifacts: nil, history: nil, metadata: nil)
  @id = id
  @context_id = context_id
  @kind = kind
  @status = status.is_a?(TaskStatus) ? status : TaskStatus.from_h(status)
  @artifacts = artifacts&.map { |a| a.is_a?(Artifact) ? a : Artifact.from_h(a) }
  @history = history&.map { |m| m.is_a?(Message) ? m : Message.from_h(m) }
   = 

  validate!
end

Instance Attribute Details

#artifactsObject (readonly)

Returns the value of attribute artifacts.



12
13
14
# File 'lib/a2a/types/task.rb', line 12

def artifacts
  @artifacts
end

#context_idObject (readonly)

Returns the value of attribute context_id.



12
13
14
# File 'lib/a2a/types/task.rb', line 12

def context_id
  @context_id
end

#historyObject (readonly)

Returns the value of attribute history.



12
13
14
# File 'lib/a2a/types/task.rb', line 12

def history
  @history
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/a2a/types/task.rb', line 12

def id
  @id
end

#kindObject (readonly)

Returns the value of attribute kind.



12
13
14
# File 'lib/a2a/types/task.rb', line 12

def kind
  @kind
end

#metadataObject (readonly)

Returns the value of attribute metadata.



12
13
14
# File 'lib/a2a/types/task.rb', line 12

def 
  
end

#statusObject (readonly)

Returns the value of attribute status.



12
13
14
# File 'lib/a2a/types/task.rb', line 12

def status
  @status
end

Instance Method Details

#add_artifact(artifact) ⇒ Object

Add an artifact to the task

Parameters:

  • The artifact to add



40
41
42
43
# File 'lib/a2a/types/task.rb', line 40

def add_artifact(artifact)
  @artifacts ||= []
  @artifacts << artifact
end

#add_message(message) ⇒ Object

Add a message to the history

Parameters:

  • The message to add



49
50
51
52
# File 'lib/a2a/types/task.rb', line 49

def add_message(message)
  @history ||= []
  @history << message
end

#cancelable?Boolean

Check if the task can be canceled

Returns:

  • True if the task can be canceled



74
75
76
# File 'lib/a2a/types/task.rb', line 74

def cancelable?
  %w[submitted working input-required].include?(@status.state)
end

#terminal?Boolean

Check if the task is in a terminal state

Returns:

  • True if the task is completed, canceled, failed, or rejected



66
67
68
# File 'lib/a2a/types/task.rb', line 66

def terminal?
  %w[completed canceled failed rejected].include?(@status.state)
end

#update_status(new_status) ⇒ Object

Update the task status

Parameters:

  • The new status



58
59
60
# File 'lib/a2a/types/task.rb', line 58

def update_status(new_status)
  @status = new_status.is_a?(TaskStatus) ? new_status : TaskStatus.from_h(new_status)
end

#validate!Object (private)



80
81
82
83
84
85
86
# File 'lib/a2a/types/task.rb', line 80

def validate!
  validate_required(:id, :context_id, :status, :kind)
  validate_inclusion(:kind, [KIND_TASK])
  validate_type(:status, TaskStatus)
  validate_array_type(:artifacts, Artifact) if @artifacts
  validate_array_type(:history, Message) if @history
end