Class: LanguageOperator::CLI::Commands::Tool::Base

Inherits:
BaseCommand
  • Object
show all
Includes:
Install, Search, Test, Helpers::ClusterValidator, LanguageOperator::Constants
Defined in:
lib/language_operator/cli/commands/tool/base.rb

Overview

Base tool command class

Constant Summary

Constants included from LanguageOperator::Constants

LanguageOperator::Constants::ALL_MODE_ALIASES, LanguageOperator::Constants::EXECUTION_MODES, LanguageOperator::Constants::PRIMARY_MODES, LanguageOperator::Constants::RESOURCE_AGENT, LanguageOperator::Constants::RESOURCE_AGENT_VERSION, LanguageOperator::Constants::RESOURCE_MODEL, LanguageOperator::Constants::RESOURCE_PERSONA, LanguageOperator::Constants::RESOURCE_TOOL

Instance Method Summary collapse

Methods included from Search

included

Methods included from Test

included

Methods included from Install

included

Methods included from Helpers::ClusterValidator

current_cluster, current_cluster_config, ensure_cluster_selected!, get_cluster, get_cluster_config, kubernetes_client, validate_cluster_exists!, validate_kubeconfig!

Methods included from LanguageOperator::Constants

normalize_mode, valid_mode?

Methods included from Helpers::UxHelper

#box, #highlight_ruby_code, #logo, #pastel, #prompt, #spinner, #table

Instance Method Details

#delete(name) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/language_operator/cli/commands/tool/base.rb', line 203

def delete(name)
  handle_command_error('delete tool') do
    get_resource_or_exit(RESOURCE_TOOL, name)

    # Check dependencies and get confirmation
    return unless check_dependencies_and_confirm('tool', name, force: options[:force])

    # Confirm deletion unless --force
    return unless confirm_deletion_with_force('tool', name, ctx.name, force: options[:force])

    # Delete tool
    Formatters::ProgressFormatter.with_spinner("Deleting tool '#{name}'") do
      ctx.client.delete_resource(RESOURCE_TOOL, name, ctx.namespace)
    end
  end
end

#inspect(name) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/language_operator/cli/commands/tool/base.rb', line 71

