initialize method

Future<void> initialize()

Probes and connects the first reachable candidate.

Safe to call when nothing is reachable: that is the ordinary offline case, and it leaves the app fully usable with every AI affordance gated off. It does not throw.

Implementation

Future<void> initialize() async {
  // A root that configured no transports has not *failed* to reach one — it
  // ships without AI, which is a build decision and not an episode to report.
  // Saying "no AI server could be reached" here would be untrue, and it would
  // put a notice on every launch of a build that never intended to have one.
  // The gate is shut either way, which is all the gate has to say.
  if (candidates.isEmpty) {
    _active = null;
    // Said even though no notice is emitted: a build that ships without AI
    // owes the user an explanation for the greyed-out control, and "nothing
    // was configured" is a different fact from "nothing answered".
    _unavailableReason = llmNotConfiguredReason;
    _unavailableKind = AgentSdkUnavailableKind.notConfigured;
    _installable = null;
    return;
  }

  final failures = <String, String>{};
  final kinds = <AgentSdkUnavailableKind>{};
  AgentSdkInstallableTransport? installable;

  for (final candidate in candidates) {
    if (!await candidate.probe()) {
      // The transport's own reason when it has one. "Not reachable" is the
      // honest answer only when nothing more specific was recorded; a
      // missing Node runtime and a stopped CLI server are both "not
      // reachable" and neither is fixable from that sentence.
      failures[candidate.id] = candidate.unavailableReason ?? 'not reachable';
      final kind = candidate is AgentSdkInstallableTransport
          ? candidate.unavailableKind
          : null;
      kinds.add(kind ?? AgentSdkUnavailableKind.unreachable);
      if (kind == AgentSdkUnavailableKind.notInstalled) {
        installable ??= candidate as AgentSdkInstallableTransport;
      }
      continue;
    }
    try {
      await candidate.connect();
      // The connection proved the candidate; it is not the one runs use.
      // Every run is served from a session, which holds a
      // connection of its own, so this one is released rather than left
      // open — for the standalone transport that is a Node child nothing
      // would ever send a query to.
      await candidate.close();
      _active = candidate;
      _reportedUnavailable = false;
      _unavailableReason = null;
      _unavailableKind = null;
      _installable = null;
      _announce(candidate);
      return;
    } on LlmTransportException catch (e) {
      failures[candidate.id] = e.message;
      kinds.add(AgentSdkUnavailableKind.unreachable);
    } on Object catch (e) {
      failures[candidate.id] = e.toString();
      kinds.add(AgentSdkUnavailableKind.unreachable);
    }
  }

  _active = null;
  _installable = installable;
  _unavailableKind =
      [
        AgentSdkUnavailableKind.notInstalled,
        AgentSdkUnavailableKind.noRuntime,
        AgentSdkUnavailableKind.unreachable,
      ].firstWhere(
        kinds.contains,
        orElse: () => AgentSdkUnavailableKind.unreachable,
      );
  _report(failures);
}