86 lines
2.4 KiB
Dart
86 lines
2.4 KiB
Dart
import 'dart:convert';
|
|
import 'package:flutter/services.dart';
|
|
import 'models.dart';
|
|
|
|
class AiConfigFile {
|
|
const AiConfigFile({
|
|
required this.provider,
|
|
required this.endpoint,
|
|
required this.model,
|
|
this.reasoningEffort = 'low',
|
|
this.apiKey,
|
|
this.description,
|
|
});
|
|
|
|
final AiProviderType provider;
|
|
final String endpoint;
|
|
final String model;
|
|
final String reasoningEffort;
|
|
final String? apiKey;
|
|
final String? description;
|
|
|
|
static const String defaultAssetPath = 'assets/config/ai_config.json';
|
|
|
|
factory AiConfigFile.fromJson(Map<String, dynamic> json) {
|
|
final providerStr = json['provider'] as String? ?? 'compatible';
|
|
final provider = AiProviderType.values.firstWhere(
|
|
(p) => p.name.toLowerCase() == providerStr.toLowerCase(),
|
|
orElse: () => AiProviderType.compatible,
|
|
);
|
|
final effort =
|
|
(json['reasoningEffort'] as String? ??
|
|
json['reasoning_effort'] as String? ??
|
|
'low')
|
|
.trim();
|
|
return AiConfigFile(
|
|
provider: provider,
|
|
endpoint: (json['endpoint'] as String? ?? '').trim(),
|
|
model: (json['model'] as String? ?? '').trim(),
|
|
reasoningEffort: effort.isEmpty ? 'low' : effort,
|
|
apiKey: json['apiKey'] as String?,
|
|
description: json['description'] as String?,
|
|
);
|
|
}
|
|
|
|
factory AiConfigFile.parse(String rawJson) {
|
|
final data = jsonDecode(rawJson) as Map<String, dynamic>;
|
|
return AiConfigFile.fromJson(data);
|
|
}
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
'provider': provider.name,
|
|
'endpoint': endpoint,
|
|
'model': model,
|
|
'reasoningEffort': reasoningEffort,
|
|
if (apiKey != null) 'apiKey': apiKey,
|
|
if (description != null) 'description': description,
|
|
};
|
|
|
|
static Future<AiConfigFile?> loadFromAsset([
|
|
String path = defaultAssetPath,
|
|
]) async {
|
|
try {
|
|
final content = await rootBundle.loadString(path);
|
|
return AiConfigFile.parse(content);
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
AiConfigFile copyWith({
|
|
AiProviderType? provider,
|
|
String? endpoint,
|
|
String? model,
|
|
String? reasoningEffort,
|
|
String? apiKey,
|
|
String? description,
|
|
}) => AiConfigFile(
|
|
provider: provider ?? this.provider,
|
|
endpoint: endpoint ?? this.endpoint,
|
|
model: model ?? this.model,
|
|
reasoningEffort: reasoningEffort ?? this.reasoningEffort,
|
|
apiKey: apiKey ?? this.apiKey,
|
|
description: description ?? this.description,
|
|
);
|
|
}
|