Class: Ask::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/ask/tools/result.rb

Overview

Standardized return value for tool execution.

Every tool’s #execute method should return an Ask::Result. Use the factory methods .ok and .error for common cases.

Ask::Result.ok(data: "hello world")
Ask::Result.error(message: "something went wrong")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ok:, output: nil, error: nil, metadata: {}) ⇒ Result

Returns a new instance of Result.



27
28
29
30
31
32
# File 'lib/ask/tools/result.rb', line 27

def initialize(ok:, output: nil, error: nil, metadata: {})
  @ok = ok
  @output = output
  @error = error
  @metadata = 
end

Instance Attribute Details

#errorString? (readonly)

Returns the error message when the tool failed.

Returns:

  • (String, nil)

    the error message when the tool failed



20
21
22
# File 'lib/ask/tools/result.rb', line 20

def error
  @error
end

#metadataHash (readonly)

Returns arbitrary metadata attached to the result.

Returns:

  • (Hash)

    arbitrary metadata attached to the result



23
24
25
# File 'lib/ask/tools/result.rb', line 23

def 
  @metadata
end

#okBoolean (readonly)

Returns whether the tool completed successfully.

Returns:

  • (Boolean)

    whether the tool completed successfully



14
15
16
# File 'lib/ask/tools/result.rb', line 14

def ok
  @ok
end

#outputObject? (readonly)

Returns the output data when the tool succeeded.

Returns:

  • (Object, nil)

    the output data when the tool succeeded



17
18
19
# File 'lib/ask/tools/result.rb', line 17

def output
  @output
end

Class Method Details

.error(message:, metadata: {}) ⇒ Ask::Result

Create a failed result.

Parameters:

  • message (String)

    description of the failure

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

    optional metadata

Returns:



48
49
50
# File 'lib/ask/tools/result.rb', line 48

def self.error(message:, metadata: {})
  new(ok: false, output: nil, error: message, metadata: )
end

.ok(data:, metadata: {}) ⇒ Ask::Result

Create a successful result.

Parameters:

  • data (Object)

    the tool’s output

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

    optional metadata

Returns:



39
40
41
# File 'lib/ask/tools/result.rb', line 39

def self.ok(data:, metadata: {})
  new(ok: true, output: data, error: nil, metadata: )
end

Instance Method Details

#inspectString

Returns inspect string.

Returns:

  • (String)

    inspect string



73
74
75
76
77
78
79
# File 'lib/ask/tools/result.rb', line 73

def inspect
  if ok?
    "#<Ask::Result ok=true output=#{output.inspect}>"
  else
    "#<Ask::Result ok=false error=#{error.inspect}>"
  end
end

#ok?Boolean

Returns whether the tool completed successfully.

Returns:

  • (Boolean)

    whether the tool completed successfully



25
26
27
# File 'lib/ask/tools/result.rb', line 25

def ok
  @ok
end

#to_hHash

Hash representation suitable for serialization.

Returns:

  • (Hash)


63
64
65
66
67
68
69
70
# File 'lib/ask/tools/result.rb', line 63

def to_h
  {
    ok: ok,
    output: output,
    error: error,
    metadata: 
  }
end

#to_sString

Human-readable representation. Returns the output for success or the error message for failure.

Returns:

  • (String)


56
57
58
# File 'lib/ask/tools/result.rb', line 56

def to_s
  ok? ? output.to_s : error.to_s
end