Class: Vangrail::Call

Inherits:
Object
  • Object
show all
Defined in:
lib/vangrail/reference_monitor.rb

Overview

Immutable attempt to exercise one tool grant.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tool:, request:, arguments:, conversation_id:, sink: nil, confirmation: nil, transaction: false, idempotency_key: nil, id: nil) ⇒ Call

Returns a new instance of Call.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vangrail/reference_monitor.rb', line 17

def initialize(tool:, request:, arguments:, conversation_id:, sink: nil,
               confirmation: nil, transaction: false, idempotency_key: nil,
               id: nil)
  raise ArgumentError, 'request must be a Cell' unless request.is_a?(Cell)

  @id = (id || SecureRandom.uuid).to_s.freeze
  @tool = tool.to_sym
  @request = request
  payload = arguments.nil? ? {} : arguments
  @arguments = payload.is_a?(Cell) ? payload : Cell.data(payload)
  @conversation_id = conversation_id.to_s.freeze
  @sink = sink&.to_sym
  @confirmation = confirmation
  @transaction = transaction.equal?(true)
  @idempotency_key = idempotency_key&.to_s&.freeze
  freeze
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



14
15
16
# File 'lib/vangrail/reference_monitor.rb', line 14

def arguments
  @arguments
end

#confirmationObject (readonly)

Returns the value of attribute confirmation.



14
15
16
# File 'lib/vangrail/reference_monitor.rb', line 14

def confirmation
  @confirmation
end

#conversation_idObject (readonly)

Returns the value of attribute conversation_id.



14
15
16
# File 'lib/vangrail/reference_monitor.rb', line 14

def conversation_id
  @conversation_id
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/vangrail/reference_monitor.rb', line 14

def id
  @id
end

#idempotency_keyObject (readonly)

Returns the value of attribute idempotency_key.



14
15
16
# File 'lib/vangrail/reference_monitor.rb', line 14

def idempotency_key
  @idempotency_key
end

#requestObject (readonly)

Returns the value of attribute request.



14
15
16
# File 'lib/vangrail/reference_monitor.rb', line 14

def request
  @request
end

#sinkObject (readonly)

Returns the value of attribute sink.



14
15
16
# File 'lib/vangrail/reference_monitor.rb', line 14

def sink
  @sink
end

#toolObject (readonly)

Returns the value of attribute tool.



14
15
16
# File 'lib/vangrail/reference_monitor.rb', line 14

def tool
  @tool
end

Instance Method Details

#confirmed?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/vangrail/reference_monitor.rb', line 35

def confirmed?
  !confirmation.nil?
end

#fieldsObject



43
44
45
46
47
# File 'lib/vangrail/reference_monitor.rb', line 43

def fields
  return { value: arguments }.freeze unless arguments.value.is_a?(Hash)

  arguments.value.transform_keys(&:to_sym).freeze
end

#fingerprintObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vangrail/reference_monitor.rb', line 63

def fingerprint
  material = [
    tool,
    request.raw,
    request.label.to_h,
    fields.sort_by { |name, _| name.to_s }.map { |name, cell| [name, cell.raw, cell.label.to_h] },
    conversation_id,
    sink,
    transaction?,
    idempotency_key,
  ]
  Digest::SHA256.hexdigest(Marshal.dump(material))
end

#to_hObject



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vangrail/reference_monitor.rb', line 77

def to_h
  {
    'id' => id,
    'tool' => tool.to_s,
    'conversation_id' => conversation_id,
    'arguments' => arguments.to_h,
    'sink' => sink&.to_s,
    'confirmed' => confirmed?,
    'transaction' => transaction?,
    'idempotency_key' => idempotency_key,
  }.compact
end

#transaction?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/vangrail/reference_monitor.rb', line 39

def transaction?
  @transaction
end

#with_confirmation(token) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vangrail/reference_monitor.rb', line 49

def with_confirmation(token)
  self.class.new(
    id: id,
    tool: tool,
    request: request,
    arguments: arguments,
    conversation_id: conversation_id,
    sink: sink,
    confirmation: token,
    transaction: transaction?,
    idempotency_key: idempotency_key,
  )
end