Class: Legion::CLI::Prompt

Inherits:
Thor
  • Object
show all
Defined in:
lib/legion/cli/prompt_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/legion/cli/prompt_command.rb', line 8

def self.exit_on_failure?
  true
end

Instance Method Details

#create(name) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/legion/cli/prompt_command.rb', line 74

def create(name)
  out = formatter
  with_prompt_client do |client|
    params = parse_model_params(options[:model_params], out)
    return if params.nil?

    result = client.create_prompt(
      name:         name,
      template:     options[:template],
      description:  options[:description],
      model_params: params
    )
    if options[:json]
      out.json(result)
    else
      out.success("Created prompt '#{result[:name]}' (version #{result[:version]})")
    end
  end
end

#diff(name, ver1, ver2) ⇒ Object



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
# File 'lib/legion/cli/prompt_command.rb', line 116

def diff(name, ver1, ver2)
  out = formatter
  with_prompt_client do |client|
    r1 = client.get_prompt(name: name, version: ver1.to_i)
    r2 = client.get_prompt(name: name, version: ver2.to_i)

    if r1[:error]
      out.error("Version #{ver1}: #{r1[:error]}")
      raise SystemExit, 1
    end
    if r2[:error]
      out.error("Version #{ver2}: #{r2[:error]}")
      raise SystemExit, 1
    end

    if options[:json]
      out.json({ name: name, v1: ver1.to_i, v2: ver2.to_i,
                 template_v1: r1[:template], template_v2: r2[:template] })
    else
      require 'diff/lcs' if defined?(Diff::LCS)
      puts "--- v#{ver1}"
      puts "+++ v#{ver2}"
      puts diff_lines(r1[:template].to_s, r2[:template].to_s)
    end
  end
end

#listObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/legion/cli/prompt_command.rb', line 18

def list
  out = formatter
  with_prompt_client do |client|
    prompts = client.list_prompts
    if options[:json]
      out.json(prompts)
    elsif prompts.empty?
      out.warn('No prompts found')
    else
      rows = prompts.map do |p|
        [p[:name].to_s, (p[:description] || '').to_s,
         (p[:latest_version] || '-').to_s, (p[:updated_at] || '-').to_s]
      end
      out.table(%w[name description version updated_at], rows)
    end
  end
end

#play(name) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/legion/cli/prompt_command.rb', line 149

def play(name)
  out = formatter
  with_prompt_client do |client|
    unless defined?(Legion::LLM) && Legion::LLM.started?
      out.error('legion-llm is not available. Install legion-llm and configure a provider.')
      raise SystemExit, 1
    end

    vars = parse_variables(options[:variables], out)
    return if vars.nil?

    llm_kwargs = {}
    llm_kwargs[:model]    = options[:model]    if options[:model]
    llm_kwargs[:provider] = options[:provider] if options[:provider]

    base_ctx = { name: name, vars: vars, llm_kwargs: llm_kwargs, client: client, out: out }
    if options[:compare]
      run_compare(base_ctx.merge(ver_a: options[:version], ver_b: options[:compare]))
    else
      run_single(base_ctx.merge(version: options[:version]))
    end
  end
end

#show(name) ⇒ Object



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
68
# File 'lib/legion/cli/prompt_command.rb', line 40

def show(name)
  out = formatter
  with_prompt_client do |client|
    kwargs = { name: name }
    kwargs[:version] = options[:version] if options[:version]
    kwargs[:tag]     = options[:tag]     if options[:tag]
    result = client.get_prompt(**kwargs)
    if result[:error]
      out.error("Prompt '#{name}': #{result[:error]}")
      raise SystemExit, 1
    end

    if options[:json]
      out.json(result)
    else
      out.header("Prompt: #{result[:name]}")
      out.spacer
      out.detail({ version: result[:version], content_hash: result[:content_hash],
                   created_at: result[:created_at] })
      unless result[:model_params].nil? || result[:model_params].empty?
        out.spacer
        out.header('Model Params')
        out.detail(result[:model_params])
      end
      out.spacer
      puts result[:template]
    end
  end
end

#tag(name, tag_name) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/legion/cli/prompt_command.rb', line 96

def tag(name, tag_name)
  out = formatter
  with_prompt_client do |client|
    kwargs = { name: name, tag: tag_name }
    kwargs[:version] = options[:version] if options[:version]
    result = client.tag_prompt(**kwargs)
    if result[:error]
      out.error("Prompt '#{name}': #{result[:error]}")
      raise SystemExit, 1
    end

    if options[:json]
      out.json(result)
    else
      out.success("Tagged '#{result[:name]}' v#{result[:version]} as '#{result[:tag]}'")
    end
  end
end