Class: Legion::CLI::Notebook

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/legion/cli/notebook_command.rb', line 12

def self.exit_on_failure?
  true
end

Instance Method Details

#cells(path) ⇒ Object



47
48
49
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
# File 'lib/legion/cli/notebook_command.rb', line 47

def cells(path)
  out = formatter
  load_notebook(path, out)

  require 'legion/notebook/parser'

  parsed = Legion::Notebook::Parser.parse(path)
  color  = !options[:no_color]

  if options[:json]
    cell_list = parsed[:cells].each_with_index.map do |cell, i|
      { index: i + 1, type: cell[:type], lines: cell[:source].lines.count }
    end
    out.json(cells: cell_list, total: parsed[:cells].length)
  else
    parsed[:cells].each_with_index do |cell, i|
      lines  = cell[:source].lines.count
      plural = lines == 1 ? '' : 's'
      label  = "  [#{(i + 1).to_s.rjust(2)}] #{cell[:type].to_s.ljust(8)}  #{lines} line#{plural}"
      if color
        type_color = cell[:type] == 'code' ? "\e[36m" : "\e[33m"
        puts "#{type_color}#{label}\e[0m"
      else
        puts label
      end
    end
    out.spacer
    puts "Total: #{parsed[:cells].length} cell#{'s' unless parsed[:cells].length == 1}"
  end
rescue CLI::Error => e
  formatter.error(e.message)
  raise SystemExit, 1
end

#create(path) ⇒ Object



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

def create(path)
  out = formatter
  setup_llm_connection(out)

  require 'legion/notebook/generator'

  description = options[:description]
  if description.nil? || description.strip.empty?
    out.error('--description is required for notebook creation')
    raise SystemExit, 1
  end

  out.success("Generating notebook: #{description}") unless options[:json]

  notebook_data = Legion::Notebook::Generator.generate(
    description: description,
    kernel:      options[:kernel],
    model:       options[:model],
    provider:    options[:provider]
  )

  Legion::Notebook::Generator.write(path, notebook_data)
  cell_count = Array(notebook_data['cells']).length

  if options[:json]
    out.json(path: path, cells: cell_count, kernel: options[:kernel])
  else
    out.success("Created #{path} (#{cell_count} cells)")
  end
rescue ArgumentError, CLI::Error => e
  formatter.error(e.message)
  raise SystemExit, 1
ensure
  Connection.shutdown
end

#export(path) ⇒ Object



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

def export(path)
  out = formatter
  load_notebook(path, out)

  require 'legion/notebook/parser'

  parsed = Legion::Notebook::Parser.parse(path)
  lang   = parsed[:language]

  content = case options[:format]
            when 'script'
              export_as_script(parsed[:cells], lang)
            else
              export_as_markdown(parsed[:cells], lang)
            end

  if options[:output]
    File.write(options[:output], content)
    out.success("Exported to #{options[:output]}")
  elsif options[:json]
    out.json(content: content, format: options[:format], path: path)
  else
    puts content
  end
rescue CLI::Error => e
  formatter.error(e.message)
  raise SystemExit, 1
end

#read(path) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/legion/cli/notebook_command.rb', line 22

def read(path)
  out = formatter
  load_notebook(path, out)
  color = !options[:no_color]

  require 'legion/notebook/parser'
  require 'legion/notebook/renderer'

  parsed   = Legion::Notebook::Parser.parse(path)
  rendered = Legion::Notebook::Renderer.render_notebook(parsed, color: color)

  if options[:json]
    out.json(cells: parsed[:cells].length, kernel: parsed[:kernel], path: path)
  else
    puts rendered
    out.spacer
    count = parsed[:cells].length
    puts "#{count} cell#{'s' unless count == 1} total"
  end
rescue CLI::Error => e
  formatter.error(e.message)
  raise SystemExit, 1
end