Class: LanguageOperator::CLI::Commands::Persona

Inherits:
BaseCommand
  • Object
show all
Includes:
Helpers::ClusterValidator, Helpers::UxHelper, LanguageOperator::Constants
Defined in:
lib/language_operator/cli/commands/persona.rb

Overview

Persona management commands

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 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(name) ⇒ Object



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
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
# File 'lib/language_operator/cli/commands/persona.rb', line 113

def create(name)
  handle_command_error('create persona') do
    # Check if persona already exists
    begin
      ctx.client.get_resource(RESOURCE_PERSONA, name, ctx.namespace)
      Formatters::ProgressFormatter.error("Persona '#{name}' already exists in cluster '#{ctx.name}'")
      puts
      puts 'Use a different name or delete the existing persona first:'
      puts "  langop persona delete #{name}"
      exit 1
    rescue K8s::Error::NotFound
      # Good - persona doesn't exist yet
    end

    # If --from flag provided, copy from existing persona
    base_persona = nil
    if options[:from]
      base_persona = get_resource_or_exit(RESOURCE_PERSONA, options[:from],
                                          error_message: "Source persona '#{options[:from]}' not found")
      Formatters::ProgressFormatter.info("Copying from persona '#{options[:from]}'")
      puts
    end

    # Interactive prompts
    require 'tty-prompt'
    # prompt available via UxHelper

    puts
    puts '=' * 80
    puts '  Create New Persona'
    puts '=' * 80
    puts

    # Get display name
    default_display_name = base_persona&.dig('spec', 'displayName') || name.split('-').map(&:capitalize).join(' ')
    display_name = prompt.ask('Display Name:', default: default_display_name)

    # Get description
    default_description = base_persona&.dig('spec', 'description') || ''
    description = prompt.ask('Description:', default: default_description) do |q|
      q.required true
    end

    # Get tone
    default_tone = base_persona&.dig('spec', 'tone') || 'neutral'
    tone = prompt.select('Tone:', %w[neutral friendly professional technical creative], default: default_tone)

    # Get system prompt
    puts
    puts 'System Prompt (press Enter to open editor):'
    prompt.keypress('Press any key to continue...')

    default_system_prompt = base_persona&.dig('spec', 'systemPrompt') || ''
    system_prompt = Helpers::EditorHelper.edit_content(default_system_prompt, 'persona-system-prompt', '.txt')

    if system_prompt.strip.empty?
      Formatters::ProgressFormatter.error('System prompt cannot be empty')
      exit 1
    end

    # Get capabilities
    puts
    puts 'Capabilities (optional, press Enter to open editor, or leave empty to skip):'
    puts 'Describe what this persona can do, one per line.'
    prompt.keypress('Press any key to continue...')

    default_capabilities = base_persona&.dig('spec', 'capabilities')&.join("\n") || ''
    capabilities_text = Helpers::EditorHelper.edit_content(default_capabilities, 'persona-capabilities', '.txt')
    capabilities = capabilities_text.strip.empty? ? [] : capabilities_text.strip.split("\n").map(&:strip).reject(&:empty?)

    # Build persona resource
    persona_spec = {
      'displayName' => display_name,
      'description' => description,
      'tone' => tone,
      'systemPrompt' => system_prompt.strip
    }
    persona_spec['capabilities'] = capabilities unless capabilities.empty?

    persona_resource = Kubernetes::ResourceBuilder.build_persona(
      name: name,
      spec: persona_spec,
      namespace: ctx.namespace,
      cluster_ref: ctx.name,
      k8s_client: ctx.client
    )

    # Show preview
    puts
    puts '=' * 80
    puts 'Preview:'
    puts '=' * 80
    puts YAML.dump(persona_resource)
    puts '=' * 80
    puts

    # Confirm creation
    unless prompt.yes?('Create this persona?')
      puts 'Creation cancelled'
      return
    end

    # Create persona
    Formatters::ProgressFormatter.with_spinner("Creating persona '#{name}'") do
      ctx.client.create_resource(persona_resource)
    end

    Formatters::ProgressFormatter.success("Persona '#{name}' created successfully")
    puts
    puts 'Use this persona when creating agents:'
    puts "  langop agent create \"description\" --persona #{name}"
  end
end

#delete(name) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/language_operator/cli/commands/persona.rb', line 291

