Class: LanguageOperator::CLI::Commands::Agent::Base

Inherits:
BaseCommand
  • Object
show all
Includes:
CodeOperations, Helpers::CodeParser, Helpers::SynthesisWatcher, Lifecycle, Logs, Workspace, Helpers::ClusterValidator, Helpers::UxHelper, LanguageOperator::Constants
Defined in:
lib/language_operator/cli/commands/agent/base.rb

Overview

Base agent 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 Lifecycle

included

Methods included from Logs

included

Methods included from CodeOperations

included

Methods included from Workspace

included

Methods included from Helpers::CodeParser

#extract_hash_value, #extract_string_value, #load_agent_definition, #parse_agent_code

Methods included from Helpers::UxHelper

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

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?

Instance Method Details

#create(description = nil) ⇒ Object



64
65
66
67
68
69
70
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
# File 'lib/language_operator/cli/commands/agent/base.rb', line 64

def create(description = nil)
  handle_command_error('create agent') do
    # Read from stdin if available and no description provided
    description = $stdin.read.strip if description.nil? && !$stdin.tty?

    # Activate wizard mode if --wizard flag or no description provided
    if options[:wizard] || description.nil? || description.empty?
      wizard = Wizards::AgentWizard.new
      description = wizard.run

      # User cancelled wizard
      unless description
        Formatters::ProgressFormatter.info('Agent creation cancelled')
        return
      end
    end

    # Handle --create-cluster flag
    if options[:create_cluster]
      cluster_name = options[:create_cluster]
      unless Config::ClusterConfig.cluster_exists?(cluster_name)
        Formatters::ProgressFormatter.info("Creating cluster '#{cluster_name}'...")
        # Delegate to cluster create command
        require_relative '../cluster'
        Cluster.new.invoke(:create, [cluster_name], switch: true)
      end
      cluster = cluster_name
    else
      # Validate cluster selection (this will exit if none selected)
      cluster = CLI::Helpers::ClusterValidator.get_cluster(options[:cluster])
    end

    ctx = CLI::Helpers::ClusterContext.from_options(options.merge(cluster: cluster))

    # Generate agent name from description if not provided
    agent_name = options[:name] || generate_agent_name(description)

    # Get models: use specified models, or default to all available models in cluster
    models = options[:models]
    if models.nil? || models.empty?
      available_models = ctx.client.list_resources(RESOURCE_MODEL, namespace: ctx.namespace)
      models = available_models.map { |m| m.dig('metadata', 'name') }

      Errors::Handler.handle_no_models_available(cluster: ctx.name) if models.empty?
    end

    # Build LanguageAgent resource
    agent_resource = Kubernetes::ResourceBuilder.language_agent(
      agent_name,
      instructions: description,
      cluster: ctx.namespace,
      cluster_ref: ctx.name,
      persona: options[:persona],
      tools: options[:tools] || [],
      models: models,
      workspace: options[:workspace],
      k8s_client: ctx.client
    )

    # Dry-run mode: preview without applying
    if options[:dry_run]
      display_dry_run_preview(agent_resource, ctx.name, description)
      return
    end

    # Apply resource to cluster
    Formatters::ProgressFormatter.with_spinner("Creating agent '#{agent_name}'") do
      ctx.client.apply_resource(agent_resource)
    end

    # Watch synthesis status
    synthesis_result = watch_synthesis_status(ctx.client, agent_name, ctx.namespace)

    # Exit if synthesis failed
    exit 1 unless synthesis_result[:success]

    # Fetch the updated agent to get complete details
    agent = ctx.client.get_resource(RESOURCE_AGENT, agent_name, ctx.namespace)

    # Display enhanced success output
    display_agent_created(agent, ctx, description, synthesis_result)
  end
end

#delete(name) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/language_operator/cli/commands/agent/base.rb', line 296

