Class: EasyAI::Command::Claude

Inherits:
EasyAI::Command show all
Defined in:
lib/easyai/command/claude.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) ⇒ Claude

Returns a new instance of Claude.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/easyai/command/claude.rb', line 48

def initialize(argv)
  @platform_flag = argv.flag?('platform')
  @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
  
  # 剩余的参数传递给 claude
  @claude_args = remaining_args
end

Class Method Details

.optionsObject



40
41
42
43
44
45
46
# File 'lib/easyai/command/claude.rb', line 40

def self.options
  [
    ['--platform', '选择认证平台(交互式选择)'],
    ['--no-keychain', '禁用自动密码存储'],
    ['--verbose', '显示详细信息']
  ].concat(super)
end

Instance Method Details

#runObject



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
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/easyai/command/claude.rb', line 72

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
    # 从远程下载配置,传递选项(指定工具类型为 claude)
    print_status("🔄 获取远程配置", "默认用户")
    options = {
      no_keychain: @no_keychain,
      verbose: @verbose_mode,
      tool_type: "claude",  # 指定使用 claude 组的配置
      platform_flag: @platform_flag   # 传递 platform 标志
    }
    remote_config = ConfigManager.download_user_config(nil, options)

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

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

  # 检查是否使用 claude_auth,如果是则进行环境检查
  if remote_config && remote_config['_config_path']&.include?('claude_auth')
    unless check_claude_auth_environment
      exit 1
    end
  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

  # 使用 Auth::AuthClaude 模块配置认证
  print_status("🔐 配置认证", "Claude OAuth")
  unless Auth::AuthClaude.configure(remote_config, nil)
    print_error("配置认证失败")
    exit 1
  end
  print_success("认证配置完成")

  # 构建环境变量
  env = build_environment(remote_config)
  
  # 导出环境变量到当前进程(不包括 CLAUDE_CODE_OAUTH_TOKEN)
  export_environment_variables(env)
  
  # 打印分隔线
  puts "" * 60
  puts "🚀 #{'Claude CLI 已启动'.green}"
  puts "" * 60
  puts
  
  # 启动 Claude
  exec(env, 'claude', *@claude_args)
  
  rescue => e
    print_error("Claude 命令执行失败: #{e.message}")
    puts "  请检查配置文件和网络连接"
    puts "  错误详情: #{e.class.name}" if @verbose_mode
    exit 1
  end
end

#validate!Object



67
68
69
70
# File 'lib/easyai/command/claude.rb', line 67

def validate!
  super
  help! '未找到 Claude CLI。请安装:npm install -g @anthropic-ai/claude-code' unless claude_available?
end