Class: EasyAI::Command::AIToolBase

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

Overview

AI 工具命令的公共基类:claude / gemini / codex 共享的运行流程

子类必须实现:

- tool_name      返回 config.json 顶层键,如 'claude'
- exec_command   返回子进程命令名,如 'claude'
- install_hint   返回未安装时的中文安装提示

Direct Known Subclasses

Claude, Codex, Gemini

Constant Summary collapse

PROTECTED_ENV_KEYS =

系统关键变量保护清单:永远不允许被 config 中的 env / proxy 覆盖

%w[PATH HOME USER SHELL].freeze

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) ⇒ AIToolBase

Returns a new instance of AIToolBase.



31
32
33
34
35
36
37
38
39
40
# File 'lib/easyai/command/ai_tool_base.rb', line 31

def initialize(argv)
  @platform = argv.option('platform')
  @verbose = argv.flag?('verbose')

  super

  remaining = @argv.remainder!
  @local_config_file = extract_local_config_file!(remaining)
  @passthrough_args = remaining
end

Class Method Details

.optionsObject



24
25
26
27
28
29
# File 'lib/easyai/command/ai_tool_base.rb', line 24

def self.options
  [
    ['--platform=PLATFORM', '指定平台(如 claude_official / kimi / deepseek)'],
    ['--verbose', '显示详细信息']
  ].concat(super)
end

Instance Method Details

#default_envObject

子类可选重写:返回该工具固定要注入的环境变量。优先级低于 cfg.env(用户在 config.json 中显式声明的同名 key 会覆盖默认值)。



94
95
96
# File 'lib/easyai/command/ai_tool_base.rb', line 94

def default_env
  {}
end

#exec_commandObject

Raises:

  • (NotImplementedError)


84
85
86
# File 'lib/easyai/command/ai_tool_base.rb', line 84

def exec_command
  raise NotImplementedError, "#{self.class} 必须实现 #exec_command"
end

#install_hintObject

Raises:

  • (NotImplementedError)


88
89
90
# File 'lib/easyai/command/ai_tool_base.rb', line 88

def install_hint
  raise NotImplementedError, "#{self.class} 必须实现 #install_hint"
end

#pre_execObject

子类可选重写:在 exec 启动子进程之前执行的钩子(如:写入子进程依赖的状态文件)。默认 no-op;不应阻塞主流程,异常应降级为 warning。



100
101
102
# File 'lib/easyai/command/ai_tool_base.rb', line 100

def pre_exec
  # no-op
end

#runObject



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

def run
  cfg = resolve_config
  print_platform_env(cfg)
  env = build_environment(cfg)
  export_environment_variables(env)
  pre_exec
  exec(env, exec_command, *@passthrough_args)
rescue Config::LocalConfig::NotFoundError => e
  print_error(e.message)
  puts "  请运行: #{'easyai setup'.yellow}"
  exit 1
rescue Config::LocalConfig::PlatformNotFoundError => e
  print_error(e.message)
  available = Config::LocalConfig.available_platforms(tool_name)
  puts "  当前可用平台:#{available.join(', ')}" unless available.empty?
  exit 1
rescue Config::LocalConfig::ToolNotConfiguredError => e
  print_error(e.message)
  puts "  请运行: #{"easyai setup --tool=#{tool_name}".yellow}"
  exit 1
rescue Config::LocalConfig::ParseError, Config::LocalConfig::IncompatibleVersionError => e
  print_error(e.message)
  puts "  可运行: #{'easyai setup --reset'.yellow} 重新生成配置"
  exit 1
rescue Interrupt
  puts
  print_error('用户取消操作')
  exit 130
end

#tool_nameObject

子类必须实现的抽象方法

Raises:

  • (NotImplementedError)


80
81
82
# File 'lib/easyai/command/ai_tool_base.rb', line 80

def tool_name
  raise NotImplementedError, "#{self.class} 必须实现 #tool_name"
end

#validate!Object



42
43
44
45
46
47
# File 'lib/easyai/command/ai_tool_base.rb', line 42

def validate!
  super
  return if Base::SystemInfo.which_command(exec_command)

  help! "未找到 #{exec_command} CLI。#{install_hint}"
end