def inspect(name)
  handle_command_error('inspect tool') do
    tool = get_resource_or_exit(RESOURCE_TOOL, name)

    # Main tool information
    puts
    highlighted_box(
      title: RESOURCE_TOOL,
      rows: {
        'Name' => pastel.white.bold(name),
        'Namespace' => ctx.namespace,
        'Cluster' => ctx.name,
        'Status' => tool.dig('status', 'phase') || 'Unknown',
        'Type' => tool.dig('spec', 'type') || 'mcp',
        'Image' => tool.dig('spec', 'image'),
        'Deployment Mode' => tool.dig('spec', 'deploymentMode') || 'sidecar',
        'Port' => tool.dig('spec', 'port') || 8080,
        'Replicas' => tool.dig('spec', 'replicas') || 1
      }
    )
    puts

    # Resources
    resources = tool.dig('spec', 'resources')
    if resources
      resource_rows = {}
      requests = resources['requests'] || {}
      limits = resources['limits'] || {}

      # CPU
      cpu_request = requests['cpu']
      cpu_limit = limits['cpu']
      resource_rows['CPU'] = [cpu_request, cpu_limit].compact.join(' / ') if cpu_request || cpu_limit

      # Memory
      memory_request = requests['memory']
      memory_limit = limits['memory']
      resource_rows['Memory'] = [memory_request, memory_limit].compact.join(' / ') if memory_request || memory_limit

      highlighted_box(title: 'Resources (Request/Limit)', rows: resource_rows, color: :cyan) unless resource_rows.empty?
      puts
    end

    # RBAC
    rbac = tool.dig('spec', 'rbac')
    if rbac && rbac['clusterRole']
      rules = rbac.dig('clusterRole', 'rules') || []
      puts "RBAC Permissions (#{rules.length} rules):"
      rules.each_with_index do |rule, idx|
        puts "  Rule #{idx + 1}:"
        puts "    API Groups: #{rule['apiGroups'].join(', ')}"
        puts "    Resources:  #{rule['resources'].join(', ')}"
        puts "    Verbs:      #{rule['verbs'].join(', ')}"
      end
      puts
    end

    # Egress rules
    egress = tool.dig('spec', 'egress') || []
    if egress.any?
      puts "Network Egress (#{egress.length} rules):"
      egress.each_with_index do |rule, idx|
        puts "  Rule #{idx + 1}: #{rule['description']}"
        puts "    DNS:   #{rule['dns'].join(', ')}" if rule['dns']
        puts "    CIDR:  #{rule['cidr']}" if rule['cidr']
        if rule['ports']
          ports_str = rule['ports'].map { |p| "#{p['port']}/#{p['protocol']}" }.join(', ')
          puts "    Ports: #{ports_str}"
        end
      end
      puts
    end

    # Try to fetch MCP capabilities
    capabilities = fetch_mcp_capabilities(name, tool, ctx.namespace)
    if capabilities && capabilities['tools'] && capabilities['tools'].any?
      puts "MCP Tools (#{capabilities['tools'].length}):"
      capabilities['tools'].each_with_index do |mcp_tool, idx|
        tool_name = mcp_tool['name']

        # Generate a meaningful name if empty
        if tool_name.nil? || tool_name.empty?
          # Try to derive from description (first few words)
          if mcp_tool['description']
            # Take first 3-4 words and convert to snake_case
            words = mcp_tool['description'].split(/\s+/).first(4)
            derived_name = words.join('_').downcase.gsub(/[^a-z0-9_]/, '')
            tool_name = "#{name}_#{derived_name}".gsub(/__+/, '_').sub(/_$/, '')
          else
            tool_name = "#{name}_tool_#{idx + 1}"
          end
        end

        puts "  #{tool_name}"
        puts "    Description: #{mcp_tool['description']}" if mcp_tool['description']
        next unless mcp_tool['inputSchema'] && mcp_tool['inputSchema']['properties']

        params = mcp_tool['inputSchema']['properties'].keys
        required = mcp_tool['inputSchema']['required'] || []
        param_list = params.map { |p| required.include?(p) ? "#{p}*" : p }
        puts "    Parameters:  #{param_list.join(', ')}"
      end
      puts '    (* = required)'
      puts
    end

    # Get agents using this tool
    agents = ctx.client.list_resources(RESOURCE_AGENT, namespace: ctx.namespace)
    agents_using = agents.select do |agent|
      tools = agent.dig('spec', 'tools') || []
      tools.include?(name)
    end
    agent_names = agents_using.map { |agent| agent.dig('metadata', 'name') }

    list_box(
      title: 'Agents using this tool',
      items: agent_names
    )

    puts
    labels = tool.dig('metadata', 'labels') || {}
    list_box(
      title: 'Labels',
      items: labels,
      style: :key_value
    )
  end
end

#listObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/language_operator/cli/commands/tool/base.rb', line 29

def list
  handle_command_error('list tools') do
    tools = list_resources_or_empty(RESOURCE_TOOL, resource_name: 'tools') do
      puts
      puts 'Install a tool with:'
      puts '  langop tool install <name>'
    end

    return if tools.empty?

    # Get agents to count usage
    agents = ctx.client.list_resources(RESOURCE_AGENT, namespace: ctx.namespace)

    table_data = tools.map do |tool|
      name = tool.dig('metadata', 'name')
      tool.dig('spec', 'type') || 'unknown'
      status = tool.dig('status', 'phase') || 'Unknown'

      # Count agents using this tool
      Helpers::ResourceDependencyChecker.tool_usage_count(agents, name)

      # Get health status
      health = tool.dig('status', 'health') || 'unknown'
      case health.downcase
      when 'healthy' then ''
      when 'unhealthy' then ''
      else '?'
      end

      {
        name: name,
        namespace: tool.dig('metadata', 'namespace') || ctx.namespace,
        status: status
      }
    end

    Formatters::TableFormatter.tools(table_data)
  end
end