def delete(name)
  handle_command_error('delete persona') do
    # Get persona
    get_resource_or_exit(RESOURCE_PERSONA, name)

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

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

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

#edit(name) ⇒ Object



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
# File 'lib/language_operator/cli/commands/persona.rb', line 238

def edit(name)
  handle_command_error('edit persona') do
    # Get current persona
    persona = get_resource_or_exit(RESOURCE_PERSONA, name)

    # Open in editor
    original_yaml = YAML.dump(persona)
    edited_yaml = Helpers::EditorHelper.edit_content(original_yaml, "persona-#{name}", '.txt')

    # Check if changed
    if edited_yaml.strip == original_yaml.strip
      Formatters::ProgressFormatter.info('No changes made')
      return
    end

    # Parse edited YAML
    begin
      edited_persona = YAML.safe_load(edited_yaml)
    rescue Psych::SyntaxError => e
      Formatters::ProgressFormatter.error("Invalid YAML: #{e.message}")
      exit 1
    end

    # Validate structure
    unless edited_persona.is_a?(Hash) && edited_persona['spec']
      Formatters::ProgressFormatter.error('Invalid persona structure: missing spec')
      exit 1
    end

    # Update persona
    Formatters::ProgressFormatter.with_spinner("Updating persona '#{name}'") do
      ctx.client.update_resource(edited_persona)
    end

    Formatters::ProgressFormatter.success("Persona '#{name}' updated successfully")

    # Check for agents using this persona
    agents = ctx.client.list_resources(RESOURCE_AGENT, namespace: ctx.namespace)
    agents_using = Helpers::ResourceDependencyChecker.agents_using_persona(agents, name)

    if agents_using.any?
      puts
      Formatters::ProgressFormatter.info("#{agents_using.count} agent(s) will be re-synthesized automatically:")
      agents_using.each do |agent|
        puts "  - #{agent.dig('metadata', 'name')}"
      end
    end
  end
end

#listObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/language_operator/cli/commands/persona.rb', line 17

def list
  handle_command_error('list personas') do
    personas = list_resources_or_empty(RESOURCE_PERSONA) do
      puts
      puts 'Personas define the personality and capabilities of agents.'
      puts
      puts 'Create a persona with:'
      puts '  langop persona create <name>'
    end

    return if personas.empty?

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

    table_data = personas.map do |persona|
      name = persona.dig('metadata', 'name')
      used_by = agents.count { |a| a.dig('spec', 'persona') == name }

      {
        name: name,
        tone: persona.dig('spec', 'tone') || 'neutral',
        used_by: used_by,
        description: persona.dig('spec', 'description') || ''
      }
    end

    Formatters::TableFormatter.personas(table_data)
  end
end

#show(name) ⇒ Object



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
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
# File 'lib/language_operator/cli/commands/persona.rb', line 50

def show(name)
  handle_command_error('show persona') do
    persona = get_resource_or_exit(RESOURCE_PERSONA, name)

    puts
    puts "Persona: #{pastel.cyan.bold(name)}"
    puts '' * 80
    puts

    # Format and display key persona details
    spec = persona['spec'] || {}
    puts "#{pastel.bold('Display Name:')} #{spec['displayName']}"
    puts "#{pastel.bold('Tone:')} #{pastel.yellow(spec['tone'])}" if spec['tone']
    puts
    puts pastel.bold('Description:')
    puts "  #{spec['description']}"
    puts

    if spec['systemPrompt']
      puts pastel.bold('System Prompt:')
      puts "  #{spec['systemPrompt']}"
      puts
    end

    if spec['capabilities']&.any?
      puts pastel.bold('Capabilities:')
      spec['capabilities'].each do |cap|
        puts "  #{pastel.green('')} #{cap}"
      end
      puts
    end

    if spec['toolPreferences']&.any?
      puts pastel.bold('Tool Preferences:')
      spec['toolPreferences'].each do |pref|
        puts "  #{pastel.green('')} #{pref}"
      end
      puts
    end

    if spec['responseFormat']
      puts "#{pastel.bold('Response Format:')} #{spec['responseFormat']}"
      puts
    end

    puts '' * 80
    puts
    Formatters::ProgressFormatter.info('Use this persona when creating agents:')
    puts "  langop agent create \"description\" --persona #{name}"
    puts
  end
end