chooseAgentSdkPlatform function

AgentSdkPlatformChoice chooseAgentSdkPlatform({
  1. required String nodePlatform,
  2. required String nodeArch,
  3. required Set<String> shipped,
  4. String? sdkVersion,
})

Decides which Claude Code CLI package an install for a Node reporting nodePlatform / nodeArch (process.platform, process.arch) gets, from the set of platform suffixes the locked SDK version ships (shipped).

Never "nothing". npm installs the platform package whose os/cpu match the installing Node and silently skips every other optional dependency — so a Node the SDK ships no CLI for produces a tree that installs cleanly and cannot answer a single prompt. This is the check that turns that into a sentence before anything is downloaded.

Windows on ARM refuses rather than falling back to the x64 CLI. The fallback looks free — Windows on ARM runs x64 binaries under emulation — and is not: the SDK resolves the CLI at run time by the running Node's process.arch, so an x64 CLI installed beside an arm64 Node is never found. The fallback that does work is an x64 Node, for which npm installs, and the SDK resolves, the x64 CLI; that is the remedy the refusal names. (SDK 0.3.286 ships win32-arm64, so today this branch is reached only by a lockfile that pins an SDK without it.)

Implementation

AgentSdkPlatformChoice chooseAgentSdkPlatform({
  required String nodePlatform,
  required String nodeArch,
  required Set<String> shipped,
  String? sdkVersion,
}) {
  final sdk = sdkVersion == null
      ? "Anthropic's Claude Agent SDK"
      : "Anthropic's Claude Agent SDK $sdkVersion";
  if (!const {'darwin', 'linux', 'win32'}.contains(nodePlatform) ||
      !const {'arm64', 'x64'}.contains(nodeArch)) {
    return AgentSdkPlatformChoice._(
      null,
      '$sdk ships no Claude Code CLI for $nodePlatform-$nodeArch, so the AI '
      'server cannot run with this Node.js. Use a 64-bit Node.js on macOS, '
      'Linux or Windows',
    );
  }
  final suffix = '$nodePlatform-$nodeArch';
  final accepted = {suffix, if (nodePlatform == 'linux') '$suffix-musl'};
  if (shipped.any(accepted.contains)) {
    return AgentSdkPlatformChoice._(suffix, null);
  }
  if (suffix == 'win32-arm64' && shipped.contains('win32-x64')) {
    return AgentSdkPlatformChoice._(
      null,
      '$sdk ships no Claude Code CLI for Windows on ARM (win32-arm64). '
      'Install the x64 build of Node.js 20 or newer and point the app at it: '
      'Windows on ARM runs it under emulation, npm then installs the x64 CLI, '
      'and the SDK finds it — an x64 CLI beside an arm64 Node is never found',
    );
  }
  return AgentSdkPlatformChoice._(
    null,
    '$sdk ships no Claude Code CLI for $suffix (it ships '
    '${(shipped.toList()..sort()).join(', ')}), so the AI server cannot run '
    'with this Node.js',
  );
}