Module: Legion::API::Helpers::Acp

Defined in:
lib/legion/api/acp.rb

Instance Method Summary collapse

Instance Method Details

#build_agent_cardObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/legion/api/acp.rb', line 7

def build_agent_card
  name = begin
    Legion::Settings[:client][:name]
  rescue StandardError => e
    Legion::Logging.debug "Acp#build_agent_card failed to read client name: #{e.message}" if defined?(Legion::Logging)
    'legion'
  end
  port = begin
    settings.port || 4567
  rescue StandardError => e
    Legion::Logging.debug "Acp#build_agent_card failed to read port: #{e.message}" if defined?(Legion::Logging)
    4567
  end
  {
    name:               name,
    description:        'LegionIO digital worker',
    url:                "http://#{request.host}:#{port}/api/acp",
    version:            '2.0',
    protocol:           'acp/1.0',
    capabilities:       discover_capabilities,
    authentication:     { schemes: ['bearer'] },
    defaultInputModes:  ['text/plain', 'application/json'],
    defaultOutputModes: ['text/plain', 'application/json']
  }
end

#discover_capabilitiesObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/legion/api/acp.rb', line 33

def discover_capabilities
  if defined?(Legion::Extensions::Mesh::Helpers::Registry)
    Legion::Extensions::Mesh::Helpers::Registry.new.capabilities.keys.map(&:to_s)
  else
    []
  end
rescue StandardError => e
  Legion::Logging.warn "Acp#discover_capabilities failed: #{e.message}" if defined?(Legion::Logging)
  []
end

#find_task(id) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/legion/api/acp.rb', line 44

def find_task(id)
  return nil unless defined?(Legion::Data)

  Legion::Data::Model::Task[id.to_i]&.values
rescue StandardError => e
  Legion::Logging.warn "Acp#find_task failed for id=#{id}: #{e.message}" if defined?(Legion::Logging)
  nil
end

#translate_status(status) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/legion/api/acp.rb', line 53

def translate_status(status)
  case status&.to_s
  when /completed/ then 'completed'
  when /exception|failed/ then 'failed'
  when /queued|scheduled/ then 'queued'
  else 'in_progress'
  end
end