registerAiCommand function

Future<void> registerAiCommand({
  1. required LocalCommandRegistry registry,
  2. required SettingsStore settings,
  3. required OmnyShellService service,
  4. required void openSettings(),
})

Builds and registers the :ai command on registry from the user's stored AI settings and the Hub's advertised default.

Provider HTTPS calls go through the Hub (a browser cannot reach AI APIs directly, and the Hub holds the key) via a HubHttpClient:

  • Custom key — when the user opted out of the Hub default and supplied an API key, the request is forwarded verbatim (HttpProxyCredentialMode.none).
  • Hub default — otherwise the Hub's configured provider/model is used and the Hub injects its key (HttpProxyCredentialMode.hubDefault).

When neither is available (no custom key and the Hub has no default), a stub :ai is registered that points the user at Settings. All failures fall back to the stub so session open is never blocked.

Implementation

Future<void> registerAiCommand({
  required LocalCommandRegistry registry,
  required SettingsStore settings,
  required OmnyShellService service,
  required void Function() openSettings,
}) async {
  final wiring = await resolveAiWiring(settings: settings, service: service);
  if (wiring == null) {
    registry.register(AiSetupCommand(openSettings));
    return;
  }
  registry.addAiCommand(
    config: wiring.config,
    httpClient: wiring.httpClient,
    style: const AnsiAgentStyle(),
    onModeChanged: (mode) => settings.aiMode = mode.wireName,
    onLanguageChanged: (language) => settings.aiLanguage = language,
  );
}