def delete(name)
  handle_command_error('delete agent') do
    ctx = CLI::Helpers::ClusterContext.from_options(options)

    # Get agent to verify it exists
    get_resource_or_exit(RESOURCE_AGENT, name)

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

    # Delete the agent
    puts
    Formatters::ProgressFormatter.with_spinner("Deleting agent '#{name}'") do
      ctx.client.delete_resource(RESOURCE_AGENT, name, ctx.namespace)
    end

    # Verify deletion completed
    verify_agent_deletion(ctx, name)
  end
end

#inspect(name) ⇒ Object



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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/language_operator/cli/commands/agent/base.rb', line 162

def inspect(name)
  handle_command_error('inspect agent') do
    ctx = CLI::Helpers::ClusterContext.from_options(options)

    begin
      agent = ctx.client.get_resource(RESOURCE_AGENT, name, ctx.namespace)
    rescue K8s::Error::NotFound
      handle_agent_not_found(name, ctx)
      return
    end

    # Main agent information
    puts
    status = agent.dig('status', 'phase') || 'Unknown'
    creation_timestamp = agent.dig('metadata', 'creationTimestamp')
    formatted_created = creation_timestamp ? Formatters::ValueFormatter.time_ago(Time.parse(creation_timestamp)) : nil

    format_agent_details(
      name: name,
      namespace: ctx.namespace,
      cluster: ctx.name,
      status: format_status(status),
      mode: agent.dig('spec', 'executionMode') || 'autonomous',
      schedule: agent.dig('spec', 'schedule'),
      persona: agent.dig('spec', 'persona') || 'None',
      created: formatted_created
    )
    puts

    # Execution stats (only for scheduled agents)
    mode = agent.dig('spec', 'executionMode') || 'autonomous'
    if mode == 'scheduled'
      exec_data = get_execution_data(name, ctx)

      exec_rows = {
        'Last Run' => exec_data[:last_run] || 'Never'
      }
      exec_rows['Next Run'] = exec_data[:next_run] || 'N/A' if agent.dig('spec', 'schedule')

      highlighted_box(title: 'Executions', rows: exec_rows, color: :blue)
      puts
    end

    # Learning status
    display_learning_section(agent, name, ctx)
    puts

    # Resources
    resources = agent.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

    # Instructions
    instructions = agent.dig('spec', 'instructions')
    if instructions
      puts pastel.white.bold('Instructions')
      puts instructions
      puts
    end

    # Tools
    tools = agent.dig('spec', 'tools') || []
    unless tools.empty?
      list_box(title: 'Tools', items: tools)
      puts
    end

    # Models
    model_refs = agent.dig('spec', 'modelRefs') || []
    unless model_refs.empty?
      model_names = model_refs.map { |ref| ref['name'] }
      list_box(title: 'Models', items: model_names, bullet: '')
      puts
    end

    # Synthesis info
    synthesis = agent.dig('status', 'synthesis')
    if synthesis
      highlighted_box(
        title: 'Synthesis',
        rows: {
          'Status' => synthesis['status'],
          'Model' => synthesis['model'],
          'Completed' => synthesis['completedAt'],
          'Duration' => synthesis['duration'],
          'Token Count' => synthesis['tokenCount']
        }
      )
      puts
    end

    # Conditions
    conditions = agent.dig('status', 'conditions') || []
    unless conditions.empty?
      list_box(
        title: 'Conditions',
        items: conditions,
        style: :conditions
      )
      puts
    end

    # Labels
    labels = agent.dig('metadata', 'labels') || {}
    list_box(
      title: 'Labels',
      items: labels,
      style: :key_value
    )

    # Recent events (if available)
    # This would require querying events, which we can add later
  end
end

#listObject



151
152
153
154
155
156
157
158
# File 'lib/language_operator/cli/commands/agent/base.rb', line 151

def list
  if options[:all_clusters]
    list_all_clusters
  else
    cluster = options[:cluster]
    list_cluster_agents(cluster)
  end
end