Class: Legion::Extensions::Llm::Canonical::ToolCall

Inherits:
Data
  • Object
show all
Defined in:
lib/legion/extensions/llm/canonical/tool_call.rb

Overview

rubocop:disable Lint/ConstantDefinitionInBlock – required for Data.define block scope Canonical tool call with source enum and compliance fields. Ports field vocabulary from Legion::LLM::Types::ToolCall. Source enum per R7: :client | :registry | :special | :extension | :mcp Compliance fields per R8: data_handling_classification, policy_decision

Constant Summary collapse

SOURCE_VALUES =
%i[client registry special extension mcp].freeze
STATUS_VALUES =
%i[pending running success error].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments

Returns:

  • (Object)

    the current value of arguments



15
16
17
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 15

def arguments
  @arguments
end

#categoryObject (readonly)

Returns the value of attribute category

Returns:

  • (Object)

    the current value of category



15
16
17
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 15

def category
  @category
end

#data_handling_classificationObject (readonly)

Returns the value of attribute data_handling_classification

Returns:

  • (Object)

    the current value of data_handling_classification



15
16
17
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 15

def data_handling_classification
  @data_handling_classification
end

#duration_msObject (readonly)

Returns the value of attribute duration_ms

Returns:

  • (Object)

    the current value of duration_ms



15
16
17
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 15

def duration_ms
  @duration_ms
end

#errorObject (readonly)

Returns the value of attribute error

Returns:

  • (Object)

    the current value of error



15
16
17
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 15

def error
  @error
end

#exchange_idObject (readonly)

Returns the value of attribute exchange_id

Returns:

  • (Object)

    the current value of exchange_id



15
16
17
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 15

def exchange_id
  @exchange_id
end

#finished_atObject (readonly)

Returns the value of attribute finished_at

Returns:

  • (Object)

    the current value of finished_at



15
16
17
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 15

def finished_at
  @finished_at
end

#idObject (readonly)

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



15
16
17
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 15

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



15
16
17
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 15

def name
  @name
end

#policy_decisionObject (readonly)

Returns the value of attribute policy_decision

Returns:

  • (Object)

    the current value of policy_decision



15
16
17
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 15

def policy_decision
  @policy_decision
end

#resultObject (readonly)

Returns the value of attribute result

Returns:

  • (Object)

    the current value of result



15
16
17
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 15

def result
  @result
end

#sourceObject (readonly)

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



15
16
17
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 15

def source
  @source
end

#started_atObject (readonly)

Returns the value of attribute started_at

Returns:

  • (Object)

    the current value of started_at



15
16
17
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 15

def started_at
  @started_at
end

#statusObject (readonly)

Returns the value of attribute status

Returns:

  • (Object)

    the current value of status



15
16
17
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 15

def status
  @status
end

Class Method Details

.build(name:, id: nil, exchange_id: nil, arguments: nil, source: nil, status: nil, duration_ms: nil, result: nil, error: nil, started_at: nil, finished_at: nil, category: nil, data_handling_classification: nil, policy_decision: nil) ⇒ Object

Build from keyword args (primary constructor).



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 25

def self.build(
  name:, id: nil, exchange_id: nil, arguments: nil, source: nil,
  status: nil, duration_ms: nil, result: nil, error: nil,
  started_at: nil, finished_at: nil, category: nil,
  data_handling_classification: nil, policy_decision: nil
)
  new(
    id: id || "call_#{SecureRandom.hex(12)}",
    exchange_id: exchange_id,
    name: name,
    arguments: arguments || {},
    source: source,
    status: status,
    duration_ms: duration_ms,
    result: result,
    error: error,
    started_at: started_at,
    finished_at: finished_at,
    category: category,
    data_handling_classification: data_handling_classification,
    policy_decision: policy_decision
  )
end

.from_hash(hash) ⇒ Object

Build from a Hash (raw provider response or deserialized wire payload).



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 50

def self.from_hash(hash)
  return nil if hash.nil?

  h = hash.transform_keys(&:to_sym)

  # Normalize source to symbol
  source_raw = h[:source]
  h[:source] = source_raw&.to_sym if source_raw.is_a?(String)

  # Normalize status to symbol
  status_raw = h[:status]
  h[:status] = status_raw&.to_sym if status_raw.is_a?(String)

  # Parse arguments if they're a JSON string
  args = h[:arguments]
  if args.is_a?(String) && !args.empty?
    begin
      h[:arguments] = Legion::JSON.load(args)
    rescue Legion::JSON::ParseError => e
      Legion::Logging.debug("[lex-llm][canonical][tool_call] arguments not parseable as JSON, leaving as string: #{e.message}")
    end
  end

  build(**h)
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 100

def error?
  status == :error
end

#success?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 96

def success?
  status == :success
end

#to_audit_hashObject

Subset for audit/ledger emission.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 110

def to_audit_hash
  {
    id: id,
    name: name,
    arguments: arguments,
    status: status,
    duration_ms: duration_ms,
    error: error,
    exchange_id: exchange_id,
    source: source,
    category: category,
    data_handling_classification: data_handling_classification,
    policy_decision: policy_decision
  }.compact
end

#to_hObject

Serialize to a Hash for AMQP/fleet/wire transport.



105
106
107
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 105

def to_h
  super.compact
end

#with_result(result:, status:, duration_ms: nil, finished_at: nil) ⇒ Object

Return a new ToolCall with execution result attached.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/legion/extensions/llm/canonical/tool_call.rb', line 77

def with_result(result:, status:, duration_ms: nil, finished_at: nil)
  self.class.new(
    id: id,
    exchange_id: exchange_id,
    name: name,
    arguments: arguments,
    source: source,
    status: status,
    duration_ms: duration_ms,
    result: result,
    error: status == :error ? result : error,
    started_at: started_at,
    finished_at: finished_at || ::Time.now,
    category: category,
    data_handling_classification: data_handling_classification,
    policy_decision: policy_decision
  )
end