Class: Kreator::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/kreator/cli.rb

Constant Summary collapse

DEFAULT_PROVIDER =
"openai"
DEFAULT_MODEL =
"gpt-4o-mini"
SESSION_COMMANDS =
{
  list_sessions: :list_sessions_payload,
  search_sessions: :search_sessions_payload,
  label_session: :label_session_payload,
  export_session: :export_session_payload,
  cleanup_empty_sessions: :cleanup_empty_sessions_payload
}.freeze
PLUGIN_COMMANDS =
{
  "list" => :plugin_list_payload,
  "validate" => :plugin_validate_payload,
  "install" => :plugin_install_payload,
  "update" => :plugin_update_payload,
  "remove" => :plugin_remove_payload
}.freeze
PLUGIN_RENDERERS =
{
  "plugins" => :render_plugin_list,
  "validation" => :render_plugin_validation,
  "plugin" => :render_single_plugin,
  "removed" => :render_removed_plugin
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdout:, stderr:, stdin: $stdin, provider_builder: Providers.method(:build)) ⇒ CLI

Returns a new instance of CLI.



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
# File 'lib/kreator/cli.rb', line 31

def initialize(argv, stdout:, stderr:, stdin: $stdin, provider_builder: Providers.method(:build))
  @argv = argv.dup
  @stdin = stdin
  @stdout = stdout
  @stderr = stderr
  @provider_builder = provider_builder
  @options = {
    provider: ENV.fetch("KREATOR_PROVIDER", DEFAULT_PROVIDER),
    model: ENV.fetch("KREATOR_MODEL", DEFAULT_MODEL),
    tools: nil,
    no_tools: false,
    timeout: ToolContext::DEFAULT_BASH_TIMEOUT,
    session: nil,
    continue: false,
    no_session: false,
    session_dir: SessionManager::DEFAULT_SESSION_DIR,
    resource_home: ENV.fetch("KREATOR_HOME", Resources::DEFAULT_HOME),
    plugins_enabled: true,
    plugins: nil,
    prompt_template: nil,
    compact_threshold: ENV["KREATOR_COMPACT_THRESHOLD"]&.to_i,
    approval_policy: ENV.fetch("KREATOR_APPROVAL_POLICY", "auto"),
    plugin_approval_policy: ENV.fetch("KREATOR_PLUGIN_TOOL_POLICY", "prompt"),
    allow_paths: nil,
    bash_deny_patterns: [],
    plugin_install_name: nil,
    list_sessions: false,
    search_sessions: nil,
    label_session: nil,
    export_session: nil,
    cleanup_empty_sessions: false,
    json: false,
    rpc: false
  }
end

Instance Method Details

#runObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/kreator/cli.rb', line 67

def run
  parser.parse!(@argv)
  return run_rpc if @options.fetch(:rpc)

  command_status = run_option_command
  return command_status if command_status

  prompt = @argv.join(" ").strip
  return handle_missing_prompt if prompt.empty?

  result = run_prompt(prompt)
  emit_prompt_result(result)
  0
rescue OptionParser::ParseError, ArgumentError, Providers::Error, ToolRegistry::Error, AgentLoop::Error => e
  if @options[:json]
    @stdout.puts JSON.generate("ok" => false, "error" => error_hash(e))
  else
    @stderr.puts "kreator: #{e.message}"
  end
  1
end