install method
Future<AgentSdkServerInstallResult>
install({
- required LlmNodeResolution node,
- void onProgress(
- String line
Installs the server on the Node node resolved. Never throws; every
failure is an outcome with a sentence for the user, and every one leaves
the previous install alone.
onProgress receives a line per step and every line npm writes, so the
notice can show that a download of a quarter of a gigabyte is moving.
Implementation
Future<AgentSdkServerInstallResult> install({
required LlmNodeResolution node,
void Function(String line)? onProgress,
}) async {
void say(String line) => onProgress?.call(line);
final sep = Platform.pathSeparator;
final missing = agentSdkServerSourceFiles
.where((name) => !File('$sourceDir$sep$name').existsSync())
.toList();
if (missing.isNotEmpty) {
return AgentSdkServerInstallResult.failed(
'this copy of the app carries no AI server to install: '
'${missing.join(", ")} missing in "$sourceDir" (a release is '
'produced with tool/package_app.dart)',
);
}
say('Checking Node.js (${node.executable ?? 'none found'})…');
final (runtime, nodeProblem) = await _probeNode(node);
if (runtime == null) {
return AgentSdkServerInstallResult.failed(nodeProblem!);
}
final lock = agentSdkInLockfile(
File('$sourceDir$sep$agentSdkServerLockfileName').readAsStringSync(),
);
if (lock == null) {
return AgentSdkServerInstallResult.failed(
'the shipped package-lock.json does not lock the Agent SDK, so there '
'is nothing to install',
);
}
final choice = chooseAgentSdkPlatform(
nodePlatform: runtime.platform,
nodeArch: runtime.arch,
shipped: lock.platforms,
sdkVersion: lock.version,
);
if (!choice.ok) {
return AgentSdkServerInstallResult.failed(choice.refusal!);
}
say(
'Node.js ${runtime.version} (${choice.platform}); installing Anthropic\'s '
'Claude Agent SDK ${lock.version ?? ''} into "$installDir"…',
);
final staging = Directory('$installDir.installing');
try {
if (staging.existsSync()) staging.deleteSync(recursive: true);
staging.createSync(recursive: true);
for (final name in agentSdkServerSourceFiles) {
File('$sourceDir$sep$name').copySync('${staging.path}$sep$name');
}
} on FileSystemException catch (e) {
return AgentSdkServerInstallResult.failed(
'could not prepare "${staging.path}": ${e.message}',
);
}
void discard() {
try {
if (staging.existsSync()) staging.deleteSync(recursive: true);
} on FileSystemException {
// Best effort; the next install replaces it anyway.
}
}
final (npm, npmLeading) = _npm(runtime);
final nodeDir = File(runtime.execPath).parent.path;
final inherited = Platform.environment['PATH'] ?? '';
final pathSeparator = Platform.isWindows ? ';' : ':';
final tail = <String>[];
say('npm ci --omit=dev');
final int code;
try {
code = await _run(
npm,
[...npmLeading, 'ci', '--omit=dev', '--no-audit', '--no-fund'],
workingDirectory: staging.path,
// The probed Node first on PATH: npm and the SDK's install scripts
// spawn `node` by name, and it has to be this one.
environment: {
'PATH': inherited.isEmpty
? nodeDir
: '$nodeDir$pathSeparator$inherited',
},
onOutput: (line) {
tail.add(line);
if (tail.length > 5) tail.removeAt(0);
say(line);
},
);
} on ProcessException catch (e) {
discard();
return AgentSdkServerInstallResult.failed(
'could not run npm ("$npm"): ${e.message}. npm ships with Node.js; '
'reinstall Node.js $requiredNodeMajorVersion or newer',
);
}
if (code != 0) {
discard();
return AgentSdkServerInstallResult.failed(
'npm ci failed with exit code $code'
'${tail.isEmpty ? '' : ': ${tail.join(' | ')}'}',
);
}
say('Checking the installed server…');
final verification = verifyAgentSdkPayloadFor(
payloadDir: staging.path,
expectedPlatform: choice.platform,
);
if (!verification.ok) {
discard();
return AgentSdkServerInstallResult.failed(
'the installed server failed its check:\n$verification',
verification: verification,
);
}
try {
File('${staging.path}$sep$agentSdkInstallMarkerName').writeAsStringSync(
const JsonEncoder.withIndent(' ').convert({
'sdkVersion': lock.version,
'platform': choice.platform,
'node': runtime.version,
'installedAt': DateTime.now().toUtc().toIso8601String(),
}),
);
final target = Directory(installDir);
if (target.existsSync()) {
final previous = Directory('$installDir.previous');
if (previous.existsSync()) previous.deleteSync(recursive: true);
target.renameSync(previous.path);
staging.renameSync(installDir);
previous.deleteSync(recursive: true);
} else {
target.parent.createSync(recursive: true);
staging.renameSync(installDir);
}
} on FileSystemException catch (e) {
discard();
return AgentSdkServerInstallResult.failed(
'could not move the installed server into "$installDir": '
'${e.message}',
);
}
say('Installed (${choice.platform}).');
return AgentSdkServerInstallResult._(
ok: true,
failure: null,
platform: choice.platform,
sdkVersion: lock.version,
verification: verification,
);
}