Class: EasyAI::Command::GPT

Inherits:
EasyAI::Command show all
Defined in:
lib/easyai/command/gpt.rb

Constant Summary

Constants inherited from EasyAI::Command

DEFAULT_OPTIONS, DEFAULT_ROOT_OPTIONS

Instance Attribute Summary

Attributes inherited from EasyAI::Command

#args_help_flag

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EasyAI::Command

run

Constructor Details

#initialize(argv) ⇒ GPT

Returns a new instance of GPT.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/easyai/command/gpt.rb', line 46

def initialize(argv)
  @no_keychain = argv.flag?('no-keychain')
  @verbose_mode = argv.flag?('verbose')

  super

  # 获取剩余参数
  remaining_args = @argv.remainder!

  # 检查第一个参数是否是配置文件
  if remaining_args.first && File.exist?(remaining_args.first) && remaining_args.first.end_with?('.json')
    @config_file = remaining_args.shift
  end

  # 剩余的参数传递给 openai
  @gpt_args = remaining_args
end

Class Method Details

.optionsObject



39
40
41
42
43
44
# File 'lib/easyai/command/gpt.rb', line 39

def self.options
  [
    ['--no-keychain', '禁用自动密码存储'],
    ['--verbose', '显示详细信息']
  ].concat(super)
end

Instance Method Details

#runObject



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
# File 'lib/easyai/command/gpt.rb', line 69

def run
  begin
    # 首先尝试获取配置
    remote_config = nil

    if @config_file
      # 使用指定的本地配置文件
      print_status("📁 使用本地配置", File.basename(@config_file))
      remote_config = load_local_config(@config_file)
      print_success("配置加载成功") if remote_config
    else
      # 从远程下载配置,传递选项(指定工具类型为 gpt)
      print_status("🔄 获取远程配置", "默认用户")
      options = {
        no_keychain: @no_keychain,
        verbose: @verbose_mode,
        tool_type: "gpt"  # 指定使用 gpt 组的配置
      }
      remote_config = ConfigManager.download_user_config(nil, options)

      # 处理用户取消授权的情况
      if remote_config == :user_cancelled
        print_error("用户取消了授权登录")
        exit 0
      end

      print_success("配置加载成功") if remote_config
    end

    # 如果远程配置获取失败,回退到本地配置
    if remote_config.nil?
      print_warning("使用本地配置")
      remote_config = load_local_yaml_config

      # 如果本地配置也为空,提示用户先进行设置
      if remote_config.empty?
        print_error("未找到有效配置")
        puts "  请先运行: #{'easyai --setup'.yellow}"
        exit 1
      end
    end

    # 构建环境变量
    env = build_environment(remote_config)

    # 运行 OpenAI CLI
    print_status("🚀 启动 OpenAI", "openai #{@gpt_args.join(' ')}")
    exec(env, 'openai', *@gpt_args)

  rescue Interrupt
    puts "\n已取消"
    exit 0
  rescue => e
    print_error("运行失败: #{e.message}")
    puts e.backtrace if @verbose_mode
    exit 1
  end
end

#validate!Object



64
65
66
67
# File 'lib/easyai/command/gpt.rb', line 64

def validate!
  super
  help! '未找到 OpenAI CLI。请安装:pip install openai' unless gpt_available?
end