addAiCommand method

void addAiCommand({
  1. String? home,
  2. bool color = false,
})

Implementation

void addAiCommand({String? home, bool color = false}) {
  final config = AiConfigIo.load(home: home);
  if (config == null) {
    register(_AiSetupCommand());
    return;
  }
  final provider = providerFor(config, http.Client());
  // Enable the knowledge-base risk detector on top of the structural defaults
  // so a command's knowledge-base risk also drives the verdict: a KB-`critical`
  // tool (e.g. `mkfs`, `dd of=/dev/...`) is hard-blocked in every `:ai` mode,
  // while `highRisk`/`mediumRisk` tools carry an accurate risk tag. Structural
  // detectors still provide wrapper look-through (`sudo mkfs …`) and pipe
  // detection (`curl … | sh`).
  final shield = CommandShield(
    analyzer: Analyzer(
      securityAnalyzer: SecurityAnalyzer(
        detectors: [
          ...SecurityAnalyzer.defaultDetectors,
          KnowledgeRiskDetector(),
        ],
      ),
    ),
  );
  register(
    AiCommand(
      config: config,
      provider: provider,
      shield: shield,
      style: color ? const AnsiAgentStyle() : const AgentStyle(),
      onModeChanged: (mode) => AiConfigIo.writeMode(mode, home: home),
      // Persist the language; an empty string clears it (load reads '' as
      // unset) since the writer skips null values.
      onLanguageChanged: (language) =>
          AiConfigIo.write(language: language ?? '', home: home),
    ),
  );
}