initialize method
Performs any setup required before tool calls, optionally using ctx.
Implementation
@override
Future<void> initialize({Context? ctx}) async {
isFeatureEnabled(FeatureName.daytonaEnvironment);
if (isInitialized && _sandboxId != null) {
return;
}
final http.Client client = _httpClient ?? http.Client();
try {
final Map<String, dynamic> payload = <String, dynamic>{
if (image != null) 'image': image,
if (envVars != null) 'env': envVars,
};
final Uri uri = Uri.parse('$_effectiveApiUrl/sandbox');
final http.Response response = await client.post(
uri,
headers: _headers,
body: jsonEncode(payload),
);
if (response.statusCode < 200 || response.statusCode >= 300) {
throw HttpException(
'Failed to create Daytona sandbox (${response.statusCode}): '
'${response.body}',
uri: uri,
);
}
final dynamic data = jsonDecode(response.body);
if (data is! Map || data['id'] == null || '${data['id']}'.isEmpty) {
throw StateError(
'Daytona sandbox creation response is missing a valid id.',
);
}
_sandboxId = '${data['id']}';
isInitialized = true;
} finally {
if (_httpClient == null) {
client.close();
}
}
}