Class: Agentic::TaskFailure

Inherits:
Object
  • Object
show all
Defined in:
lib/agentic/task_failure.rb

Overview

Represents a failure that occurred during task execution

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message:, type:, context: {}) ⇒ TaskFailure

Initializes a new task failure

Parameters:

  • message (String)

    The failure message

  • type (String)

    The type of failure

  • context (Hash) (defaults to: {})

    Additional context about the failure



17
18
19
20
21
22
# File 'lib/agentic/task_failure.rb', line 17

def initialize(message:, type:, context: {})
  @message = message
  @type = type
  @timestamp = Time.now
  @context = context
end

Instance Attribute Details

#contextHash (readonly)

Additional context about the failure

Returns:

  • (Hash)

    the current value of context



9
10
11
# File 'lib/agentic/task_failure.rb', line 9

def context
  @context
end

#messageString (readonly)

The failure message

Returns:

  • (String)

    the current value of message



9
10
11
# File 'lib/agentic/task_failure.rb', line 9

def message
  @message
end

#timestampTime (readonly)

When the failure occurred

Returns:

  • (Time)

    the current value of timestamp



9
10
11
# File 'lib/agentic/task_failure.rb', line 9

def timestamp
  @timestamp
end

#typeString (readonly)

The type of failure

Returns:

  • (String)

    the current value of type



9
10
11
# File 'lib/agentic/task_failure.rb', line 9

def type
  @type
end

Class Method Details

.from_exception(exception, context = {}) ⇒ TaskFailure

Creates a task failure from an exception

Parameters:

  • exception (Exception)

    The exception

  • context (Hash) (defaults to: {})

    Additional context about the failure

Returns:



39
40
41
42
43
44
45
46
47
# File 'lib/agentic/task_failure.rb', line 39

def self.from_exception(exception, context = {})
  new(
    message: exception.message,
    type: exception.class.name,
    context: context.merge(
      backtrace: exception.backtrace&.first(10)
    )
  )
end

.from_hash(hash) ⇒ TaskFailure

Creates a task failure from a hash

Parameters:

  • hash (Hash)

    The hash representation of a task failure

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/agentic/task_failure.rb', line 52

def self.from_hash(hash)
  # Handle the case where hash is not actually a hash
  return new(message: "Unknown error", type: "UnknownError") unless hash.is_a?(Hash)

  # Convert string keys to symbols if necessary
  hash = hash.transform_keys(&:to_sym) if hash.keys.first.is_a?(String)

  new(
    message: hash[:message] || "Unknown error",
    type: hash[:type] || "UnknownError",
    context: hash[:context] || {}
  )
end

Instance Method Details

#to_hHash

Returns a serializable representation of the failure

Returns:

  • (Hash)

    The failure as a hash



26
27
28
29
30
31
32
33
# File 'lib/agentic/task_failure.rb', line 26

def to_h
  {
    message: @message,
    type: @type,
    timestamp: @timestamp.iso8601,
    context: @context
  }
end