AiClient constructor
AiClient({
- required AiProvider provider,
- String apiKey = '',
- String? model,
- String? baseUrl,
- HttpClient? httpClient,
- Duration? timeout,
Implementation
AiClient({
required this.provider,
String apiKey = '',
String? model,
String? baseUrl,
HttpClient? httpClient,
Duration? timeout,
}) : _apiKey = apiKey.trim(),
model = model ?? provider.defaultModel,
_localUri = _localEndpoint(provider, baseUrl),
timeout =
timeout ??
Duration(seconds: provider == AiProvider.local ? 180 : 60),
_http = httpClient ?? HttpClient(),
_ownsClient = httpClient == null {
if ((_apiKey.isEmpty && provider != AiProvider.local) ||
(_apiKey.isNotEmpty && !RegExp(r'^[\x21-\x7e]+$').hasMatch(_apiKey))) {
throw ArgumentError(
'Set a valid ${provider.environmentKey} environment variable.',
);
}
if (provider == AiProvider.local && this.model.isEmpty) {
throw ArgumentError(
'Local AI requires --model with an installed model ID.',
);
}
final modelPattern = provider == AiProvider.local
? r'^[a-zA-Z0-9][a-zA-Z0-9._:/-]{0,255}$'
: r'^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,127}$';
if (!RegExp(modelPattern).hasMatch(this.model)) {
throw ArgumentError('Invalid AI model ID.');
}
if (this.timeout <= Duration.zero) {
throw ArgumentError('AI timeout must be positive.');
}
}