Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

1.9 KB
Raw Blame
import 'usage_window.dart';

/// One account's usage, fetched from whoever knows it.
///
/// An interface rather than two hard-wired calls, because the two vendors agree
/// on nothing but the question: different hosts, different auth, different
/// field names, different window sets. It is also the seam a per-machine source
/// would arrive through — the CLI could answer this for a remote machine one
/// day without a single widget changing.
abstract class UsageSource {
  UsageProvider get provider;

  /// The current reading, or the state that stands in for one.
  ///
  /// Never throws: every failure is a [ProviderUsage] carrying its own reason,
  /// because the rail has to draw *something* and a thrown exception would make
  /// the caller invent the sentence instead of the source that knows it.
  Future<ProviderUsage> read();
}

/// Turns a finished request into the state the rail should draw.
///
/// Shared because the two vendors fail in the same four ways even though they
/// succeed in different shapes.
ProviderUsage? usageFailureFor(UsageProvider provider, int? statusCode) {
  if (statusCode == null) {
    return ProviderUsage(
      provider: provider,
      status: UsageStatus.failed,
      message: 'Could not reach ${provider.label}',
    );
  }
  if (statusCode == 401 || statusCode == 403) {
    return ProviderUsage(
      provider: provider,
      status: UsageStatus.signedOut,
      message: 'Sign in to ${provider.label} to see usage',
    );
  }
  if (statusCode >= 400) {
    return ProviderUsage(
      provider: provider,
      status: UsageStatus.failed,
      message: '${provider.label} answered $statusCode',
    );
  }
  return null;
}

/// The state a provider is in when its CLI has never signed in here.
ProviderUsage signedOut(UsageProvider provider) => ProviderUsage(
  provider: provider,
  status: UsageStatus.signedOut,
  message: 'Sign in to ${provider.label} to see usage',
);